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