f08d4e7
To: vim_dev@googlegroups.com
f08d4e7
Subject: Patch 7.4.140
f08d4e7
Fcc: outbox
f08d4e7
From: Bram Moolenaar <Bram@moolenaar.net>
f08d4e7
Mime-Version: 1.0
f08d4e7
Content-Type: text/plain; charset=UTF-8
f08d4e7
Content-Transfer-Encoding: 8bit
f08d4e7
------------
f08d4e7
f08d4e7
Patch 7.4.140
f08d4e7
Problem:    Crash when wiping out buffer triggers autocommand that wipes out
f08d4e7
	    only other buffer.
f08d4e7
Solution:   Do not delete the last buffer, make it empty. (Hirohito Higashi)
f08d4e7
Files:	    src/buffer.c
f08d4e7
f08d4e7
f08d4e7
*** ../vim-7.4.139/src/buffer.c	2013-11-06 05:26:08.000000000 +0100
f08d4e7
--- src/buffer.c	2014-01-10 16:41:22.000000000 +0100
f08d4e7
***************
f08d4e7
*** 994,999 ****
f08d4e7
--- 994,1043 ----
f08d4e7
  #if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
f08d4e7
  	|| defined(FEAT_PYTHON3) || defined(PROTO)
f08d4e7
  
f08d4e7
+ static int	empty_curbuf __ARGS((int close_others, int forceit, int action));
f08d4e7
+ 
f08d4e7
+ /*
f08d4e7
+  * Make the current buffer empty.
f08d4e7
+  * Used when it is wiped out and it's the last buffer.
f08d4e7
+  */
f08d4e7
+     static int
f08d4e7
+ empty_curbuf(close_others, forceit, action)
f08d4e7
+     int close_others;
f08d4e7
+     int forceit;
f08d4e7
+     int action;
f08d4e7
+ {
f08d4e7
+     int	    retval;
f08d4e7
+     buf_T   *buf = curbuf;
f08d4e7
+ 
f08d4e7
+     if (action == DOBUF_UNLOAD)
f08d4e7
+     {
f08d4e7
+ 	EMSG(_("E90: Cannot unload last buffer"));
f08d4e7
+ 	return FAIL;
f08d4e7
+     }
f08d4e7
+ 
f08d4e7
+     if (close_others)
f08d4e7
+     {
f08d4e7
+ 	/* Close any other windows on this buffer, then make it empty. */
f08d4e7
+ #ifdef FEAT_WINDOWS
f08d4e7
+ 	close_windows(buf, TRUE);
f08d4e7
+ #endif
f08d4e7
+     }
f08d4e7
+ 
f08d4e7
+     setpcmark();
f08d4e7
+     retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
f08d4e7
+ 					  forceit ? ECMD_FORCEIT : 0, curwin);
f08d4e7
+ 
f08d4e7
+     /*
f08d4e7
+      * do_ecmd() may create a new buffer, then we have to delete
f08d4e7
+      * the old one.  But do_ecmd() may have done that already, check
f08d4e7
+      * if the buffer still exists.
f08d4e7
+      */
f08d4e7
+     if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
f08d4e7
+ 	close_buffer(NULL, buf, action, FALSE);
f08d4e7
+     if (!close_others)
f08d4e7
+ 	need_fileinfo = FALSE;
f08d4e7
+     return retval;
f08d4e7
+ }
f08d4e7
  /*
f08d4e7
   * Implementation of the commands for the buffer list.
f08d4e7
   *
f08d4e7
***************
f08d4e7
*** 1114,1120 ****
f08d4e7
      if (unload)
f08d4e7
      {
f08d4e7
  	int	forward;
f08d4e7
- 	int	retval;
f08d4e7
  
f08d4e7
  	/* When unloading or deleting a buffer that's already unloaded and
f08d4e7
  	 * unlisted: fail silently. */
f08d4e7
--- 1158,1163 ----
f08d4e7
***************
f08d4e7
*** 1155,1184 ****
f08d4e7
  	    if (bp->b_p_bl && bp != buf)
f08d4e7
  		break;
f08d4e7
  	if (bp == NULL && buf == curbuf)
