Skip to content

Commit 59d8e56

Browse files
committed
patch 8.2.1967: the session file does not restore the alternate file
Problem: The session file does not restore the alternate file. Solution: Add ":balt". Works like ":badd" and also sets the buffer as the alternate file. Use it in the session file. (closes #7269, closes #6714)
1 parent cbcd9cb commit 59d8e56

9 files changed

Lines changed: 74 additions & 37 deletions

File tree

runtime/doc/windows.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*windows.txt* For Vim version 8.2. Last change: 2020 Sep 02
1+
*windows.txt* For Vim version 8.2. Last change: 2020 Nov 07
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -1119,6 +1119,11 @@ list of buffers. |unlisted-buffer|
11191119
line when the buffer is first entered. Note that other
11201120
commands after the + will be ignored.
11211121

1122+
*:balt*
1123+
:balt [+lnum] {fname}
1124+
Like `:badd` and also set the alternate file for the current
1125+
window to {fname}.
1126+
11221127
:[N]bd[elete][!] *:bd* *:bdel* *:bdelete* *E516*
11231128
:bd[elete][!] [N]
11241129
Unload buffer [N] (default: current buffer) and delete it from

src/ex_cmdidxs.h

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,30 @@ static const unsigned short cmdidxs1[26] =
77
{
88
/* a */ 0,
99
/* b */ 19,
10-
/* c */ 42,
11-
/* d */ 108,
12-
/* e */ 133,
13-
/* f */ 156,
14-
/* g */ 173,
15-
/* h */ 179,
16-
/* i */ 188,
17-
/* j */ 207,
18-
/* k */ 209,
19-
/* l */ 214,
20-
/* m */ 276,
21-
/* n */ 294,
22-
/* o */ 314,
23-
/* p */ 326,
24-
/* q */ 365,
25-
/* r */ 368,
26-
/* s */ 388,
27-
/* t */ 457,
28-
/* u */ 502,
29-
/* v */ 513,
30-
/* w */ 533,
31-
/* x */ 547,
32-
/* y */ 557,
33-
/* z */ 558
10+
/* c */ 43,
11+
/* d */ 109,
12+
/* e */ 134,
13+
/* f */ 157,
14+
/* g */ 174,
15+
/* h */ 180,
16+
/* i */ 189,
17+
/* j */ 208,
18+
/* k */ 210,
19+
/* l */ 215,
20+
/* m */ 277,
21+
/* n */ 295,
22+
/* o */ 315,
23+
/* p */ 327,
24+
/* q */ 366,
25+
/* r */ 369,
26+
/* s */ 389,
27+
/* t */ 458,
28+
/* u */ 503,
29+
/* v */ 514,
30+
/* w */ 534,
31+
/* x */ 548,
32+
/* y */ 558,
33+
/* z */ 559
3434
};
3535

3636
/*
@@ -42,7 +42,7 @@ static const unsigned short cmdidxs1[26] =
4242
static const unsigned char cmdidxs2[26][26] =
4343
{ /* a b c d e f g h i j k l m n o p q r s t u v w x y z */
4444
/* a */ { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 6, 0, 0, 0, 7, 15, 0, 16, 0, 0, 0, 0, 0 },
45-
/* b */ { 2, 0, 0, 4, 5, 7, 0, 0, 0, 0, 0, 8, 9, 10, 11, 12, 0, 13, 0, 0, 0, 0, 22, 0, 0, 0 },
45+
/* b */ { 2, 0, 0, 5, 6, 8, 0, 0, 0, 0, 0, 9, 10, 11, 12, 13, 0, 14, 0, 0, 0, 0, 23, 0, 0, 0 },
4646
/* c */ { 3, 12, 16, 18, 20, 22, 25, 0, 0, 0, 0, 33, 37, 40, 46, 56, 58, 59, 60, 0, 62, 0, 65, 0, 0, 0 },
4747
/* d */ { 0, 0, 0, 0, 0, 0, 0, 0, 8, 18, 0, 19, 0, 0, 20, 0, 0, 22, 23, 0, 0, 0, 0, 0, 0, 0 },
4848
/* e */ { 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 7, 9, 10, 0, 0, 0, 0, 0, 0, 0, 17, 0, 18, 0, 0 },
@@ -69,4 +69,4 @@ static const unsigned char cmdidxs2[26][26] =
6969
/* z */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
7070
};
7171

