Thursday, June 20, 2013

Re: Call a function in insertmode without triggering InsertLeave

On Thursday, June 20, 2013 3:46:33 AM UTC-5, EinfachToll wrote:
> I want to have some stuff executed when I press Escape, so I set an autocommand InsertLeave which works fine. But I don't want to execute it when Vim goes to normal mode during a mapping. So I changed
>
> :imap key <Esc>:call function()<CR>
>
> to
>
> :imap key <C-c>:call function()<CR>
>
> because the help says that <C-c> doesn't trigger the InsertLeave command. This is true when I type <C-c> manually, but not when I use it in a mapping. The autocommand is still executed. Is it a bug or a feature?
>

It sure doesn't look like it is acting as documented but maybe I'm missing something. I can reproduce with a specific simple example on Windows 7 gvim 7.3.822, and also on Solaris with gvim 7.3.1169:

gvim -N -u NONE -i NONE
let g:test = 0
au InsertLeave * let g:test+=1 | echomsg g:test "events"
imap <F1> <Esc>
imap <F2> <C-C>

a<Esc> outputs "1 events"
a<C-C> doesn't output anything
a<F1> outputs "2 events"
a<F2> outputs "3 events"

As a workaround, you could use an <expr> mapping that returns "\<C-c>:call function()\<CR>". But before returning, it would set eventignore to include InsertLeave. Then in your function called by the mapping, you could restore eventignore.

But if you have plugins or autocmds which rely on balanced InsertLeave/InsertEnter events, they may break in unexpected ways.

I can't find anything in the help that would explain the different behavior of <C-C> in a mapping (I did a :helpgrep CTRL-C).

There is this nugget in todo.txt:
8 Give a warning when using CTRL-C in the lhs of a mapping. It will never
(?) work.

--
--
You received this message from the "vim_use" 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_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: