Hi Ben!

On Do, 07 Jun 2012, Ben Fritz wrote:

> On Thursday, June 7, 2012 9:09:16 AM UTC-5, Karthick wrote:
> > I tried doing this and ran into an issue..
> > 
> > The script:
> > au TabEnter * call TabEnterFunc()
> > au TabLeave * call TabLeaveFunc()
> > 
> > func! TabEnterFunc()
> >    echo 'Enter: ' . tabpagenr("$")
> >    sleep 1
> > endfunc
> > 
> > func! TabLeaveFunc()
> >    echo 'Leave: ' . tabpagenr("$")
> >    sleep 1
> > endfunction
> > 
> > If a new tab is created with:
> > :tabnew
> > the prints are,
> > Leave: 1
> > Enter: 2
> > (i.e, the number of tabs on TabEnter is one more than when TabLeave
> > triggered). This is good.
> > 
> > But when we quit a tab with :q, we get,
> > Leave: 2
> > Enter: 2
> > 
> > Shouldn't the number of buffers as returned by tabpagenr("$") be
> > up-to-date by the time TabEnter gets triggered?
> > 
> > :version
> > VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Aug  9 2008 18:46:22)
> > MS-Windows 32-bit GUI version with OLE support
> > Compiled by Bram@KIBAALE
> > Big version with GUI.

Bram,
the problem is, that enter_tabpage() is called when still both tabpages 
are valid (before freeing that tabpage), so tabpagenr() returns the 
wrong number in case of the :tabclose command.

Attached is a patch, that fixes it.

regards,
Christian

-- 
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
diff --git a/src/buffer.c b/src/buffer.c
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -4461,7 +4461,7 @@
      * When the ":tab" modifier was used do this for all tab pages.
      */
     if (had_tab > 0)
-	goto_tabpage_tp(first_tabpage);
+	goto_tabpage_tp(first_tabpage, TRUE);
     for (;;)
     {
 	tpnext = curtab->tp_next;
@@ -4573,7 +4573,7 @@
 	if (!valid_tabpage(tpnext))
 	    tpnext = first_tabpage;	/* start all over...*/
 # endif
-	goto_tabpage_tp(tpnext);
+	goto_tabpage_tp(tpnext, TRUE);
     }
 
     /*
@@ -4677,13 +4677,13 @@
     if (last_curtab != new_curtab)
     {
 	if (valid_tabpage(last_curtab))
-	    goto_tabpage_tp(last_curtab);
+	    goto_tabpage_tp(last_curtab, TRUE);
 	if (win_valid(last_curwin))
 	    win_enter(last_curwin, FALSE);
     }
     /* to window with first arg */
     if (valid_tabpage(new_curtab))
-	goto_tabpage_tp(new_curtab);
+	goto_tabpage_tp(new_curtab, TRUE);
     if (win_valid(new_curwin))
 	win_enter(new_curwin, FALSE);
 
@@ -4735,7 +4735,7 @@
      */
 #ifdef FEAT_WINDOWS
     if (had_tab > 0)
-	goto_tabpage_tp(first_tabpage);
+	goto_tabpage_tp(first_tabpage, TRUE);
     for (;;)
     {
 #endif
@@ -4771,7 +4771,7 @@
 	/* Without the ":tab" modifier only do the current tab page. */
 	if (had_tab == 0 || tpnext == NULL)
 	    break;
-	goto_tabpage_tp(tpnext);
+	goto_tabpage_tp(tpnext, TRUE);
     }
 #endif
 
diff --git a/src/eval.c b/src/eval.c
--- a/src/eval.c
+++ b/src/eval.c
@@ -16415,7 +16415,7 @@
     if (tp != NULL && varname != NULL && varp != NULL)
     {
 	save_curtab = curtab;
-	goto_tabpage_tp(tp);
+	goto_tabpage_tp(tp, TRUE);
 
 	tabvarname = alloc((unsigned)STRLEN(varname) + 3);
 	if (tabvarname != NULL)
@@ -16428,7 +16428,7 @@
 
 	/* Restore current tabpage */
 	if (valid_tabpage(save_curtab))
-	    goto_tabpage_tp(save_curtab);
+	    goto_tabpage_tp(save_curtab, TRUE);
     }
 }
 
@@ -16492,7 +16492,7 @@
 	/* set curwin to be our win, temporarily */
 	save_curwin = curwin;
 	save_curtab = curtab;
-	goto_tabpage_tp(tp);
+	goto_tabpage_tp(tp, TRUE);
 	if (!win_valid(win))
 	    return;
 	curwin = win;
@@ -16527,7 +16527,7 @@
 	/* Restore current tabpage and window, if still valid (autocomands can
 	 * make them invalid). */
 	if (valid_tabpage(save_curtab))
-	    goto_tabpage_tp(save_curtab);
+	    goto_tabpage_tp(save_curtab, TRUE);
 	if (win_valid(save_curwin))
 	{
 	    curwin = save_curwin;
diff --git a/src/ex_cmds2.c b/src/ex_cmds2.c
--- a/src/ex_cmds2.c
+++ b/src/ex_cmds2.c
@@ -2476,7 +2476,7 @@
 		/* go to window "tp" */
 		if (!valid_tabpage(tp))
 		    break;
-		goto_tabpage_tp(tp);
+		goto_tabpage_tp(tp, TRUE);
 		tp = tp->tp_next;
 	    }
 #endif
