Skip to content

Commit 26d4b89

Browse files
committed
patch 8.1.0149: session is wrong with multiple tabs when :lcd was used
Problem: The generated sessions file does not restore tabs properly if :lcd was used in one of them. Solution: Create the tab pages before setting the directory. (Yee Cheng Chin, closes #3152)
1 parent 92959fa commit 26d4b89

3 files changed

Lines changed: 87 additions & 8 deletions

File tree

src/ex_docmd.c

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5491,7 +5491,7 @@ ex_abclear(exarg_T *eap)
54915491
ex_autocmd(exarg_T *eap)
54925492
{
54935493
/*
5494-
* Disallow auto commands from .exrc and .vimrc in current
5494+
* Disallow autocommands from .exrc and .vimrc in current
54955495
* directory for security reasons.
54965496
*/
54975497
if (secure)
@@ -11080,10 +11080,13 @@ makeopens(
1108011080
#endif
1108111081

1108211082
/*
11083-
* Close all windows but one.
11083+
* Close all windows and tabs but one.
1108411084
*/
1108511085
if (put_line(fd, "silent only") == FAIL)
1108611086
return FAIL;
11087+
if ((ssop_flags & SSOP_TABPAGES)
11088+
&& put_line(fd, "silent tabonly") == FAIL)
11089+
return FAIL;
1108711090

1108811091
/*
1108911092
* Now a :cd command to the session directory or the current directory
@@ -11193,9 +11196,33 @@ makeopens(
1119311196
*/
1119411197
tab_firstwin = firstwin; /* first window in tab page "tabnr" */
1119511198
tab_topframe = topframe;
11199+
if ((ssop_flags & SSOP_TABPAGES))
11200+
{
11201+
int num_tabs;
11202+
11203+
/*
11204+
* Similar to ses_win_rec() below, populate the tab pages first so
11205+
* later local options won't be copied to the new tabs.
11206+
*/
11207+
for (tabnr = 1; ; ++tabnr)
11208+
{
11209+
tabpage_T *tp = find_tabpage(tabnr);
11210+
11211+
if (tp == NULL) /* done all tab pages */
11212+
break;
11213+
11214+
if (tabnr > 1 && put_line(fd, "tabnew") == FAIL)
11215+
return FAIL;
11216+
}
11217+
11218+
num_tabs = tabnr - 1;
11219+
if (num_tabs > 1 && (fprintf(fd, "tabnext -%d", num_tabs - 1) < 0
11220+
|| put_eol(fd) == FAIL))
11221+
return FAIL;
11222+
}
1119611223
for (tabnr = 1; ; ++tabnr)
1119711224
{
11198-
int need_tabnew = FALSE;
11225+
int need_tabnext = FALSE;
1119911226
int cnr = 1;
1120011227

1120111228
if ((ssop_flags & SSOP_TABPAGES))
@@ -11215,7 +11242,7 @@ makeopens(
1121511242
tab_topframe = tp->tp_topframe;
1121611243
}
1121711244
if (tabnr > 1)
11218-
need_tabnew = TRUE;
11245+
need_tabnext = TRUE;
1121911246
}
1122011247

1122111248
/*
@@ -11233,19 +11260,22 @@ makeopens(
1123311260
#endif
1123411261
)
1123511262
{
11236-
if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
11263+
if (need_tabnext && put_line(fd, "tabnext") == FAIL)
11264+
return FAIL;
11265+
need_tabnext = FALSE;
11266+
11267+
if (fputs("edit ", fd) < 0
1123711268
|| ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
1123811269
== FAIL)
1123911270
return FAIL;
11240-
need_tabnew = FALSE;
1124111271
if (!wp->w_arg_idx_invalid)
1124211272
edited_win = wp;
1124311273
break;
1124411274
}
1124511275
}
1124611276

1124711277
/* If no file got edited create an empty tab page. */
11248-
if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11278+
if (need_tabnext && put_line(fd, "tabnext") == FAIL)
1124911279
return FAIL;
1125011280

1125111281
/*
@@ -11348,7 +11378,7 @@ makeopens(
1134811378
/*
1134911379
* Wipe out an empty unnamed buffer we started in.
1135011380
*/
11351-
if (put_line(fd, "if exists('s:wipebuf') && s:wipebuf != bufnr('%')")
11381+
if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
1135211382
== FAIL)
1135311383
return FAIL;
1135411384
if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)

src/testdir/test_mksession.vim

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,53 @@ func Test_mksession_one_buffer_two_windows()
162162
call delete('Xtest_mks.out')
163163
endfunc
164164

165+
func Test_mksession_lcd_multiple_tabs()
166+
tabnew
167+
tabnew
168+
lcd
169+
tabfirst
170+
lcd
171+
mksession! Xtest_mks.out
172+
tabonly
173+
source Xtest_mks.out
174+
call assert_true(haslocaldir(), 'Tab 1 localdir')
175+
tabnext 2
176+
call assert_true(!haslocaldir(), 'Tab 2 localdir')
177+
tabnext 3
178+
call assert_true(haslocaldir(), 'Tab 3 localdir')
179+
call delete('Xtest_mks.out')
180+
endfunc
181+
182+
func Test_mksession_blank_tabs()
183+
tabnew
184+
tabnew
185+
tabnew
186+
tabnext 3
187+
mksession! Xtest_mks.out
188+
tabnew
189+
tabnew
190+
tabnext 2
191+
source Xtest_mks.out
192+
call assert_equal(4, tabpagenr('$'), 'session restore should restore number of tabs')
193+
call assert_equal(3, tabpagenr(), 'session restore should restore the active tab')
194+
call delete('Xtest_mks.out')
195+
endfunc
196+
197+
func Test_mksession_blank_windows()
198+
split
199+
split
200+
split
201+
3 wincmd w
202+
mksession! Xtest_mks.out
203+
split
204+
split
205+
2 wincmd w
206+
source Xtest_mks.out
207+
call assert_equal(4, winnr('$'), 'session restore should restore number of windows')
208+
call assert_equal(3, winnr(), 'session restore should restore the active window')
209+
call delete('Xtest_mks.out')
210+
endfunc
211+
165212
if has('terminal')
166213

167214
func Test_mksession_terminal_shell()

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,8 @@ static char *(features[]) =
789789

790790
static int included_patches[] =
791791
{ /* Add new patch number below this line */
792+
/**/
793+
149,
792794
/**/
793795
148,
794796
/**/

0 commit comments

Comments
 (0)