From 1e49a1c888a3d9a581f4aa409a26ada3ac2417cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dundar=20G=C3=B6c?= Date: Mon, 6 Sep 2021 10:48:31 +0200 Subject: [PATCH] refactor: format files with uncrustify --- contrib/uncrustify.cfg | 6 +- src/nvim/ex_cmds.c | 1372 +++++++++-------- src/nvim/ex_docmd.c | 3167 +++++++++++++++++++++------------------- src/nvim/normal.c | 2347 ++++++++++++++++------------- src/nvim/ops.c | 1366 +++++++++-------- src/nvim/screen.c | 72 +- src/nvim/window.c | 1449 +++++++++--------- 7 files changed, 5280 insertions(+), 4499 deletions(-) diff --git a/contrib/uncrustify.cfg b/contrib/uncrustify.cfg index 8dbd752d05..d984484391 100644 --- a/contrib/uncrustify.cfg +++ b/contrib/uncrustify.cfg @@ -1638,12 +1638,12 @@ nl_multi_line_define = false # true/false nl_before_case = false # true/false # Whether to add a newline after a 'case' statement. -nl_after_case = false # true/false +nl_after_case = true # true/false # Add or remove newline between a case ':' and '{'. # # Overrides nl_after_case. -nl_case_colon_brace = ignore # ignore/add/remove/force/not_defined +nl_case_colon_brace = remove # ignore/add/remove/force/not_defined # Add or remove newline between ')' and 'throw'. nl_before_throw = ignore # ignore/add/remove/force/not_defined @@ -3279,5 +3279,5 @@ set PREPROC REAL_FATTR_CONST set PREPROC REAL_FATTR_NONNULL_ALL set PREPROC REAL_FATTR_PURE set PREPROC REAL_FATTR_WARN_UNUSED_RESULT -# option(s) with 'not default' value: 56 +# option(s) with 'not default' value: 58 # diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index f7a1327c87..8a93eafc81 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -7,52 +7,55 @@ #include #include -#include -#include -#include #include #include +#include +#include +#include +#include "nvim/api/buffer.h" #include "nvim/api/private/defs.h" #include "nvim/api/vim.h" -#include "nvim/api/buffer.h" -#include "nvim/log.h" -#include "nvim/vim.h" #include "nvim/ascii.h" -#include "nvim/ex_cmds.h" #include "nvim/buffer.h" +#include "nvim/buffer_updates.h" #include "nvim/change.h" #include "nvim/charset.h" #include "nvim/cursor.h" +#include "nvim/decoration.h" #include "nvim/diff.h" #include "nvim/digraph.h" #include "nvim/edit.h" #include "nvim/eval.h" +#include "nvim/ex_cmds.h" #include "nvim/ex_cmds2.h" #include "nvim/ex_docmd.h" #include "nvim/ex_eval.h" #include "nvim/ex_getln.h" +#include "nvim/extmark.h" #include "nvim/fileio.h" #include "nvim/fold.h" +#include "nvim/garray.h" #include "nvim/getchar.h" #include "nvim/highlight.h" #include "nvim/indent.h" -#include "nvim/buffer_updates.h" +#include "nvim/log.h" #include "nvim/main.h" #include "nvim/mark.h" -#include "nvim/extmark.h" -#include "nvim/decoration.h" #include "nvim/mbyte.h" #include "nvim/memline.h" +#include "nvim/memory.h" #include "nvim/message.h" #include "nvim/misc1.h" -#include "nvim/garray.h" -#include "nvim/memory.h" -#include "nvim/move.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/shell.h" +#include "nvim/os/time.h" #include "nvim/os_unix.h" #include "nvim/path.h" #include "nvim/plines.h" @@ -66,11 +69,8 @@ #include "nvim/tag.h" #include "nvim/ui.h" #include "nvim/undo.h" +#include "nvim/vim.h" #include "nvim/window.h" -#include "nvim/os/os.h" -#include "nvim/os/shell.h" -#include "nvim/os/input.h" -#include "nvim/os/time.h" /// Case matching style to use for :substitute @@ -147,17 +147,17 @@ void do_ascii(const exarg_T *const eap) dig = get_digraph_for_char(cval); if (dig != NULL) { - iobuff_len += ( - vim_snprintf((char *)IObuff + iobuff_len, - sizeof(IObuff) - iobuff_len, - _("<%s>%s%s %d, Hex %02x, Oct %03o, Digr %s"), - transchar(c), buf1, buf2, cval, cval, cval, dig)); + iobuff_len += ( + vim_snprintf((char *)IObuff + iobuff_len, + sizeof(IObuff) - iobuff_len, + _("<%s>%s%s %d, Hex %02x, Oct %03o, Digr %s"), + transchar(c), buf1, buf2, cval, cval, cval, dig)); } else { - iobuff_len += ( - vim_snprintf((char *)IObuff + iobuff_len, - sizeof(IObuff) - iobuff_len, - _("<%s>%s%s %d, Hex %02x, Octal %03o"), - transchar(c), buf1, buf2, cval, cval, cval)); + iobuff_len += ( + vim_snprintf((char *)IObuff + iobuff_len, + sizeof(IObuff) - iobuff_len, + _("<%s>%s%s %d, Hex %02x, Octal %03o"), + transchar(c), buf1, buf2, cval, cval, cval)); } c = cc[ci++]; @@ -197,21 +197,21 @@ void do_ascii(const exarg_T *const eap) dig = get_digraph_for_char(c); if (dig != NULL) { - iobuff_len += ( - vim_snprintf((char *)IObuff + iobuff_len, - sizeof(IObuff) - iobuff_len, - (c < 0x10000 + iobuff_len += ( + vim_snprintf((char *)IObuff + iobuff_len, + sizeof(IObuff) - iobuff_len, + (c < 0x10000 ? _("> %d, Hex %04x, Oct %o, Digr %s") : _("> %d, Hex %08x, Oct %o, Digr %s")), - c, c, c, dig)); + c, c, c, dig)); } else { - iobuff_len += ( - vim_snprintf((char *)IObuff + iobuff_len, - sizeof(IObuff) - iobuff_len, - (c < 0x10000 + iobuff_len += ( + vim_snprintf((char *)IObuff + iobuff_len, + sizeof(IObuff) - iobuff_len, + (c < 0x10000 ? _("> %d, Hex %04x, Octal %o") : _("> %d, Hex %08x, Octal %o")), - c, c, c)); + c, c, c)); } if (ci == MAX_MCO) { break; @@ -238,26 +238,29 @@ void ex_align(exarg_T *eap) int width; if (curwin->w_p_rl) { - /* switch left and right aligning */ - if (eap->cmdidx == CMD_right) + // switch left and right aligning + if (eap->cmdidx == CMD_right) { eap->cmdidx = CMD_left; - else if (eap->cmdidx == CMD_left) + } else if (eap->cmdidx == CMD_left) { eap->cmdidx = CMD_right; + } } width = atoi((char *)eap->arg); save_curpos = curwin->w_cursor; - if (eap->cmdidx == CMD_left) { /* width is used for new indent */ - if (width >= 0) + if (eap->cmdidx == CMD_left) { // width is used for new indent + if (width >= 0) { indent = width; + } } else { /* * if 'textwidth' set, use it * else if 'wrapmargin' set, use it * if invalid value, use 80 */ - if (width <= 0) + if (width <= 0) { width = curbuf->b_p_tw; + } if (width == 0 && curbuf->b_p_wm > 0) { width = curwin->w_width_inner - curbuf->b_p_wm; } @@ -266,31 +269,33 @@ void ex_align(exarg_T *eap) } } - if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL) + if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL) { return; + } for (curwin->w_cursor.lnum = eap->line1; curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum) { - if (eap->cmdidx == CMD_left) /* left align */ + if (eap->cmdidx == CMD_left) { // left align new_indent = indent; - else { - has_tab = FALSE; /* avoid uninit warnings */ + } else { + has_tab = FALSE; // avoid uninit warnings len = linelen(eap->cmdidx == CMD_right ? &has_tab - : NULL) - get_indent(); + : NULL) - get_indent(); - if (len <= 0) /* skip blank lines */ + if (len <= 0) { // skip blank lines continue; + } - if (eap->cmdidx == CMD_center) + if (eap->cmdidx == CMD_center) { new_indent = (width - len) / 2; - else { - new_indent = width - len; /* right align */ + } else { + new_indent = width - len; // right align /* * Make sure that embedded TABs don't make the text go too far * to the right. */ - if (has_tab) + if (has_tab) { while (new_indent > 0) { (void)set_indent(new_indent, 0); if (linelen(NULL) <= width) { @@ -306,11 +311,13 @@ void ex_align(exarg_T *eap) } --new_indent; } + } } } - if (new_indent < 0) + if (new_indent < 0) { new_indent = 0; - (void)set_indent(new_indent, 0); /* set indent */ + } + (void)set_indent(new_indent, 0); // set indent } changed_lines(eap->line1, 0, eap->line2 + 1, 0L, true); curwin->w_cursor = save_curpos; @@ -400,11 +407,13 @@ static int sort_compare(const void *s1, const void *s2) /* If the user interrupts, there's no way to stop qsort() immediately, but * if we return 0 every time, qsort will assume it's done sorting and * exit. */ - if (sort_abort) + if (sort_abort) { return 0; + } fast_breakcheck(); - if (got_int) + if (got_int) { sort_abort = TRUE; + } // When sorting numbers "start_col_nr" is the number, not the column // number. @@ -436,9 +445,10 @@ static int sort_compare(const void *s1, const void *s2) result = string_compare(sortbuf1, sortbuf2); } - /* If two lines have the same value, preserve the original line order. */ - if (result == 0) + // If two lines have the same value, preserve the original line order. + if (result == 0) { return (int)(l1.lnum - l2.lnum); + } return result; } @@ -623,10 +633,12 @@ void ex_sort(exarg_T *eap) nrs[lnum - eap->line1].lnum = lnum; - if (regmatch.regprog != NULL) + if (regmatch.regprog != NULL) { fast_breakcheck(); - if (got_int) + } + if (got_int) { goto sortend; + } } // Allocate a buffer that can hold the longest line. @@ -636,8 +648,9 @@ void ex_sort(exarg_T *eap) // Sort the array of line numbers. Note: can't be interrupted! qsort((void *)nrs, count, sizeof(sorti_T), sort_compare); - if (sort_abort) + if (sort_abort) { goto sortend; + } bcount_t old_count = 0, new_count = 0; @@ -665,8 +678,9 @@ void ex_sort(exarg_T *eap) new_count += bytelen; } fast_breakcheck(); - if (got_int) + if (got_int) { goto sortend; + } } // delete the original lines if appending worked @@ -731,11 +745,11 @@ void ex_retab(exarg_T *eap) char_u *new_ts_str; // string value of tab argument int save_list; - linenr_T first_line = 0; /* first changed line */ - linenr_T last_line = 0; /* last changed line */ + linenr_T first_line = 0; // first changed line + linenr_T last_line = 0; // last changed line save_list = curwin->w_p_list; - curwin->w_p_list = 0; /* don't want list mode here */ + curwin->w_p_list = 0; // don't want list mode here new_ts_str = eap->arg; if (!tabstop_set(eap->arg, &new_vts_array)) { @@ -762,7 +776,7 @@ void ex_retab(exarg_T *eap) for (;; ) { if (ascii_iswhite(ptr[col])) { if (!got_tab && num_spaces == 0) { - /* First consecutive white-space */ + // First consecutive white-space start_vcol = vcol; start_col = col; } @@ -773,9 +787,9 @@ void ex_retab(exarg_T *eap) } } else { if (got_tab || (eap->forceit && num_spaces > 1)) { - /* Retabulate this string of white-space */ + // Retabulate this string of white-space - /* len is virtual length of white string */ + // len is virtual length of white string len = num_spaces = vcol - start_vcol; num_tabs = 0; if (!curbuf->b_p_et) { @@ -797,16 +811,17 @@ void ex_retab(exarg_T *eap) } } - /* len is actual number of white characters used */ + // len is actual number of white characters used len = num_spaces + num_tabs; old_len = (long)STRLEN(ptr); long new_len = old_len - col + start_col + len + 1; new_line = xmalloc(new_len); - if (start_col > 0) + if (start_col > 0) { memmove(new_line, ptr, (size_t)start_col); + } memmove(new_line + start_col + len, - ptr + col, (size_t)(old_len - col + 1)); + ptr + col, (size_t)(old_len - col + 1)); ptr = new_line + start_col; for (col = 0; col < len; col++) { ptr[col] = (col < num_tabs) ? '\t' : ' '; @@ -828,17 +843,20 @@ void ex_retab(exarg_T *eap) got_tab = false; num_spaces = 0; } - if (ptr[col] == NUL) + if (ptr[col] == NUL) { break; + } vcol += win_chartabsize(curwin, ptr + col, (colnr_T)vcol); col += utfc_ptr2len(ptr + col); } - if (new_line == NULL) /* out of memory */ + if (new_line == NULL) { // out of memory break; + } line_breakcheck(); } - if (got_int) + if (got_int) { EMSG(_(e_interr)); + } // If a single value was given then it can be considered equal to // either the value of 'tabstop' or the value of 'vartabstop'. @@ -856,7 +874,7 @@ void ex_retab(exarg_T *eap) changed_lines(first_line, 0, last_line + 1, 0L, true); } - curwin->w_p_list = save_list; /* restore 'list' */ + curwin->w_p_list = save_list; // restore 'list' if (new_ts_str != NULL) { // set the new tabstop // If 'vartabstop' is in use or if the value given to retab has more @@ -923,14 +941,16 @@ int do_move(linenr_T line1, linenr_T line2, linenr_T dest) * First we copy the old text to its new location -- webb * Also copy the flag that ":global" command uses. */ - if (u_save(dest, dest + 1) == FAIL) + if (u_save(dest, dest + 1) == FAIL) { return FAIL; + } for (extra = 0, l = line1; l <= line2; l++) { str = vim_strsave(ml_get(l + extra)); ml_append(dest + l - line1, str, (colnr_T)0, false); xfree(str); - if (dest < line1) + if (dest < line1) { extra++; + } } /* @@ -986,17 +1006,19 @@ int do_move(linenr_T line1, linenr_T line2, linenr_T dest) /* * Now we delete the original text -- webb */ - if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL) + if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL) { return FAIL; + } for (l = line1; l <= line2; l++) { ml_delete(line1 + extra, true); } if (!global_busy && num_lines > p_report) { - if (num_lines == 1) + if (num_lines == 1) { MSG(_("1 line moved")); - else + } else { smsg(_("%" PRId64 " lines moved"), (int64_t)num_lines); + } } extmark_move_region(curbuf, line1-1, 0, start_byte, @@ -1007,16 +1029,18 @@ int do_move(linenr_T line1, linenr_T line2, linenr_T dest) /* * Leave the cursor on the last of the moved lines. */ - if (dest >= line1) + if (dest >= line1) { curwin->w_cursor.lnum = dest; - else + } else { curwin->w_cursor.lnum = dest + (line2 - line1) + 1; + } if (line1 < dest) { dest += num_lines + 1; last_line = curbuf->b_ml.ml_line_count; - if (dest > last_line + 1) + if (dest > last_line + 1) { dest = last_line + 1; + } changed_lines(line1, 0, dest, 0L, false); } else { changed_lines(dest + 1, 0, line1 + num_lines, 0L, false); @@ -1052,8 +1076,9 @@ void ex_copy(linenr_T line1, linenr_T line2, linenr_T n) * line1 = start of source (while copying) * line2 = end of source (while copying) */ - if (u_save(n, n + 1) == FAIL) + if (u_save(n, n + 1) == FAIL) { return; + } curwin->w_cursor.lnum = n; while (line1 <= line2) { @@ -1063,14 +1088,17 @@ void ex_copy(linenr_T line1, linenr_T line2, linenr_T n) ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, false); xfree(p); - /* situation 2: skip already copied lines */ - if (line1 == n) + // situation 2: skip already copied lines + if (line1 == n) { line1 = curwin->w_cursor.lnum; + } ++line1; - if (curwin->w_cursor.lnum < line1) + if (curwin->w_cursor.lnum < line1) { ++line1; - if (curwin->w_cursor.lnum < line2) + } + if (curwin->w_cursor.lnum < line2) { ++line2; + } ++curwin->w_cursor.lnum; } @@ -1079,7 +1107,7 @@ void ex_copy(linenr_T line1, linenr_T line2, linenr_T n) msgmore((long)count); } -static char_u *prevcmd = NULL; /* the previous command */ +static char_u *prevcmd = NULL; // the previous command #if defined(EXITFREE) void free_prev_shellcmd(void) @@ -1090,12 +1118,11 @@ void free_prev_shellcmd(void) #endif /* - * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd" + * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd" * Bangs in the argument are replaced with the previously entered command. * Remember the argument. */ -void do_bang(int addr_count, exarg_T *eap, bool forceit, - bool do_in, bool do_out) +void do_bang(int addr_count, exarg_T *eap, bool forceit, bool do_in, bool do_out) FUNC_ATTR_NONNULL_ALL { char_u *arg = eap->arg; // command @@ -1117,8 +1144,8 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, return; } - if (addr_count == 0) { /* :! */ - msg_scroll = FALSE; /* don't scroll here */ + if (addr_count == 0) { // :! + msg_scroll = FALSE; // don't scroll here autowrite_all(); msg_scroll = scroll_save; } @@ -1131,8 +1158,9 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, trailarg = arg; do { len = (int)STRLEN(trailarg) + 1; - if (newcmd != NULL) + if (newcmd != NULL) { len += (int)STRLEN(newcmd); + } if (ins_prevcmd) { if (prevcmd == NULL) { EMSG(_(e_noprev)); @@ -1143,10 +1171,12 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, } t = xmalloc(len); *t = NUL; - if (newcmd != NULL) + if (newcmd != NULL) { STRCAT(t, newcmd); - if (ins_prevcmd) + } + if (ins_prevcmd) { STRCAT(t, prevcmd); + } p = t + STRLEN(t); STRCAT(t, trailarg); xfree(newcmd); @@ -1159,9 +1189,9 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, trailarg = NULL; while (*p) { if (*p == '!') { - if (p > newcmd && p[-1] == '\\') + if (p > newcmd && p[-1] == '\\') { STRMOVE(p - 1, p); - else { + } else { trailarg = p; *trailarg++ = NUL; ins_prevcmd = true; @@ -1175,7 +1205,7 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, xfree(prevcmd); prevcmd = newcmd; - if (bangredo) { /* put cmd in redo buffer for ! command */ + if (bangredo) { // put cmd in redo buffer for ! command /* If % or # appears in the command, it must have been escaped. * Reescape them, so that redoing them does not substitute them by the * buffername. */ @@ -1196,8 +1226,8 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, STRCAT(newcmd, p_shq); free_newcmd = true; } - if (addr_count == 0) { /* :! */ - /* echo the command */ + if (addr_count == 0) { // :! + // echo the command msg_start(); msg_putchar(':'); msg_putchar('!'); @@ -1206,36 +1236,34 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, ui_cursor_goto(msg_row, msg_col); do_shell(newcmd, 0); - } else { /* :range! */ + } else { // :range! /* Careful: This may recursively call do_bang() again! (because of * autocommands) */ do_filter(line1, line2, eap, newcmd, do_in, do_out); apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf); } - if (free_newcmd) + if (free_newcmd) { xfree(newcmd); + } } -// do_filter: filter lines through a command given by the user -// -// We mostly use temp files and the call_shell() routine here. This would -// normally be done using pipes on a Unix system, but this is more portable -// to non-Unix systems. The call_shell() routine needs to be able -// to deal with redirection somehow, and should handle things like looking -// at the PATH env. variable, and adding reasonable extensions to the -// command name given by the user. All reasonable versions of call_shell() -// do this. -// Alternatively, if on Unix and redirecting input or output, but not both, -// and the 'shelltemp' option isn't set, use pipes. -// We use input redirection if do_in is true. -// We use output redirection if do_out is true. -static void do_filter( - linenr_T line1, - linenr_T line2, - exarg_T *eap, /* for forced 'ff' and 'fenc' */ - char_u *cmd, - bool do_in, - bool do_out) +/// do_filter: filter lines through a command given by the user +/// +/// We mostly use temp files and the call_shell() routine here. This would +/// normally be done using pipes on a Unix system, but this is more portable +/// to non-Unix systems. The call_shell() routine needs to be able +/// to deal with redirection somehow, and should handle things like looking +/// at the PATH env. variable, and adding reasonable extensions to the +/// command name given by the user. All reasonable versions of call_shell() +/// do this. +/// Alternatively, if on Unix and redirecting input or output, but not both, +/// and the 'shelltemp' option isn't set, use pipes. +/// We use input redirection if do_in is true. +/// We use output redirection if do_out is true. +/// +/// @param eap for forced 'ff' and 'fenc' +static void do_filter(linenr_T line1, linenr_T line2, exarg_T *eap, char_u *cmd, bool do_in, + bool do_out) { char_u *itmp = NULL; char_u *otmp = NULL; @@ -1247,8 +1275,9 @@ static void do_filter( int shell_flags = 0; const int stmp = p_stmp; - if (*cmd == NUL) /* no filter command */ + if (*cmd == NUL) { // no filter command return; + } cursor_save = curwin->w_cursor; @@ -1271,8 +1300,9 @@ static void do_filter( * pipe only need to do 3. */ - if (do_out) + if (do_out) { shell_flags |= kShellOptDoOut; + } if (!do_in && do_out && !stmp) { // Use a pipe to fetch stdout of the command, do not use a temp file. @@ -1291,7 +1321,7 @@ static void do_filter( curbuf->b_op_end.lnum = line2; curwin->w_cursor.lnum = line2; } else if ((do_in && (itmp = vim_tempname()) == NULL) - || (do_out && (otmp = vim_tempname()) == NULL)) { + || (do_out && (otmp = vim_tempname()) == NULL)) { EMSG(_(e_notmp)); goto filterend; } @@ -1300,7 +1330,7 @@ static void do_filter( * The writing and reading of temp files will not be shown. * Vi also doesn't do this and the messages are not very informative. */ - ++no_wait_return; /* don't call wait_return() while busy */ + ++no_wait_return; // don't call wait_return() while busy if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap, false, false, false, true) == FAIL) { msg_putchar('\n'); // Keep message from buf_write(). @@ -1310,13 +1340,15 @@ static void do_filter( } goto filterend; } - if (curbuf != old_curbuf) + if (curbuf != old_curbuf) { goto filterend; + } - if (!do_out) + if (!do_out) { msg_putchar('\n'); + } - /* Create the shell command in allocated memory. */ + // Create the shell command in allocated memory. cmd_buf = make_filter_cmd(cmd, itmp, otmp); ui_cursor_goto(Rows - 1, 0); @@ -1352,8 +1384,9 @@ static void do_filter( } goto error; } - if (curbuf != old_curbuf) + if (curbuf != old_curbuf) { goto filterend; + } } read_linecount = curbuf->b_ml.ml_line_count - read_linecount; @@ -1401,22 +1434,24 @@ static void do_filter( curwin->w_cursor.lnum = curbuf->b_op_end.lnum; } - beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */ + beginline(BL_WHITE | BL_FIX); // cursor on first non-blank --no_wait_return; if (linecount > p_report) { if (do_in) { vim_snprintf((char *)msg_buf, sizeof(msg_buf), - _("%" PRId64 " lines filtered"), (int64_t)linecount); - if (msg(msg_buf) && !msg_scroll) - /* save message to display it after redraw */ + _("%" PRId64 " lines filtered"), (int64_t)linecount); + if (msg(msg_buf) && !msg_scroll) { + // save message to display it after redraw set_keep_msg(msg_buf, 0); - } else + } + } else { msgmore((long)linecount); + } } } else { error: - /* put cursor back in same position for ":w !cmd" */ + // put cursor back in same position for ":w !cmd" curwin->w_cursor = cursor_save; --no_wait_return; wait_return(FALSE); @@ -1428,21 +1463,21 @@ filterend: --no_wait_return; EMSG(_("E135: *Filter* Autocommands must not change current buffer")); } - if (itmp != NULL) + if (itmp != NULL) { os_remove((char *)itmp); - if (otmp != NULL) + } + if (otmp != NULL) { os_remove((char *)otmp); + } xfree(itmp); xfree(otmp); } -// Call a shell to execute a command. -// When "cmd" is NULL start an interactive shell. -void -do_shell( - char_u *cmd, - int flags // may be SHELL_DOOUT when output is redirected -) +/// Call a shell to execute a command. +/// When "cmd" is NULL start an interactive shell. +/// +/// @param flags may be SHELL_DOOUT when output is redirected +void do_shell(char_u *cmd, int flags) { // Disallow shell commands from .exrc and .vimrc in current directory for // security reasons. @@ -1524,11 +1559,11 @@ char_u *make_filter_cmd(char_u *cmd, char_u *itmp, char_u *otmp) size_t len = STRLEN(cmd) + 1; // At least enough space for cmd + NULL. - len += is_fish_shell ? sizeof("begin; ""; end") - 1 - : sizeof("("")") - 1; + len += is_fish_shell ? sizeof("begin; " "; end") - 1 + : sizeof("(" ")") - 1; if (itmp != NULL) { - len += STRLEN(itmp) + sizeof(" { "" < "" } ") - 1; + len += STRLEN(itmp) + sizeof(" { " " < " " } ") - 1; } if (otmp != NULL) { len += STRLEN(otmp) + STRLEN(p_srr) + 2; // two extra spaces (" "), @@ -1576,9 +1611,9 @@ char_u *make_filter_cmd(char_u *cmd, char_u *itmp, char_u *otmp) } #endif if (otmp != NULL) { - append_redir(buf, len, (char *) p_srr, (char *) otmp); + append_redir(buf, len, (char *)p_srr, (char *)otmp); } - return (char_u *) buf; + return (char_u *)buf; } /// Append output redirection for the given file to the end of the buffer @@ -1590,8 +1625,8 @@ char_u *make_filter_cmd(char_u *cmd, char_u *itmp, char_u *otmp) /// a space, opt, a space and then fname if `%s` is not found /// there. /// @param[in] fname File name to append. -void append_redir(char *const buf, const size_t buflen, - const char *const opt, const char *const fname) +void append_redir(char *const buf, const size_t buflen, const char *const opt, + const char *const fname) { char *const end = buf + strlen(buf); // find "%s" @@ -1605,9 +1640,9 @@ void append_redir(char *const buf, const size_t buflen, } if (p != NULL) { *end = ' '; // not really needed? Not with sh, ksh or bash - vim_snprintf(end + 1, (size_t) (buflen - (end + 1 - buf)), opt, fname); + vim_snprintf(end + 1, (size_t)(buflen - (end + 1 - buf)), opt, fname); } else { - vim_snprintf(end, (size_t) (buflen - (end - buf)), " %s %s", opt, fname); + vim_snprintf(end, (size_t)(buflen - (end - buf)), " %s %s", opt, fname); } } @@ -1654,11 +1689,13 @@ int rename_buffer(char_u *new_fname) buf = curbuf; apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf); - /* buffer changed, don't change name now */ - if (buf != curbuf) + // buffer changed, don't change name now + if (buf != curbuf) { return FAIL; - if (aborting()) /* autocmds may abort script processing */ + } + if (aborting()) { // autocmds may abort script processing return FAIL; + } /* * The name of the current buffer will be changed. * A new (unlisted) buffer entry needs to be made to hold the old file @@ -1686,7 +1723,7 @@ int rename_buffer(char_u *new_fname) xfree(fname); xfree(sfname); apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf); - /* Change directories when the 'acd' option is set. */ + // Change directories when the 'acd' option is set. do_autochdir(); return OK; } @@ -1724,8 +1761,9 @@ void ex_file(exarg_T *eap) */ void ex_update(exarg_T *eap) { - if (curbufIsChanged()) + if (curbufIsChanged()) { (void)do_write(eap); + } } /* @@ -1757,15 +1795,16 @@ void ex_write(exarg_T *eap) int do_write(exarg_T *eap) { int other; - char_u *fname = NULL; /* init to shut up gcc */ + char_u *fname = NULL; // init to shut up gcc char_u *ffname; int retval = FAIL; char_u *free_fname = NULL; buf_T *alt_buf = NULL; int name_was_missing; - if (not_writing()) /* check 'write' option */ + if (not_writing()) { // check 'write' option return FAIL; + } ffname = eap->arg; if (*ffname == NUL) { @@ -1781,8 +1820,9 @@ int do_write(exarg_T *eap) * When out-of-memory, keep unexpanded file name, because we MUST be * able to write the file in this situation. */ - if (free_fname != NULL) + if (free_fname != NULL) { ffname = free_fname; + } other = otherfile(ffname); } @@ -1791,10 +1831,11 @@ int do_write(exarg_T *eap) */ if (other) { if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL - || eap->cmdidx == CMD_saveas) + || eap->cmdidx == CMD_saveas) { alt_buf = setaltfname(ffname, fname, (linenr_T)1); - else + } else { alt_buf = buflist_findname(ffname); + } if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL) { /* Overwriting a file that is loaded in another buffer is not a * good idea. */ @@ -1825,8 +1866,9 @@ int do_write(exarg_T *eap) && !p_wa) { if (p_confirm || cmdmod.confirm) { if (vim_dialog_yesno(VIM_QUESTION, NULL, - (char_u *)_("Write partial file?"), 2) != VIM_YES) + (char_u *)_("Write partial file?"), 2) != VIM_YES) { goto theend; + } eap->forceit = TRUE; } else { EMSG(_("E140: Use ! to write partial buffer")); @@ -1842,7 +1884,7 @@ int do_write(exarg_T *eap) apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf); apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf); if (curbuf != was_curbuf || aborting()) { - /* buffer changed, don't change name now */ + // buffer changed, don't change name now retval = FAIL; goto theend; } @@ -1868,7 +1910,7 @@ int do_write(exarg_T *eap) apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf); } if (curbuf != was_curbuf || aborting()) { - /* buffer changed, don't write the file */ + // buffer changed, don't write the file retval = FAIL; goto theend; } @@ -1888,9 +1930,9 @@ int do_write(exarg_T *eap) name_was_missing = curbuf->b_ffname == NULL; retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2, - eap, eap->append, eap->forceit, TRUE, FALSE); + eap, eap->append, eap->forceit, TRUE, FALSE); - /* After ":saveas fname" reset 'readonly'. */ + // After ":saveas fname" reset 'readonly'. if (eap->cmdidx == CMD_saveas) { if (retval == OK) { curbuf->b_p_ro = FALSE; @@ -1910,21 +1952,16 @@ theend: return retval; } -/* - * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED, - * BF_NEW or BF_READERR, check for overwriting current file. - * May set eap->forceit if a dialog says it's OK to overwrite. - * Return OK if it's OK, FAIL if it is not. - */ -int -check_overwrite( - exarg_T *eap, - buf_T *buf, - char_u *fname, // file name to be used (can differ from - // buf->ffname) - char_u *ffname, // full path version of fname - int other // writing under other name -) +/// Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED, +/// BF_NEW or BF_READERR, check for overwriting current file. +/// May set eap->forceit if a dialog says it's OK to overwrite. +/// +/// @param fname file name to be used (can differ from buf->ffname) +/// @param ffname full path version of fname +/// @param other writing under other name +/// +/// @return OK if it's OK, FAIL if it is not. +int check_overwrite(exarg_T *eap, buf_T *buf, char_u *fname, char_u *ffname, int other) { /* * write to other file or b_flags set or not writing the whole file: @@ -1950,8 +1987,9 @@ check_overwrite( char_u buff[DIALOG_MSG_SIZE]; dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname); - if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES) + if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES) { return FAIL; + } eap->forceit = TRUE; } else { EMSG(_(e_exists)); @@ -1959,7 +1997,7 @@ check_overwrite( } } - /* For ":w! filename" check that no swap file exists for "filename". */ + // For ":w! filename" check that no swap file exists for "filename". if (other && !emsg_silent) { char_u *dir; char_u *p; @@ -1985,8 +2023,8 @@ check_overwrite( char_u buff[DIALOG_MSG_SIZE]; dialog_msg(buff, - _("Swap file \"%s\" exists, overwrite anyway?"), - swapname); + _("Swap file \"%s\" exists, overwrite anyway?"), + swapname); if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES) { xfree(swapname); @@ -1995,7 +2033,7 @@ check_overwrite( eap->forceit = TRUE; } else { EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"), - swapname); + swapname); xfree(swapname); return FAIL; } @@ -2013,14 +2051,16 @@ void ex_wnext(exarg_T *eap) { int i; - if (eap->cmd[1] == 'n') + if (eap->cmd[1] == 'n') { i = curwin->w_arg_idx + (int)eap->line2; - else + } else { i = curwin->w_arg_idx - (int)eap->line2; + } eap->line1 = 1; eap->line2 = curbuf->b_ml.ml_line_count; - if (do_write(eap) != FAIL) + if (do_write(eap) != FAIL) { do_argfile(eap, i); + } } /* @@ -2060,7 +2100,7 @@ void do_wqall(exarg_T *eap) ++error; } else if (check_readonly(&eap->forceit, buf) || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname, - FALSE) == FAIL) { + FALSE) == FAIL) { ++error; } else { bufref_T bufref; @@ -2073,11 +2113,12 @@ void do_wqall(exarg_T *eap) buf = firstbuf; } } - eap->forceit = save_forceit; /* check_overwrite() may set it */ + eap->forceit = save_forceit; // check_overwrite() may set it } if (exiting) { - if (!error) - getout(0); /* exit Vim */ + if (!error) { + getout(0); // exit Vim + } not_exiting(); } } @@ -2088,8 +2129,9 @@ void do_wqall(exarg_T *eap) */ int not_writing(void) { - if (p_write) + if (p_write) { return FALSE; + } EMSG(_("E142: File not written: Writing is disabled by 'write' option")); return TRUE; } @@ -2109,28 +2151,30 @@ static int check_readonly(int *forceit, buf_T *buf) if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL) { char_u buff[DIALOG_MSG_SIZE]; - if (buf->b_p_ro) + if (buf->b_p_ro) { dialog_msg(buff, - _( - "'readonly' option is set for \"%s\".\nDo you wish to write anyway?"), - buf->b_fname); - else + _( "'readonly' option is set for \"%s\".\nDo you wish to write anyway?"), + buf->b_fname); + } else { dialog_msg(buff, - _( - "File permissions of \"%s\" are read-only.\nIt may still be possible to write it.\nDo you wish to try?"), - buf->b_fname); + _( + "File permissions of \"%s\" are read-only.\nIt may still be possible to write it.\nDo you wish to try?"), + buf->b_fname); + } if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES) { - /* Set forceit, to force the writing of a readonly file */ + // Set forceit, to force the writing of a readonly file *forceit = TRUE; return FALSE; - } else + } else { return TRUE; - } else if (buf->b_p_ro) + } + } else if (buf->b_p_ro) { EMSG(_(e_readonly)); - else + } else { EMSG2(_("E505: \"%s\" is read-only (add ! to override)"), - buf->b_fname); + buf->b_fname); + } return TRUE; } @@ -2146,8 +2190,7 @@ static int check_readonly(int *forceit, buf_T *buf) // GETFILE_NOT_WRITTEN for "not written" error, // GETFILE_SAME_FILE for success // GETFILE_OPEN_OTHER for successfully opening another file. -int getfile(int fnum, char_u *ffname_arg, char_u *sfname_arg, int setpm, - linenr_T lnum, int forceit) +int getfile(int fnum, char_u *ffname_arg, char_u *sfname_arg, int setpm, linenr_T lnum, int forceit) { char_u *ffname = ffname_arg; char_u *sfname = sfname_arg; @@ -2163,12 +2206,13 @@ int getfile(int fnum, char_u *ffname_arg, char_u *sfname_arg, int setpm, } if (fnum == 0) { - /* make ffname full path, set sfname */ + // make ffname full path, set sfname fname_expand(curbuf, &ffname, &sfname); other = otherfile(ffname); - free_me = ffname; /* has been allocated, free() later */ - } else + free_me = ffname; // has been allocated, free() later + } else { other = (fnum != curbuf->b_fnum); + } if (other) { no_wait_return++; // don't wait for autowrite message @@ -2185,10 +2229,12 @@ int getfile(int fnum, char_u *ffname_arg, char_u *sfname_arg, int setpm, goto theend; } } - if (other) + if (other) { --no_wait_return; - if (setpm) + } + if (setpm) { setpcmark(); + } if (!other) { if (lnum != 0) { curwin->w_cursor.lnum = lnum; @@ -2220,7 +2266,7 @@ theend: /// - NULL to start an empty buffer /// @param sfname the short file name (or NULL) /// @param eap contains the command to be executed after loading the file -/// and forced 'ff' and 'fenc' +/// and forced 'ff' and 'fenc'. Can be NULL! /// @param newlnum if > 0: put cursor on this line number (if possible) /// ECMD_LASTL: use last position in loaded file /// ECMD_LAST: use last position in all files @@ -2238,18 +2284,11 @@ theend: /// info of the previous buffer for "oldwin" is stored. /// /// @return FAIL for failure, OK otherwise -int do_ecmd( - int fnum, - char_u *ffname, - char_u *sfname, - exarg_T *eap, /* can be NULL! */ - linenr_T newlnum, - int flags, - win_T *oldwin -) +int do_ecmd(int fnum, char_u *ffname, char_u *sfname, exarg_T *eap, linenr_T newlnum, int flags, + win_T *oldwin) { - int other_file; /* TRUE if editing another file */ - int oldbuf; /* TRUE if using existing buffer */ + int other_file; // TRUE if editing another file + int oldbuf; // TRUE if using existing buffer int auto_buf = FALSE; /* TRUE if autocommands brought us into the buffer unexpectedly */ char_u *new_name = NULL; @@ -2271,22 +2310,26 @@ int do_ecmd( bool did_inc_redrawing_disabled = false; long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so; - if (eap != NULL) + if (eap != NULL) { command = eap->do_ecmd_cmd; + } set_bufref(&old_curbuf, curbuf); if (fnum != 0) { - if (fnum == curbuf->b_fnum) /* file is already being edited */ - return OK; /* nothing to do */ + if (fnum == curbuf->b_fnum) { // file is already being edited + return OK; // nothing to do + } other_file = TRUE; } else { - /* if no short name given, use ffname for short name */ - if (sfname == NULL) + // if no short name given, use ffname for short name + if (sfname == NULL) { sfname = ffname; + } #ifdef USE_FNAME_CASE - if (sfname != NULL) + if (sfname != NULL) { path_fix_case(sfname); // set correct case for sfname + } #endif if ((flags & (ECMD_ADDBUF | ECMD_ALTBUF)) @@ -2294,19 +2337,21 @@ int do_ecmd( goto theend; } - if (ffname == NULL) + if (ffname == NULL) { other_file = TRUE; - /* there is no file name */ - else if (*ffname == NUL && curbuf->b_ffname == NULL) + } + // there is no file name + else if (*ffname == NUL && curbuf->b_ffname == NULL) { other_file = FALSE; - else { - if (*ffname == NUL) { /* re-edit with same file name */ + } else { + if (*ffname == NUL) { // re-edit with same file name ffname = curbuf->b_ffname; sfname = curbuf->b_fname; } - free_fname = (char_u *)fix_fname((char *)ffname); /* may expand to full path name */ - if (free_fname != NULL) + free_fname = (char_u *)fix_fname((char *)ffname); // may expand to full path name + if (free_fname != NULL) { ffname = free_fname; + } other_file = otherfile(ffname); } } @@ -2380,13 +2425,14 @@ int do_ecmd( if (command != NULL) { tlnum = atol((char *)command); - if (tlnum <= 0) + if (tlnum <= 0) { tlnum = 1L; + } } // Add BLN_NOCURWIN to avoid a new wininfo items are associated // with the current window. const buf_T *const newbuf - = buflist_new(ffname, sfname, tlnum, BLN_LISTED | BLN_NOCURWIN); + = buflist_new(ffname, sfname, tlnum, BLN_LISTED | BLN_NOCURWIN); if (newbuf != NULL && (flags & ECMD_ALTBUF)) { curwin->w_alt_fnum = newbuf->b_fnum; } @@ -2400,8 +2446,9 @@ int do_ecmd( } set_bufref(&old_curbuf, curbuf); } - if (buf == NULL) + if (buf == NULL) { goto theend; + } if (buf->b_ml.ml_mfp == NULL) { // No memfile yet. oldbuf = false; @@ -2460,7 +2507,7 @@ int do_ecmd( delbuf_msg(new_name); // Frees new_name. goto theend; } - if (aborting()) { /* autocmds may abort script processing */ + if (aborting()) { // autocmds may abort script processing xfree(new_name); goto theend; } @@ -2482,10 +2529,9 @@ int do_ecmd( // Close the link to the current buffer. This will set // oldwin->w_buffer to NULL. u_sync(false); - const bool did_decrement = close_buffer( - oldwin, curbuf, - (flags & ECMD_HIDE) || curbuf->terminal ? 0 : DOBUF_UNLOAD, - false); + const bool did_decrement = close_buffer(oldwin, curbuf, + (flags & ECMD_HIDE) || curbuf->terminal ? 0 : DOBUF_UNLOAD, + false); // Autocommands may have closed the window. if (win_valid(the_curwin)) { @@ -2526,7 +2572,7 @@ int do_ecmd( curbuf = buf; ++curbuf->b_nwindows; - /* Set 'fileformat', 'binary' and 'fenc' when forced. */ + // Set 'fileformat', 'binary' and 'fenc' when forced. if (!oldbuf && eap != NULL) { set_file_options(TRUE, eap); set_forced_fenc(eap); @@ -2539,7 +2585,6 @@ int do_ecmd( * values. Also restores old folding stuff. */ get_winopts(curbuf); did_get_winopts = TRUE; - } xfree(new_name); au_new_curbuf.br_buf = NULL; @@ -2571,10 +2616,12 @@ int do_ecmd( /* If autocommands change buffers under our fingers, forget about * editing the file. */ - if (buf != curbuf) + if (buf != curbuf) { goto theend; - if (aborting()) /* autocmds may abort script processing */ + } + if (aborting()) { // autocmds may abort script processing goto theend; + } /* Since we are starting to edit a file, consider the filetype to be * unset. Helps for when an autocommand changes files and expects syntax @@ -2582,14 +2629,14 @@ int do_ecmd( did_filetype = FALSE; /* - * other_file oldbuf - * FALSE FALSE re-edit same file, buffer is re-used - * FALSE TRUE re-edit same file, nothing changes - * TRUE FALSE start editing new file, new buffer - * TRUE TRUE start editing in existing buffer (nothing to do) + * other_file oldbuf + * FALSE FALSE re-edit same file, buffer is re-used + * FALSE TRUE re-edit same file, nothing changes + * TRUE FALSE start editing new file, new buffer + * TRUE TRUE start editing in existing buffer (nothing to do) */ - if (!other_file && !oldbuf) { /* re-use the buffer */ - set_last_cursor(curwin); /* may set b_last_cursor */ + if (!other_file && !oldbuf) { // re-use the buffer + set_last_cursor(curwin); // may set b_last_cursor if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL) { newlnum = curwin->w_cursor.lnum; solcol = curwin->w_cursor.col; @@ -2635,12 +2682,14 @@ int do_ecmd( /* If autocommands change buffers under our fingers, forget about * re-editing the file. Should do the buf_clear_file(), but perhaps * the autocommands changed the buffer... */ - if (buf != curbuf) + if (buf != curbuf) { goto theend; - if (aborting()) /* autocmds may abort script processing */ + } + if (aborting()) { // autocmds may abort script processing goto theend; + } buf_clear_file(curbuf); - curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */ + curbuf->b_op_start.lnum = 0; // clear '[ and '] marks curbuf->b_op_end.lnum = 0; } @@ -2648,7 +2697,7 @@ int do_ecmd( * If we get here we are sure to start editing */ - /* Assume success now */ + // Assume success now retval = OK; /* @@ -2672,7 +2721,7 @@ int do_ecmd( } } - /* Change directories when the 'acd' option is set. */ + // Change directories when the 'acd' option is set. do_autochdir(); /* @@ -2681,18 +2730,20 @@ int do_ecmd( */ orig_pos = curwin->w_cursor; topline = curwin->w_topline; - if (!oldbuf) { /* need to read the file */ + if (!oldbuf) { // need to read the file swap_exists_action = SEA_DIALOG; - curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */ + curbuf->b_flags |= BF_CHECK_RO; // set/reset 'ro' flag /* * Open the buffer and read the file. */ - if (should_abort(open_buffer(FALSE, eap, readfile_flags))) + if (should_abort(open_buffer(FALSE, eap, readfile_flags))) { retval = FAIL; + } - if (swap_exists_action == SEA_QUIT) + if (swap_exists_action == SEA_QUIT) { retval = FAIL; + } handle_swap_exists(&old_curbuf); } else { /* Read the modelines, but only to set window-local options. Any @@ -2701,9 +2752,9 @@ int do_ecmd( do_modelines(OPT_WINONLY); apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, - &retval); + &retval); apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf, - &retval); + &retval); } check_arg_idx(curwin); @@ -2719,10 +2770,11 @@ int do_ecmd( newcol = curwin->w_cursor.col; } } - if (curwin->w_topline == topline) + if (curwin->w_topline == topline) { topline = 0; + } - /* Even when cursor didn't move we need to recompute topline. */ + // Even when cursor didn't move we need to recompute topline. changed_line_abv_curs(); maketitle(); @@ -2738,28 +2790,31 @@ int do_ecmd( /* If the window options were changed may need to set the spell language. * Can only do this after the buffer has been properly setup. */ - if (did_get_winopts && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL) + if (did_get_winopts && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL) { (void)did_set_spelllang(curwin); + } if (command == NULL) { - if (newcol >= 0) { /* position set by autocommands */ + if (newcol >= 0) { // position set by autocommands curwin->w_cursor.lnum = newlnum; curwin->w_cursor.col = newcol; check_cursor(); - } else if (newlnum > 0) { /* line number from caller or old position */ + } else if (newlnum > 0) { // line number from caller or old position curwin->w_cursor.lnum = newlnum; check_cursor_lnum(); if (solcol >= 0 && !p_sol) { - /* 'sol' is off: Use last known column. */ + // 'sol' is off: Use last known column. curwin->w_cursor.col = solcol; check_cursor_col(); curwin->w_cursor.coladd = 0; curwin->w_set_curswant = TRUE; - } else + } else { beginline(BL_SOL | BL_FIX); - } else { /* no line number, go to last line in Ex mode */ - if (exmode_active) + } + } else { // no line number, go to last line in Ex mode + if (exmode_active) { curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; + } beginline(BL_WHITE | BL_FIX); } } @@ -2772,16 +2827,17 @@ int do_ecmd( * Do this after setting the cursor. */ if (oldbuf - && !auto_buf - ) { + && !auto_buf) { int msg_scroll_save = msg_scroll; /* Obey the 'O' flag in 'cpoptions': overwrite any previous file * message. */ - if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) + if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) { msg_scroll = FALSE; - if (!msg_scroll) /* wait a bit when overwriting an error msg */ + } + if (!msg_scroll) { // wait a bit when overwriting an error msg check_for_delay(FALSE); + } msg_start(); msg_scroll = msg_scroll_save; msg_scrolled_ign = TRUE; @@ -2795,11 +2851,13 @@ int do_ecmd( curbuf->b_last_used = time(NULL); - if (command != NULL) + if (command != NULL) { do_cmdline(command, NULL, NULL, DOCMD_VERBOSE); + } - if (curbuf->b_kmap_state & KEYMAP_INIT) + if (curbuf->b_kmap_state & KEYMAP_INIT) { (void)keymap_init(); + } RedrawingDisabled--; did_inc_redrawing_disabled = false; @@ -2818,7 +2876,7 @@ int do_ecmd( need_start_insertmode = true; } - /* Change directories when the 'acd' option is set. */ + // Change directories when the 'acd' option is set. do_autochdir(); @@ -2840,13 +2898,13 @@ theend: static void delbuf_msg(char_u *name) { EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"), - name == NULL ? (char_u *)"" : name); + name == NULL ? (char_u *)"" : name); xfree(name); au_new_curbuf.br_buf = NULL; au_new_curbuf.br_buf_free_count = 0; } -static int append_indent = 0; /* autoindent for first line */ +static int append_indent = 0; // autoindent for first line /* * ":insert" and ":append", also used by ":change" @@ -2861,24 +2919,29 @@ void ex_append(exarg_T *eap) int vcol; int empty = (curbuf->b_ml.ml_flags & ML_EMPTY); - /* the ! flag toggles autoindent */ - if (eap->forceit) + // the ! flag toggles autoindent + if (eap->forceit) { curbuf->b_p_ai = !curbuf->b_p_ai; + } - /* First autoindent comes from the line we start on */ - if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0) + // First autoindent comes from the line we start on + if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0) { append_indent = get_indent_lnum(lnum); + } - if (eap->cmdidx != CMD_append) + if (eap->cmdidx != CMD_append) { --lnum; + } // when the buffer is empty need to delete the dummy line - if (empty && lnum == 1) + if (empty && lnum == 1) { lnum = 0; + } - State = INSERT; /* behave like in Insert mode */ - if (curbuf->b_p_iminsert == B_IMODE_LMAP) + State = INSERT; // behave like in Insert mode + if (curbuf->b_p_iminsert == B_IMODE_LMAP) { State |= LANGMAP; + } for (;; ) { msg_scroll = TRUE; @@ -2887,17 +2950,20 @@ void ex_append(exarg_T *eap) if (append_indent >= 0) { indent = append_indent; append_indent = -1; - } else if (lnum > 0) + } else if (lnum > 0) { indent = get_indent_lnum(lnum); + } } if (eap->getline == NULL) { /* No getline() function, use the lines that follow. This ends * when there is no more. */ - if (eap->nextcmd == NULL || *eap->nextcmd == NUL) + if (eap->nextcmd == NULL || *eap->nextcmd == NUL) { break; + } p = vim_strchr(eap->nextcmd, NL); - if (p == NULL) + if (p == NULL) { p = eap->nextcmd + STRLEN(eap->nextcmd); + } theline = vim_strnsave(eap->nextcmd, p - eap->nextcmd); if (*p != NUL) { p++; @@ -2908,24 +2974,25 @@ void ex_append(exarg_T *eap) // when getline() returns. int save_State = State; State = CMDLINE; - theline = eap->getline( - eap->cstack->cs_looplevel > 0 ? -1 : - NUL, eap->cookie, indent, true); + theline = eap->getline(eap->cstack->cs_looplevel > 0 ? -1 : + NUL, eap->cookie, indent, true); State = save_State; } lines_left = Rows - 1; - if (theline == NULL) + if (theline == NULL) { break; + } - /* Look for the "." after automatic indent. */ + // Look for the "." after automatic indent. vcol = 0; for (p = theline; indent > vcol; ++p) { - if (*p == ' ') + if (*p == ' ') { ++vcol; - else if (*p == TAB) + } else if (*p == TAB) { vcol += 8 - vcol % 8; - else + } else { break; + } } if ((p[0] == '.' && p[1] == NUL) || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0)) @@ -2934,9 +3001,10 @@ void ex_append(exarg_T *eap) break; } - /* don't use autoindent if nothing was typed. */ - if (p[0] == NUL) + // don't use autoindent if nothing was typed. + if (p[0] == NUL) { theline[0] = NUL; + } did_undo = true; ml_append(lnum, theline, (colnr_T)0, false); @@ -2952,8 +3020,9 @@ void ex_append(exarg_T *eap) } State = NORMAL; - if (eap->forceit) + if (eap->forceit) { curbuf->b_p_ai = !curbuf->b_p_ai; + } /* "start" is set to eap->line2+1 unless that position is invalid (when * eap->line2 pointed to the end of the buffer and nothing was appended) @@ -2961,8 +3030,9 @@ void ex_append(exarg_T *eap) * it is the same than "start" -- Acevedo */ curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ? eap->line2 + 1 : curbuf->b_ml.ml_line_count; - if (eap->cmdidx != CMD_append) + if (eap->cmdidx != CMD_append) { --curbuf->b_op_start.lnum; + } curbuf->b_op_end.lnum = (eap->line2 < lnum) ? lnum : curbuf->b_op_start.lnum; curbuf->b_op_start.col = curbuf->b_op_end.col = 0; @@ -2982,24 +3052,27 @@ void ex_change(exarg_T *eap) linenr_T lnum; if (eap->line2 >= eap->line1 - && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL) + && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL) { return; + } - /* the ! flag toggles autoindent */ - if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai) + // the ! flag toggles autoindent + if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai) { append_indent = get_indent_lnum(eap->line1); + } for (lnum = eap->line2; lnum >= eap->line1; --lnum) { - if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */ + if (curbuf->b_ml.ml_flags & ML_EMPTY) { // nothing to delete break; + } ml_delete(eap->line1, false); } - /* make sure the cursor is not beyond the end of the file now */ + // make sure the cursor is not beyond the end of the file now check_cursor_lnum(); deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum)); - /* ":append" on the line above the deleted lines. */ + // ":append" on the line above the deleted lines. eap->line2 = eap->line1; ex_append(eap); } @@ -3030,10 +3103,12 @@ void ex_z(exarg_T *eap) x = eap->arg; kind = x; if (*kind == '-' || *kind == '+' || *kind == '=' - || *kind == '^' || *kind == '.') + || *kind == '^' || *kind == '.') { ++x; - while (*x == '-' || *x == '+') + } + while (*x == '-' || *x == '+') { ++x; + } if (*x != 0) { if (!ascii_isdigit(*x)) { @@ -3053,10 +3128,12 @@ void ex_z(exarg_T *eap) } } - /* the number of '-' and '+' multiplies the distance */ - if (*kind == '-' || *kind == '+') - for (x = kind + 1; *x == *kind; ++x) + // the number of '-' and '+' multiplies the distance + if (*kind == '-' || *kind == '+') { + for (x = kind + 1; *x == *kind; ++x) { ; + } + } switch (*kind) { case '-': @@ -3084,22 +3161,25 @@ void ex_z(exarg_T *eap) curs = end; break; - default: /* '+' */ + default: // '+' start = lnum; - if (*kind == '+') + if (*kind == '+') { start += bigness * (linenr_T)(x - kind - 1) + 1; - else if (eap->addr_count == 0) + } else if (eap->addr_count == 0) { ++start; + } end = start + bigness - 1; curs = end; break; } - if (start < 1) + if (start < 1) { start = 1; + } - if (end > curbuf->b_ml.ml_line_count) + if (end > curbuf->b_ml.ml_line_count) { end = curbuf->b_ml.ml_line_count; + } if (curs > curbuf->b_ml.ml_line_count) { curs = curbuf->b_ml.ml_line_count; @@ -3111,8 +3191,9 @@ void ex_z(exarg_T *eap) if (minus && i == lnum) { msg_putchar('\n'); - for (j = 1; j < Columns; j++) + for (j = 1; j < Columns; j++) { msg_putchar('-'); + } } print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST); @@ -3120,8 +3201,9 @@ void ex_z(exarg_T *eap) if (minus && i == lnum) { msg_putchar('\n'); - for (j = 1; j < Columns; j++) + for (j = 1; j < Columns; j++) { msg_putchar('-'); + } } } @@ -3155,7 +3237,7 @@ int check_secure(void) } /// Previous substitute replacement string -static SubReplacementString old_sub = {NULL, 0, NULL}; +static SubReplacementString old_sub = { NULL, 0, NULL }; static int global_need_beginline; // call beginline() after ":g" @@ -3192,8 +3274,7 @@ void sub_set_replacement(SubReplacementString sub) /// @param[in] save Save pattern to options, history /// /// @returns true if :substitute can be replaced with a join command -static bool sub_joining_lines(exarg_T *eap, char_u *pat, char_u *sub, - char_u *cmd, bool save) +static bool sub_joining_lines(exarg_T *eap, char_u *pat, char_u *sub, char_u *cmd, bool save) FUNC_ATTR_NONNULL_ARG(1, 3, 4) { // TODO(vim): find a generic solution to make line-joining operations more @@ -3220,8 +3301,8 @@ static bool sub_joining_lines(exarg_T *eap, char_u *pat, char_u *sub, // The number of lines joined is the number of lines in the range linenr_T joined_lines_count = eap->line2 - eap->line1 + 1 - // plus one extra line if not at the end of file. - + (eap->line2 < curbuf->b_ml.ml_line_count ? 1 : 0); + // plus one extra line if not at the end of file. + + (eap->line2 < curbuf->b_ml.ml_line_count ? 1 : 0); if (joined_lines_count > 1) { do_join(joined_lines_count, FALSE, TRUE, FALSE, true); sub_nsubs = joined_lines_count - 1; @@ -3288,8 +3369,7 @@ static char_u *sub_grow_buf(char_u **new_start, int needed_len) /// @param[in,out] which_pat pattern type from which to get default search /// /// @returns pointer to the end of the flags, which may be the end of the string -static char_u *sub_parse_flags(char_u *cmd, subflags_T *subflags, - int *which_pat) +static char_u *sub_parse_flags(char_u *cmd, subflags_T *subflags, int *which_pat) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET { // Find trailing options. When '&' is used, keep old options. @@ -3361,8 +3441,7 @@ static int check_regexp_delim(int c) /// /// @param do_buf_event If `true`, send buffer updates. /// @return buffer used for 'inccommand' preview -static buf_T *do_sub(exarg_T *eap, proftime_T timeout, - bool do_buf_event, handle_T bufnr) +static buf_T *do_sub(exarg_T *eap, proftime_T timeout, bool do_buf_event, handle_T bufnr) { long i = 0; regmmatch_T regmatch; @@ -3407,12 +3486,12 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, } start_nsubs = sub_nsubs; - if (eap->cmdidx == CMD_tilde) - which_pat = RE_LAST; /* use last used regexp */ - else - which_pat = RE_SUBST; /* use last substitute regexp */ - - /* new pattern and substitution */ + if (eap->cmdidx == CMD_tilde) { + which_pat = RE_LAST; // use last used regexp + } else { + which_pat = RE_SUBST; // use last substitute regexp + } + // new pattern and substitution if (eap->cmd[0] == 's' && *cmd != NUL && !ascii_iswhite(*cmd) && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL) { // don't accept alphanumeric for separator @@ -3450,11 +3529,11 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, * Small incompatibility: vi sees '\n' as end of the command, but in * Vim we want to use '\n' to find/substitute a NUL. */ - sub = cmd; /* remember the start of the substitution */ + sub = cmd; // remember the start of the substitution while (cmd[0]) { - if (cmd[0] == delimiter) { /* end delimiter found */ - *cmd++ = NUL; /* replace it with a NUL */ + if (cmd[0] == delimiter) { // end delimiter found + *cmd++ = NUL; // replace it with a NUL break; } if (cmd[0] == '\\' && cmd[1] != 0) { // skip escaped characters @@ -3465,18 +3544,18 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, if (!eap->skip && !preview) { sub_set_replacement((SubReplacementString) { - .sub = xstrdup((char *) sub), + .sub = xstrdup((char *)sub), .timestamp = os_time(), .additional_elements = NULL, }); } - } else if (!eap->skip) { /* use previous pattern and substitution */ - if (old_sub.sub == NULL) { /* there is no previous command */ + } else if (!eap->skip) { // use previous pattern and substitution + if (old_sub.sub == NULL) { // there is no previous command EMSG(_(e_nopresub)); return NULL; } - pat = NULL; /* search_regcomp() will use previous pattern */ - sub = (char_u *) old_sub.sub; + pat = NULL; // search_regcomp() will use previous pattern + sub = (char_u *)old_sub.sub; /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the * last column after using "$". */ @@ -3502,15 +3581,16 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, } eap->line1 = eap->line2; eap->line2 += i - 1; - if (eap->line2 > curbuf->b_ml.ml_line_count) + if (eap->line2 > curbuf->b_ml.ml_line_count) { eap->line2 = curbuf->b_ml.ml_line_count; + } } /* * check for trailing command or garbage */ cmd = skipwhite(cmd); - if (*cmd && *cmd != '"') { /* if not end-of-line or comment */ + if (*cmd && *cmd != '"') { // if not end-of-line or comment eap->nextcmd = check_nextcmd(cmd); if (eap->nextcmd == NULL) { EMSG(_(e_trailing)); @@ -3580,29 +3660,29 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, /* * The new text is build up step by step, to avoid too much * copying. There are these pieces: - * sub_firstline The old text, unmodified. - * copycol Column in the old text where we started - * looking for a match; from here old text still - * needs to be copied to the new text. - * matchcol Column number of the old text where to look - * for the next match. It's just after the - * previous match or one further. - * prev_matchcol Column just after the previous match (if any). - * Mostly equal to matchcol, except for the first - * match and after skipping an empty match. - * regmatch.*pos Where the pattern matched in the old text. - * new_start The new text, all that has been produced so - * far. - * new_end The new text, where to append new text. + * sub_firstline The old text, unmodified. + * copycol Column in the old text where we started + * looking for a match; from here old text still + * needs to be copied to the new text. + * matchcol Column number of the old text where to look + * for the next match. It's just after the + * previous match or one further. + * prev_matchcol Column just after the previous match (if any). + * Mostly equal to matchcol, except for the first + * match and after skipping an empty match. + * regmatch.*pos Where the pattern matched in the old text. + * new_start The new text, all that has been produced so + * far. + * new_end The new text, where to append new text. * - * lnum The line number where we found the start of - * the match. Can be below the line we searched - * when there is a \n before a \zs in the - * pattern. - * sub_firstlnum The line number in the buffer where to look - * for a match. Can be different from "lnum" - * when the pattern or substitute string contains - * line breaks. + * lnum The line number where we found the start of + * the match. Can be below the line we searched + * when there is a \n before a \zs in the + * pattern. + * sub_firstlnum The line number in the buffer where to look + * for a match. Can be different from "lnum" + * when the pattern or substitute string contains + * line breaks. * * Special situations: * - When the substitute string contains a line break, the part up @@ -3625,7 +3705,7 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, copycol = 0; matchcol = 0; - /* At first match, remember current cursor position. */ + // At first match, remember current cursor position. if (!got_match) { setpcmark(); got_match = TRUE; @@ -3721,8 +3801,9 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, did_sub = TRUE; /* Skip the substitution, unless an expression is used, * then it is evaluated in the sandbox. */ - if (!(sub[0] == '\\' && sub[1] == '=')) + if (!(sub[0] == '\\' && sub[1] == '=')) { goto skip; + } } if (subflags.do_ask && !preview) { @@ -3732,7 +3813,7 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, * properly */ int save_State = State; State = CONFIRM; - setmouse(); /* disable mouse in xterm */ + setmouse(); // disable mouse in xterm curwin->w_cursor.col = regmatch.startpos[0].col; if (curwin->w_p_crb) { @@ -3741,8 +3822,9 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, /* When 'cpoptions' contains "u" don't sync undo when * asking for confirmation. */ - if (vim_strchr(p_cpo, CPO_UNDO) != NULL) + if (vim_strchr(p_cpo, CPO_UNDO) != NULL) { ++no_u_sync; + } /* * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed. @@ -3861,10 +3943,12 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, got_quit = true; break; } - if (typed == 'n') + if (typed == 'n') { break; - if (typed == 'y') + } + if (typed == 'y') { break; + } if (typed == 'l') { // last: replace and then stop subflags.do_all = false; @@ -3875,15 +3959,17 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, subflags.do_ask = false; break; } - if (typed == Ctrl_E) + if (typed == Ctrl_E) { scrollup_clamp(); - else if (typed == Ctrl_Y) + } else if (typed == Ctrl_Y) { scrolldown_clamp(); + } } State = save_State; setmouse(); - if (vim_strchr(p_cpo, CPO_UNDO) != NULL) + if (vim_strchr(p_cpo, CPO_UNDO) != NULL) { --no_u_sync; + } if (typed == 'n') { /* For a multi-line match, put matchcol at the NUL at @@ -3897,8 +3983,9 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, } goto skip; } - if (got_quit) + if (got_quit) { goto skip; + } } /* Move the cursor to the start of the match, so that we can @@ -3914,28 +4001,28 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, } #define ADJUST_SUB_FIRSTLNUM() \ - do { \ - /* For a multi-line match, make a copy of the last matched */ \ - /* line and continue in that one. */ \ - if (nmatch > 1) { \ - sub_firstlnum += nmatch - 1; \ - xfree(sub_firstline); \ - sub_firstline = vim_strsave(ml_get(sub_firstlnum)); \ - /* When going beyond the last line, stop substituting. */ \ - if (sub_firstlnum <= line2) { \ - do_again = true; \ - } else { \ - subflags.do_all = false; \ - } \ - } \ - if (skip_match) { \ - /* Already hit end of the buffer, sub_firstlnum is one */ \ - /* less than what it ought to be. */ \ - xfree(sub_firstline); \ - sub_firstline = vim_strsave((char_u *)""); \ - copycol = 0; \ - } \ - } while (0) + do { \ + /* For a multi-line match, make a copy of the last matched */ \ + /* line and continue in that one. */ \ + if (nmatch > 1) { \ + sub_firstlnum += nmatch - 1; \ + xfree(sub_firstline); \ + sub_firstline = vim_strsave(ml_get(sub_firstlnum)); \ + /* When going beyond the last line, stop substituting. */ \ + if (sub_firstlnum <= line2) { \ + do_again = true; \ + } else { \ + subflags.do_all = false; \ + } \ + } \ + if (skip_match) { \ + /* Already hit end of the buffer, sub_firstlnum is one */ \ + /* less than what it ought to be. */ \ + xfree(sub_firstline); \ + sub_firstline = vim_strsave((char_u *)""); \ + copycol = 0; \ + } \ + } while (0) // Save the line numbers for the preview buffer // NOTE: If the pattern matches a final newline, the next line will @@ -4148,8 +4235,9 @@ skip: * it in the buffer. */ ++lnum; - if (u_savedel(lnum, nmatch_tl) != OK) + if (u_savedel(lnum, nmatch_tl) != OK) { break; + } for (i = 0; i < nmatch_tl; i++) { ml_delete(lnum, false); } @@ -4175,7 +4263,7 @@ skip: } sub_firstlnum = lnum; - xfree(sub_firstline); /* free the temp buffer */ + xfree(sub_firstline); // free the temp buffer sub_firstline = new_start; new_start = NULL; matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol; @@ -4183,9 +4271,10 @@ skip: - prev_matchcol; copycol = 0; } - if (nmatch == -1 && !lastone) + if (nmatch == -1 && !lastone) { nmatch = vim_regexec_multi(®match, curwin, curbuf, sub_firstlnum, matchcol, NULL, NULL); + } /* * 5. break if there isn't another match in this line @@ -4194,23 +4283,24 @@ skip: /* If the match found didn't start where we were * searching, do the next search in the line where we * found the match. */ - if (nmatch == -1) + if (nmatch == -1) { lnum -= regmatch.startpos[0].lnum; + } #define PUSH_PREVIEW_LINES() \ - do { \ - linenr_T match_lines = current_match.end.lnum \ - - current_match.start.lnum +1; \ - if (preview_lines.subresults.size > 0) { \ - linenr_T last = kv_last(preview_lines.subresults).end.lnum; \ - if (last == current_match.start.lnum) { \ - preview_lines.lines_needed += match_lines - 1; \ - } \ - } else { \ - preview_lines.lines_needed += match_lines; \ - } \ - kv_push(preview_lines.subresults, current_match); \ - } while (0) + do { \ + linenr_T match_lines = current_match.end.lnum \ + - current_match.start.lnum +1; \ + if (preview_lines.subresults.size > 0) { \ + linenr_T last = kv_last(preview_lines.subresults).end.lnum; \ + if (last == current_match.start.lnum) { \ + preview_lines.lines_needed += match_lines - 1; \ + } \ + } else { \ + preview_lines.lines_needed += match_lines; \ + } \ + kv_push(preview_lines.subresults, current_match); \ + } while (0) // Push the match to preview_lines. PUSH_PREVIEW_LINES(); @@ -4253,7 +4343,7 @@ skip: do_buf_event); } - xfree(sub_firstline); /* may have to free allocated copy of the line */ + xfree(sub_firstline); // may have to free allocated copy of the line // ":s/pat//n" doesn't move the cursor if (subflags.do_count) { @@ -4261,7 +4351,7 @@ skip: } if (sub_nsubs > start_nsubs) { - /* Set the '[ and '] marks. */ + // Set the '[ and '] marks. curbuf->b_op_start.lnum = eap->line1; curbuf->b_op_end.lnum = line2; curbuf->b_op_start.col = curbuf->b_op_end.col = 0; @@ -4340,15 +4430,13 @@ skip: #undef PUSH_PREVIEW_LINES } // NOLINT(readability/fn_size) -/* - * Give message for number of substitutions. - * Can also be used after a ":global" command. - * Return TRUE if a message was given. - */ -bool -do_sub_msg ( - bool count_only /* used 'n' flag for ":s" */ -) +/// Give message for number of substitutions. +/// Can also be used after a ":global" command. +/// +/// @param count_only used 'n' flag for ":s" +/// +/// @return TRUE if a message was given. +bool do_sub_msg(bool count_only) { /* * Only report substitutions when: @@ -4359,27 +4447,31 @@ do_sub_msg ( if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1)) || count_only) && messaging()) { - if (got_int) + if (got_int) { STRCPY(msg_buf, _("(Interrupted) ")); - else + } else { *msg_buf = NUL; - if (sub_nsubs == 1) + } + if (sub_nsubs == 1) { vim_snprintf_add((char *)msg_buf, sizeof(msg_buf), - "%s", count_only ? _("1 match") : _("1 substitution")); - else + "%s", count_only ? _("1 match") : _("1 substitution")); + } else { vim_snprintf_add((char *)msg_buf, sizeof(msg_buf), - count_only ? _("%" PRId64 " matches") - : _("%" PRId64 " substitutions"), - (int64_t)sub_nsubs); - if (sub_nlines == 1) + count_only ? _("%" PRId64 " matches") + : _("%" PRId64 " substitutions"), + (int64_t)sub_nsubs); + } + if (sub_nlines == 1) { vim_snprintf_add((char *)msg_buf, sizeof(msg_buf), - "%s", _(" on 1 line")); - else + "%s", _(" on 1 line")); + } else { vim_snprintf_add((char *)msg_buf, sizeof(msg_buf), - _(" on %" PRId64 " lines"), (int64_t)sub_nlines); - if (msg(msg_buf)) - /* save message to display it after redraw */ + _(" on %" PRId64 " lines"), (int64_t)sub_nlines); + } + if (msg(msg_buf)) { + // save message to display it after redraw set_keep_msg(msg_buf, 0); + } return true; } if (got_int) { @@ -4418,12 +4510,12 @@ static void global_exe_one(char_u *const cmd, const linenr_T lnum) */ void ex_global(exarg_T *eap) { - linenr_T lnum; /* line number according to old situation */ + linenr_T lnum; // line number according to old situation int ndone = 0; - int type; /* first char of cmd: 'v' or 'g' */ - char_u *cmd; /* command argument */ + int type; // first char of cmd: 'v' or 'g' + char_u *cmd; // command argument - char_u delim; /* delimiter, normally '/' */ + char_u delim; // delimiter, normally '/' char_u *pat; regmmatch_T regmatch; int match; @@ -4438,17 +4530,18 @@ void ex_global(exarg_T *eap) return; } - if (eap->forceit) /* ":global!" is like ":vglobal" */ + if (eap->forceit) { // ":global!" is like ":vglobal" type = 'v'; - else + } else { type = *eap->cmd; + } cmd = eap->arg; - which_pat = RE_LAST; /* default: use last used regexp */ + which_pat = RE_LAST; // default: use last used regexp /* * undocumented vi feature: - * "\/" and "\?": use previous search pattern. - * "\&": use previous substitute pattern. + * "\/" and "\?": use previous search pattern. + * "\&": use previous substitute pattern. */ if (*cmd == '\\') { ++cmd; @@ -4456,10 +4549,11 @@ void ex_global(exarg_T *eap) EMSG(_(e_backslash)); return; } - if (*cmd == '&') - which_pat = RE_SUBST; /* use previous substitute pattern */ - else - which_pat = RE_SEARCH; /* use previous search pattern */ + if (*cmd == '&') { + which_pat = RE_SUBST; // use previous substitute pattern + } else { + which_pat = RE_SEARCH; // use previous search pattern + } ++cmd; pat = (char_u *)""; } else if (*cmd == NUL) { @@ -4468,13 +4562,15 @@ void ex_global(exarg_T *eap) } else if (check_regexp_delim(*cmd) == FAIL) { return; } else { - delim = *cmd; /* get the delimiter */ - if (delim) - ++cmd; /* skip delimiter if there is one */ - pat = cmd; /* remember start of pattern */ + delim = *cmd; // get the delimiter + if (delim) { + ++cmd; // skip delimiter if there is one + } + pat = cmd; // remember start of pattern cmd = skip_regexp(cmd, delim, p_magic, &eap->arg); - if (cmd[0] == delim) /* end delimiter found */ - *cmd++ = NUL; /* replace it with a NUL */ + if (cmd[0] == delim) { // end delimiter found + *cmd++ = NUL; // replace it with a NUL + } } if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, ®match) == FAIL) { @@ -4578,19 +4674,17 @@ void global_exe(char_u *cmd) #if defined(EXITFREE) void free_old_sub(void) { - sub_set_replacement((SubReplacementString) {NULL, 0, NULL}); + sub_set_replacement((SubReplacementString) { NULL, 0, NULL }); } #endif -/* - * Set up for a tagpreview. - * Return TRUE when it was created. - */ -bool -prepare_tagpreview ( - bool undo_sync /* sync undo when leaving the window */ -) +/// Set up for a tagpreview. +/// +/// @param undo_sync sync undo when leaving the window +/// +/// @return TRUE when it was created. +bool prepare_tagpreview(bool undo_sync) { /* * If there is already a preview window open, use that one. @@ -4609,8 +4703,9 @@ prepare_tagpreview ( * There is no preview window open yet. Create one. */ if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0) - == FAIL) + == FAIL) { return false; + } curwin->w_p_pvw = TRUE; curwin->w_p_wfh = TRUE; RESET_BINDING(curwin); /* don't take over 'scrollbind' @@ -4633,7 +4728,7 @@ void ex_help(exarg_T *eap) { char_u *arg; char_u *tag; - FILE *helpfd; /* file descriptor of help file */ + FILE *helpfd; // file descriptor of help file int n; int i; win_T *wp; @@ -4667,49 +4762,57 @@ void ex_help(exarg_T *eap) return; } - if (eap->skip) /* not executing commands */ + if (eap->skip) { // not executing commands return; - } else + } + } else { arg = (char_u *)""; + } - /* remove trailing blanks */ + // remove trailing blanks p = arg + STRLEN(arg) - 1; - while (p > arg && ascii_iswhite(*p) && p[-1] != '\\') + while (p > arg && ascii_iswhite(*p) && p[-1] != '\\') { *p-- = NUL; + } - /* Check for a specified language */ + // Check for a specified language lang = check_help_lang(arg); - /* When no argument given go to the index. */ - if (*arg == NUL) + // When no argument given go to the index. + if (*arg == NUL) { arg = (char_u *)"help.txt"; + } /* * Check if there is a match for the argument. */ n = find_help_tags(arg, &num_matches, &matches, - eap != NULL && eap->forceit); + eap != NULL && eap->forceit); i = 0; - if (n != FAIL && lang != NULL) - /* Find first item with the requested language. */ + if (n != FAIL && lang != NULL) { + // Find first item with the requested language. for (i = 0; i < num_matches; ++i) { len = (int)STRLEN(matches[i]); if (len > 3 && matches[i][len - 3] == '@' - && STRICMP(matches[i] + len - 2, lang) == 0) + && STRICMP(matches[i] + len - 2, lang) == 0) { break; + } } + } if (i >= num_matches || n == FAIL) { - if (lang != NULL) + if (lang != NULL) { EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg); - else + } else { EMSG2(_("E149: Sorry, no help for %s"), arg); - if (n != FAIL) + } + if (n != FAIL) { FreeWild(num_matches, matches); + } return; } - /* The first match (in the requested language) is the best match. */ + // The first match (in the requested language) is the best match. tag = vim_strsave(matches[i]); FreeWild(num_matches, matches); @@ -4718,8 +4821,7 @@ void ex_help(exarg_T *eap) * Always open a new one for ":tab help". */ if (!bt_help(curwin->w_buffer) - || cmdmod.tab != 0 - ) { + || cmdmod.tab != 0) { if (cmdmod.tab != 0) { wp = NULL; } else { @@ -4747,13 +4849,16 @@ void ex_help(exarg_T *eap) * narrow. */ n = WSP_HELP; if (cmdmod.split == 0 && curwin->w_width != Columns - && curwin->w_width < 80) + && curwin->w_width < 80) { n |= WSP_TOP; - if (win_split(0, n) == FAIL) + } + if (win_split(0, n) == FAIL) { goto erret; + } - if (curwin->w_height < p_hh) + if (curwin->w_height < p_hh) { win_setheight((int)p_hh); + } /* * Open help file (do_ecmd() will set b_help flag, readfile() will @@ -4762,18 +4867,19 @@ void ex_help(exarg_T *eap) */ alt_fnum = curbuf->b_fnum; (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL, - ECMD_HIDE + ECMD_SET_HELP, - NULL /* buffer is still open, don't store info */ - ); - if (!cmdmod.keepalt) + ECMD_HIDE + ECMD_SET_HELP, + NULL // buffer is still open, don't store info + ); + if (!cmdmod.keepalt) { curwin->w_alt_fnum = alt_fnum; + } empty_fnum = curbuf->b_fnum; } } - if (!p_im) - restart_edit = 0; /* don't want insert mode in help file */ - + if (!p_im) { + restart_edit = 0; // don't want insert mode in help file + } /* Restore KeyTyped, setting 'filetype=help' may reset it. * It is needed for do_tag top open folds under the cursor. */ KeyTyped = old_KeyTyped; @@ -4790,9 +4896,10 @@ void ex_help(exarg_T *eap) } } - /* keep the previous alternate file */ - if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt) + // keep the previous alternate file + if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt) { curwin->w_alt_fnum = alt_fnum; + } erret: xfree(tag); @@ -4810,37 +4917,37 @@ char_u *check_help_lang(char_u *arg) if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2]) && ASCII_ISALPHA(arg[len - 1])) { - arg[len - 3] = NUL; /* remove the '@' */ + arg[len - 3] = NUL; // remove the '@' return arg + len - 2; } return NULL; } -/* - * Return a heuristic indicating how well the given string matches. The - * smaller the number, the better the match. This is the order of priorities, - * from best match to worst match: - * - Match with least alphanumeric characters is better. - * - Match with least total characters is better. - * - Match towards the start is better. - * - Match starting with "+" is worse (feature instead of command) - * Assumption is made that the matched_string passed has already been found to - * match some string for which help is requested. webb. - */ -int -help_heuristic( - char_u *matched_string, - int offset, // offset for match - int wrong_case // no matching case -) +/// Return a heuristic indicating how well the given string matches. The +/// smaller the number, the better the match. This is the order of priorities, +/// from best match to worst match: +/// - Match with least alphanumeric characters is better. +/// - Match with least total characters is better. +/// - Match towards the start is better. +/// - Match starting with "+" is worse (feature instead of command) +/// Assumption is made that the matched_string passed has already been found to +/// match some string for which help is requested. webb. +/// +/// @param offset offset for match +/// @param wrong_case no matching case +/// +/// @return a heuristic indicating how well the given string matches. +int help_heuristic(char_u *matched_string, int offset, int wrong_case) { int num_letters; char_u *p; num_letters = 0; - for (p = matched_string; *p; p++) - if (ASCII_ISALNUM(*p)) + for (p = matched_string; *p; p++) { + if (ASCII_ISALNUM(*p)) { num_letters++; + } + } /* * Multiply the number of letters by 100 to give it a much bigger @@ -4887,39 +4994,38 @@ static int help_compare(const void *s1, const void *s2) // the number of matches in num_matches. // The matches will be sorted with a "best" match algorithm. // When "keep_lang" is true try keeping the language of the current buffer. -int find_help_tags(const char_u *arg, int *num_matches, char_u ***matches, - bool keep_lang) +int find_help_tags(const char_u *arg, int *num_matches, char_u ***matches, bool keep_lang) { int i; static const char *(mtable[]) = { - "*", "g*", "[*", "]*", - "/*", "/\\*", "\"*", "**", - "/\\(\\)", "/\\%(\\)", - "?", ":?", "?", "g?", "g?g?", "g??", - "-?", "q?", "v_g?", - "/\\?", "/\\z(\\)", "\\=", ":s\\=", - "[count]", "[quotex]", - "[range]", ":[range]", - "[pattern]", "\\|", "\\%$", - "s/\\~", "s/\\U", "s/\\L", - "s/\\1", "s/\\2", "s/\\3", "s/\\9" + "*", "g*", "[*", "]*", + "/*", "/\\*", "\"*", "**", + "/\\(\\)", "/\\%(\\)", + "?", ":?", "?", "g?", "g?g?", "g??", + "-?", "q?", "v_g?", + "/\\?", "/\\z(\\)", "\\=", ":s\\=", + "[count]", "[quotex]", + "[range]", ":[range]", + "[pattern]", "\\|", "\\%$", + "s/\\~", "s/\\U", "s/\\L", + "s/\\1", "s/\\2", "s/\\3", "s/\\9" }; static const char *(rtable[]) = { - "star", "gstar", "[star", "]star", - "/star", "/\\\\star", "quotestar", "starstar", - "/\\\\(\\\\)", "/\\\\%(\\\\)", - "?", ":?", "?", "g?", "g?g?", "g??", - "-?", "q?", "v_g?", - "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=", - "\\[count]", "\\[quotex]", - "\\[range]", ":\\[range]", - "\\[pattern]", "\\\\bar", "/\\\\%\\$", - "s/\\\\\\~", "s/\\\\U", "s/\\\\L", - "s/\\\\1", "s/\\\\2", "s/\\\\3", "s/\\\\9" + "star", "gstar", "[star", "]star", + "/star", "/\\\\star", "quotestar", "starstar", + "/\\\\(\\\\)", "/\\\\%(\\\\)", + "?", ":?", "?", "g?", "g?g?", "g??", + "-?", "q?", "v_g?", + "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=", + "\\[count]", "\\[quotex]", + "\\[range]", ":\\[range]", + "\\[pattern]", "\\\\bar", "/\\\\%\\$", + "s/\\\\\\~", "s/\\\\U", "s/\\\\L", + "s/\\\\1", "s/\\\\2", "s/\\\\3", "s/\\\\9" }; static const char *(expr_table[]) = { - "!=?", "!~?", "<=?", "=?", ">?", "is?", "isnot?" + "!=?", "!~?", "<=?", "=?", ">?", "is?", "isnot?" }; char_u *d = IObuff; // assume IObuff is long enough! @@ -4952,7 +5058,7 @@ int find_help_tags(const char_u *arg, int *num_matches, char_u ***matches, } } - if (i < 0) { /* no match in table */ + if (i < 0) { // no match in table /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched. * Also replace "\%^" and "\%(", they match every tag too. * Also "\zs", "\z1", etc. @@ -4964,9 +5070,10 @@ int find_help_tags(const char_u *arg, int *num_matches, char_u ***matches, && arg[2] != NUL))) { STRCPY(d, "/\\\\"); STRCPY(d + 3, arg + 1); - /* Check for "/\\_$", should be "/\\_\$" */ - if (d[3] == '_' && d[4] == '$') + // Check for "/\\_$", should be "/\\_\$" + if (d[3] == '_' && d[4] == '$') { STRCPY(d + 4, "\\$"); + } } else { /* Replace: * "[:...:]" with "\[:...:]" @@ -4975,12 +5082,13 @@ int find_help_tags(const char_u *arg, int *num_matches, char_u ***matches, */ if ((arg[0] == '[' && (arg[1] == ':' || (arg[1] == '+' && arg[2] == '+'))) - || (arg[0] == '\\' && arg[1] == '{')) + || (arg[0] == '\\' && arg[1] == '{')) { *d++ = '\\'; + } // If tag starts with "('", skip the "(". Fixes CTRL-] on ('option'. if (*arg == '(' && arg[1] == '\'') { - arg++; + arg++; } for (const char_u *s = arg; *s; s++) { // Replace "|" with "bar" and '"' with "quote" to match the name of @@ -4993,19 +5101,24 @@ int find_help_tags(const char_u *arg, int *num_matches, char_u ***matches, break; } switch (*s) { - case '|': STRCPY(d, "bar"); + case '|': + STRCPY(d, "bar"); d += 3; continue; - case '"': STRCPY(d, "quote"); + case '"': + STRCPY(d, "quote"); d += 5; continue; - case '*': *d++ = '.'; + case '*': + *d++ = '.'; break; - case '?': *d++ = '.'; + case '?': + *d++ = '.'; continue; case '$': case '.': - case '~': *d++ = '\\'; + case '~': + *d++ = '\\'; break; } @@ -5016,31 +5129,36 @@ int find_help_tags(const char_u *arg, int *num_matches, char_u ***matches, */ if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1]) || vim_strchr((char_u *) - "?@[\\]^", - s[1]) != NULL))) { - if (d > IObuff && d[-1] != '_' && d[-1] != '\\') - *d++ = '_'; /* prepend a '_' to make x_CTRL-x */ + "?@[\\]^", + s[1]) != NULL))) { + if (d > IObuff && d[-1] != '_' && d[-1] != '\\') { + *d++ = '_'; // prepend a '_' to make x_CTRL-x + } STRCPY(d, "CTRL-"); d += 5; if (*s < ' ') { *d++ = *s + '@'; - if (d[-1] == '\\') - *d++ = '\\'; /* double a backslash */ - } else + if (d[-1] == '\\') { + *d++ = '\\'; // double a backslash + } + } else { *d++ = *++s; - if (s[1] != NUL && s[1] != '_') - *d++ = '_'; /* append a '_' */ + } + if (s[1] != NUL && s[1] != '_') { + *d++ = '_'; // append a '_' + } continue; - } else if (*s == '^') /* "^" or "CTRL-^" or "^_" */ + } else if (*s == '^') { // "^" or "CTRL-^" or "^_" *d++ = '\\'; - + } /* * Insert a backslash before a backslash after a slash, for search * pattern tags: "/\|" --> "/\\|". */ else if (s[0] == '\\' && s[1] != '\\' - && *arg == '/' && s == arg + 1) + && *arg == '/' && s == arg + 1) { *d++ = '\\'; + } /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in * "CTRL-\_CTRL-N" */ @@ -5072,16 +5190,16 @@ int find_help_tags(const char_u *arg, int *num_matches, char_u ***matches, if (*IObuff == '`') { if (d > IObuff + 2 && d[-1] == '`') { - /* remove the backticks from `command` */ + // remove the backticks from `command` memmove(IObuff, IObuff + 1, STRLEN(IObuff)); d[-2] = NUL; } else if (d > IObuff + 3 && d[-2] == '`' && d[-1] == ',') { - /* remove the backticks and comma from `command`, */ + // remove the backticks and comma from `command`, memmove(IObuff, IObuff + 1, STRLEN(IObuff)); d[-3] = NUL; } else if (d > IObuff + 4 && d[-3] == '`' && d[-2] == '\\' && d[-1] == '.') { - /* remove the backticks and dot from `command`\. */ + // remove the backticks and dot from `command`\. memmove(IObuff, IObuff + 1, STRLEN(IObuff)); d[-4] = NUL; } @@ -5100,10 +5218,11 @@ int find_help_tags(const char_u *arg, int *num_matches, char_u ***matches, /* Sort the matches found on the heuristic number that is after the * tag name. */ qsort((void *)*matches, (size_t)*num_matches, - sizeof(char_u *), help_compare); - /* Delete more than TAG_MANY to reduce the size of the listing. */ - while (*num_matches > TAG_MANY) + sizeof(char_u *), help_compare); + // Delete more than TAG_MANY to reduce the size of the listing. + while (*num_matches > TAG_MANY) { xfree((*matches)[--*num_matches]); + } } return OK; } @@ -5170,7 +5289,7 @@ void fix_help_buffer(void) line = ml_get_buf(curbuf, lnum, false); const size_t len = STRLEN(line); if (in_example && len > 0 && !ascii_iswhite(line[0])) { - /* End of example: non-white or '<' in first column. */ + // End of example: non-white or '<' in first column. if (line[0] == '<') { // blank-out a '<' in the first column line = ml_get_buf(curbuf, lnum, true); @@ -5203,8 +5322,7 @@ void fix_help_buffer(void) && ASCII_ISALPHA(fname[5]) && ASCII_ISALPHA(fname[6]) && TOLOWER_ASC(fname[7]) == 'x' - && fname[8] == NUL) - ) { + && fname[8] == NUL)) { for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; lnum++) { line = ml_get_buf(curbuf, lnum, false); if (strstr((char *)line, "*local-additions*") == NULL) { @@ -5235,9 +5353,9 @@ void fix_help_buffer(void) // Note: We cannot just do `&NameBuff` because it is a statically sized array // so `NameBuff == &NameBuff` according to C semantics. - char_u *buff_list[1] = {NameBuff}; + char_u *buff_list[1] = { NameBuff }; if (gen_expand_wildcards(1, buff_list, &fcount, - &fnames, EW_FILE|EW_SILENT) == OK + &fnames, EW_FILE|EW_SILENT) == OK && fcount > 0) { // If foo.abx is found use it instead of foo.txt in // the same directory. @@ -5294,8 +5412,9 @@ void fix_help_buffer(void) IObuff[0] = '|'; *s = '|'; while (*s != NUL) { - if (*s == '\r' || *s == '\n') + if (*s == '\r' || *s == '\n') { *s = NUL; + } /* The text is utf-8 when a byte * above 127 is found and no * illegal byte sequence is found. @@ -5314,10 +5433,9 @@ void fix_help_buffer(void) * conversion to the current * 'encoding' may be required. */ vc.vc_type = CONV_NONE; - convert_setup( - &vc, - (char_u *)(this_utf == kTrue ? "utf-8" : "latin1"), - p_enc); + convert_setup(&vc, + (char_u *)(this_utf == kTrue ? "utf-8" : "latin1"), + p_enc); if (vc.vc_type == CONV_NONE) { // No conversion needed. cp = IObuff; @@ -5374,8 +5492,8 @@ void ex_viusage(exarg_T *eap) /// French) /// @param add_help_tags Whether to add the "help-tags" tag /// @param ignore_writeerr ignore write error -static void helptags_one(char_u *dir, const char_u *ext, const char_u *tagfname, - bool add_help_tags, bool ignore_writeerr) +static void helptags_one(char_u *dir, const char_u *ext, const char_u *tagfname, bool add_help_tags, + bool ignore_writeerr) FUNC_ATTR_NONNULL_ALL { garray_T ga; @@ -5397,9 +5515,9 @@ static void helptags_one(char_u *dir, const char_u *ext, const char_u *tagfname, // Note: We cannot just do `&NameBuff` because it is a statically sized array // so `NameBuff == &NameBuff` according to C semantics. - char_u *buff_list[1] = {NameBuff}; + char_u *buff_list[1] = { NameBuff }; if (gen_expand_wildcards(1, buff_list, &filecount, &files, - EW_FILE|EW_SILENT) == FAIL + EW_FILE|EW_SILENT) == FAIL || filecount == 0) { if (!got_int) { EMSG2(_("E151: No match: %s"), NameBuff); @@ -5470,9 +5588,8 @@ static void helptags_one(char_u *dir, const char_u *ext, const char_u *tagfname, if (utf8 == kNone) { // first file utf8 = this_utf8; } else if (utf8 != this_utf8) { - EMSG2(_( - "E670: Mix of help file encodings within a language: %s"), - files[fi]); + EMSG2(_("E670: Mix of help file encodings within a language: %s"), + files[fi]); mix = !got_int; got_int = TRUE; } @@ -5527,8 +5644,8 @@ static void helptags_one(char_u *dir, const char_u *ext, const char_u *tagfname, if (*p2 == '\t') { *p2 = NUL; vim_snprintf((char *)NameBuff, MAXPATHL, - _("E154: Duplicate tag \"%s\" in file %s/%s"), - ((char_u **)ga.ga_data)[i], dir, p2 + 1); + _("E154: Duplicate tag \"%s\" in file %s/%s"), + ((char_u **)ga.ga_data)[i], dir, p2 + 1); EMSG(NameBuff); *p2 = '\t'; break; @@ -5570,8 +5687,7 @@ static void helptags_one(char_u *dir, const char_u *ext, const char_u *tagfname, } /// Generate tags in one help directory, taking care of translations. -static void do_helptags(char_u *dirname, bool add_help_tags, - bool ignore_writeerr) +static void do_helptags(char_u *dirname, bool add_help_tags, bool ignore_writeerr) FUNC_ATTR_NONNULL_ALL { int len; @@ -5592,7 +5708,7 @@ static void do_helptags(char_u *dirname, bool add_help_tags, // Note: We cannot just do `&NameBuff` because it is a statically sized array // so `NameBuff == &NameBuff` according to C semantics. - char_u *buff_list[1] = {NameBuff}; + char_u *buff_list[1] = { NameBuff }; if (gen_expand_wildcards(1, buff_list, &filecount, &files, EW_FILE|EW_SILENT) == FAIL || filecount == 0) { @@ -5610,18 +5726,19 @@ static void do_helptags(char_u *dirname, bool add_help_tags, continue; } if (STRICMP(files[i] + len - 4, ".txt") == 0) { - /* ".txt" -> language "en" */ + // ".txt" -> language "en" lang[0] = 'e'; lang[1] = 'n'; } else if (files[i][len - 4] == '.' && ASCII_ISALPHA(files[i][len - 3]) && ASCII_ISALPHA(files[i][len - 2]) && TOLOWER_ASC(files[i][len - 1]) == 'x') { - /* ".abx" -> language "ab" */ + // ".abx" -> language "ab" lang[0] = TOLOWER_ASC(files[i][len - 3]); lang[1] = TOLOWER_ASC(files[i][len - 2]); - } else + } else { continue; + } // Did we find this language already? for (j = 0; j < ga.ga_len; j += 2) { @@ -5645,11 +5762,11 @@ static void do_helptags(char_u *dirname, bool add_help_tags, fname[5] = ((char_u *)ga.ga_data)[j]; fname[6] = ((char_u *)ga.ga_data)[j + 1]; if (fname[5] == 'e' && fname[6] == 'n') { - /* English is an exception: use ".txt" and "tags". */ + // English is an exception: use ".txt" and "tags". fname[4] = NUL; STRCPY(ext, ".txt"); } else { - /* Language "ab" uses ".abx" and "tags-ab". */ + // Language "ab" uses ".abx" and "tags-ab". STRCPY(ext, ".xxx"); ext[1] = fname[5]; ext[2] = fname[6]; @@ -5664,7 +5781,7 @@ static void do_helptags(char_u *dirname, bool add_help_tags, static void helptags_cb(char_u *fname, void *cookie) FUNC_ATTR_NONNULL_ALL { - do_helptags(fname, *(bool *)cookie, true); + do_helptags(fname, *(bool *)cookie, true); } /* @@ -5676,7 +5793,7 @@ void ex_helptags(exarg_T *eap) char_u *dirname; bool add_help_tags = false; - /* Check for ":helptags ++t {dir}". */ + // Check for ":helptags ++t {dir}". if (STRNCMP(eap->arg, "++t", 3) == 0 && ascii_iswhite(eap->arg[3])) { add_help_tags = true; eap->arg = skipwhite(eap->arg + 3); @@ -5732,9 +5849,8 @@ int sub_preview_win(buf_T *preview_buf) /// Shows the effects of the :substitute command being typed ('inccommand'). /// If inccommand=split, shows a preview window and later restores the layout. -static buf_T *show_sub(exarg_T *eap, pos_T old_cusr, - PreviewLines *preview_lines, int hl_id, int src_id, - handle_T bufnr) +static buf_T *show_sub(exarg_T *eap, pos_T old_cusr, PreviewLines *preview_lines, int hl_id, + int src_id, handle_T bufnr) FUNC_ATTR_NONNULL_ALL { win_T *save_curwin = curwin; @@ -5883,12 +5999,12 @@ static buf_T *show_sub(exarg_T *eap, pos_T old_cusr, /// Closes any open windows for inccommand preview buffer. void close_preview_windows(void) { - block_autocmds(); - buf_T *buf = preview_bufnr ? buflist_findnr(preview_bufnr) : NULL; - if (buf != NULL) { - close_windows(buf, false); - } - unblock_autocmds(); + block_autocmds(); + buf_T *buf = preview_bufnr ? buflist_findnr(preview_bufnr) : NULL; + if (buf != NULL) { + close_windows(buf, false); + } + unblock_autocmds(); } /// :substitute command @@ -5936,7 +6052,9 @@ void ex_substitute(exarg_T *eap) if (save_changedtick != buf_get_changedtick(curbuf)) { // Undo invisibly. This also moves the cursor! - if (!u_undo_and_forget(1)) { abort(); } + if (!u_undo_and_forget(1)) { + abort(); + } // Restore newhead. It is meaningless when curhead is valid, but we must // restore it so that undotree() is identical before/after the preview. curbuf->b_u_newhead = save_b_u_newhead; 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 -#include +#include #include #include -#include +#include -#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 ":" 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, ""); 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); + } + // Skip "to" part. + while (arg[0] != NUL && (uint8_t)arg[0] != delim) { + if (arg[0] == '\\' && arg[1] != NUL) { + arg++; + } arg++; } - arg++; + if (arg[0] != NUL) { // Skip delimiter. + arg++; + } + while (arg[0] && strchr("|\"#", arg[0]) == NULL) { + arg++; + } + if (arg[0] != NUL) { + return arg; + } + break; } - if (arg[0] != NUL) { // Skip delimiter. - arg++; - } - while (arg[0] && strchr("|\"#", arg[0]) == NULL) { - arg++; - } - if (arg[0] != NUL) { - return arg; - } - 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; + 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; } - 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; + 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))) { + 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_ARGUMENTS: - // add 1 if ARGCOUNT is 0 - if (eap->line2 > ARGCOUNT + (!ARGCOUNT)) { + 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_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) { - 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; + 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 () */ - /* This is hard, so only do it once, and cache the result */ - if (*split_buf == NULL) + case 2: // Quote and split () + // 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; + 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; + 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 = '"'; - } - - break; - } - - case ct_MODS: - { - result = quote ? 2 : 0; - if (buf != NULL) { if (quote) { - *buf++ = '"'; + result += 2; } - *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); + if (buf != NULL) { + if (quote) { + *buf++ = '"'; + } + STRCPY(buf, num_buf); + buf += num_len; + if (quote) { + *buf = '"'; + } } + + break; } - // TODO(vim): How to support :noautocmd? - // TODO(vim): How to support :sandbox? + case ct_MODS: { + result = quote ? 2 : 0; + if (buf != NULL) { + if (quote) { + *buf++ = '"'; + } + *buf = '\0'; + } - // :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); - } + bool multi_mods = false; - // TODO(vim): How to support :unsilent? + // :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); + } - // :verbose - if (p_verbose > 0) { - result += add_cmd_modifier(buf, "verbose", &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? + + // :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? + + // :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; } - // :vertical - if (cmdmod.split & WSP_VERT) { - result += add_cmd_modifier(buf, "vertical", &multi_mods); - } - if (quote && buf != NULL) { - buf += result - 2; - *buf = '"'; - } - 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 is found */ - if (start == NULL || end == NULL) + // break if there no 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; + // ":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); + 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 */ -) +/// 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 */ + 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 " command and alikes. - */ -void -do_exedit( - exarg_T *eap, - win_T *old_curwin /* curwin before doing a split or NULL */ -) +/// ":edit " 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 - * '' to word under the cursor - * '' to WORD under the cursor - * '' to C-expression under the cursor - * '' to path name under the cursor - * '' to sourced file name - * '' to sourced file line number - * '' to file name for autocommand - * '' to buffer number for autocommand - * '' 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 +/// '' to word under the cursor +/// '' to WORD under the cursor +/// '' to C-expression under the cursor +/// '' to path name under the cursor +/// '' to sourced file name +/// '' to sourced file line number +/// '' to file name for autocommand +/// '' to buffer number for autocommand +/// '' 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 #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 \"\""); + *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"\""); 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 \"\""); + *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"\""); 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 \"\""); + *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"\""); 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 \"\""); + *errormsg = (char_u *)_("E498: no :source file name to substitute for \"\""); 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, "", 7) != 0) + if (STRNCMP(p, "", 7) != 0) { ++p; - else { - /* replace "" with the sourced file name, and do ":" stuff */ + } else { + // replace "" 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))); diff --git a/src/nvim/normal.c b/src/nvim/normal.c index 4c3d7d58de..85897bac12 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -9,14 +9,12 @@ #include #include -#include #include #include +#include -#include "nvim/log.h" -#include "nvim/vim.h" +#include "nvim/api/private/helpers.h" #include "nvim/ascii.h" -#include "nvim/normal.h" #include "nvim/buffer.h" #include "nvim/change.h" #include "nvim/charset.h" @@ -25,6 +23,7 @@ #include "nvim/digraph.h" #include "nvim/edit.h" #include "nvim/eval/userfunc.h" +#include "nvim/event/loop.h" #include "nvim/ex_cmds.h" #include "nvim/ex_cmds2.h" #include "nvim/ex_docmd.h" @@ -34,35 +33,35 @@ #include "nvim/getchar.h" #include "nvim/indent.h" #include "nvim/indent_c.h" +#include "nvim/keymap.h" +#include "nvim/log.h" #include "nvim/main.h" #include "nvim/mark.h" #include "nvim/memline.h" #include "nvim/memory.h" #include "nvim/message.h" #include "nvim/misc1.h" -#include "nvim/keymap.h" -#include "nvim/move.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/time.h" #include "nvim/plines.h" #include "nvim/quickfix.h" #include "nvim/screen.h" #include "nvim/search.h" #include "nvim/spell.h" #include "nvim/spellfile.h" +#include "nvim/state.h" #include "nvim/strings.h" #include "nvim/syntax.h" #include "nvim/tag.h" #include "nvim/ui.h" -#include "nvim/mouse.h" #include "nvim/undo.h" +#include "nvim/vim.h" #include "nvim/window.h" -#include "nvim/state.h" -#include "nvim/event/loop.h" -#include "nvim/os/time.h" -#include "nvim/os/input.h" -#include "nvim/api/private/helpers.h" typedef struct normal_state { VimState state; @@ -88,10 +87,10 @@ typedef struct normal_state { /* * The Visual area is remembered for reselection. */ -static int resel_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */ -static linenr_T resel_VIsual_line_count; /* number of lines */ -static colnr_T resel_VIsual_vcol; /* nr of cols or end col */ -static int VIsual_mode_orig = NUL; /* saved Visual mode */ +static int resel_VIsual_mode = NUL; // 'v', 'V', or Ctrl-V +static linenr_T resel_VIsual_line_count; // number of lines +static colnr_T resel_VIsual_vcol; // nr of cols or end col +static int VIsual_mode_orig = NUL; // saved Visual mode #ifdef INCLUDE_GENERATED_DECLARATIONS @@ -119,18 +118,18 @@ static char *e_noident = N_("E349: No identifier under cursor"); */ typedef void (*nv_func_T)(cmdarg_T *cap); -/* Values for cmd_flags. */ -#define NV_NCH 0x01 /* may need to get a second char */ -#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */ -#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */ -#define NV_LANG 0x08 /* second char needs language adjustment */ +// Values for cmd_flags. +#define NV_NCH 0x01 // may need to get a second char +#define NV_NCH_NOP (0x02|NV_NCH) // get second char when no operator pending +#define NV_NCH_ALW (0x04|NV_NCH) // always get a second char +#define NV_LANG 0x08 // second char needs language adjustment -#define NV_SS 0x10 /* may start selection */ -#define NV_SSS 0x20 /* may start selection with shift modifier */ -#define NV_STS 0x40 /* may stop selection without shift modif. */ -#define NV_RL 0x80 /* 'rightleft' modifies command */ -#define NV_KEEPREG 0x100 /* don't clear regname */ -#define NV_NCW 0x200 /* not allowed in command-line window */ +#define NV_SS 0x10 // may start selection +#define NV_SSS 0x20 // may start selection with shift modifier +#define NV_STS 0x40 // may stop selection without shift modif. +#define NV_RL 0x80 // 'rightleft' modifies command +#define NV_KEEPREG 0x100 // don't clear regname +#define NV_NCW 0x200 // not allowed in command-line window /* * Generally speaking, every Normal mode command should either clear any @@ -148,10 +147,10 @@ typedef void (*nv_func_T)(cmdarg_T *cap); * It is faster when all keys from zero to '~' are present. */ static const struct nv_cmd { - int cmd_char; /* (first) command character */ - nv_func_T cmd_func; /* function for this command */ - uint16_t cmd_flags; /* NV_ flags */ - short cmd_arg; /* value for ca.arg */ + int cmd_char; // (first) command character + nv_func_T cmd_func; // function for this command + uint16_t cmd_flags; // NV_ flags + short cmd_arg; // value for ca.arg } nv_cmds[] = { { NUL, nv_error, 0, 0 }, @@ -344,10 +343,10 @@ static const struct nv_cmd { { K_COMMAND, nv_colon, 0, 0 }, }; -/* Number of commands in nv_cmds[]. */ +// Number of commands in nv_cmds[]. #define NV_CMDS_SIZE ARRAY_SIZE(nv_cmds) -/* Sorted index of commands in nv_cmds[]. */ +// Sorted index of commands in nv_cmds[]. static short nv_cmd_idx[NV_CMDS_SIZE]; /* The highest index for which @@ -362,13 +361,15 @@ static int nv_compare(const void *s1, const void *s2) { int c1, c2; - /* The commands are sorted on absolute value. */ + // The commands are sorted on absolute value. c1 = nv_cmds[*(const short *)s1].cmd_char; c2 = nv_cmds[*(const short *)s2].cmd_char; - if (c1 < 0) + if (c1 < 0) { c1 = -c1; - if (c2 < 0) + } + if (c2 < 0) { c2 = -c2; + } return c1 - c2; } @@ -379,15 +380,15 @@ void init_normal_cmds(void) { assert(NV_CMDS_SIZE <= SHRT_MAX); - /* Fill the index table with a one to one relation. */ + // Fill the index table with a one to one relation. for (short int i = 0; i < (short int)NV_CMDS_SIZE; ++i) { nv_cmd_idx[i] = i; } - /* Sort the commands by the command character. */ + // Sort the commands by the command character. qsort(&nv_cmd_idx, NV_CMDS_SIZE, sizeof(short), nv_compare); - /* Find the first entry that can't be indexed by the command character. */ + // Find the first entry that can't be indexed by the command character. short int i; for (i = 0; i < (short int)NV_CMDS_SIZE; ++i) { if (i != nv_cmds[nv_cmd_idx[i]].cmd_char) { @@ -408,38 +409,43 @@ static int find_command(int cmdchar) int top, bot; int c; - /* A multi-byte character is never a command. */ - if (cmdchar >= 0x100) + // A multi-byte character is never a command. + if (cmdchar >= 0x100) { return -1; + } /* We use the absolute value of the character. Special keys have a * negative value, but are sorted on their absolute value. */ - if (cmdchar < 0) + if (cmdchar < 0) { cmdchar = -cmdchar; + } /* If the character is in the first part: The character is the index into * nv_cmd_idx[]. */ assert(nv_max_linear < (int)NV_CMDS_SIZE); - if (cmdchar <= nv_max_linear) + if (cmdchar <= nv_max_linear) { return nv_cmd_idx[cmdchar]; + } - /* Perform a binary search. */ + // Perform a binary search. bot = nv_max_linear + 1; top = NV_CMDS_SIZE - 1; idx = -1; while (bot <= top) { i = (top + bot) / 2; c = nv_cmds[nv_cmd_idx[i]].cmd_char; - if (c < 0) + if (c < 0) { c = -c; + } if (cmdchar == c) { idx = nv_cmd_idx[i]; break; } - if (cmdchar > c) + if (cmdchar > c) { bot = i + 1; - else + } else { top = i - 1; + } } return idx; } @@ -544,59 +550,60 @@ static bool normal_need_additional_char(NormalState *s) int cmdchar = s->ca.cmdchar; // without NV_NCH we never need to check for an additional char return flags & NV_NCH && ( - // NV_NCH_NOP is set and no operator is pending, get a second char - ((flags & NV_NCH_NOP) == NV_NCH_NOP && !pending_op) - // NV_NCH_ALW is set, always get a second char - || (flags & NV_NCH_ALW) == NV_NCH_ALW - // 'q' without a pending operator, recording or executing a register, - // needs to be followed by a second char, examples: - // - qc => record using register c - // - q: => open command-line window - || (cmdchar == 'q' - && !pending_op - && reg_recording == 0 - && reg_executing == 0) - // 'a' or 'i' after an operator is a text object, examples: - // - ciw => change inside word - // - da( => delete parenthesis and everything inside. - // Also, don't do anything when these keys are received in visual mode - // so just get another char. - // - // TODO(tarruda): Visual state needs to be refactored into a - // separate state that "inherits" from normal state. - || ((cmdchar == 'a' || cmdchar == 'i') && (pending_op || VIsual_active))); + // NV_NCH_NOP is set and no operator is pending, get a second char + ((flags & NV_NCH_NOP) == NV_NCH_NOP && !pending_op) + // NV_NCH_ALW is set, always get a second char + || (flags & NV_NCH_ALW) == NV_NCH_ALW + // 'q' without a pending operator, recording or executing a register, + // needs to be followed by a second char, examples: + // - qc => record using register c + // - q: => open command-line window + || (cmdchar == 'q' + && !pending_op + && reg_recording == 0 + && reg_executing == 0) + // 'a' or 'i' after an operator is a text object, examples: + // - ciw => change inside word + // - da( => delete parenthesis and everything inside. + // Also, don't do anything when these keys are received in visual mode + // so just get another char. + // + // TODO(tarruda): Visual state needs to be refactored into a + // separate state that "inherits" from normal state. + || ((cmdchar == 'a' || cmdchar == 'i') && + (pending_op || VIsual_active))); } static bool normal_need_redraw_mode_message(NormalState *s) { return ( - // 'showmode' is set and messages can be printed - ((p_smd && msg_silent == 0 - // must restart insert mode(ctrl+o or ctrl+l) or we just entered visual - // mode - && (restart_edit != 0 || (VIsual_active - && s->old_pos.lnum == curwin->w_cursor.lnum - && s->old_pos.col == curwin->w_cursor.col)) - // command-line must be cleared or redrawn - && (clear_cmdline || redraw_cmdline) - // some message was printed or scrolled - && (msg_didout || (msg_didany && msg_scroll)) - // it is fine to remove the current message - && !msg_nowait - // the command was the result of direct user input and not a mapping - && KeyTyped) - // must restart insert mode, not in visual mode and error message is - // being shown - || (restart_edit != 0 && !VIsual_active && msg_scroll - && emsg_on_display)) - // no register was used - && s->oa.regname == 0 - && !(s->ca.retval & CA_COMMAND_BUSY) - && stuff_empty() - && typebuf_typed() - && emsg_silent == 0 - && !did_wait_return - && s->oa.op_type == OP_NOP); + // 'showmode' is set and messages can be printed + ((p_smd && msg_silent == 0 + // must restart insert mode(ctrl+o or ctrl+l) or we just entered visual + // mode + && (restart_edit != 0 || (VIsual_active + && s->old_pos.lnum == curwin->w_cursor.lnum + && s->old_pos.col == curwin->w_cursor.col)) + // command-line must be cleared or redrawn + && (clear_cmdline || redraw_cmdline) + // some message was printed or scrolled + && (msg_didout || (msg_didany && msg_scroll)) + // it is fine to remove the current message + && !msg_nowait + // the command was the result of direct user input and not a mapping + && KeyTyped) + // must restart insert mode, not in visual mode and error message is + // being shown + || (restart_edit != 0 && !VIsual_active && msg_scroll + && emsg_on_display)) + // no register was used + && s->oa.regname == 0 + && !(s->ca.retval & CA_COMMAND_BUSY) + && stuff_empty() + && typebuf_typed() + && emsg_silent == 0 + && !did_wait_return + && s->oa.op_type == OP_NOP); } static void normal_redraw_mode_message(NormalState *s) @@ -707,7 +714,7 @@ static void normal_get_additional_char(NormalState *s) if (!lit) { // Typing CTRL-K gets a digraph. if (*cp == Ctrl_K && ((nv_cmds[s->idx].cmd_flags & NV_LANG) - || cp == &s->ca.extra_char) + || cp == &s->ca.extra_char) && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL) { s->c = get_digraph(false); if (s->c > 0) { @@ -735,7 +742,7 @@ static void normal_get_additional_char(NormalState *s) s->ca.nchar = s->ca.extra_char; s->idx = find_command(s->ca.cmdchar); } else if ((s->ca.nchar == 'n' || s->ca.nchar == 'N') - && s->ca.cmdchar == 'g') { + && s->ca.cmdchar == 'g') { s->ca.oap->op_type = get_op_type(*cp, NUL); } else if (*cp == Ctrl_BSL) { long towait = (p_ttm >= 0 ? p_ttm : p_tm); @@ -769,7 +776,7 @@ static void normal_get_additional_char(NormalState *s) && (s->c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1)) { s->c = plain_vgetc(); if (!utf_iscomposing(s->c)) { - vungetc(s->c); /* it wasn't, put it back */ + vungetc(s->c); // it wasn't, put it back break; } else if (s->ca.ncharC1 == 0) { s->ca.ncharC1 = s->c; @@ -785,16 +792,26 @@ static void normal_get_additional_char(NormalState *s) static void normal_invert_horizontal(NormalState *s) { switch (s->ca.cmdchar) { - case 'l': s->ca.cmdchar = 'h'; break; - case K_RIGHT: s->ca.cmdchar = K_LEFT; break; - case K_S_RIGHT: s->ca.cmdchar = K_S_LEFT; break; - case K_C_RIGHT: s->ca.cmdchar = K_C_LEFT; break; - case 'h': s->ca.cmdchar = 'l'; break; - case K_LEFT: s->ca.cmdchar = K_RIGHT; break; - case K_S_LEFT: s->ca.cmdchar = K_S_RIGHT; break; - case K_C_LEFT: s->ca.cmdchar = K_C_RIGHT; break; - case '>': s->ca.cmdchar = '<'; break; - case '<': s->ca.cmdchar = '>'; break; + case 'l': + s->ca.cmdchar = 'h'; break; + case K_RIGHT: + s->ca.cmdchar = K_LEFT; break; + case K_S_RIGHT: + s->ca.cmdchar = K_S_LEFT; break; + case K_C_RIGHT: + s->ca.cmdchar = K_C_LEFT; break; + case 'h': + s->ca.cmdchar = 'l'; break; + case K_LEFT: + s->ca.cmdchar = K_RIGHT; break; + case K_S_LEFT: + s->ca.cmdchar = K_S_RIGHT; break; + case K_C_LEFT: + s->ca.cmdchar = K_C_RIGHT; break; + case '>': + s->ca.cmdchar = '<'; break; + case '<': + s->ca.cmdchar = '>'; break; } s->idx = find_command(s->ca.cmdchar); } @@ -808,7 +825,7 @@ static bool normal_get_command_count(NormalState *s) // Note that '0' is a command and not the start of a count, but it's // part of a count after other digits. while ((s->c >= '1' && s->c <= '9') || (s->ca.count0 != 0 - && (s->c == K_DEL || s->c == K_KDEL || s->c == '0'))) { + && (s->c == K_DEL || s->c == K_KDEL || s->c == '0'))) { if (s->c == K_DEL || s->c == K_KDEL) { s->ca.count0 /= 10; del_from_showcmd(4); // delete the digit and ~@% @@ -941,11 +958,11 @@ normal_end: // if still inside a mapping that started in Visual mode). // May switch from Visual to Select mode after CTRL-O command. if (s->oa.op_type == OP_NOP - && ((restart_edit != 0 && !VIsual_active && s->old_mapped_len == 0) - || restart_VIsual_select == 1) - && !(s->ca.retval & CA_COMMAND_BUSY) - && stuff_empty() - && s->oa.regname == 0) { + && ((restart_edit != 0 && !VIsual_active && s->old_mapped_len == 0) + || restart_VIsual_select == 1) + && !(s->ca.retval & CA_COMMAND_BUSY) + && stuff_empty() + && s->oa.regname == 0) { if (restart_VIsual_select == 1) { VIsual_select = true; showmode(); @@ -968,7 +985,7 @@ static int normal_execute(VimState *state, int key) { NormalState *s = (NormalState *)state; s->command_finished = false; - s->ctrl_w = false; /* got CTRL-W command */ + s->ctrl_w = false; // got CTRL-W command s->old_col = curwin->w_curswant; s->c = key; @@ -980,7 +997,7 @@ static int normal_execute(VimState *state, int key) if (restart_edit == 0) { s->old_mapped_len = 0; } else if (s->old_mapped_len || (VIsual_active && s->mapped_len == 0 - && typebuf_maplen() > 0)) { + && typebuf_maplen() > 0)) { s->old_mapped_len = typebuf_maplen(); } @@ -990,7 +1007,7 @@ static int normal_execute(VimState *state, int key) // In Select mode, typed text replaces the selection. if (VIsual_active && VIsual_select && (vim_isprintc(s->c) - || s->c == NL || s->c == CAR || s->c == K_KENTER)) { + || s->c == NL || s->c == CAR || s->c == K_KENTER)) { // Fake a "c"hange command. When "restart_edit" is set (e.g., because // 'insertmode' is set) fake a "d"elete command, Insert mode will // restart automatically. @@ -1008,14 +1025,14 @@ static int normal_execute(VimState *state, int key) s->need_flushbuf = add_to_showcmd(s->c); - while (normal_get_command_count(s)) continue; + while (normal_get_command_count(s)) { continue; } if (s->c == K_EVENT) { // Save the count values so that ca.opcount and ca.count0 are exactly // the same when coming back here after handling K_EVENT. s->oa.prev_opcount = s->ca.opcount; s->oa.prev_count0 = s->ca.count0; - } else if (s->ca.opcount != 0) { + } else if (s->ca.opcount != 0) { // If we're in the middle of an operator (including after entering a // yank buffer with '"') AND we had a count before the operator, then // that count overrides the current value of ca.count0. @@ -1427,11 +1444,12 @@ static void set_vcount_ca(cmdarg_T *cap, bool *set_prevcount) { long count = cap->count0; - /* multiply with cap->opcount the same way as above */ - if (cap->opcount != 0) + // multiply with cap->opcount the same way as above + if (cap->opcount != 0) { count = cap->opcount * (count == 0 ? 1 : count); + } set_vcount(count, count == 0 ? 1 : count, *set_prevcount); - *set_prevcount = false; /* only set v:prevcount once */ + *set_prevcount = false; // only set v:prevcount once } // Handle an operator after Visual mode or when the movement is finished. @@ -1508,12 +1526,11 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) && oap->op_type != OP_FOLDCLOSE && oap->op_type != OP_FOLDCLOSEREC && oap->op_type != OP_FOLDDEL - && oap->op_type != OP_FOLDDELREC - ) { + && oap->op_type != OP_FOLDDELREC) { prep_redo(oap->regname, cap->count0, - get_op_char(oap->op_type), get_extra_op_char(oap->op_type), - oap->motion_force, cap->cmdchar, cap->nchar); - if (cap->cmdchar == '/' || cap->cmdchar == '?') { /* was a search */ + get_op_char(oap->op_type), get_extra_op_char(oap->op_type), + oap->motion_force, cap->cmdchar, cap->nchar); + if (cap->cmdchar == '/' || cap->cmdchar == '?') { // was a search /* * If 'cpoptions' does not contain 'r', insert the search * pattern to really repeat the same command. @@ -1541,8 +1558,9 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) * redo_VIsual_line_count and redo_VIsual_vcol. */ oap->start = curwin->w_cursor; curwin->w_cursor.lnum += redo_VIsual_line_count - 1; - if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) + if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) { curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; + } VIsual_mode = redo_VIsual_mode; if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v') { if (VIsual_mode == 'v') { @@ -1550,8 +1568,9 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) validate_virtcol(); curwin->w_curswant = curwin->w_virtcol + redo_VIsual_vcol - 1; - } else + } else { curwin->w_curswant = redo_VIsual_vcol; + } } else { curwin->w_curswant = MAXCOL; } @@ -1561,7 +1580,7 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) cap->count1 = (cap->count0 == 0 ? 1 : cap->count0); } else if (VIsual_active) { if (!gui_yank) { - /* Save the current VIsual area for '< and '> marks, and "gv" */ + // Save the current VIsual area for '< and '> marks, and "gv" curbuf->b_visual.vi_start = VIsual; curbuf->b_visual.vi_end = curwin->w_cursor; curbuf->b_visual.vi_mode = VIsual_mode; @@ -1607,10 +1626,11 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) * to the end of the operated text. w_cursor is equal to oap->start. */ if (lt(oap->start, curwin->w_cursor)) { - /* Include folded lines completely. */ + // Include folded lines completely. if (!VIsual_active) { - if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL)) + if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL)) { oap->start.col = 0; + } if ((curwin->w_cursor.col > 0 || oap->inclusive || oap->motion_type == kMTLineWise) @@ -1645,7 +1665,7 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) check_pos(curwin->w_buffer, &oap->end); oap->line_count = oap->end.lnum - oap->start.lnum + 1; - /* Set "virtual_op" before resetting VIsual_active. */ + // Set "virtual_op" before resetting VIsual_active. virtual_op = virtual_active(); if (VIsual_active || redo_VIsual_busy) { @@ -1657,19 +1677,22 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) * size of the Visual text */ resel_VIsual_mode = VIsual_mode; - if (curwin->w_curswant == MAXCOL) + if (curwin->w_curswant == MAXCOL) { resel_VIsual_vcol = MAXCOL; - else { - if (VIsual_mode != Ctrl_V) + } else { + if (VIsual_mode != Ctrl_V) { getvvcol(curwin, &(oap->end), - NULL, NULL, &oap->end_vcol); + NULL, NULL, &oap->end_vcol); + } if (VIsual_mode == Ctrl_V || oap->line_count <= 1) { - if (VIsual_mode != Ctrl_V) + if (VIsual_mode != Ctrl_V) { getvvcol(curwin, &(oap->start), - &oap->start_vcol, NULL, NULL); + &oap->start_vcol, NULL, NULL); + } resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1; - } else + } else { resel_VIsual_vcol = oap->end_vcol; + } } resel_VIsual_line_count = oap->line_count; } @@ -1684,8 +1707,7 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) && oap->op_type != OP_FOLDCLOSEREC && oap->op_type != OP_FOLDDEL && oap->op_type != OP_FOLDDELREC - && oap->motion_force == NUL - ) { + && oap->motion_force == NUL) { /* Prepare for redoing. Only use the nchar field for "r", * otherwise it might be the second char of the operator. */ if (cap->cmdchar == 'g' && (cap->nchar == 'n' @@ -1725,8 +1747,7 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) } else if (VIsual_mode == 'v') { oap->motion_type = kMTCharWise; if (*ml_get_pos(&(oap->end)) == NUL - && (include_line_break || !virtual_op) - ) { + && (include_line_break || !virtual_op)) { oap->inclusive = false; // Try to include the newline, unless it's an operator // that works on lines only. @@ -1828,22 +1849,24 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) oap->inclusive = true; } } - } else + } else { oap->end_adjusted = false; + } switch (oap->op_type) { case OP_LSHIFT: case OP_RSHIFT: op_shift(oap, true, - oap->is_VIsual ? (int)cap->count1 : - 1); + oap->is_VIsual ? (int)cap->count1 : + 1); auto_format(false, true); break; case OP_JOIN_NS: case OP_JOIN: - if (oap->line_count < 2) + if (oap->line_count < 2) { oap->line_count = 2; + } if (curwin->w_cursor.lnum + oap->line_count - 1 > curbuf->b_ml.ml_line_count) { beep_flush(); @@ -1855,7 +1878,7 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) break; case OP_DELETE: - VIsual_reselect = false; /* don't reselect now */ + VIsual_reselect = false; // don't reselect now if (empty_region_error) { vim_beep(BO_OPER); CancelRedo(); @@ -1885,7 +1908,7 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) break; case OP_CHANGE: - VIsual_reselect = false; /* don't reselect now */ + VIsual_reselect = false; // don't reselect now if (empty_region_error) { vim_beep(BO_OPER); CancelRedo(); @@ -1894,10 +1917,11 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) * remember it to make 'insertmode' work with mappings for * Visual mode. But do this only once and not when typed and * 'insertmode' isn't set. */ - if (p_im || !KeyTyped) + if (p_im || !KeyTyped) { restart_edit_save = restart_edit; - else + } else { restart_edit_save = 0; + } restart_edit = 0; // Restore linebreak, so that when the user edits it looks as before. @@ -1905,10 +1929,12 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) // Reset finish_op now, don't want it set inside edit(). finish_op = false; - if (op_change(oap)) /* will call edit() */ + if (op_change(oap)) { // will call edit() cap->retval |= CA_COMMAND_BUSY; - if (restart_edit == 0) + } + if (restart_edit == 0) { restart_edit = restart_edit_save; + } } break; @@ -1932,8 +1958,8 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) break; } op_reindent(oap, - *curbuf->b_p_inde != NUL ? get_expr_indent : - get_c_indent); + *curbuf->b_p_inde != NUL ? get_expr_indent : + get_c_indent); break; } @@ -1947,8 +1973,9 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) if (empty_region_error) { vim_beep(BO_OPER); CancelRedo(); - } else + } else { op_tilde(oap); + } check_cursor_col(); break; @@ -1965,7 +1992,7 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) break; case OP_FORMAT2: - op_format(oap, true); /* use internal function */ + op_format(oap, true); // use internal function break; case OP_FUNCTION: @@ -1977,7 +2004,7 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) case OP_INSERT: case OP_APPEND: - VIsual_reselect = false; /* don't reselect now */ + VIsual_reselect = false; // don't reselect now if (empty_region_error) { vim_beep(BO_OPER); CancelRedo(); @@ -2009,7 +2036,7 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) break; case OP_REPLACE: - VIsual_reselect = false; /* don't reselect now */ + VIsual_reselect = false; // don't reselect now if (empty_region_error) { vim_beep(BO_OPER); CancelRedo(); @@ -2141,10 +2168,10 @@ static void op_function(const oparg_T *oap) { const TriState save_virtual_op = virtual_op; - if (*p_opfunc == NUL) + if (*p_opfunc == NUL) { EMSG(_("E774: 'operatorfunc' is empty")); - else { - /* Set '[ and '] marks to text to be operated on. */ + } else { + // Set '[ and '] marks to text to be operated on. curbuf->b_op_start = oap->start; curbuf->b_op_end = oap->end; if (oap->motion_type != kMTLineWise && !oap->inclusive) { @@ -2157,10 +2184,10 @@ static void op_function(const oparg_T *oap) argv[1].v_type = VAR_UNKNOWN; argv[0].vval.v_string = (char_u *)(((const char *const[]) { - [kMTBlockWise] = "block", - [kMTLineWise] = "line", - [kMTCharWise] = "char", - })[oap->motion_type]); + [kMTBlockWise] = "block", + [kMTLineWise] = "line", + [kMTCharWise] = "char", + })[oap->motion_type]); // Reset virtual_op so that 'virtualedit' can be changed in the // function. @@ -2178,70 +2205,66 @@ static void move_tab_to_mouse(void) { int tabnr = tab_page_click_defs[mouse_col].tabnr; if (tabnr <= 0) { - tabpage_move(9999); + tabpage_move(9999); } else if (tabnr < tabpage_index(curtab)) { - tabpage_move(tabnr - 1); + tabpage_move(tabnr - 1); } else { - tabpage_move(tabnr); + tabpage_move(tabnr); } } -/* - * Do the appropriate action for the current mouse click in the current mode. - * Not used for Command-line mode. - * - * Normal Mode: - * event modi- position visual change action - * fier cursor window - * left press - yes end yes - * left press C yes end yes "^]" (2) - * left press S yes end yes "*" (2) - * left drag - yes start if moved no - * left relse - yes start if moved no - * middle press - yes if not active no put register - * middle press - yes if active no yank and put - * right press - yes start or extend yes - * right press S yes no change yes "#" (2) - * right drag - yes extend no - * right relse - yes extend no - * - * Insert or Replace Mode: - * event modi- position visual change action - * fier cursor window - * left press - yes (cannot be active) yes - * left press C yes (cannot be active) yes "CTRL-O^]" (2) - * left press S yes (cannot be active) yes "CTRL-O*" (2) - * left drag - yes start or extend (1) no CTRL-O (1) - * left relse - yes start or extend (1) no CTRL-O (1) - * middle press - no (cannot be active) no put register - * right press - yes start or extend yes CTRL-O - * right press S yes (cannot be active) yes "CTRL-O#" (2) - * - * (1) only if mouse pointer moved since press - * (2) only if click is in same buffer - * - * Return true if start_arrow() should be called for edit mode. - */ -bool -do_mouse ( - oparg_T *oap, /* operator argument, can be NULL */ - int c, /* K_LEFTMOUSE, etc */ - int dir, /* Direction to 'put' if necessary */ - long count, - bool fixindent /* PUT_FIXINDENT if fixing indent necessary */ -) +/// Do the appropriate action for the current mouse click in the current mode. +/// Not used for Command-line mode. +/// +/// Normal Mode: +/// event modi- position visual change action +/// fier cursor window +/// left press - yes end yes +/// left press C yes end yes "^]" (2) +/// left press S yes end yes "*" (2) +/// left drag - yes start if moved no +/// left relse - yes start if moved no +/// middle press - yes if not active no put register +/// middle press - yes if active no yank and put +/// right press - yes start or extend yes +/// right press S yes no change yes "#" (2) +/// right drag - yes extend no +/// right relse - yes extend no +/// +/// Insert or Replace Mode: +/// event modi- position visual change action +/// fier cursor window +/// left press - yes (cannot be active) yes +/// left press C yes (cannot be active) yes "CTRL-O^]" (2) +/// left press S yes (cannot be active) yes "CTRL-O*" (2) +/// left drag - yes start or extend (1) no CTRL-O (1) +/// left relse - yes start or extend (1) no CTRL-O (1) +/// middle press - no (cannot be active) no put register +/// right press - yes start or extend yes CTRL-O +/// right press S yes (cannot be active) yes "CTRL-O#" (2) +/// +/// (1) only if mouse pointer moved since press +/// (2) only if click is in same buffer +/// +/// @param oap operator argument, can be NULL +/// @param c K_LEFTMOUSE, etc +/// @param dir Direction to 'put' if necessary +/// @param fixindent PUT_FIXINDENT if fixing indent necessary +/// +/// @return true if start_arrow() should be called for edit mode. +bool do_mouse(oparg_T *oap, int c, int dir, long count, bool fixindent) { - static bool got_click = false; /* got a click some time back */ + static bool got_click = false; // got a click some time back - int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */ - bool is_click; /* If false it's a drag or release event */ - bool is_drag; /* If true it's a drag event */ - int jump_flags = 0; /* flags for jump_to_mouse() */ + int which_button; // MOUSE_LEFT, _MIDDLE or _RIGHT + bool is_click; // If false it's a drag or release event + bool is_drag; // If true it's a drag event + int jump_flags = 0; // flags for jump_to_mouse() pos_T start_visual; - bool moved; /* Has cursor moved? */ - bool in_status_line; /* mouse in status line */ - static bool in_tab_line = false; /* mouse clicked in tab line */ - bool in_sep_line; /* mouse in vertical separator line */ + bool moved; // Has cursor moved? + bool in_status_line; // mouse in status line + static bool in_tab_line = false; // mouse clicked in tab line + bool in_sep_line; // mouse in vertical separator line int c1, c2; pos_T save_cursor; win_T *old_curwin = curwin; @@ -2269,8 +2292,9 @@ do_mouse ( /* Need to get the character, peeking doesn't get the actual * one. */ nc = safe_vgetc(); - if (c == nc) + if (c == nc) { continue; + } vungetc(nc); mouse_grid = save_mouse_grid; mouse_row = save_mouse_row; @@ -2288,12 +2312,13 @@ do_mouse ( /* * Ignore drag and release events if we didn't get a click. */ - if (is_click) + if (is_click) { got_click = true; - else { - if (!got_click) /* didn't get click, ignore */ + } else { + if (!got_click) { // didn't get click, ignore return false; - if (!is_drag) { /* release, reset got_click */ + } + if (!is_drag) { // release, reset got_click got_click = false; if (in_tab_line) { in_tab_line = false; @@ -2307,20 +2332,23 @@ do_mouse ( * CTRL right mouse button does CTRL-T */ if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT) { - if (State & INSERT) + if (State & INSERT) { stuffcharReadbuff(Ctrl_O); - if (count > 1) + } + if (count > 1) { stuffnumReadbuff(count); + } stuffcharReadbuff(Ctrl_T); - got_click = false; /* ignore drag&release now */ + got_click = false; // ignore drag&release now return false; } /* * CTRL only works with left mouse button */ - if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT) + if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT) { return false; + } /* * When a modifier is down, ignore drag and release events, as well as @@ -2337,22 +2365,24 @@ do_mouse ( && which_button == MOUSE_LEFT) && !((mod_mask & MOD_MASK_ALT) && !mouse_model_popup() - && which_button == MOUSE_RIGHT) - ) + && which_button == MOUSE_RIGHT)) { return false; + } /* * If the button press was used as the movement command for an operator * (eg "d"), or it is the middle button that is held down, ignore * drag/release events. */ - if (!is_click && which_button == MOUSE_MIDDLE) + if (!is_click && which_button == MOUSE_MIDDLE) { return false; + } - if (oap != NULL) + if (oap != NULL) { regname = oap->regname; - else + } else { regname = 0; + } /* * Middle mouse button does a 'put' of the selected text @@ -2386,8 +2416,9 @@ do_mouse ( /* * The rest is below jump_to_mouse() */ - } else if ((State & INSERT) == 0) + } else if ((State & INSERT) == 0) { return false; + } /* * Middle click in insert mode doesn't move the mouse, just insert the @@ -2409,7 +2440,7 @@ do_mouse ( do_put(regname, NULL, BACKWARD, 1L, (fixindent ? PUT_FIXINDENT : 0) | PUT_CURSEND); - /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */ + // Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r AppendCharToRedobuff(Ctrl_R); AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O); AppendCharToRedobuff(regname == 0 ? '"' : regname); @@ -2419,9 +2450,10 @@ do_mouse ( } } - /* When dragging or button-up stay in the same window. */ - if (!is_click) + // When dragging or button-up stay in the same window. + if (!is_click) { jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE; + } start_visual.lnum = 0; @@ -2434,17 +2466,17 @@ do_mouse ( return false; } - /* click in a tab selects that tab page */ + // click in a tab selects that tab page if (is_click && cmdwin_type == 0 && mouse_col < Columns) { in_tab_line = true; c1 = tab_page_click_defs[mouse_col].tabnr; switch (tab_page_click_defs[mouse_col].type) { - case kStlClickDisabled: { + case kStlClickDisabled: { break; } - case kStlClickTabClose: { + case kStlClickTabClose: { tabpage_T *tp; // Close the current or specified tab page. @@ -2462,7 +2494,7 @@ do_mouse ( } break; } - case kStlClickTabSwitch: { + case kStlClickTabSwitch: { if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK) { // double click opens new page end_visual_mode(); @@ -2480,13 +2512,13 @@ do_mouse ( } break; } - case kStlClickFuncRun: { + case kStlClickFuncRun: { typval_T argv[] = { { .v_lock = VAR_FIXED, .v_type = VAR_NUMBER, .vval = { - .v_number = (varnumber_T) tab_page_click_defs[mouse_col].tabnr + .v_number = (varnumber_T)tab_page_click_defs[mouse_col].tabnr }, }, { @@ -2508,7 +2540,7 @@ do_mouse ( { .v_lock = VAR_FIXED, .v_type = VAR_STRING, - .vval = { .v_string = (char_u *) (which_button == MOUSE_LEFT + .vval = { .v_string = (char_u *)(which_button == MOUSE_LEFT ? "l" : which_button == MOUSE_RIGHT ? "r" @@ -2521,10 +2553,10 @@ do_mouse ( .v_type = VAR_STRING, .vval = { .v_string = (char_u[]) { - (char_u) (mod_mask & MOD_MASK_SHIFT ? 's' : ' '), - (char_u) (mod_mask & MOD_MASK_CTRL ? 'c' : ' '), - (char_u) (mod_mask & MOD_MASK_ALT ? 'a' : ' '), - (char_u) (mod_mask & MOD_MASK_META ? 'm' : ' '), + (char_u)(mod_mask & MOD_MASK_SHIFT ? 's' : ' '), + (char_u)(mod_mask & MOD_MASK_CTRL ? 'c' : ' '), + (char_u)(mod_mask & MOD_MASK_ALT ? 'a' : ' '), + (char_u)(mod_mask & MOD_MASK_META ? 'm' : ' '), NUL } }, @@ -2577,8 +2609,9 @@ do_mouse ( if (is_click) { /* stop Visual mode for a left click in a window, but not when * on a status line */ - if (VIsual_active) + if (VIsual_active) { jump_flags |= MOUSE_MAY_STOP_VIS; + } } else { jump_flags |= MOUSE_MAY_VIS; } @@ -2610,9 +2643,10 @@ do_mouse ( oap->motion_type = kMTCharWise; } - /* When releasing the button let jump_to_mouse() know. */ - if (!is_click && !is_drag) + // When releasing the button let jump_to_mouse() know. + if (!is_click && !is_drag) { jump_flags |= MOUSE_RELEASED; + } /* * JUMP! @@ -2628,8 +2662,9 @@ do_mouse ( /* When jumping to another window, clear a pending operator. That's a bit * friendlier than beeping and not jumping to that window. */ - if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP) + if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP) { clearop(oap); + } if (mod_mask == 0 && !is_drag @@ -2659,16 +2694,17 @@ do_mouse ( } } - /* When dragging the mouse above the window, scroll down. */ + // When dragging the mouse above the window, scroll down. if (is_drag && mouse_row < 0 && !in_status_line) { scroll_redraw(false, 1L); mouse_row = 0; } - if (start_visual.lnum) { /* right click in visual mode */ - /* When ALT is pressed make Visual mode blockwise. */ - if (mod_mask & MOD_MASK_ALT) + if (start_visual.lnum) { // right click in visual mode + // When ALT is pressed make Visual mode blockwise. + if (mod_mask & MOD_MASK_ALT) { VIsual_mode = Ctrl_V; + } /* * In Visual-block mode, divide the area in four, pick up the corner @@ -2676,55 +2712,58 @@ do_mouse ( */ if (VIsual_mode == Ctrl_V) { getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol); - if (curwin->w_curswant > (leftcol + rightcol) / 2) + if (curwin->w_curswant > (leftcol + rightcol) / 2) { end_visual.col = leftcol; - else + } else { end_visual.col = rightcol; + } if (curwin->w_cursor.lnum >= (start_visual.lnum + end_visual.lnum) / 2) { end_visual.lnum = start_visual.lnum; } - /* move VIsual to the right column */ - start_visual = curwin->w_cursor; /* save the cursor pos */ + // move VIsual to the right column + start_visual = curwin->w_cursor; // save the cursor pos curwin->w_cursor = end_visual; coladvance(end_visual.col); VIsual = curwin->w_cursor; - curwin->w_cursor = start_visual; /* restore the cursor */ + curwin->w_cursor = start_visual; // restore the cursor } else { /* * If the click is before the start of visual, change the start. * If the click is after the end of visual, change the end. If * the click is inside the visual, change the closest side. */ - if (lt(curwin->w_cursor, start_visual)) + if (lt(curwin->w_cursor, start_visual)) { VIsual = end_visual; - else if (lt(end_visual, curwin->w_cursor)) + } else if (lt(end_visual, curwin->w_cursor)) { VIsual = start_visual; - else { - /* In the same line, compare column number */ + } else { + // In the same line, compare column number if (end_visual.lnum == start_visual.lnum) { if (curwin->w_cursor.col - start_visual.col > - end_visual.col - curwin->w_cursor.col) + end_visual.col - curwin->w_cursor.col) { VIsual = start_visual; - else + } else { VIsual = end_visual; + } } - /* In different lines, compare line number */ + // In different lines, compare line number else { diff = (curwin->w_cursor.lnum - start_visual.lnum) - (end_visual.lnum - curwin->w_cursor.lnum); - if (diff > 0) /* closest to end */ + if (diff > 0) { // closest to end VIsual = start_visual; - else if (diff < 0) /* closest to start */ + } else if (diff < 0) { // closest to start VIsual = end_visual; - else { /* in the middle line */ + } else { // in the middle line if (curwin->w_cursor.col < - (start_visual.col + end_visual.col) / 2) + (start_visual.col + end_visual.col) / 2) { VIsual = end_visual; - else + } else { VIsual = start_visual; + } } } } @@ -2733,8 +2772,9 @@ do_mouse ( /* * If Visual mode started in insert mode, execute "CTRL-O" */ - else if ((State & INSERT) && VIsual_active) + else if ((State & INSERT) && VIsual_active) { stuffcharReadbuff(Ctrl_O); + } /* * Middle mouse click: Put text before cursor. @@ -2744,10 +2784,12 @@ do_mouse ( regname = '*'; } if (yank_register_mline(regname)) { - if (mouse_past_bottom) + if (mouse_past_bottom) { dir = FORWARD; - } else if (mouse_past_eol) + } + } else if (mouse_past_eol) { dir = FORWARD; + } if (fixindent) { c1 = (dir == BACKWARD) ? '[' : ']'; @@ -2762,8 +2804,9 @@ do_mouse ( * Remember where the paste started, so in edit() Insstart can be set * to this position */ - if (restart_edit != 0) + if (restart_edit != 0) { where_paste_started = curwin->w_cursor; + } do_put(regname, NULL, dir, count, (fixindent ? PUT_FIXINDENT : 0)| PUT_CURSEND); } @@ -2789,10 +2832,11 @@ do_mouse ( && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)) { - if (State & INSERT) + if (State & INSERT) { stuffcharReadbuff(Ctrl_O); + } stuffcharReadbuff(Ctrl_RSB); - got_click = false; /* ignore drag&release now */ + got_click = false; // ignore drag&release now } /* * Shift-Mouse click searches for the next occurrence of the word under @@ -2800,15 +2844,16 @@ do_mouse ( */ else if ((mod_mask & MOD_MASK_SHIFT)) { if (State & INSERT - || (VIsual_active && VIsual_select) - ) + || (VIsual_active && VIsual_select)) { stuffcharReadbuff(Ctrl_O); - if (which_button == MOUSE_LEFT) + } + if (which_button == MOUSE_LEFT) { stuffcharReadbuff('*'); - else /* MOUSE_RIGHT */ + } else { // MOUSE_RIGHT stuffcharReadbuff('#'); + } } - /* Handle double clicks, unless on status line */ + // Handle double clicks, unless on status line else if (in_status_line) { } else if (in_sep_line) { } else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))) { @@ -2820,20 +2865,22 @@ do_mouse ( orig_cursor = VIsual; VIsual_active = true; VIsual_reselect = true; - /* start Select mode if 'selectmode' contains "mouse" */ + // start Select mode if 'selectmode' contains "mouse" may_start_select('o'); setmouse(); } if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK) { - /* Double click with ALT pressed makes it blockwise. */ - if (mod_mask & MOD_MASK_ALT) + // Double click with ALT pressed makes it blockwise. + if (mod_mask & MOD_MASK_ALT) { VIsual_mode = Ctrl_V; - else + } else { VIsual_mode = 'v'; - } else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK) + } + } else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK) { VIsual_mode = 'V'; - else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK) + } else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK) { VIsual_mode = Ctrl_V; + } } /* * A double click selects a word or a block. @@ -2847,8 +2894,9 @@ do_mouse ( * not a word character, try finding a match and select a (), * {}, [], #if/#endif, etc. block. */ end_visual = curwin->w_cursor; - while (gc = gchar_pos(&end_visual), ascii_iswhite(gc)) + while (gc = gchar_pos(&end_visual), ascii_iswhite(gc)) { inc(&end_visual); + } if (oap != NULL) { oap->motion_type = kMTCharWise; } @@ -2878,28 +2926,32 @@ do_mouse ( find_end_of_word(&VIsual); } else { find_start_of_word(&VIsual); - if (*p_sel == 'e' && *get_cursor_pos_ptr() != NUL) + if (*p_sel == 'e' && *get_cursor_pos_ptr() != NUL) { curwin->w_cursor.col += (*mb_ptr2len)(get_cursor_pos_ptr()); + } find_end_of_word(&curwin->w_cursor); } } curwin->w_set_curswant = true; } - if (is_click) - redraw_curbuf_later(INVERTED); /* update the inversion */ + if (is_click) { + redraw_curbuf_later(INVERTED); // update the inversion + } } else if (VIsual_active && !old_active) { - if (mod_mask & MOD_MASK_ALT) + if (mod_mask & MOD_MASK_ALT) { VIsual_mode = Ctrl_V; - else + } else { VIsual_mode = 'v'; + } } - /* If Visual mode changed show it later. */ + // If Visual mode changed show it later. if ((!VIsual_active && old_active && mode_displayed) || (VIsual_active && p_smd && msg_silent == 0 - && (!old_active || VIsual_mode != old_mode))) + && (!old_active || VIsual_mode != old_mode))) { redraw_cmdline = true; + } return moved; } @@ -2945,8 +2997,9 @@ static void find_end_of_word(pos_T *pos) while (line[pos->col] != NUL) { col = pos->col + (*mb_ptr2len)(line + pos->col); if (get_mouse_class(line + col) != cclass) { - if (*p_sel == 'e') + if (*p_sel == 'e') { pos->col = col; + } break; } pos->col = col; @@ -2980,8 +3033,9 @@ static int get_mouse_class(char_u *p) * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each * character is in its own class. */ - if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL) + if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL) { return 1; + } return c; } @@ -2992,19 +3046,19 @@ static int get_mouse_class(char_u *p) */ void end_visual_mode(void) { - VIsual_active = false; setmouse(); mouse_dragging = 0; - /* Save the current VIsual area for '< and '> marks, and "gv" */ + // Save the current VIsual area for '< and '> marks, and "gv" curbuf->b_visual.vi_mode = VIsual_mode; curbuf->b_visual.vi_start = VIsual; curbuf->b_visual.vi_end = curwin->w_cursor; curbuf->b_visual.vi_curswant = curwin->w_curswant; curbuf->b_visual_mode_eval = VIsual_mode; - if (!virtual_active()) + if (!virtual_active()) { curwin->w_cursor.coladd = 0; + } may_clear_cmdline(); @@ -3018,7 +3072,7 @@ void reset_VIsual_and_resel(void) { if (VIsual_active) { end_visual_mode(); - redraw_curbuf_later(INVERTED); /* delete the inversion later */ + redraw_curbuf_later(INVERTED); // delete the inversion later } VIsual_reselect = false; } @@ -3030,7 +3084,7 @@ void reset_VIsual(void) { if (VIsual_active) { end_visual_mode(); - redraw_curbuf_later(INVERTED); /* delete the inversion later */ + redraw_curbuf_later(INVERTED); // delete the inversion later VIsual_reselect = false; } } @@ -3041,11 +3095,8 @@ void reset_VIsual(void) // "dir" is FORWARD or BACKWARD, the direction of searching. // "*colp" is in/decremented if "ptr[-dir]" should also be included. // "bnp" points to a counter for square brackets. -static bool find_is_eval_item( - const char_u *const ptr, - int *const colp, - int *const bnp, - const int dir) +static bool find_is_eval_item(const char_u *const ptr, int *const colp, int *const bnp, + const int dir) { // Accept everything inside []. if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD)) { @@ -3098,17 +3149,12 @@ size_t find_ident_under_cursor(char_u **text, int find_type) curwin->w_cursor.col, text, NULL, find_type); } -/* - * Like find_ident_under_cursor(), but for any window and any position. - * However: Uses 'iskeyword' from the current window!. - */ -size_t find_ident_at_pos( - win_T *wp, - linenr_T lnum, - colnr_T startcol, - char_u **text, - int *textcol, // column where "text" starts, can be NULL - int find_type) +/// Like find_ident_under_cursor(), but for any window and any position. +/// However: Uses 'iskeyword' from the current window!. +/// +/// @param textcol column where "text" starts, can be NULL +size_t find_ident_at_pos(win_T *wp, linenr_T lnum, colnr_T startcol, char_u **text, int *textcol, + int find_type) FUNC_ATTR_NONNULL_ARG(1, 4) { int col = 0; // init to shut up GCC @@ -3214,7 +3260,7 @@ size_t find_ident_at_pos( static void prep_redo_cmd(cmdarg_T *cap) { prep_redo(cap->oap->regname, cap->count0, - NUL, cap->cmdchar, NUL, NUL, cap->nchar); + NUL, cap->cmdchar, NUL, NUL, cap->nchar); } /* @@ -3224,23 +3270,29 @@ static void prep_redo_cmd(cmdarg_T *cap) static void prep_redo(int regname, long num, int cmd1, int cmd2, int cmd3, int cmd4, int cmd5) { ResetRedobuff(); - if (regname != 0) { /* yank from specified buffer */ + if (regname != 0) { // yank from specified buffer AppendCharToRedobuff('"'); AppendCharToRedobuff(regname); } - if (num) + if (num) { AppendNumberToRedobuff(num); + } - if (cmd1 != NUL) + if (cmd1 != NUL) { AppendCharToRedobuff(cmd1); - if (cmd2 != NUL) + } + if (cmd2 != NUL) { AppendCharToRedobuff(cmd2); - if (cmd3 != NUL) + } + if (cmd3 != NUL) { AppendCharToRedobuff(cmd3); - if (cmd4 != NUL) + } + if (cmd4 != NUL) { AppendCharToRedobuff(cmd4); - if (cmd5 != NUL) + } + if (cmd5 != NUL) { AppendCharToRedobuff(cmd5); + } } /* @@ -3250,8 +3302,9 @@ static void prep_redo(int regname, long num, int cmd1, int cmd2, int cmd3, int c */ static bool checkclearop(oparg_T *oap) { - if (oap->op_type == OP_NOP) + if (oap->op_type == OP_NOP) { return false; + } clearopbeep(oap); return true; } @@ -3264,9 +3317,9 @@ static bool checkclearop(oparg_T *oap) static bool checkclearopq(oparg_T *oap) { if (oap->op_type == OP_NOP - && !VIsual_active - ) + && !VIsual_active) { return false; + } clearopbeep(oap); return true; } @@ -3292,12 +3345,18 @@ static void clearopbeep(oparg_T *oap) static void unshift_special(cmdarg_T *cap) { switch (cap->cmdchar) { - case K_S_RIGHT: cap->cmdchar = K_RIGHT; break; - case K_S_LEFT: cap->cmdchar = K_LEFT; break; - case K_S_UP: cap->cmdchar = K_UP; break; - case K_S_DOWN: cap->cmdchar = K_DOWN; break; - case K_S_HOME: cap->cmdchar = K_HOME; break; - case K_S_END: cap->cmdchar = K_END; break; + case K_S_RIGHT: + cap->cmdchar = K_RIGHT; break; + case K_S_LEFT: + cap->cmdchar = K_LEFT; break; + case K_S_UP: + cap->cmdchar = K_UP; break; + case K_S_DOWN: + cap->cmdchar = K_DOWN; break; + case K_S_HOME: + cap->cmdchar = K_HOME; break; + case K_S_END: + cap->cmdchar = K_END; break; } cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask); } @@ -3317,15 +3376,16 @@ static void may_clear_cmdline(void) // Routines for displaying a partly typed command # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30 static char_u showcmd_buf[SHOWCMD_BUFLEN]; -static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */ +static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd() static bool showcmd_is_clear = true; static bool showcmd_visual = false; void clear_showcmd(void) { - if (!p_sc) + if (!p_sc) { return; + } if (VIsual_active && !char_avail()) { int cursor_bot = lt(VIsual, curwin->w_cursor); @@ -3333,7 +3393,7 @@ void clear_showcmd(void) colnr_T leftcol, rightcol; linenr_T top, bot; - /* Show the size of the Visual area. */ + // Show the size of the Visual area. if (cursor_bot) { top = VIsual.lnum; bot = curwin->w_cursor.lnum; @@ -3349,7 +3409,7 @@ void clear_showcmd(void) if (VIsual_mode == Ctrl_V) { char_u *saved_sbr = p_sbr; - /* Make 'sbr' empty for a moment to get the correct size. */ + // Make 'sbr' empty for a moment to get the correct size. p_sbr = empty_option; getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol); p_sbr = saved_sbr; @@ -3375,16 +3435,17 @@ void clear_showcmd(void) if (l == 0) { ++bytes; ++chars; - break; /* end of line */ + break; // end of line } bytes += l; ++chars; s += l; } - if (bytes == chars) + if (bytes == chars) { sprintf((char *)showcmd_buf, "%d", chars); - else + } else { sprintf((char *)showcmd_buf, "%d-%d", chars, bytes); + } } int limit = ui_has(kUIMessages) ? SHOWCMD_BUFLEN-1 : SHOWCMD_COLS; showcmd_buf[limit] = NUL; // truncate @@ -3393,9 +3454,10 @@ void clear_showcmd(void) showcmd_buf[0] = NUL; showcmd_visual = false; - /* Don't actually display something if there is nothing to clear. */ - if (showcmd_is_clear) + // Don't actually display something if there is nothing to clear. + if (showcmd_is_clear) { return; + } } display_showcmd(); @@ -3421,23 +3483,28 @@ bool add_to_showcmd(int c) 0 }; - if (!p_sc || msg_silent != 0) + if (!p_sc || msg_silent != 0) { return false; + } if (showcmd_visual) { showcmd_buf[0] = NUL; showcmd_visual = false; } - /* Ignore keys that are scrollbar updates and mouse clicks */ - if (IS_SPECIAL(c)) - for (i = 0; ignore[i] != 0; ++i) - if (ignore[i] == c) + // Ignore keys that are scrollbar updates and mouse clicks + if (IS_SPECIAL(c)) { + for (i = 0; ignore[i] != 0; ++i) { + if (ignore[i] == c) { return false; + } + } + } p = transchar(c); - if (*p == ' ') + if (*p == ' ') { STRCPY(p, "<20>"); + } size_t old_len = STRLEN(showcmd_buf); size_t extra_len = STRLEN(p); size_t limit = ui_has(kUIMessages) ? SHOWCMD_BUFLEN-1 : SHOWCMD_COLS; @@ -3447,8 +3514,9 @@ bool add_to_showcmd(int c) } STRCAT(showcmd_buf, p); - if (char_avail()) + if (char_avail()) { return false; + } display_showcmd(); @@ -3468,16 +3536,19 @@ static void del_from_showcmd(int len) { int old_len; - if (!p_sc) + if (!p_sc) { return; + } old_len = (int)STRLEN(showcmd_buf); - if (len > old_len) + if (len > old_len) { len = old_len; + } showcmd_buf[old_len - len] = NUL; - if (!char_avail()) + if (!char_avail()) { display_showcmd(); + } } /* @@ -3486,14 +3557,16 @@ static void del_from_showcmd(int len) */ void push_showcmd(void) { - if (p_sc) + if (p_sc) { STRCPY(old_showcmd_buf, showcmd_buf); + } } void pop_showcmd(void) { - if (!p_sc) + if (!p_sc) { return; + } STRCPY(showcmd_buf, old_showcmd_buf); @@ -3551,9 +3624,9 @@ void do_check_scrollbind(bool check) if (check && curwin->w_p_scb) { /* If a ":syncbind" command was just used, don't scroll, only reset * the values. */ - if (did_syncbind) + if (did_syncbind) { did_syncbind = false; - else if (curwin == old_curwin) { + } else if (curwin == old_curwin) { /* * Synchronize other windows, as necessary according to * 'scrollbind'. Don't do this after an ":edit" command, except @@ -3566,9 +3639,9 @@ void do_check_scrollbind(bool check) || curwin->w_topfill != old_topfill || curwin->w_leftcol != old_leftcol)) { check_scrollbind(curwin->w_topline - old_topline, - (long)(curwin->w_leftcol - old_leftcol)); + (long)(curwin->w_leftcol - old_leftcol)); } - } else if (vim_strchr(p_sbo, 'j')) { /* jump flag set in 'scrollopt' */ + } else if (vim_strchr(p_sbo, 'j')) { // jump flag set in 'scrollopt' /* * When switching between windows, make sure that the relative * vertical offset is valid for the new window. The relative @@ -3622,7 +3695,7 @@ void check_scrollbind(linenr_T topline_diff, long leftcol_diff) FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { curwin = wp; curbuf = curwin->w_buffer; - /* skip original window and windows with 'noscrollbind' */ + // skip original window and windows with 'noscrollbind' if (curwin == old_curwin || !curwin->w_p_scb) { continue; } @@ -3635,16 +3708,19 @@ void check_scrollbind(linenr_T topline_diff, long leftcol_diff) } else { curwin->w_scbind_pos += topline_diff; topline = curwin->w_scbind_pos; - if (topline > curbuf->b_ml.ml_line_count) + if (topline > curbuf->b_ml.ml_line_count) { topline = curbuf->b_ml.ml_line_count; - if (topline < 1) + } + if (topline < 1) { topline = 1; + } y = topline - curwin->w_topline; - if (y > 0) + if (y > 0) { scrollup(y, false); - else + } else { scrolldown(-y, false); + } } redraw_later(curwin, VALID); @@ -3677,7 +3753,7 @@ void check_scrollbind(linenr_T topline_diff, long leftcol_diff) */ static void nv_ignore(cmdarg_T *cap) { - cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */ + cap->retval |= CA_COMMAND_BUSY; // don't call edit() now } /* @@ -3701,8 +3777,9 @@ static void nv_error(cmdarg_T *cap) */ static void nv_help(cmdarg_T *cap) { - if (!checkclearopq(cap->oap)) + if (!checkclearopq(cap->oap)) { ex_help(NULL); + } } /* @@ -3731,25 +3808,22 @@ static void nv_page(cmdarg_T *cap) { if (!checkclearop(cap->oap)) { if (mod_mask & MOD_MASK_CTRL) { - /* : tab page back; : tab page forward */ - if (cap->arg == BACKWARD) + // : tab page back; : tab page forward + if (cap->arg == BACKWARD) { goto_tabpage(-(int)cap->count1); - else + } else { goto_tabpage((int)cap->count0); - } else + } + } else { (void)onepage(cap->arg, cap->count1); + } } } -/* - * Implementation of "gd" and "gD" command. - */ -static void -nv_gd ( - oparg_T *oap, - int nchar, - int thisblock /* 1 for "1gd" and "1gD" */ -) +/// Implementation of "gd" and "gD" command. +/// +/// @param thisblock 1 for "1gd" and "1gD" +static void nv_gd(oparg_T *oap, int nchar, int thisblock) { size_t len; char_u *ptr; @@ -3794,21 +3868,15 @@ static bool is_ident(char_u *line, int offset) return incomment == false && instring == 0; } -/* - * Search for variable declaration of "ptr[len]". - * When "locally" is true in the current function ("gd"), otherwise in the - * current file ("gD"). - * When "thisblock" is true check the {} block scope. - * Return fail when not found. - */ -bool -find_decl ( - char_u *ptr, - size_t len, - bool locally, - bool thisblock, - int flags_arg // flags passed to searchit() -) +/// Search for variable declaration of "ptr[len]". +/// When "locally" is true in the current function ("gd"), otherwise in the +/// current file ("gD"). +/// +/// @param thisblock when true check the {} block scope. +/// @param flags_arg flags passed to searchit() +/// +/// @return fail when not found. +bool find_decl(char_u *ptr, size_t len, bool locally, bool thisblock, int flags_arg) { char_u *pat; pos_T old_pos; @@ -3831,8 +3899,8 @@ find_decl ( old_pos = curwin->w_cursor; save_p_ws = p_ws; save_p_scs = p_scs; - p_ws = false; /* don't wrap around end of file now */ - p_scs = false; /* don't switch ignorecase off now */ + p_ws = false; // don't wrap around end of file now + p_scs = false; // don't switch ignorecase off now /* * With "gD" go to line 1. @@ -3840,18 +3908,19 @@ find_decl ( * back until a blank line. If this fails go to line 1. */ if (!locally || !findpar(&incll, BACKWARD, 1L, '{', false)) { - setpcmark(); /* Set in findpar() otherwise */ + setpcmark(); // Set in findpar() otherwise curwin->w_cursor.lnum = 1; par_pos = curwin->w_cursor; } else { par_pos = curwin->w_cursor; while (curwin->w_cursor.lnum > 1 - && *skipwhite(get_cursor_line_ptr()) != NUL) + && *skipwhite(get_cursor_line_ptr()) != NUL) { --curwin->w_cursor.lnum; + } } curwin->w_cursor.col = 0; - /* Search forward for the identifier, ignore comment lines. */ + // Search forward for the identifier, ignore comment lines. clearpos(&found_pos); for (;; ) { t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD, @@ -3875,7 +3944,7 @@ find_decl ( } if (t == false) { - /* If we previously found a valid position, use it. */ + // If we previously found a valid position, use it. if (found_pos.lnum != 0) { curwin->w_cursor = found_pos; t = true; @@ -3883,7 +3952,7 @@ find_decl ( break; } if (get_leader_len(get_cursor_line_ptr(), NULL, false, true) > 0) { - /* Ignore this line, continue at start of next line. */ + // Ignore this line, continue at start of next line. ++curwin->w_cursor.lnum; curwin->w_cursor.col = 0; continue; @@ -3925,7 +3994,7 @@ find_decl ( curwin->w_cursor = old_pos; } else { curwin->w_set_curswant = true; - /* "n" searches forward now */ + // "n" searches forward now reset_search_dir(); } @@ -3949,10 +4018,10 @@ static bool nv_screengo(oparg_T *oap, int dir, long dist) bool retval = true; bool atend = false; int n; - int col_off1; /* margin offset for first screen line */ - int col_off2; /* margin offset for wrapped screen line */ - int width1; /* text width for first screen line */ - int width2; /* test width for wrapped screen line */ + int col_off1; // margin offset for first screen line + int col_off2; // margin offset for wrapped screen line + int width1; // text width for first screen line + int width2; // test width for wrapped screen line oap->motion_type = kMTCharWise; oap->inclusive = (curwin->w_curswant == MAXCOL); @@ -3972,20 +4041,22 @@ static bool nv_screengo(oparg_T *oap, int dir, long dist) if (curwin->w_curswant == MAXCOL) { atend = true; validate_virtcol(); - if (width1 <= 0) + if (width1 <= 0) { curwin->w_curswant = 0; - else { + } else { curwin->w_curswant = width1 - 1; - if (curwin->w_virtcol > curwin->w_curswant) + if (curwin->w_virtcol > curwin->w_curswant) { curwin->w_curswant += ((curwin->w_virtcol - curwin->w_curswant - 1) / width2 + 1) * width2; + } } } else { - if (linelen > width1) + if (linelen > width1) { n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1; - else + } else { n = width1; + } if (curwin->w_curswant >= n) { curwin->w_curswant = n - 1; } @@ -4021,11 +4092,12 @@ static bool nv_screengo(oparg_T *oap, int dir, long dist) curwin->w_curswant += w; } } - } else { /* dir == FORWARD */ - if (linelen > width1) + } else { // dir == FORWARD + if (linelen > width1) { n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1; - else + } else { n = width1; + } if (curwin->w_curswant + width2 < (colnr_T)n && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)) { // move forward within line @@ -4055,10 +4127,11 @@ static bool nv_screengo(oparg_T *oap, int dir, long dist) } } - if (virtual_active() && atend) + if (virtual_active() && atend) { coladvance(MAXCOL); - else + } else { coladvance(curwin->w_curswant); + } if (curwin->w_cursor.col > 0 && curwin->w_p_wrap) { /* @@ -4068,20 +4141,22 @@ static bool nv_screengo(oparg_T *oap, int dir, long dist) */ validate_virtcol(); colnr_T virtcol = curwin->w_virtcol; - if (virtcol > (colnr_T)width1 && *p_sbr != NUL) - virtcol -= vim_strsize(p_sbr); + if (virtcol > (colnr_T)width1 && *p_sbr != NUL) { + virtcol -= vim_strsize(p_sbr); + } if (virtcol > curwin->w_curswant && (curwin->w_curswant < (colnr_T)width1 ? (curwin->w_curswant > (colnr_T)width1 / 2) : ((curwin->w_curswant - width1) % width2 - > (colnr_T)width2 / 2))) + > (colnr_T)width2 / 2))) { --curwin->w_cursor.col; + } } - if (atend) - curwin->w_curswant = MAXCOL; /* stick in the last column */ - + if (atend) { + curwin->w_curswant = MAXCOL; // stick in the last column + } return retval; } @@ -4146,8 +4221,9 @@ static void nv_mouse(cmdarg_T *cap) */ static void nv_scroll_line(cmdarg_T *cap) { - if (!checkclearop(cap->oap)) + if (!checkclearop(cap->oap)) { scroll_redraw(cap->arg, cap->count1); + } } /* @@ -4160,8 +4236,8 @@ void scroll_redraw(int up, long count) linenr_T prev_lnum = curwin->w_cursor.lnum; bool moved = up ? - scrollup(count, true) : - scrolldown(count, true); + scrollup(count, true) : + scrolldown(count, true); if (get_scrolloff_value(curwin)) { // Adjust the cursor position for 'scrolloff'. Mark w_topline as @@ -4174,17 +4250,18 @@ void scroll_redraw(int up, long count) * we get stuck at one position. Don't move the cursor up if the * first line of the buffer is already on the screen */ while (curwin->w_topline == prev_topline - && curwin->w_topfill == prev_topfill - ) { + && curwin->w_topfill == prev_topfill) { if (up) { if (curwin->w_cursor.lnum > prev_lnum - || cursor_down(1L, false) == false) + || cursor_down(1L, false) == false) { break; + } } else { if (curwin->w_cursor.lnum < prev_lnum || prev_topline == 1L - || cursor_up(1L, false) == false) + || cursor_up(1L, false) == false) { break; + } } /* Mark w_topline as valid, otherwise the screen jumps back at the * end of the file. */ @@ -4220,8 +4297,9 @@ static void nv_zet(cmdarg_T *cap) /* * "z123{nchar}": edit the count before obtaining {nchar} */ - if (checkclearop(cap->oap)) + if (checkclearop(cap->oap)) { return; + } n = nchar - '0'; for (;; ) { no_mapping++; @@ -4229,11 +4307,11 @@ static void nv_zet(cmdarg_T *cap) LANGMAP_ADJUST(nchar, true); no_mapping--; (void)add_to_showcmd(nchar); - if (nchar == K_DEL || nchar == K_KDEL) + if (nchar == K_DEL || nchar == K_KDEL) { n /= 10; - else if (ascii_isdigit(nchar)) + } else if (ascii_isdigit(nchar)) { n = n * 10 + (nchar - '0'); - else if (nchar == CAR) { + } else if (nchar == CAR) { win_setheight(n); break; } else if (nchar == 'l' @@ -4270,15 +4348,16 @@ dozet: && cap->count0 && cap->count0 != curwin->w_cursor.lnum) { setpcmark(); - if (cap->count0 > curbuf->b_ml.ml_line_count) + if (cap->count0 > curbuf->b_ml.ml_line_count) { curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; - else + } else { curwin->w_cursor.lnum = cap->count0; + } check_cursor_col(); } switch (nchar) { - /* "z+", "z" and "zt": put cursor at top of screen */ + // "z+", "z" and "zt": put cursor at top of screen case '+': if (cap->count0 == 0) { // No count given: put cursor at the line below screen @@ -4296,16 +4375,19 @@ dozet: beginline(BL_WHITE | BL_FIX); FALLTHROUGH; - case 't': scroll_cursor_top(0, true); + case 't': + scroll_cursor_top(0, true); redraw_later(curwin, VALID); set_fraction(curwin); break; - /* "z." and "zz": put cursor in middle of screen */ - case '.': beginline(BL_WHITE | BL_FIX); - FALLTHROUGH; + // "z." and "zz": put cursor in middle of screen + case '.': + beginline(BL_WHITE | BL_FIX); + FALLTHROUGH; - case 'z': scroll_cursor_halfway(true); + case 'z': + scroll_cursor_halfway(true); redraw_later(curwin, VALID); set_fraction(curwin); break; @@ -4317,74 +4399,83 @@ dozet: if (cap->count0 != 0) { scroll_cursor_bot(0, true); curwin->w_cursor.lnum = curwin->w_topline; - } else if (curwin->w_topline == 1) + } else if (curwin->w_topline == 1) { curwin->w_cursor.lnum = 1; - else + } else { curwin->w_cursor.lnum = curwin->w_topline - 1; + } FALLTHROUGH; case '-': beginline(BL_WHITE | BL_FIX); FALLTHROUGH; - case 'b': scroll_cursor_bot(0, true); + case 'b': + scroll_cursor_bot(0, true); redraw_later(curwin, VALID); set_fraction(curwin); break; - /* "zH" - scroll screen right half-page */ + // "zH" - scroll screen right half-page case 'H': cap->count1 *= curwin->w_width_inner / 2; FALLTHROUGH; - /* "zh" - scroll screen to the right */ + // "zh" - scroll screen to the right case 'h': case K_LEFT: if (!curwin->w_p_wrap) { - if ((colnr_T)cap->count1 > curwin->w_leftcol) + if ((colnr_T)cap->count1 > curwin->w_leftcol) { curwin->w_leftcol = 0; - else + } else { curwin->w_leftcol -= (colnr_T)cap->count1; + } leftcol_changed(); } break; // "zL" - scroll screen left half-page - case 'L': cap->count1 *= curwin->w_width_inner / 2; + case 'L': + cap->count1 *= curwin->w_width_inner / 2; FALLTHROUGH; - /* "zl" - scroll screen to the left */ + // "zl" - scroll screen to the left case 'l': case K_RIGHT: if (!curwin->w_p_wrap) { - /* scroll the window left */ + // scroll the window left curwin->w_leftcol += (colnr_T)cap->count1; leftcol_changed(); } break; - /* "zs" - scroll screen, cursor at the start */ - case 's': if (!curwin->w_p_wrap) { - if (hasFolding(curwin->w_cursor.lnum, NULL, NULL)) - col = 0; /* like the cursor is in col 0 */ - else + // "zs" - scroll screen, cursor at the start + case 's': + if (!curwin->w_p_wrap) { + if (hasFolding(curwin->w_cursor.lnum, NULL, NULL)) { + col = 0; // like the cursor is in col 0 + } else { getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL); - if (col > l_p_siso) + } + if (col > l_p_siso) { col -= l_p_siso; - else + } else { col = 0; + } if (curwin->w_leftcol != col) { curwin->w_leftcol = col; redraw_later(curwin, NOT_VALID); } - } + } break; - /* "ze" - scroll screen, cursor at the end */ - case 'e': if (!curwin->w_p_wrap) { - if (hasFolding(curwin->w_cursor.lnum, NULL, NULL)) - col = 0; /* like the cursor is in col 0 */ - else + // "ze" - scroll screen, cursor at the end + case 'e': + if (!curwin->w_p_wrap) { + if (hasFolding(curwin->w_cursor.lnum, NULL, NULL)) { + col = 0; // like the cursor is in col 0 + } else { getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col); + } n = curwin->w_width_inner - curwin_col_off(); if (col + l_p_siso < n) { col = 0; @@ -4395,7 +4486,7 @@ dozet: curwin->w_leftcol = col; redraw_later(curwin, NOT_VALID); } - } + } break; // "zp", "zP" in block mode put without addind trailing spaces @@ -4404,41 +4495,46 @@ dozet: nv_put(cap); break; // "zy" Yank without trailing spaces - case 'y': nv_operator(cap); - break; + case 'y': + nv_operator(cap); + break; - /* "zF": create fold command */ - /* "zf": create fold operator */ + // "zF": create fold command + // "zf": create fold operator case 'F': - case 'f': if (foldManualAllowed(true)) { + case 'f': + if (foldManualAllowed(true)) { cap->nchar = 'f'; nv_operator(cap); curwin->w_p_fen = true; - /* "zF" is like "zfzf" */ + // "zF" is like "zfzf" if (nchar == 'F' && cap->oap->op_type == OP_FOLD) { nv_operator(cap); finish_op = true; } - } else + } else { clearopbeep(cap->oap); + } break; - /* "zd": delete fold at cursor */ - /* "zD": delete fold at cursor recursively */ + // "zd": delete fold at cursor + // "zD": delete fold at cursor recursively case 'd': - case 'D': if (foldManualAllowed(false)) { + case 'D': + if (foldManualAllowed(false)) { if (VIsual_active) { nv_operator(cap); } else { deleteFold(curwin, curwin->w_cursor.lnum, curwin->w_cursor.lnum, nchar == 'D', false); } - } + } break; - /* "zE": erase all folds */ - case 'E': if (foldmethodIsManual(curwin)) { + // "zE": erase all folds + case 'E': + if (foldmethodIsManual(curwin)) { clearFolding(curwin); changed_window_setting(); } else if (foldmethodIsMarker(curwin)) { @@ -4448,20 +4544,24 @@ dozet: } break; - /* "zn": fold none: reset 'foldenable' */ - case 'n': curwin->w_p_fen = false; + // "zn": fold none: reset 'foldenable' + case 'n': + curwin->w_p_fen = false; break; - /* "zN": fold Normal: set 'foldenable' */ - case 'N': curwin->w_p_fen = true; + // "zN": fold Normal: set 'foldenable' + case 'N': + curwin->w_p_fen = true; break; - /* "zi": invert folding: toggle 'foldenable' */ - case 'i': curwin->w_p_fen = !curwin->w_p_fen; + // "zi": invert folding: toggle 'foldenable' + case 'i': + curwin->w_p_fen = !curwin->w_p_fen; break; // "za": open closed fold or close open fold at cursor - case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL)) { + case 'a': + if (hasFolding(curwin->w_cursor.lnum, NULL, NULL)) { openFold(curwin->w_cursor, cap->count1); } else { closeFold(curwin->w_cursor, cap->count1); @@ -4470,7 +4570,8 @@ dozet: break; // "zA": open fold at cursor recursively - case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL)) { + case 'A': + if (hasFolding(curwin->w_cursor.lnum, NULL, NULL)) { openFoldRecurse(curwin->w_cursor); } else { closeFoldRecurse(curwin->w_cursor); @@ -4479,7 +4580,8 @@ dozet: break; // "zo": open fold at cursor or Visual area - case 'o': if (VIsual_active) { + case 'o': + if (VIsual_active) { nv_operator(cap); } else { openFold(curwin->w_cursor, cap->count1); @@ -4487,7 +4589,8 @@ dozet: break; // "zO": open fold recursively - case 'O': if (VIsual_active) { + case 'O': + if (VIsual_active) { nv_operator(cap); } else { openFoldRecurse(curwin->w_cursor); @@ -4495,16 +4598,18 @@ dozet: break; // "zc": close fold at cursor or Visual area - case 'c': if (VIsual_active) { + case 'c': + if (VIsual_active) { nv_operator(cap); - } else { + } else { closeFold(curwin->w_cursor, cap->count1); } curwin->w_p_fen = true; break; // "zC": close fold recursively - case 'C': if (VIsual_active) { + case 'C': + if (VIsual_active) { nv_operator(cap); } else { closeFoldRecurse(curwin->w_cursor); @@ -4512,24 +4617,27 @@ dozet: curwin->w_p_fen = true; break; - /* "zv": open folds at the cursor */ - case 'v': foldOpenCursor(); - break; - - /* "zx": re-apply 'foldlevel' and open folds at the cursor */ - case 'x': curwin->w_p_fen = true; - curwin->w_foldinvalid = true; /* recompute folds */ - newFoldLevel(); /* update right now */ + // "zv": open folds at the cursor + case 'v': foldOpenCursor(); break; - /* "zX": undo manual opens/closes, re-apply 'foldlevel' */ - case 'X': curwin->w_p_fen = true; - curwin->w_foldinvalid = true; /* recompute folds */ - old_fdl = -1; /* force an update */ + // "zx": re-apply 'foldlevel' and open folds at the cursor + case 'x': + curwin->w_p_fen = true; + curwin->w_foldinvalid = true; // recompute folds + newFoldLevel(); // update right now + foldOpenCursor(); break; - /* "zm": fold more */ + // "zX": undo manual opens/closes, re-apply 'foldlevel' + case 'X': + curwin->w_p_fen = true; + curwin->w_foldinvalid = true; // recompute folds + old_fdl = -1; // force an update + break; + + // "zm": fold more case 'm': if (curwin->w_p_fdl > 0) { curwin->w_p_fdl -= cap->count1; @@ -4537,17 +4645,18 @@ dozet: curwin->w_p_fdl = 0; } } - old_fdl = -1; /* force an update */ + old_fdl = -1; // force an update curwin->w_p_fen = true; break; - /* "zM": close all folds */ - case 'M': curwin->w_p_fdl = 0; - old_fdl = -1; /* force an update */ + // "zM": close all folds + case 'M': + curwin->w_p_fdl = 0; + old_fdl = -1; // force an update curwin->w_p_fen = true; break; - /* "zr": reduce folding */ + // "zr": reduce folding case 'r': curwin->w_p_fdl += cap->count1; { @@ -4563,11 +4672,12 @@ dozet: old_fdl = -1; // force an update break; - case 'j': /* "zj" move to next fold downwards */ - case 'k': /* "zk" move to next fold upwards */ + case 'j': // "zj" move to next fold downwards + case 'k': // "zk" move to next fold upwards if (foldMoveTo(true, nchar == 'j' ? FORWARD : BACKWARD, - cap->count1) == false) + cap->count1) == false) { clearopbeep(cap->oap); + } break; @@ -4584,55 +4694,61 @@ dozet: undo = true; FALLTHROUGH; - case 'g': /* "zg": add good word to word list */ - case 'w': /* "zw": add wrong word to word list */ - case 'G': /* "zG": add good word to temp word list */ - case 'W': /* "zW": add wrong word to temp word list */ - { - char_u *ptr = NULL; - size_t len; + case 'g': // "zg": add good word to word list + case 'w': // "zw": add wrong word to word list + case 'G': // "zG": add good word to temp word list + case 'W': // "zW": add wrong word to temp word list + { + char_u *ptr = NULL; + size_t len; - if (checkclearop(cap->oap)) - break; - if (VIsual_active && !get_visual_text(cap, &ptr, &len)) - return; - if (ptr == NULL) { - pos_T pos = curwin->w_cursor; + if (checkclearop(cap->oap)) { + break; + } + if (VIsual_active && !get_visual_text(cap, &ptr, &len)) { + return; + } + if (ptr == NULL) { + pos_T pos = curwin->w_cursor; - /* Find bad word under the cursor. When 'spell' is - * off this fails and find_ident_under_cursor() is - * used below. */ - emsg_off++; - len = spell_move_to(curwin, FORWARD, true, true, NULL); - emsg_off--; - if (len != 0 && curwin->w_cursor.col <= pos.col) - ptr = ml_get_pos(&curwin->w_cursor); - curwin->w_cursor = pos; - } + /* Find bad word under the cursor. When 'spell' is + * off this fails and find_ident_under_cursor() is + * used below. */ + emsg_off++; + len = spell_move_to(curwin, FORWARD, true, true, NULL); + emsg_off--; + if (len != 0 && curwin->w_cursor.col <= pos.col) { + ptr = ml_get_pos(&curwin->w_cursor); + } + curwin->w_cursor = pos; + } - if (ptr == NULL && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0) - return; - assert(len <= INT_MAX); - spell_add_word(ptr, (int)len, - nchar == 'w' || nchar == 'W' + if (ptr == NULL && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0) { + return; + } + assert(len <= INT_MAX); + spell_add_word(ptr, (int)len, + nchar == 'w' || nchar == 'W' ? SPELL_ADD_BAD : SPELL_ADD_GOOD, - (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, - undo); - } - break; - - case '=': /* "z=": suggestions for a badly spelled word */ - if (!checkclearop(cap->oap)) - spell_suggest((int)cap->count0); + (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, + undo); + } break; - default: clearopbeep(cap->oap); + case '=': // "z=": suggestions for a badly spelled word + if (!checkclearop(cap->oap)) { + spell_suggest((int)cap->count0); + } + break; + + default: + clearopbeep(cap->oap); } - /* Redraw when 'foldenable' changed */ + // Redraw when 'foldenable' changed if (old_fen != curwin->w_p_fen) { if (foldmethodIsDiff(curwin) && curwin->w_p_scb) { - /* Adjust 'foldenable' in diff-synced windows. */ + // Adjust 'foldenable' in diff-synced windows. FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb) { wp->w_p_fen = curwin->w_p_fen; @@ -4643,9 +4759,10 @@ dozet: changed_window_setting(); } - /* Redraw when 'foldlevel' changed. */ - if (old_fdl != curwin->w_p_fdl) + // Redraw when 'foldlevel' changed. + if (old_fdl != curwin->w_p_fdl) { newFoldLevel(); + } } @@ -4688,9 +4805,10 @@ static void nv_colon(cmdarg_T *cap) } } - /* When typing, don't type below an old message */ - if (KeyTyped) + // When typing, don't type below an old message + if (KeyTyped) { compute_cmdrow(); + } old_p_im = p_im; @@ -4698,26 +4816,28 @@ static void nv_colon(cmdarg_T *cap) cmd_result = do_cmdline(NULL, is_cmdkey ? getcmdkeycmd : getexline, NULL, cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0); - /* If 'insertmode' changed, enter or exit Insert mode */ + // If 'insertmode' changed, enter or exit Insert mode if (p_im != old_p_im) { - if (p_im) + if (p_im) { restart_edit = 'i'; - else + } else { restart_edit = 0; + } } - if (cmd_result == false) - /* The Ex command failed, do not execute the operator. */ + if (cmd_result == false) { + // The Ex command failed, do not execute the operator. clearop(cap->oap); - else if (cap->oap->op_type != OP_NOP - && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count - || cap->oap->start.col > - (colnr_T)STRLEN(ml_get(cap->oap->start.lnum)) - || did_emsg - )) + } else if (cap->oap->op_type != OP_NOP + && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count + || cap->oap->start.col > + (colnr_T)STRLEN(ml_get(cap->oap->start.lnum)) + || did_emsg + )) { /* The start of the operator has become invalid by the Ex command. */ clearopbeep(cap->oap); + } } } @@ -4726,12 +4846,13 @@ static void nv_colon(cmdarg_T *cap) */ static void nv_ctrlg(cmdarg_T *cap) { - if (VIsual_active) { /* toggle Selection/Visual mode */ + if (VIsual_active) { // toggle Selection/Visual mode VIsual_select = !VIsual_select; showmode(); - } else if (!checkclearop(cap->oap)) - /* print full name if count given or :cd used */ + } else if (!checkclearop(cap->oap)) { + // print full name if count given or :cd used fileinfo((int)cap->count0, false, true); + } } /* @@ -4740,10 +4861,11 @@ static void nv_ctrlg(cmdarg_T *cap) static void nv_ctrlh(cmdarg_T *cap) { if (VIsual_active && VIsual_select) { - cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */ + cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode v_visop(cap); - } else + } else { nv_left(cap); + } } /* @@ -4752,7 +4874,7 @@ static void nv_ctrlh(cmdarg_T *cap) static void nv_clear(cmdarg_T *cap) { if (!checkclearop(cap->oap)) { - /* Clear all syntax states to force resyncing. */ + // Clear all syntax states to force resyncing. syn_stack_free_all(curwin->w_s); FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { wp->w_s->b_syn_slow = false; @@ -4770,7 +4892,7 @@ static void nv_ctrlo(cmdarg_T *cap) if (VIsual_active && VIsual_select) { VIsual_select = false; showmode(); - restart_VIsual_select = 2; /* restart Select mode later */ + restart_VIsual_select = 2; // restart Select mode later } else { cap->count1 = -cap->count1; nv_pcmark(cap); @@ -4781,9 +4903,10 @@ static void nv_ctrlo(cmdarg_T *cap) // not named. static void nv_hat(cmdarg_T *cap) { - if (!checkclearopq(cap->oap)) + if (!checkclearopq(cap->oap)) { (void)buflist_getfile((int)cap->count0, (linenr_T)0, - GETF_SETMARK|GETF_ALT, false); + GETF_SETMARK|GETF_ALT, false); + } } /* @@ -4793,15 +4916,18 @@ static void nv_Zet(cmdarg_T *cap) { if (!checkclearopq(cap->oap)) { switch (cap->nchar) { - /* "ZZ": equivalent to ":x". */ - case 'Z': do_cmdline_cmd("x"); + // "ZZ": equivalent to ":x". + case 'Z': + do_cmdline_cmd("x"); break; - /* "ZQ": equivalent to ":q!" (Elvis compatible). */ - case 'Q': do_cmdline_cmd("q!"); + // "ZQ": equivalent to ":q!" (Elvis compatible). + case 'Q': + do_cmdline_cmd("q!"); break; - default: clearopbeep(cap->oap); + default: + clearopbeep(cap->oap); } } } @@ -4824,23 +4950,23 @@ void do_nv_ident(int c1, int c2) /* * Handle the commands that use the word under the cursor. - * [g] CTRL-] :ta to current identifier - * [g] 'K' run program for current identifier - * [g] '*' / to current identifier or string - * [g] '#' ? to current identifier or string - * g ']' :tselect for current identifier + * [g] CTRL-] :ta to current identifier + * [g] 'K' run program for current identifier + * [g] '*' / to current identifier or string + * [g] '#' ? to current identifier or string + * g ']' :tselect for current identifier */ static void nv_ident(cmdarg_T *cap) { char_u *ptr = NULL; char_u *p; - size_t n = 0; /* init for GCC */ + size_t n = 0; // init for GCC int cmdchar; - bool g_cmd; /* "g" command */ + bool g_cmd; // "g" command bool tag_cmd = false; char_u *aux_ptr; - if (cap->cmdchar == 'g') { /* "g*", "g#", "g]" and "gCTRL-]" */ + if (cap->cmdchar == 'g') { // "g*", "g#", "g]" and "gCTRL-]" cmdchar = cap->nchar; g_cmd = true; } else { @@ -4848,17 +4974,20 @@ static void nv_ident(cmdarg_T *cap) g_cmd = false; } - if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */ + if (cmdchar == POUND) { // the pound sign, '#' for English keyboards cmdchar = '#'; + } /* * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode. */ if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K') { - if (VIsual_active && get_visual_text(cap, &ptr, &n) == false) + if (VIsual_active && get_visual_text(cap, &ptr, &n) == false) { return; - if (checkclearopq(cap->oap)) + } + if (checkclearopq(cap->oap)) { return; + } } if (ptr == NULL && (n = find_ident_under_cursor(&ptr, @@ -4895,11 +5024,12 @@ static void nv_ident(cmdarg_T *cap) * it was. */ setpcmark(); - curwin->w_cursor.col = (colnr_T) (ptr - get_cursor_line_ptr()); + curwin->w_cursor.col = (colnr_T)(ptr - get_cursor_line_ptr()); - if (!g_cmd && vim_iswordp(ptr)) + if (!g_cmd && vim_iswordp(ptr)) { STRCPY(buf, "\\<"); - no_smartcase = true; /* don't use 'smartcase' now */ + } + no_smartcase = true; // don't use 'smartcase' now break; case 'K': @@ -4919,7 +5049,7 @@ static void nv_ident(cmdarg_T *cap) --n; } if (n == 0) { - EMSG(_(e_noident)); /* found dashes only */ + EMSG(_(e_noident)); // found dashes only xfree(buf); return; } @@ -4942,7 +5072,7 @@ static void nv_ident(cmdarg_T *cap) STRCAT(buf, " "); if (cap->count0 != 0 && (isman || isman_s)) { snprintf(buf + STRLEN(buf), buf_size - STRLEN(buf), "%" PRId64, - (int64_t)cap->count0); + (int64_t)cap->count0); STRCAT(buf, " "); } } @@ -4950,21 +5080,23 @@ static void nv_ident(cmdarg_T *cap) case ']': tag_cmd = true; - if (p_cst) + if (p_cst) { STRCPY(buf, "cstag "); - else + } else { STRCPY(buf, "ts "); + } break; default: tag_cmd = true; - if (curbuf->b_help) + if (curbuf->b_help) { STRCPY(buf, "he! "); - else { - if (g_cmd) + } else { + if (g_cmd) { STRCPY(buf, "tj "); - else + } else { snprintf(buf, buf_size, "%" PRId64 "ta ", (int64_t)cap->count0); + } } } @@ -4984,24 +5116,27 @@ static void nv_ident(cmdarg_T *cap) STRCAT(buf, p); xfree(p); } else { - if (cmdchar == '*') + if (cmdchar == '*') { aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\"); - else if (cmdchar == '#') + } else if (cmdchar == '#') { aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\"); - else if (tag_cmd) { - if (curbuf->b_help) - /* ":help" handles unescaped argument */ + } else if (tag_cmd) { + if (curbuf->b_help) { + // ":help" handles unescaped argument aux_ptr = (char_u *)""; - else + } else { aux_ptr = (char_u *)"\\|\"\n["; - } else + } + } else { aux_ptr = (char_u *)"\\|\"\n*?["; + } p = (char_u *)buf + STRLEN(buf); while (n-- > 0) { - /* put a backslash before \ and some others */ - if (vim_strchr(aux_ptr, *ptr) != NULL) + // put a backslash before \ and some others + if (vim_strchr(aux_ptr, *ptr) != NULL) { *p++ = '\\'; + } /* When current byte is a part of multibyte character, copy all * bytes of that character. */ const size_t len = (size_t)(utfc_ptr2len(ptr) - 1); @@ -5046,22 +5181,21 @@ static void nv_ident(cmdarg_T *cap) xfree(buf); } -/* - * Get visually selected text, within one line only. - * Returns false if more than one line selected. - */ -bool -get_visual_text ( - cmdarg_T *cap, - char_u **pp, /* return: start of selected text */ - size_t *lenp /* return: length of selected text */ -) +/// Get visually selected text, within one line only. +/// +/// @param pp return: start of selected text +/// @param lenp return: length of selected text +/// +/// @return false if more than one line selected. +bool get_visual_text(cmdarg_T *cap, char_u **pp, size_t *lenp) { - if (VIsual_mode != 'V') + if (VIsual_mode != 'V') { unadjust_for_sel(); + } if (VIsual.lnum != curwin->w_cursor.lnum) { - if (cap != NULL) + if (cap != NULL) { clearopbeep(cap->oap); + } return false; } if (VIsual_mode == 'V') { @@ -5087,8 +5221,9 @@ get_visual_text ( */ static void nv_tagpop(cmdarg_T *cap) { - if (!checkclearopq(cap->oap)) + if (!checkclearopq(cap->oap)) { do_tag((char_u *)"", DT_POP, (int)cap->count1, false, true); + } } /* @@ -5107,23 +5242,24 @@ static void nv_scroll(cmdarg_T *cap) if (cap->cmdchar == 'L') { validate_botline(curwin); // make sure curwin->w_botline is valid curwin->w_cursor.lnum = curwin->w_botline - 1; - if (cap->count1 - 1 >= curwin->w_cursor.lnum) + if (cap->count1 - 1 >= curwin->w_cursor.lnum) { curwin->w_cursor.lnum = 1; - else { + } else { if (hasAnyFolding(curwin)) { - /* Count a fold for one screen line. */ + // Count a fold for one screen line. for (n = cap->count1 - 1; n > 0 && curwin->w_cursor.lnum > curwin->w_topline; --n) { (void)hasFolding(curwin->w_cursor.lnum, - &curwin->w_cursor.lnum, NULL); + &curwin->w_cursor.lnum, NULL); --curwin->w_cursor.lnum; } - } else + } else { curwin->w_cursor.lnum -= cap->count1 - 1; + } } } else { if (cap->cmdchar == 'M') { - /* Don't count filler lines above the window. */ + // Don't count filler lines above the window. used -= diff_check_fill(curwin, curwin->w_topline) - curwin->w_topfill; validate_botline(curwin); // make sure w_empty_rows is valid @@ -5132,7 +5268,7 @@ static void nv_scroll(cmdarg_T *cap) // Count half he number of filler lines to be "below this // line" and half to be "above the next line". if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline - + n) / 2 >= half) { + + n) / 2 >= half) { --n; break; } @@ -5150,7 +5286,7 @@ static void nv_scroll(cmdarg_T *cap) } else { // (cap->cmdchar == 'H') n = cap->count1 - 1; if (hasAnyFolding(curwin)) { - /* Count a fold for one screen line. */ + // Count a fold for one screen line. lnum = curwin->w_topline; while (n-- > 0 && lnum < curwin->w_botline - 1) { (void)hasFolding(lnum, NULL, &lnum); @@ -5160,8 +5296,9 @@ static void nv_scroll(cmdarg_T *cap) } } curwin->w_cursor.lnum = curwin->w_topline + n; - if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) + if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) { curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; + } } // Correct for 'so', except when an operator is pending. @@ -5180,9 +5317,10 @@ static void nv_right(cmdarg_T *cap) int PAST_LINE; if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) { - /* and move a word or WORD right */ - if (mod_mask & MOD_MASK_CTRL) + // and move a word or WORD right + if (mod_mask & MOD_MASK_CTRL) { cap->arg = true; + } nv_wordcmd(cap); return; } @@ -5195,13 +5333,14 @@ static void nv_right(cmdarg_T *cap) * In virtual mode, there's no such thing as "PAST_LINE", as lines are * (theoretically) infinitely long. */ - if (virtual_active()) + if (virtual_active()) { PAST_LINE = 0; + } for (n = cap->count1; n > 0; --n) { if ((!PAST_LINE && oneright() == false) - || (PAST_LINE && *get_cursor_pos_ptr() == NUL) - ) { + || (PAST_LINE && + *get_cursor_pos_ptr() == NUL)) { // wraps to next line if 'whichwrap' has 's'. // 'l' wraps to next line if 'whichwrap' has 'l'. // CURS_RIGHT wraps to next line if 'whichwrap' has '>'. @@ -5246,8 +5385,9 @@ static void nv_right(cmdarg_T *cap) } } if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped - && cap->oap->op_type == OP_NOP) + && cap->oap->op_type == OP_NOP) { foldOpenCursor(); + } } /* @@ -5260,9 +5400,10 @@ static void nv_left(cmdarg_T *cap) long n; if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) { - /* and move a word or WORD left */ - if (mod_mask & MOD_MASK_CTRL) + // and move a word or WORD left + if (mod_mask & MOD_MASK_CTRL) { cap->arg = 1; + } nv_bck_word(cap); return; } @@ -5272,8 +5413,8 @@ static void nv_left(cmdarg_T *cap) for (n = cap->count1; n > 0; --n) { if (oneleft() == false) { /* and wrap to previous line if 'whichwrap' has 'b'. - * 'h' wraps to previous line if 'whichwrap' has 'h'. - * CURS_LEFT wraps to previous line if 'whichwrap' has '<'. + * 'h' wraps to previous line if 'whichwrap' has 'h'. + * CURS_LEFT wraps to previous line if 'whichwrap' has '<'. */ if ((((cap->cmdchar == K_BS || cap->cmdchar == Ctrl_H) && vim_strchr(p_ww, 'b') != NULL) @@ -5299,15 +5440,17 @@ static void nv_left(cmdarg_T *cap) } continue; } - /* Only beep and flush if not moved at all */ - else if (cap->oap->op_type == OP_NOP && n == cap->count1) + // Only beep and flush if not moved at all + else if (cap->oap->op_type == OP_NOP && n == cap->count1) { beep_flush(); + } break; } } if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped - && cap->oap->op_type == OP_NOP) + && cap->oap->op_type == OP_NOP) { foldOpenCursor(); + } } /* @@ -5317,7 +5460,7 @@ static void nv_left(cmdarg_T *cap) static void nv_up(cmdarg_T *cap) { if (mod_mask & MOD_MASK_SHIFT) { - /* is page up */ + // is page up cap->arg = BACKWARD; nv_page(cap); } else { @@ -5337,7 +5480,7 @@ static void nv_up(cmdarg_T *cap) static void nv_down(cmdarg_T *cap) { if (mod_mask & MOD_MASK_SHIFT) { - /* is page down */ + // is page down cap->arg = FORWARD; nv_page(cap); } else if (bt_quickfix(curbuf) && cap->cmdchar == CAR) { @@ -5399,8 +5542,9 @@ static void nv_gotofile(cmdarg_T *cap) beginline(BL_SOL | BL_FIX); } xfree(ptr); - } else + } else { clearop(cap->oap); + } } /* @@ -5408,10 +5552,10 @@ static void nv_gotofile(cmdarg_T *cap) */ static void nv_end(cmdarg_T *cap) { - if (cap->arg || (mod_mask & MOD_MASK_CTRL)) { /* CTRL-END = goto last line */ + if (cap->arg || (mod_mask & MOD_MASK_CTRL)) { // CTRL-END = goto last line cap->arg = true; nv_goto(cap); - cap->count1 = 1; /* to end of current line */ + cap->count1 = 1; // to end of current line } nv_dollar(cap); } @@ -5427,13 +5571,15 @@ static void nv_dollar(cmdarg_T *cap) * is pending (whew!) keep the cursor where it is. * Otherwise, send it to the end of the line. */ if (!virtual_active() || gchar_cursor() != NUL - || cap->oap->op_type == OP_NOP) - curwin->w_curswant = MAXCOL; /* so we stay at the end */ + || cap->oap->op_type == OP_NOP) { + curwin->w_curswant = MAXCOL; // so we stay at the end + } if (cursor_down(cap->count1 - 1, - cap->oap->op_type == OP_NOP) == false) + cap->oap->op_type == OP_NOP) == false) { clearopbeep(cap->oap); - else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP) + } else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP) { foldOpenCursor(); + } } /* @@ -5446,7 +5592,7 @@ static void nv_search(cmdarg_T *cap) pos_T save_cursor = curwin->w_cursor; if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13) { - /* Translate "g??" to "g?g?" */ + // Translate "g??" to "g?g?" cap->cmdchar = 'g'; cap->nchar = '?'; nv_operator(cap); @@ -5487,18 +5633,13 @@ static void nv_next(cmdarg_T *cap) } } -/* - * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat). - * Uses only cap->count1 and cap->oap from "cap". - * Return 0 for failure, 1 for found, 2 for found and line offset added. - */ -static int normal_search( - cmdarg_T *cap, - int dir, - char_u *pat, - int opt, // extra flags for do_search() - int *wrapped -) +/// Search for "pat" in direction "dir" ('/' or '?', 0 for repeat). +/// Uses only cap->count1 and cap->oap from "cap". +/// +/// @param opt extra flags for do_search() +/// +/// @return 0 for failure, 1 for found, 2 for found and line offset added. +static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped) { int i; searchit_arg_T sia; @@ -5521,8 +5662,9 @@ static int normal_search( cap->oap->motion_type = kMTLineWise; } curwin->w_cursor.coladd = 0; - if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped) + if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped) { foldOpenCursor(); + } } /* "/$" will put the cursor after the end of the line, may need to @@ -5541,28 +5683,31 @@ static void nv_csearch(cmdarg_T *cap) { bool t_cmd; - if (cap->cmdchar == 't' || cap->cmdchar == 'T') + if (cap->cmdchar == 't' || cap->cmdchar == 'T') { t_cmd = true; - else + } else { t_cmd = false; + } cap->oap->motion_type = kMTCharWise; if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == false) { clearopbeep(cap->oap); } else { curwin->w_set_curswant = true; - /* Include a Tab for "tx" and for "dfx". */ + // Include a Tab for "tx" and for "dfx". if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD && (t_cmd || cap->oap->op_type != OP_NOP)) { colnr_T scol, ecol; getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol); curwin->w_cursor.coladd = ecol - scol; - } else + } else { curwin->w_cursor.coladd = 0; + } adjust_for_sel(cap); - if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP) + if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP) { foldOpenCursor(); + } } } @@ -5574,8 +5719,8 @@ static void nv_brackets(cmdarg_T *cap) { pos_T new_pos = { 0, 0, 0 }; pos_T prev_pos; - pos_T *pos = NULL; /* init for GCC */ - pos_T old_pos; /* cursor position before command */ + pos_T *pos = NULL; // init for GCC + pos_T old_pos; // cursor position before command int flag; long n; int findc; @@ -5584,32 +5729,32 @@ static void nv_brackets(cmdarg_T *cap) cap->oap->motion_type = kMTCharWise; cap->oap->inclusive = false; old_pos = curwin->w_cursor; - curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */ + curwin->w_cursor.coladd = 0; // TODO: don't do this for an error. /* * "[f" or "]f" : Edit file under the cursor (same as "gf") */ - if (cap->nchar == 'f') + if (cap->nchar == 'f') { nv_gotofile(cap); - else + } else /* * Find the occurrence(s) of the identifier or define under cursor * in current and included files or jump to the first occurrence. * - * search list jump - * fwd bwd fwd bwd fwd bwd - * identifier "]i" "[i" "]I" "[I" "]^I" "[^I" - * define "]d" "[d" "]D" "[D" "]^D" "[^D" + * search list jump + * fwd bwd fwd bwd fwd bwd + * identifier "]i" "[i" "]I" "[I" "]^I" "[^I" + * define "]d" "[d" "]D" "[D" "]^D" "[^D" */ if (vim_strchr((char_u *) - "iI\011dD\004", - cap->nchar) != NULL) { + "iI\011dD\004", + cap->nchar) != NULL) { char_u *ptr; size_t len; - if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0) + if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0) { clearop(cap->oap); - else { + } else { find_pattern_in_path(ptr, 0, len, true, cap->count0 == 0 ? !isupper(cap->nchar) : false, (((cap->nchar & 0xf) == ('d' & 0xf)) @@ -5637,14 +5782,16 @@ static void nv_brackets(cmdarg_T *cap) && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL) || (cap->cmdchar == ']' && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL)) { - if (cap->nchar == '*') + if (cap->nchar == '*') { cap->nchar = '/'; + } prev_pos.lnum = 0; if (cap->nchar == 'm' || cap->nchar == 'M') { - if (cap->cmdchar == '[') + if (cap->cmdchar == '[') { findc = '{'; - else + } else { findc = '}'; + } n = 9999; } else { findc = cap->nchar; @@ -5652,12 +5799,14 @@ static void nv_brackets(cmdarg_T *cap) } for (; n > 0; --n) { if ((pos = findmatchlimit(cap->oap, findc, - (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL) { - if (new_pos.lnum == 0) { /* nothing found */ - if (cap->nchar != 'm' && cap->nchar != 'M') + (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL) { + if (new_pos.lnum == 0) { // nothing found + if (cap->nchar != 'm' && cap->nchar != 'M') { clearopbeep(cap->oap); - } else - pos = &new_pos; /* use last one found */ + } + } else { + pos = &new_pos; // use last one found + } break; } prev_pos = new_pos; @@ -5673,24 +5822,27 @@ static void nv_brackets(cmdarg_T *cap) * Also repeat for the given count. */ if (cap->nchar == 'm' || cap->nchar == 'M') { - /* norm is true for "]M" and "[m" */ + // norm is true for "]M" and "[m" int norm = ((findc == '{') == (cap->nchar == 'm')); n = cap->count1; - /* found a match: we were inside a method */ + // found a match: we were inside a method if (prev_pos.lnum != 0) { pos = &prev_pos; curwin->w_cursor = prev_pos; - if (norm) + if (norm) { --n; - } else + } + } else { pos = NULL; + } while (n > 0) { for (;; ) { if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0) { - /* if not found anything, that's an error */ - if (pos == NULL) + // if not found anything, that's an error + if (pos == NULL) { clearopbeep(cap->oap); + } n = 0; break; } @@ -5709,54 +5861,59 @@ static void nv_brackets(cmdarg_T *cap) new_pos = curwin->w_cursor; pos = &new_pos; } - /* found start/end of other method: go to match */ + // found start/end of other method: go to match else if ((pos = findmatchlimit(cap->oap, findc, - (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, - 0)) == NULL) + (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, + 0)) == NULL) { n = 0; - else + } else { curwin->w_cursor = *pos; + } break; } } --n; } curwin->w_cursor = old_pos; - if (pos == NULL && new_pos.lnum != 0) + if (pos == NULL && new_pos.lnum != 0) { clearopbeep(cap->oap); + } } if (pos != NULL) { setpcmark(); curwin->w_cursor = *pos; curwin->w_set_curswant = true; if ((fdo_flags & FDO_BLOCK) && KeyTyped - && cap->oap->op_type == OP_NOP) + && cap->oap->op_type == OP_NOP) { foldOpenCursor(); + } } } /* * "[[", "[]", "]]" and "][": move to start or end of function */ else if (cap->nchar == '[' || cap->nchar == ']') { - if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */ + if (cap->nchar == cap->cmdchar) { // "]]" or "[[" flag = '{'; - else - flag = '}'; /* "][" or "[]" */ - + } else { + flag = '}'; // "][" or "[]" + } curwin->w_set_curswant = true; /* * Imitate strange Vi behaviour: When using "]]" with an operator * we also stop at '}'. */ if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag, - (cap->oap->op_type != OP_NOP - && cap->arg == FORWARD && flag == '{'))) + (cap->oap->op_type != OP_NOP + && cap->arg == FORWARD && flag == '{'))) { clearopbeep(cap->oap); - else { - if (cap->oap->op_type == OP_NOP) + } else { + if (cap->oap->op_type == OP_NOP) { beginline(BL_WHITE | BL_FIX); - if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP) + } + if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP) { foldOpenCursor(); + } } } else if (cap->nchar == 'p' || cap->nchar == 'P') { // "[p", "[P", "]P" and "]p": put with indent adjustment @@ -5770,12 +5927,14 @@ static void nv_brackets(cmdarg_T *cap) for (n = cap->count1; n > 0; --n) { prev_pos = *pos; pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD, - cap->nchar == '\''); - if (pos == NULL) + cap->nchar == '\''); + if (pos == NULL) { break; + } } - if (pos == NULL) + if (pos == NULL) { pos = &prev_pos; + } nv_cursormark(cap, cap->nchar == '\'', pos); } /* @@ -5784,31 +5943,33 @@ static void nv_brackets(cmdarg_T *cap) */ else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE) { (void)do_mouse(cap->oap, cap->nchar, - (cap->cmdchar == ']') ? FORWARD : BACKWARD, - cap->count1, PUT_FIXINDENT); + (cap->cmdchar == ']') ? FORWARD : BACKWARD, + cap->count1, PUT_FIXINDENT); } /* * "[z" and "]z": move to start or end of open fold. */ else if (cap->nchar == 'z') { if (foldMoveTo(false, cap->cmdchar == ']' ? FORWARD : BACKWARD, - cap->count1) == false) + cap->count1) == false) { clearopbeep(cap->oap); + } } /* * "[c" and "]c": move to next or previous diff-change. */ else if (cap->nchar == 'c') { if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD, - cap->count1) == false) + cap->count1) == false) { clearopbeep(cap->oap); + } } /* * "[s", "[S", "]s" and "]S": move to next spell error. */ else if (cap->nchar == 's' || cap->nchar == 'S') { setpcmark(); - for (n = 0; n < cap->count1; ++n) + for (n = 0; n < cap->count1; ++n) { if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD, cap->nchar == 's', false, NULL) == 0) { clearopbeep(cap->oap); @@ -5816,12 +5977,15 @@ static void nv_brackets(cmdarg_T *cap) } else { curwin->w_set_curswant = true; } - if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped) + } + if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped) { foldOpenCursor(); + } } - /* Not a valid cap->nchar. */ - else + // Not a valid cap->nchar. + else { clearopbeep(cap->oap); + } } /* @@ -5861,9 +6025,9 @@ static void nv_percent(cmdarg_T *cap) } else { // "%" : go to matching paren cap->oap->motion_type = kMTCharWise; cap->oap->use_reg_one = true; - if ((pos = findmatch(cap->oap, NUL)) == NULL) + if ((pos = findmatch(cap->oap, NUL)) == NULL) { clearopbeep(cap->oap); - else { + } else { setpcmark(); curwin->w_cursor = *pos; curwin->w_set_curswant = true; @@ -5874,8 +6038,9 @@ static void nv_percent(cmdarg_T *cap) if (cap->oap->op_type == OP_NOP && lnum != curwin->w_cursor.lnum && (fdo_flags & FDO_PERCENT) - && KeyTyped) + && KeyTyped) { foldOpenCursor(); + } } /* @@ -5886,18 +6051,19 @@ static void nv_brace(cmdarg_T *cap) { cap->oap->motion_type = kMTCharWise; cap->oap->use_reg_one = true; - /* The motion used to be inclusive for "(", but that is not what Vi does. */ + // The motion used to be inclusive for "(", but that is not what Vi does. cap->oap->inclusive = false; curwin->w_set_curswant = true; - if (findsent(cap->arg, cap->count1) == false) + if (findsent(cap->arg, cap->count1) == false) { clearopbeep(cap->oap); - else { - /* Don't leave the cursor on the NUL past end of line. */ + } else { + // Don't leave the cursor on the NUL past end of line. adjust_cursor(cap->oap); curwin->w_cursor.coladd = 0; - if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP) + if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP) { foldOpenCursor(); + } } } @@ -5907,8 +6073,9 @@ static void nv_brace(cmdarg_T *cap) static void nv_mark(cmdarg_T *cap) { if (!checkclearop(cap->oap)) { - if (setmark(cap->nchar) == false) + if (setmark(cap->nchar) == false) { clearopbeep(cap->oap); + } } } @@ -5922,12 +6089,13 @@ static void nv_findpar(cmdarg_T *cap) cap->oap->inclusive = false; cap->oap->use_reg_one = true; curwin->w_set_curswant = true; - if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, false)) + if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, false)) { clearopbeep(cap->oap); - else { + } else { curwin->w_cursor.coladd = 0; - if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP) + if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP) { foldOpenCursor(); + } } } @@ -5937,14 +6105,14 @@ static void nv_findpar(cmdarg_T *cap) static void nv_undo(cmdarg_T *cap) { if (cap->oap->op_type == OP_LOWER - || VIsual_active - ) { - /* translate "u" to "gu" and "guu" to "gugu" */ + || VIsual_active) { + // translate "u" to "gu" and "guu" to "gugu" cap->cmdchar = 'g'; cap->nchar = 'u'; nv_operator(cap); - } else + } else { nv_kundo(cap); + } } /* @@ -5978,26 +6146,29 @@ static void nv_replace(cmdarg_T *cap) return; } - /* get another character */ + // get another character if (cap->nchar == Ctrl_V) { had_ctrl_v = Ctrl_V; cap->nchar = get_literal(); - /* Don't redo a multibyte character with CTRL-V. */ - if (cap->nchar > DEL) + // Don't redo a multibyte character with CTRL-V. + if (cap->nchar > DEL) { had_ctrl_v = NUL; - } else + } + } else { had_ctrl_v = NUL; + } - /* Abort if the character is a special key. */ + // Abort if the character is a special key. if (IS_SPECIAL(cap->nchar)) { clearopbeep(cap->oap); return; } - /* Visual mode "r" */ + // Visual mode "r" if (VIsual_active) { - if (got_int) + if (got_int) { reset_VIsual(); + } if (had_ctrl_v) { // Use a special (negative) number to make a difference between a // literal CR or NL and a line break. @@ -6011,24 +6182,25 @@ static void nv_replace(cmdarg_T *cap) return; } - /* Break tabs, etc. */ + // Break tabs, etc. if (virtual_active()) { - if (u_save_cursor() == false) + if (u_save_cursor() == false) { return; + } if (gchar_cursor() == NUL) { - /* Add extra space and put the cursor on the first one. */ + // Add extra space and put the cursor on the first one. coladvance_force((colnr_T)(getviscol() + cap->count1)); assert(cap->count1 <= INT_MAX); curwin->w_cursor.col -= (colnr_T)cap->count1; - } else if (gchar_cursor() == TAB) + } else if (gchar_cursor() == TAB) { coladvance_force(getviscol()); + } } - /* Abort if not enough characters to replace. */ + // Abort if not enough characters to replace. ptr = get_cursor_pos_ptr(); if (STRLEN(ptr) < (unsigned)cap->count1 - || (mb_charlen(ptr) < cap->count1) - ) { + || (mb_charlen(ptr) < cap->count1)) { clearopbeep(cap->oap); return; } @@ -6045,9 +6217,10 @@ static void nv_replace(cmdarg_T *cap) return; } - /* save line for undo */ - if (u_save_cursor() == false) + // save line for undo + if (u_save_cursor() == false) { return; + } if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n')) { /* @@ -6055,18 +6228,18 @@ static void nv_replace(cmdarg_T *cap) * Strange vi behaviour: Only one newline is inserted. * Delete the characters here. * Insert the newline with an insert command, takes care of - * autoindent. The insert command depends on being on the last + * autoindent. The insert command depends on being on the last * character of a line or not. */ - (void)del_chars(cap->count1, false); /* delete the characters */ + (void)del_chars(cap->count1, false); // delete the characters stuffcharReadbuff('\r'); stuffcharReadbuff(ESC); - /* Give 'r' to edit(), to get the redo command right. */ + // Give 'r' to edit(), to get the redo command right. invoke_edit(cap, true, 'r', false); } else { prep_redo(cap->oap->regname, cap->count1, - NUL, 'r', NUL, had_ctrl_v, cap->nchar); + NUL, 'r', NUL, had_ctrl_v, cap->nchar); curbuf->b_op_start = curwin->w_cursor; const int old_State = State; @@ -6103,7 +6276,7 @@ static void nv_replace(cmdarg_T *cap) ins_char(cap->ncharC2); } } - --curwin->w_cursor.col; /* cursor on the last replaced char */ + --curwin->w_cursor.col; // cursor on the last replaced char /* if the character on the left of the current cursor is a multi-byte * character, move two characters left */ mb_adjust_cursor(); @@ -6135,16 +6308,18 @@ static void v_swap_corners(int cmdchar) curwin->w_curswant = right; /* 'selection "exclusive" and cursor at right-bottom corner: move it * right one column */ - if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e') + if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e') { ++curwin->w_curswant; + } coladvance(curwin->w_curswant); if (curwin->w_cursor.col == old_cursor.col && (!virtual_active() - || curwin->w_cursor.coladd == old_cursor.coladd) - ) { + || curwin->w_cursor.coladd == + old_cursor.coladd)) { curwin->w_cursor.lnum = VIsual.lnum; - if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e') + if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e') { ++right; + } coladvance(right); VIsual = curwin->w_cursor; @@ -6165,18 +6340,19 @@ static void v_swap_corners(int cmdchar) */ static void nv_Replace(cmdarg_T *cap) { - if (VIsual_active) { /* "R" is replace lines */ + if (VIsual_active) { // "R" is replace lines cap->cmdchar = 'c'; cap->nchar = NUL; - VIsual_mode_orig = VIsual_mode; /* remember original area for gv */ + VIsual_mode_orig = VIsual_mode; // remember original area for gv VIsual_mode = 'V'; nv_operator(cap); } else if (!checkclearopq(cap->oap)) { if (!MODIFIABLE(curbuf)) { EMSG(_(e_modifiable)); } else { - if (virtual_active()) + if (virtual_active()) { coladvance(getviscol()); + } invoke_edit(cap, false, cap->arg ? 'V' : 'R', false); } } @@ -6190,17 +6366,19 @@ static void nv_vreplace(cmdarg_T *cap) if (VIsual_active) { cap->cmdchar = 'r'; cap->nchar = cap->extra_char; - nv_replace(cap); /* Do same as "r" in Visual mode for now */ + nv_replace(cap); // Do same as "r" in Visual mode for now } else if (!checkclearopq(cap->oap)) { if (!MODIFIABLE(curbuf)) { EMSG(_(e_modifiable)); } else { - if (cap->extra_char == Ctrl_V) /* get another character */ + if (cap->extra_char == Ctrl_V) { // get another character cap->extra_char = get_literal(); + } stuffcharReadbuff(cap->extra_char); stuffcharReadbuff(ESC); - if (virtual_active()) + if (virtual_active()) { coladvance(getviscol()); + } invoke_edit(cap, true, 'v', false); } } @@ -6226,8 +6404,9 @@ static void n_swapchar(cmdarg_T *cap) prep_redo_cmd(cap); - if (u_save_cursor() == false) + if (u_save_cursor() == false) { return; + } startpos = curwin->w_cursor; for (n = cap->count1; n > 0; --n) { @@ -6239,12 +6418,14 @@ static void n_swapchar(cmdarg_T *cap) ++curwin->w_cursor.lnum; curwin->w_cursor.col = 0; if (n > 1) { - if (u_savesub(curwin->w_cursor.lnum) == false) + if (u_savesub(curwin->w_cursor.lnum) == false) { break; + } u_clearline(); } - } else + } else { break; + } } } @@ -6256,8 +6437,9 @@ static void n_swapchar(cmdarg_T *cap) 0L, true); curbuf->b_op_start = startpos; curbuf->b_op_end = curwin->w_cursor; - if (curbuf->b_op_end.col > 0) + if (curbuf->b_op_end.col > 0) { --curbuf->b_op_end.col; + } } } @@ -6266,19 +6448,21 @@ static void n_swapchar(cmdarg_T *cap) */ static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos) { - if (check_mark(pos) == false) + if (check_mark(pos) == false) { clearop(cap->oap); - else { + } else { if (cap->cmdchar == '\'' || cap->cmdchar == '`' || cap->cmdchar == '[' - || cap->cmdchar == ']') + || cap->cmdchar == ']') { setpcmark(); + } curwin->w_cursor = *pos; - if (flag) + if (flag) { beginline(BL_WHITE | BL_FIX); - else + } else { check_cursor(); + } } cap->oap->motion_type = flag ? kMTLineWise : kMTCharWise; if (cap->cmdchar == '`') { @@ -6301,8 +6485,9 @@ static void v_visop(cmdarg_T *cap) if (VIsual_mode != Ctrl_V) { VIsual_mode_orig = VIsual_mode; VIsual_mode = 'V'; - } else if (cap->cmdchar == 'C' || cap->cmdchar == 'D') + } else if (cap->cmdchar == 'C' || cap->cmdchar == 'D') { curwin->w_curswant = MAXCOL; + } } cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1); nv_operator(cap); @@ -6324,8 +6509,9 @@ static void nv_subst(cmdarg_T *cap) } cap->cmdchar = 'c'; nv_operator(cap); - } else + } else { nv_optrans(cap); + } } /* @@ -6333,14 +6519,15 @@ static void nv_subst(cmdarg_T *cap) */ static void nv_abbrev(cmdarg_T *cap) { - if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL) - cap->cmdchar = 'x'; /* DEL key behaves like 'x' */ - - /* in Visual mode these commands are operators */ - if (VIsual_active) + if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL) { + cap->cmdchar = 'x'; // DEL key behaves like 'x' + } + // in Visual mode these commands are operators + if (VIsual_active) { v_visop(cap); - else + } else { nv_optrans(cap); + } } /* @@ -6372,19 +6559,22 @@ static void nv_gomark(cmdarg_T *cap) pos_T old_cursor = curwin->w_cursor; const bool old_KeyTyped = KeyTyped; // getting file may reset it - if (cap->cmdchar == 'g') + if (cap->cmdchar == 'g') { c = cap->extra_char; - else + } else { c = cap->nchar; + } pos = getmark(c, (cap->oap->op_type == OP_NOP)); - if (pos == (pos_T *)-1) { /* jumped to other file */ + if (pos == (pos_T *)-1) { // jumped to other file if (cap->arg) { check_cursor_lnum(); beginline(BL_WHITE | BL_FIX); - } else + } else { check_cursor(); - } else + } + } else { nv_cursormark(cap, cap->arg, pos); + } // May need to clear the coladd that a mark includes. if (!virtual_active()) { @@ -6420,22 +6610,25 @@ static void nv_pcmark(cmdarg_T *cap) if (pos == (pos_T *)-1) { // jump to other file curwin->w_set_curswant = true; check_cursor(); - } else if (pos != NULL) /* can jump */ + } else if (pos != NULL) { // can jump nv_cursormark(cap, false, pos); - else if (cap->cmdchar == 'g') { - if (curbuf->b_changelistlen == 0) + } else if (cap->cmdchar == 'g') { + if (curbuf->b_changelistlen == 0) { EMSG(_("E664: changelist is empty")); - else if (cap->count1 < 0) + } else if (cap->count1 < 0) { EMSG(_("E662: At start of changelist")); - else + } else { EMSG(_("E663: At end of changelist")); - } else + } + } else { clearopbeep(cap->oap); + } if (cap->oap->op_type == OP_NOP && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum) && (fdo_flags & FDO_MARK) - && old_KeyTyped) + && old_KeyTyped) { foldOpenCursor(); + } } } @@ -6444,16 +6637,19 @@ static void nv_pcmark(cmdarg_T *cap) */ static void nv_regname(cmdarg_T *cap) { - if (checkclearop(cap->oap)) + if (checkclearop(cap->oap)) { return; - if (cap->nchar == '=') + } + if (cap->nchar == '=') { cap->nchar = get_expr_register(); + } if (cap->nchar != NUL && valid_yank_reg(cap->nchar, false)) { cap->oap->regname = cap->nchar; - cap->opcount = cap->count0; /* remember count before '"' */ + cap->opcount = cap->count0; // remember count before '"' set_reg_var(cap->oap->regname); - } else + } else { clearopbeep(cap->oap); + } } /* @@ -6464,8 +6660,9 @@ static void nv_regname(cmdarg_T *cap) */ static void nv_visual(cmdarg_T *cap) { - if (cap->cmdchar == Ctrl_Q) + if (cap->cmdchar == Ctrl_Q) { cap->cmdchar = Ctrl_V; + } // 'v', 'V' and CTRL-V can be used while an operator is pending to make it // charwise, linewise, or blockwise. @@ -6476,28 +6673,30 @@ static void nv_visual(cmdarg_T *cap) } VIsual_select = cap->arg; - if (VIsual_active) { /* change Visual mode */ - if (VIsual_mode == cap->cmdchar) /* stop visual mode */ + if (VIsual_active) { // change Visual mode + if (VIsual_mode == cap->cmdchar) { // stop visual mode end_visual_mode(); - else { /* toggle char/block mode */ - /* or char/line mode */ + } else { // toggle char/block mode + // or char/line mode VIsual_mode = cap->cmdchar; showmode(); } redraw_curbuf_later(INVERTED); // update the inversion } else { // start Visual mode if (cap->count0 > 0 && resel_VIsual_mode != NUL) { - /* use previously selected part */ + // use previously selected part VIsual = curwin->w_cursor; VIsual_active = true; VIsual_reselect = true; - if (!cap->arg) - /* start Select mode when 'selectmode' contains "cmd" */ + if (!cap->arg) { + // start Select mode when 'selectmode' contains "cmd" may_start_select('c'); + } setmouse(); - if (p_smd && msg_silent == 0) - redraw_cmdline = true; /* show visual mode later */ + if (p_smd && msg_silent == 0) { + redraw_cmdline = true; // show visual mode later + } /* * For V and ^V, we multiply the number of lines even if there * was only one -- webb @@ -6505,8 +6704,9 @@ static void nv_visual(cmdarg_T *cap) if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1) { curwin->w_cursor.lnum += resel_VIsual_line_count * cap->count0 - 1; - if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) + if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) { curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; + } } VIsual_mode = resel_VIsual_mode; if (VIsual_mode == 'v') { @@ -6515,8 +6715,9 @@ static void nv_visual(cmdarg_T *cap) assert(cap->count0 >= INT_MIN && cap->count0 <= INT_MAX); curwin->w_curswant = (curwin->w_virtcol + resel_VIsual_vcol * (int)cap->count0 - 1); - } else + } else { curwin->w_curswant = resel_VIsual_vcol; + } coladvance(curwin->w_curswant); } if (resel_VIsual_vcol == MAXCOL) { @@ -6528,22 +6729,26 @@ static void nv_visual(cmdarg_T *cap) curwin->w_curswant = (curwin->w_virtcol + resel_VIsual_vcol * (int)cap->count0 - 1); coladvance(curwin->w_curswant); - } else + } else { curwin->w_set_curswant = true; - redraw_curbuf_later(INVERTED); /* show the inversion */ + } + redraw_curbuf_later(INVERTED); // show the inversion } else { - if (!cap->arg) - /* start Select mode when 'selectmode' contains "cmd" */ + if (!cap->arg) { + // start Select mode when 'selectmode' contains "cmd" may_start_select('c'); + } n_start_visual_mode(cap->cmdchar); - if (VIsual_mode != 'V' && *p_sel == 'e') - ++cap->count1; /* include one more char */ + if (VIsual_mode != 'V' && *p_sel == 'e') { + ++cap->count1; // include one more char + } if (cap->count0 > 0 && --cap->count1 > 0) { - /* With a count select that many characters or lines. */ - if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V) + // With a count select that many characters or lines. + if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V) { nv_right(cap); - else if (VIsual_mode == 'V') + } else if (VIsual_mode == 'V') { nv_down(cap); + } } } } @@ -6554,7 +6759,7 @@ static void nv_visual(cmdarg_T *cap) */ void start_selection(void) { - /* if 'selectmode' contains "key", start Select mode */ + // if 'selectmode' contains "key", start Select mode may_start_select('k'); n_start_visual_mode('v'); } @@ -6592,9 +6797,9 @@ static void n_start_visual_mode(int c) // Check for redraw after changing the state. conceal_check_cursor_line(); - if (p_smd && msg_silent == 0) - redraw_cmdline = true; /* show visual mode later */ - + if (p_smd && msg_silent == 0) { + redraw_cmdline = true; // show visual mode later + } /* Only need to redraw this line, unless still need to redraw an old * Visual area (when 'lazyredraw' is set). */ if (curwin->w_redr_type < INVERTED) { @@ -6625,8 +6830,9 @@ static void nv_window(cmdarg_T *cap) static void nv_suspend(cmdarg_T *cap) { clearop(cap->oap); - if (VIsual_active) - end_visual_mode(); /* stop Visual mode */ + if (VIsual_active) { + end_visual_mode(); // stop Visual mode + } do_cmdline_cmd("st"); } @@ -6670,18 +6876,19 @@ static void nv_g_cmd(cmdarg_T *cap) /* * "gv": Reselect the previous Visual area. If Visual already active, - * exchange previous and current Visual area. + * exchange previous and current Visual area. */ case 'v': - if (checkclearop(oap)) + if (checkclearop(oap)) { break; + } - if ( curbuf->b_visual.vi_start.lnum == 0 - || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count - || curbuf->b_visual.vi_end.lnum == 0) + if (curbuf->b_visual.vi_start.lnum == 0 + || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count + || curbuf->b_visual.vi_end.lnum == 0) { beep_flush(); - else { - /* set w_cursor to the start of the Visual area, tpos to the end */ + } else { + // set w_cursor to the start of the Visual area, tpos to the end if (VIsual_active) { i = VIsual_mode; VIsual_mode = curbuf->b_visual.vi_mode; @@ -6727,7 +6934,7 @@ static void nv_g_cmd(cmdarg_T *cap) break; /* * "gV": Don't reselect the previous Visual area after a Select mode - * mapping of menu. + * mapping of menu. */ case 'V': VIsual_reselect = false; @@ -6755,8 +6962,9 @@ static void nv_g_cmd(cmdarg_T *cap) */ case 'N': case 'n': - if (!current_search(cap->count1, cap->nchar == 'n')) + if (!current_search(cap->count1, cap->nchar == 'n')) { clearopbeep(oap); + } break; /* @@ -6769,10 +6977,12 @@ static void nv_g_cmd(cmdarg_T *cap) if (!curwin->w_p_wrap) { oap->motion_type = kMTLineWise; i = cursor_down(cap->count1, oap->op_type == OP_NOP); - } else + } else { i = nv_screengo(oap, FORWARD, cap->count1); - if (!i) + } + if (!i) { clearopbeep(oap); + } break; case 'k': @@ -6781,10 +6991,12 @@ static void nv_g_cmd(cmdarg_T *cap) if (!curwin->w_p_wrap) { oap->motion_type = kMTLineWise; i = cursor_up(cap->count1, oap->op_type == OP_NOP); - } else + } else { i = nv_screengo(oap, BACKWARD, cap->count1); - if (!i) + } + if (!i) { clearopbeep(oap); + } break; /* @@ -6809,17 +7021,18 @@ static void nv_g_cmd(cmdarg_T *cap) oap->motion_type = kMTCharWise; oap->inclusive = false; if (curwin->w_p_wrap - && curwin->w_width_inner != 0 - ) { + && curwin->w_width_inner != 0) { int width1 = curwin->w_width_inner - curwin_col_off(); int width2 = width1 + curwin_col_off2(); validate_virtcol(); i = 0; - if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0) + if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0) { i = (curwin->w_virtcol - width1) / width2 * width2 + width1; - } else + } + } else { i = curwin->w_leftcol; + } /* Go to the middle of the screen line. When 'number' or * 'relativenumber' is on and lines are wrapping the middle can be more * to the left. */ @@ -6838,8 +7051,7 @@ static void nv_g_cmd(cmdarg_T *cap) curwin->w_set_curswant = true; break; - case 'M': - { + case 'M': { const char_u *const ptr = get_cursor_line_ptr(); oap->motion_type = kMTCharWise; @@ -6861,19 +7073,21 @@ static void nv_g_cmd(cmdarg_T *cap) cap->oap->inclusive = true; curwin->w_curswant = MAXCOL; if (cursor_down(cap->count1 - 1, - cap->oap->op_type == OP_NOP) == false) + cap->oap->op_type == OP_NOP) == false) { clearopbeep(cap->oap); - else { + } else { char_u *ptr = get_cursor_line_ptr(); - /* In Visual mode we may end up after the line. */ - if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL) + // In Visual mode we may end up after the line. + if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL) { --curwin->w_cursor.col; + } - /* Decrease the cursor column until it's on a non-blank. */ + // Decrease the cursor column until it's on a non-blank. while (curwin->w_cursor.col > 0 - && ascii_iswhite(ptr[curwin->w_cursor.col])) + && ascii_iswhite(ptr[curwin->w_cursor.col])) { --curwin->w_cursor.col; + } curwin->w_set_curswant = true; adjust_for_sel(cap); } @@ -6881,57 +7095,58 @@ static void nv_g_cmd(cmdarg_T *cap) case '$': case K_END: - case K_KEND: - { - int col_off = curwin_col_off(); + case K_KEND: { + int col_off = curwin_col_off(); - oap->motion_type = kMTCharWise; - oap->inclusive = true; - if (curwin->w_p_wrap - && curwin->w_width_inner != 0 - ) { - curwin->w_curswant = MAXCOL; /* so we stay at the end */ - if (cap->count1 == 1) { - int width1 = curwin->w_width_inner - col_off; - int width2 = width1 + curwin_col_off2(); + oap->motion_type = kMTCharWise; + oap->inclusive = true; + if (curwin->w_p_wrap + && curwin->w_width_inner != 0) { + curwin->w_curswant = MAXCOL; // so we stay at the end + if (cap->count1 == 1) { + int width1 = curwin->w_width_inner - col_off; + int width2 = width1 + curwin_col_off2(); - validate_virtcol(); - i = width1 - 1; - if (curwin->w_virtcol >= (colnr_T)width1) - i += ((curwin->w_virtcol - width1) / width2 + 1) - * width2; + validate_virtcol(); + i = width1 - 1; + if (curwin->w_virtcol >= (colnr_T)width1) { + i += ((curwin->w_virtcol - width1) / width2 + 1) + * width2; + } + coladvance((colnr_T)i); + + // Make sure we stick in this column. + validate_virtcol(); + curwin->w_curswant = curwin->w_virtcol; + curwin->w_set_curswant = false; + if (curwin->w_cursor.col > 0 && curwin->w_p_wrap) { + /* + * Check for landing on a character that got split at + * the end of the line. We do not want to advance to + * the next screen line. + */ + if (curwin->w_virtcol > (colnr_T)i) { + --curwin->w_cursor.col; + } + } + } else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == false) { + clearopbeep(oap); + } + } else { + if (cap->count1 > 1) { + // if it fails, let the cursor still move to the last char + (void)cursor_down(cap->count1 - 1, false); + } + i = curwin->w_leftcol + curwin->w_width_inner - col_off - 1; coladvance((colnr_T)i); - /* Make sure we stick in this column. */ + // Make sure we stick in this column. validate_virtcol(); curwin->w_curswant = curwin->w_virtcol; curwin->w_set_curswant = false; - if (curwin->w_cursor.col > 0 && curwin->w_p_wrap) { - /* - * Check for landing on a character that got split at - * the end of the line. We do not want to advance to - * the next screen line. - */ - if (curwin->w_virtcol > (colnr_T)i) - --curwin->w_cursor.col; - } - } else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == false) - clearopbeep(oap); - } else { - if (cap->count1 > 1) { - // if it fails, let the cursor still move to the last char - (void)cursor_down(cap->count1 - 1, false); } - i = curwin->w_leftcol + curwin->w_width_inner - col_off - 1; - coladvance((colnr_T)i); - - // Make sure we stick in this column. - validate_virtcol(); - curwin->w_curswant = curwin->w_virtcol; - curwin->w_set_curswant = false; } - } - break; + break; /* * "g*" and "g#", like "*" and "#" but without using "\<" and "\>" @@ -6939,10 +7154,10 @@ static void nv_g_cmd(cmdarg_T *cap) case '*': case '#': #if POUND != '#' - case POUND: /* pound sign (sometimes equal to '#') */ + case POUND: // pound sign (sometimes equal to '#') #endif - case Ctrl_RSB: /* :tag or :tselect for current identifier */ - case ']': /* :tselect for current identifier */ + case Ctrl_RSB: // :tag or :tselect for current identifier + case ']': // :tselect for current identifier nv_ident(cap); break; @@ -6954,8 +7169,9 @@ static void nv_g_cmd(cmdarg_T *cap) oap->motion_type = kMTCharWise; curwin->w_set_curswant = true; oap->inclusive = true; - if (bckend_word(cap->count1, cap->nchar == 'E', false) == false) + if (bckend_word(cap->count1, cap->nchar == 'E', false) == false) { clearopbeep(oap); + } break; // "g CTRL-G": display info about cursor position @@ -6970,8 +7186,9 @@ static void nv_g_cmd(cmdarg_T *cap) check_cursor_lnum(); i = (int)STRLEN(get_cursor_line_ptr()); if (curwin->w_cursor.col > (colnr_T)i) { - if (virtual_active()) + if (virtual_active()) { curwin->w_cursor.coladd += curwin->w_cursor.col - i; + } curwin->w_cursor.col = i; } } @@ -6984,8 +7201,9 @@ static void nv_g_cmd(cmdarg_T *cap) */ case 'I': beginline(0); - if (!checkclearopq(oap)) + if (!checkclearopq(oap)) { invoke_edit(cap, false, 'g', false); + } break; /* @@ -6997,7 +7215,7 @@ static void nv_g_cmd(cmdarg_T *cap) nv_gotofile(cap); break; - /* "g'm" and "g`m": jump to mark without setting pcmark */ + // "g'm" and "g`m": jump to mark without setting pcmark case '\'': cap->arg = true; FALLTHROUGH; @@ -7014,7 +7232,7 @@ static void nv_g_cmd(cmdarg_T *cap) /* * "ga": Display the ascii value of the character under the - * cursor. It is displayed in decimal, hex, and octal. -- webb + * cursor. It is displayed in decimal, hex, and octal. -- webb */ case 'a': do_ascii(NULL); @@ -7022,14 +7240,15 @@ static void nv_g_cmd(cmdarg_T *cap) /* * "g8": Display the bytes used for the UTF-8 character under the - * cursor. It is displayed in hex. + * cursor. It is displayed in hex. * "8g8" finds illegal byte sequence. */ case '8': - if (cap->count0 == 8) + if (cap->count0 == 8) { utf_find_illegal(); - else + } else { show_utf8(); + } break; // "g<": show scrollback text case '<': @@ -7046,14 +7265,14 @@ static void nv_g_cmd(cmdarg_T *cap) break; /* - * Two-character operators: - * "gq" Format text - * "gw" Format text and keep cursor position - * "g~" Toggle the case of the text. - * "gu" Change text to lower case. - * "gU" Change text to upper case. - * "g?" rot13 encoding - * "g@" call 'operatorfunc' + * Two-character operators: + * "gq" Format text + * "gw" Format text and keep cursor position + * "g~" Toggle the case of the text. + * "gu" Change text to lower case. + * "gU" Change text to upper case. + * "g?" rot13 encoding + * "g@" call 'operatorfunc' */ case 'q': case 'w': @@ -7069,7 +7288,7 @@ static void nv_g_cmd(cmdarg_T *cap) /* * "gd": Find first occurrence of pattern under the cursor in the - * current function + * current function * "gD": idem, but in the current file. */ case 'd': @@ -7111,12 +7330,12 @@ static void nv_g_cmd(cmdarg_T *cap) nv_put(cap); break; - /* "go": goto byte count from start of buffer */ + // "go": goto byte count from start of buffer case 'o': goto_byte(cap->count0); break; - /* "gQ": improved Ex mode */ + // "gQ": improved Ex mode case 'Q': if (text_locked()) { clearopbeep(cap->oap); @@ -7139,12 +7358,14 @@ static void nv_g_cmd(cmdarg_T *cap) break; case 't': - if (!checkclearop(oap)) + if (!checkclearop(oap)) { goto_tabpage((int)cap->count0); + } break; case 'T': - if (!checkclearop(oap)) + if (!checkclearop(oap)) { goto_tabpage(-(int)cap->count1); + } break; case TAB: if (!checkclearop(oap)) { @@ -7153,10 +7374,11 @@ static void nv_g_cmd(cmdarg_T *cap) break; case '+': - case '-': /* "g+" and "g-": undo or redo along the timeline */ - if (!checkclearopq(oap)) + case '-': // "g+" and "g-": undo or redo along the timeline + if (!checkclearopq(oap)) { undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1, - false, false, false); + false, false, false); + } break; default: @@ -7171,19 +7393,20 @@ static void nv_g_cmd(cmdarg_T *cap) static void n_opencmd(cmdarg_T *cap) { if (!checkclearopq(cap->oap)) { - if (cap->cmdchar == 'O') - /* Open above the first line of a folded sequence of lines */ + if (cap->cmdchar == 'O') { + // Open above the first line of a folded sequence of lines (void)hasFolding(curwin->w_cursor.lnum, - &curwin->w_cursor.lnum, NULL); - else - /* Open below the last line of a folded sequence of lines */ + &curwin->w_cursor.lnum, NULL); + } else { + // Open below the last line of a folded sequence of lines (void)hasFolding(curwin->w_cursor.lnum, - NULL, &curwin->w_cursor.lnum); + NULL, &curwin->w_cursor.lnum); + } if (u_save((linenr_T)(curwin->w_cursor.lnum - (cap->cmdchar == 'O' ? 1 : 0)), - (linenr_T)(curwin->w_cursor.lnum + - (cap->cmdchar == 'o' ? 1 : 0)) - ) + (linenr_T)(curwin->w_cursor.lnum + + (cap->cmdchar == 'o' ? 1 : 0)) + ) && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD, has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0, @@ -7208,8 +7431,9 @@ static void nv_dot(cmdarg_T *cap) * instead of the last command (inserting text). This is used for * CTRL-O <.> in insert mode. */ - if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == false) + if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == false) { clearopbeep(cap->oap); + } } } @@ -7229,11 +7453,10 @@ static void nv_redo(cmdarg_T *cap) */ static void nv_Undo(cmdarg_T *cap) { - /* In Visual mode and typing "gUU" triggers an operator */ + // In Visual mode and typing "gUU" triggers an operator if (cap->oap->op_type == OP_UPPER - || VIsual_active - ) { - /* translate "gUU" to "gUgU" */ + || VIsual_active) { + // translate "gUU" to "gUgU" cap->cmdchar = 'g'; cap->nchar = 'U'; nv_operator(cap); @@ -7278,9 +7501,9 @@ static void nv_operator(cmdarg_T *cap) return; } - if (op_type == cap->oap->op_type) /* double operator works on lines */ + if (op_type == cap->oap->op_type) { // double operator works on lines nv_lineop(cap); - else if (!checkclearop(cap->oap)) { + } else if (!checkclearop(cap->oap)) { cap->oap->start = curwin->w_cursor; cap->oap->op_type = op_type; set_op_var(op_type); @@ -7298,11 +7521,11 @@ static void set_op_var(int optype) char opchars[3]; int opchar0 = get_op_char(optype); assert(opchar0 >= 0 && opchar0 <= UCHAR_MAX); - opchars[0] = (char) opchar0; + opchars[0] = (char)opchar0; int opchar1 = get_extra_op_char(optype); assert(opchar1 >= 0 && opchar1 <= UCHAR_MAX); - opchars[1] = (char) opchar1; + opchars[1] = (char)opchar1; opchars[2] = NUL; set_vim_var_string(VV_OP, opchars, -1); @@ -7340,10 +7563,10 @@ static void nv_lineop(cmdarg_T *cap) */ static void nv_home(cmdarg_T *cap) { - /* CTRL-HOME is like "gg" */ - if (mod_mask & MOD_MASK_CTRL) + // CTRL-HOME is like "gg" + if (mod_mask & MOD_MASK_CTRL) { nv_goto(cap); - else { + } else { cap->count0 = 1; nv_pipe(cap); } @@ -7362,8 +7585,9 @@ static void nv_pipe(cmdarg_T *cap) if (cap->count0 > 0) { coladvance((colnr_T)(cap->count0 - 1)); curwin->w_curswant = (colnr_T)(cap->count0 - 1); - } else + } else { curwin->w_curswant = 0; + } /* keep curswant at the column where we wanted to go, not where * we ended; differs if line is too short */ curwin->w_set_curswant = false; @@ -7378,10 +7602,11 @@ static void nv_bck_word(cmdarg_T *cap) cap->oap->motion_type = kMTCharWise; cap->oap->inclusive = false; curwin->w_set_curswant = true; - if (bck_word(cap->count1, cap->arg, false) == false) + if (bck_word(cap->count1, cap->arg, false) == false) { clearopbeep(cap->oap); - else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP) + } else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP) { foldOpenCursor(); + } } /* @@ -7398,10 +7623,11 @@ static void nv_wordcmd(cmdarg_T *cap) /* * Set inclusive for the "E" and "e" command. */ - if (cap->cmdchar == 'e' || cap->cmdchar == 'E') + if (cap->cmdchar == 'e' || cap->cmdchar == 'E') { word_end = true; - else + } else { word_end = false; + } cap->oap->inclusive = word_end; /* @@ -7428,22 +7654,25 @@ static void nv_wordcmd(cmdarg_T *cap) cap->oap->motion_type = kMTCharWise; curwin->w_set_curswant = true; - if (word_end) + if (word_end) { n = end_word(cap->count1, cap->arg, flag, false); - else + } else { n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP); + } /* Don't leave the cursor on the NUL past the end of line. Unless we * didn't move it forward. */ - if (lt(startpos, curwin->w_cursor)) + if (lt(startpos, curwin->w_cursor)) { adjust_cursor(cap->oap); + } - if (n == false && cap->oap->op_type == OP_NOP) + if (n == false && cap->oap->op_type == OP_NOP) { clearopbeep(cap->oap); - else { + } else { adjust_for_sel(cap); - if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP) + if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP) { foldOpenCursor(); + } } } @@ -7461,8 +7690,8 @@ static void adjust_cursor(oparg_T *oap) */ if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL && (!VIsual_active || *p_sel == 'o') - && !virtual_active() && (ve_flags & VE_ONEMORE) == 0 - ) { + && !virtual_active() && + (ve_flags & VE_ONEMORE) == 0) { curwin->w_cursor.col--; // prevent cursor from moving on the trail byte mb_adjust_cursor(); @@ -7479,8 +7708,9 @@ static void nv_beginline(cmdarg_T *cap) cap->oap->motion_type = kMTCharWise; cap->oap->inclusive = false; beginline(cap->arg); - if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP) + if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP) { foldOpenCursor(); + } ins_at_eol = false; /* Don't move cursor past eol (only necessary in a one-character line). */ } @@ -7507,10 +7737,11 @@ static bool unadjust_for_sel(void) pos_T *pp; if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor)) { - if (lt(VIsual, curwin->w_cursor)) + if (lt(VIsual, curwin->w_cursor)) { pp = &curwin->w_cursor; - else + } else { pp = &VIsual; + } if (pp->coladd > 0) { pp->coladd--; } else if (pp->col > 0) { @@ -7530,10 +7761,10 @@ static bool unadjust_for_sel(void) */ static void nv_select(cmdarg_T *cap) { - if (VIsual_active) + if (VIsual_active) { VIsual_select = true; - else if (VIsual_reselect) { - cap->nchar = 'v'; /* fake "gv" command */ + } else if (VIsual_reselect) { + cap->nchar = 'v'; // fake "gv" command cap->arg = true; nv_g_cmd(cap); } @@ -7548,24 +7779,28 @@ static void nv_goto(cmdarg_T *cap) { linenr_T lnum; - if (cap->arg) + if (cap->arg) { lnum = curbuf->b_ml.ml_line_count; - else + } else { lnum = 1L; + } cap->oap->motion_type = kMTLineWise; setpcmark(); - /* When a count is given, use it instead of the default lnum */ - if (cap->count0 != 0) + // When a count is given, use it instead of the default lnum + if (cap->count0 != 0) { lnum = cap->count0; - if (lnum < 1L) + } + if (lnum < 1L) { lnum = 1L; - else if (lnum > curbuf->b_ml.ml_line_count) + } else if (lnum > curbuf->b_ml.ml_line_count) { lnum = curbuf->b_ml.ml_line_count; + } curwin->w_cursor.lnum = lnum; beginline(BL_SOL | BL_FIX); - if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP) + if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP) { foldOpenCursor(); + } } /* @@ -7575,20 +7810,24 @@ static void nv_normal(cmdarg_T *cap) { if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G) { clearop(cap->oap); - if (restart_edit != 0 && mode_displayed) - clear_cmdline = true; /* unshow mode later */ + if (restart_edit != 0 && mode_displayed) { + clear_cmdline = true; // unshow mode later + } restart_edit = 0; - if (cmdwin_type != 0) + if (cmdwin_type != 0) { cmdwin_result = Ctrl_C; + } if (VIsual_active) { - end_visual_mode(); /* stop Visual */ + end_visual_mode(); // stop Visual redraw_curbuf_later(INVERTED); } - /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */ - if (cap->nchar == Ctrl_G && p_im) + // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. + if (cap->nchar == Ctrl_G && p_im) { restart_edit = 'a'; - } else + } + } else { clearopbeep(cap->oap); + } } /* @@ -7605,7 +7844,7 @@ static void nv_esc(cmdarg_T *cap) && cap->oap->regname == 0 && !p_im); - if (cap->arg) { /* true for CTRL-C */ + if (cap->arg) { // true for CTRL-C if (restart_edit == 0 && cmdwin_type == 0 && !VIsual_active @@ -7620,11 +7859,12 @@ static void nv_esc(cmdarg_T *cap) /* Don't reset "restart_edit" when 'insertmode' is set, it won't be * set again below when halfway through a mapping. */ - if (!p_im) + if (!p_im) { restart_edit = 0; + } if (cmdwin_type != 0) { cmdwin_result = K_IGNORE; - got_int = false; /* don't stop executing autocommands et al. */ + got_int = false; // don't stop executing autocommands et al. return; } } else if (cmdwin_type != 0 && ex_normal_busy) { @@ -7636,8 +7876,8 @@ static void nv_esc(cmdarg_T *cap) } if (VIsual_active) { - end_visual_mode(); /* stop Visual */ - check_cursor_col(); /* make sure cursor is not beyond EOL */ + end_visual_mode(); // stop Visual + check_cursor_col(); // make sure cursor is not beyond EOL curwin->w_set_curswant = true; redraw_curbuf_later(INVERTED); } else if (no_reason) { @@ -7648,9 +7888,9 @@ static void nv_esc(cmdarg_T *cap) /* A CTRL-C is often used at the start of a menu. When 'insertmode' is * set return to Insert mode afterwards. */ if (restart_edit == 0 && goto_im() - && ex_normal_busy == 0 - ) + && ex_normal_busy == 0) { restart_edit = 'a'; + } } // Move the cursor for the "A" command. @@ -7681,7 +7921,7 @@ static void nv_edit(cmdarg_T *cap) // in Visual mode "A" and "I" are an operator if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I')) { v_visop(cap); - // in Visual mode and after an operator "a" and "i" are for text objects + // in Visual mode and after an operator "a" and "i" are for text objects } else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i') && (cap->oap->op_type != OP_NOP || VIsual_active)) { nv_object(cap); @@ -7695,20 +7935,21 @@ static void nv_edit(cmdarg_T *cap) set_cursor_for_append_to_line(); break; - case 'I': /* "I"nsert before the first non-blank */ + case 'I': // "I"nsert before the first non-blank beginline(BL_WHITE); break; - case 'a': /* "a"ppend is like "i"nsert on the next character. */ + case 'a': // "a"ppend is like "i"nsert on the next character. /* increment coladd when in virtual space, increment the * column otherwise, also to append after an unprintable char */ if (virtual_active() && (curwin->w_cursor.coladd > 0 || *get_cursor_pos_ptr() == NUL - || *get_cursor_pos_ptr() == TAB)) + || *get_cursor_pos_ptr() == TAB)) { curwin->w_cursor.coladd++; - else if (*get_cursor_pos_ptr() != NUL) + } else if (*get_cursor_pos_ptr() != NUL) { inc_cursor(); + } break; } @@ -7726,35 +7967,32 @@ static void nv_edit(cmdarg_T *cap) } } -/* - * Invoke edit() and take care of "restart_edit" and the return value. - */ -static void -invoke_edit ( - cmdarg_T *cap, - int repl, /* "r" or "gr" command */ - int cmd, - int startln -) +/// Invoke edit() and take care of "restart_edit" and the return value. +/// +/// @param repl "r" or "gr" command +static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln) { int restart_edit_save = 0; /* Complicated: When the user types "aa" we don't want to do Insert * mode recursively. But when doing "a." or "arx" we do allow * it. */ - if (repl || !stuff_empty()) + if (repl || !stuff_empty()) { restart_edit_save = restart_edit; - else + } else { restart_edit_save = 0; + } - /* Always reset "restart_edit", this is not a restarted edit. */ + // Always reset "restart_edit", this is not a restarted edit. restart_edit = 0; - if (edit(cmd, startln, cap->count1)) + if (edit(cmd, startln, cap->count1)) { cap->retval |= CA_COMMAND_BUSY; + } - if (restart_edit == 0) + if (restart_edit == 0) { restart_edit = restart_edit_save; + } } /* @@ -7766,41 +8004,41 @@ static void nv_object(cmdarg_T *cap) bool include; char_u *mps_save; - if (cap->cmdchar == 'i') - include = false; /* "ix" = inner object: exclude white space */ - else - include = true; /* "ax" = an object: include white space */ - - /* Make sure (), [], {} and <> are in 'matchpairs' */ + if (cap->cmdchar == 'i') { + include = false; // "ix" = inner object: exclude white space + } else { + include = true; // "ax" = an object: include white space + } + // Make sure (), [], {} and <> are in 'matchpairs' mps_save = curbuf->b_p_mps; curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>"; switch (cap->nchar) { - case 'w': /* "aw" = a word */ + case 'w': // "aw" = a word flag = current_word(cap->oap, cap->count1, include, false); break; - case 'W': /* "aW" = a WORD */ + case 'W': // "aW" = a WORD flag = current_word(cap->oap, cap->count1, include, true); break; - case 'b': /* "ab" = a braces block */ + case 'b': // "ab" = a braces block case '(': case ')': flag = current_block(cap->oap, cap->count1, include, '(', ')'); break; - case 'B': /* "aB" = a Brackets block */ + case 'B': // "aB" = a Brackets block case '{': case '}': flag = current_block(cap->oap, cap->count1, include, '{', '}'); break; - case '[': /* "a[" = a [] block */ + case '[': // "a[" = a [] block case ']': flag = current_block(cap->oap, cap->count1, include, '[', ']'); break; - case '<': /* "a<" = a <> block */ + case '<': // "a<" = a <> block case '>': flag = current_block(cap->oap, cap->count1, include, '<', '>'); break; - case 't': /* "at" = a tag block (xml and html) */ + case 't': // "at" = a tag block (xml and html) // Do not adjust oap->end in do_pending_operator() // otherwise there are different results for 'dit' // (note leading whitespace in last line): @@ -7810,17 +8048,17 @@ static void nv_object(cmdarg_T *cap) cap->retval |= CA_NO_ADJ_OP_END; flag = current_tagblock(cap->oap, cap->count1, include); break; - case 'p': /* "ap" = a paragraph */ + case 'p': // "ap" = a paragraph flag = current_par(cap->oap, cap->count1, include, 'p'); break; - case 's': /* "as" = a sentence */ + case 's': // "as" = a sentence flag = current_sent(cap->oap, cap->count1, include); break; - case '"': /* "a"" = a double quoted string */ - case '\'': /* "a'" = a single quoted string */ - case '`': /* "a`" = a backtick quoted string */ + case '"': // "a"" = a double quoted string + case '\'': // "a'" = a single quoted string + case '`': // "a`" = a backtick quoted string flag = current_quote(cap->oap, cap->count1, include, - cap->nchar); + cap->nchar); break; default: flag = false; @@ -7828,8 +8066,9 @@ static void nv_object(cmdarg_T *cap) } curbuf->b_p_mps = mps_save; - if (!flag) + if (!flag) { clearopbeep(cap->oap); + } adjust_cursor_col(); curwin->w_set_curswant = true; } @@ -7841,7 +8080,7 @@ static void nv_object(cmdarg_T *cap) static void nv_record(cmdarg_T *cap) { if (cap->oap->op_type == OP_FORMAT) { - /* "gqq" is the same as "gqgq": format line */ + // "gqq" is the same as "gqgq": format line cap->cmdchar = 'g'; cap->nchar = 'q'; nv_operator(cap); @@ -7864,11 +8103,13 @@ static void nv_record(cmdarg_T *cap) */ static void nv_at(cmdarg_T *cap) { - if (checkclearop(cap->oap)) + if (checkclearop(cap->oap)) { return; + } if (cap->nchar == '=') { - if (get_expr_register() == NUL) + if (get_expr_register() == NUL) { return; + } } while (cap->count1-- && !got_int) { if (do_execreg(cap->nchar, false, false, false) == false) { @@ -7886,10 +8127,11 @@ static void nv_halfpage(cmdarg_T *cap) { if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1) || (cap->cmdchar == Ctrl_D - && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)) + && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)) { clearopbeep(cap->oap); - else if (!checkclearop(cap->oap)) + } else if (!checkclearop(cap->oap)) { halfpage(cap->cmdchar == Ctrl_D, cap->count0); + } } /* @@ -7939,7 +8181,7 @@ static void nv_put_opt(cmdarg_T *cap, bool fix_indent) int flags = 0; if (cap->oap->op_type != OP_NOP) { - /* "dp" is ":diffput" */ + // "dp" is ":diffput" if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p') { clearop(cap->oap); assert(cap->opcount >= 0); @@ -7976,7 +8218,7 @@ static void nv_put_opt(cmdarg_T *cap, bool fix_indent) regname = cap->oap->regname; // '+' and '*' could be the same selection bool clipoverwrite = (regname == '+' || regname == '*') - && (cb_flags & CB_UNNAMEDMASK); + && (cb_flags & CB_UNNAMEDMASK); if (regname == 0 || regname == '"' || clipoverwrite || ascii_isdigit(regname) || regname == '-') { // The delete might overwrite the register we want to put, save it first @@ -8017,11 +8259,12 @@ static void nv_put_opt(cmdarg_T *cap, bool fix_indent) if ((VIsual_mode != 'V' && curwin->w_cursor.col < curbuf->b_op_start.col) || (VIsual_mode == 'V' - && curwin->w_cursor.lnum < curbuf->b_op_start.lnum)) + && curwin->w_cursor.lnum < curbuf->b_op_start.lnum)) { /* cursor is at the end of the line or end of file, put * forward. */ dir = FORWARD; - /* May have been reset in do_put(). */ + } + // May have been reset in do_put(). VIsual_active = true; } do_put(cap->oap->regname, savereg, dir, cap->count1, flags); @@ -8065,7 +8308,7 @@ static void nv_put_opt(cmdarg_T *cap, bool fix_indent) */ static void nv_open(cmdarg_T *cap) { - /* "do" is ":diffget" */ + // "do" is ":diffget" if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o') { clearop(cap->oap); assert(cap->opcount >= 0); @@ -8080,12 +8323,10 @@ static void nv_open(cmdarg_T *cap) } } -// Calculate start/end virtual columns for operating in block mode. -static void get_op_vcol( - oparg_T *oap, - colnr_T redo_VIsual_vcol, - bool initial // when true: adjust position for 'selectmode' -) +/// Calculate start/end virtual columns for operating in block mode. +/// +/// @param initial when true: adjust position for 'selectmode' +static void get_op_vcol(oparg_T *oap, colnr_T redo_VIsual_vcol, bool initial) { colnr_T start; colnr_T end; diff --git a/src/nvim/ops.c b/src/nvim/ops.c index a06db4a551..b493200005 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -11,27 +11,27 @@ #include #include -#include "nvim/vim.h" #include "nvim/ascii.h" -#include "nvim/ops.h" +#include "nvim/assert.h" #include "nvim/buffer.h" #include "nvim/change.h" #include "nvim/charset.h" #include "nvim/cursor.h" -#include "nvim/assert.h" #include "nvim/edit.h" #include "nvim/eval.h" #include "nvim/eval/typval.h" #include "nvim/ex_cmds.h" #include "nvim/ex_cmds2.h" #include "nvim/ex_getln.h" +#include "nvim/extmark.h" #include "nvim/fileio.h" #include "nvim/fold.h" #include "nvim/getchar.h" #include "nvim/indent.h" +#include "nvim/lib/kvec.h" #include "nvim/log.h" +#include "nvim/macros.h" #include "nvim/mark.h" -#include "nvim/extmark.h" #include "nvim/mbyte.h" #include "nvim/memline.h" #include "nvim/memory.h" @@ -39,7 +39,10 @@ #include "nvim/misc1.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/time.h" #include "nvim/path.h" #include "nvim/plines.h" #include "nvim/screen.h" @@ -49,15 +52,12 @@ #include "nvim/terminal.h" #include "nvim/ui.h" #include "nvim/undo.h" -#include "nvim/macros.h" +#include "nvim/vim.h" #include "nvim/window.h" -#include "nvim/lib/kvec.h" -#include "nvim/os/input.h" -#include "nvim/os/time.h" static yankreg_T y_regs[NUM_REGISTERS]; -static yankreg_T *y_previous = NULL; /* ptr to last written yankreg */ +static yankreg_T *y_previous = NULL; // ptr to last written yankreg // for behavior between start_batch_changes() and end_batch_changes()) static int batch_change_count = 0; // inside a script @@ -70,20 +70,20 @@ static bool clipboard_didwarn = false; * also op_change, op_shift, op_insert, op_replace - AKelly */ struct block_def { - int startspaces; /* 'extra' cols before first char */ - int endspaces; /* 'extra' cols after last char */ - int textlen; /* chars in block */ - char_u *textstart; /* pointer to 1st char (partially) in block */ - colnr_T textcol; /* index of chars (partially) in block */ - colnr_T start_vcol; /* start col of 1st char wholly inside block */ - colnr_T end_vcol; /* start col of 1st char wholly after block */ - int is_short; /* TRUE if line is too short to fit in block */ - int is_MAX; /* TRUE if curswant==MAXCOL when starting */ - int is_oneChar; /* TRUE if block within one character */ - int pre_whitesp; /* screen cols of ws before block */ - int pre_whitesp_c; /* chars of ws before block */ - colnr_T end_char_vcols; /* number of vcols of post-block char */ - colnr_T start_char_vcols; /* number of vcols of pre-block char */ + int startspaces; // 'extra' cols before first char + int endspaces; // 'extra' cols after last char + int textlen; // chars in block + char_u *textstart; // pointer to 1st char (partially) in block + colnr_T textcol; // index of chars (partially) in block + colnr_T start_vcol; // start col of 1st char wholly inside block + colnr_T end_vcol; // start col of 1st char wholly after block + int is_short; // TRUE if line is too short to fit in block + int is_MAX; // TRUE if curswant==MAXCOL when starting + int is_oneChar; // TRUE if block within one character + int pre_whitesp; // screen cols of ws before block + int pre_whitesp_c; // chars of ws before block + colnr_T end_char_vcols; // number of vcols of post-block char + colnr_T start_char_vcols; // number of vcols of pre-block char }; #ifdef INCLUDE_GENERATED_DECLARATIONS @@ -183,7 +183,7 @@ int op_on_lines(int op) // Return TRUE if operator "op" changes text. int op_is_change(int op) { - return opchars[op][2] & OPF_CHANGE; + return opchars[op][2] & OPF_CHANGE; } /* @@ -214,8 +214,9 @@ void op_shift(oparg_T *oap, int curs_top, int amount) int block_col = 0; if (u_save((linenr_T)(oap->start.lnum - 1), - (linenr_T)(oap->end.lnum + 1)) == FAIL) + (linenr_T)(oap->end.lnum + 1)) == FAIL) { return; + } if (oap->motion_type == kMTBlockWise) { block_col = curwin->w_cursor.col; @@ -238,32 +239,35 @@ void op_shift(oparg_T *oap, int curs_top, int amount) if (oap->motion_type == kMTBlockWise) { curwin->w_cursor.lnum = oap->start.lnum; curwin->w_cursor.col = block_col; - } else if (curs_top) { /* put cursor on first line, for ">>" */ + } else if (curs_top) { // put cursor on first line, for ">>" curwin->w_cursor.lnum = oap->start.lnum; - beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */ - } else - --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */ - + beginline(BL_SOL | BL_FIX); // shift_line() may have set cursor.col + } else { + --curwin->w_cursor.lnum; // put cursor on last line, for ":>" + } // The cursor line is not in a closed fold foldOpenCursor(); if (oap->line_count > p_report) { - if (oap->op_type == OP_RSHIFT) + if (oap->op_type == OP_RSHIFT) { s = (char_u *)">"; - else - s = (char_u *)"<"; - if (oap->line_count == 1) { - if (amount == 1) - sprintf((char *)IObuff, _("1 line %sed 1 time"), s); - else - sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount); } else { - if (amount == 1) + s = (char_u *)"<"; + } + if (oap->line_count == 1) { + if (amount == 1) { + sprintf((char *)IObuff, _("1 line %sed 1 time"), s); + } else { + sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount); + } + } else { + if (amount == 1) { sprintf((char *)IObuff, _("%" PRId64 " lines %sed 1 time"), - (int64_t)oap->line_count, s); - else + (int64_t)oap->line_count, s); + } else { sprintf((char *)IObuff, _("%" PRId64 " lines %sed %d times"), - (int64_t)oap->line_count, s, amount); + (int64_t)oap->line_count, s, amount); + } } msg_attr_keep(IObuff, 0, true, false); } @@ -281,14 +285,11 @@ void op_shift(oparg_T *oap, int curs_top, int amount) changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L, true); } -// Shift the current line one shiftwidth left (if left != 0) or right -// leaves cursor on first blank in the line. -void shift_line( - int left, - int round, - int amount, - int call_changed_bytes // call changed_bytes() -) +/// Shift the current line one shiftwidth left (if left != 0) or right +/// leaves cursor on first blank in the line. +/// +/// @param call_changed_bytes call changed_bytes() +void shift_line(int left, int round, int amount, int call_changed_bytes) { int count; int i, j; @@ -304,18 +305,22 @@ void shift_line( } if (left) { i -= amount; - if (i < 0) + if (i < 0) { i = 0; - } else + } + } else { i += amount; + } count = i * p_sw; } else { // original vi indent if (left) { count -= p_sw * amount; - if (count < 0) + if (count < 0) { count = 0; - } else + } + } else { count += p_sw * amount; + } } // Set new indent @@ -344,7 +349,7 @@ static void shift_block(oparg_T *oap, int amount) int i = 0, j = 0; const int old_p_ri = p_ri; - p_ri = 0; /* don't want revins in indent */ + p_ri = 0; // don't want revins in indent State = INSERT; // don't want REPLACE for State block_prep(oap, &bd, curwin->w_cursor.lnum, true); @@ -408,7 +413,7 @@ static void shift_block(oparg_T *oap, int amount) newlen = i+j; memset(newp + bd.textcol, TAB, (size_t)i); memset(newp + bd.textcol + i, ' ', (size_t)j); - /* the end */ + // the end memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len); } else { // left colnr_T destination_col; // column to which text in block will @@ -462,15 +467,17 @@ static void shift_block(oparg_T *oap, int amount) /* If "bd.startspaces" is set, "bd.textstart" points to the character * preceding the block. We have to subtract its width to obtain its * column number. */ - if (bd.startspaces) + if (bd.startspaces) { verbatim_copy_width -= bd.start_char_vcols; + } while (verbatim_copy_width < destination_col) { char_u *line = verbatim_copy_end; // TODO: is passing verbatim_copy_end for start of the line OK? incr = lbr_chartabsize(line, verbatim_copy_end, verbatim_copy_width); - if (verbatim_copy_width + incr > destination_col) + if (verbatim_copy_width + incr > destination_col) { break; + } verbatim_copy_width += incr; MB_PTR_ADV(verbatim_copy_end); } @@ -535,20 +542,23 @@ static void block_insert(oparg_T *oap, char_u *s, int b_insert, struct block_def if (b_insert) { p_ts = bdp->start_char_vcols; spaces = bdp->startspaces; - if (spaces != 0) - count = p_ts - 1; /* we're cutting a TAB */ + if (spaces != 0) { + count = p_ts - 1; // we're cutting a TAB + } offset = bdp->textcol; - } else { /* append */ + } else { // append p_ts = bdp->end_char_vcols; - if (!bdp->is_short) { /* spaces = padding after block */ + if (!bdp->is_short) { // spaces = padding after block spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0); - if (spaces != 0) - count = p_ts - 1; /* we're cutting a TAB */ + if (spaces != 0) { + count = p_ts - 1; // we're cutting a TAB + } offset = bdp->textcol + bdp->textlen - (spaces != 0); - } else { /* spaces = padding to block edge */ - /* if $ used, just append to EOL (ie spaces==0) */ - if (!bdp->is_MAX) + } else { // spaces = padding to block edge + // if $ used, just append to EOL (ie spaces==0) + if (!bdp->is_MAX) { spaces = (oap->end_vcol - bdp->end_vcol) + 1; + } count = spaces; offset = bdp->textcol + bdp->textlen; } @@ -594,8 +604,9 @@ static void block_insert(oparg_T *oap, char_u *s, int b_insert, struct block_def skipped = 1; } - if (spaces > 0) + if (spaces > 0) { offset += count; + } STRMOVE(newp + offset, oldp); ml_replace(lnum, newp, false); @@ -608,7 +619,7 @@ static void block_insert(oparg_T *oap, char_u *s, int b_insert, struct block_def curbuf->b_op_end.lnum = oap->end.lnum; curbuf->b_op_end.col = offset; } - } /* for all lnum */ + } // for all lnum changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L, true); @@ -627,7 +638,7 @@ void op_reindent(oparg_T *oap, Indenter how) linenr_T last_changed = 0; linenr_T start_lnum = curwin->w_cursor.lnum; - /* Don't even try when 'modifiable' is off. */ + // Don't even try when 'modifiable' is off. if (!MODIFIABLE(curbuf)) { EMSG(_(e_modifiable)); return; @@ -639,8 +650,9 @@ void op_reindent(oparg_T *oap, Indenter how) if (i > 1 && (i % 50 == 0 || i == oap->line_count - 1) - && oap->line_count > p_report) + && oap->line_count > p_report) { smsg(_("%" PRId64 " lines to indent... "), (int64_t)i); + } /* * Be vi-compatible: For lisp indenting the first line is not @@ -649,11 +661,11 @@ void op_reindent(oparg_T *oap, Indenter how) if (i != oap->line_count - 1 || oap->line_count == 1 || how != get_lisp_indent) { l = skipwhite(get_cursor_line_ptr()); - if (*l == NUL) /* empty or blank line */ + if (*l == NUL) { // empty or blank line amount = 0; - else - amount = how(); /* get the indent for this line */ - + } else { + amount = how(); // get the indent for this line + } if (amount >= 0 && set_indent(amount, SIN_UNDO)) { // did change the indent, call changed_lines() later if (first_changed == 0) { @@ -663,10 +675,10 @@ void op_reindent(oparg_T *oap, Indenter how) } } ++curwin->w_cursor.lnum; - curwin->w_cursor.col = 0; /* make sure it's valid */ + curwin->w_cursor.col = 0; // make sure it's valid } - /* put cursor on first non-blank of indented line */ + // put cursor on first non-blank of indented line curwin->w_cursor.lnum = start_lnum; beginline(BL_SOL | BL_FIX); @@ -683,12 +695,13 @@ void op_reindent(oparg_T *oap, Indenter how) if (oap->line_count > p_report) { i = oap->line_count - (i + 1); - if (i == 1) + if (i == 1) { MSG(_("1 line indented ")); - else + } else { smsg(_("%" PRId64 " lines indented "), (int64_t)i); + } } - /* set '[ and '] marks */ + // set '[ and '] marks curbuf->b_op_start = oap->start; curbuf->b_op_end = oap->end; } @@ -738,8 +751,9 @@ char_u *get_expr_line(void) char_u *rv; static int nested = 0; - if (expr_line == NULL) + if (expr_line == NULL) { return NULL; + } /* Make a copy of the expression, because evaluating it may cause it to be * changed. */ @@ -747,8 +761,9 @@ char_u *get_expr_line(void) /* When we are invoked recursively limit the evaluation to 10 levels. * Then return the string as-is. */ - if (nested >= 10) + if (nested >= 10) { return expr_copy; + } ++nested; rv = eval_to_string(expr_copy, NULL, TRUE); @@ -762,8 +777,9 @@ char_u *get_expr_line(void) */ char_u *get_expr_line_src(void) { - if (expr_line == NULL) + if (expr_line == NULL) { return NULL; + } return vim_strsave(expr_line); } @@ -776,7 +792,7 @@ char_u *get_expr_line_src(void) bool valid_yank_reg(int regname, bool writing) { if ((regname > 0 && ASCII_ISALNUM(regname)) - || (!writing && vim_strchr((char_u *) "/.%:=" , regname) != NULL) + || (!writing && vim_strchr((char_u *)"/.%:=", regname) != NULL) || regname == '#' || regname == '"' || regname == '-' @@ -820,8 +836,8 @@ yankreg_T *get_yank_register(int regname, int mode) // reg is set to clipboard contents. return reg; } else if (mode != YREG_YANK - && (regname == 0 || regname == '"' || regname == '*' || regname == '+') - && y_previous != NULL) { + && (regname == 0 || regname == '"' || regname == '*' || regname == '+') + && y_previous != NULL) { // in case clipboard not available, paste from previous used register return y_previous; } @@ -910,7 +926,7 @@ int do_record(int c) regname = c; retval = OK; } - } else { /* stop recording */ + } else { // stop recording /* * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this * needs to be removed again to put it in a register. exec_reg then @@ -923,10 +939,10 @@ int do_record(int c) MSG(""); } p = get_recorded(); - if (p == NULL) + if (p == NULL) { retval = FAIL; - else { - /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */ + } else { + // Remove escaping for CSI and K_SPECIAL in multi-byte chars. vim_unescape_csi(p); /* @@ -961,12 +977,12 @@ static void set_yreg_additional_data(yankreg_T *reg, dict_T *additional_data) */ static int stuff_yank(int regname, char_u *p) { - /* check for read-only register */ + // check for read-only register if (regname != 0 && !valid_yank_reg(regname, true)) { xfree(p); return FAIL; } - if (regname == '_') { /* black hole: don't do anything */ + if (regname == '_') { // black hole: don't do anything xfree(p); return OK; } @@ -996,36 +1012,35 @@ static int execreg_lastc = NUL; /// Execute a yank register: copy it into the stuff buffer /// -/// Return FAIL for failure, OK otherwise -int -do_execreg( - int regname, - int colon, /* insert ':' before each line */ - int addcr, /* always add '\n' to end of line */ - int silent /* set "silent" flag in typeahead buffer */ -) +/// @param colon insert ':' before each line +/// @param addcr always add '\n' to end of line +/// @param silent set "silent" flag in typeahead buffer +/// +/// @return FAIL for failure, OK otherwise +int do_execreg(int regname, int colon, int addcr, int silent) { char_u *p; int retval = OK; - if (regname == '@') { /* repeat previous one */ + if (regname == '@') { // repeat previous one if (execreg_lastc == NUL) { EMSG(_("E748: No previously used register")); return FAIL; } regname = execreg_lastc; } - /* check for valid regname */ + // check for valid regname if (regname == '%' || regname == '#' || !valid_yank_reg(regname, false)) { emsg_invreg(regname); return FAIL; } execreg_lastc = regname; - if (regname == '_') /* black hole: don't stuff anything */ + if (regname == '_') { // black hole: don't stuff anything return OK; + } - if (regname == ':') { /* use last command line */ + if (regname == ':') { // use last command line if (last_cmdline == NULL) { EMSG(_(e_nolastcmd)); return FAIL; @@ -1033,13 +1048,12 @@ do_execreg( // don't keep the cmdline containing @: XFREE_CLEAR(new_last_cmdline); // Escape all control characters with a CTRL-V - p = vim_strsave_escaped_ext( - last_cmdline, - (char_u *)"\001\002\003\004\005\006\007" - "\010\011\012\013\014\015\016\017" - "\020\021\022\023\024\025\026\027" - "\030\031\032\033\034\035\036\037", - Ctrl_V, false); + p = vim_strsave_escaped_ext(last_cmdline, + (char_u *)"\001\002\003\004\005\006\007" + "\010\011\012\013\014\015\016\017" + "\020\021\022\023\024\025\026\027" + "\030\031\032\033\034\035\036\037", + Ctrl_V, false); // When in Visual mode "'<,'>" will be prepended to the command. // Remove it when it's already there. if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0) { @@ -1050,11 +1064,12 @@ do_execreg( xfree(p); } else if (regname == '=') { p = get_expr_line(); - if (p == NULL) + if (p == NULL) { return FAIL; + } retval = put_in_typebuf(p, true, colon, silent); xfree(p); - } else if (regname == '.') { /* use last inserted text */ + } else if (regname == '.') { // use last inserted text p = get_last_insert_save(); if (p == NULL) { EMSG(_(e_noinstext)); @@ -1064,8 +1079,9 @@ do_execreg( xfree(p); } else { yankreg_T *reg = get_yank_register(regname, YREG_PASTE); - if (reg->y_array == NULL) + if (reg->y_array == NULL) { return FAIL; + } // Disallow remapping for ":@r". int remap = colon ? REMAP_NONE : REMAP_YES; @@ -1158,16 +1174,13 @@ static int put_in_typebuf(char_u *s, bool esc, bool colon, int silent) return retval; } -/* - * Insert a yank register: copy it into the Read buffer. - * Used by CTRL-R command and middle mouse button in insert mode. - * - * return FAIL for failure, OK otherwise - */ -int insert_reg( - int regname, - bool literally_arg // insert literally, not as if typed -) +/// Insert a yank register: copy it into the Read buffer. +/// Used by CTRL-R command and middle mouse button in insert mode. +/// +/// @param literally_arg insert literally, not as if typed +/// +/// @return FAIL for failure, OK otherwise +int insert_reg(int regname, bool literally_arg) { int retval = OK; bool allocated; @@ -1179,12 +1192,14 @@ int insert_reg( * If you hit CTRL-C, the loop will be broken here. */ os_breakcheck(); - if (got_int) + if (got_int) { return FAIL; + } - /* check for valid regname */ - if (regname != NUL && !valid_yank_reg(regname, false)) + // check for valid regname + if (regname != NUL && !valid_yank_reg(regname, false)) { return FAIL; + } char_u *arg; if (regname == '.') { // Insert last inserted text. @@ -1241,7 +1256,7 @@ static void stuffescaped(const char *arg, int literally) stuffReadbuffLen(start, (long)(arg - start)); } - /* stuff a single special character */ + // stuff a single special character if (*arg != NUL) { const int c = mb_cptr2char_adv((const char_u **)&arg); if (literally && ((c < ' ' && c != TAB) || c == DEL)) { @@ -1252,23 +1267,24 @@ static void stuffescaped(const char *arg, int literally) } } -// If "regname" is a special register, return true and store a pointer to its -// value in "argp". -bool get_spec_reg( - int regname, - char_u **argp, - bool *allocated, // return: true when value was allocated - bool errmsg // give error message when failing -) +/// If "regname" is a special register, return true and store a pointer to its +/// value in "argp". +/// +/// @param allocated return: true when value was allocated +/// @param errmsg give error message when failing +/// +/// @return true if "regname" is a special register, +bool get_spec_reg(int regname, char_u **argp, bool *allocated, bool errmsg) { size_t cnt; *argp = NULL; *allocated = false; switch (regname) { - case '%': /* file name */ - if (errmsg) - check_fname(); /* will give emsg if not set */ + case '%': // file name + if (errmsg) { + check_fname(); // will give emsg if not set + } *argp = curbuf->b_fname; return true; @@ -1276,24 +1292,26 @@ bool get_spec_reg( *argp = getaltfname(errmsg); // may give emsg if not set return true; - case '=': /* result of expression */ + case '=': // result of expression *argp = get_expr_line(); *allocated = true; return true; - case ':': /* last command line */ - if (last_cmdline == NULL && errmsg) + case ':': // last command line + if (last_cmdline == NULL && errmsg) { EMSG(_(e_nolastcmd)); + } *argp = last_cmdline; return true; - case '/': /* last search-pattern */ - if (last_search_pat() == NULL && errmsg) + case '/': // last search-pattern + if (last_search_pat() == NULL && errmsg) { EMSG(_(e_noprevre)); + } *argp = last_search_pat(); return true; - case '.': /* last inserted text */ + case '.': // last inserted text *argp = get_last_insert_save(); *allocated = true; if (*argp == NULL && errmsg) { @@ -1306,9 +1324,8 @@ bool get_spec_reg( if (!errmsg) { return false; } - *argp = file_name_at_cursor( - FNAME_MESS | FNAME_HYP | (regname == Ctrl_P ? FNAME_EXP : 0), - 1L, NULL); + *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP | (regname == Ctrl_P ? FNAME_EXP : 0), + 1L, NULL); *allocated = true; return true; @@ -1332,7 +1349,7 @@ bool get_spec_reg( *argp = ml_get_buf(curwin->w_buffer, curwin->w_cursor.lnum, false); return true; - case '_': /* black hole: always empty */ + case '_': // black hole: always empty *argp = (char_u *)""; return true; } @@ -1356,8 +1373,9 @@ bool cmdline_paste_reg(int regname, bool literally_arg, bool remcr) const bool literally = literally_arg || is_literal_register(regname); yankreg_T *reg = get_yank_register(regname, YREG_PASTE); - if (reg->y_array == NULL) + if (reg->y_array == NULL) { return FAIL; + } for (size_t i = 0; i < reg->y_size; i++) { cmdline_paste_str(reg->y_array[i], literally); @@ -1370,8 +1388,9 @@ bool cmdline_paste_reg(int regname, bool literally_arg, bool remcr) /* Check for CTRL-C, in case someone tries to paste a few thousand * lines and gets bored. */ os_breakcheck(); - if (got_int) + if (got_int) { return FAIL; + } } return OK; } @@ -1430,8 +1449,9 @@ int op_delete(oparg_T *oap) && oap->motion_force == NUL && oap->op_type == OP_DELETE) { ptr = ml_get(oap->end.lnum) + oap->end.col; - if (*ptr != NUL) + if (*ptr != NUL) { ptr += oap->inclusive; + } ptr = skipwhite(ptr); if (*ptr == NUL && inindent(0)) { oap->motion_type = kMTLineWise; @@ -1504,7 +1524,6 @@ int op_delete(oparg_T *oap) set_clipboard(oap->regname, reg); do_autocmd_textyankpost(oap, reg); } - } /* @@ -1522,7 +1541,7 @@ int op_delete(oparg_T *oap) continue; } - /* Adjust cursor position for tab replaced by spaces and 'lbr'. */ + // Adjust cursor position for tab replaced by spaces and 'lbr'. if (lnum == curwin->w_cursor.lnum) { curwin->w_cursor.col = bd.textcol + bd.startspaces; curwin->w_cursor.coladd = 0; @@ -1566,8 +1585,9 @@ int op_delete(oparg_T *oap) del_lines(oap->line_count - 1, true); curwin->w_cursor.lnum = lnum; } - if (u_save_cursor() == FAIL) + if (u_save_cursor() == FAIL) { return FAIL; + } if (curbuf->b_p_ai) { // don't delete indent beginline(BL_WHITE); // cursor on first non-white did_ai = true; // delete the indent when ESC hit @@ -1583,25 +1603,27 @@ int op_delete(oparg_T *oap) (int)curwin->w_cursor.lnum-1, curwin->w_cursor.col, old_len - curwin->w_cursor.col, 0, kExtmarkUndo); - // leave cursor past last char in line + // leave cursor past last char in line if (oap->line_count > 1) { u_clearline(); // "U" command not possible after "2cc" } } else { del_lines(oap->line_count, true); beginline(BL_WHITE | BL_FIX); - u_clearline(); /* "U" command not possible after "dd" */ + u_clearline(); // "U" command not possible after "dd" } } else { if (virtual_op) { int endcol = 0; - /* For virtualedit: break the tabs that are partly included. */ + // For virtualedit: break the tabs that are partly included. if (gchar_pos(&oap->start) == '\t') { - if (u_save_cursor() == FAIL) /* save first line for undo */ + if (u_save_cursor() == FAIL) { // save first line for undo return FAIL; - if (oap->line_count == 1) + } + if (oap->line_count == 1) { endcol = getviscol2(oap->end.col, oap->end.coladd); + } coladvance_force(getviscol2(oap->start.col, oap->start.coladd)); oap->start = curwin->w_cursor; if (oap->line_count == 1) { @@ -1612,14 +1634,15 @@ int op_delete(oparg_T *oap) } } - /* Break a tab only when it's included in the area. */ + // Break a tab only when it's included in the area. if (gchar_pos(&oap->end) == '\t' && oap->end.coladd == 0 && oap->inclusive) { - /* save last line for undo */ + // save last line for undo if (u_save((linenr_T)(oap->end.lnum - 1), - (linenr_T)(oap->end.lnum + 1)) == FAIL) + (linenr_T)(oap->end.lnum + 1)) == FAIL) { return FAIL; + } curwin->w_cursor = oap->end; coladvance_force(getviscol2(oap->end.col, oap->end.coladd)); oap->end = curwin->w_cursor; @@ -1628,17 +1651,18 @@ int op_delete(oparg_T *oap) mb_adjust_opend(oap); } - if (oap->line_count == 1) { /* delete characters within one line */ - if (u_save_cursor() == FAIL) /* save line for undo */ + if (oap->line_count == 1) { // delete characters within one line + if (u_save_cursor() == FAIL) { // save line for undo return FAIL; + } - /* if 'cpoptions' contains '$', display '$' at end of change */ - if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL - && oap->op_type == OP_CHANGE - && oap->end.lnum == curwin->w_cursor.lnum - && !oap->is_VIsual - ) + // if 'cpoptions' contains '$', display '$' at end of change + if (vim_strchr(p_cpo, CPO_DOLLAR) != NULL + && oap->op_type == OP_CHANGE + && oap->end.lnum == curwin->w_cursor.lnum + && !oap->is_VIsual) { display_dollar(oap->end.col - !oap->inclusive); + } n = oap->end.col - oap->start.col + 1 - !oap->inclusive; @@ -1651,15 +1675,18 @@ int op_delete(oparg_T *oap) if (oap->end.coladd != 0 && (int)oap->end.col >= len - 1 - && !(oap->start.coladd && (int)oap->end.col >= len - 1)) + && !(oap->start.coladd && (int)oap->end.col >= len - 1)) { n++; - /* Delete at least one char (e.g, when on a control char). */ - if (n == 0 && oap->start.coladd != oap->end.coladd) + } + // Delete at least one char (e.g, when on a control char). + if (n == 0 && oap->start.coladd != oap->end.coladd) { n = 1; + } - /* When deleted a char in the line, reset coladd. */ - if (gchar_cursor() != NUL) + // When deleted a char in the line, reset coladd. + if (gchar_cursor() != NUL) { curwin->w_cursor.coladd = 0; + } } (void)del_bytes((colnr_T)n, !virtual_op, @@ -1668,16 +1695,17 @@ int op_delete(oparg_T *oap) // delete characters between lines pos_T curpos; - /* save deleted and changed lines for undo */ + // save deleted and changed lines for undo if (u_save((linenr_T)(curwin->w_cursor.lnum - 1), - (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL) + (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL) { return FAIL; + } curbuf_splice_pending++; pos_T startpos = curwin->w_cursor; // start position for delete - bcount_t deleted_bytes = get_region_bytecount( - curbuf, startpos.lnum, oap->end.lnum, startpos.col, - oap->end.col) + oap->inclusive; + bcount_t deleted_bytes = get_region_bytecount(curbuf, startpos.lnum, oap->end.lnum, + startpos.col, + oap->end.col) + oap->inclusive; truncate_line(true); // delete from cursor to end of line curpos = curwin->w_cursor; // remember curwin->w_cursor @@ -1766,9 +1794,9 @@ int op_replace(oparg_T *oap, int c) char_u *after_p = NULL; int had_ctrl_v_cr = false; - if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty) - return OK; /* nothing to do */ - + if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty) { + return OK; // nothing to do + } if (c == REPLACE_CR_NCHAR) { had_ctrl_v_cr = true; c = CAR; @@ -1780,8 +1808,9 @@ int op_replace(oparg_T *oap, int c) mb_adjust_opend(oap); if (u_save((linenr_T)(oap->start.lnum - 1), - (linenr_T)(oap->end.lnum + 1)) == FAIL) + (linenr_T)(oap->end.lnum + 1)) == FAIL) { return FAIL; + } /* * block mode replace @@ -1808,18 +1837,20 @@ int op_replace(oparg_T *oap, int c) getvpos(&vpos, oap->start_vcol); bd.startspaces += vpos.coladd; n = bd.startspaces; - } else - /* allow for pre spaces */ + } else { + // allow for pre spaces n = (bd.startspaces ? bd.start_char_vcols - 1 : 0); + } - /* allow for post spp */ + // allow for post spp n += (bd.endspaces && !bd.is_oneChar && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0; - /* Figure out how many characters to replace. */ + // Figure out how many characters to replace. numc = oap->end_vcol - oap->start_vcol + 1; - if (bd.is_short && (!virtual_op || bd.is_MAX)) + if (bd.is_short && (!virtual_op || bd.is_MAX)) { numc -= (oap->end_vcol - bd.end_vcol) + 1; + } /* A double-wide character can be replaced only up to half the * times. */ @@ -1831,7 +1862,7 @@ int op_replace(oparg_T *oap, int c) numc = numc / 2; } - /* Compute bytes needed, move character count to num_chars. */ + // Compute bytes needed, move character count to num_chars. num_chars = numc; numc *= (*mb_char2len)(c); @@ -1859,19 +1890,19 @@ int op_replace(oparg_T *oap, int c) assert(col >= 0); int newrows = 0, newcols = 0; if (had_ctrl_v_cr || (c != '\r' && c != '\n')) { - // strlen(newp) at this point - int newp_len = bd.textcol + bd.startspaces; - while (--num_chars >= 0) { - newp_len += utf_char2bytes(c, newp + newp_len); - } - if (!bd.is_short) { - // insert post-spaces - memset(newp + newp_len, ' ', (size_t)bd.endspaces); - newp_len += bd.endspaces; - // copy the part after the changed part - memmove(newp + newp_len, oldp, (size_t)col); - } - newcols = newp_len - bd.textcol; + // strlen(newp) at this point + int newp_len = bd.textcol + bd.startspaces; + while (--num_chars >= 0) { + newp_len += utf_char2bytes(c, newp + newp_len); + } + if (!bd.is_short) { + // insert post-spaces + memset(newp + newp_len, ' ', (size_t)bd.endspaces); + newp_len += bd.endspaces; + // copy the part after the changed part + memmove(newp + newp_len, oldp, (size_t)col); + } + newcols = newp_len - bd.textcol; } else { // Replacing with \r or \n means splitting the line. after_p_len = (size_t)col; @@ -1900,10 +1931,12 @@ int op_replace(oparg_T *oap, int c) oap->start.col = 0; curwin->w_cursor.col = 0; oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum)); - if (oap->end.col) + if (oap->end.col) { --oap->end.col; - } else if (!oap->inclusive) + } + } else if (!oap->inclusive) { dec(&(oap->end)); + } // TODO(bfredl): we could batch all the splicing // done on the same line, at least @@ -1913,8 +1946,9 @@ int op_replace(oparg_T *oap, int c) if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1) { /* This is slow, but it handles replacing a single-byte * with a multi-byte and the other way around. */ - if (curwin->w_cursor.lnum == oap->end.lnum) + if (curwin->w_cursor.lnum == oap->end.lnum) { oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n); + } replace_character(c); } else { if (n == TAB) { @@ -1924,11 +1958,12 @@ int op_replace(oparg_T *oap, int c) /* oap->end has to be recalculated when * the tab breaks */ end_vcol = getviscol2(oap->end.col, - oap->end.coladd); + oap->end.coladd); } coladvance_force(getviscol()); - if (curwin->w_cursor.lnum == oap->end.lnum) + if (curwin->w_cursor.lnum == oap->end.lnum) { getvpos(&oap->end, end_vcol); + } } pbyte(curwin->w_cursor, c); } @@ -1936,8 +1971,9 @@ int op_replace(oparg_T *oap, int c) int virtcols = oap->end.coladd; if (curwin->w_cursor.lnum == oap->start.lnum - && oap->start.col == oap->end.col && oap->start.coladd) + && oap->start.col == oap->end.col && oap->start.coladd) { virtcols -= oap->start.coladd; + } /* oap->end has been trimmed so it's effectively inclusive; * as a result an extra +1 must be counted so we don't @@ -1956,9 +1992,10 @@ int op_replace(oparg_T *oap, int c) } } - /* Advance to next character, stop at the end of the file. */ - if (inc_cursor() == -1) + // Advance to next character, stop at the end of the file. + if (inc_cursor() == -1) { break; + } } } @@ -1966,7 +2003,7 @@ int op_replace(oparg_T *oap, int c) check_cursor(); changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L, true); - /* Set "'[" and "']" marks. */ + // Set "'[" and "']" marks. curbuf->b_op_start = oap->start; curbuf->b_op_end = oap->end; @@ -1984,8 +2021,9 @@ void op_tilde(oparg_T *oap) int did_change = FALSE; if (u_save((linenr_T)(oap->start.lnum - 1), - (linenr_T)(oap->end.lnum + 1)) == FAIL) + (linenr_T)(oap->end.lnum + 1)) == FAIL) { return; + } pos = oap->start; if (oap->motion_type == kMTBlockWise) { // Visual block mode @@ -1996,7 +2034,6 @@ void op_tilde(oparg_T *oap) pos.col = bd.textcol; one_change = swapchars(oap->op_type, &pos, bd.textlen); did_change |= one_change; - } if (did_change) { changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L, true); @@ -2006,31 +2043,36 @@ void op_tilde(oparg_T *oap) oap->start.col = 0; pos.col = 0; oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum)); - if (oap->end.col) + if (oap->end.col) { --oap->end.col; - } else if (!oap->inclusive) + } + } else if (!oap->inclusive) { dec(&(oap->end)); + } - if (pos.lnum == oap->end.lnum) + if (pos.lnum == oap->end.lnum) { did_change = swapchars(oap->op_type, &pos, - oap->end.col - pos.col + 1); - else + oap->end.col - pos.col + 1); + } else { for (;; ) { did_change |= swapchars(oap->op_type, &pos, - pos.lnum == oap->end.lnum ? oap->end.col + 1 : - (int)STRLEN(ml_get_pos(&pos))); - if (ltoreq(oap->end, pos) || inc(&pos) == -1) + pos.lnum == oap->end.lnum ? oap->end.col + 1 : + (int)STRLEN(ml_get_pos(&pos))); + if (ltoreq(oap->end, pos) || inc(&pos) == -1) { break; + } } + } if (did_change) { changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L, true); } } - if (!did_change && oap->is_VIsual) - /* No change: need to remove the Visual selection */ + if (!did_change && oap->is_VIsual) { + // No change: need to remove the Visual selection redraw_curbuf_later(INVERTED); + } /* * Set '[ and '] marks. @@ -2039,10 +2081,11 @@ void op_tilde(oparg_T *oap) curbuf->b_op_end = oap->end; if (oap->line_count > p_report) { - if (oap->line_count == 1) + if (oap->line_count == 1) { MSG(_("1 line changed")); - else + } else { smsg(_("%" PRId64 " lines changed"), (int64_t)oap->line_count); + } } } @@ -2066,8 +2109,9 @@ static int swapchars(int op_type, pos_T *pos, int length) todo -= len - 1; } did_change |= swapchar(op_type, pos); - if (inc(pos) == -1) /* at end of file */ + if (inc(pos) == -1) { // at end of file break; + } } return did_change; } @@ -2090,7 +2134,7 @@ bool swapchar(int op_type, pos_T *pos) if (op_type == OP_UPPER && c == 0xdf) { pos_T sp = curwin->w_cursor; - /* Special handling of German sharp s: change to "SS". */ + // Special handling of German sharp s: change to "SS". curwin->w_cursor = *pos; del_char(false); ins_char('S'); @@ -2142,10 +2186,10 @@ void op_insert(oparg_T *oap, long count1) int i; pos_T t1; - /* edit() changes this - record it for OP_APPEND */ + // edit() changes this - record it for OP_APPEND bd.is_MAX = (curwin->w_curswant == MAXCOL); - /* vis block is still marked. Get rid of it now. */ + // vis block is still marked. Get rid of it now. curwin->w_cursor.lnum = oap->start.lnum; update_screen(INVERTED); @@ -2158,12 +2202,14 @@ void op_insert(oparg_T *oap, long count1) unsigned old_ve_flags = ve_flags; ve_flags = VE_ALL; - if (u_save_cursor() == FAIL) + if (u_save_cursor() == FAIL) { return; + } coladvance_force(oap->op_type == OP_APPEND ? oap->end_vcol + 1 : getviscol()); - if (oap->op_type == OP_APPEND) + if (oap->op_type == OP_APPEND) { --curwin->w_cursor.col; + } ve_flags = old_ve_flags; } // Get the info about the block before entering the text @@ -2180,18 +2226,19 @@ void op_insert(oparg_T *oap, long count1) if (oap->op_type == OP_APPEND) { if (oap->motion_type == kMTBlockWise - && curwin->w_cursor.coladd == 0 - ) { - /* Move the cursor to the character right of the block. */ + && curwin->w_cursor.coladd == 0) { + // Move the cursor to the character right of the block. curwin->w_set_curswant = TRUE; while (*get_cursor_pos_ptr() != NUL - && (curwin->w_cursor.col < bd.textcol + bd.textlen)) + && (curwin->w_cursor.col < bd.textcol + bd.textlen)) { ++curwin->w_cursor.col; + } if (bd.is_short && !bd.is_MAX) { /* First line was too short, make it longer and adjust the * values in "bd". */ - if (u_save_cursor() == FAIL) + if (u_save_cursor() == FAIL) { return; + } for (i = 0; i < bd.endspaces; i++) { ins_char(' '); } @@ -2223,8 +2270,9 @@ void op_insert(oparg_T *oap, long count1) /* If user has moved off this line, we don't know what to do, so do * nothing. * Also don't repeat the insert when Insert mode ended with CTRL-C. */ - if (curwin->w_cursor.lnum != oap->start.lnum || got_int) + if (curwin->w_cursor.lnum != oap->start.lnum || got_int) { return; + } if (oap->motion_type == kMTBlockWise) { struct block_def bd2; @@ -2260,7 +2308,7 @@ void op_insert(oparg_T *oap, long count1) int t = getviscol2(curbuf->b_op_start_orig.col, curbuf->b_op_start_orig.coladd); oap->start.col = curbuf->b_op_start_orig.col; - /* reset pre_textlen to the value of OP_INSERT */ + // reset pre_textlen to the value of OP_INSERT pre_textlen += bd.textlen; pre_textlen -= t - oap->start_vcol; oap->start_vcol = t; @@ -2277,8 +2325,9 @@ void op_insert(oparg_T *oap, long count1) if (!bd.is_MAX || bd2.textlen < bd.textlen) { if (oap->op_type == OP_APPEND) { pre_textlen += bd2.textlen - bd.textlen; - if (bd2.endspaces) + if (bd2.endspaces) { --bd2.textlen; + } } bd.textcol = bd2.textcol; bd.textlen = bd2.textlen; @@ -2338,18 +2387,20 @@ int op_change(oparg_T *oap) if (oap->motion_type == kMTLineWise) { l = 0; if (!p_paste && curbuf->b_p_si - && !curbuf->b_p_cin - ) + && !curbuf->b_p_cin) { can_si = true; // It's like opening a new line, do si + } } /* First delete the text in the region. In an empty buffer only need to * save for undo */ if (curbuf->b_ml.ml_flags & ML_EMPTY) { - if (u_save_cursor() == FAIL) + if (u_save_cursor() == FAIL) { return FALSE; - } else if (op_delete(oap) == FAIL) + } + } else if (op_delete(oap) == FAIL) { return FALSE; + } if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum) && !virtual_op) { @@ -2460,9 +2511,9 @@ void clear_registers(void) #endif - /// Free contents of yankreg `reg`. - /// Called for normal freeing and in case of error. - /// `reg` must not be NULL (but `reg->y_array` might be) +/// Free contents of yankreg `reg`. +/// Called for normal freeing and in case of error. +/// `reg` must not be NULL (but `reg->y_array` might be) void free_register(yankreg_T *reg) FUNC_ATTR_NONNULL_ALL { @@ -2492,7 +2543,7 @@ bool op_yank(oparg_T *oap, bool message, int deleting) return false; } if (oap->regname == '_') { - return true; // black hole: nothing to do + return true; // black hole: nothing to do } yankreg_T *reg = get_yank_register(oap->regname, YREG_YANK); @@ -2554,8 +2605,9 @@ static void op_yank_reg(oparg_T *oap, bool message, yankreg_T *reg, bool append) // Visual block mode reg->y_width = oap->end_vcol - oap->start_vcol; - if (curwin->w_curswant == MAXCOL && reg->y_width > 0) + if (curwin->w_curswant == MAXCOL && reg->y_width > 0) { reg->y_width--; + } } for (; lnum <= yankendlnum; lnum++, y_idx++) { @@ -2569,76 +2621,76 @@ static void op_yank_reg(oparg_T *oap, bool message, yankreg_T *reg, bool append) reg->y_array[y_idx] = vim_strsave(ml_get(lnum)); break; - case kMTCharWise: - { - colnr_T startcol = 0, endcol = MAXCOL; - int is_oneChar = false; - colnr_T cs, ce; - p = ml_get(lnum); - bd.startspaces = 0; - bd.endspaces = 0; + case kMTCharWise: { + colnr_T startcol = 0, endcol = MAXCOL; + int is_oneChar = false; + colnr_T cs, ce; + p = ml_get(lnum); + bd.startspaces = 0; + bd.endspaces = 0; - if (lnum == oap->start.lnum) { - startcol = oap->start.col; - if (virtual_op) { - getvcol(curwin, &oap->start, &cs, NULL, &ce); - if (ce != cs && oap->start.coladd > 0) { - /* Part of a tab selected -- but don't - * double-count it. */ - bd.startspaces = (ce - cs + 1) - - oap->start.coladd; - startcol++; - } - } - } - - if (lnum == oap->end.lnum) { - endcol = oap->end.col; - if (virtual_op) { - getvcol(curwin, &oap->end, &cs, NULL, &ce); - if (p[endcol] == NUL || (cs + oap->end.coladd < ce - // Don't add space for double-wide - // char; endcol will be on last byte - // of multi-byte char. - && utf_head_off(p, p + endcol) == 0)) { - if (oap->start.lnum == oap->end.lnum - && oap->start.col == oap->end.col) { - // Special case: inside a single char - is_oneChar = true; - bd.startspaces = oap->end.coladd - - oap->start.coladd + oap->inclusive; - endcol = startcol; - } else { - bd.endspaces = oap->end.coladd - + oap->inclusive; - endcol -= oap->inclusive; + if (lnum == oap->start.lnum) { + startcol = oap->start.col; + if (virtual_op) { + getvcol(curwin, &oap->start, &cs, NULL, &ce); + if (ce != cs && oap->start.coladd > 0) { + /* Part of a tab selected -- but don't + * double-count it. */ + bd.startspaces = (ce - cs + 1) + - oap->start.coladd; + startcol++; } } } + + if (lnum == oap->end.lnum) { + endcol = oap->end.col; + if (virtual_op) { + getvcol(curwin, &oap->end, &cs, NULL, &ce); + if (p[endcol] == NUL || (cs + oap->end.coladd < ce + // Don't add space for double-wide + // char; endcol will be on last byte + // of multi-byte char. + && utf_head_off(p, p + endcol) == 0)) { + if (oap->start.lnum == oap->end.lnum + && oap->start.col == oap->end.col) { + // Special case: inside a single char + is_oneChar = true; + bd.startspaces = oap->end.coladd + - oap->start.coladd + oap->inclusive; + endcol = startcol; + } else { + bd.endspaces = oap->end.coladd + + oap->inclusive; + endcol -= oap->inclusive; + } + } + } + } + if (endcol == MAXCOL) { + endcol = (colnr_T)STRLEN(p); + } + if (startcol > endcol + || is_oneChar) { + bd.textlen = 0; + } else { + bd.textlen = endcol - startcol + oap->inclusive; + } + bd.textstart = p + startcol; + yank_copy_line(reg, &bd, y_idx, false); + break; } - if (endcol == MAXCOL) - endcol = (colnr_T)STRLEN(p); - if (startcol > endcol - || is_oneChar - ) { - bd.textlen = 0; - } else { - bd.textlen = endcol - startcol + oap->inclusive; - } - bd.textstart = p + startcol; - yank_copy_line(reg, &bd, y_idx, false); - break; - } // NOTREACHED case kMTUnknown: - abort(); + abort(); } } - if (curr != reg) { /* append the new block to the old block */ + if (curr != reg) { // append the new block to the old block new_ptr = xmalloc(sizeof(char_u *) * (curr->y_size + reg->y_size)); - for (j = 0; j < curr->y_size; ++j) + for (j = 0; j < curr->y_size; ++j) { new_ptr[j] = curr->y_array[j]; + } xfree(curr->y_array); curr->y_array = new_ptr; @@ -2659,10 +2711,12 @@ static void op_yank_reg(oparg_T *oap, bool message, yankreg_T *reg, bool append) xfree(reg->y_array[0]); curr->y_array[j++] = pnew; y_idx = 1; - } else + } else { y_idx = 0; - while (y_idx < reg->y_size) + } + while (y_idx < reg->y_size) { curr->y_array[j++] = reg->y_array[y_idx++]; + } curr->y_size = j; xfree(reg->y_array); } @@ -2715,8 +2769,8 @@ static void op_yank_reg(oparg_T *oap, bool message, yankreg_T *reg, bool append) // Copy a block range into a register. // If "exclude_trailing_space" is set, do not copy trailing whitespaces. -static void yank_copy_line(yankreg_T *reg, struct block_def *bd, - size_t y_idx, bool exclude_trailing_space) +static void yank_copy_line(yankreg_T *reg, struct block_def *bd, size_t y_idx, + bool exclude_trailing_space) FUNC_ATTR_NONNULL_ALL { if (exclude_trailing_space) { @@ -2831,8 +2885,8 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) long nr_lines = 0; pos_T new_cursor; int indent; - int orig_indent = 0; /* init for gcc */ - int indent_diff = 0; /* init for gcc */ + int orig_indent = 0; // init for gcc + int indent_diff = 0; // init for gcc int first_indent = TRUE; int lendiff = 0; pos_T old_pos; @@ -2840,11 +2894,12 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) bool allocated = false; long cnt; - if (flags & PUT_FIXINDENT) + if (flags & PUT_FIXINDENT) { orig_indent = get_indent(); + } - curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */ - curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */ + curbuf->b_op_start = curwin->w_cursor; // default for '[ mark + curbuf->b_op_end = curwin->w_cursor; // default for '] mark /* * Using inserted text works differently, because the register includes @@ -2855,7 +2910,7 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) // PUT_LINE has special handling below which means we use 'i' to start. char command_start_char = non_linewise_vis ? 'c' : - (flags & PUT_LINE ? 'i' : (dir == FORWARD ? 'a' : 'i')); + (flags & PUT_LINE ? 'i' : (dir == FORWARD ? 'a' : 'i')); // To avoid 'autoindent' on linewise puts, create a new line with `:put _`. if (flags & PUT_LINE) { @@ -2957,27 +3012,30 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) y_size = 0; ptr = insert_string; while (ptr != NULL) { - if (y_array != NULL) + if (y_array != NULL) { y_array[y_size] = ptr; + } ++y_size; ptr = vim_strchr(ptr, '\n'); if (ptr != NULL) { - if (y_array != NULL) + if (y_array != NULL) { *ptr = NUL; + } ++ptr; - /* A trailing '\n' makes the register linewise. */ + // A trailing '\n' makes the register linewise. if (*ptr == NUL) { y_type = kMTLineWise; break; } } } - if (y_array != NULL) + if (y_array != NULL) { break; + } y_array = (char_u **)xmalloc(y_size * sizeof(char_u *)); } } else { - y_size = 1; /* use fake one-line yank register */ + y_size = 1; // use fake one-line yank register y_array = &insert_string; } } else { @@ -3025,12 +3083,12 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) dir = FORWARD; } if (flags & PUT_LINE_FORWARD) { - /* Must be "p" for a Visual block, put lines below the block. */ + // Must be "p" for a Visual block, put lines below the block. curwin->w_cursor = curbuf->b_visual.vi_end; dir = FORWARD; } - curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */ - curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */ + curbuf->b_op_start = curwin->w_cursor; // default for '[ mark + curbuf->b_op_end = curwin->w_cursor; // default for '] mark } if (flags & PUT_LINE) { // :put command or "p" in Visual line mode. @@ -3039,7 +3097,7 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) if (y_size == 0 || y_array == NULL) { EMSG2(_("E353: Nothing in register %s"), - regname == 0 ? (char_u *)"\"" : transchar(regname)); + regname == 0 ? (char_u *)"\"" : transchar(regname)); goto end; } @@ -3109,10 +3167,11 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) colnr_T endcol2 = 0; if (dir == FORWARD && c != NUL) { - if (ve_flags == VE_ALL) + if (ve_flags == VE_ALL) { getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2); - else + } else { getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col); + } // move to start of next multi-byte character curwin->w_cursor.col += utfc_ptr2len(get_cursor_pos_ptr()); @@ -3132,10 +3191,12 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) curwin->w_cursor.col++; } if (c == TAB) { - if (dir == BACKWARD && curwin->w_cursor.col) + if (dir == BACKWARD && curwin->w_cursor.col) { curwin->w_cursor.col--; - if (dir == FORWARD && col - 1 == endcol2) + } + if (dir == FORWARD && col - 1 == endcol2) { curwin->w_cursor.col++; + } } } curwin->w_cursor.coladd = 0; @@ -3152,7 +3213,7 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) vcol = 0; delcount = 0; - /* add a new line */ + // add a new line if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) { if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"", (colnr_T)1, false) == FAIL) { @@ -3161,11 +3222,11 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) nr_lines++; lines_appended = 1; } - /* get the old line and advance to the position to insert at */ + // get the old line and advance to the position to insert at oldp = get_cursor_line_ptr(); oldlen = STRLEN(oldp); for (ptr = oldp; vcol < col && *ptr; ) { - /* Count a tab for what it's worth (if list mode not on) */ + // Count a tab for what it's worth (if list mode not on) incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol); vcol += incr; } @@ -3173,9 +3234,9 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) shortline = (vcol < col) || (vcol == col && !*ptr); - if (vcol < col) /* line too short, padd with spaces */ + if (vcol < col) { // line too short, padd with spaces bd.startspaces = col - vcol; - else if (vcol > col) { + } else if (vcol > col) { bd.endspaces = vcol - col; bd.startspaces = incr - bd.endspaces; --bd.textcol; @@ -3241,18 +3302,19 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) delcount, addcount, kExtmarkUndo); ++curwin->w_cursor.lnum; - if (i == 0) + if (i == 0) { curwin->w_cursor.col += bd.startspaces; + } } changed_lines(lnum, 0, curbuf->b_op_start.lnum + (linenr_T)y_size - - (linenr_T)nr_lines , nr_lines, true); + - (linenr_T)nr_lines, nr_lines, true); - /* Set '[ mark. */ + // Set '[ mark. curbuf->b_op_start = curwin->w_cursor; curbuf->b_op_start.lnum = lnum; - /* adjust '] mark */ + // adjust '] mark curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1; curbuf->b_op_end.col = bd.textcol + (colnr_T)totlen - 1; curbuf->b_op_end.coladd = 0; @@ -3262,12 +3324,14 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) curwin->w_cursor = curbuf->b_op_end; curwin->w_cursor.col++; - /* in Insert mode we might be after the NUL, correct for that */ + // in Insert mode we might be after the NUL, correct for that len = (colnr_T)STRLEN(get_cursor_line_ptr()); - if (curwin->w_cursor.col > len) + if (curwin->w_cursor.col > len) { curwin->w_cursor.col = len; - } else + } + } else { curwin->w_cursor.lnum = lnum; + } } else { // Character or Line mode if (y_type == kMTCharWise) { @@ -3288,8 +3352,9 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) /* * Line mode: BACKWARD is the same as FORWARD on the previous line */ - else if (dir == BACKWARD) + else if (dir == BACKWARD) { --lnum; + } new_cursor = curwin->w_cursor; // simple case: insert into one line at a time @@ -3300,7 +3365,7 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) if (VIsual_active) { end_lnum = curbuf->b_visual.vi_end.lnum; if (end_lnum < curbuf->b_visual.vi_start.lnum) { - end_lnum = curbuf->b_visual.vi_start.lnum; + end_lnum = curbuf->b_visual.vi_start.lnum; } if (end_lnum > start_lnum) { // "col" is valid for the first line, in following lines @@ -3357,7 +3422,7 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) } } while (VIsual_active && lnum <= end_lnum); - if (VIsual_active) { /* reset lnum to the last visual line */ + if (VIsual_active) { // reset lnum to the last visual line lnum--; } @@ -3378,7 +3443,7 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) lnum = new_cursor.lnum; ptr = ml_get(lnum) + col; totlen = STRLEN(y_array[y_size - 1]); - newp = (char_u *) xmalloc((size_t)(STRLEN(ptr) + totlen + 1)); + newp = (char_u *)xmalloc((size_t)(STRLEN(ptr) + totlen + 1)); STRCPY(newp, y_array[y_size - 1]); STRCAT(newp, ptr); // insert second line @@ -3409,23 +3474,26 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) old_pos = curwin->w_cursor; curwin->w_cursor.lnum = lnum; ptr = ml_get(lnum); - if (cnt == count && i == y_size - 1) + if (cnt == count && i == y_size - 1) { lendiff = (int)STRLEN(ptr); - if (*ptr == '#' && preprocs_left()) - indent = 0; /* Leave # lines at start */ - else if (*ptr == NUL) - indent = 0; /* Ignore empty lines */ - else if (first_indent) { + } + if (*ptr == '#' && preprocs_left()) { + indent = 0; // Leave # lines at start + } else if (*ptr == NUL) { + indent = 0; // Ignore empty lines + } else if (first_indent) { indent_diff = orig_indent - get_indent(); indent = orig_indent; first_indent = FALSE; - } else if ((indent = get_indent() + indent_diff) < 0) + } else if ((indent = get_indent() + indent_diff) < 0) { indent = 0; + } (void)set_indent(indent, SIN_NOMARK); curwin->w_cursor = old_pos; - /* remember how many chars were removed */ - if (cnt == count && i == y_size - 1) + // remember how many chars were removed + if (cnt == count && i == y_size - 1) { lendiff -= (int)STRLEN(ml_get(lnum)); + } } } @@ -3454,8 +3522,9 @@ error: // Adjust marks. if (y_type == kMTLineWise) { curbuf->b_op_start.col = 0; - if (dir == FORWARD) + if (dir == FORWARD) { curbuf->b_op_start.lnum++; + } } ExtmarkOp kind = (y_type == kMTLineWise && !(flags & PUT_LINE_SPLIT)) @@ -3472,17 +3541,18 @@ error: curbuf->b_op_start.lnum, nr_lines, true); } - /* put '] mark at last inserted character */ + // put '] mark at last inserted character curbuf->b_op_end.lnum = lnum; - /* correct length for change in indent */ + // correct length for change in indent col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff; - if (col > 1) + if (col > 1) { curbuf->b_op_end.col = col - 1; - else + } else { curbuf->b_op_end.col = 0; + } if (flags & PUT_CURSLINE) { - /* ":put": put cursor on last inserted line */ + // ":put": put cursor on last inserted line curwin->w_cursor.lnum = lnum; beginline(BL_WHITE | BL_FIX); } else if (flags & PUT_CURSEND) { @@ -3501,11 +3571,13 @@ error: } else if (y_type == kMTLineWise) { // put cursor on first non-blank in first inserted line curwin->w_cursor.col = 0; - if (dir == FORWARD) + if (dir == FORWARD) { ++curwin->w_cursor.lnum; + } beginline(BL_WHITE | BL_FIX); - } else /* put cursor on first inserted character */ + } else { // put cursor on first inserted character curwin->w_cursor = new_cursor; + } } } @@ -3513,14 +3585,16 @@ error: curwin->w_set_curswant = TRUE; end: - if (allocated) + if (allocated) { xfree(insert_string); - if (regname == '=') + } + if (regname == '=') { xfree(y_array); + } VIsual_active = FALSE; - /* If the cursor is past the end of the line put it at the end. */ + // If the cursor is past the end of the line put it at the end. adjust_cursor_eol(); } // NOLINT(readability/fn_size) @@ -3534,13 +3608,13 @@ void adjust_cursor_eol(void) && gchar_cursor() == NUL && (ve_flags & VE_ONEMORE) == 0 && !(restart_edit || (State & INSERT))) { - /* Put the cursor on the last character in the line. */ + // Put the cursor on the last character in the line. dec_cursor(); if (ve_flags == VE_ALL) { colnr_T scol, ecol; - /* Coladd is set to the width of the last character. */ + // Coladd is set to the width of the last character. getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol); curwin->w_cursor.coladd = ecol - scol + 1; } @@ -3557,20 +3631,20 @@ int preprocs_left(void) && curbuf->b_ind_hash_comment == 0)); } -/* Return the character name of the register with the given number */ +// Return the character name of the register with the given number int get_register_name(int num) { - if (num == -1) + if (num == -1) { return '"'; - else if (num < 10) + } else if (num < 10) { return num + '0'; - else if (num == DELETION_REGISTER) + } else if (num == DELETION_REGISTER) { return '-'; - else if (num == STAR_REGISTER) + } else if (num == STAR_REGISTER) { return '*'; - else if (num == PLUS_REGISTER) + } else if (num == PLUS_REGISTER) { return '+'; - else { + } else { return num + 'a' - 10; } } @@ -3587,8 +3661,9 @@ void ex_display(exarg_T *eap) int clen; char_u type[2]; - if (arg != NULL && *arg == NUL) + if (arg != NULL && *arg == NUL) { arg = NULL; + } int attr = HL_ATTR(HLF_8); // Highlight title @@ -3596,23 +3671,28 @@ void ex_display(exarg_T *eap) for (int i = -1; i < NUM_REGISTERS && !got_int; i++) { name = get_register_name(i); switch (get_reg_type(name, NULL)) { - case kMTLineWise: type[0] = 'l'; break; - case kMTCharWise: type[0] = 'c'; break; - default: type[0] = 'b'; break; + case kMTLineWise: + type[0] = 'l'; break; + case kMTCharWise: + type[0] = 'c'; break; + default: + type[0] = 'b'; break; } if (arg != NULL && vim_strchr(arg, name) == NULL) { - continue; /* did not ask for this register */ + continue; // did not ask for this register } if (i == -1) { - if (y_previous != NULL) + if (y_previous != NULL) { yb = y_previous; - else + } else { yb = &(y_regs[0]); - } else + } + } else { yb = &(y_regs[i]); + } get_clipboard(name, &yb, true); @@ -3710,15 +3790,11 @@ void ex_display(exarg_T *eap) } } -/* - * display a string for do_dis() - * truncate at end of screen line - */ -static void -dis_msg( - const char_u *p, - bool skip_esc // if true, ignore trailing ESC -) +/// display a string for do_dis() +/// truncate at end of screen line +/// +/// @param skip_esc if true, ignore trailing ESC +static void dis_msg(const char_u *p, bool skip_esc) FUNC_ATTR_NONNULL_ALL { int n; @@ -3731,8 +3807,9 @@ dis_msg( if ((l = utfc_ptr2len(p)) > 1) { msg_outtrans_len(p, l); p += l; - } else + } else { msg_outtrans_len(p++, 1); + } } os_breakcheck(); } @@ -3748,9 +3825,7 @@ dis_msg( /// @param include_space - whether to skip space following the comment leader /// @param[out] is_comment - whether the current line ends with an unclosed /// comment. -char_u *skip_comment( - char_u *line, bool process, bool include_space, bool *is_comment -) +char_u *skip_comment(char_u *line, bool process, bool include_space, bool *is_comment) { char_u *comment_flags = NULL; int lead_len; @@ -3779,8 +3854,9 @@ char_u *skip_comment( lead_len = get_leader_len(line, &comment_flags, false, include_space); - if (lead_len == 0) + if (lead_len == 0) { return line; + } /* Find: * - COM_END, @@ -3814,21 +3890,17 @@ char_u *skip_comment( // to set those marks. // // return FAIL for failure, OK otherwise -int do_join(size_t count, - int insert_space, - int save_undo, - int use_formatoptions, - bool setmark) +int do_join(size_t count, int insert_space, int save_undo, int use_formatoptions, bool setmark) { char_u *curr = NULL; char_u *curr_start = NULL; char_u *cend; char_u *newp; - char_u *spaces; /* number of spaces inserted before a line */ + char_u *spaces; // number of spaces inserted before a line int endcurr1 = NUL; int endcurr2 = NUL; - int currsize = 0; /* size of the current line */ - int sumsize = 0; /* size of the long new line */ + int currsize = 0; // size of the current line + int sumsize = 0; // size of the long new line linenr_T t; colnr_T col = 0; int ret = OK; @@ -3882,13 +3954,14 @@ int do_join(size_t count, || (utf_ptr2char(curr) < 0x100 && endcurr1 < 0x100)) && (!has_format_option(FO_MBYTE_JOIN2) || (utf_ptr2char(curr) < 0x100 && !utf_eat_space(endcurr1)) - || (endcurr1 < 0x100 && !utf_eat_space(utf_ptr2char(curr)))) - ) { - /* don't add a space if the line is ending in a space */ - if (endcurr1 == ' ') + || (endcurr1 < 0x100 && + !utf_eat_space(utf_ptr2char(curr))))) { + // don't add a space if the line is ending in a space + if (endcurr1 == ' ') { endcurr1 = endcurr2; - else + } else { ++spaces[t]; + } // Extra space when 'joinspaces' set and line ends in '.', '?', or '!'. if (p_js && (endcurr1 == '.' || endcurr1 == '?' || endcurr1 == '!')) { ++spaces[t]; @@ -3963,10 +4036,12 @@ int do_join(size_t count, } curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1)); - if (remove_comments) + if (remove_comments) { curr += comments[t - 1]; - if (insert_space && t > 1) + } + if (insert_space && t > 1) { curr = skipwhite(curr); + } currsize = (int)STRLEN(curr); } @@ -3998,7 +4073,7 @@ int do_join(size_t count, /* * Set the cursor column: * Vi compatible: use the column of the first join - * vim: use the column of the last join + * vim: use the column of the last join */ curwin->w_cursor.col = (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col); @@ -4009,8 +4084,9 @@ int do_join(size_t count, theend: xfree(spaces); - if (remove_comments) + if (remove_comments) { xfree(comments); + } return ret; } @@ -4019,15 +4095,17 @@ theend: * the first line. White-space is ignored. Note that the whole of * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb */ -static int same_leader(linenr_T lnum, int leader1_len, char_u *leader1_flags, int leader2_len, char_u *leader2_flags) +static int same_leader(linenr_T lnum, int leader1_len, char_u *leader1_flags, int leader2_len, + char_u *leader2_flags) { int idx1 = 0, idx2 = 0; char_u *p; char_u *line1; char_u *line2; - if (leader1_len == 0) + if (leader1_len == 0) { return leader2_len == 0; + } /* * If first leader has 'f' flag, the lines can be joined only if the @@ -4038,18 +4116,24 @@ static int same_leader(linenr_T lnum, int leader1_len, char_u *leader1_flags, in */ if (leader1_flags != NULL) { for (p = leader1_flags; *p && *p != ':'; ++p) { - if (*p == COM_FIRST) + if (*p == COM_FIRST) { return leader2_len == 0; - if (*p == COM_END) + } + if (*p == COM_END) { return FALSE; + } if (*p == COM_START) { - if (*(ml_get(lnum) + leader1_len) == NUL) + if (*(ml_get(lnum) + leader1_len) == NUL) { return FALSE; - if (leader2_flags == NULL || leader2_len == 0) + } + if (leader2_flags == NULL || leader2_len == 0) { return FALSE; - for (p = leader2_flags; *p && *p != ':'; ++p) - if (*p == COM_MIDDLE) + } + for (p = leader2_flags; *p && *p != ':'; ++p) { + if (*p == COM_MIDDLE) { return TRUE; + } + } return FALSE; } } @@ -4060,30 +4144,30 @@ static int same_leader(linenr_T lnum, int leader1_len, char_u *leader1_flags, in * The first line has to be saved, only one line can be locked at a time. */ line1 = vim_strsave(ml_get(lnum)); - for (idx1 = 0; ascii_iswhite(line1[idx1]); ++idx1) + for (idx1 = 0; ascii_iswhite(line1[idx1]); ++idx1) { ; + } line2 = ml_get(lnum + 1); for (idx2 = 0; idx2 < leader2_len; ++idx2) { if (!ascii_iswhite(line2[idx2])) { - if (line1[idx1++] != line2[idx2]) + if (line1[idx1++] != line2[idx2]) { break; - } else - while (ascii_iswhite(line1[idx1])) + } + } else { + while (ascii_iswhite(line1[idx1])) { ++idx1; + } + } } xfree(line1); return idx2 == leader2_len && idx1 == leader1_len; } -/* - * Implementation of the format operator 'gq'. - */ -void -op_format( - oparg_T *oap, - int keep_cursor /* keep cursor on same text char */ -) +/// Implementation of the format operator 'gq'. +/// +/// @param keep_cursor keep cursor on same text char +void op_format(oparg_T *oap, int keep_cursor) { long old_line_count = curbuf->b_ml.ml_line_count; @@ -4092,21 +4176,24 @@ op_format( curwin->w_cursor = oap->cursor_start; if (u_save((linenr_T)(oap->start.lnum - 1), - (linenr_T)(oap->end.lnum + 1)) == FAIL) + (linenr_T)(oap->end.lnum + 1)) == FAIL) { return; + } curwin->w_cursor = oap->start; - if (oap->is_VIsual) - /* When there is no change: need to remove the Visual selection */ + if (oap->is_VIsual) { + // When there is no change: need to remove the Visual selection redraw_curbuf_later(INVERTED); + } - /* Set '[ mark at the start of the formatted area */ + // Set '[ mark at the start of the formatted area curbuf->b_op_start = oap->start; /* For "gw" remember the cursor position and put it back below (adjusted * for joined and split lines). */ - if (keep_cursor) + if (keep_cursor) { saved_cursor = oap->cursor_start; + } format_lines(oap->line_count, keep_cursor); @@ -4115,13 +4202,14 @@ op_format( * If the cursor was moved one line back (e.g. with "Q}") go to the next * line, so "." will do the next lines. */ - if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) + if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) { ++curwin->w_cursor.lnum; + } beginline(BL_WHITE | BL_FIX); old_line_count = curbuf->b_ml.ml_line_count - old_line_count; msgmore(old_line_count); - /* put '] mark on the end of the formatted area */ + // put '] mark on the end of the formatted area curbuf->b_op_end = curwin->w_cursor; if (keep_cursor) { @@ -4149,25 +4237,22 @@ op_format( */ void op_formatexpr(oparg_T *oap) { - if (oap->is_VIsual) - /* When there is no change: need to remove the Visual selection */ + if (oap->is_VIsual) { + // When there is no change: need to remove the Visual selection redraw_curbuf_later(INVERTED); + } - if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0) + if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0) { /* As documented: when 'formatexpr' returns non-zero fall back to * internal formatting. */ op_format(oap, FALSE); + } } -int -fex_format( - linenr_T lnum, - long count, - int c /* character to be inserted */ -) +/// @param c character to be inserted +int fex_format(linenr_T lnum, long count, int c) { - int use_sandbox = was_set_insecurely( - curwin, (char_u *)"formatexpr", OPT_LOCAL); + int use_sandbox = was_set_insecurely(curwin, (char_u *)"formatexpr", OPT_LOCAL); int r; char_u *fex; @@ -4196,17 +4281,13 @@ fex_format( return r; } -/* - * Format "line_count" lines, starting at the cursor position. - * When "line_count" is negative, format until the end of the paragraph. - * Lines after the cursor line are saved for undo, caller must have saved the - * first line. - */ -void -format_lines( - linenr_T line_count, - int avoid_fex /* don't use 'formatexpr' */ -) +/// Format "line_count" lines, starting at the cursor position. +/// When "line_count" is negative, format until the end of the paragraph. +/// Lines after the cursor line are saved for undo, caller must have saved the +/// first line. +/// +/// @param avoid_fex don't use 'formatexpr' +void format_lines(linenr_T line_count, int avoid_fex) { bool is_not_par; // current line not part of parag. bool next_is_not_par; // next line not part of paragraph @@ -4244,11 +4325,12 @@ format_lines( is_not_par = true; } next_is_not_par = fmt_check_par(curwin->w_cursor.lnum - , &next_leader_len, &next_leader_flags, do_comments - ); + , &next_leader_len, &next_leader_flags, do_comments + ); is_end_par = (is_not_par || next_is_not_par); - if (!is_end_par && do_trail_white) + if (!is_end_par && do_trail_white) { is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1); + } curwin->w_cursor.lnum--; for (count = line_count; count != 0 && !got_int; --count) { @@ -4272,23 +4354,26 @@ format_lines( next_leader_flags = NULL; } else { next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1 - , &next_leader_len, &next_leader_flags, do_comments - ); - if (do_number_indent) + , &next_leader_len, &next_leader_flags, do_comments + ); + if (do_number_indent) { next_is_start_par = (get_number_indent(curwin->w_cursor.lnum + 1) > 0); + } } advance = true; is_end_par = (is_not_par || next_is_not_par || next_is_start_par); - if (!is_end_par && do_trail_white) + if (!is_end_par && do_trail_white) { is_end_par = !ends_in_white(curwin->w_cursor.lnum); + } /* * Skip lines that are not in a paragraph. */ if (is_not_par) { - if (line_count < 0) + if (line_count < 0) { break; + } } else { /* * For the first line of a paragraph, check indent of second line. @@ -4300,7 +4385,7 @@ format_lines( && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) { if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1)) { if (leader_len == 0 && next_leader_len == 0) { - /* no comment found */ + // no comment found second_indent = get_indent_lnum(curwin->w_cursor.lnum + 1); } else { @@ -4309,11 +4394,11 @@ format_lines( } } else if (do_number_indent) { if (leader_len == 0 && next_leader_len == 0) { - /* no comment found */ + // no comment found second_indent = get_number_indent(curwin->w_cursor.lnum); } else { - /* get_number_indent() is now "comment aware"... */ + // get_number_indent() is now "comment aware"... second_indent = get_number_indent(curwin->w_cursor.lnum); do_comments_list = 1; @@ -4326,20 +4411,22 @@ format_lines( */ if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count || !same_leader(curwin->w_cursor.lnum, - leader_len, leader_flags, - next_leader_len, next_leader_flags) - ) + leader_len, leader_flags, + next_leader_len, + next_leader_flags)) { is_end_par = true; + } /* * If we have got to the end of a paragraph, or the line is * getting long, format it. */ if (is_end_par || force_format) { - if (need_set_indent) + if (need_set_indent) { /* replace indent in first line with minimal number of * tabs and spaces, according to current options */ (void)set_indent(get_indent(), SIN_CHANGED); + } // put cursor on last non-space State = NORMAL; // don't go past end-of-line @@ -4348,25 +4435,26 @@ format_lines( dec_cursor(); } - /* do the formatting, without 'showmode' */ - State = INSERT; /* for open_line() */ + // do the formatting, without 'showmode' + State = INSERT; // for open_line() smd_save = p_smd; p_smd = FALSE; insertchar(NUL, INSCHAR_FORMAT - + (do_comments ? INSCHAR_DO_COM : 0) - + (do_comments && do_comments_list + + (do_comments ? INSCHAR_DO_COM : 0) + + (do_comments && do_comments_list ? INSCHAR_COM_LIST : 0) - + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent); + + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent); State = old_State; p_smd = smd_save; second_indent = -1; - /* at end of par.: need to set indent of next par. */ + // at end of par.: need to set indent of next par. need_set_indent = is_end_par; if (is_end_par) { /* When called with a negative line count, break at the * end of the paragraph. */ - if (line_count < 0) + if (line_count < 0) { break; + } first_par_line = true; } force_format = false; @@ -4380,8 +4468,9 @@ format_lines( advance = false; curwin->w_cursor.lnum++; curwin->w_cursor.col = 0; - if (line_count < 0 && u_save_cursor() == FAIL) + if (line_count < 0 && u_save_cursor() == FAIL) { break; + } if (next_leader_len > 0) { (void)del_bytes(next_leader_len, false, false); mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L, @@ -4421,8 +4510,9 @@ static int ends_in_white(linenr_T lnum) char_u *s = ml_get(lnum); size_t l; - if (*s == NUL) + if (*s == NUL) { return FALSE; + } l = STRLEN(s) - 1; return ascii_iswhite(s[l]); } @@ -4437,22 +4527,24 @@ static int ends_in_white(linenr_T lnum) */ static int fmt_check_par(linenr_T lnum, int *leader_len, char_u **leader_flags, int do_comments) { - char_u *flags = NULL; /* init for GCC */ + char_u *flags = NULL; // init for GCC char_u *ptr; ptr = ml_get(lnum); - if (do_comments) + if (do_comments) { *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE); - else + } else { *leader_len = 0; + } if (*leader_len > 0) { /* * Search for 'e' flag in comment leader flags. */ flags = *leader_flags; - while (*flags && *flags != ':' && *flags != COM_END) + while (*flags && *flags != ':' && *flags != COM_END) { ++flags; + } } return *skipwhite(ptr + *leader_len) == NUL @@ -4472,13 +4564,13 @@ int paragraph_start(linenr_T lnum) int next_leader_len = 0; // leader len of next line char_u *next_leader_flags = NULL; // flags for leader of next line - if (lnum <= 1) - return TRUE; /* start of the file */ - + if (lnum <= 1) { + return TRUE; // start of the file + } p = ml_get(lnum - 1); - if (*p == NUL) - return TRUE; /* after empty line */ - + if (*p == NUL) { + return TRUE; // after empty line + } const bool do_comments = has_format_option(FO_Q_COMS); // format comments if (fmt_check_par(lnum - 1, &leader_len, &leader_flags, do_comments)) { return true; // after non-paragraph line @@ -4488,16 +4580,16 @@ int paragraph_start(linenr_T lnum) return true; // "lnum" is not a paragraph line } - if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1)) - return TRUE; /* missing trailing space in previous line. */ - - if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0)) - return TRUE; /* numbered item starts in "lnum". */ - + if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1)) { + return TRUE; // missing trailing space in previous line. + } + if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0)) { + return TRUE; // numbered item starts in "lnum". + } if (!same_leader(lnum - 1, leader_len, leader_flags, - next_leader_len, next_leader_flags)) - return TRUE; /* change of comment leader. */ - + next_leader_len, next_leader_flags)) { + return TRUE; // change of comment leader. + } return FALSE; } @@ -4514,8 +4606,7 @@ int paragraph_start(linenr_T lnum) * - start/endspaces is the number of columns of the first/last yanked char * that are to be yanked. */ -static void block_prep(oparg_T *oap, struct block_def *bdp, linenr_T lnum, - bool is_del) +static void block_prep(oparg_T *oap, struct block_def *bdp, linenr_T lnum, bool is_del) { int incr = 0; char_u *pend; @@ -4543,7 +4634,7 @@ static void block_prep(oparg_T *oap, struct block_def *bdp, linenr_T lnum, pstart = line; prev_pstart = line; while (bdp->start_vcol < oap->start_vcol && *pstart) { - /* Count a tab for what it's worth (if list mode not on) */ + // Count a tab for what it's worth (if list mode not on) incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol); bdp->start_vcol += incr; if (ascii_iswhite(*pstart)) { @@ -4557,7 +4648,7 @@ static void block_prep(oparg_T *oap, struct block_def *bdp, linenr_T lnum, MB_PTR_ADV(pstart); } bdp->start_char_vcols = incr; - if (bdp->start_vcol < oap->start_vcol) { /* line too short */ + if (bdp->start_vcol < oap->start_vcol) { // line too short bdp->end_vcol = bdp->start_vcol; bdp->is_short = true; if (!is_del || oap->op_type == OP_APPEND) { @@ -4567,8 +4658,9 @@ static void block_prep(oparg_T *oap, struct block_def *bdp, linenr_T lnum, /* notice: this converts partly selected Multibyte characters to * spaces, too. */ bdp->startspaces = bdp->start_vcol - oap->start_vcol; - if (is_del && bdp->startspaces) + if (is_del && bdp->startspaces) { bdp->startspaces = bdp->start_char_vcols - bdp->startspaces; + } pend = pstart; bdp->end_vcol = bdp->start_vcol; if (bdp->end_vcol > oap->end_vcol) { // it's all in one character @@ -4592,7 +4684,7 @@ static void block_prep(oparg_T *oap, struct block_def *bdp, linenr_T lnum, } else { prev_pend = pend; while (bdp->end_vcol <= oap->end_vcol && *pend != NUL) { - /* Count a tab for what it's worth (if list mode not on) */ + // Count a tab for what it's worth (if list mode not on) prev_pend = pend; incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol); bdp->end_vcol += incr; @@ -4614,17 +4706,19 @@ static void block_prep(oparg_T *oap, struct block_def *bdp, linenr_T lnum, bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1; if (!is_del && bdp->endspaces) { bdp->endspaces = incr - bdp->endspaces; - if (pend != pstart) + if (pend != pstart) { pend = prev_pend; + } } } } bdp->end_char_vcols = incr; - if (is_del && bdp->startspaces) + if (is_del && bdp->startspaces) { pstart = prev_pstart; + } bdp->textlen = (int)(pend - pstart); } - bdp->textcol = (colnr_T) (pstart - line); + bdp->textcol = (colnr_T)(pstart - line); bdp->textstart = pstart; curwin->w_p_lbr = lbr_saved; } @@ -4801,14 +4895,14 @@ int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1) && ptr[col - 1] == '0' && !utf_head_off(ptr, ptr + col - 1) && ascii_isxdigit(ptr[col + 1])))) { - // In case of binary/hexadecimal pattern overlap match, rescan + // In case of binary/hexadecimal pattern overlap match, rescan - col = curwin->w_cursor.col; + col = curwin->w_cursor.col; - while (col > 0 && ascii_isdigit(ptr[col])) { - col--; - col -= utf_head_off(ptr, ptr + col); - } + while (col > 0 && ascii_isdigit(ptr[col])) { + col--; + col -= utf_head_off(ptr, ptr + col); + } } if ((do_hex @@ -4824,8 +4918,8 @@ int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1) && !utf_head_off(ptr, ptr + col - 1) && ascii_isbdigit(ptr[col + 1]))) { // Found hexadecimal or binary number, move to its start. - col--; - col -= utf_head_off(ptr, ptr + col); + col--; + col -= utf_head_off(ptr, ptr + col); } else { // Search forward and then backward to find the start of number. col = pos->col; @@ -5031,17 +5125,16 @@ int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1) // leading zeros for (bits = 8 * sizeof(n); bits > 0; bits--) { - if ((n >> (bits - 1)) & 0x1) { - break; - } + if ((n >> (bits - 1)) & 0x1) { + break; + } } while (bits > 0) { - buf2[i++] = ((n >> --bits) & 0x1) ? '1' : '0'; + buf2[i++] = ((n >> --bits) & 0x1) ? '1' : '0'; } buf2[i] = '\0'; - } else if (pre == 0) { vim_snprintf((char *)buf2, ARRAY_SIZE(buf2), "%" PRIu64, (uint64_t)n); } else if (pre == '0') { @@ -5099,18 +5192,18 @@ theend: MotionType get_reg_type(int regname, colnr_T *reg_width) { switch (regname) { - case '%': // file name - case '#': // alternate file name - case '=': // expression - case ':': // last command line - case '/': // last search-pattern - case '.': // last inserted text - case Ctrl_F: // Filename under cursor - case Ctrl_P: // Path under cursor, expand via "path" - case Ctrl_W: // word under cursor - case Ctrl_A: // WORD (mnemonic All) under cursor - case '_': // black hole: always empty - return kMTCharWise; + case '%': // file name + case '#': // alternate file name + case '=': // expression + case ':': // last command line + case '/': // last search-pattern + case '.': // last inserted text + case Ctrl_F: // Filename under cursor + case Ctrl_P: // Path under cursor, expand via "path" + case Ctrl_W: // word under cursor + case Ctrl_A: // WORD (mnemonic All) under cursor + case '_': // black hole: always empty + return kMTCharWise; } if (regname != NUL && !valid_yank_reg(regname, false)) { @@ -5135,26 +5228,25 @@ MotionType get_reg_type(int regname, colnr_T *reg_width) /// @param[out] buf Buffer to store formatted string. The allocated size should /// be at least NUMBUFLEN+2 to always fit the value. /// @param buf_len The allocated size of the buffer. -void format_reg_type(MotionType reg_type, colnr_T reg_width, - char *buf, size_t buf_len) +void format_reg_type(MotionType reg_type, colnr_T reg_width, char *buf, size_t buf_len) FUNC_ATTR_NONNULL_ALL { assert(buf_len > 1); switch (reg_type) { - case kMTLineWise: - buf[0] = 'V'; - buf[1] = NUL; - break; - case kMTCharWise: - buf[0] = 'v'; - buf[1] = NUL; - break; - case kMTBlockWise: - snprintf(buf, buf_len, CTRL_V_STR "%" PRIdCOLNR, reg_width + 1); - break; - case kMTUnknown: - buf[0] = NUL; - break; + case kMTLineWise: + buf[0] = 'V'; + buf[1] = NUL; + break; + case kMTCharWise: + buf[0] = 'v'; + buf[1] = NUL; + break; + case kMTBlockWise: + snprintf(buf, buf_len, CTRL_V_STR "%" PRIdCOLNR, reg_width + 1); + break; + case kMTUnknown: + buf[0] = NUL; + break; } } @@ -5195,12 +5287,14 @@ void *get_reg_contents(int regname, int flags) return get_reg_wrap_one_line(get_expr_line(), flags); } - if (regname == '@') /* "@@" is used for unnamed register */ + if (regname == '@') { // "@@" is used for unnamed register regname = '"'; + } - /* check for valid regname */ - if (regname != NUL && !valid_yank_reg(regname, false)) + // check for valid regname + if (regname != NUL && !valid_yank_reg(regname, false)) { return NULL; + } char_u *retval; bool allocated; @@ -5215,8 +5309,9 @@ void *get_reg_contents(int regname, int flags) } yankreg_T *reg = get_yank_register(regname, YREG_PASTE); - if (reg->y_array == NULL) + if (reg->y_array == NULL) { return NULL; + } if (flags & kGRegList) { list_T *const list = tv_list_alloc((ptrdiff_t)reg->y_size); @@ -5277,7 +5372,7 @@ static yankreg_T *init_write_reg(int name, yankreg_T **old_y_previous, bool must yankreg_T *reg = get_yank_register(name, YREG_YANK); if (!is_append_register(name) && !must_append) { - free_register(reg); + free_register(reg); } return reg; } @@ -5296,14 +5391,12 @@ static void finish_write_reg(int name, yankreg_T *reg, yankreg_T *old_y_previous /// write_reg_contents - store `str` in register `name` /// /// @see write_reg_contents_ex -void write_reg_contents(int name, const char_u *str, ssize_t len, - int must_append) +void write_reg_contents(int name, const char_u *str, ssize_t len, int must_append) { write_reg_contents_ex(name, str, len, must_append, kMTUnknown, 0L); } -void write_reg_contents_lst(int name, char_u **strings, - bool must_append, MotionType yank_type, +void write_reg_contents_lst(int name, char_u **strings, bool must_append, MotionType yank_type, colnr_T block_len) { if (name == '/' || name == '=') { @@ -5352,18 +5445,14 @@ void write_reg_contents_lst(int name, char_u **strings, /// is an uppercase letter. /// @param yank_type The motion type (kMTUnknown to auto detect) /// @param block_len width of visual block -void write_reg_contents_ex(int name, - const char_u *str, - ssize_t len, - bool must_append, - MotionType yank_type, - colnr_T block_len) +void write_reg_contents_ex(int name, const char_u *str, ssize_t len, bool must_append, + MotionType yank_type, colnr_T block_len) { if (len < 0) { - len = (ssize_t) STRLEN(str); + len = (ssize_t)STRLEN(str); } - /* Special case: '/' search pattern */ + // Special case: '/' search pattern if (name == '/') { set_last_search_pat(str, RE_SEARCH, TRUE, TRUE); return; @@ -5392,7 +5481,7 @@ void write_reg_contents_ex(int name, if (name == '=') { size_t offset = 0; - size_t totlen = (size_t) len; + size_t totlen = (size_t)len; if (must_append && expr_line) { // append has been specified and expr_line already exists, so we'll @@ -5435,9 +5524,8 @@ void write_reg_contents_ex(int name, /// @param len length of the string (Ignored when str_list=true.) /// @param blocklen width of visual block, or -1 for "I don't know." /// @param str_list True if str is `char_u **`. -static void str_to_reg(yankreg_T *y_ptr, MotionType yank_type, - const char_u *str, size_t len, colnr_T blocklen, - bool str_list) +static void str_to_reg(yankreg_T *y_ptr, MotionType yank_type, const char_u *str, size_t len, + colnr_T blocklen, bool str_list) FUNC_ATTR_NONNULL_ALL { if (y_ptr->y_array == NULL) { // NULL means empty register @@ -5456,7 +5544,7 @@ static void str_to_reg(yankreg_T *y_ptr, MotionType yank_type, // Count the number of lines within the string if (str_list) { - for (char_u **ss = (char_u **) str; *ss != NULL; ++ss) { + for (char_u **ss = (char_u **)str; *ss != NULL; ++ss) { newlines++; } } else { @@ -5484,7 +5572,7 @@ static void str_to_reg(yankreg_T *y_ptr, MotionType yank_type, // Find the end of each line and save it into the array. if (str_list) { - for (char_u **ss = (char_u **) str; *ss != NULL; ++ss, ++lnum) { + for (char_u **ss = (char_u **)str; *ss != NULL; ++ss, ++lnum) { size_t ss_len = STRLEN(*ss); pp[lnum] = xmemdupz(*ss, ss_len); if (ss_len > maxlen) { @@ -5525,7 +5613,7 @@ static void str_to_reg(yankreg_T *y_ptr, MotionType yank_type, set_yreg_additional_data(y_ptr, NULL); y_ptr->timestamp = os_time(); if (yank_type == kMTBlockWise) { - y_ptr->y_width = (blocklen == -1 ? (colnr_T) maxlen - 1 : blocklen); + y_ptr->y_width = (blocklen == -1 ? (colnr_T)maxlen - 1 : blocklen); } else { y_ptr->y_width = 0; } @@ -5551,9 +5639,8 @@ void clear_oparg(oparg_T *oap) * case, eol_size will be added to the character count to account for * the size of the EOL character. */ -static varnumber_T line_count_info(char_u *line, varnumber_T *wc, - varnumber_T *cc, varnumber_T limit, - int eol_size) +static varnumber_T line_count_info(char_u *line, varnumber_T *wc, varnumber_T *cc, + varnumber_T limit, int eol_size) { varnumber_T i; varnumber_T words = 0; @@ -5566,17 +5653,19 @@ static varnumber_T line_count_info(char_u *line, varnumber_T *wc, words++; is_word = 0; } - } else if (!ascii_isspace(line[i])) + } else if (!ascii_isspace(line[i])) { is_word = 1; + } ++chars; i += (*mb_ptr2len)(line + i); } - if (is_word) + if (is_word) { words++; + } *wc += words; - /* Add eol_size if the end of line was reached before hitting limit. */ + // Add eol_size if the end of line was reached before hitting limit. if (i < limit && line[i] == NUL) { i += eol_size; chars += eol_size; @@ -5618,10 +5707,11 @@ void cursor_pos_info(dict_T *dict) return; } } else { - if (get_fileformat(curbuf) == EOL_DOS) + if (get_fileformat(curbuf) == EOL_DOS) { eol_size = 2; - else + } else { eol_size = 1; + } if (l_VIsual_active) { if (lt(VIsual, curwin->w_cursor)) { @@ -5631,23 +5721,25 @@ void cursor_pos_info(dict_T *dict) min_pos = curwin->w_cursor; max_pos = VIsual; } - if (*p_sel == 'e' && max_pos.col > 0) + if (*p_sel == 'e' && max_pos.col > 0) { --max_pos.col; + } if (l_VIsual_mode == Ctrl_V) { char_u * saved_sbr = p_sbr; - /* Make 'sbr' empty for a moment to get the correct size. */ + // Make 'sbr' empty for a moment to get the correct size. p_sbr = empty_option; oparg.is_VIsual = true; oparg.motion_type = kMTBlockWise; oparg.op_type = OP_NOP; getvcols(curwin, &min_pos, &max_pos, - &oparg.start_vcol, &oparg.end_vcol); + &oparg.start_vcol, &oparg.end_vcol); p_sbr = saved_sbr; - if (curwin->w_curswant == MAXCOL) + if (curwin->w_curswant == MAXCOL) { oparg.end_vcol = MAXCOL; - /* Swap the start, end vcol if needed */ + } + // Swap the start, end vcol if needed if (oparg.end_vcol < oparg.start_vcol) { oparg.end_vcol += oparg.start_vcol; oparg.start_vcol = oparg.end_vcol - oparg.start_vcol; @@ -5658,15 +5750,16 @@ void cursor_pos_info(dict_T *dict) } for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) { - /* Check for a CTRL-C every 100000 characters. */ + // Check for a CTRL-C every 100000 characters. if (byte_count > last_check) { os_breakcheck(); - if (got_int) + if (got_int) { return; + } last_check = byte_count + 100000L; } - /* Do extra processing for VIsual mode. */ + // Do extra processing for VIsual mode. if (l_VIsual_active && lnum >= min_pos.lnum && lnum <= max_pos.lnum) { char_u *s = NULL; @@ -5684,37 +5777,37 @@ void cursor_pos_info(dict_T *dict) s = ml_get(lnum); len = MAXCOL; break; - case 'v': - { - colnr_T start_col = (lnum == min_pos.lnum) + case 'v': { + colnr_T start_col = (lnum == min_pos.lnum) ? min_pos.col : 0; - colnr_T end_col = (lnum == max_pos.lnum) + colnr_T end_col = (lnum == max_pos.lnum) ? max_pos.col - start_col + 1 : MAXCOL; - s = ml_get(lnum) + start_col; - len = end_col; - } - break; + s = ml_get(lnum) + start_col; + len = end_col; + } + break; } if (s != NULL) { byte_count_cursor += line_count_info(s, &word_count_cursor, - &char_count_cursor, len, eol_size); + &char_count_cursor, len, eol_size); if (lnum == curbuf->b_ml.ml_line_count && !curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol) - && (long)STRLEN(s) < len) + && (long)STRLEN(s) < len) { byte_count_cursor -= eol_size; + } } } else { - /* In non-visual mode, check for the line the cursor is on */ + // In non-visual mode, check for the line the cursor is on if (lnum == curwin->w_cursor.lnum) { word_count_cursor += word_count; char_count_cursor += char_count; byte_count_cursor = byte_count - + line_count_info(ml_get(lnum), &word_count_cursor, - &char_count_cursor, - (varnumber_T)curwin->w_cursor.col + 1, - eol_size); + + line_count_info(ml_get(lnum), &word_count_cursor, + &char_count_cursor, + (varnumber_T)curwin->w_cursor.col + 1, + eol_size); } } // Add to the running totals @@ -5912,13 +6005,16 @@ bool prepare_yankreg_from_object(yankreg_T *reg, String regtype, size_t lines) case 0: reg->y_type = kMTUnknown; break; - case 'v': case 'c': + case 'v': + case 'c': reg->y_type = kMTCharWise; break; - case 'V': case 'l': + case 'V': + case 'l': reg->y_type = kMTLineWise; break; - case 'b': case Ctrl_V: + case 'b': + case Ctrl_V: reg->y_type = kMTBlockWise; break; default: @@ -6023,13 +6119,16 @@ static bool get_clipboard(int name, yankreg_T **target, bool quiet) case 0: reg->y_type = kMTUnknown; break; - case 'v': case 'c': + case 'v': + case 'c': reg->y_type = kMTCharWise; break; - case 'V': case 'l': + case 'V': + case 'l': reg->y_type = kMTLineWise; break; - case 'b': case Ctrl_V: + case 'b': + case Ctrl_V: reg->y_type = kMTBlockWise; break; default: @@ -6053,8 +6152,7 @@ static bool get_clipboard(int name, yankreg_T **target, bool quiet) if (TV_LIST_ITEM_TV(li)->v_type != VAR_STRING) { goto err; } - reg->y_array[tv_idx++] = (char_u *)xstrdupnul( - (const char *)TV_LIST_ITEM_TV(li)->vval.v_string); + reg->y_array[tv_idx++] = (char_u *)xstrdupnul((const char *)TV_LIST_ITEM_TV(li)->vval.v_string); }); if (reg->y_size > 0 && strlen((char*)reg->y_array[reg->y_size-1]) == 0) { @@ -6112,8 +6210,7 @@ static void set_clipboard(int name, yankreg_T *reg) return; } - list_T *const lines = tv_list_alloc( - (ptrdiff_t)reg->y_size + (reg->y_type != kMTCharWise)); + list_T *const lines = tv_list_alloc((ptrdiff_t)reg->y_size + (reg->y_type != kMTCharWise)); for (size_t i = 0; i < reg->y_size; i++) { tv_list_append_string(lines, (const char *)reg->y_array[i], -1); @@ -6121,21 +6218,21 @@ static void set_clipboard(int name, yankreg_T *reg) char regtype; switch (reg->y_type) { - case kMTLineWise: { + case kMTLineWise: { regtype = 'V'; tv_list_append_string(lines, NULL, 0); break; } - case kMTCharWise: { + case kMTCharWise: { regtype = 'v'; break; } - case kMTBlockWise: { + case kMTBlockWise: { regtype = 'b'; tv_list_append_string(lines, NULL, 0); break; } - case kMTUnknown: { + case kMTUnknown: { abort(); } } @@ -6211,8 +6308,8 @@ static inline bool reg_empty(const yankreg_T *const reg) /// Iterate over global registers. /// /// @see op_register_iter -const void *op_global_reg_iter(const void *const iter, char *const name, - yankreg_T *const reg, bool *is_unnamed) +const void *op_global_reg_iter(const void *const iter, char *const name, yankreg_T *const reg, + bool *is_unnamed) FUNC_ATTR_NONNULL_ARG(2, 3, 4) FUNC_ATTR_WARN_UNUSED_RESULT { return op_reg_iter(iter, y_regs, name, reg, is_unnamed); @@ -6227,9 +6324,8 @@ const void *op_global_reg_iter(const void *const iter, char *const name, /// /// @return Pointer that must be passed to next `op_register_iter` call or /// NULL if iteration is over. -const void *op_reg_iter(const void *const iter, const yankreg_T *const regs, - char *const name, yankreg_T *const reg, - bool *is_unnamed) +const void *op_reg_iter(const void *const iter, const yankreg_T *const regs, char *const name, + yankreg_T *const reg, bool *is_unnamed) FUNC_ATTR_NONNULL_ARG(3, 4, 5) FUNC_ATTR_WARN_UNUSED_RESULT { *name = NUL; @@ -6248,7 +6344,7 @@ const void *op_reg_iter(const void *const iter, const yankreg_T *const regs, *is_unnamed = (iter_reg == y_previous); while (++iter_reg - &(regs[0]) < NUM_SAVED_REGISTERS) { if (!reg_empty(iter_reg)) { - return (void *) iter_reg; + return (void *)iter_reg; } } return NULL; @@ -6323,8 +6419,7 @@ bool op_reg_set_previous(const char name) /// Get the byte count of buffer region. End-exclusive. /// /// @return number of bytes -bcount_t get_region_bytecount(buf_T *buf, linenr_T start_lnum, - linenr_T end_lnum, colnr_T start_col, +bcount_t get_region_bytecount(buf_T *buf, linenr_T start_lnum, linenr_T end_lnum, colnr_T start_col, colnr_T end_col) { linenr_T max_lnum = buf->b_ml.ml_line_count; @@ -6341,8 +6436,7 @@ bcount_t get_region_bytecount(buf_T *buf, linenr_T start_lnum, if (start_lnum + i > max_lnum) { return deleted_bytes; } - deleted_bytes += (bcount_t)STRLEN( - ml_get_buf(buf, start_lnum + i, false)) + 1; + deleted_bytes += (bcount_t)STRLEN(ml_get_buf(buf, start_lnum + i, false)) + 1; } if (end_lnum > max_lnum) { return deleted_bytes; diff --git a/src/nvim/screen.c b/src/nvim/screen.c index 0ccf15ad4f..cc0f0feef7 100644 --- a/src/nvim/screen.c +++ b/src/nvim/screen.c @@ -4941,15 +4941,15 @@ static int skip_status_match_char(expand_T *xp, char_u *s) return 0; } -/* - * Show wildchar matches in the status line. - * Show at least the "match" item. - * We start at item 'first_match' in the list and show all matches that fit. - * - * If inversion is possible we use it. Else '=' characters are used. - */ -void win_redr_status_matches(expand_T *xp, int num_matches, char_u **matches, // list of matches - int match, int showtail) +/// Show wildchar matches in the status line. +/// Show at least the "match" item. +/// We start at item 'first_match' in the list and show all matches that fit. +/// +/// If inversion is possible we use it. Else '=' characters are used. +/// +/// @param matches list of matches +void win_redr_status_matches(expand_T *xp, int num_matches, char_u **matches, int match, + int showtail) { #define L_MATCH(m) (showtail ? sm_gettail(matches[m], false) : matches[m]) int row; @@ -6018,18 +6018,18 @@ static void prepare_search_hl(win_T *wp, linenr_T lnum) } } -/* - * Search for a next 'hlsearch' or match. - * Uses shl->buf. - * Sets shl->lnum and shl->rm contents. - * Note: Assumes a previous match is always before "lnum", unless - * shl->lnum is zero. - * Careful: Any pointers for buffer lines will become invalid. - */ -static void next_search_hl(win_T *win, match_T *shl, // points to search_hl or a match - linenr_T lnum, colnr_T mincol, // minimal column for a match - matchitem_T *cur // to retrieve match positions if any - ) +/// Search for a next 'hlsearch' or match. +/// Uses shl->buf. +/// Sets shl->lnum and shl->rm contents. +/// Note: Assumes a previous match is always before "lnum", unless +/// shl->lnum is zero. +/// Careful: Any pointers for buffer lines will become invalid. +/// +/// @param shl points to search_hl or a match +/// @param mincol minimal column for a match +/// @param cur to retrieve match positions if any +static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, + matchitem_T *cur) FUNC_ATTR_NONNULL_ARG(2) { linenr_T l; @@ -6139,12 +6139,12 @@ static void next_search_hl(win_T *win, match_T *shl, // points to } } -/// If there is a match fill "shl" and return one. -/// Return zero otherwise. -static int next_search_hl_pos(match_T *shl, // points to a match - linenr_T lnum, posmatch_T *posmatch, // match positions - colnr_T mincol // minimal column for a match - ) +/// @param shl points to a match. Fill on match. +/// @param posmatch match positions +/// @param mincol minimal column for a match +/// +/// @return one on match, otherwise return zero. +static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *posmatch, colnr_T mincol) FUNC_ATTR_NONNULL_ALL { int i; @@ -6960,12 +6960,18 @@ int showmode(void) switch ((VIsual_select ? 4 : 0) + (VIsual_mode == Ctrl_V) * 2 + (VIsual_mode == 'V')) { - case 0: p = N_(" VISUAL"); break; - case 1: p = N_(" VISUAL LINE"); break; - case 2: p = N_(" VISUAL BLOCK"); break; - case 4: p = N_(" SELECT"); break; - case 5: p = N_(" SELECT LINE"); break; - default: p = N_(" SELECT BLOCK"); break; + case 0: + p = N_(" VISUAL"); break; + case 1: + p = N_(" VISUAL LINE"); break; + case 2: + p = N_(" VISUAL BLOCK"); break; + case 4: + p = N_(" SELECT"); break; + case 5: + p = N_(" SELECT LINE"); break; + default: + p = N_(" SELECT BLOCK"); break; } MSG_PUTS_ATTR(_(p), attr); } diff --git a/src/nvim/window.c b/src/nvim/window.c index eddbe14da2..222c1363dc 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -6,9 +6,7 @@ #include #include "nvim/api/private/helpers.h" -#include "nvim/vim.h" #include "nvim/ascii.h" -#include "nvim/window.h" #include "nvim/buffer.h" #include "nvim/charset.h" #include "nvim/cursor.h" @@ -20,8 +18,10 @@ #include "nvim/ex_docmd.h" #include "nvim/ex_eval.h" #include "nvim/ex_getln.h" +#include "nvim/file_search.h" #include "nvim/fileio.h" #include "nvim/fold.h" +#include "nvim/garray.h" #include "nvim/getchar.h" #include "nvim/hashtab.h" #include "nvim/main.h" @@ -30,15 +30,14 @@ #include "nvim/memory.h" #include "nvim/message.h" #include "nvim/misc1.h" -#include "nvim/plines.h" -#include "nvim/file_search.h" -#include "nvim/garray.h" -#include "nvim/move.h" #include "nvim/mouse.h" +#include "nvim/move.h" #include "nvim/normal.h" #include "nvim/option.h" +#include "nvim/os/os.h" #include "nvim/os_unix.h" #include "nvim/path.h" +#include "nvim/plines.h" #include "nvim/quickfix.h" #include "nvim/regexp.h" #include "nvim/screen.h" @@ -47,10 +46,11 @@ #include "nvim/strings.h" #include "nvim/syntax.h" #include "nvim/terminal.h" -#include "nvim/undo.h" #include "nvim/ui.h" #include "nvim/ui_compositor.h" -#include "nvim/os/os.h" +#include "nvim/undo.h" +#include "nvim/vim.h" +#include "nvim/window.h" #ifdef INCLUDE_GENERATED_DECLARATIONS @@ -60,22 +60,17 @@ -#define NOWIN (win_T *)-1 /* non-existing window */ +#define NOWIN (win_T *)-1 // non-existing window # define ROWS_AVAIL (Rows - p_ch - tabline_height()) static char *m_onlyone = N_("Already only one window"); -/* - * all CTRL-W window commands are handled here, called from normal_cmd(). - */ -void -do_window( - int nchar, - long Prenum, - int xchar /* extra char from ":wincmd gx" or NUL */ -) +/// all CTRL-W window commands are handled here, called from normal_cmd(). +/// +/// @param xchar extra char from ":wincmd gx" or NUL +void do_window(int nchar, long Prenum, int xchar) { long Prenum1; win_T *wp; @@ -96,7 +91,7 @@ do_window( } while (0) switch (nchar) { - /* split current window in two parts, horizontally */ + // split current window in two parts, horizontally case 'S': case Ctrl_S: case 's': @@ -110,7 +105,7 @@ do_window( (void)win_split((int)Prenum, 0); break; - /* split current window in two parts, vertically */ + // split current window in two parts, vertically case Ctrl_V: case 'v': CHECK_CMDWIN; @@ -123,7 +118,7 @@ do_window( (void)win_split((int)Prenum, WSP_VERT); break; - /* split current window and edit alternate file */ + // split current window and edit alternate file case Ctrl_HAT: case '^': CHECK_CMDWIN; @@ -144,17 +139,18 @@ do_window( } break; - /* open new window */ + // open new window case Ctrl_N: case 'n': CHECK_CMDWIN; reset_VIsual_and_resel(); // stop Visual mode newwindow: - if (Prenum) - /* window height */ + if (Prenum) { + // window height vim_snprintf(cbuf, sizeof(cbuf) - 5, "%" PRId64, (int64_t)Prenum); - else + } else { cbuf[0] = NUL; + } if (nchar == 'v' || nchar == Ctrl_V) { xstrlcat(cbuf, "v", sizeof(cbuf)); } @@ -162,23 +158,23 @@ newwindow: do_cmdline_cmd(cbuf); break; - /* quit current window */ + // quit current window case Ctrl_Q: case 'q': - reset_VIsual_and_resel(); /* stop Visual mode */ + reset_VIsual_and_resel(); // stop Visual mode cmd_with_count("quit", (char_u *)cbuf, sizeof(cbuf), Prenum); do_cmdline_cmd(cbuf); break; - /* close current window */ + // close current window case Ctrl_C: case 'c': - reset_VIsual_and_resel(); /* stop Visual mode */ + reset_VIsual_and_resel(); // stop Visual mode cmd_with_count("close", (char_u *)cbuf, sizeof(cbuf), Prenum); do_cmdline_cmd(cbuf); break; - /* close preview window */ + // close preview window case Ctrl_Z: case 'z': CHECK_CMDWIN; @@ -186,7 +182,7 @@ newwindow: do_cmdline_cmd("pclose"); break; - /* cursor to preview window */ + // cursor to preview window case 'P': wp = NULL; FOR_ALL_WINDOWS_IN_TAB(wp2, curtab) { @@ -202,7 +198,7 @@ newwindow: } break; - /* close all but current window */ + // close all but current window case Ctrl_O: case 'o': CHECK_CMDWIN; @@ -211,10 +207,10 @@ newwindow: do_cmdline_cmd(cbuf); break; - /* cursor to next window with wrap around */ + // cursor to next window with wrap around case Ctrl_W: case 'w': - /* cursor to previous window with wrap around */ + // cursor to previous window with wrap around case 'W': CHECK_CMDWIN; if (ONE_WINDOW && Prenum != 1) { // just one window @@ -222,10 +218,11 @@ newwindow: } else { if (Prenum) { // go to specified window for (wp = firstwin; --Prenum > 0; ) { - if (wp->w_next == NULL) + if (wp->w_next == NULL) { break; - else + } else { wp = wp->w_next; + } } } else { if (nchar == 'W') { // go to previous window @@ -252,7 +249,7 @@ newwindow: } break; - /* cursor to window below */ + // cursor to window below case 'j': case K_DOWN: case Ctrl_J: @@ -260,7 +257,7 @@ newwindow: win_goto_ver(false, Prenum1); break; - /* cursor to window above */ + // cursor to window above case 'k': case K_UP: case Ctrl_K: @@ -268,7 +265,7 @@ newwindow: win_goto_ver(true, Prenum1); break; - /* cursor to left window */ + // cursor to left window case 'h': case K_LEFT: case Ctrl_H: @@ -277,7 +274,7 @@ newwindow: win_goto_hor(true, Prenum1); break; - /* cursor to right window */ + // cursor to right window case 'l': case K_RIGHT: case Ctrl_L: @@ -285,11 +282,11 @@ newwindow: win_goto_hor(false, Prenum1); break; - /* move window to new tab page */ + // move window to new tab page case 'T': - if (one_window()) + if (one_window()) { MSG(_(m_onlyone)); - else { + } else { tabpage_T *oldtab = curtab; tabpage_T *newtab; @@ -311,19 +308,19 @@ newwindow: } break; - /* cursor to top-left window */ + // cursor to top-left window case 't': case Ctrl_T: win_goto(firstwin); break; - /* cursor to bottom-right window */ + // cursor to bottom-right window case 'b': case Ctrl_B: win_goto(lastwin_nofloating()); break; - /* cursor to last accessed (previous) window */ + // cursor to last accessed (previous) window case 'p': case Ctrl_P: if (!win_valid(prevwin)) { @@ -333,14 +330,14 @@ newwindow: } break; - /* exchange current and next window */ + // exchange current and next window case 'x': case Ctrl_X: CHECK_CMDWIN; win_exchange(Prenum); break; - /* rotate windows downwards */ + // rotate windows downwards case Ctrl_R: case 'r': CHECK_CMDWIN; @@ -348,14 +345,14 @@ newwindow: win_rotate(false, (int)Prenum1); // downwards break; - /* rotate windows upwards */ + // rotate windows upwards case 'R': CHECK_CMDWIN; reset_VIsual_and_resel(); // stop Visual mode win_rotate(true, (int)Prenum1); // upwards break; - /* move window to the very top/bottom/left/right */ + // move window to the very top/bottom/left/right case 'K': case 'J': case 'H': @@ -366,43 +363,43 @@ newwindow: | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT)); break; - /* make all windows the same height */ + // make all windows the same height case '=': win_equal(NULL, false, 'b'); break; - /* increase current window height */ + // increase current window height case '+': win_setheight(curwin->w_height + (int)Prenum1); break; - /* decrease current window height */ + // decrease current window height case '-': win_setheight(curwin->w_height - (int)Prenum1); break; - /* set current window height */ + // set current window height case Ctrl__: case '_': win_setheight(Prenum ? (int)Prenum : Rows-1); break; - /* increase current window width */ + // increase current window width case '>': win_setwidth(curwin->w_width + (int)Prenum1); break; - /* decrease current window width */ + // decrease current window width case '<': win_setwidth(curwin->w_width - (int)Prenum1); break; - /* set current window width */ + // set current window width case '|': win_setwidth(Prenum != 0 ? (int)Prenum : Columns); break; - /* jump to tag and split window if tag exists (in preview window) */ + // jump to tag and split window if tag exists (in preview window) case '}': CHECK_CMDWIN; if (Prenum) { @@ -415,10 +412,11 @@ newwindow: case Ctrl_RSB: CHECK_CMDWIN; // Keep visual mode, can select words to use as a tag. - if (Prenum) + if (Prenum) { postponed_split = Prenum; - else + } else { postponed_split = -1; + } if (nchar != '}') { g_do_tagpreview = 0; @@ -429,7 +427,7 @@ newwindow: do_nv_ident(Ctrl_RSB, NUL); break; - /* edit file name under cursor in a new window */ + // edit file name under cursor in a new window case 'f': case 'F': case Ctrl_F: @@ -460,7 +458,7 @@ wingotofile: /* Go to the first occurrence of the identifier under cursor along path in a * new window -- webb */ - case 'i': /* Go to any match */ + case 'i': // Go to any match case Ctrl_I: type = FIND_ANY; FALLTHROUGH; @@ -484,7 +482,7 @@ wingotofile: break; - /* CTRL-W g extended commands */ + // CTRL-W g extended commands case 'g': case Ctrl_G: CHECK_CMDWIN; @@ -498,18 +496,20 @@ wingotofile: switch (xchar) { case '}': xchar = Ctrl_RSB; - if (Prenum) + if (Prenum) { g_do_tagpreview = Prenum; - else + } else { g_do_tagpreview = p_pvh; + } FALLTHROUGH; case ']': case Ctrl_RSB: // Keep visual mode, can select words to use as a tag. - if (Prenum) + if (Prenum) { postponed_split = Prenum; - else + } else { postponed_split = -1; + } /* Execute the command right here, required when * "wincmd g}" was used in a function. */ @@ -520,8 +520,8 @@ wingotofile: goto_tabpage_lastused(); break; - case 'f': /* CTRL-W gf: "gf" in a new tab page */ - case 'F': /* CTRL-W gF: "gF" in a new tab page */ + case 'f': // CTRL-W gf: "gf" in a new tab page + case 'F': // CTRL-W gF: "gF" in a new tab page cmdmod.tab = tabpage_index(curtab) + 1; nchar = xchar; goto wingotofile; @@ -555,13 +555,13 @@ wingotofile: } break; - default: beep_flush(); + default: + beep_flush(); break; } } -static void cmd_with_count(char *cmd, char_u *bufp, size_t bufsize, - int64_t Prenum) +static void cmd_with_count(char *cmd, char_u *bufp, size_t bufsize, int64_t Prenum) { size_t len = xstrlcpy((char *)bufp, cmd, bufsize); @@ -834,7 +834,6 @@ void ui_ext_win_position(win_T *wp) } else { ui_call_win_external_pos(wp->w_grid_alloc.handle, wp->handle); } - } void ui_ext_win_viewport(win_T *wp) @@ -870,11 +869,12 @@ void ui_ext_win_viewport(win_T *wp) */ int win_split(int size, int flags) { - /* When the ":tab" modifier was used open a new tab page instead. */ - if (may_open_tabpage() == OK) + // When the ":tab" modifier was used open a new tab page instead. + if (may_open_tabpage() == OK) { return OK; + } - /* Add flags from ":vertical", ":topleft" and ":botright". */ + // Add flags from ":vertical", ":topleft" and ":botright". flags |= cmdmod.split; if ((flags & WSP_TOP) && (flags & WSP_BOT)) { EMSG(_("E442: Can't split topleft and botright at the same time")); @@ -883,10 +883,11 @@ int win_split(int size, int flags) /* When creating the help window make a snapshot of the window layout. * Otherwise clear the snapshot, it's now invalid. */ - if (flags & WSP_HELP) + if (flags & WSP_HELP) { make_snapshot(SNAP_HELP_IDX); - else + } else { clear_snapshot(curtab, SNAP_HELP_IDX); + } return win_split_ins(size, flags, NULL, 0); } @@ -981,8 +982,9 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir) EMSG(_(e_noroom)); return FAIL; } - if (new_size == 0) + if (new_size == 0) { new_size = oldwin->w_width / 2; + } if (new_size > available - minwidth - 1) { new_size = available - minwidth - 1; } @@ -1065,8 +1067,9 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir) oldwin->w_status_height = STATUS_HEIGHT; oldwin_height -= STATUS_HEIGHT; } - if (new_size == 0) + if (new_size == 0) { new_size = oldwin_height / 2; + } if (new_size > available - minheight - STATUS_HEIGHT) { new_size = available - minheight - STATUS_HEIGHT; @@ -1090,10 +1093,11 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir) did_set_fraction = true; win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT, - oldwin); + oldwin); oldwin_height = oldwin->w_height; - if (need_status) + if (need_status) { oldwin_height -= STATUS_HEIGHT; + } } /* Only make all windows the same height if one of them (except oldwin) @@ -1138,12 +1142,13 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir) } if (new_wp == NULL) { - if (wp == NULL) + if (wp == NULL) { return FAIL; + } new_frame(wp); - /* make the contents of the new window the same as the current one */ + // make the contents of the new window the same as the current one win_init(wp, curwin, flags); } else if (wp->w_floating) { new_frame(wp); @@ -1159,25 +1164,29 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir) if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0) || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0)) { curfrp = topframe->fr_child; - if (flags & WSP_BOT) - while (curfrp->fr_next != NULL) + if (flags & WSP_BOT) { + while (curfrp->fr_next != NULL) { curfrp = curfrp->fr_next; - } else + } + } + } else { curfrp = topframe; + } before = (flags & WSP_TOP); } else { curfrp = oldwin->w_frame; - if (flags & WSP_BELOW) + if (flags & WSP_BELOW) { before = FALSE; - else if (flags & WSP_ABOVE) + } else if (flags & WSP_ABOVE) { before = TRUE; - else if (flags & WSP_VERT) + } else if (flags & WSP_VERT) { before = !p_spr; - else + } else { before = !p_sb; + } } if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout) { - /* Need to create a new frame in the tree to make a branch. */ + // Need to create a new frame in the tree to make a branch. frp = xcalloc(1, sizeof(frame_T)); *frp = *curfrp; curfrp->fr_layout = layout; @@ -1196,17 +1205,19 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir) } } - if (new_wp == NULL) + if (new_wp == NULL) { frp = wp->w_frame; - else + } else { frp = new_wp->w_frame; + } frp->fr_parent = curfrp->fr_parent; - /* Insert the new frame at the right place in the frame list. */ - if (before) + // Insert the new frame at the right place in the frame list. + if (before) { frame_insert(curfrp, frp); - else + } else { frame_append(curfrp, frp); + } /* Set w_fraction now so that the cursor keeps the same relative * vertical position. */ @@ -1223,12 +1234,12 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir) oldwin->w_status_height = need_status; } if (flags & (WSP_TOP | WSP_BOT)) { - /* set height and row of new window to full height */ + // set height and row of new window to full height wp->w_winrow = tabline_height(); win_new_height(wp, curfrp->fr_height - (p_ls > 0)); wp->w_status_height = (p_ls > 0); } else { - /* height and row of new window is same as current window */ + // height and row of new window is same as current window wp->w_winrow = oldwin->w_winrow; win_new_height(wp, oldwin->w_height); wp->w_status_height = oldwin->w_status_height; @@ -1238,16 +1249,17 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir) /* "new_size" of the current window goes to the new window, use * one column for the vertical separator */ win_new_width(wp, new_size); - if (before) + if (before) { wp->w_vsep_width = 1; - else { + } else { wp->w_vsep_width = oldwin->w_vsep_width; oldwin->w_vsep_width = 1; } if (flags & (WSP_TOP | WSP_BOT)) { - if (flags & WSP_BOT) + if (flags & WSP_BOT) { frame_add_vsep(curfrp); - /* Set width of neighbor frame */ + } + // Set width of neighbor frame frame_new_width(curfrp, curfrp->fr_width - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP, false); @@ -1257,12 +1269,13 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir) if (before) { // new window left of current one wp->w_wincol = oldwin->w_wincol; oldwin->w_wincol += new_size + 1; - } else /* new window right of current one */ + } else { // new window right of current one wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1; + } frame_fix_width(oldwin); frame_fix_width(wp); } else { - /* width and column of new window is same as current window */ + // width and column of new window is same as current window if (flags & (WSP_TOP | WSP_BOT)) { wp->w_wincol = 0; win_new_width(wp, Columns); @@ -1298,14 +1311,16 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir) oldwin->w_status_height = STATUS_HEIGHT; } } - if (flags & WSP_BOT) + if (flags & WSP_BOT) { frame_add_statusline(curfrp); + } frame_fix_height(wp); frame_fix_height(oldwin); } - if (flags & (WSP_TOP | WSP_BOT)) + if (flags & (WSP_TOP | WSP_BOT)) { (void)win_comp_pos(); + } // Both windows need redrawing. Update all status lines, in case they // show something related to the window count or position. @@ -1316,32 +1331,34 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir) if (need_status) { msg_row = Rows - 1; msg_col = sc_col; - msg_clr_eos_force(); /* Old command/ruler may still be there */ + msg_clr_eos_force(); // Old command/ruler may still be there comp_col(); msg_row = Rows - 1; - msg_col = 0; /* put position back at start of line */ + msg_col = 0; // put position back at start of line } /* * equalize the window sizes. */ - if (do_equal || dir != 0) + if (do_equal || dir != 0) { win_equal(wp, true, - (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h') - : dir == 'h' ? 'b' : - 'v'); + (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h') + : dir == 'h' ? 'b' : + 'v'); + } /* Don't change the window height/width to 'winheight' / 'winwidth' if a * size was given. */ if (flags & WSP_VERT) { i = p_wiw; - if (size != 0) + if (size != 0) { p_wiw = size; - + } } else { i = p_wh; - if (size != 0) + if (size != 0) { p_wh = size; + } } // Keep same changelist position in new window. @@ -1397,7 +1414,7 @@ static void win_init(win_T *newp, win_T *oldp, int flags) newp->w_prev_fraction_row = oldp->w_prev_fraction_row; copy_jumplist(oldp, newp); if (flags & WSP_NEWLOC) { - /* Don't copy the location list. */ + // Don't copy the location list. newp->w_llist = NULL; newp->w_llist_ref = NULL; } else { @@ -1406,7 +1423,7 @@ static void win_init(win_T *newp, win_T *oldp, int flags) newp->w_localdir = (oldp->w_localdir == NULL) ? NULL : vim_strsave(oldp->w_localdir); - /* copy tagstack and folds */ + // copy tagstack and folds for (i = 0; i < oldp->w_tagstacklen; i++) { taggy_T *tag = &newp->w_tagstack[i]; *tag = oldp->w_tagstack[i]; @@ -1432,12 +1449,12 @@ static void win_init(win_T *newp, win_T *oldp, int flags) */ static void win_init_some(win_T *newp, win_T *oldp) { - /* Use the same argument list. */ + // Use the same argument list. newp->w_alist = oldp->w_alist; ++newp->w_alist->al_refcount; newp->w_arg_idx = oldp->w_arg_idx; - /* copy options from existing window */ + // copy options from existing window win_copy_options(oldp, newp); } @@ -1543,10 +1560,12 @@ int make_windows(int count, bool vertical) - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT); } - if (maxcount < 2) + if (maxcount < 2) { maxcount = 2; - if (count > maxcount) + } + if (count > maxcount) { count = maxcount; + } /* * add status line now, otherwise first window will be too big @@ -1561,22 +1580,25 @@ int make_windows(int count, bool vertical) */ block_autocmds(); - /* todo is number of windows left to create */ - for (todo = count - 1; todo > 0; --todo) + // todo is number of windows left to create + for (todo = count - 1; todo > 0; --todo) { if (vertical) { if (win_split(curwin->w_width - (curwin->w_width - todo) - / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL) + / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL) { break; + } } else { if (win_split(curwin->w_height - (curwin->w_height - todo * STATUS_HEIGHT) / (todo + 1) - - STATUS_HEIGHT, WSP_ABOVE) == FAIL) + - STATUS_HEIGHT, WSP_ABOVE) == FAIL) { break; + } } + } unblock_autocmds(); - /* return actual number of windows */ + // return actual number of windows return count - todo; } @@ -1608,17 +1630,20 @@ static void win_exchange(long Prenum) */ if (Prenum) { frp = curwin->w_frame->fr_parent->fr_child; - while (frp != NULL && --Prenum > 0) + while (frp != NULL && --Prenum > 0) { frp = frp->fr_next; - } else if (curwin->w_frame->fr_next != NULL) /* Swap with next */ + } + } else if (curwin->w_frame->fr_next != NULL) { // Swap with next frp = curwin->w_frame->fr_next; - else /* Swap last window in row/col with previous */ + } else { // Swap last window in row/col with previous frp = curwin->w_frame->fr_prev; + } /* We can only exchange a window with another window, not with a frame * containing windows. */ - if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin) + if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin) { return; + } wp = frp->fr_win; /* @@ -1641,10 +1666,11 @@ static void win_exchange(long Prenum) win_remove(wp, NULL); frame_remove(wp->w_frame); win_append(wp2, wp); - if (frp2 == NULL) + if (frp2 == NULL) { frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame); - else + } else { frame_append(frp2, wp->w_frame); + } } temp = curwin->w_status_height; curwin->w_status_height = wp->w_status_height; @@ -1669,7 +1695,7 @@ static void win_exchange(long Prenum) frame_fix_width(wp); } - (void)win_comp_pos(); /* recompute window positions */ + (void)win_comp_pos(); // recompute window positions win_enter(wp, true); redraw_later(curwin, NOT_VALID); @@ -1705,8 +1731,8 @@ static void win_rotate(bool upwards, int count) } while (count--) { - if (upwards) { /* first window becomes last window */ - /* remove first window/frame from the list */ + if (upwards) { // first window becomes last window + // remove first window/frame from the list frp = curwin->w_frame->fr_parent->fr_child; assert(frp != NULL); wp1 = frp->fr_win; @@ -1714,30 +1740,32 @@ static void win_rotate(bool upwards, int count) frame_remove(frp); assert(frp->fr_parent->fr_child); - /* find last frame and append removed window/frame after it */ - for (; frp->fr_next != NULL; frp = frp->fr_next) + // find last frame and append removed window/frame after it + for (; frp->fr_next != NULL; frp = frp->fr_next) { ; + } win_append(frp->fr_win, wp1); frame_append(frp, wp1->w_frame); - wp2 = frp->fr_win; /* previously last window */ - } else { /* last window becomes first window */ - /* find last window/frame in the list and remove it */ + wp2 = frp->fr_win; // previously last window + } else { // last window becomes first window + // find last window/frame in the list and remove it for (frp = curwin->w_frame; frp->fr_next != NULL; - frp = frp->fr_next) + frp = frp->fr_next) { ; + } wp1 = frp->fr_win; - wp2 = wp1->w_prev; /* will become last window */ + wp2 = wp1->w_prev; // will become last window win_remove(wp1, NULL); frame_remove(frp); assert(frp->fr_parent->fr_child); - /* append the removed window/frame before the first in the list */ + // append the removed window/frame before the first in the list win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1); frame_insert(frp->fr_parent->fr_child, frp); } - /* exchange status height and vsep width of old and new last window */ + // exchange status height and vsep width of old and new last window n = wp2->w_status_height; wp2->w_status_height = wp1->w_status_height; wp1->w_status_height = n; @@ -1749,7 +1777,7 @@ static void win_rotate(bool upwards, int count) frame_fix_width(wp1); frame_fix_width(wp2); - /* recompute w_winrow and w_wincol for all windows */ + // recompute w_winrow and w_wincol for all windows (void)win_comp_pos(); } @@ -1789,14 +1817,14 @@ static void win_totop(int size, int flags) last_status(false); // may need to remove last status line (void)win_comp_pos(); // recompute window positions - /* Split a window on the desired side and put the window there. */ + // Split a window on the desired side and put the window there. (void)win_split_ins(size, flags, curwin, dir); if (!(flags & WSP_VERT)) { win_setheight(height); - if (p_ea) + if (p_ea) { win_equal(curwin, true, 'v'); + } } - } /* @@ -1807,11 +1835,12 @@ void win_move_after(win_T *win1, win_T *win2) { int height; - /* check if the arguments are reasonable */ - if (win1 == win2) + // check if the arguments are reasonable + if (win1 == win2) { return; + } - /* check if there is something to do */ + // check if there is something to do if (win2->w_next != win1) { /* may need move the status line/vertical separator of the last window * */ @@ -1854,41 +1883,37 @@ void win_move_after(win_T *win1, win_T *win2) win2->w_pos_changed = true; } -/* - * Make all windows the same height. - * 'next_curwin' will soon be the current window, make sure it has enough - * rows. - */ -void win_equal( - win_T *next_curwin, // pointer to current window to be or NULL - bool current, // do only frame with current window - int dir // 'v' for vertically, 'h' for horizontally, - // 'b' for both, 0 for using p_ead -) +/// Make all windows the same height. +///'next_curwin' will soon be the current window, make sure it has enough rows. +/// +/// @param next_curwin pointer to current window to be or NULL +/// @param current do only frame with current window +/// @param dir 'v' for vertically, 'h' for horizontally, 'b' for both, 0 for using p_ead +void win_equal(win_T *next_curwin, bool current, int dir) { - if (dir == 0) + if (dir == 0) { dir = *p_ead; + } win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current, topframe, dir, 0, tabline_height(), Columns, topframe->fr_height); } -/* - * Set a frame to a new position and height, spreading the available room - * equally over contained frames. - * The window "next_curwin" (if not NULL) should at least get the size from - * 'winheight' and 'winwidth' if possible. - */ -static void win_equal_rec( - win_T *next_curwin, /* pointer to current window to be or NULL */ - bool current, /* do only frame with current window */ - frame_T *topfr, /* frame to set size off */ - int dir, /* 'v', 'h' or 'b', see win_equal() */ - int col, /* horizontal position for frame */ - int row, /* vertical position for frame */ - int width, /* new width of frame */ - int height /* new height of frame */ -) +/// Set a frame to a new position and height, spreading the available room +/// equally over contained frames. +/// The window "next_curwin" (if not NULL) should at least get the size from +/// 'winheight' and 'winwidth' if possible. +/// +/// @param next_curwin pointer to current window to be or NULL +/// @param current do only frame with current window +/// @param topfr frame to set size off +/// @param dir 'v', 'h' or 'b', see win_equal() +/// @param col horizontal position for frame +/// @param row vertical position for frame +/// @param width new width of frame +/// @param height new height of frame +static void win_equal_rec(win_T *next_curwin, bool current, frame_T *topfr, int dir, int col, + int row, int width, int height) { int n, m; int extra_sep = 0; @@ -1904,8 +1929,8 @@ static void win_equal_rec( /* Set the width/height of this frame. * Redraw when size or position changes */ if (topfr->fr_height != height || topfr->fr_win->w_winrow != row - || topfr->fr_width != width || topfr->fr_win->w_wincol != col - ) { + || topfr->fr_width != width || + topfr->fr_win->w_wincol != col) { topfr->fr_win->w_winrow = row; frame_new_height(topfr, height, false, false); topfr->fr_win->w_wincol = col; @@ -1916,15 +1941,16 @@ static void win_equal_rec( topfr->fr_width = width; topfr->fr_height = height; - if (dir != 'v') { /* equalize frame widths */ + if (dir != 'v') { // equalize frame widths /* Compute the maximum number of windows horizontally in this * frame. */ n = frame_minwidth(topfr, NOWIN); - /* add one for the rightmost window, it doesn't have a separator */ - if (col + width == Columns) + // add one for the rightmost window, it doesn't have a separator + if (col + width == Columns) { extra_sep = 1; - else + } else { extra_sep = 0; + } totwincount = (n + extra_sep) / (p_wmw + 1); has_next_curwin = frame_has_win(topfr, next_curwin); @@ -1952,12 +1978,14 @@ static void win_equal_rec( if (frame_has_win(fr, next_curwin)) { room += p_wiw - p_wmw; next_curwin_size = 0; - if (new_size < p_wiw) + if (new_size < p_wiw) { new_size = p_wiw; - } else - /* These windows don't use up room. */ + } + } else { + // These windows don't use up room. totwincount -= (n + (fr->fr_next == NULL ? extra_sep : 0)) / (p_wmw + 1); + } room -= new_size - n; if (room < 0) { new_size += room; @@ -1966,38 +1994,40 @@ static void win_equal_rec( fr->fr_newwidth = new_size; } if (next_curwin_size == -1) { - if (!has_next_curwin) + if (!has_next_curwin) { next_curwin_size = 0; - else if (totwincount > 1 - && (room + (totwincount - 2)) - / (totwincount - 1) > p_wiw) { + } else if (totwincount > 1 + && (room + (totwincount - 2)) + / (totwincount - 1) > p_wiw) { /* Can make all windows wider than 'winwidth', spread * the room equally. */ next_curwin_size = (room + p_wiw + (totwincount - 1) * p_wmw + (totwincount - 1)) / totwincount; room -= next_curwin_size - p_wiw; - } else + } else { next_curwin_size = p_wiw; + } } } - if (has_next_curwin) - --totwincount; /* don't count curwin */ + if (has_next_curwin) { + --totwincount; // don't count curwin + } } FOR_ALL_FRAMES(fr, topfr->fr_child) { wincount = 1; - if (fr->fr_next == NULL) - /* last frame gets all that remains (avoid roundoff error) */ + if (fr->fr_next == NULL) { + // last frame gets all that remains (avoid roundoff error) new_size = width; - else if (dir == 'v') + } else if (dir == 'v') { new_size = fr->fr_width; - else if (frame_fixed_width(fr)) { + } else if (frame_fixed_width(fr)) { new_size = fr->fr_newwidth; - wincount = 0; /* doesn't count as a sizeable window */ + wincount = 0; // doesn't count as a sizeable window } else { - /* Compute the maximum number of windows horiz. in "fr". */ + // Compute the maximum number of windows horiz. in "fr". n = frame_minwidth(fr, NOWIN); wincount = (n + (fr->fr_next == NULL ? extra_sep : 0)) / (p_wmw + 1); @@ -2019,8 +2049,9 @@ static void win_equal_rec( next_curwin_size -= p_wiw - (m - n); new_size += next_curwin_size; room -= new_size - next_curwin_size; - } else + } else { room -= new_size; + } new_size += n; } @@ -2028,25 +2059,27 @@ static void win_equal_rec( * window, unless equalizing all frames. */ if (!current || dir != 'v' || topfr->fr_parent != NULL || (new_size != fr->fr_width) - || frame_has_win(fr, next_curwin)) + || frame_has_win(fr, next_curwin)) { win_equal_rec(next_curwin, current, fr, dir, col, row, - new_size, height); + new_size, height); + } col += new_size; width -= new_size; totwincount -= wincount; } - } else { /* topfr->fr_layout == FR_COL */ + } else { // topfr->fr_layout == FR_COL topfr->fr_width = width; topfr->fr_height = height; - if (dir != 'h') { /* equalize frame heights */ - /* Compute maximum number of windows vertically in this frame. */ + if (dir != 'h') { // equalize frame heights + // Compute maximum number of windows vertically in this frame. n = frame_minheight(topfr, NOWIN); - /* add one for the bottom window if it doesn't have a statusline */ - if (row + height == cmdline_row && p_ls == 0) + // add one for the bottom window if it doesn't have a statusline + if (row + height == cmdline_row && p_ls == 0) { extra_sep = 1; - else + } else { extra_sep = 0; + } totwincount = (n + extra_sep) / (p_wmh + 1); has_next_curwin = frame_has_win(topfr, next_curwin); @@ -2076,12 +2109,14 @@ static void win_equal_rec( if (frame_has_win(fr, next_curwin)) { room += p_wh - p_wmh; next_curwin_size = 0; - if (new_size < p_wh) + if (new_size < p_wh) { new_size = p_wh; - } else - /* These windows don't use up room. */ + } + } else { + // These windows don't use up room. totwincount -= (n + (fr->fr_next == NULL ? extra_sep : 0)) / (p_wmh + 1); + } room -= new_size - n; if (room < 0) { new_size += room; @@ -2090,38 +2125,40 @@ static void win_equal_rec( fr->fr_newheight = new_size; } if (next_curwin_size == -1) { - if (!has_next_curwin) + if (!has_next_curwin) { next_curwin_size = 0; - else if (totwincount > 1 - && (room + (totwincount - 2)) - / (totwincount - 1) > p_wh) { + } else if (totwincount > 1 + && (room + (totwincount - 2)) + / (totwincount - 1) > p_wh) { /* can make all windows higher than 'winheight', * spread the room equally. */ next_curwin_size = (room + p_wh + (totwincount - 1) * p_wmh + (totwincount - 1)) / totwincount; room -= next_curwin_size - p_wh; - } else + } else { next_curwin_size = p_wh; + } } } - if (has_next_curwin) - --totwincount; /* don't count curwin */ + if (has_next_curwin) { + --totwincount; // don't count curwin + } } FOR_ALL_FRAMES(fr, topfr->fr_child) { wincount = 1; - if (fr->fr_next == NULL) - /* last frame gets all that remains (avoid roundoff error) */ + if (fr->fr_next == NULL) { + // last frame gets all that remains (avoid roundoff error) new_size = height; - else if (dir == 'h') + } else if (dir == 'h') { new_size = fr->fr_height; - else if (frame_fixed_height(fr)) { + } else if (frame_fixed_height(fr)) { new_size = fr->fr_newheight; - wincount = 0; /* doesn't count as a sizeable window */ + wincount = 0; // doesn't count as a sizeable window } else { - /* Compute the maximum number of windows vert. in "fr". */ + // Compute the maximum number of windows vert. in "fr". n = frame_minheight(fr, NOWIN); wincount = (n + (fr->fr_next == NULL ? extra_sep : 0)) / (p_wmh + 1); @@ -2143,17 +2180,19 @@ static void win_equal_rec( next_curwin_size -= p_wh - (m - n); new_size += next_curwin_size; room -= new_size - next_curwin_size; - } else + } else { room -= new_size; + } new_size += n; } /* Skip frame that is full width when splitting or closing a * window, unless equalizing all frames. */ if (!current || dir != 'h' || topfr->fr_parent != NULL || (new_size != fr->fr_height) - || frame_has_win(fr, next_curwin)) + || frame_has_win(fr, next_curwin)) { win_equal_rec(next_curwin, current, fr, dir, col, row, - width, new_size); + width, new_size); + } row += new_size; height -= new_size; totwincount -= wincount; @@ -2179,13 +2218,14 @@ void close_windows(buf_T *buf, int keep_curwin) break; } - /* Start all over, autocommands may change the window layout. */ + // Start all over, autocommands may change the window layout. wp = firstwin; - } else + } else { wp = wp->w_next; + } } - /* Also check windows in other tab pages. */ + // Also check windows in other tab pages. for (tp = first_tabpage; tp != NULL; tp = nexttp) { nexttp = tp->tp_next; if (tp != curtab) { @@ -2259,8 +2299,7 @@ bool last_nonfloat(win_T *wp) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT /// last window in the tabpage /// /// @return true when the window was closed already. -static bool close_last_window_tabpage(win_T *win, bool free_buf, - tabpage_T *prev_curtab) +static bool close_last_window_tabpage(win_T *win, bool free_buf, tabpage_T *prev_curtab) FUNC_ATTR_NONNULL_ARG(1) { if (!ONE_WINDOW) { @@ -2295,8 +2334,9 @@ static bool close_last_window_tabpage(win_T *win, bool free_buf, int h = tabline_height(); win_close_othertab(win, free_buf, prev_curtab); - if (h != tabline_height()) + if (h != tabline_height()) { shell_new_rows(); + } } // Since goto_tabpage_tp above did not trigger *Enter autocommands, do @@ -2352,8 +2392,9 @@ int win_close(win_T *win, bool free_buf) /* When closing the last window in a tab page first go to another tab page * and then close the window and the tab page to avoid that curwin and * curtab are invalid while we are freeing memory. */ - if (close_last_window_tabpage(win, free_buf, prev_curtab)) + if (close_last_window_tabpage(win, free_buf, prev_curtab)) { return FAIL; + } /* When closing the help window, try restoring a snapshot after closing * the window. Otherwise clear the snapshot, it's now invalid. */ @@ -2386,11 +2427,13 @@ int win_close(win_T *win, bool free_buf) other_buffer = true; win->w_closing = true; apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf); - if (!win_valid(win)) + if (!win_valid(win)) { return FAIL; + } win->w_closing = false; - if (last_window()) + if (last_window()) { return FAIL; + } } win->w_closing = true; apply_autocmds(EVENT_WINLEAVE, NULL, NULL, false, curbuf); @@ -2398,11 +2441,13 @@ int win_close(win_T *win, bool free_buf) return FAIL; } win->w_closing = false; - if (last_window()) + if (last_window()) { return FAIL; - /* autocmds may abort script processing */ - if (aborting()) + } + // autocmds may abort script processing + if (aborting()) { return FAIL; + } } bool was_floating = win->w_floating; @@ -2509,12 +2554,14 @@ int win_close(win_T *win, bool free_buf) * finding another window to go to. */ for (;; ) { - if (wp->w_next == NULL) + if (wp->w_next == NULL) { wp = firstwin; - else + } else { wp = wp->w_next; - if (wp == curwin) + } + if (wp == curwin) { break; + } if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer)) { curwin = wp; break; @@ -2555,8 +2602,9 @@ int win_close(win_T *win, bool free_buf) /* After closing the help window, try restoring the window layout from * before it was opened. */ - if (help_window) + if (help_window) { restore_snapshot(SNAP_HELP_IDX, close_curwin); + } // If the window had 'diff' set and now there is only one window left in // the tab page with 'diff' set, and "closeoff" is in 'diffopt', then @@ -2627,26 +2675,28 @@ void win_close_othertab(win_T *win, int free_buf, tabpage_T *tp) /* Careful: Autocommands may have closed the tab page or made it the * current tab page. */ - for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next) + for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next) { ; - if (ptp == NULL || tp == curtab) + } + if (ptp == NULL || tp == curtab) { return; + } - /* Autocommands may have closed the window already. */ + // Autocommands may have closed the window already. { bool found_window = false; FOR_ALL_WINDOWS_IN_TAB(wp, tp) { if (wp == win) { - found_window = true; - break; + found_window = true; + break; } } if (!found_window) { - return; + return; } } - /* When closing the last window in a tab page remove the tab page. */ + // When closing the last window in a tab page remove the tab page. if (tp->tp_firstwin == tp->tp_lastwin) { char_u prev_idx[NUMBUFLEN]; if (has_event(EVENT_TABCLOSED)) { @@ -2673,20 +2723,21 @@ void win_close_othertab(win_T *win, int free_buf, tabpage_T *tp) } } - /* Free the memory used for the window. */ + // Free the memory used for the window. win_free_mem(win, &dir, tp); - if (free_tp) + if (free_tp) { free_tabpage(tp); + } } -// Free the memory used for a window. -// Returns a pointer to the window that got the freed up space. -static win_T *win_free_mem( - win_T *win, - int *dirp, // set to 'v' or 'h' for direction if 'ea' - tabpage_T *tp // tab page "win" is in, NULL for current -) +/// Free the memory used for a window. +/// +/// @param dirp set to 'v' or 'h' for direction if 'ea' +/// @param tp tab page "win" is in, NULL for current +/// +/// @return a pointer to the window that got the freed up space. +static win_T *win_free_mem(win_T *win, int *dirp, tabpage_T *tp) { frame_T *frp; win_T *wp; @@ -2724,8 +2775,9 @@ void win_free_all(void) { int dummy; - while (first_tabpage->tp_next != NULL) + while (first_tabpage->tp_next != NULL) { tabpage_close(TRUE); + } while (lastwin != NULL && lastwin->w_floating) { win_T *wp = lastwin; @@ -2741,8 +2793,9 @@ void win_free_all(void) aucmd_win = NULL; } - while (firstwin != NULL) + while (firstwin != NULL) { (void)win_free_mem(firstwin, &dummy, NULL); + } // No window should be used after this. Set curwin to NULL to crash // instead of using freed memory. @@ -2751,16 +2804,13 @@ void win_free_all(void) #endif -/* - * Remove a window and its frame from the tree of frames. - * Returns a pointer to the window that got the freed up space. - */ -win_T * -winframe_remove ( - win_T *win, - int *dirp, /* set to 'v' or 'h' for direction if 'ea' */ - tabpage_T *tp /* tab page "win" is in, NULL for current */ -) +/// Remove a window and its frame from the tree of frames. +/// +/// @param dirp set to 'v' or 'h' for direction if 'ea' +/// @param tp tab page "win" is in, NULL for current +/// +/// @return a pointer to the window that got the freed up space. +win_T *winframe_remove(win_T *win, int *dirp, tabpage_T *tp) { frame_T *frp, *frp2, *frp3; frame_T *frp_close = win->w_frame; @@ -2769,8 +2819,9 @@ winframe_remove ( /* * If there is only one window there is nothing to remove. */ - if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin) + if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin) { return NULL; + } /* * Remove the window from its frame. @@ -2778,7 +2829,7 @@ winframe_remove ( frp2 = win_altframe(win, tp); wp = frame2win(frp2); - /* Remove this frame from the list of frames. */ + // Remove this frame from the list of frames. frame_remove(frp_close); if (frp_close->fr_parent->fr_layout == FR_COL) { @@ -2859,8 +2910,9 @@ winframe_remove ( frp->fr_parent = frp2->fr_parent; } frp2->fr_parent->fr_win = frp2->fr_win; - if (frp2->fr_win != NULL) + if (frp2->fr_win != NULL) { frp2->fr_win->w_frame = frp2->fr_parent; + } frp = frp2->fr_parent; if (topframe->fr_child == frp2) { topframe->fr_child = frp; @@ -2871,18 +2923,21 @@ winframe_remove ( if (frp2 != NULL && frp2->fr_layout == frp->fr_layout) { /* The frame above the parent has the same layout, have to merge * the frames into this list. */ - if (frp2->fr_child == frp) + if (frp2->fr_child == frp) { frp2->fr_child = frp->fr_child; + } assert(frp->fr_child); frp->fr_child->fr_prev = frp->fr_prev; - if (frp->fr_prev != NULL) + if (frp->fr_prev != NULL) { frp->fr_prev->fr_next = frp->fr_child; + } for (frp3 = frp->fr_child;; frp3 = frp3->fr_next) { frp3->fr_parent = frp2; if (frp3->fr_next == NULL) { frp3->fr_next = frp->fr_next; - if (frp->fr_next != NULL) + if (frp->fr_next != NULL) { frp->fr_next->fr_prev = frp3; + } break; } } @@ -2896,19 +2951,17 @@ winframe_remove ( return wp; } -// Return a pointer to the frame that will receive the empty screen space that -// is left over after "win" is closed. -// -// If 'splitbelow' or 'splitright' is set, the space goes above or to the left -// by default. Otherwise, the free space goes below or to the right. The -// result is that opening a window and then immediately closing it will -// preserve the initial window layout. The 'wfh' and 'wfw' settings are -// respected when possible. -static frame_T * -win_altframe ( - win_T *win, - tabpage_T *tp /* tab page "win" is in, NULL for current */ -) +/// If 'splitbelow' or 'splitright' is set, the space goes above or to the left +/// by default. Otherwise, the free space goes below or to the right. The +/// result is that opening a window and then immediately closing it will +/// preserve the initial window layout. The 'wfh' and 'wfw' settings are +/// respected when possible. +/// +/// @param tp tab page "win" is in, NULL for current +/// +/// @return a pointer to the frame that will receive the empty screen space that +/// is left over after "win" is closed. +static frame_T *win_altframe(win_T *win, tabpage_T *tp) { frame_T *frp; @@ -2954,13 +3007,15 @@ static tabpage_T *alt_tabpage(void) { tabpage_T *tp; - /* Use the next tab page if possible. */ - if (curtab->tp_next != NULL) + // Use the next tab page if possible. + if (curtab->tp_next != NULL) { return curtab->tp_next; + } - /* Find the last but one tab page. */ - for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next) + // Find the last but one tab page. + for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next) { ; + } return tp; } @@ -2969,8 +3024,9 @@ static tabpage_T *alt_tabpage(void) */ static win_T *frame2win(frame_T *frp) { - while (frp->fr_win == NULL) + while (frp->fr_win == NULL) { frp = frp->fr_child; + } return frp->fr_win; } @@ -2999,8 +3055,7 @@ static bool frame_has_win(const frame_T *frp, const win_T *wp) /// @param topfirst resize topmost contained frame first. /// @param wfh obey 'winfixheight' when there is a choice; /// may cause the height not to be set. -static void frame_new_height(frame_T *topfrp, int height, bool topfirst, - bool wfh) +static void frame_new_height(frame_T *topfrp, int height, bool topfirst, bool wfh) FUNC_ATTR_NONNULL_ALL { frame_T *frp; @@ -3017,37 +3072,42 @@ static void frame_new_height(frame_T *topfrp, int height, bool topfirst, FOR_ALL_FRAMES(frp, topfrp->fr_child) { frame_new_height(frp, height, topfirst, wfh); if (frp->fr_height > height) { - /* Could not fit the windows, make the whole row higher. */ + // Could not fit the windows, make the whole row higher. height = frp->fr_height; break; } } } while (frp != NULL); - } else { /* fr_layout == FR_COL */ + } else { // fr_layout == FR_COL /* Complicated case: Resize a column of frames. Resize the bottom * frame first, frames above that when needed. */ frp = topfrp->fr_child; - if (wfh) - /* Advance past frames with one window with 'wfh' set. */ + if (wfh) { + // Advance past frames with one window with 'wfh' set. while (frame_fixed_height(frp)) { frp = frp->fr_next; - if (frp == NULL) - return; /* no frame without 'wfh', give up */ + if (frp == NULL) { + return; // no frame without 'wfh', give up + } } + } if (!topfirst) { - /* Find the bottom frame of this column */ - while (frp->fr_next != NULL) + // Find the bottom frame of this column + while (frp->fr_next != NULL) { frp = frp->fr_next; - if (wfh) - /* Advance back for frames with one window with 'wfh' set. */ - while (frame_fixed_height(frp)) + } + if (wfh) { + // Advance back for frames with one window with 'wfh' set. + while (frame_fixed_height(frp)) { frp = frp->fr_prev; + } + } } extra_lines = height - topfrp->fr_height; if (extra_lines < 0) { - /* reduce height of contained frames, bottom or top frame first */ + // reduce height of contained frames, bottom or top frame first while (frp != NULL) { h = frame_minheight(frp, NULL); if (frp->fr_height + extra_lines < h) { @@ -3055,7 +3115,7 @@ static void frame_new_height(frame_T *topfrp, int height, bool topfirst, frame_new_height(frp, h, topfirst, wfh); } else { frame_new_height(frp, frp->fr_height + extra_lines, - topfirst, wfh); + topfirst, wfh); break; } if (topfirst) { @@ -3067,12 +3127,13 @@ static void frame_new_height(frame_T *topfrp, int height, bool topfirst, frp = frp->fr_prev; while (wfh && frp != NULL && frame_fixed_height(frp)); } - /* Increase "height" if we could not reduce enough frames. */ - if (frp == NULL) + // Increase "height" if we could not reduce enough frames. + if (frp == NULL) { height -= extra_lines; + } } } else if (extra_lines > 0) { - /* increase height of bottom or top frame */ + // increase height of bottom or top frame frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh); } } @@ -3158,8 +3219,9 @@ static void frame_add_statusline(frame_T *frp) if (frp->fr_layout == FR_LEAF) { wp = frp->fr_win; if (wp->w_status_height == 0) { - if (wp->w_height > 0) /* don't make it negative */ + if (wp->w_height > 0) { // don't make it negative --wp->w_height; + } wp->w_status_height = STATUS_HEIGHT; } } else if (frp->fr_layout == FR_ROW) { @@ -3182,8 +3244,7 @@ static void frame_add_statusline(frame_T *frp) /// @param leftfirst resize leftmost contained frame first. /// @param wfw obey 'winfixwidth' when there is a choice; /// may cause the width not to be set. -static void frame_new_width(frame_T *topfrp, int width, bool leftfirst, - bool wfw) +static void frame_new_width(frame_T *topfrp, int width, bool leftfirst, bool wfw) { frame_T *frp; int extra_cols; @@ -3191,14 +3252,17 @@ static void frame_new_width(frame_T *topfrp, int width, bool leftfirst, win_T *wp; if (topfrp->fr_layout == FR_LEAF) { - /* Simple case: just one window. */ + // Simple case: just one window. wp = topfrp->fr_win; - /* Find out if there are any windows right of this one. */ - for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent) - if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL) + // Find out if there are any windows right of this one. + for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent) { + if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL) { break; - if (frp->fr_parent == NULL) + } + } + if (frp->fr_parent == NULL) { wp->w_vsep_width = 0; + } win_new_width(wp, width - wp->w_vsep_width); } else if (topfrp->fr_layout == FR_COL) { do { @@ -3206,37 +3270,42 @@ static void frame_new_width(frame_T *topfrp, int width, bool leftfirst, FOR_ALL_FRAMES(frp, topfrp->fr_child) { frame_new_width(frp, width, leftfirst, wfw); if (frp->fr_width > width) { - /* Could not fit the windows, make whole column wider. */ + // Could not fit the windows, make whole column wider. width = frp->fr_width; break; } } } while (frp != NULL); - } else { /* fr_layout == FR_ROW */ + } else { // fr_layout == FR_ROW /* Complicated case: Resize a row of frames. Resize the rightmost * frame first, frames left of it when needed. */ frp = topfrp->fr_child; - if (wfw) - /* Advance past frames with one window with 'wfw' set. */ + if (wfw) { + // Advance past frames with one window with 'wfw' set. while (frame_fixed_width(frp)) { frp = frp->fr_next; - if (frp == NULL) - return; /* no frame without 'wfw', give up */ + if (frp == NULL) { + return; // no frame without 'wfw', give up + } } + } if (!leftfirst) { - /* Find the rightmost frame of this row */ - while (frp->fr_next != NULL) + // Find the rightmost frame of this row + while (frp->fr_next != NULL) { frp = frp->fr_next; - if (wfw) - /* Advance back for frames with one window with 'wfw' set. */ - while (frame_fixed_width(frp)) + } + if (wfw) { + // Advance back for frames with one window with 'wfw' set. + while (frame_fixed_width(frp)) { frp = frp->fr_prev; + } + } } extra_cols = width - topfrp->fr_width; if (extra_cols < 0) { - /* reduce frame width, rightmost frame first */ + // reduce frame width, rightmost frame first while (frp != NULL) { w = frame_minwidth(frp, NULL); if (frp->fr_width + extra_cols < w) { @@ -3244,7 +3313,7 @@ static void frame_new_width(frame_T *topfrp, int width, bool leftfirst, frame_new_width(frp, w, leftfirst, wfw); } else { frame_new_width(frp, frp->fr_width + extra_cols, - leftfirst, wfw); + leftfirst, wfw); break; } if (leftfirst) { @@ -3256,12 +3325,13 @@ static void frame_new_width(frame_T *topfrp, int width, bool leftfirst, frp = frp->fr_prev; while (wfw && frp != NULL && frame_fixed_width(frp)); } - /* Increase "width" if we could not reduce enough frames. */ - if (frp == NULL) + // Increase "width" if we could not reduce enough frames. + if (frp == NULL) { width -= extra_cols; + } } } else if (extra_cols > 0) { - /* increase width of rightmost frame */ + // increase width of rightmost frame frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw); } } @@ -3280,8 +3350,9 @@ static void frame_add_vsep(const frame_T *frp) if (frp->fr_layout == FR_LEAF) { wp = frp->fr_win; if (wp->w_vsep_width == 0) { - if (wp->w_width > 0) /* don't make it negative */ + if (wp->w_width > 0) { // don't make it negative --wp->w_width; + } wp->w_vsep_width = 1; } } else if (frp->fr_layout == FR_COL) { @@ -3293,8 +3364,9 @@ static void frame_add_vsep(const frame_T *frp) assert(frp->fr_layout == FR_ROW); // Only need to handle the last frame in the row. frp = frp->fr_child; - while (frp->fr_next != NULL) + while (frp->fr_next != NULL) { frp = frp->fr_next; + } frame_add_vsep(frp); } } @@ -3343,15 +3415,16 @@ static int frame_minheight(frame_T *topfrp, win_T *next_curwin) } } } else if (topfrp->fr_layout == FR_ROW) { - /* get the minimal height from each frame in this row */ + // get the minimal height from each frame in this row m = 0; FOR_ALL_FRAMES(frp, topfrp->fr_child) { n = frame_minheight(frp, next_curwin); - if (n > m) + if (n > m) { m = n; + } } } else { - /* Add up the minimal heights for all frames in this column. */ + // Add up the minimal heights for all frames in this column. m = 0; FOR_ALL_FRAMES(frp, topfrp->fr_child) { m += frame_minheight(frp, next_curwin); @@ -3361,41 +3434,39 @@ static int frame_minheight(frame_T *topfrp, win_T *next_curwin) return m; } -/* - * Compute the minimal width for frame "topfrp". - * When "next_curwin" isn't NULL, use p_wiw for this window. - * When "next_curwin" is NOWIN, don't use at least one column for the current - * window. - */ -static int -frame_minwidth ( - frame_T *topfrp, - win_T *next_curwin /* use p_wh and p_wiw for next_curwin */ -) +/// Compute the minimal width for frame "topfrp". +/// When "next_curwin" isn't NULL, use p_wiw for this window. +/// When "next_curwin" is NOWIN, don't use at least one column for the current +/// window. +/// +/// @param next_curwin use p_wh and p_wiw for next_curwin +static int frame_minwidth(frame_T *topfrp, win_T *next_curwin) { frame_T *frp; int m, n; if (topfrp->fr_win != NULL) { - if (topfrp->fr_win == next_curwin) + if (topfrp->fr_win == next_curwin) { m = p_wiw + topfrp->fr_win->w_vsep_width; - else { - /* window: minimal width of the window plus separator column */ + } else { + // window: minimal width of the window plus separator column m = p_wmw + topfrp->fr_win->w_vsep_width; - /* Current window is minimal one column wide */ - if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL) + // Current window is minimal one column wide + if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL) { ++m; + } } } else if (topfrp->fr_layout == FR_COL) { - /* get the minimal width from each frame in this column */ + // get the minimal width from each frame in this column m = 0; FOR_ALL_FRAMES(frp, topfrp->fr_child) { n = frame_minwidth(frp, next_curwin); - if (n > m) + if (n > m) { m = n; + } } } else { - /* Add up the minimal widths for all frames in this row. */ + // Add up the minimal widths for all frames in this row. m = 0; FOR_ALL_FRAMES(frp, topfrp->fr_child) { m += frame_minwidth(frp, next_curwin); @@ -3406,18 +3477,14 @@ frame_minwidth ( } -/* - * Try to close all windows except current one. - * Buffers in the other windows become hidden if 'hidden' is set, or '!' is - * used and the buffer was modified. - * - * Used by ":bdel" and ":only". - */ -void -close_others ( - int message, - int forceit /* always hide all other windows */ -) +/// Try to close all windows except current one. +/// Buffers in the other windows become hidden if 'hidden' is set, or '!' is +/// used and the buffer was modified. +/// +/// Used by ":bdel" and ":only". +/// +/// @param forceit always hide all other windows +void close_others(int message, int forceit) { win_T *wp; win_T *nextwp; @@ -3432,23 +3499,22 @@ close_others ( if (one_window() && !lastwin->w_floating) { if (message - && !autocmd_busy - ) { + && !autocmd_busy) { MSG(_(m_onlyone)); } return; } - /* Be very careful here: autocommands may change the window layout. */ + // Be very careful here: autocommands may change the window layout. for (wp = firstwin; win_valid(wp); wp = nextwp) { nextwp = wp->w_next; - if (wp == curwin) { /* don't close current window */ + if (wp == curwin) { // don't close current window continue; } - /* Check if it's allowed to abandon this window */ + // Check if it's allowed to abandon this window r = can_abandon(wp->w_buffer, forceit); - if (!win_valid(wp)) { /* autocommands messed wp up */ + if (!win_valid(wp)) { // autocommands messed wp up nextwp = firstwin; continue; } @@ -3460,14 +3526,16 @@ close_others ( continue; } } - if (bufIsChanged(wp->w_buffer)) + if (bufIsChanged(wp->w_buffer)) { continue; + } } win_close(wp, !buf_hide(wp->w_buffer) && !bufIsChanged(wp->w_buffer)); } - if (message && !ONE_WINDOW) + if (message && !ONE_WINDOW) { EMSG(_("E445: Other window contains changes")); + } } @@ -3487,7 +3555,7 @@ void win_init_empty(win_T *wp) wp->w_cursor.lnum = 1; wp->w_curswant = wp->w_cursor.col = 0; wp->w_cursor.coladd = 0; - wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */ + wp->w_pcmark.lnum = 1; // pcmark not cleared but set to line 1 wp->w_pcmark.col = 0; wp->w_prev_pcmark.lnum = 0; wp->w_prev_pcmark.col = 0; @@ -3505,8 +3573,9 @@ void win_init_empty(win_T *wp) */ int win_alloc_first(void) { - if (win_alloc_firstwin(NULL) == FAIL) + if (win_alloc_firstwin(NULL) == FAIL) { return FAIL; + } first_tabpage = alloc_tabpage(); first_tabpage->tp_topframe = topframe; @@ -3550,14 +3619,14 @@ static int win_alloc_firstwin(win_T *oldwin) } curwin->w_buffer = curbuf; curwin->w_s = &(curbuf->b_s); - curbuf->b_nwindows = 1; /* there is one window */ + curbuf->b_nwindows = 1; // there is one window curwin->w_alist = &global_alist; - curwin_init(); /* init current window */ + curwin_init(); // init current window } else { - /* First window in new tab page, initialize it from "oldwin". */ + // First window in new tab page, initialize it from "oldwin". win_init(curwin, oldwin, 0); - /* We don't want cursor- and scroll-binding in the first window. */ + // We don't want cursor- and scroll-binding in the first window. RESET_BINDING(curwin); } @@ -3621,9 +3690,10 @@ void free_tabpage(tabpage_T *tp) pmap_del(handle_T)(&tabpage_handles, tp->handle); diff_clear(tp); - for (idx = 0; idx < SNAP_COUNT; ++idx) + for (idx = 0; idx < SNAP_COUNT; ++idx) { clear_snapshot(tp, idx); - vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */ + } + vars_clear(&tp->tp_vars->dv_hashtab); // free all t: variables hash_init(&tp->tp_vars->dv_hashtab); unref_var_dict(tp->tp_vars); @@ -3676,8 +3746,9 @@ int win_new_tabpage(int after, char_u *filename) // Put new tab page before tab page "after". n = 2; for (tp = first_tabpage; tp->tp_next != NULL - && n < after; tp = tp->tp_next) + && n < after; tp = tp->tp_next) { ++n; + } } newtp->tp_next = tp->tp_next; tp->tp_next = newtp; @@ -3720,7 +3791,7 @@ int may_open_tabpage(void) int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab; if (n != 0) { - cmdmod.tab = 0; /* reset it to avoid doing it twice */ + cmdmod.tab = 0; // reset it to avoid doing it twice postponed_split_tab = 0; return win_new_tabpage(n, NULL); } @@ -3736,9 +3807,10 @@ int make_tabpages(int maxcount) int count = maxcount; int todo; - /* Limit to 'tabpagemax' tabs. */ - if (count > p_tpm) + // Limit to 'tabpagemax' tabs. + if (count > p_tpm) { count = p_tpm; + } /* * Don't execute autocommands while creating the tab pages. Must do that @@ -3754,7 +3826,7 @@ int make_tabpages(int maxcount) unblock_autocmds(); - /* return actual number of tab pages */ + // return actual number of tab pages return count - todo; } @@ -3818,8 +3890,9 @@ tabpage_T *find_tabpage(int n) tabpage_T *tp; int i = 1; - for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next) + for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next) { ++i; + } return tp; } @@ -3832,8 +3905,9 @@ int tabpage_index(tabpage_T *ftp) int i = 1; tabpage_T *tp; - for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next) + for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next) { ++i; + } return i; } @@ -3849,19 +3923,22 @@ static int leave_tabpage(buf_T *new_curbuf, bool trigger_leave_autocmds) { tabpage_T *tp = curtab; - reset_VIsual_and_resel(); /* stop Visual mode */ + reset_VIsual_and_resel(); // stop Visual mode if (trigger_leave_autocmds) { if (new_curbuf != curbuf) { apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf); - if (curtab != tp) + if (curtab != tp) { return FAIL; + } } apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf); - if (curtab != tp) + if (curtab != tp) { return FAIL; + } apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf); - if (curtab != tp) + if (curtab != tp) { return FAIL; + } } tp->tp_curwin = curwin; tp->tp_prevwin = prevwin; @@ -3879,8 +3956,7 @@ static int leave_tabpage(buf_T *new_curbuf, bool trigger_leave_autocmds) /// /// @param trigger_enter_autocmds when true trigger *Enter autocommands. /// @param trigger_leave_autocmds when true trigger *Leave autocommands. -static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, - bool trigger_enter_autocmds, +static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, bool trigger_enter_autocmds, bool trigger_leave_autocmds) { int old_off = tp->tp_firstwin->w_winrow; @@ -3893,7 +3969,7 @@ static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, topframe = tp->tp_topframe; if (old_curtab != curtab) { - tabpage_check_windows(old_curtab); + tabpage_check_windows(old_curtab); } /* We would like doing the TabEnter event first, but we don't have a @@ -3936,8 +4012,9 @@ static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, * 'columns' have been set correctly. */ if (trigger_enter_autocmds) { apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf); - if (old_curbuf != curbuf) + if (old_curbuf != curbuf) { apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf); + } } redraw_all_later(NOT_VALID); @@ -3988,35 +4065,39 @@ void goto_tabpage(int n) return; } - /* If there is only one it can't work. */ + // If there is only one it can't work. if (first_tabpage->tp_next == NULL) { - if (n > 1) + if (n > 1) { beep_flush(); + } return; } if (n == 0) { - /* No count, go to next tab page, wrap around end. */ - if (curtab->tp_next == NULL) + // No count, go to next tab page, wrap around end. + if (curtab->tp_next == NULL) { tp = first_tabpage; - else + } else { tp = curtab->tp_next; + } } else if (n < 0) { /* "gT": go to previous tab page, wrap around end. "N gT" repeats * this N times. */ ttp = curtab; for (i = n; i < 0; ++i) { for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL; - tp = tp->tp_next) + tp = tp->tp_next) { ; + } ttp = tp; } } else if (n == 9999) { - /* Go to last tab page. */ - for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next) + // Go to last tab page. + for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next) { ; + } } else { - /* Go to tab page "n". */ + // Go to tab page "n". tp = find_tabpage(n); if (tp == NULL) { beep_flush(); @@ -4032,20 +4113,20 @@ void goto_tabpage(int n) /// /// @param trigger_enter_autocmds when true trigger *Enter autocommands. /// @param trigger_leave_autocmds when true trigger *Leave autocommands. -void goto_tabpage_tp(tabpage_T *tp, bool trigger_enter_autocmds, - bool trigger_leave_autocmds) +void goto_tabpage_tp(tabpage_T *tp, bool trigger_enter_autocmds, bool trigger_leave_autocmds) { - /* Don't repeat a message in another tab page. */ + // Don't repeat a message in another tab page. set_keep_msg(NULL, 0); if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer, - trigger_leave_autocmds) == OK) { - if (valid_tabpage(tp)) + trigger_leave_autocmds) == OK) { + if (valid_tabpage(tp)) { enter_tabpage(tp, curbuf, trigger_enter_autocmds, - trigger_leave_autocmds); - else + trigger_leave_autocmds); + } else { enter_tabpage(curtab, curbuf, trigger_enter_autocmds, - trigger_leave_autocmds); + trigger_leave_autocmds); + } } } @@ -4141,13 +4222,15 @@ void win_goto(win_T *wp) text_locked_msg(); return; } - if (curbuf_locked()) + if (curbuf_locked()) { return; + } - if (wp->w_buffer != curbuf) + if (wp->w_buffer != curbuf) { reset_VIsual_and_resel(); - else if (VIsual_active) + } else if (VIsual_active) { wp->w_cursor = curwin->w_cursor; + } win_enter(wp, true); @@ -4233,9 +4316,11 @@ win_T *win_vert_neighbor(tabpage_T *tp, win_T *wp, bool up, long count) fr = fr->fr_next; } } - if (nfr->fr_layout == FR_COL && up) - while (fr->fr_next != NULL) + if (nfr->fr_layout == FR_COL && up) { + while (fr->fr_next != NULL) { fr = fr->fr_next; + } + } nfr = fr; } } @@ -4307,15 +4392,18 @@ win_T *win_horz_neighbor(tabpage_T *tp, win_T *wp, bool left, long count) } fr = nfr->fr_child; if (nfr->fr_layout == FR_COL) { - /* Find the frame at the cursor row. */ + // Find the frame at the cursor row. while (fr->fr_next != NULL && frame2win(fr)->w_winrow + fr->fr_height - <= wp->w_winrow + wp->w_wrow) + <= wp->w_winrow + wp->w_wrow) { fr = fr->fr_next; + } } - if (nfr->fr_layout == FR_ROW && left) - while (fr->fr_next != NULL) + if (nfr->fr_layout == FR_ROW && left) { + while (fr->fr_next != NULL) { fr = fr->fr_next; + } + } nfr = fr; } } @@ -4348,15 +4436,14 @@ void win_enter(win_T *wp, bool undo_sync) /// /// @param curwin_invalid curwin has just been closed and /// isn't valid when true. -static void win_enter_ext(win_T *wp, bool undo_sync, bool curwin_invalid, - bool trigger_new_autocmds, - bool trigger_enter_autocmds, - bool trigger_leave_autocmds) +static void win_enter_ext(win_T *wp, bool undo_sync, bool curwin_invalid, bool trigger_new_autocmds, + bool trigger_enter_autocmds, bool trigger_leave_autocmds) { int other_buffer = FALSE; - if (wp == curwin && !curwin_invalid) /* nothing to do */ + if (wp == curwin && !curwin_invalid) { // nothing to do return; + } if (!curwin_invalid && trigger_leave_autocmds) { /* @@ -4365,15 +4452,18 @@ static void win_enter_ext(win_T *wp, bool undo_sync, bool curwin_invalid, if (wp->w_buffer != curbuf) { apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf); other_buffer = TRUE; - if (!win_valid(wp)) + if (!win_valid(wp)) { return; + } } apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf); - if (!win_valid(wp)) + if (!win_valid(wp)) { return; - /* autocmds may abort script processing */ - if (aborting()) + } + // autocmds may abort script processing + if (aborting()) { return; + } } // sync undo before leaving the current buffer @@ -4390,16 +4480,17 @@ static void win_enter_ext(win_T *wp, bool undo_sync, bool curwin_invalid, buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP); } if (!curwin_invalid) { - prevwin = curwin; /* remember for CTRL-W p */ + prevwin = curwin; // remember for CTRL-W p curwin->w_redr_status = TRUE; } curwin = wp; curbuf = wp->w_buffer; check_cursor(); - if (!virtual_active()) + if (!virtual_active()) { curwin->w_cursor.coladd = 0; - changed_line_abv_curs(); /* assume cursor position needs updating */ + } + changed_line_abv_curs(); // assume cursor position needs updating // New directory is either the local directory of the window, tab or NULL. char *new_dir = (char *)(curwin->w_localdir @@ -4474,9 +4565,9 @@ static void win_enter_ext(win_T *wp, bool undo_sync, bool curwin_invalid, win_setwidth((int)p_wiw); } - setmouse(); /* in case jumped to/from help buffer */ + setmouse(); // in case jumped to/from help buffer - /* Change directories when the 'acd' option is set. */ + // Change directories when the 'acd' option is set. do_autochdir(); } @@ -4505,12 +4596,12 @@ win_T *buf_jump_open_win(buf_T *buf) /// @return the found window, or NULL. win_T *buf_jump_open_tab(buf_T *buf) { - // First try the current tab page. { win_T *wp = buf_jump_open_win(buf); - if (wp != NULL) + if (wp != NULL) { return wp; + } } FOR_ALL_TABS(tp) { @@ -4563,13 +4654,14 @@ static win_T *win_alloc(win_T *after, bool hidden) /* * link the window in the window list */ - if (!hidden) + if (!hidden) { win_append(after, new_wp); + } new_wp->w_wincol = 0; new_wp->w_width = Columns; - /* position the display and the cursor at the top of the file. */ + // position the display and the cursor at the top of the file. new_wp->w_topline = 1; new_wp->w_topfill = 0; new_wp->w_botline = 2; @@ -4583,7 +4675,7 @@ static win_T *win_alloc(win_T *after, bool hidden) new_wp->w_p_so = -1; new_wp->w_p_siso = -1; - /* We won't calculate w_fraction until resizing the window */ + // We won't calculate w_fraction until resizing the window new_wp->w_fraction = 0; new_wp->w_prev_fraction_row = -1; @@ -4596,8 +4688,7 @@ static win_T *win_alloc(win_T *after, bool hidden) // Free one wininfo_T. -void -free_wininfo(wininfo_T *wip, buf_T *bp) +void free_wininfo(wininfo_T *wip, buf_T *bp) { if (wip->wi_optset) { clear_winopt(&wip->wi_opt); @@ -4607,14 +4698,10 @@ free_wininfo(wininfo_T *wip, buf_T *bp) } -/* - * Remove window 'wp' from the window list and free the structure. - */ -static void -win_free ( - win_T *wp, - tabpage_T *tp /* tab page "win" is in, NULL for current */ -) +/// Remove window 'wp' from the window list and free the structure. +/// +/// @param tp tab page "win" is in, NULL for current +static void win_free(win_T *wp, tabpage_T *tp) { int i; wininfo_T *wip; @@ -4622,7 +4709,7 @@ win_free ( pmap_del(handle_T)(&window_handles, wp->handle); clearFolding(wp); - /* reduce the reference count to the argument list. */ + // reduce the reference count to the argument list. alist_unlink(wp->w_alist); /* Don't execute autocommands while the window is halfway being deleted. @@ -4632,7 +4719,7 @@ win_free ( clear_winopt(&wp->w_onebuf_opt); clear_winopt(&wp->w_allbuf_opt); - vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */ + vars_clear(&wp->w_vars->dv_hashtab); // free all w: variables hash_init(&wp->w_vars->dv_hashtab); unref_var_dict(wp->w_vars); @@ -4695,8 +4782,9 @@ win_free ( win_free_grid(wp, false); - if (wp != aucmd_win) + if (wp != aucmd_win) { win_remove(wp, tp); + } if (autocmd_busy) { wp->w_next = au_pending_free_win; au_pending_free_win = wp; @@ -4727,31 +4815,30 @@ void win_append(win_T *after, win_T *wp) { win_T *before; - if (after == NULL) /* after NULL is in front of the first */ + if (after == NULL) { // after NULL is in front of the first before = firstwin; - else + } else { before = after->w_next; + } wp->w_next = before; wp->w_prev = after; - if (after == NULL) + if (after == NULL) { firstwin = wp; - else + } else { after->w_next = wp; - if (before == NULL) + } + if (before == NULL) { lastwin = wp; - else + } else { before->w_prev = wp; + } } -/* - * Remove a window from the window list. - */ -void -win_remove ( - win_T *wp, - tabpage_T *tp /* tab page "win" is in, NULL for current */ -) +/// Remove a window from the window list. +/// +/// @param tp tab page "win" is in, NULL for current +void win_remove(win_T *wp, tabpage_T *tp) { if (wp->w_prev != NULL) { wp->w_prev->w_next = wp->w_next; @@ -4776,8 +4863,9 @@ static void frame_append(frame_T *after, frame_T *frp) { frp->fr_next = after->fr_next; after->fr_next = frp; - if (frp->fr_next != NULL) + if (frp->fr_next != NULL) { frp->fr_next->fr_prev = frp; + } frp->fr_prev = after; } @@ -4789,10 +4877,11 @@ static void frame_insert(frame_T *before, frame_T *frp) frp->fr_next = before; frp->fr_prev = before->fr_prev; before->fr_prev = frp; - if (frp->fr_prev != NULL) + if (frp->fr_prev != NULL) { frp->fr_prev->fr_next = frp; - else + } else { frp->fr_parent->fr_child = frp; + } } /* @@ -4823,10 +4912,12 @@ void shell_new_rows(void) { int h = (int)ROWS_AVAIL; - if (firstwin == NULL) /* not initialized yet */ + if (firstwin == NULL) { // not initialized yet return; - if (h < frame_minheight(topframe, NULL)) + } + if (h < frame_minheight(topframe, NULL)) { h = frame_minheight(topframe, NULL); + } /* First try setting the heights of windows with 'winfixheight'. If * that doesn't result in the right height, forget about that option. */ @@ -4839,7 +4930,6 @@ void shell_new_rows(void) win_reconfig_floats(); // The size of floats might change compute_cmdrow(); curtab->tp_ch_used = p_ch; - } /* @@ -4847,8 +4937,9 @@ void shell_new_rows(void) */ void shell_new_columns(void) { - if (firstwin == NULL) /* not initialized yet */ + if (firstwin == NULL) { // not initialized yet return; + } /* First try setting the widths of windows with 'winfixwidth'. If that * doesn't result in the right width, forget about that option. */ @@ -4921,7 +5012,7 @@ void win_size_restore(garray_T *gap) } } } - /* recompute the window positions */ + // recompute the window positions (void)win_comp_pos(); } } @@ -4971,9 +5062,8 @@ static void frame_comp_pos(frame_T *topfrp, int *row, int *col) wp = topfrp->fr_win; if (wp != NULL) { if (wp->w_winrow != *row - || wp->w_wincol != *col - ) { - /* position changed, redraw */ + || wp->w_wincol != *col) { + // position changed, redraw wp->w_winrow = *row; wp->w_wincol = *col; redraw_later(wp, NOT_VALID); @@ -5044,7 +5134,6 @@ void win_setheight_win(int height, win_T *win) msg_col = 0; redraw_all_later(NOT_VALID); } - } @@ -5063,22 +5152,24 @@ void win_setheight_win(int height, win_T *win) */ static void frame_setheight(frame_T *curfrp, int height) { - int room; /* total number of lines available */ - int take; /* number of lines taken from other windows */ - int room_cmdline; /* lines available from cmdline */ + int room; // total number of lines available + int take; // number of lines taken from other windows + int room_cmdline; // lines available from cmdline int run; frame_T *frp; int h; int room_reserved; - /* If the height already is the desired value, nothing to do. */ - if (curfrp->fr_height == height) + // If the height already is the desired value, nothing to do. + if (curfrp->fr_height == height) { return; + } if (curfrp->fr_parent == NULL) { - /* topframe: can only change the command line */ - if (height > ROWS_AVAIL) + // topframe: can only change the command line + if (height > ROWS_AVAIL) { height = ROWS_AVAIL; + } if (height > 0) { frame_new_height(curfrp, height, false, false); } @@ -5086,8 +5177,9 @@ static void frame_setheight(frame_T *curfrp, int height) /* Row of frames: Also need to resize frames left and right of this * one. First check for the minimal height of these. */ h = frame_minheight(curfrp->fr_parent, NULL); - if (height < h) + if (height < h) { height = h; + } frame_setheight(curfrp->fr_parent, height); } else { /* @@ -5119,7 +5211,7 @@ static void frame_setheight(frame_T *curfrp, int height) } else { win_T *wp = lastwin_nofloating(); room_cmdline = Rows - p_ch - - (wp->w_winrow + wp->w_height + wp->w_status_height); + - (wp->w_winrow + wp->w_height + wp->w_status_height); if (room_cmdline < 0) { room_cmdline = 0; } @@ -5133,8 +5225,8 @@ static void frame_setheight(frame_T *curfrp, int height) break; } frame_setheight(curfrp->fr_parent, height - + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1); - /*NOTREACHED*/ + + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1); + //NOTREACHED } /* @@ -5145,17 +5237,20 @@ static void frame_setheight(frame_T *curfrp, int height) /* If there is not enough room, also reduce the height of a window * with 'winfixheight' set. */ - if (height > room + room_cmdline - room_reserved) + if (height > room + room_cmdline - room_reserved) { room_reserved = room + room_cmdline - height; + } /* If there is only a 'winfixheight' window and making the * window smaller, need to make the other window taller. */ - if (take < 0 && room - curfrp->fr_height < room_reserved) + if (take < 0 && room - curfrp->fr_height < room_reserved) { room_reserved = 0; + } if (take > 0 && room_cmdline > 0) { - /* use lines from cmdline first */ - if (take < room_cmdline) + // use lines from cmdline first + if (take < room_cmdline) { room_cmdline = take; + } take -= room_cmdline; topframe->fr_height += room_cmdline; } @@ -5171,20 +5266,22 @@ static void frame_setheight(frame_T *curfrp, int height) * frame. */ for (run = 0; run < 2; ++run) { - if (run == 0) - frp = curfrp->fr_next; /* 1st run: start with next window */ - else - frp = curfrp->fr_prev; /* 2nd run: start with prev window */ + if (run == 0) { + frp = curfrp->fr_next; // 1st run: start with next window + } else { + frp = curfrp->fr_prev; // 2nd run: start with prev window + } while (frp != NULL && take != 0) { h = frame_minheight(frp, NULL); if (room_reserved > 0 && frp->fr_win != NULL && frp->fr_win->w_p_wfh) { - if (room_reserved >= frp->fr_height) + if (room_reserved >= frp->fr_height) { room_reserved -= frp->fr_height; - else { - if (frp->fr_height - room_reserved > take) + } else { + if (frp->fr_height - room_reserved > take) { room_reserved = frp->fr_height - take; + } take -= frp->fr_height - room_reserved; frame_new_height(frp, room_reserved, false, false); room_reserved = 0; @@ -5198,10 +5295,11 @@ static void frame_setheight(frame_T *curfrp, int height) take = 0; } } - if (run == 0) + if (run == 0) { frp = frp->fr_next; - else + } else { frp = frp->fr_prev; + } } } } @@ -5221,10 +5319,12 @@ void win_setwidth_win(int width, win_T *wp) /* Always keep current window at least one column wide, even when * 'winminwidth' is zero. */ if (wp == curwin) { - if (width < p_wmw) + if (width < p_wmw) { width = p_wmw; - if (width == 0) + } + if (width == 0) { width = 1; + } } else if (width < 0) { width = 0; } @@ -5239,7 +5339,6 @@ void win_setwidth_win(int width, win_T *wp) (void)win_comp_pos(); redraw_all_later(NOT_VALID); } - } /* @@ -5251,27 +5350,30 @@ void win_setwidth_win(int width, win_T *wp) */ static void frame_setwidth(frame_T *curfrp, int width) { - int room; /* total number of lines available */ - int take; /* number of lines taken from other windows */ + int room; // total number of lines available + int take; // number of lines taken from other windows int run; frame_T *frp; int w; int room_reserved; - /* If the width already is the desired value, nothing to do. */ - if (curfrp->fr_width == width) + // If the width already is the desired value, nothing to do. + if (curfrp->fr_width == width) { return; + } - if (curfrp->fr_parent == NULL) - /* topframe: can't change width */ + if (curfrp->fr_parent == NULL) { + // topframe: can't change width return; + } if (curfrp->fr_parent->fr_layout == FR_COL) { /* Column of frames: Also need to resize frames above and below of * this one. First check for the minimal width of these. */ w = frame_minwidth(curfrp->fr_parent, NULL); - if (width < w) + if (width < w) { width = w; + } frame_setwidth(curfrp->fr_parent, width); } else { /* @@ -5297,14 +5399,15 @@ static void frame_setwidth(frame_T *curfrp, int width) } } - if (width <= room) + if (width <= room) { break; + } if (run == 2 || curfrp->fr_height >= ROWS_AVAIL) { width = room; break; } frame_setwidth(curfrp->fr_parent, width - + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1); + + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1); } /* @@ -5315,12 +5418,14 @@ static void frame_setwidth(frame_T *curfrp, int width) /* If there is not enough room, also reduce the width of a window * with 'winfixwidth' set. */ - if (width > room - room_reserved) + if (width > room - room_reserved) { room_reserved = room - width; + } /* If there is only a 'winfixwidth' window and making the * window smaller, need to make the other window narrower. */ - if (take < 0 && room - curfrp->fr_width < room_reserved) + if (take < 0 && room - curfrp->fr_width < room_reserved) { room_reserved = 0; + } /* * set the current frame to the new width @@ -5333,20 +5438,22 @@ static void frame_setwidth(frame_T *curfrp, int width) * frame. */ for (run = 0; run < 2; ++run) { - if (run == 0) - frp = curfrp->fr_next; /* 1st run: start with next window */ - else - frp = curfrp->fr_prev; /* 2nd run: start with prev window */ + if (run == 0) { + frp = curfrp->fr_next; // 1st run: start with next window + } else { + frp = curfrp->fr_prev; // 2nd run: start with prev window + } while (frp != NULL && take != 0) { w = frame_minwidth(frp, NULL); if (room_reserved > 0 && frp->fr_win != NULL && frp->fr_win->w_p_wfw) { - if (room_reserved >= frp->fr_width) + if (room_reserved >= frp->fr_width) { room_reserved -= frp->fr_width; - else { - if (frp->fr_width - room_reserved > take) + } else { + if (frp->fr_width - room_reserved > take) { room_reserved = frp->fr_width - take; + } take -= frp->fr_width - room_reserved; frame_new_width(frp, room_reserved, false, false); room_reserved = 0; @@ -5360,10 +5467,11 @@ static void frame_setwidth(frame_T *curfrp, int width) take = 0; } } - if (run == 0) + if (run == 0) { frp = frp->fr_next; - else + } else { frp = frp->fr_prev; + } } } } @@ -5421,40 +5529,44 @@ void win_drag_status_line(win_T *dragwin, int offset) fr = dragwin->w_frame; curfr = fr; - if (fr != topframe) { /* more than one window */ + if (fr != topframe) { // more than one window fr = fr->fr_parent; /* When the parent frame is not a column of frames, its parent should * be. */ if (fr->fr_layout != FR_COL) { curfr = fr; - if (fr != topframe) /* only a row of windows, may drag statusline */ + if (fr != topframe) { // only a row of windows, may drag statusline fr = fr->fr_parent; + } } } /* If this is the last frame in a column, may want to resize the parent * frame instead (go two up to skip a row of frames). */ while (curfr != topframe && curfr->fr_next == NULL) { - if (fr != topframe) + if (fr != topframe) { fr = fr->fr_parent; + } curfr = fr; - if (fr != topframe) + if (fr != topframe) { fr = fr->fr_parent; + } } if (offset < 0) { // drag up up = true; offset = -offset; - /* sum up the room of the current frame and above it */ + // sum up the room of the current frame and above it if (fr == curfr) { - /* only one window */ + // only one window room = fr->fr_height - frame_minheight(fr, NULL); } else { room = 0; for (fr = fr->fr_child;; fr = fr->fr_next) { room += fr->fr_height - frame_minheight(fr, NULL); - if (fr == curfr) + if (fr == curfr) { break; + } } } fr = curfr->fr_next; // put fr at frame that grows @@ -5462,12 +5574,14 @@ void win_drag_status_line(win_T *dragwin, int offset) up = false; // Only dragging the last status line can reduce p_ch. room = Rows - cmdline_row; - if (curfr->fr_next == NULL) + if (curfr->fr_next == NULL) { room -= 1; - else + } else { room -= p_ch; - if (room < 0) + } + if (room < 0) { room = 0; + } // sum up the room of frames below of the current one FOR_ALL_FRAMES(fr, curfr->fr_next) { room += fr->fr_height - frame_minheight(fr, NULL); @@ -5475,10 +5589,12 @@ void win_drag_status_line(win_T *dragwin, int offset) fr = curfr; // put fr at window that grows } - if (room < offset) /* Not enough room */ - offset = room; /* Move as far as we can */ - if (offset <= 0) + if (room < offset) { // Not enough room + offset = room; // Move as far as we can + } + if (offset <= 0) { return; + } /* * Grow frame fr by "offset" lines. @@ -5488,11 +5604,11 @@ void win_drag_status_line(win_T *dragwin, int offset) frame_new_height(fr, fr->fr_height + offset, up, false); } - if (up) - fr = curfr; /* current frame gets smaller */ - else - fr = curfr->fr_next; /* next frame gets smaller */ - + if (up) { + fr = curfr; // current frame gets smaller + } else { + fr = curfr->fr_next; // next frame gets smaller + } /* * Now make the other frames smaller. */ @@ -5505,10 +5621,11 @@ void win_drag_status_line(win_T *dragwin, int offset) frame_new_height(fr, fr->fr_height - offset, !up, false); break; } - if (up) + if (up) { fr = fr->fr_prev; - else + } else { fr = fr->fr_next; + } } row = win_comp_pos(); grid_fill(&default_grid, row, cmdline_row, 0, Columns, ' ', ' ', 0); @@ -5517,8 +5634,9 @@ void win_drag_status_line(win_T *dragwin, int offset) } cmdline_row = row; p_ch = Rows - cmdline_row; - if (p_ch < 1) + if (p_ch < 1) { p_ch = 1; + } curtab->tp_ch_used = p_ch; redraw_all_later(SOME_VALID); showmode(); @@ -5536,14 +5654,16 @@ void win_drag_vsep_line(win_T *dragwin, int offset) int n; fr = dragwin->w_frame; - if (fr == topframe) /* only one window (cannot happen?) */ + if (fr == topframe) { // only one window (cannot happen?) return; + } curfr = fr; fr = fr->fr_parent; - /* When the parent frame is not a row of frames, its parent should be. */ + // When the parent frame is not a row of frames, its parent should be. if (fr->fr_layout != FR_ROW) { - if (fr == topframe) /* only a column of windows (cannot happen?) */ + if (fr == topframe) { // only a column of windows (cannot happen?) return; + } curfr = fr; fr = fr->fr_parent; } @@ -5551,8 +5671,9 @@ void win_drag_vsep_line(win_T *dragwin, int offset) /* If this is the last frame in a row, may want to resize a parent * frame instead. */ while (curfr->fr_next == NULL) { - if (fr == topframe) + if (fr == topframe) { break; + } curfr = fr; fr = fr->fr_parent; if (fr != topframe) { @@ -5564,12 +5685,13 @@ void win_drag_vsep_line(win_T *dragwin, int offset) if (offset < 0) { // drag left left = true; offset = -offset; - /* sum up the room of the current frame and left of it */ + // sum up the room of the current frame and left of it room = 0; for (fr = fr->fr_child;; fr = fr->fr_next) { room += fr->fr_width - frame_minwidth(fr, NULL); - if (fr == curfr) + if (fr == curfr) { break; + } } fr = curfr->fr_next; // put fr at frame that grows } else { // drag right @@ -5600,12 +5722,12 @@ void win_drag_vsep_line(win_T *dragwin, int offset) // grow frame fr by offset lines frame_new_width(fr, fr->fr_width + offset, left, false); - /* shrink other frames: current and at the left or at the right */ - if (left) - fr = curfr; /* current frame gets smaller */ - else - fr = curfr->fr_next; /* next frame gets smaller */ - + // shrink other frames: current and at the left or at the right + if (left) { + fr = curfr; // current frame gets smaller + } else { + fr = curfr->fr_next; // next frame gets smaller + } while (fr != NULL && offset > 0) { n = frame_minwidth(fr, NULL); if (fr->fr_width - offset <= n) { @@ -5615,10 +5737,11 @@ void win_drag_vsep_line(win_T *dragwin, int offset) frame_new_width(fr, fr->fr_width - offset, !left, false); break; } - if (left) + if (left) { fr = fr->fr_prev; - else + } else { fr = fr->fr_next; + } } (void)win_comp_pos(); redraw_all_later(NOT_VALID); @@ -5636,7 +5759,7 @@ void set_fraction(win_T *wp) // it's halfway that line. Thus with two lines it is 25%, with three // lines 17%, etc. Similarly for the last line: 75%, 83%, etc. wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT + FRACTION_MULT / 2) - / (long)wp->w_height_inner; + / (long)wp->w_height_inner; } } @@ -5662,9 +5785,9 @@ void win_new_height(win_T *wp, int height) void scroll_to_fraction(win_T *wp, int prev_height) { - linenr_T lnum; - int sline, line_size; - int height = wp->w_height_inner; + linenr_T lnum; + int sline, line_size; + int height = wp->w_height_inner; // Don't change w_topline in any of these cases: // - window height is 0 @@ -5673,15 +5796,16 @@ void scroll_to_fraction(win_T *wp, int prev_height) // is visible. if (height > 0 && (!wp->w_p_scb || wp == curwin) - && (height < wp->w_buffer->b_ml.ml_line_count || wp->w_topline > 1) - ) { + && (height < wp->w_buffer->b_ml.ml_line_count || + wp->w_topline > 1)) { /* * Find a value for w_topline that shows the cursor at the same * relative position in the window as before (more or less). */ lnum = wp->w_cursor.lnum; - if (lnum < 1) /* can happen when starting up */ + if (lnum < 1) { // can happen when starting up lnum = 1; + } wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT; line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1; sline = wp->w_wrow - line_size; @@ -5717,7 +5841,7 @@ void scroll_to_fraction(win_T *wp, int prev_height) while (sline > 0 && lnum > 1) { (void)hasFoldingWin(wp, lnum, &lnum, NULL, true, NULL); if (lnum == 1) { - /* first line in buffer is folded */ + // first line in buffer is folded line_size = 1; --sline; break; @@ -5870,15 +5994,16 @@ void command_height(void) frp = frp->fr_parent; } - /* Avoid changing the height of a window with 'winfixheight' set. */ + // Avoid changing the height of a window with 'winfixheight' set. while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF - && frp->fr_win->w_p_wfh) + && frp->fr_win->w_p_wfh) { frp = frp->fr_prev; + } if (starting != NO_SCREEN) { cmdline_row = Rows - p_ch; - if (p_ch > old_p_ch) { /* p_ch got bigger */ + if (p_ch > old_p_ch) { // p_ch got bigger while (p_ch > old_p_ch) { if (frp == NULL) { EMSG(_(e_noroom)); @@ -5888,14 +6013,15 @@ void command_height(void) break; } h = frp->fr_height - frame_minheight(frp, NULL); - if (h > p_ch - old_p_ch) + if (h > p_ch - old_p_ch) { h = p_ch - old_p_ch; + } old_p_ch += h; frame_add_height(frp, -h); frp = frp->fr_prev; } - /* Recompute window positions. */ + // Recompute window positions. (void)win_comp_pos(); // clear the lines added to cmdline @@ -5907,15 +6033,17 @@ void command_height(void) return; } - if (msg_row < cmdline_row) + if (msg_row < cmdline_row) { msg_row = cmdline_row; + } redraw_cmdline = true; } frame_add_height(frp, (int)(old_p_ch - p_ch)); - /* Recompute window positions. */ - if (frp != lastwin->w_frame) + // Recompute window positions. + if (frp != lastwin->w_frame) { (void)win_comp_pos(); + } } /* @@ -5927,8 +6055,9 @@ static void frame_add_height(frame_T *frp, int n) frame_new_height(frp, frp->fr_height + n, false, false); for (;; ) { frp = frp->fr_parent; - if (frp == NULL) + if (frp == NULL) { break; + } frp->fr_height += n; } } @@ -5944,8 +6073,9 @@ char_u *grab_file_name(long count, linenr_T *file_lnum) if (VIsual_active) { size_t len; char_u *ptr; - if (get_visual_text(NULL, &ptr, &len) == FAIL) + if (get_visual_text(NULL, &ptr, &len) == FAIL) { return NULL; + } // Only recognize ":123" here if (file_lnum != NULL && ptr[len] == ':' && isdigit(ptr[len + 1])) { char_u *p = ptr + len + 1; @@ -5965,31 +6095,24 @@ char_u *grab_file_name(long count, linenr_T *file_lnum) * NULL is returned if the file name or file is not found. * * options: - * FNAME_MESS give error messages - * FNAME_EXP expand to path - * FNAME_HYP check for hypertext link - * FNAME_INCL apply "includeexpr" + * FNAME_MESS give error messages + * FNAME_EXP expand to path + * FNAME_HYP check for hypertext link + * FNAME_INCL apply "includeexpr" */ char_u *file_name_at_cursor(int options, long count, linenr_T *file_lnum) { return file_name_in_line(get_cursor_line_ptr(), - curwin->w_cursor.col, options, count, curbuf->b_ffname, - file_lnum); + curwin->w_cursor.col, options, count, curbuf->b_ffname, + file_lnum); } -/* - * Return the name of the file under or after ptr[col]. - * Otherwise like file_name_at_cursor(). - */ -char_u * -file_name_in_line ( - char_u *line, - int col, - int options, - long count, - char_u *rel_fname, /* file we are searching relative to */ - linenr_T *file_lnum /* line number after the file name */ -) +/// @param rel_fname file we are searching relative to +/// @param file_lnum line number after the file name +/// +/// @return the name of the file under or after ptr[col]. Otherwise like file_name_at_cursor(). +char_u *file_name_in_line(char_u *line, int col, int options, long count, char_u *rel_fname, + linenr_T *file_lnum) { char_u *ptr; size_t len; @@ -6056,8 +6179,9 @@ file_name_in_line ( * But don't remove "..", could be a directory name. */ if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL - && ptr[len - 2] != '.') + && ptr[len - 2] != '.') { --len; + } if (file_lnum != NULL) { char_u *p; @@ -6095,7 +6219,7 @@ file_name_in_line ( /// @param morewin pretend there are two or more windows if true. void last_status(bool morewin) { - /* Don't make a difference between horizontal or vertical split. */ + // Don't make a difference between horizontal or vertical split. last_status_rec(topframe, (p_ls == 2 || (p_ls == 1 && (morewin || !one_window())))); } @@ -6108,12 +6232,12 @@ static void last_status_rec(frame_T *fr, bool statusline) if (fr->fr_layout == FR_LEAF) { wp = fr->fr_win; if (wp->w_status_height != 0 && !statusline) { - /* remove status line */ + // remove status line win_new_height(wp, wp->w_height + 1); wp->w_status_height = 0; comp_col(); } else if (wp->w_status_height == 0 && statusline) { - /* Find a frame to take a line from. */ + // Find a frame to take a line from. fp = fr; while (fp->fr_height <= frame_minheight(fp, NULL)) { if (fp == topframe) { @@ -6122,18 +6246,20 @@ static void last_status_rec(frame_T *fr, bool statusline) } /* In a column of frames: go to frame above. If already at * the top or in a row of frames: go to parent. */ - if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL) + if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL) { fp = fp->fr_prev; - else + } else { fp = fp->fr_parent; + } } wp->w_status_height = 1; if (fp != fr) { frame_new_height(fp, fp->fr_height - 1, false, false); frame_fix_height(wp); (void)win_comp_pos(); - } else + } else { win_new_height(wp, wp->w_height - 1); + } comp_col(); redraw_all_later(SOME_VALID); } @@ -6143,9 +6269,10 @@ static void last_status_rec(frame_T *fr, bool statusline) last_status_rec(fp, statusline); } } else { - /* horizontally split window, set status line for last one */ - for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next) + // horizontally split window, set status line for last one + for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next) { ; + } last_status_rec(fp, statusline); } } @@ -6160,8 +6287,10 @@ int tabline_height(void) } assert(first_tabpage); switch (p_stal) { - case 0: return 0; - case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1; + case 0: + return 0; + case 1: + return (first_tabpage->tp_next == NULL) ? 0 : 1; } return 1; } @@ -6172,8 +6301,9 @@ int tabline_height(void) */ int min_rows(void) { - if (firstwin == NULL) /* not initialized yet */ + if (firstwin == NULL) { // not initialized yet return MIN_LINES; + } int total = 0; FOR_ALL_TABS(tp) { @@ -6183,7 +6313,7 @@ int min_rows(void) } } total += tabline_height(); - total += 1; /* count the room for the command line */ + total += 1; // count the room for the command line return total; } @@ -6278,12 +6408,15 @@ static void make_snapshot_rec(frame_T *fr, frame_T **frp) (*frp)->fr_layout = fr->fr_layout; (*frp)->fr_width = fr->fr_width; (*frp)->fr_height = fr->fr_height; - if (fr->fr_next != NULL) + if (fr->fr_next != NULL) { make_snapshot_rec(fr->fr_next, &((*frp)->fr_next)); - if (fr->fr_child != NULL) + } + if (fr->fr_child != NULL) { make_snapshot_rec(fr->fr_child, &((*frp)->fr_child)); - if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin) + } + if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin) { (*frp)->fr_win = curwin; + } } /* @@ -6304,16 +6437,12 @@ static void clear_snapshot_rec(frame_T *fr) } } -/* - * Restore a previously created snapshot, if there is any. - * This is only done if the screen size didn't change and the window layout is - * still the same. - */ -void -restore_snapshot ( - int idx, - int close_curwin /* closing current window */ -) +/// Restore a previously created snapshot, if there is any. +/// This is only done if the screen size didn't change and the window layout is +/// still the same. +/// +/// @param close_curwin closing current window +void restore_snapshot(int idx, int close_curwin) { win_T *wp; @@ -6342,8 +6471,9 @@ static int check_snapshot_rec(frame_T *sn, frame_T *fr) && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL) || (sn->fr_child != NULL && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL) - || (sn->fr_win != NULL && !win_valid(sn->fr_win))) + || (sn->fr_win != NULL && !win_valid(sn->fr_win))) { return FAIL; + } return OK; } @@ -6366,13 +6496,15 @@ static win_T *restore_snapshot_rec(frame_T *sn, frame_T *fr) } if (sn->fr_next != NULL) { wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next); - if (wp2 != NULL) + if (wp2 != NULL) { wp = wp2; + } } if (sn->fr_child != NULL) { wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child); - if (wp2 != NULL) + if (wp2 != NULL) { wp = wp2; + } } return wp; } @@ -6407,16 +6539,16 @@ static win_T *get_snapshot_focus(int idx) /// triggered, another tabpage access is limited. /// /// @return FAIL if switching to "win" failed. -int switch_win(win_T **save_curwin, tabpage_T **save_curtab, win_T *win, - tabpage_T *tp, bool no_display) +int switch_win(win_T **save_curwin, tabpage_T **save_curtab, win_T *win, tabpage_T *tp, + bool no_display) { block_autocmds(); return switch_win_noblock(save_curwin, save_curtab, win, tp, no_display); } // As switch_win() but without blocking autocommands. -int switch_win_noblock(win_T **save_curwin, tabpage_T **save_curtab, - win_T *win, tabpage_T *tp, bool no_display) +int switch_win_noblock(win_T **save_curwin, tabpage_T **save_curtab, win_T *win, tabpage_T *tp, + bool no_display) { *save_curwin = curwin; if (tp != NULL) { @@ -6449,8 +6581,7 @@ void restore_win(win_T *save_curwin, tabpage_T *save_curtab, bool no_display) } // As restore_win() but without unblocking autocommands. -void restore_win_noblock(win_T *save_curwin, tabpage_T *save_curtab, - bool no_display) +void restore_win_noblock(win_T *save_curwin, tabpage_T *save_curtab, bool no_display) { if (save_curtab != NULL && valid_tabpage(save_curtab)) { if (no_display) { @@ -6506,9 +6637,8 @@ void restore_buffer(bufref_T *save_curbuf) /// particular ID is desired /// @param[in] conceal_char pointer to conceal replacement char /// @return ID of added match, -1 on failure. -int match_add(win_T *wp, const char *const grp, const char *const pat, - int prio, int id, list_T *pos_list, - const char *const conceal_char) +int match_add(win_T *wp, const char *const grp, const char *const pat, int prio, int id, + list_T *pos_list, const char *const conceal_char) FUNC_ATTR_NONNULL_ARG(1, 2) { matchitem_T *cur; @@ -6548,10 +6678,12 @@ int match_add(win_T *wp, const char *const grp, const char *const pat, // Find available match ID. while (id == -1) { cur = wp->w_match_head; - while (cur != NULL && cur->id != wp->w_next_match_id) + while (cur != NULL && cur->id != wp->w_next_match_id) { cur = cur->next; - if (cur == NULL) + } + if (cur == NULL) { id = wp->w_next_match_id; + } wp->w_next_match_id++; } @@ -6643,8 +6775,8 @@ int match_add(win_T *wp, const char *const grp, const char *const pat, } }); - // Calculate top and bottom lines for redrawing area - if (toplnum != 0){ + // Calculate top and bottom lines for redrawing area + if (toplnum != 0) { if (wp->w_buffer->b_mod_set) { if (wp->w_buffer->b_mod_top > toplnum) { wp->w_buffer->b_mod_top = toplnum; @@ -6672,10 +6804,11 @@ int match_add(win_T *wp, const char *const grp, const char *const pat, prev = cur; cur = cur->next; } - if (cur == prev) + if (cur == prev) { wp->w_match_head = m; - else + } else { prev->next = m; + } m->next = cur; redraw_later(wp, rtype); @@ -6714,10 +6847,11 @@ int match_delete(win_T *wp, int id, bool perr) } return -1; } - if (cur == prev) + if (cur == prev) { wp->w_match_head = cur->next; - else + } else { prev->next = cur->next; + } vim_regfree(cur->match.regprog); xfree(cur->pattern); if (cur->pos.toplnum != 0) { @@ -6766,8 +6900,9 @@ matchitem_T *get_match(win_T *wp, int id) { matchitem_T *cur = wp->w_match_head; - while (cur != NULL && cur->id != id) + while (cur != NULL && cur->id != id) { cur = cur->next; + } return cur; }