aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Edmund Lazo <jan.lazo@mail.utoronto.ca>2020-08-02 22:38:15 -0400
committerJan Edmund Lazo <jan.lazo@mail.utoronto.ca>2020-08-14 18:20:28 -0400
commit7a047d8dc2dff4078649290696b3e507aaaf8f04 (patch)
treefdbbbe2e0cf31a413c585e5553bd2c3f002cf3e0
parent0680b5218e29f2ebb947d61210d8775ff720f8bb (diff)
downloadrneovim-7a047d8dc2dff4078649290696b3e507aaaf8f04.tar.gz
rneovim-7a047d8dc2dff4078649290696b3e507aaaf8f04.tar.bz2
rneovim-7a047d8dc2dff4078649290696b3e507aaaf8f04.zip
vim-patch:8.2.1347: cannot easily get the script ID
Problem: Cannot easily get the script ID. Solution: Support expand('<SID>'). https://github.com/vim/vim/commit/909443028b57d7514ce3c71f00e9d808f2126b4f
-rw-r--r--runtime/doc/map.txt4
-rw-r--r--src/nvim/ex_docmd.c18
-rw-r--r--src/nvim/testdir/test_expand_func.vim47
3 files changed, 52 insertions, 17 deletions
diff --git a/runtime/doc/map.txt b/runtime/doc/map.txt
index a2b7aca4fd..1514f03c55 100644
--- a/runtime/doc/map.txt
+++ b/runtime/doc/map.txt
@@ -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
diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c
index 608b7dc94c..66ad47612c 100644
--- a/src/nvim/ex_docmd.c
+++ b/src/nvim/ex_docmd.c
@@ -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 *)"";
diff --git a/src/nvim/testdir/test_expand_func.vim b/src/nvim/testdir/test_expand_func.vim
index fb29c3eb7a..9588d3b89d 100644
--- a/src/nvim/testdir/test_expand_func.vim
+++ b/src/nvim/testdir/test_expand_func.vim
@@ -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())
+func s:sid_test()
+ return 'works'
+endfunc
- " Call in command
- command Flnum echo expand('<sflnum>')
- call assert_equal(64, str2nr(trim(execute('Flnum'))))
- delcommand Flnum
+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