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