72-
static const int command_count = 573;
72+
static const int command_count = 574;

src/ex_cmds.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,6 +2459,7 @@ getfile(
24592459
* ECMD_OLDBUF: use existing buffer if it exists
24602460
* ECMD_FORCEIT: ! used for Ex command
24612461
* ECMD_ADDBUF: don't edit, just add to buffer list
2462+
* ECMD_ALTBUF: like ECMD_ADDBUF and also set the alternate file
24622463
* oldwin: Should be "curwin" when editing a new buffer in the current
24632464
* window, NULL when splitting the window first. When not NULL info
24642465
* of the previous buffer for "oldwin" is stored.
@@ -2555,7 +2556,8 @@ do_ecmd(
25552556
fname_case(sfname, 0); // set correct case for sfname
25562557
#endif
25572558

2558-
if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL))
2559+
if ((flags & (ECMD_ADDBUF | ECMD_ALTBUF))
2560+
&& (ffname == NULL || *ffname == NUL))
25592561
goto theend;
25602562

25612563
if (ffname == NULL)
@@ -2584,7 +2586,7 @@ do_ecmd(
25842586
*/
25852587
if ( ((!other_file && !(flags & ECMD_OLDBUF))
25862588
|| (curbuf->b_nwindows == 1
2587-
&& !(flags & (ECMD_HIDE | ECMD_ADDBUF))))
2589+
&& !(flags & (ECMD_HIDE | ECMD_ADDBUF | ECMD_ALTBUF))))
25882590
&& check_changed(curbuf, (p_awa ? CCGD_AW : 0)
25892591
| (other_file ? 0 : CCGD_MULTWIN)
25902592
| ((flags & ECMD_FORCEIT) ? CCGD_FORCEIT : 0)
@@ -2633,7 +2635,7 @@ do_ecmd(
26332635
*/
26342636
if (other_file)
26352637
{
2636-
if (!(flags & ECMD_ADDBUF))
2638+
if (!(flags & (ECMD_ADDBUF | ECMD_ALTBUF)))
26372639
{
26382640
if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
26392641
curwin->w_alt_fnum = curbuf->b_fnum;
@@ -2645,22 +2647,25 @@ do_ecmd(
26452647
buf = buflist_findnr(fnum);
26462648
else
26472649
{
2648-
if (flags & ECMD_ADDBUF)
2650+
if (flags & (ECMD_ADDBUF | ECMD_ALTBUF))
26492651
{
26502652
// Default the line number to zero to avoid that a wininfo item
26512653
// is added for the current window.
26522654
linenr_T tlnum = 0;
2655+
buf_T *newbuf;
26532656

26542657
if (command != NULL)
26552658
{
26562659
tlnum = atol((char *)command);
26572660
if (tlnum <= 0)
26582661
tlnum = 1L;
26592662
}
2660-
// Add BLN_NOCURWIN to avoid a new wininfo items is assocated
2663+
// Add BLN_NOCURWIN to avoid a new wininfo items are assocated
26612664
// with the current window.
2662-
(void)buflist_new(ffname, sfname, tlnum,
2665+
newbuf = buflist_new(ffname, sfname, tlnum,
26632666
BLN_LISTED | BLN_NOCURWIN);
2667+
if (newbuf != NULL && (flags & ECMD_ALTBUF))
2668+
curwin->w_alt_fnum = newbuf->b_fnum;
26642669
goto theend;
26652670
}
26662671
buf = buflist_new(ffname, sfname, 0L,
@@ -2818,7 +2823,7 @@ do_ecmd(
28182823
}
28192824
else // !other_file
28202825
{
2821-
if ((flags & ECMD_ADDBUF) || check_fname() == FAIL)
2826+
if ((flags & (ECMD_ADDBUF | ECMD_ALTBUF)) || check_fname() == FAIL)
28222827
goto theend;
28232828

28242829
oldbuf = (flags & ECMD_OLDBUF);

src/ex_cmds.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ EXCMD(CMD_ball, "ball", ex_buffer_all,
178178
EXCMD(CMD_badd, "badd", ex_edit,
179179
EX_NEEDARG|EX_FILE1|EX_CMDARG|EX_TRLBAR|EX_CMDWIN|EX_LOCK_OK,
180180
ADDR_NONE),
181+
EXCMD(CMD_balt, "balt", ex_edit,
182+
EX_NEEDARG|EX_FILE1|EX_CMDARG|EX_TRLBAR|EX_CMDWIN|EX_LOCK_OK,
183+
ADDR_NONE),
181184
EXCMD(CMD_bdelete, "bdelete", ex_bunload,
182185
EX_BANG|EX_RANGE|EX_BUFNAME|EX_COUNT|EX_EXTRA|EX_TRLBAR,
183186
ADDR_BUFFERS),

src/ex_docmd.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6506,7 +6506,7 @@ ex_open(exarg_T *eap)
65066506
}
65076507

65086508
/*
6509-
* ":edit", ":badd", ":visual".
6509+
* ":edit", ":badd", ":balt", ":visual".
65106510
*/
65116511
static void
65126512
ex_edit(exarg_T *eap)
@@ -6621,7 +6621,8 @@ do_exedit(
66216621
+ (eap->forceit ? ECMD_FORCEIT : 0)
66226622
// after a split we can use an existing buffer
66236623
+ (old_curwin != NULL ? ECMD_OLDBUF : 0)
6624-
+ (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
6624+
+ (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
6625+
+ (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
66256626
, old_curwin == NULL ? curwin : NULL) == FAIL)
66266627
{
66276628
// Editing the file failed. If the window was split, close it.
@@ -8472,7 +8473,7 @@ find_cmdline_var(char_u *src, int *usedlen)
84728473
* Evaluate cmdline variables.
84738474
*
84748475
* change '%' to curbuf->b_ffname
8475-
* '#' to curwin->w_altfile
8476+
* '#' to curwin->w_alt_fnum
84768477
* '<cword>' to word under the cursor
84778478
* '<cWORD>' to WORD under the cursor
84788479
* '<cexpr>' to C-expression under the cursor

src/session.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,19 @@ put_view(
397397
}
398398
}
399399

400+
if (wp->w_alt_fnum)
401+
{
402+
buf_T *alt = buflist_findnr(wp->w_alt_fnum);
403+
404+
// Set the alternate file.
405+
if (alt != NULL
406+
&& alt->b_fname != NULL
407+
&& *alt->b_fname != NUL
408+
&& (fputs("balt ", fd) < 0
409+
|| ses_fname(fd, alt, flagp, TRUE) == FAIL))
410+
return FAIL;
411+
}
412+
400413
// Local mappings and abbreviations.
401414
if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
402415
&& makemap(fd, wp->w_buffer) == FAIL)

src/testdir/test_buffer.vim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,11 @@ func Test_badd_options()
374374
bwipe! SomeNewBuffer
375375
endfunc
376376

377+
func Test_balt()
378+
new SomeNewBuffer
379+
balt +3 OtherBuffer
380+
e #
381+
call assert_equal('OtherBuffer', bufname())
382+
endfunc
383+
377384
" vim: shiftwidth=2 sts=2 expandtab

src/version.c

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

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
1967,
753755
/**/
754756
1966,
755757
/**/

src/vim.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,7 @@ extern int (*dyn_libintl_wputenv)(const wchar_t *envstring);
10291029
#define ECMD_OLDBUF 0x04 // use existing buffer if it exists
10301030
#define ECMD_FORCEIT 0x08 // ! used in Ex command
10311031
#define ECMD_ADDBUF 0x10 // don't edit, just add to buffer list
1032+
#define ECMD_ALTBUF 0x20 // like ECMD_ADDBUF and set the alternate file
10321033

10331034
// for lnum argument in do_ecmd()
10341035
#define ECMD_LASTL (linenr_T)0 // use last position in loaded file

0 commit comments

Comments
 (0)