diff options
Diffstat (limited to 'src/nvim/ex_docmd.c')
-rw-r--r-- | src/nvim/ex_docmd.c | 3169 |
1 files changed, 1678 insertions, 1491 deletions
diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index 3afcd9ec5a..1d7f83df99 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -4,14 +4,13 @@ // ex_docmd.c: functions for executing an Ex command line. #include <assert.h> -#include <string.h> +#include <inttypes.h> #include <stdbool.h> #include <stdlib.h> -#include <inttypes.h> +#include <string.h> -#include "nvim/vim.h" +#include "nvim/api/private/helpers.h" #include "nvim/ascii.h" -#include "nvim/ex_docmd.h" #include "nvim/buffer.h" #include "nvim/change.h" #include "nvim/charset.h" @@ -21,16 +20,26 @@ #include "nvim/edit.h" #include "nvim/eval.h" #include "nvim/eval/userfunc.h" +#include "nvim/event/rstream.h" +#include "nvim/event/wstream.h" #include "nvim/ex_cmds.h" #include "nvim/ex_cmds2.h" +#include "nvim/ex_cmds_defs.h" +#include "nvim/ex_docmd.h" #include "nvim/ex_eval.h" #include "nvim/ex_getln.h" +#include "nvim/ex_session.h" +#include "nvim/file_search.h" #include "nvim/fileio.h" #include "nvim/fold.h" #include "nvim/func_attr.h" +#include "nvim/garray.h" #include "nvim/getchar.h" +#include "nvim/globals.h" #include "nvim/hardcopy.h" #include "nvim/if_cscope.h" +#include "nvim/keymap.h" +#include "nvim/lua/executor.h" #include "nvim/main.h" #include "nvim/mark.h" #include "nvim/mbyte.h" @@ -39,20 +48,21 @@ #include "nvim/menu.h" #include "nvim/message.h" #include "nvim/misc1.h" -#include "nvim/ex_session.h" -#include "nvim/keymap.h" -#include "nvim/file_search.h" -#include "nvim/garray.h" +#include "nvim/mouse.h" #include "nvim/move.h" #include "nvim/normal.h" #include "nvim/ops.h" #include "nvim/option.h" +#include "nvim/os/input.h" +#include "nvim/os/os.h" +#include "nvim/os/time.h" #include "nvim/os_unix.h" #include "nvim/path.h" #include "nvim/quickfix.h" #include "nvim/regexp.h" #include "nvim/screen.h" #include "nvim/search.h" +#include "nvim/shada.h" #include "nvim/sign.h" #include "nvim/spell.h" #include "nvim/spellfile.h" @@ -63,18 +73,8 @@ #include "nvim/ui.h" #include "nvim/undo.h" #include "nvim/version.h" +#include "nvim/vim.h" #include "nvim/window.h" -#include "nvim/os/os.h" -#include "nvim/os/input.h" -#include "nvim/os/time.h" -#include "nvim/ex_cmds_defs.h" -#include "nvim/mouse.h" -#include "nvim/event/rstream.h" -#include "nvim/event/wstream.h" -#include "nvim/shada.h" -#include "nvim/lua/executor.h" -#include "nvim/globals.h" -#include "nvim/api/private/helpers.h" static int quitmore = 0; static bool ex_pressedreturn = false; @@ -90,9 +90,9 @@ typedef struct ucmd { char_u *uc_compl_arg; // completion argument if any } ucmd_T; -#define UC_BUFFER 1 /* -buffer: local to current buffer */ +#define UC_BUFFER 1 // -buffer: local to current buffer -static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL}; +static garray_T ucmds = { 0, 0, sizeof(ucmd_T), 4, NULL }; #define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i]) #define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i]) @@ -100,10 +100,10 @@ static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL}; // Whether a command index indicates a user command. # define IS_USER_CMDIDX(idx) ((int)(idx) < 0) -/* Struct for storing a line inside a while/for loop */ +// Struct for storing a line inside a while/for loop typedef struct { - char_u *line; /* command line */ - linenr_T lnum; /* sourcing_lnum of the line */ + char_u *line; // command line + linenr_T lnum; // sourcing_lnum of the line } wcmd_T; #define FREE_WCMD(wcmd) xfree((wcmd)->line) @@ -123,7 +123,7 @@ struct loop_cookie { }; -/* Struct to save a few things while debugging. Used in do_cmdline() only. */ +// Struct to save a few things while debugging. Used in do_cmdline() only. struct dbg_stuff { int trylevel; int force_abort; @@ -152,7 +152,7 @@ struct dbg_stuff { # include "ex_cmds_defs.generated.h" #endif -static char_u dollar_command[2] = {'$', 0}; +static char_u dollar_command[2] = { '$', 0 }; static void save_dbg_stuff(struct dbg_stuff *dsp) { @@ -198,8 +198,9 @@ void do_exmode(void) /* When using ":global /pat/ visual" and then "Q" we return to continue * the :global command. */ - if (global_busy) + if (global_busy) { return; + } save_msg_scroll = msg_scroll; RedrawingDisabled++; // don't redisplay the window @@ -207,7 +208,7 @@ void do_exmode(void) MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode.")); while (exmode_active) { - /* Check for a ":normal" command and no more characters left. */ + // Check for a ":normal" command and no more characters left. if (ex_normal_busy > 0 && typebuf.tb_len == 0) { exmode_active = false; break; @@ -232,18 +233,20 @@ void do_exmode(void) /* go up one line, to overwrite the ":<CR>" line, so the * output doesn't contain empty lines. */ msg_row = prev_msg_row; - if (prev_msg_row == Rows - 1) + if (prev_msg_row == Rows - 1) { msg_row--; + } } msg_col = 0; print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE); msg_clr_eos(); } - } else if (ex_pressedreturn && !ex_no_reprint) { /* must be at EOF */ - if (curbuf->b_ml.ml_flags & ML_EMPTY) + } else if (ex_pressedreturn && !ex_no_reprint) { // must be at EOF + if (curbuf->b_ml.ml_flags & ML_EMPTY) { EMSG(_(e_emptybuf)); - else + } else { EMSG(_("E501: At end-of-file")); + } } } @@ -301,10 +304,10 @@ int do_cmdline_cmd(const char *cmd) /// DOCMD_EXCRESET - Reset the exception environment (used for debugging). /// DOCMD_KEEPLINE - Store first typed line (for repeating with "."). /// +/// @param cookie argument for fgetline() +/// /// @return FAIL if cmdline could not be executed, OK otherwise -int do_cmdline(char_u *cmdline, LineGetter fgetline, - void *cookie, /* argument for fgetline() */ - int flags) +int do_cmdline(char_u *cmdline, LineGetter fgetline, void *cookie, int flags) { char_u *next_cmdline; // next cmd to execute char_u *cmdline_copy = NULL; // copy of cmd line @@ -333,7 +336,7 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline, struct loop_cookie cmd_loop_cookie; void *real_cookie; int getline_is_func; - static int call_depth = 0; /* recursiveness */ + static int call_depth = 0; // recursiveness /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory * location for storing error messages to be converted to an exception. @@ -363,10 +366,11 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline, real_cookie = getline_cookie(fgetline, cookie); - /* Inside a function use a higher nesting level. */ + // Inside a function use a higher nesting level. getline_is_func = getline_equal(fgetline, cookie, get_func_line); - if (getline_is_func && ex_nesting_level == func_level(real_cookie)) + if (getline_is_func && ex_nesting_level == func_level(real_cookie)) { ++ex_nesting_level; + } /* Get the function or script name and the address where the next breakpoint * line and the debug tick for a function or script are stored. */ @@ -422,13 +426,14 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline, do { getline_is_func = getline_equal(fgetline, cookie, get_func_line); - /* stop skipping cmds for an error msg after all endif/while/for */ + // stop skipping cmds for an error msg after all endif/while/for if (next_cmdline == NULL && !force_abort && cstack.cs_idx < 0 - && !(getline_is_func && func_has_abort(real_cookie)) - ) + && !(getline_is_func && + func_has_abort(real_cookie))) { did_emsg = FALSE; + } /* * 1. If repeating a line in a loop, get a line from lines_ga. @@ -436,7 +441,7 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline, * 3. If a line is given: Make a copy, so we can mess with it. */ - /* 1. If repeating, get a previous line from lines_ga. */ + // 1. If repeating, get a previous line from lines_ga. if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len) { /* Each '|' separated command is stored separately in lines_ga, to * be able to jump to it. Don't use next_cmdline now. */ @@ -445,49 +450,50 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline, /* Check if a function has returned or, unless it has an unclosed * try conditional, aborted. */ if (getline_is_func) { - if (do_profiling == PROF_YES) + if (do_profiling == PROF_YES) { func_line_end(real_cookie); + } if (func_has_ended(real_cookie)) { retval = FAIL; break; } } else if (do_profiling == PROF_YES - && getline_equal(fgetline, cookie, getsourceline)) + && getline_equal(fgetline, cookie, getsourceline)) { script_line_end(); + } - /* Check if a sourced file hit a ":finish" command. */ + // Check if a sourced file hit a ":finish" command. if (source_finished(fgetline, cookie)) { retval = FAIL; break; } - /* If breakpoints have been added/deleted need to check for it. */ + // If breakpoints have been added/deleted need to check for it. if (breakpoint != NULL && dbg_tick != NULL && *dbg_tick != debug_tick) { - *breakpoint = dbg_find_breakpoint( - getline_equal(fgetline, cookie, getsourceline), - fname, sourcing_lnum); + *breakpoint = dbg_find_breakpoint(getline_equal(fgetline, cookie, getsourceline), + fname, sourcing_lnum); *dbg_tick = debug_tick; } next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line; sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum; - /* Did we encounter a breakpoint? */ + // Did we encounter a breakpoint? if (breakpoint != NULL && *breakpoint != 0 && *breakpoint <= sourcing_lnum) { dbg_breakpoint(fname, sourcing_lnum); - /* Find next breakpoint. */ - *breakpoint = dbg_find_breakpoint( - getline_equal(fgetline, cookie, getsourceline), - fname, sourcing_lnum); + // Find next breakpoint. + *breakpoint = dbg_find_breakpoint(getline_equal(fgetline, cookie, getsourceline), + fname, sourcing_lnum); *dbg_tick = debug_tick; } if (do_profiling == PROF_YES) { - if (getline_is_func) + if (getline_is_func) { func_line_start(real_cookie); - else if (getline_equal(fgetline, cookie, getsourceline)) + } else if (getline_equal(fgetline, cookie, getsourceline)) { script_line_start(); + } } } @@ -509,7 +515,7 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline, cmd_cookie = cookie; } - /* 2. If no line given, get an allocated line with fgetline(). */ + // 2. If no line given, get an allocated line with fgetline(). if (next_cmdline == NULL) { /* * Need to set msg_didout for the first line after an ":if", @@ -539,13 +545,14 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline, */ if (flags & DOCMD_KEEPLINE) { xfree(repeat_cmdline); - if (count == 0) + if (count == 0) { repeat_cmdline = vim_strsave(next_cmdline); - else + } else { repeat_cmdline = NULL; + } } } - /* 3. Make a copy of the command so we can mess with it. */ + // 3. Make a copy of the command so we can mess with it. else if (cmdline_copy == NULL) { next_cmdline = vim_strsave(next_cmdline); } @@ -575,8 +582,8 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline, msg_didout_before_start = msg_didout; msg_didany = false; // no output yet msg_start(); - msg_scroll = TRUE; /* put messages below each other */ - ++no_wait_return; /* don't wait for return until finished */ + msg_scroll = TRUE; // put messages below each other + ++no_wait_return; // don't wait for return until finished ++RedrawingDisabled; did_inc = TRUE; } @@ -602,10 +609,11 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline, next_cmdline = NULL; } - if (cmd_cookie == (void *)&cmd_loop_cookie) + if (cmd_cookie == (void *)&cmd_loop_cookie) { /* Use "current_line" from "cmd_loop_cookie", it may have been * incremented when defining a function. */ current_line = cmd_loop_cookie.current_line; + } if (next_cmdline == NULL) { XFREE_CLEAR(cmdline_copy); @@ -627,11 +635,12 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline, } - /* reset did_emsg for a function that is not aborted by an error */ + // reset did_emsg for a function that is not aborted by an error if (did_emsg && !force_abort && getline_equal(fgetline, cookie, get_func_line) - && !func_has_abort(real_cookie)) + && !func_has_abort(real_cookie)) { did_emsg = FALSE; + } if (cstack.cs_looplevel > 0) { ++current_line; @@ -656,24 +665,24 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline, && cstack.cs_line[cstack.cs_idx] >= 0 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE)) { current_line = cstack.cs_line[cstack.cs_idx]; - /* remember we jumped there */ + // remember we jumped there cstack.cs_lflags |= CSL_HAD_LOOP; - line_breakcheck(); /* check if CTRL-C typed */ + line_breakcheck(); // check if CTRL-C typed /* Check for the next breakpoint at or after the ":while" * or ":for". */ if (breakpoint != NULL) { - *breakpoint = dbg_find_breakpoint( - getline_equal(fgetline, cookie, getsourceline), - fname, - ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1); + *breakpoint = dbg_find_breakpoint(getline_equal(fgetline, cookie, getsourceline), + fname, + ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1); *dbg_tick = debug_tick; } } else { - /* can only get here with ":endwhile" or ":endfor" */ - if (cstack.cs_idx >= 0) + // can only get here with ":endwhile" or ":endfor" + if (cstack.cs_idx >= 0) { rewind_conditionals(&cstack, cstack.cs_idx - 1, - CSF_WHILE | CSF_FOR, &cstack.cs_looplevel); + CSF_WHILE | CSF_FOR, &cstack.cs_looplevel); + } } } /* @@ -726,9 +735,8 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline, force_abort = false; } - /* Convert an interrupt to an exception if appropriate. */ + // Convert an interrupt to an exception if appropriate. (void)do_intthrow(&cstack); - } /* * Continue executing command lines when: @@ -766,14 +774,15 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline, && !source_finished(fgetline, cookie)) || (getline_equal(fgetline, cookie, get_func_line) && !func_has_ended(real_cookie)))) { - if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY) + if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY) { EMSG(_(e_endtry)); - else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE) + } else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE) { EMSG(_(e_endwhile)); - else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR) + } else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR) { EMSG(_(e_endfor)); - else + } else { EMSG(_(e_endif)); + } } /* @@ -786,10 +795,11 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline, do { int idx = cleanup_conditionals(&cstack, 0, TRUE); - if (idx >= 0) - --idx; /* remove try block not in its finally clause */ + if (idx >= 0) { + --idx; // remove try block not in its finally clause + } rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR, - &cstack.cs_looplevel); + &cstack.cs_looplevel); } while (cstack.cs_idx >= 0); trylevel = initial_trylevel; } @@ -821,8 +831,8 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline, switch (current_exception->type) { case ET_USER: vim_snprintf((char *)IObuff, IOSIZE, - _("E605: Exception not caught: %s"), - current_exception->value); + _("E605: Exception not caught: %s"), + current_exception->value); p = vim_strsave(IObuff); break; case ET_ERROR: @@ -886,27 +896,30 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline, check_cstack = true; } } else { - /* When leaving a function, reduce nesting level. */ - if (getline_equal(fgetline, cookie, get_func_line)) + // When leaving a function, reduce nesting level. + if (getline_equal(fgetline, cookie, get_func_line)) { --ex_nesting_level; + } /* * Go to debug mode when returning from a function in which we are * single-stepping. */ if ((getline_equal(fgetline, cookie, getsourceline) || getline_equal(fgetline, cookie, get_func_line)) - && ex_nesting_level + 1 <= debug_break_level) + && ex_nesting_level + 1 <= debug_break_level) { do_debug(getline_equal(fgetline, cookie, getsourceline) ? (char_u *)_("End of sourced file") : (char_u *)_("End of function")); + } } /* * Restore the exception environment (done after returning from the * debugger). */ - if (flags & DOCMD_EXCRESET) + if (flags & DOCMD_EXCRESET) { restore_dbg_stuff(&debug_saved); + } msg_list = saved_msg_list; @@ -925,8 +938,7 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline, * wait for hit-return. Also for an error situation. */ if (retval == FAIL - || (did_endif && KeyTyped && !did_emsg) - ) { + || (did_endif && KeyTyped && !did_emsg)) { need_wait_return = false; msg_didany = false; // don't wait when restarting edit } else if (need_wait_return) { @@ -957,9 +969,9 @@ static char_u *get_loop_line(int c, void *cookie, int indent, bool do_concat) char_u *line; if (cp->current_line + 1 >= cp->lines_gap->ga_len) { - if (cp->repeating) - return NULL; /* trying to read past ":endwhile"/":endfor" */ - + if (cp->repeating) { + return NULL; // trying to read past ":endwhile"/":endfor" + } // First time inside the ":while"/":for": get line normally. if (cp->getline == NULL) { line = getcmdline(c, 0L, indent, do_concat); @@ -991,13 +1003,11 @@ static void store_loop_line(garray_T *gap, char_u *line) p->lnum = sourcing_lnum; } -/* - * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals - * "func". * Otherwise return TRUE when "fgetline" equals "func". - */ -int getline_equal(LineGetter fgetline, - void *cookie, /* argument for fgetline() */ - LineGetter func) +/// If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals +/// "func". * Otherwise return TRUE when "fgetline" equals "func". +/// +/// @param cookie argument for fgetline() +int getline_equal(LineGetter fgetline, void *cookie, LineGetter func) { LineGetter gp; struct loop_cookie *cp; @@ -1014,13 +1024,11 @@ int getline_equal(LineGetter fgetline, return gp == func; } -/* - * If "fgetline" is get_loop_line(), return the cookie used by the original - * getline function. Otherwise return "cookie". - */ -void * getline_cookie(LineGetter fgetline, - void *cookie /* argument for fgetline() */ - ) +/// If "fgetline" is get_loop_line(), return the cookie used by the original +/// getline function. Otherwise return "cookie". +/// +/// @param cookie argument for fgetline() +void * getline_cookie(LineGetter fgetline, void *cookie) { LineGetter gp; struct loop_cookie *cp; @@ -1049,16 +1057,18 @@ static int compute_buffer_local_count(int addr_type, int lnum, int offset) int count = offset; buf = firstbuf; - while (buf->b_next != NULL && buf->b_fnum < lnum) + while (buf->b_next != NULL && buf->b_fnum < lnum) { buf = buf->b_next; + } while (count != 0) { count += (count < 0) ? 1 : -1; nextbuf = (offset < 0) ? buf->b_prev : buf->b_next; - if (nextbuf == NULL) + if (nextbuf == NULL) { break; + } buf = nextbuf; - if (addr_type == ADDR_LOADED_BUFFERS) - /* skip over unloaded buffers */ + if (addr_type == ADDR_LOADED_BUFFERS) { + // skip over unloaded buffers while (buf->b_ml.ml_mfp == NULL) { nextbuf = (offset < 0) ? buf->b_prev : buf->b_next; if (nextbuf == NULL) { @@ -1066,13 +1076,15 @@ static int compute_buffer_local_count(int addr_type, int lnum, int offset) } buf = nextbuf; } + } } // we might have gone too far, last buffer is not loaded if (addr_type == ADDR_LOADED_BUFFERS) { while (buf->b_ml.ml_mfp == NULL) { nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next; - if (nextbuf == NULL) + if (nextbuf == NULL) { break; + } buf = nextbuf; } } @@ -1088,8 +1100,9 @@ static int current_win_nr(const win_T *win) FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { ++nr; - if (wp == win) + if (wp == win) { break; + } } return nr; } @@ -1100,8 +1113,9 @@ static int current_tab_nr(tabpage_T *tab) FOR_ALL_TABS(tp) { ++nr; - if (tp == tab) + if (tp == tab) { break; + } } return nr; } @@ -1116,87 +1130,87 @@ static int current_tab_nr(tabpage_T *tab) static void get_wincmd_addr_type(char_u *arg, exarg_T *eap) { switch (*arg) { - case 'S': - case Ctrl_S: - case 's': - case Ctrl_N: - case 'n': - case 'j': - case Ctrl_J: - case 'k': - case Ctrl_K: - case 'T': - case Ctrl_R: - case 'r': - case 'R': - case 'K': - case 'J': - case '+': - case '-': - case Ctrl__: - case '_': - case '|': - case ']': - case Ctrl_RSB: - case 'g': - case Ctrl_G: - case Ctrl_V: - case 'v': - case 'h': - case Ctrl_H: - case 'l': - case Ctrl_L: - case 'H': - case 'L': - case '>': - case '<': - case '}': - case 'f': - case 'F': - case Ctrl_F: - case 'i': - case Ctrl_I: - case 'd': - case Ctrl_D: - // window size or any count - eap->addr_type = ADDR_OTHER; // -V1037 - break; + case 'S': + case Ctrl_S: + case 's': + case Ctrl_N: + case 'n': + case 'j': + case Ctrl_J: + case 'k': + case Ctrl_K: + case 'T': + case Ctrl_R: + case 'r': + case 'R': + case 'K': + case 'J': + case '+': + case '-': + case Ctrl__: + case '_': + case '|': + case ']': + case Ctrl_RSB: + case 'g': + case Ctrl_G: + case Ctrl_V: + case 'v': + case 'h': + case Ctrl_H: + case 'l': + case Ctrl_L: + case 'H': + case 'L': + case '>': + case '<': + case '}': + case 'f': + case 'F': + case Ctrl_F: + case 'i': + case Ctrl_I: + case 'd': + case Ctrl_D: + // window size or any count + eap->addr_type = ADDR_OTHER; // -V1037 + break; - case Ctrl_HAT: - case '^': - // buffer number - eap->addr_type = ADDR_BUFFERS; - break; + case Ctrl_HAT: + case '^': + // buffer number + eap->addr_type = ADDR_BUFFERS; + break; - case Ctrl_Q: - case 'q': - case Ctrl_C: - case 'c': - case Ctrl_O: - case 'o': - case Ctrl_W: - case 'w': - case 'W': - case 'x': - case Ctrl_X: - // window number - eap->addr_type = ADDR_WINDOWS; - break; + case Ctrl_Q: + case 'q': + case Ctrl_C: + case 'c': + case Ctrl_O: + case 'o': + case Ctrl_W: + case 'w': + case 'W': + case 'x': + case Ctrl_X: + // window number + eap->addr_type = ADDR_WINDOWS; + break; - case Ctrl_Z: - case 'z': - case 'P': - case 't': - case Ctrl_T: - case 'b': - case Ctrl_B: - case 'p': - case Ctrl_P: - case '=': - case CAR: - // no count - eap->addr_type = ADDR_NONE; - break; + case Ctrl_Z: + case 'z': + case 'P': + case 't': + case Ctrl_T: + case 'b': + case Ctrl_B: + case 'p': + case Ctrl_P: + case '=': + case CAR: + // no count + eap->addr_type = ADDR_NONE; + break; } } @@ -1217,29 +1231,25 @@ static char_u *skip_colon_white(const char_u *p, bool skipleadingwhite) return (char_u *)p; } -/* - * Execute one Ex command. - * - * If 'sourcing' is TRUE, the command will be included in the error message. - * - * 1. skip comment lines and leading space - * 2. handle command modifiers - * 3. skip over the range to find the command - * 4. parse the range - * 5. parse the command - * 6. parse arguments - * 7. switch on command name - * - * Note: "fgetline" can be NULL. - * - * This function may be called recursively! - */ -static char_u * do_one_cmd(char_u **cmdlinep, - int flags, - cstack_T *cstack, - LineGetter fgetline, - void *cookie /* argument for fgetline() */ - ) +/// Execute one Ex command. +/// +/// If 'sourcing' is TRUE, the command will be included in the error message. +/// +/// 1. skip comment lines and leading space +/// 2. handle command modifiers +/// 3. skip over the range to find the command +/// 4. parse the range +/// 5. parse the command +/// 6. parse arguments +/// 7. switch on command name +/// +/// Note: "fgetline" can be NULL. +/// +/// This function may be called recursively! +/// +/// @param cookie argument for fgetline() +static char_u * do_one_cmd(char_u **cmdlinep, int flags, cstack_T *cstack, LineGetter fgetline, + void *cookie) { char_u *p; linenr_T lnum; @@ -1257,14 +1267,15 @@ static char_u * do_one_cmd(char_u **cmdlinep, ea.line2 = 1; ex_nesting_level++; - /* When the last file has not been edited :q has to be typed twice. */ + // When the last file has not been edited :q has to be typed twice. if (quitmore - /* avoid that a function call in 'statusline' does this */ + // avoid that a function call in 'statusline' does this && !getline_equal(fgetline, cookie, get_func_line) - /* avoid that an autocommand, e.g. QuitPre, does this */ - && !getline_equal(fgetline, cookie, getnextac) - ) + // avoid that an autocommand, e.g. QuitPre, does this + && !getline_equal(fgetline, cookie, + getnextac)) { --quitmore; + } /* * Reset browse, confirm, etc.. They are restored when returning, for @@ -1421,13 +1432,14 @@ static char_u * do_one_cmd(char_u **cmdlinep, ea.line2 = curbuf->b_ml.ml_line_count; } - if (ea.line2 < 0) + if (ea.line2 < 0) { errormsg = (char_u *)_(e_invrange); - else { - if (ea.line2 == 0) + } else { + if (ea.line2 == 0) { curwin->w_cursor.lnum = 1; - else + } else { curwin->w_cursor.lnum = ea.line2; + } beginline(BL_SOL | BL_FIX); } } @@ -1452,8 +1464,9 @@ static char_u * do_one_cmd(char_u **cmdlinep, } if (p == NULL) { - if (!ea.skip) + if (!ea.skip) { errormsg = (char_u *)_("E464: Ambiguous use of user-defined command"); + } goto doend; } // Check for wrong commands. @@ -1475,8 +1488,8 @@ static char_u * do_one_cmd(char_u **cmdlinep, // set when Not Implemented const int ni = !IS_USER_CMDIDX(ea.cmdidx) - && (cmdnames[ea.cmdidx].cmd_func == ex_ni - || cmdnames[ea.cmdidx].cmd_func == ex_script_ni); + && (cmdnames[ea.cmdidx].cmd_func == ex_ni + || cmdnames[ea.cmdidx].cmd_func == ex_script_ni); // Forced commands. @@ -1502,7 +1515,7 @@ static char_u * do_one_cmd(char_u **cmdlinep, if (!MODIFIABLE(curbuf) && (ea.argt & EX_MODIFY) // allow :put in terminals && (!curbuf->terminal || ea.cmdidx != CMD_put)) { - /* Command not allowed in non-'modifiable' buffer */ + // Command not allowed in non-'modifiable' buffer errormsg = (char_u *)_(e_modifiable); goto doend; } @@ -1561,8 +1574,9 @@ static char_u * do_one_cmd(char_u **cmdlinep, ea.line1 = ea.line2; ea.line2 = lnum; } - if ((errormsg = invalid_range(&ea)) != NULL) + if ((errormsg = invalid_range(&ea)) != NULL) { goto doend; + } } if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0) { @@ -1585,17 +1599,19 @@ static char_u * do_one_cmd(char_u **cmdlinep, * option here, so things like % get expanded. */ p = replace_makeprg(&ea, p, cmdlinep); - if (p == NULL) + if (p == NULL) { goto doend; + } /* * Skip to start of argument. * Don't do this for the ":!" command, because ":!! -l" needs the space. */ - if (ea.cmdidx == CMD_bang) + if (ea.cmdidx == CMD_bang) { ea.arg = p; - else + } else { ea.arg = skipwhite(p); + } // ":file" cannot be run with an argument when "curbuf->b_ro_locked" is set if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked()) { @@ -1616,14 +1632,14 @@ static char_u * do_one_cmd(char_u **cmdlinep, } if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update) { - if (*ea.arg == '>') { /* append */ - if (*++ea.arg != '>') { /* typed wrong */ + if (*ea.arg == '>') { // append + if (*++ea.arg != '>') { // typed wrong errormsg = (char_u *)_("E494: Use w or w>>"); goto doend; } ea.arg = skipwhite(ea.arg + 1); ea.append = TRUE; - } else if (*ea.arg == '!' && ea.cmdidx == CMD_write) { /* :w !filter */ + } else if (*ea.arg == '!' && ea.cmdidx == CMD_write) { // :w !filter ++ea.arg; ea.usefilter = TRUE; } @@ -1631,9 +1647,9 @@ static char_u * do_one_cmd(char_u **cmdlinep, if (ea.cmdidx == CMD_read) { if (ea.forceit) { - ea.usefilter = TRUE; /* :r! filter if ea.forceit */ + ea.usefilter = TRUE; // :r! filter if ea.forceit ea.forceit = FALSE; - } else if (*ea.arg == '!') { /* :r !filter */ + } else if (*ea.arg == '!') { // :r !filter ++ea.arg; ea.usefilter = TRUE; } @@ -1641,7 +1657,7 @@ static char_u * do_one_cmd(char_u **cmdlinep, if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift) { ea.amount = 1; - while (*ea.arg == *ea.cmd) { /* count number of '>' or '<' */ + while (*ea.arg == *ea.cmd) { // count number of '>' or '<' ++ea.arg; ++ea.amount; } @@ -1692,67 +1708,67 @@ static char_u * do_one_cmd(char_u **cmdlinep, ea.line1 = 1; switch (ea.addr_type) { - case ADDR_LINES: - case ADDR_OTHER: - ea.line2 = curbuf->b_ml.ml_line_count; - break; - case ADDR_LOADED_BUFFERS: - buf = firstbuf; - while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL) { - buf = buf->b_next; - } - ea.line1 = buf->b_fnum; - buf = lastbuf; - while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL) { - buf = buf->b_prev; - } - ea.line2 = buf->b_fnum; - break; - case ADDR_BUFFERS: - ea.line1 = firstbuf->b_fnum; - ea.line2 = lastbuf->b_fnum; - break; - case ADDR_WINDOWS: - ea.line2 = LAST_WIN_NR; - break; - case ADDR_TABS: - ea.line2 = LAST_TAB_NR; - break; - case ADDR_TABS_RELATIVE: + case ADDR_LINES: + case ADDR_OTHER: + ea.line2 = curbuf->b_ml.ml_line_count; + break; + case ADDR_LOADED_BUFFERS: + buf = firstbuf; + while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL) { + buf = buf->b_next; + } + ea.line1 = buf->b_fnum; + buf = lastbuf; + while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL) { + buf = buf->b_prev; + } + ea.line2 = buf->b_fnum; + break; + case ADDR_BUFFERS: + ea.line1 = firstbuf->b_fnum; + ea.line2 = lastbuf->b_fnum; + break; + case ADDR_WINDOWS: + ea.line2 = LAST_WIN_NR; + break; + case ADDR_TABS: + ea.line2 = LAST_TAB_NR; + break; + case ADDR_TABS_RELATIVE: + ea.line2 = 1; + break; + case ADDR_ARGUMENTS: + if (ARGCOUNT == 0) { + ea.line1 = ea.line2 = 0; + } else { + ea.line2 = ARGCOUNT; + } + break; + case ADDR_QUICKFIX_VALID: + ea.line2 = qf_get_valid_size(&ea); + if (ea.line2 == 0) { ea.line2 = 1; - break; - case ADDR_ARGUMENTS: - if (ARGCOUNT == 0) { - ea.line1 = ea.line2 = 0; - } else { - ea.line2 = ARGCOUNT; - } - break; - case ADDR_QUICKFIX_VALID: - ea.line2 = qf_get_valid_size(&ea); - if (ea.line2 == 0) { - ea.line2 = 1; - } - break; - case ADDR_NONE: - case ADDR_UNSIGNED: - case ADDR_QUICKFIX: - IEMSG(_("INTERNAL: Cannot use EX_DFLALL " - "with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX")); - break; + } + break; + case ADDR_NONE: + case ADDR_UNSIGNED: + case ADDR_QUICKFIX: + IEMSG(_("INTERNAL: Cannot use EX_DFLALL " + "with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX")); + break; } } // accept numbered register only when no count allowed (:put) if ((ea.argt & EX_REGSTR) && *ea.arg != NUL - /* Do not allow register = for user commands */ + // Do not allow register = for user commands && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=') && !((ea.argt & EX_COUNT) && ascii_isdigit(*ea.arg))) { if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put && !IS_USER_CMDIDX(ea.cmdidx)))) { ea.regname = *ea.arg++; - /* for '=' register: accept the rest of the line as an expression */ + // for '=' register: accept the rest of the line as an expression if (ea.arg[-1] == '=' && ea.arg[0] != NUL) { set_expr_line(vim_strsave(ea.arg)); ea.arg += STRLEN(ea.arg); @@ -1776,8 +1792,9 @@ static char_u * do_one_cmd(char_u **cmdlinep, } if (ea.addr_type != ADDR_LINES) { // e.g. :buffer 2, :sleep 3 ea.line2 = n; - if (ea.addr_count == 0) + if (ea.addr_count == 0) { ea.addr_count = 1; + } } else { ea.line1 = ea.line2; ea.line2 += n - 1; @@ -1815,7 +1832,7 @@ static char_u * do_one_cmd(char_u **cmdlinep, */ if (ea.skip) { switch (ea.cmdidx) { - /* commands that need evaluation */ + // commands that need evaluation case CMD_while: case CMD_endwhile: case CMD_for: @@ -1916,20 +1933,20 @@ static char_u * do_one_cmd(char_u **cmdlinep, * number. Don't do this for a user command. */ if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0 - && !IS_USER_CMDIDX(ea.cmdidx) - ) { + && !IS_USER_CMDIDX(ea.cmdidx)) { /* * :bdelete, :bwipeout and :bunload take several arguments, separated * by spaces: find next space (skipping over escaped characters). * The others take one argument: ignore trailing spaces. */ if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout - || ea.cmdidx == CMD_bunload) + || ea.cmdidx == CMD_bunload) { p = skiptowhite_esc(ea.arg); - else { + } else { p = ea.arg + STRLEN(ea.arg); - while (p > ea.arg && ascii_iswhite(p[-1])) + while (p > ea.arg && ascii_iswhite(p[-1])) { --p; + } } ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0, false, false); @@ -1962,8 +1979,9 @@ static char_u * do_one_cmd(char_u **cmdlinep, */ ea.errmsg = NULL; (cmdnames[ea.cmdidx].cmd_func)(&ea); - if (ea.errmsg != NULL) + if (ea.errmsg != NULL) { errormsg = (char_u *)_(ea.errmsg); + } } /* @@ -1973,14 +1991,15 @@ static char_u * do_one_cmd(char_u **cmdlinep, * exception, or reanimate a returned function or finished script file and * return or finish it again. */ - if (need_rethrow) + if (need_rethrow) { do_throw(cstack); - else if (check_cstack) { - if (source_finished(fgetline, cookie)) + } else if (check_cstack) { + if (source_finished(fgetline, cookie)) { do_finish(&ea, TRUE); - else if (getline_equal(fgetline, cookie, get_func_line) - && current_func_returned()) + } else if (getline_equal(fgetline, cookie, get_func_line) + && current_func_returned()) { do_return(&ea, TRUE, FALSE, NULL); + } } need_rethrow = check_cstack = FALSE; @@ -2002,7 +2021,7 @@ doend: emsg(errormsg); } do_errthrow(cstack, - (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx)) + (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx)) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL); @@ -2014,8 +2033,9 @@ doend: sandbox--; } - if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */ + if (ea.nextcmd && *ea.nextcmd == NUL) { // not really a next command ea.nextcmd = NULL; + } --ex_nesting_level; @@ -2074,12 +2094,15 @@ int parse_command_modifiers(exarg_T *eap, char_u **errormsg, bool skip_only) p = skip_range(eap->cmd, NULL); switch (*p) { // When adding an entry, also modify cmd_exists(). - case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3)) + case 'a': + if (!checkforcmd(&eap->cmd, "aboveleft", 3)) { break; + } cmdmod.split |= WSP_ABOVE; continue; - case 'b': if (checkforcmd(&eap->cmd, "belowright", 3)) { + case 'b': + if (checkforcmd(&eap->cmd, "belowright", 3)) { cmdmod.split |= WSP_BELOW; continue; } @@ -2093,15 +2116,18 @@ int parse_command_modifiers(exarg_T *eap, char_u **errormsg, bool skip_only) cmdmod.split |= WSP_BOT; continue; - case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4)) + case 'c': + if (!checkforcmd(&eap->cmd, "confirm", 4)) { break; + } cmdmod.confirm = true; continue; - case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3)) { + case 'k': + if (checkforcmd(&eap->cmd, "keepmarks", 3)) { cmdmod.keepmarks = true; continue; - } + } if (checkforcmd(&eap->cmd, "keepalt", 5)) { cmdmod.keepalt = true; continue; @@ -2117,49 +2143,52 @@ int parse_command_modifiers(exarg_T *eap, char_u **errormsg, bool skip_only) continue; case 'f': { // only accept ":filter {pat} cmd" - char_u *reg_pat; + char_u *reg_pat; - if (!checkforcmd(&p, "filter", 4) || *p == NUL || ends_excmd(*p)) { - break; - } - if (*p == '!') { - cmdmod.filter_force = true; - p = skipwhite(p + 1); - if (*p == NUL || ends_excmd(*p)) { + if (!checkforcmd(&p, "filter", 4) || *p == NUL || ends_excmd(*p)) { break; } - } - if (skip_only) { - p = skip_vimgrep_pat(p, NULL, NULL); - } else { - // NOTE: This puts a NUL after the pattern. - p = skip_vimgrep_pat(p, ®_pat, NULL); - } - if (p == NULL || *p == NUL) { - break; - } - if (!skip_only) { - cmdmod.filter_regmatch.regprog = vim_regcomp(reg_pat, RE_MAGIC); - if (cmdmod.filter_regmatch.regprog == NULL) { + if (*p == '!') { + cmdmod.filter_force = true; + p = skipwhite(p + 1); + if (*p == NUL || ends_excmd(*p)) { + break; + } + } + if (skip_only) { + p = skip_vimgrep_pat(p, NULL, NULL); + } else { + // NOTE: This puts a NUL after the pattern. + p = skip_vimgrep_pat(p, ®_pat, NULL); + } + if (p == NULL || *p == NUL) { break; } + if (!skip_only) { + cmdmod.filter_regmatch.regprog = vim_regcomp(reg_pat, RE_MAGIC); + if (cmdmod.filter_regmatch.regprog == NULL) { + break; + } + } + eap->cmd = p; + continue; } - eap->cmd = p; - continue; - } // ":hide" and ":hide | cmd" are not modifiers - case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3) - || *p == NUL || ends_excmd(*p)) + case 'h': + if (p != eap->cmd || !checkforcmd(&p, "hide", 3) + || *p == NUL || ends_excmd(*p)) { break; + } eap->cmd = p; cmdmod.hide = true; continue; - case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3)) { + case 'l': + if (checkforcmd(&eap->cmd, "lockmarks", 3)) { cmdmod.lockmarks = true; continue; - } + } if (!checkforcmd(&eap->cmd, "leftabove", 5)) { break; @@ -2184,12 +2213,15 @@ int parse_command_modifiers(exarg_T *eap, char_u **errormsg, bool skip_only) cmdmod.noswapfile = true; continue; - case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6)) + case 'r': + if (!checkforcmd(&eap->cmd, "rightbelow", 6)) { break; + } cmdmod.split |= WSP_BELOW; continue; - case 's': if (checkforcmd(&eap->cmd, "sandbox", 3)) { + case 's': + if (checkforcmd(&eap->cmd, "sandbox", 3)) { if (!skip_only) { if (!eap->did_sandbox) { sandbox++; @@ -2197,7 +2229,7 @@ int parse_command_modifiers(exarg_T *eap, char_u **errormsg, bool skip_only) eap->did_sandbox = true; } continue; - } + } if (!checkforcmd(&eap->cmd, "silent", 3)) { break; } @@ -2217,32 +2249,34 @@ int parse_command_modifiers(exarg_T *eap, char_u **errormsg, bool skip_only) } continue; - case 't': if (checkforcmd(&p, "tab", 3)) { - if (!skip_only) { - long tabnr = get_address( - eap, &eap->cmd, ADDR_TABS, eap->skip, skip_only, false, 1); + case 't': + if (checkforcmd(&p, "tab", 3)) { + if (!skip_only) { + long tabnr = get_address(eap, &eap->cmd, ADDR_TABS, eap->skip, skip_only, false, 1); - if (tabnr == MAXLNUM) { - cmdmod.tab = tabpage_index(curtab) + 1; - } else { - if (tabnr < 0 || tabnr > LAST_TAB_NR) { - *errormsg = (char_u *)_(e_invrange); - return false; + if (tabnr == MAXLNUM) { + cmdmod.tab = tabpage_index(curtab) + 1; + } else { + if (tabnr < 0 || tabnr > LAST_TAB_NR) { + *errormsg = (char_u *)_(e_invrange); + return false; + } + cmdmod.tab = tabnr + 1; } - cmdmod.tab = tabnr + 1; } + eap->cmd = p; + continue; } - eap->cmd = p; - continue; - } if (!checkforcmd(&eap->cmd, "topleft", 2)) { break; } cmdmod.split |= WSP_TOP; continue; - case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3)) + case 'u': + if (!checkforcmd(&eap->cmd, "unsilent", 3)) { break; + } if (!skip_only) { if (eap->save_msg_silent == -1) { eap->save_msg_silent = msg_silent; @@ -2251,12 +2285,14 @@ int parse_command_modifiers(exarg_T *eap, char_u **errormsg, bool skip_only) } continue; - case 'v': if (checkforcmd(&eap->cmd, "vertical", 4)) { + case 'v': + if (checkforcmd(&eap->cmd, "vertical", 4)) { cmdmod.split |= WSP_VERT; continue; - } - if (!checkforcmd(&p, "verbose", 4)) + } + if (!checkforcmd(&p, "verbose", 4)) { break; + } if (!skip_only) { if (eap->verbose_save < 0) { eap->verbose_save = p_verbose; @@ -2328,40 +2364,40 @@ int parse_cmd_address(exarg_T *eap, char_u **errormsg, bool silent) for (;;) { eap->line1 = eap->line2; switch (eap->addr_type) { - case ADDR_LINES: - case ADDR_OTHER: - // default is current line number - eap->line2 = curwin->w_cursor.lnum; - break; - case ADDR_WINDOWS: - eap->line2 = CURRENT_WIN_NR; - break; - case ADDR_ARGUMENTS: - eap->line2 = curwin->w_arg_idx + 1; - if (eap->line2 > ARGCOUNT) { - eap->line2 = ARGCOUNT; - } - break; - case ADDR_LOADED_BUFFERS: - case ADDR_BUFFERS: - eap->line2 = curbuf->b_fnum; - break; - case ADDR_TABS: - eap->line2 = CURRENT_TAB_NR; - break; - case ADDR_TABS_RELATIVE: - case ADDR_UNSIGNED: - eap->line2 = 1; - break; - case ADDR_QUICKFIX: - eap->line2 = qf_get_cur_idx(eap); - break; - case ADDR_QUICKFIX_VALID: - eap->line2 = qf_get_cur_valid_idx(eap); - break; - case ADDR_NONE: - // Will give an error later if a range is found. - break; + case ADDR_LINES: + case ADDR_OTHER: + // default is current line number + eap->line2 = curwin->w_cursor.lnum; + break; + case ADDR_WINDOWS: + eap->line2 = CURRENT_WIN_NR; + break; + case ADDR_ARGUMENTS: + eap->line2 = curwin->w_arg_idx + 1; + if (eap->line2 > ARGCOUNT) { + eap->line2 = ARGCOUNT; + } + break; + case ADDR_LOADED_BUFFERS: + case ADDR_BUFFERS: + eap->line2 = curbuf->b_fnum; + break; + case ADDR_TABS: + eap->line2 = CURRENT_TAB_NR; + break; + case ADDR_TABS_RELATIVE: + case ADDR_UNSIGNED: + eap->line2 = 1; + break; + case ADDR_QUICKFIX: + eap->line2 = qf_get_cur_idx(eap); + break; + case ADDR_QUICKFIX_VALID: + eap->line2 = qf_get_cur_valid_idx(eap); + break; + case ADDR_NONE: + // Will give an error later if a range is found. + break; } eap->cmd = skipwhite(eap->cmd); lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent, @@ -2373,12 +2409,12 @@ int parse_cmd_address(exarg_T *eap, char_u **errormsg, bool silent) if (*eap->cmd == '%') { // '%' - all lines eap->cmd++; switch (eap->addr_type) { - case ADDR_LINES: - case ADDR_OTHER: - eap->line1 = 1; - eap->line2 = curbuf->b_ml.ml_line_count; - break; - case ADDR_LOADED_BUFFERS: { + case ADDR_LINES: + case ADDR_OTHER: + eap->line1 = 1; + eap->line2 = curbuf->b_ml.ml_line_count; + break; + case ADDR_LOADED_BUFFERS: { buf_T *buf = firstbuf; while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL) { @@ -2392,46 +2428,46 @@ int parse_cmd_address(exarg_T *eap, char_u **errormsg, bool silent) eap->line2 = buf->b_fnum; break; } - case ADDR_BUFFERS: - eap->line1 = firstbuf->b_fnum; - eap->line2 = lastbuf->b_fnum; - break; - case ADDR_WINDOWS: - case ADDR_TABS: - if (IS_USER_CMDIDX(eap->cmdidx)) { - eap->line1 = 1; - eap->line2 = eap->addr_type == ADDR_WINDOWS + case ADDR_BUFFERS: + eap->line1 = firstbuf->b_fnum; + eap->line2 = lastbuf->b_fnum; + break; + case ADDR_WINDOWS: + case ADDR_TABS: + if (IS_USER_CMDIDX(eap->cmdidx)) { + eap->line1 = 1; + eap->line2 = eap->addr_type == ADDR_WINDOWS ? LAST_WIN_NR : LAST_TAB_NR; - } else { - // there is no Vim command which uses '%' and - // ADDR_WINDOWS or ADDR_TABS - *errormsg = (char_u *)_(e_invrange); - return FAIL; - } - break; - case ADDR_TABS_RELATIVE: - case ADDR_UNSIGNED: - case ADDR_QUICKFIX: + } else { + // there is no Vim command which uses '%' and + // ADDR_WINDOWS or ADDR_TABS *errormsg = (char_u *)_(e_invrange); return FAIL; - case ADDR_ARGUMENTS: - if (ARGCOUNT == 0) { - eap->line1 = eap->line2 = 0; - } else { - eap->line1 = 1; - eap->line2 = ARGCOUNT; - } - break; - case ADDR_QUICKFIX_VALID: + } + break; + case ADDR_TABS_RELATIVE: + case ADDR_UNSIGNED: + case ADDR_QUICKFIX: + *errormsg = (char_u *)_(e_invrange); + return FAIL; + case ADDR_ARGUMENTS: + if (ARGCOUNT == 0) { + eap->line1 = eap->line2 = 0; + } else { eap->line1 = 1; - eap->line2 = qf_get_valid_size(eap); - if (eap->line2 == 0) { - eap->line2 = 1; - } - break; - case ADDR_NONE: - // Will give an error later if a range is found. - break; + eap->line2 = ARGCOUNT; + } + break; + case ADDR_QUICKFIX_VALID: + eap->line1 = 1; + eap->line2 = qf_get_valid_size(eap); + if (eap->line2 == 0) { + eap->line2 = 1; + } + break; + case ADDR_NONE: + // Will give an error later if a range is found. + break; } eap->addr_count++; } else if (*eap->cmd == '*') { @@ -2487,22 +2523,21 @@ int parse_cmd_address(exarg_T *eap, char_u **errormsg, bool silent) return OK; } -/* - * Check for an Ex command with optional tail. - * If there is a match advance "pp" to the argument and return TRUE. - */ -int -checkforcmd( - char_u **pp, // start of command - char *cmd, // name of command - int len // required length -) +/// Check for an Ex command with optional tail. +/// If there is a match advance "pp" to the argument and return TRUE. +/// +/// @param pp start of command +/// @param cmd name of command +/// @param len required length +int checkforcmd(char_u **pp, char *cmd, int len) { int i; - for (i = 0; cmd[i] != NUL; ++i) - if (((char_u *)cmd)[i] != (*pp)[i]) + for (i = 0; cmd[i] != NUL; ++i) { + if (((char_u *)cmd)[i] != (*pp)[i]) { break; + } + } if (i >= len && !isalpha((*pp)[i])) { *pp = skipwhite(*pp + i); return TRUE; @@ -2527,8 +2562,9 @@ static void append_command(char_u *cmd) s += 2; STRCPY(d, "<a0>"); d += 4; - } else + } else { MB_COPY_CHAR(s, d); + } } *d = NUL; } @@ -2550,8 +2586,8 @@ static char_u *find_command(exarg_T *eap, int *full) * Exceptions: * - the 'k' command can directly be followed by any character. * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r' - * but :sre[wind] is another command, as are :scr[iptnames], - * :scs[cope], :sim[alt], :sig[ns] and :sil[ent]. + * but :sre[wind] is another command, as are :scr[iptnames], + * :scs[cope], :sim[alt], :sig[ns] and :sil[ent]. * - the "d" command can directly be followed by 'l' or 'p' flag. */ p = eap->cmd; @@ -2571,29 +2607,36 @@ static char_u *find_command(exarg_T *eap, int *full) eap->cmdidx = CMD_substitute; ++p; } else { - while (ASCII_ISALPHA(*p)) + while (ASCII_ISALPHA(*p)) { ++p; - /* for python 3.x support ":py3", ":python3", ":py3file", etc. */ - if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y') - while (ASCII_ISALNUM(*p)) + } + // for python 3.x support ":py3", ":python3", ":py3file", etc. + if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y') { + while (ASCII_ISALNUM(*p)) { ++p; + } + } - /* check for non-alpha command */ - if (p == eap->cmd && vim_strchr((char_u *)"@!=><&~#", *p) != NULL) + // check for non-alpha command + if (p == eap->cmd && vim_strchr((char_u *)"@!=><&~#", *p) != NULL) { ++p; + } len = (int)(p - eap->cmd); if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p')) { /* Check for ":dl", ":dell", etc. to ":deletel": that's * :delete with the 'l' flag. Same for 'p'. */ - for (i = 0; i < len; ++i) - if (eap->cmd[i] != ((char_u *)"delete")[i]) + for (i = 0; i < len; ++i) { + if (eap->cmd[i] != ((char_u *)"delete")[i]) { break; + } + } if (i == len - 1) { --len; - if (p[-1] == 'l') + if (p[-1] == 'l') { eap->flags |= EXFLAG_LIST; - else + } else { eap->flags |= EXFLAG_PRINT; + } } } @@ -2617,51 +2660,51 @@ static char_u *find_command(exarg_T *eap, int *full) } for (; (int)eap->cmdidx < (int)CMD_SIZE; - eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1)) + eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1)) { if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd, - (size_t)len) == 0) { + (size_t)len) == 0) { if (full != NULL - && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL) + && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL) { *full = TRUE; + } break; } + } // Look for a user defined command as a last resort. if ((eap->cmdidx == CMD_SIZE) && *eap->cmd >= 'A' && *eap->cmd <= 'Z') { - /* User defined commands may contain digits. */ - while (ASCII_ISALNUM(*p)) + // User defined commands may contain digits. + while (ASCII_ISALNUM(*p)) { ++p; + } p = find_ucmd(eap, p, full, NULL, NULL); } - if (p == eap->cmd) + if (p == eap->cmd) { eap->cmdidx = CMD_SIZE; + } } return p; } -/* - * Search for a user command that matches "eap->cmd". - * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx". - * Return a pointer to just after the command. - * Return NULL if there is no matching command. - */ -static char_u * -find_ucmd ( - exarg_T *eap, - char_u *p, // end of the command (possibly including count) - int *full, // set to TRUE for a full match - expand_T *xp, // used for completion, NULL otherwise - int *complp // completion flags or NULL -) +/// Search for a user command that matches "eap->cmd". +/// Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx". +/// Return a pointer to just after the command. +/// Return NULL if there is no matching command. +/// +/// @param *p end of the command (possibly including count) +/// @param full set to TRUE for a full match +/// @param xp used for completion, NULL otherwise +/// @param complp completion flags or NULL +static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *complp) { int len = (int)(p - eap->cmd); int j, k, matchlen = 0; ucmd_T *uc; int found = FALSE; int possible = FALSE; - char_u *cp, *np; /* Point into typed cmd and test name */ + char_u *cp, *np; // Point into typed cmd and test name garray_T *gap; int amb_local = FALSE; /* Found ambiguous buffer-local command, only full match global is accepted. */ @@ -2676,15 +2719,17 @@ find_ucmd ( cp = eap->cmd; np = uc->uc_name; k = 0; - while (k < len && *np != NUL && *cp++ == *np++) + while (k < len && *np != NUL && *cp++ == *np++) { k++; + } if (k == len || (*np == NUL && ascii_isdigit(eap->cmd[k]))) { /* If finding a second match, the command is ambiguous. But * not if a buffer-local command wasn't a full match and a * global command is a full match. */ if (k == len && found && *np != NUL) { - if (gap == &ucmds) + if (gap == &ucmds) { return NULL; + } amb_local = TRUE; } @@ -2693,15 +2738,17 @@ find_ucmd ( * be another command including the digit that we * should use instead. */ - if (k == len) + if (k == len) { found = TRUE; - else + } else { possible = TRUE; + } - if (gap == &ucmds) + if (gap == &ucmds) { eap->cmdidx = CMD_USER; - else + } else { eap->cmdidx = CMD_USER_BUF; + } eap->argt = uc->uc_argt; eap->useridx = j; eap->addr_type = uc->uc_addr_type; @@ -2718,8 +2765,9 @@ find_ucmd ( * if this is an exact match. */ matchlen = k; if (k == len && *np == NUL) { - if (full != NULL) + if (full != NULL) { *full = TRUE; + } amb_local = FALSE; break; } @@ -2727,30 +2775,33 @@ find_ucmd ( } } - /* Stop if we found a full match or searched all. */ - if (j < gap->ga_len || gap == &ucmds) + // Stop if we found a full match or searched all. + if (j < gap->ga_len || gap == &ucmds) { break; + } gap = &ucmds; } - /* Only found ambiguous matches. */ + // Only found ambiguous matches. if (amb_local) { - if (xp != NULL) + if (xp != NULL) { xp->xp_context = EXPAND_UNSUCCESSFUL; + } return NULL; } /* The match we found may be followed immediately by a number. Move "p" * back to point to it. */ - if (found || possible) + if (found || possible) { return p + (matchlen - len); + } return p; } static struct cmdmod { char *name; int minlen; - int has_count; /* :123verbose :3tab */ + int has_count; // :123verbose :3tab } cmdmods[] = { { "aboveleft", 3, false }, { "belowright", 3, false }, @@ -2833,26 +2884,26 @@ int cmd_exists(const char *const name) ea.cmdidx = (cmdidx_T)0; int full = false; p = find_command(&ea, &full); - if (p == NULL) + if (p == NULL) { return 3; - if (ascii_isdigit(*name) && ea.cmdidx != CMD_match) + } + if (ascii_isdigit(*name) && ea.cmdidx != CMD_match) { return 0; - if (*skipwhite(p) != NUL) - return 0; /* trailing garbage */ + } + if (*skipwhite(p) != NUL) { + return 0; // trailing garbage + } return ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1); } -/* - * This is all pretty much copied from do_one_cmd(), with all the extra stuff - * we don't need/want deleted. Maybe this could be done better if we didn't - * repeat all this stuff. The only problem is that they may not stay - * perfectly compatible with each other, but then the command line syntax - * probably won't change that much -- webb. - */ -const char * set_one_cmd_context( - expand_T *xp, - const char *buff // buffer for command string -) +/// This is all pretty much copied from do_one_cmd(), with all the extra stuff +/// we don't need/want deleted. Maybe this could be done better if we didn't +/// repeat all this stuff. The only problem is that they may not stay +/// perfectly compatible with each other, but then the command line syntax +/// probably won't change that much -- webb. +/// +/// @param buff buffer for command string +const char * set_one_cmd_context(expand_T *xp, const char *buff) { size_t len = 0; exarg_T ea; @@ -2871,9 +2922,10 @@ const char * set_one_cmd_context( } xp->xp_pattern = (char_u *)cmd; - if (*cmd == NUL) + if (*cmd == NUL) { return NULL; - if (*cmd == '"') { /* ignore comment lines */ + } + if (*cmd == '"') { // ignore comment lines xp->xp_context = EXPAND_NOTHING; return NULL; } @@ -2895,9 +2947,9 @@ const char * set_one_cmd_context( return NULL; } - if (*cmd == '|' || *cmd == '\n') - return cmd + 1; /* There's another command */ - + if (*cmd == '|' || *cmd == '\n') { + return cmd + 1; // There's another command + } /* * Isolate the command and search for it in the command table. * Exceptions: @@ -2972,12 +3024,12 @@ const char * set_one_cmd_context( } } if (ea.cmdidx == CMD_SIZE) { - /* Not still touching the command and it was an illegal one */ + // Not still touching the command and it was an illegal one xp->xp_context = EXPAND_UNSUCCESSFUL; return NULL; } - xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */ + xp->xp_context = EXPAND_NOTHING; // Default now that we're past command if (*p == '!') { // forced commands forceit = true; @@ -3050,9 +3102,10 @@ const char * set_one_cmd_context( */ if ((ea.argt & EX_TRLBAR) && !usefilter) { p = arg; - /* ":redir @" is not the start of a comment */ - if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"') + // ":redir @" is not the start of a comment + if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"') { p += 2; + } while (*p) { if (*p == Ctrl_V) { if (p[1] != NUL) { @@ -3062,9 +3115,10 @@ const char * set_one_cmd_context( || *p == '|' || *p == '\n') { if (*(p - 1) != '\\') { - if (*p == '|' || *p == '\n') + if (*p == '|' || *p == '\n') { return p + 1; - return NULL; /* It's a comment */ + } + return NULL; // It's a comment } } MB_PTR_ADV(p); @@ -3076,7 +3130,7 @@ const char * set_one_cmd_context( return NULL; } - /* Find start of last argument (argument just before cursor): */ + // Find start of last argument (argument just before cursor): p = buff; xp->xp_pattern = (char_u *)p; len = strlen(buff); @@ -3117,10 +3171,10 @@ const char * set_one_cmd_context( /* An argument can contain just about everything, except * characters that end the command and white space. */ else if (c == '|' - || c == '\n' - || c == '"' - || ascii_iswhite(c)) { - len = 0; /* avoid getting stuck when space is in 'isfname' */ + || c == '\n' + || c == '"' + || ascii_iswhite(c)) { + len = 0; // avoid getting stuck when space is in 'isfname' while (*p != NUL) { c = utf_ptr2char((const char_u *)p); if (c == '`' || vim_isfilec_or_wc(c)) { @@ -3148,7 +3202,7 @@ const char * set_one_cmd_context( } xp->xp_context = EXPAND_FILES; - /* For a shell command more chars need to be escaped. */ + // For a shell command more chars need to be escaped. if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal) { #ifndef BACKSLASH_IN_FILENAME xp->xp_shell = TRUE; @@ -3175,7 +3229,7 @@ const char * set_one_cmd_context( } } } - /* Check for user names */ + // Check for user names if (*xp->xp_pattern == '~') { for (p = (const char *)xp->xp_pattern + 1; *p != NUL && *p != '/'; p++) { } @@ -3197,8 +3251,9 @@ const char * set_one_cmd_context( case CMD_find: case CMD_sfind: case CMD_tabfind: - if (xp->xp_context == EXPAND_FILES) + if (xp->xp_context == EXPAND_FILES) { xp->xp_context = EXPAND_FILES_IN_PATH; + } break; case CMD_cd: case CMD_chdir: @@ -3263,7 +3318,7 @@ const char * set_one_cmd_context( case CMD_match: if (*arg == NUL || !ends_excmd(*arg)) { - /* also complete "None" */ + // also complete "None" set_context_in_echohl_cmd(xp, arg); arg = (const char *)skipwhite(skiptowhite((const char_u *)arg)); if (*arg != NUL) { @@ -3279,7 +3334,7 @@ const char * set_one_cmd_context( */ case CMD_command: - /* Check for attributes */ + // Check for attributes while (*arg == '-') { arg++; // Skip "-". p = (const char *)skiptowhite((const char_u *)arg); @@ -3331,47 +3386,48 @@ const char * set_one_cmd_context( case CMD_global: case CMD_vglobal: { - const int delim = (uint8_t)(*arg); // Get the delimiter. - if (delim) { - arg++; // Skip delimiter if there is one. - } + const int delim = (uint8_t)(*arg); // Get the delimiter. + if (delim) { + arg++; // Skip delimiter if there is one. + } - while (arg[0] != NUL && (uint8_t)arg[0] != delim) { - if (arg[0] == '\\' && arg[1] != NUL) { + while (arg[0] != NUL && (uint8_t)arg[0] != delim) { + if (arg[0] == '\\' && arg[1] != NUL) { + arg++; + } arg++; } - arg++; + if (arg[0] != NUL) { + return arg + 1; + } + break; } - if (arg[0] != NUL) - return arg + 1; - break; - } case CMD_and: case CMD_substitute: { - const int delim = (uint8_t)(*arg); - if (delim) { - // Skip "from" part. - arg++; - arg = (const char *)skip_regexp((char_u *)arg, delim, p_magic, NULL); - } - // Skip "to" part. - while (arg[0] != NUL && (uint8_t)arg[0] != delim) { - if (arg[0] == '\\' && arg[1] != NUL) { + const int delim = (uint8_t)(*arg); + if (delim) { + // Skip "from" part. arg++; + arg = (const char *)skip_regexp((char_u *)arg, delim, p_magic, NULL); } - arg++; - } - if (arg[0] != NUL) { // Skip delimiter. - arg++; - } - while (arg[0] && strchr("|\"#", arg[0]) == NULL) { - arg++; - } - if (arg[0] != NUL) { - return arg; + // Skip "to" part. + while (arg[0] != NUL && (uint8_t)arg[0] != delim) { + if (arg[0] == '\\' && arg[1] != NUL) { + arg++; + } + arg++; + } + if (arg[0] != NUL) { // Skip delimiter. + arg++; + } + while (arg[0] && strchr("|\"#", arg[0]) == NULL) { + arg++; + } + if (arg[0] != NUL) { + return arg; + } + break; } - break; - } case CMD_isearch: case CMD_dsearch: case CMD_ilist: @@ -3528,9 +3584,9 @@ const char * set_one_cmd_context( } else if (context == EXPAND_COMMANDS) { return arg; } else if (context == EXPAND_MAPPINGS) { - return (const char *)set_context_in_map_cmd( - xp, (char_u *)"map", (char_u *)arg, forceit, false, false, - CMD_map); + return (const char *)set_context_in_map_cmd(xp, (char_u *)"map", (char_u *)arg, forceit, + false, false, + CMD_map); } // Find start of last argument. p = arg; @@ -3548,17 +3604,26 @@ const char * set_one_cmd_context( xp->xp_context = context; } break; - case CMD_map: case CMD_noremap: - case CMD_nmap: case CMD_nnoremap: - case CMD_vmap: case CMD_vnoremap: - case CMD_omap: case CMD_onoremap: - case CMD_imap: case CMD_inoremap: - case CMD_cmap: case CMD_cnoremap: - case CMD_lmap: case CMD_lnoremap: - case CMD_smap: case CMD_snoremap: - case CMD_xmap: case CMD_xnoremap: - return (const char *)set_context_in_map_cmd( - xp, (char_u *)cmd, (char_u *)arg, forceit, false, false, ea.cmdidx); + case CMD_map: + case CMD_noremap: + case CMD_nmap: + case CMD_nnoremap: + case CMD_vmap: + case CMD_vnoremap: + case CMD_omap: + case CMD_onoremap: + case CMD_imap: + case CMD_inoremap: + case CMD_cmap: + case CMD_cnoremap: + case CMD_lmap: + case CMD_lnoremap: + case CMD_smap: + case CMD_snoremap: + case CMD_xmap: + case CMD_xnoremap: + return (const char *)set_context_in_map_cmd(xp, (char_u *)cmd, (char_u *)arg, forceit, false, + false, ea.cmdidx); case CMD_unmap: case CMD_nunmap: case CMD_vunmap: @@ -3568,8 +3633,8 @@ const char * set_one_cmd_context( case CMD_lunmap: case CMD_sunmap: case CMD_xunmap: - return (const char *)set_context_in_map_cmd( - xp, (char_u *)cmd, (char_u *)arg, forceit, false, true, ea.cmdidx); + return (const char *)set_context_in_map_cmd(xp, (char_u *)cmd, (char_u *)arg, forceit, false, + true, ea.cmdidx); case CMD_mapclear: case CMD_nmapclear: case CMD_vmapclear: @@ -3583,27 +3648,45 @@ const char * set_one_cmd_context( xp->xp_pattern = (char_u *)arg; break; - case CMD_abbreviate: case CMD_noreabbrev: - case CMD_cabbrev: case CMD_cnoreabbrev: - case CMD_iabbrev: case CMD_inoreabbrev: - return (const char *)set_context_in_map_cmd( - xp, (char_u *)cmd, (char_u *)arg, forceit, true, false, ea.cmdidx); + case CMD_abbreviate: + case CMD_noreabbrev: + case CMD_cabbrev: + case CMD_cnoreabbrev: + case CMD_iabbrev: + case CMD_inoreabbrev: + return (const char *)set_context_in_map_cmd(xp, (char_u *)cmd, (char_u *)arg, forceit, true, + false, ea.cmdidx); case CMD_unabbreviate: case CMD_cunabbrev: case CMD_iunabbrev: - return (const char *)set_context_in_map_cmd( - xp, (char_u *)cmd, (char_u *)arg, forceit, true, true, ea.cmdidx); - case CMD_menu: case CMD_noremenu: case CMD_unmenu: - case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu: - case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu: - case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu: - case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu: - case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu: - case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu: - case CMD_tmenu: case CMD_tunmenu: - case CMD_popup: case CMD_emenu: - return (const char *)set_context_in_menu_cmd( - xp, cmd, (char_u *)arg, forceit); + return (const char *)set_context_in_map_cmd(xp, (char_u *)cmd, (char_u *)arg, forceit, true, + true, ea.cmdidx); + case CMD_menu: + case CMD_noremenu: + case CMD_unmenu: + case CMD_amenu: + case CMD_anoremenu: + case CMD_aunmenu: + case CMD_nmenu: + case CMD_nnoremenu: + case CMD_nunmenu: + case CMD_vmenu: + case CMD_vnoremenu: + case CMD_vunmenu: + case CMD_omenu: + case CMD_onoremenu: + case CMD_ounmenu: + case CMD_imenu: + case CMD_inoremenu: + case CMD_iunmenu: + case CMD_cmenu: + case CMD_cnoremenu: + case CMD_cunmenu: + case CMD_tmenu: + case CMD_tunmenu: + case CMD_popup: + case CMD_emenu: + return (const char *)set_context_in_menu_cmd(xp, cmd, (char_u *)arg, forceit); case CMD_colorscheme: xp->xp_context = EXPAND_COLORS; @@ -3693,17 +3776,17 @@ const char * set_one_cmd_context( return NULL; } -// Skip a range specifier of the form: addr [,addr] [;addr] .. -// -// Backslashed delimiters after / or ? will be skipped, and commands will -// not be expanded between /'s and ?'s or after "'". -// -// Also skip white space and ":" characters. -// Returns the "cmd" pointer advanced to beyond the range. -char_u *skip_range( - const char_u *cmd, - int *ctx // pointer to xp_context or NULL -) +/// Skip a range specifier of the form: addr [,addr] [;addr] .. +/// +/// Backslashed delimiters after / or ? will be skipped, and commands will +/// not be expanded between /'s and ?'s or after "'". +/// +/// Also skip white space and ":" characters. +/// +/// @param ctx pointer to xp_context or NULL +/// +/// @return the "cmd" pointer advanced to beyond the range. +char_u *skip_range(const char_u *cmd, int *ctx) { unsigned delim; @@ -3720,14 +3803,18 @@ char_u *skip_range( } } else if (*cmd == '/' || *cmd == '?') { delim = *cmd++; - while (*cmd != NUL && *cmd != delim) - if (*cmd++ == '\\' && *cmd != NUL) + while (*cmd != NUL && *cmd != delim) { + if (*cmd++ == '\\' && *cmd != NUL) { ++cmd; - if (*cmd == NUL && ctx != NULL) + } + } + if (*cmd == NUL && ctx != NULL) { *ctx = EXPAND_NOTHING; + } } - if (*cmd != NUL) + if (*cmd != NUL) { ++cmd; + } } // Skip ":" and white space. @@ -3745,20 +3832,20 @@ static void addr_error(cmd_addr_T addr_type) } } -// Get a single EX address -// -// Set ptr to the next character after the part that was interpreted. -// Set ptr to NULL when an error is encountered. -// This may set the last used search pattern. -// -// Return MAXLNUM when no Ex address was found. -static linenr_T get_address(exarg_T *eap, - char_u **ptr, - cmd_addr_T addr_type, - int skip, // only skip the address, don't use it - bool silent, // no errors or side effects - int to_other_file, // flag: may jump to other file - int address_count) // 1 for first, >1 after comma +/// Get a single EX address +/// +/// Set ptr to the next character after the part that was interpreted. +/// Set ptr to NULL when an error is encountered. +/// This may set the last used search pattern. +/// +/// @param skip only skip the address, don't use it +/// @param silent no errors or side effects +/// @param to_other_file flag: may jump to other file +/// @param address_count 1 for first, >1 after comma +/// +/// @return MAXLNUM when no Ex address was found. +static linenr_T get_address(exarg_T *eap, char_u **ptr, cmd_addr_T addr_type, int skip, bool silent, + int to_other_file, int address_count) FUNC_ATTR_NONNULL_ALL { int c; @@ -3774,94 +3861,94 @@ static linenr_T get_address(exarg_T *eap, lnum = MAXLNUM; do { switch (*cmd) { - case '.': /* '.' - Cursor position */ + case '.': // '.' - Cursor position ++cmd; switch (addr_type) { - case ADDR_LINES: - case ADDR_OTHER: - lnum = curwin->w_cursor.lnum; - break; - case ADDR_WINDOWS: - lnum = CURRENT_WIN_NR; - break; - case ADDR_ARGUMENTS: - lnum = curwin->w_arg_idx + 1; - break; - case ADDR_LOADED_BUFFERS: - case ADDR_BUFFERS: - lnum = curbuf->b_fnum; - break; - case ADDR_TABS: - lnum = CURRENT_TAB_NR; - break; - case ADDR_NONE: - case ADDR_TABS_RELATIVE: - case ADDR_UNSIGNED: - addr_error(addr_type); - cmd = NULL; - goto error; - break; - case ADDR_QUICKFIX: - lnum = qf_get_cur_idx(eap); - break; - case ADDR_QUICKFIX_VALID: - lnum = qf_get_cur_valid_idx(eap); - break; + case ADDR_LINES: + case ADDR_OTHER: + lnum = curwin->w_cursor.lnum; + break; + case ADDR_WINDOWS: + lnum = CURRENT_WIN_NR; + break; + case ADDR_ARGUMENTS: + lnum = curwin->w_arg_idx + 1; + break; + case ADDR_LOADED_BUFFERS: + case ADDR_BUFFERS: + lnum = curbuf->b_fnum; + break; + case ADDR_TABS: + lnum = CURRENT_TAB_NR; + break; + case ADDR_NONE: + case ADDR_TABS_RELATIVE: + case ADDR_UNSIGNED: + addr_error(addr_type); + cmd = NULL; + goto error; + break; + case ADDR_QUICKFIX: + lnum = qf_get_cur_idx(eap); + break; + case ADDR_QUICKFIX_VALID: + lnum = qf_get_cur_valid_idx(eap); + break; } break; - case '$': /* '$' - last line */ + case '$': // '$' - last line ++cmd; switch (addr_type) { - case ADDR_LINES: - case ADDR_OTHER: - lnum = curbuf->b_ml.ml_line_count; - break; - case ADDR_WINDOWS: - lnum = LAST_WIN_NR; - break; - case ADDR_ARGUMENTS: - lnum = ARGCOUNT; - break; - case ADDR_LOADED_BUFFERS: - buf = lastbuf; - while (buf->b_ml.ml_mfp == NULL) { - if (buf->b_prev == NULL) { - break; - } - buf = buf->b_prev; - } - lnum = buf->b_fnum; - break; - case ADDR_BUFFERS: - lnum = lastbuf->b_fnum; - break; - case ADDR_TABS: - lnum = LAST_TAB_NR; - break; - case ADDR_NONE: - case ADDR_TABS_RELATIVE: - case ADDR_UNSIGNED: - addr_error(addr_type); - cmd = NULL; - goto error; - break; - case ADDR_QUICKFIX: - lnum = qf_get_size(eap); - if (lnum == 0) { - lnum = 1; - } - break; - case ADDR_QUICKFIX_VALID: - lnum = qf_get_valid_size(eap); - if (lnum == 0) { - lnum = 1; + case ADDR_LINES: + case ADDR_OTHER: + lnum = curbuf->b_ml.ml_line_count; + break; + case ADDR_WINDOWS: + lnum = LAST_WIN_NR; + break; + case ADDR_ARGUMENTS: + lnum = ARGCOUNT; + break; + case ADDR_LOADED_BUFFERS: + buf = lastbuf; + while (buf->b_ml.ml_mfp == NULL) { + if (buf->b_prev == NULL) { + break; } - break; + buf = buf->b_prev; + } + lnum = buf->b_fnum; + break; + case ADDR_BUFFERS: + lnum = lastbuf->b_fnum; + break; + case ADDR_TABS: + lnum = LAST_TAB_NR; + break; + case ADDR_NONE: + case ADDR_TABS_RELATIVE: + case ADDR_UNSIGNED: + addr_error(addr_type); + cmd = NULL; + goto error; + break; + case ADDR_QUICKFIX: + lnum = qf_get_size(eap); + if (lnum == 0) { + lnum = 1; + } + break; + case ADDR_QUICKFIX_VALID: + lnum = qf_get_valid_size(eap); + if (lnum == 0) { + lnum = 1; + } + break; } break; - case '\'': /* ''' - mark */ + case '\'': // ''' - mark if (*++cmd == NUL) { cmd = NULL; goto error; @@ -3871,17 +3958,17 @@ static linenr_T get_address(exarg_T *eap, cmd = NULL; goto error; } - if (skip) + if (skip) { ++cmd; - else { + } else { /* Only accept a mark in another file when it is * used by itself: ":'M". */ fp = getmark(*cmd, to_other_file && cmd[1] == NUL); ++cmd; - if (fp == (pos_T *)-1) - /* Jumped to another file. */ + if (fp == (pos_T *)-1) { + // Jumped to another file. lnum = curwin->w_cursor.lnum; - else { + } else { if (check_mark(fp) == FAIL) { cmd = NULL; goto error; @@ -3892,17 +3979,18 @@ static linenr_T get_address(exarg_T *eap, break; case '/': - case '?': /* '/' or '?' - search */ + case '?': // '/' or '?' - search c = *cmd++; if (addr_type != ADDR_LINES) { addr_error(addr_type); cmd = NULL; goto error; } - if (skip) { /* skip "/pat/" */ + if (skip) { // skip "/pat/" cmd = skip_regexp(cmd, c, p_magic, NULL); - if (*cmd == c) + if (*cmd == c) { ++cmd; + } } else { int flags; @@ -3934,23 +4022,23 @@ static linenr_T get_address(exarg_T *eap, } lnum = curwin->w_cursor.lnum; curwin->w_cursor = pos; - /* adjust command string pointer */ + // adjust command string pointer cmd += searchcmdlen; } break; - case '\\': /* "\?", "\/" or "\&", repeat search */ + case '\\': // "\?", "\/" or "\&", repeat search ++cmd; if (addr_type != ADDR_LINES) { addr_error(addr_type); cmd = NULL; goto error; } - if (*cmd == '&') + if (*cmd == '&') { i = RE_SUBST; - else if (*cmd == '?' || *cmd == '/') + } else if (*cmd == '?' || *cmd == '/') { i = RE_SEARCH; - else { + } else { EMSG(_(e_backslash)); cmd = NULL; goto error; @@ -3988,37 +4076,37 @@ static linenr_T get_address(exarg_T *eap, if (lnum == MAXLNUM) { switch (addr_type) { - case ADDR_LINES: - case ADDR_OTHER: - // "+1" is same as ".+1" - lnum = curwin->w_cursor.lnum; - break; - case ADDR_WINDOWS: - lnum = CURRENT_WIN_NR; - break; - case ADDR_ARGUMENTS: - lnum = curwin->w_arg_idx + 1; - break; - case ADDR_LOADED_BUFFERS: - case ADDR_BUFFERS: - lnum = curbuf->b_fnum; - break; - case ADDR_TABS: - lnum = CURRENT_TAB_NR; - break; - case ADDR_TABS_RELATIVE: - lnum = 1; - break; - case ADDR_QUICKFIX: - lnum = qf_get_cur_idx(eap); - break; - case ADDR_QUICKFIX_VALID: - lnum = qf_get_cur_valid_idx(eap); - break; - case ADDR_NONE: - case ADDR_UNSIGNED: - lnum = 0; - break; + case ADDR_LINES: + case ADDR_OTHER: + // "+1" is same as ".+1" + lnum = curwin->w_cursor.lnum; + break; + case ADDR_WINDOWS: + lnum = CURRENT_WIN_NR; + break; + case ADDR_ARGUMENTS: + lnum = curwin->w_arg_idx + 1; + break; + case ADDR_LOADED_BUFFERS: + case ADDR_BUFFERS: + lnum = curbuf->b_fnum; + break; + case ADDR_TABS: + lnum = CURRENT_TAB_NR; + break; + case ADDR_TABS_RELATIVE: + lnum = 1; + break; + case ADDR_QUICKFIX: + lnum = qf_get_cur_idx(eap); + break; + case ADDR_QUICKFIX_VALID: + lnum = qf_get_cur_valid_idx(eap); + break; + case ADDR_NONE: + case ADDR_UNSIGNED: + lnum = 0; + break; } } @@ -4038,8 +4126,7 @@ static linenr_T get_address(exarg_T *eap, cmd = NULL; goto error; } else if (addr_type == ADDR_LOADED_BUFFERS || addr_type == ADDR_BUFFERS) { - lnum = compute_buffer_local_count( - addr_type, lnum, (i == '-') ? -1 * n : n); + lnum = compute_buffer_local_count(addr_type, lnum, (i == '-') ? -1 * n : n); } else { // Relative line addressing, need to adjust for folded lines // now, but only do it after the first address. @@ -4067,12 +4154,13 @@ error: static void get_flags(exarg_T *eap) { while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL) { - if (*eap->arg == 'l') + if (*eap->arg == 'l') { eap->flags |= EXFLAG_LIST; - else if (*eap->arg == 'p') + } else if (*eap->arg == 'p') { eap->flags |= EXFLAG_PRINT; - else + } else { eap->flags |= EXFLAG_NR; + } eap->arg = skipwhite(eap->arg + 1); } } @@ -4080,9 +4168,9 @@ static void get_flags(exarg_T *eap) /// Stub function for command which is Not Implemented. NI! void ex_ni(exarg_T *eap) { - if (!eap->skip) - eap->errmsg = (char_u *)N_( - "E319: The command is not available in this version"); + if (!eap->skip) { + eap->errmsg = (char_u *)N_("E319: The command is not available in this version"); + } } /// Stub function for script command which is Not Implemented. NI! @@ -4110,77 +4198,77 @@ static char_u *invalid_range(exarg_T *eap) if (eap->argt & EX_RANGE) { switch (eap->addr_type) { - case ADDR_LINES: - if (eap->line2 > (curbuf->b_ml.ml_line_count - + (eap->cmdidx == CMD_diffget))) { - return (char_u *)_(e_invrange); - } - break; - case ADDR_ARGUMENTS: - // add 1 if ARGCOUNT is 0 - if (eap->line2 > ARGCOUNT + (!ARGCOUNT)) { - return (char_u *)_(e_invrange); - } - break; - case ADDR_BUFFERS: - if (eap->line1 < firstbuf->b_fnum - || eap->line2 > lastbuf->b_fnum) { - return (char_u *)_(e_invrange); - } - break; - case ADDR_LOADED_BUFFERS: - buf = firstbuf; - while (buf->b_ml.ml_mfp == NULL) { - if (buf->b_next == NULL) { - return (char_u *)_(e_invrange); - } - buf = buf->b_next; - } - if (eap->line1 < buf->b_fnum) { - return (char_u *)_(e_invrange); - } - buf = lastbuf; - while (buf->b_ml.ml_mfp == NULL) { - if (buf->b_prev == NULL) { - return (char_u *)_(e_invrange); - } - buf = buf->b_prev; - } - if (eap->line2 > buf->b_fnum) { - return (char_u *)_(e_invrange); - } - break; - case ADDR_WINDOWS: - if (eap->line2 > LAST_WIN_NR) { - return (char_u *)_(e_invrange); - } - break; - case ADDR_TABS: - if (eap->line2 > LAST_TAB_NR) { - return (char_u *)_(e_invrange); - } - break; - case ADDR_TABS_RELATIVE: - case ADDR_OTHER: - // Any range is OK. - break; - case ADDR_QUICKFIX: - assert(eap->line2 >= 0); - // No error for value that is too big, will use the last entry. - if (eap->line2 <= 0) { + case ADDR_LINES: + if (eap->line2 > (curbuf->b_ml.ml_line_count + + (eap->cmdidx == CMD_diffget))) { + return (char_u *)_(e_invrange); + } + break; + case ADDR_ARGUMENTS: + // add 1 if ARGCOUNT is 0 + if (eap->line2 > ARGCOUNT + (!ARGCOUNT)) { + return (char_u *)_(e_invrange); + } + break; + case ADDR_BUFFERS: + if (eap->line1 < firstbuf->b_fnum + || eap->line2 > lastbuf->b_fnum) { + return (char_u *)_(e_invrange); + } + break; + case ADDR_LOADED_BUFFERS: + buf = firstbuf; + while (buf->b_ml.ml_mfp == NULL) { + if (buf->b_next == NULL) { return (char_u *)_(e_invrange); } - break; - case ADDR_QUICKFIX_VALID: - if ((eap->line2 != 1 && (size_t)eap->line2 > qf_get_valid_size(eap)) - || eap->line2 < 0) { + buf = buf->b_next; + } + if (eap->line1 < buf->b_fnum) { + return (char_u *)_(e_invrange); + } + buf = lastbuf; + while (buf->b_ml.ml_mfp == NULL) { + if (buf->b_prev == NULL) { return (char_u *)_(e_invrange); } - break; - case ADDR_UNSIGNED: - case ADDR_NONE: - // Will give an error elsewhere. - break; + buf = buf->b_prev; + } + if (eap->line2 > buf->b_fnum) { + return (char_u *)_(e_invrange); + } + break; + case ADDR_WINDOWS: + if (eap->line2 > LAST_WIN_NR) { + return (char_u *)_(e_invrange); + } + break; + case ADDR_TABS: + if (eap->line2 > LAST_TAB_NR) { + return (char_u *)_(e_invrange); + } + break; + case ADDR_TABS_RELATIVE: + case ADDR_OTHER: + // Any range is OK. + break; + case ADDR_QUICKFIX: + assert(eap->line2 >= 0); + // No error for value that is too big, will use the last entry. + if (eap->line2 <= 0) { + return (char_u *)_(e_invrange); + } + break; + case ADDR_QUICKFIX_VALID: + if ((eap->line2 != 1 && (size_t)eap->line2 > qf_get_valid_size(eap)) + || eap->line2 < 0) { + return (char_u *)_(e_invrange); + } + break; + case ADDR_UNSIGNED: + case ADDR_NONE: + // Will give an error elsewhere. + break; } } return NULL; @@ -4215,8 +4303,9 @@ static char_u *skip_grep_pat(exarg_T *eap) || eap->cmdidx == CMD_lvimgrepadd || grep_internal(eap->cmdidx))) { p = skip_vimgrep_pat(p, NULL, NULL); - if (p == NULL) + if (p == NULL) { p = eap->arg; + } } return p; } @@ -4244,24 +4333,27 @@ static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep) && !grep_internal(eap->cmdidx)) { if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd) { - if (*curbuf->b_p_gp == NUL) + if (*curbuf->b_p_gp == NUL) { program = p_gp; - else + } else { program = curbuf->b_p_gp; + } } else { - if (*curbuf->b_p_mp == NUL) + if (*curbuf->b_p_mp == NUL) { program = p_mp; - else + } else { program = curbuf->b_p_mp; + } } p = skipwhite(p); if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL) { - /* replace $* by given arguments */ + // replace $* by given arguments i = 1; - while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL) + while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL) { ++i; + } len = (int)STRLEN(p); new_cmdline = xmalloc(STRLEN(program) + i * (len - 2) + 1); ptr = new_cmdline; @@ -4281,7 +4373,7 @@ static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep) } msg_make(p); - /* 'eap->cmd' is not set here, because it is not used at CMD_make */ + // 'eap->cmd' is not set here, because it is not used at CMD_make xfree(*cmdlinep); *cmdlinep = new_cmdline; p = new_cmdline; @@ -4294,13 +4386,13 @@ static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep) // Return FAIL for failure, OK otherwise. int expand_filename(exarg_T *eap, char_u **cmdlinep, char_u **errormsgp) { - int has_wildcards; /* need to expand wildcards */ + int has_wildcards; // need to expand wildcards char_u *repl; size_t srclen; char_u *p; int escaped; - /* Skip a regexp pattern for ":vimgrep[add] pat file..." */ + // Skip a regexp pattern for ":vimgrep[add] pat file..." p = skip_grep_pat(eap); /* @@ -4310,12 +4402,13 @@ int expand_filename(exarg_T *eap, char_u **cmdlinep, char_u **errormsgp) */ has_wildcards = path_has_wildcard(p); while (*p != NUL) { - /* Skip over `=expr`, wildcards in it are not expanded. */ + // Skip over `=expr`, wildcards in it are not expanded. if (p[0] == '`' && p[1] == '=') { p += 2; (void)skip_expr(&p); - if (*p == '`') + if (*p == '`') { ++p; + } continue; } /* @@ -4331,10 +4424,11 @@ int expand_filename(exarg_T *eap, char_u **cmdlinep, char_u **errormsgp) * Try to find a match at this position. */ repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum), - errormsgp, &escaped); - if (*errormsgp != NULL) /* error detected */ + errormsgp, &escaped); + if (*errormsgp != NULL) { // error detected return FAIL; - if (repl == NULL) { /* no match found */ + } + if (repl == NULL) { // no match found p += srclen; continue; } @@ -4364,8 +4458,7 @@ int expand_filename(exarg_T *eap, char_u **cmdlinep, char_u **errormsgp) && eap->cmdidx != CMD_lmake && eap->cmdidx != CMD_make && eap->cmdidx != CMD_terminal - && !(eap->argt & EX_NOSPC) - ) { + && !(eap->argt & EX_NOSPC)) { char_u *l; #ifdef BACKSLASH_IN_FILENAME /* Don't escape a backslash here, because rem_backslash() doesn't @@ -4376,13 +4469,14 @@ int expand_filename(exarg_T *eap, char_u **cmdlinep, char_u **errormsgp) # define ESCAPE_CHARS escape_chars #endif - for (l = repl; *l; ++l) + for (l = repl; *l; ++l) { if (vim_strchr(ESCAPE_CHARS, *l) != NULL) { l = vim_strsave_escaped(repl, ESCAPE_CHARS); xfree(repl); repl = l; break; } + } } // For a shell command a '!' must be escaped. @@ -4418,11 +4512,12 @@ int expand_filename(exarg_T *eap, char_u **cmdlinep, char_u **errormsgp) if (vim_strchr(eap->arg, '$') != NULL || vim_strchr(eap->arg, '~') != NULL) { expand_env_esc(eap->arg, NameBuff, MAXPATHL, - TRUE, TRUE, NULL); + TRUE, TRUE, NULL); has_wildcards = path_has_wildcard(NameBuff); p = NameBuff; - } else + } else { p = NULL; + } if (p != NULL) { (void)repl_cmdline(eap, eap->arg, STRLEN(eap->arg), p, cmdlinep); } @@ -4444,8 +4539,9 @@ int expand_filename(exarg_T *eap, char_u **cmdlinep, char_u **errormsgp) ExpandInit(&xpc); xpc.xp_context = EXPAND_FILES; - if (p_wic) + if (p_wic) { options += WILD_ICASE; + } p = ExpandOne(&xpc, eap->arg, NULL, options, WILD_EXPAND_FREE); if (p == NULL) { return FAIL; @@ -4464,8 +4560,8 @@ int expand_filename(exarg_T *eap, char_u **cmdlinep, char_u **errormsgp) * "repl" is the replacement string. * Returns a pointer to the character after the replaced string. */ -static char_u *repl_cmdline(exarg_T *eap, char_u *src, size_t srclen, - char_u *repl, char_u **cmdlinep) +static char_u *repl_cmdline(exarg_T *eap, char_u *src, size_t srclen, char_u *repl, + char_u **cmdlinep) { /* * The new command line is build in new_cmdline[]. @@ -4474,8 +4570,9 @@ static char_u *repl_cmdline(exarg_T *eap, char_u *src, size_t srclen, */ size_t len = STRLEN(repl); size_t i = (size_t)(src - *cmdlinep) + STRLEN(src + srclen) + len + 3; - if (eap->nextcmd != NULL) - i += STRLEN(eap->nextcmd); /* add space for next command */ + if (eap->nextcmd != NULL) { + i += STRLEN(eap->nextcmd); // add space for next command + } char_u *new_cmdline = xmalloc(i); /* @@ -4484,23 +4581,24 @@ static char_u *repl_cmdline(exarg_T *eap, char_u *src, size_t srclen, * Copy what came after the expanded part. * Copy the next commands, if there are any. */ - i = (size_t)(src - *cmdlinep); /* length of part before match */ + i = (size_t)(src - *cmdlinep); // length of part before match memmove(new_cmdline, *cmdlinep, i); memmove(new_cmdline + i, repl, len); - i += len; /* remember the end of the string */ + i += len; // remember the end of the string STRCPY(new_cmdline + i, src + srclen); - src = new_cmdline + i; /* remember where to continue */ + src = new_cmdline + i; // remember where to continue - if (eap->nextcmd != NULL) { /* append next command */ + if (eap->nextcmd != NULL) { // append next command i = STRLEN(new_cmdline) + 1; STRCPY(new_cmdline + i, eap->nextcmd); eap->nextcmd = new_cmdline + i; } eap->cmd = new_cmdline + (eap->cmd - *cmdlinep); eap->arg = new_cmdline + (eap->arg - *cmdlinep); - if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command) + if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command) { eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep); + } xfree(*cmdlinep); *cmdlinep = new_cmdline; @@ -4535,16 +4633,16 @@ void separate_nextcmd(exarg_T *eap) break; } } else if ( - // Check for '"': start of comment or '|': next command */ - // :@" does not start a comment! - // :redir @" doesn't either. - (*p == '"' - && !(eap->argt & EX_NOTRLCOM) - && (eap->cmdidx != CMD_at || p != eap->arg) - && (eap->cmdidx != CMD_redir - || p != eap->arg + 1 || p[-1] != '@')) - || *p == '|' - || *p == '\n') { + // Check for '"': start of comment or '|': next command */ + // :@" does not start a comment! + // :redir @" doesn't either. + (*p == '"' + && !(eap->argt & EX_NOTRLCOM) + && (eap->cmdidx != CMD_at || p != eap->arg) + && (eap->cmdidx != CMD_redir + || p != eap->arg + 1 || p[-1] != '@')) + || *p == '|' + || *p == '\n') { // We remove the '\' before the '|', unless EX_CTRLV is used // AND 'b' is present in 'cpoptions'. if ((vim_strchr(p_cpo, CPO_BAR) == NULL @@ -4572,38 +4670,36 @@ static char_u *getargcmd(char_u **argp) char_u *arg = *argp; char_u *command = NULL; - if (*arg == '+') { /* +[command] */ + if (*arg == '+') { // +[command] ++arg; - if (ascii_isspace(*arg) || *arg == '\0') + if (ascii_isspace(*arg) || *arg == '\0') { command = dollar_command; - else { + } else { command = arg; arg = skip_cmd_arg(command, TRUE); - if (*arg != NUL) - *arg++ = NUL; /* terminate command with NUL */ + if (*arg != NUL) { + *arg++ = NUL; // terminate command with NUL + } } - arg = skipwhite(arg); /* skip over spaces */ + arg = skipwhite(arg); // skip over spaces *argp = arg; } return command; } -/* - * Find end of "+command" argument. Skip over "\ " and "\\". - */ -static char_u * -skip_cmd_arg ( - char_u *p, - int rembs /* TRUE to halve the number of backslashes */ -) +/// Find end of "+command" argument. Skip over "\ " and "\\". +/// +/// @param rembs TRUE to halve the number of backslashes +static char_u *skip_cmd_arg(char_u *p, int rembs) { while (*p && !ascii_isspace(*p)) { if (*p == '\\' && p[1] != NUL) { - if (rembs) + if (rembs) { STRMOVE(p, p + 1); - else + } else { ++p; + } } MB_PTR_ADV(p); } @@ -4636,20 +4732,22 @@ static int getargopt(exarg_T *eap) int bad_char_idx; char_u *p; - /* ":edit ++[no]bin[ary] file" */ + // ":edit ++[no]bin[ary] file" if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0) { if (*arg == 'n') { arg += 2; eap->force_bin = FORCE_NOBIN; - } else + } else { eap->force_bin = FORCE_BIN; - if (!checkforcmd(&arg, "binary", 3)) + } + if (!checkforcmd(&arg, "binary", 3)) { return FAIL; + } eap->arg = skipwhite(arg); return OK; } - /* ":read ++edit file" */ + // ":read ++edit file" if (STRNCMP(arg, "edit", 4) == 0) { eap->read_edit = TRUE; eap->arg = skipwhite(arg + 4); @@ -4663,18 +4761,20 @@ static int getargopt(exarg_T *eap) arg += 10; pp = &eap->force_ff; } else if (STRNCMP(arg, "enc", 3) == 0) { - if (STRNCMP(arg, "encoding", 8) == 0) + if (STRNCMP(arg, "encoding", 8) == 0) { arg += 8; - else + } else { arg += 3; + } pp = &eap->force_enc; } else if (STRNCMP(arg, "bad", 3) == 0) { arg += 3; pp = &bad_char_idx; } - if (pp == NULL || *arg != '=') + if (pp == NULL || *arg != '=') { return FAIL; + } ++arg; *pp = (int)(arg - eap->cmd); @@ -4688,9 +4788,10 @@ static int getargopt(exarg_T *eap) } eap->force_ff = eap->cmd[eap->force_ff]; } else if (pp == &eap->force_enc) { - /* Make 'fileencoding' lower case. */ - for (p = eap->cmd + eap->force_enc; *p != NUL; ++p) + // Make 'fileencoding' lower case. + for (p = eap->cmd + eap->force_enc; *p != NUL; ++p) { *p = TOLOWER_ASC(*p); + } } else { /* Check ++bad= argument. Must be a single-byte character, "keep" or * "drop". */ @@ -4713,8 +4814,8 @@ static int get_tabpage_arg(exarg_T *eap) if (eap->arg && *eap->arg != NUL) { char_u *p = eap->arg; char_u *p_save; - int relative = 0; // argument +N/-N means: go to N places to the - // right/left relative to the current position. + int relative = 0; // argument +N/-N means: go to N places to the + // right/left relative to the current position. if (*p == '-') { relative = -1; @@ -4741,8 +4842,7 @@ static int get_tabpage_arg(exarg_T *eap) } else { if (*p_save == NUL) { tab_number = 1; - } - else if (p == p_save || *p_save == '-' || *p != NUL || tab_number == 0) { + } else if (p == p_save || *p_save == '-' || *p != NUL || tab_number == 0) { // No numbers as argument. eap->errmsg = e_invarg; goto theend; @@ -4772,8 +4872,9 @@ static int get_tabpage_arg(exarg_T *eap) switch (eap->cmdidx) { case CMD_tabnext: tab_number = tabpage_index(curtab) + 1; - if (tab_number > LAST_TAB_NR) + if (tab_number > LAST_TAB_NR) { tab_number = 1; + } break; case CMD_tabmove: tab_number = LAST_TAB_NR; @@ -4792,7 +4893,7 @@ theend: */ static void ex_abbreviate(exarg_T *eap) { - do_exmap(eap, TRUE); /* almost the same as mapping */ + do_exmap(eap, TRUE); // almost the same as mapping } /* @@ -4843,10 +4944,11 @@ static void ex_autocmd(exarg_T *eap) if (secure) { secure = 2; eap->errmsg = e_curdir; - } else if (eap->cmdidx == CMD_autocmd) + } else if (eap->cmdidx == CMD_autocmd) { do_autocmd(eap->arg, eap->forceit); - else + } else { do_augroup(eap->arg, eap->forceit); + } } /* @@ -4872,16 +4974,16 @@ static void ex_doautocmd(exarg_T *eap) */ static void ex_bunload(exarg_T *eap) { - eap->errmsg = do_bufdel( - eap->cmdidx == CMD_bdelete ? DOBUF_DEL - : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE - : DOBUF_UNLOAD, eap->arg, - eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit); + eap->errmsg = do_bufdel(eap->cmdidx == CMD_bdelete ? DOBUF_DEL + : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE + : DOBUF_UNLOAD, + eap->arg, + eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit); } /* - * :[N]buffer [N] to buffer N - * :[N]sbuffer [N] to buffer N + * :[N]buffer [N] to buffer N + * :[N]sbuffer [N] to buffer N */ static void ex_buffer(exarg_T *eap) { @@ -4900,8 +5002,8 @@ static void ex_buffer(exarg_T *eap) } /* - * :[N]bmodified [N] to next mod. buffer - * :[N]sbmodified [N] to next mod. buffer + * :[N]bmodified [N] to next mod. buffer + * :[N]sbmodified [N] to next mod. buffer */ static void ex_bmodified(exarg_T *eap) { @@ -4912,8 +5014,8 @@ static void ex_bmodified(exarg_T *eap) } /* - * :[N]bnext [N] to next buffer - * :[N]sbnext [N] split and to next buffer + * :[N]bnext [N] to next buffer + * :[N]sbnext [N] split and to next buffer */ static void ex_bnext(exarg_T *eap) { @@ -4924,10 +5026,10 @@ static void ex_bnext(exarg_T *eap) } /* - * :[N]bNext [N] to previous buffer - * :[N]bprevious [N] to previous buffer - * :[N]sbNext [N] split and to previous buffer - * :[N]sbprevious [N] split and to previous buffer + * :[N]bNext [N] to previous buffer + * :[N]bprevious [N] to previous buffer + * :[N]sbNext [N] split and to previous buffer + * :[N]sbprevious [N] split and to previous buffer */ static void ex_bprevious(exarg_T *eap) { @@ -4938,10 +5040,10 @@ static void ex_bprevious(exarg_T *eap) } /* - * :brewind to first buffer - * :bfirst to first buffer - * :sbrewind split and to first buffer - * :sbfirst split and to first buffer + * :brewind to first buffer + * :bfirst to first buffer + * :sbrewind split and to first buffer + * :sbfirst split and to first buffer */ static void ex_brewind(exarg_T *eap) { @@ -4952,8 +5054,8 @@ static void ex_brewind(exarg_T *eap) } /* - * :blast to last buffer - * :sblast split and to last buffer + * :blast to last buffer + * :sblast split and to last buffer */ static void ex_blast(exarg_T *eap) { @@ -4987,28 +5089,23 @@ char_u *find_nextcmd(const char_u *p) /// Return NULL if it isn't, the following character if it is. char_u *check_nextcmd(char_u *p) { - char_u *s = skipwhite(p); + char_u *s = skipwhite(p); - if (*s == '|' || *s == '\n') { - return (s + 1); - } else { - return NULL; - } + if (*s == '|' || *s == '\n') { + return (s + 1); + } else { + return NULL; + } } -/* - * - if there are more files to edit - * - and this is the last window - * - and forceit not used - * - and not repeated twice on a row - * return FAIL and give error message if 'message' TRUE - * return OK otherwise - */ -static int -check_more( - int message, // when FALSE check only, no messages - bool forceit -) +/// - if there are more files to edit +/// - and this is the last window +/// - and forceit not used +/// - and not repeated twice on a row +/// @return FAIL and give error message if 'message' TRUE, return OK otherwise +/// +/// @param message when FALSE check only, no messages +static int check_more(int message, bool forceit) { int n = ARGCOUNT - curwin->w_arg_idx - 1; @@ -5018,21 +5115,24 @@ check_more( if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL) { char_u buff[DIALOG_MSG_SIZE]; - if (n == 1) + if (n == 1) { STRLCPY(buff, _("1 more file to edit. Quit anyway?"), - DIALOG_MSG_SIZE); - else + DIALOG_MSG_SIZE); + } else { vim_snprintf((char *)buff, DIALOG_MSG_SIZE, - _("%d more files to edit. Quit anyway?"), n); - if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES) + _("%d more files to edit. Quit anyway?"), n); + } + if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES) { return OK; + } return FAIL; } - if (n == 1) + if (n == 1) { EMSG(_("E173: 1 more file to edit")); - else + } else { EMSGN(_("E173: %" PRId64 " more files to edit"), n); - quitmore = 2; /* next try to quit is allowed */ + } + quitmore = 2; // next try to quit is allowed } return FAIL; } @@ -5044,14 +5144,14 @@ check_more( */ char_u *get_command_name(expand_T *xp, int idx) { - if (idx >= (int)CMD_SIZE) + if (idx >= (int)CMD_SIZE) { return get_user_command_name(idx); + } return cmdnames[idx].cmd_name; } -static int uc_add_command(char_u *name, size_t name_len, char_u *rep, - uint32_t argt, long def, int flags, int compl, - char_u *compl_arg, cmd_addr_T addr_type, bool force) +static int uc_add_command(char_u *name, size_t name_len, char_u *rep, uint32_t argt, long def, + int flags, int compl, char_u *compl_arg, cmd_addr_T addr_type, bool force) FUNC_ATTR_NONNULL_ARG(1, 3) { ucmd_T *cmd = NULL; @@ -5063,19 +5163,21 @@ static int uc_add_command(char_u *name, size_t name_len, char_u *rep, replace_termcodes(rep, STRLEN(rep), &rep_buf, false, false, true, CPO_TO_CPO_FLAGS); if (rep_buf == NULL) { - /* Can't replace termcodes - try using the string as is */ + // Can't replace termcodes - try using the string as is rep_buf = vim_strsave(rep); } - /* get address of growarray: global or in curbuf */ + // get address of growarray: global or in curbuf if (flags & UC_BUFFER) { gap = &curbuf->b_ucmds; - if (gap->ga_itemsize == 0) + if (gap->ga_itemsize == 0) { ga_init(gap, (int)sizeof(ucmd_T), 4); - } else + } + } else { gap = &ucmds; + } - /* Search for the command in the already defined commands. */ + // Search for the command in the already defined commands. for (i = 0; i < gap->ga_len; ++i) { size_t len; @@ -5083,10 +5185,11 @@ static int uc_add_command(char_u *name, size_t name_len, char_u *rep, len = STRLEN(cmd->uc_name); cmp = STRNCMP(name, cmd->uc_name, name_len); if (cmp == 0) { - if (name_len < len) + if (name_len < len) { cmp = -1; - else if (name_len > len) + } else if (name_len > len) { cmp = 1; + } } if (cmp == 0) { @@ -5105,12 +5208,13 @@ static int uc_add_command(char_u *name, size_t name_len, char_u *rep, break; } - /* Stop as soon as we pass the name to add */ - if (cmp < 0) + // Stop as soon as we pass the name to add + if (cmp < 0) { break; + } } - /* Extend the array unless we're replacing an existing command */ + // Extend the array unless we're replacing an existing command if (cmp != 0) { ga_grow(gap, 1); @@ -5247,8 +5351,9 @@ static void uc_list(char_u *name, size_t name_len) } found = true; msg_putchar('\n'); - if (got_int) + if (got_int) { break; + } // Special cases int len = 4; @@ -5287,21 +5392,21 @@ static void uc_list(char_u *name, size_t name_len) // Arguments switch (a & (EX_EXTRA | EX_NOSPC | EX_NEEDARG)) { - case 0: - IObuff[len++] = '0'; - break; - case (EX_EXTRA): - IObuff[len++] = '*'; - break; - case (EX_EXTRA | EX_NOSPC): - IObuff[len++] = '?'; - break; - case (EX_EXTRA | EX_NEEDARG): - IObuff[len++] = '+'; - break; - case (EX_EXTRA | EX_NOSPC | EX_NEEDARG): - IObuff[len++] = '1'; - break; + case 0: + IObuff[len++] = '0'; + break; + case (EX_EXTRA): + IObuff[len++] = '*'; + break; + case (EX_EXTRA | EX_NOSPC): + IObuff[len++] = '?'; + break; + case (EX_EXTRA | EX_NEEDARG): + IObuff[len++] = '+'; + break; + case (EX_EXTRA | EX_NOSPC | EX_NEEDARG): + IObuff[len++] = '1'; + break; } do { @@ -5369,18 +5474,19 @@ static void uc_list(char_u *name, size_t name_len) break; } } - if (gap == &ucmds || i < gap->ga_len) + if (gap == &ucmds || i < gap->ga_len) { break; + } gap = &ucmds; } - if (!found) + if (!found) { MSG(_("No user-defined commands found")); + } } -static int uc_scan_attr(char_u *attr, size_t len, uint32_t *argt, long *def, - int *flags, int *complp, char_u **compl_arg, - cmd_addr_T *addr_type_arg) +static int uc_scan_attr(char_u *attr, size_t len, uint32_t *argt, long *def, int *flags, + int *complp, char_u **compl_arg, cmd_addr_T *addr_type_arg) FUNC_ATTR_NONNULL_ALL { char_u *p; @@ -5469,17 +5575,20 @@ invalid_count: if (val != NULL) { p = val; - if (*def >= 0) + if (*def >= 0) { goto two_count; + } *def = getdigits_long(&p, true, 0); - if (p != val + vallen) + if (p != val + vallen) { goto invalid_count; + } } - if (*def < 0) + if (*def < 0) { *def = 0; + } } else if (STRNICMP(attr, "complete", attrlen) == 0) { if (val == NULL) { EMSG(_("E179: argument required for -complete")); @@ -5514,8 +5623,7 @@ invalid_count: return OK; } -static char e_complete_used_without_nargs[] = N_( - "E1208: -complete used without -nargs"); +static char e_complete_used_without_nargs[] = N_("E1208: -complete used without -nargs"); /* * ":command ..." @@ -5536,7 +5644,7 @@ static void ex_command(exarg_T *eap) p = eap->arg; - /* Check for attributes */ + // Check for attributes while (*p == '-') { ++p; end = skiptowhite(p); @@ -5614,11 +5722,13 @@ static void ex_delcommand(exarg_T *eap) for (i = 0; i < gap->ga_len; ++i) { cmd = USER_CMD_GA(gap, i); cmp = STRCMP(eap->arg, cmd->uc_name); - if (cmp <= 0) + if (cmp <= 0) { break; + } } - if (gap == &ucmds || cmp == 0) + if (gap == &ucmds || cmp == 0) { break; + } gap = &ucmds; } @@ -5633,8 +5743,9 @@ static void ex_delcommand(exarg_T *eap) --gap->ga_len; - if (i < gap->ga_len) + if (i < gap->ga_len) { memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T)); + } } /* @@ -5647,9 +5758,9 @@ static char_u *uc_split_args(char_u *arg, size_t *lenp) char_u *q; int len; - /* Precalculate length */ + // Precalculate length p = arg; - len = 2; /* Initial and final quotes */ + len = 2; // Initial and final quotes while (*p) { if (p[0] == '\\' && p[1] == '\\') { @@ -5663,9 +5774,10 @@ static char_u *uc_split_args(char_u *arg, size_t *lenp) p += 1; } else if (ascii_iswhite(*p)) { p = skipwhite(p); - if (*p == NUL) + if (*p == NUL) { break; - len += 3; /* "," */ + } + len += 3; // "," } else { const int charlen = utfc_ptr2len(p); @@ -5692,8 +5804,9 @@ static char_u *uc_split_args(char_u *arg, size_t *lenp) *q++ = *p++; } else if (ascii_iswhite(*p)) { p = skipwhite(p); - if (*p == NUL) + if (*p == NUL) { break; + } *q++ = '"'; *q++ = ','; *q++ = '"'; @@ -5726,25 +5839,19 @@ static size_t add_cmd_modifier(char_u *buf, char *mod_str, bool *multi_mods) return result; } -/* - * Check for a <> code in a user command. - * "code" points to the '<'. "len" the length of the <> (inclusive). - * "buf" is where the result is to be added. - * "split_buf" points to a buffer used for splitting, caller should free it. - * "split_len" is the length of what "split_buf" contains. - * Returns the length of the replacement, which has been added to "buf". - * Returns -1 if there was no match, and only the "<" has been copied. - */ -static size_t -uc_check_code( - char_u *code, - size_t len, - char_u *buf, - ucmd_T *cmd, /* the user command we're expanding */ - exarg_T *eap, /* ex arguments */ - char_u **split_buf, - size_t *split_len -) +/// Check for a <> code in a user command. +/// +/// @param code points to the '<'. "len" the length of the <> (inclusive). +/// @param buf is where the result is to be added. +/// @param cmd the user command we're expanding +/// @param eap ex arguments +/// @param split_buf points to a buffer used for splitting, caller should free it. +/// @param split_len is the length of what "split_buf" contains. +/// +/// @return the length of the replacement, which has been added to "buf". +/// Return -1 if there was no match, and only the "<" has been copied. +static size_t uc_check_code(char_u *code, size_t len, char_u *buf, ucmd_T *cmd, exarg_T *eap, + char_u **split_buf, size_t *split_len) { size_t result = 0; char_u *p = code + 1; @@ -5794,14 +5901,16 @@ uc_check_code( switch (type) { case ct_ARGS: - /* Simple case first */ + // Simple case first if (*eap->arg == NUL) { if (quote == 1) { result = 2; - if (buf != NULL) + if (buf != NULL) { STRCPY(buf, "''"); - } else + } + } else { result = 0; + } break; } @@ -5812,12 +5921,13 @@ uc_check_code( } switch (quote) { - case 0: /* No quoting, no splitting */ + case 0: // No quoting, no splitting result = STRLEN(eap->arg); - if (buf != NULL) + if (buf != NULL) { STRCPY(buf, eap->arg); + } break; - case 1: /* Quote, but don't split */ + case 1: // Quote, but don't split result = STRLEN(eap->arg) + 2; for (p = eap->arg; *p; p++) { if (*p == '\\' || *p == '"') { @@ -5837,14 +5947,16 @@ uc_check_code( } break; - case 2: /* Quote and split (<f-args>) */ - /* This is hard, so only do it once, and cache the result */ - if (*split_buf == NULL) + case 2: // Quote and split (<f-args>) + // This is hard, so only do it once, and cache the result + if (*split_buf == NULL) { *split_buf = uc_split_args(eap->arg, split_len); + } result = *split_len; - if (buf != NULL && result != 0) + if (buf != NULL && result != 0) { STRCPY(buf, *split_buf); + } break; } @@ -5852,155 +5964,166 @@ uc_check_code( case ct_BANG: result = eap->forceit ? 1 : 0; - if (quote) + if (quote) { result += 2; + } if (buf != NULL) { - if (quote) + if (quote) { *buf++ = '"'; - if (eap->forceit) + } + if (eap->forceit) { *buf++ = '!'; - if (quote) + } + if (quote) { *buf = '"'; + } } break; case ct_LINE1: case ct_LINE2: case ct_RANGE: - case ct_COUNT: - { - char num_buf[20]; - long num = (type == ct_LINE1) ? eap->line1 : - (type == ct_LINE2) ? eap->line2 : - (type == ct_RANGE) ? eap->addr_count : - (eap->addr_count > 0) ? eap->line2 : cmd->uc_def; - size_t num_len; - - sprintf(num_buf, "%" PRId64, (int64_t)num); - num_len = STRLEN(num_buf); - result = num_len; - - if (quote) - result += 2; - - if (buf != NULL) { - if (quote) - *buf++ = '"'; - STRCPY(buf, num_buf); - buf += num_len; - if (quote) - *buf = '"'; - } + case ct_COUNT: { + char num_buf[20]; + long num = (type == ct_LINE1) ? eap->line1 : + (type == ct_LINE2) ? eap->line2 : + (type == ct_RANGE) ? eap->addr_count : + (eap->addr_count > 0) ? eap->line2 : cmd->uc_def; + size_t num_len; + + sprintf(num_buf, "%" PRId64, (int64_t)num); + num_len = STRLEN(num_buf); + result = num_len; - break; - } - - case ct_MODS: - { - result = quote ? 2 : 0; - if (buf != NULL) { if (quote) { - *buf++ = '"'; + result += 2; } - *buf = '\0'; - } - bool multi_mods = false; + if (buf != NULL) { + if (quote) { + *buf++ = '"'; + } + STRCPY(buf, num_buf); + buf += num_len; + if (quote) { + *buf = '"'; + } + } - // :aboveleft and :leftabove - if (cmdmod.split & WSP_ABOVE) { - result += add_cmd_modifier(buf, "aboveleft", &multi_mods); - } - // :belowright and :rightbelow - if (cmdmod.split & WSP_BELOW) { - result += add_cmd_modifier(buf, "belowright", &multi_mods); - } - // :botright - if (cmdmod.split & WSP_BOT) { - result += add_cmd_modifier(buf, "botright", &multi_mods); + break; } - typedef struct { - bool *set; - char *name; - } mod_entry_T; - static mod_entry_T mod_entries[] = { - { &cmdmod.browse, "browse" }, - { &cmdmod.confirm, "confirm" }, - { &cmdmod.hide, "hide" }, - { &cmdmod.keepalt, "keepalt" }, - { &cmdmod.keepjumps, "keepjumps" }, - { &cmdmod.keepmarks, "keepmarks" }, - { &cmdmod.keeppatterns, "keeppatterns" }, - { &cmdmod.lockmarks, "lockmarks" }, - { &cmdmod.noswapfile, "noswapfile" } - }; - // the modifiers that are simple flags - for (size_t i = 0; i < ARRAY_SIZE(mod_entries); i++) { - if (*mod_entries[i].set) { - result += add_cmd_modifier(buf, mod_entries[i].name, &multi_mods); + case ct_MODS: { + result = quote ? 2 : 0; + if (buf != NULL) { + if (quote) { + *buf++ = '"'; + } + *buf = '\0'; + } + + bool multi_mods = false; + + // :aboveleft and :leftabove + if (cmdmod.split & WSP_ABOVE) { + result += add_cmd_modifier(buf, "aboveleft", &multi_mods); + } + // :belowright and :rightbelow + if (cmdmod.split & WSP_BELOW) { + result += add_cmd_modifier(buf, "belowright", &multi_mods); + } + // :botright + if (cmdmod.split & WSP_BOT) { + result += add_cmd_modifier(buf, "botright", &multi_mods); + } + + typedef struct { + bool *set; + char *name; + } mod_entry_T; + static mod_entry_T mod_entries[] = { + { &cmdmod.browse, "browse" }, + { &cmdmod.confirm, "confirm" }, + { &cmdmod.hide, "hide" }, + { &cmdmod.keepalt, "keepalt" }, + { &cmdmod.keepjumps, "keepjumps" }, + { &cmdmod.keepmarks, "keepmarks" }, + { &cmdmod.keeppatterns, "keeppatterns" }, + { &cmdmod.lockmarks, "lockmarks" }, + { &cmdmod.noswapfile, "noswapfile" } + }; + // the modifiers that are simple flags + for (size_t i = 0; i < ARRAY_SIZE(mod_entries); i++) { + if (*mod_entries[i].set) { + result += add_cmd_modifier(buf, mod_entries[i].name, &multi_mods); + } } - } - // TODO(vim): How to support :noautocmd? - // TODO(vim): How to support :sandbox? + // TODO(vim): How to support :noautocmd? + // TODO(vim): How to support :sandbox? - // :silent - if (msg_silent > 0) { - result += add_cmd_modifier(buf, emsg_silent > 0 ? "silent!" : "silent", - &multi_mods); - } - // :tab - if (cmdmod.tab > 0) { - result += add_cmd_modifier(buf, "tab", &multi_mods); - } - // :topleft - if (cmdmod.split & WSP_TOP) { - result += add_cmd_modifier(buf, "topleft", &multi_mods); - } + // :silent + if (msg_silent > 0) { + result += add_cmd_modifier(buf, emsg_silent > 0 ? "silent!" : "silent", + &multi_mods); + } + // :tab + if (cmdmod.tab > 0) { + result += add_cmd_modifier(buf, "tab", &multi_mods); + } + // :topleft + if (cmdmod.split & WSP_TOP) { + result += add_cmd_modifier(buf, "topleft", &multi_mods); + } - // TODO(vim): How to support :unsilent? + // TODO(vim): How to support :unsilent? - // :verbose - if (p_verbose > 0) { - result += add_cmd_modifier(buf, "verbose", &multi_mods); - } - // :vertical - if (cmdmod.split & WSP_VERT) { - result += add_cmd_modifier(buf, "vertical", &multi_mods); - } - if (quote && buf != NULL) { - buf += result - 2; - *buf = '"'; + // :verbose + if (p_verbose > 0) { + result += add_cmd_modifier(buf, "verbose", &multi_mods); + } + // :vertical + if (cmdmod.split & WSP_VERT) { + result += add_cmd_modifier(buf, "vertical", &multi_mods); + } + if (quote && buf != NULL) { + buf += result - 2; + *buf = '"'; + } + break; } - break; - } case ct_REGISTER: result = eap->regname ? 1 : 0; - if (quote) + if (quote) { result += 2; + } if (buf != NULL) { - if (quote) + if (quote) { *buf++ = '\''; - if (eap->regname) + } + if (eap->regname) { *buf++ = eap->regname; - if (quote) + } + if (quote) { *buf = '\''; + } } break; case ct_LT: result = 1; - if (buf != NULL) + if (buf != NULL) { *buf = '<'; + } break; default: - /* Not recognized: just copy the '<' and return -1. */ + // Not recognized: just copy the '<' and return -1. result = (size_t)-1; - if (buf != NULL) + if (buf != NULL) { *buf = '<'; + } break; } @@ -6023,10 +6146,11 @@ static void do_ucmd(exarg_T *eap) ucmd_T *cmd; const sctx_T save_current_sctx = current_sctx; - if (eap->cmdidx == CMD_USER) + if (eap->cmdidx == CMD_USER) { cmd = USER_CMD(eap->useridx); - else + } else { cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx); + } /* * Replace <> in the command by the arguments. @@ -6035,14 +6159,15 @@ static void do_ucmd(exarg_T *eap) */ buf = NULL; for (;; ) { - p = cmd->uc_rep; /* source */ - q = buf; /* destination */ + p = cmd->uc_rep; // source + q = buf; // destination totlen = 0; for (;; ) { start = vim_strchr(p, '<'); - if (start != NULL) + if (start != NULL) { end = vim_strchr(start + 1, '>'); + } if (buf != NULL) { for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ksp++) { } @@ -6064,41 +6189,44 @@ static void do_ucmd(exarg_T *eap) } } - /* break if there no <item> is found */ - if (start == NULL || end == NULL) + // break if there no <item> is found + if (start == NULL || end == NULL) { break; + } - /* Include the '>' */ + // Include the '>' ++end; - /* Take everything up to the '<' */ + // Take everything up to the '<' len = start - p; - if (buf == NULL) + if (buf == NULL) { totlen += len; - else { + } else { memmove(q, p, len); q += len; } len = uc_check_code(start, end - start, q, cmd, eap, - &split_buf, &split_len); + &split_buf, &split_len); if (len == (size_t)-1) { - /* no match, continue after '<' */ + // no match, continue after '<' p = start + 1; len = 1; - } else + } else { p = end; - if (buf == NULL) + } + if (buf == NULL) { totlen += len; - else + } else { q += len; + } } - if (buf != NULL) { /* second time here, finished */ + if (buf != NULL) { // second time here, finished STRCPY(q, p); break; } - totlen += STRLEN(p); /* Add on the trailing characters */ + totlen += STRLEN(p); // Add on the trailing characters buf = xmalloc(totlen + 1); } @@ -6149,12 +6277,13 @@ char_u *get_user_commands(expand_T *xp FUNC_ATTR_UNUSED, int idx) */ char_u *get_user_cmd_flags(expand_T *xp, int idx) { - static char *user_cmd_flags[] = {"addr", "bang", "bar", - "buffer", "complete", "count", - "nargs", "range", "register"}; + static char *user_cmd_flags[] = { "addr", "bang", "bar", + "buffer", "complete", "count", + "nargs", "range", "register" }; - if (idx >= (int)ARRAY_SIZE(user_cmd_flags)) + if (idx >= (int)ARRAY_SIZE(user_cmd_flags)) { return NULL; + } return (char_u *)user_cmd_flags[idx]; } @@ -6163,10 +6292,11 @@ char_u *get_user_cmd_flags(expand_T *xp, int idx) */ char_u *get_user_cmd_nargs(expand_T *xp, int idx) { - static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"}; + static char *user_cmd_nargs[] = { "0", "1", "*", "?", "+" }; - if (idx >= (int)ARRAY_SIZE(user_cmd_nargs)) + if (idx >= (int)ARRAY_SIZE(user_cmd_nargs)) { return NULL; + } return (char_u *)user_cmd_nargs[idx]; } @@ -6222,8 +6352,8 @@ int parse_addr_type_arg(char_u *value, int vallen, cmd_addr_T *addr_type_arg) * copied to allocated memory and stored in "*compl_arg". * Returns FAIL if something is wrong. */ -int parse_compl_arg(const char_u *value, int vallen, int *complp, - uint32_t *argt, char_u **compl_arg) +int parse_compl_arg(const char_u *value, int vallen, int *complp, uint32_t *argt, + char_u **compl_arg) FUNC_ATTR_NONNULL_ALL { const char_u *arg = NULL; @@ -6231,7 +6361,7 @@ int parse_compl_arg(const char_u *value, int vallen, int *complp, int i; int valend = vallen; - /* Look for any argument part - which is the part after any ',' */ + // Look for any argument part - which is the part after any ',' for (i = 0; i < vallen; ++i) { if (value[i] == ',') { arg = &value[i + 1]; @@ -6282,17 +6412,17 @@ int parse_compl_arg(const char_u *value, int vallen, int *complp, int cmdcomplete_str_to_type(const char *complete_str) { - for (int i = 0; i < (int)(ARRAY_SIZE(command_complete)); i++) { - char *cmd_compl = get_command_complete(i); - if (cmd_compl == NULL) { - continue; - } - if (strcmp(complete_str, command_complete[i]) == 0) { - return i; - } + for (int i = 0; i < (int)(ARRAY_SIZE(command_complete)); i++) { + char *cmd_compl = get_command_complete(i); + if (cmd_compl == NULL) { + continue; } + if (strcmp(complete_str, command_complete[i]) == 0) { + return i; + } + } - return EXPAND_NOTHING; + return EXPAND_NOTHING; } static void ex_colorscheme(exarg_T *eap) @@ -6309,10 +6439,12 @@ static void ex_colorscheme(exarg_T *eap) if (p != NULL) { MSG(p); xfree(p); - } else + } else { MSG("default"); - } else if (load_colors(eap->arg) == FAIL) + } + } else if (load_colors(eap->arg) == FAIL) { EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg); + } } static void ex_highlight(exarg_T *eap) @@ -6370,7 +6502,7 @@ static void ex_quit(exarg_T *eap) cmdwin_result = Ctrl_C; return; } - /* Don't quit while editing the command line. */ + // Don't quit while editing the command line. if (text_locked()) { text_locked_msg(); return; @@ -6382,8 +6514,9 @@ static void ex_quit(exarg_T *eap) int wnr = eap->line2; for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next) { - if (--wnr <= 0) + if (--wnr <= 0) { break; + } } } else { wp = curwin; @@ -6445,7 +6578,7 @@ static void ex_quit_all(exarg_T *eap) return; } - /* Don't quit while editing the command line. */ + // Don't quit while editing the command line. if (text_locked()) { text_locked_msg(); return; @@ -6482,8 +6615,9 @@ static void ex_close(exarg_T *eap) break; } } - if (win == NULL) + if (win == NULL) { win = lastwin; + } ex_win_close(eap->forceit, win, NULL); } } @@ -6502,16 +6636,11 @@ static void ex_pclose(exarg_T *eap) } } -/* - * Close window "win" and take care of handling closing the last window for a - * modified buffer. - */ -void -ex_win_close( - int forceit, - win_T *win, - tabpage_T *tp /* NULL or the tab page "win" is in */ -) +/// Close window "win" and take care of handling closing the last window for a +/// modified buffer. +/// +/// @param tp NULL or the tab page "win" is in +void ex_win_close(int forceit, win_T *win, tabpage_T *tp) { int need_hide; buf_T *buf = win->w_buffer; @@ -6555,11 +6684,11 @@ static void ex_tabclose(exarg_T *eap) { tabpage_T *tp; - if (cmdwin_type != 0) + if (cmdwin_type != 0) { cmdwin_result = K_IGNORE; - else if (first_tabpage->tp_next == NULL) + } else if (first_tabpage->tp_next == NULL) { EMSG(_("E784: Cannot close last tab page")); - else { + } else { int tab_number = get_tabpage_arg(eap); if (eap->errmsg == NULL) { tp = find_tabpage(tab_number); @@ -6583,7 +6712,7 @@ static void ex_tabonly(exarg_T *eap) if (cmdwin_type != 0) { cmdwin_result = K_IGNORE; } else if (first_tabpage->tp_next == NULL) { - MSG(_("Already only one tab page")); + MSG(_("Already only one tab page")); } else { int tab_number = get_tabpage_arg(eap); if (eap->errmsg == NULL) { @@ -6651,8 +6780,9 @@ void tabpage_close_other(tabpage_T *tp, int forceit) /* Autocommands may delete the tab page under our fingers and we may * fail to close a window with a modified buffer. */ - if (!valid_tabpage(tp) || tp->tp_firstwin == wp) + if (!valid_tabpage(tp) || tp->tp_firstwin == wp) { break; + } } redraw_tabline = true; @@ -6672,10 +6802,11 @@ static void ex_only(exarg_T *eap) if (eap->addr_count > 0) { wnr = eap->line2; for (wp = firstwin; --wnr > 0;) { - if (wp->w_next == NULL) + if (wp->w_next == NULL) { break; - else + } else { wp = wp->w_next; + } } } else { wp = curwin; @@ -6692,34 +6823,35 @@ static void ex_only(exarg_T *eap) */ void ex_all(exarg_T *eap) { - if (eap->addr_count == 0) + if (eap->addr_count == 0) { eap->line2 = 9999; + } do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop); } static void ex_hide(exarg_T *eap) { - // ":hide" or ":hide | cmd": hide current window - if (!eap->skip) { - if (eap->addr_count == 0) { - win_close(curwin, false); // don't free buffer - } else { - int winnr = 0; - win_T *win = NULL; - - FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { - winnr++; - if (winnr == eap->line2) { - win = wp; - break; - } - } - if (win == NULL) { - win = lastwin; - } - win_close(win, false); + // ":hide" or ":hide | cmd": hide current window + if (!eap->skip) { + if (eap->addr_count == 0) { + win_close(curwin, false); // don't free buffer + } else { + int winnr = 0; + win_T *win = NULL; + + FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { + winnr++; + if (winnr == eap->line2) { + win = wp; + break; } + } + if (win == NULL) { + win = lastwin; + } + win_close(win, false); } + } } /// ":stop" and ":suspend": Suspend Vim. @@ -6750,7 +6882,7 @@ static void ex_exit(exarg_T *eap) cmdwin_result = Ctrl_C; return; } - /* Don't quit while editing the command line. */ + // Don't quit while editing the command line. if (text_locked()) { text_locked_msg(); return; @@ -6784,20 +6916,21 @@ static void ex_exit(exarg_T *eap) */ static void ex_print(exarg_T *eap) { - if (curbuf->b_ml.ml_flags & ML_EMPTY) + if (curbuf->b_ml.ml_flags & ML_EMPTY) { EMSG(_(e_emptybuf)); - else { + } else { for (; !got_int; os_breakcheck()) { print_line(eap->line1, - (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound - || (eap->flags & EXFLAG_NR)), - eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST)); - if (++eap->line1 > eap->line2) + (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound + || (eap->flags & EXFLAG_NR)), + eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST)); + if (++eap->line1 > eap->line2) { break; - ui_flush(); /* show one line at a time */ + } + ui_flush(); // show one line at a time } setpcmark(); - /* put cursor at last line */ + // put cursor at last line curwin->w_cursor.lnum = eap->line2; beginline(BL_SOL | BL_FIX); } @@ -6872,15 +7005,16 @@ void alist_expand(int *fnum_list, int fnum_len) * can't set the options. */ p_su = empty_option; old_arg_files = xmalloc(sizeof(*old_arg_files) * GARGCOUNT); - for (i = 0; i < GARGCOUNT; ++i) + for (i = 0; i < GARGCOUNT; ++i) { old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname); + } old_arg_count = GARGCOUNT; if (expand_wildcards(old_arg_count, old_arg_files, - &new_arg_file_count, &new_arg_files, - EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK + &new_arg_file_count, &new_arg_files, + EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK && new_arg_file_count > 0) { alist_set(&global_alist, new_arg_file_count, new_arg_files, - TRUE, fnum_list, fnum_len); + TRUE, fnum_list, fnum_len); FreeWild(old_arg_count, old_arg_files); } p_su = save_p_su; @@ -6909,15 +7043,17 @@ void alist_set(alist_T *al, int count, char_u **files, int use_curbuf, int *fnum if (got_int) { /* When adding many buffers this can take a long time. Allow * interrupting here. */ - while (i < count) + while (i < count) { xfree(files[i++]); + } break; } /* May set buffer name of a buffer previously used for the * argument list, so that it's re-used by alist_add. */ - if (fnum_list != NULL && i < fnum_len) + if (fnum_list != NULL && i < fnum_len) { buf_set_name(fnum_list[i], files[i]); + } alist_add(al, files[i], use_curbuf ? 2 : 1); os_breakcheck(); @@ -6931,26 +7067,23 @@ void alist_set(alist_T *al, int count, char_u **files, int use_curbuf, int *fnum recursive--; } -/* - * Add file "fname" to argument list "al". - * "fname" must have been allocated and "al" must have been checked for room. - */ -void -alist_add( - alist_T *al, - char_u *fname, - int set_fnum /* 1: set buffer number; 2: re-use curbuf */ -) -{ - if (fname == NULL) /* don't add NULL file names */ +/// Add file "fname" to argument list "al". +/// "fname" must have been allocated and "al" must have been checked for room. +/// +/// @param set_fnum 1: set buffer number; 2: re-use curbuf +void alist_add(alist_T *al, char_u *fname, int set_fnum) +{ + if (fname == NULL) { // don't add NULL file names return; + } #ifdef BACKSLASH_IN_FILENAME slash_adjust(fname); #endif AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname; - if (set_fnum > 0) + if (set_fnum > 0) { AARGLIST(al)[al->al_ga.ga_len].ae_fnum = buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0)); + } ++al->al_ga.ga_len; } @@ -6992,9 +7125,9 @@ static void ex_recover(exarg_T *eap) // Set recoverymode right away to avoid the ATTENTION prompt. recoverymode = true; if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0) - | CCGD_MULTWIN - | (eap->forceit ? CCGD_FORCEIT : 0) - | CCGD_EXCMD) + | CCGD_MULTWIN + | (eap->forceit ? CCGD_FORCEIT : 0) + | CCGD_EXCMD) && (*eap->arg == NUL || setfname(curbuf, eap->arg, NULL, true) == OK)) { @@ -7012,40 +7145,43 @@ static void ex_wrongmodifier(exarg_T *eap) } /* - * :sview [+command] file split window with new file, read-only - * :split [[+command] file] split window with current or new file - * :vsplit [[+command] file] split window vertically with current or new file - * :new [[+command] file] split window with no or new file - * :vnew [[+command] file] split vertically window with no or new file - * :sfind [+command] file split window with file in 'path' + * :sview [+command] file split window with new file, read-only + * :split [[+command] file] split window with current or new file + * :vsplit [[+command] file] split window vertically with current or new file + * :new [[+command] file] split window with no or new file + * :vnew [[+command] file] split vertically window with no or new file + * :sfind [+command] file split window with file in 'path' * - * :tabedit open new Tab page with empty window - * :tabedit [+command] file open new Tab page and edit "file" - * :tabnew [[+command] file] just like :tabedit - * :tabfind [+command] file open new Tab page and find "file" + * :tabedit open new Tab page with empty window + * :tabedit [+command] file open new Tab page and edit "file" + * :tabnew [[+command] file] just like :tabedit + * :tabfind [+command] file open new Tab page and find "file" */ void ex_splitview(exarg_T *eap) { win_T *old_curwin = curwin; char_u *fname = NULL; const bool use_tab = eap->cmdidx == CMD_tabedit - || eap->cmdidx == CMD_tabfind - || eap->cmdidx == CMD_tabnew; + || eap->cmdidx == CMD_tabfind + || eap->cmdidx == CMD_tabnew; /* A ":split" in the quickfix window works like ":new". Don't want two * quickfix windows. But it's OK when doing ":tab split". */ if (bt_quickfix(curbuf) && cmdmod.tab == 0) { - if (eap->cmdidx == CMD_split) + if (eap->cmdidx == CMD_split) { eap->cmdidx = CMD_new; - if (eap->cmdidx == CMD_vsplit) + } + if (eap->cmdidx == CMD_vsplit) { eap->cmdidx = CMD_vnew; + } } if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind) { fname = find_file_in_path(eap->arg, STRLEN(eap->arg), FNAME_MESS, TRUE, curbuf->b_ffname); - if (fname == NULL) + if (fname == NULL) { goto theend; + } eap->arg = fname; } @@ -7058,15 +7194,16 @@ void ex_splitview(exarg_T *eap) do_exedit(eap, old_curwin); apply_autocmds(EVENT_TABNEWENTERED, NULL, NULL, FALSE, curbuf); - /* set the alternate buffer for the window we came from */ + // set the alternate buffer for the window we came from if (curwin != old_curwin && win_valid(old_curwin) && old_curwin->w_buffer != curbuf - && !cmdmod.keepalt) + && !cmdmod.keepalt) { old_curwin->w_alt_fnum = curbuf->b_fnum; + } } } else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0, - *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL) { + *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL) { /* Reset 'scrollbind' when editing another file, but keep it when * doing ":split" without arguments. */ if (*eap->arg != NUL @@ -7173,7 +7310,7 @@ static void ex_tabs(exarg_T *eap) FOR_ALL_TABS(tp) { if (got_int) { - break; + break; } msg_putchar('\n'); @@ -7192,13 +7329,14 @@ static void ex_tabs(exarg_T *eap) msg_putchar(' '); msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' '); msg_putchar(' '); - if (buf_spname(wp->w_buffer) != NULL) + if (buf_spname(wp->w_buffer) != NULL) { STRLCPY(IObuff, buf_spname(wp->w_buffer), IOSIZE); - else + } else { home_replace(wp->w_buffer, wp->w_buffer->b_fname, - IObuff, IOSIZE, TRUE); + IObuff, IOSIZE, TRUE); + } msg_outtrans(IObuff); - ui_flush(); /* output one line at a time */ + ui_flush(); // output one line at a time os_breakcheck(); } } @@ -7230,8 +7368,9 @@ static void ex_resize(exarg_T *eap) if (eap->addr_count > 0) { n = eap->line2; - for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next) + for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next) { ; + } } n = atol((char *)eap->arg); @@ -7285,14 +7424,10 @@ static void ex_edit(exarg_T *eap) do_exedit(eap, NULL); } -/* - * ":edit <file>" command and alikes. - */ -void -do_exedit( - exarg_T *eap, - win_T *old_curwin /* curwin before doing a split or NULL */ -) +/// ":edit <file>" command and alikes. +/// +/// @param old_curwin curwin before doing a split or NULL +void do_exedit(exarg_T *eap, win_T *old_curwin) { int n; int need_hide; @@ -7305,7 +7440,7 @@ do_exedit( exmode_active = false; ex_pressedreturn = false; if (*eap->arg == NUL) { - /* Special case: ":global/pat/visual\NLvi-commands" */ + // Special case: ":global/pat/visual\NLvi-commands" if (global_busy) { int rd = RedrawingDisabled; int nwr = no_wait_return; @@ -7337,11 +7472,11 @@ do_exedit( || eap->cmdidx == CMD_tabedit || eap->cmdidx == CMD_vnew ) && *eap->arg == NUL) { - /* ":new" or ":tabnew" without argument: edit an new empty buffer */ + // ":new" or ":tabnew" without argument: edit an new empty buffer setpcmark(); (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE, - ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0), - old_curwin == NULL ? curwin : NULL); + ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0), + old_curwin == NULL ? curwin : NULL); } else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit) || *eap->arg != NUL) { // Can't edit another file when "curbuf->b_ro_lockec" is set. Only ":edit" @@ -7394,12 +7529,14 @@ do_exedit( } readonlymode = n; } else { - if (eap->do_ecmd_cmd != NULL) + if (eap->do_ecmd_cmd != NULL) { do_cmdline_cmd((char *)eap->do_ecmd_cmd); + } n = curwin->w_arg_idx_invalid; check_arg_idx(curwin); - if (n != curwin->w_arg_idx_invalid) + if (n != curwin->w_arg_idx_invalid) { maketitle(); + } } /* @@ -7411,8 +7548,9 @@ do_exedit( && curwin != old_curwin && win_valid(old_curwin) && old_curwin->w_buffer != curbuf - && !cmdmod.keepalt) + && !cmdmod.keepalt) { old_curwin->w_alt_fnum = curbuf->b_fnum; + } ex_no_reprint = true; } @@ -7427,10 +7565,11 @@ static void ex_nogui(exarg_T *eap) static void ex_swapname(exarg_T *eap) { - if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL) + if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL) { MSG(_("No swap file")); - else + } else { msg(curbuf->b_ml.ml_mfp->mf_fname); + } } /* @@ -7477,10 +7616,11 @@ static void ex_syncbind(exarg_T *eap) if (curwin->w_p_scb) { curbuf = curwin->w_buffer; y = topline - curwin->w_topline; - if (y > 0) + if (y > 0) { scrollup(y, TRUE); - else + } else { scrolldown(-y, TRUE); + } curwin->w_scbind_pos = topline; redraw_later(curwin, VALID); cursor_correct(); @@ -7521,13 +7661,13 @@ static void ex_read(exarg_T *eap) return; } i = readfile(curbuf->b_ffname, curbuf->b_fname, - eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0); + eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0); } else { - if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL) + if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL) { (void)setaltfname(eap->arg, eap->arg, (linenr_T)1); + } i = readfile(eap->arg, NULL, - eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0); - + eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0); } if (i != OK) { if (!aborting()) { @@ -7537,10 +7677,11 @@ static void ex_read(exarg_T *eap) if (empty && exmode_active) { /* Delete the empty line that remains. Historically ex does * this but vi doesn't. */ - if (eap->line2 == 0) + if (eap->line2 == 0) { lnum = curbuf->b_ml.ml_line_count; - else + } else { lnum = 1; + } if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK) { ml_delete(lnum, false); if (curwin->w_cursor.lnum > 1 @@ -7620,16 +7761,17 @@ void ex_cd(exarg_T *eap) new_dir = eap->arg; #if !defined(UNIX) - /* for non-UNIX ":cd" means: print current directory */ - if (*new_dir == NUL) + // for non-UNIX ":cd" means: print current directory + if (*new_dir == NUL) { ex_pwd(NULL); - else + } else #endif { - if (allbuf_locked()) + if (allbuf_locked()) { return; + } - /* ":cd -": Change to previous directory */ + // ":cd -": Change to previous directory if (STRCMP(new_dir, "-") == 0) { if (prev_dir == NULL) { EMSG(_("E186: No previous directory")); @@ -7638,12 +7780,13 @@ void ex_cd(exarg_T *eap) new_dir = prev_dir; } - /* Save current directory for next ":cd -" */ + // Save current directory for next ":cd -" tofree = prev_dir; - if (os_dirname(NameBuff, MAXPATHL) == OK) + if (os_dirname(NameBuff, MAXPATHL) == OK) { prev_dir = vim_strsave(NameBuff); - else + } else { prev_dir = NULL; + } #if defined(UNIX) // On Unix ":cd" means: go to home directory. @@ -7692,8 +7835,9 @@ static void ex_pwd(exarg_T *eap) slash_adjust(NameBuff); #endif msg(NameBuff); - } else + } else { EMSG(_("E187: Unknown")); + } } /* @@ -7712,15 +7856,19 @@ static void ex_sleep(exarg_T *eap) if (cursor_valid()) { n = curwin->w_winrow + curwin->w_wrow - msg_scrolled; - if (n >= 0) + if (n >= 0) { ui_cursor_goto(n, curwin->w_wincol + curwin->w_wcol); + } } len = eap->line2; switch (*eap->arg) { - case 'm': break; - case NUL: len *= 1000L; break; - default: EMSG2(_(e_invarg2), eap->arg); return; + case 'm': + break; + case NUL: + len *= 1000L; break; + default: + EMSG2(_(e_invarg2), eap->arg); return; } do_sleep(len); } @@ -7753,10 +7901,12 @@ static void do_exmap(exarg_T *eap, int isabbrev) mode = get_map_mode(&cmdp, eap->forceit || isabbrev); switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'), - eap->arg, mode, isabbrev)) { - case 1: EMSG(_(e_invarg)); + eap->arg, mode, isabbrev)) { + case 1: + EMSG(_(e_invarg)); break; - case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap)); + case 2: + EMSG(isabbrev ? _(e_noabbr) : _(e_nomap)); break; } } @@ -7789,22 +7939,23 @@ static void ex_wincmd(exarg_T *eap) char_u *p; if (*eap->arg == 'g' || *eap->arg == Ctrl_G) { - /* CTRL-W g and CTRL-W CTRL-G have an extra command character */ + // CTRL-W g and CTRL-W CTRL-G have an extra command character if (eap->arg[1] == NUL) { EMSG(_(e_invarg)); return; } xchar = eap->arg[1]; p = eap->arg + 2; - } else + } else { p = eap->arg + 1; + } eap->nextcmd = check_nextcmd(p); p = skipwhite(p); - if (*p != NUL && *p != '"' && eap->nextcmd == NULL) + if (*p != NUL && *p != '"' && eap->nextcmd == NULL) { EMSG(_(e_invarg)); - else if (!eap->skip) { - /* Pass flags on for ":vertical wincmd ]". */ + } else if (!eap->skip) { + // Pass flags on for ":vertical wincmd ]". postponed_split_flags = cmdmod.split; postponed_split_tab = cmdmod.tab; do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar); @@ -7833,8 +7984,9 @@ static void ex_operators(exarg_T *eap) beginline(BL_SOL | BL_FIX); } - if (VIsual_active) + if (VIsual_active) { end_visual_mode(); + } switch (eap->cmdidx) { case CMD_delete: @@ -7847,13 +7999,14 @@ static void ex_operators(exarg_T *eap) (void)op_yank(&oa, true, false); break; - default: /* CMD_rshift or CMD_lshift */ + default: // CMD_rshift or CMD_lshift if ( - (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl - ) + (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl + ) { oa.op_type = OP_RSHIFT; - else + } else { oa.op_type = OP_LSHIFT; + } op_shift(&oa, FALSE, eap->amount); break; } @@ -7866,7 +8019,7 @@ static void ex_operators(exarg_T *eap) */ static void ex_put(exarg_T *eap) { - /* ":0put" works like ":1put!". */ + // ":0put" works like ":1put!". if (eap->line2 == 0) { eap->line2 = 1; eap->forceit = TRUE; @@ -7897,10 +8050,12 @@ static void ex_copymove(exarg_T *eap) } if (eap->cmdidx == CMD_move) { - if (do_move(eap->line1, eap->line2, n) == FAIL) + if (do_move(eap->line1, eap->line2, n) == FAIL) { return; - } else + } + } else { ex_copy(eap->line1, eap->line2, n); + } u_clearline(); beginline(BL_SOL | BL_FIX); ex_may_print(eap); @@ -7935,8 +8090,9 @@ static void ex_join(exarg_T *eap) { curwin->w_cursor.lnum = eap->line1; if (eap->line1 == eap->line2) { - if (eap->addr_count >= 2) /* :2,2join does nothing */ + if (eap->addr_count >= 2) { // :2,2join does nothing return; + } if (eap->line2 == curbuf->b_ml.ml_line_count) { beep_flush(); return; @@ -7964,7 +8120,7 @@ static void ex_at(exarg_T *eap) c = '@'; } - /* Put the register in the typeahead buffer with the "silent" flag. */ + // Put the register in the typeahead buffer with the "silent" flag. if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE) == FAIL) { beep_flush(); @@ -7978,8 +8134,9 @@ static void ex_at(exarg_T *eap) * Continue until the stuff buffer is empty and all added characters * have been consumed. */ - while (!stuff_empty() || typebuf.tb_len > prev_len) + while (!stuff_empty() || typebuf.tb_len > prev_len) { (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE); + } exec_from_reg = save_efr; } @@ -8040,11 +8197,16 @@ static void ex_later(exarg_T *eap) } else if (isdigit(*p)) { count = getdigits_long(&p, false, 0); switch (*p) { - case 's': ++p; sec = true; break; - case 'm': ++p; sec = true; count *= 60; break; - case 'h': ++p; sec = true; count *= 60 * 60; break; - case 'd': ++p; sec = true; count *= 24 * 60 * 60; break; - case 'f': ++p; file = true; break; + case 's': + ++p; sec = true; break; + case 'm': + ++p; sec = true; count *= 60; break; + case 'h': + ++p; sec = true; count *= 60 * 60; break; + case 'd': + ++p; sec = true; count *= 24 * 60 * 60; break; + case 'f': + ++p; file = true; break; } } @@ -8065,39 +8227,42 @@ static void ex_redir(exarg_T *eap) char_u *fname; char_u *arg = eap->arg; - if (STRICMP(eap->arg, "END") == 0) + if (STRICMP(eap->arg, "END") == 0) { close_redir(); - else { + } else { if (*arg == '>') { ++arg; if (*arg == '>') { ++arg; mode = "a"; - } else + } else { mode = "w"; + } arg = skipwhite(arg); close_redir(); - /* Expand environment variables and "~/". */ + // Expand environment variables and "~/". fname = expand_env_save(arg); - if (fname == NULL) + if (fname == NULL) { return; + } redir_fd = open_exfile(fname, eap->forceit, mode); xfree(fname); } else if (*arg == '@') { - /* redirect to a register a-z (resp. A-Z for appending) */ + // redirect to a register a-z (resp. A-Z for appending) close_redir(); ++arg; if (valid_yank_reg(*arg, true) && *arg != '_') { redir_reg = *arg++; - if (*arg == '>' && arg[1] == '>') /* append */ + if (*arg == '>' && arg[1] == '>') { // append arg += 2; - else { - /* Can use both "@a" and "@a>". */ - if (*arg == '>') + } else { + // Can use both "@a" and "@a>". + if (*arg == '>') { arg++; + } // Make register empty when not using @A-@Z and the // command is valid. if (*arg == NUL && !isupper(redir_reg)) { @@ -8112,30 +8277,33 @@ static void ex_redir(exarg_T *eap) } else if (*arg == '=' && arg[1] == '>') { int append; - /* redirect to a variable */ + // redirect to a variable close_redir(); arg += 2; if (*arg == '>') { ++arg; append = TRUE; - } else + } else { append = FALSE; + } - if (var_redir_start(skipwhite(arg), append) == OK) + if (var_redir_start(skipwhite(arg), append) == OK) { redir_vname = 1; + } } - /* TODO: redirect to a buffer */ - else + // TODO: redirect to a buffer + else { EMSG2(_(e_invarg2), eap->arg); + } } /* Make sure redirection is not off. Can happen for cmdline completion * that indirectly invokes a command to catch its output. */ if (redir_fd != NULL - || redir_reg || redir_vname - ) + || redir_reg || redir_vname) { redir_off = false; + } } /// ":redraw": force redraw @@ -8155,7 +8323,7 @@ static void ex_redraw(exarg_T *eap) redraw_all_later(NOT_VALID); } update_screen(eap->forceit ? NOT_VALID - : VIsual_active ? INVERTED : 0); + : VIsual_active ? INVERTED : 0); if (need_maketitle) { maketitle(); } @@ -8183,13 +8351,13 @@ static void ex_redrawstatus(exarg_T *eap) RedrawingDisabled = 0; p_lz = FALSE; - if (eap->forceit) + if (eap->forceit) { status_redraw_all(); - else + } else { status_redraw_curbuf(); - update_screen( - VIsual_active ? INVERTED : - 0); + } + update_screen(VIsual_active ? INVERTED : + 0); RedrawingDisabled = r; p_lz = p; ui_flush(); @@ -8241,21 +8409,17 @@ int vim_mkdir_emsg(const char *const name, const int prot) return OK; } -/* - * Open a file for writing for an Ex command, with some checks. - * Return file descriptor, or NULL on failure. - */ -FILE * -open_exfile ( - char_u *fname, - int forceit, - char *mode /* "w" for create new file or "a" for append */ -) +/// Open a file for writing for an Ex command, with some checks. +/// +/// @param mode "w" for create new file or "a" for append +/// +/// @return file descriptor, or NULL on failure. +FILE *open_exfile(char_u *fname, int forceit, char *mode) { FILE *fd; #ifdef UNIX - /* with Unix it is possible to open a directory */ + // with Unix it is possible to open a directory if (os_isdir(fname)) { EMSG2(_(e_isadir2), fname); return NULL; @@ -8280,17 +8444,18 @@ static void ex_mark(exarg_T *eap) { pos_T pos; - if (*eap->arg == NUL) /* No argument? */ + if (*eap->arg == NUL) { // No argument? EMSG(_(e_argreq)); - else if (eap->arg[1] != NUL) /* more than one character? */ + } else if (eap->arg[1] != NUL) { // more than one character? EMSG(_(e_trailing)); - else { - pos = curwin->w_cursor; /* save curwin->w_cursor */ + } else { + pos = curwin->w_cursor; // save curwin->w_cursor curwin->w_cursor.lnum = eap->line2; beginline(BL_WHITE | BL_FIX); - if (setmark(*eap->arg) == FAIL) /* set mark */ + if (setmark(*eap->arg) == FAIL) { // set mark EMSG(_("E191: Argument must be a letter or forward/backward quote")); - curwin->w_cursor = pos; /* restore curwin->w_cursor */ + } + curwin->w_cursor = pos; // restore curwin->w_cursor } } @@ -8389,12 +8554,14 @@ static void ex_normal(exarg_T *eap) { int len = 0; - /* Count the number of characters to be escaped. */ + // Count the number of characters to be escaped. for (p = eap->arg; *p != NUL; ++p) { - for (l = (*mb_ptr2len)(p) - 1; l > 0; --l) - if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */ - ) + for (l = (*mb_ptr2len)(p) - 1; l > 0; --l) { + if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI + ) { len += 2; + } + } } if (len > 0) { arg = xmalloc(STRLEN(eap->arg) + len + 1); @@ -8430,7 +8597,7 @@ static void ex_normal(exarg_T *eap) } while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int); } - /* Might not return to the main loop when in an event handler. */ + // Might not return to the main loop when in an event handler. update_topline_cursor(); restore_current_state(&save_state); @@ -8461,16 +8628,18 @@ static void ex_startinsert(exarg_T *eap) return; } - if (eap->cmdidx == CMD_startinsert) + if (eap->cmdidx == CMD_startinsert) { restart_edit = 'a'; - else if (eap->cmdidx == CMD_startreplace) + } else if (eap->cmdidx == CMD_startreplace) { restart_edit = 'R'; - else + } else { restart_edit = 'V'; + } if (!eap->forceit) { - if (eap->cmdidx == CMD_startinsert) + if (eap->cmdidx == CMD_startinsert) { restart_edit = 'i'; + } curwin->w_curswant = 0; // avoid MAXCOL } @@ -8543,19 +8712,20 @@ static void ex_findpat(exarg_T *eap) int action; switch (cmdnames[eap->cmdidx].cmd_name[2]) { - case 'e': /* ":psearch", ":isearch" and ":dsearch" */ - if (cmdnames[eap->cmdidx].cmd_name[0] == 'p') + case 'e': // ":psearch", ":isearch" and ":dsearch" + if (cmdnames[eap->cmdidx].cmd_name[0] == 'p') { action = ACTION_GOTO; - else + } else { action = ACTION_SHOW; + } break; - case 'i': /* ":ilist" and ":dlist" */ + case 'i': // ":ilist" and ":dlist" action = ACTION_SHOW_ALL; break; - case 'u': /* ":ijump" and ":djump" */ + case 'u': // ":ijump" and ":djump" action = ACTION_GOTO; break; - default: /* ":isplit" and ":dsplit" */ + default: // ":isplit" and ":dsplit" action = ACTION_SPLIT; break; } @@ -8581,10 +8751,11 @@ static void ex_findpat(exarg_T *eap) } } } - if (!eap->skip) + if (!eap->skip) { find_pattern_in_path(eap->arg, 0, STRLEN(eap->arg), whole, !eap->forceit, *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY, n, action, eap->line1, eap->line2); + } } @@ -8593,7 +8764,7 @@ static void ex_findpat(exarg_T *eap) */ static void ex_ptag(exarg_T *eap) { - g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */ + g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd() ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1); } @@ -8646,21 +8817,28 @@ static void ex_tag_cmd(exarg_T *eap, char_u *name) int cmd; switch (name[1]) { - case 'j': cmd = DT_JUMP; // ":tjump" + case 'j': + cmd = DT_JUMP; // ":tjump" break; - case 's': cmd = DT_SELECT; // ":tselect" + case 's': + cmd = DT_SELECT; // ":tselect" break; case 'p': // ":tprevious" - case 'N': cmd = DT_PREV; // ":tNext" + case 'N': + cmd = DT_PREV; // ":tNext" break; - case 'n': cmd = DT_NEXT; // ":tnext" + case 'n': + cmd = DT_NEXT; // ":tnext" break; - case 'o': cmd = DT_POP; // ":pop" + case 'o': + cmd = DT_POP; // ":pop" break; case 'f': // ":tfirst" - case 'r': cmd = DT_FIRST; // ":trewind" + case 'r': + cmd = DT_FIRST; // ":trewind" break; - case 'l': cmd = DT_LAST; // ":tlast" + case 'l': + cmd = DT_LAST; // ":tlast" break; default: // ":tag" if (p_cst && *eap->arg != NUL) { @@ -8676,7 +8854,7 @@ static void ex_tag_cmd(exarg_T *eap, char_u *name) } do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1, - eap->forceit, TRUE); + eap->forceit, TRUE); } enum { @@ -8735,37 +8913,35 @@ ssize_t find_cmdline_var(const char_u *src, size_t *usedlen) return -1; } -/* - * Evaluate cmdline variables. - * - * change '%' to curbuf->b_ffname - * '#' to curwin->w_alt_fnum - * '<cword>' to word under the cursor - * '<cWORD>' to WORD under the cursor - * '<cexpr>' to C-expression under the cursor - * '<cfile>' to path name under the cursor - * '<sfile>' to sourced file name - * '<slnum>' to sourced file line number - * '<afile>' to file name for autocommand - * '<abuf>' to buffer number for autocommand - * '<amatch>' to matching name for autocommand - * - * When an error is detected, "errormsg" is set to a non-NULL pointer (may be - * "" for error without a message) and NULL is returned. - * Returns an allocated string if a valid match was found. - * Returns NULL if no match was found. "usedlen" then still contains the - * number of characters to skip. - */ -char_u * -eval_vars ( - char_u *src, /* pointer into commandline */ - char_u *srcstart, /* beginning of valid memory for src */ - size_t *usedlen, /* characters after src that are used */ - linenr_T *lnump, /* line number for :e command, or NULL */ - char_u **errormsg, /* pointer to error message */ - int *escaped /* return value has escaped white space (can - * be NULL) */ -) +/// Evaluate cmdline variables. +/// +/// change '%' to curbuf->b_ffname +/// '#' to curwin->w_alt_fnum +/// '<cword>' to word under the cursor +/// '<cWORD>' to WORD under the cursor +/// '<cexpr>' to C-expression under the cursor +/// '<cfile>' to path name under the cursor +/// '<sfile>' to sourced file name +/// '<slnum>' to sourced file line number +/// '<afile>' to file name for autocommand +/// '<abuf>' to buffer number for autocommand +/// '<amatch>' to matching name for autocommand +/// +/// When an error is detected, "errormsg" is set to a non-NULL pointer (may be +/// "" for error without a message) and NULL is returned. +/// +/// @param src pointer into commandline +/// @param srcstart beginning of valid memory for src +/// @param usedlen characters after src that are used +/// @param lnump line number for :e command, or NULL +/// @param errormsg pointer to error message +/// @param escaped return value has escaped white space (can be NULL) +/// +/// @return an allocated string if a valid match was found. +/// Returns NULL if no match was found. "usedlen" then still contains the +/// number of characters to skip. +char_u *eval_vars(char_u *src, char_u *srcstart, size_t *usedlen, linenr_T *lnump, + char_u **errormsg, int *escaped) { int i; char_u *s; @@ -8779,14 +8955,15 @@ eval_vars ( char strbuf[30]; *errormsg = NULL; - if (escaped != NULL) + if (escaped != NULL) { *escaped = FALSE; + } /* * Check if there is something to do. */ ssize_t spec_idx = find_cmdline_var(src, usedlen); - if (spec_idx < 0) { /* no match */ + if (spec_idx < 0) { // no match *usedlen = 1; return NULL; } @@ -8797,7 +8974,7 @@ eval_vars ( */ if (src > srcstart && src[-1] == '\\') { *usedlen = 0; - STRMOVE(src - 1, src); /* remove backslash */ + STRMOVE(src - 1, src); // remove backslash return NULL; } @@ -8807,9 +8984,8 @@ eval_vars ( if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD || spec_idx == SPEC_CEXPR) { - resultlen = find_ident_under_cursor( - &result, - spec_idx == SPEC_CWORD + resultlen = find_ident_under_cursor(&result, + spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING) : (spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL) @@ -8818,13 +8994,13 @@ eval_vars ( *errormsg = (char_u *)""; return NULL; } - // - // '#': Alternate file name - // '%': Current file name - // File name under the cursor - // File name for autocommand - // and following modifiers - // + // + // '#': Alternate file name + // '%': Current file name + // File name under the cursor + // File name for autocommand + // and following modifiers + // } else { switch (spec_idx) { case SPEC_PERC: // '%': current file @@ -8837,13 +9013,14 @@ eval_vars ( } break; - case SPEC_HASH: /* '#' or "#99": alternate file */ - if (src[1] == '#') { /* "##": the argument list */ + case SPEC_HASH: // '#' or "#99": alternate file + if (src[1] == '#') { // "##": the argument list result = arg_all(); resultbuf = result; *usedlen = 2; - if (escaped != NULL) + if (escaped != NULL) { *escaped = TRUE; + } skip_mod = TRUE; break; } @@ -8860,7 +9037,7 @@ eval_vars ( if (src[1] == '<' && i != 0) { if (*usedlen < 2) { - /* Should we give an error message for #<text? */ + // Should we give an error message for #<text? *usedlen = 1; return NULL; } @@ -8876,12 +9053,12 @@ eval_vars ( } buf = buflist_findnr(i); if (buf == NULL) { - *errormsg = (char_u *)_( - "E194: No alternate file name to substitute for '#'"); + *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'"); return NULL; } - if (lnump != NULL) + if (lnump != NULL) { *lnump = ECMD_LAST; + } if (buf->b_fname == NULL) { result = (char_u *)""; valid = 0; // Must have ":p:h" to be valid @@ -8892,13 +9069,13 @@ eval_vars ( } break; - case SPEC_CFILE: /* file name under cursor */ + case SPEC_CFILE: // file name under cursor result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL); if (result == NULL) { *errormsg = (char_u *)""; return NULL; } - resultbuf = result; /* remember allocated string */ + resultbuf = result; // remember allocated string break; case SPEC_AFILE: // file name for autocommand @@ -8915,37 +9092,33 @@ eval_vars ( } result = autocmd_fname; if (result == NULL) { - *errormsg = (char_u *)_( - "E495: no autocommand file name to substitute for \"<afile>\""); + *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\""); return NULL; } result = path_try_shorten_fname(result); break; - case SPEC_ABUF: /* buffer number for autocommand */ + case SPEC_ABUF: // buffer number for autocommand if (autocmd_bufnr <= 0) { - *errormsg = (char_u *)_( - "E496: no autocommand buffer number to substitute for \"<abuf>\""); + *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\""); return NULL; } snprintf(strbuf, sizeof(strbuf), "%d", autocmd_bufnr); result = (char_u *)strbuf; break; - case SPEC_AMATCH: /* match name for autocommand */ + case SPEC_AMATCH: // match name for autocommand result = autocmd_match; if (result == NULL) { - *errormsg = (char_u *)_( - "E497: no autocommand match name to substitute for \"<amatch>\""); + *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\""); return NULL; } break; - case SPEC_SFILE: /* file name for ":so" command */ + case SPEC_SFILE: // file name for ":so" command result = sourcing_name; if (result == NULL) { - *errormsg = (char_u *)_( - "E498: no :source file name to substitute for \"<sfile>\""); + *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\""); return NULL; } break; @@ -9005,15 +9178,16 @@ eval_vars ( } if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH) { - if (valid != VALID_HEAD + VALID_PATH) - /* xgettext:no-c-format */ - *errormsg = (char_u *)_( - "E499: Empty file name for '%' or '#', only works with \":p:h\""); - else + if (valid != VALID_HEAD + VALID_PATH) { + // xgettext:no-c-format + *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\""); + } else { *errormsg = (char_u *)_("E500: Evaluates to an empty string"); + } result = NULL; - } else + } else { result = vim_strnsave(result, resultlen); + } xfree(resultbuf); return result; } @@ -9043,9 +9217,10 @@ static char_u *arg_all(void) continue; } if (len > 0) { - /* insert a space in between names */ - if (retval != NULL) + // insert a space in between names + if (retval != NULL) { retval[len] = ' '; + } ++len; } for (; *p != NUL; p++) { @@ -9067,13 +9242,13 @@ static char_u *arg_all(void) } } - /* second time: break here */ + // second time: break here if (retval != NULL) { retval[len] = NUL; break; } - /* allocate memory */ + // allocate memory retval = xmalloc(len + 1); } @@ -9098,18 +9273,19 @@ char_u *expand_sfile(char_u *arg) result = vim_strsave(arg); for (p = result; *p; ) { - if (STRNCMP(p, "<sfile>", 7) != 0) + if (STRNCMP(p, "<sfile>", 7) != 0) { ++p; - else { - /* replace "<sfile>" with the sourced file name, and do ":" stuff */ + } else { + // replace "<sfile>" with the sourced file name, and do ":" stuff repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL); if (errormsg != NULL) { - if (*errormsg) + if (*errormsg) { emsg(errormsg); + } xfree(result); return NULL; } - if (repl == NULL) { /* no match (cannot happen) */ + if (repl == NULL) { // no match (cannot happen) p += srclen; continue; } @@ -9122,7 +9298,7 @@ char_u *expand_sfile(char_u *arg) xfree(repl); xfree(result); result = newres; - p = newres + len; /* continue after the match */ + p = newres + len; // continue after the match } } @@ -9137,12 +9313,13 @@ static void ex_shada(exarg_T *eap) char_u *save_shada; save_shada = p_shada; - if (*p_shada == NUL) + if (*p_shada == NUL) { p_shada = (char_u *)"'100"; + } if (eap->cmdidx == CMD_rviminfo || eap->cmdidx == CMD_rshada) { - (void) shada_read_everything((char *) eap->arg, eap->forceit, false); + (void)shada_read_everything((char *)eap->arg, eap->forceit, false); } else { - shada_write_file((char *) eap->arg, eap->forceit); + shada_write_file((char *)eap->arg, eap->forceit); } p_shada = save_shada; } @@ -9153,8 +9330,9 @@ static void ex_shada(exarg_T *eap) */ void dialog_msg(char_u *buff, char *format, char_u *fname) { - if (fname == NULL) + if (fname == NULL) { fname = (char_u *)_("Untitled"); + } vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname); } @@ -9184,10 +9362,12 @@ static void ex_behave(exarg_T *eap) */ char_u *get_behave_arg(expand_T *xp, int idx) { - if (idx == 0) + if (idx == 0) { return (char_u *)"mswin"; - if (idx == 1) + } + if (idx == 1) { return (char_u *)"xterm"; + } return NULL; } @@ -9229,7 +9409,7 @@ static void ex_filetype(exarg_T *eap) bool indent = false; if (*eap->arg == NUL) { - /* Print current status. */ + // Print current status. smsg("filetype detection:%s plugin:%s indent:%s", filetype_detect == kTrue ? "ON" : "OFF", filetype_plugin == kTrue ? (filetype_detect == kTrue ? "ON" : "(on)") : "OFF", // NOLINT(whitespace/line_length) @@ -9237,7 +9417,7 @@ static void ex_filetype(exarg_T *eap) return; } - /* Accept "plugin" and "indent" in any order. */ + // Accept "plugin" and "indent" in any order. for (;; ) { if (STRNCMP(arg, "plugin", 6) == 0) { plugin = true; @@ -9282,8 +9462,9 @@ static void ex_filetype(exarg_T *eap) source_runtime((char_u *)FTOFF_FILE, DIP_ALL); filetype_detect = kFalse; } - } else + } else { EMSG2(_(e_invarg2), arg); + } } /// Set all :filetype options ON if user did not explicitly set any to OFF. @@ -9333,10 +9514,11 @@ static void ex_set(exarg_T *eap) { int flags = 0; - if (eap->cmdidx == CMD_setlocal) + if (eap->cmdidx == CMD_setlocal) { flags = OPT_LOCAL; - else if (eap->cmdidx == CMD_setglobal) + } else if (eap->cmdidx == CMD_setglobal) { flags = OPT_GLOBAL; + } (void)do_set(eap->arg, flags); } @@ -9536,16 +9718,16 @@ bool cmd_can_preview(char_u *cmd) char_u *end = find_command(&ea, NULL); switch (ea.cmdidx) { - case CMD_substitute: - case CMD_smagic: - case CMD_snomagic: - // Only preview once the pattern delimiter has been typed - if (*end && !ASCII_ISALNUM(*end)) { - return true; - } - break; - default: - break; + case CMD_substitute: + case CMD_smagic: + case CMD_snomagic: + // Only preview once the pattern delimiter has been typed + if (*end && !ASCII_ISALNUM(*end)) { + return true; + } + break; + default: + break; } return false; @@ -9576,11 +9758,16 @@ Dictionary commands_array(buf_T *buf) PUT(d, "register", BOOLEAN_OBJ(!!(cmd->uc_argt & EX_REGSTR))); switch (cmd->uc_argt & (EX_EXTRA | EX_NOSPC | EX_NEEDARG)) { - case 0: arg[0] = '0'; break; - case(EX_EXTRA): arg[0] = '*'; break; - case(EX_EXTRA | EX_NOSPC): arg[0] = '?'; break; - case(EX_EXTRA | EX_NEEDARG): arg[0] = '+'; break; - case(EX_EXTRA | EX_NOSPC | EX_NEEDARG): arg[0] = '1'; break; + case 0: + arg[0] = '0'; break; + case (EX_EXTRA): + arg[0] = '*'; break; + case (EX_EXTRA | EX_NOSPC): + arg[0] = '?'; break; + case (EX_EXTRA | EX_NEEDARG): + arg[0] = '+'; break; + case (EX_EXTRA | EX_NOSPC | EX_NEEDARG): + arg[0] = '1'; break; } PUT(d, "nargs", STRING_OBJ(cstr_to_string(arg))); |