]> granicus.if.org Git - vim/commitdiff
patch 8.2.1347: cannot easily get the script ID v8.2.1347
authorBram Moolenaar <Bram@vim.org>
Sat, 1 Aug 2020 18:45:11 +0000 (20:45 +0200)
committerBram Moolenaar <Bram@vim.org>
Sat, 1 Aug 2020 18:45:11 +0000 (20:45 +0200)
Problem:    Cannot easily get the script ID.
Solution:   Support expand('<SID>').

runtime/doc/map.txt
src/ex_docmd.c
src/testdir/test_expand_func.vim
src/version.c

index 0ad43d449b7820b35b546dc6b947ca61a91197cf..79c46f712534baf952b8c3bb7ebbb81e7b0bd310 100644 (file)
@@ -1167,6 +1167,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
index 7f9d716354fdbace3c504d57e236807607159cbf..1f36bf7e054c6c2cd4866b0bd0aefdef307c9c29 100644 (file)
@@ -8302,9 +8302,11 @@ find_cmdline_var(char_u *src, int *usedlen)
 #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)
 #ifdef FEAT_CLIENTSERVER
                    "<client>"
-# 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, "<SNR>%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,
index 41673e50afd9858ec035877b8001ad5c80af6f7d..42aa8e01bcd540e5a2b48cee3e9293a16994f595 100644 (file)
@@ -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>'))
@@ -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('<sflnum>')))
+  call assert_equal(7, s:sflnum)
+  call assert_equal(24, str2nr(expand('<sflnum>')))
 
   " Line-continuation
   call assert_equal(
-        \ 25,
+        \ 27,
         \ str2nr(expand('<sflnum>')))
 
   " 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('<sflnum>')
-  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('<slnum>')))
 
   " 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('<SID>')
+  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
index cdb26a25854d07140fb25fcd60eb1da09c67a30f..6b1a3677378ec2ded8719c3a5b22dc649bba25ba 100644 (file)
@@ -754,6 +754,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1347,
 /**/
     1346,
 /**/