aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/ex_docmd.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/ex_docmd.c')
-rw-r--r--src/nvim/ex_docmd.c1356
1 files changed, 787 insertions, 569 deletions
diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c
index eccece7ac7..d1405978b3 100644
--- a/src/nvim/ex_docmd.c
+++ b/src/nvim/ex_docmd.c
@@ -1,3 +1,6 @@
+// This is an open source non-commercial project. Dear PVS-Studio, please check
+// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
+
/*
* ex_docmd.c: functions for executing an Ex command line.
*/
@@ -50,6 +53,7 @@
#include "nvim/screen.h"
#include "nvim/search.h"
#include "nvim/spell.h"
+#include "nvim/spellfile.h"
#include "nvim/strings.h"
#include "nvim/syntax.h"
#include "nvim/tag.h"
@@ -66,6 +70,8 @@
#include "nvim/event/rstream.h"
#include "nvim/event/wstream.h"
#include "nvim/shada.h"
+#include "nvim/lua/executor.h"
+#include "nvim/globals.h"
static int quitmore = 0;
static int ex_pressedreturn = FALSE;
@@ -268,7 +274,7 @@ do_exmode (
/*
* Execute a simple command line. Used for translated commands like "*".
*/
-int do_cmdline_cmd(char *cmd)
+int do_cmdline_cmd(const char *cmd)
{
return do_cmdline((char_u *)cmd, NULL, NULL,
DOCMD_NOWAIT|DOCMD_KEYTYPED);
@@ -334,12 +340,13 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline,
msg_list = &private_msg_list;
private_msg_list = NULL;
- /* It's possible to create an endless loop with ":execute", catch that
- * here. The value of 200 allows nested function calls, ":source", etc. */
- if (call_depth == 200) {
+ // It's possible to create an endless loop with ":execute", catch that
+ // here. The value of 200 allows nested function calls, ":source", etc.
+ // Allow 200 or 'maxfuncdepth', whatever is larger.
+ if (call_depth >= 200 && call_depth >= p_mfd) {
EMSG(_("E169: Command too recursive"));
- /* When converting to an exception, we do not include the command name
- * since this is not an error of the specific command. */
+ // When converting to an exception, we do not include the command name
+ // since this is not an error of the specific command.
do_errthrow((struct condstack *)NULL, (char_u *)NULL);
msg_list = saved_msg_list;
return FAIL;
@@ -583,10 +590,10 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline,
++no_wait_return;
verbose_enter_scroll();
- smsg(_("line %" PRId64 ": %s"),
- (int64_t)sourcing_lnum, cmdline_copy);
- if (msg_silent == 0)
- msg_puts((char_u *)"\n"); /* don't overwrite this */
+ smsg(_("line %" PRIdLINENR ": %s"), sourcing_lnum, cmdline_copy);
+ if (msg_silent == 0) {
+ msg_puts("\n"); // don't overwrite this either
+ }
verbose_leave_scroll();
--no_wait_return;
@@ -838,8 +845,6 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline,
break;
case ET_INTERRUPT:
break;
- default:
- p = vim_strsave((char_u *)_(e_internal));
}
saved_sourcing_name = sourcing_name;
@@ -1244,6 +1249,7 @@ static char_u * do_one_cmd(char_u **cmdlinep,
cmdmod_T save_cmdmod;
int ni; /* set when Not Implemented */
char_u *cmd;
+ int address_count = 1;
memset(&ea, 0, sizeof(ea));
ea.line1 = 1;
@@ -1301,7 +1307,6 @@ static char_u * do_one_cmd(char_u **cmdlinep,
/*
* 2. Handle command modifiers.
*/
- p = ea.cmd;
p = skip_range(ea.cmd, NULL);
switch (*p) {
/* When adding an entry, also modify cmd_exists(). */
@@ -1345,6 +1350,31 @@ static char_u * do_one_cmd(char_u **cmdlinep,
cmdmod.keepjumps = true;
continue;
+ case 'f': { // only accept ":filter {pat} cmd"
+ char_u *reg_pat;
+
+ if (!checkforcmd(&p, "filter", 4) || *p == NUL || ends_excmd(*p)) {
+ break;
+ }
+ if (*p == '!') {
+ cmdmod.filter_force = true;
+ p = skipwhite(p + 1);
+ if (*p == NUL || ends_excmd(*p)) {
+ break;
+ }
+ }
+ p = skip_vimgrep_pat(p, &reg_pat, NULL);
+ if (p == NULL || *p == NUL) {
+ break;
+ }
+ cmdmod.filter_regmatch.regprog = vim_regcomp(reg_pat, RE_MAGIC);
+ if (cmdmod.filter_regmatch.regprog == NULL) {
+ break;
+ }
+ ea.cmd = p;
+ continue;
+ }
+
/* ":hide" and ":hide | cmd" are not modifiers */
case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
|| *p == NUL || ends_excmd(*p))
@@ -1405,7 +1435,7 @@ static char_u * do_one_cmd(char_u **cmdlinep,
continue;
case 't': if (checkforcmd(&p, "tab", 3)) {
- long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS, ea.skip, false);
+ long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS, ea.skip, false, 1);
if (tabnr == MAXLNUM) {
cmdmod.tab = tabpage_index(curtab) + 1;
} else {
@@ -1447,6 +1477,7 @@ static char_u * do_one_cmd(char_u **cmdlinep,
}
break;
}
+ char_u *after_modifier = ea.cmd;
ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
&& !(cstack->cs_flags[cstack->
@@ -1520,8 +1551,7 @@ static char_u * do_one_cmd(char_u **cmdlinep,
ea.line2 = curwin->w_cursor.lnum;
break;
case ADDR_WINDOWS:
- lnum = CURRENT_WIN_NR;
- ea.line2 = lnum;
+ ea.line2 = CURRENT_WIN_NR;
break;
case ADDR_ARGUMENTS:
ea.line2 = curwin->w_arg_idx + 1;
@@ -1534,8 +1564,10 @@ static char_u * do_one_cmd(char_u **cmdlinep,
ea.line2 = curbuf->b_fnum;
break;
case ADDR_TABS:
- lnum = CURRENT_TAB_NR;
- ea.line2 = lnum;
+ ea.line2 = CURRENT_TAB_NR;
+ break;
+ case ADDR_TABS_RELATIVE:
+ ea.line2 = 1;
break;
case ADDR_QUICKFIX:
ea.line2 = qf_get_cur_valid_idx(&ea);
@@ -1543,7 +1575,7 @@ static char_u * do_one_cmd(char_u **cmdlinep,
}
ea.cmd = skipwhite(ea.cmd);
lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
- ea.addr_count == 0);
+ ea.addr_count == 0, address_count++);
if (ea.cmd == NULL) { // error detected
goto doend;
}
@@ -1585,6 +1617,10 @@ static char_u * do_one_cmd(char_u **cmdlinep,
goto doend;
}
break;
+ case ADDR_TABS_RELATIVE:
+ errormsg = (char_u *)_(e_invrange);
+ goto doend;
+ break;
case ADDR_ARGUMENTS:
if (ARGCOUNT == 0) {
ea.line1 = ea.line2 = 0;
@@ -1630,11 +1666,15 @@ static char_u * do_one_cmd(char_u **cmdlinep,
ea.addr_count++;
if (*ea.cmd == ';') {
- if (!ea.skip)
+ if (!ea.skip) {
curwin->w_cursor.lnum = ea.line2;
- } else if (*ea.cmd != ',')
+ // Don't leave the cursor on an illegal line (caused by ';')
+ check_cursor_lnum();
+ }
+ } else if (*ea.cmd != ',') {
break;
- ++ea.cmd;
+ }
+ ea.cmd++;
}
/* One address given: set start and end lines */
@@ -1645,9 +1685,6 @@ static char_u * do_one_cmd(char_u **cmdlinep,
ea.addr_count = 0;
}
- /* Don't leave the cursor on an illegal line (caused by ';') */
- check_cursor_lnum();
-
/*
* 5. Parse the command.
*/
@@ -1719,16 +1756,18 @@ static char_u * do_one_cmd(char_u **cmdlinep,
errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
goto doend;
}
- /* Check for wrong commands. */
- if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78) {
- errormsg = uc_fun_cmd();
- goto doend;
- }
+ // Check for wrong commands.
if (ea.cmdidx == CMD_SIZE) {
if (!ea.skip) {
STRCPY(IObuff, _("E492: Not an editor command"));
if (!(flags & DOCMD_VERBOSE)) {
- append_command(*cmdlinep);
+ // If the modifier was parsed OK the error must be in the following
+ // command
+ if (after_modifier != NULL) {
+ append_command(after_modifier);
+ } else {
+ append_command(*cmdlinep);
+ }
}
errormsg = IObuff;
did_emsg_syntax = TRUE;
@@ -1815,9 +1854,9 @@ static char_u * do_one_cmd(char_u **cmdlinep,
errormsg = (char_u *)_("E493: Backwards range given");
goto doend;
}
- if (ask_yesno((char_u *)
- _("Backwards range given, OK to swap"), FALSE) != 'y')
+ if (ask_yesno(_("Backwards range given, OK to swap"), false) != 'y') {
goto doend;
+ }
}
lnum = ea.line1;
ea.line1 = ea.line2;
@@ -1971,6 +2010,9 @@ static char_u * do_one_cmd(char_u **cmdlinep,
case ADDR_TABS:
ea.line2 = LAST_TAB_NR;
break;
+ case ADDR_TABS_RELATIVE:
+ ea.line2 = 1;
+ break;
case ADDR_ARGUMENTS:
if (ARGCOUNT == 0) {
ea.line1 = ea.line2 = 0;
@@ -2026,11 +2068,11 @@ static char_u * do_one_cmd(char_u **cmdlinep,
ea.line1 = ea.line2;
ea.line2 += n - 1;
++ea.addr_count;
- /*
- * Be vi compatible: no error message for out of range.
- */
- if (ea.line2 > curbuf->b_ml.ml_line_count)
+ // Be vi compatible: no error message for out of range.
+ if (ea.addr_type == ADDR_LINES
+ && ea.line2 > curbuf->b_ml.ml_line_count) {
ea.line2 = curbuf->b_ml.ml_line_count;
+ }
}
}
@@ -2095,6 +2137,7 @@ static char_u * do_one_cmd(char_u **cmdlinep,
case CMD_echomsg:
case CMD_echon:
case CMD_execute:
+ case CMD_filter:
case CMD_help:
case CMD_hide:
case CMD_ijump:
@@ -2246,6 +2289,10 @@ doend:
free_string_option(cmdmod.save_ei);
}
+ if (cmdmod.filter_regmatch.regprog != NULL) {
+ vim_regfree(cmdmod.filter_regmatch.regprog);
+ }
+
cmdmod = save_cmdmod;
if (save_msg_silent != -1) {
@@ -2531,28 +2578,29 @@ static struct cmdmod {
int minlen;
int has_count; /* :123verbose :3tab */
} cmdmods[] = {
- {"aboveleft", 3, FALSE},
- {"belowright", 3, FALSE},
- {"botright", 2, FALSE},
- {"browse", 3, FALSE},
- {"confirm", 4, FALSE},
- {"hide", 3, FALSE},
- {"keepalt", 5, FALSE},
- {"keepjumps", 5, FALSE},
- {"keepmarks", 3, FALSE},
- {"keeppatterns", 5, FALSE},
- {"leftabove", 5, FALSE},
- {"lockmarks", 3, FALSE},
- {"noautocmd", 3, FALSE},
- {"noswapfile", 3, FALSE},
- {"rightbelow", 6, FALSE},
- {"sandbox", 3, FALSE},
- {"silent", 3, FALSE},
- {"tab", 3, TRUE},
- {"topleft", 2, FALSE},
- {"unsilent", 3, FALSE},
- {"verbose", 4, TRUE},
- {"vertical", 4, FALSE},
+ { "aboveleft", 3, false },
+ { "belowright", 3, false },
+ { "botright", 2, false },
+ { "browse", 3, false },
+ { "confirm", 4, false },
+ { "filter", 4, false },
+ { "hide", 3, false },
+ { "keepalt", 5, false },
+ { "keepjumps", 5, false },
+ { "keepmarks", 3, false },
+ { "keeppatterns", 5, false },
+ { "leftabove", 5, false },
+ { "lockmarks", 3, false },
+ { "noautocmd", 3, false },
+ { "noswapfile", 3, false },
+ { "rightbelow", 6, false },
+ { "sandbox", 3, false },
+ { "silent", 3, false },
+ { "tab", 3, true },
+ { "topleft", 2, false },
+ { "unsilent", 3, false },
+ { "verbose", 4, true },
+ { "vertical", 4, false },
};
/*
@@ -2582,27 +2630,29 @@ int modifier_len(char_u *cmd)
* Return 2 if there is an exact match.
* Return 3 if there is an ambiguous match.
*/
-int cmd_exists(char_u *name)
+int cmd_exists(const char *const name)
{
exarg_T ea;
- int full = FALSE;
- int i;
- int j;
char_u *p;
- /* Check command modifiers. */
- for (i = 0; i < (int)ARRAY_SIZE(cmdmods); ++i) {
- for (j = 0; name[j] != NUL; ++j)
- if (name[j] != cmdmods[i].name[j])
+ // Check command modifiers.
+ for (int i = 0; i < (int)ARRAY_SIZE(cmdmods); i++) {
+ int j;
+ for (j = 0; name[j] != NUL; j++) {
+ if (name[j] != (char)cmdmods[i].name[j]) {
break;
- if (name[j] == NUL && j >= cmdmods[i].minlen)
+ }
+ }
+ if (name[j] == NUL && j >= cmdmods[i].minlen) {
return cmdmods[i].name[j] == NUL ? 2 : 1;
+ }
}
/* Check built-in commands and user defined commands.
* For ":2match" and ":3match" we need to skip the number. */
- ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
+ ea.cmd = (char_u *)((*name == '2' || *name == '3') ? name + 1 : name);
ea.cmdidx = (cmdidx_T)0;
+ int full = false;
p = find_command(&ea, &full);
if (p == NULL)
return 3;
@@ -2620,32 +2670,27 @@ int cmd_exists(char_u *name)
* perfectly compatible with each other, but then the command line syntax
* probably won't change that much -- webb.
*/
-char_u *
-set_one_cmd_context (
+const char * set_one_cmd_context(
expand_T *xp,
- char_u *buff /* buffer for command string */
+ const char *buff // buffer for command string
)
{
- char_u *p;
- char_u *cmd, *arg;
- int len = 0;
+ size_t len = 0;
exarg_T ea;
- int compl = EXPAND_NOTHING;
- int delim;
- int forceit = FALSE;
- int usefilter = FALSE; /* filter instead of file name */
+ int context = EXPAND_NOTHING;
+ int forceit = false;
+ int usefilter = false; // Filter instead of file name.
ExpandInit(xp);
- xp->xp_pattern = buff;
- xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
+ xp->xp_pattern = (char_u *)buff;
+ xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
ea.argt = 0;
- /*
- * 2. skip comment lines and leading space, colons or bars
- */
- for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
- ;
- xp->xp_pattern = cmd;
+ // 2. skip comment lines and leading space, colons or bars
+ const char *cmd;
+ for (cmd = buff; vim_strchr((const char_u *)" \t:|", *cmd) != NULL; cmd++) {
+ }
+ xp->xp_pattern = (char_u *)cmd;
if (*cmd == NUL)
return NULL;
@@ -2657,14 +2702,15 @@ set_one_cmd_context (
/*
* 3. parse a range specifier of the form: addr [,addr] [;addr] ..
*/
- cmd = skip_range(cmd, &xp->xp_context);
+ cmd = (const char *)skip_range((const char_u *)cmd, &xp->xp_context);
/*
* 4. parse command
*/
- xp->xp_pattern = cmd;
- if (*cmd == NUL)
+ xp->xp_pattern = (char_u *)cmd;
+ if (*cmd == NUL) {
return NULL;
+ }
if (*cmd == '"') {
xp->xp_context = EXPAND_NOTHING;
return NULL;
@@ -2680,6 +2726,7 @@ set_one_cmd_context (
* do accept "keepmarks", "keepalt" and "keepjumps".
* - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
*/
+ const char *p;
if (*cmd == 'k' && cmd[1] != 'e') {
ea.cmdidx = CMD_k;
p = cmd + 1;
@@ -2702,20 +2749,21 @@ set_one_cmd_context (
}
}
// check for non-alpha command
- if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL) {
+ if (p == cmd && vim_strchr((const char_u *)"@*!=><&~#", *p) != NULL) {
p++;
}
- len = (int)(p - cmd);
+ len = (size_t)(p - cmd);
if (len == 0) {
xp->xp_context = EXPAND_UNSUCCESSFUL;
return NULL;
}
for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
- ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
- if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
- (size_t)len) == 0)
+ ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1)) {
+ if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd, len) == 0) {
break;
+ }
+ }
if (cmd[0] >= 'A' && cmd[0] <= 'Z') {
while (ASCII_ISALNUM(*p) || *p == '*') { // Allow * wild card
@@ -2732,16 +2780,15 @@ set_one_cmd_context (
return NULL;
if (ea.cmdidx == CMD_SIZE) {
- if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL) {
+ if (*cmd == 's' && vim_strchr((const char_u *)"cgriI", cmd[1]) != NULL) {
ea.cmdidx = CMD_substitute;
p = cmd + 1;
} else if (cmd[0] >= 'A' && cmd[0] <= 'Z') {
- ea.cmd = cmd;
- p = find_ucmd(&ea, p, NULL, xp,
- &compl
- );
- if (p == NULL)
- ea.cmdidx = CMD_SIZE; /* ambiguous user command */
+ ea.cmd = (char_u *)cmd;
+ p = (const char *)find_ucmd(&ea, (char_u *)p, NULL, xp, &context);
+ if (p == NULL) {
+ ea.cmdidx = CMD_SIZE; // Ambiguous user command.
+ }
}
}
if (ea.cmdidx == CMD_SIZE) {
@@ -2764,16 +2811,17 @@ set_one_cmd_context (
ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
}
- arg = skipwhite(p);
+ const char *arg = (const char *)skipwhite((const char_u *)p);
if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update) {
- if (*arg == '>') { /* append */
- if (*++arg == '>')
- ++arg;
- arg = skipwhite(arg);
- } else if (*arg == '!' && ea.cmdidx == CMD_write) { /* :w !filter */
- ++arg;
- usefilter = TRUE;
+ if (*arg == '>') { // Append.
+ if (*++arg == '>') {
+ arg++;
+ }
+ arg = (const char *)skipwhite((const char_u *)arg);
+ } else if (*arg == '!' && ea.cmdidx == CMD_write) { // :w !filter
+ arg++;
+ usefilter = true;
}
}
@@ -2786,23 +2834,24 @@ set_one_cmd_context (
}
if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift) {
- while (*arg == *cmd) /* allow any number of '>' or '<' */
- ++arg;
- arg = skipwhite(arg);
+ while (*arg == *cmd) { // allow any number of '>' or '<'
+ arg++;
+ }
+ arg = (const char *)skipwhite((const char_u *)arg);
}
/* Does command allow "+command"? */
if ((ea.argt & EDITCMD) && !usefilter && *arg == '+') {
/* Check if we're in the +command */
p = arg + 1;
- arg = skip_cmd_arg(arg, FALSE);
+ arg = (const char *)skip_cmd_arg((char_u *)arg, false);
/* Still touching the command after '+'? */
if (*arg == NUL)
return p;
- /* Skip space(s) after +command to get to the real argument */
- arg = skipwhite(arg);
+ // Skip space(s) after +command to get to the real argument.
+ arg = (const char *)skipwhite((const char_u *)arg);
}
/*
@@ -2831,19 +2880,18 @@ set_one_cmd_context (
}
// no arguments allowed
- if (!(ea.argt & EXTRA) && *arg != NUL
- && vim_strchr((char_u *)"|\"", *arg) == NULL) {
+ if (!(ea.argt & EXTRA) && *arg != NUL && strchr("|\"", *arg) == NULL) {
return NULL;
}
/* Find start of last argument (argument just before cursor): */
p = buff;
- xp->xp_pattern = p;
- len = (int)STRLEN(buff);
+ xp->xp_pattern = (char_u *)p;
+ len = strlen(buff);
while (*p && p < buff + len) {
if (*p == ' ' || *p == TAB) {
- /* argument starts after a space */
- xp->xp_pattern = ++p;
+ // Argument starts after a space.
+ xp->xp_pattern = (char_u *)++p;
} else {
if (*p == '\\' && *(p + 1) != NUL)
++p; /* skip over escaped character */
@@ -2853,25 +2901,26 @@ set_one_cmd_context (
if (ea.argt & XFILE) {
int c;
- int in_quote = FALSE;
- char_u *bow = NULL; /* Beginning of word */
+ int in_quote = false;
+ const char *bow = NULL; // Beginning of word.
/*
* Allow spaces within back-quotes to count as part of the argument
* being expanded.
*/
- xp->xp_pattern = skipwhite(arg);
- p = xp->xp_pattern;
+ xp->xp_pattern = skipwhite((const char_u *)arg);
+ p = (const char *)xp->xp_pattern;
while (*p != NUL) {
- if (has_mbyte)
- c = mb_ptr2char(p);
- else
- c = *p;
- if (c == '\\' && p[1] != NUL)
- ++p;
- else if (c == '`') {
+ if (has_mbyte) {
+ c = mb_ptr2char((const char_u *)p);
+ } else {
+ c = (uint8_t)(*p);
+ }
+ if (c == '\\' && p[1] != NUL) {
+ p++;
+ } else if (c == '`') {
if (!in_quote) {
- xp->xp_pattern = p;
+ xp->xp_pattern = (char_u *)p;
bow = p + 1;
}
in_quote = !in_quote;
@@ -2884,22 +2933,26 @@ set_one_cmd_context (
|| ascii_iswhite(c)) {
len = 0; /* avoid getting stuck when space is in 'isfname' */
while (*p != NUL) {
- if (has_mbyte)
- c = mb_ptr2char(p);
- else
+ if (has_mbyte) {
+ c = mb_ptr2char((const char_u *)p);
+ } else {
c = *p;
- if (c == '`' || vim_isfilec_or_wc(c))
+ }
+ if (c == '`' || vim_isfilec_or_wc(c)) {
break;
- if (has_mbyte)
- len = (*mb_ptr2len)(p);
- else
+ }
+ if (has_mbyte) {
+ len = (size_t)(*mb_ptr2len)((const char_u *)p);
+ } else {
len = 1;
+ }
mb_ptr_adv(p);
}
- if (in_quote)
+ if (in_quote) {
bow = p;
- else
- xp->xp_pattern = p;
+ } else {
+ xp->xp_pattern = (char_u *)p;
+ }
p -= len;
}
mb_ptr_adv(p);
@@ -2909,8 +2962,9 @@ set_one_cmd_context (
* If we are still inside the quotes, and we passed a space, just
* expand from there.
*/
- if (bow != NULL && in_quote)
- xp->xp_pattern = bow;
+ if (bow != NULL && in_quote) {
+ xp->xp_pattern = (char_u *)bow;
+ }
xp->xp_context = EXPAND_FILES;
/* For a shell command more chars need to be escaped. */
@@ -2918,33 +2972,36 @@ set_one_cmd_context (
#ifndef BACKSLASH_IN_FILENAME
xp->xp_shell = TRUE;
#endif
- /* When still after the command name expand executables. */
- if (xp->xp_pattern == skipwhite(arg))
+ // When still after the command name expand executables.
+ if (xp->xp_pattern == skipwhite((const char_u *)arg)) {
xp->xp_context = EXPAND_SHELLCMD;
+ }
}
- /* Check for environment variable */
- if (*xp->xp_pattern == '$'
- ) {
- for (p = xp->xp_pattern + 1; *p != NUL; ++p)
- if (!vim_isIDc(*p))
+ // Check for environment variable.
+ if (*xp->xp_pattern == '$') {
+ for (p = (const char *)xp->xp_pattern + 1; *p != NUL; p++) {
+ if (!vim_isIDc((uint8_t)(*p))) {
break;
+ }
+ }
if (*p == NUL) {
xp->xp_context = EXPAND_ENV_VARS;
- ++xp->xp_pattern;
- /* Avoid that the assignment uses EXPAND_FILES again. */
- if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
- compl = EXPAND_ENV_VARS;
+ xp->xp_pattern++;
+ // Avoid that the assignment uses EXPAND_FILES again.
+ if (context != EXPAND_USER_DEFINED && context != EXPAND_USER_LIST) {
+ context = EXPAND_ENV_VARS;
+ }
}
}
/* Check for user names */
if (*xp->xp_pattern == '~') {
- for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
- ;
- /* Complete ~user only if it partially matches a user name.
- * A full match ~user<Tab> will be replaced by user's home
- * directory i.e. something like ~user<Tab> -> /home/user/ */
- if (*p == NUL && p > xp->xp_pattern + 1
+ for (p = (const char *)xp->xp_pattern + 1; *p != NUL && *p != '/'; p++) {
+ }
+ // Complete ~user only if it partially matches a user name.
+ // A full match ~user<Tab> will be replaced by user's home
+ // directory i.e. something like ~user<Tab> -> /home/user/
+ if (*p == NUL && p > (const char *)xp->xp_pattern + 1
&& match_user(xp->xp_pattern + 1) == 1) {
xp->xp_context = EXPAND_USER;
++xp->xp_pattern;
@@ -2974,7 +3031,7 @@ set_one_cmd_context (
break;
case CMD_help:
xp->xp_context = EXPAND_HELP;
- xp->xp_pattern = arg;
+ xp->xp_pattern = (char_u *)arg;
break;
/* Command modifiers: return the argument.
@@ -3013,17 +3070,28 @@ set_one_cmd_context (
case CMD_windo:
return arg;
+ case CMD_filter:
+ if (*arg != NUL) {
+ arg = (const char *)skip_vimgrep_pat((char_u *)arg, NULL, NULL);
+ }
+ if (arg == NULL || *arg == NUL) {
+ xp->xp_context = EXPAND_NOTHING;
+ return NULL;
+ }
+ return (const char *)skipwhite((const char_u *)arg);
+
case CMD_match:
if (*arg == NUL || !ends_excmd(*arg)) {
/* also complete "None" */
set_context_in_echohl_cmd(xp, arg);
- arg = skipwhite(skiptowhite(arg));
+ arg = (const char *)skipwhite(skiptowhite((const char_u *)arg));
if (*arg != NUL) {
xp->xp_context = EXPAND_NOTHING;
- arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
+ arg = (const char *)skip_regexp((char_u *)arg + 1, (uint8_t)(*arg),
+ p_magic, NULL);
}
}
- return find_nextcmd(arg);
+ return (const char *)find_nextcmd((char_u *)arg);
/*
* All completion for the +cmdline_compl feature goes here.
@@ -3032,15 +3100,15 @@ set_one_cmd_context (
case CMD_command:
/* Check for attributes */
while (*arg == '-') {
- arg++; /* Skip "-" */
- p = skiptowhite(arg);
+ arg++; // Skip "-".
+ p = (const char *)skiptowhite((const char_u *)arg);
if (*p == NUL) {
- /* Cursor is still in the attribute */
- p = vim_strchr(arg, '=');
+ // Cursor is still in the attribute.
+ p = strchr(arg, '=');
if (p == NULL) {
- /* No "=", so complete attribute names */
+ // No "=", so complete attribute names.
xp->xp_context = EXPAND_USER_CMD_FLAGS;
- xp->xp_pattern = arg;
+ xp->xp_pattern = (char_u *)arg;
return NULL;
}
@@ -3048,73 +3116,81 @@ set_one_cmd_context (
// their arguments as well.
if (STRNICMP(arg, "complete", p - arg) == 0) {
xp->xp_context = EXPAND_USER_COMPLETE;
- xp->xp_pattern = p + 1;
+ xp->xp_pattern = (char_u *)p + 1;
return NULL;
} else if (STRNICMP(arg, "nargs", p - arg) == 0) {
xp->xp_context = EXPAND_USER_NARGS;
- xp->xp_pattern = p + 1;
+ xp->xp_pattern = (char_u *)p + 1;
return NULL;
} else if (STRNICMP(arg, "addr", p - arg) == 0) {
xp->xp_context = EXPAND_USER_ADDR_TYPE;
- xp->xp_pattern = p + 1;
+ xp->xp_pattern = (char_u *)p + 1;
return NULL;
}
return NULL;
}
- arg = skipwhite(p);
+ arg = (const char *)skipwhite((char_u *)p);
}
- /* After the attributes comes the new command name */
- p = skiptowhite(arg);
+ // After the attributes comes the new command name.
+ p = (const char *)skiptowhite((const char_u *)arg);
if (*p == NUL) {
xp->xp_context = EXPAND_USER_COMMANDS;
- xp->xp_pattern = arg;
+ xp->xp_pattern = (char_u *)arg;
break;
}
- /* And finally comes a normal command */
- return skipwhite(p);
+ // And finally comes a normal command.
+ return (const char *)skipwhite((const char_u *)p);
case CMD_delcommand:
xp->xp_context = EXPAND_USER_COMMANDS;
- xp->xp_pattern = arg;
+ xp->xp_pattern = (char_u *)arg;
break;
case CMD_global:
- case CMD_vglobal:
- delim = *arg; /* get the delimiter */
- if (delim)
- ++arg; /* skip delimiter if there is one */
+ case CMD_vglobal: {
+ const int delim = (uint8_t)(*arg); // Get the delimiter.
+ if (delim) {
+ arg++; // Skip delimiter if there is one.
+ }
- while (arg[0] != NUL && arg[0] != delim) {
- if (arg[0] == '\\' && arg[1] != NUL)
- ++arg;
- ++arg;
+ while (arg[0] != NUL && (uint8_t)arg[0] != delim) {
+ if (arg[0] == '\\' && arg[1] != NUL) {
+ arg++;
+ }
+ arg++;
}
if (arg[0] != NUL)
return arg + 1;
break;
+ }
case CMD_and:
- case CMD_substitute:
- delim = *arg;
+ case CMD_substitute: {
+ const int delim = (uint8_t)(*arg);
if (delim) {
- /* skip "from" part */
- ++arg;
- arg = skip_regexp(arg, delim, p_magic, NULL);
+ // Skip "from" part.
+ arg++;
+ arg = (const char *)skip_regexp((char_u *)arg, delim, p_magic, NULL);
}
- /* skip "to" part */
- while (arg[0] != NUL && arg[0] != delim) {
- if (arg[0] == '\\' && arg[1] != NUL)
- ++arg;
- ++arg;
+ // Skip "to" part.
+ while (arg[0] != NUL && (uint8_t)arg[0] != delim) {
+ if (arg[0] == '\\' && arg[1] != NUL) {
+ arg++;
+ }
+ arg++;
}
- if (arg[0] != NUL) /* skip delimiter */
- ++arg;
- while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
- ++arg;
- if (arg[0] != NUL)
+ if (arg[0] != NUL) { // Skip delimiter.
+ arg++;
+ }
+ while (arg[0] && strchr("|\"#", arg[0]) == NULL) {
+ arg++;
+ }
+ if (arg[0] != NUL) {
return arg;
+ }
break;
+ }
case CMD_isearch:
case CMD_dsearch:
case CMD_ilist:
@@ -3124,36 +3200,40 @@ set_one_cmd_context (
case CMD_djump:
case CMD_isplit:
case CMD_dsplit:
- arg = skipwhite(skipdigits(arg)); /* skip count */
- if (*arg == '/') { /* Match regexp, not just whole words */
- for (++arg; *arg && *arg != '/'; arg++)
- if (*arg == '\\' && arg[1] != NUL)
+ // Skip count.
+ arg = (const char *)skipwhite(skipdigits((const char_u *)arg));
+ if (*arg == '/') { // Match regexp, not just whole words.
+ for (++arg; *arg && *arg != '/'; arg++) {
+ if (*arg == '\\' && arg[1] != NUL) {
arg++;
+ }
+ }
if (*arg) {
- arg = skipwhite(arg + 1);
+ arg = (const char *)skipwhite((const char_u *)arg + 1);
- /* Check for trailing illegal characters */
- if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
+ // Check for trailing illegal characters.
+ if (*arg && strchr("|\"\n", *arg) == NULL) {
xp->xp_context = EXPAND_NOTHING;
- else
+ } else {
return arg;
+ }
}
}
break;
case CMD_autocmd:
- return set_context_in_autocmd(xp, arg, FALSE);
+ return (const char *)set_context_in_autocmd(xp, (char_u *)arg, false);
case CMD_doautocmd:
case CMD_doautoall:
- return set_context_in_autocmd(xp, arg, TRUE);
+ return (const char *)set_context_in_autocmd(xp, (char_u *)arg, true);
case CMD_set:
- set_context_in_set_cmd(xp, arg, 0);
+ set_context_in_set_cmd(xp, (char_u *)arg, 0);
break;
case CMD_setglobal:
- set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
+ set_context_in_set_cmd(xp, (char_u *)arg, OPT_GLOBAL);
break;
case CMD_setlocal:
- set_context_in_set_cmd(xp, arg, OPT_LOCAL);
+ set_context_in_set_cmd(xp, (char_u *)arg, OPT_LOCAL);
break;
case CMD_tag:
case CMD_stag:
@@ -3165,15 +3245,16 @@ set_one_cmd_context (
case CMD_tjump:
case CMD_stjump:
case CMD_ptjump:
- if (*p_wop != NUL)
+ if (*p_wop != NUL) {
xp->xp_context = EXPAND_TAGS_LISTFILES;
- else
+ } else {
xp->xp_context = EXPAND_TAGS;
- xp->xp_pattern = arg;
+ }
+ xp->xp_pattern = (char_u *)arg;
break;
case CMD_augroup:
xp->xp_context = EXPAND_AUGROUP;
- xp->xp_pattern = arg;
+ xp->xp_pattern = (char_u *)arg;
break;
case CMD_syntax:
set_context_in_syntax_cmd(xp, arg);
@@ -3190,20 +3271,21 @@ set_one_cmd_context (
case CMD_echoerr:
case CMD_call:
case CMD_return:
- set_context_for_expression(xp, arg, ea.cmdidx);
+ set_context_for_expression(xp, (char_u *)arg, ea.cmdidx);
break;
case CMD_unlet:
- while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
- arg = xp->xp_pattern + 1;
+ while ((xp->xp_pattern = (char_u *)strchr(arg, ' ')) != NULL) {
+ arg = (const char *)xp->xp_pattern + 1;
+ }
xp->xp_context = EXPAND_USER_VARS;
- xp->xp_pattern = arg;
+ xp->xp_pattern = (char_u *)arg;
break;
case CMD_function:
case CMD_delfunction:
xp->xp_context = EXPAND_USER_FUNC;
- xp->xp_pattern = arg;
+ xp->xp_pattern = (char_u *)arg;
break;
case CMD_echohl:
@@ -3218,33 +3300,37 @@ set_one_cmd_context (
set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
break;
case CMD_sign:
- set_context_in_sign_cmd(xp, arg);
+ set_context_in_sign_cmd(xp, (char_u *)arg);
break;
case CMD_bdelete:
case CMD_bwipeout:
case CMD_bunload:
- while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
- arg = xp->xp_pattern + 1;
- /*FALLTHROUGH*/
+ while ((xp->xp_pattern = (char_u *)strchr(arg, ' ')) != NULL) {
+ arg = (const char *)xp->xp_pattern + 1;
+ }
+ // FALLTHROUGH
case CMD_buffer:
case CMD_sbuffer:
case CMD_checktime:
xp->xp_context = EXPAND_BUFFERS;
- xp->xp_pattern = arg;
+ xp->xp_pattern = (char_u *)arg;
break;
case CMD_USER:
case CMD_USER_BUF:
- if (compl != EXPAND_NOTHING) {
- /* XFILE: file names are handled above */
+ if (context != EXPAND_NOTHING) {
+ // XFILE: file names are handled above.
if (!(ea.argt & XFILE)) {
- if (compl == EXPAND_MENUS)
- return set_context_in_menu_cmd(xp, cmd, arg, forceit);
- if (compl == EXPAND_COMMANDS)
+ if (context == EXPAND_MENUS) {
+ return (const char *)set_context_in_menu_cmd(xp, (char_u *)cmd,
+ (char_u *)arg, forceit);
+ } else if (context == EXPAND_COMMANDS) {
return arg;
- if (compl == EXPAND_MAPPINGS)
- return set_context_in_map_cmd(xp, (char_u *)"map",
- arg, forceit, FALSE, FALSE, CMD_map);
- /* Find start of last argument. */
+ } else if (context == EXPAND_MAPPINGS) {
+ return (const char *)set_context_in_map_cmd(
+ xp, (char_u *)"map", (char_u *)arg, forceit, false, false,
+ CMD_map);
+ }
+ // Find start of last argument.
p = arg;
while (*p) {
if (*p == ' ')
@@ -3254,9 +3340,9 @@ set_one_cmd_context (
++p; /* skip over escaped character */
mb_ptr_adv(p);
}
- xp->xp_pattern = arg;
+ xp->xp_pattern = (char_u *)arg;
}
- xp->xp_context = compl;
+ xp->xp_context = context;
}
break;
case CMD_map: case CMD_noremap:
@@ -3268,8 +3354,8 @@ set_one_cmd_context (
case CMD_lmap: case CMD_lnoremap:
case CMD_smap: case CMD_snoremap:
case CMD_xmap: case CMD_xnoremap:
- return set_context_in_map_cmd(xp, cmd, arg, forceit,
- FALSE, FALSE, ea.cmdidx);
+ return (const char *)set_context_in_map_cmd(
+ xp, (char_u *)cmd, (char_u *)arg, forceit, false, false, ea.cmdidx);
case CMD_unmap:
case CMD_nunmap:
case CMD_vunmap:
@@ -3279,18 +3365,18 @@ set_one_cmd_context (
case CMD_lunmap:
case CMD_sunmap:
case CMD_xunmap:
- return set_context_in_map_cmd(xp, cmd, arg, forceit,
- FALSE, TRUE, ea.cmdidx);
+ return (const char *)set_context_in_map_cmd(
+ xp, (char_u *)cmd, (char_u *)arg, forceit, false, true, ea.cmdidx);
case CMD_abbreviate: case CMD_noreabbrev:
case CMD_cabbrev: case CMD_cnoreabbrev:
case CMD_iabbrev: case CMD_inoreabbrev:
- return set_context_in_map_cmd(xp, cmd, arg, forceit,
- TRUE, FALSE, ea.cmdidx);
+ return (const char *)set_context_in_map_cmd(
+ xp, (char_u *)cmd, (char_u *)arg, forceit, true, false, ea.cmdidx);
case CMD_unabbreviate:
case CMD_cunabbrev:
case CMD_iunabbrev:
- return set_context_in_map_cmd(xp, cmd, arg, forceit,
- TRUE, TRUE, ea.cmdidx);
+ return (const char *)set_context_in_map_cmd(
+ xp, (char_u *)cmd, (char_u *)arg, forceit, true, true, ea.cmdidx);
case CMD_menu: case CMD_noremenu: case CMD_unmenu:
case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
@@ -3300,47 +3386,49 @@ set_one_cmd_context (
case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
case CMD_tmenu: case CMD_tunmenu:
case CMD_popup: case CMD_emenu:
- return set_context_in_menu_cmd(xp, cmd, arg, forceit);
+ return (const char *)set_context_in_menu_cmd(
+ xp, (char_u *)cmd, (char_u *)arg, forceit);
case CMD_colorscheme:
xp->xp_context = EXPAND_COLORS;
- xp->xp_pattern = arg;
+ xp->xp_pattern = (char_u *)arg;
break;
case CMD_compiler:
xp->xp_context = EXPAND_COMPILER;
- xp->xp_pattern = arg;
+ xp->xp_pattern = (char_u *)arg;
break;
case CMD_ownsyntax:
xp->xp_context = EXPAND_OWNSYNTAX;
- xp->xp_pattern = arg;
+ xp->xp_pattern = (char_u *)arg;
break;
case CMD_setfiletype:
xp->xp_context = EXPAND_FILETYPE;
- xp->xp_pattern = arg;
+ xp->xp_pattern = (char_u *)arg;
break;
case CMD_packadd:
xp->xp_context = EXPAND_PACKADD;
- xp->xp_pattern = arg;
+ xp->xp_pattern = (char_u *)arg;
break;
#ifdef HAVE_WORKING_LIBINTL
case CMD_language:
- p = skiptowhite(arg);
+ p = (const char *)skiptowhite((const char_u *)arg);
if (*p == NUL) {
xp->xp_context = EXPAND_LANGUAGE;
- xp->xp_pattern = arg;
+ xp->xp_pattern = (char_u *)arg;
} else {
- if ( STRNCMP(arg, "messages", p - arg) == 0
- || STRNCMP(arg, "ctype", p - arg) == 0
- || STRNCMP(arg, "time", p - arg) == 0) {
+ if (strncmp(arg, "messages", p - arg) == 0
+ || strncmp(arg, "ctype", p - arg) == 0
+ || strncmp(arg, "time", p - arg) == 0) {
xp->xp_context = EXPAND_LOCALES;
- xp->xp_pattern = skipwhite(p);
- } else
+ xp->xp_pattern = skipwhite((const char_u *)p);
+ } else {
xp->xp_context = EXPAND_NOTHING;
+ }
}
break;
#endif
@@ -3349,16 +3437,21 @@ set_one_cmd_context (
break;
case CMD_behave:
xp->xp_context = EXPAND_BEHAVE;
- xp->xp_pattern = arg;
+ xp->xp_pattern = (char_u *)arg;
+ break;
+
+ case CMD_messages:
+ xp->xp_context = EXPAND_MESSAGES;
+ xp->xp_pattern = (char_u *)arg;
break;
case CMD_history:
xp->xp_context = EXPAND_HISTORY;
- xp->xp_pattern = arg;
+ xp->xp_pattern = (char_u *)arg;
break;
case CMD_syntime:
xp->xp_context = EXPAND_SYNTIME;
- xp->xp_pattern = arg;
+ xp->xp_pattern = (char_u *)arg;
break;
@@ -3377,10 +3470,9 @@ set_one_cmd_context (
* Also skip white space and ":" characters.
* Returns the "cmd" pointer advanced to beyond the range.
*/
-char_u *
-skip_range (
- char_u *cmd,
- int *ctx /* pointer to xp_context or NULL */
+char_u *skip_range(
+ const char_u *cmd,
+ int *ctx // pointer to xp_context or NULL
)
{
unsigned delim;
@@ -3405,7 +3497,7 @@ skip_range (
while (*cmd == ':')
cmd = skipwhite(cmd + 1);
- return cmd;
+ return (char_u *)cmd;
}
/*
@@ -3420,8 +3512,8 @@ static linenr_T get_address(exarg_T *eap,
char_u **ptr,
int addr_type, // flag: one of ADDR_LINES, ...
int skip, // only skip the address, don't use it
- int to_other_file // flag: may jump to other file
- )
+ int to_other_file, // flag: may jump to other file
+ int address_count) // 1 for first, >1 after comma
{
int c;
int i;
@@ -3455,6 +3547,11 @@ static linenr_T get_address(exarg_T *eap,
case ADDR_TABS:
lnum = CURRENT_TAB_NR;
break;
+ case ADDR_TABS_RELATIVE:
+ EMSG(_(e_invrange));
+ cmd = NULL;
+ goto error;
+ break;
case ADDR_QUICKFIX:
lnum = qf_get_cur_valid_idx(eap);
break;
@@ -3489,6 +3586,11 @@ static linenr_T get_address(exarg_T *eap,
case ADDR_TABS:
lnum = LAST_TAB_NR;
break;
+ case ADDR_TABS_RELATIVE:
+ EMSG(_(e_invrange));
+ cmd = NULL;
+ goto error;
+ break;
case ADDR_QUICKFIX:
lnum = qf_get_size(eap);
if (lnum == 0) {
@@ -3637,6 +3739,9 @@ static linenr_T get_address(exarg_T *eap,
case ADDR_TABS:
lnum = CURRENT_TAB_NR;
break;
+ case ADDR_TABS_RELATIVE:
+ lnum = 1;
+ break;
case ADDR_QUICKFIX:
lnum = qf_get_cur_valid_idx(eap);
break;
@@ -3651,13 +3756,27 @@ static linenr_T get_address(exarg_T *eap,
n = 1;
else
n = getdigits(&cmd);
- if (addr_type == ADDR_LOADED_BUFFERS || addr_type == ADDR_BUFFERS)
+
+ if (addr_type == ADDR_TABS_RELATIVE) {
+ EMSG(_(e_invrange));
+ cmd = NULL;
+ goto error;
+ } else if (addr_type == ADDR_LOADED_BUFFERS || addr_type == ADDR_BUFFERS) {
lnum = compute_buffer_local_count(
addr_type, lnum, (i == '-') ? -1 * n : n);
- else if (i == '-')
- lnum -= n;
- else
- lnum += n;
+ } else {
+ // Relative line addressing, need to adjust for folded lines
+ // now, but only do it after the first address.
+ if (addr_type == ADDR_LINES && (i == '-' || i == '+')
+ && address_count >= 2) {
+ (void)hasFolding(lnum, NULL, &lnum);
+ }
+ if (i == '-') {
+ lnum -= n;
+ } else {
+ lnum += n;
+ }
+ }
}
} while (*cmd == '/' || *cmd == '?');
@@ -3694,10 +3813,12 @@ void ex_ni(exarg_T *eap)
/// Skips over ":perl <<EOF" constructs.
static void ex_script_ni(exarg_T *eap)
{
- if (!eap->skip)
+ if (!eap->skip) {
ex_ni(eap);
- else
- xfree(script_get(eap, eap->arg));
+ } else {
+ size_t len;
+ xfree(script_get(eap, &len));
+ }
}
/*
@@ -3764,6 +3885,9 @@ static char_u *invalid_range(exarg_T *eap)
return (char_u *)_(e_invrange);
}
break;
+ case ADDR_TABS_RELATIVE:
+ // Do nothing
+ break;
case ADDR_QUICKFIX:
assert(eap->line2 >= 0);
if (eap->line2 != 1 && (size_t)eap->line2 > qf_get_size(eap)) {
@@ -3854,7 +3978,7 @@ static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
ptr = new_cmdline;
while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL) {
i = (int)(pos - program);
- STRNCPY(ptr, program, i);
+ memcpy(ptr, program, i);
STRCPY(ptr += i, p);
ptr += len;
program = pos + 2;
@@ -3940,8 +4064,6 @@ int expand_filename(exarg_T *eap, char_u **cmdlinep, char_u **errormsgp)
* Don't do this for:
* - replacement that already has been escaped: "##"
* - shell commands (may have to use quotes instead).
- * - non-unix systems when there is a single argument (spaces don't
- * separate arguments then).
*/
if (!eap->usefilter
&& !escaped
@@ -3952,9 +4074,7 @@ int expand_filename(exarg_T *eap, char_u **cmdlinep, char_u **errormsgp)
&& eap->cmdidx != CMD_lgrep
&& eap->cmdidx != CMD_grepadd
&& eap->cmdidx != CMD_lgrepadd
-#ifndef UNIX
&& !(eap->argt & NOSPC)
-#endif
) {
char_u *l;
#ifdef BACKSLASH_IN_FILENAME
@@ -4016,28 +4136,6 @@ int expand_filename(exarg_T *eap, char_u **cmdlinep, char_u **errormsgp)
}
}
- // Replace any other wildcards, remove backslashes.
-#ifdef UNIX
- /*
- * Only for Unix we check for more than one file name.
- * For other systems spaces are considered to be part
- * of the file name.
- * Only check here if there is no wildcard, otherwise
- * ExpandOne() will check for errors. This allows
- * ":e `ls ve*.c`" on Unix.
- */
- if (!has_wildcards)
- for (p = eap->arg; *p; ++p) {
- /* skip escaped characters */
- if (p[1] && (*p == '\\' || *p == Ctrl_V))
- ++p;
- else if (ascii_iswhite(*p)) {
- *errormsgp = (char_u *)_("E172: Only one file name allowed");
- return FAIL;
- }
- }
-#endif
-
/*
* Halve the number of backslashes (this is Vi compatible).
* For Unix, when wildcards are expanded, this is
@@ -4056,14 +4154,12 @@ int expand_filename(exarg_T *eap, char_u **cmdlinep, char_u **errormsgp)
xpc.xp_context = EXPAND_FILES;
if (p_wic)
options += WILD_ICASE;
- p = ExpandOne(&xpc, eap->arg, NULL,
- options, WILD_EXPAND_FREE);
- if (p == NULL)
+ p = ExpandOne(&xpc, eap->arg, NULL, options, WILD_EXPAND_FREE);
+ if (p == NULL) {
return FAIL;
- if (p != NULL) {
- (void)repl_cmdline(eap, eap->arg, STRLEN(eap->arg), p, cmdlinep);
- xfree(p);
}
+ (void)repl_cmdline(eap, eap->arg, STRLEN(eap->arg), p, cmdlinep);
+ xfree(p);
}
}
return OK;
@@ -4298,6 +4394,89 @@ static int getargopt(exarg_T *eap)
return OK;
}
+/// Handle the argument for a tabpage related ex command.
+/// Returns a tabpage number.
+/// When an error is encountered then eap->errmsg is set.
+static int get_tabpage_arg(exarg_T *eap)
+{
+ int tab_number = 0;
+ int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
+
+ if (eap->arg && *eap->arg != NUL) {
+ char_u *p = eap->arg;
+ char_u *p_save;
+ int relative = 0; // argument +N/-N means: go to N places to the
+ // right/left relative to the current position.
+
+ if (*p == '-') {
+ relative = -1;
+ p++;
+ } else if (*p == '+') {
+ relative = 1;
+ p++;
+ }
+
+ p_save = p;
+ tab_number = getdigits(&p);
+
+ if (relative == 0) {
+ if (STRCMP(p, "$") == 0) {
+ tab_number = LAST_TAB_NR;
+ } else if (p == p_save || *p_save == '-' || *p != NUL
+ || tab_number > LAST_TAB_NR) {
+ // No numbers as argument.
+ eap->errmsg = e_invarg;
+ goto theend;
+ }
+ } else {
+ if (*p_save == NUL) {
+ tab_number = 1;
+ }
+ else if (p == p_save || *p_save == '-' || *p != NUL || tab_number == 0) {
+ // No numbers as argument.
+ eap->errmsg = e_invarg;
+ goto theend;
+ }
+ tab_number = tab_number * relative + tabpage_index(curtab);
+ if (!unaccept_arg0 && relative == -1) {
+ --tab_number;
+ }
+ }
+ if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR) {
+ eap->errmsg = e_invarg;
+ }
+ } else if (eap->addr_count > 0) {
+ if (unaccept_arg0 && eap->line2 == 0) {
+ eap->errmsg = e_invrange;
+ tab_number = 0;
+ } else {
+ tab_number = eap->line2;
+ if (!unaccept_arg0 && **eap->cmdlinep == '-') {
+ --tab_number;
+ if (tab_number < unaccept_arg0) {
+ eap->errmsg = e_invarg;
+ }
+ }
+ }
+ } else {
+ switch (eap->cmdidx) {
+ case CMD_tabnext:
+ tab_number = tabpage_index(curtab) + 1;
+ if (tab_number > LAST_TAB_NR)
+ tab_number = 1;
+ break;
+ case CMD_tabmove:
+ tab_number = LAST_TAB_NR;
+ break;
+ default:
+ tab_number = tabpage_index(curtab);
+ }
+ }
+
+theend:
+ return tab_number;
+}
+
/*
* ":abbreviate" and friends.
*/
@@ -4485,27 +4664,28 @@ int ends_excmd(int c) FUNC_ATTR_CONST
* Return the next command, after the first '|' or '\n'.
* Return NULL if not found.
*/
-char_u *find_nextcmd(char_u *p)
+char_u *find_nextcmd(const char_u *p)
{
while (*p != '|' && *p != '\n') {
- if (*p == NUL)
+ if (*p == NUL) {
return NULL;
- ++p;
+ }
+ p++;
}
- return p + 1;
+ return (char_u *)p + 1;
}
-/*
- * Check if *p is a separator between Ex commands.
- * Return NULL if it isn't, (p + 1) if it is.
- */
+/// Check if *p is a separator between Ex commands, skipping over white space.
+/// Return NULL if it isn't, the following character if it is.
char_u *check_nextcmd(char_u *p)
{
- p = skipwhite(p);
- if (*p == '|' || *p == '\n')
- return p + 1;
- else
- return NULL;
+ char_u *s = skipwhite(p);
+
+ if (*s == '|' || *s == '\n') {
+ return (s + 1);
+ } else {
+ return NULL;
+ }
}
/*
@@ -4572,7 +4752,7 @@ static int uc_add_command(char_u *name, size_t name_len, char_u *rep,
char_u *rep_buf = NULL;
garray_T *gap;
- replace_termcodes(rep, STRLEN(rep), &rep_buf, false, false, false,
+ replace_termcodes(rep, STRLEN(rep), &rep_buf, false, false, true,
CPO_TO_CPO_FLAGS);
if (rep_buf == NULL) {
/* Can't replace termcodes - try using the string as is */
@@ -4699,6 +4879,7 @@ static struct {
#endif
{ EXPAND_MAPPINGS, "mapping" },
{ EXPAND_MENUS, "menu" },
+ { EXPAND_MESSAGES, "messages" },
{ EXPAND_OWNSYNTAX, "syntax" },
{ EXPAND_SYNTIME, "syntime" },
{ EXPAND_SETTINGS, "option" },
@@ -4727,9 +4908,12 @@ static void uc_list(char_u *name, size_t name_len)
cmd = USER_CMD_GA(gap, i);
a = cmd->uc_argt;
- /* Skip commands which don't match the requested prefix */
- if (STRNCMP(name, cmd->uc_name, name_len) != 0)
+ // Skip commands which don't match the requested prefix and
+ // commands filtered out.
+ if (STRNCMP(name, cmd->uc_name, name_len) != 0
+ || message_filtered(cmd->uc_name)) {
continue;
+ }
/* Put out the title first time */
if (!found)
@@ -4834,20 +5018,6 @@ static void uc_list(char_u *name, size_t name_len)
MSG(_("No user-defined commands found"));
}
-static char_u *uc_fun_cmd(void)
-{
- static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
- 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
- 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
- 0xb9, 0x7f, 0};
- int i;
-
- for (i = 0; fcmd[i]; ++i)
- IObuff[i] = fcmd[i] - 0x40;
- IObuff[i] = 0;
- return IObuff;
-}
-
static int uc_scan_attr(char_u *attr, size_t len, uint32_t *argt, long *def,
int *flags, int * compl, char_u **compl_arg,
int *addr_type_arg)
@@ -5655,10 +5825,10 @@ int parse_addr_type_arg(char_u *value, int vallen, uint32_t *argt,
* copied to allocated memory and stored in "*compl_arg".
* Returns FAIL if something is wrong.
*/
-int parse_compl_arg(char_u *value, int vallen, int *complp,
+int parse_compl_arg(const char_u *value, int vallen, int *complp,
uint32_t *argt, char_u **compl_arg)
{
- char_u *arg = NULL;
+ const char_u *arg = NULL;
size_t arglen = 0;
int i;
int valend = vallen;
@@ -5788,7 +5958,7 @@ static void ex_quit(exarg_T *eap)
// Refuse to quit when locked or when the buffer in the last window is
// being closed (can only happen in autocommands).
if (curbuf_locked()
- || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_closing)) {
+ || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0)) {
return;
}
@@ -5812,7 +5982,7 @@ static void ex_quit(exarg_T *eap)
// specified. Example:
// :h|wincmd w|1q - don't quit
// :h|wincmd w|q - quit
- if (only_one_window() && (firstwin == lastwin || eap->addr_count == 0)) {
+ if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0)) {
getout(0);
}
/* close window; may free buffer */
@@ -5846,11 +6016,12 @@ static void ex_quit_all(exarg_T *eap)
text_locked_msg();
return;
}
- apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
- /* Refuse to quit when locked or when the buffer in the last window is
- * being closed (can only happen in autocommands). */
- if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
+ apply_autocmds(EVENT_QUITPRE, NULL, NULL, false, curbuf);
+ // Refuse to quit when locked or when the buffer in the last window is
+ // being closed (can only happen in autocommands).
+ if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0)) {
return;
+ }
exiting = true;
if (eap->forceit || !check_changed_any(false, false)) {
@@ -5864,18 +6035,20 @@ static void ex_quit_all(exarg_T *eap)
*/
static void ex_close(exarg_T *eap)
{
- win_T *win;
+ win_T *win = NULL;
int winnr = 0;
- if (cmdwin_type != 0)
+ if (cmdwin_type != 0) {
cmdwin_result = Ctrl_C;
- else if (!text_locked() && !curbuf_locked()) {
- if (eap->addr_count == 0)
+ } else if (!text_locked() && !curbuf_locked()) {
+ if (eap->addr_count == 0) {
ex_win_close(eap->forceit, curwin, NULL);
- else {
- for (win = firstwin; win != NULL; win = win->w_next) {
+ } else {
+ FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
winnr++;
- if (winnr == eap->line2)
+ if (winnr == eap->line2) {
+ win = wp;
break;
+ }
}
if (win == NULL)
win = lastwin;
@@ -5914,10 +6087,13 @@ ex_win_close (
need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
if (need_hide && !P_HID(buf) && !forceit) {
if ((p_confirm || cmdmod.confirm) && p_write) {
- dialog_changed(buf, FALSE);
- if (buf_valid(buf) && bufIsChanged(buf))
+ bufref_T bufref;
+ set_bufref(&bufref, buf);
+ dialog_changed(buf, false);
+ if (bufref_valid(&bufref) && bufIsChanged(buf)) {
return;
- need_hide = FALSE;
+ }
+ need_hide = false;
} else {
EMSG(_(e_nowrtmsg));
return;
@@ -5945,8 +6121,9 @@ static void ex_tabclose(exarg_T *eap)
else if (first_tabpage->tp_next == NULL)
EMSG(_("E784: Cannot close last tab page"));
else {
- if (eap->addr_count > 0) {
- tp = find_tabpage((int)eap->line2);
+ int tab_number = get_tabpage_arg(eap);
+ if (eap->errmsg == NULL) {
+ tp = find_tabpage(tab_number);
if (tp == NULL) {
beep_flush();
return;
@@ -5954,44 +6131,46 @@ static void ex_tabclose(exarg_T *eap)
if (tp != curtab) {
tabpage_close_other(tp, eap->forceit);
return;
+ } else if (!text_locked() && !curbuf_locked()) {
+ tabpage_close(eap->forceit);
}
}
- if (!text_locked()
- && !curbuf_locked()
- )
- tabpage_close(eap->forceit);
}
}
-/*
- * ":tabonly": close all tab pages except the current one
- */
+/// ":tabonly": close all tab pages except the current one
static void ex_tabonly(exarg_T *eap)
{
- if (cmdwin_type != 0)
+ if (cmdwin_type != 0) {
cmdwin_result = K_IGNORE;
- else if (first_tabpage->tp_next == NULL)
- MSG(_("Already only one tab page"));
- else {
- if (eap->addr_count > 0)
- goto_tabpage(eap->line2);
- /* Repeat this up to a 1000 times, because autocommands may mess
- * up the lists. */
- for (int done = 0; done < 1000; ++done) {
- FOR_ALL_TABS(tp) {
- if (tp->tp_topframe != topframe) {
- tabpage_close_other(tp, eap->forceit);
- /* if we failed to close it quit */
- if (valid_tabpage(tp))
- done = 1000;
- /* start over, "tp" is now invalid */
+ } else if (first_tabpage->tp_next == NULL) {
+ MSG(_("Already only one tab page"));
+ } else {
+ int tab_number = get_tabpage_arg(eap);
+ if (eap->errmsg == NULL) {
+ goto_tabpage(tab_number);
+ // Repeat this up to a 1000 times, because autocommands may
+ // mess up the lists.
+ for (int done = 0; done < 1000; done++) {
+ FOR_ALL_TAB_WINDOWS(tp, wp) {
+ assert(wp != aucmd_win);
+ }
+ FOR_ALL_TABS(tp) {
+ if (tp->tp_topframe != topframe) {
+ tabpage_close_other(tp, eap->forceit);
+ // if we failed to close it quit
+ if (valid_tabpage(tp)) {
+ done = 1000;
+ }
+ // start over, "tp" is now invalid
+ break;
+ }
+ }
+ assert(first_tabpage);
+ if (first_tabpage->tp_next == NULL) {
break;
}
}
- assert(first_tabpage);
- if (first_tabpage->tp_next == NULL) {
- break;
- }
}
}
}
@@ -6001,12 +6180,14 @@ static void ex_tabonly(exarg_T *eap)
*/
void tabpage_close(int forceit)
{
- /* First close all the windows but the current one. If that worked then
- * close the last window in this tab, that will close it. */
- if (lastwin != firstwin)
- close_others(TRUE, forceit);
- if (lastwin == firstwin)
+ // First close all the windows but the current one. If that worked then
+ // close the last window in this tab, that will close it.
+ if (!ONE_WINDOW) {
+ close_others(true, forceit);
+ }
+ if (ONE_WINDOW) {
ex_win_close(forceit, curwin, NULL);
+ }
}
/*
@@ -6074,29 +6255,27 @@ void ex_all(exarg_T *eap)
static void ex_hide(exarg_T *eap)
{
- if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
- eap->errmsg = e_invarg;
- else {
- /* ":hide" or ":hide | cmd": hide current window */
- eap->nextcmd = check_nextcmd(eap->arg);
+ // ":hide" or ":hide | cmd": hide current window
if (!eap->skip) {
- if (eap->addr_count == 0)
- win_close(curwin, FALSE); /* don't free buffer */
- else {
- int winnr = 0;
- win_T *win;
-
- for (win = firstwin; win != NULL; win = win->w_next) {
- winnr++;
- if (winnr == eap->line2)
- break;
+ if (eap->addr_count == 0) {
+ win_close(curwin, false); // don't free buffer
+ } else {
+ int winnr = 0;
+ win_T *win = NULL;
+
+ FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
+ winnr++;
+ if (winnr == eap->line2) {
+ win = wp;
+ break;
+ }
+ }
+ if (win == NULL) {
+ win = lastwin;
+ }
+ win_close(win, false);
}
- if (win == NULL)
- win = lastwin;
- win_close(win, FALSE);
- }
}
- }
}
/*
@@ -6110,13 +6289,14 @@ static void ex_stop(exarg_T *eap)
autowrite_all();
}
ui_cursor_goto((int)Rows - 1, 0);
- ui_putc('\n');
+ ui_linefeed();
+ ui_flush();
+ ui_call_suspend(); // call machine specific function
ui_flush();
- ui_suspend(); /* call machine specific function */
maketitle();
- resettitle(); /* force updating the title */
+ resettitle(); // force updating the title
redraw_later_clear();
- ui_refresh(); /* may have resized window */
+ ui_refresh(); // may have resized window
}
}
@@ -6134,11 +6314,12 @@ static void ex_exit(exarg_T *eap)
text_locked_msg();
return;
}
- apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
- /* Refuse to quit when locked or when the buffer in the last window is
- * being closed (can only happen in autocommands). */
- if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
+ apply_autocmds(EVENT_QUITPRE, NULL, NULL, false, curbuf);
+ // Refuse to quit when locked or when the buffer in the last window is
+ // being closed (can only happen in autocommands).
+ if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0)) {
return;
+ }
// if more files or windows we won't exit
if (check_more(false, eap->forceit) == OK && only_one_window()) {
@@ -6477,6 +6658,8 @@ void tabpage_new(void)
*/
static void ex_tabnext(exarg_T *eap)
{
+ int tab_number;
+
switch (eap->cmdidx) {
case CMD_tabfirst:
case CMD_tabrewind:
@@ -6487,66 +6670,48 @@ static void ex_tabnext(exarg_T *eap)
break;
case CMD_tabprevious:
case CMD_tabNext:
- goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
- break;
- default: /* CMD_tabnext */
- goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
- break;
- }
-}
+ if (eap->arg && *eap->arg != NUL) {
+ char_u *p = eap->arg;
+ char_u *p_save = p;
-/*
- * :tabmove command
- */
-static void ex_tabmove(exarg_T *eap)
-{
- int tab_number;
-
- if (eap->arg && *eap->arg != NUL) {
- char_u *p = eap->arg;
- int relative = 0; /* argument +N/-N means: move N places to the
- * right/left relative to the current position. */
-
- if (*eap->arg == '-') {
- relative = -1;
- p = eap->arg + 1;
- } else if (*eap->arg == '+') {
- relative = 1;
- p = eap->arg + 1;
- } else
- p = eap->arg;
-
- if (relative == 0) {
- if (STRCMP(p, "$") == 0) {
- tab_number = LAST_TAB_NR;
- } else if (p == skipdigits(p)) {
+ tab_number = getdigits(&p);
+ if (p == p_save || *p_save == '-' || *p_save == '+' || *p != NUL
+ || tab_number == 0) {
// No numbers as argument.
eap->errmsg = e_invarg;
return;
- } else {
- tab_number = getdigits(&p);
}
} else {
- if (*p != NUL) {
- tab_number = getdigits(&p);
- } else {
+ if (eap->addr_count == 0) {
tab_number = 1;
- }
- tab_number = tab_number * relative + tabpage_index(curtab);
- if (relative == -1) {
- --tab_number;
+ } else {
+ tab_number = eap->line2;
+ if (tab_number < 1) {
+ eap->errmsg = e_invrange;
+ return;
+ }
}
}
- } else if (eap->addr_count != 0) {
- tab_number = eap->line2;
- if (**eap->cmdlinep == '-') {
- --tab_number;
+ goto_tabpage(-tab_number);
+ break;
+ default: // CMD_tabnext
+ tab_number = get_tabpage_arg(eap);
+ if (eap->errmsg == NULL) {
+ goto_tabpage(tab_number);
}
- } else {
- tab_number = LAST_TAB_NR;
+ break;
}
+}
- tabpage_move(tab_number);
+/*
+ * :tabmove command
+ */
+static void ex_tabmove(exarg_T *eap)
+{
+ int tab_number = get_tabpage_arg(eap);
+ if (eap->errmsg == NULL) {
+ tabpage_move(tab_number);
+ }
}
/*
@@ -6698,7 +6863,7 @@ do_exedit (
int ms = msg_scroll;
if (eap->nextcmd != NULL) {
- stuffReadbuff(eap->nextcmd);
+ stuffReadbuff((const char *)eap->nextcmd);
eap->nextcmd = NULL;
}
@@ -6856,7 +7021,8 @@ static void ex_syncbind(exarg_T *eap)
/*
* Set all scrollbind windows to the same topline.
*/
- for (curwin = firstwin; curwin; curwin = curwin->w_next) {
+ FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
+ curwin = wp;
if (curwin->w_p_scb) {
curbuf = curwin->w_buffer;
y = topline - curwin->w_topline;
@@ -6910,9 +7076,10 @@ static void ex_read(exarg_T *eap)
eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
}
- if (i == FAIL) {
- if (!aborting())
+ if (i != OK) {
+ if (!aborting()) {
EMSG2(_(e_notopen), eap->arg);
+ }
} else {
if (empty && exmode_active) {
/* Delete the empty line that remains. Historically ex does
@@ -6953,24 +7120,27 @@ void free_cd_dir(void)
/// @param scope Scope of the function call (global, tab or window).
void post_chdir(CdScope scope)
{
- // The local directory of the current window is always overwritten.
+ // Always overwrite the window-local CWD.
xfree(curwin->w_localdir);
curwin->w_localdir = NULL;
- // Overwrite the local directory of the current tab page for `cd` and `tcd`
+ // Overwrite the tab-local CWD for :cd, :tcd.
if (scope >= kCdScopeTab) {
- xfree(curtab->localdir);
- curtab->localdir = NULL;
+ xfree(curtab->tp_localdir);
+ curtab->tp_localdir = NULL;
}
if (scope < kCdScopeGlobal) {
- // If still in global directory, need to remember current directory as
- // global directory.
+ // If still in global directory, set CWD as the global directory.
if (globaldir == NULL && prev_dir != NULL) {
globaldir = vim_strsave(prev_dir);
}
}
+ char cwd[MAXPATHL];
+ if (os_dirname((char_u *)cwd, MAXPATHL) != OK) {
+ return;
+ }
switch (scope) {
case kCdScopeGlobal:
// We are now in the global directory, no need to remember its name.
@@ -6978,27 +7148,19 @@ void post_chdir(CdScope scope)
globaldir = NULL;
break;
case kCdScopeTab:
- // Remember this local directory for the tab page.
- if (os_dirname(NameBuff, MAXPATHL) == OK) {
- curtab->localdir = vim_strsave(NameBuff);
- }
+ curtab->tp_localdir = (char_u *)xstrdup(cwd);
break;
case kCdScopeWindow:
- // Remember this local directory for the window.
- if (os_dirname(NameBuff, MAXPATHL) == OK) {
- curwin->w_localdir = vim_strsave(NameBuff);
- }
+ curwin->w_localdir = (char_u *)xstrdup(cwd);
break;
case kCdScopeInvalid:
- // We should never get here
assert(false);
}
- shorten_fnames(TRUE);
+ shorten_fnames(true);
+ do_autocmd_dirchanged(cwd, scope);
}
-
-
/// `:cd`, `:tcd`, `:lcd`, `:chdir`, `:tchdir` and `:lchdir`.
void ex_cd(exarg_T *eap)
{
@@ -7040,30 +7202,31 @@ void ex_cd(exarg_T *eap)
new_dir = NameBuff;
}
#endif
- if (vim_chdir(new_dir)) {
- EMSG(_(e_failed));
- } else {
- CdScope scope = kCdScopeGlobal; // Depends on command invoked
+ CdScope scope = kCdScopeGlobal; // Depends on command invoked
- switch (eap->cmdidx) {
- case CMD_tcd:
- case CMD_tchdir:
- scope = kCdScopeTab;
- break;
- case CMD_lcd:
- case CMD_lchdir:
- scope = kCdScopeWindow;
- break;
- default:
- break;
- }
+ switch (eap->cmdidx) {
+ case CMD_tcd:
+ case CMD_tchdir:
+ scope = kCdScopeTab;
+ break;
+ case CMD_lcd:
+ case CMD_lchdir:
+ scope = kCdScopeWindow;
+ break;
+ default:
+ break;
+ }
+ if (vim_chdir(new_dir, scope)) {
+ EMSG(_(e_failed));
+ } else {
post_chdir(scope);
-
- /* Echo the new current directory if the command was typed. */
- if (KeyTyped || p_verbose >= 5)
+ // Echo the new current directory if the command was typed.
+ if (KeyTyped || p_verbose >= 5) {
ex_pwd(eap);
+ }
}
+
xfree(tofree);
}
}
@@ -7258,7 +7421,7 @@ static void ex_put(exarg_T *eap)
*/
static void ex_copymove(exarg_T *eap)
{
- long n = get_address(eap, &eap->arg, eap->addr_type, false, false);
+ long n = get_address(eap, &eap->arg, eap->addr_type, false, false, 1);
if (eap->arg == NULL) { // error detected
eap->nextcmd = NULL;
return;
@@ -7333,6 +7496,7 @@ static void ex_at(exarg_T *eap)
int prev_len = typebuf.tb_len;
curwin->w_cursor.lnum = eap->line2;
+ check_cursor_col();
// Get the register name. No name means use the previous one.
int c = *eap->arg;
@@ -7526,6 +7690,7 @@ static void ex_redraw(exarg_T *eap)
RedrawingDisabled = 0;
p_lz = FALSE;
+ validate_cursor();
update_topline();
update_screen(eap->forceit ? CLEAR :
VIsual_active ? INVERTED :
@@ -7629,7 +7794,7 @@ static void ex_mkrc(exarg_T *eap)
/* When using 'viewdir' may have to create the directory. */
if (using_vdir && !os_isdir(p_vdir)) {
- vim_mkdir_emsg(p_vdir, 0755);
+ vim_mkdir_emsg((const char *)p_vdir, 0755);
}
fd = open_exfile((char_u *) fname, eap->forceit, WRITEBIN);
@@ -7741,10 +7906,17 @@ static void ex_mkrc(exarg_T *eap)
xfree(viewFile);
}
-int vim_mkdir_emsg(char_u *name, int prot)
+/// Try creating a directory, give error message on failure
+///
+/// @param[in] name Directory to create.
+/// @param[in] prot Directory permissions.
+///
+/// @return OK in case of success, FAIL otherwise.
+int vim_mkdir_emsg(const char *const name, const int prot)
+ FUNC_ATTR_NONNULL_ALL
{
int ret;
- if ((ret = os_mkdir((char *)name, prot)) != 0) {
+ if ((ret = os_mkdir(name, prot)) != 0) {
EMSG3(_(e_mkdir), name, os_strerror(ret));
return FAIL;
}
@@ -8148,7 +8320,7 @@ static void ex_tag_cmd(exarg_T *eap, char_u *name)
break;
default: /* ":tag" */
if (p_cst && *eap->arg != NUL) {
- do_cstag(eap);
+ ex_cstag(eap);
return;
}
cmd = DT_TAG;
@@ -8243,9 +8415,9 @@ eval_vars (
char_u *resultbuf = NULL;
size_t resultlen;
buf_T *buf;
- int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
- int skip_mod = FALSE;
- char_u strbuf[30];
+ int valid = VALID_HEAD | VALID_PATH; // Assume valid result.
+ int skip_mod = false;
+ char strbuf[30];
*errormsg = NULL;
if (escaped != NULL)
@@ -8321,8 +8493,8 @@ eval_vars (
*usedlen = 1;
return NULL;
}
- result = list_find_str(get_vim_var_list(VV_OLDFILES),
- (long)i);
+ result = (char_u *)tv_list_find_str(get_vim_var_list(VV_OLDFILES),
+ i - 1);
if (result == NULL) {
*errormsg = (char_u *)"";
return NULL;
@@ -8377,8 +8549,8 @@ eval_vars (
"E496: no autocommand buffer number to substitute for \"<abuf>\"");
return NULL;
}
- sprintf((char *)strbuf, "%d", autocmd_bufnr);
- result = strbuf;
+ snprintf(strbuf, sizeof(strbuf), "%d", autocmd_bufnr);
+ result = (char_u *)strbuf;
break;
case SPEC_AMATCH: /* match name for autocommand */
@@ -8403,8 +8575,8 @@ eval_vars (
*errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
return NULL;
}
- sprintf((char *)strbuf, "%" PRId64, (int64_t)sourcing_lnum);
- result = strbuf;
+ snprintf(strbuf, sizeof(strbuf), "%" PRIdLINENR, sourcing_lnum);
+ result = (char_u *)strbuf;
break;
default:
// should not happen
@@ -8680,15 +8852,16 @@ makeopens (
*/
tab_firstwin = firstwin; /* first window in tab page "tabnr" */
tab_topframe = topframe;
- for (tabnr = 1;; ++tabnr) {
+ for (tabnr = 1;; tabnr++) {
+ tabpage_T *tp = find_tabpage(tabnr);
+ if (tp == NULL) {
+ break; // done all tab pages
+ }
+
int need_tabnew = false;
int cnr = 1;
if ((ssop_flags & SSOP_TABPAGES)) {
- tabpage_T *tp = find_tabpage(tabnr);
-
- if (tp == NULL)
- break; /* done all tab pages */
if (tp == curtab) {
tab_firstwin = firstwin;
tab_topframe = topframe;
@@ -8801,6 +8974,16 @@ makeopens (
if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
return FAIL;
+ // Take care of tab-local working directories if applicable
+ if (tp->tp_localdir) {
+ if (fputs("if has('nvim') | tcd ", fd) < 0
+ || ses_put_fname(fd, tp->tp_localdir, &ssop_flags) == FAIL
+ || fputs(" | endif", fd) < 0
+ || put_eol(fd) == FAIL) {
+ return FAIL;
+ }
+ }
+
/* Don't continue in another tab page when doing only the current one
* or when at the last tab page. */
if (!(ssop_flags & SSOP_TABPAGES))
@@ -9252,8 +9435,8 @@ static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
*p = '/';
}
- /* escape special characters */
- p = vim_strsave_fnameescape(sname, FALSE);
+ // Escape special characters.
+ p = (char_u *)vim_strsave_fnameescape((const char *)sname, false);
xfree(sname);
/* write the result */
@@ -9388,18 +9571,18 @@ void dialog_msg(char_u *buff, char *format, char_u *fname)
static void ex_behave(exarg_T *eap)
{
if (STRCMP(eap->arg, "mswin") == 0) {
- set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
- set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
- set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
- set_option_value((char_u *)"keymodel", 0L,
- (char_u *)"startsel,stopsel", 0);
+ set_option_value("selection", 0L, "exclusive", 0);
+ set_option_value("selectmode", 0L, "mouse,key", 0);
+ set_option_value("mousemodel", 0L, "popup", 0);
+ set_option_value("keymodel", 0L, "startsel,stopsel", 0);
} else if (STRCMP(eap->arg, "xterm") == 0) {
- set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
- set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
- set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
- set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
- } else
+ set_option_value("selection", 0L, "inclusive", 0);
+ set_option_value("selectmode", 0L, "", 0);
+ set_option_value("mousemodel", 0L, "extend", 0);
+ set_option_value("keymodel", 0L, "", 0);
+ } else {
EMSG2(_(e_invarg2), eap->arg);
+ }
}
/*
@@ -9415,6 +9598,16 @@ char_u *get_behave_arg(expand_T *xp, int idx)
return NULL;
}
+// Function given to ExpandGeneric() to obtain the possible arguments of the
+// ":messages {clear}" command.
+char_u *get_messages_arg(expand_T *xp FUNC_ATTR_UNUSED, int idx)
+{
+ if (idx == 0) {
+ return (char_u *)"clear";
+ }
+ return NULL;
+}
+
static TriState filetype_detect = kNone;
static TriState filetype_plugin = kNone;
static TriState filetype_indent = kNone;
@@ -9513,8 +9706,9 @@ void filetype_maybe_enable(void)
*/
static void ex_setfiletype(exarg_T *eap)
{
- if (!did_filetype)
- set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
+ if (!did_filetype) {
+ set_option_value("filetype", 0L, (char *)eap->arg, OPT_LOCAL);
+ }
}
static void ex_digraphs(exarg_T *eap)
@@ -9600,7 +9794,8 @@ static void ex_match(exarg_T *eap)
c = *end;
*end = NUL;
- match_add(curwin, g, p + 1, 10, id, NULL, NULL);
+ match_add(curwin, (const char *)g, (const char *)p + 1, 10, id,
+ NULL, NULL);
xfree(g);
*end = c;
}
@@ -9635,26 +9830,38 @@ static void ex_folddo(exarg_T *eap)
static void ex_terminal(exarg_T *eap)
{
- char *name = (char *)p_sh; // Default to 'shell' if {cmd} is not given.
- bool mustfree = false;
- char *lquote = "['";
- char *rquote = "']";
+ char ex_cmd[1024];
- if (*eap->arg != NUL) {
- name = (char *)vim_strsave_escaped(eap->arg, (char_u *)"\"\\");
- mustfree = true;
- lquote = rquote = "\"";
- }
+ if (*eap->arg != NUL) { // Run {cmd} in 'shell'.
+ char *name = (char *)vim_strsave_escaped(eap->arg, (char_u *)"\"\\");
+ snprintf(ex_cmd, sizeof(ex_cmd),
+ ":enew%s | call termopen(\"%s\")",
+ eap->forceit ? "!" : "", name);
+ xfree(name);
+ } else { // No {cmd}: run the job with tokenized 'shell'.
+ if (*p_sh == NUL) {
+ EMSG(_(e_shellempty));
+ return;
+ }
- char ex_cmd[512];
- snprintf(ex_cmd, sizeof(ex_cmd),
- ":enew%s | call termopen(%s%s%s) | startinsert",
- eap->forceit==TRUE ? "!" : "", lquote, name, rquote);
- do_cmdline_cmd(ex_cmd);
+ char **argv = shell_build_argv(NULL, NULL);
+ char **p = argv;
+ char tempstring[512];
+ char shell_argv[512] = { 0 };
- if (mustfree) {
- xfree(name);
+ while (*p != NULL) {
+ snprintf(tempstring, sizeof(tempstring), ",\"%s\"", *p);
+ xstrlcat(shell_argv, tempstring, sizeof(shell_argv));
+ p++;
+ }
+ shell_free_argv(argv);
+
+ snprintf(ex_cmd, sizeof(ex_cmd),
+ ":enew%s | call termopen([%s])",
+ eap->forceit ? "!" : "", shell_argv + 1);
}
+
+ do_cmdline_cmd(ex_cmd);
}
/// Checks if `cmd` is "previewable" (i.e. supported by 'inccommand').
@@ -9674,9 +9881,20 @@ bool cmd_can_preview(char_u *cmd)
if (*ea.cmd == '*') {
ea.cmd = skipwhite(ea.cmd + 1);
}
- find_command(&ea, NULL);
+ char_u *end = find_command(&ea, NULL);
+
+ switch (ea.cmdidx) {
+ case CMD_substitute:
+ case CMD_smagic:
+ case CMD_snomagic:
+ // Only preview once the pattern delimiter has been typed
+ if (*end && !ASCII_ISALNUM(*end)) {
+ return true;
+ }
+ break;
+ default:
+ break;
+ }
- return ea.cmdidx == CMD_substitute
- || ea.cmdidx == CMD_smagic
- || ea.cmdidx == CMD_snomagic;
+ return false;
}