Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getpos returns the wrong result when in visual mode #30

Open
sgrj opened this issue Jun 4, 2014 · 3 comments
Open

getpos returns the wrong result when in visual mode #30

sgrj opened this issue Jun 4, 2014 · 3 comments

Comments

@sgrj
Copy link

sgrj commented Jun 4, 2014

If a selection function is called when in visual mode, the function thinks that the cursor is positioned at the beginning of the selection, even if it is at the end.

The following code illustrates the problem.

call textobj#user#plugin('test', {
\      '-': {
\        'select-a': 'at',  '*select-a-function*': 'Select_a_test',
\      }
\    })

function! Select_a_test()
  let startpos = getpos('.')
  echom "Cursor at ".string(startpos)
  echom "Current mode: ".mode()

  return ['V', startpos, startpos]
endfunction

Now edit any file, and in normal mode do ggVGat. Only the first line is selected, and :messages displays Cursor at [0,1,1,0], although the cursor was on the last line. It should only select the last line.

Furthermore, :messages displays Current mode: n, so the function thinks we are in normal mode, although we are in visual line. (I don't know whether that can really be regarded as a bug. But if we knew that we are in visual mode, we could use this information to expand the selection to the outer block, if the visual selection already selects a block.)

@kana
Copy link
Owner

kana commented Jun 4, 2014

It is not wrong. Because Select_a_test is not called in Visual mode. When you type at in Visual mode, at will be mapped to an internal stuff to target the text object, and the stuff is something like :<C-u>call Select_a_test()<Return>. So that you are not in Visual mode, you were in Visual mode.

@sgrj
Copy link
Author

sgrj commented Jun 4, 2014

I see. Thanks for the explanation. So there is no way to know that we were in visual mode and where the cursor was before we typed at?

@kana
Copy link
Owner

kana commented Jun 5, 2014

There is no built-in functions to get such information. And the cursor position just before typing at does not matter. What you want to do is do extend the current selection. You can get the last selection by getpos("'<"), getpos("'>") and visualmode(). The missing part is the mode in which a text object was used.

Fortunately, the mode in which a text object was used can be provided by vim-textobj-user. The last problem is to define API to provide this information to custom text objects, in a backward-compatible manner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants