On 09-Apr-2011 14:54, Andy Wokula wrote:
> For example, I have a command :InFunc .  It's quite a trivial command,
> it takes an argument Ex-command and executes it within a function.
> Purpose is to automatically restore the highlighting state and the last
> search pattern.
>     :h function-search-undo
> 
> Thus, mostly the Ex-command will be :global or :substitute .
> 
> Problem: :global has the default range "1,$" whereas :substitute has the
> default range ".".  For :InFunc, I'm urged to specify a default range
> (e.g. either -range (current line) or -range=% (whole buffer)).  I don't
> want that, instead I want the default range of the argument command to
> be in effect.  But at the moment, it's not possible to check for an
> empty range.
> 
> 
> " What I use now:
>     :[range]InFunc {cmd}        " execute :[range]{cmd}, for :subst
>     :[range]InFunc! {cmd}       " execute :{cmd}, for :global
> 
> com! -bang -range -nargs=+  InFunc <line1>,<line2>call InFunc(<bang>0, 
> <q-args>)
> 
> func! InFunc(bang, cmd) range
>     if a:bang
>         exec a:cmd
>     else
>         exec a:firstline.",".a:lastline. a:cmd
>     endif
> endfunc
> 
> 
> " I'd like to write the above this way:
> 
> com! -range=NoDefault -nargs=+  InFunc <range>call InFunc(<q-args>)
> 
> func! InFunc(cmd) range
>     exec a:range. a:cmd
> endfunc
> 
> 
> Oops, we would also need a new variable  a:range !
> 
> Maybe, actually, <range> and a:range are not needed, I just want to be
> able to check for an empty range:
> 
> 
> com! -range -nargs=+  InFunc <line1>,<line2>call InFunc(<q-args>)
> 
> func! InFunc(cmd) range
>     if range_is_empty()
>         exec a:cmd
>     else
>         exec a:firstline.",".a:lastline. a:cmd
>     endif
> endfunc
> 
> 
> This would just a require a new function range_is_empty().

Unless there are additional use cases where this could be useful, my first hunch
is that it's not worth the effort.

My first idea to implement your use case (with current Vim functionality) was a
cabbr <expr>, and then get the range via getcmdline(), extract and store it
somewhere, and then replace the InFunc cabbr with InFuncImpl, which would then
simply prepend the stored range to the passed command. The problem with this is
that for the cabbr to be expanded, there would need to be a whitespace before
it, so :2,4 InFunc instead of :2,4InFunc would need to be used. But the cabbr
could be replaced with a cmap, that wouldn't have that problem.

Reflecting on it some more, I think that the :InFunc is cumbersome (to type),
anyway. I would solve this via a :cmap <S-CR> which restores the previous
pattern from the search history. So instead of prepending InFunc to the Ex
command, I would just conclude the Ex command with <S-CR> instead of <CR>.

-- regards, ingo

-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

Raspunde prin e-mail lui