blob: 3b9fec1cd51ebeaf510b24760b1d4f9777e99c71 [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
Bram Moolenaar307c5a52019-08-25 15:41:00 +020021/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010022 * Initialize the execution stack.
23 */
24 void
25estack_init(void)
26{
27 estack_T *entry;
28
29 if (ga_grow(&exestack, 10) == FAIL)
30 mch_exit(0);
31 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
32 entry->es_type = ETYPE_TOP;
33 entry->es_name = NULL;
34 entry->es_lnum = 0;
Bram Moolenaar09d46402019-12-29 23:53:01 +010035#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010036 entry->es_info.ufunc = NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +010037#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010038 ++exestack.ga_len;
39}
40
41/*
42 * Add an item to the execution stack.
43 * Returns the new entry or NULL when out of memory.
44 */
45 estack_T *
46estack_push(etype_T type, char_u *name, long lnum)
47{
48 estack_T *entry;
49
50 // If memory allocation fails then we'll pop more than we push, eventually
51 // at the top level it will be OK again.
52 if (ga_grow(&exestack, 1) == OK)
53 {
54 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
55 entry->es_type = type;
56 entry->es_name = name;
57 entry->es_lnum = lnum;
Bram Moolenaar09d46402019-12-29 23:53:01 +010058#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010059 entry->es_info.ufunc = NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +010060#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010061 ++exestack.ga_len;
62 return entry;
63 }
64 return NULL;
65}
66
Bram Moolenaar09d46402019-12-29 23:53:01 +010067#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010068/*
69 * Add a user function to the execution stack.
70 */
Bram Moolenaarc620c052020-07-08 15:16:19 +020071 estack_T *
Bram Moolenaar25e0f582020-05-25 22:36:50 +020072estack_push_ufunc(ufunc_T *ufunc, long lnum)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010073{
Bram Moolenaar25e0f582020-05-25 22:36:50 +020074 estack_T *entry = estack_push(ETYPE_UFUNC,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010075 ufunc->uf_name_exp != NULL
76 ? ufunc->uf_name_exp : ufunc->uf_name, lnum);
77 if (entry != NULL)
78 entry->es_info.ufunc = ufunc;
Bram Moolenaarc620c052020-07-08 15:16:19 +020079 return entry;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010080}
Bram Moolenaar25e0f582020-05-25 22:36:50 +020081
82/*
83 * Return TRUE if "ufunc" with "lnum" is already at the top of the exe stack.
84 */
85 int
86estack_top_is_ufunc(ufunc_T *ufunc, long lnum)
87{
88 estack_T *entry;
89
90 if (exestack.ga_len == 0)
91 return FALSE;
92 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
93 return entry->es_type == ETYPE_UFUNC
94 && STRCMP( entry->es_name, ufunc->uf_name_exp != NULL
95 ? ufunc->uf_name_exp : ufunc->uf_name) == 0
96 && entry->es_lnum == lnum;
97}
Bram Moolenaar09d46402019-12-29 23:53:01 +010098#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010099
100/*
Bram Moolenaarc620c052020-07-08 15:16:19 +0200101 * Take an item off of the execution stack and return it.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100102 */
Bram Moolenaarc620c052020-07-08 15:16:19 +0200103 estack_T *
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100104estack_pop(void)
105{
Bram Moolenaarc620c052020-07-08 15:16:19 +0200106 if (exestack.ga_len == 0)
107 return NULL;
108 --exestack.ga_len;
109 return ((estack_T *)exestack.ga_data) + exestack.ga_len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100110}
111
112/*
113 * Get the current value for <sfile> in allocated memory.
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200114 * "which" is ESTACK_SFILE for <sfile> and ESTACK_STACK for <stack>.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100115 */
116 char_u *
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200117estack_sfile(estack_arg_T which UNUSED)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100118{
Bram Moolenaar85b09572019-12-30 10:57:00 +0100119 estack_T *entry;
120#ifdef FEAT_EVAL
Bram Moolenaara5d04232020-07-26 15:37:02 +0200121 garray_T ga;
Bram Moolenaar4d7a2482020-01-06 19:53:43 +0100122 size_t len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100123 int idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200124 etype_T last_type = ETYPE_SCRIPT;
125 char *type_name;
Bram Moolenaar85b09572019-12-30 10:57:00 +0100126#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100127
128 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100129#ifdef FEAT_EVAL
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200130 if (which == ESTACK_SFILE && entry->es_type != ETYPE_UFUNC)
Bram Moolenaar09d46402019-12-29 23:53:01 +0100131#endif
Bram Moolenaara5d04232020-07-26 15:37:02 +0200132 {
133 if (entry->es_name == NULL)
134 return NULL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100135 return vim_strsave(entry->es_name);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200136 }
Bram Moolenaar09d46402019-12-29 23:53:01 +0100137#ifdef FEAT_EVAL
Bram Moolenaarc4492712021-11-22 15:37:15 +0000138 // expand('<sfile>') works in a function for backwards compatibility, but
139 // may give an unexpected result. Disallow it in Vim 9 script.
140 if (which == ESTACK_SFILE && in_vim9script())
141 {
142 int save_emsg_off = emsg_off;
143
144 if (emsg_off == 1)
145 // f_expand() silences errors but we do want this one
146 emsg_off = 0;
147 emsg(_(e_cannot_expand_sfile_in_vim9_function));
148 emsg_off = save_emsg_off;
149 return NULL;
150 }
151
Bram Moolenaara5d04232020-07-26 15:37:02 +0200152 // Give information about each stack entry up to the root.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100153 // For a function we compose the call stack, as it was done in the past:
154 // "function One[123]..Two[456]..Three"
Bram Moolenaara5d04232020-07-26 15:37:02 +0200155 ga_init2(&ga, sizeof(char), 100);
156 for (idx = 0; idx < exestack.ga_len; ++idx)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100157 {
158 entry = ((estack_T *)exestack.ga_data) + idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200159 if (entry->es_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100160 {
Bram Moolenaara810db32020-09-11 17:59:23 +0200161 long lnum = 0;
162 char *dots;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200163
Bram Moolenaara5d04232020-07-26 15:37:02 +0200164 len = STRLEN(entry->es_name) + 15;
165 type_name = "";
166 if (entry->es_type != last_type)
167 {
168 switch (entry->es_type)
169 {
170 case ETYPE_SCRIPT: type_name = "script "; break;
171 case ETYPE_UFUNC: type_name = "function "; break;
172 default: type_name = ""; break;
173 }
174 last_type = entry->es_type;
175 }
176 len += STRLEN(type_name);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200177 if (ga_grow(&ga, (int)len) == FAIL)
Bram Moolenaara5d04232020-07-26 15:37:02 +0200178 break;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200179 if (idx == exestack.ga_len - 1)
180 lnum = which == ESTACK_STACK ? SOURCING_LNUM : 0;
181 else
182 lnum = entry->es_lnum;
Bram Moolenaara810db32020-09-11 17:59:23 +0200183 dots = idx == exestack.ga_len - 1 ? "" : "..";
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200184 if (lnum == 0)
185 // For the bottom entry of <sfile>: do not add the line number,
186 // it is used in <slnum>. Also leave it out when the number is
187 // not set.
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200188 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s%s",
Bram Moolenaara810db32020-09-11 17:59:23 +0200189 type_name, entry->es_name, dots);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200190 else
Bram Moolenaara810db32020-09-11 17:59:23 +0200191 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s[%ld]%s",
192 type_name, entry->es_name, lnum, dots);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200193 ga.ga_len += (int)STRLEN((char *)ga.ga_data + ga.ga_len);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100194 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100195 }
196
Bram Moolenaara5d04232020-07-26 15:37:02 +0200197 return (char_u *)ga.ga_data;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100198#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100199}
200
201/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200202 * ":runtime [what] {name}"
203 */
204 void
205ex_runtime(exarg_T *eap)
206{
207 char_u *arg = eap->arg;
208 char_u *p = skiptowhite(arg);
209 int len = (int)(p - arg);
210 int flags = eap->forceit ? DIP_ALL : 0;
211
212 if (STRNCMP(arg, "START", len) == 0)
213 {
214 flags += DIP_START + DIP_NORTP;
215 arg = skipwhite(arg + len);
216 }
217 else if (STRNCMP(arg, "OPT", len) == 0)
218 {
219 flags += DIP_OPT + DIP_NORTP;
220 arg = skipwhite(arg + len);
221 }
222 else if (STRNCMP(arg, "PACK", len) == 0)
223 {
224 flags += DIP_START + DIP_OPT + DIP_NORTP;
225 arg = skipwhite(arg + len);
226 }
227 else if (STRNCMP(arg, "ALL", len) == 0)
228 {
229 flags += DIP_START + DIP_OPT;
230 arg = skipwhite(arg + len);
231 }
232
233 source_runtime(arg, flags);
234}
235
236 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100237source_callback(char_u *fname, void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200238{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200240}
241
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000242#ifdef FEAT_EVAL
243/*
244 * Find an already loaded script "name".
245 * If found returns its script ID. If not found returns -1.
246 */
247 static int
248find_script_by_name(char_u *name)
249{
250 int sid;
251 scriptitem_T *si;
252
253 for (sid = script_items.ga_len; sid > 0; --sid)
254 {
255 // We used to check inode here, but that doesn't work:
256 // - If a script is edited and written, it may get a different
257 // inode number, even though to the user it is the same script.
258 // - If a script is deleted and another script is written, with a
259 // different name, the inode may be re-used.
260 si = SCRIPT_ITEM(sid);
261 if (si->sn_name != NULL && fnamecmp(si->sn_name, name) == 0)
262 return sid;
263 }
264 return -1;
265}
266
267/*
268 * Add a new scriptitem with all items initialized.
269 * When running out of memory "error" is set to FAIL.
270 * Returns the script ID.
271 */
272 static int
273get_new_scriptitem(int *error)
274{
275 static scid_T last_current_SID = 0;
276 int sid = ++last_current_SID;
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000277 scriptitem_T *si = NULL;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000278
279 if (ga_grow(&script_items, (int)(sid - script_items.ga_len)) == FAIL)
280 {
281 *error = FAIL;
282 return sid;
283 }
284 while (script_items.ga_len < sid)
285 {
286 si = ALLOC_CLEAR_ONE(scriptitem_T);
287 if (si == NULL)
288 {
289 *error = FAIL;
290 return sid;
291 }
292 ++script_items.ga_len;
293 SCRIPT_ITEM(script_items.ga_len) = si;
294 si->sn_name = NULL;
295 si->sn_version = 1;
296
297 // Allocate the local script variables to use for this script.
298 new_script_vars(script_items.ga_len);
299 ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
300 hash_init(&si->sn_all_vars.dv_hashtab);
301 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
302 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
303# ifdef FEAT_PROFILE
304 si->sn_prof_on = FALSE;
305# endif
306 }
307
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000308 // "si" can't be NULL, check only to avoid a compiler warning
309 if (si != NULL)
310 // Used to check script variable index is still valid.
311 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000312
313 return sid;
314}
315
316 static void
317find_script_callback(char_u *fname, void *cookie)
318{
319 int sid;
320 int error = OK;
321 int *ret_sid = cookie;
322
323 sid = find_script_by_name(fname);
324 if (sid < 0)
325 {
326 // script does not exist yet, create a new scriptitem
327 sid = get_new_scriptitem(&error);
328 if (error == OK)
329 {
330 scriptitem_T *si = SCRIPT_ITEM(sid);
331
332 si->sn_name = vim_strsave(fname);
333 si->sn_state = SN_STATE_NOT_LOADED;
334 }
335 }
336 *ret_sid = sid;
337}
338#endif
339
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200340/*
341 * Find the file "name" in all directories in "path" and invoke
342 * "callback(fname, cookie)".
343 * "name" can contain wildcards.
344 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
345 * When "flags" has DIP_DIR: find directories instead of files.
346 * When "flags" has DIP_ERR: give an error message if there is no match.
347 *
348 * return FAIL when no file could be sourced, OK otherwise.
349 */
350 int
351do_in_path(
352 char_u *path,
353 char_u *name,
354 int flags,
355 void (*callback)(char_u *fname, void *ck),
356 void *cookie)
357{
358 char_u *rtp;
359 char_u *np;
360 char_u *buf;
361 char_u *rtp_copy;
362 char_u *tail;
363 int num_files;
364 char_u **files;
365 int i;
366 int did_one = FALSE;
367#ifdef AMIGA
368 struct Process *proc = (struct Process *)FindTask(0L);
369 APTR save_winptr = proc->pr_WindowPtr;
370
371 // Avoid a requester here for a volume that doesn't exist.
372 proc->pr_WindowPtr = (APTR)-1L;
373#endif
374
375 // Make a copy of 'runtimepath'. Invoking the callback may change the
376 // value.
377 rtp_copy = vim_strsave(path);
378 buf = alloc(MAXPATHL);
379 if (buf != NULL && rtp_copy != NULL)
380 {
Bram Moolenaar647a5302020-05-03 17:01:24 +0200381 if (p_verbose > 10 && name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200382 {
383 verbose_enter();
384 smsg(_("Searching for \"%s\" in \"%s\""),
385 (char *)name, (char *)path);
386 verbose_leave();
387 }
388
389 // Loop over all entries in 'runtimepath'.
390 rtp = rtp_copy;
391 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
392 {
393 size_t buflen;
394
395 // Copy the path from 'runtimepath' to buf[].
396 copy_option_part(&rtp, buf, MAXPATHL, ",");
397 buflen = STRLEN(buf);
398
399 // Skip after or non-after directories.
400 if (flags & (DIP_NOAFTER | DIP_AFTER))
401 {
402 int is_after = buflen >= 5
403 && STRCMP(buf + buflen - 5, "after") == 0;
404
405 if ((is_after && (flags & DIP_NOAFTER))
406 || (!is_after && (flags & DIP_AFTER)))
407 continue;
408 }
409
410 if (name == NULL)
411 {
412 (*callback)(buf, (void *) &cookie);
413 if (!did_one)
414 did_one = (cookie == NULL);
415 }
416 else if (buflen + STRLEN(name) + 2 < MAXPATHL)
417 {
418 add_pathsep(buf);
419 tail = buf + STRLEN(buf);
420
421 // Loop over all patterns in "name"
422 np = name;
423 while (*np != NUL && ((flags & DIP_ALL) || !did_one))
424 {
425 // Append the pattern from "name" to buf[].
426 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
427 "\t ");
428
Bram Moolenaar647a5302020-05-03 17:01:24 +0200429 if (p_verbose > 10)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200430 {
431 verbose_enter();
432 smsg(_("Searching for \"%s\""), buf);
433 verbose_leave();
434 }
435
436 // Expand wildcards, invoke the callback for each match.
437 if (gen_expand_wildcards(1, &buf, &num_files, &files,
438 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
439 {
440 for (i = 0; i < num_files; ++i)
441 {
442 (*callback)(files[i], cookie);
443 did_one = TRUE;
444 if (!(flags & DIP_ALL))
445 break;
446 }
447 FreeWild(num_files, files);
448 }
449 }
450 }
451 }
452 }
453 vim_free(buf);
454 vim_free(rtp_copy);
455 if (!did_one && name != NULL)
456 {
457 char *basepath = path == p_rtp ? "runtimepath" : "packpath";
458
459 if (flags & DIP_ERR)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000460 semsg(_(e_directory_not_found_in_str_str), basepath, name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200461 else if (p_verbose > 0)
462 {
463 verbose_enter();
464 smsg(_("not found in '%s': \"%s\""), basepath, name);
465 verbose_leave();
466 }
467 }
468
469#ifdef AMIGA
470 proc->pr_WindowPtr = save_winptr;
471#endif
472
473 return did_one ? OK : FAIL;
474}
475
476/*
477 * Find "name" in "path". When found, invoke the callback function for
478 * it: callback(fname, "cookie")
479 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first
480 * one is used.
481 * Returns OK when at least one match found, FAIL otherwise.
482 *
483 * If "name" is NULL calls callback for each entry in "path". Cookie is
484 * passed by reference in this case, setting it to NULL indicates that callback
485 * has done its job.
486 */
487 static int
488do_in_path_and_pp(
489 char_u *path,
490 char_u *name,
491 int flags,
492 void (*callback)(char_u *fname, void *ck),
493 void *cookie)
494{
495 int done = FAIL;
496 char_u *s;
497 int len;
498 char *start_dir = "pack/*/start/*/%s";
499 char *opt_dir = "pack/*/opt/*/%s";
500
501 if ((flags & DIP_NORTP) == 0)
502 done = do_in_path(path, name, flags, callback, cookie);
503
504 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
505 {
506 len = (int)(STRLEN(start_dir) + STRLEN(name));
507 s = alloc(len);
508 if (s == NULL)
509 return FAIL;
510 vim_snprintf((char *)s, len, start_dir, name);
511 done = do_in_path(p_pp, s, flags, callback, cookie);
512 vim_free(s);
513 }
514
515 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
516 {
517 len = (int)(STRLEN(opt_dir) + STRLEN(name));
518 s = alloc(len);
519 if (s == NULL)
520 return FAIL;
521 vim_snprintf((char *)s, len, opt_dir, name);
522 done = do_in_path(p_pp, s, flags, callback, cookie);
523 vim_free(s);
524 }
525
526 return done;
527}
528
529/*
530 * Just like do_in_path_and_pp(), using 'runtimepath' for "path".
531 */
532 int
533do_in_runtimepath(
534 char_u *name,
535 int flags,
536 void (*callback)(char_u *fname, void *ck),
537 void *cookie)
538{
539 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
540}
541
542/*
543 * Source the file "name" from all directories in 'runtimepath'.
544 * "name" can contain wildcards.
545 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
546 *
547 * return FAIL when no file could be sourced, OK otherwise.
548 */
549 int
550source_runtime(char_u *name, int flags)
551{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100552 return source_in_path(p_rtp, name, flags, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200553}
554
555/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000556 * Just like source_runtime(), but use "path" instead of 'runtimepath'
557 * and return the script ID in "ret_sid".
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200558 */
559 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200561{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200563}
564
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200565#if defined(FEAT_EVAL) || defined(PROTO)
566
567/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000568 * Find "name" in 'runtimepath'. If found a new scriptitem is created for it
569 * and it's script ID is returned.
570 * If not found returns -1.
571 */
572 int
573find_script_in_rtp(char_u *name)
574{
575 int sid = -1;
576
577 (void)do_in_path_and_pp(p_rtp, name, DIP_NOAFTER,
578 find_script_callback, &sid);
579 return sid;
580}
581
582/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200583 * Expand wildcards in "pat" and invoke do_source() for each match.
584 */
585 static void
586source_all_matches(char_u *pat)
587{
588 int num_files;
589 char_u **files;
590 int i;
591
592 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK)
593 {
594 for (i = 0; i < num_files; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100595 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200596 FreeWild(num_files, files);
597 }
598}
599
600/*
601 * Add the package directory to 'runtimepath'.
602 */
603 static int
604add_pack_dir_to_rtp(char_u *fname)
605{
606 char_u *p4, *p3, *p2, *p1, *p;
607 char_u *entry;
608 char_u *insp = NULL;
609 int c;
610 char_u *new_rtp;
611 int keep;
612 size_t oldlen;
613 size_t addlen;
614 size_t new_rtp_len;
615 char_u *afterdir = NULL;
616 size_t afterlen = 0;
617 char_u *after_insp = NULL;
618 char_u *ffname = NULL;
619 size_t fname_len;
620 char_u *buf = NULL;
621 char_u *rtp_ffname;
622 int match;
623 int retval = FAIL;
624
625 p4 = p3 = p2 = p1 = get_past_head(fname);
626 for (p = p1; *p; MB_PTR_ADV(p))
627 if (vim_ispathsep_nocolon(*p))
628 {
629 p4 = p3; p3 = p2; p2 = p1; p1 = p;
630 }
631
632 // now we have:
633 // rtp/pack/name/start/name
634 // p4 p3 p2 p1
635 //
636 // find the part up to "pack" in 'runtimepath'
637 c = *++p4; // append pathsep in order to expand symlink
638 *p4 = NUL;
639 ffname = fix_fname(fname);
640 *p4 = c;
641 if (ffname == NULL)
642 return FAIL;
643
644 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
645 // Also stop at the first "after" directory.
646 fname_len = STRLEN(ffname);
647 buf = alloc(MAXPATHL);
648 if (buf == NULL)
649 goto theend;
650 for (entry = p_rtp; *entry != NUL; )
651 {
652 char_u *cur_entry = entry;
653
654 copy_option_part(&entry, buf, MAXPATHL, ",");
655 if (insp == NULL)
656 {
657 add_pathsep(buf);
658 rtp_ffname = fix_fname(buf);
659 if (rtp_ffname == NULL)
660 goto theend;
661 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
662 vim_free(rtp_ffname);
663 if (match)
664 // Insert "ffname" after this entry (and comma).
665 insp = entry;
666 }
667
668 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
669 && p > buf
670 && vim_ispathsep(p[-1])
671 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
672 {
673 if (insp == NULL)
674 // Did not find "ffname" before the first "after" directory,
675 // insert it before this entry.
676 insp = cur_entry;
677 after_insp = cur_entry;
678 break;
679 }
680 }
681
682 if (insp == NULL)
683 // Both "fname" and "after" not found, append at the end.
684 insp = p_rtp + STRLEN(p_rtp);
685
686 // check if rtp/pack/name/start/name/after exists
687 afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
688 if (afterdir != NULL && mch_isdir(afterdir))
689 afterlen = STRLEN(afterdir) + 1; // add one for comma
690
691 oldlen = STRLEN(p_rtp);
692 addlen = STRLEN(fname) + 1; // add one for comma
693 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
694 if (new_rtp == NULL)
695 goto theend;
696
697 // We now have 'rtp' parts: {keep}{keep_after}{rest}.
698 // Create new_rtp, first: {keep},{fname}
699 keep = (int)(insp - p_rtp);
700 mch_memmove(new_rtp, p_rtp, keep);
701 new_rtp_len = keep;
702 if (*insp == NUL)
703 new_rtp[new_rtp_len++] = ','; // add comma before
704 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
705 new_rtp_len += addlen - 1;
706 if (*insp != NUL)
707 new_rtp[new_rtp_len++] = ','; // add comma after
708
709 if (afterlen > 0 && after_insp != NULL)
710 {
711 int keep_after = (int)(after_insp - p_rtp);
712
713 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
714 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
715 keep_after - keep);
716 new_rtp_len += keep_after - keep;
717 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
718 new_rtp_len += afterlen - 1;
719 new_rtp[new_rtp_len++] = ',';
720 keep = keep_after;
721 }
722
723 if (p_rtp[keep] != NUL)
724 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
725 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
726 else
727 new_rtp[new_rtp_len] = NUL;
728
729 if (afterlen > 0 && after_insp == NULL)
730 {
731 // Append afterdir when "after" was not found:
732 // {keep},{fname}{rest},{afterdir}
733 STRCAT(new_rtp, ",");
734 STRCAT(new_rtp, afterdir);
735 }
736
737 set_option_value((char_u *)"rtp", 0L, new_rtp, 0);
738 vim_free(new_rtp);
739 retval = OK;
740
741theend:
742 vim_free(buf);
743 vim_free(ffname);
744 vim_free(afterdir);
745 return retval;
746}
747
748/*
749 * Load scripts in "plugin" and "ftdetect" directories of the package.
750 */
751 static int
752load_pack_plugin(char_u *fname)
753{
754 static char *plugpat = "%s/plugin/**/*.vim";
755 static char *ftpat = "%s/ftdetect/*.vim";
756 int len;
757 char_u *ffname = fix_fname(fname);
758 char_u *pat = NULL;
759 int retval = FAIL;
760
761 if (ffname == NULL)
762 return FAIL;
763 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
764 pat = alloc(len);
765 if (pat == NULL)
766 goto theend;
767 vim_snprintf((char *)pat, len, plugpat, ffname);
768 source_all_matches(pat);
769
770 {
771 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
772
773 // If runtime/filetype.vim wasn't loaded yet, the scripts will be
774 // found when it loads.
775 if (cmd != NULL && eval_to_number(cmd) > 0)
776 {
777 do_cmdline_cmd((char_u *)"augroup filetypedetect");
778 vim_snprintf((char *)pat, len, ftpat, ffname);
779 source_all_matches(pat);
780 do_cmdline_cmd((char_u *)"augroup END");
781 }
782 vim_free(cmd);
783 }
784 vim_free(pat);
785 retval = OK;
786
787theend:
788 vim_free(ffname);
789 return retval;
790}
791
792// used for "cookie" of add_pack_plugin()
793static int APP_ADD_DIR;
794static int APP_LOAD;
795static int APP_BOTH;
796
797 static void
798add_pack_plugin(char_u *fname, void *cookie)
799{
800 if (cookie != &APP_LOAD)
801 {
802 char_u *buf = alloc(MAXPATHL);
803 char_u *p;
804 int found = FALSE;
805
806 if (buf == NULL)
807 return;
808 p = p_rtp;
809 while (*p != NUL)
810 {
811 copy_option_part(&p, buf, MAXPATHL, ",");
812 if (pathcmp((char *)buf, (char *)fname, -1) == 0)
813 {
814 found = TRUE;
815 break;
816 }
817 }
818 vim_free(buf);
819 if (!found)
820 // directory is not yet in 'runtimepath', add it
821 if (add_pack_dir_to_rtp(fname) == FAIL)
822 return;
823 }
824
825 if (cookie != &APP_ADD_DIR)
826 load_pack_plugin(fname);
827}
828
829/*
830 * Add all packages in the "start" directory to 'runtimepath'.
831 */
832 void
833add_pack_start_dirs(void)
834{
835 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
836 add_pack_plugin, &APP_ADD_DIR);
837}
838
839/*
840 * Load plugins from all packages in the "start" directory.
841 */
842 void
843load_start_packages(void)
844{
845 did_source_packages = TRUE;
846 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
847 add_pack_plugin, &APP_LOAD);
848}
849
850/*
851 * ":packloadall"
852 * Find plugins in the package directories and source them.
853 */
854 void
855ex_packloadall(exarg_T *eap)
856{
857 if (!did_source_packages || eap->forceit)
858 {
859 // First do a round to add all directories to 'runtimepath', then load
860 // the plugins. This allows for plugins to use an autoload directory
861 // of another plugin.
862 add_pack_start_dirs();
863 load_start_packages();
864 }
865}
866
867/*
868 * ":packadd[!] {name}"
869 */
870 void
871ex_packadd(exarg_T *eap)
872{
873 static char *plugpat = "pack/*/%s/%s";
874 int len;
875 char *pat;
876 int round;
877 int res = OK;
878
879 // Round 1: use "start", round 2: use "opt".
880 for (round = 1; round <= 2; ++round)
881 {
882 // Only look under "start" when loading packages wasn't done yet.
883 if (round == 1 && did_source_packages)
884 continue;
885
886 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
887 pat = alloc(len);
888 if (pat == NULL)
889 return;
890 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
891 // The first round don't give a "not found" error, in the second round
892 // only when nothing was found in the first round.
893 res = do_in_path(p_pp, (char_u *)pat,
894 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
895 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
896 vim_free(pat);
897 }
898}
899#endif
900
901/*
Bram Moolenaar26262f82019-09-04 20:59:15 +0200902 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
903 * list of file names in allocated memory.
904 */
905 void
906remove_duplicates(garray_T *gap)
907{
908 int i;
909 int j;
910 char_u **fnames = (char_u **)gap->ga_data;
911
912 sort_strings(fnames, gap->ga_len);
913 for (i = gap->ga_len - 1; i > 0; --i)
914 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
915 {
916 vim_free(fnames[i]);
917 for (j = i + 1; j < gap->ga_len; ++j)
918 fnames[j - 1] = fnames[j];
919 --gap->ga_len;
920 }
921}
922
923/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200924 * Expand color scheme, compiler or filetype names.
925 * Search from 'runtimepath':
926 * 'runtimepath'/{dirnames}/{pat}.vim
927 * When "flags" has DIP_START: search also from 'start' of 'packpath':
928 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
929 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
930 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
931 * "dirnames" is an array with one or more directory names.
932 */
933 int
934ExpandRTDir(
935 char_u *pat,
936 int flags,
937 int *num_file,
938 char_u ***file,
939 char *dirnames[])
940{
941 char_u *s;
942 char_u *e;
943 char_u *match;
944 garray_T ga;
945 int i;
946 int pat_len;
947
948 *num_file = 0;
949 *file = NULL;
950 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000951 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200952
953 for (i = 0; dirnames[i] != NULL; ++i)
954 {
955 s = alloc(STRLEN(dirnames[i]) + pat_len + 7);
956 if (s == NULL)
957 {
958 ga_clear_strings(&ga);
959 return FAIL;
960 }
961 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
962 globpath(p_rtp, s, &ga, 0);
963 vim_free(s);
964 }
965
966 if (flags & DIP_START) {
967 for (i = 0; dirnames[i] != NULL; ++i)
968 {
969 s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
970 if (s == NULL)
971 {
972 ga_clear_strings(&ga);
973 return FAIL;
974 }
975 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
976 globpath(p_pp, s, &ga, 0);
977 vim_free(s);
978 }
979 }
980
981 if (flags & DIP_OPT) {
982 for (i = 0; dirnames[i] != NULL; ++i)
983 {
984 s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
985 if (s == NULL)
986 {
987 ga_clear_strings(&ga);
988 return FAIL;
989 }
990 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
991 globpath(p_pp, s, &ga, 0);
992 vim_free(s);
993 }
994 }
995
996 for (i = 0; i < ga.ga_len; ++i)
997 {
998 match = ((char_u **)ga.ga_data)[i];
999 s = match;
1000 e = s + STRLEN(s);
1001 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
1002 {
1003 e -= 4;
1004 for (s = e; s > match; MB_PTR_BACK(match, s))
1005 if (s < match || vim_ispathsep(*s))
1006 break;
1007 ++s;
1008 *e = NUL;
1009 mch_memmove(match, s, e - s + 1);
1010 }
1011 }
1012
1013 if (ga.ga_len == 0)
1014 return FAIL;
1015
1016 // Sort and remove duplicates which can happen when specifying multiple
1017 // directories in dirnames.
1018 remove_duplicates(&ga);
1019
1020 *file = ga.ga_data;
1021 *num_file = ga.ga_len;
1022 return OK;
1023}
1024
1025/*
1026 * Expand loadplugin names:
1027 * 'packpath'/pack/ * /opt/{pat}
1028 */
1029 int
1030ExpandPackAddDir(
1031 char_u *pat,
1032 int *num_file,
1033 char_u ***file)
1034{
1035 char_u *s;
1036 char_u *e;
1037 char_u *match;
1038 garray_T ga;
1039 int i;
1040 int pat_len;
1041
1042 *num_file = 0;
1043 *file = NULL;
1044 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001045 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001046
1047 s = alloc(pat_len + 26);
1048 if (s == NULL)
1049 {
1050 ga_clear_strings(&ga);
1051 return FAIL;
1052 }
1053 sprintf((char *)s, "pack/*/opt/%s*", pat);
1054 globpath(p_pp, s, &ga, 0);
1055 vim_free(s);
1056
1057 for (i = 0; i < ga.ga_len; ++i)
1058 {
1059 match = ((char_u **)ga.ga_data)[i];
1060 s = gettail(match);
1061 e = s + STRLEN(s);
1062 mch_memmove(match, s, e - s + 1);
1063 }
1064
1065 if (ga.ga_len == 0)
1066 return FAIL;
1067
1068 // Sort and remove duplicates which can happen when specifying multiple
1069 // directories in dirnames.
1070 remove_duplicates(&ga);
1071
1072 *file = ga.ga_data;
1073 *num_file = ga.ga_len;
1074 return OK;
1075}
1076
1077 static void
1078cmd_source(char_u *fname, exarg_T *eap)
1079{
1080 if (*fname == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001081 emsg(_(e_argument_required));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001082
1083 else if (eap != NULL && eap->forceit)
1084 // ":source!": read Normal mode commands
1085 // Need to execute the commands directly. This is required at least
1086 // for:
1087 // - ":g" command busy
1088 // - after ":argdo", ":windo" or ":bufdo"
1089 // - another command follows
1090 // - inside a loop
1091 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
1092#ifdef FEAT_EVAL
1093 || eap->cstack->cs_idx >= 0
1094#endif
1095 );
1096
1097 // ":source" read ex commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001098 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001099 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001100}
1101
1102/*
1103 * ":source {fname}"
1104 */
1105 void
1106ex_source(exarg_T *eap)
1107{
1108#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02001109 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001110 {
1111 char_u *fname = NULL;
1112
1113 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
1114 NULL, NULL,
1115 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1116 if (fname != NULL)
1117 {
1118 cmd_source(fname, eap);
1119 vim_free(fname);
1120 }
1121 }
1122 else
1123#endif
1124 cmd_source(eap->arg, eap);
1125}
1126
1127#if defined(FEAT_EVAL) || defined(PROTO)
1128/*
1129 * ":options"
1130 */
1131 void
1132ex_options(
1133 exarg_T *eap UNUSED)
1134{
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001135 char_u buf[500];
1136 int multi_mods = 0;
1137
1138 buf[0] = NUL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001139 (void)add_win_cmd_modifers(buf, &cmdmod, &multi_mods);
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001140
1141 vim_setenv((char_u *)"OPTWIN_CMD", buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001142 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
1143}
1144#endif
1145
1146/*
1147 * ":source" and associated commands.
1148 */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001149
1150#ifdef FEAT_EVAL
1151/*
1152 * Return the address holding the next breakpoint line for a source cookie.
1153 */
1154 linenr_T *
1155source_breakpoint(void *cookie)
1156{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001157 return &((source_cookie_T *)cookie)->breakpoint;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001158}
1159
1160/*
1161 * Return the address holding the debug tick for a source cookie.
1162 */
1163 int *
1164source_dbg_tick(void *cookie)
1165{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001166 return &((source_cookie_T *)cookie)->dbg_tick;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001167}
1168
1169/*
1170 * Return the nesting level for a source cookie.
1171 */
1172 int
1173source_level(void *cookie)
1174{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001175 return ((source_cookie_T *)cookie)->level;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001176}
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001177
1178/*
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02001179 * Return the readahead line. Note that the pointer may become invalid when
1180 * getting the next line, if it's concatenated with the next one.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001181 */
1182 char_u *
1183source_nextline(void *cookie)
1184{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001185 return ((source_cookie_T *)cookie)->nextline;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001186}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001187#endif
1188
1189#if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC)
1190# define USE_FOPEN_NOINH
1191/*
1192 * Special function to open a file without handle inheritance.
1193 * When possible the handle is closed on exec().
1194 */
1195 static FILE *
1196fopen_noinh_readbin(char *filename)
1197{
1198# ifdef MSWIN
1199 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
1200# else
1201 int fd_tmp = mch_open(filename, O_RDONLY, 0);
1202# endif
1203
1204 if (fd_tmp == -1)
1205 return NULL;
1206
1207# ifdef HAVE_FD_CLOEXEC
1208 {
1209 int fdflags = fcntl(fd_tmp, F_GETFD);
1210 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1211 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
1212 }
1213# endif
1214
1215 return fdopen(fd_tmp, READBIN);
1216}
1217#endif
1218
1219/*
1220 * do_source: Read the file "fname" and execute its lines as EX commands.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1222 * again.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001223 *
1224 * This function may be called recursively!
1225 *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001226 * Return FAIL if file could not be opened, OK otherwise.
1227 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001228 */
1229 int
1230do_source(
1231 char_u *fname,
1232 int check_other, // check for .vimrc and _vimrc
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233 int is_vimrc, // DOSO_ value
1234 int *ret_sid UNUSED)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001235{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001236 source_cookie_T cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001237 char_u *p;
1238 char_u *fname_exp;
1239 char_u *firstline = NULL;
1240 int retval = FAIL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001241 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001242#ifdef FEAT_EVAL
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001243 static int last_current_SID_seq = 0;
1244 funccal_entry_T funccalp_entry;
1245 int save_debug_break_level = debug_break_level;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001246 int sid;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001247 scriptitem_T *si = NULL;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001248 int save_estack_compiling = estack_compiling;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001249#endif
1250#ifdef STARTUPTIME
1251 struct timeval tv_rel;
1252 struct timeval tv_start;
1253#endif
1254#ifdef FEAT_PROFILE
1255 proftime_T wait_start;
1256#endif
1257 int trigger_source_post = FALSE;
Bram Moolenaare31ee862020-01-07 20:59:34 +01001258 ESTACK_CHECK_DECLARATION
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001259
1260 p = expand_env_save(fname);
1261 if (p == NULL)
1262 return retval;
1263 fname_exp = fix_fname(p);
1264 vim_free(p);
1265 if (fname_exp == NULL)
1266 return retval;
1267 if (mch_isdir(fname_exp))
1268 {
1269 smsg(_("Cannot source a directory: \"%s\""), fname);
1270 goto theend;
1271 }
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001272#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001273 estack_compiling = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001274
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275 // See if we loaded this script before.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001276 sid = find_script_by_name(fname_exp);
Bram Moolenaarf479cac2022-01-12 12:54:55 +00001277 if (sid > 0 && ret_sid != NULL
1278 && SCRIPT_ITEM(sid)->sn_state != SN_STATE_NOT_LOADED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 {
1280 // Already loaded and no need to load again, return here.
1281 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001282 retval = OK;
1283 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001284 }
1285#endif
1286
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001287 // Apply SourceCmd autocommands, they should get the file and source it.
1288 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1289 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1290 FALSE, curbuf))
1291 {
1292#ifdef FEAT_EVAL
1293 retval = aborting() ? FAIL : OK;
1294#else
1295 retval = OK;
1296#endif
1297 if (retval == OK)
1298 // Apply SourcePost autocommands.
1299 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1300 FALSE, curbuf);
1301 goto theend;
1302 }
1303
1304 // Apply SourcePre autocommands, they may get the file.
1305 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1306
1307#ifdef USE_FOPEN_NOINH
1308 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1309#else
1310 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1311#endif
1312 if (cookie.fp == NULL && check_other)
1313 {
1314 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1315 // and ".exrc" by "_exrc" or vice versa.
1316 p = gettail(fname_exp);
1317 if ((*p == '.' || *p == '_')
1318 && (STRICMP(p + 1, "vimrc") == 0
1319 || STRICMP(p + 1, "gvimrc") == 0
1320 || STRICMP(p + 1, "exrc") == 0))
1321 {
1322 if (*p == '_')
1323 *p = '.';
1324 else
1325 *p = '_';
1326#ifdef USE_FOPEN_NOINH
1327 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1328#else
1329 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1330#endif
1331 }
1332 }
1333
1334 if (cookie.fp == NULL)
1335 {
1336 if (p_verbose > 0)
1337 {
1338 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001339 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001340 smsg(_("could not source \"%s\""), fname);
1341 else
1342 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001343 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001344 verbose_leave();
1345 }
1346 goto theend;
1347 }
1348
1349 // The file exists.
1350 // - In verbose mode, give a message.
1351 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1352 if (p_verbose > 1)
1353 {
1354 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001355 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001356 smsg(_("sourcing \"%s\""), fname);
1357 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001358 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001359 verbose_leave();
1360 }
1361 if (is_vimrc == DOSO_VIMRC)
1362 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1363 else if (is_vimrc == DOSO_GVIMRC)
1364 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1365
1366#ifdef USE_CRNL
1367 // If no automatic file format: Set default to CR-NL.
1368 if (*p_ffs == NUL)
1369 cookie.fileformat = EOL_DOS;
1370 else
1371 cookie.fileformat = EOL_UNKNOWN;
1372 cookie.error = FALSE;
1373#endif
1374
1375 cookie.nextline = NULL;
1376 cookie.sourcing_lnum = 0;
1377 cookie.finished = FALSE;
1378
1379#ifdef FEAT_EVAL
1380 // Check if this script has a breakpoint.
1381 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1382 cookie.fname = fname_exp;
1383 cookie.dbg_tick = debug_tick;
1384
1385 cookie.level = ex_nesting_level;
1386#endif
1387
1388 // Keep the sourcing name/lnum, for recursive calls.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001389 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001390 ESTACK_CHECK_SETUP
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001391
1392#ifdef STARTUPTIME
1393 if (time_fd != NULL)
1394 time_push(&tv_rel, &tv_start);
1395#endif
1396
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001397 save_current_sctx = current_sctx;
1398 current_sctx.sc_version = 1; // default script version
1399
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001400#ifdef FEAT_EVAL
1401# ifdef FEAT_PROFILE
1402 if (do_profiling == PROF_YES)
1403 prof_child_enter(&wait_start); // entering a child now
1404# endif
1405
1406 // Don't use local function variables, if called from a function.
1407 // Also starts profiling timer for nested script.
1408 save_funccal(&funccalp_entry);
1409
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001410 current_sctx.sc_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001411
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001412 // Check if this script was sourced before to find its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001413 // Always use a new sequence number.
1414 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001416 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417 hashtab_T *ht;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001418 int todo;
1419 hashitem_T *hi;
1420 dictitem_T *di;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421
1422 // loading the same script again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001424 si = SCRIPT_ITEM(sid);
1425 if (si->sn_state == SN_STATE_NOT_LOADED)
1426 {
1427 // this script was found but not loaded yet
1428 si->sn_state = SN_STATE_NEW;
1429 }
1430 else
1431 {
1432 si->sn_state = SN_STATE_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001434 // Script-local variables remain but "const" can be set again.
1435 // In Vim9 script variables will be cleared when "vim9script" is
1436 // encountered without the "noclear" argument.
1437 ht = &SCRIPT_VARS(sid);
1438 todo = (int)ht->ht_used;
1439 for (hi = ht->ht_array; todo > 0; ++hi)
1440 if (!HASHITEM_EMPTY(hi))
1441 {
1442 --todo;
1443 di = HI2DI(hi);
1444 di->di_flags |= DI_FLAGS_RELOAD;
1445 }
1446 // imports can be redefined once
1447 mark_imports_for_reload(sid);
Bram Moolenaar0123cc12021-02-07 17:17:58 +01001448
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001449 // reset version, "vim9script" may have been added or removed.
1450 si->sn_version = 1;
1451 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001452 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001454 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001455 int error = OK;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001456
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001457 // It's new, generate a new SID and initialize the scriptitem.
1458 current_sctx.sc_sid = get_new_scriptitem(&error);
1459 if (error == FAIL)
1460 goto almosttheend;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001461 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001462 si->sn_name = fname_exp;
1463 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464 if (ret_sid != NULL)
1465 *ret_sid = current_sctx.sc_sid;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001466
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001467 // Remember the "is_vimrc" flag for when the file is sourced again.
1468 si->sn_is_vimrc = is_vimrc;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001469 }
1470
1471# ifdef FEAT_PROFILE
1472 if (do_profiling == PROF_YES)
1473 {
1474 int forceit;
1475
1476 // Check if we do profiling for this script.
1477 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
1478 {
1479 script_do_profile(si);
1480 si->sn_pr_force = forceit;
1481 }
1482 if (si->sn_prof_on)
1483 {
1484 ++si->sn_pr_count;
1485 profile_start(&si->sn_pr_start);
1486 profile_zero(&si->sn_pr_children);
1487 }
1488 }
1489# endif
1490#endif
1491
1492 cookie.conv.vc_type = CONV_NONE; // no conversion
1493
1494 // Read the first line so we can check for a UTF-8 BOM.
1495 firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1496 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1497 && firstline[1] == 0xbb && firstline[2] == 0xbf)
1498 {
1499 // Found BOM; setup conversion, skip over BOM and recode the line.
1500 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1501 p = string_convert(&cookie.conv, firstline + 3, NULL);
1502 if (p == NULL)
1503 p = vim_strsave(firstline + 3);
1504 if (p != NULL)
1505 {
1506 vim_free(firstline);
1507 firstline = p;
1508 }
1509 }
1510
1511 // Call do_cmdline, which will call getsourceline() to get the lines.
1512 do_cmdline(firstline, getsourceline, (void *)&cookie,
1513 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1514 retval = OK;
1515
1516#ifdef FEAT_PROFILE
1517 if (do_profiling == PROF_YES)
1518 {
1519 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001520 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001521 if (si->sn_prof_on)
1522 {
1523 profile_end(&si->sn_pr_start);
1524 profile_sub_wait(&wait_start, &si->sn_pr_start);
1525 profile_add(&si->sn_pr_total, &si->sn_pr_start);
1526 profile_self(&si->sn_pr_self, &si->sn_pr_start,
1527 &si->sn_pr_children);
1528 }
1529 }
1530#endif
1531
1532 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001533 emsg(_(e_interrupted));
Bram Moolenaare31ee862020-01-07 20:59:34 +01001534 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001535 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001536 if (p_verbose > 1)
1537 {
1538 verbose_enter();
1539 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001540 if (SOURCING_NAME != NULL)
1541 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001542 verbose_leave();
1543 }
1544#ifdef STARTUPTIME
1545 if (time_fd != NULL)
1546 {
1547 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1548 time_msg((char *)IObuff, &tv_start);
1549 time_pop(&tv_rel);
1550 }
1551#endif
1552
1553 if (!got_int)
1554 trigger_source_post = TRUE;
1555
1556#ifdef FEAT_EVAL
1557 // After a "finish" in debug mode, need to break at first command of next
1558 // sourced file.
1559 if (save_debug_break_level > ex_nesting_level
1560 && debug_break_level == ex_nesting_level)
1561 ++debug_break_level;
1562#endif
1563
1564#ifdef FEAT_EVAL
1565almosttheend:
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001566 // If "sn_save_cpo" is set that means we encountered "vim9script": restore
1567 // 'cpoptions', unless in the main .vimrc file.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001569 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001570 if (si->sn_save_cpo != NULL && si->sn_is_vimrc == DOSO_NONE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571 {
Bram Moolenaar3e191692021-03-17 17:46:00 +01001572 if (STRCMP(p_cpo, CPO_VIM) != 0)
1573 {
1574 char_u *f;
1575 char_u *t;
1576
1577 // 'cpo' was changed in the script. Apply the same change to the
1578 // saved value, if possible.
1579 for (f = (char_u *)CPO_VIM; *f != NUL; ++f)
1580 if (vim_strchr(p_cpo, *f) == NULL
1581 && (t = vim_strchr(si->sn_save_cpo, *f)) != NULL)
1582 // flag was removed, also remove it from the saved 'cpo'
1583 mch_memmove(t, t + 1, STRLEN(t));
1584 for (f = p_cpo; *f != NUL; ++f)
1585 if (vim_strchr((char_u *)CPO_VIM, *f) == NULL
1586 && vim_strchr(si->sn_save_cpo, *f) == NULL)
1587 {
1588 // flag was added, also add it to the saved 'cpo'
1589 t = alloc(STRLEN(si->sn_save_cpo) + 2);
1590 if (t != NULL)
1591 {
1592 *t = *f;
1593 STRCPY(t + 1, si->sn_save_cpo);
1594 vim_free(si->sn_save_cpo);
1595 si->sn_save_cpo = t;
1596 }
1597 }
1598 }
Bram Moolenaar37294bd2021-03-10 13:40:08 +01001599 set_option_value((char_u *)"cpo", 0L, si->sn_save_cpo, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 }
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001601 VIM_CLEAR(si->sn_save_cpo);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001603 restore_funccal();
1604# ifdef FEAT_PROFILE
1605 if (do_profiling == PROF_YES)
1606 prof_child_exit(&wait_start); // leaving a child now
1607# endif
1608#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001609 current_sctx = save_current_sctx;
1610
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001611 fclose(cookie.fp);
1612 vim_free(cookie.nextline);
1613 vim_free(firstline);
1614 convert_setup(&cookie.conv, NULL, NULL);
1615
1616 if (trigger_source_post)
1617 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1618
1619theend:
1620 vim_free(fname_exp);
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001621#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001622 estack_compiling = save_estack_compiling;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001623#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001624 return retval;
1625}
1626
1627#if defined(FEAT_EVAL) || defined(PROTO)
1628
1629/*
1630 * ":scriptnames"
1631 */
1632 void
1633ex_scriptnames(exarg_T *eap)
1634{
1635 int i;
1636
1637 if (eap->addr_count > 0)
1638 {
1639 // :script {scriptId}: edit the script
Bram Moolenaare3d46852020-08-29 13:39:17 +02001640 if (!SCRIPT_ID_VALID(eap->line2))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001641 emsg(_(e_invalid_argument));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001642 else
1643 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001644 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001645 do_exedit(eap, NULL);
1646 }
1647 return;
1648 }
1649
1650 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar6079da72022-01-18 14:16:59 +00001651 {
1652 scriptitem_T *si = SCRIPT_ITEM(i);
1653
1654 if (si->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001655 {
Bram Moolenaar6079da72022-01-18 14:16:59 +00001656 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
1657 vim_snprintf((char *)IObuff, IOSIZE, "%3d%s: %s",
1658 i,
1659 si->sn_state == SN_STATE_NOT_LOADED ? " A" : "",
1660 NameBuff);
Bram Moolenaar769f5892022-02-09 14:31:05 +00001661 if (!message_filtered(IObuff))
1662 {
1663 msg_putchar('\n');
1664 msg_outtrans(IObuff);
1665 out_flush(); // output one line at a time
1666 ui_breakcheck();
1667 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001668 }
Bram Moolenaar6079da72022-01-18 14:16:59 +00001669 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001670}
1671
1672# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1673/*
1674 * Fix slashes in the list of script names for 'shellslash'.
1675 */
1676 void
1677scriptnames_slash_adjust(void)
1678{
1679 int i;
1680
1681 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001682 if (SCRIPT_ITEM(i)->sn_name != NULL)
1683 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001684}
1685# endif
1686
1687/*
1688 * Get a pointer to a script name. Used for ":verbose set".
Bram Moolenaar74667062020-12-28 15:41:41 +01001689 * Message appended to "Last set from "
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001690 */
1691 char_u *
1692get_scriptname(scid_T id)
1693{
1694 if (id == SID_MODELINE)
1695 return (char_u *)_("modeline");
1696 if (id == SID_CMDARG)
1697 return (char_u *)_("--cmd argument");
1698 if (id == SID_CARG)
1699 return (char_u *)_("-c argument");
1700 if (id == SID_ENV)
1701 return (char_u *)_("environment variable");
1702 if (id == SID_ERROR)
1703 return (char_u *)_("error handler");
Bram Moolenaar74667062020-12-28 15:41:41 +01001704 if (id == SID_WINLAYOUT)
1705 return (char_u *)_("changed window size");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001706 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001707}
1708
1709# if defined(EXITFREE) || defined(PROTO)
1710 void
1711free_scriptnames(void)
1712{
1713 int i;
1714
1715 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02001716 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001717 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001719 // the variables themselves are cleared in evalvars_clear()
1720 vim_free(si->sn_vars);
1721
1722 vim_free(si->sn_name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001723 free_imports_and_script_vars(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001724 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02001725# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001726 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02001727# endif
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00001728 vim_free(si->sn_autoload_prefix);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001729 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02001730 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001731 ga_clear(&script_items);
1732}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001733
1734 void
1735free_autoload_scriptnames(void)
1736{
1737 ga_clear_strings(&ga_loaded);
1738}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001739# endif
1740
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001741 linenr_T
Bram Moolenaar66250c92020-08-20 15:02:42 +02001742get_sourced_lnum(
1743 char_u *(*fgetline)(int, void *, int, getline_opt_T),
1744 void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001745{
1746 return fgetline == getsourceline
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001747 ? ((source_cookie_T *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001748 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001749}
Dominique Pelle748b3082022-01-08 12:41:16 +00001750#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001751
1752 static char_u *
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001753get_one_sourceline(source_cookie_T *sp)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001754{
1755 garray_T ga;
1756 int len;
1757 int c;
1758 char_u *buf;
1759#ifdef USE_CRNL
1760 int has_cr; // CR-LF found
1761#endif
1762 int have_read = FALSE;
1763
1764 // use a growarray to store the sourced line
1765 ga_init2(&ga, 1, 250);
1766
1767 // Loop until there is a finished line (or end-of-file).
1768 ++sp->sourcing_lnum;
1769 for (;;)
1770 {
1771 // make room to read at least 120 (more) characters
1772 if (ga_grow(&ga, 120) == FAIL)
1773 break;
1774 buf = (char_u *)ga.ga_data;
1775
1776 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
1777 sp->fp) == NULL)
1778 break;
1779 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
1780#ifdef USE_CRNL
1781 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
1782 // CTRL-Z by its own, or after a NL.
1783 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
1784 && sp->fileformat == EOL_DOS
1785 && buf[len - 1] == Ctrl_Z)
1786 {
1787 buf[len - 1] = NUL;
1788 break;
1789 }
1790#endif
1791
1792 have_read = TRUE;
1793 ga.ga_len = len;
1794
1795 // If the line was longer than the buffer, read more.
1796 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
1797 continue;
1798
1799 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
1800 {
1801#ifdef USE_CRNL
1802 has_cr = (len >= 2 && buf[len - 2] == '\r');
1803 if (sp->fileformat == EOL_UNKNOWN)
1804 {
1805 if (has_cr)
1806 sp->fileformat = EOL_DOS;
1807 else
1808 sp->fileformat = EOL_UNIX;
1809 }
1810
1811 if (sp->fileformat == EOL_DOS)
1812 {
1813 if (has_cr) // replace trailing CR
1814 {
1815 buf[len - 2] = '\n';
1816 --len;
1817 --ga.ga_len;
1818 }
1819 else // lines like ":map xx yy^M" will have failed
1820 {
1821 if (!sp->error)
1822 {
1823 msg_source(HL_ATTR(HLF_W));
1824 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
1825 }
1826 sp->error = TRUE;
1827 sp->fileformat = EOL_UNIX;
1828 }
1829 }
1830#endif
1831 // The '\n' is escaped if there is an odd number of ^V's just
1832 // before it, first set "c" just before the 'V's and then check
1833 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
1834 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
1835 ;
1836 if ((len & 1) != (c & 1)) // escaped NL, read more
1837 {
1838 ++sp->sourcing_lnum;
1839 continue;
1840 }
1841
1842 buf[len - 1] = NUL; // remove the NL
1843 }
1844
1845 // Check for ^C here now and then, so recursive :so can be broken.
1846 line_breakcheck();
1847 break;
1848 }
1849
1850 if (have_read)
1851 return (char_u *)ga.ga_data;
1852
1853 vim_free(ga.ga_data);
1854 return NULL;
1855}
1856
1857/*
1858 * Get one full line from a sourced file.
1859 * Called by do_cmdline() when it's called from do_source().
1860 *
1861 * Return a pointer to the line in allocated memory.
1862 * Return NULL for end-of-file or some error.
1863 */
1864 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02001865getsourceline(
1866 int c UNUSED,
1867 void *cookie,
1868 int indent UNUSED,
1869 getline_opt_T options)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001870{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001871 source_cookie_T *sp = (source_cookie_T *)cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001872 char_u *line;
1873 char_u *p;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001874 int do_vim9_all = in_vim9script()
1875 && options == GETLINE_CONCAT_ALL;
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01001876 int do_bar_cont = do_vim9_all
1877 || options == GETLINE_CONCAT_CONTBAR;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001878
1879#ifdef FEAT_EVAL
1880 // If breakpoints have been added/deleted need to check for it.
1881 if (sp->dbg_tick < debug_tick)
1882 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001883 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001884 sp->dbg_tick = debug_tick;
1885 }
1886# ifdef FEAT_PROFILE
1887 if (do_profiling == PROF_YES)
1888 script_line_end();
1889# endif
1890#endif
1891
1892 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001893 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001894
1895 // Get current line. If there is a read-ahead line, use it, otherwise get
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001896 // one now. "fp" is NULL if actually using a string.
1897 if (sp->finished || sp->fp == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001898 line = NULL;
1899 else if (sp->nextline == NULL)
1900 line = get_one_sourceline(sp);
1901 else
1902 {
1903 line = sp->nextline;
1904 sp->nextline = NULL;
1905 ++sp->sourcing_lnum;
1906 }
1907#ifdef FEAT_PROFILE
1908 if (line != NULL && do_profiling == PROF_YES)
1909 script_line_start();
1910#endif
1911
1912 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
1913 // contain the 'C' flag.
Bram Moolenaar66250c92020-08-20 15:02:42 +02001914 if (line != NULL && options != GETLINE_NONE
1915 && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001916 {
Bram Moolenaar5072b472021-06-03 21:56:10 +02001917 int comment_char = in_vim9script() ? '#' : '"';
1918
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001919 // compensate for the one line read-ahead
1920 --sp->sourcing_lnum;
1921
1922 // Get the next line and concatenate it when it starts with a
1923 // backslash. We always need to read the next line, keep it in
1924 // sp->nextline.
1925 /* Also check for a comment in between continuation lines: "\ */
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001926 // Also check for a Vim9 comment, empty line, line starting with '|',
1927 // but not "||".
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001928 sp->nextline = get_one_sourceline(sp);
1929 if (sp->nextline != NULL
1930 && (*(p = skipwhite(sp->nextline)) == '\\'
Bram Moolenaar5072b472021-06-03 21:56:10 +02001931 || (p[0] == comment_char
1932 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001933 || (do_vim9_all && (*p == NUL
1934 || vim9_comment_start(p)))
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01001935 || (do_bar_cont && p[0] == '|' && p[1] != '|')))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001936 {
1937 garray_T ga;
1938
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001939 ga_init2(&ga, sizeof(char_u), 400);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001940 ga_concat(&ga, line);
1941 if (*p == '\\')
1942 ga_concat(&ga, p + 1);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001943 else if (*p == '|')
1944 {
1945 ga_concat(&ga, (char_u *)" ");
1946 ga_concat(&ga, p);
1947 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001948 for (;;)
1949 {
1950 vim_free(sp->nextline);
1951 sp->nextline = get_one_sourceline(sp);
1952 if (sp->nextline == NULL)
1953 break;
1954 p = skipwhite(sp->nextline);
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01001955 if (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|'))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001956 {
1957 // Adjust the growsize to the current length to speed up
1958 // concatenating many lines.
1959 if (ga.ga_len > 400)
1960 {
1961 if (ga.ga_len > 8000)
1962 ga.ga_growsize = 8000;
1963 else
1964 ga.ga_growsize = ga.ga_len;
1965 }
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001966 if (*p == '\\')
1967 ga_concat(&ga, p + 1);
1968 else
1969 {
1970 ga_concat(&ga, (char_u *)" ");
1971 ga_concat(&ga, p);
1972 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001973 }
Bram Moolenaar5072b472021-06-03 21:56:10 +02001974 else if (!(p[0] == (comment_char)
Bram Moolenaar0f37e352021-06-02 15:28:15 +02001975 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001976 && !(do_vim9_all && (*p == NUL || vim9_comment_start(p))))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001977 break;
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001978 /* drop a # comment or "\ comment line */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001979 }
1980 ga_append(&ga, NUL);
1981 vim_free(line);
1982 line = ga.ga_data;
1983 }
1984 }
1985
1986 if (line != NULL && sp->conv.vc_type != CONV_NONE)
1987 {
1988 char_u *s;
1989
1990 // Convert the encoding of the script line.
1991 s = string_convert(&sp->conv, line, NULL);
1992 if (s != NULL)
1993 {
1994 vim_free(line);
1995 line = s;
1996 }
1997 }
1998
1999#ifdef FEAT_EVAL
2000 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002001 if (sp->breakpoint != 0 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002002 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002003 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002004 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002005 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002006 sp->dbg_tick = debug_tick;
2007 }
2008#endif
2009
2010 return line;
2011}
2012
2013/*
2014 * ":scriptencoding": Set encoding conversion for a sourced script.
2015 */
2016 void
2017ex_scriptencoding(exarg_T *eap)
2018{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002019 source_cookie_T *sp;
2020 char_u *name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002021
2022 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
2023 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00002024 emsg(_(e_scriptencoding_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002025 return;
2026 }
2027
2028 if (*eap->arg != NUL)
2029 {
2030 name = enc_canonize(eap->arg);
2031 if (name == NULL) // out of memory
2032 return;
2033 }
2034 else
2035 name = eap->arg;
2036
2037 // Setup for conversion from the specified encoding to 'encoding'.
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002038 sp = (source_cookie_T *)getline_cookie(eap->getline, eap->cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002039 convert_setup(&sp->conv, name, p_enc);
2040
2041 if (name != eap->arg)
2042 vim_free(name);
2043}
2044
2045/*
2046 * ":scriptversion": Set Vim script version for a sourced script.
2047 */
2048 void
2049ex_scriptversion(exarg_T *eap UNUSED)
2050{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002051 int nr;
2052
2053 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
2054 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002055 emsg(_(e_scriptversion_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002056 return;
2057 }
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002058 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002060 emsg(_(e_cannot_use_scriptversion_after_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002061 return;
2062 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002063
2064 nr = getdigits(&eap->arg);
2065 if (nr == 0 || *eap->arg != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002066 emsg(_(e_invalid_argument));
Bram Moolenaar67979662020-06-20 22:50:47 +02002067 else if (nr > SCRIPT_VERSION_MAX)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002068 semsg(_(e_scriptversion_not_supported_nr), nr);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002069 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002070 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002071 current_sctx.sc_version = nr;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002072#ifdef FEAT_EVAL
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002073 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002074#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002075 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002076}
2077
2078#if defined(FEAT_EVAL) || defined(PROTO)
2079/*
2080 * ":finish": Mark a sourced file as finished.
2081 */
2082 void
2083ex_finish(exarg_T *eap)
2084{
2085 if (getline_equal(eap->getline, eap->cookie, getsourceline))
2086 do_finish(eap, FALSE);
2087 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00002088 emsg(_(e_finish_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002089}
2090
2091/*
2092 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
2093 * Also called for a pending finish at the ":endtry" or after returning from
2094 * an extra do_cmdline(). "reanimate" is used in the latter case.
2095 */
2096 void
2097do_finish(exarg_T *eap, int reanimate)
2098{
2099 int idx;
2100
2101 if (reanimate)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002102 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002103 eap->cookie))->finished = FALSE;
2104
2105 // Cleanup (and inactivate) conditionals, but stop when a try conditional
2106 // not in its finally clause (which then is to be executed next) is found.
2107 // In this case, make the ":finish" pending for execution at the ":endtry".
2108 // Otherwise, finish normally.
2109 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
2110 if (idx >= 0)
2111 {
2112 eap->cstack->cs_pending[idx] = CSTP_FINISH;
2113 report_make_pending(CSTP_FINISH, NULL);
2114 }
2115 else
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002116 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002117 eap->cookie))->finished = TRUE;
2118}
2119
2120
2121/*
2122 * Return TRUE when a sourced file had the ":finish" command: Don't give error
2123 * message for missing ":endif".
2124 * Return FALSE when not sourcing a file.
2125 */
2126 int
2127source_finished(
Bram Moolenaar66250c92020-08-20 15:02:42 +02002128 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002129 void *cookie)
2130{
2131 return (getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002132 && ((source_cookie_T *)getline_cookie(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002133 fgetline, cookie))->finished);
2134}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002135
2136/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002137 * Find the path of a script below the "autoload" directory.
2138 * Returns NULL if there is no "/autoload/" in the script name.
2139 */
2140 char_u *
2141script_name_after_autoload(scriptitem_T *si)
2142{
2143 char_u *p = si->sn_name;
2144 char_u *res = NULL;
2145
2146 for (;;)
2147 {
2148 char_u *n = (char_u *)strstr((char *)p, "autoload");
2149
2150 if (n == NULL)
2151 break;
2152 if (n > p && vim_ispathsep(n[-1]) && vim_ispathsep(n[8]))
2153 res = n + 9;
2154 p = n + 8;
2155 }
2156 return res;
2157}
2158
2159/*
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002160 * For an autoload script "autoload/dir/script.vim" return the prefix
2161 * "dir#script#" in allocated memory.
2162 * Returns NULL if anything is wrong.
2163 */
2164 char_u *
2165get_autoload_prefix(scriptitem_T *si)
2166{
2167 char_u *p = script_name_after_autoload(si);
2168 char_u *prefix;
2169
2170 if (p == NULL)
2171 return NULL;
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002172 prefix = vim_strsave(p);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002173 if (prefix == NULL)
2174 return NULL;
2175
2176 // replace all '/' with '#' and locate ".vim" at the end
2177 for (p = prefix; *p != NUL; p += mb_ptr2len(p))
2178 {
2179 if (vim_ispathsep(*p))
2180 *p = '#';
2181 else if (STRCMP(p, ".vim") == 0)
2182 {
2183 p[0] = '#';
2184 p[1] = NUL;
2185 return prefix;
2186 }
2187 }
2188
2189 // did not find ".vim" at the end
2190 vim_free(prefix);
2191 return NULL;
2192}
2193
2194/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002195 * If in a Vim9 autoload script return "name" with the autoload prefix for the
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002196 * script. If successful the returned name is allocated.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002197 * Otherwise it returns "name" unmodified.
2198 */
2199 char_u *
2200may_prefix_autoload(char_u *name)
2201{
2202 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
2203 {
2204 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2205
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002206 if (si->sn_autoload_prefix != NULL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002207 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002208 char_u *basename = name;
2209 size_t len;
2210 char_u *res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002211
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002212 if (*name == K_SPECIAL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002213 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002214 char_u *p = vim_strchr(name, '_');
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002215
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002216 // skip over "<SNR>99_"
2217 if (p != NULL)
2218 basename = p + 1;
2219 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002220
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002221 len = STRLEN(si->sn_autoload_prefix) + STRLEN(basename) + 2;
2222 res = alloc(len);
2223 if (res != NULL)
2224 {
2225 vim_snprintf((char *)res, len, "%s%s",
2226 si->sn_autoload_prefix, basename);
2227 return res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002228 }
2229 }
2230 }
2231 return name;
2232}
2233
2234/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002235 * Return the autoload script name for a function or variable name.
2236 * Returns NULL when out of memory.
2237 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
2238 */
2239 char_u *
2240autoload_name(char_u *name)
2241{
2242 char_u *p, *q = NULL;
2243 char_u *scriptname;
2244
2245 // Get the script file name: replace '#' with '/', append ".vim".
2246 scriptname = alloc(STRLEN(name) + 14);
2247 if (scriptname == NULL)
2248 return NULL;
2249 STRCPY(scriptname, "autoload/");
Bram Moolenaara1773442020-08-12 15:21:22 +02002250 STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002251 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
2252 q = p, ++p)
2253 *p = '/';
2254 STRCPY(q, ".vim");
2255 return scriptname;
2256}
2257
2258/*
2259 * If "name" has a package name try autoloading the script for it.
2260 * Return TRUE if a package was loaded.
2261 */
2262 int
2263script_autoload(
2264 char_u *name,
2265 int reload) // load script again when already loaded
2266{
2267 char_u *p;
2268 char_u *scriptname, *tofree;
2269 int ret = FALSE;
2270 int i;
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002271 int ret_sid;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002272
2273 // If there is no '#' after name[0] there is no package name.
2274 p = vim_strchr(name, AUTOLOAD_CHAR);
2275 if (p == NULL || p == name)
2276 return FALSE;
2277
2278 tofree = scriptname = autoload_name(name);
2279 if (scriptname == NULL)
2280 return FALSE;
2281
2282 // Find the name in the list of previously loaded package names. Skip
2283 // "autoload/", it's always the same.
2284 for (i = 0; i < ga_loaded.ga_len; ++i)
2285 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2286 break;
2287 if (!reload && i < ga_loaded.ga_len)
2288 ret = FALSE; // was loaded already
2289 else
2290 {
2291 // Remember the name if it wasn't loaded already.
2292 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2293 {
2294 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2295 tofree = NULL;
2296 }
2297
2298 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002299 // Use "ret_sid" to avoid loading the same script again.
=?UTF-8?q?Bj=C3=B6rn=20Linse?=223a9502022-01-31 17:26:05 +00002300 if (source_in_path(p_rtp, scriptname, DIP_START, &ret_sid) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002301 ret = TRUE;
2302 }
2303
2304 vim_free(tofree);
2305 return ret;
2306}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002307#endif