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

Pressing Ctrl+C to exit Insert mode sometimes (on removal of autoindent) causes API functions called from an on_lines handler in Lua to error with "Keyboard interrupt" #16416

Open
dmitmel opened this issue Nov 23, 2021 · 3 comments
Labels
bug issues reporting wrong behavior has:workaround issue is not fixed but can be circumvented until then lua stdlib

Comments

@dmitmel
Copy link
Contributor

dmitmel commented Nov 23, 2021

Neovim version (nvim -v)

0.5.1

Vim (not Nvim) behaves the same?

N/A

Operating system/version

Arch Linux

Terminal name/version

kitty 0.23.1

$TERM environment variable

xterm-kitty

Installation

pacman, official repositories

How to reproduce the issue

test_init.lua:

function Repro()
  vim.api.nvim_buf_attach(0, false, {
    on_lines = function(_, _, changedtick, firstline, old_lastline, new_lastline)
      print('before')
      print(vim.api.nvim_buf_get_mark(0, '.'))
      print('after')
    end,
  })
end
vim.cmd('autocmd BufEnter * lua Repro()')

test_file.js

function foo() {
}
  1. nvim -u test_lua.lua test_file.js
  2. gg, o
  3. Press Ctrl+C (^C)

Expected behavior

No error is thrown.

Actual behavior

nvim_buf_get_mark throws the following error:

Error executing lua callback: test_init.lua:5: Keyboard interrupt

I also want to add that the issue occurs because of autoindentation (that's why I wrote "sometimes" in the title), in particular, that exiting Insert mode from a newly-indented line causes Vim to remove the indentation whitespace. on_lines here is most likely invoked right after the editor has switched to Normal mode. The bug also occurs only when calling API functions which use try_start and try_end in their implementation, nvim_buf_get_mark was chosen as the simplest example (thus, e.g., nvim_buf_get_lines is unaffected).

Workaround

For those who encounter this bug and are used to Ctrl+C instead of Esc, this should be a good enough workaround:

inoremap <C-c> <Esc>
@dmitmel dmitmel added the bug issues reporting wrong behavior label Nov 23, 2021
@zeertzjq zeertzjq added lua stdlib has:workaround issue is not fixed but can be circumvented until then labels Nov 23, 2021
@callms
Copy link

callms commented Dec 14, 2021

@dmitmel the cursor may move after you hit ^C, I fixed it using the following instead:

inoremap <C-c> <Esc>`^

@craigmac
Copy link
Contributor

Mapping <C-c> to <Esc> will break q: cmdline window functionality, Telescope prompt (C-c closes Telescope, Esc just goes to Normal mode), and probably a bunch of other cases, these are just the ones I can think of off the top of my head. I don't think the "has workaround" label should apply here if it doesn't fix the problem.

@zeertzjq
Copy link
Member

Mapping <C-C> to <C-C> also works.

rafamadriz added a commit to rafamadriz/dotfiles that referenced this issue Apr 23, 2023
Instead of remapping `<C-c>` to `<ESC>`, remap to `<C-c>` which also
seems to fix the issue (A bit confussing I know). But this way you don't
break Telescope prompt where `<C-c>` closes Telescope and `<ESC>` goes
into normal mode and just like this there is a bunch of other cases
where remapping to `<ESC>` will break some funcionality.

reference: <neovim/neovim#16416>
rafamadriz added a commit to rafamadriz/dotfiles that referenced this issue Apr 30, 2023
Instead of remapping `<C-c>` to `<ESC>`, remap to `<C-c>` which also
seems to fix the issue (A bit confussing I know). But this way you don't
break Telescope prompt where `<C-c>` closes Telescope and `<ESC>` goes
into normal mode and just like this there is a bunch of other cases
where remapping to `<ESC>` will break some funcionality.

reference: <neovim/neovim#16416>
xu-cheng added a commit to xu-cheng/LuaSnip that referenced this issue Nov 4, 2023
Due to the neovim's issue (neovim/neovim#16416),
using `vim.fn.expand` sometimes throws Keyboard interrupt error when
pressing Ctrl-C (a common alternative to Esc).

This works around the above issue by simply getting the buffer number
from event argument.
L3MON4D3 pushed a commit to L3MON4D3/LuaSnip that referenced this issue Nov 4, 2023
Due to the neovim's issue (neovim/neovim#16416),
using `vim.fn.expand` sometimes throws Keyboard interrupt error when
pressing Ctrl-C (a common alternative to Esc).

This works around the above issue by simply getting the buffer number
from event argument.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug issues reporting wrong behavior has:workaround issue is not fixed but can be circumvented until then lua stdlib
Projects
None yet
Development

No branches or pull requests

4 participants