Skip to content

Commit 4389f9c

Browse files
committed
patch 8.2.2228: Vim9: cannot use ":e #" because # starts a comment
Problem: Vim9: cannot use ":e #" because # starts a comment. Solution: Support using %% instead of #.
1 parent e462f52 commit 4389f9c

3 files changed

Lines changed: 91 additions & 31 deletions

File tree

src/ex_docmd.c

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8535,18 +8535,19 @@ find_cmdline_var(char_u *src, int *usedlen)
85358535
/*
85368536
* Evaluate cmdline variables.
85378537
*
8538-
* change '%' to curbuf->b_ffname
8539-
* '#' to curwin->w_alt_fnum
8540-
* '<cword>' to word under the cursor
8541-
* '<cWORD>' to WORD under the cursor
8542-
* '<cexpr>' to C-expression under the cursor
8543-
* '<cfile>' to path name under the cursor
8544-
* '<sfile>' to sourced file name
8545-
* '<stack>' to call stack
8546-
* '<slnum>' to sourced file line number
8547-
* '<afile>' to file name for autocommand
8548-
* '<abuf>' to buffer number for autocommand
8549-
* '<amatch>' to matching name for autocommand
8538+
* change "%" to curbuf->b_ffname
8539+
* "#" to curwin->w_alt_fnum
8540+
* "%%" to curwin->w_alt_fnum in Vim9 script
8541+
* "<cword>" to word under the cursor
8542+
* "<cWORD>" to WORD under the cursor
8543+
* "<cexpr>" to C-expression under the cursor
8544+
* "<cfile>" to path name under the cursor
8545+
* "<sfile>" to sourced file name
8546+
* "<stack>" to call stack
8547+
* "<slnum>" to sourced file line number
8548+
* "<afile>" to file name for autocommand
8549+
* "<abuf>" to buffer number for autocommand
8550+
* "<amatch>" to matching name for autocommand
85508551
*
85518552
* When an error is detected, "errormsg" is set to a non-NULL pointer (may be
85528553
* "" for error without a message) and NULL is returned.
@@ -8627,47 +8628,57 @@ eval_vars(
86278628
*/
86288629
else
86298630
{
8631+
int off = 0;
8632+
86308633
switch (spec_idx)
86318634
{
8632-
case SPEC_PERC: // '%': current file
8633-
if (curbuf->b_fname == NULL)
8634-
{
8635-
result = (char_u *)"";
8636-
valid = 0; // Must have ":p:h" to be valid
8637-
}
8638-
else
8635+
case SPEC_PERC:
8636+
if (!in_vim9script() || src[1] != '%')
86398637
{
8640-
result = curbuf->b_fname;
8641-
tilde_file = STRCMP(result, "~") == 0;
8638+
// '%': current file
8639+
if (curbuf->b_fname == NULL)
8640+
{
8641+
result = (char_u *)"";
8642+
valid = 0; // Must have ":p:h" to be valid
8643+
}
8644+
else
8645+
{
8646+
result = curbuf->b_fname;
8647+
tilde_file = STRCMP(result, "~") == 0;
8648+
}
8649+
break;
86428650
}
8643-
break;
8651+
// "%%" alternate file
8652+
off = 1;
8653+
// FALLTHROUGH
86448654

86458655
case SPEC_HASH: // '#' or "#99": alternate file
8646-
if (src[1] == '#') // "##": the argument list
8656+
if (off == 0 ? src[1] == '#' : src[2] == '%')
86478657
{
8658+
// "##" or "%%%": the argument list
86488659
result = arg_all();
86498660
resultbuf = result;
8650-
*usedlen = 2;
8661+
*usedlen = off + 2;
86518662
if (escaped != NULL)
86528663
*escaped = TRUE;
86538664
skip_mod = TRUE;
86548665
break;
86558666
}
8656-
s = src + 1;
8667+
s = src + off + 1;
86578668
if (*s == '<') // "#<99" uses v:oldfiles
86588669
++s;
86598670
i = (int)getdigits(&s);
8660-
if (s == src + 2 && src[1] == '-')
8671+
if (s == src + off + 2 && src[off + 1] == '-')
86618672
// just a minus sign, don't skip over it
86628673
s--;
86638674
*usedlen = (int)(s - src); // length of what we expand
86648675

8665-
if (src[1] == '<' && i != 0)
8676+
if (src[off + 1] == '<' && i != 0)
86668677
{
8667-
if (*usedlen < 2)
8678+
if (*usedlen < off + 2)
86688679
{
86698680
// Should we give an error message for #<text?
8670-
*usedlen = 1;
8681+
*usedlen = off + 1;
86718682
return NULL;
86728683
}
86738684
#ifdef FEAT_EVAL
@@ -8685,8 +8696,8 @@ eval_vars(
86858696
}
86868697
else
86878698
{
8688-
if (i == 0 && src[1] == '<' && *usedlen > 1)
8689-
*usedlen = 1;
8699+
if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
8700+
*usedlen = off + 1;
86908701
buf = buflist_findnr(i);
86918702
if (buf == NULL)
86928703
{

src/testdir/test_vim9_cmd.vim

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,53 @@ def Test_edit_wildcards()
2525
CheckDefFailure(['edit `="foo"'], 'E1083:')
2626
enddef
2727

28+
def Test_expand_alternate_file()
29+
var lines =<< trim END
30+
edit Xfileone
31+
var bone = bufnr()
32+
edit Xfiletwo
33+
var btwo = bufnr()
34+
edit Xfilethree
35+
var bthree = bufnr()
36+
37+
edit #
38+
assert_equal(bthree, bufnr())
39+
edit %%
40+
assert_equal(btwo, bufnr())
41+
edit %% # comment
42+
assert_equal(bthree, bufnr())
43+
edit %%yy
44+
assert_equal('Xfiletwoyy', bufname())
45+
46+
exe "edit %%" .. bone
47+
assert_equal(bone, bufnr())
48+
exe "edit %%" .. btwo .. "xx"
49+
assert_equal('Xfiletwoxx', bufname())
50+
51+
next Xfileone Xfiletwo Xfilethree
52+
assert_equal('Xfileone', argv(0))
53+
assert_equal('Xfiletwo', argv(1))
54+
assert_equal('Xfilethree', argv(2))
55+
next %%%zz
56+
assert_equal('Xfileone', argv(0))
57+
assert_equal('Xfiletwo', argv(1))
58+
assert_equal('Xfilethreezz', argv(2))
59+
60+
v:oldfiles = ['Xonefile', 'Xtwofile']
61+
edit %%<1
62+
assert_equal('Xonefile', bufname())
63+
edit %%<2
64+
assert_equal('Xtwofile', bufname())
65+
assert_fails('edit %%<3', 'E684:')
66+
67+
edit Xfileone.vim
68+
edit Xfiletwo
69+
edit %%:r
70+
assert_equal('Xfileone', bufname())
71+
END
72+
CheckDefAndScriptSuccess(lines)
73+
enddef
74+
2875
def Test_global_backtick_expansion()
2976
new
3077
setline(1, 'xx')

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ static char *(features[]) =
750750

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2228,
753755
/**/
754756
2227,
755757
/**/

0 commit comments

Comments
 (0)