diff src/testdir/test_autocmd.vim @ 16401:3b2db762a509 v8.1.1205

patch 8.1.1205: a BufReadPre autocommand may cause the cursor to move commit https://github.com/vim/vim/commit/a68e59590905da9b4448ff1fcac929ad1a18da9e Author: Bram Moolenaar <Bram@vim.org> Date: Thu Apr 25 22:22:01 2019 +0200 patch 8.1.1205: a BufReadPre autocommand may cause the cursor to move Problem: A BufReadPre autocommand may cause the cursor to move. Solution: Restore the cursor position after executing the autocommand, unless the autocommand moved it. (Christian Brabandt, closes #4302, closes #4294)
author Bram Moolenaar <Bram@vim.org>
date Thu, 25 Apr 2019 22:30:05 +0200
parents 7a942d92980d
children 24233eeaadd0
line wrap: on
line diff
--- a/src/testdir/test_autocmd.vim
+++ b/src/testdir/test_autocmd.vim
@@ -1485,6 +1485,51 @@ func Test_autocmd_once()
   call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
 endfunc
 
+func Test_autocmd_bufreadpre()
+  new
+  let b:bufreadpre = 1
+  call append(0, range(100))
+  w! XAutocmdBufReadPre.txt
+  autocmd BufReadPre <buffer> :let b:bufreadpre += 1
+  norm! 50gg
+  sp
+  norm! 100gg
+  wincmd p
+  let g:wsv1 = winsaveview()
+  wincmd p
+  let g:wsv2 = winsaveview()
+  " triggers BufReadPre, should not move the cursor in either window
+  " The topline may change one line in a large window.
+  edit
+  call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
+  call assert_equal(g:wsv2.lnum, winsaveview().lnum)
+  call assert_equal(2, b:bufreadpre)
+  wincmd p
+  call assert_equal(g:wsv1.topline, winsaveview().topline)
+  call assert_equal(g:wsv1.lnum, winsaveview().lnum)
+  call assert_equal(2, b:bufreadpre)
+  " Now set the cursor position in an BufReadPre autocommand
+  " (even though the position will be invalid, this should make Vim reset the
+  " cursor position in the other window.
+  wincmd p
+  set cpo+=g
+  " won't do anything, but try to set the cursor on an invalid lnum
+  autocmd BufReadPre <buffer> :norm! 70gg
+  " triggers BufReadPre, should not move the cursor in either window
+  e
+  call assert_equal(1, winsaveview().topline)
+  call assert_equal(1, winsaveview().lnum)
+  call assert_equal(3, b:bufreadpre)
+  wincmd p
+  call assert_equal(g:wsv1.topline, winsaveview().topline)
+  call assert_equal(g:wsv1.lnum, winsaveview().lnum)
+  call assert_equal(3, b:bufreadpre)
+  close
+  close
+  call delete('XAutocmdBufReadPre.txt')
+  set cpo-=g
+endfunc
+
 " FileChangedShell tested in test_filechanged.vim
 
 " Tests for the following autocommands: