patch 9.1.1349: CmdlineLeavePre may trigger twice

Commit: 
https://github.com/vim/vim/commit/46755e6b52b0c4346c1c7eaa311c204fdd9185df
Author: Girish Palya <giris...@gmail.com>
Date:   Sun Apr 27 19:28:06 2025 +0200

    patch 9.1.1349: CmdlineLeavePre may trigger twice
    
    Problem:  CmdlineLeavePre may trigger twice
              (after v9.1.1329)
    Solution: check that the key was typed, trigger it when it wasn't before
              (Girish Palya)
    
    There are two problems:
    - CmdlineLeavePre may be triggered twice when a cabbr is present.
    - CmdlineLeavePre fails to trigger when exiting the command-line via
      <Backspace>.
    
    Check if the Carriage Return (Enter) key was actually typed.
    Trigger the event when the command-line is exited using Backspace and
    other keys.
    
    closes: #17214
    
    Signed-off-by: Girish Palya <giris...@gmail.com>
    Signed-off-by: Christian Brabandt <c...@256bit.org>

diff --git a/src/ex_getln.c b/src/ex_getln.c
index 1be5a0c1f..24ff7a908 100644
--- a/src/ex_getln.c
+++ b/src/ex_getln.c
@@ -1620,6 +1620,7 @@ getcmdline_int(
     int                did_save_ccline = FALSE;
     int                cmdline_type;
     int                wild_type = 0;
+    int                event_cmdlineleavepre_triggered = FALSE;
 
     // one recursion level deeper
     ++depth;
@@ -1917,8 +1918,15 @@ getcmdline_int(
        }
 
        // Trigger CmdlineLeavePre autocommand
-       if (c == '
' || c == '
' || c == K_KENTER || c == ESC || c == Ctrl_C)
+       if (KeyTyped && (c == '
' || c == '
' || c == K_KENTER || c == ESC
+#ifdef UNIX
+                   || c == intr_char
+#endif
+                   || c == Ctrl_C))
+       {
            trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVEPRE);
+           event_cmdlineleavepre_triggered = TRUE;
+       }
 
        // The wildmenu is cleared if the pressed key is not used for
        // navigating the wild menu (i.e. the key is not 'wildchar' or
@@ -2608,6 +2616,10 @@ returncmd:
     if (some_key_typed)
        need_wait_return = FALSE;
 
+    // Trigger CmdlineLeavePre autocommands if not already triggered.
+    if (!event_cmdlineleavepre_triggered)
+       trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVEPRE);
+
     // Trigger CmdlineLeave autocommands.
     trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
 
diff --git a/src/testdir/test_autocmd.vim b/src/testdir/test_autocmd.vim
index 6f83b9cae..599153b08 100644
--- a/src/testdir/test_autocmd.vim
+++ b/src/testdir/test_autocmd.vim
@@ -2002,51 +2002,101 @@ endfunc
 
 func Test_Cmdline_Trigger()
   autocmd CmdlineLeavePre : let g:log = "CmdlineLeavePre"
+  autocmd CmdlineLeavePre : let g:log2 = "CmdlineLeave"
   new
   let g:log = ''
+  let g:log2 = ''
   nnoremap <F1> <Cmd>echo "hello"<CR>
   call feedkeys("\<F1>", 'x')
   call assert_equal('', g:log)
+  call assert_equal('', g:log2)
   nunmap <F1>
+
   let g:log = ''
+  let g:log2 = ''
   nnoremap <F1> :echo "hello"<CR>
   call feedkeys("\<F1>", 'x')
   call assert_equal('CmdlineLeavePre', g:log)
+  call assert_equal('CmdlineLeave', g:log2)
   nunmap <F1>
+
+  let g:log = ''
+  let g:log2 = ''
+  call feedkeys(":\<bs>", "tx")
+  call assert_equal('CmdlineLeavePre', g:log)
+  call assert_equal('CmdlineLeave', g:log2)
+
   let g:log = ''
+  let g:log2 = ''
   split
   call assert_equal('', g:log)
   call feedkeys(":echo hello", "tx")
   call assert_equal('CmdlineLeavePre', g:log)
+  call assert_equal('CmdlineLeave', g:log2)
+
   let g:log = ''
+  let g:log2 = ''
   close
   call assert_equal('', g:log)
   call feedkeys(":echo hello", "tx")
   call assert_equal('CmdlineLeavePre', g:log)
+  call assert_equal('CmdlineLeave', g:log2)
+
   let g:log = ''
+  let g:log2 = ''
   tabnew
   call assert_equal('', g:log)
   call feedkeys(":echo hello", "tx")
   call assert_equal('CmdlineLeavePre', g:log)
+  call assert_equal('CmdlineLeave', g:log2)
+
   let g:log = ''
+  let g:log2 = ''
   split
   call assert_equal('', g:log)
   call feedkeys(":echo hello", "tx")
   call assert_equal('CmdlineLeavePre', g:log)
+  call assert_equal('CmdlineLeave', g:log2)
+
   let g:log = ''
+  let g:log2 = ''
   tabclose
   call assert_equal('', g:log)
   call feedkeys(":echo hello", "tx")
   call assert_equal('CmdlineLeavePre', g:log)
+  call assert_equal('CmdlineLeave', g:log2)
+
   let g:count = 0
   autocmd CmdlineLeavePre * let g:count += 1
   call feedkeys(":let c = input('? ')\<cr>B\<cr>", "tx")
   call assert_equal(2, g:count)
   unlet! g:count
   unlet! g:log
+  unlet! g:log2
   bw!
 endfunc
 
+" Ensure :cabbr does not cause a spurious CmdlineLeavePre.
+func Test_CmdlineLeavePre_cabbr()
+  CheckFeature terminal
+  let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], 
{'term_rows': 3})
+  call assert_equal('running', term_getstatus(buf))
+  call term_sendkeys(buf, ":let g:a=0\<cr>")
+  call term_wait(buf, 50)
+  call term_sendkeys(buf, ":cabbr v v\<cr>")
+  call term_wait(buf, 50)
+  call term_sendkeys(buf, ":command! -nargs=* Foo echo\<cr>")
+  call term_wait(buf, 50)
+  call term_sendkeys(buf, ":au! CmdlineLeavePre * :let g:a+=1\<cr>")
+  call term_wait(buf, 50)
+  call term_sendkeys(buf, ":Foo v\<cr>")
+  call term_wait(buf, 50)
+  call term_sendkeys(buf, ":echo g:a\<cr>")
+  call term_wait(buf, 50)
+  call WaitForAssert({-> assert_match('^2.*$', term_getline(buf, 3))})
+  bwipe!
+endfunc
+
 func Test_Cmdline()
   au! CmdlineChanged : let g:text = getcmdline()
   let g:text = 0
diff --git a/src/version.c b/src/version.c
index 91f216430..5e40ee5b5 100644
--- a/src/version.c
+++ b/src/version.c
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1349,
 /**/
     1348,
 /**/

-- 
-- 
You received this message from the "vim_dev" 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_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to vim_dev+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/vim_dev/E1u963q-00BS32-R6%40256bit.org.

Raspunde prin e-mail lui