dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 44.6% 87 / 0 / 195
Functions: 41.2% 7 / 0 / 17
Branches: 22.9% 11 / 14 / 62

src/editor.c
Line Branch Exec Source
1 #include <langinfo.h>
2 #include <locale.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
7 #include "editor.h"
8 #include "bind.h"
9 #include "bookmark.h"
10 #include "compiler.h"
11 #include "encoding.h"
12 #include "file-option.h"
13 #include "filetype.h"
14 #include "lock.h"
15 #include "signals.h"
16 #include "syntax/syntax.h"
17 #include "terminal/color.h"
18 #include "terminal/input.h"
19 #include "terminal/key.h"
20 #include "terminal/output.h"
21 #include "terminal/paste.h"
22 #include "ui.h"
23 #include "util/exitcode.h"
24 #include "util/intern.h"
25 #include "util/log.h"
26 #include "util/str-util.h"
27 #include "util/time-util.h"
28 #include "util/xmalloc.h"
29 #include "util/xsnprintf.h"
30 #include "util/xstdio.h"
31 #include "version.h"
32
33 11 static void set_and_check_locale(void)
34 {
35 11 const char *default_locale = setlocale(LC_CTYPE, "");
36
1/2
✓ Branch 3 → 4 taken 11 times.
✗ Branch 3 → 8 not taken.
11 if (likely(default_locale)) {
37 11 const char *codeset = nl_langinfo(CODESET);
38 11 LOG_INFO("locale: %s (codeset: %s)", default_locale, codeset);
39
1/2
✗ Branch 7 → 9 not taken.
✓ Branch 7 → 19 taken 11 times.
11 if (likely(lookup_encoding(codeset) == UTF8)) {
40 return;
41 }
42 } else {
43 LOG_ERROR("failed to set default locale");
44 }
45
46 static const char fallbacks[][12] = {"C.UTF-8", "en_US.UTF-8"};
47 const char *fallback = NULL;
48 for (size_t i = 0; i < ARRAYLEN(fallbacks) && !fallback; i++) {
49 fallback = setlocale(LC_CTYPE, fallbacks[i]);
50 }
51 if (fallback) {
52 LOG_NOTICE("using fallback locale for LC_CTYPE: %s", fallback);
53 return;
54 }
55
56 LOG_ERROR("no UTF-8 fallback locales found");
57 fputs("setlocale() failed\n", stderr);
58 exit(EC_CONFIG_ERROR);
59 }
60
61 // Get permissions mask for new files
62 11 static mode_t get_umask(void)
63 {
64 11 mode_t old = umask(0); // Get by setting a dummy value
65 11 umask(old); // Restore previous value
66 11 return old;
67 }
68
69 11 static const char *get_user_config_dir(const char *home, const char *dte_home)
70 {
71
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 10 times.
11 if (dte_home) {
72 1 return str_intern(dte_home);
73 }
74
75 // TODO: Use "${XDG_CONFIG_HOME:-$HOME/.config}/dte", either if it
76 // exists or (as a default) if "$HOME/.dte" *doesn't* exist. The
77 // latter case may also require one or more calls to mkdir(3).
78
79 10 char buf[8192];
80 10 return mem_intern(buf, xsnprintf(buf, sizeof buf, "%s/.dte", home));
81 }
82
83 11 EditorState *init_editor_state(const char *home, const char *dte_home)
84 {
85 11 set_and_check_locale();
86
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 11 times.
11 home = home ? home : "";
87 11 EditorState *e = xmalloc(sizeof(*e));
88
89 22 *e = (EditorState) {
90 .status = EDITOR_INITIALIZING,
91 11 .home_dir = strview_intern(home),
92 11 .user_config_dir = get_user_config_dir(home, dte_home),
93 .flags = EFLAG_HEADLESS,
94 11 .regexp_word_tokens = regexp_get_word_boundary_tokens(),
95 11 .modes = hashmap_new(3, HMAP_BORROWED_KEYS),
96 11 .aliases = hashmap_new(32, HMAP_NO_FLAGS),
97 11 .required_syntax_files = hashset_new(0, false),
98 11 .required_syntax_builtins = hashset_new(0, false),
99 .command_history = {
100 .max_entries = 512,
101 },
102 .search_history = {
103 .max_entries = 128,
104 },
105 .terminal = {
106 11 .obuf = term_outbuf(),
107 },
108 .cursor_styles = {
109 [CURSOR_MODE_DEFAULT] = {.type = CURSOR_DEFAULT, .color = COLOR_DEFAULT},
110 [CURSOR_MODE_INSERT] = {.type = CURSOR_KEEP, .color = COLOR_KEEP},
111 [CURSOR_MODE_OVERWRITE] = {.type = CURSOR_KEEP, .color = COLOR_KEEP},
112 [CURSOR_MODE_CMDLINE] = {.type = CURSOR_KEEP, .color = COLOR_KEEP},
113 },
114 .options = {
115 .auto_indent = true,
116 .detect_indent = 0,
117 .editorconfig = false,
118 .emulate_tab = false,
119 .expand_tab = false,
120 .file_history = true,
121 .indent_width = 8,
122 .overwrite = false,
123 .save_unmodified = SAVE_FULL,
124 .syntax = true,
125 .tab_width = 8,
126 .text_width = 72,
127 .ws_error = WSE_SPECIAL,
128
129 // Global-only options
130 .case_sensitive_search = CSS_TRUE,
131 .center_at_eof = false,
132 .crlf_newlines = false,
133 .display_special = false,
134 .esc_timeout = 100,
135 .filesize_limit = 250ULL << 20, // 250MiB
136 .lock_files = true,
137 .msg_compile = 0,
138 .msg_tag = 0,
139 .optimize_true_color = false,
140 .scroll_margin = 0,
141 .select_cursor_char = true,
142 .set_window_title = false,
143 .show_line_numbers = false,
144 11 .statusline_left = str_intern(" %f%s%m%s%r%s%M"),
145 11 .statusline_right = str_intern(" %y,%X %u %o %E%s%b%s%n %t %p "),
146 .syntax_line_limit = 512ULL << 10, // 512KiB
147 .syntax_size_limit = 100ULL << 20, // 100MiB
148 .tab_bar = true,
149 .utf8_bom = false,
150 .window_separator = WINSEP_BAR,
151 }
152 };
153
154 11 sanity_check_global_options(&e->options);
155
156 11 pid_t pid = getpid();
157 11 bool leader = pid == getsid(0);
158 11 e->session_leader = leader;
159
1/2
✓ Branch 18 → 19 taken 11 times.
✗ Branch 18 → 20 not taken.
22 LOG_INFO("pid: %jd%s", (intmax_t)pid, leader ? " (session leader)" : "");
160
161 11 pid_t pgid = getpgrp();
162
1/2
✓ Branch 22 → 23 taken 11 times.
✗ Branch 22 → 24 not taken.
11 if (pgid != pid) {
163 11 LOG_INFO("pgid: %jd", (intmax_t)pgid);
164 }
165
166 11 mode_t mask = get_umask();
167 11 e->new_file_mode = 0666 & ~mask;
168 11 LOG_INFO("umask: %04o", 0777u & (unsigned int)mask);
169
170 11 init_file_locks_context(&e->locks_ctx, e->user_config_dir, pid);
171
172 // Allow child processes to detect that they're running under dte
173 11 xsetenv("DTE_VERSION", VERSION);
174
175 // Initial capacities here should accommodate at least the number
176 // of bindings created by exec_rc_files()
177 11 e->normal_mode = new_mode(&e->modes, "normal", &normal_commands, 150);
178 11 e->command_mode = new_mode(&e->modes, "command", &cmd_mode_commands, 40);
179 11 e->search_mode = new_mode(&e->modes, "search", &search_mode_commands, 40);
180 11 e->mode = e->normal_mode;
181
182 11 return e;
183 }
184
185 34 static void free_mode_handler(ModeHandler *handler)
186 {
187 34 ptr_array_free_array(&handler->fallthrough_modes);
188 34 free_bindings(&handler->key_bindings);
189 34 free(handler);
190 34 }
191
192 12 void clear_all_messages(EditorState *e)
193 {
194
2/2
✓ Branch 5 → 3 taken 36 times.
✓ Branch 5 → 6 taken 12 times.
48 for (size_t i = 0; i < ARRAYLEN(e->messages); i++) {
195 36 clear_messages(&e->messages[i]);
196 }
197 12 }
198
199 11 void free_editor_state(EditorState *e)
200 {
201 11 size_t n = e->terminal.obuf.count;
202
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 11 times.
11 if (n) {
203 LOG_DEBUG("%zu unflushed bytes in terminal output buffer", n);
204 }
205 11 n = e->terminal.ibuf.len;
206
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 11 times.
11 if (n) {
207 LOG_DEBUG("%zu unprocessed bytes in terminal input buffer", n);
208 }
209
210 11 free(e->clipboard.buf);
211 11 free_file_options(&e->file_options);
212 11 free_filetypes(&e->filetypes);
213 11 free_syntaxes(&e->syntaxes);
214 11 file_history_free(&e->file_history);
215 11 history_free(&e->command_history);
216 11 history_free(&e->search_history);
217 11 search_free_regexp(&e->search);
218 11 clear_all_messages(e);
219 11 cmdline_free(&e->cmdline);
220 11 free_macro(&e->macro);
221 11 tag_file_free(&e->tagfile);
222 11 free_buffers(&e->buffers, &e->err, &e->locks_ctx);
223 11 free_file_locks_context(&e->locks_ctx);
224
225 11 ptr_array_free_cb(&e->bookmarks, FREE_FUNC(file_location_free));
226 11 hashmap_free(&e->compilers, FREE_FUNC(free_compiler));
227 11 hashmap_free(&e->modes, FREE_FUNC(free_mode_handler));
228 11 hashmap_free(&e->styles.other, free);
229 11 hashmap_free(&e->aliases, free);
230 11 hashset_free(&e->required_syntax_files);
231 11 hashset_free(&e->required_syntax_builtins);
232
233 11 free_interned_strings();
234 11 free_interned_regexps();
235 11 free(e);
236 11 }
237
238 static bool buffer_contains_block(const Buffer *buffer, const Block *ref)
239 {
240 const Block *blk;
241 block_for_each(blk, &buffer->blocks) {
242 if (blk == ref) {
243 return true;
244 }
245 }
246 return false;
247 }
248
249 static void sanity_check(const View *view)
250 {
251 if (!DEBUG_ASSERTIONS_ENABLED) {
252 return;
253 }
254 const BlockIter *cursor = &view->cursor;
255 BUG_ON(!buffer_contains_block(view->buffer, cursor->blk));
256 BUG_ON(cursor->offset > cursor->blk->size);
257 }
258
259 void any_key(Terminal *term, unsigned int esc_timeout)
260 {
261 KeyCode key;
262 xfputs("Press any key to continue\r\n", stderr);
263
264 do {
265 key = term_read_input(term, esc_timeout);
266 } while (key == KEY_NONE || key == KEYCODE_REDRAW);
267
268 bool bpaste = key == KEYCODE_BRACKETED_PASTE;
269 if (bpaste || key == KEYCODE_DETECTED_PASTE) {
270 term_discard_paste(&term->ibuf, bpaste);
271 }
272 }
273
274 NOINLINE
275 void ui_resize(EditorState *e)
276 {
277 BUG_ON(e->flags & EFLAG_HEADLESS);
278 resized = 0;
279 update_screen_size(&e->terminal, e->root_frame);
280
281 const ScreenState dummyval = {.id = 0};
282 e->screen_update |= UPDATE_ALL;
283 update_screen(e, &dummyval);
284 }
285
286 void ui_start(EditorState *e)
287 {
288 BUG_ON(e->flags & EFLAG_HEADLESS);
289 Terminal *term = &e->terminal;
290
291 // Note: the order of these calls is important - Kitty saves/restores
292 // some terminal state when switching buffers, so switching to the
293 // alternate screen buffer needs to happen before modes are enabled
294 term_use_alt_screen_buffer(term);
295 term_enable_private_modes(term);
296
297 term_restore_and_save_title(term);
298 ui_resize(e);
299 }
300
301 // Like ui_start(), but to be called only the first time the UI is started.
302 // Terminal queries are buffered before ui_resize() is called, so that only
303 // a single term_output_flush() is needed (i.e. as opposed to calling
304 // ui_start() + term_put_initial_queries() + term_output_flush()).
305 static void ui_first_start(EditorState *e, unsigned int terminal_query_level)
306 {
307 BUG_ON(e->flags & EFLAG_HEADLESS);
308 Terminal *term = &e->terminal;
309
310 // The order of these calls is important; see ui_start()
311 term_use_alt_screen_buffer(term);
312 term_enable_private_modes(term);
313
314 term_save_title(term);
315 term_put_initial_queries(term, terminal_query_level);
316 ui_resize(e);
317 }
318
319 void ui_end(Terminal *term, bool final)
320 {
321 if (final) {
322 term_restore_title(term);
323 } else {
324 term_restore_and_save_title(term);
325 }
326
327 TermOutputBuffer *obuf = &term->obuf;
328 term_clear_screen(obuf);
329 term_move_cursor(obuf, 0, term->height - 1);
330 term_restore_cursor_style(term);
331 term_show_cursor(term);
332 term_restore_private_modes(term);
333 term_use_normal_screen_buffer(term);
334 term_end_sync_update(term);
335 term_output_flush(obuf);
336 }
337
338 static ScreenState get_screen_state(const EditorState *e)
339 {
340 const View *view = e->view;
341 return (ScreenState) {
342 .is_modified = buffer_modified(view->buffer),
343 .set_window_title = e->options.set_window_title,
344 .id = view->buffer->id,
345 .cy = view->cy,
346 .vx = view->vx,
347 .vy = view->vy
348 };
349 }
350
351 static void log_timing_info(const struct timespec *start, bool enabled)
352 {
353 struct timespec end;
354 if (likely(!enabled) || !xgettime(&end)) {
355 return;
356 }
357
358 double ms = timespec_to_fp_milliseconds(timespec_subtract(&end, start));
359 LOG_INFO("main loop time: %.3f ms", ms);
360 }
361
362 void main_loop(EditorState *e, unsigned int terminal_query_level, bool timing)
363 {
364 BUG_ON(e->flags & EFLAG_HEADLESS);
365 ui_first_start(e, terminal_query_level);
366
367 while (e->status == EDITOR_RUNNING) {
368 if (unlikely(resized)) {
369 LOG_INFO("SIGWINCH received");
370 ui_resize(e);
371 }
372
373 KeyCode key = term_read_input(&e->terminal, e->options.esc_timeout);
374 if (unlikely(key == KEY_NONE)) {
375 continue;
376 }
377
378 struct timespec start;
379 timing = unlikely(timing) && xgettime(&start);
380 const ScreenState s = get_screen_state(e);
381
382 if (unlikely(key == KEYCODE_REDRAW)) {
383 e->screen_update |= UPDATE_ALL_WINDOWS;
384 } else {
385 clear_error(&e->err);
386 handle_input(e, key);
387 sanity_check(e->view);
388 }
389
390 update_screen(e, &s);
391 log_timing_info(&start, timing);
392 }
393
394 BUG_ON(e->status < 0 || e->status > EDITOR_EXIT_MAX);
395 }
396