treemd 0.5.12

A markdown navigator with tree-based structural navigation and syntax highlighting
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
//! Default keybindings for treemd
//!
//! This module defines the default keybindings that are used when no
//! user configuration is provided. Uses keybinds-rs key string syntax.

use super::{Action, KeybindingMode, Keybindings};

/// Create the default keybindings configuration
pub fn default_keybindings() -> Keybindings {
    let mut kb = Keybindings::new();

    // Normal mode
    add_normal_mode(&mut kb);

    // Help mode
    add_help_mode(&mut kb);

    // Theme picker mode
    add_theme_picker_mode(&mut kb);

    // Interactive mode
    add_interactive_mode(&mut kb);

    // Interactive table mode
    add_interactive_table_mode(&mut kb);

    // Link follow mode
    add_link_follow_mode(&mut kb);

    // Link search mode
    add_link_search_mode(&mut kb);

    // File picker mode
    add_file_picker_mode(&mut kb);

    // File search mode
    add_file_search_mode(&mut kb);

    // Search mode
    add_search_mode(&mut kb);

    // Doc search mode
    add_doc_search_mode(&mut kb);

    // Command palette mode
    add_command_palette_mode(&mut kb);

    // Confirm dialog mode
    add_confirm_dialog_mode(&mut kb);

    // Cell edit mode
    add_cell_edit_mode(&mut kb);

    kb
}

/// Bind a key, panicking on invalid key syntax (only used for built-in defaults)
fn bind(kb: &mut Keybindings, mode: KeybindingMode, key: &str, action: Action) {
    kb.bind(mode, key, action)
        .unwrap_or_else(|e| panic!("Invalid default keybinding '{}': {}", key, e));
}

fn add_normal_mode(kb: &mut Keybindings) {
    use Action::*;
    use KeybindingMode::Normal;

    // Navigation
    bind(kb, Normal, "j", Next);
    bind(kb, Normal, "Down", Next);
    bind(kb, Normal, "k", Previous);
    bind(kb, Normal, "Up", Previous);
    bind(kb, Normal, "g", First);
    bind(kb, Normal, "G", Last);
    bind(kb, Normal, "d", PageDown);
    bind(kb, Normal, "PageDown", PageDown);
    bind(kb, Normal, "u", PageUp);
    bind(kb, Normal, "PageUp", PageUp);
    bind(kb, Normal, "Home", First);
    bind(kb, Normal, "End", Last);
    bind(kb, Normal, "p", JumpToParent);

    // Outline
    bind(kb, Normal, "Enter", ToggleExpand);
    bind(kb, Normal, "Space", ToggleExpand);
    bind(kb, Normal, "Tab", ToggleFocus);
    bind(kb, Normal, "Shift+Tab", ToggleFocusBack);
    bind(kb, Normal, "h", Collapse);
    bind(kb, Normal, "Left", Collapse);
    bind(kb, Normal, "l", Expand);
    bind(kb, Normal, "Right", Expand);
    bind(kb, Normal, "w", ToggleOutline);
    bind(kb, Normal, "[", OutlineWidthDecrease);
    bind(kb, Normal, "]", OutlineWidthIncrease);
    bind(kb, Normal, "T", ToggleTodoFilter);
    bind(kb, Normal, "#", ToggleHeadingMarkers);

    // Bookmarks
    bind(kb, Normal, "m", SetBookmark);
    bind(kb, Normal, "'", JumpToBookmark);

    // Mode transitions
    bind(kb, Normal, "i", EnterInteractiveMode);
    bind(kb, Normal, "f", EnterLinkFollowMode);
    bind(kb, Normal, "s", EnterSearchMode);
    bind(kb, Normal, "/", EnterDocSearch);
    bind(kb, Normal, ":", OpenCommandPalette);

    // View
    bind(kb, Normal, "r", ToggleRawSource);
    bind(kb, Normal, "M", ToggleMouseCapture);
    bind(kb, Normal, "t", ToggleThemePicker);
    bind(kb, Normal, "?", ToggleHelp);

    // Clipboard
    bind(kb, Normal, "y", CopyContent);
    bind(kb, Normal, "Y", CopyAnchor);

    // File operations
    bind(kb, Normal, "b", GoBack);
    bind(kb, Normal, "Backspace", GoBack);
    bind(kb, Normal, "F", GoForward);
    bind(kb, Normal, "e", OpenInEditor);
    bind(kb, Normal, "Ctrl+o", OpenFilePicker);
    bind(kb, Normal, "o", OpenFilePicker);

    // Application
    bind(kb, Normal, "q", Quit);
    bind(kb, Normal, "Escape", Quit);
    bind(kb, Normal, "Ctrl+l", Redraw);

    // Jump to heading by number
    bind(kb, Normal, "1", JumpToHeading1);
    bind(kb, Normal, "2", JumpToHeading2);
    bind(kb, Normal, "3", JumpToHeading3);
    bind(kb, Normal, "4", JumpToHeading4);
    bind(kb, Normal, "5", JumpToHeading5);
    bind(kb, Normal, "6", JumpToHeading6);
    bind(kb, Normal, "7", JumpToHeading7);
    bind(kb, Normal, "8", JumpToHeading8);
    bind(kb, Normal, "9", JumpToHeading9);

    // Search match navigation (when matches exist)
    bind(kb, Normal, "n", NextMatch);
    bind(kb, Normal, "N", PrevMatch);
}

fn add_help_mode(kb: &mut Keybindings) {
    use Action::*;
    use KeybindingMode::Help;

    // Navigation
    bind(kb, Help, "j", HelpScrollDown);
    bind(kb, Help, "Down", HelpScrollDown);
    bind(kb, Help, "k", HelpScrollUp);
    bind(kb, Help, "Up", HelpScrollUp);
    bind(kb, Help, "d", PageDown);
    bind(kb, Help, "PageDown", PageDown);
    bind(kb, Help, "u", PageUp);
    bind(kb, Help, "PageUp", PageUp);
    bind(kb, Help, "g", First);
    bind(kb, Help, "G", Last);
    bind(kb, Help, "Home", First);
    bind(kb, Help, "End", Last);

    // Close help
    bind(kb, Help, "?", ToggleHelp);
    bind(kb, Help, "Escape", ToggleHelp);

    // Clipboard (available everywhere)
    bind(kb, Help, "y", CopyContent);
    bind(kb, Help, "Y", CopyAnchor);

    // Quit
    bind(kb, Help, "q", Quit);
}

fn add_theme_picker_mode(kb: &mut Keybindings) {
    use Action::*;
    use KeybindingMode::ThemePicker;

    // Navigation
    bind(kb, ThemePicker, "j", ThemePickerNext);
    bind(kb, ThemePicker, "Down", ThemePickerNext);
    bind(kb, ThemePicker, "k", ThemePickerPrevious);
    bind(kb, ThemePicker, "Up", ThemePickerPrevious);

    // Actions
    bind(kb, ThemePicker, "Enter", ApplyTheme);
    bind(kb, ThemePicker, "Escape", ToggleThemePicker);

    // Clipboard (available everywhere)
    bind(kb, ThemePicker, "y", CopyContent);
    bind(kb, ThemePicker, "Y", CopyAnchor);

    // Quit
    bind(kb, ThemePicker, "q", Quit);
}

fn add_interactive_mode(kb: &mut Keybindings) {
    use Action::*;
    use KeybindingMode::Interactive;

    // Exit
    bind(kb, Interactive, "Escape", ExitInteractiveMode);
    bind(kb, Interactive, "i", ExitInteractiveMode);

    // Navigation
    bind(kb, Interactive, "j", InteractiveNext);
    bind(kb, Interactive, "Down", InteractiveNext);
    bind(kb, Interactive, "k", InteractivePrevious);
    bind(kb, Interactive, "Up", InteractivePrevious);

    // Link navigation within element
    bind(kb, Interactive, "Tab", InteractiveNextLink);
    bind(kb, Interactive, "Shift+Tab", InteractivePreviousLink);

    // Activate element
    bind(kb, Interactive, "Enter", InteractiveActivate);
    bind(kb, Interactive, "Space", InteractiveActivate);

    // Page navigation
    bind(kb, Interactive, "d", PageDown);
    bind(kb, Interactive, "PageDown", PageDown);
    bind(kb, Interactive, "u", PageUp);
    bind(kb, Interactive, "PageUp", PageUp);
    bind(kb, Interactive, "g", First);
    bind(kb, Interactive, "G", Last);
    bind(kb, Interactive, "Home", First);
    bind(kb, Interactive, "End", Last);

    // Document search from interactive mode
    bind(kb, Interactive, "/", EnterDocSearch);

    // Search match navigation
    bind(kb, Interactive, "n", NextMatch);
    bind(kb, Interactive, "N", PrevMatch);

    // Clipboard
    bind(kb, Interactive, "y", CopyContent);

    // Undo last edit
    bind(kb, Interactive, "Ctrl+z", UndoEdit);

    // Open in editor
    bind(kb, Interactive, "e", OpenInEditor);

    // Quit
    bind(kb, Interactive, "q", Quit);
}

