dte test coverage


Directory: ./
File: src/commands.c
Date: 2025-09-07 23:01:39
Exec Total Coverage
Lines: 1180 1532 77.0%
Functions: 100 108 92.6%
Branches: 496 885 56.0%

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