Skip to content

Commit f768c3d

Browse files
yegappanbrammool
authored andcommitted
patch 9.0.0244: cannot easily get the list of sourced scripts
Problem: Cannot easily get the list of sourced scripts. Solution: Add the getscriptinfo() function. (Yegappan Lakshmanan, closes #10957)
1 parent e89aeed commit f768c3d

File tree

8 files changed

+70
-8
lines changed

8 files changed

+70
-8
lines changed

runtime/doc/builtin.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ getreg([{regname} [, 1 [, {list}]]])
253253
String or List contents of a register
254254
getreginfo([{regname}]) Dict information about a register
255255
getregtype([{regname}]) String type of a register
256+
getscriptinfo() List list of sourced scripts
256257
gettabinfo([{expr}]) List list of tab pages
257258
gettabvar({nr}, {varname} [, {def}])
258259
any variable {varname} in tab {nr} or {def}
@@ -4088,6 +4089,18 @@ getregtype([{regname}]) *getregtype()*
40884089
Can also be used as a |method|: >
40894090
GetRegname()->getregtype()
40904091
4092+
getscriptinfo() *getscriptinfo()*
4093+
Returns a |List| with information about all the sourced Vim
4094+
scripts in the order they were sourced. (|:scriptinfo|)
4095+
4096+
Each item in the returned List is a |Dict| with the following
4097+
items:
4098+
autoload set to TRUE for a script that was used with
4099+
|import autoload| but was not actually sourced
4100+
yet.
4101+
name vim script file name.
4102+
sid script ID |<SID>|.
4103+
40914104
gettabinfo([{tabnr}]) *gettabinfo()*
40924105
If {tabnr} is not specified, then information about all the
40934106
tab pages is returned as a |List|. Each List item is a

runtime/doc/usr_41.txt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,14 @@ Prompt Buffer: *promptbuffer-functions*
13101310
prompt_setinterrupt() set interrupt callback for a buffer
13111311
prompt_setprompt() set the prompt text for a buffer
13121312

1313+
Registers: *register-functions*
1314+
getreg() get contents of a register
1315+
getreginfo() get information about a register
1316+
getregtype() get type of a register
1317+
setreg() set contents and type of a register
1318+
reg_executing() return the name of the register being executed
1319+
reg_recording() return the name of the register being recorded
1320+
13131321
Text Properties: *text-property-functions*
13141322
prop_add() attach a property at a position
13151323
prop_add_list() attach a property at multiple positions
@@ -1341,6 +1349,7 @@ Various: *various-functions*
13411349
did_filetype() check if a FileType autocommand was used
13421350
eventhandler() check if invoked by an event handler
13431351
getpid() get process ID of Vim
1352+
getscriptinfo() get list of sourced vim scripts
13441353
getimstatus() check if IME status is active
13451354
interrupt() interrupt script execution
13461355
windowsversion() get MS-Windows version
@@ -1352,13 +1361,6 @@ Various: *various-functions*
13521361
undofile() get the name of the undo file
13531362
undotree() return the state of the undo tree
13541363

1355-
getreg() get contents of a register
1356-
getreginfo() get information about a register
1357-
getregtype() get type of a register
1358-
setreg() set contents and type of a register
1359-
reg_executing() return the name of the register being executed
1360-
reg_recording() return the name of the register being recorded
1361-
13621364
shiftwidth() effective value of 'shiftwidth'
13631365

13641366
wordcount() get byte/word/char count of buffer

src/evalfunc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,8 @@ static funcentry_T global_functions[] =
19351935
ret_dict_any, f_getreginfo},
19361936
{"getregtype", 0, 1, FEARG_1, arg1_string,
19371937
ret_string, f_getregtype},
1938+
{"getscriptinfo", 0, 0, 0, NULL,
1939+
ret_list_dict_any, f_getscriptinfo},
19381940
{"gettabinfo", 0, 1, FEARG_1, arg1_number,
19391941
ret_list_dict_any, f_gettabinfo},
19401942
{"gettabvar", 2, 3, FEARG_1, arg3_number_string_any,

src/proto/scriptfile.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ char_u *get_scriptname(scid_T id);
3333
void free_scriptnames(void);
3434
void free_autoload_scriptnames(void);
3535
linenr_T get_sourced_lnum(char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
36+
void f_getscriptinfo(typval_T *argvars, typval_T *rettv);
3637
char_u *getsourceline(int c, void *cookie, int indent, getline_opt_T options);
3738
int sourcing_a_script(exarg_T *eap);
3839
void ex_scriptencoding(exarg_T *eap);

src/scriptfile.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,36 @@ get_sourced_lnum(
19331933
? ((source_cookie_T *)cookie)->sourcing_lnum
19341934
: SOURCING_LNUM;
19351935
}
1936+
1937+
void
1938+
f_getscriptinfo(typval_T *argvars UNUSED, typval_T *rettv)
1939+
{
1940+
int i;
1941+
list_T *l;
1942+
1943+
if (rettv_list_alloc(rettv) == FAIL)
1944+
return;
1945+
1946+
l = rettv->vval.v_list;
1947+
1948+
for (i = 1; i <= script_items.ga_len; ++i)
1949+
{
1950+
scriptitem_T *si = SCRIPT_ITEM(i);
1951+
dict_T *d;
1952+
1953+
if (si->sn_name == NULL)
1954+
continue;
1955+
1956+
if ((d = dict_alloc()) == NULL
1957+
|| list_append_dict(l, d) == FAIL
1958+
|| dict_add_string(d, "name", si->sn_name) == FAIL
1959+
|| dict_add_number(d, "sid", i) == FAIL
1960+
|| dict_add_bool(d, "autoload",
1961+
si->sn_state == SN_STATE_NOT_LOADED) == FAIL)
1962+
return;
1963+
}
1964+
}
1965+
19361966
#endif
19371967

19381968
static char_u *

src/testdir/test_scriptnames.vim

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
" Test for :scriptnames
21

2+
" Test for the :scriptnames command
33
func Test_scriptnames()
44
call writefile(['let did_load_script = 123'], 'Xscripting')
55
source Xscripting
@@ -29,4 +29,14 @@ func Test_scriptnames()
2929
call assert_equal(msgs, execute('messages'))
3030
endfunc
3131

32+
" Test for the getscriptinfo() function
33+
func Test_getscriptinfo()
34+
call writefile(['let loaded_script_id = expand("<SID>")'], 'Xscript')
35+
source Xscript
36+
let l = getscriptinfo()
37+
call assert_match('Xscript$', l[-1].name)
38+
call assert_equal(g:loaded_script_id, $"<SNR>{l[-1].sid}_")
39+
call delete('Xscript')
40+
endfunc
41+
3242
" vim: shiftwidth=2 sts=2 expandtab

src/testdir/test_vim9_import.vim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,8 @@ def Test_use_relative_autoload_import_in_mapping()
732732

733733
source Xmapscript.vim
734734
assert_match('\d\+ A: .*XrelautoloadExport.vim', execute('scriptnames')->split("\n")[-1])
735+
assert_match('XrelautoloadExport.vim$', getscriptinfo()[-1].name)
736+
assert_true(getscriptinfo()[-1].autoload)
735737
feedkeys("\<F3>", "xt")
736738
assert_equal(42, g:result)
737739

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,8 @@ static char *(features[]) =
731731

732732
static int included_patches[] =
733733
{ /* Add new patch number below this line */
734+
/**/
735+
244,
734736
/**/
735737
243,
736738
/**/

0 commit comments

Comments
 (0)