| 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 | ✗ | char *cmd; | |
| 88 | ✗ | if (pattern) { | |
| 89 | ✗ | StringView pat = strview(pattern); | |
| 90 | ✗ | String buf = string_new(pat.length + sizeof("search -r -H -- ") + 8); | |
| 91 | ✗ | string_append_cstring(&buf, "search "); | |
| 92 | ✗ | string_append_cstring(&buf, reverse ? "-r " : ""); | |
| 93 | ✗ | string_append_cstring(&buf, add_to_history ? "" : "-H "); | |
| 94 | ✗ | string_append_cstring(&buf, unlikely(pattern[0] == '-') ? "-- " : ""); | |
| 95 | ✗ | string_append_escaped_arg_sv(&buf, pat, true); | |
| 96 | ✗ | cmd = string_steal_cstring(&buf); | |
| 97 | } else { | ||
| 98 | ✗ | cmd = xstrdup(reverse ? "search -p" : "search -n"); | |
| 99 | } | ||
| 100 | |||
| 101 | ✗ | merge_insert_buffer(m); | |
| 102 | ✗ | ptr_array_append(&m->macro, cmd); | |
| 103 | } | ||
| 104 | |||
| 105 | 5 | void macro_insert_char_hook(MacroRecorder *m, CodePoint c) | |
| 106 | { | ||
| 107 |
1/2✓ Branch 0 (2→3) taken 5 times.
✗ Branch 1 (2→4) not taken.
|
5 | if (m->recording) { |
| 108 | 5 | string_append_codepoint(&m->insert_buffer, c); | |
| 109 | } | ||
| 110 | 5 | } | |
| 111 | |||
| 112 | 2 | void macro_insert_text_hook(MacroRecorder *m, const char *text, size_t size) | |
| 113 | { | ||
| 114 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 2 times.
|
2 | if (!m->recording) { |
| 115 | ✗ | return; | |
| 116 | } | ||
| 117 | |||
| 118 | 2 | String buf = string_new(512); | |
| 119 | 2 | StringView sv = string_view(text, size); | |
| 120 | 2 | string_append_literal(&buf, "insert -m "); | |
| 121 | |||
| 122 |
2/2✓ Branch 0 (7→8) taken 1 times.
✓ Branch 1 (7→9) taken 1 times.
|
2 | if (unlikely(strview_has_prefix(sv, "-"))) { |
| 123 | 1 | string_append_literal(&buf, "-- "); | |
| 124 | } | ||
| 125 | |||
| 126 | 2 | string_append_escaped_arg_sv(&buf, sv, true); | |
| 127 | 2 | merge_insert_buffer(m); | |
| 128 | 2 | ptr_array_append(&m->macro, string_steal_cstring(&buf)); | |
| 129 | } | ||
| 130 | |||
| 131 | 2 | String dump_macro(const MacroRecorder *m) | |
| 132 | { | ||
| 133 | 2 | String buf = string_new(4096); | |
| 134 |
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++) { |
| 135 | 18 | const char *cmd_str = m->macro.ptrs[i]; | |
| 136 | 18 | string_append_cstring(&buf, cmd_str); | |
| 137 | 18 | string_append_byte(&buf, '\n'); | |
| 138 | } | ||
| 139 | 2 | return buf; | |
| 140 | } | ||
| 141 | |||
| 142 | 11 | void free_macro(MacroRecorder *m) | |
| 143 | { | ||
| 144 | 11 | string_free(&m->insert_buffer); | |
| 145 | 11 | ptr_array_free(&m->macro); | |
| 146 | 11 | ptr_array_free(&m->prev_macro); | |
| 147 | 11 | m->recording = false; | |
| 148 | 11 | } | |
| 149 |