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

mode() incorrectly reports current mode #8342

Closed
pierreganty opened this issue Jun 7, 2021 · 6 comments
Closed

mode() incorrectly reports current mode #8342

pierreganty opened this issue Jun 7, 2021 · 6 comments

Comments

@pierreganty
Copy link
Contributor

Describe the bug
Mode incorrectly reported using mode().

To Reproduce
Detailed steps to reproduce the behavior:

  1. Run vim -Nu NONE -S mwe.vim9 where mwe.vim9 is
vim9script
snoremap <expr> s- execute('g:mode = mode(1)')->slice(0, 0)
xnoremap <expr> X- execute('g:mode = mode(1)')->slice(0, 0)
noremap <expr> n- execute('g:mode = mode(1)')->slice(0, 0)
vnoremap <expr> v- execute('g:mode = mode(1)')->slice(0, 0)
feedkeys('n-', 'x')
assert_equal('n', g:mode)
feedkeys('ghs-', 'x')
assert_equal('s', g:mode)
feedkeys("\<C-g>X-", 'x')
assert_equal('s', g:mode)
echom v:errors
  1. The last assert should fail because, after inputting <C-g> we left select mode to go to visual.
    Therefore the mode() invokation (via mapping X-) should return visual mode.
    Note that the X- (visual only) mapping seems to be triggered correctly though.

Expected behavior
The last assert of mwe.vim9 should fail.

Environment (please complete the following information):

  • Vim version 8.2.2932
  • OS: macOS 11.3.1
  • Terminal: Terminal.app
@pierreganty
Copy link
Contributor Author

@lacygoill I came up with the mwe following your help on my last issue report but I am not too confident about my vim9script skills.

@pierreganty pierreganty changed the title Function mode() incorrectly reports current mode mode() incorrectly reports current mode Jun 7, 2021
@lacygoill
Copy link

Yes, it seems that mode() doesn't pick up the correct mode at the end:

vim9script

snoremap <expr> s- execute('g:mode = mode(1)')->slice(0, 0)
xnoremap <expr> X- execute('g:mode = mode(1)')->slice(0, 0)

feedkeys('ghs-', 'x')
assert_equal('s', g:mode)

feedkeys("\<C-g>X-", 'x')
assert_equal('v', g:mode)

echom v:errors
['command line..script ... line 10: Expected ''v'' but got ''s''']

@brammool
Copy link
Contributor

brammool commented Jun 7, 2021

At the second feedkeys() select mode is not active, it works OK when it first starts select mode with "gh".
The mapping results in an empty string, this is what causes select mode to end.
That is because it looks like there is something to read, but it ends up empty. vgetc() then returns Esc to get out.

@lacygoill
Copy link

At the second feedkeys() select mode is not active

Ah yes, the contents of g:mode is actually stale, which explains this:

vim9script

snoremap <expr> s- execute('g:mode = mode(1)')->slice(0, 0)
xnoremap <expr> X- execute('g:mode = mode(1)')->slice(0, 0)

feedkeys('ghs-', 'x')
assert_equal('s', g:mode)

unlet g:mode
feedkeys("\<C-g>X-", 'x')
assert_equal('v', g:mode)

echom v:errors
E121: Undefined variable: g:mode
E116: Invalid arguments for function assert_equal('v', g:mode)

it works OK when it first starts select mode with "gh".
The mapping results in an empty string, this is what causes select mode to end.

OK, so this explains what the OP observed. I guess it's working as intended.

@lacygoill
Copy link

To the OP, you can work around this issue by using the special keycode <Ignore>. That is, instead of writing this:

snoremap <expr> s- execute('g:mode = mode(1)')->slice(0, 0)

Write this:

snoremap <expr> s- execute('g:mode = mode(1)')->slice(0, 0) .. '<Ignore>'
                                                            ^-----------^

@pierreganty
Copy link
Contributor Author

Thanks to both of you. This is very helpful.

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

3 participants