fn add_interactive_table_mode(kb: &mut Keybindings) {
    use Action::*;
    use KeybindingMode::InteractiveTable;

    // Exit table mode
    bind(kb, InteractiveTable, "Escape", ExitMode);

    // Table navigation
    bind(kb, InteractiveTable, "h", InteractiveLeft);
    bind(kb, InteractiveTable, "Left", InteractiveLeft);
    bind(kb, InteractiveTable, "l", InteractiveRight);
    bind(kb, InteractiveTable, "Right", InteractiveRight);
    bind(kb, InteractiveTable, "j", InteractiveNext);
    bind(kb, InteractiveTable, "Down", InteractiveNext);
    bind(kb, InteractiveTable, "k", InteractivePrevious);
    bind(kb, InteractiveTable, "Up", InteractivePrevious);

    // Clipboard
    bind(kb, InteractiveTable, "y", CopyTableCell);
    bind(kb, InteractiveTable, "Y", CopyTableRow);
    bind(kb, InteractiveTable, "r", CopyTableMarkdown);

    // Activate (follow link or edit cell)
    bind(kb, InteractiveTable, "Enter", InteractiveActivate);

    // Undo last edit
    bind(kb, InteractiveTable, "Ctrl+z", UndoEdit);

    // Quit
    bind(kb, InteractiveTable, "q", Quit);
}

fn add_link_follow_mode(kb: &mut Keybindings) {
    use Action::*;
    use KeybindingMode::LinkFollow;

    // Exit
    bind(kb, LinkFollow, "Escape", ExitMode);

    // Navigation
    bind(kb, LinkFollow, "j", NextLink);
    bind(kb, LinkFollow, "Down", NextLink);
    bind(kb, LinkFollow, "Tab", NextLink);
    bind(kb, LinkFollow, "k", PreviousLink);
    bind(kb, LinkFollow, "Up", PreviousLink);
    bind(kb, LinkFollow, "Shift+Tab", PreviousLink);

    // Actions
    bind(kb, LinkFollow, "Enter", FollowLink);
    bind(kb, LinkFollow, "/", LinkSearch);
    bind(kb, LinkFollow, "p", JumpToParent);

    // Jump to link by number
    bind(kb, LinkFollow, "1", JumpToLink1);
    bind(kb, LinkFollow, "2", JumpToLink2);
    bind(kb, LinkFollow, "3", JumpToLink3);
    bind(kb, LinkFollow, "4", JumpToLink4);
    bind(kb, LinkFollow, "5", JumpToLink5);
    bind(kb, LinkFollow, "6", JumpToLink6);
    bind(kb, LinkFollow, "7", JumpToLink7);
    bind(kb, LinkFollow, "8", JumpToLink8);
    bind(kb, LinkFollow, "9", JumpToLink9);

    // Clipboard
    bind(kb, LinkFollow, "y", CopyContent);
    bind(kb, LinkFollow, "Y", CopyAnchor);

    // Quit
    bind(kb, LinkFollow, "q", Quit);
}

fn add_link_search_mode(kb: &mut Keybindings) {
    use Action::*;
    use KeybindingMode::LinkSearch;

    // Exit search (back to link follow)
    bind(kb, LinkSearch, "Escape", ExitMode);

    // Select filtered result
    bind(kb, LinkSearch, "Enter", FollowLink);

    // Navigation while searching
    bind(kb, LinkSearch, "Down", NextLink);
    bind(kb, LinkSearch, "Up", PreviousLink);

    // Delete character
    bind(kb, LinkSearch, "Backspace", SearchBackspace);
}

fn add_search_mode(kb: &mut Keybindings) {
    use Action::*;
    use KeybindingMode::Search;

    // Exit search
    bind(kb, Search, "Escape", ExitMode);

    // Confirm search (select result)
    bind(kb, Search, "Enter", ConfirmAction);

    // Delete character
    bind(kb, Search, "Backspace", SearchBackspace);

    // Toggle to doc search
    bind(kb, Search, "Tab", ToggleSearchMode);
}

