Line | Branch | Exec | Source |
---|---|---|---|
1 | #include <errno.h> | ||
2 | #include <fcntl.h> | ||
3 | #include <glob.h> | ||
4 | #include <signal.h> | ||
5 | #include <stdlib.h> | ||
6 | #include <string.h> | ||
7 | #include <sys/stat.h> | ||
8 | #include <unistd.h> | ||
9 | #include "commands.h" | ||
10 | #include "bind.h" | ||
11 | #include "bookmark.h" | ||
12 | #include "buffer.h" | ||
13 | #include "case.h" | ||
14 | #include "change.h" | ||
15 | #include "cmdline.h" | ||
16 | #include "command/alias.h" | ||
17 | #include "command/args.h" | ||
18 | #include "command/error.h" | ||
19 | #include "command/macro.h" | ||
20 | #include "compiler.h" | ||
21 | #include "config.h" | ||
22 | #include "convert.h" | ||
23 | #include "copy.h" | ||
24 | #include "delete.h" | ||
25 | #include "editor.h" | ||
26 | #include "encoding.h" | ||
27 | #include "exec.h" | ||
28 | #include "file-option.h" | ||
29 | #include "filetype.h" | ||
30 | #include "frame.h" | ||
31 | #include "history.h" | ||
32 | #include "insert.h" | ||
33 | #include "join.h" | ||
34 | #include "load-save.h" | ||
35 | #include "lock.h" | ||
36 | #include "mode.h" | ||
37 | #include "move.h" | ||
38 | #include "msg.h" | ||
39 | #include "regexp.h" | ||
40 | #include "replace.h" | ||
41 | #include "search.h" | ||
42 | #include "selection.h" | ||
43 | #include "shift.h" | ||
44 | #include "show.h" | ||
45 | #include "spawn.h" | ||
46 | #include "syntax/color.h" | ||
47 | #include "syntax/syntax.h" | ||
48 | #include "tag.h" | ||
49 | #include "terminal/cursor.h" | ||
50 | #include "terminal/mode.h" | ||
51 | #include "terminal/osc52.h" | ||
52 | #include "terminal/style.h" | ||
53 | #include "terminal/terminal.h" | ||
54 | #include "ui.h" | ||
55 | #include "util/arith.h" | ||
56 | #include "util/array.h" | ||
57 | #include "util/ascii.h" | ||
58 | #include "util/bit.h" | ||
59 | #include "util/bsearch.h" | ||
60 | #include "util/debug.h" | ||
61 | #include "util/intern.h" | ||
62 | #include "util/log.h" | ||
63 | #include "util/path.h" | ||
64 | #include "util/str-array.h" | ||
65 | #include "util/str-util.h" | ||
66 | #include "util/strtonum.h" | ||
67 | #include "util/time-util.h" | ||
68 | #include "util/xmalloc.h" | ||
69 | #include "util/xsnprintf.h" | ||
70 | #include "view.h" | ||
71 | #include "window.h" | ||
72 | #include "wrap.h" | ||
73 | |||
74 | NOINLINE | ||
75 | 16 | static void do_selection_noinline(View *view, SelectionType sel) | |
76 | { | ||
77 | // Should only be called from do_selection() | ||
78 | 16 | BUG_ON(sel == view->selection); | |
79 | |||
80 |
2/2✓ Branch 0 (4→5) taken 1 times.
✓ Branch 1 (4→7) taken 15 times.
|
16 | if (sel == SELECT_NONE) { |
81 | 1 | unselect(view); | |
82 | 1 | return; | |
83 | } | ||
84 | |||
85 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→11) taken 15 times.
|
15 | if (view->selection) { |
86 | ✗ | if (view->selection != sel) { | |
87 | ✗ | view->selection = sel; | |
88 | // TODO: be less brute force about this; only the first/last | ||
89 | // line of the selection can change in this case | ||
90 | ✗ | mark_all_lines_changed(view->buffer); | |
91 | } | ||
92 | ✗ | return; | |
93 | } | ||
94 | |||
95 | 15 | view->sel_so = block_iter_get_offset(&view->cursor); | |
96 | 15 | view->sel_eo = SEL_EO_RECALC; | |
97 | 15 | view->selection = sel; | |
98 | |||
99 | // Need to mark current line changed because cursor might | ||
100 | // move up or down before screen is updated | ||
101 | 15 | view_update_cursor_y(view); | |
102 | 15 | buffer_mark_lines_changed(view->buffer, view->cy, view->cy); | |
103 | } | ||
104 | |||
105 | 130 | static void do_selection(View *view, SelectionType sel) | |
106 | { | ||
107 |
2/2✓ Branch 0 (2→3) taken 114 times.
✓ Branch 1 (2→6) taken 16 times.
|
130 | if (likely(sel == view->selection)) { |
108 | // If `sel` is SELECT_NONE here, it's always equal to select_mode | ||
109 | 114 | BUG_ON(!sel && view->select_mode); | |
110 | return; | ||
111 | } | ||
112 | |||
113 | 16 | do_selection_noinline(view, sel); | |
114 | } | ||
115 | |||
116 | 18 | static char last_flag_or_default(const CommandArgs *a, char def) | |
117 | { | ||
118 | 18 | size_t n = a->nr_flags; | |
119 |
2/2✓ Branch 0 (2→3) taken 14 times.
✓ Branch 1 (2→4) taken 4 times.
|
18 | return n ? a->flags[n - 1] : def; |
120 | } | ||
121 | |||
122 | 16 | static char last_flag(const CommandArgs *a) | |
123 | { | ||
124 | 16 | return last_flag_or_default(a, 0); | |
125 | } | ||
126 | |||
127 | 6307 | static bool has_flag(const CommandArgs *a, unsigned char flag) | |
128 | { | ||
129 | 6307 | return cmdargs_has_flag(a, flag); | |
130 | } | ||
131 | |||
132 | 127 | static void handle_selection_flags(View *view, const CommandArgs *a) | |
133 | { | ||
134 | 127 | SelectionType sel; | |
135 |
2/2✓ Branch 0 (3→4) taken 112 times.
✓ Branch 1 (3→9) taken 15 times.
|
127 | if (has_flag(a, 'l')) { |
136 | sel = SELECT_LINES; | ||
137 |
2/2✓ Branch 0 (5→6) taken 10 times.
✓ Branch 1 (5→8) taken 102 times.
|
112 | } else if (has_flag(a, 'c')) { |
138 | 10 | static_assert(SELECT_CHARS < SELECT_LINES); | |
139 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 10 times.
|
10 | sel = MAX(SELECT_CHARS, view->select_mode); |
140 | } else { | ||
141 | 102 | sel = view->select_mode; | |
142 | } | ||
143 | 127 | do_selection(view, sel); | |
144 | 127 | } | |
145 | |||
146 | 170 | static bool cmd_alias(EditorState *e, const CommandArgs *a) | |
147 | { | ||
148 | 170 | const char *const name = a->args[0]; | |
149 | 170 | const char *const cmd = a->args[1]; | |
150 | |||
151 |
2/2✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→4) taken 169 times.
|
170 | if (unlikely(name[0] == '\0')) { |
152 | 1 | return error_msg(&e->err, "Empty alias name not allowed"); | |
153 | } | ||
154 |
2/2✓ Branch 0 (4→5) taken 1 times.
✓ Branch 1 (4→11) taken 168 times.
|
169 | if (unlikely(name[0] == '-')) { |
155 | // Disallowing this simplifies auto-completion for "alias " | ||
156 | 1 | return error_msg(&e->err, "Alias name cannot begin with '-'"); | |
157 | } | ||
158 | |||
159 |
2/2✓ Branch 0 (11→6) taken 1282 times.
✓ Branch 1 (11→12) taken 167 times.
|
1449 | for (size_t i = 0; name[i]; i++) { |
160 | 1282 | unsigned char c = name[i]; | |
161 |
5/6✓ Branch 0 (6→7) taken 99 times.
✓ Branch 1 (6→10) taken 1183 times.
✓ Branch 2 (7→8) taken 1 times.
✓ Branch 3 (7→10) taken 98 times.
✓ Branch 4 (8→9) taken 1 times.
✗ Branch 5 (8→10) not taken.
|
1282 | if (unlikely(!(is_word_byte(c) || c == '-' || c == '?' || c == '!'))) { |
162 | 1 | return error_msg(&e->err, "Invalid byte in alias name: %c (0x%02hhX)", c, c); | |
163 | } | ||
164 | } | ||
165 | |||
166 |
2/2✓ Branch 0 (13→14) taken 1 times.
✓ Branch 1 (13→15) taken 166 times.
|
167 | if (unlikely(find_normal_command(name))) { |
167 | 1 | return error_msg(&e->err, "Can't replace existing command %s with an alias", name); | |
168 | } | ||
169 | |||
170 |
2/2✓ Branch 0 (15→16) taken 164 times.
✓ Branch 1 (15→17) taken 2 times.
|
166 | if (likely(cmd)) { |
171 | 164 | add_alias(&e->aliases, name, cmd); | |
172 | } else { | ||
173 | 2 | remove_alias(&e->aliases, name); | |
174 | } | ||
175 | |||
176 | return true; | ||
177 | } | ||
178 | |||
179 | 1130 | static bool cmd_bind(EditorState *e, const CommandArgs *a) | |
180 | { | ||
181 | 1130 | const char *keystr = a->args[a->nr_flag_args]; | |
182 | 1130 | const char *cmd = a->args[a->nr_flag_args + 1]; | |
183 | 1130 | KeyCode key = parse_key_string(keystr); | |
184 |
2/2✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→9) taken 1129 times.
|
1130 | if (unlikely(key == KEY_NONE)) { |
185 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→8) taken 1 times.
|
1 | if (has_flag(a, 'q')) { |
186 | ✗ | LOG_INFO("bind -q: dropped invalid key string: %s", keystr); | |
187 | ✗ | return false; | |
188 | } | ||
189 | 1 | return error_msg(&e->err, "invalid key string: %s", keystr); | |
190 | } | ||
191 | |||
192 | 1129 | ModeHandler *modes[10]; | |
193 | 1129 | size_t nmodes = 0; | |
194 | 1129 | static_assert(ARRAYLEN(modes) <= ARRAYLEN(a->flags)); | |
195 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 1129 times.
|
1129 | if (has_flag(a, 'n')) { |
196 | ✗ | modes[nmodes++] = e->normal_mode; | |
197 | } | ||
198 |
2/2✓ Branch 0 (13→14) taken 259 times.
✓ Branch 1 (13→15) taken 870 times.
|
1129 | if (has_flag(a, 'c')) { |
199 | 259 | modes[nmodes++] = e->command_mode; | |
200 | } | ||
201 |
2/2✓ Branch 0 (16→17) taken 266 times.
✓ Branch 1 (16→18) taken 863 times.
|
1129 | if (has_flag(a, 's')) { |
202 | 266 | modes[nmodes++] = e->search_mode; | |
203 | } | ||
204 | |||
205 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→24) taken 1129 times.
|
1129 | if (unlikely(nmodes + a->nr_flag_args > ARRAYLEN(modes))) { |
206 | // This is already prevented by the ARGERR_TOO_MANY_OPTIONS check | ||
207 | // in do_parse_args(), but since that's only incidental, it's still | ||
208 | // checked here | ||
209 | ✗ | return error_msg(&e->err, "too many modes specified"); | |
210 | } | ||
211 | |||
212 | // Gather pointers to modes specified via `-T modename`. This is done | ||
213 | // separately from adding/removing bindings, partly so that either all | ||
214 | // modes are processed or none are (if the error below is triggered). | ||
215 |
2/2✓ Branch 0 (24→20) taken 1 times.
✓ Branch 1 (24→25) taken 1128 times.
|
1129 | for (size_t i = 0, n = a->nr_flag_args; i < n; i++) { |
216 | 1 | const char *name = a->args[i]; | |
217 | 1 | ModeHandler *mode = get_mode_handler(&e->modes, name); | |
218 |
1/2✓ Branch 0 (21→22) taken 1 times.
✗ Branch 1 (21→23) not taken.
|
1 | if (unlikely(!mode)) { |
219 | 1 | return error_msg(&e->err, "can't bind key in unknown mode '%s'", name); | |
220 | } | ||
221 | ✗ | modes[nmodes++] = mode; | |
222 | } | ||
223 | |||
224 |
2/2✓ Branch 0 (25→26) taken 848 times.
✓ Branch 1 (25→27) taken 280 times.
|
1128 | if (nmodes == 0) { |
225 | // No [-cnsT] flags used; default to normal mode | ||
226 | 848 | modes[nmodes++] = e->normal_mode; | |
227 | } | ||
228 | |||
229 |
1/2✗ Branch 0 (27→30) not taken.
✓ Branch 1 (27→31) taken 1128 times.
|
1128 | if (!cmd) { |
230 | ✗ | for (size_t i = 0; i < nmodes; i++) { | |
231 | ✗ | remove_binding(&modes[i]->key_bindings, key); | |
232 | } | ||
233 | return true; | ||
234 | } | ||
235 | |||
236 | 1128 | CommandRunner runner = cmdrunner(e, NULL); | |
237 |
2/2✓ Branch 0 (35→32) taken 1373 times.
✓ Branch 1 (35→36) taken 1128 times.
|
2501 | for (size_t i = 0; i < nmodes; i++) { |
238 | 1373 | runner.cmds = modes[i]->cmds; | |
239 | 1373 | CachedCommand *cc = cached_command_new(&runner, cmd); | |
240 | 1373 | add_binding(&modes[i]->key_bindings, key, cc); | |
241 | } | ||
242 | |||
243 | return true; | ||
244 | } | ||
245 | |||
246 | 4 | static bool cmd_bof(EditorState *e, const CommandArgs *a) | |
247 | { | ||
248 | 4 | handle_selection_flags(e->view, a); | |
249 | 4 | move_bof(e->view); | |
250 | 4 | return true; | |
251 | } | ||
252 | |||
253 | 7 | static bool cmd_bol(EditorState *e, const CommandArgs *a) | |
254 | { | ||
255 | 7 | SmartBolType type = BOL_SIMPLE; | |
256 |
1/4✗ Branch 0 (3→4) not taken.
✗ Branch 1 (3→5) not taken.
✗ Branch 2 (3→6) not taken.
✓ Branch 3 (3→7) taken 7 times.
|
7 | switch (cmdargs_pick_winning_flag(a, "rst")) { |
257 | ✗ | case 'r': type = BOL_TOGGLE_LR; break; | |
258 | ✗ | case 's': type = BOL_INDENT; break; | |
259 | ✗ | case 't': type = BOL_TOGGLE_RL; break; | |
260 | } | ||
261 | |||
262 | 7 | handle_selection_flags(e->view, a); | |
263 | 7 | move_bol(e->view, type); | |
264 | 7 | return true; | |
265 | } | ||
266 | |||
267 | 1 | static bool cmd_bolsf(EditorState *e, const CommandArgs *a) | |
268 | { | ||
269 | 1 | BUG_ON(a->nr_args); | |
270 | 1 | View *view = e->view; | |
271 | 1 | handle_selection_flags(view, a); | |
272 | |||
273 |
1/2✓ Branch 0 (6→7) taken 1 times.
✗ Branch 1 (6→11) not taken.
|
1 | if (!block_iter_bol(&view->cursor)) { |
274 | 1 | long top = view->vy + window_get_scroll_margin(e->window); | |
275 |
1/2✓ Branch 0 (8→9) taken 1 times.
✗ Branch 1 (8→10) not taken.
|
1 | if (view->cy > top) { |
276 | 1 | move_up(view, view->cy - top); | |
277 | } else { | ||
278 | ✗ | block_iter_bof(&view->cursor); | |
279 | } | ||
280 | } | ||
281 | |||
282 | 1 | view_reset_preferred_x(view); | |
283 | 1 | return true; | |
284 | } | ||
285 | |||
286 | 1 | static bool cmd_bookmark(EditorState *e, const CommandArgs *a) | |
287 | { | ||
288 | 1 | PointerArray *bookmarks = &e->bookmarks; | |
289 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 1 times.
|
1 | if (has_flag(a, 'r')) { |
290 | ✗ | bookmark_pop(bookmarks, e->window); | |
291 | ✗ | return true; | |
292 | } | ||
293 | |||
294 | 1 | bookmark_push(bookmarks, get_current_file_location(e->view)); | |
295 | 1 | return true; | |
296 | } | ||
297 | |||
298 | 2 | static bool cmd_case(EditorState *e, const CommandArgs *a) | |
299 | { | ||
300 | 2 | change_case(e->view, last_flag_or_default(a, 't')); | |
301 | 2 | return true; | |
302 | } | ||
303 | |||
304 | 4 | static void mark_tabbar_changed(Window *window, void* UNUSED_ARG(data)) | |
305 | { | ||
306 | 4 | window->update_tabbar = true; | |
307 | 4 | } | |
308 | |||
309 | 6 | static bool cmd_cd(EditorState *e, const CommandArgs *a) | |
310 | { | ||
311 | 6 | const char *dir = a->args[0]; | |
312 | 6 | ErrorBuffer *ebuf = &e->err; | |
313 |
2/2✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→4) taken 5 times.
|
6 | if (unlikely(dir[0] == '\0')) { |
314 | 1 | return error_msg(ebuf, "directory argument cannot be empty"); | |
315 | } | ||
316 | |||
317 |
2/2✓ Branch 0 (4→5) taken 2 times.
✓ Branch 1 (4→8) taken 3 times.
|
5 | if (streq(dir, "-")) { |
318 | 2 | dir = xgetenv("OLDPWD"); | |
319 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 2 times.
|
2 | if (!dir) { |
320 | ✗ | return error_msg(ebuf, "OLDPWD not set"); | |
321 | } | ||
322 | } | ||
323 | |||
324 | 5 | char buf[8192]; | |
325 | 5 | const char *cwd = getcwd(buf, sizeof(buf)); | |
326 |
2/2✓ Branch 0 (10→11) taken 1 times.
✓ Branch 1 (10→12) taken 4 times.
|
5 | if (chdir(dir) != 0) { |
327 | 1 | return error_msg_errno(ebuf, "changing directory failed"); | |
328 | } | ||
329 | |||
330 |
1/2✓ Branch 0 (12→13) taken 4 times.
✗ Branch 1 (12→17) not taken.
|
4 | if (likely(cwd)) { |
331 | 4 | int r = setenv("OLDPWD", cwd, 1); | |
332 |
1/2✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→17) taken 4 times.
|
4 | if (unlikely(r != 0)) { |
333 | ✗ | LOG_WARNING("failed to set OLDPWD: %s", strerror(errno)); | |
334 | } | ||
335 | } | ||
336 | |||
337 | 4 | cwd = getcwd(buf, sizeof(buf)); | |
338 |
1/2✓ Branch 0 (18→19) taken 4 times.
✗ Branch 1 (18→23) not taken.
|
4 | if (likely(cwd)) { |
339 | 4 | int r = setenv("PWD", cwd, 1); | |
340 |
1/2✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→23) taken 4 times.
|
4 | if (unlikely(r != 0)) { |
341 | ✗ | LOG_WARNING("failed to set PWD: %s", strerror(errno)); | |
342 | } | ||
343 | } | ||
344 | |||
345 |
2/2✓ Branch 0 (26→24) taken 136 times.
✓ Branch 1 (26→27) taken 4 times.
|
140 | for (size_t i = 0, n = e->buffers.count; i < n; i++) { |
346 | 136 | Buffer *buffer = e->buffers.ptrs[i]; | |
347 | 136 | buffer_update_short_filename_cwd(buffer, &e->home_dir, cwd); | |
348 | } | ||
349 | |||
350 | 4 | frame_for_each_window(e->root_frame, mark_tabbar_changed, NULL); | |
351 | 4 | e->screen_update |= UPDATE_TERM_TITLE; | |
352 | |||
353 | 4 | bool verbose = has_flag(a, 'v'); | |
354 |
1/6✗ Branch 0 (29→30) not taken.
✓ Branch 1 (29→35) taken 4 times.
✗ Branch 2 (30→31) not taken.
✗ Branch 3 (30→32) not taken.
✗ Branch 4 (33→34) not taken.
✗ Branch 5 (33→35) not taken.
|
4 | return !verbose || info_msg(ebuf, "changed directory to: %s", cwd ? cwd : dir); |
355 | } | ||
356 | |||
357 | ✗ | static bool cmd_center_view(EditorState *e, const CommandArgs *a) | |
358 | { | ||
359 | ✗ | BUG_ON(a->nr_args); | |
360 | ✗ | e->view->force_center = true; | |
361 | ✗ | return true; | |
362 | } | ||
363 | |||
364 | 4 | static bool cmd_clear(EditorState *e, const CommandArgs *a) | |
365 | { | ||
366 | 4 | char iflag = cmdargs_pick_winning_flag(a, "iI"); | |
367 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 4 times.
|
4 | bool auto_indent = iflag ? (iflag == 'i') : e->buffer->options.auto_indent; |
368 | 4 | clear_lines(e->view, auto_indent); | |
369 | 4 | return true; | |
370 | } | ||
371 | |||
372 | 25 | static bool cmd_close(EditorState *e, const CommandArgs *a) | |
373 | { | ||
374 | 25 | bool force = has_flag(a, 'f'); | |
375 |
4/4✓ Branch 0 (3→4) taken 18 times.
✓ Branch 1 (3→13) taken 7 times.
✓ Branch 2 (5→6) taken 2 times.
✓ Branch 3 (5→13) taken 16 times.
|
25 | if (!force && !view_can_close(e->view)) { |
376 | 2 | bool prompt = has_flag(a, 'p'); | |
377 |
2/2✓ Branch 0 (7→8) taken 1 times.
✓ Branch 1 (7→9) taken 1 times.
|
2 | if (!prompt) { |
378 | 1 | return error_msg ( | |
379 | &e->err, | ||
380 | "The buffer is modified; " | ||
381 | "save or run 'close -f' to close without saving" | ||
382 | ); | ||
383 | } | ||
384 | |||
385 |
1/2✓ Branch 0 (9→10) taken 1 times.
✗ Branch 1 (9→11) not taken.
|
1 | if (unlikely(e->flags & EFLAG_HEADLESS)) { |
386 | 1 | return error_msg(&e->err, "-p flag unavailable in headless mode"); | |
387 | } | ||
388 | |||
389 | ✗ | static const char str[] = "Close without saving changes? [y/N]"; | |
390 | ✗ | if (dialog_prompt(e, str, "ny") != 'y') { | |
391 | return false; | ||
392 | } | ||
393 | } | ||
394 | |||
395 | 23 | bool allow_quit = has_flag(a, 'q'); | |
396 |
1/6✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→18) taken 23 times.
✗ Branch 2 (15→16) not taken.
✗ Branch 3 (15→18) not taken.
✗ Branch 4 (16→17) not taken.
✗ Branch 5 (16→18) not taken.
|
23 | if (allow_quit && e->buffers.count == 1 && e->root_frame->frames.count <= 1) { |
397 | ✗ | e->status = EDITOR_EXIT_OK; | |
398 | ✗ | return true; | |
399 | } | ||
400 | |||
401 | 23 | bool allow_wclose = has_flag(a, 'w'); | |
402 |
1/4✗ Branch 0 (19→20) not taken.
✓ Branch 1 (19→23) taken 23 times.
✗ Branch 2 (20→21) not taken.
✗ Branch 3 (20→23) not taken.
|
23 | if (allow_wclose && e->window->views.count <= 1) { |
403 | ✗ | window_close(e->window); | |
404 | ✗ | return true; | |
405 | } | ||
406 | |||
407 | 23 | window_close_current_view(e->window); | |
408 | 23 | set_view(e->window->view); | |
409 | 23 | return true; | |
410 | } | ||
411 | |||
412 | ✗ | static bool cmd_command(EditorState *e, const CommandArgs *a) | |
413 | { | ||
414 | ✗ | const char *text = a->args[0]; | |
415 | ✗ | push_input_mode(e, e->command_mode); | |
416 | ✗ | if (text) { | |
417 | ✗ | cmdline_set_text(&e->cmdline, text); | |
418 | } | ||
419 | ✗ | return true; | |
420 | } | ||
421 | |||
422 | 1 | static bool cmd_compile(EditorState *e, const CommandArgs *a) | |
423 | { | ||
424 | 1 | Compiler *compiler = find_compiler(&e->compilers, a->args[0]); | |
425 |
1/2✓ Branch 0 (3→4) taken 1 times.
✗ Branch 1 (3→5) not taken.
|
1 | if (unlikely(!compiler)) { |
426 | 1 | return error_msg(&e->err, "No such error parser %s", a->args[0]); | |
427 | } | ||
428 | |||
429 | ✗ | bool quiet = has_flag(a, 's'); | |
430 | ✗ | if ((e->flags & EFLAG_HEADLESS) && !quiet) { | |
431 | ✗ | LOG_INFO("automatically added -s flag to compile command (headless mode)"); | |
432 | ✗ | quiet = true; | |
433 | } | ||
434 | |||
435 | ✗ | SpawnContext ctx = { | |
436 | ✗ | .argv = (const char **)a->args + 1, | |
437 | ✗ | .ebuf = &e->err, | |
438 | .quiet = quiet, | ||
439 | }; | ||
440 | |||
441 | ✗ | MessageArray *messages = &e->messages; | |
442 | ✗ | clear_messages(messages); | |
443 | |||
444 | ✗ | yield_terminal(e, quiet); | |
445 | ✗ | bool prompt = has_flag(a, 'p'); | |
446 | ✗ | bool read_stdout = has_flag(a, '1'); | |
447 | ✗ | bool spawned = spawn_compiler(&ctx, compiler, messages, read_stdout); | |
448 | ✗ | resume_terminal(e, quiet, spawned && prompt); | |
449 | |||
450 | ✗ | activate_current_message_save(messages, &e->bookmarks, e->view); | |
451 | ✗ | return spawned; | |
452 | } | ||
453 | |||
454 | 4 | static bool cmd_copy(EditorState *e, const CommandArgs *a) | |
455 | { | ||
456 | 4 | const char *text = a->args[0]; | |
457 | 4 | Terminal *term = &e->terminal; | |
458 | 4 | bool clipboard = has_flag(a, 'b'); | |
459 | 4 | bool primary = has_flag(a, 'p'); | |
460 |
2/4✓ Branch 0 (5→6) taken 4 times.
✗ Branch 1 (5→7) not taken.
✗ Branch 2 (6→7) not taken.
✓ Branch 3 (6→10) taken 4 times.
|
4 | bool internal = has_flag(a, 'i') || !(clipboard || primary); |
461 | ✗ | bool osc52 = (clipboard || primary) && term->features & TFLAG_OSC52_COPY; | |
462 | |||
463 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→19) taken 4 times.
|
4 | if (text) { |
464 | ✗ | size_t len = strlen(text); | |
465 | ✗ | if (internal) { | |
466 | ✗ | record_copy(&e->clipboard, xstrdup(text), len, false); | |
467 | } | ||
468 | ✗ | if (osc52) { | |
469 | ✗ | if (!term_osc52_copy(&term->obuf, text, len, clipboard, primary)) { | |
470 | ✗ | error_msg_errno(&e->err, "OSC 52 copy failed"); | |
471 | } | ||
472 | } | ||
473 | ✗ | return true; | |
474 | } | ||
475 | |||
476 | 4 | const View *view = e->view; | |
477 | 4 | BlockIter bi; | |
478 | 4 | size_t size; | |
479 | 4 | bool line_copy; | |
480 |
1/2✗ Branch 0 (19→20) not taken.
✓ Branch 1 (19→22) taken 4 times.
|
4 | if (view->selection) { |
481 | ✗ | SelectionInfo info = init_selection(view); | |
482 | ✗ | size = info.eo - info.so; | |
483 | ✗ | bi = info.si; | |
484 | ✗ | line_copy = (view->selection == SELECT_LINES); | |
485 | } else { | ||
486 | 4 | bi = view->cursor; | |
487 | 4 | block_iter_bol(&bi); | |
488 | 4 | BlockIter tmp = bi; | |
489 | 4 | size = block_iter_eat_line(&tmp); | |
490 | 4 | line_copy = true; | |
491 | } | ||
492 | |||
493 |
1/2✓ Branch 0 (25→26) taken 4 times.
✗ Branch 1 (25→40) not taken.
|
4 | if (unlikely(size == 0)) { |
494 | return true; | ||
495 | } | ||
496 | |||
497 | 4 | char *buf = block_iter_get_bytes(&bi, size); | |
498 |
1/2✗ Branch 0 (27→28) not taken.
✓ Branch 1 (27→31) taken 4 times.
|
4 | if (osc52) { |
499 | ✗ | if (!term_osc52_copy(&term->obuf, buf, size, clipboard, primary)) { | |
500 | ✗ | error_msg_errno(&e->err, "OSC 52 copy failed"); | |
501 | } | ||
502 | } | ||
503 | |||
504 |
1/2✓ Branch 0 (31→32) taken 4 times.
✗ Branch 1 (31→33) not taken.
|
4 | if (internal) { |
505 | // Clipboard takes ownership of `buf` | ||
506 | 4 | record_copy(&e->clipboard, buf, size, line_copy); | |
507 | } else { | ||
508 | ✗ | free(buf); | |
509 | } | ||
510 | |||
511 | // TODO: return false if term_osc52_copy() failed? | ||
512 |
2/4✓ Branch 0 (35→36) taken 4 times.
✗ Branch 1 (35→39) not taken.
✓ Branch 2 (37→38) taken 4 times.
✗ Branch 3 (37→39) not taken.
|
8 | return has_flag(a, 'k') || unselect(e->view); |
513 | } | ||
514 | |||
515 | 3 | static bool cmd_cursor(EditorState *e, const CommandArgs *a) | |
516 | { | ||
517 |
1/2✗ Branch 0 (2→4) not taken.
✓ Branch 1 (2→6) taken 3 times.
|
3 | if (unlikely(a->nr_args == 0)) { |
518 | // Reset all cursor styles | ||
519 | ✗ | for (CursorInputMode m = 0; m < ARRAYLEN(e->cursor_styles); m++) { | |
520 | ✗ | e->cursor_styles[m] = get_default_cursor_style(m); | |
521 | } | ||
522 | ✗ | e->screen_update |= UPDATE_CURSOR_STYLE; | |
523 | ✗ | return true; | |
524 | } | ||
525 | |||
526 | 3 | CursorInputMode mode = cursor_mode_from_str(a->args[0]); | |
527 |
2/2✓ Branch 0 (7→8) taken 1 times.
✓ Branch 1 (7→9) taken 2 times.
|
3 | if (unlikely(mode >= NR_CURSOR_MODES)) { |
528 | 1 | return error_msg(&e->err, "invalid mode argument: %s", a->args[0]); | |
529 | } | ||
530 | |||
531 | 2 | TermCursorStyle style = get_default_cursor_style(mode); | |
532 |
1/2✓ Branch 0 (9→10) taken 2 times.
✗ Branch 1 (9→13) not taken.
|
2 | if (a->nr_args >= 2) { |
533 | 2 | style.type = cursor_type_from_str(a->args[1]); | |
534 |
2/2✓ Branch 0 (11→12) taken 1 times.
✓ Branch 1 (11→13) taken 1 times.
|
2 | if (unlikely(style.type == CURSOR_INVALID)) { |
535 | 1 | return error_msg(&e->err, "invalid cursor type: %s", a->args[1]); | |
536 | } | ||
537 | } | ||
538 | |||
539 |
1/2✓ Branch 0 (13→14) taken 1 times.
✗ Branch 1 (13→17) not taken.
|
1 | if (a->nr_args >= 3) { |
540 | 1 | style.color = cursor_color_from_str(a->args[2]); | |
541 |
1/2✓ Branch 0 (15→16) taken 1 times.
✗ Branch 1 (15→17) not taken.
|
1 | if (unlikely(style.color == COLOR_INVALID)) { |
542 | 1 | return error_msg(&e->err, "invalid cursor color: %s", a->args[2]); | |
543 | } | ||
544 | } | ||
545 | |||
546 | ✗ | e->cursor_styles[mode] = style; | |
547 | ✗ | e->screen_update |= UPDATE_CURSOR_STYLE; | |
548 | ✗ | return true; | |
549 | } | ||
550 | |||
551 | 2 | static bool cmd_cut(EditorState *e, const CommandArgs *a) | |
552 | { | ||
553 | 2 | BUG_ON(a->nr_args); | |
554 | 2 | View *view = e->view; | |
555 | 2 | long preferred_x = view_get_preferred_x(view); | |
556 | 2 | size_t size; | |
557 | 2 | bool line_copy; | |
558 | |||
559 |
2/2✓ Branch 0 (5→6) taken 1 times.
✓ Branch 1 (5→8) taken 1 times.
|
2 | if (view->selection) { |
560 | 1 | line_copy = (view->selection == SELECT_LINES); | |
561 | 1 | size = prepare_selection(view); | |
562 | 1 | unselect(view); | |
563 | } else { | ||
564 | 1 | line_copy = true; | |
565 | 1 | block_iter_bol(&view->cursor); | |
566 | 1 | BlockIter tmp = view->cursor; | |
567 | 1 | size = block_iter_eat_line(&tmp); | |
568 | } | ||
569 | |||
570 |
1/2✓ Branch 0 (11→12) taken 2 times.
✗ Branch 1 (11→17) not taken.
|
2 | if (size == 0) { |
571 | return true; | ||
572 | } | ||
573 | |||
574 | 2 | char *buf = block_iter_get_bytes(&view->cursor, size); | |
575 | 2 | record_copy(&e->clipboard, buf, size, line_copy); | |
576 | 2 | buffer_delete_bytes(view, size); | |
577 | |||
578 |
2/2✓ Branch 0 (15→16) taken 1 times.
✓ Branch 1 (15→17) taken 1 times.
|
2 | if (line_copy) { |
579 | 1 | move_to_preferred_x(view, preferred_x); | |
580 | } | ||
581 | |||
582 | return true; | ||
583 | } | ||
584 | |||
585 | 9 | static bool cmd_def_mode(EditorState *e, const CommandArgs *a) | |
586 | { | ||
587 | 9 | const char *name = a->args[0]; | |
588 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→4) taken 7 times.
|
9 | if (name[0] == '\0' || name[0] == '-' ) { |
589 | 2 | return error_msg(&e->err, "mode name can't be empty or start with '-'"); | |
590 | } | ||
591 | |||
592 | 7 | HashMap *modes = &e->modes; | |
593 |
2/2✓ Branch 0 (5→6) taken 4 times.
✓ Branch 1 (5→7) taken 3 times.
|
7 | if (hashmap_get(modes, name)) { |
594 | 4 | return error_msg(&e->err, "mode '%s' already exists", name); | |
595 | } | ||
596 | |||
597 | 3 | PointerArray ftmodes = PTR_ARRAY_INIT; | |
598 |
2/2✓ Branch 0 (17→8) taken 2 times.
✓ Branch 1 (17→18) taken 1 times.
|
3 | for (size_t i = 1, n = a->nr_args; i < n; i++) { |
599 | 2 | const char *ftname = a->args[i]; | |
600 | 2 | ModeHandler *mode = get_mode_handler(modes, ftname); | |
601 |
2/2✓ Branch 0 (9→10) taken 1 times.
✓ Branch 1 (9→12) taken 1 times.
|
2 | if (unlikely(!mode)) { |
602 | 1 | ptr_array_free_array(&ftmodes); | |
603 | 1 | return error_msg(&e->err, "unknown fallthrough mode '%s'", ftname); | |
604 | } | ||
605 |
1/2✓ Branch 0 (12→13) taken 1 times.
✗ Branch 1 (12→15) not taken.
|
1 | if (unlikely(mode->cmds != &normal_commands)) { |
606 | // TODO: Support "command" and "search" as fallback modes? | ||
607 | // If implemented, all involved modes need to use the same | ||
608 | // `CommandSet`. | ||
609 | 1 | ptr_array_free_array(&ftmodes); | |
610 | 1 | return error_msg(&e->err, "unable to use '%s' as fall-through mode", ftname); | |
611 | } | ||
612 | ✗ | ptr_array_append(&ftmodes, mode); | |
613 | } | ||
614 | |||
615 | 1 | static const FlagMapping map[] = { | |
616 | {'u', MHF_NO_TEXT_INSERTION}, | ||
617 | {'U', MHF_NO_TEXT_INSERTION | MHF_NO_TEXT_INSERTION_RECURSIVE}, | ||
618 | }; | ||
619 | |||
620 | 1 | ModeHandler *mode = new_mode(modes, xstrdup(name), &normal_commands); | |
621 | 1 | mode->flags = cmdargs_convert_flags(a, map, ARRAYLEN(map)); | |
622 | 1 | mode->fallthrough_modes = ftmodes; | |
623 | 1 | return true; | |
624 | } | ||
625 | |||
626 | 9 | static bool cmd_delete(EditorState *e, const CommandArgs *a) | |
627 | { | ||
628 | 9 | BUG_ON(a->nr_args); | |
629 | 9 | delete_ch(e->view); | |
630 | 9 | return true; | |
631 | } | ||
632 | |||
633 | 2 | static bool cmd_delete_eol(EditorState *e, const CommandArgs *a) | |
634 | { | ||
635 | 2 | View *view = e->view; | |
636 |
1/2✓ Branch 0 (2→3) taken 2 times.
✗ Branch 1 (2→14) not taken.
|
2 | if (view->selection) { |
637 | // TODO: return false? | ||
638 | return true; | ||
639 | } | ||
640 | |||
641 | 2 | bool delete_newline_if_at_eol = has_flag(a, 'n'); | |
642 | 2 | BlockIter bi = view->cursor; | |
643 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→11) taken 2 times.
|
2 | if (delete_newline_if_at_eol) { |
644 | ✗ | CodePoint ch; | |
645 | ✗ | if (block_iter_get_char(&view->cursor, &ch) == 1 && ch == '\n') { | |
646 | ✗ | delete_ch(view); | |
647 | ✗ | return true; | |
648 | } | ||
649 | } | ||
650 | |||
651 | 2 | buffer_delete_bytes(view, block_iter_eol(&bi)); | |
652 | 2 | return true; | |
653 | } | ||
654 | |||
655 | ✗ | static bool cmd_delete_line(EditorState *e, const CommandArgs *a) | |
656 | { | ||
657 | ✗ | BUG_ON(a->nr_args); | |
658 | ✗ | View *view = e->view; | |
659 | ✗ | long x = view_get_preferred_x(view); | |
660 | ✗ | bool whole_lines = true; | |
661 | ✗ | size_t del_count; | |
662 | |||
663 | ✗ | if (view->selection) { | |
664 | ✗ | whole_lines = !has_flag(a, 'S'); | |
665 | ✗ | view->selection = whole_lines ? SELECT_LINES : view->selection; | |
666 | ✗ | del_count = prepare_selection(view); | |
667 | ✗ | unselect(view); | |
668 | } else { | ||
669 | ✗ | block_iter_bol(&view->cursor); | |
670 | ✗ | BlockIter tmp = view->cursor; | |
671 | ✗ | del_count = block_iter_eat_line(&tmp); | |
672 | } | ||
673 | |||
674 | ✗ | buffer_delete_bytes(view, del_count); | |
675 | ✗ | if (whole_lines) { | |
676 | ✗ | move_to_preferred_x(view, x); | |
677 | } | ||
678 | |||
679 | ✗ | return true; | |
680 | } | ||
681 | |||
682 | 1 | static bool cmd_delete_word(EditorState *e, const CommandArgs *a) | |
683 | { | ||
684 | 1 | bool skip_non_word = has_flag(a, 's'); | |
685 | 1 | BlockIter bi = e->view->cursor; | |
686 | 1 | buffer_delete_bytes(e->view, word_fwd(&bi, skip_non_word)); | |
687 | 1 | return true; | |
688 | } | ||
689 | |||
690 | 11 | static bool cmd_down(EditorState *e, const CommandArgs *a) | |
691 | { | ||
692 | 11 | handle_selection_flags(e->view, a); | |
693 | 11 | move_down(e->view, 1); | |
694 | 11 | return true; | |
695 | } | ||
696 | |||
697 | 6 | static bool cmd_eof(EditorState *e, const CommandArgs *a) | |
698 | { | ||
699 | 6 | handle_selection_flags(e->view, a); | |
700 | 6 | move_eof(e->view); | |
701 | 6 | return true; | |
702 | } | ||
703 | |||
704 | 6 | static bool cmd_eol(EditorState *e, const CommandArgs *a) | |
705 | { | ||
706 | 6 | handle_selection_flags(e->view, a); | |
707 | 6 | move_eol(e->view); | |
708 | 6 | return true; | |
709 | } | ||
710 | |||
711 | 1 | static bool cmd_eolsf(EditorState *e, const CommandArgs *a) | |
712 | { | ||
713 | 1 | BUG_ON(a->nr_args); | |
714 | 1 | View *view = e->view; | |
715 | 1 | handle_selection_flags(view, a); | |
716 | |||
717 |
1/2✓ Branch 0 (6→7) taken 1 times.
✗ Branch 1 (6→11) not taken.
|
1 | if (!block_iter_eol(&view->cursor)) { |
718 | 1 | Window *window = e->window; | |
719 | 1 | long margin = window_get_scroll_margin(window); | |
720 | 1 | long bottom = view->vy + window->edit_h - 1 - margin; | |
721 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 1 times.
|
1 | if (view->cy < bottom) { |
722 | ✗ | move_down(view, bottom - view->cy); | |
723 | } else { | ||
724 | 1 | block_iter_eof(&view->cursor); | |
725 | } | ||
726 | } | ||
727 | |||
728 | 1 | view_reset_preferred_x(view); | |
729 | 1 | return true; | |
730 | } | ||
731 | |||
732 | 6 | static bool cmd_erase(EditorState *e, const CommandArgs *a) | |
733 | { | ||
734 | 6 | BUG_ON(a->nr_args); | |
735 | 6 | erase(e->view); | |
736 | 6 | return true; | |
737 | } | ||
738 | |||
739 | 1 | static bool cmd_erase_bol(EditorState *e, const CommandArgs *a) | |
740 | { | ||
741 | 1 | BUG_ON(a->nr_args); | |
742 | 1 | buffer_erase_bytes(e->view, block_iter_bol(&e->view->cursor)); | |
743 | 1 | return true; | |
744 | } | ||
745 | |||
746 | 2 | static bool cmd_erase_word(EditorState *e, const CommandArgs *a) | |
747 | { | ||
748 | 2 | View *view = e->view; | |
749 | 2 | bool skip_non_word = has_flag(a, 's'); | |
750 | 2 | buffer_erase_bytes(view, word_bwd(&view->cursor, skip_non_word)); | |
751 | 2 | return true; | |
752 | } | ||
753 | |||
754 | 212 | static bool cmd_errorfmt(EditorState *e, const CommandArgs *a) | |
755 | { | ||
756 | 212 | BUG_ON(a->nr_args == 0 || a->nr_args > 2 + ERRORFMT_CAPTURE_MAX); | |
757 | 212 | const char *name = a->args[0]; | |
758 |
2/2✓ Branch 0 (4→5) taken 21 times.
✓ Branch 1 (4→7) taken 191 times.
|
212 | if (a->nr_args == 1) { |
759 | 21 | remove_compiler(&e->compilers, name); | |
760 | 21 | return true; | |
761 | } | ||
762 | |||
763 | 191 | static_assert(NR_ERRFMT_INDICES == 4); | |
764 | 191 | size_t max_idx = 0; | |
765 | 191 | int8_t indices[NR_ERRFMT_INDICES] = { | |
766 | [ERRFMT_FILE] = -1, | ||
767 | [ERRFMT_LINE] = -1, | ||
768 | [ERRFMT_COLUMN] = -1, | ||
769 | [ERRFMT_MESSAGE] = 0, | ||
770 | }; | ||
771 | |||
772 |
2/2✓ Branch 0 (15→8) taken 227 times.
✓ Branch 1 (15→16) taken 190 times.
|
417 | for (size_t i = 0, n = a->nr_args - 2; i < n; i++) { |
773 | 227 | char *cap_name = a->args[i + 2]; | |
774 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 227 times.
|
227 | if (streq(cap_name, "_")) { |
775 | ✗ | continue; | |
776 | } | ||
777 | 227 | ssize_t cap_idx = errorfmt_capture_name_to_index(cap_name); | |
778 |
2/2✓ Branch 0 (11→12) taken 1 times.
✓ Branch 1 (11→13) taken 226 times.
|
227 | if (unlikely(cap_idx < 0)) { |
779 | 1 | return error_msg(&e->err, "unknown substring name %s", cap_name); | |
780 | } | ||
781 | 226 | max_idx = i + 1; | |
782 | 226 | indices[cap_idx] = max_idx; | |
783 | } | ||
784 | |||
785 | |||
786 | 190 | const char *pattern = a->args[1]; | |
787 | 190 | regex_t re; | |
788 |
1/2✓ Branch 0 (17→18) taken 190 times.
✗ Branch 1 (17→24) not taken.
|
190 | if (unlikely(!regexp_compile(&e->err, &re, pattern, 0))) { |
789 | return false; | ||
790 | } | ||
791 | |||
792 |
2/2✓ Branch 0 (18→19) taken 1 times.
✓ Branch 1 (18→21) taken 189 times.
|
190 | if (unlikely(max_idx > re.re_nsub)) { |
793 | 1 | regfree(&re); | |
794 | 1 | return error_msg(&e->err, "invalid substring count"); | |
795 | } | ||
796 | |||
797 | 189 | bool ignore = has_flag(a, 'i'); | |
798 | 189 | add_error_fmt(&e->compilers, name, pattern, &re, indices, ignore); | |
799 | 189 | return true; | |
800 | } | ||
801 | |||
802 | 18 | static bool cmd_exec(EditorState *e, const CommandArgs *a) | |
803 | { | ||
804 | 18 | ExecAction actions[3] = {EXEC_TTY, EXEC_TTY, EXEC_TTY}; | |
805 | 18 | ExecFlags exec_flags = 0; | |
806 | 18 | bool lflag = false; | |
807 | 18 | bool move_after_insert = false; | |
808 | |||
809 |
2/2✓ Branch 0 (18→3) taken 36 times.
✓ Branch 1 (18→19) taken 15 times.
|
51 | for (size_t i = 0, n = a->nr_flags, argidx = 0, fd; i < n; i++) { |
810 |
4/10✓ Branch 0 (3→4) taken 4 times.
✓ Branch 1 (3→5) taken 6 times.
✗ Branch 2 (3→6) not taken.
✓ Branch 3 (3→7) taken 18 times.
✗ Branch 4 (3→8) not taken.
✗ Branch 5 (3→9) not taken.
✗ Branch 6 (3→10) not taken.
✗ Branch 7 (3→11) not taken.
✗ Branch 8 (3→12) not taken.
✓ Branch 9 (3→13) taken 8 times.
|
36 | switch (a->flags[i]) { |
811 | case 'e': fd = STDERR_FILENO; break; | ||
812 | 4 | case 'i': fd = STDIN_FILENO; break; | |
813 | 6 | case 'o': fd = STDOUT_FILENO; break; | |
814 | ✗ | case 'p': exec_flags |= EXECFLAG_PROMPT; continue; | |
815 | 18 | case 's': exec_flags |= EXECFLAG_QUIET; continue; | |
816 | ✗ | case 't': exec_flags &= ~EXECFLAG_QUIET; continue; | |
817 | ✗ | case 'l': lflag = true; continue; | |
818 | ✗ | case 'm': move_after_insert = true; continue; | |
819 | ✗ | case 'n': exec_flags |= EXECFLAG_STRIP_NL; continue; | |
820 | ✗ | default: BUG("unexpected flag"); return false; | |
821 | } | ||
822 | 18 | const char *action_name = a->args[argidx++]; | |
823 | 18 | ExecAction action = lookup_exec_action(action_name, fd); | |
824 |
2/2✓ Branch 0 (14→15) taken 3 times.
✓ Branch 1 (14→16) taken 15 times.
|
18 | if (unlikely(action == EXEC_INVALID)) { |
825 | 3 | return error_msg(&e->err, "invalid action for -%c: '%s'", a->flags[i], action_name); | |
826 | } | ||
827 | 15 | actions[fd] = action; | |
828 | } | ||
829 | |||
830 |
1/4✗ Branch 0 (19→20) not taken.
✓ Branch 1 (19→22) taken 15 times.
✗ Branch 2 (20→21) not taken.
✗ Branch 3 (20→22) not taken.
|
15 | if (lflag && actions[STDIN_FILENO] == EXEC_BUFFER) { |
831 | // For compat. with old "filter" and "pipe-to" commands | ||
832 | ✗ | actions[STDIN_FILENO] = EXEC_LINE; | |
833 | } | ||
834 | |||
835 |
2/4✓ Branch 0 (22→23) taken 15 times.
✗ Branch 1 (22→26) not taken.
✗ Branch 2 (23→24) not taken.
✓ Branch 3 (23→26) taken 15 times.
|
15 | if ((e->flags & EFLAG_HEADLESS) && !(exec_flags & EXECFLAG_QUIET)) { |
836 | ✗ | LOG_INFO("automatically added -s flag to exec command (headless mode)"); | |
837 | ✗ | exec_flags |= EXECFLAG_QUIET; | |
838 | } | ||
839 | |||
840 | 15 | const char **argv = (const char **)a->args + a->nr_flag_args; | |
841 | 15 | ssize_t outlen = handle_exec(e, argv, actions, exec_flags); | |
842 |
2/2✓ Branch 0 (27→28) taken 10 times.
✓ Branch 1 (27→29) taken 5 times.
|
15 | if (outlen <= 0) { |
843 | 10 | return outlen == 0; | |
844 | } | ||
845 | |||
846 |
1/4✗ Branch 0 (29→30) not taken.
✓ Branch 1 (29→32) taken 5 times.
✗ Branch 2 (30→31) not taken.
✗ Branch 3 (30→32) not taken.
|
5 | if (move_after_insert && actions[STDOUT_FILENO] == EXEC_BUFFER) { |
847 | ✗ | block_iter_skip_bytes(&e->view->cursor, outlen); | |
848 | } | ||
849 | return true; | ||
850 | } | ||
851 | |||
852 | 16 | static bool cmd_ft(EditorState *e, const CommandArgs *a) | |
853 | { | ||
854 | 16 | ErrorBuffer *ebuf = &e->err; | |
855 | 16 | char **args = a->args; | |
856 | 16 | const char *filetype = args[0]; | |
857 |
2/2✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→4) taken 15 times.
|
16 | if (unlikely(!is_valid_filetype_name(filetype))) { |
858 | 1 | return error_msg(ebuf, "Invalid filetype name: '%s'", filetype); | |
859 | } | ||
860 | |||
861 | 15 | FileDetectionType dt = FT_EXTENSION; | |
862 |
2/5✗ Branch 0 (4→5) not taken.
✗ Branch 1 (4→6) not taken.
✓ Branch 2 (4→7) taken 14 times.
✗ Branch 3 (4→8) not taken.
✓ Branch 4 (4→9) taken 1 times.
|
15 | switch (last_flag(a)) { |
863 | ✗ | case 'b': dt = FT_BASENAME; break; | |
864 | ✗ | case 'c': dt = FT_CONTENT; break; | |
865 | 14 | case 'f': dt = FT_FILENAME; break; | |
866 | ✗ | case 'i': dt = FT_INTERPRETER; break; | |
867 | } | ||
868 | |||
869 | 15 | PointerArray *filetypes = &e->filetypes; | |
870 | 15 | size_t nfailed = 0; | |
871 |
2/2✓ Branch 0 (14→10) taken 15 times.
✓ Branch 1 (14→15) taken 15 times.
|
30 | for (size_t i = 1, n = a->nr_args; i < n; i++) { |
872 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 15 times.
|
15 | if (!add_filetype(filetypes, filetype, args[i], dt, ebuf)) { |
873 | ✗ | nfailed++; | |
874 | } | ||
875 | } | ||
876 | |||
877 | 15 | return nfailed == 0; | |
878 | } | ||
879 | |||
880 | 648 | static bool cmd_hi(EditorState *e, const CommandArgs *a) | |
881 | { | ||
882 |
2/2✓ Branch 0 (2→3) taken 7 times.
✓ Branch 1 (2→5) taken 641 times.
|
648 | if (unlikely(a->nr_args == 0)) { |
883 | 7 | exec_builtin_color_reset(e); | |
884 | 7 | goto update; | |
885 | } | ||
886 | |||
887 | 641 | char **strs = a->args + 1; | |
888 | 641 | size_t strs_len = a->nr_args - 1; | |
889 | 641 | TermStyle style; | |
890 | 641 | ssize_t n = parse_term_style(&style, strs, strs_len); | |
891 |
2/2✓ Branch 0 (6→7) taken 2 times.
✓ Branch 1 (6→12) taken 639 times.
|
641 | if (unlikely(n != strs_len)) { |
892 |
2/2✓ Branch 0 (7→8) taken 1 times.
✓ Branch 1 (7→9) taken 1 times.
|
2 | if (n < 0) { |
893 | 1 | return error_msg(&e->err, "too many colors"); | |
894 | } | ||
895 | 1 | BUG_ON(n > strs_len); | |
896 | 1 | return error_msg(&e->err, "invalid color or attribute: '%s'", strs[n]); | |
897 | } | ||
898 | |||
899 | 639 | TermFeatureFlags features = e->terminal.features; | |
900 | 639 | bool true_color = !!(features & TFLAG_TRUE_COLOR); | |
901 |
1/4✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 639 times.
✗ Branch 2 (13→14) not taken.
✗ Branch 3 (13→15) not taken.
|
639 | bool optimize = (true_color && e->options.optimize_true_color); |
902 | 639 | int32_t fg = color_to_nearest(style.fg, features, optimize); | |
903 | 639 | int32_t bg = color_to_nearest(style.bg, features, optimize); | |
904 |
7/8✓ Branch 0 (17→18) taken 639 times.
✗ Branch 1 (17→22) not taken.
✓ Branch 2 (19→20) taken 168 times.
✓ Branch 3 (19→22) taken 471 times.
✓ Branch 4 (20→21) taken 44 times.
✓ Branch 5 (20→26) taken 124 times.
✓ Branch 6 (21→22) taken 20 times.
✓ Branch 7 (21→26) taken 24 times.
|
639 | if (!true_color && has_flag(a, 'c') && (fg != style.fg || bg != style.bg)) { |
905 | return true; | ||
906 | } | ||
907 | |||
908 | 491 | style.fg = fg; | |
909 | 491 | style.bg = bg; | |
910 | 491 | set_highlight_style(&e->styles, a->args[0], &style); | |
911 | |||
912 | 498 | update: | |
913 | // Don't call update_all_syntax_styles() needlessly; it's called | ||
914 | // right after config has been loaded | ||
915 |
1/2✗ Branch 0 (23→24) not taken.
✓ Branch 1 (23→26) taken 498 times.
|
498 | if (e->status != EDITOR_INITIALIZING) { |
916 | ✗ | update_all_syntax_styles(&e->syntaxes, &e->styles); | |
917 | ✗ | e->screen_update |= UPDATE_ALL_WINDOWS; | |
918 | } | ||
919 | |||
920 | return true; | ||
921 | } | ||
922 | |||
923 | 96 | static bool cmd_include(EditorState *e, const CommandArgs *a) | |
924 | { | ||
925 | 96 | ConfigFlags flags = has_flag(a, 'q') ? CFG_NOFLAGS : CFG_MUST_EXIST; | |
926 |
2/2✓ Branch 0 (4→5) taken 29 times.
✓ Branch 1 (4→6) taken 67 times.
|
96 | if (has_flag(a, 'b')) { |
927 | 29 | flags |= CFG_BUILTIN; | |
928 | } | ||
929 | |||
930 | 96 | int err = read_normal_config(e, a->args[0], flags); | |
931 | // TODO: Clean up read_normal_config() so this can be simplified to `err == 0` | ||
932 |
5/6✓ Branch 0 (7→8) taken 2 times.
✓ Branch 1 (7→11) taken 94 times.
✓ Branch 2 (8→9) taken 1 times.
✓ Branch 3 (8→10) taken 1 times.
✓ Branch 4 (9→10) taken 1 times.
✗ Branch 5 (9→11) not taken.
|
96 | return err == 0 || (err == ENOENT && !(flags & CFG_MUST_EXIST)); |
933 | } | ||
934 | |||
935 | 84 | static bool cmd_insert(EditorState *e, const CommandArgs *a) | |
936 | { | ||
937 | 84 | const char *str = a->args[0]; | |
938 |
2/2✓ Branch 0 (3→6) taken 20 times.
✓ Branch 1 (3→7) taken 64 times.
|
84 | if (has_flag(a, 'k')) { |
939 |
2/2✓ Branch 0 (6→4) taken 142 times.
✓ Branch 1 (6→10) taken 20 times.
|
162 | for (size_t i = 0; str[i]; i++) { |
940 | 142 | insert_ch(e->view, str[i]); | |
941 | } | ||
942 | return true; | ||
943 | } | ||
944 | |||
945 | 64 | bool move_after = has_flag(a, 'm'); | |
946 | 64 | insert_text(e->view, str, strlen(str), move_after); | |
947 | 64 | return true; | |
948 | } | ||
949 | |||
950 | 16 | static bool cmd_join(EditorState *e, const CommandArgs *a) | |
951 | { | ||
952 |
2/2✓ Branch 0 (2→3) taken 14 times.
✓ Branch 1 (2→4) taken 2 times.
|
16 | const char *delim = a->args[0] ? a->args[0] : " "; |
953 | 16 | join_lines(e->view, delim, strlen(delim)); | |
954 | 16 | return true; | |
955 | } | ||
956 | |||
957 | 16 | static bool cmd_left(EditorState *e, const CommandArgs *a) | |
958 | { | ||
959 | 16 | handle_selection_flags(e->view, a); | |
960 | 16 | move_cursor_left(e->view); | |
961 | 16 | return true; | |
962 | } | ||
963 | |||
964 | 4 | static bool cmd_line(EditorState *e, const CommandArgs *a) | |
965 | { | ||
966 | 4 | const char *str = a->args[0]; | |
967 | 4 | size_t line, column; | |
968 |
2/2✓ Branch 0 (3→4) taken 3 times.
✓ Branch 1 (3→5) taken 1 times.
|
4 | if (unlikely(!str_to_xfilepos(str, &line, &column))) { |
969 | 3 | return error_msg(&e->err, "Invalid line number: %s", str); | |
970 | } | ||
971 | |||
972 | 1 | View *view = e->view; | |
973 | 1 | long x = view_get_preferred_x(view); | |
974 | 1 | handle_selection_flags(view, a); | |
975 | |||
976 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 1 times.
|
1 | if (column >= 1) { |
977 | // Column was specified; move to exact position | ||
978 | ✗ | move_to_filepos(view, line, column); | |
979 | } else { | ||
980 | // Column was omitted; move to line while preserving current column | ||
981 | 1 | move_to_line(view, line); | |
982 | 1 | move_to_preferred_x(view, x); | |
983 | } | ||
984 | |||
985 | return true; | ||
986 | } | ||
987 | |||
988 | 2 | static bool cmd_macro(EditorState *e, const CommandArgs *a) | |
989 | { | ||
990 | 2 | MacroRecorder *m = &e->macro; | |
991 | 2 | const char *action = a->args[0]; | |
992 | |||
993 |
3/4✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→4) taken 1 times.
✗ Branch 2 (3→4) not taken.
✓ Branch 3 (3→9) taken 1 times.
|
2 | if (streq(action, "play") || streq(action, "run")) { |
994 |
2/2✓ Branch 0 (8→5) taken 9 times.
✓ Branch 1 (8→30) taken 1 times.
|
10 | for (size_t i = 0, n = m->macro.count; i < n; i++) { |
995 | 9 | const char *cmd_str = m->macro.ptrs[i]; | |
996 |
1/2✓ Branch 0 (6→7) taken 9 times.
✗ Branch 1 (6→30) not taken.
|
9 | if (!handle_normal_command(e, cmd_str, false)) { |
997 | return false; | ||
998 | } | ||
999 | } | ||
1000 | return true; | ||
1001 | } | ||
1002 | |||
1003 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→12) taken 1 times.
|
1 | if (streq(action, "toggle")) { |
1004 | ✗ | action = m->recording ? "stop" : "record"; | |
1005 | } | ||
1006 | |||
1007 |
1/2✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→17) taken 1 times.
|
1 | if (streq(action, "record")) { |
1008 | ✗ | bool r = macro_record(m); | |
1009 | ✗ | return info_msg(&e->err, "%s", r ? "Recording macro" : "Already recording"); | |
1010 | } | ||
1011 | |||
1012 |
1/2✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→24) taken 1 times.
|
1 | if (streq(action, "stop")) { |
1013 | ✗ | if (!macro_stop(m)) { | |
1014 | ✗ | return info_msg(&e->err, "Not recording"); | |
1015 | } | ||
1016 | ✗ | size_t count = m->macro.count; | |
1017 | ✗ | const char *plural = (count != 1) ? "s" : ""; | |
1018 | ✗ | return info_msg(&e->err, "Macro recording stopped; %zu command%s saved", count, plural); | |
1019 | } | ||
1020 | |||
1021 |
1/2✗ Branch 0 (24→25) not taken.
✓ Branch 1 (24→29) taken 1 times.
|
1 | if (streq(action, "cancel")) { |
1022 | ✗ | bool r = macro_cancel(m); | |
1023 | ✗ | return info_msg(&e->err, "%s", r ? "Macro recording cancelled" : "Not recording"); | |
1024 | } | ||
1025 | |||
1026 | 1 | return error_msg(&e->err, "Unknown action '%s'", action); | |
1027 | } | ||
1028 | |||
1029 | 5 | static bool cmd_match_bracket(EditorState *e, const CommandArgs *a) | |
1030 | { | ||
1031 | 5 | BUG_ON(a->nr_args); | |
1032 | 5 | View *view = e->view; | |
1033 | 5 | CodePoint cursor_char; | |
1034 |
2/2✓ Branch 0 (5→6) taken 1 times.
✓ Branch 1 (5→7) taken 4 times.
|
5 | if (!block_iter_get_char(&view->cursor, &cursor_char)) { |
1035 | 1 | return error_msg(&e->err, "No character under cursor"); | |
1036 | } | ||
1037 | |||
1038 | 4 | CodePoint target = cursor_char; | |
1039 | 4 | BlockIter bi = view->cursor; | |
1040 | 4 | size_t level = 0; | |
1041 | 4 | CodePoint u = 0; | |
1042 | |||
1043 |
4/5✓ Branch 0 (7→8) taken 1 times.
✓ Branch 1 (7→9) taken 1 times.
✓ Branch 2 (7→11) taken 1 times.
✗ Branch 3 (7→12) not taken.
✓ Branch 4 (7→13) taken 1 times.
|
4 | switch (cursor_char) { |
1044 | 1 | case '<': | |
1045 | case '[': | ||
1046 | case '{': | ||
1047 | 1 | target++; | |
1048 | // Fallthrough | ||
1049 | 2 | case '(': | |
1050 | 2 | target++; | |
1051 | 2 | goto search_fwd; | |
1052 | 1 | case '>': | |
1053 | case ']': | ||
1054 | case '}': | ||
1055 | 1 | target--; | |
1056 | // Fallthrough | ||
1057 | 1 | case ')': | |
1058 | 1 | target--; | |
1059 | 1 | goto search_bwd; | |
1060 | 1 | default: | |
1061 | 1 | return error_msg(&e->err, "Character under cursor not matchable"); | |
1062 | } | ||
1063 | |||
1064 | 2 | search_fwd: | |
1065 | 2 | block_iter_next_char(&bi, &u); | |
1066 | 2 | BUG_ON(u != cursor_char); | |
1067 |
2/2✓ Branch 0 (24→15) taken 46 times.
✓ Branch 1 (24→25) taken 1 times.
|
47 | while (block_iter_next_char(&bi, &u)) { |
1068 |
2/2✓ Branch 0 (15→16) taken 2 times.
✓ Branch 1 (15→20) taken 44 times.
|
46 | if (u == target) { |
1069 |
2/2✓ Branch 0 (16→17) taken 1 times.
✓ Branch 1 (16→19) taken 1 times.
|
2 | if (level == 0) { |
1070 | 1 | block_iter_prev_char(&bi, &u); | |
1071 | 1 | goto found; | |
1072 | } | ||
1073 | 1 | level--; | |
1074 |
2/2✓ Branch 0 (20→21) taken 1 times.
✓ Branch 1 (20→22) taken 43 times.
|
44 | } else if (u == cursor_char) { |
1075 | 1 | level++; | |
1076 | } | ||
1077 | } | ||
1078 | 1 | goto not_found; | |
1079 | |||
1080 | 1 | search_bwd: | |
1081 |
1/2✓ Branch 0 (34→26) taken 38 times.
✗ Branch 1 (34→35) not taken.
|
38 | while (block_iter_prev_char(&bi, &u)) { |
1082 |
2/2✓ Branch 0 (26→27) taken 1 times.
✓ Branch 1 (26→30) taken 37 times.
|
38 | if (u == target) { |
1083 |
1/2✓ Branch 0 (27→28) taken 1 times.
✗ Branch 1 (27→29) not taken.
|
1 | if (level == 0) { |
1084 | 1 | goto found; | |
1085 | } | ||
1086 | ✗ | level--; | |
1087 |
1/2✗ Branch 0 (30→31) not taken.
✓ Branch 1 (30→32) taken 37 times.
|
37 | } else if (u == cursor_char) { |
1088 | ✗ | level++; | |
1089 | } | ||
1090 | } | ||
1091 | |||
1092 | ✗ | not_found: | |
1093 | 1 | return error_msg(&e->err, "No matching bracket found"); | |
1094 | |||
1095 | 2 | found: | |
1096 | 2 | handle_selection_flags(view, a); | |
1097 | 2 | view->cursor = bi; | |
1098 | 2 | return true; | |
1099 | } | ||
1100 | |||
1101 | 1 | static bool cmd_mode(EditorState *e, const CommandArgs *a) | |
1102 | { | ||
1103 | 1 | const char *name = a->args[0]; | |
1104 | 1 | ModeHandler *handler = get_mode_handler(&e->modes, name); | |
1105 |
1/2✓ Branch 0 (3→4) taken 1 times.
✗ Branch 1 (3→5) not taken.
|
1 | if (!handler) { |
1106 | 1 | return error_msg(&e->err, "unknown mode '%s'", name); | |
1107 | } | ||
1108 | |||
1109 | ✗ | push_input_mode(e, handler); | |
1110 | ✗ | return true; | |
1111 | } | ||
1112 | |||
1113 | 11 | static bool cmd_move_tab(EditorState *e, const CommandArgs *a) | |
1114 | { | ||
1115 | 11 | Window *window = e->window; | |
1116 | 11 | const size_t ntabs = window->views.count; | |
1117 | 11 | const char *str = a->args[0]; | |
1118 | 11 | size_t to, from = ptr_array_xindex(&window->views, e->view); | |
1119 |
2/2✓ Branch 0 (3→4) taken 6 times.
✓ Branch 1 (3→6) taken 5 times.
|
11 | if (streq(str, "left")) { |
1120 | 6 | to = size_decrement_wrapped(from, ntabs); | |
1121 |
2/2✓ Branch 0 (6→7) taken 3 times.
✓ Branch 1 (6→9) taken 2 times.
|
5 | } else if (streq(str, "right")) { |
1122 | 3 | to = size_increment_wrapped(from, ntabs); | |
1123 | } else { | ||
1124 |
3/4✓ Branch 0 (10→11) taken 2 times.
✗ Branch 1 (10→12) not taken.
✓ Branch 2 (11→12) taken 1 times.
✓ Branch 3 (11→13) taken 1 times.
|
2 | if (!str_to_size(str, &to) || to == 0) { |
1125 | 1 | return error_msg(&e->err, "Invalid tab position %s", str); | |
1126 | } | ||
1127 | 1 | to = MIN(to, ntabs) - 1; | |
1128 | } | ||
1129 | 10 | ptr_array_move(&window->views, from, to); | |
1130 | 10 | window->update_tabbar = true; | |
1131 | 10 | return true; | |
1132 | } | ||
1133 | |||
1134 | 3 | static bool cmd_msg(EditorState *e, const CommandArgs *a) | |
1135 | { | ||
1136 | 3 | const char *str = a->args[0]; | |
1137 | 3 | CommandFlagSet np = cmdargs_flagset_from_str("np"); | |
1138 |
2/2✓ Branch 0 (3→4) taken 2 times.
✓ Branch 1 (3→5) taken 1 times.
|
3 | if (u64_popcount(a->flag_set & np) + !!str >= 2) { |
1139 | 2 | return error_msg(&e->err, "flags [-n|-p] and [number] argument are mutually exclusive"); | |
1140 | } | ||
1141 | |||
1142 | 1 | MessageArray *msgs = &e->messages; | |
1143 | 1 | size_t count = msgs->array.count; | |
1144 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→21) taken 1 times.
|
1 | if (count == 0) { |
1145 | return true; | ||
1146 | } | ||
1147 | |||
1148 | ✗ | size_t p = msgs->pos; | |
1149 | ✗ | BUG_ON(p >= count); | |
1150 | ✗ | if (has_flag(a, 'n')) { | |
1151 | ✗ | p = MIN(p + 1, count - 1); | |
1152 | ✗ | } else if (has_flag(a, 'p')) { | |
1153 | ✗ | p -= (p > 0); | |
1154 | ✗ | } else if (str) { | |
1155 | ✗ | if (!str_to_size(str, &p) || p == 0) { | |
1156 | ✗ | return error_msg(&e->err, "invalid message index: %s", str); | |
1157 | } | ||
1158 | ✗ | p = MIN(p - 1, count - 1); | |
1159 | } | ||
1160 | |||
1161 | ✗ | msgs->pos = p; | |
1162 | ✗ | return activate_current_message(msgs, e->window); | |
1163 | } | ||
1164 | |||
1165 | 8 | static bool cmd_new_line(EditorState *e, const CommandArgs *a) | |
1166 | { | ||
1167 | 8 | char iflag = cmdargs_pick_winning_flag(a, "iI"); | |
1168 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 8 times.
|
8 | bool auto_indent = iflag ? (iflag == 'i') : e->buffer->options.auto_indent; |
1169 | 8 | bool above_cursor = has_flag(a, 'a'); | |
1170 | 8 | new_line(e->view, auto_indent, above_cursor); | |
1171 | 8 | return true; | |
1172 | } | ||
1173 | |||
1174 | 1 | static bool cmd_next(EditorState *e, const CommandArgs *a) | |
1175 | { | ||
1176 | 1 | BUG_ON(a->nr_args); | |
1177 | 1 | const PointerArray *views = &e->window->views; | |
1178 | 1 | size_t current = ptr_array_xindex(views, e->view); | |
1179 | 1 | size_t next = size_increment_wrapped(current, views->count); | |
1180 | 1 | set_view(views->ptrs[next]); | |
1181 | 1 | return true; | |
1182 | } | ||
1183 | |||
1184 | 1 | static bool xglob(ErrorBuffer *ebuf, char **args, glob_t *globbuf) | |
1185 | { | ||
1186 | 1 | BUG_ON(!args); | |
1187 | 1 | BUG_ON(!args[0]); | |
1188 | 1 | int err = glob(*args, GLOB_NOCHECK, NULL, globbuf); | |
1189 |
2/4✓ Branch 0 (9→10) taken 1 times.
✗ Branch 1 (9→11) not taken.
✗ Branch 2 (10→8) not taken.
✓ Branch 3 (10→11) taken 1 times.
|
1 | while (err == 0 && *++args) { |
1190 | ✗ | err = glob(*args, GLOB_NOCHECK | GLOB_APPEND, NULL, globbuf); | |
1191 | } | ||
1192 | |||
1193 |
1/2✓ Branch 0 (11→12) taken 1 times.
✗ Branch 1 (11→18) not taken.
|
1 | if (likely(err == 0)) { |
1194 | 1 | BUG_ON(globbuf->gl_pathc == 0); | |
1195 | 1 | BUG_ON(!globbuf->gl_pathv); | |
1196 | 1 | BUG_ON(!globbuf->gl_pathv[0]); | |
1197 | return true; | ||
1198 | } | ||
1199 | |||
1200 | ✗ | BUG_ON(err == GLOB_NOMATCH); | |
1201 | ✗ | globfree(globbuf); | |
1202 | ✗ | return error_msg(ebuf, "glob: %s", (err == GLOB_NOSPACE) ? strerror(ENOMEM) : "failed"); | |
1203 | } | ||
1204 | |||
1205 | 47 | static bool cmd_open(EditorState *e, const CommandArgs *a) | |
1206 | { | ||
1207 | 47 | bool temporary = has_flag(a, 't'); | |
1208 |
3/4✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→6) taken 46 times.
✓ Branch 2 (4→5) taken 1 times.
✗ Branch 3 (4→6) not taken.
|
47 | if (unlikely(temporary && a->nr_args > 0)) { |
1209 | 1 | return error_msg(&e->err, "'open -t' can't be used with filename arguments"); | |
1210 | } | ||
1211 | |||
1212 | 46 | const char *requested_encoding = NULL; | |
1213 | 46 | char **args = a->args; | |
1214 |
2/2✓ Branch 0 (6→7) taken 3 times.
✓ Branch 1 (6→21) taken 43 times.
|
46 | if (unlikely(a->nr_flag_args > 0)) { |
1215 | // The "-e" flag is the only one that takes an argument, so the | ||
1216 | // above condition implies it was used | ||
1217 | 3 | BUG_ON(!has_flag(a, 'e')); | |
1218 | 3 | requested_encoding = args[a->nr_flag_args - 1]; | |
1219 | 3 | args += a->nr_flag_args; | |
1220 | } | ||
1221 | |||
1222 | 3 | const char *encoding = NULL; // (Auto-detect) | |
1223 |
1/2✓ Branch 0 (10→11) taken 3 times.
✗ Branch 1 (10→21) not taken.
|
3 | if (requested_encoding) { |
1224 | 3 | EncodingType enctype = lookup_encoding(requested_encoding); | |
1225 |
1/2✓ Branch 0 (12→13) taken 3 times.
✗ Branch 1 (12→14) not taken.
|
3 | if (enctype == UTF8) { |
1226 | 3 | encoding = encoding_from_type(UTF8); | |
1227 | ✗ | } else if (conversion_supported_by_iconv(requested_encoding, "UTF-8")) { | |
1228 | ✗ | encoding = encoding_normalize(requested_encoding); | |
1229 | } else { | ||
1230 | ✗ | if (errno == EINVAL) { | |
1231 | ✗ | return error_msg(&e->err, "Unsupported encoding '%s'", requested_encoding); | |
1232 | } | ||
1233 | ✗ | return error_msg ( | |
1234 | &e->err, | ||
1235 | "iconv conversion from '%s' failed: %s", | ||
1236 | requested_encoding, | ||
1237 | strerror(errno) | ||
1238 | ); | ||
1239 | } | ||
1240 | } | ||
1241 | |||
1242 | 46 | Window *window = e->window; | |
1243 |
2/2✓ Branch 0 (21→22) taken 41 times.
✓ Branch 1 (21→26) taken 5 times.
|
46 | if (a->nr_args == 0) { |
1244 | 41 | View *view = window_open_new_file(window); | |
1245 | 41 | view->buffer->temporary = temporary; | |
1246 |
2/2✓ Branch 0 (23→24) taken 3 times.
✓ Branch 1 (23→25) taken 38 times.
|
41 | if (requested_encoding) { |
1247 | 3 | buffer_set_encoding(view->buffer, encoding, e->options.utf8_bom); | |
1248 | } | ||
1249 | 41 | return true; | |
1250 | } | ||
1251 | |||
1252 | 5 | char **paths = args; | |
1253 | 5 | glob_t globbuf; | |
1254 | 5 | bool use_glob = has_flag(a, 'g'); | |
1255 |
2/2✓ Branch 0 (27→28) taken 1 times.
✓ Branch 1 (27→31) taken 4 times.
|
5 | if (use_glob) { |
1256 |
1/2✓ Branch 0 (29→30) taken 1 times.
✗ Branch 1 (29→35) not taken.
|
1 | if (!xglob(&e->err, args, &globbuf)) { |
1257 | return false; | ||
1258 | } | ||
1259 | 1 | paths = globbuf.gl_pathv; | |
1260 | } | ||
1261 | |||
1262 | 5 | View *first_opened = window_open_files(window, paths, encoding); | |
1263 | |||
1264 |
2/2✓ Branch 0 (32→33) taken 1 times.
✓ Branch 1 (32→34) taken 4 times.
|
5 | if (use_glob) { |
1265 | 1 | globfree(&globbuf); | |
1266 | } | ||
1267 | |||
1268 | 5 | return !!first_opened; | |
1269 | } | ||
1270 | |||
1271 | 110 | static bool cmd_option(EditorState *e, const CommandArgs *a) | |
1272 | { | ||
1273 | 110 | BUG_ON(a->nr_args < 3); | |
1274 | 110 | const char *arg0 = a->args[0]; | |
1275 | 110 | char **strs = a->args + 1; | |
1276 | 110 | size_t nstrs = a->nr_args - 1; | |
1277 |
2/2✓ Branch 0 (4→5) taken 3 times.
✓ Branch 1 (4→6) taken 107 times.
|
110 | if (unlikely(arg0[0] == '\0')) { |
1278 | 3 | return error_msg(&e->err, "first argument cannot be empty"); | |
1279 | } | ||
1280 |
2/2✓ Branch 0 (6→7) taken 1 times.
✓ Branch 1 (6→8) taken 106 times.
|
107 | if (unlikely(nstrs & 1)) { |
1281 | 1 | return error_msg(&e->err, "missing option value"); | |
1282 | } | ||
1283 |
2/2✓ Branch 0 (9→10) taken 100 times.
✓ Branch 1 (9→28) taken 6 times.
|
106 | if (unlikely(!validate_local_options(&e->err, strs))) { |
1284 | return false; | ||
1285 | } | ||
1286 | |||
1287 | 100 | PointerArray *opts = &e->file_options; | |
1288 |
2/2✓ Branch 0 (11→12) taken 8 times.
✓ Branch 1 (11→17) taken 92 times.
|
100 | if (has_flag(a, 'r')) { |
1289 | 8 | FileTypeOrFileName u = {.filename = regexp_intern(&e->err, arg0)}; | |
1290 |
1/2✓ Branch 0 (13→14) taken 8 times.
✗ Branch 1 (13→16) not taken.
|
8 | if (unlikely(!u.filename)) { |
1291 | return false; | ||
1292 | } | ||
1293 | 8 | add_file_options(opts, FOPTS_FILENAME, u, strs, nstrs); | |
1294 | 8 | return true; | |
1295 | } | ||
1296 | |||
1297 | 92 | size_t errors = 0; | |
1298 |
2/2✓ Branch 0 (26→18) taken 282 times.
✓ Branch 1 (26→27) taken 92 times.
|
374 | for (size_t pos = 0, len = strlen(arg0); pos < len; ) { |
1299 | 282 | const StringView ft = get_delim(arg0, &pos, len, ','); | |
1300 |
2/2✓ Branch 0 (19→20) taken 8 times.
✓ Branch 1 (19→22) taken 274 times.
|
282 | if (unlikely(!is_valid_filetype_name_sv(&ft))) { |
1301 | 8 | error_msg(&e->err, "invalid filetype name: '%.*s'", (int)ft.length, ft.data); | |
1302 | 8 | errors++; | |
1303 | 8 | continue; | |
1304 | } | ||
1305 | 274 | FileTypeOrFileName u = {.filetype = mem_intern(ft.data, ft.length)}; | |
1306 | 274 | add_file_options(opts, FOPTS_FILETYPE, u, strs, nstrs); | |
1307 | } | ||
1308 | |||
1309 | 92 | return !errors; | |
1310 | } | ||
1311 | |||
1312 | 9 | static bool cmd_blkdown(EditorState *e, const CommandArgs *a) | |
1313 | { | ||
1314 | 9 | View *view = e->view; | |
1315 | 9 | handle_selection_flags(view, a); | |
1316 | |||
1317 | // If current line is blank, skip past consecutive blank lines | ||
1318 | 9 | StringView line = get_current_line(&view->cursor); | |
1319 |
2/2✓ Branch 0 (4→7) taken 2 times.
✓ Branch 1 (4→11) taken 7 times.
|
9 | if (strview_isblank(&line)) { |
1320 |
1/2✓ Branch 0 (8→5) taken 3 times.
✗ Branch 1 (8→11) not taken.
|
3 | while (block_iter_next_line(&view->cursor)) { |
1321 | 3 | line = block_iter_get_line(&view->cursor); | |
1322 |
2/2✓ Branch 0 (6→7) taken 1 times.
✓ Branch 1 (6→11) taken 2 times.
|
3 | if (!strview_isblank(&line)) { |
1323 | break; | ||
1324 | } | ||
1325 | } | ||
1326 | } | ||
1327 | |||
1328 | // Skip past non-blank lines | ||
1329 |
2/2✓ Branch 0 (13→9) taken 21 times.
✓ Branch 1 (13→14) taken 4 times.
|
25 | while (block_iter_next_line(&view->cursor)) { |
1330 | 21 | line = block_iter_get_line(&view->cursor); | |
1331 |
2/2✓ Branch 0 (10→12) taken 16 times.
✓ Branch 1 (10→14) taken 5 times.
|
21 | if (strview_isblank(&line)) { |
1332 | break; | ||
1333 | } | ||
1334 | } | ||
1335 | |||
1336 | // If we reach the last populated line in the buffer, move down one line | ||
1337 | 9 | BlockIter tmp = view->cursor; | |
1338 | 9 | block_iter_eol(&tmp); | |
1339 | 9 | block_iter_skip_bytes(&tmp, 1); | |
1340 |
2/2✓ Branch 0 (16→17) taken 4 times.
✓ Branch 1 (16→18) taken 5 times.
|
9 | if (block_iter_is_eof(&tmp)) { |
1341 | 4 | view->cursor = tmp; | |
1342 | } | ||
1343 | |||
1344 | 9 | return true; | |
1345 | } | ||
1346 | |||
1347 | 4 | static bool cmd_blkup(EditorState *e, const CommandArgs *a) | |
1348 | { | ||
1349 | 4 | View *view = e->view; | |
1350 | 4 | handle_selection_flags(view, a); | |
1351 | |||
1352 | // If cursor is on the first line, just move to bol | ||
1353 |
2/2✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→6) taken 3 times.
|
4 | if (view->cy == 0) { |
1354 | 1 | block_iter_bol(&view->cursor); | |
1355 | 1 | return true; | |
1356 | } | ||
1357 | |||
1358 | // If current line is blank, skip past consecutive blank lines | ||
1359 | 3 | StringView line = get_current_line(&view->cursor); | |
1360 |
2/2✓ Branch 0 (7→10) taken 1 times.
✓ Branch 1 (7→14) taken 2 times.
|
3 | if (strview_isblank(&line)) { |
1361 |
1/2✓ Branch 0 (11→8) taken 3 times.
✗ Branch 1 (11→14) not taken.
|
3 | while (block_iter_prev_line(&view->cursor)) { |
1362 | 3 | line = block_iter_get_line(&view->cursor); | |
1363 |
2/2✓ Branch 0 (9→10) taken 2 times.
✓ Branch 1 (9→14) taken 1 times.
|
3 | if (!strview_isblank(&line)) { |
1364 | break; | ||
1365 | } | ||
1366 | } | ||
1367 | } | ||
1368 | |||
1369 | // Skip past non-blank lines | ||
1370 |
2/2✓ Branch 0 (16→12) taken 14 times.
✓ Branch 1 (16→17) taken 2 times.
|
16 | while (block_iter_prev_line(&view->cursor)) { |
1371 | 14 | line = block_iter_get_line(&view->cursor); | |
1372 |
2/2✓ Branch 0 (13→15) taken 13 times.
✓ Branch 1 (13→17) taken 1 times.
|
14 | if (strview_isblank(&line)) { |
1373 | break; | ||
1374 | } | ||
1375 | } | ||
1376 | |||
1377 | return true; | ||
1378 | } | ||
1379 | |||
1380 | 6 | static bool cmd_paste(EditorState *e, const CommandArgs *a) | |
1381 | { | ||
1382 | 6 | PasteLinesType type = PASTE_LINES_BELOW_CURSOR; | |
1383 |
2/3✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 2 times.
✓ Branch 2 (3→6) taken 4 times.
|
6 | switch (cmdargs_pick_winning_flag(a, "ac")) { |
1384 | ✗ | case 'a': type = PASTE_LINES_ABOVE_CURSOR; break; | |
1385 | 2 | case 'c': type = PASTE_LINES_INLINE; break; | |
1386 | } | ||
1387 | |||
1388 | 6 | bool move_after = has_flag(a, 'm'); | |
1389 | 6 | paste(&e->clipboard, e->view, type, move_after); | |
1390 | 6 | return true; | |
1391 | } | ||
1392 | |||
1393 | 2 | static bool cmd_pgdown(EditorState *e, const CommandArgs *a) | |
1394 | { | ||
1395 | 2 | View *view = e->view; | |
1396 | 2 | handle_selection_flags(view, a); | |
1397 | |||
1398 | 2 | Window *window = e->window; | |
1399 | 2 | long margin = window_get_scroll_margin(window); | |
1400 | 2 | long bottom = view->vy + window->edit_h - 1 - margin; | |
1401 | 2 | long count; | |
1402 | |||
1403 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 2 times.
|
2 | if (view->cy < bottom) { |
1404 | ✗ | count = bottom - view->cy; | |
1405 | } else { | ||
1406 | 2 | count = window->edit_h - 1 - margin * 2; | |
1407 | } | ||
1408 | |||
1409 | 2 | move_down(view, count); | |
1410 | 2 | return true; | |
1411 | } | ||
1412 | |||
1413 | 1 | static bool cmd_pgup(EditorState *e, const CommandArgs *a) | |
1414 | { | ||
1415 | 1 | View *view = e->view; | |
1416 | 1 | handle_selection_flags(view, a); | |
1417 | |||
1418 | 1 | Window *window = e->window; | |
1419 | 1 | long margin = window_get_scroll_margin(window); | |
1420 | 1 | long top = view->vy + margin; | |
1421 | 1 | long count; | |
1422 | |||
1423 |
1/2✓ Branch 0 (4→5) taken 1 times.
✗ Branch 1 (4→6) not taken.
|
1 | if (view->cy > top) { |
1424 | 1 | count = view->cy - top; | |
1425 | } else { | ||
1426 | ✗ | count = window->edit_h - 1 - margin * 2; | |
1427 | } | ||
1428 | |||
1429 | 1 | move_up(view, count); | |
1430 | 1 | return true; | |
1431 | } | ||
1432 | |||
1433 | 1 | static bool cmd_prev(EditorState *e, const CommandArgs *a) | |
1434 | { | ||
1435 | 1 | BUG_ON(a->nr_args); | |
1436 | 1 | const PointerArray *views = &e->window->views; | |
1437 | 1 | size_t current = ptr_array_xindex(views, e->view); | |
1438 | 1 | size_t prev = size_decrement_wrapped(current, views->count); | |
1439 | 1 | set_view(views->ptrs[prev]); | |
1440 | 1 | return true; | |
1441 | } | ||
1442 | |||
1443 | 3 | static View *window_find_modified_view(Window *window) | |
1444 | { | ||
1445 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→7) taken 3 times.
|
3 | if (buffer_modified(window->view->buffer)) { |
1446 | return window->view; | ||
1447 | } | ||
1448 | ✗ | for (size_t i = 0, n = window->views.count; i < n; i++) { | |
1449 | ✗ | View *view = window->views.ptrs[i]; | |
1450 | ✗ | if (buffer_modified(view->buffer)) { | |
1451 | return view; | ||
1452 | } | ||
1453 | } | ||
1454 | return NULL; | ||
1455 | } | ||
1456 | |||
1457 | 5 | static size_t count_modified_buffers(const PointerArray *buffers, View **first) | |
1458 | { | ||
1459 | 5 | View *modified = NULL; | |
1460 | 5 | size_t nr_modified = 0; | |
1461 |
2/2✓ Branch 0 (8→3) taken 8 times.
✓ Branch 1 (8→9) taken 5 times.
|
13 | for (size_t i = 0, n = buffers->count; i < n; i++) { |
1462 | 8 | Buffer *buffer = buffers->ptrs[i]; | |
1463 |
2/2✓ Branch 0 (3→4) taken 5 times.
✓ Branch 1 (3→5) taken 3 times.
|
8 | if (!buffer_modified(buffer)) { |
1464 | 5 | continue; | |
1465 | } | ||
1466 | 3 | nr_modified++; | |
1467 |
1/2✓ Branch 0 (5→6) taken 3 times.
✗ Branch 1 (5→7) not taken.
|
3 | if (!modified) { |
1468 | 3 | modified = buffer_get_first_view(buffer); | |
1469 | } | ||
1470 | } | ||
1471 | |||
1472 | 5 | BUG_ON(nr_modified > 0 && !modified); | |
1473 | 5 | *first = modified; | |
1474 | 5 | return nr_modified; | |
1475 | } | ||
1476 | |||
1477 | 7 | static bool cmd_quit(EditorState *e, const CommandArgs *a) | |
1478 | { | ||
1479 | 7 | static const FlagMapping fmap[] = { | |
1480 | {'C', EFLAG_CMD_HIST}, | ||
1481 | {'S', EFLAG_SEARCH_HIST}, | ||
1482 | {'F', EFLAG_FILE_HIST}, | ||
1483 | {'H', EFLAG_ALL_HIST}, | ||
1484 | }; | ||
1485 | |||
1486 | 7 | int exit_code = EDITOR_EXIT_OK; | |
1487 |
2/2✓ Branch 0 (2→3) taken 5 times.
✓ Branch 1 (2→9) taken 2 times.
|
7 | if (a->nr_args) { |
1488 |
2/2✓ Branch 0 (4→5) taken 1 times.
✓ Branch 1 (4→6) taken 4 times.
|
5 | if (!str_to_int(a->args[0], &exit_code)) { |
1489 | 1 | return error_msg(&e->err, "Not a valid integer argument: '%s'", a->args[0]); | |
1490 | } | ||
1491 | 4 | int max = EDITOR_EXIT_MAX; | |
1492 |
3/4✓ Branch 0 (6→7) taken 4 times.
✗ Branch 1 (6→8) not taken.
✓ Branch 2 (7→8) taken 1 times.
✓ Branch 3 (7→9) taken 3 times.
|
4 | if (exit_code < 0 || exit_code > max) { |
1493 | 1 | return error_msg(&e->err, "Exit code should be between 0 and %d", max); | |
1494 | } | ||
1495 | } | ||
1496 | |||
1497 | 5 | View *first_modified = NULL; | |
1498 | 5 | size_t n = count_modified_buffers(&e->buffers, &first_modified); | |
1499 |
2/2✓ Branch 0 (10→11) taken 2 times.
✓ Branch 1 (10→12) taken 3 times.
|
5 | if (n == 0) { |
1500 | 2 | goto exit; | |
1501 | } | ||
1502 | |||
1503 | 3 | BUG_ON(!first_modified); | |
1504 |
1/2✓ Branch 0 (14→15) taken 3 times.
✗ Branch 1 (14→16) not taken.
|
3 | const char *plural = (n > 1) ? "s" : ""; |
1505 |
1/2✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→20) taken 3 times.
|
3 | if (has_flag(a, 'f')) { |
1506 | ✗ | LOG_INFO("force quitting with %zu modified buffer%s", n, plural); | |
1507 | ✗ | goto exit; | |
1508 | } | ||
1509 | |||
1510 | // Activate a modified view (giving preference to the current view or | ||
1511 | // a view in the current window) | ||
1512 | 3 | View *view = window_find_modified_view(e->window); | |
1513 |
1/2✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→22) taken 3 times.
|
3 | set_view(view ? view : first_modified); |
1514 | |||
1515 |
2/2✓ Branch 0 (24→25) taken 1 times.
✓ Branch 1 (24→26) taken 2 times.
|
3 | if (!has_flag(a, 'p')) { |
1516 | 1 | return error_msg(&e->err, "Save modified files or run 'quit -f' to quit without saving"); | |
1517 | } | ||
1518 | |||
1519 |
1/2✓ Branch 0 (26→27) taken 2 times.
✗ Branch 1 (26→28) not taken.
|
2 | if (unlikely(e->flags & EFLAG_HEADLESS)) { |
1520 | 2 | return error_msg(&e->err, "-p flag unavailable in headless mode"); | |
1521 | } | ||
1522 | |||
1523 | ✗ | char question[128]; | |
1524 | ✗ | xsnprintf ( | |
1525 | question, sizeof question, | ||
1526 | "Quit without saving %zu modified buffer%s? [y/N]", | ||
1527 | n, plural | ||
1528 | ); | ||
1529 | |||
1530 | ✗ | if (dialog_prompt(e, question, "ny") != 'y') { | |
1531 | return false; | ||
1532 | } | ||
1533 | ✗ | LOG_INFO("quit prompt accepted with %zu modified buffer%s", n, plural); | |
1534 | |||
1535 | 2 | exit:; | |
1536 | 2 | EditorFlags histflags = cmdargs_convert_flags(a, fmap, ARRAYLEN(fmap)); | |
1537 | 2 | e->flags &= ~histflags; | |
1538 | 2 | e->status = exit_code; | |
1539 | 2 | return true; | |
1540 | } | ||
1541 | |||
1542 | 7 | static bool cmd_redo(EditorState *e, const CommandArgs *a) | |
1543 | { | ||
1544 | 7 | char *arg = a->args[0]; | |
1545 | 7 | unsigned long change_id = 0; | |
1546 |
2/2✓ Branch 0 (2→3) taken 6 times.
✓ Branch 1 (2→7) taken 1 times.
|
7 | if (arg) { |
1547 |
3/4✓ Branch 0 (4→5) taken 5 times.
✓ Branch 1 (4→6) taken 1 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→7) taken 5 times.
|
6 | if (!str_to_ulong(arg, &change_id) || change_id == 0) { |
1548 | 1 | return error_msg(&e->err, "Invalid change id: %s", arg); | |
1549 | } | ||
1550 | } | ||
1551 |
2/2✓ Branch 0 (8→9) taken 3 times.
✓ Branch 1 (8→10) taken 3 times.
|
6 | if (!redo(e->view, change_id)) { |
1552 | return false; | ||
1553 | } | ||
1554 | |||
1555 | 3 | return unselect(e->view); | |
1556 | } | ||
1557 | |||
1558 | 1 | static bool cmd_refresh(EditorState *e, const CommandArgs *a) | |
1559 | { | ||
1560 | 1 | BUG_ON(a->nr_args); | |
1561 | 1 | e->screen_update |= UPDATE_ALL; | |
1562 | 1 | return true; | |
1563 | } | ||
1564 | |||
1565 | 8 | static bool repeat_insert(EditorState *e, const char *str, unsigned int count, bool move_after) | |
1566 | { | ||
1567 | 8 | BUG_ON(count < 2); | |
1568 | 8 | size_t str_len = strlen(str); | |
1569 | 8 | size_t bufsize; | |
1570 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 8 times.
|
8 | if (unlikely(size_multiply_overflows(count, str_len, &bufsize))) { |
1571 | ✗ | return error_msg(&e->err, "Repeated insert would overflow"); | |
1572 | } | ||
1573 |
1/2✓ Branch 0 (7→8) taken 8 times.
✗ Branch 1 (7→29) not taken.
|
8 | if (unlikely(bufsize == 0)) { |
1574 | return true; | ||
1575 | } | ||
1576 | |||
1577 | 8 | char *buf = malloc(bufsize); | |
1578 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 8 times.
|
8 | if (unlikely(!buf)) { |
1579 | ✗ | return error_msg_errno(&e->err, "malloc"); | |
1580 | } | ||
1581 | |||
1582 | 8 | char tmp[4096]; | |
1583 |
2/2✓ Branch 0 (10→11) taken 3 times.
✓ Branch 1 (10→13) taken 5 times.
|
8 | if (str_len == 1) { |
1584 | 3 | memset(buf, str[0], bufsize); | |
1585 | 3 | LOG_DEBUG("Optimized %u inserts of 1 byte into 1 memset()", count); | |
1586 | 3 | goto insert; | |
1587 |
3/4✓ Branch 0 (13→14) taken 1 times.
✓ Branch 1 (13→16) taken 4 times.
✗ Branch 2 (14→16) not taken.
✓ Branch 3 (14→19) taken 1 times.
|
5 | } else if (bufsize < 2 * sizeof(tmp) || str_len > sizeof(tmp) / 8) { |
1588 |
2/2✓ Branch 0 (17→15) taken 65 times.
✓ Branch 1 (17→18) taken 4 times.
|
69 | for (size_t i = 0; i < count; i++) { |
1589 | 65 | memcpy(buf + (i * str_len), str, str_len); | |
1590 | } | ||
1591 | 4 | goto insert; | |
1592 | } | ||
1593 | |||
1594 | 1 | size_t strs_per_tmp = sizeof(tmp) / str_len; | |
1595 | 1 | size_t tmp_len = strs_per_tmp * str_len; | |
1596 | 1 | size_t tmps_per_buf = bufsize / tmp_len; | |
1597 | 1 | size_t remainder = bufsize % tmp_len; | |
1598 | |||
1599 | // Create a block of text containing `strs_per_tmp` concatenated strs | ||
1600 |
2/2✓ Branch 0 (21→20) taken 1024 times.
✓ Branch 1 (21→23) taken 1 times.
|
1025 | for (size_t i = 0; i < strs_per_tmp; i++) { |
1601 | 1024 | memcpy(tmp + (i * str_len), str, str_len); | |
1602 | } | ||
1603 | |||
1604 | // Copy `tmps_per_buf` copies of `tmp` into `buf` | ||
1605 |
2/2✓ Branch 0 (23→22) taken 2 times.
✓ Branch 1 (23→24) taken 1 times.
|
3 | for (size_t i = 0; i < tmps_per_buf; i++) { |
1606 | 2 | memcpy(buf + (i * tmp_len), tmp, tmp_len); | |
1607 | } | ||
1608 | |||
1609 | // Copy the remainder into `buf` (if any) | ||
1610 |
1/2✓ Branch 0 (24→25) taken 1 times.
✗ Branch 1 (24→26) not taken.
|
1 | if (remainder) { |
1611 | 1 | memcpy(buf + (tmps_per_buf * tmp_len), tmp, remainder); | |
1612 | } | ||
1613 | |||
1614 | 1 | LOG_DEBUG ( | |
1615 | "Optimized %u inserts of %zu bytes into %zu inserts of %zu bytes", | ||
1616 | count, str_len, | ||
1617 | tmps_per_buf, tmp_len | ||
1618 | ); | ||
1619 | |||
1620 | 8 | insert: | |
1621 | 8 | insert_text(e->view, buf, bufsize, move_after); | |
1622 | 8 | free(buf); | |
1623 | 8 | return true; | |
1624 | } | ||
1625 | |||
1626 | ✗ | static bool cmd_reopen(EditorState *e, const CommandArgs* UNUSED_ARG(a)) | |
1627 | { | ||
1628 | ✗ | for (const FileHistoryEntry *h = e->file_history.last; h; h = h->prev) { | |
1629 | ✗ | const char *path = h->filename; | |
1630 | // The combination of walking the history and doing a linear search | ||
1631 | // (with find_buffer()) here makes this O(m*n) in the worst case, | ||
1632 | // although n (e->buffers.count) will typically be small and m | ||
1633 | // (e->file_history.entries.count) is bounded by FILEHIST_MAX_ENTRIES | ||
1634 | ✗ | if ( | |
1635 | ✗ | !find_buffer(&e->buffers, path) // Not already open in the editor | |
1636 | ✗ | && access(path, R_OK) == 0 // File exists and is readable | |
1637 | ✗ | && window_open_file(e->window, path, NULL) // Reopened successfully | |
1638 | ) { | ||
1639 | return true; | ||
1640 | } | ||
1641 | } | ||
1642 | |||
1643 | ✗ | return error_msg(&e->err, "no reopenable files in history"); | |
1644 | } | ||
1645 | |||
1646 | 39 | static bool cmd_repeat(EditorState *e, const CommandArgs *a) | |
1647 | { | ||
1648 | 39 | ErrorBuffer *ebuf = &e->err; | |
1649 | 39 | unsigned int count; | |
1650 |
2/2✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→5) taken 38 times.
|
39 | if (unlikely(!str_to_uint(a->args[0], &count))) { |
1651 | 1 | return error_msg(ebuf, "Not a valid repeat count: %s", a->args[0]); | |
1652 | } | ||
1653 |
2/2✓ Branch 0 (5→6) taken 36 times.
✓ Branch 1 (5→20) taken 2 times.
|
38 | if (unlikely(count == 0)) { |
1654 | return true; | ||
1655 | } | ||
1656 | |||
1657 | 36 | const Command *cmd = find_normal_command(a->args[1]); | |
1658 |
2/2✓ Branch 0 (7→8) taken 1 times.
✓ Branch 1 (7→9) taken 35 times.
|
36 | if (unlikely(!cmd)) { |
1659 | 1 | return error_msg(ebuf, "No such command: %s", a->args[1]); | |
1660 | } | ||
1661 | |||
1662 | 35 | CommandArgs a2 = cmdargs_new(a->args + 2); | |
1663 |
1/2✓ Branch 0 (10→11) taken 35 times.
✗ Branch 1 (10→20) not taken.
|
35 | if (unlikely(!parse_args(cmd, &a2, ebuf))) { |
1664 | return false; | ||
1665 | } | ||
1666 | |||
1667 |
6/6✓ Branch 0 (11→12) taken 32 times.
✓ Branch 1 (11→18) taken 3 times.
✓ Branch 2 (12→13) taken 10 times.
✓ Branch 3 (12→18) taken 22 times.
✓ Branch 4 (14→15) taken 8 times.
✓ Branch 5 (14→18) taken 2 times.
|
35 | if (count > 1 && cmd->cmd == cmd_insert && !has_flag(&a2, 'k')) { |
1668 | // Use optimized implementation for repeated "insert" | ||
1669 | 8 | return repeat_insert(e, a2.args[0], count, has_flag(&a2, 'm')); | |
1670 | } | ||
1671 | |||
1672 |
2/2✓ Branch 0 (19→17) taken 1096 times.
✓ Branch 1 (19→20) taken 27 times.
|
1123 | while (count--) { |
1673 | 1096 | command_func_call(e, ebuf, cmd, &a2); | |
1674 | } | ||
1675 | |||
1676 | // TODO: return false if fn() fails? | ||
1677 | return true; | ||
1678 | } | ||
1679 | |||
1680 | 14 | static bool cmd_replace(EditorState *e, const CommandArgs *a) | |
1681 | { | ||
1682 | 14 | static const FlagMapping map[] = { | |
1683 | {'b', REPLACE_BASIC}, | ||
1684 | {'c', REPLACE_CONFIRM}, | ||
1685 | {'g', REPLACE_GLOBAL}, | ||
1686 | {'i', REPLACE_IGNORE_CASE}, | ||
1687 | }; | ||
1688 | |||
1689 | 14 | const char *pattern = a->args[0]; | |
1690 | 14 | ReplaceFlags flags = cmdargs_convert_flags(a, map, ARRAYLEN(map)); | |
1691 | |||
1692 |
3/4✓ Branch 0 (3→4) taken 2 times.
✓ Branch 1 (3→6) taken 12 times.
✓ Branch 2 (4→5) taken 2 times.
✗ Branch 3 (4→6) not taken.
|
14 | if (unlikely((flags & REPLACE_CONFIRM) && (e->flags & EFLAG_HEADLESS))) { |
1693 | 2 | return error_msg(&e->err, "-c flag unavailable in headless mode"); | |
1694 | } | ||
1695 | |||
1696 | 12 | char *alloc = NULL; | |
1697 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→11) taken 12 times.
|
12 | if (has_flag(a, 'e')) { |
1698 | ✗ | size_t len = strlen(pattern); | |
1699 | ✗ | if (strn_contains_ascii_char_type(pattern, len, ASCII_REGEX)) { | |
1700 | ✗ | pattern = alloc = regexp_escape(pattern, len); | |
1701 | } | ||
1702 | ✗ | flags &= ~REPLACE_BASIC; | |
1703 | } | ||
1704 | |||
1705 |
2/2✓ Branch 0 (11→12) taken 10 times.
✓ Branch 1 (11→13) taken 2 times.
|
12 | const char *replacement = a->args[1] ? a->args[1] : ""; |
1706 | 12 | bool r = reg_replace(e->view, pattern, replacement, flags); | |
1707 | 12 | free(alloc); | |
1708 | 12 | return r; | |
1709 | } | ||
1710 | |||
1711 | 31 | static bool cmd_right(EditorState *e, const CommandArgs *a) | |
1712 | { | ||
1713 | 31 | handle_selection_flags(e->view, a); | |
1714 | 31 | move_cursor_right(e->view); | |
1715 | 31 | return true; | |
1716 | } | ||
1717 | |||
1718 | ✗ | static bool stat_changed(const FileInfo *file, const struct stat *st) | |
1719 | { | ||
1720 | // Don't compare st_mode because we allow chmod 755 etc. | ||
1721 | ✗ | return !timespecs_equal(get_stat_mtime(st), &file->mtime) | |
1722 | ✗ | || st->st_dev != file->dev | |
1723 | ✗ | || st->st_ino != file->ino | |
1724 | ✗ | || st->st_size != file->size; | |
1725 | } | ||
1726 | |||
1727 | ✗ | static bool save_unmodified_buffer(Buffer *buffer, const char *filename) | |
1728 | { | ||
1729 | ✗ | SaveUnmodifiedType type = buffer->options.save_unmodified; | |
1730 | ✗ | if (type == SAVE_NONE) { | |
1731 | ✗ | LOG_INFO("buffer unchanged; leaving file untouched"); | |
1732 | ✗ | return true; | |
1733 | } | ||
1734 | |||
1735 | ✗ | BUG_ON(type != SAVE_TOUCH); | |
1736 | ✗ | struct timespec times[2]; | |
1737 | ✗ | if (unlikely(clock_gettime(CLOCK_REALTIME, ×[0]) != 0)) { | |
1738 | ✗ | LOG_ERRNO("aborting partial save; clock_gettime() failed"); | |
1739 | ✗ | return false; | |
1740 | } | ||
1741 | |||
1742 | ✗ | times[1] = times[0]; | |
1743 | ✗ | if (unlikely(utimensat(AT_FDCWD, filename, times, 0) != 0)) { | |
1744 | ✗ | LOG_ERRNO("aborting partial save; utimensat() failed"); | |
1745 | ✗ | return false; | |
1746 | } | ||
1747 | |||
1748 | ✗ | buffer->file.mtime = times[0]; | |
1749 | ✗ | LOG_INFO("buffer unchanged; mtime/atime updated"); | |
1750 | ✗ | return true; | |
1751 | } | ||
1752 | |||
1753 | 24 | static bool cmd_save(EditorState *e, const CommandArgs *a) | |
1754 | { | ||
1755 | 24 | Buffer *buffer = e->buffer; | |
1756 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 24 times.
|
24 | if (unlikely(buffer->stdout_buffer)) { |
1757 | ✗ | const char *f = buffer_filename(buffer); | |
1758 | ✗ | return info_msg(&e->err, "%s can't be saved; it will be piped to stdout on exit", f); | |
1759 | } | ||
1760 | |||
1761 | 24 | char du_flag = cmdargs_pick_winning_flag(a, "du"); | |
1762 |
2/2✓ Branch 0 (6→7) taken 1 times.
✓ Branch 1 (6→8) taken 23 times.
|
24 | bool crlf = du_flag ? (du_flag == 'd') : buffer->crlf_newlines; |
1763 | |||
1764 | 24 | const char *requested_encoding = NULL; | |
1765 | 24 | char **args = a->args; | |
1766 |
2/2✓ Branch 0 (9→10) taken 2 times.
✓ Branch 1 (9→14) taken 22 times.
|
24 | if (unlikely(a->nr_flag_args > 0)) { |
1767 | 2 | BUG_ON(!has_flag(a, 'e')); | |
1768 | 2 | requested_encoding = args[a->nr_flag_args - 1]; | |
1769 | 2 | args += a->nr_flag_args; | |
1770 | } | ||
1771 | |||
1772 | 24 | const char *encoding = buffer->encoding; | |
1773 | 24 | bool bom = buffer->bom; | |
1774 |
2/2✓ Branch 0 (14→15) taken 2 times.
✓ Branch 1 (14→32) taken 22 times.
|
24 | if (requested_encoding) { |
1775 | 2 | EncodingType et = lookup_encoding(requested_encoding); | |
1776 |
2/2✓ Branch 0 (16→17) taken 1 times.
✓ Branch 1 (16→21) taken 1 times.
|
2 | if (et == UTF8) { |
1777 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→32) taken 1 times.
|
1 | if (!encoding_is_utf8(encoding)) { |
1778 | // Encoding changed | ||
1779 | ✗ | encoding = encoding_from_type(UTF8); | |
1780 | ✗ | bom = e->options.utf8_bom; | |
1781 | } | ||
1782 |
1/2✓ Branch 0 (22→23) taken 1 times.
✗ Branch 1 (22→28) not taken.
|
1 | } else if (conversion_supported_by_iconv("UTF-8", requested_encoding)) { |
1783 | 1 | encoding = encoding_normalize(requested_encoding); | |
1784 |
1/2✓ Branch 0 (24→25) taken 1 times.
✗ Branch 1 (24→32) not taken.
|
1 | if (encoding != buffer->encoding) { |
1785 | // Encoding changed | ||
1786 | 1 | bom = !!get_bom_for_encoding(lookup_encoding(encoding)); | |
1787 | } | ||
1788 | } else { | ||
1789 | ✗ | if (errno == EINVAL) { | |
1790 | ✗ | return error_msg(&e->err, "Unsupported encoding '%s'", requested_encoding); | |
1791 | } | ||
1792 | ✗ | return error_msg ( | |
1793 | &e->err, | ||
1794 | "iconv conversion to '%s' failed: %s", | ||
1795 | requested_encoding, | ||
1796 | strerror(errno) | ||
1797 | ); | ||
1798 | } | ||
1799 | } | ||
1800 | |||
1801 | 24 | char b_flag = cmdargs_pick_winning_flag(a, "bB"); | |
1802 |
1/2✗ Branch 0 (33→34) not taken.
✓ Branch 1 (33→35) taken 24 times.
|
24 | bom = b_flag ? (b_flag == 'b') : bom; |
1803 | |||
1804 | 24 | char *absolute = buffer->abs_filename; | |
1805 | 24 | bool force = has_flag(a, 'f'); | |
1806 | 24 | bool new_locked = false; | |
1807 |
2/2✓ Branch 0 (36→37) taken 23 times.
✓ Branch 1 (36→45) taken 1 times.
|
24 | if (a->nr_args > 0) { |
1808 |
2/2✓ Branch 0 (37→38) taken 1 times.
✓ Branch 1 (37→39) taken 22 times.
|
23 | if (args[0][0] == '\0') { |
1809 | 1 | return error_msg(&e->err, "Empty filename not allowed"); | |
1810 | } | ||
1811 | 22 | char *tmp = path_absolute(args[0]); | |
1812 |
1/2✗ Branch 0 (40→41) not taken.
✓ Branch 1 (40→42) taken 22 times.
|
22 | if (!tmp) { |
1813 | ✗ | return error_msg_errno(&e->err, "Failed to make absolute path"); | |
1814 | } | ||
1815 |
3/4✓ Branch 0 (42→43) taken 2 times.
✓ Branch 1 (42→55) taken 20 times.
✗ Branch 2 (43→44) not taken.
✓ Branch 3 (43→55) taken 2 times.
|
22 | if (absolute && streq(tmp, absolute)) { |
1816 | ✗ | free(tmp); | |
1817 | } else { | ||
1818 | absolute = tmp; | ||
1819 | } | ||
1820 | } else { | ||
1821 |
1/2✓ Branch 0 (45→46) taken 1 times.
✗ Branch 1 (45→52) not taken.
|
1 | if (!absolute) { |
1822 |
1/2✓ Branch 0 (47→48) taken 1 times.
✗ Branch 1 (47→49) not taken.
|
1 | if (!has_flag(a, 'p')) { |
1823 | 1 | return error_msg(&e->err, "No filename"); | |
1824 | } | ||
1825 | ✗ | push_input_mode(e, e->command_mode); | |
1826 | ✗ | cmdline_set_text(&e->cmdline, "save "); | |
1827 | ✗ | return true; | |
1828 | } | ||
1829 | ✗ | if (buffer->readonly && !force) { | |
1830 | ✗ | return error_msg(&e->err, "Use -f to force saving read-only file"); | |
1831 | } | ||
1832 | } | ||
1833 | |||
1834 | 22 | mode_t old_mode = buffer->file.mode; | |
1835 | 22 | bool hardlinks = false; | |
1836 | 22 | struct stat st; | |
1837 | 22 | bool stat_ok = !stat(absolute, &st); | |
1838 |
2/2✓ Branch 0 (56→57) taken 21 times.
✓ Branch 1 (56→61) taken 1 times.
|
22 | if (!stat_ok) { |
1839 |
1/2✗ Branch 0 (57→58) not taken.
✓ Branch 1 (57→70) taken 21 times.
|
21 | if (errno != ENOENT) { |
1840 | ✗ | error_msg(&e->err, "stat failed for %s: %s", absolute, strerror(errno)); | |
1841 | ✗ | goto error; | |
1842 | } | ||
1843 | } else { | ||
1844 | 1 | if ( | |
1845 |
1/2✗ Branch 0 (61→62) not taken.
✓ Branch 1 (61→66) taken 1 times.
|
1 | absolute == buffer->abs_filename |
1846 | ✗ | && !force | |
1847 | ✗ | && stat_changed(&buffer->file, &st) | |
1848 | ) { | ||
1849 | ✗ | error_msg ( | |
1850 | &e->err, | ||
1851 | "File has been modified by another process; " | ||
1852 | "use 'save -f' to force overwrite" | ||
1853 | ); | ||
1854 | ✗ | goto error; | |
1855 | } | ||
1856 |
1/2✓ Branch 0 (66→67) taken 1 times.
✗ Branch 1 (66→69) not taken.
|
1 | if (S_ISDIR(st.st_mode)) { |
1857 | 1 | error_msg(&e->err, "Will not overwrite directory %s", absolute); | |
1858 | 1 | goto error; | |
1859 | } | ||
1860 | ✗ | hardlinks = (st.st_nlink >= 2); | |
1861 | } | ||
1862 | |||
1863 |
1/2✗ Branch 0 (70→71) not taken.
✓ Branch 1 (70→84) taken 21 times.
|
21 | if (e->options.lock_files) { |
1864 | ✗ | if (absolute == buffer->abs_filename) { | |
1865 | ✗ | if (!buffer->locked) { | |
1866 | ✗ | if (!lock_file(&e->locks_ctx, &e->err, absolute)) { | |
1867 | ✗ | if (!force) { | |
1868 | ✗ | error_msg(&e->err, "Can't lock file %s", absolute); | |
1869 | ✗ | goto error; | |
1870 | } | ||
1871 | } else { | ||
1872 | ✗ | buffer->locked = true; | |
1873 | } | ||
1874 | } | ||
1875 | } else { | ||
1876 | ✗ | if (!lock_file(&e->locks_ctx, &e->err, absolute)) { | |
1877 | ✗ | if (!force) { | |
1878 | ✗ | error_msg(&e->err, "Can't lock file %s", absolute); | |
1879 | ✗ | goto error; | |
1880 | } | ||
1881 | } else { | ||
1882 | new_locked = true; | ||
1883 | } | ||
1884 | } | ||
1885 | } | ||
1886 | |||
1887 |
1/2✗ Branch 0 (84→85) not taken.
✓ Branch 1 (84→102) taken 21 times.
|
21 | if (stat_ok) { |
1888 | ✗ | if (absolute != buffer->abs_filename && !force) { | |
1889 | ✗ | error_msg(&e->err, "Use -f to overwrite %s", absolute); | |
1890 | ✗ | goto error; | |
1891 | } | ||
1892 | // Allow chmod 755 etc. | ||
1893 | ✗ | buffer->file.mode = st.st_mode; | |
1894 | } | ||
1895 | |||
1896 | ✗ | if ( | |
1897 | stat_ok | ||
1898 | ✗ | && buffer->options.save_unmodified != SAVE_FULL | |
1899 | ✗ | && !stat_changed(&buffer->file, &st) | |
1900 | ✗ | && st.st_uid == buffer->file.uid | |
1901 | ✗ | && st.st_gid == buffer->file.gid | |
1902 | ✗ | && !buffer_modified(buffer) | |
1903 | ✗ | && absolute == buffer->abs_filename | |
1904 | ✗ | && encoding == buffer->encoding | |
1905 | ✗ | && crlf == buffer->crlf_newlines | |
1906 | ✗ | && bom == buffer->bom | |
1907 | ✗ | && save_unmodified_buffer(buffer, absolute) | |
1908 | ) { | ||
1909 | ✗ | BUG_ON(new_locked); | |
1910 | return true; | ||
1911 | } | ||
1912 | |||
1913 | 21 | FileSaveContext ctx = { | |
1914 | 21 | .ebuf = &e->err, | |
1915 | .encoding = encoding, | ||
1916 | 21 | .new_file_mode = e->new_file_mode, | |
1917 | .crlf = crlf, | ||
1918 | .write_bom = bom, | ||
1919 | .hardlinks = hardlinks, | ||
1920 | }; | ||
1921 | |||
1922 |
1/2✗ Branch 0 (103→104) not taken.
✓ Branch 1 (103→105) taken 21 times.
|
21 | if (!save_buffer(buffer, absolute, &ctx)) { |
1923 | ✗ | goto error; | |
1924 | } | ||
1925 | |||
1926 | 21 | buffer->saved_change = buffer->cur_change; | |
1927 | 21 | buffer->readonly = false; | |
1928 | 21 | buffer->temporary = false; | |
1929 | 21 | buffer->crlf_newlines = crlf; | |
1930 | 21 | buffer->bom = bom; | |
1931 |
2/2✓ Branch 0 (105→106) taken 2 times.
✓ Branch 1 (105→107) taken 19 times.
|
21 | if (requested_encoding) { |
1932 | 2 | buffer->encoding = encoding; | |
1933 | } | ||
1934 | |||
1935 |
1/2✓ Branch 0 (107→108) taken 21 times.
✗ Branch 1 (107→112) not taken.
|
21 | if (absolute != buffer->abs_filename) { |
1936 |
1/2✗ Branch 0 (108→109) not taken.
✓ Branch 1 (108→110) taken 21 times.
|
21 | if (buffer->locked) { |
1937 | // Filename changes, release old file lock | ||
1938 | ✗ | unlock_file(&e->locks_ctx, &e->err, buffer->abs_filename); | |
1939 | } | ||
1940 | 21 | buffer->locked = new_locked; | |
1941 | |||
1942 | 21 | free(buffer->abs_filename); | |
1943 | 21 | buffer->abs_filename = absolute; | |
1944 | 21 | buffer_update_short_filename(buffer, &e->home_dir); | |
1945 | 21 | e->screen_update |= UPDATE_TERM_TITLE; | |
1946 | |||
1947 | // Filename change is not detected (only buffer_modified() change) | ||
1948 | 21 | buffer_mark_tabbars_changed(buffer); | |
1949 | } | ||
1950 |
3/4✓ Branch 0 (112→113) taken 19 times.
✓ Branch 1 (112→123) taken 2 times.
✓ Branch 2 (113→114) taken 19 times.
✗ Branch 3 (113→123) not taken.
|
21 | if (!old_mode && streq(buffer->options.filetype, "none")) { |
1951 | // New file and most likely user has not changed the filetype | ||
1952 |
1/2✗ Branch 0 (115→116) not taken.
✓ Branch 1 (115→123) taken 19 times.
|
19 | if (buffer_detect_filetype(buffer, &e->filetypes)) { |
1953 | ✗ | set_file_options(e, buffer); | |
1954 | ✗ | set_editorconfig_options(buffer); | |
1955 | ✗ | buffer_update_syntax(e, buffer); | |
1956 | } | ||
1957 | } | ||
1958 | |||
1959 | return true; | ||
1960 | |||
1961 | ✗ | error: | |
1962 |
0/2✗ Branch 0 (119→120) not taken.
✗ Branch 1 (119→121) not taken.
|
1 | if (new_locked) { |
1963 | ✗ | unlock_file(&e->locks_ctx, &e->err, absolute); | |
1964 | } | ||
1965 |
1/2✓ Branch 0 (121→122) taken 1 times.
✗ Branch 1 (121→123) not taken.
|
1 | if (absolute != buffer->abs_filename) { |
1966 | 1 | free(absolute); | |
1967 | } | ||
1968 | return false; | ||
1969 | } | ||
1970 | |||
1971 | 1 | static bool cmd_scroll_down(EditorState *e, const CommandArgs *a) | |
1972 | { | ||
1973 | 1 | BUG_ON(a->nr_args); | |
1974 | 1 | View *view = e->view; | |
1975 | 1 | handle_selection_flags(view, a); | |
1976 | 1 | view->vy++; | |
1977 | |||
1978 |
1/2✓ Branch 0 (6→7) taken 1 times.
✗ Branch 1 (6→10) not taken.
|
1 | if (has_flag(a, 'M')) { |
1979 | return true; // Don't move cursor, even at scroll margin | ||
1980 | } | ||
1981 | |||
1982 | 1 | unsigned int margin = window_get_scroll_margin(e->window); | |
1983 |
1/2✓ Branch 0 (8→9) taken 1 times.
✗ Branch 1 (8→10) not taken.
|
1 | if (view->cy < view->vy + margin) { |
1984 | 1 | move_down(view, 1); | |
1985 | } | ||
1986 | |||
1987 | return true; | ||
1988 | } | ||
1989 | |||
1990 | 1 | static bool cmd_scroll_pgdown(EditorState *e, const CommandArgs *a) | |
1991 | { | ||
1992 | 1 | BUG_ON(a->nr_args); | |
1993 | 1 | View *view = e->view; | |
1994 | 1 | handle_selection_flags(view, a); | |
1995 | |||
1996 | 1 | const long edit_h = e->window->edit_h; | |
1997 | 1 | const long max = view->buffer->nl - edit_h + 1; | |
1998 | 1 | long count; | |
1999 | |||
2000 |
2/4✓ Branch 0 (5→6) taken 1 times.
✗ Branch 1 (5→11) not taken.
✓ Branch 2 (6→7) taken 1 times.
✗ Branch 3 (6→11) not taken.
|
1 | if (view->vy < max && max > 0) { |
2001 | 1 | bool half = has_flag(a, 'h'); | |
2002 | 1 | unsigned int shift = half & 1; | |
2003 | 1 | count = (edit_h - 1) >> shift; | |
2004 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 1 times.
|
1 | if (view->vy + count > max) { |
2005 | ✗ | count = max - view->vy; | |
2006 | } | ||
2007 | 1 | view->vy += count; | |
2008 | ✗ | } else if (view->cy < view->buffer->nl) { | |
2009 | ✗ | count = view->buffer->nl - view->cy; | |
2010 | } else { | ||
2011 | return true; | ||
2012 | } | ||
2013 | |||
2014 | 1 | move_down(view, count); | |
2015 | 1 | return true; | |
2016 | } | ||
2017 | |||
2018 | 1 | static bool cmd_scroll_pgup(EditorState *e, const CommandArgs *a) | |
2019 | { | ||
2020 | 1 | BUG_ON(a->nr_args); | |
2021 | 1 | View *view = e->view; | |
2022 | 1 | handle_selection_flags(view, a); | |
2023 | |||
2024 | 1 | long count; | |
2025 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→8) taken 1 times.
|
1 | if (view->vy > 0) { |
2026 | ✗ | bool half = has_flag(a, 'h'); | |
2027 | ✗ | unsigned int shift = half & 1; | |
2028 | ✗ | count = MIN((e->window->edit_h - 1) >> shift, view->vy); | |
2029 | ✗ | view->vy -= count; | |
2030 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→11) taken 1 times.
|
1 | } else if (view->cy > 0) { |
2031 | count = view->cy; | ||
2032 | } else { | ||
2033 | return true; | ||
2034 | } | ||
2035 | |||
2036 | ✗ | move_up(view, count); | |
2037 | ✗ | return true; | |
2038 | } | ||
2039 | |||
2040 | 1 | static bool cmd_scroll_up(EditorState *e, const CommandArgs *a) | |
2041 | { | ||
2042 | 1 | BUG_ON(a->nr_args); | |
2043 | 1 | View *view = e->view; | |
2044 | 1 | handle_selection_flags(view, a); | |
2045 | 1 | view->vy -= (view->vy > 0); | |
2046 | |||
2047 |
1/2✓ Branch 0 (6→7) taken 1 times.
✗ Branch 1 (6→10) not taken.
|
1 | if (has_flag(a, 'M')) { |
2048 | return true; // Don't move cursor, even at scroll margin | ||
2049 | } | ||
2050 | |||
2051 | 1 | const Window *window = e->window; | |
2052 | 1 | unsigned int margin = window_get_scroll_margin(window); | |
2053 |
1/2✓ Branch 0 (8→9) taken 1 times.
✗ Branch 1 (8→10) not taken.
|
1 | if (view->vy + (window->edit_h - margin) <= view->cy) { |
2054 | 1 | move_up(view, 1); | |
2055 | } | ||
2056 | |||
2057 | return true; | ||
2058 | } | ||
2059 | |||
2060 | 161 | static unsigned int count_npw_flags(const CommandArgs *a) | |
2061 | { | ||
2062 | 161 | const CommandFlagSet npw = cmdargs_flagset_from_str("npw"); | |
2063 | 161 | return u64_popcount(a->flag_set & npw); | |
2064 | } | ||
2065 | |||
2066 | 7 | static bool cmd_search(EditorState *e, const CommandArgs *a) | |
2067 | { | ||
2068 | 7 | const char *pattern = a->args[0]; | |
2069 |
2/2✓ Branch 0 (3→4) taken 2 times.
✓ Branch 1 (3→5) taken 5 times.
|
7 | if (count_npw_flags(a) + !!pattern > 1) { |
2070 | 2 | return error_msg(&e->err, "flags [-n|-p|-w] and [pattern] argument are mutually exclusive"); | |
2071 | } | ||
2072 | |||
2073 | 5 | View *view = e->view; | |
2074 | 5 | char pattbuf[4096]; | |
2075 | 5 | bool use_word_under_cursor = has_flag(a, 'w'); | |
2076 | |||
2077 |
2/2✓ Branch 0 (6→7) taken 1 times.
✓ Branch 1 (6→13) taken 4 times.
|
5 | if (use_word_under_cursor) { |
2078 | 1 | StringView word = view_get_word_under_cursor(view); | |
2079 |
1/2✓ Branch 0 (8→9) taken 1 times.
✗ Branch 1 (8→12) not taken.
|
1 | if (word.length == 0) { |
2080 | 1 | return false; // Error message wouldn't be very useful here | |
2081 | } | ||
2082 | 1 | const RegexpWordBoundaryTokens *rwbt = &e->regexp_word_tokens; | |
2083 | 1 | const size_t bmax = sizeof(rwbt->start); | |
2084 | 1 | static_assert_compatible_types(rwbt->start, char[8]); | |
2085 |
1/2✓ Branch 0 (9→10) taken 1 times.
✗ Branch 1 (9→11) not taken.
|
1 | if (unlikely(word.length >= sizeof(pattbuf) - (bmax * 2))) { |
2086 | 1 | return error_msg(&e->err, "word under cursor too long"); | |
2087 | } | ||
2088 | ✗ | char *ptr = stpncpy(pattbuf, rwbt->start, bmax); | |
2089 | ✗ | memcpy(ptr, word.data, word.length); | |
2090 | ✗ | memcpy(ptr + word.length, rwbt->end, bmax); | |
2091 | ✗ | pattern = pattbuf; | |
2092 | } | ||
2093 | |||
2094 | 4 | SearchCaseSensitivity cs = e->options.case_sensitive_search; | |
2095 |
1/4✗ Branch 0 (14→15) not taken.
✗ Branch 1 (14→16) not taken.
✗ Branch 2 (14→17) not taken.
✓ Branch 3 (14→18) taken 4 times.
|
4 | switch (cmdargs_pick_winning_flag(a, "ais")) { |
2096 | ✗ | case 'a': cs = CSS_AUTO; break; | |
2097 | ✗ | case 'i': cs = CSS_FALSE; break; | |
2098 | ✗ | case 's': cs = CSS_TRUE; break; | |
2099 | } | ||
2100 | |||
2101 | 4 | SearchState *search = &e->search; | |
2102 | 4 | unselect(view); | |
2103 | |||
2104 |
2/2✓ Branch 0 (20→21) taken 1 times.
✓ Branch 1 (20→22) taken 3 times.
|
4 | if (has_flag(a, 'n')) { |
2105 | 1 | return search_next(view, search, cs); | |
2106 | } | ||
2107 |
2/2✓ Branch 0 (23→24) taken 1 times.
✓ Branch 1 (23→25) taken 2 times.
|
3 | if (has_flag(a, 'p')) { |
2108 | 1 | return search_prev(view, search, cs); | |
2109 | } | ||
2110 | |||
2111 | 2 | search->reverse = has_flag(a, 'r'); | |
2112 |
1/2✗ Branch 0 (26→27) not taken.
✓ Branch 1 (26→29) taken 2 times.
|
2 | if (!pattern) { |
2113 | ✗ | push_input_mode(e, e->search_mode); | |
2114 | ✗ | return true; | |
2115 | } | ||
2116 | |||
2117 | 2 | char *alloc = NULL; | |
2118 |
2/4✓ Branch 0 (29→30) taken 2 times.
✗ Branch 1 (29→34) not taken.
✗ Branch 2 (31→32) not taken.
✓ Branch 3 (31→34) taken 2 times.
|
2 | if (!use_word_under_cursor && has_flag(a, 'e')) { |
2119 | ✗ | size_t len = strlen(pattern); | |
2120 | ✗ | if (strn_contains_ascii_char_type(pattern, len, ASCII_REGEX)) { | |
2121 | ✗ | pattern = alloc = regexp_escape(pattern, len); | |
2122 | } | ||
2123 | } | ||
2124 | |||
2125 |
2/2✓ Branch 0 (35→36) taken 1 times.
✓ Branch 1 (35→37) taken 1 times.
|
2 | if (!has_flag(a, 'H')) { |
2126 | 1 | history_append(&e->search_history, pattern); | |
2127 | } | ||
2128 | |||
2129 | 2 | search_set_regexp(search, pattern); | |
2130 | 2 | free(alloc); | |
2131 |
3/4✓ Branch 0 (39→40) taken 2 times.
✗ Branch 1 (39→43) not taken.
✓ Branch 2 (41→42) taken 1 times.
✓ Branch 3 (41→43) taken 1 times.
|
3 | return has_flag(a, 'x') || do_search_next(view, search, cs, use_word_under_cursor); |
2132 | } | ||
2133 | |||
2134 | ✗ | static bool cmd_select_block(EditorState *e, const CommandArgs *a) | |
2135 | { | ||
2136 | ✗ | BUG_ON(a->nr_args); | |
2137 | ✗ | select_block(e->view); | |
2138 | |||
2139 | // TODO: return false if select_block() doesn't select anything? | ||
2140 | ✗ | return true; | |
2141 | } | ||
2142 | |||
2143 | 3 | static bool cmd_select(EditorState *e, const CommandArgs *a) | |
2144 | { | ||
2145 | 3 | View *view = e->view; | |
2146 |
1/2✓ Branch 0 (3→4) taken 3 times.
✗ Branch 1 (3→5) not taken.
|
3 | SelectionType sel = has_flag(a, 'l') ? SELECT_LINES : SELECT_CHARS; |
2147 | 3 | bool keep = has_flag(a, 'k'); | |
2148 |
2/6✓ Branch 0 (6→7) taken 3 times.
✗ Branch 1 (6→10) not taken.
✗ Branch 2 (7→8) not taken.
✓ Branch 3 (7→10) taken 3 times.
✗ Branch 4 (8→9) not taken.
✗ Branch 5 (8→10) not taken.
|
3 | if (!keep && view->selection && view->selection == sel) { |
2149 | ✗ | sel = SELECT_NONE; | |
2150 | } | ||
2151 | |||
2152 | 3 | view->select_mode = sel; | |
2153 | 3 | do_selection(view, sel); | |
2154 | 3 | return true; | |
2155 | } | ||
2156 | |||
2157 | 31 | static bool cmd_set(EditorState *e, const CommandArgs *a) | |
2158 | { | ||
2159 | 31 | bool global = has_flag(a, 'g'); | |
2160 | 31 | bool local = has_flag(a, 'l'); | |
2161 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 31 times.
|
31 | if (!e->buffer) { |
2162 | ✗ | if (unlikely(local)) { | |
2163 | ✗ | return error_msg(&e->err, "Flag -l makes no sense in config file"); | |
2164 | } | ||
2165 | global = true; | ||
2166 | } | ||
2167 | |||
2168 | 31 | char **args = a->args; | |
2169 | 31 | size_t count = a->nr_args; | |
2170 |
2/2✓ Branch 0 (7→8) taken 1 times.
✓ Branch 1 (7→9) taken 30 times.
|
31 | if (count == 1) { |
2171 | 1 | return set_bool_option(e, args[0], local, global); | |
2172 | } | ||
2173 |
2/2✓ Branch 0 (9→10) taken 1 times.
✓ Branch 1 (9→15) taken 29 times.
|
30 | if (count & 1) { |
2174 | 1 | return error_msg(&e->err, "One or even number of arguments expected"); | |
2175 | } | ||
2176 | |||
2177 | size_t errors = 0; | ||
2178 |
2/2✓ Branch 0 (15→11) taken 29 times.
✓ Branch 1 (15→16) taken 29 times.
|
58 | for (size_t i = 0; i < count; i += 2) { |
2179 |
2/2✓ Branch 0 (12→13) taken 12 times.
✓ Branch 1 (12→14) taken 17 times.
|
29 | if (!set_option(e, args[i], args[i + 1], local, global)) { |
2180 | 12 | errors++; | |
2181 | } | ||
2182 | } | ||
2183 | |||
2184 | 29 | return !errors; | |
2185 | } | ||
2186 | |||
2187 | 16 | static bool cmd_setenv(EditorState *e, const CommandArgs *a) | |
2188 | { | ||
2189 | 16 | const char *name = a->args[0]; | |
2190 |
2/2✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→4) taken 15 times.
|
16 | if (unlikely(streq(name, "DTE_VERSION"))) { |
2191 | 1 | return error_msg(&e->err, "$DTE_VERSION cannot be changed"); | |
2192 | } | ||
2193 | |||
2194 | 15 | const size_t nr_args = a->nr_args; | |
2195 | 15 | int res; | |
2196 |
2/2✓ Branch 0 (4→5) taken 11 times.
✓ Branch 1 (4→6) taken 4 times.
|
15 | if (nr_args == 2) { |
2197 | 11 | res = setenv(name, a->args[1], 1); | |
2198 | } else { | ||
2199 | 4 | BUG_ON(nr_args != 1); | |
2200 | 4 | res = unsetenv(name); | |
2201 | } | ||
2202 | |||
2203 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→15) taken 15 times.
|
15 | if (likely(res == 0)) { |
2204 | return true; | ||
2205 | } | ||
2206 | |||
2207 | ✗ | if (errno == EINVAL) { | |
2208 | ✗ | return error_msg(&e->err, "Invalid environment variable name '%s'", name); | |
2209 | } | ||
2210 | |||
2211 | ✗ | return error_msg_errno(&e->err, nr_args == 2 ? "setenv" : "unsetenv"); | |
2212 | } | ||
2213 | |||
2214 | 12 | static bool cmd_shift(EditorState *e, const CommandArgs *a) | |
2215 | { | ||
2216 | 12 | const char *arg = a->args[0]; | |
2217 | 12 | int count; | |
2218 |
2/2✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→5) taken 11 times.
|
12 | if (!str_to_int(arg, &count)) { |
2219 | 1 | return error_msg(&e->err, "Invalid number: %s", arg); | |
2220 | } | ||
2221 |
2/2✓ Branch 0 (5→6) taken 1 times.
✓ Branch 1 (5→7) taken 10 times.
|
11 | if (count == 0) { |
2222 | 1 | return error_msg(&e->err, "Count must be non-zero"); | |
2223 | } | ||
2224 | 10 | shift_lines(e->view, count); | |
2225 | 10 | return true; | |
2226 | } | ||
2227 | |||
2228 | 9 | static bool cmd_show(EditorState *e, const CommandArgs *a) | |
2229 | { | ||
2230 | 9 | bool write_to_cmdline = has_flag(a, 'c'); | |
2231 |
3/4✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→6) taken 8 times.
✓ Branch 2 (4→5) taken 1 times.
✗ Branch 3 (4→6) not taken.
|
9 | if (write_to_cmdline && a->nr_args < 2) { |
2232 | 1 | return error_msg(&e->err, "\"show -c\" requires 2 arguments"); | |
2233 | } | ||
2234 | 8 | return show(e, a->args[0], a->args[1], write_to_cmdline); | |
2235 | } | ||
2236 | |||
2237 | 2 | static bool cmd_suspend(EditorState *e, const CommandArgs *a) | |
2238 | { | ||
2239 | 2 | BUG_ON(a->nr_args); | |
2240 |
1/2✓ Branch 0 (4→5) taken 2 times.
✗ Branch 1 (4→6) not taken.
|
2 | if (e->flags & EFLAG_HEADLESS) { |
2241 | 2 | return error_msg(&e->err, "unavailable in headless mode"); | |
2242 | } | ||
2243 | |||
2244 | ✗ | if (e->status == EDITOR_INITIALIZING) { | |
2245 | ✗ | LOG_WARNING("suspend request ignored"); | |
2246 | ✗ | return false; | |
2247 | } | ||
2248 | |||
2249 | ✗ | if (e->session_leader) { | |
2250 | ✗ | return error_msg(&e->err, "Session leader can't suspend"); | |
2251 | } | ||
2252 | |||
2253 | ✗ | ui_end(e, false); | |
2254 | ✗ | term_cooked(); | |
2255 | ✗ | LOG_INFO("suspending"); | |
2256 | |||
2257 | ✗ | bool suspended = !kill(0, SIGSTOP); | |
2258 | ✗ | if (suspended) { | |
2259 | ✗ | LOG_INFO("resumed"); | |
2260 | } else { | ||
2261 | ✗ | error_msg_errno(&e->err, "kill"); | |
2262 | } | ||
2263 | |||
2264 | ✗ | term_raw(); | |
2265 | ✗ | ui_start(e); | |
2266 | ✗ | return suspended; | |
2267 | } | ||
2268 | |||
2269 | 2 | static bool cmd_tag(EditorState *e, const CommandArgs *a) | |
2270 | { | ||
2271 |
2/2✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→6) taken 1 times.
|
2 | if (has_flag(a, 'r')) { |
2272 | 1 | bookmark_pop(&e->bookmarks, e->window); | |
2273 | 1 | return true; | |
2274 | } | ||
2275 | |||
2276 | 1 | size_t nargs = a->nr_args; | |
2277 | 1 | StringView word_under_cursor; | |
2278 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 1 times.
|
1 | if (nargs == 0) { |
2279 | ✗ | word_under_cursor = view_get_word_under_cursor(e->view); | |
2280 | ✗ | if (word_under_cursor.length == 0) { | |
2281 | return false; | ||
2282 | } | ||
2283 | } | ||
2284 | |||
2285 | 1 | MessageArray *msgs = &e->messages; | |
2286 | 1 | clear_messages(msgs); | |
2287 |
1/2✓ Branch 0 (11→12) taken 1 times.
✗ Branch 1 (11→20) not taken.
|
1 | if (!load_tag_file(&e->tagfile, &e->err)) { |
2288 | return false; | ||
2289 | } | ||
2290 | |||
2291 | 1 | const char *filename = e->buffer->abs_filename; | |
2292 |
1/2✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→16) taken 1 times.
|
1 | if (nargs == 0) { |
2293 | ✗ | tag_lookup(&e->tagfile, msgs, &e->err, &word_under_cursor, filename); | |
2294 | } | ||
2295 | |||
2296 |
2/2✓ Branch 0 (17→14) taken 1 times.
✓ Branch 1 (17→18) taken 1 times.
|
2 | for (size_t i = 0; i < nargs; i++) { |
2297 | 1 | StringView tagname = strview_from_cstring(a->args[i]); | |
2298 | 1 | tag_lookup(&e->tagfile, msgs, &e->err, &tagname, filename); | |
2299 | } | ||
2300 | |||
2301 | 1 | activate_current_message_save(msgs, &e->bookmarks, e->view); | |
2302 | 1 | return (msgs->array.count > 0); | |
2303 | } | ||
2304 | |||
2305 | ✗ | static bool cmd_title(EditorState *e, const CommandArgs *a) | |
2306 | { | ||
2307 | ✗ | Buffer *buffer = e->buffer; | |
2308 | ✗ | if (buffer->abs_filename) { | |
2309 | ✗ | return error_msg(&e->err, "saved buffers can't be retitled"); | |
2310 | } | ||
2311 | |||
2312 | ✗ | buffer_set_display_filename(buffer, xstrdup(a->args[0])); | |
2313 | ✗ | buffer_mark_tabbars_changed(buffer); | |
2314 | ✗ | e->screen_update |= UPDATE_TERM_TITLE; | |
2315 | ✗ | return true; | |
2316 | } | ||
2317 | |||
2318 | 16 | static bool cmd_toggle(EditorState *e, const CommandArgs *a) | |
2319 | { | ||
2320 | 16 | bool global = has_flag(a, 'g'); | |
2321 | 16 | bool verbose = has_flag(a, 'v'); | |
2322 | 16 | const char *option_name = a->args[0]; | |
2323 | 16 | size_t nr_values = a->nr_args - 1; | |
2324 |
1/2✓ Branch 0 (4→5) taken 16 times.
✗ Branch 1 (4→6) not taken.
|
16 | if (nr_values == 0) { |
2325 | 16 | return toggle_option(e, option_name, global, verbose); | |
2326 | } | ||
2327 | |||
2328 | ✗ | char **values = a->args + 1; | |
2329 | ✗ | return toggle_option_values(e, option_name, global, verbose, values, nr_values); | |
2330 | } | ||
2331 | |||
2332 | 1020 | static bool cmd_undo(EditorState *e, const CommandArgs *a) | |
2333 | { | ||
2334 | 1020 | View *view = e->view; | |
2335 | 1020 | bool move_only = has_flag(a, 'm'); | |
2336 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→8) taken 1020 times.
|
1020 | if (move_only) { |
2337 | ✗ | const Change *change = view->buffer->cur_change; | |
2338 | ✗ | if (!change->next) { | |
2339 | // If there's only 1 change, there's nothing meaningful to move to | ||
2340 | return false; | ||
2341 | } | ||
2342 | ✗ | block_iter_goto_offset(&view->cursor, change->offset); | |
2343 | ✗ | view_reset_preferred_x(view); | |
2344 | ✗ | return true; | |
2345 | } | ||
2346 | |||
2347 |
3/4✓ Branch 0 (9→10) taken 44 times.
✓ Branch 1 (9→13) taken 976 times.
✗ Branch 2 (11→12) not taken.
✓ Branch 3 (11→13) taken 44 times.
|
1020 | return undo(view) && unselect(view); |
2348 | } | ||
2349 | |||
2350 | 1 | static bool cmd_unselect(EditorState *e, const CommandArgs *a) | |
2351 | { | ||
2352 | 1 | BUG_ON(a->nr_args); | |
2353 | 1 | return unselect(e->view); | |
2354 | } | ||
2355 | |||
2356 | 16 | static bool cmd_up(EditorState *e, const CommandArgs *a) | |
2357 | { | ||
2358 | 16 | handle_selection_flags(e->view, a); | |
2359 | 16 | move_up(e->view, 1); | |
2360 | 16 | return true; | |
2361 | } | ||
2362 | |||
2363 | 3 | static bool cmd_view(EditorState *e, const CommandArgs *a) | |
2364 | { | ||
2365 | 3 | Window *window = e->window; | |
2366 | 3 | BUG_ON(window->views.count == 0); | |
2367 | 3 | const char *arg = a->args[0]; | |
2368 | 3 | size_t idx; | |
2369 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 3 times.
|
3 | if (streq(arg, "last")) { |
2370 | ✗ | idx = window->views.count - 1; | |
2371 | } else { | ||
2372 |
4/4✓ Branch 0 (7→8) taken 2 times.
✓ Branch 1 (7→9) taken 1 times.
✓ Branch 2 (8→9) taken 1 times.
✓ Branch 3 (8→10) taken 1 times.
|
3 | if (!str_to_size(arg, &idx) || idx == 0) { |
2373 | 2 | return error_msg(&e->err, "Invalid view index: %s", arg); | |
2374 | } | ||
2375 | 1 | idx = MIN(idx, window->views.count) - 1; | |
2376 | } | ||
2377 | 1 | set_view(window->views.ptrs[idx]); | |
2378 | 1 | return true; | |
2379 | } | ||
2380 | |||
2381 | 4 | static bool cmd_wclose(EditorState *e, const CommandArgs *a) | |
2382 | { | ||
2383 | 4 | View *view = window_find_unclosable_view(e->window); | |
2384 | 4 | bool force = has_flag(a, 'f'); | |
2385 |
2/2✓ Branch 0 (4→5) taken 2 times.
✓ Branch 1 (4→6) taken 2 times.
|
4 | if (!view || force) { |
2386 | 2 | goto close; | |
2387 | } | ||
2388 | |||
2389 | 2 | bool prompt = has_flag(a, 'p'); | |
2390 | 2 | set_view(view); | |
2391 |
2/2✓ Branch 0 (8→9) taken 1 times.
✓ Branch 1 (8→10) taken 1 times.
|
2 | if (!prompt) { |
2392 | 1 | return error_msg ( | |
2393 | &e->err, | ||
2394 | "Save modified files or run 'wclose -f' to close " | ||
2395 | "window without saving" | ||
2396 | ); | ||
2397 | } | ||
2398 | |||
2399 |
1/2✓ Branch 0 (10→11) taken 1 times.
✗ Branch 1 (10→12) not taken.
|
1 | if (unlikely(e->flags & EFLAG_HEADLESS)) { |
2400 | 1 | return error_msg(&e->err, "-p flag unavailable in headless mode"); | |
2401 | } | ||
2402 | |||
2403 | ✗ | if (dialog_prompt(e, "Close window without saving? [y/N]", "ny") != 'y') { | |
2404 | return false; | ||
2405 | } | ||
2406 | |||
2407 | ✗ | close: | |
2408 | 2 | window_close(e->window); | |
2409 | 2 | return true; | |
2410 | } | ||
2411 | |||
2412 | 1 | static bool cmd_wflip(EditorState *e, const CommandArgs *a) | |
2413 | { | ||
2414 | 1 | BUG_ON(a->nr_args); | |
2415 | 1 | Frame *frame = e->window->frame; | |
2416 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 1 times.
|
1 | if (!frame->parent) { |
2417 | return false; | ||
2418 | } | ||
2419 | ✗ | frame->parent->vertical ^= 1; | |
2420 | ✗ | e->screen_update |= UPDATE_ALL_WINDOWS; | |
2421 | ✗ | return true; | |
2422 | } | ||
2423 | |||
2424 | 1 | static bool cmd_wnext(EditorState *e, const CommandArgs *a) | |
2425 | { | ||
2426 | 1 | BUG_ON(a->nr_args); | |
2427 | 1 | e->window = window_next(e->window); | |
2428 | 1 | set_view(e->window->view); | |
2429 | 1 | e->screen_update |= UPDATE_ALL; | |
2430 | 1 | frame_debug(e->root_frame); | |
2431 | 1 | return true; | |
2432 | } | ||
2433 | |||
2434 | 1 | static bool cmd_word_bwd(EditorState *e, const CommandArgs *a) | |
2435 | { | ||
2436 | 1 | handle_selection_flags(e->view, a); | |
2437 | 1 | bool skip_non_word = has_flag(a, 's'); | |
2438 | 1 | word_bwd(&e->view->cursor, skip_non_word); | |
2439 | 1 | view_reset_preferred_x(e->view); | |
2440 | 1 | return true; | |
2441 | } | ||
2442 | |||
2443 | 4 | static bool cmd_word_fwd(EditorState *e, const CommandArgs *a) | |
2444 | { | ||
2445 | 4 | handle_selection_flags(e->view, a); | |
2446 | 4 | bool skip_non_word = has_flag(a, 's'); | |
2447 | 4 | word_fwd(&e->view->cursor, skip_non_word); | |
2448 | 4 | view_reset_preferred_x(e->view); | |
2449 | 4 | return true; | |
2450 | } | ||
2451 | |||
2452 | 1 | static bool cmd_wprev(EditorState *e, const CommandArgs *a) | |
2453 | { | ||
2454 | 1 | BUG_ON(a->nr_args); | |
2455 | 1 | e->window = window_prev(e->window); | |
2456 | 1 | set_view(e->window->view); | |
2457 | 1 | e->screen_update |= UPDATE_ALL; | |
2458 | 1 | frame_debug(e->root_frame); | |
2459 | 1 | return true; | |
2460 | } | ||
2461 | |||
2462 | 4 | static bool cmd_wrap_paragraph(EditorState *e, const CommandArgs *a) | |
2463 | { | ||
2464 | 4 | const char *arg = a->args[0]; | |
2465 | 4 | unsigned int width = e->buffer->options.text_width; | |
2466 |
2/2✓ Branch 0 (2→3) taken 3 times.
✓ Branch 1 (2→9) taken 1 times.
|
4 | if (arg) { |
2467 |
2/2✓ Branch 0 (4→5) taken 1 times.
✓ Branch 1 (4→6) taken 2 times.
|
3 | if (!str_to_uint(arg, &width)) { |
2468 | 1 | return error_msg(&e->err, "invalid paragraph width: %s", arg); | |
2469 | } | ||
2470 | 2 | unsigned int max = TEXT_WIDTH_MAX; | |
2471 |
3/4✓ Branch 0 (6→7) taken 2 times.
✗ Branch 1 (6→8) not taken.
✓ Branch 2 (7→8) taken 1 times.
✓ Branch 3 (7→9) taken 1 times.
|
2 | if (width < 1 || width > max) { |
2472 | 1 | return error_msg(&e->err, "width must be between 1 and %u", max); | |
2473 | } | ||
2474 | } | ||
2475 | 2 | wrap_paragraph(e->view, width); | |
2476 | 2 | return true; | |
2477 | } | ||
2478 | |||
2479 | 2 | static bool cmd_wresize(EditorState *e, const CommandArgs *a) | |
2480 | { | ||
2481 | 2 | Window *window = e->window; | |
2482 |
2/2✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→18) taken 1 times.
|
2 | if (!window->frame->parent) { |
2483 | // Only window | ||
2484 | return false; | ||
2485 | } | ||
2486 | |||
2487 | 1 | ResizeDirection dir = RESIZE_DIRECTION_AUTO; | |
2488 |
1/3✗ Branch 0 (3→4) not taken.
✗ Branch 1 (3→5) not taken.
✓ Branch 2 (3→6) taken 1 times.
|
1 | switch (last_flag(a)) { |
2489 | ✗ | case 'h': | |
2490 | ✗ | dir = RESIZE_DIRECTION_HORIZONTAL; | |
2491 | ✗ | break; | |
2492 | ✗ | case 'v': | |
2493 | ✗ | dir = RESIZE_DIRECTION_VERTICAL; | |
2494 | ✗ | break; | |
2495 | } | ||
2496 | |||
2497 | 1 | const char *arg = a->args[0]; | |
2498 |
1/2✓ Branch 0 (6→7) taken 1 times.
✗ Branch 1 (6→15) not taken.
|
1 | if (arg) { |
2499 | 1 | int n; | |
2500 |
1/2✓ Branch 0 (8→9) taken 1 times.
✗ Branch 1 (8→11) not taken.
|
1 | if (!str_to_int(arg, &n)) { |
2501 | 1 | return error_msg(&e->err, "Invalid resize value: %s", arg); | |
2502 | } | ||
2503 | ✗ | if (arg[0] == '+' || arg[0] == '-') { | |
2504 | ✗ | frame_add_to_size(window->frame, dir, n); | |
2505 | } else { | ||
2506 | ✗ | frame_resize(window->frame, dir, n); | |
2507 | } | ||
2508 | } else { | ||
2509 | ✗ | frame_equalize_sizes(window->frame->parent); | |
2510 | } | ||
2511 | |||
2512 | ✗ | e->screen_update |= UPDATE_ALL_WINDOWS; | |
2513 | ✗ | frame_debug(e->root_frame); | |
2514 | // TODO: return false if resize failed? | ||
2515 | ✗ | return true; | |
2516 | } | ||
2517 | |||
2518 | 3 | static bool cmd_wsplit(EditorState *e, const CommandArgs *a) | |
2519 | { | ||
2520 | 3 | bool before = has_flag(a, 'b'); | |
2521 |
1/4✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 3 times.
✗ Branch 2 (5→6) not taken.
✗ Branch 3 (5→7) not taken.
|
3 | bool use_glob = has_flag(a, 'g') && a->nr_args > 0; |
2522 | 3 | bool vertical = has_flag(a, 'h'); | |
2523 | 3 | bool root = has_flag(a, 'r'); | |
2524 | 3 | bool temporary = has_flag(a, 't'); | |
2525 |
3/4✓ Branch 0 (10→11) taken 1 times.
✓ Branch 1 (10→13) taken 2 times.
✗ Branch 2 (12→13) not taken.
✓ Branch 3 (12→15) taken 1 times.
|
3 | bool empty = temporary || has_flag(a, 'n'); |
2526 | |||
2527 |
2/2✓ Branch 0 (13→14) taken 1 times.
✓ Branch 1 (13→15) taken 1 times.
|
2 | if (unlikely(empty && a->nr_args > 0)) { |
2528 | 1 | return error_msg(&e->err, "flags -n and -t can't be used with filename arguments"); | |
2529 | } | ||
2530 | |||
2531 | 2 | char **paths = a->args; | |
2532 | 2 | glob_t globbuf; | |
2533 |
1/2✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→19) taken 2 times.
|
2 | if (use_glob) { |
2534 | ✗ | if (!xglob(&e->err, a->args, &globbuf)) { | |
2535 | return false; | ||
2536 | } | ||
2537 | ✗ | paths = globbuf.gl_pathv; | |
2538 | } | ||
2539 | |||
2540 | 2 | Frame *frame; | |
2541 |
1/2✗ Branch 0 (19→20) not taken.
✓ Branch 1 (19→21) taken 2 times.
|
2 | if (root) { |
2542 | ✗ | frame = frame_split_root(e, vertical, before); | |
2543 | } else { | ||
2544 | 2 | frame = frame_split(e->window, vertical, before); | |
2545 | } | ||
2546 | |||
2547 | 2 | View *save = e->view; | |
2548 | 2 | e->window = frame->window; | |
2549 | 2 | e->view = NULL; | |
2550 | 2 | e->buffer = NULL; | |
2551 | 2 | e->screen_update |= UPDATE_ALL; | |
2552 | |||
2553 | 2 | View *view; | |
2554 |
2/2✓ Branch 0 (22→23) taken 1 times.
✓ Branch 1 (22→25) taken 1 times.
|
2 | if (empty) { |
2555 | 1 | view = window_open_new_file(e->window); | |
2556 | 1 | view->buffer->temporary = temporary; | |
2557 |
1/2✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→27) taken 1 times.
|
1 | } else if (paths[0]) { |
2558 | ✗ | view = window_open_files(e->window, paths, NULL); | |
2559 | } else { | ||
2560 | 1 | view = window_add_buffer(e->window, save->buffer); | |
2561 | 1 | view->cursor = save->cursor; | |
2562 | 1 | set_view(view); | |
2563 | } | ||
2564 | |||
2565 |
1/2✗ Branch 0 (29→30) not taken.
✓ Branch 1 (29→31) taken 2 times.
|
2 | if (use_glob) { |
2566 | ✗ | globfree(&globbuf); | |
2567 | } | ||
2568 | |||
2569 |
1/2✗ Branch 0 (31→32) not taken.
✓ Branch 1 (31→34) taken 2 times.
|
2 | if (!view) { |
2570 | // Open failed, remove new window | ||
2571 | ✗ | frame_remove(e, e->window->frame); | |
2572 | ✗ | e->view = save; | |
2573 | ✗ | e->buffer = save->buffer; | |
2574 | ✗ | e->window = save->window; | |
2575 | } | ||
2576 | |||
2577 | 2 | frame_debug(e->root_frame); | |
2578 | 2 | return !!view; | |
2579 | } | ||
2580 | |||
2581 | 1 | static bool cmd_wswap(EditorState *e, const CommandArgs *a) | |
2582 | { | ||
2583 | 1 | BUG_ON(a->nr_args); | |
2584 | 1 | Frame *frame = e->window->frame; | |
2585 | 1 | Frame *parent = frame->parent; | |
2586 |
1/2✓ Branch 0 (4→5) taken 1 times.
✗ Branch 1 (4→9) not taken.
|
1 | if (!parent) { |
2587 | return false; | ||
2588 | } | ||
2589 | |||
2590 | 1 | PointerArray *pframes = &parent->frames; | |
2591 | 1 | size_t current = ptr_array_xindex(pframes, frame); | |
2592 | 1 | size_t next = size_increment_wrapped(current, pframes->count); | |
2593 | 1 | ptr_array_swap(pframes, current, next); | |
2594 | 1 | e->screen_update |= UPDATE_ALL_WINDOWS; | |
2595 | 1 | return true; | |
2596 | } | ||
2597 | |||
2598 | enum { | ||
2599 | // Short aliases for CommandOptions: | ||
2600 | NA = 0, | ||
2601 | RC = CMDOPT_ALLOW_IN_RC, | ||
2602 | NFAA = CMDOPT_NO_FLAGS_AFTER_ARGS, | ||
2603 | }; | ||
2604 | |||
2605 | static const Command cmds[] = { | ||
2606 | {"alias", "", RC | NFAA, 1, 2, cmd_alias}, | ||
2607 | {"bind", "cnqsT=", RC | NFAA, 1, 2, cmd_bind}, | ||
2608 | {"blkdown", "cl", NA, 0, 0, cmd_blkdown}, | ||
2609 | {"blkup", "cl", NA, 0, 0, cmd_blkup}, | ||
2610 | {"bof", "cl", NA, 0, 0, cmd_bof}, | ||
2611 | {"bol", "clrst", NA, 0, 0, cmd_bol}, | ||
2612 | {"bolsf", "cl", NA, 0, 0, cmd_bolsf}, | ||
2613 | {"bookmark", "r", NA, 0, 0, cmd_bookmark}, | ||
2614 | {"case", "lu", NA, 0, 0, cmd_case}, | ||
2615 | {"cd", "v", RC, 1, 1, cmd_cd}, | ||
2616 | {"center-view", "", NA, 0, 0, cmd_center_view}, | ||
2617 | {"clear", "Ii", NA, 0, 0, cmd_clear}, | ||
2618 | {"close", "fpqw", NA, 0, 0, cmd_close}, | ||
2619 | {"command", "", NFAA, 0, 1, cmd_command}, | ||
2620 | {"compile", "1ps", NFAA, 2, -1, cmd_compile}, | ||
2621 | {"copy", "bikp", NA, 0, 1, cmd_copy}, | ||
2622 | {"cursor", "", RC, 0, 3, cmd_cursor}, | ||
2623 | {"cut", "", NA, 0, 0, cmd_cut}, | ||
2624 | {"def-mode", "Uu", RC, 1, 16, cmd_def_mode}, | ||
2625 | {"delete", "", NA, 0, 0, cmd_delete}, | ||
2626 | {"delete-eol", "n", NA, 0, 0, cmd_delete_eol}, | ||
2627 | {"delete-line", "S", NA, 0, 0, cmd_delete_line}, | ||
2628 | {"delete-word", "s", NA, 0, 0, cmd_delete_word}, | ||
2629 | {"down", "cl", NA, 0, 0, cmd_down}, | ||
2630 | {"eof", "cl", NA, 0, 0, cmd_eof}, | ||
2631 | {"eol", "cl", NA, 0, 0, cmd_eol}, | ||
2632 | {"eolsf", "cl", NA, 0, 0, cmd_eolsf}, | ||
2633 | {"erase", "", NA, 0, 0, cmd_erase}, | ||
2634 | {"erase-bol", "", NA, 0, 0, cmd_erase_bol}, | ||
2635 | {"erase-word", "s", NA, 0, 0, cmd_erase_word}, | ||
2636 | {"errorfmt", "i", RC, 1, 2 + ERRORFMT_CAPTURE_MAX, cmd_errorfmt}, | ||
2637 | {"exec", "e=i=o=lmnpst", NFAA, 1, -1, cmd_exec}, | ||
2638 | {"ft", "bcfi", RC | NFAA, 2, -1, cmd_ft}, | ||
2639 | {"hi", "c", RC | NFAA, 0, -1, cmd_hi}, | ||
2640 | {"include", "bq", RC, 1, 1, cmd_include}, | ||
2641 | {"insert", "km", NA, 1, 1, cmd_insert}, | ||
2642 | {"join", "", NA, 0, 1, cmd_join}, | ||
2643 | {"left", "cl", NA, 0, 0, cmd_left}, | ||
2644 | {"line", "cl", NA, 1, 1, cmd_line}, | ||
2645 | {"macro", "", NA, 1, 1, cmd_macro}, | ||
2646 | {"match-bracket", "cl", NA, 0, 0, cmd_match_bracket}, | ||
2647 | {"mode", "", RC, 1, 1, cmd_mode}, | ||
2648 | {"move-tab", "", NA, 1, 1, cmd_move_tab}, | ||
2649 | {"msg", "np", NA, 0, 1, cmd_msg}, | ||
2650 | {"new-line", "Iai", NA, 0, 0, cmd_new_line}, | ||
2651 | {"next", "", NA, 0, 0, cmd_next}, | ||
2652 | {"open", "e=gt", NA, 0, -1, cmd_open}, | ||
2653 | {"option", "r", RC | NFAA, 3, -1, cmd_option}, | ||
2654 | {"paste", "acm", NA, 0, 0, cmd_paste}, | ||
2655 | {"pgdown", "cl", NA, 0, 0, cmd_pgdown}, | ||
2656 | {"pgup", "cl", NA, 0, 0, cmd_pgup}, | ||
2657 | {"prev", "", NA, 0, 0, cmd_prev}, | ||
2658 | {"quit", "CFHSfp", NA, 0, 1, cmd_quit}, | ||
2659 | {"redo", "", NA, 0, 1, cmd_redo}, | ||
2660 | {"refresh", "", NA, 0, 0, cmd_refresh}, | ||
2661 | {"reopen", "", NA, 0, 0, cmd_reopen}, | ||
2662 | {"repeat", "", NFAA, 2, -1, cmd_repeat}, | ||
2663 | {"replace", "bcegi", NA, 1, 2, cmd_replace}, | ||
2664 | {"right", "cl", NA, 0, 0, cmd_right}, | ||
2665 | {"save", "Bbde=fpu", NA, 0, 1, cmd_save}, | ||
2666 | {"scroll-down", "Mcl", NA, 0, 0, cmd_scroll_down}, | ||
2667 | {"scroll-pgdown", "chl", NA, 0, 0, cmd_scroll_pgdown}, | ||
2668 | {"scroll-pgup", "chl", NA, 0, 0, cmd_scroll_pgup}, | ||
2669 | {"scroll-up", "Mcl", NA, 0, 0, cmd_scroll_up}, | ||
2670 | {"search", "Haeinprswx", NA, 0, 1, cmd_search}, | ||
2671 | {"select", "kl", NA, 0, 0, cmd_select}, | ||
2672 | {"select-block", "", NA, 0, 0, cmd_select_block}, | ||
2673 | {"set", "gl", RC, 1, -1, cmd_set}, | ||
2674 | {"setenv", "", RC, 1, 2, cmd_setenv}, | ||
2675 | {"shift", "", NA, 1, 1, cmd_shift}, | ||
2676 | {"show", "c", NA, 1, 2, cmd_show}, | ||
2677 | {"suspend", "", NA, 0, 0, cmd_suspend}, | ||
2678 | {"tag", "r", NA, 0, -1, cmd_tag}, | ||
2679 | {"title", "", NA, 1, 1, cmd_title}, | ||
2680 | {"toggle", "gv", NA, 1, -1, cmd_toggle}, | ||
2681 | {"undo", "m", NA, 0, 0, cmd_undo}, | ||
2682 | {"unselect", "", NA, 0, 0, cmd_unselect}, | ||
2683 | {"up", "cl", NA, 0, 0, cmd_up}, | ||
2684 | {"view", "", NA, 1, 1, cmd_view}, | ||
2685 | {"wclose", "fp", NA, 0, 0, cmd_wclose}, | ||
2686 | {"wflip", "", NA, 0, 0, cmd_wflip}, | ||
2687 | {"wnext", "", NA, 0, 0, cmd_wnext}, | ||
2688 | {"word-bwd", "cls", NA, 0, 0, cmd_word_bwd}, | ||
2689 | {"word-fwd", "cls", NA, 0, 0, cmd_word_fwd}, | ||
2690 | {"wprev", "", NA, 0, 0, cmd_wprev}, | ||
2691 | {"wrap-paragraph", "", NA, 0, 1, cmd_wrap_paragraph}, | ||
2692 | {"wresize", "hv", NA, 0, 1, cmd_wresize}, | ||
2693 | {"wsplit", "bghnrt", NA, 0, -1, cmd_wsplit}, | ||
2694 | {"wswap", "", NA, 0, 0, cmd_wswap}, | ||
2695 | }; | ||
2696 | |||
2697 | 247 | static bool allow_macro_recording(const Command *cmd, char **args) | |
2698 | { | ||
2699 | 247 | const CommandFunc fn = cmd->cmd; | |
2700 |
5/6✓ Branch 0 (2→3) taken 225 times.
✓ Branch 1 (2→14) taken 22 times.
✓ Branch 2 (3→4) taken 203 times.
✓ Branch 3 (3→14) taken 22 times.
✓ Branch 4 (4→5) taken 203 times.
✗ Branch 5 (4→14) not taken.
|
247 | if (fn == cmd_macro || fn == cmd_command || fn == cmd_mode) { |
2701 | return false; | ||
2702 | } | ||
2703 | |||
2704 |
2/2✓ Branch 0 (5→6) taken 154 times.
✓ Branch 1 (5→14) taken 49 times.
|
203 | if (fn == cmd_search) { |
2705 | 154 | char **args_copy = copy_string_array(args, string_array_length(args)); | |
2706 | 154 | CommandArgs a = cmdargs_new(args_copy); | |
2707 | 154 | bool ret = true; | |
2708 |
1/2✓ Branch 0 (8→9) taken 154 times.
✗ Branch 1 (8→12) not taken.
|
154 | if (do_parse_args(cmd, &a) == ARGERR_NONE) { |
2709 |
2/2✓ Branch 0 (10→11) taken 44 times.
✓ Branch 1 (10→12) taken 110 times.
|
154 | if (a.nr_args + count_npw_flags(&a) == 0) { |
2710 | // If command is "search" with no pattern argument and without | ||
2711 | // flags -n, -p or -w, the command would put the editor into | ||
2712 | // search mode, which shouldn't be recorded. | ||
2713 | 44 | ret = false; | |
2714 | } | ||
2715 | } | ||
2716 | 154 | free_string_array(args_copy); | |
2717 | 154 | return ret; | |
2718 | } | ||
2719 | |||
2720 | if (fn == cmd_exec) { | ||
2721 | // TODO: don't record -o with open/tag/eval/msg | ||
2722 | } | ||
2723 | |||
2724 | return true; | ||
2725 | } | ||
2726 | |||
2727 | 22 | UNITTEST { | |
2728 | // NOLINTBEGIN(bugprone-assert-side-effect) | ||
2729 | 22 | const char *args[4] = {NULL}; | |
2730 | 22 | char **argp = (char**)args; | |
2731 | 22 | const Command *cmd = find_normal_command("left"); | |
2732 | 22 | BUG_ON(!cmd); | |
2733 | 22 | BUG_ON(!allow_macro_recording(cmd, argp)); | |
2734 | |||
2735 | 22 | cmd = find_normal_command("exec"); | |
2736 | 22 | BUG_ON(!cmd); | |
2737 | 22 | BUG_ON(!allow_macro_recording(cmd, argp)); | |
2738 | |||
2739 | 22 | cmd = find_normal_command("command"); | |
2740 | 22 | BUG_ON(!cmd); | |
2741 | 22 | BUG_ON(allow_macro_recording(cmd, argp)); | |
2742 | |||
2743 | 22 | cmd = find_normal_command("macro"); | |
2744 | 22 | BUG_ON(!cmd); | |
2745 | 22 | BUG_ON(allow_macro_recording(cmd, argp)); | |
2746 | |||
2747 | 22 | cmd = find_normal_command("search"); | |
2748 | 22 | BUG_ON(!cmd); | |
2749 | 22 | BUG_ON(allow_macro_recording(cmd, argp)); | |
2750 | 22 | args[0] = "xyz"; | |
2751 | 22 | BUG_ON(!allow_macro_recording(cmd, argp)); | |
2752 | 22 | args[0] = "-n"; | |
2753 | 22 | BUG_ON(!allow_macro_recording(cmd, argp)); | |
2754 | 22 | args[0] = "-p"; | |
2755 | 22 | BUG_ON(!allow_macro_recording(cmd, argp)); | |
2756 | 22 | args[0] = "-w"; | |
2757 | 22 | BUG_ON(!allow_macro_recording(cmd, argp)); | |
2758 | 22 | args[0] = "-Hr"; | |
2759 | 22 | BUG_ON(allow_macro_recording(cmd, argp)); | |
2760 | 22 | args[1] = "str"; | |
2761 | 22 | BUG_ON(!allow_macro_recording(cmd, argp)); | |
2762 | // NOLINTEND(bugprone-assert-side-effect) | ||
2763 | 22 | } | |
2764 | |||
2765 | 5 | static void record_command(EditorState *e, const Command *cmd, char **args) | |
2766 | { | ||
2767 |
1/2✓ Branch 0 (3→4) taken 5 times.
✗ Branch 1 (3→5) not taken.
|
5 | if (!allow_macro_recording(cmd, args)) { |
2768 | return; | ||
2769 | } | ||
2770 | 5 | macro_command_hook(&e->macro, cmd->name, args); | |
2771 | } | ||
2772 | |||
2773 | 5217 | const Command *find_normal_command(const char *name) | |
2774 | { | ||
2775 | 5217 | return BSEARCH(name, cmds, command_cmp); | |
2776 | } | ||
2777 | |||
2778 | const CommandSet normal_commands = { | ||
2779 | .lookup = find_normal_command, | ||
2780 | .macro_record = record_command, | ||
2781 | }; | ||
2782 | |||
2783 | 116 | const char *find_normal_alias(const EditorState *e, const char *name) | |
2784 | { | ||
2785 | 116 | return find_alias(&e->aliases, name); | |
2786 | } | ||
2787 | |||
2788 | 155 | bool handle_normal_command(EditorState *e, const char *cmd, bool allow_recording) | |
2789 | { | ||
2790 | 155 | CommandRunner runner = normal_mode_cmdrunner(e); | |
2791 | 155 | runner.allow_recording = allow_recording; | |
2792 | 155 | return handle_command(&runner, cmd); | |
2793 | } | ||
2794 | |||
2795 | 41 | void exec_normal_config(EditorState *e, StringView config) | |
2796 | { | ||
2797 | 41 | CommandRunner runner = normal_mode_cmdrunner(e); | |
2798 | 41 | exec_config(&runner, config); | |
2799 | 41 | } | |
2800 | |||
2801 | 102 | int read_normal_config(EditorState *e, const char *filename, ConfigFlags flags) | |
2802 | { | ||
2803 | 102 | CommandRunner runner = normal_mode_cmdrunner(e); | |
2804 | 102 | return read_config(&runner, filename, flags); | |
2805 | } | ||
2806 | |||
2807 | 5 | void collect_normal_commands(PointerArray *a, const char *prefix) | |
2808 | { | ||
2809 | 5 | COLLECT_STRING_FIELDS(cmds, name, a, prefix); | |
2810 | 5 | } | |
2811 | |||
2812 | 22 | UNITTEST { | |
2813 | 22 | CHECK_BSEARCH_ARRAY(cmds, name, strcmp); | |
2814 | |||
2815 |
2/2✓ Branch 0 (15→4) taken 1958 times.
✓ Branch 1 (15→16) taken 22 times.
|
1980 | for (size_t i = 0, n = ARRAYLEN(cmds); i < n; i++) { |
2816 | // Check that flags array is null-terminated within bounds | ||
2817 | 1958 | const char *const flags = cmds[i].flags; | |
2818 | 1958 | BUG_ON(flags[ARRAYLEN(cmds[0].flags) - 1] != '\0'); | |
2819 | |||
2820 | // Count number of real flags (i.e. not including '=') | ||
2821 | size_t nr_real_flags = 0; | ||
2822 |
2/2✓ Branch 0 (11→6) taken 3630 times.
✓ Branch 1 (11→12) taken 1958 times.
|
5588 | for (size_t j = 0; flags[j]; j++) { |
2823 | 3630 | unsigned char flag = flags[j]; | |
2824 |
2/2✓ Branch 0 (6→7) taken 3498 times.
✓ Branch 1 (6→8) taken 132 times.
|
3630 | if (ascii_isalnum(flag)) { |
2825 | 3498 | nr_real_flags++; | |
2826 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 132 times.
|
132 | } else if (flag != '=') { |
2827 | − | BUG("invalid command flag: 0x%02hhX", flag); | |
2828 | } | ||
2829 | } | ||
2830 | |||
2831 | // Check that max. number of real flags fits in CommandArgs::flags | ||
2832 | // array (and also leaves 1 byte for null-terminator) | ||
2833 | 1958 | CommandArgs a; | |
2834 | 1958 | BUG_ON(nr_real_flags >= ARRAYLEN(a.flags)); | |
2835 | } | ||
2836 | 22 | } | |
2837 |