Skip to content

Commit 9094430

Browse files
committed
patch 8.2.1347: cannot easily get the script ID
Problem: Cannot easily get the script ID. Solution: Support expand('<SID>').
1 parent 491799b commit 9094430

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

runtime/doc/map.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,10 @@ When executing an autocommand or a user command, it will run in the context of
11671167
the script it was defined in. This makes it possible that the command calls a
11681168
local function or uses a local mapping.
11691169

1170+
In case the value is used in a context where <SID> cannot be correctly
1171+
expanded, use the expand() function: >
1172+
let &includexpr = expand('<SID>') .. 'My_includeexpr()'
1173+
11701174
Otherwise, using "<SID>" outside of a script context is an error.
11711175

11721176
If you need to get the script number to use in a complicated script, you can

src/ex_docmd.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8302,9 +8302,11 @@ find_cmdline_var(char_u *src, int *usedlen)
83028302
#define SPEC_AMATCH (SPEC_ABUF + 1)
83038303
"<sflnum>", // script file line number
83048304
#define SPEC_SFLNUM (SPEC_AMATCH + 1)
8305+
"<SID>", // script ID: <SNR>123_
8306+
#define SPEC_SID (SPEC_SFLNUM + 1)
83058307
#ifdef FEAT_CLIENTSERVER
83068308
"<client>"
8307-
# define SPEC_CLIENT (SPEC_SFLNUM + 1)
8309+
# define SPEC_CLIENT (SPEC_SID + 1)
83088310
#endif
83098311
};
83108312

@@ -8581,6 +8583,16 @@ eval_vars(
85818583
break;
85828584
#endif
85838585

8586+
case SPEC_SID:
8587+
if (current_sctx.sc_sid <= 0)
8588+
{
8589+
*errormsg = _(e_usingsid);
8590+
return NULL;
8591+
}
8592+
sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8593+
result = strbuf;
8594+
break;
8595+
85848596
#ifdef FEAT_CLIENTSERVER
85858597
case SPEC_CLIENT: // Source of last submitted input
85868598
sprintf((char *)strbuf, PRINTF_HEX_LONG_U,

src/testdir/test_expand_func.vim

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
" Tests for expand()
22

3+
source shared.vim
4+
35
let s:sfile = expand('<sfile>')
46
let s:slnum = str2nr(expand('<slnum>'))
57
let s:sflnum = str2nr(expand('<sflnum>'))
@@ -18,20 +20,20 @@ endfunc
1820

1921
" This test depends on the location in the test file, put it first.
2022
func Test_expand_sflnum()
21-
call assert_equal(5, s:sflnum)
22-
call assert_equal(22, str2nr(expand('<sflnum>')))
23+
call assert_equal(7, s:sflnum)
24+
call assert_equal(24, str2nr(expand('<sflnum>')))
2325

2426
" Line-continuation
2527
call assert_equal(
26-
\ 25,
28+
\ 27,
2729
\ str2nr(expand('<sflnum>')))
2830

2931
" Call in script-local function
30-
call assert_equal(16, s:expand_sflnum())
32+
call assert_equal(18, s:expand_sflnum())
3133

3234
" Call in command
3335
command Flnum echo expand('<sflnum>')
34-
call assert_equal(34, str2nr(trim(execute('Flnum'))))
36+
call assert_equal(36, str2nr(trim(execute('Flnum'))))
3537
delcommand Flnum
3638
endfunc
3739

@@ -60,7 +62,7 @@ func Test_expand_sfile_and_stack()
6062
endfunc
6163

6264
func Test_expand_slnum()
63-
call assert_equal(4, s:slnum)
65+
call assert_equal(6, s:slnum)
6466
call assert_equal(2, str2nr(expand('<slnum>')))
6567

6668
" Line-continuation
@@ -86,6 +88,17 @@ func Test_expand()
8688
quit
8789
endfunc
8890

91+
func s:sid_test()
92+
return 'works'
93+
endfunc
94+
95+
func Test_expand_SID()
96+
let sid = expand('<SID>')
97+
execute 'let g:sid_result = ' .. sid .. 'sid_test()'
98+
call assert_equal('works', g:sid_result)
99+
endfunc
100+
101+
89102
" Test for 'wildignore' with expand()
90103
func Test_expand_wildignore()
91104
set wildignore=*.vim

src/version.c

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

755755
static int included_patches[] =
756756
{ /* Add new patch number below this line */
757+
/**/
758+
1347,
757759
/**/
758760
1346,
759761
/**/

0 commit comments

Comments
 (0)