neovim / neovim

Vim-fork focused on extensibility and usability

Home Page:https://neovim.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

VimL heredoc (<<HERE) for user commands

ZyX-I opened this issue · comments

Currently if one wants to emulate :python with user commands he will be stuck at implementing << EOF: there is no way for user commands to have multiline input, except for not adding -bar and using :execute 'MyCmd' "multiline\nstring". On the other hand it is impossible to just add command -herestring and expect it to work fine: not unless somebody wants to state that user commands must be defined not prior to its usage, but prior to its appearance (i.e. prior to the first function definition which happens to have that command inside) and can’t switch definition after that (or give up on having proper parser). Thus the suggestion: knowing that python << EOF\n{multiline string}\nEOF yields just the same result as execute 'python' "{multiline string}" I am suggesting to add :here command used like that:

here MyCmd << EOF
foo
EOF

and

if 1
  here! MyCmd << EOF
    First non-empty line appears as having zero indent when bang is used.
   And this appears as an error for it being less indented then the first one.
  EOF
endif

. The only thing which :here command will do is calling MyCmd with multiline string in <args>, universally and without requiring any additional support in MyCmd definition.

Additionally :here may accept range and forward it to MyCmd. Also :here! is good for built-in :python itself.

Implementation notes:

  • Currently the :{lua,python,...} <<EOF logic is hacked into ex_function.
  • That could be extended to support user commands, and would call out to provider#foo#Call() in that case (via eval_call_provider)
  • Add a flag to :command, e.g. :command -heredoc ...
  • Introduce an explicit builtin :here (?) command: :here Foo <<EOF (as suggested in the original issue description)

todo:

  • does the "closed" scope (local variables) need to be passed to the heredoc block?
  • can a heredoc block return a value?

Vim 8.1.1354 has it for :let (vim/vim@f5842c5).