changeset 16912:a5e3509b33ca v8.1.1457

patch 8.1.1457: cannot reuse a buffer when loading a screen dump commit https://github.com/vim/vim/commit/87abab92f5b42319a7b10df9974ed3ce5c9b2b9b Author: Bram Moolenaar <Bram@vim.org> Date: Mon Jun 3 21:14:59 2019 +0200 patch 8.1.1457: cannot reuse a buffer when loading a screen dump Problem: Cannot reuse a buffer when loading a screen dump. Solution: Add the "bufnr" option.
author Bram Moolenaar <Bram@vim.org>
date Mon, 03 Jun 2019 21:30:05 +0200
parents aedf7194c5a0
children 1d61668fb248
files runtime/doc/eval.txt src/channel.c src/structs.h src/terminal.c src/testdir/test_terminal.vim src/version.c
diffstat 6 files changed, 77 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -9558,6 +9558,11 @@ term_dumpdiff({filename}, {filename} [, 
 		   "curwin"	     use the current window, do not split the
 				     window; fails if the current buffer
 				     cannot be |abandon|ed
+		   "bufnr"	     do not create a new buffer, use the
+				     existing buffer "bufnr".  This buffer
+				     must have been previously created with
+				     term_dumpdiff() or term_dumpload() and
+				     visible in a window.
 		   "norestore"	     do not add the terminal window to a
 				     session file
 
--- a/src/channel.c
+++ b/src/channel.c
@@ -4901,6 +4901,32 @@ get_job_options(typval_T *tv, jobopt_T *
 		opt->jo_set2 |= JO2_CURWIN;
 		opt->jo_curwin = tv_get_number(item);
 	    }
+	    else if (STRCMP(hi->hi_key, "bufnr") == 0)
+	    {
+		int nr;
+
+		if (!(supported2 & JO2_CURWIN))
+		    break;
+		opt->jo_set2 |= JO2_BUFNR;
+		nr = tv_get_number(item);
+		if (nr <= 0)
+		{
+		    semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
+		    return FAIL;
+		}
+		opt->jo_bufnr_buf = buflist_findnr(nr);
+		if (opt->jo_bufnr_buf == NULL)
+		{
+		    semsg(_(e_nobufnr), (long)nr);
+		    return FAIL;
+		}
+		if (opt->jo_bufnr_buf->b_nwindows == 0
+			|| opt->jo_bufnr_buf->b_term == NULL)
+		{
+		    semsg(_(e_invarg2), "bufnr");
+		    return FAIL;
+		}
+	    }
 	    else if (STRCMP(hi->hi_key, "hidden") == 0)
 	    {
 		if (!(supported2 & JO2_HIDDEN))
--- a/src/structs.h
+++ b/src/structs.h
@@ -1807,6 +1807,7 @@ struct channel_S {
 #define JO2_TERM_KILL	    0x4000	/* "term_kill" */
 #define JO2_ANSI_COLORS	    0x8000	/* "ansi_colors" */
 #define JO2_TTY_TYPE	    0x10000	/* "tty_type" */
+#define JO2_BUFNR	    0x20000	/* "bufnr" */
 
 #define JO_MODE_ALL	(JO_MODE + JO_IN_MODE + JO_OUT_MODE + JO_ERR_MODE)
 #define JO_CB_ALL \
@@ -1864,6 +1865,7 @@ typedef struct
     int		jo_term_cols;
     int		jo_vertical;
     int		jo_curwin;
+    buf_T	*jo_bufnr_buf;
     int		jo_hidden;
     int		jo_term_norestore;
     char_u	*jo_term_name;
--- a/src/terminal.c
+++ b/src/terminal.c
@@ -4616,7 +4616,7 @@ get_separator(int text_width, char_u *fn
 term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
 {
     jobopt_T	opt;
-    buf_T	*buf;
+    buf_T	*buf = NULL;
     char_u	buf1[NUMBUFLEN];
     char_u	buf2[NUMBUFLEN];
     char_u	*fname1;
@@ -4671,7 +4671,27 @@ term_load_dump(typval_T *argvars, typval
 	}
     }
 
-    buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
+    if (opt.jo_bufnr_buf != NULL)
+    {
+	win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
+
+	// With "bufnr" argument: enter the window with this buffer and make it
+	// empty.
+	if (wp == NULL)
+	    semsg(_(e_invarg2), "bufnr");
+	else
+	{
+	    buf = curbuf;
+	    while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
+		ml_delete((linenr_T)1, FALSE);
+	    ga_clear(&curbuf->b_term->tl_scrollback);
+	    redraw_later(NOT_VALID);
+	}
+    }
+    else
+	// Create a new terminal window.
+	buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
+
     if (buf != NULL && buf->b_term != NULL)
     {
 	int		i;
--- a/src/testdir/test_terminal.vim
+++ b/src/testdir/test_terminal.vim
@@ -1119,11 +1119,30 @@ endfunc
 
 " just testing basic functionality.
 func Test_terminal_dumpload()
+  let curbuf = winbufnr('')
   call assert_equal(1, winnr('$'))
-  call term_dumpload('dumps/Test_popup_command_01.dump')
+  let buf = term_dumpload('dumps/Test_popup_command_01.dump')
   call assert_equal(2, winnr('$'))
   call assert_equal(20, line('$'))
   call Check_dump01(0)
+
+  " Load another dump in the same window
+  let buf2 = term_dumpload('dumps/Test_diff_01.dump', {'bufnr': buf})
+  call assert_equal(buf, buf2)
+  call assert_notequal('one two three four five', trim(getline(1)))
+
+  " Load the first dump again in the same window
+  let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
+  call assert_equal(buf, buf2)
+  call Check_dump01(0)
+
+  call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
+  call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
+  new
+  let closedbuf = winbufnr('')
+  quit
+  call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
+
   quit
 endfunc
 
--- a/src/version.c
+++ b/src/version.c
@@ -768,6 +768,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1457,
+/**/
     1456,
 /**/
     1455,