]> granicus.if.org Git - vim/commitdiff
patch 9.0.0244: cannot easily get the list of sourced scripts v9.0.0244
authorYegappan Lakshmanan <yegappan@yahoo.com>
Mon, 22 Aug 2022 12:15:13 +0000 (13:15 +0100)
committerBram Moolenaar <Bram@vim.org>
Mon, 22 Aug 2022 12:15:13 +0000 (13:15 +0100)
Problem:    Cannot easily get the list of sourced scripts.
Solution:   Add the getscriptinfo() function. (Yegappan Lakshmanan,
            closes #10957)

runtime/doc/builtin.txt
runtime/doc/usr_41.txt
src/evalfunc.c
src/proto/scriptfile.pro
src/scriptfile.c
src/testdir/test_scriptnames.vim
src/testdir/test_vim9_import.vim
src/version.c

index 7ddc4a3ee5a8517ac9a57496d94bcff350761cc1..00ae665f8b35dfcfb8c903df869dccddae8a65ae 100644 (file)
@@ -253,6 +253,7 @@ getreg([{regname} [, 1 [, {list}]]])
                                String or List   contents of a register
 getreginfo([{regname}])                Dict    information about a register
 getregtype([{regname}])                String  type of a register
+getscriptinfo()                List    list of sourced scripts
 gettabinfo([{expr}])           List    list of tab pages
 gettabvar({nr}, {varname} [, {def}])
                                any     variable {varname} in tab {nr} or {def}
@@ -4088,6 +4089,18 @@ getregtype([{regname}])                                  *getregtype()*
                Can also be used as a |method|: >
                        GetRegname()->getregtype()
 
+getscriptinfo()                                        *getscriptinfo()*
+               Returns a |List| with information about all the sourced Vim
+               scripts in the order they were sourced. (|:scriptinfo|)
+
+               Each item in the returned List is a |Dict| with the following
+               items:
+                   autoload    set to TRUE for a script that was used with
+                               |import autoload| but was not actually sourced
+                               yet.
+                   name        vim script file name.
+                   sid         script ID |<SID>|.
+
 gettabinfo([{tabnr}])                                  *gettabinfo()*
                If {tabnr} is not specified, then information about all the
                tab pages is returned as a |List|. Each List item is a
index 1938e3546075504ff588185660484ba3f080f361..4e210a65ae8e4345af8951df4407623a335579b9 100644 (file)
@@ -1310,6 +1310,14 @@ Prompt Buffer:                                   *promptbuffer-functions*
        prompt_setinterrupt()   set interrupt callback for a buffer
        prompt_setprompt()      set the prompt text for a buffer
 
+Registers:                                     *register-functions*
+       getreg()                get contents of a register
+       getreginfo()            get information about a register
+       getregtype()            get type of a register
+       setreg()                set contents and type of a register
+       reg_executing()         return the name of the register being executed
+       reg_recording()         return the name of the register being recorded
+
 Text Properties:                               *text-property-functions*
        prop_add()              attach a property at a position
        prop_add_list()         attach a property at multiple positions
@@ -1341,6 +1349,7 @@ Various:                                  *various-functions*
        did_filetype()          check if a FileType autocommand was used
        eventhandler()          check if invoked by an event handler
        getpid()                get process ID of Vim
+       getscriptinfo() get list of sourced vim scripts
        getimstatus()           check if IME status is active
        interrupt()             interrupt script execution
        windowsversion()        get MS-Windows version
@@ -1352,13 +1361,6 @@ Various:                                 *various-functions*
        undofile()              get the name of the undo file
        undotree()              return the state of the undo tree
 
-       getreg()                get contents of a register
-       getreginfo()            get information about a register
-       getregtype()            get type of a register
-       setreg()                set contents and type of a register
-       reg_executing()         return the name of the register being executed
-       reg_recording()         return the name of the register being recorded
-
        shiftwidth()            effective value of 'shiftwidth'
 
        wordcount()             get byte/word/char count of buffer
index a5c8a7d23b24414f61785b2a6b22d06d9344ac0d..c199bdbe02989a48b188cd7eab302dff76391830 100644 (file)
@@ -1935,6 +1935,8 @@ static funcentry_T global_functions[] =
                        ret_dict_any,       f_getreginfo},
     {"getregtype",     0, 1, FEARG_1,      arg1_string,
                        ret_string,         f_getregtype},
+    {"getscriptinfo",  0, 0, 0,            NULL,
+                       ret_list_dict_any,  f_getscriptinfo},
     {"gettabinfo",     0, 1, FEARG_1,      arg1_number,
                        ret_list_dict_any,  f_gettabinfo},
     {"gettabvar",      2, 3, FEARG_1,      arg3_number_string_any,
index 205028d127e39cd731d10a4967a40e51608a60f7..c80ed8cc046a3d302b4532f3df8e46ee633d6261 100644 (file)
@@ -33,6 +33,7 @@ char_u *get_scriptname(scid_T id);
 void free_scriptnames(void);
 void free_autoload_scriptnames(void);
 linenr_T get_sourced_lnum(char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
+void f_getscriptinfo(typval_T *argvars, typval_T *rettv);
 char_u *getsourceline(int c, void *cookie, int indent, getline_opt_T options);
 int sourcing_a_script(exarg_T *eap);
 void ex_scriptencoding(exarg_T *eap);
index 76c78e75488b9e49997b3c294317ef63ca235b34..aae3be20abd57f46402a6ae5547a175ad38c3d1e 100644 (file)
@@ -1933,6 +1933,36 @@ get_sourced_lnum(
                        ? ((source_cookie_T *)cookie)->sourcing_lnum
                        : SOURCING_LNUM;
 }
+
+    void
+f_getscriptinfo(typval_T *argvars UNUSED, typval_T *rettv)
+{
+    int                i;
+    list_T     *l;
+
+    if (rettv_list_alloc(rettv) == FAIL)
+       return;
+
+    l = rettv->vval.v_list;
+
+    for (i = 1; i <= script_items.ga_len; ++i)
+    {
+       scriptitem_T    *si = SCRIPT_ITEM(i);
+       dict_T          *d;
+
+       if (si->sn_name == NULL)
+           continue;
+
+       if ((d = dict_alloc()) == NULL
+               || list_append_dict(l, d) == FAIL
+               || dict_add_string(d, "name", si->sn_name) == FAIL
+               || dict_add_number(d, "sid", i) == FAIL
+               || dict_add_bool(d, "autoload",
+                               si->sn_state == SN_STATE_NOT_LOADED) == FAIL)
+           return;
+    }
+}
+
 #endif
 
     static char_u *
index 44ec146666772b643397f3ccfafdf2afbc0cb271..06ae305ab7bf53418666248f8f106f85b1a7d638 100644 (file)
@@ -1,5 +1,5 @@
-" Test for :scriptnames
 
+" Test for the :scriptnames command
 func Test_scriptnames()
   call writefile(['let did_load_script = 123'], 'Xscripting')
   source Xscripting
@@ -29,4 +29,14 @@ func Test_scriptnames()
   call assert_equal(msgs, execute('messages'))
 endfunc
 
+" Test for the getscriptinfo() function
+func Test_getscriptinfo()
+  call writefile(['let loaded_script_id = expand("<SID>")'], 'Xscript')
+  source Xscript
+  let l = getscriptinfo()
+  call assert_match('Xscript$', l[-1].name)
+  call assert_equal(g:loaded_script_id, $"<SNR>{l[-1].sid}_")
+  call delete('Xscript')
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab
index f28b4adef30b0572220dc5adcf1d1f54d811e311..4cc53b2c26d7040a0f4eba1764996dca5194a357 100644 (file)
@@ -732,6 +732,8 @@ def Test_use_relative_autoload_import_in_mapping()
 
   source Xmapscript.vim
   assert_match('\d\+ A: .*XrelautoloadExport.vim', execute('scriptnames')->split("\n")[-1])
+  assert_match('XrelautoloadExport.vim$', getscriptinfo()[-1].name)
+  assert_true(getscriptinfo()[-1].autoload)
   feedkeys("\<F3>", "xt")
   assert_equal(42, g:result)
 
index a5392e63936959bc83b598c8f4e8194f06f4b44b..e2882b67a38b51d5a78cddd6472ce765886c11e6 100644 (file)
@@ -731,6 +731,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    244,
 /**/
     243,
 /**/