Line | Branch | Exec | Source |
---|---|---|---|
1 | #include <stddef.h> | ||
2 | #include "mode.h" | ||
3 | #include "bind.h" | ||
4 | #include "change.h" | ||
5 | #include "cmdline.h" | ||
6 | #include "command/macro.h" | ||
7 | #include "completion.h" | ||
8 | #include "editor.h" | ||
9 | #include "insert.h" | ||
10 | #include "shift.h" | ||
11 | #include "terminal/paste.h" | ||
12 | #include "util/debug.h" | ||
13 | #include "util/unicode.h" | ||
14 | #include "util/xmalloc.h" | ||
15 | #include "view.h" | ||
16 | |||
17 | 25 | ModeHandler *new_mode(HashMap *modes, char *name, const CommandSet *cmds) | |
18 | { | ||
19 | 25 | ModeHandler *mode = xnew0(ModeHandler, 1); | |
20 | 25 | mode->name = name; | |
21 | 25 | mode->cmds = cmds; | |
22 | 25 | return hashmap_insert(modes, name, mode); | |
23 | } | ||
24 | |||
25 | ✗ | static bool insert_paste(EditorState *e, const ModeHandler *handler, bool bracketed) | |
26 | { | ||
27 | ✗ | String str = term_read_paste(&e->terminal.ibuf, bracketed); | |
28 | ✗ | if (handler->cmds == &normal_commands) { | |
29 | ✗ | begin_change(CHANGE_MERGE_NONE); | |
30 | ✗ | insert_text(e->view, str.buffer, str.len, true); | |
31 | ✗ | end_change(); | |
32 | ✗ | macro_insert_text_hook(&e->macro, str.buffer, str.len); | |
33 | } else { | ||
34 | ✗ | CommandLine *c = &e->cmdline; | |
35 | ✗ | string_replace_byte(&str, '\n', ' '); | |
36 | ✗ | string_insert_buf(&c->buf, c->pos, str.buffer, str.len); | |
37 | ✗ | c->pos += str.len; | |
38 | ✗ | c->search_pos = NULL; | |
39 | } | ||
40 | ✗ | string_free(&str); | |
41 | ✗ | return true; | |
42 | } | ||
43 | |||
44 | 7 | static bool handle_input_single ( | |
45 | EditorState *e, | ||
46 | const ModeHandler *handler, | ||
47 | KeyCode key, | ||
48 | ModeHandlerFlags inherited_flags | ||
49 | ) { | ||
50 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 7 times.
|
7 | if (key == KEYCODE_DETECTED_PASTE || key == KEYCODE_BRACKETED_PASTE) { |
51 | ✗ | return insert_paste(e, handler, key == KEYCODE_BRACKETED_PASTE); | |
52 | } | ||
53 | |||
54 | 7 | ModeHandlerFlags noinsert_flags = MHF_NO_TEXT_INSERTION | MHF_NO_TEXT_INSERTION_RECURSIVE; | |
55 | 7 | ModeHandlerFlags flag_union = handler->flags | inherited_flags; | |
56 | 7 | bool insert_unicode_range = !(flag_union & noinsert_flags); | |
57 | |||
58 |
1/2✓ Branch 0 (4→5) taken 7 times.
✗ Branch 1 (4→25) not taken.
|
7 | if (insert_unicode_range) { |
59 | 7 | const CommandSet *cmds = handler->cmds; | |
60 |
2/2✓ Branch 0 (5→6) taken 5 times.
✓ Branch 1 (5→16) taken 2 times.
|
7 | if (cmds == &normal_commands) { |
61 | 5 | View *view = e->view; | |
62 | 5 | KeyCode shift = key & MOD_SHIFT; | |
63 |
1/4✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→12) taken 5 times.
✗ Branch 2 (7→8) not taken.
✗ Branch 3 (7→12) not taken.
|
5 | if ((key & ~shift) == KEY_TAB && view->selection == SELECT_LINES) { |
64 | // In line selections, Tab/S-Tab behave like `shift -- 1/-1` | ||
65 | ✗ | shift_lines(view, shift ? -1 : 1); | |
66 | ✗ | return true; | |
67 | } | ||
68 |
1/2✓ Branch 0 (12→13) taken 5 times.
✗ Branch 1 (12→25) not taken.
|
5 | if (u_is_unicode(key)) { |
69 | 5 | insert_ch(e->view, key); | |
70 | 5 | macro_insert_char_hook(&e->macro, key); | |
71 | 5 | return true; | |
72 | } | ||
73 | } else { | ||
74 | 2 | BUG_ON(cmds != &cmd_mode_commands && cmds != &search_mode_commands); | |
75 |
2/4✓ Branch 0 (19→20) taken 2 times.
✗ Branch 1 (19→25) not taken.
✓ Branch 2 (20→21) taken 2 times.
✗ Branch 3 (20→25) not taken.
|
2 | if (u_is_unicode(key) && (key != KEY_TAB && key != KEY_ENTER)) { |
76 | 2 | CommandLine *c = &e->cmdline; | |
77 | 2 | c->pos += string_insert_codepoint(&c->buf, c->pos, key); | |
78 |
1/2✓ Branch 0 (22→23) taken 2 times.
✗ Branch 1 (22→24) not taken.
|
2 | if (cmds == &cmd_mode_commands) { |
79 | 2 | maybe_reset_completion(c); | |
80 | } | ||
81 | 2 | c->search_pos = NULL; | |
82 | 2 | return true; | |
83 | } | ||
84 | } | ||
85 | } | ||
86 | |||
87 | ✗ | return handle_binding(e, handler, key); | |
88 | } | ||
89 | |||
90 | // NOLINTNEXTLINE(misc-no-recursion) | ||
91 | 7 | static bool handle_input_recursive ( | |
92 | EditorState *e, | ||
93 | const ModeHandler *handler, | ||
94 | KeyCode key, | ||
95 | ModeHandlerFlags inherited_flags | ||
96 | ) { | ||
97 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→9) taken 7 times.
|
7 | if (handle_input_single(e, handler, key, inherited_flags)) { |
98 | return true; | ||
99 | } | ||
100 | |||
101 | ✗ | const PointerArray *ftmodes = &handler->fallthrough_modes; | |
102 | ✗ | inherited_flags |= (handler->flags & MHF_NO_TEXT_INSERTION_RECURSIVE); | |
103 | |||
104 | ✗ | for (size_t i = 0, n = ftmodes->count; i < n; i++) { | |
105 | ✗ | const ModeHandler *h = ftmodes->ptrs[i]; | |
106 | ✗ | if (handle_input_recursive(e, h, key, inherited_flags)) { | |
107 | return true; | ||
108 | } | ||
109 | } | ||
110 | |||
111 | return false; | ||
112 | } | ||
113 | |||
114 | 7 | bool handle_input(EditorState *e, KeyCode key) | |
115 | { | ||
116 | 7 | return handle_input_recursive(e, e->mode, key, 0); | |
117 | } | ||
118 |