Am 23.03.2018 um 19:16 schrieb Ingo Karkat:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

On 23-Mar-2018 17:28 +0100, 'Andy Wokula' via vim_dev wrote:
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.

How did you dig up that old thread?! (But thanks for following up!)

Oops that old?  Feels like yesterday.

I've found out about that, too:

To distinguish between no given range (then defaulting to something like the
last modified range) and a range, use -range=-1 and check <count>:
     :command! -range=-1 Test echomsg <count> == -1 ? "'[,']" : <line1> . ',' . 
<line2>
This emulates -range=% but allows :0Test (passed via first argument boolean
flag), too:
     :command! -range=-1 Test call Test((<count> == 0), (<count> == -1 && <line2> == 1 ? 
'1,' . line('$') : '<line1>,<line2>'))

Amazing.

Right, it bothered me at the same time why a range "0" is always
turned into "1".

Best thing: I tried with gVim 5.7, it works there too.
A really old feature this is!

- -- regards, ingo
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAEBCAAGBQJatURcAAoJEA7ziXlAzQ/vsH4H/3mMbpNKoZJbgYEUmlIJAvv+
7zUphcl6TnyslZwrtz3BSDP4vcQpkFBvsMxwkjRd5z/i3+CtHC8w2v4AfgQr/SdH
fSdMipYM7JeqddWnaD37GlWyIm+gFoaPpuOuPdybnkXGYLs9A6YFZXX1+59OYXR8
QEuahc8/3M4ye9I6PVzqW/IHtMDYRSMPrxRQegZkn29F/mLjftmRh7u2W8c85zJ0
0QlaYpGvO3JqOxzMZSNOtvDWRGelscZPclE4wBuwdjtrCogtZy790ET6JJyUjFYw
Lj7/U2ydwa6UEr6RQRBSG0xmUbBiqLoQZYTLk6d9q/Ipvt9kplj3FqZlVJ9zoXc=
=e/Dn
-----END PGP SIGNATURE-----

--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Raspunde prin e-mail lui