if (buf->b_ffname == NULL)
del_buf = TRUE;
- // When closing the current buffer stop Visual mode before freeing
- // anything.
- if (buf == curbuf && VIsual_active
-#if defined(EXITFREE)
- && !entered_free_all_mem
-#endif
- )
- end_visual_mode();
-
// Free all things allocated for this buffer.
// Also calls the "BufDelete" autocommands when del_buf is TRUE.
//
// Therefore only return if curbuf changed to the deleted buffer.
if (buf == curbuf && !is_curbuf)
return;
+
+ // If curbuf, stop Visual mode just before freeing, but after autocmds that
+ // may restart it. May trigger TextYankPost, but with textlock set.
+ if (buf == curbuf && VIsual_active
+#if defined(EXITFREE)
+ && !entered_free_all_mem
+#endif
+ )
+ end_visual_mode();
+
#ifdef FEAT_DIFF
diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
#endif
static void
enter_buffer(buf_T *buf)
{
- // when closing the current buffer stop Visual mode
+ // Stop Visual mode before changing curbuf. May trigger TextYankPost, but
+ // with textlock set. Assumes curbuf and curwin->w_buffer is valid; if not,
+ // buf_freeall() should've done this already!
if (VIsual_active
#if defined(EXITFREE)
&& !entered_free_all_mem
let &columns = save_columns
bw!
endfunc
+
+func Test_visual_ended_in_wiped_buffer()
+ edit Xfoo
+ edit Xbar
+ setlocal bufhidden=wipe
+ augroup testing
+ autocmd BufWipeout * ++once normal! v
+ augroup END
+ " Must be the last window.
+ call assert_equal(1, winnr('$'))
+ call assert_equal(1, tabpagenr('$'))
+ " Was a member access on a NULL curbuf from Vim ending Visual mode.
+ buffer #
+ call assert_equal(0, bufexists('Xbar'))
+ call assert_equal('n', mode())
+
+ autocmd! testing
+ %bw!
+endfunc
+
+func Test_visual_ended_in_unloaded_buffer()
+ CheckFeature clipboard
+ CheckNotGui
+ set clipboard+=autoselect
+ edit Xfoo
+ edit Xbar
+ call setline(1, 'hi')
+ setlocal nomodified
+ let s:fired = 0
+ augroup testing
+ autocmd BufUnload Xbar call assert_equal('Xbar', bufname())
+ \| execute 'normal! V'
+ \| call assert_equal('V', mode())
+
+ " From Vim ending Visual mode. Used to occur too late, after the buffer was
+ " unloaded, so @* didn't contain the selection. Window also had a NULL
+ " w_buffer here!
+ autocmd TextYankPost * ++once let s:fired = 1
+ \| if has('clipboard_working') | call assert_equal("hi\n", @*) | endif
+ \| call tabpagebuflist() " was a NULL member access on w_buffer
+ augroup END
+
+ buffer Xfoo
+ call assert_equal(0, bufloaded('Xbar'))
+ call assert_equal('n', mode())
+ call assert_equal(1, s:fired)
+
+ autocmd! testing
+ unlet! s:fired
+ set clipboard&
+ %bw!
+endfunc
+
" vim: shiftwidth=2 sts=2 expandtab