f08d4e7
! 	{
f08d4e7
! 	    if (action == DOBUF_UNLOAD)
f08d4e7
! 	    {
f08d4e7
! 		EMSG(_("E90: Cannot unload last buffer"));
f08d4e7
! 		return FAIL;
f08d4e7
! 	    }
f08d4e7
! 
f08d4e7
! 	    /* Close any other windows on this buffer, then make it empty. */
f08d4e7
! #ifdef FEAT_WINDOWS
f08d4e7
! 	    close_windows(buf, TRUE);
f08d4e7
! #endif
f08d4e7
! 	    setpcmark();
f08d4e7
! 	    retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
f08d4e7
! 					  forceit ? ECMD_FORCEIT : 0, curwin);
f08d4e7
! 
f08d4e7
! 	    /*
f08d4e7
! 	     * do_ecmd() may create a new buffer, then we have to delete
f08d4e7
! 	     * the old one.  But do_ecmd() may have done that already, check
f08d4e7
! 	     * if the buffer still exists.
f08d4e7
! 	     */
f08d4e7
! 	    if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
f08d4e7
! 		close_buffer(NULL, buf, action, FALSE);
f08d4e7
! 	    return retval;
f08d4e7
! 	}
f08d4e7
  
f08d4e7
  #ifdef FEAT_WINDOWS
f08d4e7
  	/*
f08d4e7
--- 1198,1204 ----
f08d4e7
  	    if (bp->b_p_bl && bp != buf)
f08d4e7
  		break;
f08d4e7
  	if (bp == NULL && buf == curbuf)
f08d4e7
! 	    return empty_curbuf(TRUE, forceit, action);
f08d4e7
  
f08d4e7
  #ifdef FEAT_WINDOWS
f08d4e7
  	/*
f08d4e7
***************
f08d4e7
*** 1212,1218 ****
f08d4e7
  
f08d4e7
  	/*
f08d4e7
  	 * Deleting the current buffer: Need to find another buffer to go to.
f08d4e7
! 	 * There must be another, otherwise it would have been handled above.
f08d4e7
  	 * First use au_new_curbuf, if it is valid.
f08d4e7
  	 * Then prefer the buffer we most recently visited.
f08d4e7
  	 * Else try to find one that is loaded, after the current buffer,
f08d4e7
--- 1232,1239 ----
f08d4e7
  
f08d4e7
  	/*
f08d4e7
  	 * Deleting the current buffer: Need to find another buffer to go to.
f08d4e7
! 	 * There should be another, otherwise it would have been handled
f08d4e7
! 	 * above.  However, autocommands may have deleted all buffers.
f08d4e7
  	 * First use au_new_curbuf, if it is valid.
f08d4e7
  	 * Then prefer the buffer we most recently visited.
f08d4e7
  	 * Else try to find one that is loaded, after the current buffer,
f08d4e7
***************
f08d4e7
*** 1311,1316 ****
f08d4e7
--- 1332,1344 ----
f08d4e7
  	}
f08d4e7
      }
f08d4e7
  
f08d4e7
+     if (buf == NULL)
f08d4e7
+     {
f08d4e7
+ 	/* Autocommands must have wiped out all other buffers.  Only option
f08d4e7
+ 	 * now is to make the current buffer empty. */
f08d4e7
+ 	return empty_curbuf(FALSE, forceit, action);
f08d4e7
+     }
f08d4e7
+ 
f08d4e7
      /*
f08d4e7
       * make buf current buffer
f08d4e7
       */
f08d4e7
*** ../vim-7.4.139/src/version.c	2014-01-10 15:53:09.000000000 +0100
f08d4e7
--- src/version.c	2014-01-10 16:36:03.000000000 +0100
f08d4e7
***************
f08d4e7
*** 740,741 ****
f08d4e7
--- 740,743 ----
f08d4e7
  {   /* Add new patch number below this line */
f08d4e7
+ /**/
f08d4e7
+     140,
f08d4e7
  /**/
f08d4e7
f08d4e7
-- 
f08d4e7
hundred-and-one symptoms of being an internet addict:
f08d4e7
133. You communicate with people on other continents more than you
f08d4e7
     do with your own neighbors.
f08d4e7
f08d4e7
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
f08d4e7
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
f08d4e7
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
f08d4e7
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///