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

Re: 'virtualedit' and col() ( was: Python, 'virtualedit' and col() )

Expand Messages
  • marcel.van.der.laan@hccnet.nl
    ... Okay, that cleared up some confusion; keeps the cursor on the character after the insertion point (the cursor) when it can, or one character back
    Message 1 of 5 , Feb 23, 2006
    • 0 Attachment
      On Thu, February 23, 2006 14:27, Benji Fisher wrote:
      > On Thu, Feb 23, 2006 at 09:51:02AM +0100, marcel.van.der.laan@...
      > wrote:
      >> On Thu, February 23, 2006 00:44, A. J. Mechelynck wrote:
      >>> Marcel van der Laan wrote:
      >>>> def testCode(): # Save Virtual edit mode and change to it. ve =
      >>>> vim.eval('&ve') print "Visual edit is now >" + ve + "<"
      >>>> vim.command('set ve=all') print "Visual edit is now >" +
      >>>> vim.eval("&ve") + "<"
      >>>> # Print the current cursor position
      >>>> line = int(vim.eval('line(".")')) column = int(vim.eval('col(".")'))
      >>>> print "Line: %d\nColumn: %d" % (line, column)
      >>>> #Print What's before, and what's after
      >>>> print "Before cursor: >%s<" % vim.current.line[:column-1] print
      >>>> "After
      >>>> cursor : >%s<" % vim.current.line[column-1:] # Return to the
      >>>> previous virtualedit mode vim.command("set ve=" + ve)
      >>>> And I have the following mapping defined:
      >>>> i <S-CR> <C-O>:py testCode()<CR>
      >> Okay, after some experimentation, it seems that the problem is not so
      >> much that it is Python related, rather it looks like the implementation
      >> of the col() function (and I haven't looked at the source code) uses the
      >> column as saved when leaving Insert mode and thus being dependent on the
      >> 'virtualedit' setting when it was last in Insert mode. I would have
      >> expected the col() function to take into account the 'virtualedit'
      >> setting *at the time of calling*.
      > I think you are looking at this the wrong way. The col() function
      > does not know nor care what the 'virtualedit' option is. In your examples,
      > the cursor is on the last character of the line, column 15, after the
      > <C-O> in your mapping. This does not change when you
      > :set ve=all . IMHO it would be a Bad Idea if setting the 'virtualedit'
      > option changed the cursor position.

      Okay, that cleared up some confusion; <C-O> keeps the cursor on the
      character after the insertion point (the cursor) when it can, or one
      character back when it cannot (i.e. at the end of the line when
      'virtualedit' is off). I had somehow expected it to do something along the
      lines of the <Esc> key which placed it one character back, or on the next
      character (relative to the insertion point, not the cursor column
      position) when at the beginning of the line. That's obviously not the

      >> imap <F2> <C-O>:call EchoCol()<CR>
      > Right. If 'virtualedit' is set to "all" when you leave Insert
      > mode with <C-O>, then the cursor position will be past the end of the line.

      I get it.

      >> My work-around now seems to be messy: I have to put all the
      >> 'virtualedit'
      >> administration in my key mapping (with all the <C-O>'s), thereby taking
      >> out of the function implementation part of the actual implementation
      >> (namely that 'virtualedit' should be set to allow the col() function to
      >> work as I'd like). This I consider to be messy. Unfortunately, it's
      >> this kind of vim scripting behaviour that actually led me to start
      >> scripting in Python - but look where that got me...
      > Did you try doing this from a Vim function or Python script?
      > function EchoCol()
      > let save_ve = &ve
      > set ve=all
      > execute "normal! a\<C-O>"
      > echo col(".")
      > let &ve=save_ve
      > endfu

      I have now, but as I expected, it has some strange behaviour as soon as
      you have the cursor any place other than at the end of the line, including
      giving the wrong answer when at the beginning of the line...

      >> So it seems as though my 'problem report' just became a 'change
      >> request' (even though I still think the current behaviour with the
      >> possible work-around is a 'problem').
      >> I'd still like to hear your responses. Can the current behaviour be
      >> justified?
      > What change are you suggesting? If you want the cursor to move
      > when 'virtualedit' is changed from the default to "all", then I do not
      > agree. Maybe you have something more reasonable in mind, but I suspect
      > that a more reasonable variant will be too complex to be a good idea.

      True. It seems to be wroking as should be expected. I guess I'll have to
      live with:

      function! EchoCol()
      echo col(".")
      let &ve=g:save_ve

      function! PreEchoCol()
      let g:save_ve = &ve
      set ve=all

      and the imap:

      i <S-CR> <C-O>:call PreEchoCol()<CR><C-O>:call EchoCol()<CR>

      to keep the knowledge of what's inside EchoCol transparent in the mapping.

      > HTH

      It does, thanks.

      > --Benji Fisher

    Your message has been successfully submitted and would be delivered to recipients shortly.