Hi Bram and list,

How to reproduce:
- Prepare the following script file
  $ cat sample.vim

e aa.txt
augroup sample
  autocmd!
  autocmd BufNew * tabedit cc.txt
augroup END

normal! i1
tabedit bb.txt

- Run vanilla Vim with above script file
  $ vim -Nu NONE -S sample.vim

Expected behavior (I think):
- Three tabpages is opened.
  1st: aa.txt (modified)
  2nd: bb.txt (not modified) <-- current tabpage
  3rd: cc.txt (not modified)

Actual behavior:
- Three tabpages is opened.
  1st: aa.txt (modified)
  2nd: aa.txt (modified)
  3rd: cc.txt (not modified) <-- current tabpage


I wrote a patch and test.
Check it please.

NOTE 1: This issue was reported by Norio Takagi.
NOTE 2: The following part of the patch is a change not related to the main 
topic :-)
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -451,7 +451,7 @@ close_buffer(
     int                nwindows;
     bufref_T   bufref;
 # ifdef FEAT_WINDOWS
-    int                is_curwin = (curwin!= NULL && curwin->w_buffer == buf);
+    int                is_curwin = (curwin != NULL && curwin->w_buffer == buf);
     win_T      *the_curwin = curwin;
     tabpage_T  *the_curtab = curtab;
 # endif
@@ -1649,10 +1649,11 @@ set_curbuf(buf_T *buf, int action)
 #ifdef FEAT_AUTOCMD
     if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
 # ifdef FEAT_EVAL
-           || (bufref_valid(&bufref) && !aborting()))
+           || (bufref_valid(&bufref) && !aborting())
 # else
-           || bufref_valid(&bufref))
+           || bufref_valid(&bufref)
 # endif
+       )
 #endif
     {
 #ifdef FEAT_SYN_HL

--
Best regards,
Hirohito Higashi (a.k.a. h_east)

-- 
-- 
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.
For more options, visit https://groups.google.com/d/optout.
diff --git a/src/buffer.c b/src/buffer.c
index b013295..dcd2b71 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -451,7 +451,7 @@ close_buffer(
     int		nwindows;
     bufref_T	bufref;
 # ifdef FEAT_WINDOWS
-    int		is_curwin = (curwin!= NULL && curwin->w_buffer == buf);
+    int		is_curwin = (curwin != NULL && curwin->w_buffer == buf);
     win_T	*the_curwin = curwin;
     tabpage_T	*the_curtab = curtab;
 # endif
@@ -1649,10 +1649,11 @@ set_curbuf(buf_T *buf, int action)
 #ifdef FEAT_AUTOCMD
     if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
 # ifdef FEAT_EVAL
-	    || (bufref_valid(&bufref) && !aborting()))
+	    || (bufref_valid(&bufref) && !aborting())
 # else
-	    || bufref_valid(&bufref))
+	    || bufref_valid(&bufref)
 # endif
+       )
 #endif
     {
 #ifdef FEAT_SYN_HL
@@ -1866,6 +1867,10 @@ buflist_new(
 #ifdef UNIX
     stat_T	st;
 #endif
+# ifdef FEAT_WINDOWS
+    win_T	*the_curwin = curwin;
+    tabpage_T	*the_curtab = curtab;
+# endif
 
     if (top_file_num == 1)
 	hash_init(&buf_hashtab);
@@ -2108,6 +2113,19 @@ buflist_new(
     }
 #endif
 
+# ifdef FEAT_WINDOWS
+    /* If the window or tab page has changed, go back to that window, if it
+     * still exists.  This avoids that ":tabedit" triggering a "tabedit" BufNew
+     * autocmd leaves a window or tab page. */
+    if ((curwin != the_curwin || curtab != the_curtab)
+	     && valid_tabpage(the_curtab) && win_valid_any_tab(the_curwin))
+    {
+	block_autocmds();
+	goto_tabpage_win(the_curtab, the_curwin);
+	unblock_autocmds();
+    }
+# endif
+
     return buf;
 }
 
diff --git a/src/testdir/test_autocmd.vim b/src/testdir/test_autocmd.vim
index 6ebfee4..cceae25 100644
--- a/src/testdir/test_autocmd.vim
+++ b/src/testdir/test_autocmd.vim
@@ -322,3 +322,40 @@ func Test_three_windows()
   call delete('Xtestje2')
   call delete('Xtestje3')
 endfunc
+
+function! Test_autocmd_bufnew_with_tabedit()
+  e aa.txt
+  augroup test_group
+      autocmd!
+      autocmd BufNew * tabedit cc.txt
+  augroup END
+
+  normal! i1
+  tabedit bb.txt
+
+  autocmd! test_group
+  augroup! test_group
+
+  let tnum = tabpagenr('$')
+  call assert_equal(3, tnum)
+  call assert_equal(2, tabpagenr())
+  call assert_equal('bb.txt', bufname('%'))
+  let li = []
+  tabrewind
+  for t in range(1, tnum)
+    let di = {}
+    let di.winnum = winnr('$')
+    let di.bufname = bufname('%')
+    let di.mod = &modified
+    call add(li, di)
+    tabnext
+  endfor
+  bwipe! aa.txt
+  bwipe! bb.txt
+  bwipe! cc.txt
+
+  call assert_equal({'winnum': 1, 'bufname': 'aa.txt', 'mod': 1}, li[0])
+  call assert_equal({'winnum': 1, 'bufname': 'bb.txt', 'mod': 0}, li[1])
+  call assert_equal({'winnum': 1, 'bufname': 'cc.txt', 'mod': 0}, li[2])
+endfunction
+" vim: shiftwidth=2 sts=2 expandtab

Raspunde prin e-mail lui