aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/ex_cmds.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/ex_cmds.c')
-rw-r--r--src/nvim/ex_cmds.c2382
1 files changed, 1335 insertions, 1047 deletions
diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c
index 86f1a16216..e6e85a3f6c 100644
--- a/src/nvim/ex_cmds.c
+++ b/src/nvim/ex_cmds.c
@@ -8,7 +8,11 @@
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
+#include <math.h>
+#include "nvim/api/private/defs.h"
+#include "nvim/api/buffer.h"
+#include "nvim/log.h"
#include "nvim/vim.h"
#include "nvim/ascii.h"
#include "nvim/ex_cmds.h"
@@ -64,6 +68,33 @@
*/
typedef struct sign sign_T;
+/// Case matching style to use for :substitute
+typedef enum {
+ kSubHonorOptions = 0, ///< Honor the user's 'ignorecase'/'smartcase' options
+ kSubIgnoreCase, ///< Ignore case of the search
+ kSubMatchCase, ///< Match case of the search
+} SubIgnoreType;
+
+/// Flags kept between calls to :substitute.
+typedef struct {
+ bool do_all; ///< do multiple substitutions per line
+ bool do_ask; ///< ask for confirmation
+ bool do_count; ///< count only
+ bool do_error; ///< if false, ignore errors
+ bool do_print; ///< print last line with subs
+ bool do_list; ///< list last line with subs
+ bool do_number; ///< list last line with line nr
+ SubIgnoreType do_ic; ///< ignore case flag
+} subflags_T;
+
+/// Lines matched during :substitute.
+typedef struct {
+ linenr_T lnum;
+ long nmatch;
+ char_u *line;
+ kvec_t(colnr_T) cols; ///< columns of in-line matches
+} MatchedLine;
+typedef kvec_t(MatchedLine) MatchedLineVec;
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "ex_cmds.c.generated.h"
@@ -326,12 +357,12 @@ static int sort_compare(const void *s1, const void *s2)
// We need to copy one line into "sortbuf1", because there is no
// guarantee that the first pointer becomes invalid when obtaining the
// second one.
- STRNCPY(sortbuf1, ml_get(l1.lnum) + l1.st_u.line.start_col_nr,
- l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr + 1);
- sortbuf1[l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr] = 0;
- STRNCPY(sortbuf2, ml_get(l2.lnum) + l2.st_u.line.start_col_nr,
- l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr + 1);
- sortbuf2[l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr] = 0;
+ memcpy(sortbuf1, ml_get(l1.lnum) + l1.st_u.line.start_col_nr,
+ l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr + 1);
+ sortbuf1[l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr] = NUL;
+ memcpy(sortbuf2, ml_get(l2.lnum) + l2.st_u.line.start_col_nr,
+ l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr + 1);
+ sortbuf2[l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr] = NUL;
result = sort_ic ? STRICMP(sortbuf1, sortbuf2)
: STRCMP(sortbuf1, sortbuf2);
@@ -1136,15 +1167,11 @@ static void do_filter(
}
read_linecount = curbuf->b_ml.ml_line_count;
- /*
- * When call_shell() fails wait_return() is called to give the user a
- * chance to read the error messages. Otherwise errors are ignored, so you
- * can see the error messages from the command that appear on stdout; use
- * 'u' to fix the text
- * Switch to cooked mode when not redirecting stdin, avoids that something
- * like ":r !cat" hangs.
- * Pass on the kShellDoOut flag when the output is being redirected.
- */
+ // When call_shell() fails wait_return() is called to give the user a chance
+ // to read the error messages. Otherwise errors are ignored, so you can see
+ // the error messages from the command that appear on stdout; use 'u' to fix
+ // the text.
+ // Pass on the kShellDoOut flag when the output is being redirected.
if (call_shell(
cmd_buf,
kShellOptFilter | shell_flags,
@@ -1166,8 +1193,8 @@ static void do_filter(
if (do_out) {
if (otmp != NULL) {
- if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM,
- eap, READ_FILTER) == FAIL) {
+ if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM, eap,
+ READ_FILTER) != OK) {
if (!aborting()) {
msg_putchar('\n');
EMSG2(_(e_notread), otmp);
@@ -1377,36 +1404,34 @@ char_u *make_filter_cmd(char_u *cmd, char_u *itmp, char_u *otmp)
: "(%s)";
vim_snprintf(buf, len, fmt, (char *)cmd);
} else {
- strncpy(buf, (char *) cmd, len);
+ xstrlcpy(buf, (char *)cmd, len);
}
if (itmp != NULL) {
- strncat(buf, " < ", len);
- strncat(buf, (char *) itmp, len);
+ xstrlcat(buf, " < ", len - 1);
+ xstrlcat(buf, (const char *)itmp, len - 1);
}
#else
// For shells that don't understand braces around commands, at least allow
// the use of commands in a pipe.
- strncpy(buf, cmd, len);
+ xstrlcpy(buf, cmd, len);
if (itmp != NULL) {
- char_u *p;
-
// If there is a pipe, we have to put the '<' in front of it.
// Don't do this when 'shellquote' is not empty, otherwise the
// redirection would be inside the quotes.
if (*p_shq == NUL) {
- p = strchr(buf, '|');
+ char *const p = strchr(buf, '|');
if (p != NULL) {
*p = NUL;
}
}
- strncat(buf, " < ", len);
- strncat(buf, (char *) itmp, len);
+ xstrlcat(buf, " < ", len);
+ xstrlcat(buf, (const char *)itmp, len);
if (*p_shq == NUL) {
- p = strchr(cmd, '|');
+ const char *const p = strchr((const char *)cmd, '|');
if (p != NULL) {
- strncat(buf, " ", len); // Insert a space before the '|' for DOS
- strncat(buf, p, len);
+ xstrlcat(buf, " ", len - 1); // Insert a space before the '|' for DOS
+ xstrlcat(buf, p, len - 1);
}
}
}
@@ -1510,8 +1535,9 @@ int rename_buffer(char_u *new_fname)
curbuf->b_flags |= BF_NOTEDITED;
if (xfname != NULL && *xfname != NUL) {
buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0);
- if (buf != NULL && !cmdmod.keepalt)
+ if (buf != NULL && !cmdmod.keepalt) {
curwin->w_alt_fnum = buf->b_fnum;
+ }
}
xfree(fname);
xfree(sfname);
@@ -1583,6 +1609,7 @@ int do_write(exarg_T *eap)
int retval = FAIL;
char_u *free_fname = NULL;
buf_T *alt_buf = NULL;
+ int name_was_missing;
if (not_writing()) /* check 'write' option */
return FAIL;
@@ -1693,11 +1720,11 @@ int do_write(exarg_T *eap)
goto theend;
}
- /* If 'filetype' was empty try detecting it now. */
+ // If 'filetype' was empty try detecting it now.
if (*curbuf->b_p_ft == NUL) {
- if (au_has_group((char_u *)"filetypedetect"))
- (void)do_doautocmd((char_u *)"filetypedetect BufRead",
- TRUE);
+ if (au_has_group((char_u *)"filetypedetect")) {
+ (void)do_doautocmd((char_u *)"filetypedetect BufRead", true, NULL);
+ }
do_modelines(0);
}
@@ -1706,6 +1733,7 @@ int do_write(exarg_T *eap)
fname = curbuf->b_sfname;
}
+ name_was_missing = curbuf->b_ffname == NULL;
retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2,
eap, eap->append, eap->forceit, TRUE, FALSE);
@@ -1715,7 +1743,11 @@ int do_write(exarg_T *eap)
curbuf->b_p_ro = FALSE;
redraw_tabline = TRUE;
}
- /* Change directories when the 'acd' option is set. */
+ }
+
+ // Change directories when the 'acd' option is set and the file name
+ // got changed or set.
+ if (eap->cmdidx == CMD_saveas || name_was_missing) {
do_autochdir();
}
}
@@ -1745,14 +1777,14 @@ check_overwrite (
* write to other file or b_flags set or not writing the whole file:
* overwriting only allowed with '!'
*/
- if ( (other
- || (buf->b_flags & BF_NOTEDITED)
- || ((buf->b_flags & BF_NEW)
- && vim_strchr(p_cpo, CPO_OVERNEW) == NULL)
- || (buf->b_flags & BF_READERR))
- && !p_wa
- && !bt_nofile(buf)
- && os_file_exists(ffname)) {
+ if ((other
+ || (buf->b_flags & BF_NOTEDITED)
+ || ((buf->b_flags & BF_NEW)
+ && vim_strchr(p_cpo, CPO_OVERNEW) == NULL)
+ || (buf->b_flags & BF_READERR))
+ && !p_wa
+ && !bt_nofile(buf)
+ && os_path_exists(ffname)) {
if (!eap->forceit && !eap->append) {
#ifdef UNIX
// It is possible to open a directory on Unix.
@@ -1795,7 +1827,7 @@ check_overwrite (
}
swapname = makeswapname(fname, ffname, curbuf, dir);
xfree(dir);
- if (os_file_exists(swapname)) {
+ if (os_path_exists(swapname)) {
if (p_confirm || cmdmod.confirm) {
char_u buff[DIALOG_MSG_SIZE];
@@ -1909,7 +1941,7 @@ static int check_readonly(int *forceit, buf_T *buf)
/* Handle a file being readonly when the 'readonly' option is set or when
* the file exists and permissions are read-only. */
if (!*forceit && (buf->b_p_ro
- || (os_file_exists(buf->b_ffname)
+ || (os_path_exists(buf->b_ffname)
&& !os_file_is_writable((char *)buf->b_ffname)))) {
if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL) {
char_u buff[DIALOG_MSG_SIZE];
@@ -2005,37 +2037,34 @@ theend:
return retval;
}
-/*
- * start editing a new file
- *
- * fnum: file number; if zero use ffname/sfname
- * ffname: the file name
- * - full path if sfname used,
- * - any file name if sfname is NULL
- * - empty string to re-edit with the same file name (but may be
- * in a different directory)
- * - NULL to start an empty buffer
- * sfname: the short file name (or NULL)
- * eap: contains the command to be executed after loading the file and
- * forced 'ff' and 'fenc'
- * newlnum: if > 0: put cursor on this line number (if possible)
- * if ECMD_LASTL: use last position in loaded file
- * if ECMD_LAST: use last position in all files
- * if ECMD_ONE: use first line
- * flags:
- * ECMD_HIDE: if TRUE don't free the current buffer
- * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file
- * ECMD_OLDBUF: use existing buffer if it exists
- * ECMD_FORCEIT: ! used for Ex command
- * ECMD_ADDBUF: don't edit, just add to buffer list
- * oldwin: Should be "curwin" when editing a new buffer in the current
- * window, NULL when splitting the window first. When not NULL info
- * of the previous buffer for "oldwin" is stored.
- *
- * return FAIL for failure, OK otherwise
- */
-int
-do_ecmd (
+/// start editing a new file
+///
+/// @param fnum file number; if zero use ffname/sfname
+/// @param ffname the file name
+/// - full path if sfname used,
+/// - any file name if sfname is NULL
+/// - empty string to re-edit with the same file name (but may
+/// be in a different directory)
+/// - 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'
+/// @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
+/// ECMD_ONE: use first line
+/// @param flags ECMD_HIDE: if TRUE don't free the current buffer
+/// ECMD_SET_HELP: set b_help flag of (new) buffer before
+/// opening file
+/// ECMD_OLDBUF: use existing buffer if it exists
+/// ECMD_FORCEIT: ! used for Ex command
+/// ECMD_ADDBUF: don't edit, just add to buffer list
+/// @param oldwin Should be "curwin" when editing a new buffer in the current
+/// window, NULL when splitting the window first. When not NULL
+/// 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,
@@ -2064,6 +2093,7 @@ do_ecmd (
char_u *command = NULL;
int did_get_winopts = FALSE;
int readfile_flags = 0;
+ bool did_inc_redrawing_disabled = false;
if (eap != NULL)
command = eap->do_ecmd_cmd;
@@ -2101,6 +2131,14 @@ do_ecmd (
}
}
+ // Re-editing a terminal buffer: skip most buffer re-initialization.
+ if (!other_file && curbuf->terminal) {
+ check_arg_idx(curwin); // Needed when called from do_argfile().
+ maketitle(); // Title may show the arg index, e.g. "(2 of 5)".
+ retval = OK;
+ goto theend;
+ }
+
/*
* if the file was changed we may not be allowed to abandon it
* - if we are going to re-edit the same file
@@ -2151,9 +2189,9 @@ do_ecmd (
buflist_altfpos(oldwin);
}
- if (fnum)
+ if (fnum) {
buf = buflist_findnr(fnum);
- else {
+ } else {
if (flags & ECMD_ADDBUF) {
linenr_T tlnum = 1L;
@@ -2166,7 +2204,7 @@ do_ecmd (
goto theend;
}
buf = buflist_new(ffname, sfname, 0L,
- BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED));
+ BLN_CURBUF | (flags & ECMD_SET_HELP ? 0 : BLN_LISTED));
// Autocmds may change curwin and curbuf.
if (oldwin != NULL) {
oldwin = curwin;
@@ -2227,28 +2265,28 @@ do_ecmd (
xfree(new_name);
goto theend;
}
- if (buf == curbuf) /* already in new buffer */
- auto_buf = TRUE;
- else {
- if (curbuf == old_curbuf)
+ if (buf == curbuf) { // already in new buffer
+ auto_buf = true;
+ } else {
+ win_T *the_curwin = curwin;
+
+ // Set the w_closing flag to avoid that autocommands close the window.
+ the_curwin->w_closing = true;
+ if (curbuf == old_curbuf) {
buf_copy_options(buf, BCO_ENTER);
+ }
- /* close the link to the current buffer */
- u_sync(FALSE);
+ // Close the link to the current buffer. This will set
+ // curwin->w_buffer to NULL.
+ u_sync(false);
close_buffer(oldwin, curbuf,
- (flags & ECMD_HIDE) || curbuf->terminal ? 0 : DOBUF_UNLOAD, FALSE);
-
- /* Autocommands may open a new window and leave oldwin open
- * which leads to crashes since the above call sets
- * oldwin->w_buffer to NULL. */
- if (curwin != oldwin && oldwin != aucmd_win && win_valid(oldwin)) {
- assert(oldwin);
- if (oldwin->w_buffer == NULL) {
- win_close(oldwin, FALSE);
- }
- }
+ (flags & ECMD_HIDE) || curbuf->terminal ? 0 : DOBUF_UNLOAD,
+ false);
+
+ the_curwin->w_closing = false;
- if (aborting()) { /* autocmds may abort script processing */
+ // autocmds may abort script processing
+ if (aborting() && curwin->w_buffer != NULL) {
xfree(new_name);
goto theend;
}
@@ -2300,6 +2338,11 @@ do_ecmd (
oldbuf = (flags & ECMD_OLDBUF);
}
+ // Don't redraw until the cursor is in the right line, otherwise
+ // autocommands may cause ml_get errors.
+ RedrawingDisabled++;
+ did_inc_redrawing_disabled = true;
+
buf = curbuf;
if ((flags & ECMD_SET_HELP) || keep_help_flag) {
prepare_help_buffer();
@@ -2342,10 +2385,12 @@ do_ecmd (
if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur) {
/* Save all the text, so that the reload can be undone.
* 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)
- == FAIL)
+ u_sync(false);
+ if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, true)
+ == FAIL) {
+ xfree(new_name);
goto theend;
+ }
u_unchanged(curbuf);
buf_freeall(curbuf, BFA_KEEP_UNDO);
@@ -2376,8 +2421,6 @@ do_ecmd (
/*
* If we get here we are sure to start editing
*/
- /* don't redraw until the cursor is in the right line */
- ++RedrawingDisabled;
/* Assume success now */
retval = OK;
@@ -2529,7 +2572,8 @@ do_ecmd (
if (curbuf->b_kmap_state & KEYMAP_INIT)
(void)keymap_init();
- --RedrawingDisabled;
+ RedrawingDisabled--;
+ did_inc_redrawing_disabled = false;
if (!skip_redraw) {
n = p_so;
if (topline == 0 && command == NULL)
@@ -2548,8 +2592,12 @@ do_ecmd (
theend:
- if (did_set_swapcommand)
+ if (did_inc_redrawing_disabled) {
+ RedrawingDisabled--;
+ }
+ if (did_set_swapcommand) {
set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
+ }
xfree(free_fname);
return retval;
}
@@ -2588,7 +2636,7 @@ void ex_append(exarg_T *eap)
if (eap->cmdidx != CMD_append)
--lnum;
- /* when the buffer is empty append to line 0 and delete the dummy line */
+ // when the buffer is empty need to delete the dummy line
if (empty && lnum == 1)
lnum = 0;
@@ -2660,7 +2708,7 @@ void ex_append(exarg_T *eap)
did_undo = TRUE;
ml_append(lnum, theline, (colnr_T)0, FALSE);
- appended_lines_mark(lnum, 1L);
+ appended_lines_mark(lnum + (empty ? 1 : 0), 1L);
xfree(theline);
++lnum;
@@ -2901,52 +2949,204 @@ void sub_set_replacement(SubReplacementString sub)
old_sub = sub;
}
-/* do_sub()
- *
- * Perform a substitution from line eap->line1 to line eap->line2 using the
- * command pointed to by eap->arg which should be of the form:
- *
- * /pattern/substitution/{flags}
- *
- * The usual escapes are supported as described in the regexp docs.
- */
-void do_sub(exarg_T *eap)
+/// Recognize ":%s/\n//" and turn it into a join command, which is much
+/// more efficient.
+///
+/// @param[in] eap Ex arguments
+/// @param[in] pat Search pattern
+/// @param[in] sub Replacement string
+/// @param[in] cmd Command from :s_flags
+/// @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)
+ FUNC_ATTR_NONNULL_ARG(1, 3, 4)
+{
+ // TODO(vim): find a generic solution to make line-joining operations more
+ // efficient, avoid allocating a string that grows in size.
+ if (pat != NULL
+ && strcmp((const char *)pat, "\\n") == 0
+ && *sub == NUL
+ && (*cmd == NUL || (cmd[1] == NUL
+ && (*cmd == 'g'
+ || *cmd == 'l'
+ || *cmd == 'p'
+ || *cmd == '#')))) {
+ curwin->w_cursor.lnum = eap->line1;
+ if (*cmd == 'l') {
+ eap->flags = EXFLAG_LIST;
+ } else if (*cmd == '#') {
+ eap->flags = EXFLAG_NR;
+ } else if (*cmd == 'p') {
+ eap->flags = EXFLAG_PRINT;
+ }
+
+ // 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);
+ if (joined_lines_count > 1) {
+ do_join(joined_lines_count, FALSE, TRUE, FALSE, true);
+ sub_nsubs = joined_lines_count - 1;
+ sub_nlines = 1;
+ do_sub_msg(false);
+ ex_may_print(eap);
+ }
+
+ if (save) {
+ if (!cmdmod.keeppatterns) {
+ save_re_pat(RE_SUBST, pat, p_magic);
+ }
+ add_to_history(HIST_SEARCH, pat, true, NUL);
+ }
+
+ return true;
+ }
+
+ return false;
+}
+
+/// Allocate memory to store the replacement text for :substitute.
+///
+/// Slightly more memory that is strictly necessary is allocated to reduce the
+/// frequency of memory (re)allocation.
+///
+/// @param[in,out] new_start pointer to the memory for the replacement text
+/// @param[in] needed_len amount of memory needed
+///
+/// @returns pointer to the end of the allocated memory
+static char_u *sub_grow_buf(char_u **new_start, int needed_len)
+ FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_NONNULL_RET
+{
+ int new_start_len = 0;
+ char_u *new_end;
+ if (*new_start == NULL) {
+ // Get some space for a temporary buffer to do the
+ // substitution into (and some extra space to avoid
+ // too many calls to xmalloc()/free()).
+ new_start_len = needed_len + 50;
+ *new_start = xmalloc(new_start_len);
+ **new_start = NUL;
+ new_end = *new_start;
+ } else {
+ // Check if the temporary buffer is long enough to do the
+ // substitution into. If not, make it larger (with a bit
+ // extra to avoid too many calls to xmalloc()/free()).
+ size_t len = STRLEN(*new_start);
+ needed_len += len;
+ if (needed_len > new_start_len) {
+ new_start_len = needed_len + 50;
+ *new_start = xrealloc(*new_start, new_start_len);
+ }
+ new_end = *new_start + len;
+ }
+
+ return new_end;
+}
+
+/// Parse cmd string for :substitute's {flags} and update subflags accordingly
+///
+/// @param[in] cmd command string
+/// @param[in,out] subflags current flags defined for the :substitute command
+/// @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)
+ FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET
+{
+ // Find trailing options. When '&' is used, keep old options.
+ if (*cmd == '&') {
+ cmd++;
+ } else {
+ subflags->do_all = p_gd;
+ subflags->do_ask = false;
+ subflags->do_error = true;
+ subflags->do_print = false;
+ subflags->do_count = false;
+ subflags->do_number = false;
+ subflags->do_ic = kSubHonorOptions;
+ }
+ while (*cmd) {
+ // Note that 'g' and 'c' are always inverted.
+ // 'r' is never inverted.
+ if (*cmd == 'g') {
+ subflags->do_all = !subflags->do_all;
+ } else if (*cmd == 'c') {
+ subflags->do_ask = !subflags->do_ask;
+ } else if (*cmd == 'n') {
+ subflags->do_count = true;
+ } else if (*cmd == 'e') {
+ subflags->do_error = !subflags->do_error;
+ } else if (*cmd == 'r') { // use last used regexp
+ *which_pat = RE_LAST;
+ } else if (*cmd == 'p') {
+ subflags->do_print = true;
+ } else if (*cmd == '#') {
+ subflags->do_print = true;
+ subflags->do_number = true;
+ } else if (*cmd == 'l') {
+ subflags->do_print = true;
+ subflags->do_list = true;
+ } else if (*cmd == 'i') { // ignore case
+ subflags->do_ic = kSubIgnoreCase;
+ } else if (*cmd == 'I') { // don't ignore case
+ subflags->do_ic = kSubMatchCase;
+ } else {
+ break;
+ }
+ cmd++;
+ }
+ if (subflags->do_count) {
+ subflags->do_ask = false;
+ }
+
+ return cmd;
+}
+
+/// Perform a substitution from line eap->line1 to line eap->line2 using the
+/// command pointed to by eap->arg which should be of the form:
+///
+/// /pattern/substitution/{flags}
+///
+/// The usual escapes are supported as described in the regexp docs.
+///
+/// @return buffer used for 'inccommand' preview
+static buf_T *do_sub(exarg_T *eap, proftime_T timeout)
{
- linenr_T lnum;
long i = 0;
regmmatch_T regmatch;
- static int do_all = FALSE; /* do multiple substitutions per line */
- static int do_ask = FALSE; /* ask for confirmation */
- static bool do_count = false; /* count only */
- static int do_error = TRUE; /* if false, ignore errors */
- static int do_print = FALSE; /* print last line with subs. */
- static int do_list = FALSE; /* list last line with subs. */
- static int do_number = FALSE; /* list last line with line nr*/
- static int do_ic = 0; /* ignore case flag */
- int save_do_all; // remember user specified 'g' flag
- int save_do_ask; // remember user specified 'c' flag
- char_u *pat = NULL, *sub = NULL; /* init for GCC */
+ static subflags_T subflags = {
+ .do_all = false,
+ .do_ask = false,
+ .do_count = false,
+ .do_error = true,
+ .do_print = false,
+ .do_list = false,
+ .do_number = false,
+ .do_ic = kSubHonorOptions
+ };
+ char_u *pat = NULL, *sub = NULL; // init for GCC
int delimiter;
+ bool has_second_delim = false;
int sublen;
- int got_quit = FALSE;
- int got_match = FALSE;
- int temp;
+ int got_quit = false;
+ int got_match = false;
int which_pat;
- char_u *cmd;
- int save_State;
- linenr_T first_line = 0; /* first changed line */
- linenr_T last_line= 0; /* below last changed line AFTER the
- * change */
+ char_u *cmd = eap->arg;
+ linenr_T first_line = 0; // first changed line
+ linenr_T last_line= 0; // below last changed line AFTER the change
linenr_T old_line_count = curbuf->b_ml.ml_line_count;
- linenr_T line2;
- long nmatch; /* number of lines in match */
- char_u *sub_firstline; /* allocated copy of first sub line */
- int endcolumn = FALSE; /* cursor in last column when done */
+ char_u *sub_firstline; // allocated copy of first sub line
+ bool endcolumn = false; // cursor in last column when done
+ MatchedLineVec matched_lines = KV_INITIAL_VALUE;
pos_T old_cursor = curwin->w_cursor;
int start_nsubs;
int save_ma = 0;
+ int save_b_changed = curbuf->b_changed;
+ bool preview = (State & CMDPREVIEW);
- cmd = eap->arg;
if (!global_busy) {
sub_nsubs = 0;
sub_nlines = 0;
@@ -2964,7 +3164,7 @@ void do_sub(exarg_T *eap)
/* don't accept alphanumeric for separator */
if (isalpha(*cmd)) {
EMSG(_("E146: Regular expressions can't be delimited by letters"));
- return;
+ return NULL;
}
/*
* undocumented vi feature:
@@ -2975,21 +3175,26 @@ void do_sub(exarg_T *eap)
++cmd;
if (vim_strchr((char_u *)"/?&", *cmd) == NULL) {
EMSG(_(e_backslash));
- return;
+ return NULL;
}
- if (*cmd != '&')
- which_pat = RE_SEARCH; /* use last '/' pattern */
- pat = (char_u *)""; /* empty search pattern */
- delimiter = *cmd++; /* remember delimiter character */
- } else { /* find the end of the regexp */
- if (p_altkeymap && curwin->w_p_rl)
+ if (*cmd != '&') {
+ which_pat = RE_SEARCH; // use last '/' pattern
+ }
+ pat = (char_u *)""; // empty search pattern
+ delimiter = *cmd++; // remember delimiter character
+ has_second_delim = true;
+ } else { // find the end of the regexp
+ if (p_altkeymap && curwin->w_p_rl) {
lrF_sub(cmd);
- which_pat = RE_LAST; /* use last used regexp */
- delimiter = *cmd++; /* remember delimiter character */
- pat = cmd; /* remember start of search pat */
+ }
+ which_pat = RE_LAST; // use last used regexp
+ delimiter = *cmd++; // remember delimiter character
+ pat = cmd; // remember start of search pat
cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg);
- 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
+ has_second_delim = true;
+ }
}
/*
@@ -3008,7 +3213,7 @@ void do_sub(exarg_T *eap)
mb_ptr_adv(cmd);
}
- if (!eap->skip) {
+ if (!eap->skip && !preview) {
sub_set_replacement((SubReplacementString) {
.sub = xstrdup((char *) sub),
.timestamp = os_time(),
@@ -3018,7 +3223,7 @@ void do_sub(exarg_T *eap)
} else if (!eap->skip) { /* use previous pattern and substitution */
if (old_sub.sub == NULL) { /* there is no previous command */
EMSG(_(e_nopresub));
- return;
+ return NULL;
}
pat = NULL; /* search_regcomp() will use previous pattern */
sub = (char_u *) old_sub.sub;
@@ -3028,106 +3233,22 @@ void do_sub(exarg_T *eap)
endcolumn = (curwin->w_curswant == MAXCOL);
}
- // Recognize ":%s/\n//" and turn it into a join command, which is much
- // more efficient.
- // TODO: find a generic solution to make line-joining operations more
- // efficient, avoid allocating a string that grows in size.
- if (pat != NULL
- && strcmp((const char *)pat, "\\n") == 0
- && *sub == NUL
- && (*cmd == NUL || (cmd[1] == NUL
- && (*cmd == 'g'
- || *cmd == 'l'
- || *cmd == 'p'
- || *cmd == '#')))) {
- curwin->w_cursor.lnum = eap->line1;
- if (*cmd == 'l') {
- eap->flags = EXFLAG_LIST;
- } else if (*cmd == '#') {
- eap->flags = EXFLAG_NR;
- } else if (*cmd == 'p') {
- eap->flags = EXFLAG_PRINT;
- }
-
- // 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);
- if (joined_lines_count > 1) {
- do_join(joined_lines_count, FALSE, TRUE, FALSE, true);
- sub_nsubs = joined_lines_count - 1;
- sub_nlines = 1;
- do_sub_msg(false);
- ex_may_print(eap);
- }
-
- if (!cmdmod.keeppatterns) {
- save_re_pat(RE_SUBST, pat, p_magic);
- }
- add_to_history(HIST_SEARCH, pat, TRUE, NUL);
-
- return;
+ if (sub_joining_lines(eap, pat, sub, cmd, !preview)) {
+ return NULL;
}
- /*
- * Find trailing options. When '&' is used, keep old options.
- */
- if (*cmd == '&') {
- ++cmd;
- } else {
- // default is global on
- do_all = p_gd ? TRUE : FALSE;
-
- do_ask = FALSE;
- do_error = TRUE;
- do_print = FALSE;
- do_count = false;
- do_number = FALSE;
- do_ic = 0;
- }
- while (*cmd) {
- // Note that 'g' and 'c' are always inverted.
- // 'r' is never inverted.
- if (*cmd == 'g')
- do_all = !do_all;
- else if (*cmd == 'c')
- do_ask = !do_ask;
- else if (*cmd == 'n')
- do_count = true;
- else if (*cmd == 'e')
- do_error = !do_error;
- else if (*cmd == 'r') /* use last used regexp */
- which_pat = RE_LAST;
- else if (*cmd == 'p')
- do_print = TRUE;
- else if (*cmd == '#') {
- do_print = TRUE;
- do_number = TRUE;
- } else if (*cmd == 'l') {
- do_print = TRUE;
- do_list = TRUE;
- } else if (*cmd == 'i') /* ignore case */
- do_ic = 'i';
- else if (*cmd == 'I') /* don't ignore case */
- do_ic = 'I';
- else
- break;
- ++cmd;
- }
- if (do_count) {
- do_ask = FALSE;
- }
+ cmd = sub_parse_flags(cmd, &subflags, &which_pat);
- save_do_all = do_all;
- save_do_ask = do_ask;
+ bool save_do_all = subflags.do_all; // remember user specified 'g' flag
+ bool save_do_ask = subflags.do_ask; // remember user specified 'c' flag
// check for a trailing count
cmd = skipwhite(cmd);
if (ascii_isdigit(*cmd)) {
i = getdigits_long(&cmd);
- if (i <= 0 && !eap->skip && do_error) {
+ if (i <= 0 && !eap->skip && subflags.do_error) {
EMSG(_(e_zerocount));
- return;
+ return NULL;
}
eap->line1 = eap->line2;
eap->line2 += i - 1;
@@ -3143,31 +3264,34 @@ void do_sub(exarg_T *eap)
eap->nextcmd = check_nextcmd(cmd);
if (eap->nextcmd == NULL) {
EMSG(_(e_trailing));
- return;
+ return NULL;
}
}
- if (eap->skip) /* not executing commands, only parsing */
- return;
+ if (eap->skip) { // not executing commands, only parsing
+ return NULL;
+ }
- if (!do_count && !MODIFIABLE(curbuf)) {
- /* Substitution is not allowed in non-'modifiable' buffer */
+ if (!subflags.do_count && !MODIFIABLE(curbuf)) {
+ // Substitution is not allowed in non-'modifiable' buffer
EMSG(_(e_modifiable));
- return;
+ return NULL;
}
- if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS,
- &regmatch) == FAIL) {
- if (do_error)
+ if (search_regcomp(pat, RE_SUBST, which_pat, (preview ? 0 : SEARCH_HIS),
+ &regmatch) == FAIL) {
+ if (subflags.do_error) {
EMSG(_(e_invcmd));
- return;
+ }
+ return NULL;
}
- /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */
- if (do_ic == 'i')
- regmatch.rmm_ic = TRUE;
- else if (do_ic == 'I')
- regmatch.rmm_ic = FALSE;
+ // the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase'
+ if (subflags.do_ic == kSubIgnoreCase) {
+ regmatch.rmm_ic = true;
+ } else if (subflags.do_ic == kSubMatchCase) {
+ regmatch.rmm_ic = false;
+ }
sub_firstline = NULL;
@@ -3179,29 +3303,25 @@ void do_sub(exarg_T *eap)
if (!(sub[0] == '\\' && sub[1] == '='))
sub = regtilde(sub, p_magic);
- /*
- * Check for a match on each line.
- */
- line2 = eap->line2;
- for (lnum = eap->line1; lnum <= line2 && !(got_quit
- || aborting()
- ); ++lnum) {
- nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
- (colnr_T)0, NULL);
+ // Check for a match on each line.
+ linenr_T line2 = eap->line2;
+ for (linenr_T lnum = eap->line1;
+ lnum <= line2 && !(got_quit || aborting());
+ lnum++) {
+ long nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
+ (colnr_T)0, NULL);
if (nmatch) {
colnr_T copycol;
colnr_T matchcol;
colnr_T prev_matchcol = MAXCOL;
char_u *new_end, *new_start = NULL;
- unsigned new_start_len = 0;
char_u *p1;
int did_sub = FALSE;
int lastone;
- int len, copy_len, needed_len;
- long nmatch_tl = 0; /* nr of lines matched below lnum */
- int do_again; /* do it again after joining lines */
- int skip_match = FALSE;
- linenr_T sub_firstlnum; /* nr of first sub line */
+ long nmatch_tl = 0; // nr of lines matched below lnum
+ int do_again; // do it again after joining lines
+ int skip_match = false;
+ linenr_T sub_firstlnum; // nr of first sub line
/*
* The new text is build up step by step, to avoid too much
@@ -3241,8 +3361,7 @@ void do_sub(exarg_T *eap)
* accordingly.
*
* The new text is built up in new_start[]. It has some extra
- * room to avoid using xmalloc()/free() too often. new_start_len is
- * the length of the allocated memory at new_start.
+ * room to avoid using xmalloc()/free() too often.
*
* Make a copy of the old line, so it won't be taken away when
* updating the screen or handling a multi-line match. The "old_"
@@ -3251,6 +3370,8 @@ void do_sub(exarg_T *eap)
sub_firstlnum = lnum;
copycol = 0;
matchcol = 0;
+ // the current match
+ MatchedLine matched_line = { 0, 0, NULL, KV_INITIAL_VALUE };
/* At first match, remember current cursor position. */
if (!got_match) {
@@ -3261,9 +3382,9 @@ void do_sub(exarg_T *eap)
/*
* Loop until nothing more to replace in this line.
* 1. Handle match with empty string.
- * 2. If do_ask is set, ask for confirmation.
+ * 2. If subflags.do_ask is set, ask for confirmation.
* 3. substitute the string.
- * 4. if do_all is set, find next match
+ * 4. if subflags.do_all is set, find next match
* 5. break if there isn't another match in this line
*/
for (;; ) {
@@ -3287,6 +3408,12 @@ void do_sub(exarg_T *eap)
curwin->w_cursor.lnum = lnum;
do_again = FALSE;
+ if (preview) {
+ // Increment the in-line match count and store the column.
+ matched_line.nmatch++;
+ kv_push(matched_line.cols, regmatch.startpos[0].col);
+ }
+
/*
* 1. Match empty string does not count, except for first
* match. This reproduces the strange vi behaviour.
@@ -3314,15 +3441,13 @@ void do_sub(exarg_T *eap)
matchcol = regmatch.endpos[0].col;
prev_matchcol = matchcol;
- /*
- * 2. If do_count is set only increase the counter.
- * If do_ask is set, ask for confirmation.
- */
- if (do_count) {
- /* For a multi-line match, put matchcol at the NUL at
- * the end of the line and set nmatch to one, so that
- * we continue looking for a match on the next line.
- * Avoids that ":s/\nB\@=//gc" get stuck. */
+ // 2. If subflags.do_count is set only increase the counter.
+ // If do_ask is set, ask for confirmation.
+ if (subflags.do_count) {
+ // For a multi-line match, put matchcol at the NUL at
+ // the end of the line and set nmatch to one, so that
+ // we continue looking for a match on the next line.
+ // Avoids that ":s/\nB\@=//gc" get stuck.
if (nmatch > 1) {
matchcol = (colnr_T)STRLEN(sub_firstline);
nmatch = 1;
@@ -3336,12 +3461,12 @@ void do_sub(exarg_T *eap)
goto skip;
}
- if (do_ask) {
+ if (subflags.do_ask && !preview) {
int typed = 0;
/* change State to CONFIRM, so that the mouse works
* properly */
- save_State = State;
+ int save_State = State;
State = CONFIRM;
setmouse(); /* disable mouse in xterm */
curwin->w_cursor.col = regmatch.startpos[0].col;
@@ -3354,17 +3479,17 @@ void do_sub(exarg_T *eap)
/*
* Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed.
*/
- while (do_ask) {
+ while (subflags.do_ask) {
if (exmode_active) {
char_u *resp;
colnr_T sc, ec;
- print_line_no_prefix(lnum, do_number, do_list);
+ print_line_no_prefix(lnum, subflags.do_number, subflags.do_list);
getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL);
curwin->w_cursor.col = regmatch.endpos[0].col - 1;
getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec);
- if (do_number || curwin->w_p_nu) {
+ if (subflags.do_number || curwin->w_p_nu) {
int numw = number_width(curwin) + 1;
sc += numw;
ec += numw;
@@ -3388,7 +3513,7 @@ void do_sub(exarg_T *eap)
curwin->w_p_fen = FALSE;
/* Invert the matched string.
* Remove the inversion afterwards. */
- temp = RedrawingDisabled;
+ int temp = RedrawingDisabled;
RedrawingDisabled = 0;
if (new_start != NULL) {
@@ -3460,7 +3585,7 @@ void do_sub(exarg_T *eap)
|| typed == intr_char
#endif
) {
- got_quit = TRUE;
+ got_quit = true;
break;
}
if (typed == 'n')
@@ -3468,13 +3593,13 @@ void do_sub(exarg_T *eap)
if (typed == 'y')
break;
if (typed == 'l') {
- /* last: replace and then stop */
- do_all = FALSE;
+ // last: replace and then stop
+ subflags.do_all = false;
line2 = lnum;
break;
}
if (typed == 'a') {
- do_ask = FALSE;
+ subflags.do_ask = false;
break;
}
if (typed == Ctrl_E)
@@ -3507,156 +3632,137 @@ void do_sub(exarg_T *eap)
* use "\=col("."). */
curwin->w_cursor.col = regmatch.startpos[0].col;
- /*
- * 3. substitute the string.
- */
- if (do_count) {
- /* prevent accidentally changing the buffer by a function */
- save_ma = curbuf->b_p_ma;
- curbuf->b_p_ma = FALSE;
- sandbox++;
- }
- /* get length of substitution part */
- sublen = vim_regsub_multi(&regmatch,
- sub_firstlnum - regmatch.startpos[0].lnum,
- sub, sub_firstline, FALSE, p_magic, TRUE);
- if (do_count) {
- curbuf->b_p_ma = save_ma;
- sandbox--;
- goto skip;
- }
+ // 3. Substitute the string. During 'inccommand' preview only do this if
+ // there is a replace pattern.
+ if (!preview || has_second_delim) {
+ if (subflags.do_count) {
+ // prevent accidentally changing the buffer by a function
+ save_ma = curbuf->b_p_ma;
+ curbuf->b_p_ma = false;
+ sandbox++;
+ }
+ // Save flags for recursion. They can change for e.g.
+ // :s/^/\=execute("s#^##gn")
+ subflags_T subflags_save = subflags;
+ // get length of substitution part
+ sublen = vim_regsub_multi(&regmatch,
+ sub_firstlnum - regmatch.startpos[0].lnum,
+ sub, sub_firstline, false, p_magic, true);
+ // Don't keep flags set by a recursive call
+ subflags = subflags_save;
+ if (subflags.do_count) {
+ curbuf->b_p_ma = save_ma;
+ if (sandbox > 0) {
+ sandbox--;
+ }
+ goto skip;
+ }
- /* When the match included the "$" of the last line it may
- * go beyond the last line of the buffer. */
- if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1) {
- nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1;
- skip_match = TRUE;
- }
+ // When the match included the "$" of the last line it may
+ // go beyond the last line of the buffer.
+ if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1) {
+ nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1;
+ skip_match = true;
+ }
- /* Need room for:
- * - result so far in new_start (not for first sub in line)
- * - original text up to match
- * - length of substituted part
- * - original text after match
- */
- if (nmatch == 1)
- p1 = sub_firstline;
- else {
- p1 = ml_get(sub_firstlnum + nmatch - 1);
- nmatch_tl += nmatch - 1;
- }
- copy_len = regmatch.startpos[0].col - copycol;
- needed_len = copy_len + ((unsigned)STRLEN(p1)
- - regmatch.endpos[0].col) + sublen + 1;
- if (new_start == NULL) {
- /*
- * Get some space for a temporary buffer to do the
- * substitution into (and some extra space to avoid
- * too many calls to xmalloc()/free()).
- */
- new_start_len = needed_len + 50;
- new_start = xmalloc(new_start_len);
- *new_start = NUL;
- new_end = new_start;
- } else {
- /*
- * Check if the temporary buffer is long enough to do the
- * substitution into. If not, make it larger (with a bit
- * extra to avoid too many calls to xmalloc()/free()).
- */
- len = (unsigned)STRLEN(new_start);
- needed_len += len;
- if (needed_len > (int)new_start_len) {
- new_start_len = needed_len + 50;
- new_start = xrealloc(new_start, new_start_len);
+ // Need room for:
+ // - result so far in new_start (not for first sub in line)
+ // - original text up to match
+ // - length of substituted part
+ // - original text after match
+ if (nmatch == 1) {
+ p1 = sub_firstline;
+ } else {
+ p1 = ml_get(sub_firstlnum + nmatch - 1);
+ nmatch_tl += nmatch - 1;
}
- new_end = new_start + len;
- }
+ size_t copy_len = regmatch.startpos[0].col - copycol;
+ new_end = sub_grow_buf(&new_start,
+ (STRLEN(p1) - regmatch.endpos[0].col)
+ + copy_len + sublen + 1);
+
+ // copy the text up to the part that matched
+ memmove(new_end, sub_firstline + copycol, (size_t)copy_len);
+ new_end += copy_len;
+
+ (void)vim_regsub_multi(&regmatch,
+ sub_firstlnum - regmatch.startpos[0].lnum,
+ sub, new_end, true, p_magic, true);
+ sub_nsubs++;
+ did_sub = true;
- /*
- * copy the text up to the part that matched
- */
- memmove(new_end, sub_firstline + copycol, (size_t)copy_len);
- new_end += copy_len;
-
- (void)vim_regsub_multi(&regmatch,
- sub_firstlnum - regmatch.startpos[0].lnum,
- sub, new_end, TRUE, p_magic, TRUE);
- sub_nsubs++;
- did_sub = TRUE;
-
- /* Move the cursor to the start of the line, to avoid that it
- * is beyond the end of the line after the substitution. */
- curwin->w_cursor.col = 0;
-
- /* 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
- do_all = FALSE;
- }
+ // Move the cursor to the start of the line, to avoid that it
+ // is beyond the end of the line after the substitution.
+ curwin->w_cursor.col = 0;
- /* Remember next character to be copied. */
- copycol = regmatch.endpos[0].col;
+ // 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;
- }
+ // Remember next character to be copied.
+ copycol = regmatch.endpos[0].col;
- /*
- * Now the trick is to replace CTRL-M chars with a real line
- * break. This would make it impossible to insert a CTRL-M in
- * the text. The line break can be avoided by preceding the
- * CTRL-M with a backslash. To be able to insert a backslash,
- * they must be doubled in the string and are halved here.
- * That is Vi compatible.
- */
- for (p1 = new_end; *p1; ++p1) {
- if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */
- STRMOVE(p1, p1 + 1);
- else if (*p1 == CAR) {
- if (u_inssub(lnum) == OK) { /* prepare for undo */
- *p1 = NUL; /* truncate up to the CR */
- ml_append(lnum - 1, new_start,
- (colnr_T)(p1 - new_start + 1), FALSE);
- mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
- if (do_ask)
- appended_lines(lnum - 1, 1L);
- else {
- if (first_line == 0)
- first_line = lnum;
- last_line = lnum + 1;
+ 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;
+ }
+
+ // Now the trick is to replace CTRL-M chars with a real line
+ // break. This would make it impossible to insert a CTRL-M in
+ // the text. The line break can be avoided by preceding the
+ // CTRL-M with a backslash. To be able to insert a backslash,
+ // they must be doubled in the string and are halved here.
+ // That is Vi compatible.
+ for (p1 = new_end; *p1; p1++) {
+ if (p1[0] == '\\' && p1[1] != NUL) { // remove backslash
+ STRMOVE(p1, p1 + 1);
+ } else if (*p1 == CAR) {
+ if (u_inssub(lnum) == OK) { // prepare for undo
+ *p1 = NUL; // truncate up to the CR
+ ml_append(lnum - 1, new_start,
+ (colnr_T)(p1 - new_start + 1), false);
+ mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
+ if (subflags.do_ask) {
+ appended_lines(lnum - 1, 1L);
+ } else {
+ if (first_line == 0) {
+ first_line = lnum;
+ }
+ last_line = lnum + 1;
+ }
+ // All line numbers increase.
+ sub_firstlnum++;
+ lnum++;
+ line2++;
+ // move the cursor to the new line, like Vi
+ curwin->w_cursor.lnum++;
+ // copy the rest
+ STRMOVE(new_start, p1 + 1);
+ p1 = new_start - 1;
}
- /* All line numbers increase. */
- ++sub_firstlnum;
- ++lnum;
- ++line2;
- /* move the cursor to the new line, like Vi */
- ++curwin->w_cursor.lnum;
- /* copy the rest */
- STRMOVE(new_start, p1 + 1);
- p1 = new_start - 1;
+ } else if (has_mbyte) {
+ p1 += (*mb_ptr2len)(p1) - 1;
}
- } else if (has_mbyte)
- p1 += (*mb_ptr2len)(p1) - 1;
+ }
}
- /*
- * 4. If do_all is set, find next match.
- * Prevent endless loop with patterns that match empty
- * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g.
- * But ":s/\n/#/" is OK.
- */
+ // 4. If subflags.do_all is set, find next match.
+ // Prevent endless loop with patterns that match empty
+ // strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g.
+ // But ":s/\n/#/" is OK.
skip:
/* We already know that we did the last subst when we are at
* the end of the line, except that a pattern like
@@ -3667,7 +3773,7 @@ skip:
|| got_int
|| got_quit
|| lnum > line2
- || !(do_all || do_again)
+ || !(subflags.do_all || do_again)
|| (sub_firstline[matchcol] == NUL && nmatch <= 1
&& !re_multiline(regmatch.regprog)));
nmatch = -1;
@@ -3717,21 +3823,23 @@ skip:
for (i = 0; i < nmatch_tl; ++i)
ml_delete(lnum, (int)FALSE);
mark_adjust(lnum, lnum + nmatch_tl - 1,
- (long)MAXLNUM, -nmatch_tl);
- if (do_ask)
+ (long)MAXLNUM, -nmatch_tl);
+ if (subflags.do_ask) {
deleted_lines(lnum, nmatch_tl);
- --lnum;
- line2 -= nmatch_tl; /* nr of lines decreases */
+ }
+ lnum--;
+ line2 -= nmatch_tl; // nr of lines decreases
nmatch_tl = 0;
}
/* When asking, undo is saved each time, must also set
* changed flag each time. */
- if (do_ask)
+ if (subflags.do_ask) {
changed_bytes(lnum, 0);
- else {
- if (first_line == 0)
+ } else {
+ if (first_line == 0) {
first_line = lnum;
+ }
last_line = lnum + 1;
}
@@ -3769,9 +3877,19 @@ skip:
xfree(new_start); /* for when substitute was cancelled */
xfree(sub_firstline); /* free the copy of the original line */
sub_firstline = NULL;
+
+ if (preview) {
+ matched_line.lnum = lnum;
+ matched_line.line = vim_strsave(ml_get(lnum));
+ kv_push(matched_lines, matched_line);
+ }
}
line_breakcheck();
+
+ if (profile_passed_limit(timeout)) {
+ got_quit = true;
+ }
}
if (first_line != 0) {
@@ -3784,9 +3902,10 @@ skip:
xfree(sub_firstline); /* may have to free allocated copy of the line */
- /* ":s/pat//n" doesn't move the cursor */
- if (do_count)
+ // ":s/pat//n" doesn't move the cursor
+ if (subflags.do_count) {
curwin->w_cursor = old_cursor;
+ }
if (sub_nsubs > start_nsubs) {
/* Set the '[ and '] marks. */
@@ -3795,28 +3914,37 @@ skip:
curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
if (!global_busy) {
- if (!do_ask) { /* when interactive leave cursor on the match */
- if (endcolumn)
+ // when interactive leave cursor on the match
+ if (!subflags.do_ask) {
+ if (endcolumn) {
coladvance((colnr_T)MAXCOL);
- else
+ } else {
beginline(BL_WHITE | BL_FIX);
+ }
}
- if (!do_sub_msg(do_count) && do_ask)
+ if (!preview && !do_sub_msg(subflags.do_count) && subflags.do_ask) {
MSG("");
- } else
- global_need_beginline = TRUE;
- if (do_print)
- print_line(curwin->w_cursor.lnum, do_number, do_list);
+ }
+ } else {
+ global_need_beginline = true;
+ }
+ if (subflags.do_print) {
+ print_line(curwin->w_cursor.lnum, subflags.do_number, subflags.do_list);
+ }
} else if (!global_busy) {
- if (got_int) /* interrupted */
+ if (got_int) {
+ // interrupted
EMSG(_(e_interr));
- else if (got_match) /* did find something but nothing substituted */
+ } else if (got_match) {
+ // did find something but nothing substituted
MSG("");
- else if (do_error) /* nothing found */
+ } else if (subflags.do_error) {
+ // nothing found
EMSG2(_(e_patnotf2), get_search_pat());
+ }
}
- if (do_ask && hasAnyFolding(curwin)) {
+ if (subflags.do_ask && hasAnyFolding(curwin)) {
// Cursor position may require updating
changed_window_setting();
}
@@ -3824,9 +3952,30 @@ skip:
vim_regfree(regmatch.regprog);
// Restore the flag values, they can be used for ":&&".
- do_all = save_do_all;
- do_ask = save_do_ask;
-}
+ subflags.do_all = save_do_all;
+ subflags.do_ask = save_do_ask;
+
+ // Show 'inccommand' preview if there are matched lines.
+ buf_T *preview_buf = NULL;
+ if (preview && !aborting()) {
+ if (got_quit) { // Substitution is too slow, disable 'inccommand'.
+ set_string_option_direct((char_u *)"icm", -1, (char_u *)"", OPT_FREE,
+ SID_NONE);
+ } else if (*p_icm != NUL && matched_lines.size != 0 && pat != NULL) {
+ curbuf->b_changed = save_b_changed; // preserve 'modified' during preview
+ preview_buf = show_sub(eap, old_cursor, pat, sub, &matched_lines);
+ }
+ }
+
+ for (MatchedLine m; kv_size(matched_lines);) {
+ m = kv_pop(matched_lines);
+ xfree(m.line);
+ kv_destroy(m.cols);
+ }
+ kv_destroy(matched_lines);
+
+ return preview_buf;
+} // NOLINT(readability/fn_size)
/*
* Give message for number of substitutions.
@@ -3982,69 +4131,72 @@ void ex_global(exarg_T *eap)
smsg(_("Pattern not found: %s"), pat);
}
} else {
- start_global_changes();
global_exe(cmd);
- end_global_changes();
}
ml_clearmarked(); /* clear rest of the marks */
vim_regfree(regmatch.regprog);
}
-/*
- * Execute "cmd" on lines marked with ml_setmarked().
- */
+/// Execute `cmd` on lines marked with ml_setmarked().
void global_exe(char_u *cmd)
{
- linenr_T old_lcount; /* b_ml.ml_line_count before the command */
- buf_T *old_buf = curbuf; /* remember what buffer we started in */
- linenr_T lnum; /* line number according to old situation */
-
- /*
- * Set current position only once for a global command.
- * If global_busy is set, setpcmark() will not do anything.
- * If there is an error, global_busy will be incremented.
- */
+ linenr_T old_lcount; // b_ml.ml_line_count before the command
+ buf_T *old_buf = curbuf; // remember what buffer we started in
+ linenr_T lnum; // line number according to old situation
+ int save_mapped_ctrl_c = mapped_ctrl_c;
+
+ // Set current position only once for a global command.
+ // If global_busy is set, setpcmark() will not do anything.
+ // If there is an error, global_busy will be incremented.
setpcmark();
- /* When the command writes a message, don't overwrite the command. */
- msg_didout = TRUE;
+ // When the command writes a message, don't overwrite the command.
+ msg_didout = true;
+ // Disable CTRL-C mapping, let it interrupt (potentially long output).
+ mapped_ctrl_c = 0;
sub_nsubs = 0;
sub_nlines = 0;
- global_need_beginline = FALSE;
+ global_need_beginline = false;
global_busy = 1;
old_lcount = curbuf->b_ml.ml_line_count;
+
while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1) {
curwin->w_cursor.lnum = lnum;
curwin->w_cursor.col = 0;
- if (*cmd == NUL || *cmd == '\n')
+ if (*cmd == NUL || *cmd == '\n') {
do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT);
- else
+ } else {
do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT);
+ }
os_breakcheck();
}
+ mapped_ctrl_c = save_mapped_ctrl_c;
global_busy = 0;
- if (global_need_beginline)
+ if (global_need_beginline) {
beginline(BL_WHITE | BL_FIX);
- else
- check_cursor(); /* cursor may be beyond the end of the line */
+ } else {
+ check_cursor(); // cursor may be beyond the end of the line
+ }
- /* the cursor may not have moved in the text but a change in a previous
- * line may move it on the screen */
+ // the cursor may not have moved in the text but a change in a previous
+ // line may move it on the screen
changed_line_abv_curs();
- /* If it looks like no message was written, allow overwriting the
- * command with the report for number of changes. */
- if (msg_col == 0 && msg_scrolled == 0)
- msg_didout = FALSE;
+ // If it looks like no message was written, allow overwriting the
+ // command with the report for number of changes.
+ if (msg_col == 0 && msg_scrolled == 0) {
+ msg_didout = false;
+ }
- /* If substitutes done, report number of substitutes, otherwise report
- * number of extra or deleted lines.
- * Don't report extra or deleted lines in the edge case where the buffer
- * we are in after execution is different from the buffer we started in. */
- if (!do_sub_msg(false) && curbuf == old_buf)
+ // If substitutes done, report number of substitutes, otherwise report
+ // number of extra or deleted lines.
+ // Don't report extra or deleted lines in the edge case where the buffer
+ // we are in after execution is different from the buffer we started in.
+ if (!do_sub_msg(false) && curbuf == old_buf) {
msgmore(curbuf->b_ml.ml_line_count - old_lcount);
+ }
}
#if defined(EXITFREE)
@@ -4494,12 +4646,15 @@ int find_help_tags(char_u *arg, int *num_matches, char_u ***matches, int keep_la
break;
}
- /*
- * If tag starts with ', toss everything after a second '. Fixes
- * CTRL-] on 'option'. (would include the trailing '.').
- */
- if (*s == '\'' && s > arg && *arg == '\'')
+ // If tag starts with ', toss everything after a second '. Fixes
+ // CTRL-] on 'option'. (would include the trailing '.').
+ if (*s == '\'' && s > arg && *arg == '\'') {
+ break;
+ }
+ // Also '{' and '}'. Fixes CTRL-] on '{address}'.
+ if (*s == '}' && s > arg && *arg == '{') {
break;
+ }
}
*d = NUL;
@@ -4657,9 +4812,13 @@ void fix_help_buffer(void)
vimconv_T vc;
char_u *cp;
- /* Find all "doc/ *.txt" files in this directory. */
- add_pathsep((char *)NameBuff);
- STRCAT(NameBuff, "doc/*.??[tx]");
+ // Find all "doc/ *.txt" files in this directory.
+ if (!add_pathsep((char *)NameBuff)
+ || STRLCAT(NameBuff, "doc/*.??[tx]",
+ sizeof(NameBuff)) >= MAXPATHL) {
+ EMSG(_(e_fnametoolong));
+ continue;
+ }
// Note: We cannot just do `&NameBuff` because it is a statically sized array
// so `NameBuff == &NameBuff` according to C semantics.
@@ -4796,122 +4955,15 @@ void ex_viusage(exarg_T *eap)
}
-/*
- * ":helptags"
- */
-void ex_helptags(exarg_T *eap)
-{
- garray_T ga;
- int len;
- char_u lang[2];
- expand_T xpc;
- char_u *dirname;
- char_u ext[5];
- char_u fname[8];
- int filecount;
- char_u **files;
- int add_help_tags = FALSE;
-
- /* 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);
- }
-
- ExpandInit(&xpc);
- xpc.xp_context = EXPAND_DIRECTORIES;
- dirname = ExpandOne(&xpc, eap->arg, NULL,
- WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE);
- if (dirname == NULL || !os_isdir(dirname)) {
- EMSG2(_("E150: Not a directory: %s"), eap->arg);
- xfree(dirname);
- return;
- }
-
- /* Get a list of all files in the help directory and in subdirectories. */
- STRCPY(NameBuff, dirname);
- add_pathsep((char *)NameBuff);
- STRCAT(NameBuff, "**");
-
- // 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};
- if (gen_expand_wildcards(1, buff_list, &filecount, &files,
- EW_FILE|EW_SILENT) == FAIL
- || filecount == 0) {
- EMSG2("E151: No match: %s", NameBuff);
- xfree(dirname);
- return;
- }
-
- /* Go over all files in the directory to find out what languages are
- * present. */
- ga_init(&ga, 1, 10);
- for (int i = 0; i < filecount; ++i) {
- len = (int)STRLEN(files[i]);
- if (len <= 4) {
- continue;
- }
- if (STRICMP(files[i] + len - 4, ".txt") == 0) {
- /* ".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" */
- lang[0] = TOLOWER_ASC(files[i][len - 3]);
- lang[1] = TOLOWER_ASC(files[i][len - 2]);
- } else
- continue;
-
- int j;
- /* Did we find this language already? */
- for (j = 0; j < ga.ga_len; j += 2)
- if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0)
- break;
- if (j == ga.ga_len) {
- /* New language, add it. */
- ga_grow(&ga, 2);
- ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0];
- ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1];
- }
- }
-
- /*
- * Loop over the found languages to generate a tags file for each one.
- */
- for (int j = 0; j < ga.ga_len; j += 2) {
- STRCPY(fname, "tags-xx");
- 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". */
- fname[4] = NUL;
- STRCPY(ext, ".txt");
- } else {
- /* Language "ab" uses ".abx" and "tags-ab". */
- STRCPY(ext, ".xxx");
- ext[1] = fname[5];
- ext[2] = fname[6];
- }
- helptags_one(dirname, ext, fname, add_help_tags);
- }
-
- ga_clear(&ga);
- FreeWild(filecount, files);
-
- xfree(dirname);
-}
-
-static void
-helptags_one (
- char_u *dir, /* doc directory */
- char_u *ext, /* suffix, ".txt", ".itx", ".frx", etc. */
- char_u *tagfname, /* "tags" for English, "tags-fr" for French. */
- int add_help_tags /* add "help-tags" tag */
-)
+/// Generate tags in one help directory
+///
+/// @param dir Path to the doc directory
+/// @param ext Suffix of the help files (".txt", ".itx", ".frx", etc.)
+/// @param tagname Name of the tags file ("tags" for English, "tags-fr" for
+/// French)
+/// @param add_help_tags Whether to add the "help-tags" tag
+static void helptags_one(char_u *dir, char_u *ext, char_u *tagfname,
+ bool add_help_tags)
{
FILE *fd_tags;
FILE *fd;
@@ -4928,10 +4980,13 @@ helptags_one (
int mix = FALSE; /* detected mixed encodings */
// Find all *.txt files.
- size_t dirlen = STRLEN(dir);
- STRCPY(NameBuff, dir);
- STRCAT(NameBuff, "/**/*");
- STRCAT(NameBuff, ext);
+ size_t dirlen = STRLCPY(NameBuff, dir, sizeof(NameBuff));
+ if (dirlen >= MAXPATHL
+ || STRLCAT(NameBuff, "/**/*", sizeof(NameBuff)) >= MAXPATHL // NOLINT
+ || STRLCAT(NameBuff, ext, sizeof(NameBuff)) >= MAXPATHL) {
+ EMSG(_(e_fnametoolong));
+ return;
+ }
// Note: We cannot just do `&NameBuff` because it is a statically sized array
// so `NameBuff == &NameBuff` according to C semantics.
@@ -4944,13 +4999,16 @@ helptags_one (
return;
}
- /*
- * Open the tags file for writing.
- * Do this before scanning through all the files.
- */
- STRCPY(NameBuff, dir);
- add_pathsep((char *)NameBuff);
- STRNCAT(NameBuff, tagfname, sizeof(NameBuff) - dirlen - 2);
+ //
+ // Open the tags file for writing.
+ // Do this before scanning through all the files.
+ //
+ memcpy(NameBuff, dir, dirlen + 1);
+ if (!add_pathsep((char *)NameBuff)
+ || STRLCAT(NameBuff, tagfname, sizeof(NameBuff)) >= MAXPATHL) {
+ EMSG(_(e_fnametoolong));
+ return;
+ }
fd_tags = mch_fopen((char *)NameBuff, "w");
if (fd_tags == NULL) {
EMSG2(_("E152: Cannot open %s for writing"), NameBuff);
@@ -5111,6 +5169,136 @@ helptags_one (
fclose(fd_tags); /* there is no check for an error... */
}
+/// Generate tags in one help directory, taking care of translations.
+static void do_helptags(char_u *dirname, bool add_help_tags)
+{
+ int len;
+ garray_T ga;
+ char_u lang[2];
+ char_u ext[5];
+ char_u fname[8];
+ int filecount;
+ char_u **files;
+
+ // Get a list of all files in the help directory and in subdirectories.
+ STRLCPY(NameBuff, dirname, sizeof(NameBuff));
+ if (!add_pathsep((char *)NameBuff)
+ || STRLCAT(NameBuff, "**", sizeof(NameBuff)) >= MAXPATHL) {
+ EMSG(_(e_fnametoolong));
+ xfree(dirname);
+ return;
+ }
+
+ // 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};
+ if (gen_expand_wildcards(1, buff_list, &filecount, &files,
+ EW_FILE|EW_SILENT) == FAIL
+ || filecount == 0) {
+ EMSG2("E151: No match: %s", NameBuff);
+ xfree(dirname);
+ return;
+ }
+
+ /* Go over all files in the directory to find out what languages are
+ * present. */
+ int j;
+ ga_init(&ga, 1, 10);
+ for (int i = 0; i < filecount; i++) {
+ len = (int)STRLEN(files[i]);
+ if (len <= 4) {
+ continue;
+ }
+ if (STRICMP(files[i] + len - 4, ".txt") == 0) {
+ /* ".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" */
+ lang[0] = TOLOWER_ASC(files[i][len - 3]);
+ lang[1] = TOLOWER_ASC(files[i][len - 2]);
+ } else
+ continue;
+
+ // Did we find this language already?
+ for (j = 0; j < ga.ga_len; j += 2) {
+ if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0) {
+ break;
+ }
+ }
+ if (j == ga.ga_len) {
+ // New language, add it.
+ ga_grow(&ga, 2);
+ ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0];
+ ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1];
+ }
+ }
+
+ /*
+ * Loop over the found languages to generate a tags file for each one.
+ */
+ for (j = 0; j < ga.ga_len; j += 2) {
+ STRCPY(fname, "tags-xx");
+ 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". */
+ fname[4] = NUL;
+ STRCPY(ext, ".txt");
+ } else {
+ /* Language "ab" uses ".abx" and "tags-ab". */
+ STRCPY(ext, ".xxx");
+ ext[1] = fname[5];
+ ext[2] = fname[6];
+ }
+ helptags_one(dirname, ext, fname, add_help_tags);
+ }
+
+ ga_clear(&ga);
+ FreeWild(filecount, files);
+}
+
+ static void
+helptags_cb(char_u *fname, void *cookie)
+{
+ do_helptags(fname, *(bool *)cookie);
+}
+
+/*
+ * ":helptags"
+ */
+void ex_helptags(exarg_T *eap)
+{
+ expand_T xpc;
+ char_u *dirname;
+ bool add_help_tags = false;
+
+ /* 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);
+ }
+
+ if (STRCMP(eap->arg, "ALL") == 0) {
+ do_in_path(p_rtp, (char_u *)"doc", DIP_ALL + DIP_DIR,
+ helptags_cb, &add_help_tags);
+ } else {
+ ExpandInit(&xpc);
+ xpc.xp_context = EXPAND_DIRECTORIES;
+ dirname = ExpandOne(&xpc, eap->arg, NULL,
+ WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE);
+ if (dirname == NULL || !os_isdir(dirname)) {
+ EMSG2(_("E150: Not a directory: %s"), eap->arg);
+ } else {
+ do_helptags(dirname, add_help_tags);
+ }
+ xfree(dirname);
+ }
+}
+
struct sign
{
sign_T *sn_next; /* next sign in list */
@@ -5182,385 +5370,330 @@ static int sign_cmd_idx(
*/
void ex_sign(exarg_T *eap)
{
- char_u *arg = eap->arg;
- char_u *p;
- int idx;
- sign_T *sp;
- sign_T *sp_prev;
-
- /* Parse the subcommand. */
- p = skiptowhite(arg);
- idx = sign_cmd_idx(arg, p);
- if (idx == SIGNCMD_LAST)
- {
- EMSG2(_("E160: Unknown sign command: %s"), arg);
- return;
+ char_u *arg = eap->arg;
+ char_u *p;
+ int idx;
+ sign_T *sp;
+ sign_T *sp_prev;
+
+ // Parse the subcommand.
+ p = skiptowhite(arg);
+ idx = sign_cmd_idx(arg, p);
+ if (idx == SIGNCMD_LAST) {
+ EMSG2(_("E160: Unknown sign command: %s"), arg);
+ return;
+ }
+ arg = skipwhite(p);
+
+ if (idx <= SIGNCMD_LIST) {
+ // Define, undefine or list signs.
+ if (idx == SIGNCMD_LIST && *arg == NUL) {
+ // ":sign list": list all defined signs
+ for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next) {
+ sign_list_defined(sp);
+ }
+ } else if (*arg == NUL) {
+ EMSG(_("E156: Missing sign name"));
+ } else {
+ // Isolate the sign name. If it's a number skip leading zeroes,
+ // so that "099" and "99" are the same sign. But keep "0".
+ p = skiptowhite(arg);
+ if (*p != NUL) {
+ *p++ = NUL;
+ }
+ while (arg[0] == '0' && arg[1] != NUL) {
+ arg++;
+ }
+
+ sp_prev = NULL;
+ for (sp = first_sign; sp != NULL; sp = sp->sn_next) {
+ if (STRCMP(sp->sn_name, arg) == 0) {
+ break;
+ }
+ sp_prev = sp;
+ }
+ if (idx == SIGNCMD_DEFINE) {
+ // ":sign define {name} ...": define a sign
+ if (sp == NULL) {
+ sign_T *lp;
+ int start = next_sign_typenr;
+
+ // Allocate a new sign.
+ sp = xcalloc(1, sizeof(sign_T));
+
+ // Check that next_sign_typenr is not already being used.
+ // This only happens after wrapping around. Hopefully
+ // another one got deleted and we can use its number.
+ for (lp = first_sign; lp != NULL; ) {
+ if (lp->sn_typenr == next_sign_typenr) {
+ next_sign_typenr++;
+ if (next_sign_typenr == MAX_TYPENR) {
+ next_sign_typenr = 1;
+ }
+ if (next_sign_typenr == start) {
+ xfree(sp);
+ EMSG(_("E612: Too many signs defined"));
+ return;
+ }
+ lp = first_sign; // start all over
+ continue;
+ }
+ lp = lp->sn_next;
+ }
+
+ sp->sn_typenr = next_sign_typenr;
+ if (++next_sign_typenr == MAX_TYPENR) {
+ next_sign_typenr = 1; // wrap around
+ }
+
+ sp->sn_name = vim_strsave(arg);
+
+ // add the new sign to the list of signs
+ if (sp_prev == NULL) {
+ first_sign = sp;
+ } else {
+ sp_prev->sn_next = sp;
+ }
+ }
+
+ // set values for a defined sign.
+ for (;;) {
+ arg = skipwhite(p);
+ if (*arg == NUL) {
+ break;
+ }
+ p = skiptowhite_esc(arg);
+ if (STRNCMP(arg, "icon=", 5) == 0) {
+ arg += 5;
+ xfree(sp->sn_icon);
+ sp->sn_icon = vim_strnsave(arg, (int)(p - arg));
+ backslash_halve(sp->sn_icon);
+ } else if (STRNCMP(arg, "text=", 5) == 0) {
+ char_u *s;
+ int cells;
+ int len;
+
+ arg += 5;
+
+ // Count cells and check for non-printable chars
+ cells = 0;
+ for (s = arg; s < p; s += (*mb_ptr2len)(s)) {
+ if (!vim_isprintc((*mb_ptr2char)(s))) {
+ break;
+ }
+ cells += (*mb_ptr2cells)(s);
+ }
+ // Currently must be one or two display cells
+ if (s != p || cells < 1 || cells > 2) {
+ *p = NUL;
+ EMSG2(_("E239: Invalid sign text: %s"), arg);
+ return;
+ }
+
+ xfree(sp->sn_text);
+ // Allocate one byte more if we need to pad up
+ // with a space.
+ len = (int)(p - arg + ((cells == 1) ? 1 : 0));
+ sp->sn_text = vim_strnsave(arg, len);
+
+ if (cells == 1) {
+ STRCPY(sp->sn_text + len - 1, " ");
+ }
+ } else if (STRNCMP(arg, "linehl=", 7) == 0) {
+ arg += 7;
+ sp->sn_line_hl = syn_check_group(arg, (int)(p - arg));
+ } else if (STRNCMP(arg, "texthl=", 7) == 0) {
+ arg += 7;
+ sp->sn_text_hl = syn_check_group(arg, (int)(p - arg));
+ } else {
+ EMSG2(_(e_invarg2), arg);
+ return;
+ }
+ }
+ } else if (sp == NULL) {
+ EMSG2(_("E155: Unknown sign: %s"), arg);
+ } else if (idx == SIGNCMD_LIST) {
+ // ":sign list {name}"
+ sign_list_defined(sp);
+ } else {
+ // ":sign undefine {name}"
+ sign_undefine(sp, sp_prev);
+ }
+ }
+ } else {
+ int id = -1;
+ linenr_T lnum = -1;
+ char_u *sign_name = NULL;
+ char_u *arg1;
+
+ if (*arg == NUL) {
+ if (idx == SIGNCMD_PLACE) {
+ // ":sign place": list placed signs in all buffers
+ sign_list_placed(NULL);
+ } else if (idx == SIGNCMD_UNPLACE) {
+ // ":sign unplace": remove placed sign at cursor
+ id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum);
+ if (id > 0) {
+ buf_delsign(curwin->w_buffer, id);
+ update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum);
+ } else {
+ EMSG(_("E159: Missing sign number"));
+ }
+ } else {
+ EMSG(_(e_argreq));
+ }
+ return;
}
- arg = skipwhite(p);
- if (idx <= SIGNCMD_LIST)
- {
- /*
- * Define, undefine or list signs.
- */
- if (idx == SIGNCMD_LIST && *arg == NUL)
- {
- /* ":sign list": list all defined signs */
- for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next)
- sign_list_defined(sp);
- }
- else if (*arg == NUL)
- EMSG(_("E156: Missing sign name"));
- else
- {
- /* Isolate the sign name. If it's a number skip leading zeroes,
- * so that "099" and "99" are the same sign. But keep "0". */
- p = skiptowhite(arg);
- if (*p != NUL)
- *p++ = NUL;
- while (arg[0] == '0' && arg[1] != NUL)
- ++arg;
-
- sp_prev = NULL;
- for (sp = first_sign; sp != NULL; sp = sp->sn_next)
- {
- if (STRCMP(sp->sn_name, arg) == 0)
- break;
- sp_prev = sp;
- }
- if (idx == SIGNCMD_DEFINE)
- {
- /* ":sign define {name} ...": define a sign */
- if (sp == NULL)
- {
- sign_T *lp;
- int start = next_sign_typenr;
-
- /* Allocate a new sign. */
- sp = xcalloc(1, sizeof(sign_T));
-
- /* Check that next_sign_typenr is not already being used.
- * This only happens after wrapping around. Hopefully
- * another one got deleted and we can use its number. */
- for (lp = first_sign; lp != NULL; )
- {
- if (lp->sn_typenr == next_sign_typenr)
- {
- ++next_sign_typenr;
- if (next_sign_typenr == MAX_TYPENR)
- next_sign_typenr = 1;
- if (next_sign_typenr == start)
- {
- xfree(sp);
- EMSG(_("E612: Too many signs defined"));
- return;
- }
- lp = first_sign; /* start all over */
- continue;
- }
- lp = lp->sn_next;
- }
-
- sp->sn_typenr = next_sign_typenr;
- if (++next_sign_typenr == MAX_TYPENR)
- next_sign_typenr = 1; /* wrap around */
-
- sp->sn_name = vim_strsave(arg);
-
- /* add the new sign to the list of signs */
- if (sp_prev == NULL)
- first_sign = sp;
- else
- sp_prev->sn_next = sp;
- }
-
- /* set values for a defined sign. */
- for (;;)
- {
- arg = skipwhite(p);
- if (*arg == NUL)
- break;
- p = skiptowhite_esc(arg);
- if (STRNCMP(arg, "icon=", 5) == 0)
- {
- arg += 5;
- xfree(sp->sn_icon);
- sp->sn_icon = vim_strnsave(arg, (int)(p - arg));
- backslash_halve(sp->sn_icon);
- }
- else if (STRNCMP(arg, "text=", 5) == 0)
- {
- char_u *s;
- int cells;
- int len;
-
- arg += 5;
-
- /* Count cells and check for non-printable chars */
- if (has_mbyte)
- {
- cells = 0;
- for (s = arg; s < p; s += (*mb_ptr2len)(s))
- {
- if (!vim_isprintc((*mb_ptr2char)(s)))
- break;
- cells += (*mb_ptr2cells)(s);
- }
- }
- else
-
- {
- for (s = arg; s < p; ++s)
- if (!vim_isprintc(*s))
- break;
- cells = (int)(s - arg);
- }
- /* Currently must be one or two display cells */
- if (s != p || cells < 1 || cells > 2)
- {
- *p = NUL;
- EMSG2(_("E239: Invalid sign text: %s"), arg);
- return;
- }
-
- xfree(sp->sn_text);
- /* Allocate one byte more if we need to pad up
- * with a space. */
- len = (int)(p - arg + ((cells == 1) ? 1 : 0));
- sp->sn_text = vim_strnsave(arg, len);
-
- if (cells == 1)
- STRCPY(sp->sn_text + len - 1, " ");
- }
- else if (STRNCMP(arg, "linehl=", 7) == 0)
- {
- arg += 7;
- sp->sn_line_hl = syn_check_group(arg, (int)(p - arg));
- }
- else if (STRNCMP(arg, "texthl=", 7) == 0)
- {
- arg += 7;
- sp->sn_text_hl = syn_check_group(arg, (int)(p - arg));
- }
- else
- {
- EMSG2(_(e_invarg2), arg);
- return;
- }
- }
- }
- else if (sp == NULL)
- EMSG2(_("E155: Unknown sign: %s"), arg);
- else if (idx == SIGNCMD_LIST)
- /* ":sign list {name}" */
- sign_list_defined(sp);
- else
- /* ":sign undefine {name}" */
- sign_undefine(sp, sp_prev);
- }
+ if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL) {
+ // ":sign unplace *": remove all placed signs
+ buf_delete_all_signs();
+ return;
}
- else
- {
- int id = -1;
- linenr_T lnum = -1;
- char_u *sign_name = NULL;
- char_u *arg1;
-
- if (*arg == NUL)
- {
- if (idx == SIGNCMD_PLACE)
- {
- /* ":sign place": list placed signs in all buffers */
- sign_list_placed(NULL);
- }
- else if (idx == SIGNCMD_UNPLACE)
- {
- /* ":sign unplace": remove placed sign at cursor */
- id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum);
- if (id > 0)
- {
- buf_delsign(curwin->w_buffer, id);
- update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum);
- }
- else
- EMSG(_("E159: Missing sign number"));
- }
- else
- EMSG(_(e_argreq));
- return;
- }
-
- if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL)
- {
- /* ":sign unplace *": remove all placed signs */
- buf_delete_all_signs();
- return;
- }
-
- /* first arg could be placed sign id */
- arg1 = arg;
- if (ascii_isdigit(*arg))
- {
- id = getdigits_int(&arg);
- if (!ascii_iswhite(*arg) && *arg != NUL)
- {
- id = -1;
- arg = arg1;
- }
- else
- {
- arg = skipwhite(arg);
- if (idx == SIGNCMD_UNPLACE && *arg == NUL)
- {
- // ":sign unplace {id}": remove placed sign by number
- FOR_ALL_BUFFERS(buf) {
- if ((lnum = buf_delsign(buf, id)) != 0) {
- update_debug_sign(buf, lnum);
- }
- }
- return;
- }
- }
- }
-
- /*
- * Check for line={lnum} name={name} and file={fname} or buffer={nr}.
- * Leave "arg" pointing to {fname}.
- */
-
- buf_T *buf = NULL;
- for (;;)
- {
- if (STRNCMP(arg, "line=", 5) == 0)
- {
- arg += 5;
- lnum = atoi((char *)arg);
- arg = skiptowhite(arg);
- }
- else if (STRNCMP(arg, "*", 1) == 0 && idx == SIGNCMD_UNPLACE)
- {
- if (id != -1)
- {
- EMSG(_(e_invarg));
- return;
- }
- id = -2;
- arg = skiptowhite(arg + 1);
- }
- else if (STRNCMP(arg, "name=", 5) == 0)
- {
- arg += 5;
- sign_name = arg;
- arg = skiptowhite(arg);
- if (*arg != NUL)
- *arg++ = NUL;
- while (sign_name[0] == '0' && sign_name[1] != NUL)
- ++sign_name;
- }
- else if (STRNCMP(arg, "file=", 5) == 0)
- {
- arg += 5;
- buf = buflist_findname(arg);
- break;
- }
- else if (STRNCMP(arg, "buffer=", 7) == 0)
- {
- arg += 7;
- buf = buflist_findnr(getdigits_int(&arg));
- if (*skipwhite(arg) != NUL)
- EMSG(_(e_trailing));
- break;
- }
- else
- {
- EMSG(_(e_invarg));
- return;
- }
- arg = skipwhite(arg);
- }
-
- if (buf == NULL)
- {
- EMSG2(_("E158: Invalid buffer name: %s"), arg);
- }
- else if (id <= 0 && !(idx == SIGNCMD_UNPLACE && id == -2))
- {
- if (lnum >= 0 || sign_name != NULL)
- EMSG(_(e_invarg));
- else
- /* ":sign place file={fname}": list placed signs in one file */
- sign_list_placed(buf);
- }
- else if (idx == SIGNCMD_JUMP)
- {
- /* ":sign jump {id} file={fname}" */
- if (lnum >= 0 || sign_name != NULL)
- EMSG(_(e_invarg));
- else if ((lnum = buf_findsign(buf, id)) > 0)
- { /* goto a sign ... */
- if (buf_jump_open_win(buf) != NULL)
- { /* ... in a current window */
- curwin->w_cursor.lnum = lnum;
- check_cursor_lnum();
- beginline(BL_WHITE);
- }
- else
- { // ... not currently in a window
- char *cmd = xmalloc(STRLEN(buf->b_fname) + 25);
- sprintf(cmd, "e +%" PRId64 " %s",
- (int64_t)lnum, buf->b_fname);
- do_cmdline_cmd(cmd);
- xfree(cmd);
- }
-
- foldOpenCursor();
- }
- else
- EMSGN(_("E157: Invalid sign ID: %" PRId64), id);
- }
- else if (idx == SIGNCMD_UNPLACE)
- {
- if (lnum >= 0 || sign_name != NULL)
- EMSG(_(e_invarg));
- else if (id == -2)
- {
- /* ":sign unplace * file={fname}" */
- redraw_buf_later(buf, NOT_VALID);
- buf_delete_signs(buf);
- }
- else
- {
- /* ":sign unplace {id} file={fname}" */
- lnum = buf_delsign(buf, id);
- update_debug_sign(buf, lnum);
- }
- }
- /* idx == SIGNCMD_PLACE */
- else if (sign_name != NULL)
- {
- for (sp = first_sign; sp != NULL; sp = sp->sn_next)
- if (STRCMP(sp->sn_name, sign_name) == 0)
- break;
- if (sp == NULL)
- {
- EMSG2(_("E155: Unknown sign: %s"), sign_name);
- return;
- }
- if (lnum > 0)
- /* ":sign place {id} line={lnum} name={name} file={fname}":
- * place a sign */
- buf_addsign(buf, id, lnum, sp->sn_typenr);
- else
- /* ":sign place {id} file={fname}": change sign type */
- lnum = buf_change_sign_type(buf, id, sp->sn_typenr);
- if (lnum > 0)
- update_debug_sign(buf, lnum);
- else
- EMSG2(_("E885: Not possible to change sign %s"), sign_name);
- }
- else
- EMSG(_(e_invarg));
+
+ // first arg could be placed sign id
+ arg1 = arg;
+ if (ascii_isdigit(*arg)) {
+ id = getdigits_int(&arg);
+ if (!ascii_iswhite(*arg) && *arg != NUL) {
+ id = -1;
+ arg = arg1;
+ } else {
+ arg = skipwhite(arg);
+ if (idx == SIGNCMD_UNPLACE && *arg == NUL) {
+ // ":sign unplace {id}": remove placed sign by number
+ FOR_ALL_BUFFERS(buf) {
+ if ((lnum = buf_delsign(buf, id)) != 0) {
+ update_debug_sign(buf, lnum);
+ }
+ }
+ return;
+ }
+ }
+ }
+
+ // Check for line={lnum} name={name} and file={fname} or buffer={nr}.
+ // Leave "arg" pointing to {fname}.
+
+ buf_T *buf = NULL;
+ for (;;) {
+ if (STRNCMP(arg, "line=", 5) == 0) {
+ arg += 5;
+ lnum = atoi((char *)arg);
+ arg = skiptowhite(arg);
+ } else if (STRNCMP(arg, "*", 1) == 0 && idx == SIGNCMD_UNPLACE) {
+ if (id != -1) {
+ EMSG(_(e_invarg));
+ return;
+ }
+ id = -2;
+ arg = skiptowhite(arg + 1);
+ } else if (STRNCMP(arg, "name=", 5) == 0) {
+ arg += 5;
+ sign_name = arg;
+ arg = skiptowhite(arg);
+ if (*arg != NUL) {
+ *arg++ = NUL;
+ }
+ while (sign_name[0] == '0' && sign_name[1] != NUL) {
+ sign_name++;
+ }
+ } else if (STRNCMP(arg, "file=", 5) == 0) {
+ arg += 5;
+ buf = buflist_findname(arg);
+ break;
+ } else if (STRNCMP(arg, "buffer=", 7) == 0) {
+ arg += 7;
+ buf = buflist_findnr(getdigits_int(&arg));
+ if (*skipwhite(arg) != NUL) {
+ EMSG(_(e_trailing));
+ }
+ break;
+ } else {
+ EMSG(_(e_invarg));
+ return;
+ }
+ arg = skipwhite(arg);
+ }
+
+ if (buf == NULL) {
+ EMSG2(_("E158: Invalid buffer name: %s"), arg);
+ } else if (id <= 0 && !(idx == SIGNCMD_UNPLACE && id == -2)) {
+ if (lnum >= 0 || sign_name != NULL) {
+ EMSG(_(e_invarg));
+ } else {
+ // ":sign place file={fname}": list placed signs in one file
+ sign_list_placed(buf);
+ }
+ } else if (idx == SIGNCMD_JUMP) {
+ // ":sign jump {id} file={fname}"
+ if (lnum >= 0 || sign_name != NULL) {
+ EMSG(_(e_invarg));
+ } else if ((lnum = buf_findsign(buf, id)) > 0) {
+ // goto a sign ...
+ if (buf_jump_open_win(buf) != NULL) {
+ // ... in a current window
+ curwin->w_cursor.lnum = lnum;
+ check_cursor_lnum();
+ beginline(BL_WHITE);
+ } else {
+ // ... not currently in a window
+ if (buf->b_fname == NULL) {
+ EMSG(_("E934: Cannot jump to a buffer that does not have a name"));
+ return;
+ }
+ size_t cmdlen = STRLEN(buf->b_fname) + 24;
+ char *cmd = xmallocz(cmdlen);
+ snprintf(cmd, cmdlen, "e +%" PRId64 " %s",
+ (int64_t)lnum, buf->b_fname);
+ do_cmdline_cmd(cmd);
+ xfree(cmd);
+ }
+
+ foldOpenCursor();
+ } else {
+ EMSGN(_("E157: Invalid sign ID: %" PRId64), id);
+ }
+ } else if (idx == SIGNCMD_UNPLACE) {
+ if (lnum >= 0 || sign_name != NULL) {
+ EMSG(_(e_invarg));
+ } else if (id == -2) {
+ // ":sign unplace * file={fname}"
+ redraw_buf_later(buf, NOT_VALID);
+ buf_delete_signs(buf);
+ } else {
+ // ":sign unplace {id} file={fname}"
+ lnum = buf_delsign(buf, id);
+ update_debug_sign(buf, lnum);
+ }
+ } else if (sign_name != NULL) {
+ // idx == SIGNCMD_PLACE
+ for (sp = first_sign; sp != NULL; sp = sp->sn_next) {
+ if (STRCMP(sp->sn_name, sign_name) == 0) {
+ break;
+ }
+ }
+ if (sp == NULL) {
+ EMSG2(_("E155: Unknown sign: %s"), sign_name);
+ return;
+ }
+ if (lnum > 0) {
+ // ":sign place {id} line={lnum} name={name} file={fname}":
+ // place a sign
+ buf_addsign(buf, id, lnum, sp->sn_typenr);
+ } else {
+ // ":sign place {id} file={fname}": change sign type
+ lnum = buf_change_sign_type(buf, id, sp->sn_typenr);
+ }
+ if (lnum > 0) {
+ update_debug_sign(buf, lnum);
+ } else {
+ EMSG2(_("E885: Not possible to change sign %s"), sign_name);
+ }
+ } else {
+ EMSG(_(e_invarg));
}
+ }
}
/*
@@ -5683,50 +5816,39 @@ static enum
EXP_SIGN_NAMES /* expand with name of placed signs */
} expand_what;
-/*
- * Function given to ExpandGeneric() to obtain the sign command
- * expansion.
- */
+/// Function given to ExpandGeneric() to obtain the sign command
+/// expansion.
char_u * get_sign_name(expand_T *xp, int idx)
{
- sign_T *sp;
- int current_idx;
-
- switch (expand_what)
- {
+ switch (expand_what)
+ {
case EXP_SUBCMD:
- return (char_u *)cmds[idx];
- case EXP_DEFINE:
- {
- char *define_arg[] =
- {
- "icon=", "linehl=", "text=", "texthl=", NULL
- };
- return (char_u *)define_arg[idx];
- }
- case EXP_PLACE:
- {
- char *place_arg[] =
- {
- "line=", "name=", "file=", "buffer=", NULL
- };
- return (char_u *)place_arg[idx];
- }
- case EXP_UNPLACE:
- {
- char *unplace_arg[] = { "file=", "buffer=", NULL };
- return (char_u *)unplace_arg[idx];
- }
- case EXP_SIGN_NAMES:
- /* Complete with name of signs already defined */
- current_idx = 0;
- for (sp = first_sign; sp != NULL; sp = sp->sn_next)
- if (current_idx++ == idx)
- return sp->sn_name;
- return NULL;
+ return (char_u *)cmds[idx];
+ case EXP_DEFINE: {
+ char *define_arg[] = { "icon=", "linehl=", "text=", "texthl=", NULL };
+ return (char_u *)define_arg[idx];
+ }
+ case EXP_PLACE: {
+ char *place_arg[] = { "line=", "name=", "file=", "buffer=", NULL };
+ return (char_u *)place_arg[idx];
+ }
+ case EXP_UNPLACE: {
+ char *unplace_arg[] = { "file=", "buffer=", NULL };
+ return (char_u *)unplace_arg[idx];
+ }
+ case EXP_SIGN_NAMES: {
+ // Complete with name of signs already defined
+ int current_idx = 0;
+ for (sign_T *sp = first_sign; sp != NULL; sp = sp->sn_next) {
+ if (current_idx++ == idx) {
+ return sp->sn_name;
+ }
+ }
+ }
+ return NULL;
default:
- return NULL;
- }
+ return NULL;
+ }
}
/*
@@ -5846,3 +5968,169 @@ void set_context_in_sign_cmd(expand_T *xp, char_u *arg)
}
}
}
+
+/// 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, char_u *pat, char_u *sub,
+ MatchedLineVec *matched_lines)
+ FUNC_ATTR_NONNULL_ALL
+{
+ static handle_T bufnr = 0; // special buffer, re-used on each visit
+
+ win_T *save_curwin = curwin;
+ cmdmod_T save_cmdmod = cmdmod;
+ char_u *save_shm_p = vim_strsave(p_shm);
+ size_t sub_size = mb_string2cells(sub);
+ size_t pat_size = mb_string2cells(pat);
+
+ // We keep a special-purpose buffer around, but don't assume it exists.
+ buf_T *preview_buf = bufnr ? buflist_findnr(bufnr) : 0;
+ cmdmod.tab = 0; // disable :tab modifier
+ cmdmod.noswapfile = true; // disable swap for preview buffer
+ // disable file info message
+ set_string_option_direct((char_u *)"shm", -1, (char_u *)"F", OPT_FREE,
+ SID_NONE);
+
+ bool outside_curline = (eap->line1 != old_cusr.lnum
+ || eap->line2 != old_cusr.lnum);
+ bool split = outside_curline && (*p_icm != 'n') && (sub_size || pat_size);
+ if (preview_buf == curbuf) { // Preview buffer cannot preview itself!
+ split = false;
+ preview_buf = NULL;
+ }
+
+ // Place cursor on nearest matching line, to undo do_sub() cursor placement.
+ for (size_t i = 0; i < matched_lines->size; i++) {
+ MatchedLine curmatch = matched_lines->items[i];
+ if (curmatch.lnum >= old_cusr.lnum) {
+ curwin->w_cursor.lnum = curmatch.lnum;
+ curwin->w_cursor.col = curmatch.cols.items[0];
+ break;
+ } // Else: All matches are above, do_sub() already placed cursor.
+ }
+
+ if (split && win_split((int)p_cwh, WSP_BOT) != FAIL) {
+ buf_open_scratch(preview_buf ? bufnr : 0, "[Preview]");
+ buf_clear();
+ preview_buf = curbuf;
+ bufnr = preview_buf->handle;
+ curbuf->b_p_bl = false;
+ curbuf->b_p_ma = true;
+ curbuf->b_p_ul = -1;
+ curbuf->b_p_tw = 0; // Reset 'textwidth' (was set by ftplugin)
+ curwin->w_p_cul = false;
+ curwin->w_p_cuc = false;
+ curwin->w_p_spell = false;
+ curwin->w_p_fen = false;
+
+ // Width of the "| lnum|..." column which displays the line numbers.
+ linenr_T highest_num_line = kv_last(*matched_lines).lnum;
+ int col_width = log10(highest_num_line) + 1 + 3;
+
+ char *str = NULL;
+ size_t old_line_size = 0;
+ size_t line_size;
+ int src_id_highlight = 0;
+ int hl_id = syn_check_group((char_u *)"Substitute", 13);
+
+ // Dump the lines into the preview buffer.
+ for (size_t line = 0; line < matched_lines->size; line++) {
+ MatchedLine mat = matched_lines->items[line];
+ line_size = mb_string2cells(mat.line) + col_width + 1;
+
+ // Reallocate if str not long enough
+ if (line_size > old_line_size) {
+ str = xrealloc(str, line_size * sizeof(char));
+ old_line_size = line_size;
+ }
+
+ // Put "|lnum| line" into `str` and append it to the preview buffer.
+ snprintf(str, line_size, "|%*ld| %s", col_width - 3, mat.lnum, mat.line);
+ ml_append(line, (char_u *)str, (colnr_T)line_size, false);
+
+ // highlight the replaced part
+ if (sub_size > 0) {
+ for (size_t i = 0; i < mat.cols.size; i++) {
+ colnr_T col_start = mat.cols.items[i] + col_width
+ + i * (sub_size - pat_size) + 1;
+ colnr_T col_end = col_start - 1 + sub_size;
+ src_id_highlight = bufhl_add_hl(curbuf, src_id_highlight, hl_id,
+ line + 1, col_start, col_end);
+ }
+ }
+ }
+ xfree(str);
+ }
+
+ redraw_later(SOME_VALID);
+ win_enter(save_curwin, false); // Return to original window
+ update_topline();
+
+ // Update screen now. Must do this _before_ close_windows().
+ int save_rd = RedrawingDisabled;
+ RedrawingDisabled = 0;
+ update_screen(SOME_VALID);
+ RedrawingDisabled = save_rd;
+
+ set_string_option_direct((char_u *)"shm", -1, save_shm_p, OPT_FREE, SID_NONE);
+ xfree(save_shm_p);
+
+ cmdmod = save_cmdmod;
+
+ return preview_buf;
+}
+
+/// :substitute command
+///
+/// If 'inccommand' is empty: calls do_sub().
+/// If 'inccommand' is set: shows a "live" preview then removes the changes.
+/// from undo history.
+void ex_substitute(exarg_T *eap)
+{
+ bool preview = (State & CMDPREVIEW);
+ if (*p_icm == NUL || !preview) { // 'inccommand' is disabled
+ (void)do_sub(eap, profile_zero());
+ return;
+ }
+
+ block_autocmds(); // Disable events during command preview.
+
+ char_u *save_eap = eap->arg;
+ garray_T save_view;
+ win_size_save(&save_view); // Save current window sizes.
+ save_search_patterns();
+ int save_changedtick = curbuf->b_changedtick;
+ time_t save_b_u_time_cur = curbuf->b_u_time_cur;
+ u_header_T *save_b_u_newhead = curbuf->b_u_newhead;
+ long save_b_p_ul = curbuf->b_p_ul;
+ int save_w_p_cul = curwin->w_p_cul;
+ int save_w_p_cuc = curwin->w_p_cuc;
+
+ curbuf->b_p_ul = LONG_MAX; // make sure we can undo all changes
+ curwin->w_p_cul = false; // Disable 'cursorline'
+ curwin->w_p_cuc = false; // Disable 'cursorcolumn'
+
+ buf_T *preview_buf = do_sub(eap, profile_setlimit(p_rdt));
+
+ if (save_changedtick != curbuf->b_changedtick) {
+ // Undo invisibly. This also moves the cursor!
+ 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;
+ curbuf->b_u_time_cur = save_b_u_time_cur;
+ curbuf->b_changedtick = save_changedtick;
+ }
+ if (buf_valid(preview_buf)) {
+ // XXX: Must do this *after* u_undo_and_forget(), why?
+ close_windows(preview_buf, false);
+ }
+ curbuf->b_p_ul = save_b_p_ul;
+ curwin->w_p_cul = save_w_p_cul; // Restore 'cursorline'
+ curwin->w_p_cuc = save_w_p_cuc; // Restore 'cursorcolumn'
+ eap->arg = save_eap;
+ restore_search_patterns();
+ win_size_restore(&save_view);
+ ga_clear(&save_view);
+ unblock_autocmds();
+}