blob: 9778843423bc4e0f501433db514b701860364233 [file] [log] [blame]
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * scriptfile.c: functions for dealing with the runtime directories/files
12 */
13
14#include "vim.h"
15
Bram Moolenaarda6c0332019-09-01 16:01:30 +020016#if defined(FEAT_EVAL) || defined(PROTO)
17// The names of packages that once were loaded are remembered.
18static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
19#endif
20
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +000021// last used sequence number for sourcing scripts (current_sctx.sc_seq)
22#ifdef FEAT_EVAL
23static int last_current_SID_seq = 0;
24#endif
25
Bram Moolenaar307c5a52019-08-25 15:41:00 +020026/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010027 * Initialize the execution stack.
28 */
29 void
30estack_init(void)
31{
32 estack_T *entry;
33
34 if (ga_grow(&exestack, 10) == FAIL)
35 mch_exit(0);
36 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
37 entry->es_type = ETYPE_TOP;
38 entry->es_name = NULL;
39 entry->es_lnum = 0;
Bram Moolenaar09d46402019-12-29 23:53:01 +010040#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010041 entry->es_info.ufunc = NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +010042#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010043 ++exestack.ga_len;
44}
45
46/*
47 * Add an item to the execution stack.
48 * Returns the new entry or NULL when out of memory.
49 */
50 estack_T *
51estack_push(etype_T type, char_u *name, long lnum)
52{
53 estack_T *entry;
54
55 // If memory allocation fails then we'll pop more than we push, eventually
56 // at the top level it will be OK again.
57 if (ga_grow(&exestack, 1) == OK)
58 {
59 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
60 entry->es_type = type;
61 entry->es_name = name;
62 entry->es_lnum = lnum;
Bram Moolenaar09d46402019-12-29 23:53:01 +010063#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010064 entry->es_info.ufunc = NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +010065#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010066 ++exestack.ga_len;
67 return entry;
68 }
69 return NULL;
70}
71
Bram Moolenaar09d46402019-12-29 23:53:01 +010072#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010073/*
74 * Add a user function to the execution stack.
75 */
Bram Moolenaarc620c052020-07-08 15:16:19 +020076 estack_T *
Bram Moolenaar25e0f582020-05-25 22:36:50 +020077estack_push_ufunc(ufunc_T *ufunc, long lnum)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010078{
Bram Moolenaar25e0f582020-05-25 22:36:50 +020079 estack_T *entry = estack_push(ETYPE_UFUNC,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010080 ufunc->uf_name_exp != NULL
81 ? ufunc->uf_name_exp : ufunc->uf_name, lnum);
82 if (entry != NULL)
83 entry->es_info.ufunc = ufunc;
Bram Moolenaarc620c052020-07-08 15:16:19 +020084 return entry;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010085}
Bram Moolenaar25e0f582020-05-25 22:36:50 +020086
87/*
88 * Return TRUE if "ufunc" with "lnum" is already at the top of the exe stack.
89 */
90 int
91estack_top_is_ufunc(ufunc_T *ufunc, long lnum)
92{
93 estack_T *entry;
94
95 if (exestack.ga_len == 0)
96 return FALSE;
97 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
98 return entry->es_type == ETYPE_UFUNC
99 && STRCMP( entry->es_name, ufunc->uf_name_exp != NULL
100 ? ufunc->uf_name_exp : ufunc->uf_name) == 0
101 && entry->es_lnum == lnum;
102}
Bram Moolenaar09d46402019-12-29 23:53:01 +0100103#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100104
105/*
Bram Moolenaarc620c052020-07-08 15:16:19 +0200106 * Take an item off of the execution stack and return it.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100107 */
Bram Moolenaarc620c052020-07-08 15:16:19 +0200108 estack_T *
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100109estack_pop(void)
110{
Bram Moolenaarc620c052020-07-08 15:16:19 +0200111 if (exestack.ga_len == 0)
112 return NULL;
113 --exestack.ga_len;
114 return ((estack_T *)exestack.ga_data) + exestack.ga_len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100115}
116
117/*
118 * Get the current value for <sfile> in allocated memory.
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200119 * "which" is ESTACK_SFILE for <sfile> and ESTACK_STACK for <stack>.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100120 */
121 char_u *
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200122estack_sfile(estack_arg_T which UNUSED)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100123{
Bram Moolenaar85b09572019-12-30 10:57:00 +0100124 estack_T *entry;
125#ifdef FEAT_EVAL
Bram Moolenaara5d04232020-07-26 15:37:02 +0200126 garray_T ga;
Bram Moolenaar4d7a2482020-01-06 19:53:43 +0100127 size_t len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100128 int idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200129 etype_T last_type = ETYPE_SCRIPT;
130 char *type_name;
Bram Moolenaar85b09572019-12-30 10:57:00 +0100131#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100132
133 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100134#ifdef FEAT_EVAL
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200135 if (which == ESTACK_SFILE && entry->es_type != ETYPE_UFUNC)
Bram Moolenaar09d46402019-12-29 23:53:01 +0100136#endif
Bram Moolenaara5d04232020-07-26 15:37:02 +0200137 {
138 if (entry->es_name == NULL)
139 return NULL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100140 return vim_strsave(entry->es_name);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200141 }
Bram Moolenaar09d46402019-12-29 23:53:01 +0100142#ifdef FEAT_EVAL
Bram Moolenaarc4492712021-11-22 15:37:15 +0000143 // expand('<sfile>') works in a function for backwards compatibility, but
144 // may give an unexpected result. Disallow it in Vim 9 script.
145 if (which == ESTACK_SFILE && in_vim9script())
146 {
147 int save_emsg_off = emsg_off;
148
149 if (emsg_off == 1)
150 // f_expand() silences errors but we do want this one
151 emsg_off = 0;
152 emsg(_(e_cannot_expand_sfile_in_vim9_function));
153 emsg_off = save_emsg_off;
154 return NULL;
155 }
156
Bram Moolenaara5d04232020-07-26 15:37:02 +0200157 // Give information about each stack entry up to the root.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100158 // For a function we compose the call stack, as it was done in the past:
159 // "function One[123]..Two[456]..Three"
Bram Moolenaara5d04232020-07-26 15:37:02 +0200160 ga_init2(&ga, sizeof(char), 100);
161 for (idx = 0; idx < exestack.ga_len; ++idx)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100162 {
163 entry = ((estack_T *)exestack.ga_data) + idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200164 if (entry->es_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100165 {
Bram Moolenaara810db32020-09-11 17:59:23 +0200166 long lnum = 0;
167 char *dots;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200168
Bram Moolenaara5d04232020-07-26 15:37:02 +0200169 len = STRLEN(entry->es_name) + 15;
170 type_name = "";
171 if (entry->es_type != last_type)
172 {
173 switch (entry->es_type)
174 {
175 case ETYPE_SCRIPT: type_name = "script "; break;
176 case ETYPE_UFUNC: type_name = "function "; break;
177 default: type_name = ""; break;
178 }
179 last_type = entry->es_type;
180 }
181 len += STRLEN(type_name);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200182 if (ga_grow(&ga, (int)len) == FAIL)
Bram Moolenaara5d04232020-07-26 15:37:02 +0200183 break;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200184 if (idx == exestack.ga_len - 1)
185 lnum = which == ESTACK_STACK ? SOURCING_LNUM : 0;
186 else
187 lnum = entry->es_lnum;
Bram Moolenaara810db32020-09-11 17:59:23 +0200188 dots = idx == exestack.ga_len - 1 ? "" : "..";
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200189 if (lnum == 0)
190 // For the bottom entry of <sfile>: do not add the line number,
191 // it is used in <slnum>. Also leave it out when the number is
192 // not set.
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200193 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s%s",
Bram Moolenaara810db32020-09-11 17:59:23 +0200194 type_name, entry->es_name, dots);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200195 else
Bram Moolenaara810db32020-09-11 17:59:23 +0200196 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s[%ld]%s",
197 type_name, entry->es_name, lnum, dots);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200198 ga.ga_len += (int)STRLEN((char *)ga.ga_data + ga.ga_len);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100199 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100200 }
201
Bram Moolenaara5d04232020-07-26 15:37:02 +0200202 return (char_u *)ga.ga_data;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100203#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100204}
205
206/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200207 * ":runtime [what] {name}"
208 */
209 void
210ex_runtime(exarg_T *eap)
211{
212 char_u *arg = eap->arg;
213 char_u *p = skiptowhite(arg);
214 int len = (int)(p - arg);
215 int flags = eap->forceit ? DIP_ALL : 0;
216
217 if (STRNCMP(arg, "START", len) == 0)
218 {
219 flags += DIP_START + DIP_NORTP;
220 arg = skipwhite(arg + len);
221 }
222 else if (STRNCMP(arg, "OPT", len) == 0)
223 {
224 flags += DIP_OPT + DIP_NORTP;
225 arg = skipwhite(arg + len);
226 }
227 else if (STRNCMP(arg, "PACK", len) == 0)
228 {
229 flags += DIP_START + DIP_OPT + DIP_NORTP;
230 arg = skipwhite(arg + len);
231 }
232 else if (STRNCMP(arg, "ALL", len) == 0)
233 {
234 flags += DIP_START + DIP_OPT;
235 arg = skipwhite(arg + len);
236 }
237
238 source_runtime(arg, flags);
239}
240
241 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100242source_callback(char_u *fname, void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200243{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200245}
246
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000247#ifdef FEAT_EVAL
248/*
249 * Find an already loaded script "name".
250 * If found returns its script ID. If not found returns -1.
251 */
252 static int
253find_script_by_name(char_u *name)
254{
255 int sid;
256 scriptitem_T *si;
257
258 for (sid = script_items.ga_len; sid > 0; --sid)
259 {
260 // We used to check inode here, but that doesn't work:
261 // - If a script is edited and written, it may get a different
262 // inode number, even though to the user it is the same script.
263 // - If a script is deleted and another script is written, with a
264 // different name, the inode may be re-used.
265 si = SCRIPT_ITEM(sid);
266 if (si->sn_name != NULL && fnamecmp(si->sn_name, name) == 0)
267 return sid;
268 }
269 return -1;
270}
271
272/*
273 * Add a new scriptitem with all items initialized.
274 * When running out of memory "error" is set to FAIL.
275 * Returns the script ID.
276 */
277 static int
278get_new_scriptitem(int *error)
279{
280 static scid_T last_current_SID = 0;
281 int sid = ++last_current_SID;
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000282 scriptitem_T *si = NULL;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000283
284 if (ga_grow(&script_items, (int)(sid - script_items.ga_len)) == FAIL)
285 {
286 *error = FAIL;
287 return sid;
288 }
289 while (script_items.ga_len < sid)
290 {
291 si = ALLOC_CLEAR_ONE(scriptitem_T);
292 if (si == NULL)
293 {
294 *error = FAIL;
295 return sid;
296 }
297 ++script_items.ga_len;
298 SCRIPT_ITEM(script_items.ga_len) = si;
299 si->sn_name = NULL;
300 si->sn_version = 1;
301
302 // Allocate the local script variables to use for this script.
303 new_script_vars(script_items.ga_len);
304 ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
305 hash_init(&si->sn_all_vars.dv_hashtab);
306 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
307 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
308# ifdef FEAT_PROFILE
309 si->sn_prof_on = FALSE;
310# endif
311 }
312
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000313 // "si" can't be NULL, check only to avoid a compiler warning
314 if (si != NULL)
315 // Used to check script variable index is still valid.
316 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000317
318 return sid;
319}
320
321 static void
322find_script_callback(char_u *fname, void *cookie)
323{
324 int sid;
325 int error = OK;
326 int *ret_sid = cookie;
327
328 sid = find_script_by_name(fname);
329 if (sid < 0)
330 {
331 // script does not exist yet, create a new scriptitem
332 sid = get_new_scriptitem(&error);
333 if (error == OK)
334 {
335 scriptitem_T *si = SCRIPT_ITEM(sid);
336
337 si->sn_name = vim_strsave(fname);
338 si->sn_state = SN_STATE_NOT_LOADED;
339 }
340 }
341 *ret_sid = sid;
342}
343#endif
344
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200345/*
346 * Find the file "name" in all directories in "path" and invoke
347 * "callback(fname, cookie)".
348 * "name" can contain wildcards.
349 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
350 * When "flags" has DIP_DIR: find directories instead of files.
351 * When "flags" has DIP_ERR: give an error message if there is no match.
352 *
353 * return FAIL when no file could be sourced, OK otherwise.
354 */
355 int
356do_in_path(
357 char_u *path,
358 char_u *name,
359 int flags,
360 void (*callback)(char_u *fname, void *ck),
361 void *cookie)
362{
363 char_u *rtp;
364 char_u *np;
365 char_u *buf;
366 char_u *rtp_copy;
367 char_u *tail;
368 int num_files;
369 char_u **files;
370 int i;
371 int did_one = FALSE;
372#ifdef AMIGA
373 struct Process *proc = (struct Process *)FindTask(0L);
374 APTR save_winptr = proc->pr_WindowPtr;
375
376 // Avoid a requester here for a volume that doesn't exist.
377 proc->pr_WindowPtr = (APTR)-1L;
378#endif
379
380 // Make a copy of 'runtimepath'. Invoking the callback may change the
381 // value.
382 rtp_copy = vim_strsave(path);
383 buf = alloc(MAXPATHL);
384 if (buf != NULL && rtp_copy != NULL)
385 {
Bram Moolenaar647a5302020-05-03 17:01:24 +0200386 if (p_verbose > 10 && name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200387 {
388 verbose_enter();
389 smsg(_("Searching for \"%s\" in \"%s\""),
390 (char *)name, (char *)path);
391 verbose_leave();
392 }
393
394 // Loop over all entries in 'runtimepath'.
395 rtp = rtp_copy;
396 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
397 {
398 size_t buflen;
399
400 // Copy the path from 'runtimepath' to buf[].
401 copy_option_part(&rtp, buf, MAXPATHL, ",");
402 buflen = STRLEN(buf);
403
404 // Skip after or non-after directories.
405 if (flags & (DIP_NOAFTER | DIP_AFTER))
406 {
407 int is_after = buflen >= 5
408 && STRCMP(buf + buflen - 5, "after") == 0;
409
410 if ((is_after && (flags & DIP_NOAFTER))
411 || (!is_after && (flags & DIP_AFTER)))
412 continue;
413 }
414
415 if (name == NULL)
416 {
417 (*callback)(buf, (void *) &cookie);
418 if (!did_one)
419 did_one = (cookie == NULL);
420 }
421 else if (buflen + STRLEN(name) + 2 < MAXPATHL)
422 {
423 add_pathsep(buf);
424 tail = buf + STRLEN(buf);
425
426 // Loop over all patterns in "name"
427 np = name;
428 while (*np != NUL && ((flags & DIP_ALL) || !did_one))
429 {
430 // Append the pattern from "name" to buf[].
431 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
432 "\t ");
433
Bram Moolenaar647a5302020-05-03 17:01:24 +0200434 if (p_verbose > 10)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200435 {
436 verbose_enter();
437 smsg(_("Searching for \"%s\""), buf);
438 verbose_leave();
439 }
440
441 // Expand wildcards, invoke the callback for each match.
442 if (gen_expand_wildcards(1, &buf, &num_files, &files,
443 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
444 {
445 for (i = 0; i < num_files; ++i)
446 {
447 (*callback)(files[i], cookie);
448 did_one = TRUE;
449 if (!(flags & DIP_ALL))
450 break;
451 }
452 FreeWild(num_files, files);
453 }
454 }
455 }
456 }
457 }
458 vim_free(buf);
459 vim_free(rtp_copy);
460 if (!did_one && name != NULL)
461 {
462 char *basepath = path == p_rtp ? "runtimepath" : "packpath";
463
464 if (flags & DIP_ERR)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000465 semsg(_(e_directory_not_found_in_str_str), basepath, name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200466 else if (p_verbose > 0)
467 {
468 verbose_enter();
469 smsg(_("not found in '%s': \"%s\""), basepath, name);
470 verbose_leave();
471 }
472 }
473
474#ifdef AMIGA
475 proc->pr_WindowPtr = save_winptr;
476#endif
477
478 return did_one ? OK : FAIL;
479}
480
481/*
482 * Find "name" in "path". When found, invoke the callback function for
483 * it: callback(fname, "cookie")
484 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first
485 * one is used.
486 * Returns OK when at least one match found, FAIL otherwise.
487 *
488 * If "name" is NULL calls callback for each entry in "path". Cookie is
489 * passed by reference in this case, setting it to NULL indicates that callback
490 * has done its job.
491 */
492 static int
493do_in_path_and_pp(
494 char_u *path,
495 char_u *name,
496 int flags,
497 void (*callback)(char_u *fname, void *ck),
498 void *cookie)
499{
500 int done = FAIL;
501 char_u *s;
502 int len;
503 char *start_dir = "pack/*/start/*/%s";
504 char *opt_dir = "pack/*/opt/*/%s";
505
506 if ((flags & DIP_NORTP) == 0)
507 done = do_in_path(path, name, flags, callback, cookie);
508
509 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
510 {
511 len = (int)(STRLEN(start_dir) + STRLEN(name));
512 s = alloc(len);
513 if (s == NULL)
514 return FAIL;
515 vim_snprintf((char *)s, len, start_dir, name);
516 done = do_in_path(p_pp, s, flags, callback, cookie);
517 vim_free(s);
518 }
519
520 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
521 {
522 len = (int)(STRLEN(opt_dir) + STRLEN(name));
523 s = alloc(len);
524 if (s == NULL)
525 return FAIL;
526 vim_snprintf((char *)s, len, opt_dir, name);
527 done = do_in_path(p_pp, s, flags, callback, cookie);
528 vim_free(s);
529 }
530
531 return done;
532}
533
534/*
535 * Just like do_in_path_and_pp(), using 'runtimepath' for "path".
536 */
537 int
538do_in_runtimepath(
539 char_u *name,
540 int flags,
541 void (*callback)(char_u *fname, void *ck),
542 void *cookie)
543{
544 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
545}
546
547/*
548 * Source the file "name" from all directories in 'runtimepath'.
549 * "name" can contain wildcards.
550 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
551 *
552 * return FAIL when no file could be sourced, OK otherwise.
553 */
554 int
555source_runtime(char_u *name, int flags)
556{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100557 return source_in_path(p_rtp, name, flags, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200558}
559
560/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000561 * Just like source_runtime(), but use "path" instead of 'runtimepath'
562 * and return the script ID in "ret_sid".
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200563 */
564 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100565source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200566{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200568}
569
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200570#if defined(FEAT_EVAL) || defined(PROTO)
571
572/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000573 * Find "name" in 'runtimepath'. If found a new scriptitem is created for it
574 * and it's script ID is returned.
575 * If not found returns -1.
576 */
577 int
578find_script_in_rtp(char_u *name)
579{
580 int sid = -1;
581
582 (void)do_in_path_and_pp(p_rtp, name, DIP_NOAFTER,
583 find_script_callback, &sid);
584 return sid;
585}
586
587/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200588 * Expand wildcards in "pat" and invoke do_source() for each match.
589 */
590 static void
591source_all_matches(char_u *pat)
592{
593 int num_files;
594 char_u **files;
595 int i;
596
597 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK)
598 {
599 for (i = 0; i < num_files; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200601 FreeWild(num_files, files);
602 }
603}
604
605/*
606 * Add the package directory to 'runtimepath'.
607 */
608 static int
609add_pack_dir_to_rtp(char_u *fname)
610{
611 char_u *p4, *p3, *p2, *p1, *p;
612 char_u *entry;
613 char_u *insp = NULL;
614 int c;
615 char_u *new_rtp;
616 int keep;
617 size_t oldlen;
618 size_t addlen;
619 size_t new_rtp_len;
620 char_u *afterdir = NULL;
621 size_t afterlen = 0;
622 char_u *after_insp = NULL;
623 char_u *ffname = NULL;
624 size_t fname_len;
625 char_u *buf = NULL;
626 char_u *rtp_ffname;
627 int match;
628 int retval = FAIL;
629
630 p4 = p3 = p2 = p1 = get_past_head(fname);
631 for (p = p1; *p; MB_PTR_ADV(p))
632 if (vim_ispathsep_nocolon(*p))
633 {
634 p4 = p3; p3 = p2; p2 = p1; p1 = p;
635 }
636
637 // now we have:
638 // rtp/pack/name/start/name
639 // p4 p3 p2 p1
640 //
641 // find the part up to "pack" in 'runtimepath'
642 c = *++p4; // append pathsep in order to expand symlink
643 *p4 = NUL;
644 ffname = fix_fname(fname);
645 *p4 = c;
646 if (ffname == NULL)
647 return FAIL;
648
649 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
650 // Also stop at the first "after" directory.
651 fname_len = STRLEN(ffname);
652 buf = alloc(MAXPATHL);
653 if (buf == NULL)
654 goto theend;
655 for (entry = p_rtp; *entry != NUL; )
656 {
657 char_u *cur_entry = entry;
658
659 copy_option_part(&entry, buf, MAXPATHL, ",");
660 if (insp == NULL)
661 {
662 add_pathsep(buf);
663 rtp_ffname = fix_fname(buf);
664 if (rtp_ffname == NULL)
665 goto theend;
666 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
667 vim_free(rtp_ffname);
668 if (match)
669 // Insert "ffname" after this entry (and comma).
670 insp = entry;
671 }
672
673 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
674 && p > buf
675 && vim_ispathsep(p[-1])
676 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
677 {
678 if (insp == NULL)
679 // Did not find "ffname" before the first "after" directory,
680 // insert it before this entry.
681 insp = cur_entry;
682 after_insp = cur_entry;
683 break;
684 }
685 }
686
687 if (insp == NULL)
688 // Both "fname" and "after" not found, append at the end.
689 insp = p_rtp + STRLEN(p_rtp);
690
691 // check if rtp/pack/name/start/name/after exists
692 afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
693 if (afterdir != NULL && mch_isdir(afterdir))
694 afterlen = STRLEN(afterdir) + 1; // add one for comma
695
696 oldlen = STRLEN(p_rtp);
697 addlen = STRLEN(fname) + 1; // add one for comma
698 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
699 if (new_rtp == NULL)
700 goto theend;
701
702 // We now have 'rtp' parts: {keep}{keep_after}{rest}.
703 // Create new_rtp, first: {keep},{fname}
704 keep = (int)(insp - p_rtp);
705 mch_memmove(new_rtp, p_rtp, keep);
706 new_rtp_len = keep;
707 if (*insp == NUL)
708 new_rtp[new_rtp_len++] = ','; // add comma before
709 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
710 new_rtp_len += addlen - 1;
711 if (*insp != NUL)
712 new_rtp[new_rtp_len++] = ','; // add comma after
713
714 if (afterlen > 0 && after_insp != NULL)
715 {
716 int keep_after = (int)(after_insp - p_rtp);
717
718 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
719 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
720 keep_after - keep);
721 new_rtp_len += keep_after - keep;
722 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
723 new_rtp_len += afterlen - 1;
724 new_rtp[new_rtp_len++] = ',';
725 keep = keep_after;
726 }
727
728 if (p_rtp[keep] != NUL)
729 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
730 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
731 else
732 new_rtp[new_rtp_len] = NUL;
733
734 if (afterlen > 0 && after_insp == NULL)
735 {
736 // Append afterdir when "after" was not found:
737 // {keep},{fname}{rest},{afterdir}
738 STRCAT(new_rtp, ",");
739 STRCAT(new_rtp, afterdir);
740 }
741
742 set_option_value((char_u *)"rtp", 0L, new_rtp, 0);
743 vim_free(new_rtp);
744 retval = OK;
745
746theend:
747 vim_free(buf);
748 vim_free(ffname);
749 vim_free(afterdir);
750 return retval;
751}
752
753/*
754 * Load scripts in "plugin" and "ftdetect" directories of the package.
755 */
756 static int
757load_pack_plugin(char_u *fname)
758{
759 static char *plugpat = "%s/plugin/**/*.vim";
760 static char *ftpat = "%s/ftdetect/*.vim";
761 int len;
762 char_u *ffname = fix_fname(fname);
763 char_u *pat = NULL;
764 int retval = FAIL;
765
766 if (ffname == NULL)
767 return FAIL;
768 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
769 pat = alloc(len);
770 if (pat == NULL)
771 goto theend;
772 vim_snprintf((char *)pat, len, plugpat, ffname);
773 source_all_matches(pat);
774
775 {
776 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
777
778 // If runtime/filetype.vim wasn't loaded yet, the scripts will be
779 // found when it loads.
780 if (cmd != NULL && eval_to_number(cmd) > 0)
781 {
782 do_cmdline_cmd((char_u *)"augroup filetypedetect");
783 vim_snprintf((char *)pat, len, ftpat, ffname);
784 source_all_matches(pat);
785 do_cmdline_cmd((char_u *)"augroup END");
786 }
787 vim_free(cmd);
788 }
789 vim_free(pat);
790 retval = OK;
791
792theend:
793 vim_free(ffname);
794 return retval;
795}
796
797// used for "cookie" of add_pack_plugin()
798static int APP_ADD_DIR;
799static int APP_LOAD;
800static int APP_BOTH;
801
802 static void
803add_pack_plugin(char_u *fname, void *cookie)
804{
805 if (cookie != &APP_LOAD)
806 {
807 char_u *buf = alloc(MAXPATHL);
808 char_u *p;
809 int found = FALSE;
810
811 if (buf == NULL)
812 return;
813 p = p_rtp;
814 while (*p != NUL)
815 {
816 copy_option_part(&p, buf, MAXPATHL, ",");
817 if (pathcmp((char *)buf, (char *)fname, -1) == 0)
818 {
819 found = TRUE;
820 break;
821 }
822 }
823 vim_free(buf);
824 if (!found)
825 // directory is not yet in 'runtimepath', add it
826 if (add_pack_dir_to_rtp(fname) == FAIL)
827 return;
828 }
829
830 if (cookie != &APP_ADD_DIR)
831 load_pack_plugin(fname);
832}
833
834/*
835 * Add all packages in the "start" directory to 'runtimepath'.
836 */
837 void
838add_pack_start_dirs(void)
839{
840 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
841 add_pack_plugin, &APP_ADD_DIR);
842}
843
844/*
845 * Load plugins from all packages in the "start" directory.
846 */
847 void
848load_start_packages(void)
849{
850 did_source_packages = TRUE;
851 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
852 add_pack_plugin, &APP_LOAD);
853}
854
855/*
856 * ":packloadall"
857 * Find plugins in the package directories and source them.
858 */
859 void
860ex_packloadall(exarg_T *eap)
861{
862 if (!did_source_packages || eap->forceit)
863 {
864 // First do a round to add all directories to 'runtimepath', then load
865 // the plugins. This allows for plugins to use an autoload directory
866 // of another plugin.
867 add_pack_start_dirs();
868 load_start_packages();
869 }
870}
871
872/*
873 * ":packadd[!] {name}"
874 */
875 void
876ex_packadd(exarg_T *eap)
877{
878 static char *plugpat = "pack/*/%s/%s";
879 int len;
880 char *pat;
881 int round;
882 int res = OK;
883
884 // Round 1: use "start", round 2: use "opt".
885 for (round = 1; round <= 2; ++round)
886 {
887 // Only look under "start" when loading packages wasn't done yet.
888 if (round == 1 && did_source_packages)
889 continue;
890
891 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
892 pat = alloc(len);
893 if (pat == NULL)
894 return;
895 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
896 // The first round don't give a "not found" error, in the second round
897 // only when nothing was found in the first round.
898 res = do_in_path(p_pp, (char_u *)pat,
899 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
900 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
901 vim_free(pat);
902 }
903}
904#endif
905
906/*
Bram Moolenaar26262f82019-09-04 20:59:15 +0200907 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
908 * list of file names in allocated memory.
909 */
910 void
911remove_duplicates(garray_T *gap)
912{
913 int i;
914 int j;
915 char_u **fnames = (char_u **)gap->ga_data;
916
917 sort_strings(fnames, gap->ga_len);
918 for (i = gap->ga_len - 1; i > 0; --i)
919 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
920 {
921 vim_free(fnames[i]);
922 for (j = i + 1; j < gap->ga_len; ++j)
923 fnames[j - 1] = fnames[j];
924 --gap->ga_len;
925 }
926}
927
928/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200929 * Expand color scheme, compiler or filetype names.
930 * Search from 'runtimepath':
931 * 'runtimepath'/{dirnames}/{pat}.vim
932 * When "flags" has DIP_START: search also from 'start' of 'packpath':
933 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
934 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
935 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
936 * "dirnames" is an array with one or more directory names.
937 */
938 int
939ExpandRTDir(
940 char_u *pat,
941 int flags,
942 int *num_file,
943 char_u ***file,
944 char *dirnames[])
945{
946 char_u *s;
947 char_u *e;
948 char_u *match;
949 garray_T ga;
950 int i;
951 int pat_len;
952
953 *num_file = 0;
954 *file = NULL;
955 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000956 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200957
958 for (i = 0; dirnames[i] != NULL; ++i)
959 {
960 s = alloc(STRLEN(dirnames[i]) + pat_len + 7);
961 if (s == NULL)
962 {
963 ga_clear_strings(&ga);
964 return FAIL;
965 }
966 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
967 globpath(p_rtp, s, &ga, 0);
968 vim_free(s);
969 }
970
971 if (flags & DIP_START) {
972 for (i = 0; dirnames[i] != NULL; ++i)
973 {
974 s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
975 if (s == NULL)
976 {
977 ga_clear_strings(&ga);
978 return FAIL;
979 }
980 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
981 globpath(p_pp, s, &ga, 0);
982 vim_free(s);
983 }
984 }
985
986 if (flags & DIP_OPT) {
987 for (i = 0; dirnames[i] != NULL; ++i)
988 {
989 s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
990 if (s == NULL)
991 {
992 ga_clear_strings(&ga);
993 return FAIL;
994 }
995 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
996 globpath(p_pp, s, &ga, 0);
997 vim_free(s);
998 }
999 }
1000
1001 for (i = 0; i < ga.ga_len; ++i)
1002 {
1003 match = ((char_u **)ga.ga_data)[i];
1004 s = match;
1005 e = s + STRLEN(s);
1006 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
1007 {
1008 e -= 4;
1009 for (s = e; s > match; MB_PTR_BACK(match, s))
1010 if (s < match || vim_ispathsep(*s))
1011 break;
1012 ++s;
1013 *e = NUL;
1014 mch_memmove(match, s, e - s + 1);
1015 }
1016 }
1017
1018 if (ga.ga_len == 0)
1019 return FAIL;
1020
1021 // Sort and remove duplicates which can happen when specifying multiple
1022 // directories in dirnames.
1023 remove_duplicates(&ga);
1024
1025 *file = ga.ga_data;
1026 *num_file = ga.ga_len;
1027 return OK;
1028}
1029
1030/*
1031 * Expand loadplugin names:
1032 * 'packpath'/pack/ * /opt/{pat}
1033 */
1034 int
1035ExpandPackAddDir(
1036 char_u *pat,
1037 int *num_file,
1038 char_u ***file)
1039{
1040 char_u *s;
1041 char_u *e;
1042 char_u *match;
1043 garray_T ga;
1044 int i;
1045 int pat_len;
1046
1047 *num_file = 0;
1048 *file = NULL;
1049 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001050 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001051
1052 s = alloc(pat_len + 26);
1053 if (s == NULL)
1054 {
1055 ga_clear_strings(&ga);
1056 return FAIL;
1057 }
1058 sprintf((char *)s, "pack/*/opt/%s*", pat);
1059 globpath(p_pp, s, &ga, 0);
1060 vim_free(s);
1061
1062 for (i = 0; i < ga.ga_len; ++i)
1063 {
1064 match = ((char_u **)ga.ga_data)[i];
1065 s = gettail(match);
1066 e = s + STRLEN(s);
1067 mch_memmove(match, s, e - s + 1);
1068 }
1069
1070 if (ga.ga_len == 0)
1071 return FAIL;
1072
1073 // Sort and remove duplicates which can happen when specifying multiple
1074 // directories in dirnames.
1075 remove_duplicates(&ga);
1076
1077 *file = ga.ga_data;
1078 *num_file = ga.ga_len;
1079 return OK;
1080}
1081
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001082/*
1083 * Cookie used to source Ex commands from a buffer.
1084 */
1085typedef struct
1086{
1087 garray_T lines_to_source;
1088 int lnum;
1089 linenr_T sourcing_lnum;
1090} bufline_cookie_T;
1091
1092/*
1093 * Concatenate a Vim script line if it starts with a line continuation into a
1094 * growarray (excluding the continuation chars and leading whitespace).
1095 * Growsize of the growarray may be changed to speed up concatenations!
1096 *
1097 * Returns TRUE if this line did begin with a continuation (the next line
1098 * should also be considered, if it exists); FALSE otherwise.
1099 */
1100 static int
1101concat_continued_line(
1102 garray_T *ga,
1103 int init_growsize,
1104 char_u *nextline,
1105 int options)
1106{
1107 int comment_char = in_vim9script() ? '#' : '"';
1108 char_u *p = skipwhite(nextline);
1109 int contline;
1110 int do_vim9_all = in_vim9script()
1111 && options == GETLINE_CONCAT_ALL;
1112 int do_bar_cont = do_vim9_all
1113 || options == GETLINE_CONCAT_CONTBAR;
1114
1115 if (*p == NUL)
1116 return FALSE;
1117
1118 // Concatenate the next line when it starts with a backslash.
1119 /* Also check for a comment in between continuation lines: "\ */
1120 // Also check for a Vim9 comment, empty line, line starting with '|',
1121 // but not "||".
1122 if ((p[0] == comment_char && p[1] == '\\' && p[2] == ' ')
1123 || (do_vim9_all && (*p == NUL
1124 || vim9_comment_start(p))))
1125 return TRUE;
1126
1127 contline = (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|'));
1128 if (!contline)
1129 return FALSE;
1130
1131 // Adjust the growsize to the current length to speed up concatenating many
1132 // lines.
1133 if (ga->ga_len > init_growsize)
1134 ga->ga_growsize = ga->ga_len > 8000 ? 8000 : ga->ga_len;
1135 if (*p == '\\')
1136 ga_concat(ga, (char_u *)p + 1);
1137 else if (*p == '|')
1138 {
1139 ga_concat(ga, (char_u *)" ");
1140 ga_concat(ga, p);
1141 }
1142
1143 return TRUE;
1144}
1145
1146/*
1147 * Get one full line from a sourced string (in-memory, no file).
1148 * Called by do_cmdline() when it's called from source_using_linegetter().
1149 *
1150 * Returns a pointer to allocated line, or NULL for end-of-file.
1151 */
1152 static char_u *
1153source_getbufline(
1154 int c UNUSED,
1155 void *cookie,
1156 int indent UNUSED,
1157 getline_opt_T opts)
1158{
1159 bufline_cookie_T *p = cookie;
1160 char_u *line;
1161 garray_T ga;
1162
1163 SOURCING_LNUM = p->sourcing_lnum + 1;
1164
1165 if (p->lnum >= p->lines_to_source.ga_len)
1166 return NULL;
1167 line = ((char_u **)p->lines_to_source.ga_data)[p->lnum];
1168
1169 ga_init2(&ga, sizeof(char_u), 400);
1170 ga_concat(&ga, (char_u *)line);
1171 p->lnum++;
1172
1173 if ((opts != GETLINE_NONE) && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
1174 {
1175 while (p->lnum < p->lines_to_source.ga_len)
1176 {
1177 line = ((char_u **)p->lines_to_source.ga_data)[p->lnum];
1178 if (!concat_continued_line(&ga, 400, line, opts))
1179 break;
1180 p->sourcing_lnum++;
1181 p->lnum++;
1182 }
1183 }
1184 ga_append(&ga, NUL);
1185 p->sourcing_lnum++;
1186
1187 return ga.ga_data;
1188}
1189
1190/*
1191 * Source Ex commands from the lines in 'cookie'.
1192 */
1193 static int
1194do_sourcebuffer(
1195 void *cookie,
1196 char_u *scriptname)
1197{
1198 char_u *save_sourcing_name = SOURCING_NAME;
1199 linenr_T save_sourcing_lnum = SOURCING_LNUM;
1200 char_u sourcing_name_buf[256];
1201 sctx_T save_current_sctx;
1202#ifdef FEAT_EVAL
1203 int sid;
1204 funccal_entry_T funccalp_entry;
1205 int save_estack_compiling = estack_compiling;
1206 scriptitem_T *si = NULL;
1207#endif
1208 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
1209 int retval = FAIL;
1210 ESTACK_CHECK_DECLARATION
1211
1212 if (save_sourcing_name == NULL)
1213 SOURCING_NAME = (char_u *)scriptname;
1214 else
1215 {
1216 vim_snprintf((char *)sourcing_name_buf, sizeof(sourcing_name_buf),
1217 "%s called at %s:%ld", scriptname, save_sourcing_name,
1218 save_sourcing_lnum);
1219 SOURCING_NAME = sourcing_name_buf;
1220 }
1221 SOURCING_LNUM = 0;
1222
1223 // Keep the sourcing name/lnum, for recursive calls.
1224 estack_push(ETYPE_SCRIPT, scriptname, 0);
1225 ESTACK_CHECK_SETUP
1226
1227 // "legacy" does not apply to commands in the script
1228 sticky_cmdmod_flags = 0;
1229
1230 save_current_sctx = current_sctx;
1231 current_sctx.sc_version = 1; // default script version
1232#ifdef FEAT_EVAL
1233 estack_compiling = FALSE;
1234 // Always use a new sequence number.
1235 current_sctx.sc_seq = ++last_current_SID_seq;
1236 current_sctx.sc_lnum = save_sourcing_lnum;
1237 save_funccal(&funccalp_entry);
1238
1239 sid = find_script_by_name(scriptname);
1240 if (sid < 0)
1241 {
1242 int error = OK;
1243
1244 // First time sourcing this buffer, create a new script item.
1245
1246 sid = get_new_scriptitem(&error);
1247 if (error == FAIL)
1248 goto theend;
1249 current_sctx.sc_sid = sid;
1250 si = SCRIPT_ITEM(current_sctx.sc_sid);
1251 si->sn_name = vim_strsave(scriptname);
1252 si->sn_state = SN_STATE_NEW;
1253 }
1254 else
1255 {
1256 // the buffer was sourced previously, reuse the script ID.
1257 current_sctx.sc_sid = sid;
1258 si = SCRIPT_ITEM(current_sctx.sc_sid);
1259 si->sn_state = SN_STATE_RELOAD;
1260 }
1261#endif
1262
1263 retval = do_cmdline(NULL, source_getbufline, cookie,
1264 DOCMD_VERBOSE | DOCMD_NOWAIT | DOCMD_REPEAT);
1265
1266 if (got_int)
1267 emsg(_(e_interrupted));
1268
1269#ifdef FEAT_EVAL
1270theend:
1271#endif
1272 ESTACK_CHECK_NOW
1273 estack_pop();
1274 current_sctx = save_current_sctx;
1275 SOURCING_LNUM = save_sourcing_lnum;
1276 SOURCING_NAME = save_sourcing_name;
1277 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
1278#ifdef FEAT_EVAL
1279 restore_funccal();
1280 estack_compiling = save_estack_compiling;
1281#endif
1282
1283 return retval;
1284}
1285
1286/*
1287 * :source Ex commands from the current buffer
1288 */
1289 static void
1290cmd_source_buffer(exarg_T *eap)
1291{
1292 char_u *line = NULL;
1293 linenr_T curr_lnum;
1294 bufline_cookie_T cp;
1295 char_u sname[32];
1296
1297 if (curbuf == NULL)
1298 return;
1299
1300 // Use ":source buffer=<num>" as the script name
1301 vim_snprintf((char *)sname, sizeof(sname), ":source buffer=%d",
1302 curbuf->b_fnum);
1303
1304 ga_init2(&cp.lines_to_source, sizeof(char_u *), 100);
1305
1306 // Copy the lines from the buffer into a grow array
1307 for (curr_lnum = eap->line1; curr_lnum <= eap->line2; curr_lnum++)
1308 {
1309 line = vim_strsave(ml_get(curr_lnum));
1310 if (line == NULL)
1311 goto errret;
1312 if (ga_add_string(&cp.lines_to_source, line) == FAIL)
1313 goto errret;
1314 line = NULL;
1315 }
1316 cp.sourcing_lnum = 0;
1317 cp.lnum = 0;
1318
1319 // Execute the Ex commands
1320 do_sourcebuffer((void *)&cp, (char_u *)sname);
1321
1322errret:
1323 vim_free(line);
1324 ga_clear_strings(&cp.lines_to_source);
1325}
1326
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001327 static void
1328cmd_source(char_u *fname, exarg_T *eap)
1329{
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001330 if (*fname != NUL && eap != NULL && eap->addr_count > 0)
1331 {
1332 // if a filename is specified to :source, then a range is not allowed
1333 emsg(_(e_no_range_allowed));
1334 return;
1335 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001336
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001337 if (eap != NULL && *fname == NUL)
1338 {
1339 if (eap->forceit)
1340 // a file name is needed to source normal mode commands
1341 emsg(_(e_argument_required));
1342 else
1343 // source ex commands from the current buffer
1344 cmd_source_buffer(eap);
1345 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001346 else if (eap != NULL && eap->forceit)
1347 // ":source!": read Normal mode commands
1348 // Need to execute the commands directly. This is required at least
1349 // for:
1350 // - ":g" command busy
1351 // - after ":argdo", ":windo" or ":bufdo"
1352 // - another command follows
1353 // - inside a loop
1354 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
1355#ifdef FEAT_EVAL
1356 || eap->cstack->cs_idx >= 0
1357#endif
1358 );
1359
1360 // ":source" read ex commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001362 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001363}
1364
1365/*
1366 * ":source {fname}"
1367 */
1368 void
1369ex_source(exarg_T *eap)
1370{
1371#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02001372 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001373 {
1374 char_u *fname = NULL;
1375
1376 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
1377 NULL, NULL,
1378 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1379 if (fname != NULL)
1380 {
1381 cmd_source(fname, eap);
1382 vim_free(fname);
1383 }
1384 }
1385 else
1386#endif
1387 cmd_source(eap->arg, eap);
1388}
1389
1390#if defined(FEAT_EVAL) || defined(PROTO)
1391/*
1392 * ":options"
1393 */
1394 void
1395ex_options(
1396 exarg_T *eap UNUSED)
1397{
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001398 char_u buf[500];
1399 int multi_mods = 0;
1400
1401 buf[0] = NUL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001402 (void)add_win_cmd_modifers(buf, &cmdmod, &multi_mods);
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001403
1404 vim_setenv((char_u *)"OPTWIN_CMD", buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001405 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
1406}
1407#endif
1408
1409/*
1410 * ":source" and associated commands.
1411 */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001412
1413#ifdef FEAT_EVAL
1414/*
1415 * Return the address holding the next breakpoint line for a source cookie.
1416 */
1417 linenr_T *
1418source_breakpoint(void *cookie)
1419{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001420 return &((source_cookie_T *)cookie)->breakpoint;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001421}
1422
1423/*
1424 * Return the address holding the debug tick for a source cookie.
1425 */
1426 int *
1427source_dbg_tick(void *cookie)
1428{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001429 return &((source_cookie_T *)cookie)->dbg_tick;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001430}
1431
1432/*
1433 * Return the nesting level for a source cookie.
1434 */
1435 int
1436source_level(void *cookie)
1437{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001438 return ((source_cookie_T *)cookie)->level;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001439}
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001440
1441/*
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02001442 * Return the readahead line. Note that the pointer may become invalid when
1443 * getting the next line, if it's concatenated with the next one.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001444 */
1445 char_u *
1446source_nextline(void *cookie)
1447{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001448 return ((source_cookie_T *)cookie)->nextline;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001449}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001450#endif
1451
1452#if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC)
1453# define USE_FOPEN_NOINH
1454/*
1455 * Special function to open a file without handle inheritance.
1456 * When possible the handle is closed on exec().
1457 */
1458 static FILE *
1459fopen_noinh_readbin(char *filename)
1460{
1461# ifdef MSWIN
1462 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
1463# else
1464 int fd_tmp = mch_open(filename, O_RDONLY, 0);
1465# endif
1466
1467 if (fd_tmp == -1)
1468 return NULL;
1469
1470# ifdef HAVE_FD_CLOEXEC
1471 {
1472 int fdflags = fcntl(fd_tmp, F_GETFD);
1473 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1474 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
1475 }
1476# endif
1477
1478 return fdopen(fd_tmp, READBIN);
1479}
1480#endif
1481
1482/*
1483 * do_source: Read the file "fname" and execute its lines as EX commands.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001484 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1485 * again.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001486 *
1487 * This function may be called recursively!
1488 *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001489 * Return FAIL if file could not be opened, OK otherwise.
1490 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001491 */
1492 int
1493do_source(
1494 char_u *fname,
1495 int check_other, // check for .vimrc and _vimrc
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496 int is_vimrc, // DOSO_ value
1497 int *ret_sid UNUSED)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001498{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001499 source_cookie_T cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001500 char_u *p;
1501 char_u *fname_exp;
1502 char_u *firstline = NULL;
1503 int retval = FAIL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001504 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001505#ifdef FEAT_EVAL
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001506 funccal_entry_T funccalp_entry;
1507 int save_debug_break_level = debug_break_level;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508 int sid;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001509 scriptitem_T *si = NULL;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001510 int save_estack_compiling = estack_compiling;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001511#endif
1512#ifdef STARTUPTIME
1513 struct timeval tv_rel;
1514 struct timeval tv_start;
1515#endif
1516#ifdef FEAT_PROFILE
1517 proftime_T wait_start;
1518#endif
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001519 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001520 int trigger_source_post = FALSE;
Bram Moolenaare31ee862020-01-07 20:59:34 +01001521 ESTACK_CHECK_DECLARATION
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001522
1523 p = expand_env_save(fname);
1524 if (p == NULL)
1525 return retval;
1526 fname_exp = fix_fname(p);
1527 vim_free(p);
1528 if (fname_exp == NULL)
1529 return retval;
1530 if (mch_isdir(fname_exp))
1531 {
1532 smsg(_("Cannot source a directory: \"%s\""), fname);
1533 goto theend;
1534 }
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001535#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001536 estack_compiling = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001537
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538 // See if we loaded this script before.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001539 sid = find_script_by_name(fname_exp);
Bram Moolenaarf479cac2022-01-12 12:54:55 +00001540 if (sid > 0 && ret_sid != NULL
1541 && SCRIPT_ITEM(sid)->sn_state != SN_STATE_NOT_LOADED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001542 {
1543 // Already loaded and no need to load again, return here.
1544 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001545 retval = OK;
1546 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 }
1548#endif
1549
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001550 // Apply SourceCmd autocommands, they should get the file and source it.
1551 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1552 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1553 FALSE, curbuf))
1554 {
1555#ifdef FEAT_EVAL
1556 retval = aborting() ? FAIL : OK;
1557#else
1558 retval = OK;
1559#endif
1560 if (retval == OK)
1561 // Apply SourcePost autocommands.
1562 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1563 FALSE, curbuf);
1564 goto theend;
1565 }
1566
1567 // Apply SourcePre autocommands, they may get the file.
1568 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1569
1570#ifdef USE_FOPEN_NOINH
1571 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1572#else
1573 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1574#endif
1575 if (cookie.fp == NULL && check_other)
1576 {
1577 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1578 // and ".exrc" by "_exrc" or vice versa.
1579 p = gettail(fname_exp);
1580 if ((*p == '.' || *p == '_')
1581 && (STRICMP(p + 1, "vimrc") == 0
1582 || STRICMP(p + 1, "gvimrc") == 0
1583 || STRICMP(p + 1, "exrc") == 0))
1584 {
1585 if (*p == '_')
1586 *p = '.';
1587 else
1588 *p = '_';
1589#ifdef USE_FOPEN_NOINH
1590 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1591#else
1592 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1593#endif
1594 }
1595 }
1596
1597 if (cookie.fp == NULL)
1598 {
1599 if (p_verbose > 0)
1600 {
1601 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001602 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001603 smsg(_("could not source \"%s\""), fname);
1604 else
1605 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001606 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001607 verbose_leave();
1608 }
1609 goto theend;
1610 }
1611
1612 // The file exists.
1613 // - In verbose mode, give a message.
1614 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1615 if (p_verbose > 1)
1616 {
1617 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001618 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001619 smsg(_("sourcing \"%s\""), fname);
1620 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001621 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001622 verbose_leave();
1623 }
1624 if (is_vimrc == DOSO_VIMRC)
1625 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1626 else if (is_vimrc == DOSO_GVIMRC)
1627 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1628
1629#ifdef USE_CRNL
1630 // If no automatic file format: Set default to CR-NL.
1631 if (*p_ffs == NUL)
1632 cookie.fileformat = EOL_DOS;
1633 else
1634 cookie.fileformat = EOL_UNKNOWN;
1635 cookie.error = FALSE;
1636#endif
1637
1638 cookie.nextline = NULL;
1639 cookie.sourcing_lnum = 0;
1640 cookie.finished = FALSE;
1641
1642#ifdef FEAT_EVAL
1643 // Check if this script has a breakpoint.
1644 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1645 cookie.fname = fname_exp;
1646 cookie.dbg_tick = debug_tick;
1647
1648 cookie.level = ex_nesting_level;
1649#endif
1650
1651 // Keep the sourcing name/lnum, for recursive calls.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001652 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001653 ESTACK_CHECK_SETUP
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001654
1655#ifdef STARTUPTIME
1656 if (time_fd != NULL)
1657 time_push(&tv_rel, &tv_start);
1658#endif
1659
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001660 // "legacy" does not apply to commands in the script
1661 sticky_cmdmod_flags = 0;
1662
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001663 save_current_sctx = current_sctx;
1664 current_sctx.sc_version = 1; // default script version
1665
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001666#ifdef FEAT_EVAL
1667# ifdef FEAT_PROFILE
1668 if (do_profiling == PROF_YES)
1669 prof_child_enter(&wait_start); // entering a child now
1670# endif
1671
1672 // Don't use local function variables, if called from a function.
1673 // Also starts profiling timer for nested script.
1674 save_funccal(&funccalp_entry);
1675
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001676 current_sctx.sc_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001677
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001678 // Check if this script was sourced before to find its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001679 // Always use a new sequence number.
1680 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001682 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683 hashtab_T *ht;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001684 int todo;
1685 hashitem_T *hi;
1686 dictitem_T *di;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687
1688 // loading the same script again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001690 si = SCRIPT_ITEM(sid);
1691 if (si->sn_state == SN_STATE_NOT_LOADED)
1692 {
1693 // this script was found but not loaded yet
1694 si->sn_state = SN_STATE_NEW;
1695 }
1696 else
1697 {
1698 si->sn_state = SN_STATE_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001700 // Script-local variables remain but "const" can be set again.
1701 // In Vim9 script variables will be cleared when "vim9script" is
1702 // encountered without the "noclear" argument.
1703 ht = &SCRIPT_VARS(sid);
1704 todo = (int)ht->ht_used;
1705 for (hi = ht->ht_array; todo > 0; ++hi)
1706 if (!HASHITEM_EMPTY(hi))
1707 {
1708 --todo;
1709 di = HI2DI(hi);
1710 di->di_flags |= DI_FLAGS_RELOAD;
1711 }
1712 // imports can be redefined once
1713 mark_imports_for_reload(sid);
Bram Moolenaar0123cc12021-02-07 17:17:58 +01001714
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001715 // reset version, "vim9script" may have been added or removed.
1716 si->sn_version = 1;
1717 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001718 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001720 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001721 int error = OK;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001722
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001723 // It's new, generate a new SID and initialize the scriptitem.
1724 current_sctx.sc_sid = get_new_scriptitem(&error);
1725 if (error == FAIL)
1726 goto almosttheend;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001727 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001728 si->sn_name = fname_exp;
1729 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 if (ret_sid != NULL)
1731 *ret_sid = current_sctx.sc_sid;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001732
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001733 // Remember the "is_vimrc" flag for when the file is sourced again.
1734 si->sn_is_vimrc = is_vimrc;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001735 }
1736
1737# ifdef FEAT_PROFILE
1738 if (do_profiling == PROF_YES)
1739 {
1740 int forceit;
1741
1742 // Check if we do profiling for this script.
1743 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
1744 {
1745 script_do_profile(si);
1746 si->sn_pr_force = forceit;
1747 }
1748 if (si->sn_prof_on)
1749 {
1750 ++si->sn_pr_count;
1751 profile_start(&si->sn_pr_start);
1752 profile_zero(&si->sn_pr_children);
1753 }
1754 }
1755# endif
1756#endif
1757
1758 cookie.conv.vc_type = CONV_NONE; // no conversion
1759
1760 // Read the first line so we can check for a UTF-8 BOM.
1761 firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1762 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1763 && firstline[1] == 0xbb && firstline[2] == 0xbf)
1764 {
1765 // Found BOM; setup conversion, skip over BOM and recode the line.
1766 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1767 p = string_convert(&cookie.conv, firstline + 3, NULL);
1768 if (p == NULL)
1769 p = vim_strsave(firstline + 3);
1770 if (p != NULL)
1771 {
1772 vim_free(firstline);
1773 firstline = p;
1774 }
1775 }
1776
1777 // Call do_cmdline, which will call getsourceline() to get the lines.
1778 do_cmdline(firstline, getsourceline, (void *)&cookie,
1779 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1780 retval = OK;
1781
1782#ifdef FEAT_PROFILE
1783 if (do_profiling == PROF_YES)
1784 {
1785 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001786 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001787 if (si->sn_prof_on)
1788 {
1789 profile_end(&si->sn_pr_start);
1790 profile_sub_wait(&wait_start, &si->sn_pr_start);
1791 profile_add(&si->sn_pr_total, &si->sn_pr_start);
1792 profile_self(&si->sn_pr_self, &si->sn_pr_start,
1793 &si->sn_pr_children);
1794 }
1795 }
1796#endif
1797
1798 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001799 emsg(_(e_interrupted));
Bram Moolenaare31ee862020-01-07 20:59:34 +01001800 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001801 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001802 if (p_verbose > 1)
1803 {
1804 verbose_enter();
1805 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001806 if (SOURCING_NAME != NULL)
1807 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001808 verbose_leave();
1809 }
1810#ifdef STARTUPTIME
1811 if (time_fd != NULL)
1812 {
1813 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1814 time_msg((char *)IObuff, &tv_start);
1815 time_pop(&tv_rel);
1816 }
1817#endif
1818
1819 if (!got_int)
1820 trigger_source_post = TRUE;
1821
1822#ifdef FEAT_EVAL
1823 // After a "finish" in debug mode, need to break at first command of next
1824 // sourced file.
1825 if (save_debug_break_level > ex_nesting_level
1826 && debug_break_level == ex_nesting_level)
1827 ++debug_break_level;
1828#endif
1829
1830#ifdef FEAT_EVAL
1831almosttheend:
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001832 // If "sn_save_cpo" is set that means we encountered "vim9script": restore
1833 // 'cpoptions', unless in the main .vimrc file.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001835 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001836 if (si->sn_save_cpo != NULL && si->sn_is_vimrc == DOSO_NONE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 {
Bram Moolenaar3e191692021-03-17 17:46:00 +01001838 if (STRCMP(p_cpo, CPO_VIM) != 0)
1839 {
1840 char_u *f;
1841 char_u *t;
1842
1843 // 'cpo' was changed in the script. Apply the same change to the
1844 // saved value, if possible.
1845 for (f = (char_u *)CPO_VIM; *f != NUL; ++f)
1846 if (vim_strchr(p_cpo, *f) == NULL
1847 && (t = vim_strchr(si->sn_save_cpo, *f)) != NULL)
1848 // flag was removed, also remove it from the saved 'cpo'
1849 mch_memmove(t, t + 1, STRLEN(t));
1850 for (f = p_cpo; *f != NUL; ++f)
1851 if (vim_strchr((char_u *)CPO_VIM, *f) == NULL
1852 && vim_strchr(si->sn_save_cpo, *f) == NULL)
1853 {
1854 // flag was added, also add it to the saved 'cpo'
1855 t = alloc(STRLEN(si->sn_save_cpo) + 2);
1856 if (t != NULL)
1857 {
1858 *t = *f;
1859 STRCPY(t + 1, si->sn_save_cpo);
1860 vim_free(si->sn_save_cpo);
1861 si->sn_save_cpo = t;
1862 }
1863 }
1864 }
Bram Moolenaar37294bd2021-03-10 13:40:08 +01001865 set_option_value((char_u *)"cpo", 0L, si->sn_save_cpo, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001866 }
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001867 VIM_CLEAR(si->sn_save_cpo);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001869 restore_funccal();
1870# ifdef FEAT_PROFILE
1871 if (do_profiling == PROF_YES)
1872 prof_child_exit(&wait_start); // leaving a child now
1873# endif
1874#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001875 current_sctx = save_current_sctx;
1876
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001877 fclose(cookie.fp);
1878 vim_free(cookie.nextline);
1879 vim_free(firstline);
1880 convert_setup(&cookie.conv, NULL, NULL);
1881
1882 if (trigger_source_post)
1883 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1884
1885theend:
1886 vim_free(fname_exp);
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001887 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001888#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001889 estack_compiling = save_estack_compiling;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001890#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001891 return retval;
1892}
1893
1894#if defined(FEAT_EVAL) || defined(PROTO)
1895
1896/*
1897 * ":scriptnames"
1898 */
1899 void
1900ex_scriptnames(exarg_T *eap)
1901{
1902 int i;
1903
1904 if (eap->addr_count > 0)
1905 {
1906 // :script {scriptId}: edit the script
Bram Moolenaare3d46852020-08-29 13:39:17 +02001907 if (!SCRIPT_ID_VALID(eap->line2))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001908 emsg(_(e_invalid_argument));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001909 else
1910 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001911 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001912 do_exedit(eap, NULL);
1913 }
1914 return;
1915 }
1916
1917 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar6079da72022-01-18 14:16:59 +00001918 {
1919 scriptitem_T *si = SCRIPT_ITEM(i);
1920
1921 if (si->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001922 {
Bram Moolenaar6079da72022-01-18 14:16:59 +00001923 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
1924 vim_snprintf((char *)IObuff, IOSIZE, "%3d%s: %s",
1925 i,
1926 si->sn_state == SN_STATE_NOT_LOADED ? " A" : "",
1927 NameBuff);
Bram Moolenaar769f5892022-02-09 14:31:05 +00001928 if (!message_filtered(IObuff))
1929 {
1930 msg_putchar('\n');
1931 msg_outtrans(IObuff);
1932 out_flush(); // output one line at a time
1933 ui_breakcheck();
1934 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001935 }
Bram Moolenaar6079da72022-01-18 14:16:59 +00001936 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001937}
1938
1939# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1940/*
1941 * Fix slashes in the list of script names for 'shellslash'.
1942 */
1943 void
1944scriptnames_slash_adjust(void)
1945{
1946 int i;
1947
1948 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001949 if (SCRIPT_ITEM(i)->sn_name != NULL)
1950 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001951}
1952# endif
1953
1954/*
1955 * Get a pointer to a script name. Used for ":verbose set".
Bram Moolenaar74667062020-12-28 15:41:41 +01001956 * Message appended to "Last set from "
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001957 */
1958 char_u *
1959get_scriptname(scid_T id)
1960{
1961 if (id == SID_MODELINE)
1962 return (char_u *)_("modeline");
1963 if (id == SID_CMDARG)
1964 return (char_u *)_("--cmd argument");
1965 if (id == SID_CARG)
1966 return (char_u *)_("-c argument");
1967 if (id == SID_ENV)
1968 return (char_u *)_("environment variable");
1969 if (id == SID_ERROR)
1970 return (char_u *)_("error handler");
Bram Moolenaar74667062020-12-28 15:41:41 +01001971 if (id == SID_WINLAYOUT)
1972 return (char_u *)_("changed window size");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001973 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001974}
1975
1976# if defined(EXITFREE) || defined(PROTO)
1977 void
1978free_scriptnames(void)
1979{
1980 int i;
1981
1982 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02001983 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001984 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001986 // the variables themselves are cleared in evalvars_clear()
1987 vim_free(si->sn_vars);
1988
1989 vim_free(si->sn_name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001990 free_imports_and_script_vars(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001991 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02001992# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001993 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02001994# endif
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00001995 vim_free(si->sn_autoload_prefix);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001996 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02001997 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001998 ga_clear(&script_items);
1999}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002000
2001 void
2002free_autoload_scriptnames(void)
2003{
2004 ga_clear_strings(&ga_loaded);
2005}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002006# endif
2007
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002008 linenr_T
Bram Moolenaar66250c92020-08-20 15:02:42 +02002009get_sourced_lnum(
2010 char_u *(*fgetline)(int, void *, int, getline_opt_T),
2011 void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002012{
2013 return fgetline == getsourceline
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002014 ? ((source_cookie_T *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002015 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002016}
Dominique Pelle748b3082022-01-08 12:41:16 +00002017#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002018
2019 static char_u *
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002020get_one_sourceline(source_cookie_T *sp)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002021{
2022 garray_T ga;
2023 int len;
2024 int c;
2025 char_u *buf;
2026#ifdef USE_CRNL
2027 int has_cr; // CR-LF found
2028#endif
2029 int have_read = FALSE;
2030
2031 // use a growarray to store the sourced line
2032 ga_init2(&ga, 1, 250);
2033
2034 // Loop until there is a finished line (or end-of-file).
2035 ++sp->sourcing_lnum;
2036 for (;;)
2037 {
2038 // make room to read at least 120 (more) characters
2039 if (ga_grow(&ga, 120) == FAIL)
2040 break;
2041 buf = (char_u *)ga.ga_data;
2042
2043 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
2044 sp->fp) == NULL)
2045 break;
2046 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
2047#ifdef USE_CRNL
2048 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
2049 // CTRL-Z by its own, or after a NL.
2050 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
2051 && sp->fileformat == EOL_DOS
2052 && buf[len - 1] == Ctrl_Z)
2053 {
2054 buf[len - 1] = NUL;
2055 break;
2056 }
2057#endif
2058
2059 have_read = TRUE;
2060 ga.ga_len = len;
2061
2062 // If the line was longer than the buffer, read more.
2063 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
2064 continue;
2065
2066 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
2067 {
2068#ifdef USE_CRNL
2069 has_cr = (len >= 2 && buf[len - 2] == '\r');
2070 if (sp->fileformat == EOL_UNKNOWN)
2071 {
2072 if (has_cr)
2073 sp->fileformat = EOL_DOS;
2074 else
2075 sp->fileformat = EOL_UNIX;
2076 }
2077
2078 if (sp->fileformat == EOL_DOS)
2079 {
2080 if (has_cr) // replace trailing CR
2081 {
2082 buf[len - 2] = '\n';
2083 --len;
2084 --ga.ga_len;
2085 }
2086 else // lines like ":map xx yy^M" will have failed
2087 {
2088 if (!sp->error)
2089 {
2090 msg_source(HL_ATTR(HLF_W));
2091 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
2092 }
2093 sp->error = TRUE;
2094 sp->fileformat = EOL_UNIX;
2095 }
2096 }
2097#endif
2098 // The '\n' is escaped if there is an odd number of ^V's just
2099 // before it, first set "c" just before the 'V's and then check
2100 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
2101 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
2102 ;
2103 if ((len & 1) != (c & 1)) // escaped NL, read more
2104 {
2105 ++sp->sourcing_lnum;
2106 continue;
2107 }
2108
2109 buf[len - 1] = NUL; // remove the NL
2110 }
2111
2112 // Check for ^C here now and then, so recursive :so can be broken.
2113 line_breakcheck();
2114 break;
2115 }
2116
2117 if (have_read)
2118 return (char_u *)ga.ga_data;
2119
2120 vim_free(ga.ga_data);
2121 return NULL;
2122}
2123
2124/*
2125 * Get one full line from a sourced file.
2126 * Called by do_cmdline() when it's called from do_source().
2127 *
2128 * Return a pointer to the line in allocated memory.
2129 * Return NULL for end-of-file or some error.
2130 */
2131 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02002132getsourceline(
2133 int c UNUSED,
2134 void *cookie,
2135 int indent UNUSED,
2136 getline_opt_T options)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002137{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002138 source_cookie_T *sp = (source_cookie_T *)cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002139 char_u *line;
2140 char_u *p;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002141 int do_vim9_all = in_vim9script()
2142 && options == GETLINE_CONCAT_ALL;
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002143 int do_bar_cont = do_vim9_all
2144 || options == GETLINE_CONCAT_CONTBAR;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002145
2146#ifdef FEAT_EVAL
2147 // If breakpoints have been added/deleted need to check for it.
2148 if (sp->dbg_tick < debug_tick)
2149 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002150 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002151 sp->dbg_tick = debug_tick;
2152 }
2153# ifdef FEAT_PROFILE
2154 if (do_profiling == PROF_YES)
2155 script_line_end();
2156# endif
2157#endif
2158
2159 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002160 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002161
2162 // Get current line. If there is a read-ahead line, use it, otherwise get
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002163 // one now. "fp" is NULL if actually using a string.
2164 if (sp->finished || sp->fp == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002165 line = NULL;
2166 else if (sp->nextline == NULL)
2167 line = get_one_sourceline(sp);
2168 else
2169 {
2170 line = sp->nextline;
2171 sp->nextline = NULL;
2172 ++sp->sourcing_lnum;
2173 }
2174#ifdef FEAT_PROFILE
2175 if (line != NULL && do_profiling == PROF_YES)
2176 script_line_start();
2177#endif
2178
2179 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
2180 // contain the 'C' flag.
Bram Moolenaar66250c92020-08-20 15:02:42 +02002181 if (line != NULL && options != GETLINE_NONE
2182 && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002183 {
Bram Moolenaar5072b472021-06-03 21:56:10 +02002184 int comment_char = in_vim9script() ? '#' : '"';
2185
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002186 // compensate for the one line read-ahead
2187 --sp->sourcing_lnum;
2188
2189 // Get the next line and concatenate it when it starts with a
2190 // backslash. We always need to read the next line, keep it in
2191 // sp->nextline.
2192 /* Also check for a comment in between continuation lines: "\ */
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002193 // Also check for a Vim9 comment, empty line, line starting with '|',
2194 // but not "||".
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002195 sp->nextline = get_one_sourceline(sp);
2196 if (sp->nextline != NULL
2197 && (*(p = skipwhite(sp->nextline)) == '\\'
Bram Moolenaar5072b472021-06-03 21:56:10 +02002198 || (p[0] == comment_char
2199 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002200 || (do_vim9_all && (*p == NUL
2201 || vim9_comment_start(p)))
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002202 || (do_bar_cont && p[0] == '|' && p[1] != '|')))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002203 {
2204 garray_T ga;
2205
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002206 ga_init2(&ga, sizeof(char_u), 400);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002207 ga_concat(&ga, line);
2208 if (*p == '\\')
2209 ga_concat(&ga, p + 1);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002210 else if (*p == '|')
2211 {
2212 ga_concat(&ga, (char_u *)" ");
2213 ga_concat(&ga, p);
2214 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002215 for (;;)
2216 {
2217 vim_free(sp->nextline);
2218 sp->nextline = get_one_sourceline(sp);
2219 if (sp->nextline == NULL)
2220 break;
2221 p = skipwhite(sp->nextline);
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002222 if (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|'))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002223 {
2224 // Adjust the growsize to the current length to speed up
2225 // concatenating many lines.
2226 if (ga.ga_len > 400)
2227 {
2228 if (ga.ga_len > 8000)
2229 ga.ga_growsize = 8000;
2230 else
2231 ga.ga_growsize = ga.ga_len;
2232 }
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002233 if (*p == '\\')
2234 ga_concat(&ga, p + 1);
2235 else
2236 {
2237 ga_concat(&ga, (char_u *)" ");
2238 ga_concat(&ga, p);
2239 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002240 }
Bram Moolenaar5072b472021-06-03 21:56:10 +02002241 else if (!(p[0] == (comment_char)
Bram Moolenaar0f37e352021-06-02 15:28:15 +02002242 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002243 && !(do_vim9_all && (*p == NUL || vim9_comment_start(p))))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002244 break;
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002245 /* drop a # comment or "\ comment line */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002246 }
2247 ga_append(&ga, NUL);
2248 vim_free(line);
2249 line = ga.ga_data;
2250 }
2251 }
2252
2253 if (line != NULL && sp->conv.vc_type != CONV_NONE)
2254 {
2255 char_u *s;
2256
2257 // Convert the encoding of the script line.
2258 s = string_convert(&sp->conv, line, NULL);
2259 if (s != NULL)
2260 {
2261 vim_free(line);
2262 line = s;
2263 }
2264 }
2265
2266#ifdef FEAT_EVAL
2267 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002268 if (sp->breakpoint != 0 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002269 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002270 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002271 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002272 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002273 sp->dbg_tick = debug_tick;
2274 }
2275#endif
2276
2277 return line;
2278}
2279
2280/*
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002281 * Returns TRUE if sourcing a script either from a file or a buffer.
2282 * Otherwise returns FALSE.
2283 */
2284 int
2285sourcing_a_script(exarg_T *eap)
2286{
2287 return (getline_equal(eap->getline, eap->cookie, getsourceline)
2288 || getline_equal(eap->getline, eap->cookie, source_getbufline));
2289}
2290
2291/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002292 * ":scriptencoding": Set encoding conversion for a sourced script.
2293 */
2294 void
2295ex_scriptencoding(exarg_T *eap)
2296{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002297 source_cookie_T *sp;
2298 char_u *name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002299
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002300 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002301 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00002302 emsg(_(e_scriptencoding_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002303 return;
2304 }
2305
2306 if (*eap->arg != NUL)
2307 {
2308 name = enc_canonize(eap->arg);
2309 if (name == NULL) // out of memory
2310 return;
2311 }
2312 else
2313 name = eap->arg;
2314
2315 // Setup for conversion from the specified encoding to 'encoding'.
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002316 sp = (source_cookie_T *)getline_cookie(eap->getline, eap->cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002317 convert_setup(&sp->conv, name, p_enc);
2318
2319 if (name != eap->arg)
2320 vim_free(name);
2321}
2322
2323/*
2324 * ":scriptversion": Set Vim script version for a sourced script.
2325 */
2326 void
2327ex_scriptversion(exarg_T *eap UNUSED)
2328{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002329 int nr;
2330
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002331 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002332 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002333 emsg(_(e_scriptversion_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002334 return;
2335 }
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002336 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002338 emsg(_(e_cannot_use_scriptversion_after_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339 return;
2340 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002341
2342 nr = getdigits(&eap->arg);
2343 if (nr == 0 || *eap->arg != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002344 emsg(_(e_invalid_argument));
Bram Moolenaar67979662020-06-20 22:50:47 +02002345 else if (nr > SCRIPT_VERSION_MAX)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002346 semsg(_(e_scriptversion_not_supported_nr), nr);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002347 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002348 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002349 current_sctx.sc_version = nr;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002350#ifdef FEAT_EVAL
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002351 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002352#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002353 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002354}
2355
2356#if defined(FEAT_EVAL) || defined(PROTO)
2357/*
2358 * ":finish": Mark a sourced file as finished.
2359 */
2360 void
2361ex_finish(exarg_T *eap)
2362{
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002363 if (sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002364 do_finish(eap, FALSE);
2365 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00002366 emsg(_(e_finish_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002367}
2368
2369/*
2370 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
2371 * Also called for a pending finish at the ":endtry" or after returning from
2372 * an extra do_cmdline(). "reanimate" is used in the latter case.
2373 */
2374 void
2375do_finish(exarg_T *eap, int reanimate)
2376{
2377 int idx;
2378
2379 if (reanimate)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002380 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002381 eap->cookie))->finished = FALSE;
2382
2383 // Cleanup (and inactivate) conditionals, but stop when a try conditional
2384 // not in its finally clause (which then is to be executed next) is found.
2385 // In this case, make the ":finish" pending for execution at the ":endtry".
2386 // Otherwise, finish normally.
2387 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
2388 if (idx >= 0)
2389 {
2390 eap->cstack->cs_pending[idx] = CSTP_FINISH;
2391 report_make_pending(CSTP_FINISH, NULL);
2392 }
2393 else
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002394 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002395 eap->cookie))->finished = TRUE;
2396}
2397
2398
2399/*
2400 * Return TRUE when a sourced file had the ":finish" command: Don't give error
2401 * message for missing ":endif".
2402 * Return FALSE when not sourcing a file.
2403 */
2404 int
2405source_finished(
Bram Moolenaar66250c92020-08-20 15:02:42 +02002406 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002407 void *cookie)
2408{
2409 return (getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002410 && ((source_cookie_T *)getline_cookie(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002411 fgetline, cookie))->finished);
2412}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002413
2414/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002415 * Find the path of a script below the "autoload" directory.
2416 * Returns NULL if there is no "/autoload/" in the script name.
2417 */
2418 char_u *
2419script_name_after_autoload(scriptitem_T *si)
2420{
2421 char_u *p = si->sn_name;
2422 char_u *res = NULL;
2423
2424 for (;;)
2425 {
2426 char_u *n = (char_u *)strstr((char *)p, "autoload");
2427
2428 if (n == NULL)
2429 break;
2430 if (n > p && vim_ispathsep(n[-1]) && vim_ispathsep(n[8]))
2431 res = n + 9;
2432 p = n + 8;
2433 }
2434 return res;
2435}
2436
2437/*
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002438 * For an autoload script "autoload/dir/script.vim" return the prefix
2439 * "dir#script#" in allocated memory.
2440 * Returns NULL if anything is wrong.
2441 */
2442 char_u *
2443get_autoload_prefix(scriptitem_T *si)
2444{
2445 char_u *p = script_name_after_autoload(si);
2446 char_u *prefix;
2447
2448 if (p == NULL)
2449 return NULL;
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002450 prefix = vim_strsave(p);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002451 if (prefix == NULL)
2452 return NULL;
2453
2454 // replace all '/' with '#' and locate ".vim" at the end
2455 for (p = prefix; *p != NUL; p += mb_ptr2len(p))
2456 {
2457 if (vim_ispathsep(*p))
2458 *p = '#';
2459 else if (STRCMP(p, ".vim") == 0)
2460 {
2461 p[0] = '#';
2462 p[1] = NUL;
2463 return prefix;
2464 }
2465 }
2466
2467 // did not find ".vim" at the end
2468 vim_free(prefix);
2469 return NULL;
2470}
2471
2472/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002473 * If in a Vim9 autoload script return "name" with the autoload prefix for the
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002474 * script. If successful the returned name is allocated.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002475 * Otherwise it returns "name" unmodified.
2476 */
2477 char_u *
2478may_prefix_autoload(char_u *name)
2479{
2480 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
2481 {
2482 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2483
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002484 if (si->sn_autoload_prefix != NULL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002485 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002486 char_u *basename = name;
2487 size_t len;
2488 char_u *res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002489
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002490 if (*name == K_SPECIAL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002491 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002492 char_u *p = vim_strchr(name, '_');
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002493
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002494 // skip over "<SNR>99_"
2495 if (p != NULL)
2496 basename = p + 1;
2497 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002498
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002499 len = STRLEN(si->sn_autoload_prefix) + STRLEN(basename) + 2;
2500 res = alloc(len);
2501 if (res != NULL)
2502 {
2503 vim_snprintf((char *)res, len, "%s%s",
2504 si->sn_autoload_prefix, basename);
2505 return res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002506 }
2507 }
2508 }
2509 return name;
2510}
2511
2512/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002513 * Return the autoload script name for a function or variable name.
2514 * Returns NULL when out of memory.
2515 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
2516 */
2517 char_u *
2518autoload_name(char_u *name)
2519{
2520 char_u *p, *q = NULL;
2521 char_u *scriptname;
2522
2523 // Get the script file name: replace '#' with '/', append ".vim".
2524 scriptname = alloc(STRLEN(name) + 14);
2525 if (scriptname == NULL)
2526 return NULL;
2527 STRCPY(scriptname, "autoload/");
Bram Moolenaara1773442020-08-12 15:21:22 +02002528 STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002529 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
2530 q = p, ++p)
2531 *p = '/';
2532 STRCPY(q, ".vim");
2533 return scriptname;
2534}
2535
2536/*
2537 * If "name" has a package name try autoloading the script for it.
2538 * Return TRUE if a package was loaded.
2539 */
2540 int
2541script_autoload(
2542 char_u *name,
2543 int reload) // load script again when already loaded
2544{
2545 char_u *p;
2546 char_u *scriptname, *tofree;
2547 int ret = FALSE;
2548 int i;
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002549 int ret_sid;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002550
2551 // If there is no '#' after name[0] there is no package name.
2552 p = vim_strchr(name, AUTOLOAD_CHAR);
2553 if (p == NULL || p == name)
2554 return FALSE;
2555
2556 tofree = scriptname = autoload_name(name);
2557 if (scriptname == NULL)
2558 return FALSE;
2559
2560 // Find the name in the list of previously loaded package names. Skip
2561 // "autoload/", it's always the same.
2562 for (i = 0; i < ga_loaded.ga_len; ++i)
2563 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2564 break;
2565 if (!reload && i < ga_loaded.ga_len)
2566 ret = FALSE; // was loaded already
2567 else
2568 {
2569 // Remember the name if it wasn't loaded already.
2570 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2571 {
2572 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2573 tofree = NULL;
2574 }
2575
2576 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002577 // Use "ret_sid" to avoid loading the same script again.
=?UTF-8?q?Bj=C3=B6rn=20Linse?=223a9502022-01-31 17:26:05 +00002578 if (source_in_path(p_rtp, scriptname, DIP_START, &ret_sid) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002579 ret = TRUE;
2580 }
2581
2582 vim_free(tofree);
2583 return ret;
2584}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002585#endif