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