vim - How to pass arguments from :command to function? -


i want write command, command have format:

[range]mycmd[!] [count] [onearg] [flags] 

or

[range]mycmd[!] [onearg] [count] [flags] 

similiar

:[range]p[rint] [count] [flags] 

or

:[range]d[elete] [x] {count} 

the command mycmd call function work. wrote line (won't work)

command! -nargs=+ -range -bang -count=0  mycmd <line1>,<line2>call myfunc(<q-args>, "<bang>","<count>", .... ..) 

here problem is, how handle arguments:

  • problem 1 read help, -range , -count cannot not used @ same time, need them both. took vim commands, :delete, :print, when use commands, give both range , count. found vim doc has 2 entries of same command if {count} or [count] available. like:

    :[range]d[elete] [x]     :[range]d[elete] [x] {count} 

or

:[range]p[rint] [flags] :[range]p[rint] {count} [flags] 

why that? why not [count] ? (this another small question)

  • problem 2 design, arguments optional, have default values(see table below). how can pass arguments function (i checked <q-args> , <f-args>) , how can distinguish user-inputs argument? example user gives

    :mycmd 5 g

     5 count? or arg?   g arg or flag?  or '5 g' arg?  

here default values args:

    argname | default value | description     -------------------------------------     range   | current line  | function xxx ( ) range     count   | 0             | <count>     bang    | ""            | easy too, <bang>     arg     | " "           | argument have space     flags   | ""            | no space allowed flags     ------------------------------------- 

thanks.

for complex custom command, cannot rely solely on vim's limited parsing options; can have handle range -range, have parse combination of arguments yourself.

:command! -nargs=* -range -bang mycmd <line1>,<line2>call myfunc(<bang>0, <q-args>) 

inside myfunc (which should defined :function myfunc( ... ) range invoked once), split() arguments on whitespace. alternatively, define function use variable number of arguments , use <f-args>, note number of function arguments limited (to 20 iirc).

the <bang>0 nice trick transform bang boolean, need.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -