diff options
| author | Eliseo Martínez <eliseomarmol@gmail.com> | 2014-05-12 02:25:17 +0200 |
|---|---|---|
| committer | Eliseo Martínez <eliseomarmol@gmail.com> | 2014-05-15 20:46:01 +0200 |
| commit | da51dc9cf202772f60bd2da975dbef257bd9237c (patch) | |
| tree | 5c16b93238a153f55634e9323077f30c8133970c /src/nvim/main.c | |
| parent | ffe61e5ba1721340ca51d56bae3ddaca415fb5bc (diff) | |
| download | rneovim-da51dc9cf202772f60bd2da975dbef257bd9237c.tar.gz rneovim-da51dc9cf202772f60bd2da975dbef257bd9237c.tar.bz2 rneovim-da51dc9cf202772f60bd2da975dbef257bd9237c.zip | |
Introduce nvim namespace: Move files.
Move files from src/ to src/nvim/.
- src/nvim/ becomes the new root dir for nvim executable sources.
- src/libnvim/ is planned to become root dir of the neovim library.
Diffstat (limited to 'src/nvim/main.c')
| -rw-r--r-- | src/nvim/main.c | 2367 |
1 files changed, 2367 insertions, 0 deletions
diff --git a/src/nvim/main.c b/src/nvim/main.c new file mode 100644 index 0000000000..828a6d5200 --- /dev/null +++ b/src/nvim/main.c @@ -0,0 +1,2367 @@ +/* + * VIM - Vi IMproved by Bram Moolenaar + * + * Do ":help uganda" in Vim to read copying and usage conditions. + * Do ":help credits" in Vim to see a list of people who contributed. + * See README.txt for an overview of the Vim source code. + */ + +#define EXTERN +#include <string.h> +#include <stdbool.h> + +#include "vim.h" +#include "main.h" +#include "blowfish.h" +#include "buffer.h" +#include "charset.h" +#include "diff.h" +#include "eval.h" +#include "ex_cmds.h" +#include "ex_cmds2.h" +#include "ex_docmd.h" +#include "fileio.h" +#include "fold.h" +#include "getchar.h" +#include "hashtab.h" +#include "if_cscope.h" +#include "mark.h" +#include "mbyte.h" +#include "memline.h" +#include "message.h" +#include "misc1.h" +#include "misc2.h" +#include "crypt.h" +#include "garray.h" +#include "log.h" +#include "memory.h" +#include "move.h" +#include "normal.h" +#include "ops.h" +#include "option.h" +#include "os_unix.h" +#include "path.h" +#include "quickfix.h" +#include "screen.h" +#include "syntax.h" +#include "term.h" +#include "ui.h" +#include "version.h" +#include "window.h" +#include "os/input.h" +#include "os/os.h" +#include "os/signal.h" + +/* Maximum number of commands from + or -c arguments. */ +#define MAX_ARG_CMDS 10 + +/* values for "window_layout" */ +#define WIN_HOR 1 /* "-o" horizontally split windows */ +#define WIN_VER 2 /* "-O" vertically split windows */ +#define WIN_TABS 3 /* "-p" windows on tab pages */ + +/* Struct for various parameters passed between main() and other functions. */ +typedef struct { + int argc; + char **argv; + + int evim_mode; /* started as "evim" */ + char_u *use_vimrc; /* vimrc from -u argument */ + + int n_commands; /* no. of commands from + or -c */ + char_u *commands[MAX_ARG_CMDS]; /* commands from + or -c arg. */ + char_u cmds_tofree[MAX_ARG_CMDS]; /* commands that need free() */ + int n_pre_commands; /* no. of commands from --cmd */ + char_u *pre_commands[MAX_ARG_CMDS]; /* commands from --cmd argument */ + + int edit_type; /* type of editing to do */ + char_u *tagname; /* tag from -t argument */ + char_u *use_ef; /* 'errorfile' from -q argument */ + + int want_full_screen; + bool stdout_isatty; /* is stdout a terminal? */ + char_u *term; /* specified terminal name */ + int ask_for_key; /* -x argument */ + int no_swap_file; /* "-n" argument used */ + int use_debug_break_level; + int window_count; /* number of windows to use */ + int window_layout; /* 0, WIN_HOR, WIN_VER or WIN_TABS */ + +#if !defined(UNIX) + int literal; /* don't expand file names */ +#endif + int diff_mode; /* start with 'diff' set */ +} mparm_T; + +/* Values for edit_type. */ +#define EDIT_NONE 0 /* no edit type yet */ +#define EDIT_FILE 1 /* file name argument[s] given, use argument list */ +#define EDIT_STDIN 2 /* read file from stdin */ +#define EDIT_TAG 3 /* tag name argument given, use tagname */ +#define EDIT_QF 4 /* start in quickfix mode */ + +#if defined(UNIX) && !defined(NO_VIM_MAIN) +static int file_owned(char *fname); +#endif +static void mainerr(int, char_u *); +#ifndef NO_VIM_MAIN +static void main_msg(char *s); +static void usage(void); +static int get_number_arg(char_u *p, int *idx, int def); +# if defined(HAVE_LOCALE_H) || defined(X_LOCALE) +static void init_locale(void); +# endif +static void parse_command_name(mparm_T *parmp); +static bool parse_char_i(char_u **input, char val); +static bool parse_string(char_u **input, char* val, int len); +static void command_line_scan(mparm_T *parmp); +static void init_params(mparm_T *parmp, int argc, char **argv); +static void init_startuptime(mparm_T *parmp); +static void allocate_generic_buffers(void); +static void check_and_set_isatty(mparm_T *parmp); +static char_u* get_fname(mparm_T *parmp); +static void set_window_layout(mparm_T *parmp); +static void load_plugins(void); +static void handle_quickfix(mparm_T *parmp); +static void handle_tag(char_u *tagname); +static void check_tty(mparm_T *parmp); +static void read_stdin(void); +static void create_windows(mparm_T *parmp); +static void edit_buffers(mparm_T *parmp); +static void exe_pre_commands(mparm_T *parmp); +static void exe_commands(mparm_T *parmp); +static void source_startup_scripts(mparm_T *parmp); +static void main_start_gui(void); +# if defined(HAS_SWAP_EXISTS_ACTION) +static void check_swap_exists_action(void); +# endif +#endif /* NO_VIM_MAIN */ + + +/* + * Different types of error messages. + */ +static char *(main_errors[]) = +{ + N_("Unknown option argument"), +#define ME_UNKNOWN_OPTION 0 + N_("Too many edit arguments"), +#define ME_TOO_MANY_ARGS 1 + N_("Argument missing after"), +#define ME_ARG_MISSING 2 + N_("Garbage after option argument"), +#define ME_GARBAGE 3 + N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"), +#define ME_EXTRA_CMD 4 + N_("Invalid argument for"), +#define ME_INVALID_ARG 5 +}; + +#ifndef NO_VIM_MAIN /* skip this for unittests */ + int main(int argc, char **argv) +{ + char_u *fname = NULL; /* file name from command line */ + mparm_T params; /* various parameters passed between + * main() and other functions. */ + /* + * Do any system-specific initialisations. These can NOT use IObuff or + * NameBuff. Thus emsg2() cannot be called! + */ + mch_early_init(); + + /* Many variables are in "params" so that we can pass them to invoked + * functions without a lot of arguments. "argc" and "argv" are also + * copied, so that they can be changed. */ + init_params(¶ms, argc, argv); + + init_startuptime(¶ms); + + (void)mb_init(); /* init mb_bytelen_tab[] to ones */ + eval_init(); /* init global variables */ + +#ifdef __QNXNTO__ + qnx_init(); /* PhAttach() for clipboard, (and gui) */ +#endif + + /* Init the table of Normal mode commands. */ + init_normal_cmds(); + + /* + * Allocate space for the generic buffers (needed for set_init_1() and + * EMSG2()). + */ + allocate_generic_buffers(); + +#if defined(HAVE_LOCALE_H) || defined(X_LOCALE) + /* + * Setup to use the current locale (for ctype() and many other things). + * NOTE: Translated messages with encodings other than latin1 will not + * work until set_init_1() has been called! + */ + init_locale(); +#endif + + /* + * Check if we have an interactive window. + * On the Amiga: If there is no window, we open one with a newcli command + * (needed for :! to * work). mch_check_win() will also handle the -d or + * -dev argument. + */ + check_and_set_isatty(¶ms); + + /* + * Allocate the first window and buffer. + * Can't do anything without it, exit when it fails. + */ + if (win_alloc_first() == FAIL) + mch_exit(0); + + init_yank(); /* init yank buffers */ + + alist_init(&global_alist); /* Init the argument list to empty. */ + + /* + * Set the default values for the options. + * NOTE: Non-latin1 translated messages are working only after this, + * because this is where "has_mbyte" will be set, which is used by + * msg_outtrans_len_attr(). + * First find out the home directory, needed to expand "~" in options. + */ + init_homedir(); /* find real value of $HOME */ + set_init_1(); + TIME_MSG("inits 1"); + + set_lang_var(); /* set v:lang and v:ctype */ + + + /* + * Figure out the way to work from the command name argv[0]. + * "vimdiff" starts diff mode, "rvim" sets "restricted", etc. + */ + parse_command_name(¶ms); + + /* + * Process the command line arguments. File names are put in the global + * argument list "global_alist". + */ + command_line_scan(¶ms); + + /* + * On some systems, when we compile with the GUI, we always use it. On Mac + * there is no terminal version, and on Windows we can't fork one off with + * :gui. + */ + + if (GARGCOUNT > 0) + fname = get_fname(¶ms); + + TIME_MSG("expanding arguments"); + + if (params.diff_mode && params.window_count == -1) + params.window_count = 0; /* open up to 3 windows */ + + /* Don't redraw until much later. */ + ++RedrawingDisabled; + + /* + * When listing swap file names, don't do cursor positioning et. al. + */ + if (recoverymode && fname == NULL) + params.want_full_screen = FALSE; + + /* + * When certain to start the GUI, don't check capabilities of terminal. + * For GTK we can't be sure, but when started from the desktop it doesn't + * make sense to try using a terminal. + */ + + + /* + * mch_init() sets up the terminal (window) for use. This must be + * done after resetting full_screen, otherwise it may move the cursor + * Note that we may use mch_exit() before mch_init()! + */ + mch_init(); + TIME_MSG("shell init"); + + + /* + * Print a warning if stdout is not a terminal. + */ + check_tty(¶ms); + + /* This message comes before term inits, but after setting "silent_mode" + * when the input is not a tty. */ + if (GARGCOUNT > 1 && !silent_mode) + printf(_("%d files to edit\n"), GARGCOUNT); + + if (params.want_full_screen && !silent_mode) { + termcapinit(params.term); /* set terminal name and get terminal + capabilities (will set full_screen) */ + screen_start(); /* don't know where cursor is now */ + TIME_MSG("Termcap init"); + } + + /* + * Set the default values for the options that use Rows and Columns. + */ + ui_get_shellsize(); /* inits Rows and Columns */ + win_init_size(); + /* Set the 'diff' option now, so that it can be checked for in a .vimrc + * file. There is no buffer yet though. */ + if (params.diff_mode) + diff_win_options(firstwin, FALSE); + + cmdline_row = Rows - p_ch; + msg_row = cmdline_row; + screenalloc(FALSE); /* allocate screen buffers */ + set_init_2(); + TIME_MSG("inits 2"); + + msg_scroll = TRUE; + no_wait_return = TRUE; + + init_highlight(TRUE, FALSE); /* set the default highlight groups */ + TIME_MSG("init highlight"); + + /* Set the break level after the terminal is initialized. */ + debug_break_level = params.use_debug_break_level; + + /* Execute --cmd arguments. */ + exe_pre_commands(¶ms); + + /* Source startup scripts. */ + source_startup_scripts(¶ms); + + /* + * Read all the plugin files. + * Only when compiled with +eval, since most plugins need it. + */ + 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. + */ + if (recoverymode && fname == NULL) { + recover_names(NULL, TRUE, 0, NULL); + mch_exit(0); + } + + /* + * Set a few option defaults after reading .vimrc files: + * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'. + */ + set_init_3(); + TIME_MSG("inits 3"); + + /* + * "-n" argument: Disable swap file by setting 'updatecount' to 0. + * Note that this overrides anything from a vimrc file. + */ + if (params.no_swap_file) + p_uc = 0; + + if (curwin->w_p_rl && p_altkeymap) { + p_hkmap = FALSE; /* Reset the Hebrew keymap mode */ + curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */ + p_fkmap = TRUE; /* Set the Farsi keymap mode */ + } + + /* + * Read in registers, history etc, but not marks, from the viminfo file. + * This is where v:oldfiles gets filled. + */ + if (*p_viminfo != NUL) { + read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES); + TIME_MSG("reading viminfo"); + } + /* It's better to make v:oldfiles an empty list than NULL. */ + if (get_vim_var_list(VV_OLDFILES) == NULL) + set_vim_var_list(VV_OLDFILES, list_alloc()); + + /* + * "-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. + */ + starting = NO_BUFFERS; + no_wait_return = FALSE; + if (!exmode_active) + msg_scroll = FALSE; + + + /* + * If "-" argument given: Read file from stdin. + * Do this before starting Raw mode, because it may change things that the + * writing end of the pipe doesn't like, e.g., in case stdin and stderr + * are the same terminal: "cat | vim -". + * Using autocommands here may cause trouble... + */ + if (params.edit_type == EDIT_STDIN && !recoverymode) + read_stdin(); + +#if defined(UNIX) + /* When switching screens and something caused a message from a vimrc + * script, need to output an extra newline on exit. */ + if ((did_emsg || msg_didout) && *T_TI != NUL) + newline_on_exit = TRUE; +#endif + + /* + * When done something that is not allowed or error message call + * wait_return. This must be done before starttermcap(), because it may + * switch to another screen. It must be done after settmode(TMODE_RAW), + * because we want to react on a single key stroke. + * Call settmode and starttermcap here, so the T_KS and T_TI may be + * defined by termcapinit and redefined in .exrc. + */ + settmode(TMODE_RAW); + TIME_MSG("setting raw mode"); + + if (need_wait_return || msg_didany) { + wait_return(TRUE); + TIME_MSG("waiting for return"); + } + + starttermcap(); /* start termcap if not done by wait_return() */ + TIME_MSG("start termcap"); + may_req_ambiguous_char_width(); + + setmouse(); /* may start using the mouse */ + if (scroll_region) + scroll_region_reset(); /* In case Rows changed */ + scroll_start(); /* may scroll the screen to the right position */ + + /* + * Don't clear the screen when starting in Ex mode, unless using the GUI. + */ + if (exmode_active) + must_redraw = CLEAR; + else { + screenclear(); /* clear screen */ + TIME_MSG("clearing screen"); + } + + if (params.ask_for_key) { + (void)blowfish_self_test(); + (void)get_crypt_key(TRUE, TRUE); + TIME_MSG("getting crypt key"); + } + + no_wait_return = TRUE; + + /* + * 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 */ + set_vim_var_string(VV_SWAPCOMMAND, NULL, -1); + + /* 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. + */ + if (params.edit_type == EDIT_QF) { + qf_jump(NULL, 0, 0, FALSE); + TIME_MSG("jump to first error"); + } + + /* + * If opened more than one window, start editing files in the other + * windows. + */ + edit_buffers(¶ms); + + if (params.diff_mode) { + win_T *wp; + + /* set options in each window for "vimdiff". */ + for (wp = firstwin; wp != NULL; wp = wp->w_next) + diff_win_options(wp, TRUE); + } + + /* + * 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. + */ + handle_tag(params.tagname); + + /* Execute any "+", "-c" and "-S" arguments. */ + if (params.n_commands > 0) + exe_commands(¶ms); + + RedrawingDisabled = 0; + redraw_all_later(NOT_VALID); + no_wait_return = FALSE; + starting = 0; + + /* Requesting the termresponse is postponed until here, so that a "-c q" + * argument doesn't make it appear in the shell Vim was started from. */ + may_req_termresponse(); + + /* start in insert mode */ + if (p_im) + need_start_insertmode = TRUE; + + apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf); + TIME_MSG("VimEnter autocommands"); + + + /* 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) + stuffcharReadbuff(K_NOP); + + + TIME_MSG("before starting main loop"); + + /* + * Call the main command loop. This never returns. + */ + main_loop(FALSE, FALSE); + + return 0; +} +#endif /* NO_VIM_MAIN */ + +/* + * Main loop: Execute Normal mode commands until exiting Vim. + * Also used to handle commands in the command-line window, until the window + * is closed. + * Also used to handle ":visual" command after ":global": execute Normal mode + * commands, return when entering Ex mode. "noexmode" is TRUE then. + */ +void +main_loop ( + int cmdwin, /* TRUE when working in the command-line window */ + int noexmode /* TRUE when return on entering Ex mode */ +) +{ + oparg_T oa; /* operator arguments */ + int previous_got_int = FALSE; /* "got_int" was TRUE */ + linenr_T conceal_old_cursor_line = 0; + linenr_T conceal_new_cursor_line = 0; + int conceal_update_lines = FALSE; + + ILOG("Starting Neovim main loop."); + + clear_oparg(&oa); + while (!cmdwin + || cmdwin_result == 0 + ) { + if (stuff_empty()) { + did_check_timestamps = FALSE; + if (need_check_timestamps) + check_timestamps(FALSE); + if (need_wait_return) /* if wait_return still needed ... */ + wait_return(FALSE); /* ... call it now */ + if (need_start_insertmode && goto_im() + && !VIsual_active + ) { + need_start_insertmode = FALSE; + stuffReadbuff((char_u *)"i"); /* start insert mode next */ + /* skip the fileinfo message now, because it would be shown + * after insert mode finishes! */ + need_fileinfo = FALSE; + } + } + + /* Reset "got_int" now that we got back to the main loop. Except when + * inside a ":g/pat/cmd" command, then the "got_int" needs to abort + * the ":g" command. + * For ":g/pat/vi" we reset "got_int" when used once. When used + * a second time we go back to Ex mode and abort the ":g" command. */ + if (got_int) { + if (noexmode && global_busy && !exmode_active && previous_got_int) { + /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was + * used and keep "got_int" set, so that it aborts ":g". */ + exmode_active = EXMODE_NORMAL; + State = NORMAL; + } else if (!global_busy || !exmode_active) { + if (!quit_more) + (void)vgetc(); /* flush all buffers */ + got_int = FALSE; + } + previous_got_int = TRUE; + } else + previous_got_int = FALSE; + + if (!exmode_active) + msg_scroll = FALSE; + quit_more = FALSE; + + /* + * If skip redraw is set (for ":" in wait_return()), don't redraw now. + * If there is nothing in the stuff_buffer or do_redraw is TRUE, + * update cursor and redraw. + */ + if (skip_redraw || exmode_active) + skip_redraw = FALSE; + else if (do_redraw || stuff_empty()) { + /* Trigger CursorMoved if the cursor moved. */ + if (!finish_op && ( + has_cursormoved() + || + curwin->w_p_cole > 0 + ) + && !equalpos(last_cursormoved, curwin->w_cursor)) { + if (has_cursormoved()) + apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, + FALSE, curbuf); + if (curwin->w_p_cole > 0) { + conceal_old_cursor_line = last_cursormoved.lnum; + conceal_new_cursor_line = curwin->w_cursor.lnum; + conceal_update_lines = TRUE; + } + last_cursormoved = curwin->w_cursor; + } + + /* Trigger TextChanged if b_changedtick differs. */ + if (!finish_op && has_textchanged() + && last_changedtick != curbuf->b_changedtick) { + if (last_changedtick_buf == curbuf) + apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL, + FALSE, curbuf); + last_changedtick_buf = curbuf; + last_changedtick = curbuf->b_changedtick; + } + + /* Scroll-binding for diff mode may have been postponed until + * here. Avoids doing it for every change. */ + if (diff_need_scrollbind) { + check_scrollbind((linenr_T)0, 0L); + diff_need_scrollbind = FALSE; + } + /* Include a closed fold completely in the Visual area. */ + foldAdjustVisual(); + /* + * When 'foldclose' is set, apply 'foldlevel' to folds that don't + * contain the cursor. + * When 'foldopen' is "all", open the fold(s) under the cursor. + * This may mark the window for redrawing. + */ + if (hasAnyFolding(curwin) && !char_avail()) { + foldCheckClose(); + if (fdo_flags & FDO_ALL) + foldOpenCursor(); + } + + /* + * Before redrawing, make sure w_topline is correct, and w_leftcol + * if lines don't wrap, and w_skipcol if lines wrap. + */ + update_topline(); + validate_cursor(); + + if (VIsual_active) + update_curbuf(INVERTED); /* update inverted part */ + else if (must_redraw) + update_screen(0); + else if (redraw_cmdline || clear_cmdline) + showmode(); + redraw_statuslines(); + if (need_maketitle) + maketitle(); + /* display message after redraw */ + if (keep_msg != NULL) { + char_u *p; + + /* msg_attr_keep() will set keep_msg to NULL, must free the + * string here. */ + p = keep_msg; + keep_msg = NULL; + msg_attr(p, keep_msg_attr); + free(p); + } + if (need_fileinfo) { /* show file info after redraw */ + fileinfo(FALSE, TRUE, FALSE); + need_fileinfo = FALSE; + } + + emsg_on_display = FALSE; /* can delete error message now */ + did_emsg = FALSE; + msg_didany = FALSE; /* reset lines_left in msg_start() */ + may_clear_sb_text(); /* clear scroll-back text on next msg */ + showruler(FALSE); + + if (conceal_update_lines + && (conceal_old_cursor_line != conceal_new_cursor_line + || conceal_cursor_line(curwin) + || need_cursor_line_redraw)) { + if (conceal_old_cursor_line != conceal_new_cursor_line + && conceal_old_cursor_line + <= curbuf->b_ml.ml_line_count) + update_single_line(curwin, conceal_old_cursor_line); + update_single_line(curwin, conceal_new_cursor_line); + curwin->w_valid &= ~VALID_CROW; + } + setcursor(); + cursor_on(); + + do_redraw = FALSE; + +#ifdef STARTUPTIME + /* Now that we have drawn the first screen all the startup stuff + * has been done, close any file for startup messages. */ + if (time_fd != NULL) { + TIME_MSG("first screen update"); + TIME_MSG("--- VIM STARTED ---"); + fclose(time_fd); + time_fd = NULL; + } +#endif + } + + /* + * Update w_curswant if w_set_curswant has been set. + * Postponed until here to avoid computing w_virtcol too often. + */ + update_curswant(); + + /* + * May perform garbage collection when waiting for a character, but + * only at the very toplevel. Otherwise we may be using a List or + * Dict internally somewhere. + * "may_garbage_collect" is reset in vgetc() which is invoked through + * do_exmode() and normal_cmd(). + */ + may_garbage_collect = (!cmdwin && !noexmode); + /* + * If we're invoked as ex, do a round of ex commands. + * Otherwise, get and execute a normal mode command. + */ + if (exmode_active) { + if (noexmode) /* End of ":global/path/visual" commands */ + return; + do_exmode(exmode_active == EXMODE_VIM); + } else + normal_cmd(&oa, TRUE); + } +} + + + + +/* Exit properly */ +void getout(int exitval) +{ + buf_T *buf; + win_T *wp; + tabpage_T *tp, *next_tp; + + exiting = TRUE; + + /* When running in Ex mode an error causes us to exit with a non-zero exit + * code. POSIX requires this, although it's not 100% clear from the + * standard. */ + if (exmode_active) + exitval += ex_exitval; + + /* Position the cursor on the last screen line, below all the text */ + windgoto((int)Rows - 1, 0); + + /* Optionally print hashtable efficiency. */ + hash_debug_results(); + + + if (get_vim_var_nr(VV_DYING) <= 1) { + /* Trigger BufWinLeave for all windows, but only once per buffer. */ + for (tp = first_tabpage; tp != NULL; tp = next_tp) { + next_tp = tp->tp_next; + for (wp = (tp == curtab) + ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next) { + if (wp->w_buffer == NULL) + /* Autocmd must have close the buffer already, skip. */ + continue; + buf = wp->w_buffer; + if (buf->b_changedtick != -1) { + apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, + buf->b_fname, FALSE, buf); + buf->b_changedtick = -1; /* note that we did it already */ + /* start all over, autocommands may mess up the lists */ + next_tp = first_tabpage; + break; + } + } + } + + /* Trigger BufUnload for buffers that are loaded */ + for (buf = firstbuf; buf != NULL; buf = buf->b_next) + if (buf->b_ml.ml_mfp != NULL) { + apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, + FALSE, buf); + if (!buf_valid(buf)) /* autocmd may delete the buffer */ + break; + } + apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf); + } + + if (p_viminfo && *p_viminfo != NUL) + /* Write out the registers, history, marks etc, to the viminfo file */ + write_viminfo(NULL, FALSE); + + if (get_vim_var_nr(VV_DYING) <= 1) + apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf); + + profile_dump(); + + if (did_emsg + ) { + /* give the user a chance to read the (error) message */ + no_wait_return = FALSE; + wait_return(FALSE); + } + + /* Position the cursor again, the autocommands may have moved it */ + windgoto((int)Rows - 1, 0); + +#if defined(USE_ICONV) && defined(DYNAMIC_ICONV) + iconv_end(); +#endif + cs_end(); + if (garbage_collect_at_exit) + garbage_collect(); + + mch_exit(exitval); +} + +#ifndef NO_VIM_MAIN +/* + * Get a (optional) count for a Vim argument. + */ +static int +get_number_arg ( + char_u *p, /* pointer to argument */ + int *idx, /* index in argument, is incremented */ + int def /* default value */ +) +{ + if (vim_isdigit(p[*idx])) { + def = atoi((char *)&(p[*idx])); + while (vim_isdigit(p[*idx])) + *idx = *idx + 1; + } + return def; +} + +#if defined(HAVE_LOCALE_H) || defined(X_LOCALE) +/* + * Setup to use the current locale (for ctype() and many other things). + */ +static void init_locale(void) +{ + setlocale(LC_ALL, ""); + +# if defined(FEAT_FLOAT) && defined(LC_NUMERIC) + /* Make sure strtod() uses a decimal point, not a comma. */ + setlocale(LC_NUMERIC, "C"); +# endif + + + { + int mustfree = FALSE; + char_u *p; + + /* expand_env() doesn't work yet, because chartab[] is not initialized + * yet, call vim_getenv() directly */ + p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree); + if (p != NULL && *p != NUL) { + vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p); + bindtextdomain(VIMPACKAGE, (char *)NameBuff); + } + if (mustfree) + free(p); + textdomain(VIMPACKAGE); + } + TIME_MSG("locale set"); +} + +#endif + +/* + * Check for: [r][e][g][vi|vim|view][diff][ex[im]] + * If the executable name starts with "r" we disable shell commands. + * If the next character is "e" we run in Easy mode. + * If the next character is "g" we run the GUI version. + * If the next characters are "view" we start in readonly mode. + * If the next characters are "diff" or "vimdiff" we start in diff mode. + * If the next characters are "ex" we start in Ex mode. If it's followed + * by "im" use improved Ex mode. + */ +static void parse_command_name(mparm_T *parmp) +{ + char_u *initstr; + + initstr = path_tail((char_u *)parmp->argv[0]); + + + set_vim_var_string(VV_PROGNAME, initstr, -1); + set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1); + + if (parse_string(&initstr, "editor", 6)) + return; + + if (parse_char_i(&initstr, 'r')) + restricted = TRUE; + + if (parse_char_i(&initstr, 'e')) + parmp->evim_mode = TRUE; + + /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */ + if (parse_char_i(&initstr, 'g')) + main_start_gui(); + + if (parse_string(&initstr, "view", 4)) { + readonlymode = TRUE; + curbuf->b_p_ro = TRUE; + p_uc = 10000; /* don't update very often */ + } else { + parse_string(&initstr, "vim", 3); /* consume "vim" if it's there */ + } + + /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */ + if (parse_string(&initstr, "diff", 4)) + parmp->diff_mode = TRUE; + + if (parse_string(&initstr, "ex", 2)) { + if (parse_string(&initstr, "im", 2)) + exmode_active = EXMODE_VIM; + else + exmode_active = EXMODE_NORMAL; + change_compatible(TRUE); /* set 'compatible' */ + } +} + +static bool parse_char_i(char_u **input, char val) +{ + if (TOLOWER_ASC(**input) == val) { + *input += 1; /* or (*input)++ WITH parens */ + return true; + } + return false; +} + +static bool parse_string(input, val, len) + char_u **input; + char *val; + int len; +{ + if (STRNICMP(*input, val, len) == 0) { + *input += len; + return true; + } + return false; +} + +/* + * Scan the command line arguments. + */ +static void command_line_scan(mparm_T *parmp) +{ + int argc = parmp->argc; + char **argv = parmp->argv; + int argv_idx; /* index in argv[n][] */ + int had_minmin = FALSE; /* found "--" argument */ + int want_argument; /* option argument with argument */ + int c; + char_u *p = NULL; + long n; + + --argc; + ++argv; + argv_idx = 1; /* active option letter is argv[0][argv_idx] */ + while (argc > 0) { + /* + * "+" or "+{number}" or "+/{pat}" or "+{command}" argument. + */ + if (argv[0][0] == '+' && !had_minmin) { + if (parmp->n_commands >= MAX_ARG_CMDS) + mainerr(ME_EXTRA_CMD, NULL); + argv_idx = -1; /* skip to next argument */ + if (argv[0][1] == NUL) + parmp->commands[parmp->n_commands++] = (char_u *)"$"; + else + parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]); + } + /* + * Optional argument. + */ + else if (argv[0][0] == '-' && !had_minmin) { + want_argument = FALSE; + c = argv[0][argv_idx++]; + switch (c) { + case NUL: /* "vim -" read from stdin */ + /* "ex -" silent mode */ + if (exmode_active) + silent_mode = TRUE; + else { + if (parmp->edit_type != EDIT_NONE) + mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]); + parmp->edit_type = EDIT_STDIN; + read_cmd_fd = 2; /* read from stderr instead of stdin */ + } + argv_idx = -1; /* skip to next argument */ + break; + + case '-': /* "--" don't take any more option arguments */ + /* "--help" give help message */ + /* "--version" give version message */ + /* "--literal" take files literally */ + /* "--nofork" don't fork */ + /* "--noplugin[s]" skip plugins */ + /* "--cmd <cmd>" execute cmd before vimrc */ + if (STRICMP(argv[0] + argv_idx, "help") == 0) + usage(); + else if (STRICMP(argv[0] + argv_idx, "version") == 0) { + Columns = 80; /* need to init Columns */ + info_message = TRUE; /* use mch_msg(), not mch_errmsg() */ + list_version(); + msg_putchar('\n'); + msg_didout = FALSE; + mch_exit(0); + } else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0) { |