summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2021-11-29 12:12:43 +0000
committerBram Moolenaar <Bram@vim.org>2021-11-29 12:12:43 +0000
commitaf377e34b01ba00f9520d2b9de1f911e72db0114 (patch)
treee3863b6aeb06509afdd60fa7448163268f24aca1
parent293eb9ba4669b1500370502397d399681e7098f0 (diff)
patch 8.2.3694: cannot use quotes in the count of an Ex commandv8.2.3694
Problem: Cannot use quotes in the count of an Ex command. Solution: Add getdigits_quoted(). Give an error when misplacing a quote in a range. (closes #9240)
-rw-r--r--src/charset.c34
-rw-r--r--src/ex_docmd.c16
-rw-r--r--src/proto/charset.pro1
-rw-r--r--src/testdir/test_usercommands.vim28
-rw-r--r--src/version.c2
5 files changed, 78 insertions, 3 deletions
diff --git a/src/charset.c b/src/charset.c
index 2c46f7ad04..0c17140c75 100644
--- a/src/charset.c
+++ b/src/charset.c
@@ -1748,7 +1748,7 @@ skiptowhite_esc(char_u *p)
}
/*
- * Getdigits: Get a number from a string and skip over it.
+ * Get a number from a string and skip over it.
* Note: the argument is a pointer to a char_u pointer!
*/
long
@@ -1767,6 +1767,38 @@ getdigits(char_u **pp)
}
/*
+ * Like getdigits() but allow for embedded single quotes.
+ */
+ long
+getdigits_quoted(char_u **pp)
+{
+ char_u *p = *pp;
+ long retval = 0;
+
+ if (*p == '-')
+ ++p;
+ while (VIM_ISDIGIT(*p))
+ {
+ if (retval >= LONG_MAX / 10 - 10)
+ retval = LONG_MAX;
+ else
+ retval = retval * 10 - '0' + *p;
+ ++p;
+ if (in_vim9script() && *p == '\'' && VIM_ISDIGIT(p[1]))
+ ++p;
+ }
+ if (**pp == '-')
+ {
+ if (retval == LONG_MAX)
+ retval = LONG_MIN;
+ else
+ retval = -retval;
+ }
+ *pp = p;
+ return retval;
+}
+
+/*
* Return TRUE if "lbuf" is empty or only contains blanks.
*/
int
diff --git a/src/ex_docmd.c b/src/ex_docmd.c
index 6d2923ccb2..523d8af30e 100644
--- a/src/ex_docmd.c
+++ b/src/ex_docmd.c
@@ -2402,7 +2402,7 @@ do_one_cmd(
&& (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
|| VIM_ISWHITE(*p)))
{
- n = getdigits(&ea.arg);
+ n = getdigits_quoted(&ea.arg);
ea.arg = skipwhite(ea.arg);
if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
{
@@ -3950,10 +3950,11 @@ excmd_get_argt(cmdidx_T idx)
*/
char_u *
skip_range(
- char_u *cmd,
+ char_u *cmd_start,
int skip_star, // skip "*" used for Visual range
int *ctx) // pointer to xp_context or NULL
{
+ char_u *cmd = cmd_start;
unsigned delim;
while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
@@ -3967,6 +3968,17 @@ skip_range(
}
else if (*cmd == '\'')
{
+ char_u *p = cmd;
+
+ // a quote is only valid at the start or after a separator
+ while (p > cmd_start)
+ {
+ --p;
+ if (!VIM_ISWHITE(*p))
+ break;
+ }
+ if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
+ break;
if (*++cmd == NUL && ctx != NULL)
*ctx = EXPAND_NOTHING;
}
diff --git a/src/proto/charset.pro b/src/proto/charset.pro
index 883f39300d..c71188de35 100644
--- a/src/proto/charset.pro
+++ b/src/proto/charset.pro
@@ -54,6 +54,7 @@ int vim_tolower(int c);
char_u *skiptowhite(char_u *p);
char_u *skiptowhite_esc(char_u *p);
long getdigits(char_u **pp);
+long getdigits_quoted(char_u **pp);
int vim_isblankline(char_u *lbuf);
void vim_str2nr(char_u *start, int *prep, int *len, int what, varnumber_T *nptr, uvarnumber_T *unptr, int maxlen, int strict);
int hex2nr(int c);
diff --git a/src/testdir/test_usercommands.vim b/src/testdir/test_usercommands.vim
index 6707a03964..d560f49488 100644
--- a/src/testdir/test_usercommands.vim
+++ b/src/testdir/test_usercommands.vim
@@ -677,4 +677,32 @@ func Test_delcommand_buffer()
call assert_equal(0, exists(':Global'))
endfunc
+def Test_count_with_quotes()
+ command -count GetCount g:nr = <count>
+ execute("GetCount 1'2")
+ assert_equal(12, g:nr)
+ execute("GetCount 1'234'567")
+ assert_equal(1'234'567, g:nr)
+
+ execute("GetCount 1'234'567'890'123'456'789'012")
+ assert_equal(v:sizeoflong == 8 ? 9223372036854775807 : 2147483647, g:nr)
+
+ # TODO: test with negative number once this is supported
+
+ assert_fails("GetCount '12", "E488:")
+ assert_fails("GetCount 12'", "E488:")
+ assert_fails("GetCount 1''2", "E488:")
+
+ assert_fails(":1'2GetCount", 'E492:')
+ new
+ setline(1, 'text')
+ normal ma
+ execute(":1, 'aprint")
+ bwipe!
+
+ unlet g:nr
+ delcommand GetCount
+enddef
+
+
" vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/version.c b/src/version.c
index b7fe97b2b7..623afb2def 100644
--- a/src/version.c
+++ b/src/version.c
@@ -758,6 +758,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 3694,
+/**/
3693,
/**/
3692,