blob: 2606737545750cc8c5a817d8465f2f53f837d92c [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
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001257 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001258 int trigger_source_post = FALSE;
Bram Moolenaare31ee862020-01-07 20:59:34 +01001259 ESTACK_CHECK_DECLARATION
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001260
1261 p = expand_env_save(fname);
1262 if (p == NULL)
1263 return retval;
1264 fname_exp = fix_fname(p);
1265 vim_free(p);
1266 if (fname_exp == NULL)
1267 return retval;
1268 if (mch_isdir(fname_exp))
1269 {
1270 smsg(_("Cannot source a directory: \"%s\""), fname);
1271 goto theend;
1272 }
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001273#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001274 estack_compiling = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 // See if we loaded this script before.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001277 sid = find_script_by_name(fname_exp);
Bram Moolenaarf479cac2022-01-12 12:54:55 +00001278 if (sid > 0 && ret_sid != NULL
1279 && SCRIPT_ITEM(sid)->sn_state != SN_STATE_NOT_LOADED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001280 {
1281 // Already loaded and no need to load again, return here.
1282 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001283 retval = OK;
1284 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285 }
1286#endif
1287
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001288 // Apply SourceCmd autocommands, they should get the file and source it.
1289 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1290 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1291 FALSE, curbuf))
1292 {
1293#ifdef FEAT_EVAL
1294 retval = aborting() ? FAIL : OK;
1295#else
1296 retval = OK;
1297#endif
1298 if (retval == OK)
1299 // Apply SourcePost autocommands.
1300 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1301 FALSE, curbuf);
1302 goto theend;
1303 }
1304
1305 // Apply SourcePre autocommands, they may get the file.
1306 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1307
1308#ifdef USE_FOPEN_NOINH
1309 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1310#else
1311 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1312#endif
1313 if (cookie.fp == NULL && check_other)
1314 {
1315 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1316 // and ".exrc" by "_exrc" or vice versa.
1317 p = gettail(fname_exp);
1318 if ((*p == '.' || *p == '_')
1319 && (STRICMP(p + 1, "vimrc") == 0
1320 || STRICMP(p + 1, "gvimrc") == 0
1321 || STRICMP(p + 1, "exrc") == 0))
1322 {
1323 if (*p == '_')
1324 *p = '.';
1325 else
1326 *p = '_';
1327#ifdef USE_FOPEN_NOINH
1328 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1329#else
1330 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1331#endif
1332 }
1333 }
1334
1335 if (cookie.fp == NULL)
1336 {
1337 if (p_verbose > 0)
1338 {
1339 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001340 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001341 smsg(_("could not source \"%s\""), fname);
1342 else
1343 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001344 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001345 verbose_leave();
1346 }
1347 goto theend;
1348 }
1349
1350 // The file exists.
1351 // - In verbose mode, give a message.
1352 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1353 if (p_verbose > 1)
1354 {
1355 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001356 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001357 smsg(_("sourcing \"%s\""), fname);
1358 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001359 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001360 verbose_leave();
1361 }
1362 if (is_vimrc == DOSO_VIMRC)
1363 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1364 else if (is_vimrc == DOSO_GVIMRC)
1365 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1366
1367#ifdef USE_CRNL
1368 // If no automatic file format: Set default to CR-NL.
1369 if (*p_ffs == NUL)
1370 cookie.fileformat = EOL_DOS;
1371 else
1372 cookie.fileformat = EOL_UNKNOWN;
1373 cookie.error = FALSE;
1374#endif
1375
1376 cookie.nextline = NULL;
1377 cookie.sourcing_lnum = 0;
1378 cookie.finished = FALSE;
1379
1380#ifdef FEAT_EVAL
1381 // Check if this script has a breakpoint.
1382 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1383 cookie.fname = fname_exp;
1384 cookie.dbg_tick = debug_tick;
1385
1386 cookie.level = ex_nesting_level;
1387#endif
1388
1389 // Keep the sourcing name/lnum, for recursive calls.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001390 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001391 ESTACK_CHECK_SETUP
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001392
1393#ifdef STARTUPTIME
1394 if (time_fd != NULL)
1395 time_push(&tv_rel, &tv_start);
1396#endif
1397
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001398 // "legacy" does not apply to commands in the script
1399 sticky_cmdmod_flags = 0;
1400
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001401 save_current_sctx = current_sctx;
1402 current_sctx.sc_version = 1; // default script version
1403
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001404#ifdef FEAT_EVAL
1405# ifdef FEAT_PROFILE
1406 if (do_profiling == PROF_YES)
1407 prof_child_enter(&wait_start); // entering a child now
1408# endif
1409
1410 // Don't use local function variables, if called from a function.
1411 // Also starts profiling timer for nested script.
1412 save_funccal(&funccalp_entry);
1413
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001414 current_sctx.sc_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001415
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001416 // Check if this script was sourced before to find its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001417 // Always use a new sequence number.
1418 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001420 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 hashtab_T *ht;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001422 int todo;
1423 hashitem_T *hi;
1424 dictitem_T *di;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425
1426 // loading the same script again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001428 si = SCRIPT_ITEM(sid);
1429 if (si->sn_state == SN_STATE_NOT_LOADED)
1430 {
1431 // this script was found but not loaded yet
1432 si->sn_state = SN_STATE_NEW;
1433 }
1434 else
1435 {
1436 si->sn_state = SN_STATE_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001438 // Script-local variables remain but "const" can be set again.
1439 // In Vim9 script variables will be cleared when "vim9script" is
1440 // encountered without the "noclear" argument.
1441 ht = &SCRIPT_VARS(sid);
1442 todo = (int)ht->ht_used;
1443 for (hi = ht->ht_array; todo > 0; ++hi)
1444 if (!HASHITEM_EMPTY(hi))
1445 {
1446 --todo;
1447 di = HI2DI(hi);
1448 di->di_flags |= DI_FLAGS_RELOAD;
1449 }
1450 // imports can be redefined once
1451 mark_imports_for_reload(sid);
Bram Moolenaar0123cc12021-02-07 17:17:58 +01001452
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001453 // reset version, "vim9script" may have been added or removed.
1454 si->sn_version = 1;
1455 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001456 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001458 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001459 int error = OK;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001460
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001461 // It's new, generate a new SID and initialize the scriptitem.
1462 current_sctx.sc_sid = get_new_scriptitem(&error);
1463 if (error == FAIL)
1464 goto almosttheend;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001465 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001466 si->sn_name = fname_exp;
1467 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468 if (ret_sid != NULL)
1469 *ret_sid = current_sctx.sc_sid;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001470
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001471 // Remember the "is_vimrc" flag for when the file is sourced again.
1472 si->sn_is_vimrc = is_vimrc;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001473 }
1474
1475# ifdef FEAT_PROFILE
1476 if (do_profiling == PROF_YES)
1477 {
1478 int forceit;
1479
1480 // Check if we do profiling for this script.
1481 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
1482 {
1483 script_do_profile(si);
1484 si->sn_pr_force = forceit;
1485 }
1486 if (si->sn_prof_on)
1487 {
1488 ++si->sn_pr_count;
1489 profile_start(&si->sn_pr_start);
1490 profile_zero(&si->sn_pr_children);
1491 }
1492 }
1493# endif
1494#endif
1495
1496 cookie.conv.vc_type = CONV_NONE; // no conversion
1497
1498 // Read the first line so we can check for a UTF-8 BOM.
1499 firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1500 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1501 && firstline[1] == 0xbb && firstline[2] == 0xbf)
1502 {
1503 // Found BOM; setup conversion, skip over BOM and recode the line.
1504 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1505 p = string_convert(&cookie.conv, firstline + 3, NULL);
1506 if (p == NULL)
1507 p = vim_strsave(firstline + 3);
1508 if (p != NULL)
1509 {
1510 vim_free(firstline);
1511 firstline = p;
1512 }
1513 }
1514
1515 // Call do_cmdline, which will call getsourceline() to get the lines.
1516 do_cmdline(firstline, getsourceline, (void *)&cookie,
1517 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1518 retval = OK;
1519
1520#ifdef FEAT_PROFILE
1521 if (do_profiling == PROF_YES)
1522 {
1523 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001524 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001525 if (si->sn_prof_on)
1526 {
1527 profile_end(&si->sn_pr_start);
1528 profile_sub_wait(&wait_start, &si->sn_pr_start);
1529 profile_add(&si->sn_pr_total, &si->sn_pr_start);
1530 profile_self(&si->sn_pr_self, &si->sn_pr_start,
1531 &si->sn_pr_children);
1532 }
1533 }
1534#endif
1535
1536 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001537 emsg(_(e_interrupted));
Bram Moolenaare31ee862020-01-07 20:59:34 +01001538 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001539 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001540 if (p_verbose > 1)
1541 {
1542 verbose_enter();
1543 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001544 if (SOURCING_NAME != NULL)
1545 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001546 verbose_leave();
1547 }
1548#ifdef STARTUPTIME
1549 if (time_fd != NULL)
1550 {
1551 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1552 time_msg((char *)IObuff, &tv_start);
1553 time_pop(&tv_rel);
1554 }
1555#endif
1556
1557 if (!got_int)
1558 trigger_source_post = TRUE;
1559
1560#ifdef FEAT_EVAL
1561 // After a "finish" in debug mode, need to break at first command of next
1562 // sourced file.
1563 if (save_debug_break_level > ex_nesting_level
1564 && debug_break_level == ex_nesting_level)
1565 ++debug_break_level;
1566#endif
1567
1568#ifdef FEAT_EVAL
1569almosttheend:
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001570 // If "sn_save_cpo" is set that means we encountered "vim9script": restore
1571 // 'cpoptions', unless in the main .vimrc file.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001573 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001574 if (si->sn_save_cpo != NULL && si->sn_is_vimrc == DOSO_NONE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 {
Bram Moolenaar3e191692021-03-17 17:46:00 +01001576 if (STRCMP(p_cpo, CPO_VIM) != 0)
1577 {
1578 char_u *f;
1579 char_u *t;
1580
1581 // 'cpo' was changed in the script. Apply the same change to the
1582 // saved value, if possible.
1583 for (f = (char_u *)CPO_VIM; *f != NUL; ++f)
1584 if (vim_strchr(p_cpo, *f) == NULL
1585 && (t = vim_strchr(si->sn_save_cpo, *f)) != NULL)
1586 // flag was removed, also remove it from the saved 'cpo'
1587 mch_memmove(t, t + 1, STRLEN(t));
1588 for (f = p_cpo; *f != NUL; ++f)
1589 if (vim_strchr((char_u *)CPO_VIM, *f) == NULL
1590 && vim_strchr(si->sn_save_cpo, *f) == NULL)
1591 {
1592 // flag was added, also add it to the saved 'cpo'
1593 t = alloc(STRLEN(si->sn_save_cpo) + 2);
1594 if (t != NULL)
1595 {
1596 *t = *f;
1597 STRCPY(t + 1, si->sn_save_cpo);
1598 vim_free(si->sn_save_cpo);
1599 si->sn_save_cpo = t;
1600 }
1601 }
1602 }
Bram Moolenaar37294bd2021-03-10 13:40:08 +01001603 set_option_value((char_u *)"cpo", 0L, si->sn_save_cpo, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 }
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001605 VIM_CLEAR(si->sn_save_cpo);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001607 restore_funccal();
1608# ifdef FEAT_PROFILE
1609 if (do_profiling == PROF_YES)
1610 prof_child_exit(&wait_start); // leaving a child now
1611# endif
1612#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001613 current_sctx = save_current_sctx;
1614
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001615 fclose(cookie.fp);
1616 vim_free(cookie.nextline);
1617 vim_free(firstline);
1618 convert_setup(&cookie.conv, NULL, NULL);
1619
1620 if (trigger_source_post)
1621 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1622
1623theend:
1624 vim_free(fname_exp);
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001625 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001626#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001627 estack_compiling = save_estack_compiling;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001628#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001629 return retval;
1630}
1631
1632#if defined(FEAT_EVAL) || defined(PROTO)
1633
1634/*
1635 * ":scriptnames"
1636 */
1637 void
1638ex_scriptnames(exarg_T *eap)
1639{
1640 int i;
1641
1642 if (eap->addr_count > 0)
1643 {
1644 // :script {scriptId}: edit the script
Bram Moolenaare3d46852020-08-29 13:39:17 +02001645 if (!SCRIPT_ID_VALID(eap->line2))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001646 emsg(_(e_invalid_argument));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001647 else
1648 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001649 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001650 do_exedit(eap, NULL);
1651 }
1652 return;
1653 }
1654
1655 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar6079da72022-01-18 14:16:59 +00001656 {
1657 scriptitem_T *si = SCRIPT_ITEM(i);
1658
1659 if (si->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001660 {
Bram Moolenaar6079da72022-01-18 14:16:59 +00001661 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
1662 vim_snprintf((char *)IObuff, IOSIZE, "%3d%s: %s",
1663 i,
1664 si->sn_state == SN_STATE_NOT_LOADED ? " A" : "",
1665 NameBuff);
Bram Moolenaar769f5892022-02-09 14:31:05 +00001666 if (!message_filtered(IObuff))
1667 {
1668 msg_putchar('\n');
1669 msg_outtrans(IObuff);
1670 out_flush(); // output one line at a time
1671 ui_breakcheck();
1672 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001673 }
Bram Moolenaar6079da72022-01-18 14:16:59 +00001674 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001675}
1676
1677# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1678/*
1679 * Fix slashes in the list of script names for 'shellslash'.
1680 */
1681 void
1682scriptnames_slash_adjust(void)
1683{
1684 int i;
1685
1686 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001687 if (SCRIPT_ITEM(i)->sn_name != NULL)
1688 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001689}
1690# endif
1691
1692/*
1693 * Get a pointer to a script name. Used for ":verbose set".
Bram Moolenaar74667062020-12-28 15:41:41 +01001694 * Message appended to "Last set from "
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001695 */
1696 char_u *
1697get_scriptname(scid_T id)
1698{
1699 if (id == SID_MODELINE)
1700 return (char_u *)_("modeline");
1701 if (id == SID_CMDARG)
1702 return (char_u *)_("--cmd argument");
1703 if (id == SID_CARG)
1704 return (char_u *)_("-c argument");
1705 if (id == SID_ENV)
1706 return (char_u *)_("environment variable");
1707 if (id == SID_ERROR)
1708 return (char_u *)_("error handler");
Bram Moolenaar74667062020-12-28 15:41:41 +01001709 if (id == SID_WINLAYOUT)
1710 return (char_u *)_("changed window size");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001711 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001712}
1713
1714# if defined(EXITFREE) || defined(PROTO)
1715 void
1716free_scriptnames(void)
1717{
1718 int i;
1719
1720 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02001721 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001722 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001724 // the variables themselves are cleared in evalvars_clear()
1725 vim_free(si->sn_vars);
1726
1727 vim_free(si->sn_name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001728 free_imports_and_script_vars(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001729 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02001730# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001731 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02001732# endif
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00001733 vim_free(si->sn_autoload_prefix);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001734 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02001735 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001736 ga_clear(&script_items);
1737}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001738
1739 void
1740free_autoload_scriptnames(void)
1741{
1742 ga_clear_strings(&ga_loaded);
1743}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001744# endif
1745
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001746 linenr_T
Bram Moolenaar66250c92020-08-20 15:02:42 +02001747get_sourced_lnum(
1748 char_u *(*fgetline)(int, void *, int, getline_opt_T),
1749 void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001750{
1751 return fgetline == getsourceline
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001752 ? ((source_cookie_T *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001753 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001754}
Dominique Pelle748b3082022-01-08 12:41:16 +00001755#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001756
1757 static char_u *
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001758get_one_sourceline(source_cookie_T *sp)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001759{
1760 garray_T ga;
1761 int len;
1762 int c;
1763 char_u *buf;
1764#ifdef USE_CRNL
1765 int has_cr; // CR-LF found
1766#endif
1767 int have_read = FALSE;
1768
1769 // use a growarray to store the sourced line
1770 ga_init2(&ga, 1, 250);
1771
1772 // Loop until there is a finished line (or end-of-file).
1773 ++sp->sourcing_lnum;
1774 for (;;)
1775 {
1776 // make room to read at least 120 (more) characters
1777 if (ga_grow(&ga, 120) == FAIL)
1778 break;
1779 buf = (char_u *)ga.ga_data;
1780
1781 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
1782 sp->fp) == NULL)
1783 break;
1784 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
1785#ifdef USE_CRNL
1786 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
1787 // CTRL-Z by its own, or after a NL.
1788 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
1789 && sp->fileformat == EOL_DOS
1790 && buf[len - 1] == Ctrl_Z)
1791 {
1792 buf[len - 1] = NUL;
1793 break;
1794 }
1795#endif
1796
1797 have_read = TRUE;
1798 ga.ga_len = len;
1799
1800 // If the line was longer than the buffer, read more.
1801 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
1802 continue;
1803
1804 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
1805 {
1806#ifdef USE_CRNL
1807 has_cr = (len >= 2 && buf[len - 2] == '\r');
1808 if (sp->fileformat == EOL_UNKNOWN)
1809 {
1810 if (has_cr)
1811 sp->fileformat = EOL_DOS;
1812 else
1813 sp->fileformat = EOL_UNIX;
1814 }
1815
1816 if (sp->fileformat == EOL_DOS)
1817 {
1818 if (has_cr) // replace trailing CR
1819 {
1820 buf[len - 2] = '\n';
1821 --len;
1822 --ga.ga_len;
1823 }
1824 else // lines like ":map xx yy^M" will have failed
1825 {
1826 if (!sp->error)
1827 {
1828 msg_source(HL_ATTR(HLF_W));
1829 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
1830 }
1831 sp->error = TRUE;
1832 sp->fileformat = EOL_UNIX;
1833 }
1834 }
1835#endif
1836 // The '\n' is escaped if there is an odd number of ^V's just
1837 // before it, first set "c" just before the 'V's and then check
1838 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
1839 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
1840 ;
1841 if ((len & 1) != (c & 1)) // escaped NL, read more
1842 {
1843 ++sp->sourcing_lnum;
1844 continue;
1845 }
1846
1847 buf[len - 1] = NUL; // remove the NL
1848 }
1849
1850 // Check for ^C here now and then, so recursive :so can be broken.
1851 line_breakcheck();
1852 break;
1853 }
1854
1855 if (have_read)
1856 return (char_u *)ga.ga_data;
1857
1858 vim_free(ga.ga_data);
1859 return NULL;
1860}
1861
1862/*
1863 * Get one full line from a sourced file.
1864 * Called by do_cmdline() when it's called from do_source().
1865 *
1866 * Return a pointer to the line in allocated memory.
1867 * Return NULL for end-of-file or some error.
1868 */
1869 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02001870getsourceline(
1871 int c UNUSED,
1872 void *cookie,
1873 int indent UNUSED,
1874 getline_opt_T options)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001875{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001876 source_cookie_T *sp = (source_cookie_T *)cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001877 char_u *line;
1878 char_u *p;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001879 int do_vim9_all = in_vim9script()
1880 && options == GETLINE_CONCAT_ALL;
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01001881 int do_bar_cont = do_vim9_all
1882 || options == GETLINE_CONCAT_CONTBAR;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001883
1884#ifdef FEAT_EVAL
1885 // If breakpoints have been added/deleted need to check for it.
1886 if (sp->dbg_tick < debug_tick)
1887 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001888 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001889 sp->dbg_tick = debug_tick;
1890 }
1891# ifdef FEAT_PROFILE
1892 if (do_profiling == PROF_YES)
1893 script_line_end();
1894# endif
1895#endif
1896
1897 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001898 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001899
1900 // Get current line. If there is a read-ahead line, use it, otherwise get
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001901 // one now. "fp" is NULL if actually using a string.
1902 if (sp->finished || sp->fp == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001903 line = NULL;
1904 else if (sp->nextline == NULL)
1905 line = get_one_sourceline(sp);
1906 else
1907 {
1908 line = sp->nextline;
1909 sp->nextline = NULL;
1910 ++sp->sourcing_lnum;
1911 }
1912#ifdef FEAT_PROFILE
1913 if (line != NULL && do_profiling == PROF_YES)
1914 script_line_start();
1915#endif
1916
1917 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
1918 // contain the 'C' flag.
Bram Moolenaar66250c92020-08-20 15:02:42 +02001919 if (line != NULL && options != GETLINE_NONE
1920 && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001921 {
Bram Moolenaar5072b472021-06-03 21:56:10 +02001922 int comment_char = in_vim9script() ? '#' : '"';
1923
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001924 // compensate for the one line read-ahead
1925 --sp->sourcing_lnum;
1926
1927 // Get the next line and concatenate it when it starts with a
1928 // backslash. We always need to read the next line, keep it in
1929 // sp->nextline.
1930 /* Also check for a comment in between continuation lines: "\ */
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001931 // Also check for a Vim9 comment, empty line, line starting with '|',
1932 // but not "||".
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001933 sp->nextline = get_one_sourceline(sp);
1934 if (sp->nextline != NULL
1935 && (*(p = skipwhite(sp->nextline)) == '\\'
Bram Moolenaar5072b472021-06-03 21:56:10 +02001936 || (p[0] == comment_char
1937 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001938 || (do_vim9_all && (*p == NUL
1939 || vim9_comment_start(p)))
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01001940 || (do_bar_cont && p[0] == '|' && p[1] != '|')))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001941 {
1942 garray_T ga;
1943
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001944 ga_init2(&ga, sizeof(char_u), 400);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001945 ga_concat(&ga, line);
1946 if (*p == '\\')
1947 ga_concat(&ga, p + 1);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001948 else if (*p == '|')
1949 {
1950 ga_concat(&ga, (char_u *)" ");
1951 ga_concat(&ga, p);
1952 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001953 for (;;)
1954 {
1955 vim_free(sp->nextline);
1956 sp->nextline = get_one_sourceline(sp);
1957 if (sp->nextline == NULL)
1958 break;
1959 p = skipwhite(sp->nextline);
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01001960 if (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|'))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001961 {
1962 // Adjust the growsize to the current length to speed up
1963 // concatenating many lines.
1964 if (ga.ga_len > 400)
1965 {
1966 if (ga.ga_len > 8000)
1967 ga.ga_growsize = 8000;
1968 else
1969 ga.ga_growsize = ga.ga_len;
1970 }
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001971 if (*p == '\\')
1972 ga_concat(&ga, p + 1);
1973 else
1974 {
1975 ga_concat(&ga, (char_u *)" ");
1976 ga_concat(&ga, p);
1977 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001978 }
Bram Moolenaar5072b472021-06-03 21:56:10 +02001979 else if (!(p[0] == (comment_char)
Bram Moolenaar0f37e352021-06-02 15:28:15 +02001980 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001981 && !(do_vim9_all && (*p == NUL || vim9_comment_start(p))))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001982 break;
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001983 /* drop a # comment or "\ comment line */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001984 }
1985 ga_append(&ga, NUL);
1986 vim_free(line);
1987 line = ga.ga_data;
1988 }
1989 }
1990
1991 if (line != NULL && sp->conv.vc_type != CONV_NONE)
1992 {
1993 char_u *s;
1994
1995 // Convert the encoding of the script line.
1996 s = string_convert(&sp->conv, line, NULL);
1997 if (s != NULL)
1998 {
1999 vim_free(line);
2000 line = s;
2001 }
2002 }
2003
2004#ifdef FEAT_EVAL
2005 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002006 if (sp->breakpoint != 0 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002007 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002008 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002009 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002010 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002011 sp->dbg_tick = debug_tick;
2012 }
2013#endif
2014
2015 return line;
2016}
2017
2018/*
2019 * ":scriptencoding": Set encoding conversion for a sourced script.
2020 */
2021 void
2022ex_scriptencoding(exarg_T *eap)
2023{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002024 source_cookie_T *sp;
2025 char_u *name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002026
2027 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
2028 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00002029 emsg(_(e_scriptencoding_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002030 return;
2031 }
2032
2033 if (*eap->arg != NUL)
2034 {
2035 name = enc_canonize(eap->arg);
2036 if (name == NULL) // out of memory
2037 return;
2038 }
2039 else
2040 name = eap->arg;
2041
2042 // Setup for conversion from the specified encoding to 'encoding'.
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002043 sp = (source_cookie_T *)getline_cookie(eap->getline, eap->cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002044 convert_setup(&sp->conv, name, p_enc);
2045
2046 if (name != eap->arg)
2047 vim_free(name);
2048}
2049
2050/*
2051 * ":scriptversion": Set Vim script version for a sourced script.
2052 */
2053 void
2054ex_scriptversion(exarg_T *eap UNUSED)
2055{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002056 int nr;
2057
2058 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
2059 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002060 emsg(_(e_scriptversion_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002061 return;
2062 }
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002063 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002065 emsg(_(e_cannot_use_scriptversion_after_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 return;
2067 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002068
2069 nr = getdigits(&eap->arg);
2070 if (nr == 0 || *eap->arg != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002071 emsg(_(e_invalid_argument));
Bram Moolenaar67979662020-06-20 22:50:47 +02002072 else if (nr > SCRIPT_VERSION_MAX)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002073 semsg(_(e_scriptversion_not_supported_nr), nr);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002074 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002075 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002076 current_sctx.sc_version = nr;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002077#ifdef FEAT_EVAL
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002078 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002079#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002080 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002081}
2082
2083#if defined(FEAT_EVAL) || defined(PROTO)
2084/*
2085 * ":finish": Mark a sourced file as finished.
2086 */
2087 void
2088ex_finish(exarg_T *eap)
2089{
2090 if (getline_equal(eap->getline, eap->cookie, getsourceline))
2091 do_finish(eap, FALSE);
2092 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00002093 emsg(_(e_finish_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002094}
2095
2096/*
2097 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
2098 * Also called for a pending finish at the ":endtry" or after returning from
2099 * an extra do_cmdline(). "reanimate" is used in the latter case.
2100 */
2101 void
2102do_finish(exarg_T *eap, int reanimate)
2103{
2104 int idx;
2105
2106 if (reanimate)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002107 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002108 eap->cookie))->finished = FALSE;
2109
2110 // Cleanup (and inactivate) conditionals, but stop when a try conditional
2111 // not in its finally clause (which then is to be executed next) is found.
2112 // In this case, make the ":finish" pending for execution at the ":endtry".
2113 // Otherwise, finish normally.
2114 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
2115 if (idx >= 0)
2116 {
2117 eap->cstack->cs_pending[idx] = CSTP_FINISH;
2118 report_make_pending(CSTP_FINISH, NULL);
2119 }
2120 else
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002121 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002122 eap->cookie))->finished = TRUE;
2123}
2124
2125
2126/*
2127 * Return TRUE when a sourced file had the ":finish" command: Don't give error
2128 * message for missing ":endif".
2129 * Return FALSE when not sourcing a file.
2130 */
2131 int
2132source_finished(
Bram Moolenaar66250c92020-08-20 15:02:42 +02002133 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002134 void *cookie)
2135{
2136 return (getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002137 && ((source_cookie_T *)getline_cookie(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002138 fgetline, cookie))->finished);
2139}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002140
2141/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002142 * Find the path of a script below the "autoload" directory.
2143 * Returns NULL if there is no "/autoload/" in the script name.
2144 */
2145 char_u *
2146script_name_after_autoload(scriptitem_T *si)
2147{
2148 char_u *p = si->sn_name;
2149 char_u *res = NULL;
2150
2151 for (;;)
2152 {
2153 char_u *n = (char_u *)strstr((char *)p, "autoload");
2154
2155 if (n == NULL)
2156 break;
2157 if (n > p && vim_ispathsep(n[-1]) && vim_ispathsep(n[8]))
2158 res = n + 9;
2159 p = n + 8;
2160 }
2161 return res;
2162}
2163
2164/*
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002165 * For an autoload script "autoload/dir/script.vim" return the prefix
2166 * "dir#script#" in allocated memory.
2167 * Returns NULL if anything is wrong.
2168 */
2169 char_u *
2170get_autoload_prefix(scriptitem_T *si)
2171{
2172 char_u *p = script_name_after_autoload(si);
2173 char_u *prefix;
2174
2175 if (p == NULL)
2176 return NULL;
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002177 prefix = vim_strsave(p);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002178 if (prefix == NULL)
2179 return NULL;
2180
2181 // replace all '/' with '#' and locate ".vim" at the end
2182 for (p = prefix; *p != NUL; p += mb_ptr2len(p))
2183 {
2184 if (vim_ispathsep(*p))
2185 *p = '#';
2186 else if (STRCMP(p, ".vim") == 0)
2187 {
2188 p[0] = '#';
2189 p[1] = NUL;
2190 return prefix;
2191 }
2192 }
2193
2194 // did not find ".vim" at the end
2195 vim_free(prefix);
2196 return NULL;
2197}
2198
2199/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002200 * If in a Vim9 autoload script return "name" with the autoload prefix for the
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002201 * script. If successful the returned name is allocated.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002202 * Otherwise it returns "name" unmodified.
2203 */
2204 char_u *
2205may_prefix_autoload(char_u *name)
2206{
2207 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
2208 {
2209 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2210
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002211 if (si->sn_autoload_prefix != NULL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002212 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002213 char_u *basename = name;
2214 size_t len;
2215 char_u *res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002216
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002217 if (*name == K_SPECIAL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002218 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002219 char_u *p = vim_strchr(name, '_');
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002220
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002221 // skip over "<SNR>99_"
2222 if (p != NULL)
2223 basename = p + 1;
2224 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002225
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002226 len = STRLEN(si->sn_autoload_prefix) + STRLEN(basename) + 2;
2227 res = alloc(len);
2228 if (res != NULL)
2229 {
2230 vim_snprintf((char *)res, len, "%s%s",
2231 si->sn_autoload_prefix, basename);
2232 return res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002233 }
2234 }
2235 }
2236 return name;
2237}
2238
2239/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002240 * Return the autoload script name for a function or variable name.
2241 * Returns NULL when out of memory.
2242 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
2243 */
2244 char_u *
2245autoload_name(char_u *name)
2246{
2247 char_u *p, *q = NULL;
2248 char_u *scriptname;
2249
2250 // Get the script file name: replace '#' with '/', append ".vim".
2251 scriptname = alloc(STRLEN(name) + 14);
2252 if (scriptname == NULL)
2253 return NULL;
2254 STRCPY(scriptname, "autoload/");
Bram Moolenaara1773442020-08-12 15:21:22 +02002255 STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002256 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
2257 q = p, ++p)
2258 *p = '/';
2259 STRCPY(q, ".vim");
2260 return scriptname;
2261}
2262
2263/*
2264 * If "name" has a package name try autoloading the script for it.
2265 * Return TRUE if a package was loaded.
2266 */
2267 int
2268script_autoload(
2269 char_u *name,
2270 int reload) // load script again when already loaded
2271{
2272 char_u *p;
2273 char_u *scriptname, *tofree;
2274 int ret = FALSE;
2275 int i;
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002276 int ret_sid;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002277
2278 // If there is no '#' after name[0] there is no package name.
2279 p = vim_strchr(name, AUTOLOAD_CHAR);
2280 if (p == NULL || p == name)
2281 return FALSE;
2282
2283 tofree = scriptname = autoload_name(name);
2284 if (scriptname == NULL)
2285 return FALSE;
2286
2287 // Find the name in the list of previously loaded package names. Skip
2288 // "autoload/", it's always the same.
2289 for (i = 0; i < ga_loaded.ga_len; ++i)
2290 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2291 break;
2292 if (!reload && i < ga_loaded.ga_len)
2293 ret = FALSE; // was loaded already
2294 else
2295 {
2296 // Remember the name if it wasn't loaded already.
2297 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2298 {
2299 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2300 tofree = NULL;
2301 }
2302
2303 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002304 // Use "ret_sid" to avoid loading the same script again.
=?UTF-8?q?Bj=C3=B6rn=20Linse?=223a9502022-01-31 17:26:05 +00002305 if (source_in_path(p_rtp, scriptname, DIP_START, &ret_sid) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002306 ret = TRUE;
2307 }
2308
2309 vim_free(tofree);
2310 return ret;
2311}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002312#endif