diff --git a/src/fileio.c b/src/fileio.c
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -8917,7 +8917,7 @@
 		if (wp == aucmd_win)
 		{
 		    if (tp != curtab)
-			goto_tabpage_tp(tp);
+			goto_tabpage_tp(tp, TRUE);
 		    win_goto(aucmd_win);
 		    goto win_found;
 		}
diff --git a/src/proto/window.pro b/src/proto/window.pro
--- a/src/proto/window.pro
+++ b/src/proto/window.pro
@@ -27,7 +27,7 @@
 tabpage_T *find_tabpage __ARGS((int n));
 int tabpage_index __ARGS((tabpage_T *ftp));
 void goto_tabpage __ARGS((int n));
-void goto_tabpage_tp __ARGS((tabpage_T *tp));
+void goto_tabpage_tp __ARGS((tabpage_T *tp, int force_do_autocmd));
 void goto_tabpage_win __ARGS((tabpage_T *tp, win_T *wp));
 void tabpage_move __ARGS((int nr));
 void win_goto __ARGS((win_T *wp));
diff --git a/src/window.c b/src/window.c
--- a/src/window.c
+++ b/src/window.c
@@ -45,7 +45,7 @@
 #if defined(FEAT_WINDOWS) || defined(PROTO)
 static tabpage_T *alloc_tabpage __ARGS((void));
 static int leave_tabpage __ARGS((buf_T *new_curbuf));
-static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
+static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf, int force_do_autocmd));
 static void frame_fix_height __ARGS((win_T *wp));
 static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
 static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
@@ -355,11 +355,11 @@
 						     && valid_tabpage(oldtab))
 		    {
 			newtab = curtab;
-			goto_tabpage_tp(oldtab);
+			goto_tabpage_tp(oldtab, TRUE);
 			if (curwin == wp)
 			    win_close(curwin, FALSE);
 			if (valid_tabpage(newtab))
-			    goto_tabpage_tp(newtab);
+			    goto_tabpage_tp(newtab, TRUE);
 		    }
 		}
 		break;
@@ -2121,9 +2121,10 @@
 	 * Closing the last window in a tab page.  First go to another tab
 	 * page and then close the window and the tab page.  This avoids that
 	 * curwin and curtab are invalid while we are freeing memory, they may
-	 * be used in GUI events.
+	 * be used in GUI events,
+	 * don't trigger autocommands yet, they may use wrong values, so do that below
 	 */
-	goto_tabpage_tp(alt_tabpage());
+	goto_tabpage_tp(alt_tabpage(), FALSE);
 	redraw_tabline = TRUE;
 
 	/* Safety check: Autocommands may have closed the window when jumping
@@ -2136,6 +2137,12 @@
 	    if (h != tabline_height())
 		shell_new_rows();
 	}
+	/* since goto_tabpage_tp above did not trigger tabenter autocommands,
+	 * do that now */
+#ifdef FEAT_AUTOCMD
+	apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
+	apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
+#endif
 	return TRUE;
     }
     return FALSE;
@@ -3524,7 +3531,7 @@
     }
 
     /* Failed, get back the previous Tab page */
-    enter_tabpage(curtab, curbuf);
+    enter_tabpage(curtab, curbuf, TRUE);
     return FAIL;
 }
 
@@ -3677,11 +3684,13 @@
 /*
  * Start using tab page "tp".
  * Only to be used after leave_tabpage() or freeing the current tab page.
+ * if force_do_autocmd is FALSE, don't trigger autocommands
  */
     static void
-enter_tabpage(tp, old_curbuf)
+enter_tabpage(tp, old_curbuf, force_do_autocmd)
     tabpage_T	*tp;
     buf_T	*old_curbuf UNUSED;
+    int         force_do_autocmd;
 {
     int		old_off = tp->tp_firstwin->w_winrow;
     win_T	*next_prevwin = tp->tp_prevwin;
@@ -3729,9 +3738,12 @@
 #ifdef FEAT_AUTOCMD
     /* Apply autocommands after updating the display, when 'rows' and
      * 'columns' have been set correctly. */
-    apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
-    if (old_curbuf != curbuf)
-	apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
+    if (force_do_autocmd)
+    {
+	apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
+	if (old_curbuf != curbuf)
+	    apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
+    }
 #endif
 
     redraw_all_later(CLEAR);
@@ -3807,7 +3819,7 @@
 	}
     }
 
-    goto_tabpage_tp(tp);
+    goto_tabpage_tp(tp, TRUE);
 
 #ifdef FEAT_GUI_TABLINE
     if (gui_use_tabline())
@@ -3820,8 +3832,9 @@
  * Note: doesn't update the GUI tab.
  */
     void
-goto_tabpage_tp(tp)
+goto_tabpage_tp(tp, force_do_autocmd)
     tabpage_T	*tp;
+    int         force_do_autocmd;
 {
     /* Don't repeat a message in another tab page. */
     set_keep_msg(NULL, 0);
@@ -3829,9 +3842,9 @@
     if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
     {
 	if (valid_tabpage(tp))
-	    enter_tabpage(tp, curbuf);
+	    enter_tabpage(tp, curbuf, force_do_autocmd);
 	else
-	    enter_tabpage(curtab, curbuf);
+	    enter_tabpage(curtab, curbuf, force_do_autocmd);
     }
 }
 
@@ -3844,7 +3857,7 @@
     tabpage_T	*tp;
     win_T	*wp;
 {
-    goto_tabpage_tp(tp);
+    goto_tabpage_tp(tp, TRUE);
     if (curtab == tp && win_valid(wp))
     {
 	win_enter(wp, TRUE);

Raspunde prin e-mail lui