# HG changeset patch # User Bram Moolenaar # Date 1648930503 -7200 # Node ID 2fd2ce8a556cbd716dfe512e5cf46c1633222703 # Parent 548b4ab77157d485153683c0862931f7e3f8b3ce patch 8.2.4667: expandcmd() fails on an error Commit: https://github.com/vim/vim/commit/5018a836c030988944a9bbe2fd2e538bf5261a72 Author: Yegappan Lakshmanan Date: Sat Apr 2 21:12:21 2022 +0100 patch 8.2.4667: expandcmd() fails on an error Problem: expandcmd() fails on an error. Solution: On failure return the command unmodified. (yegappan Lakshmanan, closes #10063) diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt --- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -2299,10 +2299,13 @@ expandcmd({string}) *expandcmd()* like with |expand()|, and environment variables, anywhere in {string}. "~user" and "~/path" are only expanded at the start. - Returns the expanded string. Example: > + Returns the expanded string. If an error is encountered + during expansion, the unmodified {string} is returned. + Example: > :echo expandcmd('make %<.o') - -< Can also be used as a |method|: > +< make /path/runtime/doc/builtin.o ~ + + Can also be used as a |method|: > GetCommand()->expandcmd() < extend({expr1}, {expr2} [, {expr3}]) *extend()* diff --git a/src/evalfunc.c b/src/evalfunc.c --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -4168,9 +4168,9 @@ f_expandcmd(typval_T *argvars, typval_T eap.nextcmd = NULL; eap.cmdidx = CMD_USER; + ++emsg_off; expand_filename(&eap, &cmdstr, &errormsg); - if (errormsg != NULL && *errormsg != NUL) - emsg(errormsg); + --emsg_off; rettv->vval.v_string = cmdstr; } @@ -4444,8 +4444,7 @@ common_function(typval_T *argvars, typva arg_idx = 0; else if (list->lv_len > MAX_FUNC_ARGS) { - emsg_funcname((char *)e_too_many_arguments_for_function_str, - s); + emsg_funcname(e_too_many_arguments_for_function_str, s); vim_free(name); goto theend; } diff --git a/src/testdir/test_expand.vim b/src/testdir/test_expand.vim --- a/src/testdir/test_expand.vim +++ b/src/testdir/test_expand.vim @@ -78,10 +78,11 @@ func Test_expandcmd() edit a1a2a3.rb call assert_equal('make b1b2b3.rb a1a2a3 Xfile.o', expandcmd('make %:gs?a?b? %< #<.o')) - call assert_fails('call expandcmd("make ")', 'E495:') - call assert_fails('call expandcmd("make ")', 'E495:') + call assert_equal('make ', expandcmd("make ")) + call assert_equal('make ', expandcmd("make ")) + call assert_equal('make ', expandcmd("make ")) enew - call assert_fails('call expandcmd("make %")', 'E499:') + call assert_equal('make %', expandcmd("make %")) let $FOO="blue\tsky" call setline(1, "$FOO") call assert_equal("grep pat blue\tsky", expandcmd('grep pat ')) @@ -94,6 +95,11 @@ func Test_expandcmd() let $FOO= "foo bar baz" call assert_equal("e foo bar baz", expandcmd("e $FOO")) + if has('unix') + " test for using the shell to expand a command argument + call assert_equal('{1..4}', expandcmd('{1..4}')) + endif + unlet $FOO close! endfunc @@ -101,14 +107,14 @@ endfunc " Test for expanding , and outside of sourcing a script func Test_source_sfile() let lines =<< trim [SCRIPT] - :call assert_fails('echo expandcmd("")', 'E498:') - :call assert_fails('echo expandcmd("")', 'E842:') - :call assert_fails('echo expandcmd("")', 'E961:') - :call assert_fails('call expandcmd("edit ")', 'E446:') - :call assert_fails('call expandcmd("edit #")', 'E194:') - :call assert_fails('call expandcmd("edit #<2")', 'E684:') - :call assert_fails('call expandcmd("edit ")', 'E348:') - :call assert_fails('call expandcmd("edit ")', 'E348:') + :call assert_equal('', expandcmd("")) + :call assert_equal('', expandcmd("")) + :call assert_equal('', expandcmd("")) + :call assert_equal('edit ', expandcmd("edit ")) + :call assert_equal('edit #', expandcmd("edit #")) + :call assert_equal('edit #<2', expandcmd("edit #<2")) + :call assert_equal('edit ', expandcmd("edit ")) + :call assert_equal('edit ', expandcmd("edit ")) :call assert_fails('autocmd User MyCmd echo ""', 'E498:') :call writefile(v:errors, 'Xresult') :qall! diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -751,6 +751,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 4667, +/**/ 4666, /**/ 4665,