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