diff options
Diffstat (limited to 'src/nvim/ex_cmds.c')
-rw-r--r-- | src/nvim/ex_cmds.c | 1680 |
1 files changed, 900 insertions, 780 deletions
diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index 4af7794317..bbc1dd9717 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -7,54 +7,58 @@ #include <assert.h> #include <float.h> -#include <stdbool.h> -#include <string.h> -#include <stdlib.h> #include <inttypes.h> #include <math.h> +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> +#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" #include "nvim/quickfix.h" #include "nvim/regexp.h" #include "nvim/screen.h" @@ -65,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 @@ -103,7 +104,7 @@ typedef struct { // the preview window typedef struct { kvec_t(SubResult) subresults; - linenr_T lines_needed; // lines neede in the preview window + linenr_T lines_needed; // lines needed in the preview window } PreviewLines; #ifdef INCLUDE_GENERATED_DECLARATIONS @@ -146,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++]; @@ -196,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; @@ -237,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; } @@ -265,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) { @@ -305,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; @@ -321,9 +329,9 @@ void ex_align(exarg_T *eap) */ static int linelen(int *has_tab) { - char_u *line; - char_u *first; - char_u *last; + char_u *line; + char_u *first; + char_u *last; int save; int len; @@ -355,8 +363,8 @@ static int linelen(int *has_tab) /* Buffer for two lines used during sorting. They are allocated to * contain the longest line being sorted. */ -static char_u *sortbuf1; -static char_u *sortbuf2; +static char_u *sortbuf1; +static char_u *sortbuf2; static int sort_lc; ///< sort using locale static int sort_ic; ///< ignore case @@ -399,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. @@ -435,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; } @@ -450,9 +461,9 @@ void ex_sort(exarg_T *eap) long maxlen = 0; size_t count = (size_t)(eap->line2 - eap->line1 + 1); size_t i; - char_u *p; - char_u *s; - char_u *s2; + char_u *p; + char_u *s; + char_u *s2; char_u c; // temporary character storage bool unique = false; long deleted; @@ -598,7 +609,7 @@ void ex_sort(exarg_T *eap) } else { nrs[lnum - eap->line1].st_u.num.is_number = true; vim_str2nr(s, NULL, NULL, sort_what, - &nrs[lnum - eap->line1].st_u.num.value, NULL, 0); + &nrs[lnum - eap->line1].st_u.num.value, NULL, 0, false); } } else { s = skipwhite(p); @@ -622,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. @@ -635,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; @@ -664,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 @@ -714,7 +729,7 @@ sortend: void ex_retab(exarg_T *eap) { linenr_T lnum; - int got_tab = FALSE; + bool got_tab = false; long num_spaces = 0; long num_tabs; long len; @@ -723,18 +738,18 @@ void ex_retab(exarg_T *eap) long start_col = 0; // For start of white-space string long start_vcol = 0; // For start of white-space string long old_len; - char_u *ptr; - char_u *new_line = (char_u *)1; // init to non-NULL - int did_undo; // called u_save for current line + char_u *ptr; + char_u *new_line = (char_u *)1; // init to non-NULL + bool did_undo; // called u_save for current line long *new_vts_array = NULL; 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)) { @@ -757,23 +772,24 @@ void ex_retab(exarg_T *eap) ptr = ml_get(lnum); col = 0; vcol = 0; - did_undo = FALSE; + did_undo = false; 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; } - if (ptr[col] == ' ') + if (ptr[col] == ' ') { num_spaces++; - else - got_tab = TRUE; + } else { + got_tab = true; + } } 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) { @@ -795,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' : ' '; @@ -823,20 +840,23 @@ void ex_retab(exarg_T *eap) col = start_col + len; } } - got_tab = FALSE; + got_tab = false; num_spaces = 0; } - if (ptr[col] == NUL) + if (ptr[col] == NUL) { break; - vcol += chartabsize(ptr + col, (colnr_T)vcol); + } + 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'. @@ -854,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 @@ -886,7 +906,7 @@ void ex_retab(exarg_T *eap) */ int do_move(linenr_T line1, linenr_T line2, linenr_T dest) { - char_u *str; + char_u *str; linenr_T l; linenr_T extra; // Num lines added before line1 linenr_T num_lines; // Num lines moved @@ -921,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++; + } } /* @@ -984,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, @@ -1005,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); @@ -1032,7 +1058,7 @@ int do_move(linenr_T line1, linenr_T line2, linenr_T dest) void ex_copy(linenr_T line1, linenr_T line2, linenr_T n) { linenr_T count; - char_u *p; + char_u *p; count = line2 - line1 + 1; curbuf->b_op_start.lnum = n + 1; @@ -1050,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) { @@ -1061,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; } @@ -1077,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) @@ -1088,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 @@ -1101,9 +1130,9 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, linenr_T line2 = eap->line2; // end of range char_u *newcmd = NULL; // the new command bool free_newcmd = false; // need to free() newcmd - char_u *t; - char_u *p; - char_u *trailarg; + char_u *t; + char_u *p; + char_u *trailarg; int len; int scroll_save = msg_scroll; @@ -1115,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; } @@ -1129,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)); @@ -1141,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); @@ -1157,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; @@ -1173,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. */ @@ -1194,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('!'); @@ -1204,49 +1236,48 @@ 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); + 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; + char_u *itmp = NULL; + char_u *otmp = NULL; linenr_T linecount; linenr_T read_linecount; pos_T cursor_save; - char_u *cmd_buf; - buf_T *old_curbuf = curbuf; + char_u *cmd_buf; + buf_T *old_curbuf = curbuf; 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; @@ -1269,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. @@ -1289,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; } @@ -1298,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(). @@ -1308,18 +1340,20 @@ 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); if (do_out) { - if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL) { + if (u_save((line2), (linenr_T)(line2 + 1)) == FAIL) { xfree(cmd_buf); goto error; } @@ -1350,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; @@ -1385,11 +1420,11 @@ static void do_filter( * Adjust '[ and '] (set by buf_write()). */ curwin->w_cursor.lnum = line1; - del_lines(linecount, TRUE); - curbuf->b_op_start.lnum -= linecount; /* adjust '[ */ - curbuf->b_op_end.lnum -= linecount; /* adjust '] */ - write_lnum_adjust(-linecount); /* adjust last line - for next write */ + del_lines(linecount, true); + curbuf->b_op_start.lnum -= linecount; // adjust '[ + curbuf->b_op_end.lnum -= linecount; // adjust '] + write_lnum_adjust(-linecount); // adjust last line + // for next write foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum); } else { /* @@ -1399,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); @@ -1426,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. @@ -1482,7 +1519,7 @@ do_shell( msg_row = Rows - 1; msg_col = 0; - apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf); + apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, false, curbuf); } #if !defined(UNIX) @@ -1522,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 (" "), @@ -1574,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 @@ -1588,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" @@ -1603,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); } } @@ -1635,28 +1672,30 @@ void print_line(linenr_T lnum, int use_number, int list) msg_start(); silent_mode = FALSE; - info_message = TRUE; /* use mch_msg(), not mch_errmsg() */ + info_message = true; // use mch_msg(), not mch_errmsg() print_line_no_prefix(lnum, use_number, list); if (save_silent) { msg_putchar('\n'); ui_flush(); silent_mode = save_silent; } - info_message = FALSE; + info_message = false; } int rename_buffer(char_u *new_fname) { - char_u *fname, *sfname, *xfname; - buf_T *buf; + char_u *fname, *sfname, *xfname; + buf_T *buf; buf = curbuf; - apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf); - /* buffer changed, don't change name now */ - if (buf != curbuf) + apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, false, 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 @@ -1683,8 +1722,8 @@ 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. */ + apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, false, curbuf); + // Change directories when the 'acd' option is set. do_autochdir(); return OK; } @@ -1722,8 +1761,9 @@ void ex_file(exarg_T *eap) */ void ex_update(exarg_T *eap) { - if (curbufIsChanged()) + if (curbufIsChanged()) { (void)do_write(eap); + } } /* @@ -1755,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 *ffname; + 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; + 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) { @@ -1779,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); } @@ -1789,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. */ @@ -1823,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")); @@ -1835,12 +1879,12 @@ int do_write(exarg_T *eap) if (check_overwrite(eap, curbuf, fname, ffname, other) == OK) { if (eap->cmdidx == CMD_saveas && alt_buf != NULL) { - buf_T *was_curbuf = curbuf; + buf_T *was_curbuf = curbuf; - apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf); - apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf); + 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; } @@ -1859,14 +1903,14 @@ int do_write(exarg_T *eap) alt_buf->b_sfname = curbuf->b_sfname; curbuf->b_sfname = fname; buf_name_changed(curbuf); - apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf); - apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf); + apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, false, curbuf); + apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, false, alt_buf); if (!alt_buf->b_p_bl) { alt_buf->b_p_bl = TRUE; 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; } @@ -1886,13 +1930,13 @@ 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; - redraw_tabline = TRUE; + redraw_tabline = true; } } @@ -1908,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: @@ -1948,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)); @@ -1957,11 +1997,11 @@ 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; - char_u *swapname; + char_u *dir; + char_u *p; + char_u *swapname; /* We only try the first entry in 'directory', without checking if * it's writable. If the "." directory is not writable the write @@ -1983,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); @@ -1993,7 +2033,7 @@ check_overwrite( eap->forceit = TRUE; } else { EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"), - swapname); + swapname); xfree(swapname); return FAIL; } @@ -2011,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); + } } /* @@ -2058,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; @@ -2071,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(); } } @@ -2086,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; } @@ -2107,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; } @@ -2144,14 +2190,13 @@ 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; int other; int retval; - char_u *free_me = NULL; + char_u *free_me = NULL; if (text_locked()) { return GETFILE_ERROR; @@ -2161,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 @@ -2183,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; @@ -2218,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 @@ -2236,55 +2284,52 @@ 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 auto_buf = FALSE; /* TRUE if autocommands brought us - into the buffer unexpectedly */ - char_u *new_name = NULL; - int did_set_swapcommand = FALSE; - buf_T *buf; - bufref_T bufref; - bufref_T old_curbuf; - char_u *free_fname = NULL; + bool other_file; // true if editing another file + int oldbuf; // TRUE if using existing buffer + bool auto_buf = false; // true if autocommands brought us + // into the buffer unexpectedly + char_u *new_name = NULL; + bool did_set_swapcommand = false; + buf_T *buf; + bufref_T bufref; + bufref_T old_curbuf; + char_u *free_fname = NULL; int retval = FAIL; long n; pos_T orig_pos; linenr_T topline = 0; int newcol = -1; int solcol = -1; - pos_T *pos; - char_u *command = NULL; - int did_get_winopts = FALSE; + pos_T *pos; + char_u *command = NULL; + bool did_get_winopts = false; int readfile_flags = 0; 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 */ - other_file = TRUE; + 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)) @@ -2292,19 +2337,19 @@ int do_ecmd( goto theend; } - if (ffname == NULL) - other_file = TRUE; - /* 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 */ + if (ffname == NULL) { + other_file = true; + } else if (*ffname == NUL && curbuf->b_ffname == NULL) { // there is no file name + other_file = false; + } 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); } } @@ -2350,7 +2395,7 @@ int do_ecmd( vim_snprintf(p, len, "%" PRId64 "G", (int64_t)newlnum); } set_vim_var_string(VV_SWAPCOMMAND, p, -1); - did_set_swapcommand = TRUE; + did_set_swapcommand = true; xfree(p); } @@ -2378,13 +2423,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; } @@ -2398,8 +2444,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; @@ -2458,7 +2505,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; } @@ -2480,10 +2527,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)) { @@ -2524,7 +2570,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); @@ -2536,8 +2582,7 @@ int do_ecmd( * before, reset the local window options to the global * values. Also restores old folding stuff. */ get_winopts(curbuf); - did_get_winopts = TRUE; - + did_get_winopts = true; } xfree(new_name); au_new_curbuf.br_buf = NULL; @@ -2569,10 +2614,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 @@ -2580,14 +2627,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; @@ -2607,7 +2654,7 @@ int do_ecmd( && (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)) { // Sync first so that this is a separate undo-able action. u_sync(false); - if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, true) + if (u_savecommon(curbuf, 0, curbuf->b_ml.ml_line_count + 1, 0, true) == FAIL) { xfree(new_name); goto theend; @@ -2633,12 +2680,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; } @@ -2646,7 +2695,7 @@ int do_ecmd( * If we get here we are sure to start editing */ - /* Assume success now */ + // Assume success now retval = OK; /* @@ -2670,7 +2719,7 @@ int do_ecmd( } } - /* Change directories when the 'acd' option is set. */ + // Change directories when the 'acd' option is set. do_autochdir(); /* @@ -2679,18 +2728,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 @@ -2699,9 +2750,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); @@ -2717,10 +2768,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(); @@ -2736,68 +2788,74 @@ 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); } } - /* Check if cursors in other windows on the same buffer are still valid */ - check_lnums(FALSE); + // Check if cursors in other windows on the same buffer are still valid + check_lnums(false); /* * Did not read the file, need to show some info about the file. * 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 */ - check_for_delay(FALSE); + } + 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; + msg_scrolled_ign = true; if (!shortmess(SHM_FILEINFO)) { fileinfo(false, true, false); } - msg_scrolled_ign = FALSE; + msg_scrolled_ign = false; } 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; @@ -2812,10 +2870,11 @@ int do_ecmd( redraw_curbuf_later(NOT_VALID); // redraw this buffer later } - if (p_im) - need_start_insertmode = TRUE; + if (p_im) { + need_start_insertmode = true; + } - /* Change directories when the 'acd' option is set. */ + // Change directories when the 'acd' option is set. do_autochdir(); @@ -2837,65 +2896,72 @@ 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" */ void ex_append(exarg_T *eap) { - char_u *theline; + char_u *theline; bool did_undo = false; linenr_T lnum = eap->line2; int indent = 0; - char_u *p; + char_u *p; 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; - need_wait_return = FALSE; + need_wait_return = false; if (curbuf->b_p_ai) { if (append_indent >= 0) { indent = append_indent; append_indent = -1; - } else if (lnum > 0) + } else if (lnum > 0) { indent = get_indent_lnum(lnum); + } } - ex_keep_indent = FALSE; 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++; @@ -2906,28 +2972,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; + } - /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */ - if (ex_keep_indent) - append_indent = indent; - - /* 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)) @@ -2936,9 +2999,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); @@ -2954,8 +3018,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) @@ -2963,8 +3028,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; @@ -2972,8 +3038,8 @@ void ex_append(exarg_T *eap) check_cursor_lnum(); beginline(BL_SOL | BL_FIX); - need_wait_return = FALSE; /* don't use wait_return() now */ - ex_no_reprint = TRUE; + need_wait_return = false; // don't use wait_return() now + ex_no_reprint = true; } /* @@ -2984,33 +3050,36 @@ 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)); + deleted_lines_mark(eap->line1, (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); } void ex_z(exarg_T *eap) { - char_u *x; - int64_t bigness; - char_u *kind; + char_u *x; + int64_t bigness; + char_u *kind; int minus = 0; linenr_T start, end, curs, i; int j; @@ -3019,7 +3088,7 @@ void ex_z(exarg_T *eap) // Vi compatible: ":z!" uses display height, without a count uses // 'scroll' if (eap->forceit) { - bigness = curwin->w_height_inner; + bigness = Rows - 1; } else if (ONE_WINDOW) { bigness = curwin->w_p_scr * 2; } else { @@ -3032,10 +3101,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)) { @@ -3055,10 +3126,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 '-': @@ -3086,22 +3159,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; @@ -3113,8 +3189,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); @@ -3122,8 +3199,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('-'); + } } } @@ -3157,7 +3235,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" @@ -3194,8 +3272,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 @@ -3222,8 +3299,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; @@ -3290,8 +3367,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. @@ -3363,8 +3439,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; @@ -3382,8 +3457,8 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, int delimiter; bool has_second_delim = false; int sublen; - int got_quit = false; - int got_match = false; + bool got_quit = false; + bool got_match = false; int which_pat; char_u *cmd = eap->arg; linenr_T first_line = 0; // first changed line @@ -3409,12 +3484,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 @@ -3452,11 +3527,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 @@ -3467,18 +3542,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 "$". */ @@ -3504,15 +3579,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)); @@ -3570,9 +3646,9 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, colnr_T copycol; colnr_T matchcol; colnr_T prev_matchcol = MAXCOL; - char_u *new_end, *new_start = NULL; - char_u *p1; - int did_sub = FALSE; + char_u *new_end, *new_start = NULL; + char_u *p1; + bool did_sub = false; int lastone; long nmatch_tl = 0; // nr of lines matched below lnum int do_again; // do it again after joining lines @@ -3582,29 +3658,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 @@ -3627,10 +3703,10 @@ 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; + got_match = true; } /* @@ -3720,11 +3796,12 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, skip_match = true; } sub_nsubs++; - did_sub = TRUE; - /* Skip the substitution, unless an expression is used, - * then it is evaluated in the sandbox. */ - if (!(sub[0] == '\\' && sub[1] == '=')) + did_sub = true; + // Skip the substitution, unless an expression is used, + // then it is evaluated in the sandbox. + if (!(sub[0] == '\\' && sub[1] == '=')) { goto skip; + } } if (subflags.do_ask && !preview) { @@ -3734,7 +3811,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) { @@ -3743,15 +3820,17 @@ 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. */ while (subflags.do_ask) { if (exmode_active) { - char_u *resp; + char *prompt; + char_u *resp; colnr_T sc, ec; print_line_no_prefix(lnum, subflags.do_number, subflags.do_list); @@ -3767,13 +3846,14 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, sc += numw; ec += numw; } - msg_start(); - for (i = 0; i < (long)sc; ++i) - msg_putchar(' '); - for (; i <= (long)ec; ++i) - msg_putchar('^'); - resp = getexmodeline('?', NULL, 0, true); + prompt = xmallocz(ec + 1); + memset(prompt, ' ', sc); + memset(prompt + sc, '^', ec - sc + 1); + resp = (char_u *)getcmdline_prompt(NUL, prompt, 0, EXPAND_NOTHING, + NULL, CALLBACK_NONE); + msg_putchar('\n'); + xfree(prompt); if (resp != NULL) { typed = *resp; xfree(resp); @@ -3823,8 +3903,9 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, redraw_later(curwin, SOME_VALID); curwin->w_p_fen = save_p_fen; - if (msg_row == Rows - 1) - msg_didout = FALSE; /* avoid a scroll-up */ + if (msg_row == Rows - 1) { + msg_didout = false; // avoid a scroll-up + } msg_starthere(); i = msg_scroll; msg_scroll = 0; /* truncate msg when @@ -3835,7 +3916,7 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, _("replace with %s (y/n/a/q/l/^E/^Y)?"), sub); msg_no_more = FALSE; msg_scroll = i; - showruler(TRUE); + showruler(true); ui_cursor_goto(msg_row, msg_col); RedrawingDisabled = temp; @@ -3843,8 +3924,8 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, typed = plain_vgetc(); no_mapping--; - /* clear the question */ - msg_didout = FALSE; /* don't scroll up */ + // clear the question + msg_didout = false; // don't scroll up msg_col = 0; gotocmdline(true); p_lz = save_p_lz; @@ -3860,10 +3941,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; @@ -3874,15 +3957,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 @@ -3896,8 +3981,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 @@ -3913,28 +3999,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 @@ -3999,7 +4085,7 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, + copy_len + sublen + 1); // copy the text up to the part that matched - memmove(new_end, sub_firstline + copycol, (size_t)copy_len); + memmove(new_end, sub_firstline + copycol, copy_len); new_end += copy_len; // Finally, at this point we can know where the match actually will @@ -4147,8 +4233,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); } @@ -4174,7 +4261,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; @@ -4182,9 +4269,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 @@ -4193,23 +4281,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(); @@ -4252,7 +4341,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) { @@ -4260,7 +4349,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; @@ -4339,15 +4428,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: @@ -4358,27 +4445,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) { @@ -4417,13 +4508,13 @@ 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 *pat; + char_u delim; // delimiter, normally '/' + char_u *pat; regmmatch_T regmatch; int match; int which_pat; @@ -4437,17 +4528,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; @@ -4455,10 +4547,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) { @@ -4467,13 +4560,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) { @@ -4577,19 +4672,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. @@ -4608,8 +4701,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' @@ -4630,20 +4724,20 @@ prepare_tagpreview ( */ void ex_help(exarg_T *eap) { - char_u *arg; - char_u *tag; - FILE *helpfd; /* file descriptor of help file */ + char_u *arg; + char_u *tag; + FILE *helpfd; // file descriptor of help file int n; int i; - win_T *wp; + win_T *wp; int num_matches; - char_u **matches; - char_u *p; + char_u **matches; + char_u *p; int empty_fnum = 0; int alt_fnum = 0; - buf_T *buf; + buf_T *buf; int len; - char_u *lang; + char_u *lang; const bool old_KeyTyped = KeyTyped; if (eap != NULL) { @@ -4666,49 +4760,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); @@ -4717,8 +4819,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 { @@ -4746,13 +4847,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 @@ -4761,18 +4865,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; @@ -4789,9 +4894,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); @@ -4809,37 +4915,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 alpha-numeric 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; + 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 @@ -4874,8 +4980,8 @@ help_heuristic( */ static int help_compare(const void *s1, const void *s2) { - char *p1; - char *p2; + char *p1; + char *p2; p1 = *(char **)s1 + strlen(*(char **)s1) + 1; p2 = *(char **)s2 + strlen(*(char **)s2) + 1; @@ -4886,39 +4992,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*", "[*", "]*", - "/*", "/\\*", "\"*", "**", - "/\\(\\)", "/\\%(\\)", - "?", ":?", "?<CR>", "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*", "[*", "]*", + "/*", "/\\*", "\"*", "**", + "/\\(\\)", "/\\%(\\)", + "?", ":?", "?<CR>", "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", - "/\\\\(\\\\)", "/\\\\%(\\\\)", - "?", ":?", "?<CR>", "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", + "/\\\\(\\\\)", "/\\\\%(\\\\)", + "?", ":?", "?<CR>", "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! @@ -4951,7 +5056,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. @@ -4963,9 +5068,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 "\[:...:]" @@ -4974,12 +5080,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 @@ -4992,19 +5099,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; } @@ -5015,31 +5127,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" */ @@ -5071,16 +5188,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; } @@ -5099,10 +5216,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; } @@ -5154,14 +5272,14 @@ static void prepare_help_buffer(void) void fix_help_buffer(void) { linenr_T lnum; - char_u *line; + char_u *line; bool in_example = false; // Set filetype to "help". if (STRCMP(curbuf->b_p_ft, "help") != 0) { - curbuf_lock++; + curbuf->b_ro_locked++; set_option_value("ft", 0L, "help", OPT_LOCAL); - curbuf_lock--; + curbuf->b_ro_locked--; } if (!syntax_present(curwin)) { @@ -5169,23 +5287,23 @@ 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); + // blank-out a '<' in the first column + line = ml_get_buf(curbuf, lnum, true); line[0] = ' '; } in_example = false; } if (!in_example && len > 0) { if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' ')) { - /* blank-out a '>' in the last column (start of example) */ - line = ml_get_buf(curbuf, lnum, TRUE); + // blank-out a '>' in the last column (start of example) + line = ml_get_buf(curbuf, lnum, true); line[len - 1] = ' '; in_example = true; } else if (line[len - 1] == '~') { - /* blank-out a '~' at the end of line (header marker) */ - line = ml_get_buf(curbuf, lnum, TRUE); + // blank-out a '~' at the end of line (header marker) + line = ml_get_buf(curbuf, lnum, true); line[len - 1] = ' '; } } @@ -5202,12 +5320,12 @@ void fix_help_buffer(void) && ASCII_ISALPHA(fname[5]) && ASCII_ISALPHA(fname[6]) && TOLOWER_ASC(fname[7]) == 'x' - && 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) + && 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) { continue; + } /* Go through all directories in 'runtimepath', skipping * $VIMRUNTIME. */ @@ -5218,10 +5336,10 @@ void fix_help_buffer(void) if (rt != NULL && path_full_compare(rt, NameBuff, false, true) != kEqualFiles) { int fcount; - char_u **fnames; - char_u *s; + char_u **fnames; + char_u *s; vimconv_T vc; - char_u *cp; + char_u *cp; // Find all "doc/ *.txt" files in this directory. if (!add_pathsep((char *)NameBuff) @@ -5233,9 +5351,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. @@ -5292,8 +5410,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. @@ -5312,10 +5431,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; @@ -5372,15 +5490,15 @@ 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; int filecount; - char_u **files; - char_u *p1, *p2; - char_u *s; + char_u **files; + char_u *p1, *p2; + char_u *s; TriState utf8 = kNone; bool mix = false; // detected mixed encodings @@ -5395,9 +5513,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); @@ -5431,8 +5549,9 @@ static void helptags_one(char_u *dir, const char_u *ext, const char_u *tagfname, if (add_help_tags || path_full_compare((char_u *)"$VIMRUNTIME/doc", dir, false, true) == kEqualFiles) { - s = xmalloc(18 + STRLEN(tagfname)); - sprintf((char *)s, "help-tags\t%s\t1\n", tagfname); + size_t s_len = 18 + STRLEN(tagfname); + s = xmalloc(s_len); + snprintf((char *)s, s_len, "help-tags\t%s\t1\n", tagfname); GA_APPEND(char_u *, &ga, s); } @@ -5468,9 +5587,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; } @@ -5494,10 +5612,11 @@ static void helptags_one(char_u *dir, const char_u *ext, const char_u *tagfname, && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL || s[1] == '\0')) { *p2 = '\0'; - ++p1; - s = xmalloc((p2 - p1) + STRLEN(fname) + 2); + p1++; + size_t s_len= (p2 - p1) + STRLEN(fname) + 2; + s = xmalloc(s_len); GA_APPEND(char_u *, &ga, s); - sprintf((char *)s, "%s\t%s", p1, fname); + snprintf((char *)s, s_len, "%s\t%s", p1, fname); // find next '*' p2 = vim_strchr(p2 + 1, '*'); @@ -5525,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; @@ -5568,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; @@ -5590,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) { @@ -5608,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) { @@ -5643,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]; @@ -5662,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); } /* @@ -5674,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); @@ -5730,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; @@ -5881,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 @@ -5934,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; @@ -6005,7 +6125,7 @@ char_u *skip_vimgrep_pat(char_u *p, char_u **s, int *flags) /// List v:oldfiles in a nice way. void ex_oldfiles(exarg_T *eap) { - list_T *l = get_vim_var_list(VV_OLDFILES); + list_T *l = get_vim_var_list(VV_OLDFILES); long nr = 0; if (l == NULL) { |