blob: da6bdd010751b116c46aeb1360a28a20af8d1e7e [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.
59 if (ga_grow(&exestack, 1) == OK)
60 {
61 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
62 entry->es_type = type;
63 entry->es_name = name;
64 entry->es_lnum = lnum;
Bram Moolenaar09d46402019-12-29 23:53:01 +010065#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010066 entry->es_info.ufunc = NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +010067#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010068 ++exestack.ga_len;
69 return entry;
70 }
71 return NULL;
72}
73
Bram Moolenaar09d46402019-12-29 23:53:01 +010074#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010075/*
76 * Add a user function to the execution stack.
77 */
Bram Moolenaarc620c052020-07-08 15:16:19 +020078 estack_T *
Bram Moolenaar25e0f582020-05-25 22:36:50 +020079estack_push_ufunc(ufunc_T *ufunc, long lnum)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010080{
Bram Moolenaar25e0f582020-05-25 22:36:50 +020081 estack_T *entry = estack_push(ETYPE_UFUNC,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010082 ufunc->uf_name_exp != NULL
83 ? ufunc->uf_name_exp : ufunc->uf_name, lnum);
84 if (entry != NULL)
85 entry->es_info.ufunc = ufunc;
Bram Moolenaarc620c052020-07-08 15:16:19 +020086 return entry;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010087}
Bram Moolenaar25e0f582020-05-25 22:36:50 +020088
89/*
90 * Return TRUE if "ufunc" with "lnum" is already at the top of the exe stack.
91 */
92 int
93estack_top_is_ufunc(ufunc_T *ufunc, long lnum)
94{
95 estack_T *entry;
96
97 if (exestack.ga_len == 0)
98 return FALSE;
99 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
100 return entry->es_type == ETYPE_UFUNC
101 && STRCMP( entry->es_name, ufunc->uf_name_exp != NULL
102 ? ufunc->uf_name_exp : ufunc->uf_name) == 0
103 && entry->es_lnum == lnum;
104}
Bram Moolenaar09d46402019-12-29 23:53:01 +0100105#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100106
107/*
Bram Moolenaarc620c052020-07-08 15:16:19 +0200108 * Take an item off of the execution stack and return it.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100109 */
Bram Moolenaarc620c052020-07-08 15:16:19 +0200110 estack_T *
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100111estack_pop(void)
112{
Bram Moolenaarc620c052020-07-08 15:16:19 +0200113 if (exestack.ga_len == 0)
114 return NULL;
115 --exestack.ga_len;
116 return ((estack_T *)exestack.ga_data) + exestack.ga_len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100117}
118
119/*
Bram Moolenaar89445512022-04-14 12:58:23 +0100120 * Get the current value for "which" in allocated memory.
LemonBoy6013d002022-04-09 21:42:10 +0100121 * "which" is ESTACK_SFILE for <sfile>, ESTACK_STACK for <stack> or
122 * ESTACK_SCRIPT for <script>.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100123 */
124 char_u *
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200125estack_sfile(estack_arg_T which UNUSED)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100126{
Bram Moolenaar85b09572019-12-30 10:57:00 +0100127 estack_T *entry;
128#ifdef FEAT_EVAL
Bram Moolenaara5d04232020-07-26 15:37:02 +0200129 garray_T ga;
Bram Moolenaar4d7a2482020-01-06 19:53:43 +0100130 size_t len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100131 int idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200132 etype_T last_type = ETYPE_SCRIPT;
133 char *type_name;
Bram Moolenaar85b09572019-12-30 10:57:00 +0100134#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100135
136 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100137#ifdef FEAT_EVAL
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200138 if (which == ESTACK_SFILE && entry->es_type != ETYPE_UFUNC)
Bram Moolenaar09d46402019-12-29 23:53:01 +0100139#endif
Bram Moolenaara5d04232020-07-26 15:37:02 +0200140 {
141 if (entry->es_name == NULL)
142 return NULL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100143 return vim_strsave(entry->es_name);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200144 }
Bram Moolenaar09d46402019-12-29 23:53:01 +0100145#ifdef FEAT_EVAL
Bram Moolenaarc4492712021-11-22 15:37:15 +0000146 // expand('<sfile>') works in a function for backwards compatibility, but
147 // may give an unexpected result. Disallow it in Vim 9 script.
148 if (which == ESTACK_SFILE && in_vim9script())
149 {
150 int save_emsg_off = emsg_off;
151
152 if (emsg_off == 1)
153 // f_expand() silences errors but we do want this one
154 emsg_off = 0;
155 emsg(_(e_cannot_expand_sfile_in_vim9_function));
156 emsg_off = save_emsg_off;
157 return NULL;
158 }
159
LemonBoyeca7c602022-04-14 15:39:43 +0100160 // If evaluated in a function or autocommand, return the path of the script
161 // where it is defined, at script level the current script path is returned
LemonBoy6013d002022-04-09 21:42:10 +0100162 // instead.
163 if (which == ESTACK_SCRIPT)
164 {
LemonBoyeca7c602022-04-14 15:39:43 +0100165 // Walk the stack backwards, starting from the current frame.
166 for (idx = exestack.ga_len - 1; idx >= 0; --idx, --entry)
LemonBoy6013d002022-04-09 21:42:10 +0100167 {
zeertzjqa2471422022-08-23 19:26:05 +0100168 if (entry->es_type == ETYPE_UFUNC || entry->es_type == ETYPE_AUCMD)
LemonBoy6013d002022-04-09 21:42:10 +0100169 {
zeertzjqa2471422022-08-23 19:26:05 +0100170 sctx_T *def_ctx = entry->es_type == ETYPE_UFUNC
171 ? &entry->es_info.ufunc->uf_script_ctx
172 : acp_script_ctx(entry->es_info.aucmd);
LemonBoy6013d002022-04-09 21:42:10 +0100173
zeertzjqa2471422022-08-23 19:26:05 +0100174 return def_ctx->sc_sid > 0
175 ? vim_strsave(SCRIPT_ITEM(def_ctx->sc_sid)->sn_name)
176 : NULL;
LemonBoyeca7c602022-04-14 15:39:43 +0100177 }
178 else if (entry->es_type == ETYPE_SCRIPT)
LemonBoyeca7c602022-04-14 15:39:43 +0100179 return vim_strsave(entry->es_name);
LemonBoy6013d002022-04-09 21:42:10 +0100180 }
181 return NULL;
182 }
183
Bram Moolenaara5d04232020-07-26 15:37:02 +0200184 // Give information about each stack entry up to the root.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100185 // For a function we compose the call stack, as it was done in the past:
186 // "function One[123]..Two[456]..Three"
Bram Moolenaara5d04232020-07-26 15:37:02 +0200187 ga_init2(&ga, sizeof(char), 100);
188 for (idx = 0; idx < exestack.ga_len; ++idx)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100189 {
190 entry = ((estack_T *)exestack.ga_data) + idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200191 if (entry->es_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100192 {
Bram Moolenaara810db32020-09-11 17:59:23 +0200193 long lnum = 0;
194 char *dots;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200195
Bram Moolenaara5d04232020-07-26 15:37:02 +0200196 len = STRLEN(entry->es_name) + 15;
197 type_name = "";
198 if (entry->es_type != last_type)
199 {
200 switch (entry->es_type)
201 {
202 case ETYPE_SCRIPT: type_name = "script "; break;
203 case ETYPE_UFUNC: type_name = "function "; break;
204 default: type_name = ""; break;
205 }
206 last_type = entry->es_type;
207 }
208 len += STRLEN(type_name);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200209 if (ga_grow(&ga, (int)len) == FAIL)
Bram Moolenaara5d04232020-07-26 15:37:02 +0200210 break;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200211 if (idx == exestack.ga_len - 1)
212 lnum = which == ESTACK_STACK ? SOURCING_LNUM : 0;
213 else
214 lnum = entry->es_lnum;
Bram Moolenaara810db32020-09-11 17:59:23 +0200215 dots = idx == exestack.ga_len - 1 ? "" : "..";
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200216 if (lnum == 0)
217 // For the bottom entry of <sfile>: do not add the line number,
218 // it is used in <slnum>. Also leave it out when the number is
219 // not set.
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200220 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s%s",
Bram Moolenaara810db32020-09-11 17:59:23 +0200221 type_name, entry->es_name, dots);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200222 else
Bram Moolenaara810db32020-09-11 17:59:23 +0200223 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s[%ld]%s",
224 type_name, entry->es_name, lnum, dots);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200225 ga.ga_len += (int)STRLEN((char *)ga.ga_data + ga.ga_len);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100226 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100227 }
228
Bram Moolenaara5d04232020-07-26 15:37:02 +0200229 return (char_u *)ga.ga_data;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100230#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100231}
232
233/*
zeertzjq3770f4c2023-01-22 18:38:51 +0000234 * Get DIP_ flags from the [what] argument of a :runtime command.
235 * "*argp" is advanced to after the [what] argument.
236 */
237 static int
238get_runtime_cmd_flags(char_u **argp)
239{
240 char_u *arg = *argp;
241 char_u *p = skiptowhite(arg);
242 int what_len = (int)(p - arg);
243
244 if (what_len == 0)
245 return 0;
246
247 if (STRNCMP(arg, "START", what_len) == 0)
248 {
249 *argp = skipwhite(arg + what_len);
250 return DIP_START + DIP_NORTP;
251 }
252 if (STRNCMP(arg, "OPT", what_len) == 0)
253 {
254 *argp = skipwhite(arg + what_len);
255 return DIP_OPT + DIP_NORTP;
256 }
257 if (STRNCMP(arg, "PACK", what_len) == 0)
258 {
259 *argp = skipwhite(arg + what_len);
260 return DIP_START + DIP_OPT + DIP_NORTP;
261 }
262 if (STRNCMP(arg, "ALL", what_len) == 0)
263 {
264 *argp = skipwhite(arg + what_len);
265 return DIP_START + DIP_OPT;
266 }
267
268 return 0;
269}
270
271/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200272 * ":runtime [what] {name}"
273 */
274 void
275ex_runtime(exarg_T *eap)
276{
277 char_u *arg = eap->arg;
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200278 int flags = eap->forceit ? DIP_ALL : 0;
279
zeertzjq3770f4c2023-01-22 18:38:51 +0000280 flags += get_runtime_cmd_flags(&arg);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200281 source_runtime(arg, flags);
282}
283
zeertzjq3770f4c2023-01-22 18:38:51 +0000284static int runtime_expand_flags;
285
286/*
287 * Set the completion context for the :runtime command.
288 */
289 void
290set_context_in_runtime_cmd(expand_T *xp, char_u *arg)
291{
292 runtime_expand_flags = DIP_KEEPEXT + get_runtime_cmd_flags(&arg);
293 xp->xp_context = EXPAND_RUNTIME;
294 xp->xp_pattern = arg;
295}
296
297/*
298 * Handle command line completion for :runtime command.
299 */
300 int
301expand_runtime_cmd(char_u *pat, int *numMatches, char_u ***matches)
302{
303 char *directories[] = {"", NULL};
304 return ExpandRTDir(pat, runtime_expand_flags, numMatches, matches,
305 directories);
306}
307
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200308 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100309source_callback(char_u *fname, void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200310{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100311 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200312}
313
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000314#ifdef FEAT_EVAL
315/*
316 * Find an already loaded script "name".
317 * If found returns its script ID. If not found returns -1.
318 */
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100319 int
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000320find_script_by_name(char_u *name)
321{
322 int sid;
323 scriptitem_T *si;
324
325 for (sid = script_items.ga_len; sid > 0; --sid)
326 {
327 // We used to check inode here, but that doesn't work:
328 // - If a script is edited and written, it may get a different
329 // inode number, even though to the user it is the same script.
330 // - If a script is deleted and another script is written, with a
331 // different name, the inode may be re-used.
332 si = SCRIPT_ITEM(sid);
333 if (si->sn_name != NULL && fnamecmp(si->sn_name, name) == 0)
334 return sid;
335 }
336 return -1;
337}
338
339/*
340 * Add a new scriptitem with all items initialized.
341 * When running out of memory "error" is set to FAIL.
342 * Returns the script ID.
343 */
344 static int
345get_new_scriptitem(int *error)
346{
347 static scid_T last_current_SID = 0;
348 int sid = ++last_current_SID;
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000349 scriptitem_T *si = NULL;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000350
351 if (ga_grow(&script_items, (int)(sid - script_items.ga_len)) == FAIL)
352 {
353 *error = FAIL;
354 return sid;
355 }
356 while (script_items.ga_len < sid)
357 {
358 si = ALLOC_CLEAR_ONE(scriptitem_T);
359 if (si == NULL)
360 {
361 *error = FAIL;
362 return sid;
363 }
364 ++script_items.ga_len;
365 SCRIPT_ITEM(script_items.ga_len) = si;
366 si->sn_name = NULL;
367 si->sn_version = 1;
368
369 // Allocate the local script variables to use for this script.
370 new_script_vars(script_items.ga_len);
371 ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
372 hash_init(&si->sn_all_vars.dv_hashtab);
373 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
374 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
375# ifdef FEAT_PROFILE
376 si->sn_prof_on = FALSE;
377# endif
378 }
379
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000380 // "si" can't be NULL, check only to avoid a compiler warning
381 if (si != NULL)
382 // Used to check script variable index is still valid.
383 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000384
385 return sid;
386}
387
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100388 int
389get_new_scriptitem_for_fname(int *error, char_u *fname)
390{
391 int sid = get_new_scriptitem(error);
392
393 if (*error == OK)
394 {
395 scriptitem_T *si = SCRIPT_ITEM(sid);
396
397 si->sn_name = vim_strsave(fname);
398 si->sn_state = SN_STATE_NOT_LOADED;
399 }
400 return sid;
401}
402
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000403 static void
404find_script_callback(char_u *fname, void *cookie)
405{
406 int sid;
407 int error = OK;
408 int *ret_sid = cookie;
409
410 sid = find_script_by_name(fname);
411 if (sid < 0)
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000412 // script does not exist yet, create a new scriptitem
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100413 sid = get_new_scriptitem_for_fname(&error, fname);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000414 *ret_sid = sid;
415}
416#endif
417
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200418/*
419 * Find the file "name" in all directories in "path" and invoke
420 * "callback(fname, cookie)".
421 * "name" can contain wildcards.
422 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
423 * When "flags" has DIP_DIR: find directories instead of files.
424 * When "flags" has DIP_ERR: give an error message if there is no match.
425 *
426 * return FAIL when no file could be sourced, OK otherwise.
427 */
428 int
429do_in_path(
430 char_u *path,
431 char_u *name,
432 int flags,
433 void (*callback)(char_u *fname, void *ck),
434 void *cookie)
435{
436 char_u *rtp;
437 char_u *np;
438 char_u *buf;
439 char_u *rtp_copy;
440 char_u *tail;
441 int num_files;
442 char_u **files;
443 int i;
444 int did_one = FALSE;
445#ifdef AMIGA
446 struct Process *proc = (struct Process *)FindTask(0L);
447 APTR save_winptr = proc->pr_WindowPtr;
448
449 // Avoid a requester here for a volume that doesn't exist.
450 proc->pr_WindowPtr = (APTR)-1L;
451#endif
452
453 // Make a copy of 'runtimepath'. Invoking the callback may change the
454 // value.
455 rtp_copy = vim_strsave(path);
456 buf = alloc(MAXPATHL);
457 if (buf != NULL && rtp_copy != NULL)
458 {
Bram Moolenaar647a5302020-05-03 17:01:24 +0200459 if (p_verbose > 10 && name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200460 {
461 verbose_enter();
462 smsg(_("Searching for \"%s\" in \"%s\""),
463 (char *)name, (char *)path);
464 verbose_leave();
465 }
466
467 // Loop over all entries in 'runtimepath'.
468 rtp = rtp_copy;
469 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
470 {
471 size_t buflen;
472
473 // Copy the path from 'runtimepath' to buf[].
474 copy_option_part(&rtp, buf, MAXPATHL, ",");
475 buflen = STRLEN(buf);
476
477 // Skip after or non-after directories.
478 if (flags & (DIP_NOAFTER | DIP_AFTER))
479 {
480 int is_after = buflen >= 5
481 && STRCMP(buf + buflen - 5, "after") == 0;
482
483 if ((is_after && (flags & DIP_NOAFTER))
484 || (!is_after && (flags & DIP_AFTER)))
485 continue;
486 }
487
488 if (name == NULL)
489 {
490 (*callback)(buf, (void *) &cookie);
491 if (!did_one)
492 did_one = (cookie == NULL);
493 }
494 else if (buflen + STRLEN(name) + 2 < MAXPATHL)
495 {
496 add_pathsep(buf);
497 tail = buf + STRLEN(buf);
498
499 // Loop over all patterns in "name"
500 np = name;
501 while (*np != NUL && ((flags & DIP_ALL) || !did_one))
502 {
503 // Append the pattern from "name" to buf[].
504 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
505 "\t ");
506
Bram Moolenaar647a5302020-05-03 17:01:24 +0200507 if (p_verbose > 10)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200508 {
509 verbose_enter();
510 smsg(_("Searching for \"%s\""), buf);
511 verbose_leave();
512 }
513
514 // Expand wildcards, invoke the callback for each match.
515 if (gen_expand_wildcards(1, &buf, &num_files, &files,
516 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
517 {
518 for (i = 0; i < num_files; ++i)
519 {
520 (*callback)(files[i], cookie);
521 did_one = TRUE;
522 if (!(flags & DIP_ALL))
523 break;
524 }
525 FreeWild(num_files, files);
526 }
527 }
528 }
529 }
530 }
531 vim_free(buf);
532 vim_free(rtp_copy);
533 if (!did_one && name != NULL)
534 {
535 char *basepath = path == p_rtp ? "runtimepath" : "packpath";
536
537 if (flags & DIP_ERR)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000538 semsg(_(e_directory_not_found_in_str_str), basepath, name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200539 else if (p_verbose > 0)
540 {
541 verbose_enter();
542 smsg(_("not found in '%s': \"%s\""), basepath, name);
543 verbose_leave();
544 }
545 }
546
547#ifdef AMIGA
548 proc->pr_WindowPtr = save_winptr;
549#endif
550
551 return did_one ? OK : FAIL;
552}
553
554/*
555 * Find "name" in "path". When found, invoke the callback function for
556 * it: callback(fname, "cookie")
557 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first
558 * one is used.
559 * Returns OK when at least one match found, FAIL otherwise.
560 *
561 * If "name" is NULL calls callback for each entry in "path". Cookie is
562 * passed by reference in this case, setting it to NULL indicates that callback
563 * has done its job.
564 */
565 static int
566do_in_path_and_pp(
567 char_u *path,
568 char_u *name,
569 int flags,
570 void (*callback)(char_u *fname, void *ck),
571 void *cookie)
572{
573 int done = FAIL;
574 char_u *s;
575 int len;
576 char *start_dir = "pack/*/start/*/%s";
577 char *opt_dir = "pack/*/opt/*/%s";
578
579 if ((flags & DIP_NORTP) == 0)
580 done = do_in_path(path, name, flags, callback, cookie);
581
582 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
583 {
584 len = (int)(STRLEN(start_dir) + STRLEN(name));
585 s = alloc(len);
586 if (s == NULL)
587 return FAIL;
588 vim_snprintf((char *)s, len, start_dir, name);
589 done = do_in_path(p_pp, s, flags, callback, cookie);
590 vim_free(s);
591 }
592
593 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
594 {
595 len = (int)(STRLEN(opt_dir) + STRLEN(name));
596 s = alloc(len);
597 if (s == NULL)
598 return FAIL;
599 vim_snprintf((char *)s, len, opt_dir, name);
600 done = do_in_path(p_pp, s, flags, callback, cookie);
601 vim_free(s);
602 }
603
604 return done;
605}
606
607/*
608 * Just like do_in_path_and_pp(), using 'runtimepath' for "path".
609 */
610 int
611do_in_runtimepath(
612 char_u *name,
613 int flags,
614 void (*callback)(char_u *fname, void *ck),
615 void *cookie)
616{
617 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
618}
619
620/*
621 * Source the file "name" from all directories in 'runtimepath'.
622 * "name" can contain wildcards.
623 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
624 *
625 * return FAIL when no file could be sourced, OK otherwise.
626 */
627 int
628source_runtime(char_u *name, int flags)
629{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100630 return source_in_path(p_rtp, name, flags, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200631}
632
633/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000634 * Just like source_runtime(), but use "path" instead of 'runtimepath'
635 * and return the script ID in "ret_sid".
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200636 */
637 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200639{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100640 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200641}
642
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200643#if defined(FEAT_EVAL) || defined(PROTO)
644
645/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000646 * Find "name" in 'runtimepath'. If found a new scriptitem is created for it
647 * and it's script ID is returned.
648 * If not found returns -1.
649 */
650 int
651find_script_in_rtp(char_u *name)
652{
653 int sid = -1;
654
655 (void)do_in_path_and_pp(p_rtp, name, DIP_NOAFTER,
656 find_script_callback, &sid);
657 return sid;
658}
659
660/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200661 * Expand wildcards in "pat" and invoke do_source() for each match.
662 */
663 static void
664source_all_matches(char_u *pat)
665{
666 int num_files;
667 char_u **files;
668 int i;
669
670 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK)
671 {
672 for (i = 0; i < num_files; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200674 FreeWild(num_files, files);
675 }
676}
677
678/*
679 * Add the package directory to 'runtimepath'.
680 */
681 static int
682add_pack_dir_to_rtp(char_u *fname)
683{
684 char_u *p4, *p3, *p2, *p1, *p;
685 char_u *entry;
686 char_u *insp = NULL;
687 int c;
688 char_u *new_rtp;
689 int keep;
690 size_t oldlen;
691 size_t addlen;
692 size_t new_rtp_len;
693 char_u *afterdir = NULL;
694 size_t afterlen = 0;
695 char_u *after_insp = NULL;
696 char_u *ffname = NULL;
697 size_t fname_len;
698 char_u *buf = NULL;
699 char_u *rtp_ffname;
700 int match;
701 int retval = FAIL;
702
703 p4 = p3 = p2 = p1 = get_past_head(fname);
704 for (p = p1; *p; MB_PTR_ADV(p))
705 if (vim_ispathsep_nocolon(*p))
706 {
707 p4 = p3; p3 = p2; p2 = p1; p1 = p;
708 }
709
710 // now we have:
711 // rtp/pack/name/start/name
712 // p4 p3 p2 p1
713 //
714 // find the part up to "pack" in 'runtimepath'
715 c = *++p4; // append pathsep in order to expand symlink
716 *p4 = NUL;
717 ffname = fix_fname(fname);
718 *p4 = c;
719 if (ffname == NULL)
720 return FAIL;
721
722 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
723 // Also stop at the first "after" directory.
724 fname_len = STRLEN(ffname);
725 buf = alloc(MAXPATHL);
726 if (buf == NULL)
727 goto theend;
728 for (entry = p_rtp; *entry != NUL; )
729 {
730 char_u *cur_entry = entry;
731
732 copy_option_part(&entry, buf, MAXPATHL, ",");
733 if (insp == NULL)
734 {
735 add_pathsep(buf);
736 rtp_ffname = fix_fname(buf);
737 if (rtp_ffname == NULL)
738 goto theend;
739 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
740 vim_free(rtp_ffname);
741 if (match)
742 // Insert "ffname" after this entry (and comma).
743 insp = entry;
744 }
745
746 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
747 && p > buf
748 && vim_ispathsep(p[-1])
749 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
750 {
751 if (insp == NULL)
752 // Did not find "ffname" before the first "after" directory,
753 // insert it before this entry.
754 insp = cur_entry;
755 after_insp = cur_entry;
756 break;
757 }
758 }
759
760 if (insp == NULL)
761 // Both "fname" and "after" not found, append at the end.
762 insp = p_rtp + STRLEN(p_rtp);
763
764 // check if rtp/pack/name/start/name/after exists
765 afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
766 if (afterdir != NULL && mch_isdir(afterdir))
767 afterlen = STRLEN(afterdir) + 1; // add one for comma
768
769 oldlen = STRLEN(p_rtp);
770 addlen = STRLEN(fname) + 1; // add one for comma
771 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
772 if (new_rtp == NULL)
773 goto theend;
774
775 // We now have 'rtp' parts: {keep}{keep_after}{rest}.
776 // Create new_rtp, first: {keep},{fname}
777 keep = (int)(insp - p_rtp);
778 mch_memmove(new_rtp, p_rtp, keep);
779 new_rtp_len = keep;
780 if (*insp == NUL)
781 new_rtp[new_rtp_len++] = ','; // add comma before
782 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
783 new_rtp_len += addlen - 1;
784 if (*insp != NUL)
785 new_rtp[new_rtp_len++] = ','; // add comma after
786
787 if (afterlen > 0 && after_insp != NULL)
788 {
789 int keep_after = (int)(after_insp - p_rtp);
790
791 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
792 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
793 keep_after - keep);
794 new_rtp_len += keep_after - keep;
795 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
796 new_rtp_len += afterlen - 1;
797 new_rtp[new_rtp_len++] = ',';
798 keep = keep_after;
799 }
800
801 if (p_rtp[keep] != NUL)
802 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
803 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
804 else
805 new_rtp[new_rtp_len] = NUL;
806
807 if (afterlen > 0 && after_insp == NULL)
808 {
809 // Append afterdir when "after" was not found:
810 // {keep},{fname}{rest},{afterdir}
811 STRCAT(new_rtp, ",");
812 STRCAT(new_rtp, afterdir);
813 }
814
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100815 set_option_value_give_err((char_u *)"rtp", 0L, new_rtp, 0);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200816 vim_free(new_rtp);
817 retval = OK;
818
819theend:
820 vim_free(buf);
821 vim_free(ffname);
822 vim_free(afterdir);
823 return retval;
824}
825
826/*
827 * Load scripts in "plugin" and "ftdetect" directories of the package.
828 */
829 static int
830load_pack_plugin(char_u *fname)
831{
832 static char *plugpat = "%s/plugin/**/*.vim";
833 static char *ftpat = "%s/ftdetect/*.vim";
834 int len;
835 char_u *ffname = fix_fname(fname);
836 char_u *pat = NULL;
837 int retval = FAIL;
838
839 if (ffname == NULL)
840 return FAIL;
841 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
842 pat = alloc(len);
843 if (pat == NULL)
844 goto theend;
845 vim_snprintf((char *)pat, len, plugpat, ffname);
846 source_all_matches(pat);
847
848 {
849 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
850
851 // If runtime/filetype.vim wasn't loaded yet, the scripts will be
852 // found when it loads.
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100853 if (cmd != NULL && eval_to_number(cmd, FALSE) > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200854 {
855 do_cmdline_cmd((char_u *)"augroup filetypedetect");
856 vim_snprintf((char *)pat, len, ftpat, ffname);
857 source_all_matches(pat);
858 do_cmdline_cmd((char_u *)"augroup END");
859 }
860 vim_free(cmd);
861 }
862 vim_free(pat);
863 retval = OK;
864
865theend:
866 vim_free(ffname);
867 return retval;
868}
869
870// used for "cookie" of add_pack_plugin()
871static int APP_ADD_DIR;
872static int APP_LOAD;
873static int APP_BOTH;
874
875 static void
876add_pack_plugin(char_u *fname, void *cookie)
877{
878 if (cookie != &APP_LOAD)
879 {
880 char_u *buf = alloc(MAXPATHL);
881 char_u *p;
882 int found = FALSE;
883
884 if (buf == NULL)
885 return;
886 p = p_rtp;
887 while (*p != NUL)
888 {
889 copy_option_part(&p, buf, MAXPATHL, ",");
890 if (pathcmp((char *)buf, (char *)fname, -1) == 0)
891 {
892 found = TRUE;
893 break;
894 }
895 }
896 vim_free(buf);
897 if (!found)
898 // directory is not yet in 'runtimepath', add it
899 if (add_pack_dir_to_rtp(fname) == FAIL)
900 return;
901 }
902
903 if (cookie != &APP_ADD_DIR)
904 load_pack_plugin(fname);
905}
906
907/*
908 * Add all packages in the "start" directory to 'runtimepath'.
909 */
910 void
911add_pack_start_dirs(void)
912{
913 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
914 add_pack_plugin, &APP_ADD_DIR);
915}
916
917/*
918 * Load plugins from all packages in the "start" directory.
919 */
920 void
921load_start_packages(void)
922{
923 did_source_packages = TRUE;
924 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
925 add_pack_plugin, &APP_LOAD);
926}
927
928/*
929 * ":packloadall"
930 * Find plugins in the package directories and source them.
931 */
932 void
933ex_packloadall(exarg_T *eap)
934{
935 if (!did_source_packages || eap->forceit)
936 {
937 // First do a round to add all directories to 'runtimepath', then load
938 // the plugins. This allows for plugins to use an autoload directory
939 // of another plugin.
940 add_pack_start_dirs();
941 load_start_packages();
942 }
943}
944
945/*
946 * ":packadd[!] {name}"
947 */
948 void
949ex_packadd(exarg_T *eap)
950{
951 static char *plugpat = "pack/*/%s/%s";
952 int len;
953 char *pat;
954 int round;
955 int res = OK;
956
957 // Round 1: use "start", round 2: use "opt".
958 for (round = 1; round <= 2; ++round)
959 {
960 // Only look under "start" when loading packages wasn't done yet.
961 if (round == 1 && did_source_packages)
962 continue;
963
964 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
965 pat = alloc(len);
966 if (pat == NULL)
967 return;
968 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
969 // The first round don't give a "not found" error, in the second round
970 // only when nothing was found in the first round.
971 res = do_in_path(p_pp, (char_u *)pat,
972 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
973 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
974 vim_free(pat);
975 }
976}
977#endif
978
979/*
Bram Moolenaar26262f82019-09-04 20:59:15 +0200980 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
981 * list of file names in allocated memory.
982 */
983 void
984remove_duplicates(garray_T *gap)
985{
986 int i;
987 int j;
988 char_u **fnames = (char_u **)gap->ga_data;
989
990 sort_strings(fnames, gap->ga_len);
991 for (i = gap->ga_len - 1; i > 0; --i)
992 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
993 {
994 vim_free(fnames[i]);
995 for (j = i + 1; j < gap->ga_len; ++j)
996 fnames[j - 1] = fnames[j];
997 --gap->ga_len;
998 }
999}
1000
1001/*
zeertzjq3770f4c2023-01-22 18:38:51 +00001002 * Expand runtime file names.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001003 * Search from 'runtimepath':
1004 * 'runtimepath'/{dirnames}/{pat}.vim
zeertzjq3770f4c2023-01-22 18:38:51 +00001005 * When "flags" has DIP_START: search also from "start" of 'packpath':
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001006 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
zeertzjq3770f4c2023-01-22 18:38:51 +00001007 * When "flags" has DIP_OPT: search also from "opt" of 'packpath':
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001008 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
1009 * "dirnames" is an array with one or more directory names.
1010 */
1011 int
1012ExpandRTDir(
1013 char_u *pat,
1014 int flags,
1015 int *num_file,
1016 char_u ***file,
1017 char *dirnames[])
1018{
1019 char_u *s;
1020 char_u *e;
1021 char_u *match;
1022 garray_T ga;
1023 int i;
1024 int pat_len;
1025
1026 *num_file = 0;
1027 *file = NULL;
1028 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001029 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001030
1031 for (i = 0; dirnames[i] != NULL; ++i)
1032 {
zeertzjq3770f4c2023-01-22 18:38:51 +00001033 size_t buf_len = STRLEN(dirnames[i]) + pat_len + 22;
1034 char *buf = alloc(buf_len);
roota6759382023-01-21 21:56:06 +00001035 if (buf == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001036 {
1037 ga_clear_strings(&ga);
1038 return FAIL;
1039 }
zeertzjq3770f4c2023-01-22 18:38:51 +00001040 char *tail = buf + 15;
1041 size_t tail_buflen = buf_len - 15;
1042 int glob_flags = 0;
1043 int expand_dirs = FALSE;
1044
1045 if (*(dirnames[i]) == NUL) // empty dir used for :runtime
1046 vim_snprintf(tail, tail_buflen, "%s*.vim", pat);
roota6759382023-01-21 21:56:06 +00001047 else
zeertzjq3770f4c2023-01-22 18:38:51 +00001048 vim_snprintf(tail, tail_buflen, "%s/%s*.vim", dirnames[i], pat);
1049
1050expand:
1051 if ((flags & DIP_NORTP) == 0)
1052 globpath(p_rtp, (char_u *)tail, &ga, glob_flags, expand_dirs);
1053
1054 if (flags & DIP_START)
roota6759382023-01-21 21:56:06 +00001055 {
zeertzjq3770f4c2023-01-22 18:38:51 +00001056 memcpy(tail - 15, "pack/*/start/*/", 15);
1057 globpath(p_pp, (char_u *)tail - 15, &ga, glob_flags, expand_dirs);
roota6759382023-01-21 21:56:06 +00001058 }
zeertzjq3770f4c2023-01-22 18:38:51 +00001059
1060 if (flags & DIP_OPT)
1061 {
1062 memcpy(tail - 13, "pack/*/opt/*/", 13);
1063 globpath(p_pp, (char_u *)tail - 13, &ga, glob_flags, expand_dirs);
1064 }
1065
1066 if (*(dirnames[i]) == NUL && !expand_dirs)
1067 {
1068 // expand dir names in another round
1069 vim_snprintf(tail, tail_buflen, "%s*", pat);
1070 glob_flags = WILD_ADD_SLASH;
1071 expand_dirs = TRUE;
1072 goto expand;
1073 }
1074
roota6759382023-01-21 21:56:06 +00001075 vim_free(buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001076 }
1077
zeertzjq3770f4c2023-01-22 18:38:51 +00001078 int pat_pathsep_cnt = 0;
1079 for (i = 0; i < pat_len; ++i)
1080 if (vim_ispathsep(pat[i]))
1081 ++pat_pathsep_cnt;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001082
1083 for (i = 0; i < ga.ga_len; ++i)
1084 {
1085 match = ((char_u **)ga.ga_data)[i];
1086 s = match;
1087 e = s + STRLEN(s);
zeertzjq3770f4c2023-01-22 18:38:51 +00001088 if (e - 4 > s && (flags & DIP_KEEPEXT) == 0
1089 && STRNICMP(e - 4, ".vim", 4) == 0)
roota6759382023-01-21 21:56:06 +00001090 {
zeertzjq3770f4c2023-01-22 18:38:51 +00001091 e -= 4;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001092 *e = NUL;
roota6759382023-01-21 21:56:06 +00001093 }
1094
zeertzjq3770f4c2023-01-22 18:38:51 +00001095 int match_pathsep_cnt = (e > s && e[-1] == '/') ? -1 : 0;
1096 for (s = e; s > match; MB_PTR_BACK(match, s))
1097 if (s < match || (vim_ispathsep(*s)
1098 && ++match_pathsep_cnt > pat_pathsep_cnt))
1099 break;
1100 ++s;
1101 *e = NUL;
1102 mch_memmove(match, s, e - s + 1);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001103 }
1104
1105 if (ga.ga_len == 0)
1106 return FAIL;
1107
1108 // Sort and remove duplicates which can happen when specifying multiple
1109 // directories in dirnames.
1110 remove_duplicates(&ga);
1111
1112 *file = ga.ga_data;
1113 *num_file = ga.ga_len;
1114 return OK;
1115}
1116
1117/*
1118 * Expand loadplugin names:
1119 * 'packpath'/pack/ * /opt/{pat}
1120 */
1121 int
1122ExpandPackAddDir(
1123 char_u *pat,
1124 int *num_file,
1125 char_u ***file)
1126{
1127 char_u *s;
1128 char_u *e;
1129 char_u *match;
1130 garray_T ga;
1131 int i;
1132 int pat_len;
1133
1134 *num_file = 0;
1135 *file = NULL;
1136 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001137 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001138
1139 s = alloc(pat_len + 26);
1140 if (s == NULL)
1141 {
1142 ga_clear_strings(&ga);
1143 return FAIL;
1144 }
1145 sprintf((char *)s, "pack/*/opt/%s*", pat);
zeertzjq3770f4c2023-01-22 18:38:51 +00001146 globpath(p_pp, s, &ga, 0, TRUE);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001147 vim_free(s);
1148
1149 for (i = 0; i < ga.ga_len; ++i)
1150 {
1151 match = ((char_u **)ga.ga_data)[i];
1152 s = gettail(match);
1153 e = s + STRLEN(s);
1154 mch_memmove(match, s, e - s + 1);
1155 }
1156
1157 if (ga.ga_len == 0)
1158 return FAIL;
1159
1160 // Sort and remove duplicates which can happen when specifying multiple
1161 // directories in dirnames.
1162 remove_duplicates(&ga);
1163
1164 *file = ga.ga_data;
1165 *num_file = ga.ga_len;
1166 return OK;
1167}
1168
1169 static void
1170cmd_source(char_u *fname, exarg_T *eap)
1171{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001172 int clearvars = FALSE;
1173
1174 if (*fname != NUL && STRNCMP(fname, "++clear", 7) == 0)
1175 {
1176 // ++clear argument is supplied
1177 clearvars = TRUE;
1178 fname = fname + 7;
1179 if (*fname != NUL)
1180 {
1181 semsg(_(e_invalid_argument_str), eap->arg);
1182 return;
1183 }
1184 }
1185
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001186 if (*fname != NUL && eap != NULL && eap->addr_count > 0)
1187 {
1188 // if a filename is specified to :source, then a range is not allowed
1189 emsg(_(e_no_range_allowed));
1190 return;
1191 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001192
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001193 if (eap != NULL && *fname == NUL)
1194 {
1195 if (eap->forceit)
1196 // a file name is needed to source normal mode commands
1197 emsg(_(e_argument_required));
1198 else
1199 // source ex commands from the current buffer
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001200 do_source_ext(NULL, FALSE, FALSE, NULL, eap, clearvars);
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001201 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001202 else if (eap != NULL && eap->forceit)
1203 // ":source!": read Normal mode commands
1204 // Need to execute the commands directly. This is required at least
1205 // for:
1206 // - ":g" command busy
1207 // - after ":argdo", ":windo" or ":bufdo"
1208 // - another command follows
1209 // - inside a loop
1210 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
1211#ifdef FEAT_EVAL
1212 || eap->cstack->cs_idx >= 0
1213#endif
1214 );
1215
1216 // ":source" read ex commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001218 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001219}
1220
1221/*
1222 * ":source {fname}"
1223 */
1224 void
1225ex_source(exarg_T *eap)
1226{
1227#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02001228 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001229 {
1230 char_u *fname = NULL;
1231
1232 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
1233 NULL, NULL,
1234 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1235 if (fname != NULL)
1236 {
1237 cmd_source(fname, eap);
1238 vim_free(fname);
1239 }
1240 }
1241 else
1242#endif
1243 cmd_source(eap->arg, eap);
1244}
1245
1246#if defined(FEAT_EVAL) || defined(PROTO)
1247/*
1248 * ":options"
1249 */
1250 void
1251ex_options(
1252 exarg_T *eap UNUSED)
1253{
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001254 char_u buf[500];
1255 int multi_mods = 0;
1256
1257 buf[0] = NUL;
dundargocc57b5bc2022-11-02 13:30:51 +00001258 (void)add_win_cmd_modifiers(buf, &cmdmod, &multi_mods);
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001259
1260 vim_setenv((char_u *)"OPTWIN_CMD", buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001261 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
1262}
1263#endif
1264
1265/*
1266 * ":source" and associated commands.
1267 */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001268
1269#ifdef FEAT_EVAL
1270/*
1271 * Return the address holding the next breakpoint line for a source cookie.
1272 */
1273 linenr_T *
1274source_breakpoint(void *cookie)
1275{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001276 return &((source_cookie_T *)cookie)->breakpoint;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001277}
1278
1279/*
1280 * Return the address holding the debug tick for a source cookie.
1281 */
1282 int *
1283source_dbg_tick(void *cookie)
1284{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001285 return &((source_cookie_T *)cookie)->dbg_tick;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001286}
1287
1288/*
1289 * Return the nesting level for a source cookie.
1290 */
1291 int
1292source_level(void *cookie)
1293{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001294 return ((source_cookie_T *)cookie)->level;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001295}
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001296
1297/*
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02001298 * Return the readahead line. Note that the pointer may become invalid when
1299 * getting the next line, if it's concatenated with the next one.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001300 */
1301 char_u *
1302source_nextline(void *cookie)
1303{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001304 return ((source_cookie_T *)cookie)->nextline;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001305}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001306#endif
1307
1308#if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC)
1309# define USE_FOPEN_NOINH
1310/*
1311 * Special function to open a file without handle inheritance.
1312 * When possible the handle is closed on exec().
1313 */
1314 static FILE *
1315fopen_noinh_readbin(char *filename)
1316{
1317# ifdef MSWIN
1318 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
1319# else
1320 int fd_tmp = mch_open(filename, O_RDONLY, 0);
1321# endif
1322
1323 if (fd_tmp == -1)
1324 return NULL;
1325
1326# ifdef HAVE_FD_CLOEXEC
1327 {
1328 int fdflags = fcntl(fd_tmp, F_GETFD);
1329 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1330 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
1331 }
1332# endif
1333
1334 return fdopen(fd_tmp, READBIN);
1335}
1336#endif
1337
1338/*
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001339 * Initialization for sourcing lines from the current buffer. Reads all the
1340 * lines from the buffer and stores it in the cookie grow array.
1341 * Returns a pointer to the name ":source buffer=<n>" on success and NULL on
1342 * failure.
1343 */
1344 static char_u *
1345do_source_buffer_init(source_cookie_T *sp, exarg_T *eap)
1346{
1347 linenr_T curr_lnum;
1348 char_u *line = NULL;
1349 char_u *fname;
1350
1351 CLEAR_FIELD(*sp);
1352
1353 if (curbuf == NULL)
1354 return NULL;
1355
1356 // Use ":source buffer=<num>" as the script name
1357 vim_snprintf((char *)IObuff, IOSIZE, ":source buffer=%d", curbuf->b_fnum);
1358 fname = vim_strsave(IObuff);
1359 if (fname == NULL)
1360 return NULL;
1361
1362 ga_init2(&sp->buflines, sizeof(char_u *), 100);
1363
1364 // Copy the lines from the buffer into a grow array
1365 for (curr_lnum = eap->line1; curr_lnum <= eap->line2; curr_lnum++)
1366 {
1367 line = vim_strsave(ml_get(curr_lnum));
1368 if (line == NULL)
1369 goto errret;
1370 if (ga_add_string(&sp->buflines, line) == FAIL)
1371 goto errret;
1372 line = NULL;
1373 }
1374 sp->buf_lnum = 0;
1375 sp->source_from_buf = TRUE;
1376
1377 return fname;
1378
1379errret:
1380 vim_free(fname);
1381 vim_free(line);
1382 ga_clear_strings(&sp->buflines);
1383 return NULL;
1384}
1385
1386/*
1387 * Read the file "fname" and execute its lines as EX commands.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001388 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1389 * again.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001390 *
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001391 * The "eap" argument is used when sourcing lines from a buffer instead of a
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001392 * file.
1393 *
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001394 * If "clearvars" is TRUE, then for scripts which are loaded more than
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001395 * once, clear all the functions and variables previously defined in that
1396 * script.
1397 *
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001398 * This function may be called recursively!
1399 *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 * Return FAIL if file could not be opened, OK otherwise.
1401 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001402 */
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001403 static int
1404do_source_ext(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001405 char_u *fname,
1406 int check_other, // check for .vimrc and _vimrc
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 int is_vimrc, // DOSO_ value
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001408 int *ret_sid UNUSED,
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001409 exarg_T *eap,
1410 int clearvars UNUSED)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001411{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001412 source_cookie_T cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001413 char_u *p;
Bram Moolenaar753885b2022-08-24 16:30:36 +01001414 char_u *fname_not_fixed = NULL;
Bram Moolenaar5214b292022-08-24 17:32:35 +01001415 char_u *fname_exp = NULL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001416 char_u *firstline = NULL;
1417 int retval = FAIL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001418 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001419#ifdef FEAT_EVAL
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001420 funccal_entry_T funccalp_entry;
1421 int save_debug_break_level = debug_break_level;
Bram Moolenaar5214b292022-08-24 17:32:35 +01001422 int sid = -1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001423 scriptitem_T *si = NULL;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001424 int save_estack_compiling = estack_compiling;
Bram Moolenaar88774872022-08-16 20:24:29 +01001425 ESTACK_CHECK_DECLARATION
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001426#endif
1427#ifdef STARTUPTIME
1428 struct timeval tv_rel;
1429 struct timeval tv_start;
1430#endif
1431#ifdef FEAT_PROFILE
1432 proftime_T wait_start;
1433#endif
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001434 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001435 int trigger_source_post = FALSE;
1436
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001437 CLEAR_FIELD(cookie);
1438 if (fname == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001439 {
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001440 // sourcing lines from a buffer
1441 fname_exp = do_source_buffer_init(&cookie, eap);
1442 if (fname_exp == NULL)
1443 return FAIL;
1444 }
1445 else
1446 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01001447 fname_not_fixed = expand_env_save(fname);
1448 if (fname_not_fixed == NULL)
1449 goto theend;
1450 fname_exp = fix_fname(fname_not_fixed);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001451 if (fname_exp == NULL)
Bram Moolenaar753885b2022-08-24 16:30:36 +01001452 goto theend;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001453 if (mch_isdir(fname_exp))
1454 {
1455 smsg(_("Cannot source a directory: \"%s\""), fname);
1456 goto theend;
1457 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001458 }
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001459#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001460 estack_compiling = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001461
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462 // See if we loaded this script before.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001463 sid = find_script_by_name(fname_exp);
Bram Moolenaarf479cac2022-01-12 12:54:55 +00001464 if (sid > 0 && ret_sid != NULL
1465 && SCRIPT_ITEM(sid)->sn_state != SN_STATE_NOT_LOADED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 {
1467 // Already loaded and no need to load again, return here.
1468 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001469 retval = OK;
1470 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 }
1472#endif
1473
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001474 // Apply SourceCmd autocommands, they should get the file and source it.
1475 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1476 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1477 FALSE, curbuf))
1478 {
1479#ifdef FEAT_EVAL
1480 retval = aborting() ? FAIL : OK;
1481#else
1482 retval = OK;
1483#endif
1484 if (retval == OK)
1485 // Apply SourcePost autocommands.
1486 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1487 FALSE, curbuf);
1488 goto theend;
1489 }
1490
1491 // Apply SourcePre autocommands, they may get the file.
1492 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1493
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001494 if (!cookie.source_from_buf)
1495 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001496#ifdef USE_FOPEN_NOINH
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001497 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001498#else
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001499 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001500#endif
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001501 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001502 if (cookie.fp == NULL && check_other)
1503 {
1504 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1505 // and ".exrc" by "_exrc" or vice versa.
1506 p = gettail(fname_exp);
1507 if ((*p == '.' || *p == '_')
1508 && (STRICMP(p + 1, "vimrc") == 0
1509 || STRICMP(p + 1, "gvimrc") == 0
1510 || STRICMP(p + 1, "exrc") == 0))
1511 {
1512 if (*p == '_')
1513 *p = '.';
1514 else
1515 *p = '_';
1516#ifdef USE_FOPEN_NOINH
1517 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1518#else
1519 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1520#endif
1521 }
1522 }
1523
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001524 if (cookie.fp == NULL && !cookie.source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001525 {
1526 if (p_verbose > 0)
1527 {
1528 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001529 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001530 smsg(_("could not source \"%s\""), fname);
1531 else
1532 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001533 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001534 verbose_leave();
1535 }
1536 goto theend;
1537 }
1538
1539 // The file exists.
1540 // - In verbose mode, give a message.
1541 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1542 if (p_verbose > 1)
1543 {
1544 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001545 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001546 smsg(_("sourcing \"%s\""), fname);
1547 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001548 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001549 verbose_leave();
1550 }
1551 if (is_vimrc == DOSO_VIMRC)
1552 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1553 else if (is_vimrc == DOSO_GVIMRC)
1554 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1555
1556#ifdef USE_CRNL
1557 // If no automatic file format: Set default to CR-NL.
1558 if (*p_ffs == NUL)
1559 cookie.fileformat = EOL_DOS;
1560 else
1561 cookie.fileformat = EOL_UNKNOWN;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001562#endif
1563
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001564 if (fname == NULL)
1565 // When sourcing a range of lines from a buffer, use the buffer line
1566 // number.
1567 cookie.sourcing_lnum = eap->line1 - 1;
1568 else
1569 cookie.sourcing_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001570
1571#ifdef FEAT_EVAL
1572 // Check if this script has a breakpoint.
1573 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1574 cookie.fname = fname_exp;
1575 cookie.dbg_tick = debug_tick;
1576
1577 cookie.level = ex_nesting_level;
1578#endif
1579
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001580#ifdef STARTUPTIME
1581 if (time_fd != NULL)
1582 time_push(&tv_rel, &tv_start);
1583#endif
1584
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001585 // "legacy" does not apply to commands in the script
1586 sticky_cmdmod_flags = 0;
1587
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001588 save_current_sctx = current_sctx;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001589 if (cmdmod.cmod_flags & CMOD_VIM9CMD)
1590 // When the ":vim9cmd" command modifier is used, source the script as a
1591 // Vim9 script.
1592 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1593 else
1594 current_sctx.sc_version = 1; // default script version
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001595
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001596#ifdef FEAT_EVAL
Bram Moolenaara2942c72023-01-02 13:41:49 +00001597 current_sctx.sc_lnum = 0;
1598
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001599# ifdef FEAT_PROFILE
1600 if (do_profiling == PROF_YES)
1601 prof_child_enter(&wait_start); // entering a child now
1602# endif
1603
1604 // Don't use local function variables, if called from a function.
1605 // Also starts profiling timer for nested script.
1606 save_funccal(&funccalp_entry);
1607
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001608 // Reset "KeyTyped" to avoid some commands thinking they are invoked
1609 // interactively. E.g. defining a function would output indent.
1610 int save_KeyTyped = KeyTyped;
1611 KeyTyped = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001612
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001613 // Check if this script was sourced before to find its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001614 // Always use a new sequence number.
1615 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001617 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 hashtab_T *ht;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001619 int todo;
1620 hashitem_T *hi;
1621 dictitem_T *di;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622
1623 // loading the same script again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001625 si = SCRIPT_ITEM(sid);
1626 if (si->sn_state == SN_STATE_NOT_LOADED)
1627 {
1628 // this script was found but not loaded yet
1629 si->sn_state = SN_STATE_NEW;
1630 }
1631 else
1632 {
1633 si->sn_state = SN_STATE_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001634
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001635 if (!clearvars)
1636 {
1637 // Script-local variables remain but "const" can be set again.
1638 // In Vim9 script variables will be cleared when "vim9script"
1639 // is encountered without the "noclear" argument.
1640 ht = &SCRIPT_VARS(sid);
1641 todo = (int)ht->ht_used;
1642 for (hi = ht->ht_array; todo > 0; ++hi)
1643 if (!HASHITEM_EMPTY(hi))
1644 {
1645 --todo;
1646 di = HI2DI(hi);
1647 di->di_flags |= DI_FLAGS_RELOAD;
1648 }
1649 // imports can be redefined once
1650 mark_imports_for_reload(sid);
1651 }
1652 else
1653 clear_vim9_scriptlocal_vars(sid);
Bram Moolenaar0123cc12021-02-07 17:17:58 +01001654
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001655 // reset version, "vim9script" may have been added or removed.
1656 si->sn_version = 1;
1657 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001658 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001660 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001661 int error = OK;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001662
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001663 // It's new, generate a new SID and initialize the scriptitem.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001664 sid = get_new_scriptitem(&error);
1665 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001666 if (error == FAIL)
1667 goto almosttheend;
Bram Moolenaar753885b2022-08-24 16:30:36 +01001668 si = SCRIPT_ITEM(sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001669 si->sn_name = fname_exp;
1670 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 if (ret_sid != NULL)
Bram Moolenaar753885b2022-08-24 16:30:36 +01001672 *ret_sid = sid;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001673
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001674 // Remember the "is_vimrc" flag for when the file is sourced again.
1675 si->sn_is_vimrc = is_vimrc;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001676 }
1677
zeertzjq5a4fff42022-08-15 17:53:55 +01001678 // Keep the sourcing name/lnum, for recursive calls.
1679 estack_push(ETYPE_SCRIPT, si->sn_name, 0);
1680 ESTACK_CHECK_SETUP
1681
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001682# ifdef FEAT_PROFILE
1683 if (do_profiling == PROF_YES)
1684 {
1685 int forceit;
1686
1687 // Check if we do profiling for this script.
1688 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
1689 {
1690 script_do_profile(si);
1691 si->sn_pr_force = forceit;
1692 }
1693 if (si->sn_prof_on)
1694 {
1695 ++si->sn_pr_count;
1696 profile_start(&si->sn_pr_start);
1697 profile_zero(&si->sn_pr_children);
1698 }
1699 }
1700# endif
Bram Moolenaaraeef1f72022-09-15 12:20:18 +01001701#else
1702 // Keep the sourcing name/lnum, for recursive calls.
1703 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001704#endif
1705
1706 cookie.conv.vc_type = CONV_NONE; // no conversion
1707
1708 // Read the first line so we can check for a UTF-8 BOM.
1709 firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1710 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1711 && firstline[1] == 0xbb && firstline[2] == 0xbf)
1712 {
1713 // Found BOM; setup conversion, skip over BOM and recode the line.
1714 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1715 p = string_convert(&cookie.conv, firstline + 3, NULL);
1716 if (p == NULL)
1717 p = vim_strsave(firstline + 3);
1718 if (p != NULL)
1719 {
1720 vim_free(firstline);
1721 firstline = p;
1722 }
1723 }
1724
1725 // Call do_cmdline, which will call getsourceline() to get the lines.
1726 do_cmdline(firstline, getsourceline, (void *)&cookie,
1727 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1728 retval = OK;
1729
1730#ifdef FEAT_PROFILE
1731 if (do_profiling == PROF_YES)
1732 {
1733 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001734 si = SCRIPT_ITEM(sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001735 if (si->sn_prof_on)
1736 {
1737 profile_end(&si->sn_pr_start);
1738 profile_sub_wait(&wait_start, &si->sn_pr_start);
1739 profile_add(&si->sn_pr_total, &si->sn_pr_start);
1740 profile_self(&si->sn_pr_self, &si->sn_pr_start,
1741 &si->sn_pr_children);
1742 }
1743 }
1744#endif
1745
1746 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001747 emsg(_(e_interrupted));
Bram Moolenaar88774872022-08-16 20:24:29 +01001748#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +01001749 ESTACK_CHECK_NOW
Bram Moolenaar88774872022-08-16 20:24:29 +01001750#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001751 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001752 if (p_verbose > 1)
1753 {
1754 verbose_enter();
1755 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001756 if (SOURCING_NAME != NULL)
1757 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001758 verbose_leave();
1759 }
1760#ifdef STARTUPTIME
1761 if (time_fd != NULL)
1762 {
1763 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1764 time_msg((char *)IObuff, &tv_start);
1765 time_pop(&tv_rel);
1766 }
1767#endif
1768
1769 if (!got_int)
1770 trigger_source_post = TRUE;
1771
1772#ifdef FEAT_EVAL
1773 // After a "finish" in debug mode, need to break at first command of next
1774 // sourced file.
1775 if (save_debug_break_level > ex_nesting_level
1776 && debug_break_level == ex_nesting_level)
1777 ++debug_break_level;
1778#endif
1779
1780#ifdef FEAT_EVAL
1781almosttheend:
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001782 // If "sn_save_cpo" is set that means we encountered "vim9script": restore
1783 // 'cpoptions', unless in the main .vimrc file.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001785 si = SCRIPT_ITEM(sid);
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001786 if (si->sn_save_cpo != NULL && si->sn_is_vimrc == DOSO_NONE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 {
Bram Moolenaar3e191692021-03-17 17:46:00 +01001788 if (STRCMP(p_cpo, CPO_VIM) != 0)
1789 {
1790 char_u *f;
1791 char_u *t;
1792
1793 // 'cpo' was changed in the script. Apply the same change to the
1794 // saved value, if possible.
1795 for (f = (char_u *)CPO_VIM; *f != NUL; ++f)
1796 if (vim_strchr(p_cpo, *f) == NULL
1797 && (t = vim_strchr(si->sn_save_cpo, *f)) != NULL)
1798 // flag was removed, also remove it from the saved 'cpo'
1799 mch_memmove(t, t + 1, STRLEN(t));
1800 for (f = p_cpo; *f != NUL; ++f)
1801 if (vim_strchr((char_u *)CPO_VIM, *f) == NULL
1802 && vim_strchr(si->sn_save_cpo, *f) == NULL)
1803 {
1804 // flag was added, also add it to the saved 'cpo'
1805 t = alloc(STRLEN(si->sn_save_cpo) + 2);
1806 if (t != NULL)
1807 {
1808 *t = *f;
1809 STRCPY(t + 1, si->sn_save_cpo);
1810 vim_free(si->sn_save_cpo);
1811 si->sn_save_cpo = t;
1812 }
1813 }
1814 }
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001815 set_option_value_give_err((char_u *)"cpo",
1816 0L, si->sn_save_cpo, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 }
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001818 VIM_CLEAR(si->sn_save_cpo);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001820 restore_funccal();
1821# ifdef FEAT_PROFILE
1822 if (do_profiling == PROF_YES)
1823 prof_child_exit(&wait_start); // leaving a child now
1824# endif
Bram Moolenaara2942c72023-01-02 13:41:49 +00001825
1826 KeyTyped = save_KeyTyped;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001827#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001828 current_sctx = save_current_sctx;
1829
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001830 if (cookie.fp != NULL)
1831 fclose(cookie.fp);
1832 if (cookie.source_from_buf)
1833 ga_clear_strings(&cookie.buflines);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001834 vim_free(cookie.nextline);
1835 vim_free(firstline);
1836 convert_setup(&cookie.conv, NULL, NULL);
1837
1838 if (trigger_source_post)
1839 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1840
1841theend:
Bram Moolenaar0af2ecf2022-08-24 17:05:56 +01001842#ifdef FEAT_EVAL
Bram Moolenaar753885b2022-08-24 16:30:36 +01001843 if (sid > 0 && ret_sid != NULL
1844 && fname_not_fixed != NULL && fname_exp != NULL)
1845 {
1846 int not_fixed_sid = find_script_by_name(fname_not_fixed);
1847
1848 // If "fname_not_fixed" is a symlink then we source the linked file.
1849 // If the original name is in the script list we add the ID of the
1850 // script that was actually sourced.
1851 if (SCRIPT_ID_VALID(not_fixed_sid) && not_fixed_sid != sid)
1852 SCRIPT_ITEM(not_fixed_sid)->sn_sourced_sid = sid;
1853 }
Bram Moolenaar0af2ecf2022-08-24 17:05:56 +01001854#endif
Bram Moolenaar753885b2022-08-24 16:30:36 +01001855
1856 vim_free(fname_not_fixed);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001857 vim_free(fname_exp);
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001858 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001859#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001860 estack_compiling = save_estack_compiling;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001861#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001862 return retval;
1863}
1864
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001865 int
1866do_source(
1867 char_u *fname,
1868 int check_other, // check for .vimrc and _vimrc
1869 int is_vimrc, // DOSO_ value
Bram Moolenaar753885b2022-08-24 16:30:36 +01001870 int *ret_sid)
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001871{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001872 return do_source_ext(fname, check_other, is_vimrc, ret_sid, NULL, FALSE);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001873}
1874
1875
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001876#if defined(FEAT_EVAL) || defined(PROTO)
1877
1878/*
1879 * ":scriptnames"
1880 */
1881 void
1882ex_scriptnames(exarg_T *eap)
1883{
1884 int i;
1885
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001886 if (eap->addr_count > 0 || *eap->arg != NUL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001887 {
1888 // :script {scriptId}: edit the script
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001889 if (eap->addr_count > 0 && !SCRIPT_ID_VALID(eap->line2))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001890 emsg(_(e_invalid_argument));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001891 else
1892 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001893 if (eap->addr_count > 0)
1894 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
1895 else
1896 {
1897 expand_env(eap->arg, NameBuff, MAXPATHL);
1898 eap->arg = NameBuff;
1899 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001900 do_exedit(eap, NULL);
1901 }
1902 return;
1903 }
1904
1905 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar6079da72022-01-18 14:16:59 +00001906 {
1907 scriptitem_T *si = SCRIPT_ITEM(i);
1908
1909 if (si->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001910 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01001911 char sourced_buf[20];
1912
Bram Moolenaar6079da72022-01-18 14:16:59 +00001913 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar753885b2022-08-24 16:30:36 +01001914 if (si->sn_sourced_sid > 0)
1915 vim_snprintf(sourced_buf, 20, "->%d", si->sn_sourced_sid);
1916 else
1917 sourced_buf[0] = NUL;
1918 vim_snprintf((char *)IObuff, IOSIZE, "%3d%s%s: %s",
Bram Moolenaar6079da72022-01-18 14:16:59 +00001919 i,
Bram Moolenaar753885b2022-08-24 16:30:36 +01001920 sourced_buf,
Bram Moolenaar6079da72022-01-18 14:16:59 +00001921 si->sn_state == SN_STATE_NOT_LOADED ? " A" : "",
1922 NameBuff);
Bram Moolenaar769f5892022-02-09 14:31:05 +00001923 if (!message_filtered(IObuff))
1924 {
1925 msg_putchar('\n');
1926 msg_outtrans(IObuff);
1927 out_flush(); // output one line at a time
1928 ui_breakcheck();
1929 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001930 }
Bram Moolenaar6079da72022-01-18 14:16:59 +00001931 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001932}
1933
1934# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1935/*
1936 * Fix slashes in the list of script names for 'shellslash'.
1937 */
1938 void
1939scriptnames_slash_adjust(void)
1940{
1941 int i;
1942
1943 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001944 if (SCRIPT_ITEM(i)->sn_name != NULL)
1945 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001946}
1947# endif
1948
1949/*
1950 * Get a pointer to a script name. Used for ":verbose set".
Bram Moolenaar74667062020-12-28 15:41:41 +01001951 * Message appended to "Last set from "
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001952 */
1953 char_u *
1954get_scriptname(scid_T id)
1955{
1956 if (id == SID_MODELINE)
1957 return (char_u *)_("modeline");
1958 if (id == SID_CMDARG)
1959 return (char_u *)_("--cmd argument");
1960 if (id == SID_CARG)
1961 return (char_u *)_("-c argument");
1962 if (id == SID_ENV)
1963 return (char_u *)_("environment variable");
1964 if (id == SID_ERROR)
1965 return (char_u *)_("error handler");
Bram Moolenaar74667062020-12-28 15:41:41 +01001966 if (id == SID_WINLAYOUT)
1967 return (char_u *)_("changed window size");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001968 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001969}
1970
1971# if defined(EXITFREE) || defined(PROTO)
1972 void
1973free_scriptnames(void)
1974{
1975 int i;
1976
1977 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02001978 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001979 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001981 // the variables themselves are cleared in evalvars_clear()
1982 vim_free(si->sn_vars);
1983
1984 vim_free(si->sn_name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001985 free_imports_and_script_vars(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001986 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02001987# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001988 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02001989# endif
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00001990 vim_free(si->sn_autoload_prefix);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001991 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02001992 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001993 ga_clear(&script_items);
1994}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001995
1996 void
1997free_autoload_scriptnames(void)
1998{
1999 ga_clear_strings(&ga_loaded);
2000}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002001# endif
2002
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002003 linenr_T
Bram Moolenaar66250c92020-08-20 15:02:42 +02002004get_sourced_lnum(
2005 char_u *(*fgetline)(int, void *, int, getline_opt_T),
2006 void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002007{
2008 return fgetline == getsourceline
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002009 ? ((source_cookie_T *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002010 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002011}
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002012
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002013/*
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002014 * Return a List of script-local functions defined in the script with id
2015 * 'sid'.
2016 */
2017 static list_T *
2018get_script_local_funcs(scid_T sid)
2019{
2020 hashtab_T *functbl;
2021 hashitem_T *hi;
2022 long_u todo;
2023 list_T *l;
2024
2025 l = list_alloc();
2026 if (l == NULL)
2027 return NULL;
2028
2029 // Iterate through all the functions in the global function hash table
2030 // looking for functions with script ID 'sid'.
2031 functbl = func_tbl_get();
2032 todo = functbl->ht_used;
2033 for (hi = functbl->ht_array; todo > 0; ++hi)
2034 {
2035 ufunc_T *fp;
2036
2037 if (HASHITEM_EMPTY(hi))
2038 continue;
2039
2040 --todo;
2041 fp = HI2UF(hi);
2042
2043 // Add active functions with script id == 'sid'
2044 if (!(fp->uf_flags & FC_DEAD) && (fp->uf_script_ctx.sc_sid == sid))
2045 {
2046 char_u *name;
2047
2048 if (fp->uf_name_exp != NULL)
2049 name = fp->uf_name_exp;
2050 else
2051 name = fp->uf_name;
2052
2053 list_append_string(l, name, -1);
2054 }
2055 }
2056
2057 return l;
2058}
2059
2060/*
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002061 * getscriptinfo() function
2062 */
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002063 void
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002064f_getscriptinfo(typval_T *argvars, typval_T *rettv)
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002065{
2066 int i;
2067 list_T *l;
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002068 char_u *pat = NULL;
2069 regmatch_T regmatch;
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002070 int filterpat = FALSE;
2071 scid_T sid = -1;
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002072
2073 if (rettv_list_alloc(rettv) == FAIL)
2074 return;
2075
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002076 if (check_for_opt_dict_arg(argvars, 0) == FAIL)
2077 return;
2078
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002079 l = rettv->vval.v_list;
2080
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002081 regmatch.regprog = NULL;
2082 regmatch.rm_ic = p_ic;
2083
2084 if (argvars[0].v_type == VAR_DICT)
2085 {
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002086 sid = dict_get_number_def(argvars[0].vval.v_dict, "sid", -1);
2087 if (sid == -1)
2088 {
2089 pat = dict_get_string(argvars[0].vval.v_dict, "name", TRUE);
2090 if (pat != NULL)
2091 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2092 if (regmatch.regprog != NULL)
2093 filterpat = TRUE;
2094 }
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002095 }
2096
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002097 for (i = 1; i <= script_items.ga_len; ++i)
2098 {
2099 scriptitem_T *si = SCRIPT_ITEM(i);
2100 dict_T *d;
2101
2102 if (si->sn_name == NULL)
2103 continue;
2104
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002105 if (filterpat && !vim_regexec(&regmatch, si->sn_name, (colnr_T)0))
2106 continue;
2107
2108 if (sid != -1 && sid != i)
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002109 continue;
2110
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002111 if ((d = dict_alloc()) == NULL
2112 || list_append_dict(l, d) == FAIL
2113 || dict_add_string(d, "name", si->sn_name) == FAIL
2114 || dict_add_number(d, "sid", i) == FAIL
Bram Moolenaar753885b2022-08-24 16:30:36 +01002115 || dict_add_number(d, "sourced", si->sn_sourced_sid) == FAIL
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002116 || dict_add_number(d, "version", si->sn_version) == FAIL
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002117 || dict_add_bool(d, "autoload",
2118 si->sn_state == SN_STATE_NOT_LOADED) == FAIL)
2119 return;
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002120
2121 // When a filter pattern is specified to return information about only
2122 // specific script(s), also add the script-local variables and
2123 // functions.
2124 if (sid != -1)
2125 {
2126 dict_T *var_dict;
2127
2128 var_dict = dict_copy(&si->sn_vars->sv_dict, TRUE, TRUE,
2129 get_copyID());
2130 if (var_dict == NULL
2131 || dict_add_dict(d, "variables", var_dict) == FAIL
2132 || dict_add_list(d, "functions",
2133 get_script_local_funcs(sid)) == FAIL)
2134 return;
2135 }
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002136 }
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002137
2138 vim_regfree(regmatch.regprog);
2139 vim_free(pat);
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002140}
2141
Dominique Pelle748b3082022-01-08 12:41:16 +00002142#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002143
2144 static char_u *
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002145get_one_sourceline(source_cookie_T *sp)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002146{
2147 garray_T ga;
2148 int len;
2149 int c;
2150 char_u *buf;
2151#ifdef USE_CRNL
2152 int has_cr; // CR-LF found
2153#endif
2154 int have_read = FALSE;
2155
2156 // use a growarray to store the sourced line
2157 ga_init2(&ga, 1, 250);
2158
2159 // Loop until there is a finished line (or end-of-file).
2160 ++sp->sourcing_lnum;
2161 for (;;)
2162 {
2163 // make room to read at least 120 (more) characters
2164 if (ga_grow(&ga, 120) == FAIL)
2165 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002166 if (sp->source_from_buf)
2167 {
2168 if (sp->buf_lnum >= sp->buflines.ga_len)
2169 break; // all the lines are processed
2170 ga_concat(&ga, ((char_u **)sp->buflines.ga_data)[sp->buf_lnum]);
2171 sp->buf_lnum++;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01002172 if (ga_grow(&ga, 1) == FAIL)
2173 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002174 buf = (char_u *)ga.ga_data;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01002175 buf[ga.ga_len++] = NUL;
Bram Moolenaar4748c4b2022-05-17 17:47:07 +01002176 len = ga.ga_len;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002177 }
2178 else
2179 {
2180 buf = (char_u *)ga.ga_data;
2181 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
2182 sp->fp) == NULL)
2183 break;
Bram Moolenaar4748c4b2022-05-17 17:47:07 +01002184 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002185 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002186#ifdef USE_CRNL
2187 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
2188 // CTRL-Z by its own, or after a NL.
2189 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
2190 && sp->fileformat == EOL_DOS
2191 && buf[len - 1] == Ctrl_Z)
2192 {
2193 buf[len - 1] = NUL;
2194 break;
2195 }
2196#endif
2197
2198 have_read = TRUE;
2199 ga.ga_len = len;
2200
2201 // If the line was longer than the buffer, read more.
2202 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
2203 continue;
2204
2205 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
2206 {
2207#ifdef USE_CRNL
2208 has_cr = (len >= 2 && buf[len - 2] == '\r');
2209 if (sp->fileformat == EOL_UNKNOWN)
2210 {
2211 if (has_cr)
2212 sp->fileformat = EOL_DOS;
2213 else
2214 sp->fileformat = EOL_UNIX;
2215 }
2216
2217 if (sp->fileformat == EOL_DOS)
2218 {
2219 if (has_cr) // replace trailing CR
2220 {
2221 buf[len - 2] = '\n';
2222 --len;
2223 --ga.ga_len;
2224 }
2225 else // lines like ":map xx yy^M" will have failed
2226 {
2227 if (!sp->error)
2228 {
2229 msg_source(HL_ATTR(HLF_W));
2230 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
2231 }
2232 sp->error = TRUE;
2233 sp->fileformat = EOL_UNIX;
2234 }
2235 }
2236#endif
2237 // The '\n' is escaped if there is an odd number of ^V's just
2238 // before it, first set "c" just before the 'V's and then check
2239 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
2240 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
2241 ;
2242 if ((len & 1) != (c & 1)) // escaped NL, read more
2243 {
2244 ++sp->sourcing_lnum;
2245 continue;
2246 }
2247
2248 buf[len - 1] = NUL; // remove the NL
2249 }
2250
2251 // Check for ^C here now and then, so recursive :so can be broken.
2252 line_breakcheck();
2253 break;
2254 }
2255
2256 if (have_read)
2257 return (char_u *)ga.ga_data;
2258
2259 vim_free(ga.ga_data);
2260 return NULL;
2261}
2262
2263/*
2264 * Get one full line from a sourced file.
2265 * Called by do_cmdline() when it's called from do_source().
2266 *
2267 * Return a pointer to the line in allocated memory.
2268 * Return NULL for end-of-file or some error.
2269 */
2270 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02002271getsourceline(
2272 int c UNUSED,
2273 void *cookie,
2274 int indent UNUSED,
2275 getline_opt_T options)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002276{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002277 source_cookie_T *sp = (source_cookie_T *)cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002278 char_u *line;
2279 char_u *p;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002280 int do_vim9_all = in_vim9script()
2281 && options == GETLINE_CONCAT_ALL;
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002282 int do_bar_cont = do_vim9_all
2283 || options == GETLINE_CONCAT_CONTBAR;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002284
2285#ifdef FEAT_EVAL
2286 // If breakpoints have been added/deleted need to check for it.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002287 if ((sp->dbg_tick < debug_tick) && !sp->source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002288 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002289 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002290 sp->dbg_tick = debug_tick;
2291 }
2292# ifdef FEAT_PROFILE
2293 if (do_profiling == PROF_YES)
2294 script_line_end();
2295# endif
2296#endif
2297
2298 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002299 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002300
2301 // Get current line. If there is a read-ahead line, use it, otherwise get
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002302 // one now. "fp" is NULL if actually using a string.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002303 if (sp->finished || (!sp->source_from_buf && sp->fp == NULL))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002304 line = NULL;
2305 else if (sp->nextline == NULL)
2306 line = get_one_sourceline(sp);
2307 else
2308 {
2309 line = sp->nextline;
2310 sp->nextline = NULL;
2311 ++sp->sourcing_lnum;
2312 }
2313#ifdef FEAT_PROFILE
2314 if (line != NULL && do_profiling == PROF_YES)
2315 script_line_start();
2316#endif
2317
2318 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
2319 // contain the 'C' flag.
Bram Moolenaar66250c92020-08-20 15:02:42 +02002320 if (line != NULL && options != GETLINE_NONE
2321 && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002322 {
Bram Moolenaar5072b472021-06-03 21:56:10 +02002323 int comment_char = in_vim9script() ? '#' : '"';
2324
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002325 // compensate for the one line read-ahead
2326 --sp->sourcing_lnum;
2327
2328 // Get the next line and concatenate it when it starts with a
2329 // backslash. We always need to read the next line, keep it in
2330 // sp->nextline.
2331 /* Also check for a comment in between continuation lines: "\ */
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002332 // Also check for a Vim9 comment, empty line, line starting with '|',
2333 // but not "||".
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002334 sp->nextline = get_one_sourceline(sp);
2335 if (sp->nextline != NULL
2336 && (*(p = skipwhite(sp->nextline)) == '\\'
Bram Moolenaar5072b472021-06-03 21:56:10 +02002337 || (p[0] == comment_char
2338 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002339 || (do_vim9_all && (*p == NUL
2340 || vim9_comment_start(p)))
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002341 || (do_bar_cont && p[0] == '|' && p[1] != '|')))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002342 {
2343 garray_T ga;
2344
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002345 ga_init2(&ga, sizeof(char_u), 400);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002346 ga_concat(&ga, line);
2347 if (*p == '\\')
2348 ga_concat(&ga, p + 1);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002349 else if (*p == '|')
2350 {
2351 ga_concat(&ga, (char_u *)" ");
2352 ga_concat(&ga, p);
2353 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002354 for (;;)
2355 {
2356 vim_free(sp->nextline);
2357 sp->nextline = get_one_sourceline(sp);
2358 if (sp->nextline == NULL)
2359 break;
2360 p = skipwhite(sp->nextline);
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002361 if (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|'))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002362 {
2363 // Adjust the growsize to the current length to speed up
2364 // concatenating many lines.
2365 if (ga.ga_len > 400)
2366 {
2367 if (ga.ga_len > 8000)
2368 ga.ga_growsize = 8000;
2369 else
2370 ga.ga_growsize = ga.ga_len;
2371 }
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002372 if (*p == '\\')
2373 ga_concat(&ga, p + 1);
2374 else
2375 {
2376 ga_concat(&ga, (char_u *)" ");
2377 ga_concat(&ga, p);
2378 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002379 }
Bram Moolenaar5072b472021-06-03 21:56:10 +02002380 else if (!(p[0] == (comment_char)
Bram Moolenaar0f37e352021-06-02 15:28:15 +02002381 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002382 && !(do_vim9_all && (*p == NUL || vim9_comment_start(p))))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002383 break;
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002384 /* drop a # comment or "\ comment line */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002385 }
2386 ga_append(&ga, NUL);
2387 vim_free(line);
2388 line = ga.ga_data;
2389 }
2390 }
2391
2392 if (line != NULL && sp->conv.vc_type != CONV_NONE)
2393 {
2394 char_u *s;
2395
2396 // Convert the encoding of the script line.
2397 s = string_convert(&sp->conv, line, NULL);
2398 if (s != NULL)
2399 {
2400 vim_free(line);
2401 line = s;
2402 }
2403 }
2404
2405#ifdef FEAT_EVAL
2406 // Did we encounter a breakpoint?
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002407 if (!sp->source_from_buf && sp->breakpoint != 0
2408 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002409 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002410 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002411 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002412 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002413 sp->dbg_tick = debug_tick;
2414 }
2415#endif
2416
2417 return line;
2418}
2419
2420/*
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002421 * Returns TRUE if sourcing a script either from a file or a buffer.
2422 * Otherwise returns FALSE.
2423 */
2424 int
2425sourcing_a_script(exarg_T *eap)
2426{
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002427 return (getline_equal(eap->getline, eap->cookie, getsourceline));
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002428}
2429
2430/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002431 * ":scriptencoding": Set encoding conversion for a sourced script.
2432 */
2433 void
2434ex_scriptencoding(exarg_T *eap)
2435{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002436 source_cookie_T *sp;
2437 char_u *name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002438
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002439 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002440 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00002441 emsg(_(e_scriptencoding_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002442 return;
2443 }
2444
2445 if (*eap->arg != NUL)
2446 {
2447 name = enc_canonize(eap->arg);
2448 if (name == NULL) // out of memory
2449 return;
2450 }
2451 else
2452 name = eap->arg;
2453
2454 // Setup for conversion from the specified encoding to 'encoding'.
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002455 sp = (source_cookie_T *)getline_cookie(eap->getline, eap->cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002456 convert_setup(&sp->conv, name, p_enc);
2457
2458 if (name != eap->arg)
2459 vim_free(name);
2460}
2461
2462/*
2463 * ":scriptversion": Set Vim script version for a sourced script.
2464 */
2465 void
2466ex_scriptversion(exarg_T *eap UNUSED)
2467{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002468 int nr;
2469
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002470 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002471 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002472 emsg(_(e_scriptversion_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002473 return;
2474 }
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002475 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002477 emsg(_(e_cannot_use_scriptversion_after_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478 return;
2479 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002480
2481 nr = getdigits(&eap->arg);
2482 if (nr == 0 || *eap->arg != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002483 emsg(_(e_invalid_argument));
Bram Moolenaar67979662020-06-20 22:50:47 +02002484 else if (nr > SCRIPT_VERSION_MAX)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002485 semsg(_(e_scriptversion_not_supported_nr), nr);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002486 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002487 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002488 current_sctx.sc_version = nr;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002489#ifdef FEAT_EVAL
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002490 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002491#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002492 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002493}
2494
2495#if defined(FEAT_EVAL) || defined(PROTO)
2496/*
2497 * ":finish": Mark a sourced file as finished.
2498 */
2499 void
2500ex_finish(exarg_T *eap)
2501{
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002502 if (sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002503 do_finish(eap, FALSE);
2504 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00002505 emsg(_(e_finish_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002506}
2507
2508/*
2509 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
2510 * Also called for a pending finish at the ":endtry" or after returning from
2511 * an extra do_cmdline(). "reanimate" is used in the latter case.
2512 */
2513 void
2514do_finish(exarg_T *eap, int reanimate)
2515{
2516 int idx;
2517
2518 if (reanimate)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002519 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002520 eap->cookie))->finished = FALSE;
2521
2522 // Cleanup (and inactivate) conditionals, but stop when a try conditional
2523 // not in its finally clause (which then is to be executed next) is found.
2524 // In this case, make the ":finish" pending for execution at the ":endtry".
2525 // Otherwise, finish normally.
2526 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
2527 if (idx >= 0)
2528 {
2529 eap->cstack->cs_pending[idx] = CSTP_FINISH;
2530 report_make_pending(CSTP_FINISH, NULL);
2531 }
2532 else
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002533 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002534 eap->cookie))->finished = TRUE;
2535}
2536
2537
2538/*
2539 * Return TRUE when a sourced file had the ":finish" command: Don't give error
2540 * message for missing ":endif".
2541 * Return FALSE when not sourcing a file.
2542 */
2543 int
2544source_finished(
Bram Moolenaar66250c92020-08-20 15:02:42 +02002545 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002546 void *cookie)
2547{
2548 return (getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002549 && ((source_cookie_T *)getline_cookie(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002550 fgetline, cookie))->finished);
2551}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002552
2553/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002554 * Find the path of a script below the "autoload" directory.
2555 * Returns NULL if there is no "/autoload/" in the script name.
2556 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01002557 static char_u *
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002558script_name_after_autoload(scriptitem_T *si)
2559{
2560 char_u *p = si->sn_name;
2561 char_u *res = NULL;
2562
2563 for (;;)
2564 {
2565 char_u *n = (char_u *)strstr((char *)p, "autoload");
2566
2567 if (n == NULL)
2568 break;
2569 if (n > p && vim_ispathsep(n[-1]) && vim_ispathsep(n[8]))
2570 res = n + 9;
2571 p = n + 8;
2572 }
2573 return res;
2574}
2575
2576/*
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002577 * For an autoload script "autoload/dir/script.vim" return the prefix
2578 * "dir#script#" in allocated memory.
2579 * Returns NULL if anything is wrong.
2580 */
2581 char_u *
2582get_autoload_prefix(scriptitem_T *si)
2583{
2584 char_u *p = script_name_after_autoload(si);
2585 char_u *prefix;
2586
2587 if (p == NULL)
2588 return NULL;
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002589 prefix = vim_strsave(p);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002590 if (prefix == NULL)
2591 return NULL;
2592
2593 // replace all '/' with '#' and locate ".vim" at the end
2594 for (p = prefix; *p != NUL; p += mb_ptr2len(p))
2595 {
2596 if (vim_ispathsep(*p))
2597 *p = '#';
2598 else if (STRCMP(p, ".vim") == 0)
2599 {
2600 p[0] = '#';
2601 p[1] = NUL;
2602 return prefix;
2603 }
2604 }
2605
2606 // did not find ".vim" at the end
2607 vim_free(prefix);
2608 return NULL;
2609}
2610
2611/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002612 * If in a Vim9 autoload script return "name" with the autoload prefix for the
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002613 * script. If successful the returned name is allocated.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002614 * Otherwise it returns "name" unmodified.
2615 */
2616 char_u *
2617may_prefix_autoload(char_u *name)
2618{
2619 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
2620 {
2621 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2622
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002623 if (si->sn_autoload_prefix != NULL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002624 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002625 char_u *basename = name;
2626 size_t len;
2627 char_u *res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002628
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002629 if (*name == K_SPECIAL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002630 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002631 char_u *p = vim_strchr(name, '_');
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002632
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002633 // skip over "<SNR>99_"
2634 if (p != NULL)
2635 basename = p + 1;
2636 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002637
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002638 len = STRLEN(si->sn_autoload_prefix) + STRLEN(basename) + 2;
2639 res = alloc(len);
2640 if (res != NULL)
2641 {
2642 vim_snprintf((char *)res, len, "%s%s",
2643 si->sn_autoload_prefix, basename);
2644 return res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002645 }
2646 }
2647 }
2648 return name;
2649}
2650
2651/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002652 * Return the autoload script name for a function or variable name.
2653 * Returns NULL when out of memory.
2654 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
2655 */
2656 char_u *
2657autoload_name(char_u *name)
2658{
2659 char_u *p, *q = NULL;
2660 char_u *scriptname;
2661
2662 // Get the script file name: replace '#' with '/', append ".vim".
2663 scriptname = alloc(STRLEN(name) + 14);
2664 if (scriptname == NULL)
2665 return NULL;
2666 STRCPY(scriptname, "autoload/");
Bram Moolenaara1773442020-08-12 15:21:22 +02002667 STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002668 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
2669 q = p, ++p)
2670 *p = '/';
2671 STRCPY(q, ".vim");
2672 return scriptname;
2673}
2674
2675/*
2676 * If "name" has a package name try autoloading the script for it.
2677 * Return TRUE if a package was loaded.
2678 */
2679 int
2680script_autoload(
2681 char_u *name,
2682 int reload) // load script again when already loaded
2683{
2684 char_u *p;
2685 char_u *scriptname, *tofree;
2686 int ret = FALSE;
2687 int i;
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002688 int ret_sid;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002689
Bram Moolenaar89445512022-04-14 12:58:23 +01002690 // If the name starts with "<SNR>123_" then "123" is the script ID.
2691 if (name[0] == K_SPECIAL && name[1] == KS_EXTRA && name[2] == KE_SNR)
2692 {
2693 p = name + 3;
2694 ret_sid = (int)getdigits(&p);
2695 if (*p == '_' && SCRIPT_ID_VALID(ret_sid))
2696 {
2697 may_load_script(ret_sid, &ret);
2698 return ret;
2699 }
2700 }
2701
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002702 // If there is no '#' after name[0] there is no package name.
2703 p = vim_strchr(name, AUTOLOAD_CHAR);
2704 if (p == NULL || p == name)
2705 return FALSE;
2706
2707 tofree = scriptname = autoload_name(name);
2708 if (scriptname == NULL)
2709 return FALSE;
2710
2711 // Find the name in the list of previously loaded package names. Skip
2712 // "autoload/", it's always the same.
2713 for (i = 0; i < ga_loaded.ga_len; ++i)
2714 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2715 break;
2716 if (!reload && i < ga_loaded.ga_len)
2717 ret = FALSE; // was loaded already
2718 else
2719 {
2720 // Remember the name if it wasn't loaded already.
2721 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2722 {
2723 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2724 tofree = NULL;
2725 }
2726
2727 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002728 // Use "ret_sid" to avoid loading the same script again.
=?UTF-8?q?Bj=C3=B6rn=20Linse?=223a9502022-01-31 17:26:05 +00002729 if (source_in_path(p_rtp, scriptname, DIP_START, &ret_sid) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002730 ret = TRUE;
2731 }
2732
2733 vim_free(tofree);
2734 return ret;
2735}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002736#endif