Am 13.04.2011 um 17:07 schrieb Andy Wokula:
Am 09.04.2011 20:27, schrieb Ingo Karkat:
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.

It's more about consistency:

When you define a mapping, you can check if a count was given or not
(check v:count >= 1).

When you define a command with -range, you cannot check if a range was
given or not.

TIL it is indeed possible to check if a range was given or not:

:com! -range Crwg :echo (<count>==-1 ? 'No range was given' : 'The given range is 
<line1>,<line2>')

Works with Vim 7.0 onwards.

Here is another example:

DrChip's AsNeeded script comes with an ANX command:
     :ANX {cmd}

ANX loads the script that defines the Ex-command {cmd} and immediately
executes {cmd}.  It would be nice if :ANX could execute {cmd} with a
range.  But this cannot be done at the moment, because it's unclear if
{cmd} accepts a range and what the default range of {cmd} will be.

Again, it would be nice if we could use <range> in place of <line1>,<line2>:

     :com! -range -nargs=1 ANX  call LoadCmd(<q-args>)| <range><args>


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.

For mappings I have a solution:

     " remove trailing whitespace (except for the "-- " line in mails):
     :no <Leader>ss  :v/^-- $/s/\s\+$//e<C-B>InFunc! <C-E>

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>.

It's just that I want :InFunc to do this work.  For example, another
thing we can't check for is the highlighting state of the last search
pattern, InFunc restores it automatically.



--
Andy

--
--
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

--- You received this message because you are subscribed to the Google Groups "vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to vim_dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Raspunde prin e-mail lui