changeset 12289:294f510f6d35 v8.0.1024

patch 8.0.1024: folds lost when session file has a buffer in two windows commit https://github.com/vim/vim/commit/4bebc9a0565670b853d227f81a9a31eafdb47eed Author: Bram Moolenaar <Bram@vim.org> Date: Wed Aug 30 21:07:38 2017 +0200 patch 8.0.1024: folds lost when session file has a buffer in two windows Problem: Manual folds are lost when a session file has the same buffer in two windows. (Jeansen) Solution: Use ":edit" only once. (Christian Brabandt, closes #1958)
author Christian Brabandt <cb@256bit.org>
date Wed, 30 Aug 2017 21:15:04 +0200
parents 26922aa4be57
children 278f48d7959f
files src/ex_docmd.c src/testdir/test_mksession.vim src/version.c
diffstat 3 files changed, 55 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/ex_docmd.c
+++ b/src/ex_docmd.c
@@ -11099,7 +11099,7 @@ static int ses_do_frame(frame_T *fr);
 static int ses_do_win(win_T *wp);
 static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
 static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
-static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
+static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
 
 /*
  * Write openfile commands for the current buffers to an .exrc file.
@@ -11195,7 +11195,7 @@ makeopens(
 	{
 	    if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
 					   : buf->b_wininfo->wi_fpos.lnum) < 0
-		    || ses_fname(fd, buf, &ssop_flags) == FAIL)
+		    || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
 		return FAIL;
 	}
     }
@@ -11289,7 +11289,8 @@ makeopens(
 		    )
 	    {
 		if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
-			|| ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
+			      || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
+								       == FAIL)
 		    return FAIL;
 		need_tabnew = FALSE;
 		if (!wp->w_arg_idx_invalid)
@@ -11636,9 +11637,20 @@ put_view(
 	    /*
 	     * Editing a file in this buffer: use ":edit file".
 	     * This may have side effects! (e.g., compressed or network file).
+	     *
+	     * Note, if a buffer for that file already exists, use :badd to
+	     * edit that buffer, to not lose folding information (:edit resets
+	     * folds in other buffers)
 	     */
-	    if (fputs("edit ", fd) < 0
-		    || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
+	    if (fputs("if bufexists('", fd) < 0
+		    || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
+		    || fputs("') | buffer ", fd) < 0
+		    || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
+		    || fputs(" | else | edit ", fd) < 0
+		    || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
+		    || fputs(" | endif", fd) < 0
+		    ||
+		put_eol(fd) == FAIL)
 		return FAIL;
 	}
 	else
@@ -11651,7 +11663,7 @@ put_view(
 	    {
 		/* The buffer does have a name, but it's not a file name. */
 		if (fputs("file ", fd) < 0
-			|| ses_fname(fd, wp->w_buffer, flagp) == FAIL)
+			|| ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
 		    return FAIL;
 	    }
 #endif
@@ -11823,11 +11835,11 @@ ses_arglist(
 
 /*
  * Write a buffer name to the session file.
- * Also ends the line.
+ * Also ends the line, if "add_eol" is TRUE.
  * Returns FAIL if writing fails.
  */
     static int
-ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
+ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
 {
     char_u	*name;
 
@@ -11846,7 +11858,8 @@ ses_fname(FILE *fd, buf_T *buf, unsigned
 	name = buf->b_sfname;
     else
 	name = buf->b_ffname;
-    if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
+    if (ses_put_fname(fd, name, flagp) == FAIL
+	    || (add_eol && put_eol(fd) == FAIL))
 	return FAIL;
     return OK;
 }
--- a/src/testdir/test_mksession.vim
+++ b/src/testdir/test_mksession.vim
@@ -121,5 +121,36 @@ func Test_mksession_arglist()
   argdel *
 endfunc
 
+func Test_mksession_one_buffer_two_windows()
+  edit Xtest1
+  new Xtest2
+  split
+  mksession! Xtest_mks.out
+  let lines = readfile('Xtest_mks.out')
+  let count1 = 0
+  let count2 = 0
+  let count2buf = 0
+  for line in lines
+    if line =~ 'edit \f*Xtest1$'
+      let count1 += 1
+    endif
+    if line =~ 'edit \f\{-}Xtest2'
+      let count2 += 1
+    endif
+    if line =~ 'buffer \f\{-}Xtest2'
+      let count2buf += 1
+    endif
+  endfor
+  call assert_equal(1, count1, 'Xtest1 count')
+  call assert_equal(2, count2, 'Xtest2 count')
+  call assert_equal(2, count2buf, 'Xtest2 buffer count')
+
+  close
+  bwipe!
+  !cp Xtest_mks.out /tmp
+  call delete('Xtest_mks.out')
+endfunc
+
+
 
 " vim: shiftwidth=2 sts=2 expandtab
--- a/src/version.c
+++ b/src/version.c
@@ -770,6 +770,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1024,
+/**/
     1023,
 /**/
     1022,