]> granicus.if.org Git - vim/commitdiff
patch 8.1.1205: a BufReadPre autocommand may cause the cursor to move v8.1.1205
authorBram Moolenaar <Bram@vim.org>
Thu, 25 Apr 2019 20:22:01 +0000 (22:22 +0200)
committerBram Moolenaar <Bram@vim.org>
Thu, 25 Apr 2019 20:22:01 +0000 (22:22 +0200)
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)

src/autocmd.c
src/proto/window.pro
src/structs.h
src/testdir/test_autocmd.vim
src/version.c
src/window.c

index 2ea23ccc4ff99e78e6cfb380ef203db9df9b378c..e6cdb361ed037d168ac34e75e12cc400ddb79a7a 100644 (file)
@@ -2123,9 +2123,16 @@ apply_autocmds_group(
        for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next)
            ap->last = FALSE;
        ap->last = TRUE;
-       check_lnums(TRUE);      // make sure cursor and topline are valid
+
+       // make sure cursor and topline are valid
+       check_lnums(TRUE);
+
        do_cmdline(NULL, getnextac, (void *)&patcmd,
                                     DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
+
+       // restore cursor and topline, unless they were changed
+       reset_lnums();
+
 #ifdef FEAT_EVAL
        if (eap != NULL)
        {
index 5da322e28901d4a68088934b55f7bd0bf300efa7..b4ad98afbafe3248e53f4a4774ecb253208d24f2 100644 (file)
@@ -70,6 +70,7 @@ int tabline_height(void);
 int min_rows(void);
 int only_one_window(void);
 void check_lnums(int do_curwin);
+void reset_lnums(void);
 void make_snapshot(int idx);
 void restore_snapshot(int idx, int close_curwin);
 int switch_win(win_T **save_curwin, tabpage_T **save_curtab, win_T *win, tabpage_T *tp, int no_display);
index f2f2cc923140597dc9ce66c4db1054085ae1835c..104265ecc4dab3f21ed7fd0856f97f45c9ac9104 100644 (file)
@@ -2715,6 +2715,16 @@ struct matchitem
 #endif
 };
 
+// Structure to store last cursor position and topline.  Used by check_lnums()
+// and reset_lnums().
+typedef struct
+{
+    int                w_topline_save; // original topline value
+    int                w_topline_corr; // corrected topline value
+    pos_T      w_cursor_save;  // original cursor position
+    pos_T      w_cursor_corr;  // corrected cursor position
+} pos_save_T;
+
 #ifdef FEAT_MENU
 typedef struct {
     int                wb_startcol;
@@ -2803,6 +2813,8 @@ struct window_S
     int                w_wincol;           /* Leftmost column of window in screen. */
     int                w_width;            /* Width of window, excluding separation. */
     int                w_vsep_width;       /* Number of separator columns (0 or 1). */
+    pos_save_T w_save_cursor;      /* backup of cursor pos and topline */
+
 
     /*
      * === start of cached values ====
index 8db06d08319cb3620f6249bed6395dfdbb4d70c3..55d725e48aaba49499611c8d0993c4005ebc0226 100644 (file)
@@ -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:
index 330bdf11b29894def07464f4e6c681002f856211..743900d14f6d8f9419119b647586ae5649c9bcb0 100644 (file)
@@ -771,6 +771,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1205,
 /**/
     1204,
 /**/
index 4ca56274bcea11cd5f45914d855d19b1b8b01b2b..df82d203fef6baf7a94ad0c5febaee4a9e1f155a 100644 (file)
@@ -6196,10 +6196,39 @@ check_lnums(int do_curwin)
     FOR_ALL_TAB_WINDOWS(tp, wp)
        if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
        {
+           // save the original cursor position and topline
+           wp->w_save_cursor.w_cursor_save = wp->w_cursor;
+           wp->w_save_cursor.w_topline_save = wp->w_topline;
+
            if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
                wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
            if (wp->w_topline > curbuf->b_ml.ml_line_count)
                wp->w_topline = curbuf->b_ml.ml_line_count;
+
+           // save the corrected cursor position and topline
+           wp->w_save_cursor.w_cursor_corr = wp->w_cursor;
+           wp->w_save_cursor.w_topline_corr = wp->w_topline;
+       }
+}
+
+/*
+ * Reset cursor and topline to its stored values from check_lnums().
+ * check_lnums() must have been called first!
+ */
+    void
+reset_lnums()
+{
+    win_T      *wp;
+    tabpage_T  *tp;
+
+    FOR_ALL_TAB_WINDOWS(tp, wp)
+       if (wp->w_buffer == curbuf)
+       {
+           // Restore the value if the autocommand didn't change it.
+           if (EQUAL_POS(wp->w_save_cursor.w_cursor_corr, wp->w_cursor))
+               wp->w_cursor = wp->w_save_cursor.w_cursor_save;
+           if (wp->w_save_cursor.w_topline_corr == wp->w_topline)
+               wp->w_topline = wp->w_save_cursor.w_topline_save;
        }
 }