]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.1407: Can't use getpos('v') in OptionSet when using setbufvar() v9.1.1407
authorzeertzjq <zeertzjq@outlook.com>
Sun, 25 May 2025 14:59:50 +0000 (16:59 +0200)
committerChristian Brabandt <cb@256bit.org>
Sun, 25 May 2025 14:59:50 +0000 (16:59 +0200)
Problem:  Can't use getpos('v') in OptionSet when using setbufvar().
Solution: Don't reset Visual selection when switching to the same
          buffer (zeertzjq).

closes: #17373

Signed-off-by: zeertzjq <zeertzjq@outlook.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/autocmd.c
src/testdir/test_autocmd.vim
src/version.c

index 3d21a937bc54502aef2ec89f350745a951ca2a31..6a5f035d2e97a6ccbc8ef18e1bee36b5e5cd2759 100644 (file)
@@ -1585,9 +1585,10 @@ aucmd_prepbuf(
 #ifdef FEAT_AUTOCHDIR
     int                save_acd;
 #endif
+    int                same_buffer = buf == curbuf;
 
     // Find a window that is for the new buffer
-    if (buf == curbuf)         // be quick when buf is curbuf
+    if (same_buffer)           // be quick when buf is curbuf
        win = curwin;
     else
        FOR_ALL_WINDOWS(win)
@@ -1677,9 +1678,10 @@ aucmd_prepbuf(
     aco->new_curwin_id = curwin->w_id;
     set_bufref(&aco->new_curbuf, curbuf);
 
-    // disable the Visual area, the position may be invalid in another buffer
     aco->save_VIsual_active = VIsual_active;
-    VIsual_active = FALSE;
+    if (!same_buffer)
+       // disable the Visual area, position may be invalid in another buffer
+       VIsual_active = FALSE;
 }
 
 /*
index 1988807f75c66cf34d4e8eb929b8f99ba1e49fc9..dfeb93f06d96cdadcba6d5894a27410af8fd4f76 100644 (file)
@@ -3761,6 +3761,27 @@ func Test_Visual_doautoall_redraw()
   %bwipe!
 endfunc
 
+func Test_get_Visual_selection_in_curbuf_autocmd()
+  call test_override('starting', 1)
+  new
+  autocmd OptionSet list let b:text = getregion(getpos('.'), getpos('v'))
+  call setline(1, 'foo bar baz')
+
+  normal! gg0fbvtb
+  setlocal list
+  call assert_equal(['bar '], b:text)
+  exe "normal! \<Esc>"
+
+  normal! v0
+  call setbufvar('%', '&list', v:false)
+  call assert_equal(['foo bar '], b:text)
+  exe "normal! \<Esc>"
+
+  autocmd! OptionSet list
+  bwipe!
+  call test_override('starting', 0)
+endfunc
+
 " This was using freed memory.
 func Test_BufNew_arglocal()
   arglocal
index b9af08a7dee23343414aef877f6cd34015942bf2..637f178a035c4adae70e2fd8f992bb062f5f382e 100644 (file)
@@ -709,6 +709,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1407,
 /**/
     1406,
 /**/