]> granicus.if.org Git - vim/commitdiff
patch 8.2.1874: can't do something just before leaving Insert mode v8.2.1874
authorBram Moolenaar <Bram@vim.org>
Wed, 21 Oct 2020 10:19:53 +0000 (12:19 +0200)
committerBram Moolenaar <Bram@vim.org>
Wed, 21 Oct 2020 10:19:53 +0000 (12:19 +0200)
Problem:    Can't do something just before leaving Insert mode.
Solution:   Add the InsertLeavePre autocommand event. (closes #7177)

runtime/doc/autocmd.txt
src/autocmd.c
src/edit.c
src/testdir/test_edit.vim
src/version.c
src/vim.h

index 9355d1f405e630a15a62b6a136633325c7a6cc6e..f15cd96f0425e10d09694d03d20a03a69180c947 100644 (file)
@@ -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
index 57b5674e14d2295a19823384959b3be6a101af49..be35845c9d26d8ab8c658bb4f441a1f5ebb91378 100644 (file)
@@ -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},
index e3e4624fb0f7de089f218531e9597abfa282fe70..14a21d95739cb99c4a19c65ff9e3a17be49644a9 100644 (file)
@@ -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)
index ac893f39bc44637595d00d908fab3d047bda48c4..6a84b1c8c6b331b0fe0ec01429ffc656f0378984 100644 (file)
@@ -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
 
index 08da9925c2ff818d039484dad9d0a4dc334d8b97..2b02d486a7293b64297d3eebe9fb09137d859eef 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1874,
 /**/
     1873,
 /**/
index c43ec83e6253ee5eadad861a3bf8fa5bb5004c14..5ae8b793195f51ee01dcec47afc77ea4ae4d1299 100644 (file)
--- 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