fn add_doc_search_mode(kb: &mut Keybindings) {
    use Action::*;
    use KeybindingMode::DocSearch;

    // Exit search
    bind(kb, DocSearch, "Escape", ExitMode);

    // Accept search and enter navigation mode
    bind(kb, DocSearch, "Enter", ConfirmAction);

    // Delete character
    bind(kb, DocSearch, "Backspace", SearchBackspace);

    // Navigate matches (after accepting with Enter)
    bind(kb, DocSearch, "n", NextMatch);
    bind(kb, DocSearch, "N", PrevMatch);
    bind(kb, DocSearch, "Down", NextMatch);
    bind(kb, DocSearch, "Up", PrevMatch);

    // Toggle to outline search (while typing), or cycle matches (after Enter)
    bind(kb, DocSearch, "Tab", ToggleSearchMode);
    bind(kb, DocSearch, "Shift+Tab", PrevMatch);

    // Re-enter search input with /
    bind(kb, DocSearch, "/", EnterDocSearch);
}

fn add_command_palette_mode(kb: &mut Keybindings) {
    use Action::*;
    use KeybindingMode::CommandPalette;

    // Exit
    bind(kb, CommandPalette, "Escape", ExitMode);

    // Execute selected command
    bind(kb, CommandPalette, "Enter", ConfirmAction);

    // Navigation
    bind(kb, CommandPalette, "Down", CommandPaletteNext);
    bind(kb, CommandPalette, "j", CommandPaletteNext);
    bind(kb, CommandPalette, "Up", CommandPalettePrev);
    bind(kb, CommandPalette, "k", CommandPalettePrev);

    // Autocomplete selected command
    bind(kb, CommandPalette, "Tab", CommandPaletteAutocomplete);

    // Delete character
    bind(kb, CommandPalette, "Backspace", SearchBackspace);
}

fn add_confirm_dialog_mode(kb: &mut Keybindings) {
    use Action::*;
    use KeybindingMode::ConfirmDialog;

    // Confirm
    bind(kb, ConfirmDialog, "y", ConfirmAction);
    bind(kb, ConfirmDialog, "Y", ConfirmAction);
    bind(kb, ConfirmDialog, "Enter", ConfirmAction);

    // Cancel
    bind(kb, ConfirmDialog, "n", CancelAction);
    bind(kb, ConfirmDialog, "N", CancelAction);
    bind(kb, ConfirmDialog, "Escape", CancelAction);

    // Discard and quit (for unsaved changes dialogs)
    bind(kb, ConfirmDialog, "q", DiscardAndQuit);

    // Discard and continue (for navigation with unsaved changes)
    bind(kb, ConfirmDialog, "d", DiscardAndContinue);
}

fn add_cell_edit_mode(kb: &mut Keybindings) {
    use Action::*;
    use KeybindingMode::CellEdit;

    // Cancel editing
    bind(kb, CellEdit, "Escape", CancelAction);

    // Confirm edit
    bind(kb, CellEdit, "Enter", ConfirmAction);

    // Delete character
    bind(kb, CellEdit, "Backspace", SearchBackspace);
}

fn add_file_picker_mode(kb: &mut Keybindings) {
    use Action::*;
    use KeybindingMode::FilePicker;

    // Navigation
    bind(kb, FilePicker, "j", Next);
    bind(kb, FilePicker, "Down", Next);
    bind(kb, FilePicker, "k", Previous);
    bind(kb, FilePicker, "Up", Previous);
    bind(kb, FilePicker, "Tab", Next);
    bind(kb, FilePicker, "Shift+Tab", Previous);
    bind(kb, FilePicker, "g", First);
    bind(kb, FilePicker, "G", Last);
    bind(kb, FilePicker, "Home", First);
    bind(kb, FilePicker, "End", Last);

    // Selection
    bind(kb, FilePicker, "Enter", FollowLink);

    // Parent directory
    bind(kb, FilePicker, "Backspace", ParentDirectory);

    // Search
    bind(kb, FilePicker, "/", LinkSearch);

    // Toggle hidden files and directories
    bind(kb, FilePicker, "h", ToggleHidden);

    // Exit
    bind(kb, FilePicker, "Escape", ExitMode);
    bind(kb, FilePicker, "q", Quit);
}

fn add_file_search_mode(kb: &mut Keybindings) {
    use Action::*;
    use KeybindingMode::FileSearch;

    // Navigation while searching
    bind(kb, FileSearch, "Tab", Next);
    bind(kb, FileSearch, "Down", Next);
    bind(kb, FileSearch, "Up", Previous);

    // Exit search
    bind(kb, FileSearch, "Escape", ExitMode);
    bind(kb, FileSearch, "Enter", FollowLink);

    // Delete character
    bind(kb, FileSearch, "Backspace", SearchBackspace);
}

#[cfg(test)]
mod tests {
    use super::*;
    use crossterm::event::{KeyCode, KeyEventKind, KeyEventState, KeyModifiers};

    fn make_key_event(code: KeyCode, modifiers: KeyModifiers) -> crossterm::event::KeyEvent {
        crossterm::event::KeyEvent {
            code,
            modifiers,
            kind: KeyEventKind::Press,
            state: KeyEventState::NONE,
        }
    }

    #[test]
    fn test_default_normal_mode() {
        let mut kb = default_keybindings();

        // Check some common bindings
        assert_eq!(
            kb.dispatch(
                KeybindingMode::Normal,
                make_key_event(KeyCode::Char('j'), KeyModifiers::NONE)
            ),
            Some(Action::Next)
        );
        assert_eq!(
            kb.dispatch(
                KeybindingMode::Normal,
                make_key_event(KeyCode::Char('k'), KeyModifiers::NONE)
            ),
            Some(Action::Previous)
        );
        assert_eq!(
            kb.dispatch(
                KeybindingMode::Normal,
                make_key_event(KeyCode::Char('q'), KeyModifiers::NONE)
            ),
            Some(Action::Quit)
        );
        assert_eq!(
            kb.dispatch(
                KeybindingMode::Normal,
                make_key_event(KeyCode::Char('?'), KeyModifiers::NONE)
            ),
            Some(Action::ToggleHelp)
        );
    }

    #[test]
    fn test_default_table_mode_copy_bindings() {
        // Regression: the action-dispatch refactor (2ca73a3) silently rebound
        // table-mode y/Y/r to generic CopyContent/CopyAnchor/ToggleRawSource,
        // breaking cell/row/markdown copy. These must map to the table actions.
        let mut kb = default_keybindings();

        assert_eq!(
            kb.dispatch(
                KeybindingMode::InteractiveTable,
                make_key_event(KeyCode::Char('y'), KeyModifiers::NONE)
            ),
            Some(Action::CopyTableCell)
        );
        assert_eq!(
            kb.dispatch(
                KeybindingMode::InteractiveTable,
                make_key_event(KeyCode::Char('Y'), KeyModifiers::SHIFT)
            ),
            Some(Action::CopyTableRow)
        );
        assert_eq!(
            kb.dispatch(
                KeybindingMode::InteractiveTable,
                make_key_event(KeyCode::Char('r'), KeyModifiers::NONE)
            ),
            Some(Action::CopyTableMarkdown)
        );
    }

    #[test]
    fn test_default_mouse_capture_toggle_binding() {
        let mut kb = default_keybindings();
        assert_eq!(
            kb.dispatch(
                KeybindingMode::Normal,
                make_key_event(KeyCode::Char('M'), KeyModifiers::SHIFT)
            ),
            Some(Action::ToggleMouseCapture)
        );
    }

    #[test]
    fn test_default_interactive_mode() {
        let mut kb = default_keybindings();

        assert_eq!(
            kb.dispatch(
                KeybindingMode::Interactive,
                make_key_event(KeyCode::Esc, KeyModifiers::NONE)
            ),
            Some(Action::ExitInteractiveMode)
        );
        assert_eq!(
            kb.dispatch(
                KeybindingMode::Interactive,
                make_key_event(KeyCode::Tab, KeyModifiers::NONE)
            ),
            Some(Action::InteractiveNextLink)
        );
    }

    #[test]
    fn test_all_modes_have_bindings() {
        let kb = default_keybindings();

        let modes = [
            KeybindingMode::Normal,
            KeybindingMode::Help,
            KeybindingMode::ThemePicker,
            KeybindingMode::Interactive,
            KeybindingMode::InteractiveTable,
            KeybindingMode::LinkFollow,
            KeybindingMode::LinkSearch,
            KeybindingMode::Search,
            KeybindingMode::DocSearch,
            KeybindingMode::CommandPalette,
            KeybindingMode::ConfirmDialog,
            KeybindingMode::CellEdit,
        ];

        for mode in modes {
            assert!(
                kb.get_mode_keybinds(mode).is_some(),
                "Mode {:?} has no bindings",
                mode
            );
            assert!(
                !kb.get_mode_keybinds(mode).unwrap().as_slice().is_empty(),
                "Mode {:?} has empty bindings",
                mode
            );
        }
    }
}