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