patch 8.1.1510: a plugin cannot easily expand a command like done internally
Problem: A plugin cannot easily expand a command like done internally.
Solution: Add the expandcmd() function. (Yegappan Lakshmanan, closes #4514)
diff --git a/src/evalfunc.c b/src/evalfunc.c
index 4297379..2d8bf52 100644
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -149,6 +149,7 @@
static void f_exp(typval_T *argvars, typval_T *rettv);
#endif
static void f_expand(typval_T *argvars, typval_T *rettv);
+static void f_expandcmd(typval_T *argvars, typval_T *rettv);
static void f_extend(typval_T *argvars, typval_T *rettv);
static void f_feedkeys(typval_T *argvars, typval_T *rettv);
static void f_filereadable(typval_T *argvars, typval_T *rettv);
@@ -646,6 +647,7 @@
{"exp", 1, 1, f_exp},
#endif
{"expand", 1, 3, f_expand},
+ {"expandcmd", 1, 1, f_expandcmd},
{"extend", 2, 3, f_extend},
{"feedkeys", 1, 2, f_feedkeys},
{"file_readable", 1, 1, f_filereadable}, /* obsolete */
@@ -3791,6 +3793,35 @@
}
/*
+ * "expandcmd()" function
+ * Expand all the special characters in a command string.
+ */
+ static void
+f_expandcmd(typval_T *argvars, typval_T *rettv)
+{
+ exarg_T eap;
+ char_u *cmdstr;
+ char *errormsg = NULL;
+
+ rettv->v_type = VAR_STRING;
+ cmdstr = vim_strsave(tv_get_string(&argvars[0]));
+
+ memset(&eap, 0, sizeof(eap));
+ eap.cmd = cmdstr;
+ eap.arg = cmdstr;
+ eap.argt |= NOSPC;
+ eap.usefilter = FALSE;
+ eap.nextcmd = NULL;
+ eap.cmdidx = CMD_USER;
+
+ expand_filename(&eap, &cmdstr, &errormsg);
+ if (errormsg != NULL && *errormsg != NUL)
+ emsg(errormsg);
+
+ rettv->vval.v_string = cmdstr;
+}
+
+/*
* "extend(list, list [, idx])" function
* "extend(dict, dict [, action])" function
*/
diff --git a/src/testdir/test_expand.vim b/src/testdir/test_expand.vim
index b4f1363..bd8021f 100644
--- a/src/testdir/test_expand.vim
+++ b/src/testdir/test_expand.vim
@@ -47,3 +47,37 @@
call assert_match('\~', expand('%:p'))
bwipe!
endfunc
+
+func Test_expandcmd()
+ let $FOO = 'Test'
+ call assert_equal('e x/Test/y', expandcmd('e x/$FOO/y'))
+ unlet $FOO
+
+ new
+ edit Xfile1
+ call assert_equal('e Xfile1', expandcmd('e %'))
+ edit Xfile2
+ edit Xfile1
+ call assert_equal('e Xfile2', expandcmd('e #'))
+ edit Xfile2
+ edit Xfile3
+ edit Xfile4
+ let bnum = bufnr('Xfile2')
+ call assert_equal('e Xfile2', expandcmd('e #' . bnum))
+ call setline('.', 'Vim!@#')
+ call assert_equal('e Vim', expandcmd('e <cword>'))
+ call assert_equal('e Vim!@#', expandcmd('e <cWORD>'))
+ enew!
+ edit Xfile.java
+ call assert_equal('e Xfile.py', expandcmd('e %:r.py'))
+ call assert_equal('make abc.java', expandcmd('make abc.%:e'))
+ call assert_equal('make Xabc.java', expandcmd('make %:s?file?abc?'))
+ edit a1a2a3.rb
+ call assert_equal('make b1b2b3.rb a1a2a3 Xfile.o', expandcmd('make %:gs?a?b? %< #<.o'))
+
+ call assert_fails('call expandcmd("make <afile>")', 'E495:')
+ call assert_fails('call expandcmd("make <afile>")', 'E495:')
+ enew
+ call assert_fails('call expandcmd("make %")', 'E499:')
+ close
+endfunc
diff --git a/src/version.c b/src/version.c
index b392cfd..79faf7b 100644
--- a/src/version.c
+++ b/src/version.c
@@ -778,6 +778,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1510,
+/**/
1509,
/**/
1508,