Line | Branch | Exec | Source |
---|---|---|---|
1 | #include "macro.h" | ||
2 | #include "serialize.h" | ||
3 | #include "util/string-view.h" | ||
4 | |||
5 | 8 | static void merge_insert_buffer(MacroRecorder *m) | |
6 | { | ||
7 | 8 | size_t len = m->insert_buffer.len; | |
8 |
2/2✓ Branch 0 (2→3) taken 6 times.
✓ Branch 1 (2→4) taken 2 times.
|
8 | if (len == 0) { |
9 | 6 | return; | |
10 | } | ||
11 | |||
12 | 2 | String s = string_new(32 + len); | |
13 | 2 | StringView ibuf = strview_from_string(&m->insert_buffer); | |
14 | 2 | string_append_literal(&s, "insert -k "); | |
15 | |||
16 |
2/2✓ Branch 0 (7→8) taken 1 times.
✓ Branch 1 (7→9) taken 1 times.
|
2 | if (unlikely(strview_has_prefix(&ibuf, "-"))) { |
17 | 1 | string_append_literal(&s, "-- "); | |
18 | } | ||
19 | |||
20 | 2 | string_append_escaped_arg_sv(&s, ibuf, true); | |
21 | 2 | string_clear(&m->insert_buffer); | |
22 | 2 | ptr_array_append(&m->macro, string_steal_cstring(&s)); | |
23 | } | ||
24 | |||
25 | 2 | bool macro_record(MacroRecorder *m) | |
26 | { | ||
27 |
1/2✓ Branch 0 (2→3) taken 2 times.
✗ Branch 1 (2→5) not taken.
|
2 | if (m->recording) { |
28 | return false; | ||
29 | } | ||
30 | 2 | ptr_array_free(&m->prev_macro); | |
31 | 2 | m->prev_macro = m->macro; | |
32 | 2 | m->macro = (PointerArray) PTR_ARRAY_INIT; | |
33 | 2 | m->recording = true; | |
34 | 2 | return true; | |
35 | } | ||
36 | |||
37 | 1 | bool macro_stop(MacroRecorder *m) | |
38 | { | ||
39 |
1/2✓ Branch 0 (2→3) taken 1 times.
✗ Branch 1 (2→5) not taken.
|
1 | if (!m->recording) { |
40 | return false; | ||
41 | } | ||
42 | 1 | merge_insert_buffer(m); | |
43 | 1 | m->recording = false; | |
44 | 1 | return true; | |
45 | } | ||
46 | |||
47 | 1 | bool macro_cancel(MacroRecorder *m) | |
48 | { | ||
49 |
1/2✓ Branch 0 (2→3) taken 1 times.
✗ Branch 1 (2→5) not taken.
|
1 | if (!m->recording) { |
50 | return false; | ||
51 | } | ||
52 | 1 | ptr_array_free(&m->macro); | |
53 | 1 | m->macro = m->prev_macro; | |
54 | 1 | m->prev_macro = (PointerArray) PTR_ARRAY_INIT; | |
55 | 1 | m->recording = false; | |
56 | 1 | return true; | |
57 | } | ||
58 | |||
59 | 5 | void macro_command_hook(MacroRecorder *m, const char *cmd_name, char **args) | |
60 | { | ||
61 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 5 times.
|
5 | if (!m->recording) { |
62 | ✗ | return; | |
63 | } | ||
64 | |||
65 | 5 | String buf = string_new(512); | |
66 | 5 | string_append_cstring(&buf, cmd_name); | |
67 | |||
68 |
2/2✓ Branch 0 (10→7) taken 2 times.
✓ Branch 1 (10→11) taken 5 times.
|
7 | for (size_t i = 0; args[i]; i++) { |
69 | 2 | string_append_byte(&buf, ' '); | |
70 | 2 | string_append_escaped_arg(&buf, args[i], true); | |
71 | } | ||
72 | |||
73 | 5 | merge_insert_buffer(m); | |
74 | 5 | ptr_array_append(&m->macro, string_steal_cstring(&buf)); | |
75 | } | ||
76 | |||
77 | ✗ | void macro_search_hook ( | |
78 | MacroRecorder *m, | ||
79 | const char *pattern, | ||
80 | bool reverse, | ||
81 | bool add_to_history | ||
82 | ) { | ||
83 | ✗ | if (!m->recording) { | |
84 | ✗ | return; | |
85 | } | ||
86 | |||
87 | ✗ | const char *args[5]; | |
88 | ✗ | size_t i = 0; | |
89 | |||
90 | ✗ | if (pattern) { | |
91 | ✗ | if (reverse) { | |
92 | ✗ | args[i++] = "-r"; | |
93 | } | ||
94 | ✗ | if (!add_to_history) { | |
95 | ✗ | args[i++] = "-H"; | |
96 | } | ||
97 | ✗ | if (unlikely(pattern[0] == '-')) { | |
98 | ✗ | args[i++] = "--"; | |
99 | } | ||
100 | ✗ | args[i++] = pattern; | |
101 | } else { | ||
102 | ✗ | args[i++] = reverse ? "-p" : "-n"; | |
103 | } | ||
104 | |||
105 | ✗ | args[i] = NULL; | |
106 | ✗ | macro_command_hook(m, "search", (char**)args); | |
107 | } | ||
108 | |||
109 | 5 | void macro_insert_char_hook(MacroRecorder *m, CodePoint c) | |
110 | { | ||
111 |
1/2✓ Branch 0 (2→3) taken 5 times.
✗ Branch 1 (2→4) not taken.
|
5 | if (m->recording) { |
112 | 5 | string_append_codepoint(&m->insert_buffer, c); | |
113 | } | ||
114 | 5 | } | |
115 | |||
116 | 2 | void macro_insert_text_hook(MacroRecorder *m, const char *text, size_t size) | |
117 | { | ||
118 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 2 times.
|
2 | if (!m->recording) { |
119 | ✗ | return; | |
120 | } | ||
121 | |||
122 | 2 | String buf = string_new(512); | |
123 | 2 | StringView sv = string_view(text, size); | |
124 | 2 | string_append_literal(&buf, "insert -m "); | |
125 | |||
126 |
2/2✓ Branch 0 (7→8) taken 1 times.
✓ Branch 1 (7→9) taken 1 times.
|
2 | if (unlikely(strview_has_prefix(&sv, "-"))) { |
127 | 1 | string_append_literal(&buf, "-- "); | |
128 | } | ||
129 | |||
130 | 2 | string_append_escaped_arg_sv(&buf, sv, true); | |
131 | 2 | merge_insert_buffer(m); | |
132 | 2 | ptr_array_append(&m->macro, string_steal_cstring(&buf)); | |
133 | } | ||
134 | |||
135 | 2 | String dump_macro(const MacroRecorder *m) | |
136 | { | ||
137 | 2 | String buf = string_new(4096); | |
138 |
2/2✓ Branch 0 (7→4) taken 18 times.
✓ Branch 1 (7→8) taken 2 times.
|
20 | for (size_t i = 0, n = m->macro.count; i < n; i++) { |
139 | 18 | const char *cmd_str = m->macro.ptrs[i]; | |
140 | 18 | string_append_cstring(&buf, cmd_str); | |
141 | 18 | string_append_byte(&buf, '\n'); | |
142 | } | ||
143 | 2 | return buf; | |
144 | } | ||
145 | |||
146 | 8 | void free_macro(MacroRecorder *m) | |
147 | { | ||
148 | 8 | string_free(&m->insert_buffer); | |
149 | 8 | ptr_array_free(&m->macro); | |
150 | 8 | ptr_array_free(&m->prev_macro); | |
151 | 8 | m->recording = false; | |
152 | 8 | } | |
153 |