Skip to content

Conversation

@yegappan
Copy link
Member

@yegappan yegappan commented Jun 8, 2019

The expand() function can be used to expand special characters. If the special
characters appears anywhere in the command string, then this cannot be used.

Vim supports expanding special characters anywhere in a command string
for internal commands. For example, the 'makeprg' option accepts a command
string where special characters can occur anywhere in the string.
But a Vim plugin cannot support this. So plugins like dispatch.vim use a complicated
function to expand the special characters (look at the dispatch#expand() function in
https://github.com/tpope/vim-dispatch/blob/master/autoload/dispatch.vim).

This patch adds a expandcmd() function that can be used to expand all the
special characters in a command string.

@codecov-io
Copy link

codecov-io commented Jun 8, 2019

Codecov Report

Merging #4514 into master will decrease coverage by 0.22%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4514      +/-   ##
==========================================
- Coverage   80.63%    80.4%   -0.23%     
==========================================
  Files         110      110              
  Lines      143701   141088    -2613     
==========================================
- Hits       115874   113448    -2426     
+ Misses      27827    27640     -187
Impacted Files Coverage Δ
src/evalfunc.c 89.19% <100%> (-0.1%) ⬇️
src/version.c 56.75% <0%> (-32.19%) ⬇️
src/libvterm/src/unicode.c 87.17% <0%> (-7.7%) ⬇️
src/if_cscope.c 75.5% <0%> (-0.93%) ⬇️
src/gui.c 57.2% <0%> (-0.91%) ⬇️
src/search.c 81.22% <0%> (-0.84%) ⬇️
src/move.c 86.58% <0%> (-0.72%) ⬇️
src/ui.c 54.98% <0%> (-0.7%) ⬇️
src/tag.c 77.82% <0%> (-0.69%) ⬇️
src/gui_gtk_x11.c 48.5% <0%> (-0.62%) ⬇️
... and 85 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 260addf...d708abb. Read the comment docs.

@brammool
Copy link
Contributor

brammool commented Jun 9, 2019 via email

@vim-ml
Copy link

vim-ml commented Jun 9, 2019 via email

@vim-ml
Copy link

vim-ml commented Jun 9, 2019 via email

@vim-ml
Copy link

vim-ml commented Jun 9, 2019 via email

@vim-ml
Copy link

vim-ml commented Jun 15, 2019 via email

@vim-ml
Copy link

vim-ml commented Jun 15, 2019 via email

manuelschiller pushed a commit to manuelschiller/vim that referenced this pull request Nov 10, 2019
…ernally

Problem:    A plugin cannot easily expand a command like done internally.
Solution:   Add the expandcmd() function. (Yegappan Lakshmanan, closes vim#4514)
janlazo added a commit to janlazo/neovim that referenced this pull request Feb 22, 2020
… internally

Problem:    A plugin cannot easily expand a command like done internally.
Solution:   Add the expandcmd() function. (Yegappan Lakshmanan, closes vim/vim#4514)
vim/vim@80dad48
janlazo added a commit to janlazo/neovim that referenced this pull request Feb 22, 2020
… internally

Problem:    A plugin cannot easily expand a command like done internally.
Solution:   Add the expandcmd() function. (Yegappan Lakshmanan, closes vim/vim#4514)
vim/vim@80dad48
janlazo added a commit to janlazo/neovim that referenced this pull request Feb 22, 2020
… internally

Problem:    A plugin cannot easily expand a command like done internally.
Solution:   Add the expandcmd() function. (Yegappan Lakshmanan, closes vim/vim#4514)
vim/vim@80dad48
janlazo added a commit to janlazo/neovim that referenced this pull request Feb 23, 2020
… internally

Problem:    A plugin cannot easily expand a command like done internally.
Solution:   Add the expandcmd() function. (Yegappan Lakshmanan, closes vim/vim#4514)
vim/vim@80dad48
janlazo added a commit to janlazo/neovim that referenced this pull request Feb 25, 2020
… internally

Problem:    A plugin cannot easily expand a command like done internally.
Solution:   Add the expandcmd() function. (Yegappan Lakshmanan, closes vim/vim#4514)
vim/vim@80dad48
janlazo added a commit to janlazo/neovim that referenced this pull request Feb 28, 2020
… internally

Problem:    A plugin cannot easily expand a command like done internally.
Solution:   Add the expandcmd() function. (Yegappan Lakshmanan, closes vim/vim#4514)
vim/vim@80dad48
janlazo added a commit to janlazo/neovim that referenced this pull request Mar 1, 2020
… internally

Problem:    A plugin cannot easily expand a command like done internally.
Solution:   Add the expandcmd() function. (Yegappan Lakshmanan, closes vim/vim#4514)
vim/vim@80dad48
@lacygoill
Copy link

@yegappan Thank you for implementing expandcmd(). I would like to use it but I have 2 questions:

Would it make sense for the function to also expand // into /last search pattern/?

vim/runtime/doc/quickfix.txt

Lines 1026 to 1027 in 7a3330f

If {pattern} is empty (e.g. // is specified), the last
used search pattern is used. |last-pattern|

As an example, it could perform this expansion:

/my pattern
:echo expandcmd('vim //gj %')
vim /my pattern/gj current_file

Also, expandcmd() does not escape special characters when they're generated by an expansion (e.g. a filename containing the literal character %). For %, #, !, I guess it's not an issue, because we can use fnameescape(); but for spaces, it's different:

$ vim /tmp/foo\ bar/baz
:echo expandcmd('vim /pat/ %')

vim /pat/ /tmp/foo bar/baz

In the expansion, how can we know whether the original command was grepping into the file /tmp/foo bar/baz, or in the files /tmp/foo and $PWD/bar/baz?

Shouldn't expandcmd() at least escape a space when it's used in a filename?

vim /pat/ /tmp/foo\ bar/baz
                  ^

@vim-ml
Copy link

vim-ml commented Aug 28, 2020 via email

@lacygoill
Copy link

I think you can use the ":S" filename modifier to escape the special characters in the file name.

Ok, but I don't think it works when expanding ## into the filenames of the arglist.

$ cd /tmp
$ vim a\ b c d\ e
:echo expandcmd('vim /pat/ ##:S')

vim /pat/ a b c d e:S

I guess we need to handle ## specially:

:echo expandcmd('vim /pat/ ' .. argv()->map({_, v -> shellescape(v, 1)})->join())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants