mksession: simplify generated commands

Doing ":file …" immediately after is enough to fixup the :terminal
buffer name.

ref #5250
This commit is contained in:
Justin M. Keyes 2020-01-26 02:43:12 -08:00
parent cf67f19ac2
commit 1e103b3c12
2 changed files with 47 additions and 71 deletions

View File

@ -8090,14 +8090,12 @@ static void close_redir(void)
static int mksession_nl = FALSE; /* use NL only in put_eol() */ static int mksession_nl = FALSE; /* use NL only in put_eol() */
#endif #endif
/* /// ":mkexrc", ":mkvimrc", ":mkview", ":mksession".
* ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
*/
static void ex_mkrc(exarg_T *eap) static void ex_mkrc(exarg_T *eap)
{ {
FILE *fd; FILE *fd;
int failed = false; int failed = false;
int view_session = false; int view_session = false; // :mkview, :mksession
int using_vdir = false; // using 'viewdir'? int using_vdir = false; // using 'viewdir'?
char *viewFile = NULL; char *viewFile = NULL;
unsigned *flagp; unsigned *flagp;
@ -8159,11 +8157,11 @@ static void ex_mkrc(exarg_T *eap)
failed = TRUE; failed = TRUE;
} }
if (!view_session if (!view_session || (eap->cmdidx == CMD_mksession
|| (eap->cmdidx == CMD_mksession && (*flagp & SSOP_OPTIONS))) {
&& (*flagp & SSOP_OPTIONS)))
failed |= (makemap(fd, NULL) == FAIL failed |= (makemap(fd, NULL) == FAIL
|| makeset(fd, OPT_GLOBAL, FALSE) == FAIL); || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
}
if (!failed && view_session) { if (!failed && view_session) {
if (put_line(fd, if (put_line(fd,
@ -9119,15 +9117,13 @@ char_u *expand_sfile(char_u *arg)
} }
/* /// Writes commands for restoring the current buffers, for :mksession.
* Write openfile commands for the current buffers to an .exrc file. ///
* Return FAIL on error, OK otherwise. /// @param dirnow Current directory name
*/ /// @param fd File descriptor to write to
static int ///
makeopens( /// @return FAIL on error, OK otherwise.
FILE *fd, static int makeopens(FILE *fd, char_u *dirnow)
char_u *dirnow /* Current directory name */
)
{ {
int only_save_windows = TRUE; int only_save_windows = TRUE;
int nr; int nr;
@ -9240,12 +9236,11 @@ makeopens(
restore_stal = TRUE; restore_stal = TRUE;
} }
/* //
* May repeat putting Windows for each tab, when "tabpages" is in // For each tab:
* 'sessionoptions'. // - Put windows for each tab, when "tabpages" is in 'sessionoptions'.
* Don't use goto_tabpage(), it may change directory and trigger // - Don't use goto_tabpage(), it may change CWD and trigger autocommands.
* autocommands. //
*/
tab_firstwin = firstwin; /* first window in tab page "tabnr" */ tab_firstwin = firstwin; /* first window in tab page "tabnr" */
tab_topframe = topframe; tab_topframe = topframe;
for (tabnr = 1;; tabnr++) { for (tabnr = 1;; tabnr++) {
@ -9269,11 +9264,11 @@ makeopens(
need_tabnew = TRUE; need_tabnew = TRUE;
} }
/* //
* Before creating the window layout, try loading one file. If this // Before creating the window layout, try loading one file. If this
* is aborted we don't end up with a number of useless windows. // is aborted we don't end up with a number of useless windows.
* This may have side effects! (e.g., compressed or network file). // This may have side effects! (e.g., compressed or network file).
*/ //
for (wp = tab_firstwin; wp != NULL; wp = wp->w_next) { for (wp = tab_firstwin; wp != NULL; wp = wp->w_next) {
if (ses_do_win(wp) if (ses_do_win(wp)
&& wp->w_buffer->b_ffname != NULL && wp->w_buffer->b_ffname != NULL
@ -9343,13 +9338,9 @@ makeopens(
return FAIL; return FAIL;
} }
/* //
* Restore the view of the window (options, file, cursor, etc.). // Restore the view of the window (options, file, cursor, etc.).
*/ //
if (put_line(fd, "let s:buffer_names = []") == FAIL) {
return FAIL;
}
for (wp = tab_firstwin; wp != NULL; wp = wp->w_next) { for (wp = tab_firstwin; wp != NULL; wp = wp->w_next) {
if (!ses_do_win(wp)) if (!ses_do_win(wp))
continue; continue;
@ -9361,16 +9352,6 @@ makeopens(
next_arg_idx = wp->w_arg_idx; next_arg_idx = wp->w_arg_idx;
} }
if (put_line(fd, "for [s:name, s:tbuf] in s:buffer_names") == FAIL
|| put_line(fd, " if buflisted(s:tbuf)") == FAIL
|| put_line(fd, " execute 'buffer' fnameescape(s:tbuf)") == FAIL
|| put_line(fd, " execute 'file' fnameescape(s:name)") == FAIL
|| put_line(fd, " endif") == FAIL
|| put_line(fd, "endfor") == FAIL
|| put_line(fd, "unlet! s:buffer_names s:tbuf s:name") == FAIL) {
return FAIL;
}
/* The argument index in the first tab page is zero, need to set it in /* The argument index in the first tab page is zero, need to set it in
* each window. For further tab pages it's the window where we do * each window. For further tab pages it's the window where we do
* "tabedit". */ * "tabedit". */
@ -9677,14 +9658,7 @@ put_view(
return FAIL; return FAIL;
} }
if (fputs("call add(s:buffer_names, [bufname('%'),'", fd) < 0 if (fputs("silent file ", fd) < 0
|| ses_fname(fd, wp->w_buffer, flagp, false) == FAIL
|| fputs("'])", fd) < 0
|| put_eol(fd) == FAIL) {
return FAIL;
}
if (fputs("file ", fd) < 0
|| ses_fname(fd, wp->w_buffer, flagp, false) == FAIL || ses_fname(fd, wp->w_buffer, flagp, false) == FAIL
|| put_eol(fd) == FAIL) { || put_eol(fd) == FAIL) {
return FAIL; return FAIL;

View File

@ -26,6 +26,27 @@ describe(':mksession', function()
rmdir(tab_dir) rmdir(tab_dir)
end) end)
it('restores same :terminal buf in splits', function()
-- If the same :terminal is displayed in multiple windows, :mksession
-- should restore it as such.
-- Create two windows showing the same :terminal buffer.
command('terminal')
command('split')
command('terminal')
command('split')
command('mksession '..session_file)
-- Create a new test instance of Nvim.
command('qall!')
clear()
-- Restore session.
command('source '..session_file)
eq({3,3,2},
{funcs.winbufnr(1), funcs.winbufnr(2), funcs.winbufnr(3)})
end)
it('restores tab-local working directories', function() it('restores tab-local working directories', function()
local tmpfile_base = file_prefix .. '-tmpfile' local tmpfile_base = file_prefix .. '-tmpfile'
local cwd_dir = funcs.getcwd() local cwd_dir = funcs.getcwd()
@ -91,23 +112,4 @@ describe(':mksession', function()
matches('^term://'..pesc(expected_cwd)..'//%d+:', funcs.expand('%')) matches('^term://'..pesc(expected_cwd)..'//%d+:', funcs.expand('%'))
command('qall!') command('qall!')
end) end)
it('restores multiple windows with same terminal instances', function()
-- Create a view with two buffers referencing the same terminal instance
command('terminal')
command('split')
command('mksession ' .. session_file)
clear()
command('source ' .. session_file)
-- Getting the name of the buffer shown to compare with the other window
local eval = helpers.eval
command('exe 1 . "wincmd w"')
local expected_pid = eval('b:terminal_job_pid')
command('exe 2 . "wincmd w"')
eq(expected_pid, eval('b:terminal_job_pid'))
end)
end) end)