# HG changeset patch # User Bram Moolenaar # Date 1596308404 -7200 # Node ID 5daca8504c633bdc2e1a3138ddf99c792f8ea0bf # Parent 6015f5f423ba25cac6dd2aaa77d7121b0cac0796 patch 8.2.1347: cannot easily get the script ID Commit: https://github.com/vim/vim/commit/909443028b57d7514ce3c71f00e9d808f2126b4f Author: Bram Moolenaar Date: Sat Aug 1 20:45:11 2020 +0200 patch 8.2.1347: cannot easily get the script ID Problem: Cannot easily get the script ID. Solution: Support expand(''). diff --git a/runtime/doc/map.txt b/runtime/doc/map.txt --- a/runtime/doc/map.txt +++ b/runtime/doc/map.txt @@ -1167,6 +1167,10 @@ When executing an autocommand or a user the script it was defined in. This makes it possible that the command calls a local function or uses a local mapping. +In case the value is used in a context where cannot be correctly +expanded, use the expand() function: > + let &includexpr = expand('') .. 'My_includeexpr()' + Otherwise, using "" outside of a script context is an error. If you need to get the script number to use in a complicated script, you can diff --git a/src/ex_docmd.c b/src/ex_docmd.c --- a/src/ex_docmd.c +++ b/src/ex_docmd.c @@ -8302,9 +8302,11 @@ find_cmdline_var(char_u *src, int *usedl #define SPEC_AMATCH (SPEC_ABUF + 1) "", // script file line number #define SPEC_SFLNUM (SPEC_AMATCH + 1) + "", // script ID: 123_ +#define SPEC_SID (SPEC_SFLNUM + 1) #ifdef FEAT_CLIENTSERVER "" -# define SPEC_CLIENT (SPEC_SFLNUM + 1) +# define SPEC_CLIENT (SPEC_SID + 1) #endif }; @@ -8581,6 +8583,16 @@ eval_vars( break; #endif + case SPEC_SID: + if (current_sctx.sc_sid <= 0) + { + *errormsg = _(e_usingsid); + return NULL; + } + sprintf((char *)strbuf, "%d_", current_sctx.sc_sid); + result = strbuf; + break; + #ifdef FEAT_CLIENTSERVER case SPEC_CLIENT: // Source of last submitted input sprintf((char *)strbuf, PRINTF_HEX_LONG_U, diff --git a/src/testdir/test_expand_func.vim b/src/testdir/test_expand_func.vim --- a/src/testdir/test_expand_func.vim +++ b/src/testdir/test_expand_func.vim @@ -1,5 +1,7 @@ " Tests for expand() +source shared.vim + let s:sfile = expand('') let s:slnum = str2nr(expand('')) let s:sflnum = str2nr(expand('')) @@ -18,20 +20,20 @@ endfunc " This test depends on the location in the test file, put it first. func Test_expand_sflnum() - call assert_equal(5, s:sflnum) - call assert_equal(22, str2nr(expand(''))) + call assert_equal(7, s:sflnum) + call assert_equal(24, str2nr(expand(''))) " Line-continuation call assert_equal( - \ 25, + \ 27, \ str2nr(expand(''))) " Call in script-local function - call assert_equal(16, s:expand_sflnum()) + call assert_equal(18, s:expand_sflnum()) " Call in command command Flnum echo expand('') - call assert_equal(34, str2nr(trim(execute('Flnum')))) + call assert_equal(36, str2nr(trim(execute('Flnum')))) delcommand Flnum endfunc @@ -60,7 +62,7 @@ func Test_expand_sfile_and_stack() endfunc func Test_expand_slnum() - call assert_equal(4, s:slnum) + call assert_equal(6, s:slnum) call assert_equal(2, str2nr(expand(''))) " Line-continuation @@ -86,6 +88,17 @@ func Test_expand() quit endfunc +func s:sid_test() + return 'works' +endfunc + +func Test_expand_SID() + let sid = expand('') + execute 'let g:sid_result = ' .. sid .. 'sid_test()' + call assert_equal('works', g:sid_result) +endfunc + + " Test for 'wildignore' with expand() func Test_expand_wildignore() set wildignore=*.vim diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -755,6 +755,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 1347, +/**/ 1346, /**/ 1345,