mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
vim-patch:8.2.1347: cannot easily get the script ID
Problem: Cannot easily get the script ID.
Solution: Support expand('<SID>').
909443028b
This commit is contained in:
parent
0680b5218e
commit
7a047d8dc2
@ -1078,6 +1078,10 @@ When executing an autocommand or a user command, it will run in the context of
|
||||
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 <SID> cannot be correctly
|
||||
expanded, use the expand() function: >
|
||||
let &includexpr = expand('<SID>') .. 'My_includeexpr()'
|
||||
|
||||
Otherwise, using "<SID>" outside of a script context is an error.
|
||||
|
||||
If you need to get the script number to use in a complicated script, you can
|
||||
|
@ -8617,14 +8617,20 @@ ssize_t find_cmdline_var(const char_u *src, size_t *usedlen)
|
||||
#define SPEC_SFILE (SPEC_CFILE + 1)
|
||||
"<slnum>", // ":so" file line number
|
||||
#define SPEC_SLNUM (SPEC_SFILE + 1)
|
||||
"<stack>", // call stack
|
||||
#define SPEC_STACK (SPEC_SLNUM + 1)
|
||||
"<afile>", // autocommand file name
|
||||
#define SPEC_AFILE (SPEC_SLNUM + 1)
|
||||
#define SPEC_AFILE (SPEC_STACK + 1)
|
||||
"<abuf>", // autocommand buffer number
|
||||
#define SPEC_ABUF (SPEC_AFILE + 1)
|
||||
"<amatch>", // autocommand match name
|
||||
#define SPEC_AMATCH (SPEC_ABUF + 1)
|
||||
"<sflnum>", // script file line number
|
||||
#define SPEC_SFLNUM (SPEC_AMATCH + 1)
|
||||
"<SID>", // script ID: <SNR>123_
|
||||
#define SPEC_SID (SPEC_SFLNUM + 1)
|
||||
"<client>"
|
||||
#define SPEC_CLIENT (SPEC_SID + 1)
|
||||
};
|
||||
|
||||
for (size_t i = 0; i < ARRAY_SIZE(spec_str); ++i) {
|
||||
@ -8871,6 +8877,16 @@ eval_vars (
|
||||
result = (char_u *)strbuf;
|
||||
break;
|
||||
|
||||
case SPEC_SID:
|
||||
if (current_sctx.sc_sid <= 0) {
|
||||
*errormsg = (char_u *)_(e_usingsid);
|
||||
return NULL;
|
||||
}
|
||||
snprintf(strbuf, sizeof(strbuf), "<SNR>%" PRIdSCID "_",
|
||||
current_sctx.sc_sid);
|
||||
result = (char_u *)strbuf;
|
||||
break;
|
||||
|
||||
default:
|
||||
// should not happen
|
||||
*errormsg = (char_u *)"";
|
||||
|
@ -1,5 +1,7 @@
|
||||
" Tests for expand()
|
||||
|
||||
source shared.vim
|
||||
|
||||
let s:sfile = expand('<sfile>')
|
||||
let s:slnum = str2nr(expand('<slnum>'))
|
||||
let s:sflnum = str2nr(expand('<sflnum>'))
|
||||
@ -16,6 +18,25 @@ func s:expand_sflnum()
|
||||
return str2nr(expand('<sflnum>'))
|
||||
endfunc
|
||||
|
||||
" This test depends on the location in the test file, put it first.
|
||||
func Test_expand_sflnum()
|
||||
call assert_equal(7, s:sflnum)
|
||||
call assert_equal(24, str2nr(expand('<sflnum>')))
|
||||
|
||||
" Line-continuation
|
||||
call assert_equal(
|
||||
\ 27,
|
||||
\ str2nr(expand('<sflnum>')))
|
||||
|
||||
" Call in script-local function
|
||||
call assert_equal(18, s:expand_sflnum())
|
||||
|
||||
" Call in command
|
||||
command Flnum echo expand('<sflnum>')
|
||||
call assert_equal(36, str2nr(trim(execute('Flnum'))))
|
||||
delcommand Flnum
|
||||
endfunc
|
||||
|
||||
func Test_expand_sfile()
|
||||
call assert_match('test_expand_func\.vim$', s:sfile)
|
||||
call assert_match('^function .*\.\.Test_expand_sfile$', expand('<sfile>'))
|
||||
@ -30,7 +51,7 @@ func Test_expand_sfile()
|
||||
endfunc
|
||||
|
||||
func Test_expand_slnum()
|
||||
call assert_equal(4, s:slnum)
|
||||
call assert_equal(6, s:slnum)
|
||||
call assert_equal(2, str2nr(expand('<slnum>')))
|
||||
|
||||
" Line-continuation
|
||||
@ -47,20 +68,14 @@ func Test_expand_slnum()
|
||||
delcommand Slnum
|
||||
endfunc
|
||||
|
||||
func Test_expand_sflnum()
|
||||
call assert_equal(5, s:sflnum)
|
||||
call assert_equal(52, str2nr(expand('<sflnum>')))
|
||||
|
||||
" Line-continuation
|
||||
call assert_equal(
|
||||
\ 55,
|
||||
\ str2nr(expand('<sflnum>')))
|
||||
|
||||
" Call in script-local function
|
||||
call assert_equal(16, s:expand_sflnum())
|
||||
|
||||
" Call in command
|
||||
command Flnum echo expand('<sflnum>')
|
||||
call assert_equal(64, str2nr(trim(execute('Flnum'))))
|
||||
delcommand Flnum
|
||||
func s:sid_test()
|
||||
return 'works'
|
||||
endfunc
|
||||
|
||||
func Test_expand_SID()
|
||||
let sid = expand('<SID>')
|
||||
execute 'let g:sid_result = ' .. sid .. 'sid_test()'
|
||||
call assert_equal('works', g:sid_result)
|
||||
endfunc
|
||||
|
||||
" vim: shiftwidth=2 sts=2 expandtab
|
||||
|
Loading…
Reference in New Issue
Block a user