Loading ...
Sorry, an error occurred while loading the content.

Re: Functions with [range] do not preserve the cursor line

Expand Messages
  • Nate Soares
    That s a nifty command and mapping, but I had no trouble with getting a custom command to use the range correctly. (You can just use -range, pass and
    Message 1 of 33 , Nov 19, 2012
    • 0 Attachment
      That's a nifty command and mapping, but I had no trouble with getting a custom command to use the range correctly. (You can just use -range, pass <line1> and <line2> to the function, and have the function do winsaveview() and winrestview(), which is what I do now.)

      Then you can also default to the whole file much cleaner e.g. with -range=%. But I'm not trying to write a command, I'm trying to write a function.

      The question is, is there any way to do this in a function that takes a range, called like:

      %call SomeUniformApiFunction()

      I'm not struggling to get the functionality, I'm struggling to expose a sane (function) API for the functionality.


      On Mon, Nov 19, 2012 at 4:19 PM, Andy Wokula <anwoku@...> wrote:
      Am 19.11.2012 22:52, schrieb So8res:

      I think functions with [range] should preserve the cursor line.

      Don't break scripts that count on this.


      Assume you have:

      function RangeTest() range
         echo line('.')
      endfunction

      And you put the cursor on line 2 and :%call RangeTest()

      This function will echo 1, because the cursor is moved to the
      beginning of the range *before* entering the function.

      I was attempting to create a function which does a search-replace (:s)
      without moving the cursor (using winsaveview() and winrestview()). It
      turns out this is not possible, because the cursor is moved before
      winsaveview() can be called.

      Is this the intended behavior, or is it a bug? If it's intended, is
      there a way to save the winview before entering a function with
      [range]?

      Example: remove trailing white space in range (default `%'), keeping
          cursor position, last search pattern etc.

          :no <Leader>ss  :v/^-- $/s/\s\+$//e<C-B>KeepView InFunc! <C-E>
          :ou <Leader>ss|sunm <Leader>ss
          " final <CR> could be added

      for :KeepView and :InFunc, see
      http://vim.sourceforge.net/scripts/script.php?script_id=3800

      :KeepView   does winsaveview() and winrestview()
      :InFunc     does no more than executing the argument within a function
                  to make use of  :h function-search-undo

      You can insert any other command instead of
          v/^-- $/s/\s\+$//e
      that accepts a range.

      --
      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 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
    • So8res
      ... Any word on getting a variant of range functions that don t move the cursorline? This still bites me occasionally. -- -- You received this message from the
      Message 33 of 33 , Nov 11, 2013
      • 0 Attachment
        On Wednesday, December 5, 2012 9:13:17 AM UTC-8, So8res wrote:
        > >
        > > I don't like "stay", but can't think of something better right now.
        > >
        > > Perhaps "keepline"?
        > >
        >
        > How do you feel about using 'range!' (with a bang) instead?
        >
        > It would keep the :function argument space more concise, and would only be usable with 'range', both of which are nice. However, there's no precedent (that I can find) for command arguments having bangs.

        Any word on getting a variant of range functions that don't move the cursorline? This still bites me occasionally.

        --
        --
        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+unsubscribe@....
        For more options, visit https://groups.google.com/groups/opt_out.
      Your message has been successfully submitted and would be delivered to recipients shortly.