summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2020-10-21 12:19:53 +0200
committerBram Moolenaar <Bram@vim.org>2020-10-21 12:19:53 +0200
commitb53e13a91ab2fc9d52bd044715daa84972f4ce47 (patch)
tree6df807962d3719c50e6c33beaeaf8bc23ab8b12e
parent20d89e0ac65686481f97657008deede0b39ef5db (diff)
downloadvim-git-b53e13a91ab2fc9d52bd044715daa84972f4ce47.tar.gz
patch 8.2.1874: can't do something just before leaving Insert modev8.2.1874
Problem: Can't do something just before leaving Insert mode. Solution: Add the InsertLeavePre autocommand event. (closes #7177)
-rw-r--r--runtime/doc/autocmd.txt9
-rw-r--r--src/autocmd.c1
-rw-r--r--src/edit.c3
-rw-r--r--src/testdir/test_edit.vim11
-rw-r--r--src/version.c2
-rw-r--r--src/vim.h6
6 files changed, 27 insertions, 5 deletions
diff --git a/runtime/doc/autocmd.txt b/runtime/doc/autocmd.txt
index 9355d1f40..f15cd96f0 100644
--- a/runtime/doc/autocmd.txt
+++ b/runtime/doc/autocmd.txt
@@ -881,9 +881,14 @@ InsertEnter Just before starting Insert mode. Also for
The cursor is restored afterwards. If you do
not want that set |v:char| to a non-empty
string.
+ *InsertLeavePre*
+InsertLeavePre Just before leaving Insert mode. Also when
+ using CTRL-O |i_CTRL-O|. Be caseful not to
+ change mode or use `:normal`, it will likely
+ cause trouble.
*InsertLeave*
-InsertLeave When leaving Insert mode. Also when using
- CTRL-O |i_CTRL-O|. But not for |i_CTRL-C|.
+InsertLeave Just after leaving Insert mode. Also when
+ using CTRL-O |i_CTRL-O|. But not for |i_CTRL-C|.
*MenuPopup*
MenuPopup Just before showing the popup menu (under the
right mouse button). Useful for adjusting the
diff --git a/src/autocmd.c b/src/autocmd.c
index 57b5674e1..be35845c9 100644
--- a/src/autocmd.c
+++ b/src/autocmd.c
@@ -149,6 +149,7 @@ static struct event_name
{"InsertChange", EVENT_INSERTCHANGE},
{"InsertEnter", EVENT_INSERTENTER},
{"InsertLeave", EVENT_INSERTLEAVE},
+ {"InsertLeavePre", EVENT_INSERTLEAVEPRE},
{"InsertCharPre", EVENT_INSERTCHARPRE},
{"MenuPopup", EVENT_MENUPOPUP},
{"OptionSet", EVENT_OPTIONSET},
diff --git a/src/edit.c b/src/edit.c
index e3e4624fb..14a21d957 100644
--- a/src/edit.c
+++ b/src/edit.c
@@ -3607,6 +3607,9 @@ ins_esc(
undisplay_dollar();
}
+ if (cmdchar != 'r' && cmdchar != 'v')
+ ins_apply_autocmds(EVENT_INSERTLEAVEPRE);
+
// When an autoindent was removed, curswant stays after the
// indent
if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
diff --git a/src/testdir/test_edit.vim b/src/testdir/test_edit.vim
index ac893f39b..6a84b1c8c 100644
--- a/src/testdir/test_edit.vim
+++ b/src/testdir/test_edit.vim
@@ -1446,31 +1446,40 @@ endfunc
func Test_edit_InsertLeave()
new
+ au InsertLeavePre * let g:did_au_pre = 1
au InsertLeave * let g:did_au = 1
+ let g:did_au_pre = 0
let g:did_au = 0
call feedkeys("afoo\<Esc>", 'tx')
+ call assert_equal(1, g:did_au_pre)
call assert_equal(1, g:did_au)
call assert_equal('foo', getline(1))
+ let g:did_au_pre = 0
let g:did_au = 0
call feedkeys("Sbar\<C-C>", 'tx')
+ call assert_equal(1, g:did_au_pre)
call assert_equal(0, g:did_au)
call assert_equal('bar', getline(1))
inoremap x xx<Esc>
+ let g:did_au_pre = 0
let g:did_au = 0
call feedkeys("Saax", 'tx')
+ call assert_equal(1, g:did_au_pre)
call assert_equal(1, g:did_au)
call assert_equal('aaxx', getline(1))
inoremap x xx<C-C>
+ let g:did_au_pre = 0
let g:did_au = 0
call feedkeys("Sbbx", 'tx')
+ call assert_equal(1, g:did_au_pre)
call assert_equal(0, g:did_au)
call assert_equal('bbxx', getline(1))
bwipe!
- au! InsertLeave
+ au! InsertLeave InsertLeavePre
iunmap x
endfunc
diff --git a/src/version.c b/src/version.c
index 08da9925c..2b02d486a 100644
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1874,
+/**/
1873,
/**/
1872,
diff --git a/src/vim.h b/src/vim.h
index c43ec83e6..5ae8b7931 100644
--- a/src/vim.h
+++ b/src/vim.h
@@ -1298,7 +1298,8 @@ enum auto_event
EVENT_INSERTCHANGE, // when changing Insert/Replace mode
EVENT_INSERTCHARPRE, // before inserting a char
EVENT_INSERTENTER, // when entering Insert mode
- EVENT_INSERTLEAVE, // when leaving Insert mode
+ EVENT_INSERTLEAVEPRE, // just before leaving Insert mode
+ EVENT_INSERTLEAVE, // just after leaving Insert mode
EVENT_MENUPOPUP, // just before popup menu is displayed
EVENT_OPTIONSET, // option was set
EVENT_QUICKFIXCMDPOST, // after :make, :grep etc.
@@ -1325,7 +1326,8 @@ enum auto_event
EVENT_TABNEW, // when entering a new tab page
EVENT_TERMCHANGED, // after changing 'term'
EVENT_TERMINALOPEN, // after a terminal buffer was created
- EVENT_TERMINALWINOPEN, // after a terminal buffer was created and entering its window
+ EVENT_TERMINALWINOPEN, // after a terminal buffer was created and
+ // entering its window
EVENT_TERMRESPONSE, // after setting "v:termresponse"
EVENT_TEXTCHANGED, // text was modified not in Insert mode
EVENT_TEXTCHANGEDI, // text was modified in Insert mode