blob: aa563d90807cbd9335289bb0278fb5c9a82960c4 [file] [log] [blame]
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * scriptfile.c: functions for dealing with the runtime directories/files
12 */
13
14#include "vim.h"
15
Bram Moolenaarda6c0332019-09-01 16:01:30 +020016#if defined(FEAT_EVAL) || defined(PROTO)
17// The names of packages that once were loaded are remembered.
18static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
19#endif
20
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +000021// last used sequence number for sourcing scripts (current_sctx.sc_seq)
22#ifdef FEAT_EVAL
23static int last_current_SID_seq = 0;
24#endif
25
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +000026static int do_source_ext(char_u *fname, int check_other, int is_vimrc, int *ret_sid, exarg_T *eap, int clearvars);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +000027
Bram Moolenaar307c5a52019-08-25 15:41:00 +020028/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010029 * Initialize the execution stack.
30 */
31 void
32estack_init(void)
33{
34 estack_T *entry;
35
36 if (ga_grow(&exestack, 10) == FAIL)
37 mch_exit(0);
38 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
39 entry->es_type = ETYPE_TOP;
40 entry->es_name = NULL;
41 entry->es_lnum = 0;
Bram Moolenaar09d46402019-12-29 23:53:01 +010042#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010043 entry->es_info.ufunc = NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +010044#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010045 ++exestack.ga_len;
46}
47
48/*
49 * Add an item to the execution stack.
50 * Returns the new entry or NULL when out of memory.
51 */
52 estack_T *
53estack_push(etype_T type, char_u *name, long lnum)
54{
55 estack_T *entry;
56
57 // If memory allocation fails then we'll pop more than we push, eventually
58 // at the top level it will be OK again.
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +000059 if (ga_grow(&exestack, 1) != OK)
60 return NULL;
61
62 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
63 entry->es_type = type;
64 entry->es_name = name;
65 entry->es_lnum = lnum;
Bram Moolenaar09d46402019-12-29 23:53:01 +010066#ifdef FEAT_EVAL
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +000067 entry->es_info.ufunc = NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +010068#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +000069 ++exestack.ga_len;
70 return entry;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010071}
72
Bram Moolenaar09d46402019-12-29 23:53:01 +010073#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010074/*
75 * Add a user function to the execution stack.
76 */
Bram Moolenaarc620c052020-07-08 15:16:19 +020077 estack_T *
Bram Moolenaar25e0f582020-05-25 22:36:50 +020078estack_push_ufunc(ufunc_T *ufunc, long lnum)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010079{
Bram Moolenaar25e0f582020-05-25 22:36:50 +020080 estack_T *entry = estack_push(ETYPE_UFUNC,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010081 ufunc->uf_name_exp != NULL
82 ? ufunc->uf_name_exp : ufunc->uf_name, lnum);
83 if (entry != NULL)
84 entry->es_info.ufunc = ufunc;
Bram Moolenaarc620c052020-07-08 15:16:19 +020085 return entry;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010086}
Bram Moolenaar25e0f582020-05-25 22:36:50 +020087
88/*
89 * Return TRUE if "ufunc" with "lnum" is already at the top of the exe stack.
90 */
91 int
92estack_top_is_ufunc(ufunc_T *ufunc, long lnum)
93{
94 estack_T *entry;
95
96 if (exestack.ga_len == 0)
97 return FALSE;
98 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
99 return entry->es_type == ETYPE_UFUNC
100 && STRCMP( entry->es_name, ufunc->uf_name_exp != NULL
101 ? ufunc->uf_name_exp : ufunc->uf_name) == 0
102 && entry->es_lnum == lnum;
103}
Bram Moolenaar09d46402019-12-29 23:53:01 +0100104#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100105
106/*
Bram Moolenaarc620c052020-07-08 15:16:19 +0200107 * Take an item off of the execution stack and return it.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100108 */
Bram Moolenaarc620c052020-07-08 15:16:19 +0200109 estack_T *
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100110estack_pop(void)
111{
Bram Moolenaarc620c052020-07-08 15:16:19 +0200112 if (exestack.ga_len == 0)
113 return NULL;
114 --exestack.ga_len;
115 return ((estack_T *)exestack.ga_data) + exestack.ga_len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100116}
117
118/*
Bram Moolenaar89445512022-04-14 12:58:23 +0100119 * Get the current value for "which" in allocated memory.
LemonBoy6013d002022-04-09 21:42:10 +0100120 * "which" is ESTACK_SFILE for <sfile>, ESTACK_STACK for <stack> or
121 * ESTACK_SCRIPT for <script>.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100122 */
123 char_u *
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200124estack_sfile(estack_arg_T which UNUSED)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100125{
Bram Moolenaar85b09572019-12-30 10:57:00 +0100126 estack_T *entry;
127#ifdef FEAT_EVAL
Bram Moolenaara5d04232020-07-26 15:37:02 +0200128 garray_T ga;
Bram Moolenaar4d7a2482020-01-06 19:53:43 +0100129 size_t len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100130 int idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200131 etype_T last_type = ETYPE_SCRIPT;
132 char *type_name;
Bram Moolenaar85b09572019-12-30 10:57:00 +0100133#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100134
135 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100136#ifdef FEAT_EVAL
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200137 if (which == ESTACK_SFILE && entry->es_type != ETYPE_UFUNC)
Bram Moolenaar09d46402019-12-29 23:53:01 +0100138#endif
Bram Moolenaara5d04232020-07-26 15:37:02 +0200139 {
140 if (entry->es_name == NULL)
141 return NULL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100142 return vim_strsave(entry->es_name);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200143 }
Bram Moolenaar09d46402019-12-29 23:53:01 +0100144#ifdef FEAT_EVAL
Bram Moolenaarc4492712021-11-22 15:37:15 +0000145 // expand('<sfile>') works in a function for backwards compatibility, but
146 // may give an unexpected result. Disallow it in Vim 9 script.
147 if (which == ESTACK_SFILE && in_vim9script())
148 {
149 int save_emsg_off = emsg_off;
150
151 if (emsg_off == 1)
152 // f_expand() silences errors but we do want this one
153 emsg_off = 0;
154 emsg(_(e_cannot_expand_sfile_in_vim9_function));
155 emsg_off = save_emsg_off;
156 return NULL;
157 }
158
LemonBoyeca7c602022-04-14 15:39:43 +0100159 // If evaluated in a function or autocommand, return the path of the script
160 // where it is defined, at script level the current script path is returned
LemonBoy6013d002022-04-09 21:42:10 +0100161 // instead.
162 if (which == ESTACK_SCRIPT)
163 {
LemonBoyeca7c602022-04-14 15:39:43 +0100164 // Walk the stack backwards, starting from the current frame.
165 for (idx = exestack.ga_len - 1; idx >= 0; --idx, --entry)
LemonBoy6013d002022-04-09 21:42:10 +0100166 {
zeertzjqa2471422022-08-23 19:26:05 +0100167 if (entry->es_type == ETYPE_UFUNC || entry->es_type == ETYPE_AUCMD)
LemonBoy6013d002022-04-09 21:42:10 +0100168 {
zeertzjqa2471422022-08-23 19:26:05 +0100169 sctx_T *def_ctx = entry->es_type == ETYPE_UFUNC
170 ? &entry->es_info.ufunc->uf_script_ctx
171 : acp_script_ctx(entry->es_info.aucmd);
LemonBoy6013d002022-04-09 21:42:10 +0100172
zeertzjqa2471422022-08-23 19:26:05 +0100173 return def_ctx->sc_sid > 0
174 ? vim_strsave(SCRIPT_ITEM(def_ctx->sc_sid)->sn_name)
175 : NULL;
LemonBoyeca7c602022-04-14 15:39:43 +0100176 }
177 else if (entry->es_type == ETYPE_SCRIPT)
LemonBoyeca7c602022-04-14 15:39:43 +0100178 return vim_strsave(entry->es_name);
LemonBoy6013d002022-04-09 21:42:10 +0100179 }
180 return NULL;
181 }
182
Bram Moolenaara5d04232020-07-26 15:37:02 +0200183 // Give information about each stack entry up to the root.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100184 // For a function we compose the call stack, as it was done in the past:
185 // "function One[123]..Two[456]..Three"
Bram Moolenaara5d04232020-07-26 15:37:02 +0200186 ga_init2(&ga, sizeof(char), 100);
187 for (idx = 0; idx < exestack.ga_len; ++idx)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100188 {
189 entry = ((estack_T *)exestack.ga_data) + idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200190 if (entry->es_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100191 {
Bram Moolenaara810db32020-09-11 17:59:23 +0200192 long lnum = 0;
193 char *dots;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200194
Bram Moolenaara5d04232020-07-26 15:37:02 +0200195 len = STRLEN(entry->es_name) + 15;
196 type_name = "";
197 if (entry->es_type != last_type)
198 {
199 switch (entry->es_type)
200 {
201 case ETYPE_SCRIPT: type_name = "script "; break;
202 case ETYPE_UFUNC: type_name = "function "; break;
203 default: type_name = ""; break;
204 }
205 last_type = entry->es_type;
206 }
207 len += STRLEN(type_name);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200208 if (ga_grow(&ga, (int)len) == FAIL)
Bram Moolenaara5d04232020-07-26 15:37:02 +0200209 break;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200210 if (idx == exestack.ga_len - 1)
211 lnum = which == ESTACK_STACK ? SOURCING_LNUM : 0;
212 else
213 lnum = entry->es_lnum;
Bram Moolenaara810db32020-09-11 17:59:23 +0200214 dots = idx == exestack.ga_len - 1 ? "" : "..";
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200215 if (lnum == 0)
216 // For the bottom entry of <sfile>: do not add the line number,
217 // it is used in <slnum>. Also leave it out when the number is
218 // not set.
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200219 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s%s",
Bram Moolenaara810db32020-09-11 17:59:23 +0200220 type_name, entry->es_name, dots);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200221 else
Bram Moolenaara810db32020-09-11 17:59:23 +0200222 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s[%ld]%s",
223 type_name, entry->es_name, lnum, dots);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200224 ga.ga_len += (int)STRLEN((char *)ga.ga_data + ga.ga_len);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100225 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100226 }
227
Bram Moolenaara5d04232020-07-26 15:37:02 +0200228 return (char_u *)ga.ga_data;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100229#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100230}
231
232/*
zeertzjq3770f4c2023-01-22 18:38:51 +0000233 * Get DIP_ flags from the [what] argument of a :runtime command.
234 * "*argp" is advanced to after the [what] argument.
235 */
236 static int
237get_runtime_cmd_flags(char_u **argp)
238{
239 char_u *arg = *argp;
240 char_u *p = skiptowhite(arg);
241 int what_len = (int)(p - arg);
242
243 if (what_len == 0)
244 return 0;
245
246 if (STRNCMP(arg, "START", what_len) == 0)
247 {
248 *argp = skipwhite(arg + what_len);
249 return DIP_START + DIP_NORTP;
250 }
251 if (STRNCMP(arg, "OPT", what_len) == 0)
252 {
253 *argp = skipwhite(arg + what_len);
254 return DIP_OPT + DIP_NORTP;
255 }
256 if (STRNCMP(arg, "PACK", what_len) == 0)
257 {
258 *argp = skipwhite(arg + what_len);
259 return DIP_START + DIP_OPT + DIP_NORTP;
260 }
261 if (STRNCMP(arg, "ALL", what_len) == 0)
262 {
263 *argp = skipwhite(arg + what_len);
264 return DIP_START + DIP_OPT;
265 }
266
267 return 0;
268}
269
270/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200271 * ":runtime [what] {name}"
272 */
273 void
274ex_runtime(exarg_T *eap)
275{
276 char_u *arg = eap->arg;
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200277 int flags = eap->forceit ? DIP_ALL : 0;
278
zeertzjq3770f4c2023-01-22 18:38:51 +0000279 flags += get_runtime_cmd_flags(&arg);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200280 source_runtime(arg, flags);
281}
282
zeertzjq3770f4c2023-01-22 18:38:51 +0000283static int runtime_expand_flags;
284
285/*
286 * Set the completion context for the :runtime command.
287 */
288 void
289set_context_in_runtime_cmd(expand_T *xp, char_u *arg)
290{
291 runtime_expand_flags = DIP_KEEPEXT + get_runtime_cmd_flags(&arg);
292 xp->xp_context = EXPAND_RUNTIME;
293 xp->xp_pattern = arg;
294}
295
296/*
297 * Handle command line completion for :runtime command.
298 */
299 int
300expand_runtime_cmd(char_u *pat, int *numMatches, char_u ***matches)
301{
302 char *directories[] = {"", NULL};
303 return ExpandRTDir(pat, runtime_expand_flags, numMatches, matches,
304 directories);
305}
306
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200307 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308source_callback(char_u *fname, void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200309{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100310 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200311}
312
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000313#ifdef FEAT_EVAL
314/*
315 * Find an already loaded script "name".
316 * If found returns its script ID. If not found returns -1.
317 */
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100318 int
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000319find_script_by_name(char_u *name)
320{
321 int sid;
322 scriptitem_T *si;
323
324 for (sid = script_items.ga_len; sid > 0; --sid)
325 {
326 // We used to check inode here, but that doesn't work:
327 // - If a script is edited and written, it may get a different
328 // inode number, even though to the user it is the same script.
329 // - If a script is deleted and another script is written, with a
330 // different name, the inode may be re-used.
331 si = SCRIPT_ITEM(sid);
332 if (si->sn_name != NULL && fnamecmp(si->sn_name, name) == 0)
333 return sid;
334 }
335 return -1;
336}
337
338/*
339 * Add a new scriptitem with all items initialized.
340 * When running out of memory "error" is set to FAIL.
341 * Returns the script ID.
342 */
343 static int
344get_new_scriptitem(int *error)
345{
346 static scid_T last_current_SID = 0;
347 int sid = ++last_current_SID;
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000348 scriptitem_T *si = NULL;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000349
350 if (ga_grow(&script_items, (int)(sid - script_items.ga_len)) == FAIL)
351 {
352 *error = FAIL;
353 return sid;
354 }
355 while (script_items.ga_len < sid)
356 {
357 si = ALLOC_CLEAR_ONE(scriptitem_T);
358 if (si == NULL)
359 {
360 *error = FAIL;
361 return sid;
362 }
363 ++script_items.ga_len;
364 SCRIPT_ITEM(script_items.ga_len) = si;
365 si->sn_name = NULL;
366 si->sn_version = 1;
367
368 // Allocate the local script variables to use for this script.
369 new_script_vars(script_items.ga_len);
370 ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
371 hash_init(&si->sn_all_vars.dv_hashtab);
372 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
373 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
374# ifdef FEAT_PROFILE
375 si->sn_prof_on = FALSE;
376# endif
377 }
378
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000379 // "si" can't be NULL, check only to avoid a compiler warning
380 if (si != NULL)
381 // Used to check script variable index is still valid.
382 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000383
384 return sid;
385}
386
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100387 int
388get_new_scriptitem_for_fname(int *error, char_u *fname)
389{
390 int sid = get_new_scriptitem(error);
391
392 if (*error == OK)
393 {
394 scriptitem_T *si = SCRIPT_ITEM(sid);
395
396 si->sn_name = vim_strsave(fname);
397 si->sn_state = SN_STATE_NOT_LOADED;
398 }
399 return sid;
400}
401
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000402 static void
403find_script_callback(char_u *fname, void *cookie)
404{
405 int sid;
406 int error = OK;
407 int *ret_sid = cookie;
408
409 sid = find_script_by_name(fname);
410 if (sid < 0)
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000411 // script does not exist yet, create a new scriptitem
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100412 sid = get_new_scriptitem_for_fname(&error, fname);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000413 *ret_sid = sid;
414}
415#endif
416
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200417/*
418 * Find the file "name" in all directories in "path" and invoke
419 * "callback(fname, cookie)".
420 * "name" can contain wildcards.
421 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
422 * When "flags" has DIP_DIR: find directories instead of files.
423 * When "flags" has DIP_ERR: give an error message if there is no match.
424 *
425 * return FAIL when no file could be sourced, OK otherwise.
426 */
427 int
428do_in_path(
429 char_u *path,
430 char_u *name,
431 int flags,
432 void (*callback)(char_u *fname, void *ck),
433 void *cookie)
434{
435 char_u *rtp;
436 char_u *np;
437 char_u *buf;
438 char_u *rtp_copy;
439 char_u *tail;
440 int num_files;
441 char_u **files;
442 int i;
443 int did_one = FALSE;
444#ifdef AMIGA
445 struct Process *proc = (struct Process *)FindTask(0L);
446 APTR save_winptr = proc->pr_WindowPtr;
447
448 // Avoid a requester here for a volume that doesn't exist.
449 proc->pr_WindowPtr = (APTR)-1L;
450#endif
451
452 // Make a copy of 'runtimepath'. Invoking the callback may change the
453 // value.
454 rtp_copy = vim_strsave(path);
455 buf = alloc(MAXPATHL);
456 if (buf != NULL && rtp_copy != NULL)
457 {
Bram Moolenaar647a5302020-05-03 17:01:24 +0200458 if (p_verbose > 10 && name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200459 {
460 verbose_enter();
461 smsg(_("Searching for \"%s\" in \"%s\""),
462 (char *)name, (char *)path);
463 verbose_leave();
464 }
465
466 // Loop over all entries in 'runtimepath'.
467 rtp = rtp_copy;
468 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
469 {
470 size_t buflen;
471
472 // Copy the path from 'runtimepath' to buf[].
473 copy_option_part(&rtp, buf, MAXPATHL, ",");
474 buflen = STRLEN(buf);
475
476 // Skip after or non-after directories.
477 if (flags & (DIP_NOAFTER | DIP_AFTER))
478 {
479 int is_after = buflen >= 5
480 && STRCMP(buf + buflen - 5, "after") == 0;
481
482 if ((is_after && (flags & DIP_NOAFTER))
483 || (!is_after && (flags & DIP_AFTER)))
484 continue;
485 }
486
487 if (name == NULL)
488 {
489 (*callback)(buf, (void *) &cookie);
490 if (!did_one)
491 did_one = (cookie == NULL);
492 }
493 else if (buflen + STRLEN(name) + 2 < MAXPATHL)
494 {
495 add_pathsep(buf);
496 tail = buf + STRLEN(buf);
497
498 // Loop over all patterns in "name"
499 np = name;
500 while (*np != NUL && ((flags & DIP_ALL) || !did_one))
501 {
502 // Append the pattern from "name" to buf[].
503 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
504 "\t ");
505
Bram Moolenaar647a5302020-05-03 17:01:24 +0200506 if (p_verbose > 10)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200507 {
508 verbose_enter();
509 smsg(_("Searching for \"%s\""), buf);
510 verbose_leave();
511 }
512
513 // Expand wildcards, invoke the callback for each match.
514 if (gen_expand_wildcards(1, &buf, &num_files, &files,
515 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
516 {
517 for (i = 0; i < num_files; ++i)
518 {
519 (*callback)(files[i], cookie);
520 did_one = TRUE;
521 if (!(flags & DIP_ALL))
522 break;
523 }
524 FreeWild(num_files, files);
525 }
526 }
527 }
528 }
529 }
530 vim_free(buf);
531 vim_free(rtp_copy);
532 if (!did_one && name != NULL)
533 {
534 char *basepath = path == p_rtp ? "runtimepath" : "packpath";
535
536 if (flags & DIP_ERR)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000537 semsg(_(e_directory_not_found_in_str_str), basepath, name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200538 else if (p_verbose > 0)
539 {
540 verbose_enter();
541 smsg(_("not found in '%s': \"%s\""), basepath, name);
542 verbose_leave();
543 }
544 }
545
546#ifdef AMIGA
547 proc->pr_WindowPtr = save_winptr;
548#endif
549
550 return did_one ? OK : FAIL;
551}
552
553/*
554 * Find "name" in "path". When found, invoke the callback function for
555 * it: callback(fname, "cookie")
556 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first
557 * one is used.
558 * Returns OK when at least one match found, FAIL otherwise.
559 *
560 * If "name" is NULL calls callback for each entry in "path". Cookie is
561 * passed by reference in this case, setting it to NULL indicates that callback
562 * has done its job.
563 */
564 static int
565do_in_path_and_pp(
566 char_u *path,
567 char_u *name,
568 int flags,
569 void (*callback)(char_u *fname, void *ck),
570 void *cookie)
571{
572 int done = FAIL;
573 char_u *s;
574 int len;
575 char *start_dir = "pack/*/start/*/%s";
576 char *opt_dir = "pack/*/opt/*/%s";
577
578 if ((flags & DIP_NORTP) == 0)
579 done = do_in_path(path, name, flags, callback, cookie);
580
581 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
582 {
583 len = (int)(STRLEN(start_dir) + STRLEN(name));
584 s = alloc(len);
585 if (s == NULL)
586 return FAIL;
587 vim_snprintf((char *)s, len, start_dir, name);
588 done = do_in_path(p_pp, s, flags, callback, cookie);
589 vim_free(s);
590 }
591
592 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
593 {
594 len = (int)(STRLEN(opt_dir) + STRLEN(name));
595 s = alloc(len);
596 if (s == NULL)
597 return FAIL;
598 vim_snprintf((char *)s, len, opt_dir, name);
599 done = do_in_path(p_pp, s, flags, callback, cookie);
600 vim_free(s);
601 }
602
603 return done;
604}
605
606/*
607 * Just like do_in_path_and_pp(), using 'runtimepath' for "path".
608 */
609 int
610do_in_runtimepath(
611 char_u *name,
612 int flags,
613 void (*callback)(char_u *fname, void *ck),
614 void *cookie)
615{
616 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
617}
618
619/*
620 * Source the file "name" from all directories in 'runtimepath'.
621 * "name" can contain wildcards.
622 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
623 *
624 * return FAIL when no file could be sourced, OK otherwise.
625 */
626 int
627source_runtime(char_u *name, int flags)
628{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100629 return source_in_path(p_rtp, name, flags, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200630}
631
632/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000633 * Just like source_runtime(), but use "path" instead of 'runtimepath'
634 * and return the script ID in "ret_sid".
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200635 */
636 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200638{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100639 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200640}
641
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200642#if defined(FEAT_EVAL) || defined(PROTO)
643
644/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000645 * Find "name" in 'runtimepath'. If found a new scriptitem is created for it
646 * and it's script ID is returned.
647 * If not found returns -1.
648 */
649 int
650find_script_in_rtp(char_u *name)
651{
652 int sid = -1;
653
654 (void)do_in_path_and_pp(p_rtp, name, DIP_NOAFTER,
655 find_script_callback, &sid);
656 return sid;
657}
658
659/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200660 * Expand wildcards in "pat" and invoke do_source() for each match.
661 */
662 static void
663source_all_matches(char_u *pat)
664{
665 int num_files;
666 char_u **files;
667 int i;
668
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000669 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) != OK)
670 return;
671
672 for (i = 0; i < num_files; ++i)
673 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
674 FreeWild(num_files, files);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200675}
676
677/*
678 * Add the package directory to 'runtimepath'.
679 */
680 static int
681add_pack_dir_to_rtp(char_u *fname)
682{
683 char_u *p4, *p3, *p2, *p1, *p;
684 char_u *entry;
685 char_u *insp = NULL;
686 int c;
687 char_u *new_rtp;
688 int keep;
689 size_t oldlen;
690 size_t addlen;
691 size_t new_rtp_len;
692 char_u *afterdir = NULL;
693 size_t afterlen = 0;
694 char_u *after_insp = NULL;
695 char_u *ffname = NULL;
696 size_t fname_len;
697 char_u *buf = NULL;
698 char_u *rtp_ffname;
699 int match;
700 int retval = FAIL;
701
702 p4 = p3 = p2 = p1 = get_past_head(fname);
703 for (p = p1; *p; MB_PTR_ADV(p))
704 if (vim_ispathsep_nocolon(*p))
705 {
706 p4 = p3; p3 = p2; p2 = p1; p1 = p;
707 }
708
709 // now we have:
710 // rtp/pack/name/start/name
711 // p4 p3 p2 p1
712 //
713 // find the part up to "pack" in 'runtimepath'
714 c = *++p4; // append pathsep in order to expand symlink
715 *p4 = NUL;
716 ffname = fix_fname(fname);
717 *p4 = c;
718 if (ffname == NULL)
719 return FAIL;
720
721 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
722 // Also stop at the first "after" directory.
723 fname_len = STRLEN(ffname);
724 buf = alloc(MAXPATHL);
725 if (buf == NULL)
726 goto theend;
727 for (entry = p_rtp; *entry != NUL; )
728 {
729 char_u *cur_entry = entry;
730
731 copy_option_part(&entry, buf, MAXPATHL, ",");
732 if (insp == NULL)
733 {
734 add_pathsep(buf);
735 rtp_ffname = fix_fname(buf);
736 if (rtp_ffname == NULL)
737 goto theend;
738 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
739 vim_free(rtp_ffname);
740 if (match)
741 // Insert "ffname" after this entry (and comma).
742 insp = entry;
743 }
744
745 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
746 && p > buf
747 && vim_ispathsep(p[-1])
748 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
749 {
750 if (insp == NULL)
751 // Did not find "ffname" before the first "after" directory,
752 // insert it before this entry.
753 insp = cur_entry;
754 after_insp = cur_entry;
755 break;
756 }
757 }
758
759 if (insp == NULL)
760 // Both "fname" and "after" not found, append at the end.
761 insp = p_rtp + STRLEN(p_rtp);
762
763 // check if rtp/pack/name/start/name/after exists
764 afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
765 if (afterdir != NULL && mch_isdir(afterdir))
766 afterlen = STRLEN(afterdir) + 1; // add one for comma
767
768 oldlen = STRLEN(p_rtp);
769 addlen = STRLEN(fname) + 1; // add one for comma
770 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
771 if (new_rtp == NULL)
772 goto theend;
773
774 // We now have 'rtp' parts: {keep}{keep_after}{rest}.
775 // Create new_rtp, first: {keep},{fname}
776 keep = (int)(insp - p_rtp);
777 mch_memmove(new_rtp, p_rtp, keep);
778 new_rtp_len = keep;
779 if (*insp == NUL)
780 new_rtp[new_rtp_len++] = ','; // add comma before
781 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
782 new_rtp_len += addlen - 1;
783 if (*insp != NUL)
784 new_rtp[new_rtp_len++] = ','; // add comma after
785
786 if (afterlen > 0 && after_insp != NULL)
787 {
788 int keep_after = (int)(after_insp - p_rtp);
789
790 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
791 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
792 keep_after - keep);
793 new_rtp_len += keep_after - keep;
794 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
795 new_rtp_len += afterlen - 1;
796 new_rtp[new_rtp_len++] = ',';
797 keep = keep_after;
798 }
799
800 if (p_rtp[keep] != NUL)
801 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
802 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
803 else
804 new_rtp[new_rtp_len] = NUL;
805
806 if (afterlen > 0 && after_insp == NULL)
807 {
808 // Append afterdir when "after" was not found:
809 // {keep},{fname}{rest},{afterdir}
810 STRCAT(new_rtp, ",");
811 STRCAT(new_rtp, afterdir);
812 }
813
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100814 set_option_value_give_err((char_u *)"rtp", 0L, new_rtp, 0);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200815 vim_free(new_rtp);
816 retval = OK;
817
818theend:
819 vim_free(buf);
820 vim_free(ffname);
821 vim_free(afterdir);
822 return retval;
823}
824
825/*
826 * Load scripts in "plugin" and "ftdetect" directories of the package.
827 */
828 static int
829load_pack_plugin(char_u *fname)
830{
831 static char *plugpat = "%s/plugin/**/*.vim";
832 static char *ftpat = "%s/ftdetect/*.vim";
833 int len;
834 char_u *ffname = fix_fname(fname);
835 char_u *pat = NULL;
836 int retval = FAIL;
837
838 if (ffname == NULL)
839 return FAIL;
840 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
841 pat = alloc(len);
842 if (pat == NULL)
843 goto theend;
844 vim_snprintf((char *)pat, len, plugpat, ffname);
845 source_all_matches(pat);
846
847 {
848 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
849
850 // If runtime/filetype.vim wasn't loaded yet, the scripts will be
851 // found when it loads.
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100852 if (cmd != NULL && eval_to_number(cmd, FALSE) > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200853 {
854 do_cmdline_cmd((char_u *)"augroup filetypedetect");
855 vim_snprintf((char *)pat, len, ftpat, ffname);
856 source_all_matches(pat);
857 do_cmdline_cmd((char_u *)"augroup END");
858 }
859 vim_free(cmd);
860 }
861 vim_free(pat);
862 retval = OK;
863
864theend:
865 vim_free(ffname);
866 return retval;
867}
868
869// used for "cookie" of add_pack_plugin()
870static int APP_ADD_DIR;
871static int APP_LOAD;
872static int APP_BOTH;
873
874 static void
875add_pack_plugin(char_u *fname, void *cookie)
876{
877 if (cookie != &APP_LOAD)
878 {
879 char_u *buf = alloc(MAXPATHL);
880 char_u *p;
881 int found = FALSE;
882
883 if (buf == NULL)
884 return;
885 p = p_rtp;
886 while (*p != NUL)
887 {
888 copy_option_part(&p, buf, MAXPATHL, ",");
889 if (pathcmp((char *)buf, (char *)fname, -1) == 0)
890 {
891 found = TRUE;
892 break;
893 }
894 }
895 vim_free(buf);
896 if (!found)
897 // directory is not yet in 'runtimepath', add it
898 if (add_pack_dir_to_rtp(fname) == FAIL)
899 return;
900 }
901
902 if (cookie != &APP_ADD_DIR)
903 load_pack_plugin(fname);
904}
905
906/*
907 * Add all packages in the "start" directory to 'runtimepath'.
908 */
909 void
910add_pack_start_dirs(void)
911{
912 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
913 add_pack_plugin, &APP_ADD_DIR);
914}
915
916/*
917 * Load plugins from all packages in the "start" directory.
918 */
919 void
920load_start_packages(void)
921{
922 did_source_packages = TRUE;
923 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
924 add_pack_plugin, &APP_LOAD);
925}
926
927/*
928 * ":packloadall"
929 * Find plugins in the package directories and source them.
930 */
931 void
932ex_packloadall(exarg_T *eap)
933{
934 if (!did_source_packages || eap->forceit)
935 {
936 // First do a round to add all directories to 'runtimepath', then load
937 // the plugins. This allows for plugins to use an autoload directory
938 // of another plugin.
939 add_pack_start_dirs();
940 load_start_packages();
941 }
942}
943
944/*
945 * ":packadd[!] {name}"
946 */
947 void
948ex_packadd(exarg_T *eap)
949{
950 static char *plugpat = "pack/*/%s/%s";
951 int len;
952 char *pat;
953 int round;
954 int res = OK;
955
956 // Round 1: use "start", round 2: use "opt".
957 for (round = 1; round <= 2; ++round)
958 {
959 // Only look under "start" when loading packages wasn't done yet.
960 if (round == 1 && did_source_packages)
961 continue;
962
963 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
964 pat = alloc(len);
965 if (pat == NULL)
966 return;
967 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
968 // The first round don't give a "not found" error, in the second round
969 // only when nothing was found in the first round.
970 res = do_in_path(p_pp, (char_u *)pat,
971 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
972 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
973 vim_free(pat);
974 }
975}
976#endif
977
978/*
Bram Moolenaar26262f82019-09-04 20:59:15 +0200979 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
980 * list of file names in allocated memory.
981 */
982 void
983remove_duplicates(garray_T *gap)
984{
985 int i;
986 int j;
987 char_u **fnames = (char_u **)gap->ga_data;
988
989 sort_strings(fnames, gap->ga_len);
990 for (i = gap->ga_len - 1; i > 0; --i)
991 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
992 {
993 vim_free(fnames[i]);
994 for (j = i + 1; j < gap->ga_len; ++j)
995 fnames[j - 1] = fnames[j];
996 --gap->ga_len;
997 }
998}
999
1000/*
zeertzjq3770f4c2023-01-22 18:38:51 +00001001 * Expand runtime file names.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001002 * Search from 'runtimepath':
1003 * 'runtimepath'/{dirnames}/{pat}.vim
zeertzjq3770f4c2023-01-22 18:38:51 +00001004 * When "flags" has DIP_START: search also from "start" of 'packpath':
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001005 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
zeertzjq3770f4c2023-01-22 18:38:51 +00001006 * When "flags" has DIP_OPT: search also from "opt" of 'packpath':
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001007 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
1008 * "dirnames" is an array with one or more directory names.
1009 */
1010 int
1011ExpandRTDir(
1012 char_u *pat,
1013 int flags,
1014 int *num_file,
1015 char_u ***file,
1016 char *dirnames[])
1017{
1018 char_u *s;
1019 char_u *e;
1020 char_u *match;
1021 garray_T ga;
1022 int i;
1023 int pat_len;
1024
1025 *num_file = 0;
1026 *file = NULL;
1027 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001028 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001029
1030 for (i = 0; dirnames[i] != NULL; ++i)
1031 {
zeertzjq3770f4c2023-01-22 18:38:51 +00001032 size_t buf_len = STRLEN(dirnames[i]) + pat_len + 22;
1033 char *buf = alloc(buf_len);
roota6759382023-01-21 21:56:06 +00001034 if (buf == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001035 {
1036 ga_clear_strings(&ga);
1037 return FAIL;
1038 }
zeertzjq3770f4c2023-01-22 18:38:51 +00001039 char *tail = buf + 15;
1040 size_t tail_buflen = buf_len - 15;
1041 int glob_flags = 0;
1042 int expand_dirs = FALSE;
1043
1044 if (*(dirnames[i]) == NUL) // empty dir used for :runtime
1045 vim_snprintf(tail, tail_buflen, "%s*.vim", pat);
roota6759382023-01-21 21:56:06 +00001046 else
zeertzjq3770f4c2023-01-22 18:38:51 +00001047 vim_snprintf(tail, tail_buflen, "%s/%s*.vim", dirnames[i], pat);
1048
1049expand:
1050 if ((flags & DIP_NORTP) == 0)
1051 globpath(p_rtp, (char_u *)tail, &ga, glob_flags, expand_dirs);
1052
1053 if (flags & DIP_START)
roota6759382023-01-21 21:56:06 +00001054 {
zeertzjq3770f4c2023-01-22 18:38:51 +00001055 memcpy(tail - 15, "pack/*/start/*/", 15);
1056 globpath(p_pp, (char_u *)tail - 15, &ga, glob_flags, expand_dirs);
roota6759382023-01-21 21:56:06 +00001057 }
zeertzjq3770f4c2023-01-22 18:38:51 +00001058
1059 if (flags & DIP_OPT)
1060 {
1061 memcpy(tail - 13, "pack/*/opt/*/", 13);
1062 globpath(p_pp, (char_u *)tail - 13, &ga, glob_flags, expand_dirs);
1063 }
1064
1065 if (*(dirnames[i]) == NUL && !expand_dirs)
1066 {
1067 // expand dir names in another round
1068 vim_snprintf(tail, tail_buflen, "%s*", pat);
1069 glob_flags = WILD_ADD_SLASH;
1070 expand_dirs = TRUE;
1071 goto expand;
1072 }
1073
roota6759382023-01-21 21:56:06 +00001074 vim_free(buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001075 }
1076
zeertzjq3770f4c2023-01-22 18:38:51 +00001077 int pat_pathsep_cnt = 0;
1078 for (i = 0; i < pat_len; ++i)
1079 if (vim_ispathsep(pat[i]))
1080 ++pat_pathsep_cnt;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001081
1082 for (i = 0; i < ga.ga_len; ++i)
1083 {
1084 match = ((char_u **)ga.ga_data)[i];
1085 s = match;
1086 e = s + STRLEN(s);
zeertzjq3770f4c2023-01-22 18:38:51 +00001087 if (e - 4 > s && (flags & DIP_KEEPEXT) == 0
1088 && STRNICMP(e - 4, ".vim", 4) == 0)
roota6759382023-01-21 21:56:06 +00001089 {
zeertzjq3770f4c2023-01-22 18:38:51 +00001090 e -= 4;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001091 *e = NUL;
roota6759382023-01-21 21:56:06 +00001092 }
1093
zeertzjq3770f4c2023-01-22 18:38:51 +00001094 int match_pathsep_cnt = (e > s && e[-1] == '/') ? -1 : 0;
1095 for (s = e; s > match; MB_PTR_BACK(match, s))
1096 if (s < match || (vim_ispathsep(*s)
1097 && ++match_pathsep_cnt > pat_pathsep_cnt))
1098 break;
1099 ++s;
1100 *e = NUL;
1101 mch_memmove(match, s, e - s + 1);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001102 }
1103
1104 if (ga.ga_len == 0)
1105 return FAIL;
1106
1107 // Sort and remove duplicates which can happen when specifying multiple
1108 // directories in dirnames.
1109 remove_duplicates(&ga);
1110
1111 *file = ga.ga_data;
1112 *num_file = ga.ga_len;
1113 return OK;
1114}
1115
1116/*
1117 * Expand loadplugin names:
1118 * 'packpath'/pack/ * /opt/{pat}
1119 */
1120 int
1121ExpandPackAddDir(
1122 char_u *pat,
1123 int *num_file,
1124 char_u ***file)
1125{
1126 char_u *s;
1127 char_u *e;
1128 char_u *match;
1129 garray_T ga;
1130 int i;
1131 int pat_len;
1132
1133 *num_file = 0;
1134 *file = NULL;
1135 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001136 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001137
1138 s = alloc(pat_len + 26);
1139 if (s == NULL)
1140 {
1141 ga_clear_strings(&ga);
1142 return FAIL;
1143 }
1144 sprintf((char *)s, "pack/*/opt/%s*", pat);
zeertzjq3770f4c2023-01-22 18:38:51 +00001145 globpath(p_pp, s, &ga, 0, TRUE);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001146 vim_free(s);
1147
1148 for (i = 0; i < ga.ga_len; ++i)
1149 {
1150 match = ((char_u **)ga.ga_data)[i];
1151 s = gettail(match);
1152 e = s + STRLEN(s);
1153 mch_memmove(match, s, e - s + 1);
1154 }
1155
1156 if (ga.ga_len == 0)
1157 return FAIL;
1158
1159 // Sort and remove duplicates which can happen when specifying multiple
1160 // directories in dirnames.
1161 remove_duplicates(&ga);
1162
1163 *file = ga.ga_data;
1164 *num_file = ga.ga_len;
1165 return OK;
1166}
1167
1168 static void
1169cmd_source(char_u *fname, exarg_T *eap)
1170{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001171 int clearvars = FALSE;
1172
1173 if (*fname != NUL && STRNCMP(fname, "++clear", 7) == 0)
1174 {
1175 // ++clear argument is supplied
1176 clearvars = TRUE;
1177 fname = fname + 7;
1178 if (*fname != NUL)
1179 {
1180 semsg(_(e_invalid_argument_str), eap->arg);
1181 return;
1182 }
1183 }
1184
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001185 if (*fname != NUL && eap != NULL && eap->addr_count > 0)
1186 {
1187 // if a filename is specified to :source, then a range is not allowed
1188 emsg(_(e_no_range_allowed));
1189 return;
1190 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001191
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001192 if (eap != NULL && *fname == NUL)
1193 {
1194 if (eap->forceit)
1195 // a file name is needed to source normal mode commands
1196 emsg(_(e_argument_required));
1197 else
1198 // source ex commands from the current buffer
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001199 do_source_ext(NULL, FALSE, FALSE, NULL, eap, clearvars);
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001200 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001201 else if (eap != NULL && eap->forceit)
1202 // ":source!": read Normal mode commands
1203 // Need to execute the commands directly. This is required at least
1204 // for:
1205 // - ":g" command busy
1206 // - after ":argdo", ":windo" or ":bufdo"
1207 // - another command follows
1208 // - inside a loop
1209 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
1210#ifdef FEAT_EVAL
1211 || eap->cstack->cs_idx >= 0
1212#endif
1213 );
1214
1215 // ":source" read ex commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001217 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001218}
1219
1220/*
1221 * ":source {fname}"
1222 */
1223 void
1224ex_source(exarg_T *eap)
1225{
1226#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02001227 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001228 {
1229 char_u *fname = NULL;
1230
1231 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
1232 NULL, NULL,
1233 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1234 if (fname != NULL)
1235 {
1236 cmd_source(fname, eap);
1237 vim_free(fname);
1238 }
1239 }
1240 else
1241#endif
1242 cmd_source(eap->arg, eap);
1243}
1244
1245#if defined(FEAT_EVAL) || defined(PROTO)
1246/*
1247 * ":options"
1248 */
1249 void
1250ex_options(
1251 exarg_T *eap UNUSED)
1252{
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001253 char_u buf[500];
1254 int multi_mods = 0;
1255
1256 buf[0] = NUL;
dundargocc57b5bc2022-11-02 13:30:51 +00001257 (void)add_win_cmd_modifiers(buf, &cmdmod, &multi_mods);
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001258
1259 vim_setenv((char_u *)"OPTWIN_CMD", buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001260 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
1261}
1262#endif
1263
1264/*
1265 * ":source" and associated commands.
1266 */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001267
1268#ifdef FEAT_EVAL
1269/*
1270 * Return the address holding the next breakpoint line for a source cookie.
1271 */
1272 linenr_T *
1273source_breakpoint(void *cookie)
1274{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001275 return &((source_cookie_T *)cookie)->breakpoint;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001276}
1277
1278/*
1279 * Return the address holding the debug tick for a source cookie.
1280 */
1281 int *
1282source_dbg_tick(void *cookie)
1283{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001284 return &((source_cookie_T *)cookie)->dbg_tick;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001285}
1286
1287/*
1288 * Return the nesting level for a source cookie.
1289 */
1290 int
1291source_level(void *cookie)
1292{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001293 return ((source_cookie_T *)cookie)->level;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001294}
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001295
1296/*
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02001297 * Return the readahead line. Note that the pointer may become invalid when
1298 * getting the next line, if it's concatenated with the next one.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001299 */
1300 char_u *
1301source_nextline(void *cookie)
1302{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001303 return ((source_cookie_T *)cookie)->nextline;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001304}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001305#endif
1306
1307#if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC)
1308# define USE_FOPEN_NOINH
1309/*
1310 * Special function to open a file without handle inheritance.
1311 * When possible the handle is closed on exec().
1312 */
1313 static FILE *
1314fopen_noinh_readbin(char *filename)
1315{
1316# ifdef MSWIN
1317 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
1318# else
1319 int fd_tmp = mch_open(filename, O_RDONLY, 0);
1320# endif
1321
1322 if (fd_tmp == -1)
1323 return NULL;
1324
1325# ifdef HAVE_FD_CLOEXEC
1326 {
1327 int fdflags = fcntl(fd_tmp, F_GETFD);
1328 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1329 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
1330 }
1331# endif
1332
1333 return fdopen(fd_tmp, READBIN);
1334}
1335#endif
1336
1337/*
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001338 * Initialization for sourcing lines from the current buffer. Reads all the
1339 * lines from the buffer and stores it in the cookie grow array.
1340 * Returns a pointer to the name ":source buffer=<n>" on success and NULL on
1341 * failure.
1342 */
1343 static char_u *
1344do_source_buffer_init(source_cookie_T *sp, exarg_T *eap)
1345{
1346 linenr_T curr_lnum;
1347 char_u *line = NULL;
1348 char_u *fname;
1349
1350 CLEAR_FIELD(*sp);
1351
1352 if (curbuf == NULL)
1353 return NULL;
1354
1355 // Use ":source buffer=<num>" as the script name
1356 vim_snprintf((char *)IObuff, IOSIZE, ":source buffer=%d", curbuf->b_fnum);
1357 fname = vim_strsave(IObuff);
1358 if (fname == NULL)
1359 return NULL;
1360
1361 ga_init2(&sp->buflines, sizeof(char_u *), 100);
1362
1363 // Copy the lines from the buffer into a grow array
1364 for (curr_lnum = eap->line1; curr_lnum <= eap->line2; curr_lnum++)
1365 {
1366 line = vim_strsave(ml_get(curr_lnum));
1367 if (line == NULL)
1368 goto errret;
1369 if (ga_add_string(&sp->buflines, line) == FAIL)
1370 goto errret;
1371 line = NULL;
1372 }
1373 sp->buf_lnum = 0;
1374 sp->source_from_buf = TRUE;
1375
1376 return fname;
1377
1378errret:
1379 vim_free(fname);
1380 vim_free(line);
1381 ga_clear_strings(&sp->buflines);
1382 return NULL;
1383}
1384
1385/*
1386 * Read the file "fname" and execute its lines as EX commands.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1388 * again.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001389 *
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001390 * The "eap" argument is used when sourcing lines from a buffer instead of a
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001391 * file.
1392 *
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001393 * If "clearvars" is TRUE, then for scripts which are loaded more than
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001394 * once, clear all the functions and variables previously defined in that
1395 * script.
1396 *
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001397 * This function may be called recursively!
1398 *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399 * Return FAIL if file could not be opened, OK otherwise.
1400 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001401 */
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001402 static int
1403do_source_ext(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001404 char_u *fname,
1405 int check_other, // check for .vimrc and _vimrc
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 int is_vimrc, // DOSO_ value
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001407 int *ret_sid UNUSED,
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001408 exarg_T *eap,
1409 int clearvars UNUSED)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001410{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001411 source_cookie_T cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001412 char_u *p;
Bram Moolenaar753885b2022-08-24 16:30:36 +01001413 char_u *fname_not_fixed = NULL;
Bram Moolenaar5214b292022-08-24 17:32:35 +01001414 char_u *fname_exp = NULL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001415 char_u *firstline = NULL;
1416 int retval = FAIL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001417 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001418#ifdef FEAT_EVAL
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001419 funccal_entry_T funccalp_entry;
1420 int save_debug_break_level = debug_break_level;
Bram Moolenaar5214b292022-08-24 17:32:35 +01001421 int sid = -1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001422 scriptitem_T *si = NULL;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001423 int save_estack_compiling = estack_compiling;
Bram Moolenaar88774872022-08-16 20:24:29 +01001424 ESTACK_CHECK_DECLARATION
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001425#endif
1426#ifdef STARTUPTIME
1427 struct timeval tv_rel;
1428 struct timeval tv_start;
1429#endif
1430#ifdef FEAT_PROFILE
1431 proftime_T wait_start;
1432#endif
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001433 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001434 int trigger_source_post = FALSE;
1435
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001436 CLEAR_FIELD(cookie);
1437 if (fname == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001438 {
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001439 // sourcing lines from a buffer
1440 fname_exp = do_source_buffer_init(&cookie, eap);
1441 if (fname_exp == NULL)
1442 return FAIL;
1443 }
1444 else
1445 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01001446 fname_not_fixed = expand_env_save(fname);
1447 if (fname_not_fixed == NULL)
1448 goto theend;
1449 fname_exp = fix_fname(fname_not_fixed);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001450 if (fname_exp == NULL)
Bram Moolenaar753885b2022-08-24 16:30:36 +01001451 goto theend;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001452 if (mch_isdir(fname_exp))
1453 {
1454 smsg(_("Cannot source a directory: \"%s\""), fname);
1455 goto theend;
1456 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001457 }
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001458#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001459 estack_compiling = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 // See if we loaded this script before.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001462 sid = find_script_by_name(fname_exp);
Bram Moolenaarf479cac2022-01-12 12:54:55 +00001463 if (sid > 0 && ret_sid != NULL
1464 && SCRIPT_ITEM(sid)->sn_state != SN_STATE_NOT_LOADED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465 {
1466 // Already loaded and no need to load again, return here.
1467 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001468 retval = OK;
1469 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 }
1471#endif
1472
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001473 // Apply SourceCmd autocommands, they should get the file and source it.
1474 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1475 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1476 FALSE, curbuf))
1477 {
1478#ifdef FEAT_EVAL
1479 retval = aborting() ? FAIL : OK;
1480#else
1481 retval = OK;
1482#endif
1483 if (retval == OK)
1484 // Apply SourcePost autocommands.
1485 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1486 FALSE, curbuf);
1487 goto theend;
1488 }
1489
1490 // Apply SourcePre autocommands, they may get the file.
1491 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1492
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001493 if (!cookie.source_from_buf)
1494 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001495#ifdef USE_FOPEN_NOINH
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001496 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001497#else
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001498 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001499#endif
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001500 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001501 if (cookie.fp == NULL && check_other)
1502 {
1503 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1504 // and ".exrc" by "_exrc" or vice versa.
1505 p = gettail(fname_exp);
1506 if ((*p == '.' || *p == '_')
1507 && (STRICMP(p + 1, "vimrc") == 0
1508 || STRICMP(p + 1, "gvimrc") == 0
1509 || STRICMP(p + 1, "exrc") == 0))
1510 {
1511 if (*p == '_')
1512 *p = '.';
1513 else
1514 *p = '_';
1515#ifdef USE_FOPEN_NOINH
1516 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1517#else
1518 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1519#endif
1520 }
1521 }
1522
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001523 if (cookie.fp == NULL && !cookie.source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001524 {
1525 if (p_verbose > 0)
1526 {
1527 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001528 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001529 smsg(_("could not source \"%s\""), fname);
1530 else
1531 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001532 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001533 verbose_leave();
1534 }
1535 goto theend;
1536 }
1537
1538 // The file exists.
1539 // - In verbose mode, give a message.
1540 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1541 if (p_verbose > 1)
1542 {
1543 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001544 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001545 smsg(_("sourcing \"%s\""), fname);
1546 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001547 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001548 verbose_leave();
1549 }
1550 if (is_vimrc == DOSO_VIMRC)
1551 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1552 else if (is_vimrc == DOSO_GVIMRC)
1553 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1554
1555#ifdef USE_CRNL
1556 // If no automatic file format: Set default to CR-NL.
1557 if (*p_ffs == NUL)
1558 cookie.fileformat = EOL_DOS;
1559 else
1560 cookie.fileformat = EOL_UNKNOWN;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001561#endif
1562
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001563 if (fname == NULL)
1564 // When sourcing a range of lines from a buffer, use the buffer line
1565 // number.
1566 cookie.sourcing_lnum = eap->line1 - 1;
1567 else
1568 cookie.sourcing_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001569
1570#ifdef FEAT_EVAL
1571 // Check if this script has a breakpoint.
1572 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1573 cookie.fname = fname_exp;
1574 cookie.dbg_tick = debug_tick;
1575
1576 cookie.level = ex_nesting_level;
1577#endif
1578
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001579#ifdef STARTUPTIME
1580 if (time_fd != NULL)
1581 time_push(&tv_rel, &tv_start);
1582#endif
1583
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001584 // "legacy" does not apply to commands in the script
1585 sticky_cmdmod_flags = 0;
1586
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001587 save_current_sctx = current_sctx;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001588 if (cmdmod.cmod_flags & CMOD_VIM9CMD)
1589 // When the ":vim9cmd" command modifier is used, source the script as a
1590 // Vim9 script.
1591 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1592 else
1593 current_sctx.sc_version = 1; // default script version
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001594
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001595#ifdef FEAT_EVAL
Bram Moolenaara2942c72023-01-02 13:41:49 +00001596 current_sctx.sc_lnum = 0;
1597
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001598# ifdef FEAT_PROFILE
1599 if (do_profiling == PROF_YES)
1600 prof_child_enter(&wait_start); // entering a child now
1601# endif
1602
1603 // Don't use local function variables, if called from a function.
1604 // Also starts profiling timer for nested script.
1605 save_funccal(&funccalp_entry);
1606
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001607 // Reset "KeyTyped" to avoid some commands thinking they are invoked
1608 // interactively. E.g. defining a function would output indent.
1609 int save_KeyTyped = KeyTyped;
1610 KeyTyped = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001611
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001612 // Check if this script was sourced before to find its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001613 // Always use a new sequence number.
1614 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001616 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617 hashtab_T *ht;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001618 int todo;
1619 hashitem_T *hi;
1620 dictitem_T *di;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621
1622 // loading the same script again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001623 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001624 si = SCRIPT_ITEM(sid);
1625 if (si->sn_state == SN_STATE_NOT_LOADED)
1626 {
1627 // this script was found but not loaded yet
1628 si->sn_state = SN_STATE_NEW;
1629 }
1630 else
1631 {
1632 si->sn_state = SN_STATE_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001633
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001634 if (!clearvars)
1635 {
1636 // Script-local variables remain but "const" can be set again.
1637 // In Vim9 script variables will be cleared when "vim9script"
1638 // is encountered without the "noclear" argument.
1639 ht = &SCRIPT_VARS(sid);
1640 todo = (int)ht->ht_used;
1641 for (hi = ht->ht_array; todo > 0; ++hi)
1642 if (!HASHITEM_EMPTY(hi))
1643 {
1644 --todo;
1645 di = HI2DI(hi);
1646 di->di_flags |= DI_FLAGS_RELOAD;
1647 }
1648 // imports can be redefined once
1649 mark_imports_for_reload(sid);
1650 }
1651 else
1652 clear_vim9_scriptlocal_vars(sid);
Bram Moolenaar0123cc12021-02-07 17:17:58 +01001653
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001654 // reset version, "vim9script" may have been added or removed.
1655 si->sn_version = 1;
1656 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001657 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001659 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001660 int error = OK;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001661
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001662 // It's new, generate a new SID and initialize the scriptitem.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001663 sid = get_new_scriptitem(&error);
1664 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001665 if (error == FAIL)
1666 goto almosttheend;
Bram Moolenaar753885b2022-08-24 16:30:36 +01001667 si = SCRIPT_ITEM(sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001668 si->sn_name = fname_exp;
1669 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670 if (ret_sid != NULL)
Bram Moolenaar753885b2022-08-24 16:30:36 +01001671 *ret_sid = sid;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001672
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001673 // Remember the "is_vimrc" flag for when the file is sourced again.
1674 si->sn_is_vimrc = is_vimrc;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001675 }
1676
zeertzjq5a4fff42022-08-15 17:53:55 +01001677 // Keep the sourcing name/lnum, for recursive calls.
1678 estack_push(ETYPE_SCRIPT, si->sn_name, 0);
1679 ESTACK_CHECK_SETUP
1680
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001681# ifdef FEAT_PROFILE
1682 if (do_profiling == PROF_YES)
1683 {
1684 int forceit;
1685
1686 // Check if we do profiling for this script.
1687 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
1688 {
1689 script_do_profile(si);
1690 si->sn_pr_force = forceit;
1691 }
1692 if (si->sn_prof_on)
1693 {
1694 ++si->sn_pr_count;
1695 profile_start(&si->sn_pr_start);
1696 profile_zero(&si->sn_pr_children);
1697 }
1698 }
1699# endif
Bram Moolenaaraeef1f72022-09-15 12:20:18 +01001700#else
1701 // Keep the sourcing name/lnum, for recursive calls.
1702 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001703#endif
1704
1705 cookie.conv.vc_type = CONV_NONE; // no conversion
1706
1707 // Read the first line so we can check for a UTF-8 BOM.
1708 firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1709 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1710 && firstline[1] == 0xbb && firstline[2] == 0xbf)
1711 {
1712 // Found BOM; setup conversion, skip over BOM and recode the line.
1713 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1714 p = string_convert(&cookie.conv, firstline + 3, NULL);
1715 if (p == NULL)
1716 p = vim_strsave(firstline + 3);
1717 if (p != NULL)
1718 {
1719 vim_free(firstline);
1720 firstline = p;
1721 }
1722 }
1723
1724 // Call do_cmdline, which will call getsourceline() to get the lines.
1725 do_cmdline(firstline, getsourceline, (void *)&cookie,
1726 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1727 retval = OK;
1728
1729#ifdef FEAT_PROFILE
1730 if (do_profiling == PROF_YES)
1731 {
1732 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001733 si = SCRIPT_ITEM(sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001734 if (si->sn_prof_on)
1735 {
1736 profile_end(&si->sn_pr_start);
1737 profile_sub_wait(&wait_start, &si->sn_pr_start);
1738 profile_add(&si->sn_pr_total, &si->sn_pr_start);
1739 profile_self(&si->sn_pr_self, &si->sn_pr_start,
1740 &si->sn_pr_children);
1741 }
1742 }
1743#endif
1744
1745 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001746 emsg(_(e_interrupted));
Bram Moolenaar88774872022-08-16 20:24:29 +01001747#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +01001748 ESTACK_CHECK_NOW
Bram Moolenaar88774872022-08-16 20:24:29 +01001749#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001750 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001751 if (p_verbose > 1)
1752 {
1753 verbose_enter();
1754 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001755 if (SOURCING_NAME != NULL)
1756 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001757 verbose_leave();
1758 }
1759#ifdef STARTUPTIME
1760 if (time_fd != NULL)
1761 {
1762 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1763 time_msg((char *)IObuff, &tv_start);
1764 time_pop(&tv_rel);
1765 }
1766#endif
1767
1768 if (!got_int)
1769 trigger_source_post = TRUE;
1770
1771#ifdef FEAT_EVAL
1772 // After a "finish" in debug mode, need to break at first command of next
1773 // sourced file.
1774 if (save_debug_break_level > ex_nesting_level
1775 && debug_break_level == ex_nesting_level)
1776 ++debug_break_level;
1777#endif
1778
1779#ifdef FEAT_EVAL
1780almosttheend:
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001781 // If "sn_save_cpo" is set that means we encountered "vim9script": restore
1782 // 'cpoptions', unless in the main .vimrc file.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001784 si = SCRIPT_ITEM(sid);
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001785 if (si->sn_save_cpo != NULL && si->sn_is_vimrc == DOSO_NONE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786 {
Bram Moolenaar3e191692021-03-17 17:46:00 +01001787 if (STRCMP(p_cpo, CPO_VIM) != 0)
1788 {
1789 char_u *f;
1790 char_u *t;
1791
1792 // 'cpo' was changed in the script. Apply the same change to the
1793 // saved value, if possible.
1794 for (f = (char_u *)CPO_VIM; *f != NUL; ++f)
1795 if (vim_strchr(p_cpo, *f) == NULL
1796 && (t = vim_strchr(si->sn_save_cpo, *f)) != NULL)
1797 // flag was removed, also remove it from the saved 'cpo'
1798 mch_memmove(t, t + 1, STRLEN(t));
1799 for (f = p_cpo; *f != NUL; ++f)
1800 if (vim_strchr((char_u *)CPO_VIM, *f) == NULL
1801 && vim_strchr(si->sn_save_cpo, *f) == NULL)
1802 {
1803 // flag was added, also add it to the saved 'cpo'
1804 t = alloc(STRLEN(si->sn_save_cpo) + 2);
1805 if (t != NULL)
1806 {
1807 *t = *f;
1808 STRCPY(t + 1, si->sn_save_cpo);
1809 vim_free(si->sn_save_cpo);
1810 si->sn_save_cpo = t;
1811 }
1812 }
1813 }
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001814 set_option_value_give_err((char_u *)"cpo",
1815 0L, si->sn_save_cpo, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816 }
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001817 VIM_CLEAR(si->sn_save_cpo);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001819 restore_funccal();
1820# ifdef FEAT_PROFILE
1821 if (do_profiling == PROF_YES)
1822 prof_child_exit(&wait_start); // leaving a child now
1823# endif
Bram Moolenaara2942c72023-01-02 13:41:49 +00001824
1825 KeyTyped = save_KeyTyped;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001826#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001827 current_sctx = save_current_sctx;
1828
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001829 if (cookie.fp != NULL)
1830 fclose(cookie.fp);
1831 if (cookie.source_from_buf)
1832 ga_clear_strings(&cookie.buflines);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001833 vim_free(cookie.nextline);
1834 vim_free(firstline);
1835 convert_setup(&cookie.conv, NULL, NULL);
1836
1837 if (trigger_source_post)
1838 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1839
1840theend:
Bram Moolenaar0af2ecf2022-08-24 17:05:56 +01001841#ifdef FEAT_EVAL
Bram Moolenaar753885b2022-08-24 16:30:36 +01001842 if (sid > 0 && ret_sid != NULL
1843 && fname_not_fixed != NULL && fname_exp != NULL)
1844 {
1845 int not_fixed_sid = find_script_by_name(fname_not_fixed);
1846
1847 // If "fname_not_fixed" is a symlink then we source the linked file.
1848 // If the original name is in the script list we add the ID of the
1849 // script that was actually sourced.
1850 if (SCRIPT_ID_VALID(not_fixed_sid) && not_fixed_sid != sid)
1851 SCRIPT_ITEM(not_fixed_sid)->sn_sourced_sid = sid;
1852 }
Bram Moolenaar0af2ecf2022-08-24 17:05:56 +01001853#endif
Bram Moolenaar753885b2022-08-24 16:30:36 +01001854
1855 vim_free(fname_not_fixed);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001856 vim_free(fname_exp);
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001857 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001858#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001859 estack_compiling = save_estack_compiling;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001860#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001861 return retval;
1862}
1863
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001864 int
1865do_source(
1866 char_u *fname,
1867 int check_other, // check for .vimrc and _vimrc
1868 int is_vimrc, // DOSO_ value
Bram Moolenaar753885b2022-08-24 16:30:36 +01001869 int *ret_sid)
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001870{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001871 return do_source_ext(fname, check_other, is_vimrc, ret_sid, NULL, FALSE);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001872}
1873
1874
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001875#if defined(FEAT_EVAL) || defined(PROTO)
1876
1877/*
1878 * ":scriptnames"
1879 */
1880 void
1881ex_scriptnames(exarg_T *eap)
1882{
1883 int i;
1884
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001885 if (eap->addr_count > 0 || *eap->arg != NUL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001886 {
1887 // :script {scriptId}: edit the script
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001888 if (eap->addr_count > 0 && !SCRIPT_ID_VALID(eap->line2))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001889 emsg(_(e_invalid_argument));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001890 else
1891 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001892 if (eap->addr_count > 0)
1893 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
1894 else
1895 {
1896 expand_env(eap->arg, NameBuff, MAXPATHL);
1897 eap->arg = NameBuff;
1898 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001899 do_exedit(eap, NULL);
1900 }
1901 return;
1902 }
1903
1904 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar6079da72022-01-18 14:16:59 +00001905 {
1906 scriptitem_T *si = SCRIPT_ITEM(i);
1907
1908 if (si->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001909 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01001910 char sourced_buf[20];
1911
Bram Moolenaar6079da72022-01-18 14:16:59 +00001912 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar753885b2022-08-24 16:30:36 +01001913 if (si->sn_sourced_sid > 0)
1914 vim_snprintf(sourced_buf, 20, "->%d", si->sn_sourced_sid);
1915 else
1916 sourced_buf[0] = NUL;
1917 vim_snprintf((char *)IObuff, IOSIZE, "%3d%s%s: %s",
Bram Moolenaar6079da72022-01-18 14:16:59 +00001918 i,
Bram Moolenaar753885b2022-08-24 16:30:36 +01001919 sourced_buf,
Bram Moolenaar6079da72022-01-18 14:16:59 +00001920 si->sn_state == SN_STATE_NOT_LOADED ? " A" : "",
1921 NameBuff);
Bram Moolenaar769f5892022-02-09 14:31:05 +00001922 if (!message_filtered(IObuff))
1923 {
1924 msg_putchar('\n');
1925 msg_outtrans(IObuff);
1926 out_flush(); // output one line at a time
1927 ui_breakcheck();
1928 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001929 }
Bram Moolenaar6079da72022-01-18 14:16:59 +00001930 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001931}
1932
1933# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1934/*
1935 * Fix slashes in the list of script names for 'shellslash'.
1936 */
1937 void
1938scriptnames_slash_adjust(void)
1939{
1940 int i;
1941
1942 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001943 if (SCRIPT_ITEM(i)->sn_name != NULL)
1944 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001945}
1946# endif
1947
1948/*
1949 * Get a pointer to a script name. Used for ":verbose set".
Bram Moolenaar74667062020-12-28 15:41:41 +01001950 * Message appended to "Last set from "
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001951 */
1952 char_u *
1953get_scriptname(scid_T id)
1954{
1955 if (id == SID_MODELINE)
1956 return (char_u *)_("modeline");
1957 if (id == SID_CMDARG)
1958 return (char_u *)_("--cmd argument");
1959 if (id == SID_CARG)
1960 return (char_u *)_("-c argument");
1961 if (id == SID_ENV)
1962 return (char_u *)_("environment variable");
1963 if (id == SID_ERROR)
1964 return (char_u *)_("error handler");
Bram Moolenaar74667062020-12-28 15:41:41 +01001965 if (id == SID_WINLAYOUT)
1966 return (char_u *)_("changed window size");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001967 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001968}
1969
1970# if defined(EXITFREE) || defined(PROTO)
1971 void
1972free_scriptnames(void)
1973{
1974 int i;
1975
1976 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02001977 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001978 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001980 // the variables themselves are cleared in evalvars_clear()
1981 vim_free(si->sn_vars);
1982
1983 vim_free(si->sn_name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001984 free_imports_and_script_vars(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001985 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02001986# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001987 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02001988# endif
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00001989 vim_free(si->sn_autoload_prefix);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001990 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02001991 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001992 ga_clear(&script_items);
1993}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001994
1995 void
1996free_autoload_scriptnames(void)
1997{
1998 ga_clear_strings(&ga_loaded);
1999}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002000# endif
2001
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002002 linenr_T
Bram Moolenaar66250c92020-08-20 15:02:42 +02002003get_sourced_lnum(
2004 char_u *(*fgetline)(int, void *, int, getline_opt_T),
2005 void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002006{
2007 return fgetline == getsourceline
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002008 ? ((source_cookie_T *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002009 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002010}
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002011
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002012/*
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002013 * Return a List of script-local functions defined in the script with id
2014 * 'sid'.
2015 */
2016 static list_T *
2017get_script_local_funcs(scid_T sid)
2018{
2019 hashtab_T *functbl;
2020 hashitem_T *hi;
2021 long_u todo;
2022 list_T *l;
2023
2024 l = list_alloc();
2025 if (l == NULL)
2026 return NULL;
2027
2028 // Iterate through all the functions in the global function hash table
2029 // looking for functions with script ID 'sid'.
2030 functbl = func_tbl_get();
2031 todo = functbl->ht_used;
2032 for (hi = functbl->ht_array; todo > 0; ++hi)
2033 {
2034 ufunc_T *fp;
2035
2036 if (HASHITEM_EMPTY(hi))
2037 continue;
2038
2039 --todo;
2040 fp = HI2UF(hi);
2041
2042 // Add active functions with script id == 'sid'
2043 if (!(fp->uf_flags & FC_DEAD) && (fp->uf_script_ctx.sc_sid == sid))
2044 {
2045 char_u *name;
2046
2047 if (fp->uf_name_exp != NULL)
2048 name = fp->uf_name_exp;
2049 else
2050 name = fp->uf_name;
2051
2052 list_append_string(l, name, -1);
2053 }
2054 }
2055
2056 return l;
2057}
2058
2059/*
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002060 * getscriptinfo() function
2061 */
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002062 void
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002063f_getscriptinfo(typval_T *argvars, typval_T *rettv)
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002064{
2065 int i;
2066 list_T *l;
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002067 char_u *pat = NULL;
2068 regmatch_T regmatch;
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002069 int filterpat = FALSE;
2070 scid_T sid = -1;
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002071
2072 if (rettv_list_alloc(rettv) == FAIL)
2073 return;
2074
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002075 if (check_for_opt_dict_arg(argvars, 0) == FAIL)
2076 return;
2077
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002078 l = rettv->vval.v_list;
2079
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002080 regmatch.regprog = NULL;
2081 regmatch.rm_ic = p_ic;
2082
2083 if (argvars[0].v_type == VAR_DICT)
2084 {
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002085 sid = dict_get_number_def(argvars[0].vval.v_dict, "sid", -1);
2086 if (sid == -1)
2087 {
2088 pat = dict_get_string(argvars[0].vval.v_dict, "name", TRUE);
2089 if (pat != NULL)
2090 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2091 if (regmatch.regprog != NULL)
2092 filterpat = TRUE;
2093 }
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002094 }
2095
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002096 for (i = 1; i <= script_items.ga_len; ++i)
2097 {
2098 scriptitem_T *si = SCRIPT_ITEM(i);
2099 dict_T *d;
2100
2101 if (si->sn_name == NULL)
2102 continue;
2103
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002104 if (filterpat && !vim_regexec(&regmatch, si->sn_name, (colnr_T)0))
2105 continue;
2106
2107 if (sid != -1 && sid != i)
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002108 continue;
2109
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002110 if ((d = dict_alloc()) == NULL
2111 || list_append_dict(l, d) == FAIL
2112 || dict_add_string(d, "name", si->sn_name) == FAIL
2113 || dict_add_number(d, "sid", i) == FAIL
Bram Moolenaar753885b2022-08-24 16:30:36 +01002114 || dict_add_number(d, "sourced", si->sn_sourced_sid) == FAIL
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002115 || dict_add_number(d, "version", si->sn_version) == FAIL
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002116 || dict_add_bool(d, "autoload",
2117 si->sn_state == SN_STATE_NOT_LOADED) == FAIL)
2118 return;
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002119
2120 // When a filter pattern is specified to return information about only
2121 // specific script(s), also add the script-local variables and
2122 // functions.
2123 if (sid != -1)
2124 {
2125 dict_T *var_dict;
2126
2127 var_dict = dict_copy(&si->sn_vars->sv_dict, TRUE, TRUE,
2128 get_copyID());
2129 if (var_dict == NULL
2130 || dict_add_dict(d, "variables", var_dict) == FAIL
2131 || dict_add_list(d, "functions",
2132 get_script_local_funcs(sid)) == FAIL)
2133 return;
2134 }
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002135 }
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002136
2137 vim_regfree(regmatch.regprog);
2138 vim_free(pat);
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002139}
2140
Dominique Pelle748b3082022-01-08 12:41:16 +00002141#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002142
2143 static char_u *
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002144get_one_sourceline(source_cookie_T *sp)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002145{
2146 garray_T ga;
2147 int len;
2148 int c;
2149 char_u *buf;
2150#ifdef USE_CRNL
2151 int has_cr; // CR-LF found
2152#endif
2153 int have_read = FALSE;
2154
2155 // use a growarray to store the sourced line
2156 ga_init2(&ga, 1, 250);
2157
2158 // Loop until there is a finished line (or end-of-file).
2159 ++sp->sourcing_lnum;
2160 for (;;)
2161 {
2162 // make room to read at least 120 (more) characters
2163 if (ga_grow(&ga, 120) == FAIL)
2164 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002165 if (sp->source_from_buf)
2166 {
2167 if (sp->buf_lnum >= sp->buflines.ga_len)
2168 break; // all the lines are processed
2169 ga_concat(&ga, ((char_u **)sp->buflines.ga_data)[sp->buf_lnum]);
2170 sp->buf_lnum++;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01002171 if (ga_grow(&ga, 1) == FAIL)
2172 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002173 buf = (char_u *)ga.ga_data;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01002174 buf[ga.ga_len++] = NUL;
Bram Moolenaar4748c4b2022-05-17 17:47:07 +01002175 len = ga.ga_len;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002176 }
2177 else
2178 {
2179 buf = (char_u *)ga.ga_data;
2180 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
2181 sp->fp) == NULL)
2182 break;
Bram Moolenaar4748c4b2022-05-17 17:47:07 +01002183 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002184 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002185#ifdef USE_CRNL
2186 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
2187 // CTRL-Z by its own, or after a NL.
2188 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
2189 && sp->fileformat == EOL_DOS
2190 && buf[len - 1] == Ctrl_Z)
2191 {
2192 buf[len - 1] = NUL;
2193 break;
2194 }
2195#endif
2196
2197 have_read = TRUE;
2198 ga.ga_len = len;
2199
2200 // If the line was longer than the buffer, read more.
2201 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
2202 continue;
2203
2204 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
2205 {
2206#ifdef USE_CRNL
2207 has_cr = (len >= 2 && buf[len - 2] == '\r');
2208 if (sp->fileformat == EOL_UNKNOWN)
2209 {
2210 if (has_cr)
2211 sp->fileformat = EOL_DOS;
2212 else
2213 sp->fileformat = EOL_UNIX;
2214 }
2215
2216 if (sp->fileformat == EOL_DOS)
2217 {
2218 if (has_cr) // replace trailing CR
2219 {
2220 buf[len - 2] = '\n';
2221 --len;
2222 --ga.ga_len;
2223 }
2224 else // lines like ":map xx yy^M" will have failed
2225 {
2226 if (!sp->error)
2227 {
2228 msg_source(HL_ATTR(HLF_W));
2229 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
2230 }
2231 sp->error = TRUE;
2232 sp->fileformat = EOL_UNIX;
2233 }
2234 }
2235#endif
2236 // The '\n' is escaped if there is an odd number of ^V's just
2237 // before it, first set "c" just before the 'V's and then check
2238 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
2239 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
2240 ;
2241 if ((len & 1) != (c & 1)) // escaped NL, read more
2242 {
2243 ++sp->sourcing_lnum;
2244 continue;
2245 }
2246
2247 buf[len - 1] = NUL; // remove the NL
2248 }
2249
2250 // Check for ^C here now and then, so recursive :so can be broken.
2251 line_breakcheck();
2252 break;
2253 }
2254
2255 if (have_read)
2256 return (char_u *)ga.ga_data;
2257
2258 vim_free(ga.ga_data);
2259 return NULL;
2260}
2261
2262/*
2263 * Get one full line from a sourced file.
2264 * Called by do_cmdline() when it's called from do_source().
2265 *
2266 * Return a pointer to the line in allocated memory.
2267 * Return NULL for end-of-file or some error.
2268 */
2269 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02002270getsourceline(
2271 int c UNUSED,
2272 void *cookie,
2273 int indent UNUSED,
2274 getline_opt_T options)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002275{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002276 source_cookie_T *sp = (source_cookie_T *)cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002277 char_u *line;
2278 char_u *p;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002279 int do_vim9_all = in_vim9script()
2280 && options == GETLINE_CONCAT_ALL;
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002281 int do_bar_cont = do_vim9_all
2282 || options == GETLINE_CONCAT_CONTBAR;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002283
2284#ifdef FEAT_EVAL
2285 // If breakpoints have been added/deleted need to check for it.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002286 if ((sp->dbg_tick < debug_tick) && !sp->source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002287 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002288 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002289 sp->dbg_tick = debug_tick;
2290 }
2291# ifdef FEAT_PROFILE
2292 if (do_profiling == PROF_YES)
2293 script_line_end();
2294# endif
2295#endif
2296
2297 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002298 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002299
2300 // Get current line. If there is a read-ahead line, use it, otherwise get
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002301 // one now. "fp" is NULL if actually using a string.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002302 if (sp->finished || (!sp->source_from_buf && sp->fp == NULL))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002303 line = NULL;
2304 else if (sp->nextline == NULL)
2305 line = get_one_sourceline(sp);
2306 else
2307 {
2308 line = sp->nextline;
2309 sp->nextline = NULL;
2310 ++sp->sourcing_lnum;
2311 }
2312#ifdef FEAT_PROFILE
2313 if (line != NULL && do_profiling == PROF_YES)
2314 script_line_start();
2315#endif
2316
2317 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
2318 // contain the 'C' flag.
Bram Moolenaar66250c92020-08-20 15:02:42 +02002319 if (line != NULL && options != GETLINE_NONE
2320 && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002321 {
Bram Moolenaar5072b472021-06-03 21:56:10 +02002322 int comment_char = in_vim9script() ? '#' : '"';
2323
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002324 // compensate for the one line read-ahead
2325 --sp->sourcing_lnum;
2326
2327 // Get the next line and concatenate it when it starts with a
2328 // backslash. We always need to read the next line, keep it in
2329 // sp->nextline.
2330 /* Also check for a comment in between continuation lines: "\ */
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002331 // Also check for a Vim9 comment, empty line, line starting with '|',
2332 // but not "||".
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002333 sp->nextline = get_one_sourceline(sp);
2334 if (sp->nextline != NULL
2335 && (*(p = skipwhite(sp->nextline)) == '\\'
Bram Moolenaar5072b472021-06-03 21:56:10 +02002336 || (p[0] == comment_char
2337 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002338 || (do_vim9_all && (*p == NUL
2339 || vim9_comment_start(p)))
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002340 || (do_bar_cont && p[0] == '|' && p[1] != '|')))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002341 {
2342 garray_T ga;
2343
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002344 ga_init2(&ga, sizeof(char_u), 400);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002345 ga_concat(&ga, line);
2346 if (*p == '\\')
2347 ga_concat(&ga, p + 1);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002348 else if (*p == '|')
2349 {
2350 ga_concat(&ga, (char_u *)" ");
2351 ga_concat(&ga, p);
2352 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002353 for (;;)
2354 {
2355 vim_free(sp->nextline);
2356 sp->nextline = get_one_sourceline(sp);
2357 if (sp->nextline == NULL)
2358 break;
2359 p = skipwhite(sp->nextline);
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002360 if (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|'))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002361 {
2362 // Adjust the growsize to the current length to speed up
2363 // concatenating many lines.
2364 if (ga.ga_len > 400)
2365 {
2366 if (ga.ga_len > 8000)
2367 ga.ga_growsize = 8000;
2368 else
2369 ga.ga_growsize = ga.ga_len;
2370 }
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002371 if (*p == '\\')
2372 ga_concat(&ga, p + 1);
2373 else
2374 {
2375 ga_concat(&ga, (char_u *)" ");
2376 ga_concat(&ga, p);
2377 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002378 }
Bram Moolenaar5072b472021-06-03 21:56:10 +02002379 else if (!(p[0] == (comment_char)
Bram Moolenaar0f37e352021-06-02 15:28:15 +02002380 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002381 && !(do_vim9_all && (*p == NUL || vim9_comment_start(p))))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002382 break;
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002383 /* drop a # comment or "\ comment line */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002384 }
2385 ga_append(&ga, NUL);
2386 vim_free(line);
2387 line = ga.ga_data;
2388 }
2389 }
2390
2391 if (line != NULL && sp->conv.vc_type != CONV_NONE)
2392 {
2393 char_u *s;
2394
2395 // Convert the encoding of the script line.
2396 s = string_convert(&sp->conv, line, NULL);
2397 if (s != NULL)
2398 {
2399 vim_free(line);
2400 line = s;
2401 }
2402 }
2403
2404#ifdef FEAT_EVAL
2405 // Did we encounter a breakpoint?
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002406 if (!sp->source_from_buf && sp->breakpoint != 0
2407 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002408 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002409 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002410 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002411 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002412 sp->dbg_tick = debug_tick;
2413 }
2414#endif
2415
2416 return line;
2417}
2418
2419/*
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002420 * Returns TRUE if sourcing a script either from a file or a buffer.
2421 * Otherwise returns FALSE.
2422 */
2423 int
2424sourcing_a_script(exarg_T *eap)
2425{
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002426 return (getline_equal(eap->getline, eap->cookie, getsourceline));
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002427}
2428
2429/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002430 * ":scriptencoding": Set encoding conversion for a sourced script.
2431 */
2432 void
2433ex_scriptencoding(exarg_T *eap)
2434{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002435 source_cookie_T *sp;
2436 char_u *name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002437
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002438 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002439 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00002440 emsg(_(e_scriptencoding_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002441 return;
2442 }
2443
2444 if (*eap->arg != NUL)
2445 {
2446 name = enc_canonize(eap->arg);
2447 if (name == NULL) // out of memory
2448 return;
2449 }
2450 else
2451 name = eap->arg;
2452
2453 // Setup for conversion from the specified encoding to 'encoding'.
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002454 sp = (source_cookie_T *)getline_cookie(eap->getline, eap->cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002455 convert_setup(&sp->conv, name, p_enc);
2456
2457 if (name != eap->arg)
2458 vim_free(name);
2459}
2460
2461/*
2462 * ":scriptversion": Set Vim script version for a sourced script.
2463 */
2464 void
2465ex_scriptversion(exarg_T *eap UNUSED)
2466{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002467 int nr;
2468
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002469 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002470 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002471 emsg(_(e_scriptversion_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002472 return;
2473 }
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002474 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002476 emsg(_(e_cannot_use_scriptversion_after_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 return;
2478 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002479
2480 nr = getdigits(&eap->arg);
2481 if (nr == 0 || *eap->arg != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002482 emsg(_(e_invalid_argument));
Bram Moolenaar67979662020-06-20 22:50:47 +02002483 else if (nr > SCRIPT_VERSION_MAX)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002484 semsg(_(e_scriptversion_not_supported_nr), nr);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002485 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002486 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002487 current_sctx.sc_version = nr;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002488#ifdef FEAT_EVAL
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002489 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002490#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002491 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002492}
2493
2494#if defined(FEAT_EVAL) || defined(PROTO)
2495/*
2496 * ":finish": Mark a sourced file as finished.
2497 */
2498 void
2499ex_finish(exarg_T *eap)
2500{
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002501 if (sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002502 do_finish(eap, FALSE);
2503 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00002504 emsg(_(e_finish_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002505}
2506
2507/*
2508 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
2509 * Also called for a pending finish at the ":endtry" or after returning from
2510 * an extra do_cmdline(). "reanimate" is used in the latter case.
2511 */
2512 void
2513do_finish(exarg_T *eap, int reanimate)
2514{
2515 int idx;
2516
2517 if (reanimate)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002518 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002519 eap->cookie))->finished = FALSE;
2520
2521 // Cleanup (and inactivate) conditionals, but stop when a try conditional
2522 // not in its finally clause (which then is to be executed next) is found.
2523 // In this case, make the ":finish" pending for execution at the ":endtry".
2524 // Otherwise, finish normally.
2525 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
2526 if (idx >= 0)
2527 {
2528 eap->cstack->cs_pending[idx] = CSTP_FINISH;
2529 report_make_pending(CSTP_FINISH, NULL);
2530 }
2531 else
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002532 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002533 eap->cookie))->finished = TRUE;
2534}
2535
2536
2537/*
2538 * Return TRUE when a sourced file had the ":finish" command: Don't give error
2539 * message for missing ":endif".
2540 * Return FALSE when not sourcing a file.
2541 */
2542 int
2543source_finished(
Bram Moolenaar66250c92020-08-20 15:02:42 +02002544 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002545 void *cookie)
2546{
2547 return (getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002548 && ((source_cookie_T *)getline_cookie(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002549 fgetline, cookie))->finished);
2550}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002551
2552/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002553 * Find the path of a script below the "autoload" directory.
2554 * Returns NULL if there is no "/autoload/" in the script name.
2555 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01002556 static char_u *
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002557script_name_after_autoload(scriptitem_T *si)
2558{
2559 char_u *p = si->sn_name;
2560 char_u *res = NULL;
2561
2562 for (;;)
2563 {
2564 char_u *n = (char_u *)strstr((char *)p, "autoload");
2565
2566 if (n == NULL)
2567 break;
2568 if (n > p && vim_ispathsep(n[-1]) && vim_ispathsep(n[8]))
2569 res = n + 9;
2570 p = n + 8;
2571 }
2572 return res;
2573}
2574
2575/*
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002576 * For an autoload script "autoload/dir/script.vim" return the prefix
2577 * "dir#script#" in allocated memory.
2578 * Returns NULL if anything is wrong.
2579 */
2580 char_u *
2581get_autoload_prefix(scriptitem_T *si)
2582{
2583 char_u *p = script_name_after_autoload(si);
2584 char_u *prefix;
2585
2586 if (p == NULL)
2587 return NULL;
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002588 prefix = vim_strsave(p);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002589 if (prefix == NULL)
2590 return NULL;
2591
2592 // replace all '/' with '#' and locate ".vim" at the end
2593 for (p = prefix; *p != NUL; p += mb_ptr2len(p))
2594 {
2595 if (vim_ispathsep(*p))
2596 *p = '#';
2597 else if (STRCMP(p, ".vim") == 0)
2598 {
2599 p[0] = '#';
2600 p[1] = NUL;
2601 return prefix;
2602 }
2603 }
2604
2605 // did not find ".vim" at the end
2606 vim_free(prefix);
2607 return NULL;
2608}
2609
2610/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002611 * If in a Vim9 autoload script return "name" with the autoload prefix for the
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002612 * script. If successful the returned name is allocated.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002613 * Otherwise it returns "name" unmodified.
2614 */
2615 char_u *
2616may_prefix_autoload(char_u *name)
2617{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002618 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2619 return name;
2620
2621 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2622
2623 if (si->sn_autoload_prefix == NULL)
2624 return name;
2625
2626 char_u *basename = name;
2627 size_t len;
2628 char_u *res;
2629
2630 if (*name == K_SPECIAL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002631 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002632 char_u *p = vim_strchr(name, '_');
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002633
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002634 // skip over "<SNR>99_"
2635 if (p != NULL)
2636 basename = p + 1;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002637 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002638
2639 len = STRLEN(si->sn_autoload_prefix) + STRLEN(basename) + 2;
2640 res = alloc(len);
2641 if (res == NULL)
2642 return NULL;
2643
2644 vim_snprintf((char *)res, len, "%s%s", si->sn_autoload_prefix, basename);
2645 return res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002646}
2647
2648/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002649 * Return the autoload script name for a function or variable name.
2650 * Returns NULL when out of memory.
2651 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
2652 */
2653 char_u *
2654autoload_name(char_u *name)
2655{
2656 char_u *p, *q = NULL;
2657 char_u *scriptname;
2658
2659 // Get the script file name: replace '#' with '/', append ".vim".
2660 scriptname = alloc(STRLEN(name) + 14);
2661 if (scriptname == NULL)
2662 return NULL;
2663 STRCPY(scriptname, "autoload/");
Bram Moolenaara1773442020-08-12 15:21:22 +02002664 STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002665 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
2666 q = p, ++p)
2667 *p = '/';
2668 STRCPY(q, ".vim");
2669 return scriptname;
2670}
2671
2672/*
2673 * If "name" has a package name try autoloading the script for it.
2674 * Return TRUE if a package was loaded.
2675 */
2676 int
2677script_autoload(
2678 char_u *name,
2679 int reload) // load script again when already loaded
2680{
2681 char_u *p;
2682 char_u *scriptname, *tofree;
2683 int ret = FALSE;
2684 int i;
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002685 int ret_sid;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002686
Bram Moolenaar89445512022-04-14 12:58:23 +01002687 // If the name starts with "<SNR>123_" then "123" is the script ID.
2688 if (name[0] == K_SPECIAL && name[1] == KS_EXTRA && name[2] == KE_SNR)
2689 {
2690 p = name + 3;
2691 ret_sid = (int)getdigits(&p);
2692 if (*p == '_' && SCRIPT_ID_VALID(ret_sid))
2693 {
2694 may_load_script(ret_sid, &ret);
2695 return ret;
2696 }
2697 }
2698
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002699 // If there is no '#' after name[0] there is no package name.
2700 p = vim_strchr(name, AUTOLOAD_CHAR);
2701 if (p == NULL || p == name)
2702 return FALSE;
2703
2704 tofree = scriptname = autoload_name(name);
2705 if (scriptname == NULL)
2706 return FALSE;
2707
2708 // Find the name in the list of previously loaded package names. Skip
2709 // "autoload/", it's always the same.
2710 for (i = 0; i < ga_loaded.ga_len; ++i)
2711 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2712 break;
2713 if (!reload && i < ga_loaded.ga_len)
2714 ret = FALSE; // was loaded already
2715 else
2716 {
2717 // Remember the name if it wasn't loaded already.
2718 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2719 {
2720 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2721 tofree = NULL;
2722 }
2723
2724 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002725 // Use "ret_sid" to avoid loading the same script again.
=?UTF-8?q?Bj=C3=B6rn=20Linse?=223a9502022-01-31 17:26:05 +00002726 if (source_in_path(p_rtp, scriptname, DIP_START, &ret_sid) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002727 ret = TRUE;
2728 }
2729
2730 vim_free(tofree);
2731 return ret;
2732}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002733#endif