diff options
Diffstat (limited to 'src/nvim/main.c')
-rw-r--r-- | src/nvim/main.c | 145 |
1 files changed, 79 insertions, 66 deletions
diff --git a/src/nvim/main.c b/src/nvim/main.c index a816221a9e..56d9030a7f 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -207,7 +207,7 @@ void early_init(void) // Allocate the first window and buffer. // Can't do anything without it, exit when it fails. if (!win_alloc_first()) { - mch_exit(0); + os_exit(0); } init_yank(); // init yank buffers @@ -293,8 +293,8 @@ int main(int argc, char **argv) if (params.diff_mode && params.window_count == -1) params.window_count = 0; /* open up to 3 windows */ - /* Don't redraw until much later. */ - ++RedrawingDisabled; + // Don't redraw until much later. + RedrawingDisabled++; setbuf(stdout, NULL); @@ -384,23 +384,17 @@ int main(int argc, char **argv) syn_maybe_on(); } - /* - * Read all the plugin files. - * Only when compiled with +eval, since most plugins need it. - */ + // Read all the plugin files. load_plugins(); // Decide about window layout for diff mode after reading vimrc. set_window_layout(¶ms); - /* - * Recovery mode without a file name: List swap files. - * This uses the 'dir' option, therefore it must be after the - * initializations. - */ + // Recovery mode without a file name: List swap files. + // Uses the 'dir' option, therefore it must be after the initializations. if (recoverymode && fname == NULL) { - recover_names(NULL, TRUE, 0, NULL); - mch_exit(0); + recover_names(NULL, true, 0, NULL); + os_exit(0); } // Set some option defaults after reading vimrc files. @@ -431,17 +425,15 @@ int main(int argc, char **argv) set_vim_var_list(VV_OLDFILES, tv_list_alloc(0)); } - /* - * "-q errorfile": Load the error file now. - * If the error file can't be read, exit before doing anything else. - */ + // "-q errorfile": Load the error file now. + // If the error file can't be read, exit before doing anything else. handle_quickfix(¶ms); - /* - * Start putting things on the screen. - * Scroll screen down before drawing over it - * Clear screen now, so file message will not be cleared. - */ + // + // Start putting things on the screen. + // Scroll screen down before drawing over it + // Clear screen now, so file message will not be cleared. + // starting = NO_BUFFERS; no_wait_return = false; if (!exmode_active) { @@ -473,27 +465,26 @@ int main(int argc, char **argv) no_wait_return = true; - /* - * Create the requested number of windows and edit buffers in them. - * Also does recovery if "recoverymode" set. - */ + // + // Create the requested number of windows and edit buffers in them. + // Also does recovery if "recoverymode" set. + // create_windows(¶ms); TIME_MSG("opening buffers"); - /* clear v:swapcommand */ + // Clear v:swapcommand set_vim_var_string(VV_SWAPCOMMAND, NULL, -1); - /* Ex starts at last line of the file */ - if (exmode_active) + // Ex starts at last line of the file. + if (exmode_active) { curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; + } apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf); TIME_MSG("BufEnter autocommands"); setpcmark(); - /* - * When started with "-q errorfile" jump to first error now. - */ + // When started with "-q errorfile" jump to first error now. if (params.edit_type == EDIT_QF) { qf_jump(NULL, 0, 0, FALSE); TIME_MSG("jump to first error"); @@ -505,26 +496,23 @@ int main(int argc, char **argv) xfree(cwd); if (params.diff_mode) { - /* set options in each window for "nvim -d". */ + // set options in each window for "nvim -d". FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { diff_win_options(wp, TRUE); } } - /* - * Shorten any of the filenames, but only when absolute. - */ - shorten_fnames(FALSE); + // Shorten any of the filenames, but only when absolute. + shorten_fnames(false); - /* - * Need to jump to the tag before executing the '-c command'. - * Makes "vim -c '/return' -t main" work. - */ + // Need to jump to the tag before executing the '-c command'. + // Makes "vim -c '/return' -t main" work. handle_tag(params.tagname); - /* Execute any "+", "-c" and "-S" arguments. */ - if (params.n_commands > 0) + // Execute any "+", "-c" and "-S" arguments. + if (params.n_commands > 0) { exe_commands(¶ms); + } starting = 0; @@ -535,9 +523,10 @@ int main(int argc, char **argv) // 'autochdir' has been postponed. do_autochdir(); - /* start in insert mode */ - if (p_im) - need_start_insertmode = TRUE; + // start in insert mode + if (p_im) { + need_start_insertmode = true; + } set_vim_var_nr(VV_VIM_DID_ENTER, 1L); apply_autocmds(EVENT_VIMENTER, NULL, NULL, false, curbuf); @@ -553,18 +542,19 @@ int main(int argc, char **argv) // main loop. set_reg_var(get_default_register_name()); - /* When a startup script or session file setup for diff'ing and - * scrollbind, sync the scrollbind now. */ + // When a startup script or session file setup for diff'ing and + // scrollbind, sync the scrollbind now. if (curwin->w_p_diff && curwin->w_p_scb) { update_topline(); check_scrollbind((linenr_T)0, 0L); TIME_MSG("diff scrollbinding"); } - /* If ":startinsert" command used, stuff a dummy command to be able to - * call normal_cmd(), which will then start Insert mode. */ - if (restart_edit != 0) + // If ":startinsert" command used, stuff a dummy command to be able to + // call normal_cmd(), which will then start Insert mode. + if (restart_edit != 0) { stuffcharReadbuff(K_NOP); + } // WORKAROUND(mhi): #3023 if (cb_flags & CB_UNNAMEDMASK) { @@ -574,9 +564,7 @@ int main(int argc, char **argv) TIME_MSG("before starting main loop"); ILOG("starting main loop"); - /* - * Call the main command loop. This never returns. - */ + // Main loop: never returns. normal_enter(false, false); #if defined(WIN32) && !defined(MAKE_LIB) @@ -585,6 +573,31 @@ int main(int argc, char **argv) return 0; } +void os_exit(int r) + FUNC_ATTR_NORETURN +{ + exiting = true; + + ui_flush(); + ui_call_stop(); + ml_close_all(true); // remove all memfiles + + if (!event_teardown() && r == 0) { + r = 1; // Exit with error if main_loop did not teardown gracefully. + } + if (input_global_fd() >= 0) { + stream_set_blocking(input_global_fd(), true); // normalize stream (#2598) + } + + ILOG("Nvim exit: %d", r); + +#ifdef EXITFREE + free_all_mem(); +#endif + + exit(r); +} + /// Exit properly void getout(int exitval) FUNC_ATTR_NORETURN @@ -679,7 +692,7 @@ void getout(int exitval) garbage_collect(false); } - mch_exit(exitval); + os_exit(exitval); } /// Gets the integer value of a numeric command line argument if given, @@ -799,10 +812,10 @@ static void command_line_scan(mparm_T *parmp) // "--cmd <cmd>" execute cmd before vimrc if (STRICMP(argv[0] + argv_idx, "help") == 0) { usage(); - mch_exit(0); + os_exit(0); } else if (STRICMP(argv[0] + argv_idx, "version") == 0) { version(); - mch_exit(0); + os_exit(0); } else if (STRICMP(argv[0] + argv_idx, "api-info") == 0) { FileDescriptor fp; const int fof_ret = file_open_fd(&fp, STDOUT_FILENO, @@ -825,7 +838,7 @@ static void command_line_scan(mparm_T *parmp) if (ff_ret < 0) { msgpack_file_write_error(ff_ret); } - mch_exit(0); + os_exit(0); } else if (STRICMP(argv[0] + argv_idx, "headless") == 0) { headless_mode = true; } else if (STRICMP(argv[0] + argv_idx, "embed") == 0) { @@ -891,7 +904,7 @@ static void command_line_scan(mparm_T *parmp) case '?': // "-?" give help message (for MS-Windows) case 'h': { // "-h" give help message usage(); - mch_exit(0); + os_exit(0); } case 'H': { // "-H" start in Hebrew mode: rl + hkmap set. p_hkmap = true; @@ -988,7 +1001,7 @@ static void command_line_scan(mparm_T *parmp) } case 'v': { version(); - mch_exit(0); + os_exit(0); } case 'V': { // "-V{N}" Verbose level // default is 10: a little bit verbose @@ -1116,7 +1129,7 @@ scripterror: _("Attempt to open script file again: \"%s %s\"\n"), argv[-1], argv[0]); mch_errmsg((const char *)IObuff); - mch_exit(2); + os_exit(2); } int error; if (strequal(argv[0], "-")) { @@ -1135,7 +1148,7 @@ scripterror: _("Cannot open for reading: \"%s\": %s\n"), argv[0], os_strerror(error)); mch_errmsg((const char *)IObuff); - mch_exit(2); + os_exit(2); } save_typebuf(); break; @@ -1173,7 +1186,7 @@ scripterror: mch_errmsg(_("Cannot open for script output: \"")); mch_errmsg(argv[0]); mch_errmsg("\"\n"); - mch_exit(2); + os_exit(2); } break; } @@ -1380,7 +1393,7 @@ static void handle_quickfix(mparm_T *paramp) vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef); if (qf_init(NULL, p_ef, p_efm, true, IObuff, p_menc) < 0) { msg_putchar('\n'); - mch_exit(3); + os_exit(3); } TIME_MSG("reading errorfile"); } @@ -1943,7 +1956,7 @@ static void mainerr(const char *errstr, const char *str) mch_errmsg(prgname); mch_errmsg(" -h\"\n"); - mch_exit(1); + os_exit(1); } /// Prints version information for "nvim -v" or "nvim --version". |