Skip to content

Commit 110bd60

Browse files
committed
patch 8.1.0401: can't get swap name of another buffer
Problem: Can't get swap name of another buffer. Solution: Add swapname(). (Ozaki Kiichi, closes #3441)
1 parent d2b58c0 commit 110bd60

4 files changed

Lines changed: 59 additions & 5 deletions

File tree

runtime/doc/eval.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,6 +2417,7 @@ submatch({nr} [, {list}]) String or List
24172417
substitute({expr}, {pat}, {sub}, {flags})
24182418
String all {pat} in {expr} replaced with {sub}
24192419
swapinfo({fname}) Dict information about swap file {fname}
2420+
swapname({expr}) String swap file of buffer {expr}
24202421
synID({lnum}, {col}, {trans}) Number syntax ID at {lnum} and {col}
24212422
synIDattr({synID}, {what} [, {mode}])
24222423
String attribute {what} of syntax ID {synID}
@@ -8042,6 +8043,13 @@ swapinfo({fname}) *swapinfo()*
80428043
Not a swap file: does not contain correct block ID
80438044
Magic number mismatch: Info in first block is invalid
80448045

8046+
swapname({expr}) *swapname()*
8047+
The result is the swap file path of the buffer {expr}.
8048+
For the use of {expr}, see |bufname()| above.
8049+
If buffer {expr} is the current buffer, the result is equal to
8050+
|:swapname| (unless no swap file).
8051+
If buffer {expr} has no swap file, returns an empty string.
8052+
80458053
synID({lnum}, {col}, {trans}) *synID()*
80468054
The result is a Number, which is the syntax ID at the position
80478055
{lnum} and {col} in the current window.

src/evalfunc.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ static void f_strwidth(typval_T *argvars, typval_T *rettv);
399399
static void f_submatch(typval_T *argvars, typval_T *rettv);
400400
static void f_substitute(typval_T *argvars, typval_T *rettv);
401401
static void f_swapinfo(typval_T *argvars, typval_T *rettv);
402+
static void f_swapname(typval_T *argvars, typval_T *rettv);
402403
static void f_synID(typval_T *argvars, typval_T *rettv);
403404
static void f_synIDattr(typval_T *argvars, typval_T *rettv);
404405
static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
@@ -865,6 +866,7 @@ static struct fst
865866
{"submatch", 1, 2, f_submatch},
866867
{"substitute", 4, 4, f_substitute},
867868
{"swapinfo", 1, 1, f_swapinfo},
869+
{"swapname", 1, 1, f_swapname},
868870
{"synID", 3, 3, f_synID},
869871
{"synIDattr", 2, 3, f_synIDattr},
870872
{"synIDtrans", 1, 1, f_synIDtrans},
@@ -12341,6 +12343,23 @@ f_swapinfo(typval_T *argvars, typval_T *rettv)
1234112343
get_b0_dict(get_tv_string(argvars), rettv->vval.v_dict);
1234212344
}
1234312345

12346+
/*
12347+
* "swapname(expr)" function
12348+
*/
12349+
static void
12350+
f_swapname(typval_T *argvars, typval_T *rettv)
12351+
{
12352+
buf_T *buf;
12353+
12354+
rettv->v_type = VAR_STRING;
12355+
buf = get_buf_tv(&argvars[0], FALSE);
12356+
if (buf == NULL || buf->b_ml.ml_mfp == NULL
12357+
|| buf->b_ml.ml_mfp->mf_fname == NULL)
12358+
rettv->vval.v_string = NULL;
12359+
else
12360+
rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12361+
}
12362+
1234412363
/*
1234512364
* "synID(lnum, col, trans)" function
1234612365
*/

src/testdir/test_swap.vim

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
" Tests for the swap feature
22

3+
func s:swapname()
4+
return trim(execute('swapname'))
5+
endfunc
6+
37
" Tests for 'directory' option.
48
func Test_swap_directory()
59
if !has("unix")
@@ -17,7 +21,7 @@ func Test_swap_directory()
1721
" Verify that the swap file doesn't exist in the current directory
1822
call assert_equal([], glob(".Xtest1*.swp", 1, 1, 1))
1923
edit Xtest1
20-
let swfname = split(execute("swapname"))[0]
24+
let swfname = s:swapname()
2125
call assert_equal([swfname], glob(swfname, 1, 1, 1))
2226

2327
" './dir', swap file in a directory relative to the file
@@ -27,7 +31,7 @@ func Test_swap_directory()
2731
edit Xtest1
2832
call assert_equal([], glob(swfname, 1, 1, 1))
2933
let swfname = "Xtest2/Xtest1.swp"
30-
call assert_equal(swfname, split(execute("swapname"))[0])
34+
call assert_equal(swfname, s:swapname())
3135
call assert_equal([swfname], glob("Xtest2/*", 1, 1, 1))
3236

3337
" 'dir', swap file in directory relative to the current dir
@@ -38,7 +42,7 @@ func Test_swap_directory()
3842
edit Xtest2/Xtest3
3943
call assert_equal(["Xtest2/Xtest3"], glob("Xtest2/*", 1, 1, 1))
4044
let swfname = "Xtest.je/Xtest3.swp"
41-
call assert_equal(swfname, split(execute("swapname"))[0])
45+
call assert_equal(swfname, s:swapname())
4246
call assert_equal([swfname], glob("Xtest.je/*", 1, 1, 1))
4347

4448
set dir&
@@ -70,7 +74,7 @@ func Test_swap_group()
7074
throw 'Skipped: cannot set second group on test file'
7175
else
7276
split Xtest
73-
let swapname = substitute(execute('swapname'), '[[:space:]]', '', 'g')
77+
let swapname = s:swapname()
7478
call assert_match('Xtest', swapname)
7579
" Group of swapfile must now match original file.
7680
call assert_match(' ' . groups[1] . ' \d', system('ls -l ' . swapname))
@@ -102,7 +106,7 @@ func Test_swapinfo()
102106
new Xswapinfo
103107
call setline(1, ['one', 'two', 'three'])
104108
w
105-
let fname = trim(execute('swapname'))
109+
let fname = s:swapname()
106110
call assert_match('Xswapinfo', fname)
107111
let info = swapinfo(fname)
108112

@@ -136,3 +140,24 @@ func Test_swapinfo()
136140
call assert_equal('Not a swap file', info.error)
137141
call delete('Xnotaswapfile')
138142
endfunc
143+
144+
func Test_swapname()
145+
edit Xtest1
146+
let expected = s:swapname()
147+
call assert_equal(expected, swapname('%'))
148+
149+
new Xtest2
150+
let buf = bufnr('%')
151+
let expected = s:swapname()
152+
wincmd p
153+
call assert_equal(expected, swapname(buf))
154+
155+
new Xtest3
156+
setlocal noswapfile
157+
call assert_equal('', swapname('%'))
158+
159+
bwipe!
160+
call delete('Xtest1')
161+
call delete('Xtest2')
162+
call delete('Xtest3')
163+
endfunc

src/version.c

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

795795
static int included_patches[] =
796796
{ /* Add new patch number below this line */
797+
/**/
798+
401,
797799
/**/
798800
400,
799801
/**/

0 commit comments

Comments
 (0)