diff src/testdir/test_edit.vim @ 25790:16a7d1154be8 v8.2.3430

patch 8.2.3430: no generic way to trigger an autocommand on mode change Commit: https://github.com/vim/vim/commit/f1e8876fa2359b572d262772747405d3616db670 Author: =?UTF-8?q?Magnus=20Gro=C3=9F?= <magnus.gross@rwth-aachen.de> Date: Sun Sep 12 13:39:55 2021 +0200 patch 8.2.3430: no generic way to trigger an autocommand on mode change Problem: No generic way to trigger an autocommand on mode change. Solution: Add the ModeChanged autocommand event. (Magnus Gross, closes https://github.com/vim/vim/issues/8856)
author Bram Moolenaar <Bram@vim.org>
date Sun, 12 Sep 2021 13:45:05 +0200
parents ec62e0764ffa
children 1a5351f6803f
line wrap: on
line diff
--- a/src/testdir/test_edit.vim
+++ b/src/testdir/test_edit.vim
@@ -1907,4 +1907,38 @@ func Test_edit_put_CTRL_E()
   set encoding=utf-8
 endfunc
 
+" Test for ModeChanged pattern
+func Test_mode_changes()
+  let g:count = 0
+  func! DoIt()
+    let g:count += 1
+  endfunc
+  let g:index = 0
+  let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'n', 'V', 'v', 'n']
+  func! TestMode()
+    call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
+    call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
+    call assert_equal(mode(), get(v:event, "new_mode"))
+    let g:index += 1
+  endfunc
+
+  au ModeChanged * :call TestMode()
+  au ModeChanged n:* :call DoIt()
+  call feedkeys("i\<esc>vV\<esc>", 'tnix')
+  call assert_equal(2, g:count)
+
+  au ModeChanged V:v :call DoIt()
+  call feedkeys("Vv\<esc>", 'tnix')
+  call assert_equal(4, g:count)
+
+  call assert_equal(len(g:mode_seq) - 1, g:index)
+
+  au! ModeChanged
+  delfunc TestMode
+  unlet! g:mode_seq
+  unlet! g:index
+  delfunc DoIt
+  unlet! g:count
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab