patch 9.1.1337: Undo corrupted with 'completeopt' "preinsert" when switching 
buffer

Commit: 
https://github.com/vim/vim/commit/1343681aba56d49c16d3070615b8ece7f8b0d1bd
Author: zeertzjq <zeert...@outlook.com>
Date:   Wed Apr 23 20:46:35 2025 +0200

    patch 9.1.1337: Undo corrupted with 'completeopt' "preinsert" when 
switching buffer
    
    Problem:  Undo corrupted with 'completeopt' "preinsert" when switching
              buffer or window.
    Solution: Do not delete preinsert text when switching buffer or window.
              (zeertzjq)
    
    related: neovim/neovim#33581
    closes: #17193
    
    Signed-off-by: zeertzjq <zeert...@outlook.com>
    Signed-off-by: glepnir <glephun...@gmail.com>
    Signed-off-by: Christian Brabandt <c...@256bit.org>

diff --git a/src/insexpand.c b/src/insexpand.c
index 233586277..ace4f55b5 100644
--- a/src/insexpand.c
+++ b/src/insexpand.c
@@ -2699,7 +2699,7 @@ ins_compl_stop(int c, int prev_mode, int retval)
     char_u     *word = NULL;
 
     // Remove pre-inserted text when present.
-    if (ins_compl_preinsert_effect())
+    if (ins_compl_preinsert_effect() && ins_compl_win_active(curwin))
        ins_compl_delete();
 
     // Get here when we have finished typing a sequence of ^N and
diff --git a/src/testdir/test_ins_complete.vim 
b/src/testdir/test_ins_complete.vim
index 9d39b2a7f..b03132f7e 100644
--- a/src/testdir/test_ins_complete.vim
+++ b/src/testdir/test_ins_complete.vim
@@ -3833,7 +3833,7 @@ func Test_complete_info_completed()
   set cot&
 endfunc
 
-function Test_completeopt_preinsert()
+func Test_completeopt_preinsert()
   func Omni_test(findstart, base)
     if a:findstart
       return col(".")
@@ -3973,6 +3973,47 @@ function Test_completeopt_preinsert()
   call assert_equal(4, g:col)
   call assert_equal("wp.", getline('.'))
 
+  %delete _
+  let &l:undolevels = &l:undolevels
+  normal! ifoo
+  let &l:undolevels = &l:undolevels
+  normal! obar
+  let &l:undolevels = &l:undolevels
+  normal! obaz
+  let &l:undolevels = &l:undolevels
+
+  func CheckUndo()
+    let g:errmsg = ''
+    call assert_equal(['foo', 'bar', 'baz'], getline(1, '$'))
+    undo
+    call assert_equal(['foo', 'bar'], getline(1, '$'))
+    undo
+    call assert_equal(['foo'], getline(1, '$'))
+    undo
+    call assert_equal([''], getline(1, '$'))
+    later 3
+    call assert_equal(['foo', 'bar', 'baz'], getline(1, '$'))
+    call assert_equal('', v:errmsg)
+  endfunc
+
+  " Check that switching buffer with "preinsert" doesn't corrupt undo.
+  new
+  setlocal bufhidden=wipe
+  inoremap <buffer> <F2> <Cmd>enew!<CR>
+  call feedkeys("i\<C-X>\<C-O>\<F2>\<Esc>", 'tx')
+  bwipe!
+  call CheckUndo()
+
+  " Check that closing window with "preinsert" doesn't corrupt undo.
+  new
+  setlocal bufhidden=wipe
+  inoremap <buffer> <F2> <Cmd>close!<CR>
+  call feedkeys("i\<C-X>\<C-O>\<F2>\<Esc>", 'tx')
+  call CheckUndo()
+
+  %delete _
+  delfunc CheckUndo
+
   bw!
   set cot&
   set omnifunc&
diff --git a/src/version.c b/src/version.c
index 6d3a29536..fec0e0d18 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 */
+/**/
+    1337,
 /**/
     1336,
 /**/

-- 
-- 
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/E1u7fKL-003XcJ-CM%40256bit.org.

Raspunde prin e-mail lui