blob: 2a1f84a50e6c0a27823027cb3a770f6c283b5a70 [file] [log] [blame]
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * scriptfile.c: functions for dealing with the runtime directories/files
12 */
13
14#include "vim.h"
15
Bram Moolenaarda6c0332019-09-01 16:01:30 +020016#if defined(FEAT_EVAL) || defined(PROTO)
17// The names of packages that once were loaded are remembered.
18static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
19#endif
20
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +000021// last used sequence number for sourcing scripts (current_sctx.sc_seq)
22#ifdef FEAT_EVAL
23static int last_current_SID_seq = 0;
24#endif
25
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +000026static int do_source_ext(char_u *fname, int check_other, int is_vimrc, int *ret_sid, exarg_T *eap, int clearvars);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +000027
Bram Moolenaar307c5a52019-08-25 15:41:00 +020028/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010029 * Initialize the execution stack.
30 */
31 void
32estack_init(void)
33{
34 estack_T *entry;
35
36 if (ga_grow(&exestack, 10) == FAIL)
37 mch_exit(0);
38 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
39 entry->es_type = ETYPE_TOP;
40 entry->es_name = NULL;
41 entry->es_lnum = 0;
Bram Moolenaar09d46402019-12-29 23:53:01 +010042#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010043 entry->es_info.ufunc = NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +010044#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010045 ++exestack.ga_len;
46}
47
48/*
49 * Add an item to the execution stack.
50 * Returns the new entry or NULL when out of memory.
51 */
52 estack_T *
53estack_push(etype_T type, char_u *name, long lnum)
54{
55 estack_T *entry;
56
57 // If memory allocation fails then we'll pop more than we push, eventually
58 // at the top level it will be OK again.
59 if (ga_grow(&exestack, 1) == OK)
60 {
61 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
62 entry->es_type = type;
63 entry->es_name = name;
64 entry->es_lnum = lnum;
Bram Moolenaar09d46402019-12-29 23:53:01 +010065#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010066 entry->es_info.ufunc = NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +010067#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010068 ++exestack.ga_len;
69 return entry;
70 }
71 return NULL;
72}
73
Bram Moolenaar09d46402019-12-29 23:53:01 +010074#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010075/*
76 * Add a user function to the execution stack.
77 */
Bram Moolenaarc620c052020-07-08 15:16:19 +020078 estack_T *
Bram Moolenaar25e0f582020-05-25 22:36:50 +020079estack_push_ufunc(ufunc_T *ufunc, long lnum)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010080{
Bram Moolenaar25e0f582020-05-25 22:36:50 +020081 estack_T *entry = estack_push(ETYPE_UFUNC,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010082 ufunc->uf_name_exp != NULL
83 ? ufunc->uf_name_exp : ufunc->uf_name, lnum);
84 if (entry != NULL)
85 entry->es_info.ufunc = ufunc;
Bram Moolenaarc620c052020-07-08 15:16:19 +020086 return entry;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010087}
Bram Moolenaar25e0f582020-05-25 22:36:50 +020088
89/*
90 * Return TRUE if "ufunc" with "lnum" is already at the top of the exe stack.
91 */
92 int
93estack_top_is_ufunc(ufunc_T *ufunc, long lnum)
94{
95 estack_T *entry;
96
97 if (exestack.ga_len == 0)
98 return FALSE;
99 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
100 return entry->es_type == ETYPE_UFUNC
101 && STRCMP( entry->es_name, ufunc->uf_name_exp != NULL
102 ? ufunc->uf_name_exp : ufunc->uf_name) == 0
103 && entry->es_lnum == lnum;
104}
Bram Moolenaar09d46402019-12-29 23:53:01 +0100105#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100106
107/*
Bram Moolenaarc620c052020-07-08 15:16:19 +0200108 * Take an item off of the execution stack and return it.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100109 */
Bram Moolenaarc620c052020-07-08 15:16:19 +0200110 estack_T *
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100111estack_pop(void)
112{
Bram Moolenaarc620c052020-07-08 15:16:19 +0200113 if (exestack.ga_len == 0)
114 return NULL;
115 --exestack.ga_len;
116 return ((estack_T *)exestack.ga_data) + exestack.ga_len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100117}
118
119/*
Bram Moolenaar89445512022-04-14 12:58:23 +0100120 * Get the current value for "which" in allocated memory.
LemonBoy6013d002022-04-09 21:42:10 +0100121 * "which" is ESTACK_SFILE for <sfile>, ESTACK_STACK for <stack> or
122 * ESTACK_SCRIPT for <script>.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100123 */
124 char_u *
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200125estack_sfile(estack_arg_T which UNUSED)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100126{
Bram Moolenaar85b09572019-12-30 10:57:00 +0100127 estack_T *entry;
128#ifdef FEAT_EVAL
Bram Moolenaara5d04232020-07-26 15:37:02 +0200129 garray_T ga;
Bram Moolenaar4d7a2482020-01-06 19:53:43 +0100130 size_t len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100131 int idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200132 etype_T last_type = ETYPE_SCRIPT;
133 char *type_name;
Bram Moolenaar85b09572019-12-30 10:57:00 +0100134#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100135
136 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100137#ifdef FEAT_EVAL
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200138 if (which == ESTACK_SFILE && entry->es_type != ETYPE_UFUNC)
Bram Moolenaar09d46402019-12-29 23:53:01 +0100139#endif
Bram Moolenaara5d04232020-07-26 15:37:02 +0200140 {
141 if (entry->es_name == NULL)
142 return NULL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100143 return vim_strsave(entry->es_name);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200144 }
Bram Moolenaar09d46402019-12-29 23:53:01 +0100145#ifdef FEAT_EVAL
Bram Moolenaarc4492712021-11-22 15:37:15 +0000146 // expand('<sfile>') works in a function for backwards compatibility, but
147 // may give an unexpected result. Disallow it in Vim 9 script.
148 if (which == ESTACK_SFILE && in_vim9script())
149 {
150 int save_emsg_off = emsg_off;
151
152 if (emsg_off == 1)
153 // f_expand() silences errors but we do want this one
154 emsg_off = 0;
155 emsg(_(e_cannot_expand_sfile_in_vim9_function));
156 emsg_off = save_emsg_off;
157 return NULL;
158 }
159
LemonBoy6013d002022-04-09 21:42:10 +0100160 // If evaluated in a function return the path of the script where the
161 // function is defined, at script level the current script path is returned
162 // instead.
163 if (which == ESTACK_SCRIPT)
164 {
165 if (entry->es_type == ETYPE_UFUNC)
166 {
167 sctx_T *def_ctx = &entry->es_info.ufunc->uf_script_ctx;
168
169 if (def_ctx->sc_sid > 0)
170 return vim_strsave(SCRIPT_ITEM(def_ctx->sc_sid)->sn_name);
171 }
172 else if (exestack.ga_len > 0)
173 {
174 // Walk the stack backwards, starting from the current frame.
175 for (idx = exestack.ga_len - 1; idx; --idx)
176 {
177 entry = ((estack_T *)exestack.ga_data) + idx;
178
179 if (entry->es_type == ETYPE_SCRIPT)
180 return vim_strsave(entry->es_name);
181 }
182 }
183 return NULL;
184 }
185
Bram Moolenaara5d04232020-07-26 15:37:02 +0200186 // Give information about each stack entry up to the root.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100187 // For a function we compose the call stack, as it was done in the past:
188 // "function One[123]..Two[456]..Three"
Bram Moolenaara5d04232020-07-26 15:37:02 +0200189 ga_init2(&ga, sizeof(char), 100);
190 for (idx = 0; idx < exestack.ga_len; ++idx)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100191 {
192 entry = ((estack_T *)exestack.ga_data) + idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200193 if (entry->es_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100194 {
Bram Moolenaara810db32020-09-11 17:59:23 +0200195 long lnum = 0;
196 char *dots;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200197
Bram Moolenaara5d04232020-07-26 15:37:02 +0200198 len = STRLEN(entry->es_name) + 15;
199 type_name = "";
200 if (entry->es_type != last_type)
201 {
202 switch (entry->es_type)
203 {
204 case ETYPE_SCRIPT: type_name = "script "; break;
205 case ETYPE_UFUNC: type_name = "function "; break;
206 default: type_name = ""; break;
207 }
208 last_type = entry->es_type;
209 }
210 len += STRLEN(type_name);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200211 if (ga_grow(&ga, (int)len) == FAIL)
Bram Moolenaara5d04232020-07-26 15:37:02 +0200212 break;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200213 if (idx == exestack.ga_len - 1)
214 lnum = which == ESTACK_STACK ? SOURCING_LNUM : 0;
215 else
216 lnum = entry->es_lnum;
Bram Moolenaara810db32020-09-11 17:59:23 +0200217 dots = idx == exestack.ga_len - 1 ? "" : "..";
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200218 if (lnum == 0)
219 // For the bottom entry of <sfile>: do not add the line number,
220 // it is used in <slnum>. Also leave it out when the number is
221 // not set.
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200222 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s%s",
Bram Moolenaara810db32020-09-11 17:59:23 +0200223 type_name, entry->es_name, dots);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200224 else
Bram Moolenaara810db32020-09-11 17:59:23 +0200225 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s[%ld]%s",
226 type_name, entry->es_name, lnum, dots);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200227 ga.ga_len += (int)STRLEN((char *)ga.ga_data + ga.ga_len);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100228 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100229 }
230
Bram Moolenaara5d04232020-07-26 15:37:02 +0200231 return (char_u *)ga.ga_data;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100232#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100233}
234
235/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200236 * ":runtime [what] {name}"
237 */
238 void
239ex_runtime(exarg_T *eap)
240{
241 char_u *arg = eap->arg;
242 char_u *p = skiptowhite(arg);
243 int len = (int)(p - arg);
244 int flags = eap->forceit ? DIP_ALL : 0;
245
246 if (STRNCMP(arg, "START", len) == 0)
247 {
248 flags += DIP_START + DIP_NORTP;
249 arg = skipwhite(arg + len);
250 }
251 else if (STRNCMP(arg, "OPT", len) == 0)
252 {
253 flags += DIP_OPT + DIP_NORTP;
254 arg = skipwhite(arg + len);
255 }
256 else if (STRNCMP(arg, "PACK", len) == 0)
257 {
258 flags += DIP_START + DIP_OPT + DIP_NORTP;
259 arg = skipwhite(arg + len);
260 }
261 else if (STRNCMP(arg, "ALL", len) == 0)
262 {
263 flags += DIP_START + DIP_OPT;
264 arg = skipwhite(arg + len);
265 }
266
267 source_runtime(arg, flags);
268}
269
270 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271source_callback(char_u *fname, void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200272{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100273 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200274}
275
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000276#ifdef FEAT_EVAL
277/*
278 * Find an already loaded script "name".
279 * If found returns its script ID. If not found returns -1.
280 */
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100281 int
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000282find_script_by_name(char_u *name)
283{
284 int sid;
285 scriptitem_T *si;
286
287 for (sid = script_items.ga_len; sid > 0; --sid)
288 {
289 // We used to check inode here, but that doesn't work:
290 // - If a script is edited and written, it may get a different
291 // inode number, even though to the user it is the same script.
292 // - If a script is deleted and another script is written, with a
293 // different name, the inode may be re-used.
294 si = SCRIPT_ITEM(sid);
295 if (si->sn_name != NULL && fnamecmp(si->sn_name, name) == 0)
296 return sid;
297 }
298 return -1;
299}
300
301/*
302 * Add a new scriptitem with all items initialized.
303 * When running out of memory "error" is set to FAIL.
304 * Returns the script ID.
305 */
306 static int
307get_new_scriptitem(int *error)
308{
309 static scid_T last_current_SID = 0;
310 int sid = ++last_current_SID;
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000311 scriptitem_T *si = NULL;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000312
313 if (ga_grow(&script_items, (int)(sid - script_items.ga_len)) == FAIL)
314 {
315 *error = FAIL;
316 return sid;
317 }
318 while (script_items.ga_len < sid)
319 {
320 si = ALLOC_CLEAR_ONE(scriptitem_T);
321 if (si == NULL)
322 {
323 *error = FAIL;
324 return sid;
325 }
326 ++script_items.ga_len;
327 SCRIPT_ITEM(script_items.ga_len) = si;
328 si->sn_name = NULL;
329 si->sn_version = 1;
330
331 // Allocate the local script variables to use for this script.
332 new_script_vars(script_items.ga_len);
333 ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
334 hash_init(&si->sn_all_vars.dv_hashtab);
335 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
336 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
337# ifdef FEAT_PROFILE
338 si->sn_prof_on = FALSE;
339# endif
340 }
341
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000342 // "si" can't be NULL, check only to avoid a compiler warning
343 if (si != NULL)
344 // Used to check script variable index is still valid.
345 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000346
347 return sid;
348}
349
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100350 int
351get_new_scriptitem_for_fname(int *error, char_u *fname)
352{
353 int sid = get_new_scriptitem(error);
354
355 if (*error == OK)
356 {
357 scriptitem_T *si = SCRIPT_ITEM(sid);
358
359 si->sn_name = vim_strsave(fname);
360 si->sn_state = SN_STATE_NOT_LOADED;
361 }
362 return sid;
363}
364
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000365 static void
366find_script_callback(char_u *fname, void *cookie)
367{
368 int sid;
369 int error = OK;
370 int *ret_sid = cookie;
371
372 sid = find_script_by_name(fname);
373 if (sid < 0)
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000374 // script does not exist yet, create a new scriptitem
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100375 sid = get_new_scriptitem_for_fname(&error, fname);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000376 *ret_sid = sid;
377}
378#endif
379
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200380/*
381 * Find the file "name" in all directories in "path" and invoke
382 * "callback(fname, cookie)".
383 * "name" can contain wildcards.
384 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
385 * When "flags" has DIP_DIR: find directories instead of files.
386 * When "flags" has DIP_ERR: give an error message if there is no match.
387 *
388 * return FAIL when no file could be sourced, OK otherwise.
389 */
390 int
391do_in_path(
392 char_u *path,
393 char_u *name,
394 int flags,
395 void (*callback)(char_u *fname, void *ck),
396 void *cookie)
397{
398 char_u *rtp;
399 char_u *np;
400 char_u *buf;
401 char_u *rtp_copy;
402 char_u *tail;
403 int num_files;
404 char_u **files;
405 int i;
406 int did_one = FALSE;
407#ifdef AMIGA
408 struct Process *proc = (struct Process *)FindTask(0L);
409 APTR save_winptr = proc->pr_WindowPtr;
410
411 // Avoid a requester here for a volume that doesn't exist.
412 proc->pr_WindowPtr = (APTR)-1L;
413#endif
414
415 // Make a copy of 'runtimepath'. Invoking the callback may change the
416 // value.
417 rtp_copy = vim_strsave(path);
418 buf = alloc(MAXPATHL);
419 if (buf != NULL && rtp_copy != NULL)
420 {
Bram Moolenaar647a5302020-05-03 17:01:24 +0200421 if (p_verbose > 10 && name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200422 {
423 verbose_enter();
424 smsg(_("Searching for \"%s\" in \"%s\""),
425 (char *)name, (char *)path);
426 verbose_leave();
427 }
428
429 // Loop over all entries in 'runtimepath'.
430 rtp = rtp_copy;
431 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
432 {
433 size_t buflen;
434
435 // Copy the path from 'runtimepath' to buf[].
436 copy_option_part(&rtp, buf, MAXPATHL, ",");
437 buflen = STRLEN(buf);
438
439 // Skip after or non-after directories.
440 if (flags & (DIP_NOAFTER | DIP_AFTER))
441 {
442 int is_after = buflen >= 5
443 && STRCMP(buf + buflen - 5, "after") == 0;
444
445 if ((is_after && (flags & DIP_NOAFTER))
446 || (!is_after && (flags & DIP_AFTER)))
447 continue;
448 }
449
450 if (name == NULL)
451 {
452 (*callback)(buf, (void *) &cookie);
453 if (!did_one)
454 did_one = (cookie == NULL);
455 }
456 else if (buflen + STRLEN(name) + 2 < MAXPATHL)
457 {
458 add_pathsep(buf);
459 tail = buf + STRLEN(buf);
460
461 // Loop over all patterns in "name"
462 np = name;
463 while (*np != NUL && ((flags & DIP_ALL) || !did_one))
464 {
465 // Append the pattern from "name" to buf[].
466 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
467 "\t ");
468
Bram Moolenaar647a5302020-05-03 17:01:24 +0200469 if (p_verbose > 10)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200470 {
471 verbose_enter();
472 smsg(_("Searching for \"%s\""), buf);
473 verbose_leave();
474 }
475
476 // Expand wildcards, invoke the callback for each match.
477 if (gen_expand_wildcards(1, &buf, &num_files, &files,
478 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
479 {
480 for (i = 0; i < num_files; ++i)
481 {
482 (*callback)(files[i], cookie);
483 did_one = TRUE;
484 if (!(flags & DIP_ALL))
485 break;
486 }
487 FreeWild(num_files, files);
488 }
489 }
490 }
491 }
492 }
493 vim_free(buf);
494 vim_free(rtp_copy);
495 if (!did_one && name != NULL)
496 {
497 char *basepath = path == p_rtp ? "runtimepath" : "packpath";
498
499 if (flags & DIP_ERR)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000500 semsg(_(e_directory_not_found_in_str_str), basepath, name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200501 else if (p_verbose > 0)
502 {
503 verbose_enter();
504 smsg(_("not found in '%s': \"%s\""), basepath, name);
505 verbose_leave();
506 }
507 }
508
509#ifdef AMIGA
510 proc->pr_WindowPtr = save_winptr;
511#endif
512
513 return did_one ? OK : FAIL;
514}
515
516/*
517 * Find "name" in "path". When found, invoke the callback function for
518 * it: callback(fname, "cookie")
519 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first
520 * one is used.
521 * Returns OK when at least one match found, FAIL otherwise.
522 *
523 * If "name" is NULL calls callback for each entry in "path". Cookie is
524 * passed by reference in this case, setting it to NULL indicates that callback
525 * has done its job.
526 */
527 static int
528do_in_path_and_pp(
529 char_u *path,
530 char_u *name,
531 int flags,
532 void (*callback)(char_u *fname, void *ck),
533 void *cookie)
534{
535 int done = FAIL;
536 char_u *s;
537 int len;
538 char *start_dir = "pack/*/start/*/%s";
539 char *opt_dir = "pack/*/opt/*/%s";
540
541 if ((flags & DIP_NORTP) == 0)
542 done = do_in_path(path, name, flags, callback, cookie);
543
544 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
545 {
546 len = (int)(STRLEN(start_dir) + STRLEN(name));
547 s = alloc(len);
548 if (s == NULL)
549 return FAIL;
550 vim_snprintf((char *)s, len, start_dir, name);
551 done = do_in_path(p_pp, s, flags, callback, cookie);
552 vim_free(s);
553 }
554
555 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
556 {
557 len = (int)(STRLEN(opt_dir) + STRLEN(name));
558 s = alloc(len);
559 if (s == NULL)
560 return FAIL;
561 vim_snprintf((char *)s, len, opt_dir, name);
562 done = do_in_path(p_pp, s, flags, callback, cookie);
563 vim_free(s);
564 }
565
566 return done;
567}
568
569/*
570 * Just like do_in_path_and_pp(), using 'runtimepath' for "path".
571 */
572 int
573do_in_runtimepath(
574 char_u *name,
575 int flags,
576 void (*callback)(char_u *fname, void *ck),
577 void *cookie)
578{
579 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
580}
581
582/*
583 * Source the file "name" from all directories in 'runtimepath'.
584 * "name" can contain wildcards.
585 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
586 *
587 * return FAIL when no file could be sourced, OK otherwise.
588 */
589 int
590source_runtime(char_u *name, int flags)
591{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100592 return source_in_path(p_rtp, name, flags, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200593}
594
595/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000596 * Just like source_runtime(), but use "path" instead of 'runtimepath'
597 * and return the script ID in "ret_sid".
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200598 */
599 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200601{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100602 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200603}
604
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200605#if defined(FEAT_EVAL) || defined(PROTO)
606
607/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000608 * Find "name" in 'runtimepath'. If found a new scriptitem is created for it
609 * and it's script ID is returned.
610 * If not found returns -1.
611 */
612 int
613find_script_in_rtp(char_u *name)
614{
615 int sid = -1;
616
617 (void)do_in_path_and_pp(p_rtp, name, DIP_NOAFTER,
618 find_script_callback, &sid);
619 return sid;
620}
621
622/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200623 * Expand wildcards in "pat" and invoke do_source() for each match.
624 */
625 static void
626source_all_matches(char_u *pat)
627{
628 int num_files;
629 char_u **files;
630 int i;
631
632 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK)
633 {
634 for (i = 0; i < num_files; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200636 FreeWild(num_files, files);
637 }
638}
639
640/*
641 * Add the package directory to 'runtimepath'.
642 */
643 static int
644add_pack_dir_to_rtp(char_u *fname)
645{
646 char_u *p4, *p3, *p2, *p1, *p;
647 char_u *entry;
648 char_u *insp = NULL;
649 int c;
650 char_u *new_rtp;
651 int keep;
652 size_t oldlen;
653 size_t addlen;
654 size_t new_rtp_len;
655 char_u *afterdir = NULL;
656 size_t afterlen = 0;
657 char_u *after_insp = NULL;
658 char_u *ffname = NULL;
659 size_t fname_len;
660 char_u *buf = NULL;
661 char_u *rtp_ffname;
662 int match;
663 int retval = FAIL;
664
665 p4 = p3 = p2 = p1 = get_past_head(fname);
666 for (p = p1; *p; MB_PTR_ADV(p))
667 if (vim_ispathsep_nocolon(*p))
668 {
669 p4 = p3; p3 = p2; p2 = p1; p1 = p;
670 }
671
672 // now we have:
673 // rtp/pack/name/start/name
674 // p4 p3 p2 p1
675 //
676 // find the part up to "pack" in 'runtimepath'
677 c = *++p4; // append pathsep in order to expand symlink
678 *p4 = NUL;
679 ffname = fix_fname(fname);
680 *p4 = c;
681 if (ffname == NULL)
682 return FAIL;
683
684 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
685 // Also stop at the first "after" directory.
686 fname_len = STRLEN(ffname);
687 buf = alloc(MAXPATHL);
688 if (buf == NULL)
689 goto theend;
690 for (entry = p_rtp; *entry != NUL; )
691 {
692 char_u *cur_entry = entry;
693
694 copy_option_part(&entry, buf, MAXPATHL, ",");
695 if (insp == NULL)
696 {
697 add_pathsep(buf);
698 rtp_ffname = fix_fname(buf);
699 if (rtp_ffname == NULL)
700 goto theend;
701 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
702 vim_free(rtp_ffname);
703 if (match)
704 // Insert "ffname" after this entry (and comma).
705 insp = entry;
706 }
707
708 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
709 && p > buf
710 && vim_ispathsep(p[-1])
711 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
712 {
713 if (insp == NULL)
714 // Did not find "ffname" before the first "after" directory,
715 // insert it before this entry.
716 insp = cur_entry;
717 after_insp = cur_entry;
718 break;
719 }
720 }
721
722 if (insp == NULL)
723 // Both "fname" and "after" not found, append at the end.
724 insp = p_rtp + STRLEN(p_rtp);
725
726 // check if rtp/pack/name/start/name/after exists
727 afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
728 if (afterdir != NULL && mch_isdir(afterdir))
729 afterlen = STRLEN(afterdir) + 1; // add one for comma
730
731 oldlen = STRLEN(p_rtp);
732 addlen = STRLEN(fname) + 1; // add one for comma
733 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
734 if (new_rtp == NULL)
735 goto theend;
736
737 // We now have 'rtp' parts: {keep}{keep_after}{rest}.
738 // Create new_rtp, first: {keep},{fname}
739 keep = (int)(insp - p_rtp);
740 mch_memmove(new_rtp, p_rtp, keep);
741 new_rtp_len = keep;
742 if (*insp == NUL)
743 new_rtp[new_rtp_len++] = ','; // add comma before
744 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
745 new_rtp_len += addlen - 1;
746 if (*insp != NUL)
747 new_rtp[new_rtp_len++] = ','; // add comma after
748
749 if (afterlen > 0 && after_insp != NULL)
750 {
751 int keep_after = (int)(after_insp - p_rtp);
752
753 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
754 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
755 keep_after - keep);
756 new_rtp_len += keep_after - keep;
757 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
758 new_rtp_len += afterlen - 1;
759 new_rtp[new_rtp_len++] = ',';
760 keep = keep_after;
761 }
762
763 if (p_rtp[keep] != NUL)
764 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
765 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
766 else
767 new_rtp[new_rtp_len] = NUL;
768
769 if (afterlen > 0 && after_insp == NULL)
770 {
771 // Append afterdir when "after" was not found:
772 // {keep},{fname}{rest},{afterdir}
773 STRCAT(new_rtp, ",");
774 STRCAT(new_rtp, afterdir);
775 }
776
777 set_option_value((char_u *)"rtp", 0L, new_rtp, 0);
778 vim_free(new_rtp);
779 retval = OK;
780
781theend:
782 vim_free(buf);
783 vim_free(ffname);
784 vim_free(afterdir);
785 return retval;
786}
787
788/*
789 * Load scripts in "plugin" and "ftdetect" directories of the package.
790 */
791 static int
792load_pack_plugin(char_u *fname)
793{
794 static char *plugpat = "%s/plugin/**/*.vim";
795 static char *ftpat = "%s/ftdetect/*.vim";
796 int len;
797 char_u *ffname = fix_fname(fname);
798 char_u *pat = NULL;
799 int retval = FAIL;
800
801 if (ffname == NULL)
802 return FAIL;
803 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
804 pat = alloc(len);
805 if (pat == NULL)
806 goto theend;
807 vim_snprintf((char *)pat, len, plugpat, ffname);
808 source_all_matches(pat);
809
810 {
811 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
812
813 // If runtime/filetype.vim wasn't loaded yet, the scripts will be
814 // found when it loads.
815 if (cmd != NULL && eval_to_number(cmd) > 0)
816 {
817 do_cmdline_cmd((char_u *)"augroup filetypedetect");
818 vim_snprintf((char *)pat, len, ftpat, ffname);
819 source_all_matches(pat);
820 do_cmdline_cmd((char_u *)"augroup END");
821 }
822 vim_free(cmd);
823 }
824 vim_free(pat);
825 retval = OK;
826
827theend:
828 vim_free(ffname);
829 return retval;
830}
831
832// used for "cookie" of add_pack_plugin()
833static int APP_ADD_DIR;
834static int APP_LOAD;
835static int APP_BOTH;
836
837 static void
838add_pack_plugin(char_u *fname, void *cookie)
839{
840 if (cookie != &APP_LOAD)
841 {
842 char_u *buf = alloc(MAXPATHL);
843 char_u *p;
844 int found = FALSE;
845
846 if (buf == NULL)
847 return;
848 p = p_rtp;
849 while (*p != NUL)
850 {
851 copy_option_part(&p, buf, MAXPATHL, ",");
852 if (pathcmp((char *)buf, (char *)fname, -1) == 0)
853 {
854 found = TRUE;
855 break;
856 }
857 }
858 vim_free(buf);
859 if (!found)
860 // directory is not yet in 'runtimepath', add it
861 if (add_pack_dir_to_rtp(fname) == FAIL)
862 return;
863 }
864
865 if (cookie != &APP_ADD_DIR)
866 load_pack_plugin(fname);
867}
868
869/*
870 * Add all packages in the "start" directory to 'runtimepath'.
871 */
872 void
873add_pack_start_dirs(void)
874{
875 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
876 add_pack_plugin, &APP_ADD_DIR);
877}
878
879/*
880 * Load plugins from all packages in the "start" directory.
881 */
882 void
883load_start_packages(void)
884{
885 did_source_packages = TRUE;
886 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
887 add_pack_plugin, &APP_LOAD);
888}
889
890/*
891 * ":packloadall"
892 * Find plugins in the package directories and source them.
893 */
894 void
895ex_packloadall(exarg_T *eap)
896{
897 if (!did_source_packages || eap->forceit)
898 {
899 // First do a round to add all directories to 'runtimepath', then load
900 // the plugins. This allows for plugins to use an autoload directory
901 // of another plugin.
902 add_pack_start_dirs();
903 load_start_packages();
904 }
905}
906
907/*
908 * ":packadd[!] {name}"
909 */
910 void
911ex_packadd(exarg_T *eap)
912{
913 static char *plugpat = "pack/*/%s/%s";
914 int len;
915 char *pat;
916 int round;
917 int res = OK;
918
919 // Round 1: use "start", round 2: use "opt".
920 for (round = 1; round <= 2; ++round)
921 {
922 // Only look under "start" when loading packages wasn't done yet.
923 if (round == 1 && did_source_packages)
924 continue;
925
926 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
927 pat = alloc(len);
928 if (pat == NULL)
929 return;
930 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
931 // The first round don't give a "not found" error, in the second round
932 // only when nothing was found in the first round.
933 res = do_in_path(p_pp, (char_u *)pat,
934 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
935 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
936 vim_free(pat);
937 }
938}
939#endif
940
941/*
Bram Moolenaar26262f82019-09-04 20:59:15 +0200942 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
943 * list of file names in allocated memory.
944 */
945 void
946remove_duplicates(garray_T *gap)
947{
948 int i;
949 int j;
950 char_u **fnames = (char_u **)gap->ga_data;
951
952 sort_strings(fnames, gap->ga_len);
953 for (i = gap->ga_len - 1; i > 0; --i)
954 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
955 {
956 vim_free(fnames[i]);
957 for (j = i + 1; j < gap->ga_len; ++j)
958 fnames[j - 1] = fnames[j];
959 --gap->ga_len;
960 }
961}
962
963/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200964 * Expand color scheme, compiler or filetype names.
965 * Search from 'runtimepath':
966 * 'runtimepath'/{dirnames}/{pat}.vim
967 * When "flags" has DIP_START: search also from 'start' of 'packpath':
968 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
969 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
970 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
971 * "dirnames" is an array with one or more directory names.
972 */
973 int
974ExpandRTDir(
975 char_u *pat,
976 int flags,
977 int *num_file,
978 char_u ***file,
979 char *dirnames[])
980{
981 char_u *s;
982 char_u *e;
983 char_u *match;
984 garray_T ga;
985 int i;
986 int pat_len;
987
988 *num_file = 0;
989 *file = NULL;
990 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000991 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200992
993 for (i = 0; dirnames[i] != NULL; ++i)
994 {
995 s = alloc(STRLEN(dirnames[i]) + pat_len + 7);
996 if (s == NULL)
997 {
998 ga_clear_strings(&ga);
999 return FAIL;
1000 }
1001 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
1002 globpath(p_rtp, s, &ga, 0);
1003 vim_free(s);
1004 }
1005
1006 if (flags & DIP_START) {
1007 for (i = 0; dirnames[i] != NULL; ++i)
1008 {
1009 s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
1010 if (s == NULL)
1011 {
1012 ga_clear_strings(&ga);
1013 return FAIL;
1014 }
1015 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
1016 globpath(p_pp, s, &ga, 0);
1017 vim_free(s);
1018 }
1019 }
1020
1021 if (flags & DIP_OPT) {
1022 for (i = 0; dirnames[i] != NULL; ++i)
1023 {
1024 s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
1025 if (s == NULL)
1026 {
1027 ga_clear_strings(&ga);
1028 return FAIL;
1029 }
1030 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
1031 globpath(p_pp, s, &ga, 0);
1032 vim_free(s);
1033 }
1034 }
1035
1036 for (i = 0; i < ga.ga_len; ++i)
1037 {
1038 match = ((char_u **)ga.ga_data)[i];
1039 s = match;
1040 e = s + STRLEN(s);
1041 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
1042 {
1043 e -= 4;
1044 for (s = e; s > match; MB_PTR_BACK(match, s))
1045 if (s < match || vim_ispathsep(*s))
1046 break;
1047 ++s;
1048 *e = NUL;
1049 mch_memmove(match, s, e - s + 1);
1050 }
1051 }
1052
1053 if (ga.ga_len == 0)
1054 return FAIL;
1055
1056 // Sort and remove duplicates which can happen when specifying multiple
1057 // directories in dirnames.
1058 remove_duplicates(&ga);
1059
1060 *file = ga.ga_data;
1061 *num_file = ga.ga_len;
1062 return OK;
1063}
1064
1065/*
1066 * Expand loadplugin names:
1067 * 'packpath'/pack/ * /opt/{pat}
1068 */
1069 int
1070ExpandPackAddDir(
1071 char_u *pat,
1072 int *num_file,
1073 char_u ***file)
1074{
1075 char_u *s;
1076 char_u *e;
1077 char_u *match;
1078 garray_T ga;
1079 int i;
1080 int pat_len;
1081
1082 *num_file = 0;
1083 *file = NULL;
1084 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001085 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001086
1087 s = alloc(pat_len + 26);
1088 if (s == NULL)
1089 {
1090 ga_clear_strings(&ga);
1091 return FAIL;
1092 }
1093 sprintf((char *)s, "pack/*/opt/%s*", pat);
1094 globpath(p_pp, s, &ga, 0);
1095 vim_free(s);
1096
1097 for (i = 0; i < ga.ga_len; ++i)
1098 {
1099 match = ((char_u **)ga.ga_data)[i];
1100 s = gettail(match);
1101 e = s + STRLEN(s);
1102 mch_memmove(match, s, e - s + 1);
1103 }
1104
1105 if (ga.ga_len == 0)
1106 return FAIL;
1107
1108 // Sort and remove duplicates which can happen when specifying multiple
1109 // directories in dirnames.
1110 remove_duplicates(&ga);
1111
1112 *file = ga.ga_data;
1113 *num_file = ga.ga_len;
1114 return OK;
1115}
1116
1117 static void
1118cmd_source(char_u *fname, exarg_T *eap)
1119{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001120 int clearvars = FALSE;
1121
1122 if (*fname != NUL && STRNCMP(fname, "++clear", 7) == 0)
1123 {
1124 // ++clear argument is supplied
1125 clearvars = TRUE;
1126 fname = fname + 7;
1127 if (*fname != NUL)
1128 {
1129 semsg(_(e_invalid_argument_str), eap->arg);
1130 return;
1131 }
1132 }
1133
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001134 if (*fname != NUL && eap != NULL && eap->addr_count > 0)
1135 {
1136 // if a filename is specified to :source, then a range is not allowed
1137 emsg(_(e_no_range_allowed));
1138 return;
1139 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001140
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001141 if (eap != NULL && *fname == NUL)
1142 {
1143 if (eap->forceit)
1144 // a file name is needed to source normal mode commands
1145 emsg(_(e_argument_required));
1146 else
1147 // source ex commands from the current buffer
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001148 do_source_ext(NULL, FALSE, FALSE, NULL, eap, clearvars);
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001149 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001150 else if (eap != NULL && eap->forceit)
1151 // ":source!": read Normal mode commands
1152 // Need to execute the commands directly. This is required at least
1153 // for:
1154 // - ":g" command busy
1155 // - after ":argdo", ":windo" or ":bufdo"
1156 // - another command follows
1157 // - inside a loop
1158 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
1159#ifdef FEAT_EVAL
1160 || eap->cstack->cs_idx >= 0
1161#endif
1162 );
1163
1164 // ":source" read ex commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001166 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001167}
1168
1169/*
1170 * ":source {fname}"
1171 */
1172 void
1173ex_source(exarg_T *eap)
1174{
1175#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02001176 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001177 {
1178 char_u *fname = NULL;
1179
1180 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
1181 NULL, NULL,
1182 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1183 if (fname != NULL)
1184 {
1185 cmd_source(fname, eap);
1186 vim_free(fname);
1187 }
1188 }
1189 else
1190#endif
1191 cmd_source(eap->arg, eap);
1192}
1193
1194#if defined(FEAT_EVAL) || defined(PROTO)
1195/*
1196 * ":options"
1197 */
1198 void
1199ex_options(
1200 exarg_T *eap UNUSED)
1201{
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001202 char_u buf[500];
1203 int multi_mods = 0;
1204
1205 buf[0] = NUL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001206 (void)add_win_cmd_modifers(buf, &cmdmod, &multi_mods);
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001207
1208 vim_setenv((char_u *)"OPTWIN_CMD", buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001209 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
1210}
1211#endif
1212
1213/*
1214 * ":source" and associated commands.
1215 */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001216
1217#ifdef FEAT_EVAL
1218/*
1219 * Return the address holding the next breakpoint line for a source cookie.
1220 */
1221 linenr_T *
1222source_breakpoint(void *cookie)
1223{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001224 return &((source_cookie_T *)cookie)->breakpoint;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001225}
1226
1227/*
1228 * Return the address holding the debug tick for a source cookie.
1229 */
1230 int *
1231source_dbg_tick(void *cookie)
1232{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001233 return &((source_cookie_T *)cookie)->dbg_tick;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001234}
1235
1236/*
1237 * Return the nesting level for a source cookie.
1238 */
1239 int
1240source_level(void *cookie)
1241{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001242 return ((source_cookie_T *)cookie)->level;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001243}
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001244
1245/*
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02001246 * Return the readahead line. Note that the pointer may become invalid when
1247 * getting the next line, if it's concatenated with the next one.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001248 */
1249 char_u *
1250source_nextline(void *cookie)
1251{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001252 return ((source_cookie_T *)cookie)->nextline;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001253}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001254#endif
1255
1256#if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC)
1257# define USE_FOPEN_NOINH
1258/*
1259 * Special function to open a file without handle inheritance.
1260 * When possible the handle is closed on exec().
1261 */
1262 static FILE *
1263fopen_noinh_readbin(char *filename)
1264{
1265# ifdef MSWIN
1266 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
1267# else
1268 int fd_tmp = mch_open(filename, O_RDONLY, 0);
1269# endif
1270
1271 if (fd_tmp == -1)
1272 return NULL;
1273
1274# ifdef HAVE_FD_CLOEXEC
1275 {
1276 int fdflags = fcntl(fd_tmp, F_GETFD);
1277 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1278 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
1279 }
1280# endif
1281
1282 return fdopen(fd_tmp, READBIN);
1283}
1284#endif
1285
1286/*
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001287 * Initialization for sourcing lines from the current buffer. Reads all the
1288 * lines from the buffer and stores it in the cookie grow array.
1289 * Returns a pointer to the name ":source buffer=<n>" on success and NULL on
1290 * failure.
1291 */
1292 static char_u *
1293do_source_buffer_init(source_cookie_T *sp, exarg_T *eap)
1294{
1295 linenr_T curr_lnum;
1296 char_u *line = NULL;
1297 char_u *fname;
1298
1299 CLEAR_FIELD(*sp);
1300
1301 if (curbuf == NULL)
1302 return NULL;
1303
1304 // Use ":source buffer=<num>" as the script name
1305 vim_snprintf((char *)IObuff, IOSIZE, ":source buffer=%d", curbuf->b_fnum);
1306 fname = vim_strsave(IObuff);
1307 if (fname == NULL)
1308 return NULL;
1309
1310 ga_init2(&sp->buflines, sizeof(char_u *), 100);
1311
1312 // Copy the lines from the buffer into a grow array
1313 for (curr_lnum = eap->line1; curr_lnum <= eap->line2; curr_lnum++)
1314 {
1315 line = vim_strsave(ml_get(curr_lnum));
1316 if (line == NULL)
1317 goto errret;
1318 if (ga_add_string(&sp->buflines, line) == FAIL)
1319 goto errret;
1320 line = NULL;
1321 }
1322 sp->buf_lnum = 0;
1323 sp->source_from_buf = TRUE;
1324
1325 return fname;
1326
1327errret:
1328 vim_free(fname);
1329 vim_free(line);
1330 ga_clear_strings(&sp->buflines);
1331 return NULL;
1332}
1333
1334/*
1335 * Read the file "fname" and execute its lines as EX commands.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1337 * again.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001338 *
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001339 * The 'eap' argument is used when sourcing lines from a buffer instead of a
1340 * file.
1341 *
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001342 * If 'clearvars' is TRUE, then for scripts which are loaded more than
1343 * once, clear all the functions and variables previously defined in that
1344 * script.
1345 *
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001346 * This function may be called recursively!
1347 *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348 * Return FAIL if file could not be opened, OK otherwise.
1349 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001350 */
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001351 static int
1352do_source_ext(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001353 char_u *fname,
1354 int check_other, // check for .vimrc and _vimrc
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 int is_vimrc, // DOSO_ value
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001356 int *ret_sid UNUSED,
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001357 exarg_T *eap,
1358 int clearvars UNUSED)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001359{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001360 source_cookie_T cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001361 char_u *p;
1362 char_u *fname_exp;
1363 char_u *firstline = NULL;
1364 int retval = FAIL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001365 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001366#ifdef FEAT_EVAL
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001367 funccal_entry_T funccalp_entry;
1368 int save_debug_break_level = debug_break_level;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 int sid;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001370 scriptitem_T *si = NULL;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001371 int save_estack_compiling = estack_compiling;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001372#endif
1373#ifdef STARTUPTIME
1374 struct timeval tv_rel;
1375 struct timeval tv_start;
1376#endif
1377#ifdef FEAT_PROFILE
1378 proftime_T wait_start;
1379#endif
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001380 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001381 int trigger_source_post = FALSE;
Bram Moolenaare31ee862020-01-07 20:59:34 +01001382 ESTACK_CHECK_DECLARATION
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001383
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001384 CLEAR_FIELD(cookie);
1385 if (fname == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001386 {
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001387 // sourcing lines from a buffer
1388 fname_exp = do_source_buffer_init(&cookie, eap);
1389 if (fname_exp == NULL)
1390 return FAIL;
1391 }
1392 else
1393 {
1394 p = expand_env_save(fname);
1395 if (p == NULL)
1396 return retval;
1397 fname_exp = fix_fname(p);
1398 vim_free(p);
1399 if (fname_exp == NULL)
1400 return retval;
1401 if (mch_isdir(fname_exp))
1402 {
1403 smsg(_("Cannot source a directory: \"%s\""), fname);
1404 goto theend;
1405 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001406 }
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001407#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001408 estack_compiling = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001409
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410 // See if we loaded this script before.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001411 sid = find_script_by_name(fname_exp);
Bram Moolenaarf479cac2022-01-12 12:54:55 +00001412 if (sid > 0 && ret_sid != NULL
1413 && SCRIPT_ITEM(sid)->sn_state != SN_STATE_NOT_LOADED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001414 {
1415 // Already loaded and no need to load again, return here.
1416 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001417 retval = OK;
1418 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419 }
1420#endif
1421
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001422 // Apply SourceCmd autocommands, they should get the file and source it.
1423 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1424 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1425 FALSE, curbuf))
1426 {
1427#ifdef FEAT_EVAL
1428 retval = aborting() ? FAIL : OK;
1429#else
1430 retval = OK;
1431#endif
1432 if (retval == OK)
1433 // Apply SourcePost autocommands.
1434 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1435 FALSE, curbuf);
1436 goto theend;
1437 }
1438
1439 // Apply SourcePre autocommands, they may get the file.
1440 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1441
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001442 if (!cookie.source_from_buf)
1443 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001444#ifdef USE_FOPEN_NOINH
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001445 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001446#else
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001447 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001448#endif
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001449 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001450 if (cookie.fp == NULL && check_other)
1451 {
1452 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1453 // and ".exrc" by "_exrc" or vice versa.
1454 p = gettail(fname_exp);
1455 if ((*p == '.' || *p == '_')
1456 && (STRICMP(p + 1, "vimrc") == 0
1457 || STRICMP(p + 1, "gvimrc") == 0
1458 || STRICMP(p + 1, "exrc") == 0))
1459 {
1460 if (*p == '_')
1461 *p = '.';
1462 else
1463 *p = '_';
1464#ifdef USE_FOPEN_NOINH
1465 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1466#else
1467 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1468#endif
1469 }
1470 }
1471
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001472 if (cookie.fp == NULL && !cookie.source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001473 {
1474 if (p_verbose > 0)
1475 {
1476 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001477 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001478 smsg(_("could not source \"%s\""), fname);
1479 else
1480 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001481 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001482 verbose_leave();
1483 }
1484 goto theend;
1485 }
1486
1487 // The file exists.
1488 // - In verbose mode, give a message.
1489 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1490 if (p_verbose > 1)
1491 {
1492 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001493 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001494 smsg(_("sourcing \"%s\""), fname);
1495 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001496 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001497 verbose_leave();
1498 }
1499 if (is_vimrc == DOSO_VIMRC)
1500 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1501 else if (is_vimrc == DOSO_GVIMRC)
1502 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1503
1504#ifdef USE_CRNL
1505 // If no automatic file format: Set default to CR-NL.
1506 if (*p_ffs == NUL)
1507 cookie.fileformat = EOL_DOS;
1508 else
1509 cookie.fileformat = EOL_UNKNOWN;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001510#endif
1511
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001512 if (fname == NULL)
1513 // When sourcing a range of lines from a buffer, use the buffer line
1514 // number.
1515 cookie.sourcing_lnum = eap->line1 - 1;
1516 else
1517 cookie.sourcing_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001518
1519#ifdef FEAT_EVAL
1520 // Check if this script has a breakpoint.
1521 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1522 cookie.fname = fname_exp;
1523 cookie.dbg_tick = debug_tick;
1524
1525 cookie.level = ex_nesting_level;
1526#endif
1527
1528 // Keep the sourcing name/lnum, for recursive calls.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001529 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001530 ESTACK_CHECK_SETUP
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001531
1532#ifdef STARTUPTIME
1533 if (time_fd != NULL)
1534 time_push(&tv_rel, &tv_start);
1535#endif
1536
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001537 // "legacy" does not apply to commands in the script
1538 sticky_cmdmod_flags = 0;
1539
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001540 save_current_sctx = current_sctx;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001541 if (cmdmod.cmod_flags & CMOD_VIM9CMD)
1542 // When the ":vim9cmd" command modifier is used, source the script as a
1543 // Vim9 script.
1544 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1545 else
1546 current_sctx.sc_version = 1; // default script version
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001547
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001548#ifdef FEAT_EVAL
1549# ifdef FEAT_PROFILE
1550 if (do_profiling == PROF_YES)
1551 prof_child_enter(&wait_start); // entering a child now
1552# endif
1553
1554 // Don't use local function variables, if called from a function.
1555 // Also starts profiling timer for nested script.
1556 save_funccal(&funccalp_entry);
1557
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001558 current_sctx.sc_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001559
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001560 // Check if this script was sourced before to find its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001561 // Always use a new sequence number.
1562 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001564 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565 hashtab_T *ht;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001566 int todo;
1567 hashitem_T *hi;
1568 dictitem_T *di;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569
1570 // loading the same script again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001572 si = SCRIPT_ITEM(sid);
1573 if (si->sn_state == SN_STATE_NOT_LOADED)
1574 {
1575 // this script was found but not loaded yet
1576 si->sn_state = SN_STATE_NEW;
1577 }
1578 else
1579 {
1580 si->sn_state = SN_STATE_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001582 if (!clearvars)
1583 {
1584 // Script-local variables remain but "const" can be set again.
1585 // In Vim9 script variables will be cleared when "vim9script"
1586 // is encountered without the "noclear" argument.
1587 ht = &SCRIPT_VARS(sid);
1588 todo = (int)ht->ht_used;
1589 for (hi = ht->ht_array; todo > 0; ++hi)
1590 if (!HASHITEM_EMPTY(hi))
1591 {
1592 --todo;
1593 di = HI2DI(hi);
1594 di->di_flags |= DI_FLAGS_RELOAD;
1595 }
1596 // imports can be redefined once
1597 mark_imports_for_reload(sid);
1598 }
1599 else
1600 clear_vim9_scriptlocal_vars(sid);
Bram Moolenaar0123cc12021-02-07 17:17:58 +01001601
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001602 // reset version, "vim9script" may have been added or removed.
1603 si->sn_version = 1;
1604 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001605 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001607 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001608 int error = OK;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001609
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001610 // It's new, generate a new SID and initialize the scriptitem.
1611 current_sctx.sc_sid = get_new_scriptitem(&error);
1612 if (error == FAIL)
1613 goto almosttheend;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001614 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001615 si->sn_name = fname_exp;
1616 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617 if (ret_sid != NULL)
1618 *ret_sid = current_sctx.sc_sid;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001619
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001620 // Remember the "is_vimrc" flag for when the file is sourced again.
1621 si->sn_is_vimrc = is_vimrc;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001622 }
1623
1624# ifdef FEAT_PROFILE
1625 if (do_profiling == PROF_YES)
1626 {
1627 int forceit;
1628
1629 // Check if we do profiling for this script.
1630 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
1631 {
1632 script_do_profile(si);
1633 si->sn_pr_force = forceit;
1634 }
1635 if (si->sn_prof_on)
1636 {
1637 ++si->sn_pr_count;
1638 profile_start(&si->sn_pr_start);
1639 profile_zero(&si->sn_pr_children);
1640 }
1641 }
1642# endif
1643#endif
1644
1645 cookie.conv.vc_type = CONV_NONE; // no conversion
1646
1647 // Read the first line so we can check for a UTF-8 BOM.
1648 firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1649 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1650 && firstline[1] == 0xbb && firstline[2] == 0xbf)
1651 {
1652 // Found BOM; setup conversion, skip over BOM and recode the line.
1653 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1654 p = string_convert(&cookie.conv, firstline + 3, NULL);
1655 if (p == NULL)
1656 p = vim_strsave(firstline + 3);
1657 if (p != NULL)
1658 {
1659 vim_free(firstline);
1660 firstline = p;
1661 }
1662 }
1663
1664 // Call do_cmdline, which will call getsourceline() to get the lines.
1665 do_cmdline(firstline, getsourceline, (void *)&cookie,
1666 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1667 retval = OK;
1668
1669#ifdef FEAT_PROFILE
1670 if (do_profiling == PROF_YES)
1671 {
1672 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001673 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001674 if (si->sn_prof_on)
1675 {
1676 profile_end(&si->sn_pr_start);
1677 profile_sub_wait(&wait_start, &si->sn_pr_start);
1678 profile_add(&si->sn_pr_total, &si->sn_pr_start);
1679 profile_self(&si->sn_pr_self, &si->sn_pr_start,
1680 &si->sn_pr_children);
1681 }
1682 }
1683#endif
1684
1685 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001686 emsg(_(e_interrupted));
Bram Moolenaare31ee862020-01-07 20:59:34 +01001687 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001688 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001689 if (p_verbose > 1)
1690 {
1691 verbose_enter();
1692 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001693 if (SOURCING_NAME != NULL)
1694 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001695 verbose_leave();
1696 }
1697#ifdef STARTUPTIME
1698 if (time_fd != NULL)
1699 {
1700 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1701 time_msg((char *)IObuff, &tv_start);
1702 time_pop(&tv_rel);
1703 }
1704#endif
1705
1706 if (!got_int)
1707 trigger_source_post = TRUE;
1708
1709#ifdef FEAT_EVAL
1710 // After a "finish" in debug mode, need to break at first command of next
1711 // sourced file.
1712 if (save_debug_break_level > ex_nesting_level
1713 && debug_break_level == ex_nesting_level)
1714 ++debug_break_level;
1715#endif
1716
1717#ifdef FEAT_EVAL
1718almosttheend:
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001719 // If "sn_save_cpo" is set that means we encountered "vim9script": restore
1720 // 'cpoptions', unless in the main .vimrc file.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001721 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001722 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001723 if (si->sn_save_cpo != NULL && si->sn_is_vimrc == DOSO_NONE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724 {
Bram Moolenaar3e191692021-03-17 17:46:00 +01001725 if (STRCMP(p_cpo, CPO_VIM) != 0)
1726 {
1727 char_u *f;
1728 char_u *t;
1729
1730 // 'cpo' was changed in the script. Apply the same change to the
1731 // saved value, if possible.
1732 for (f = (char_u *)CPO_VIM; *f != NUL; ++f)
1733 if (vim_strchr(p_cpo, *f) == NULL
1734 && (t = vim_strchr(si->sn_save_cpo, *f)) != NULL)
1735 // flag was removed, also remove it from the saved 'cpo'
1736 mch_memmove(t, t + 1, STRLEN(t));
1737 for (f = p_cpo; *f != NUL; ++f)
1738 if (vim_strchr((char_u *)CPO_VIM, *f) == NULL
1739 && vim_strchr(si->sn_save_cpo, *f) == NULL)
1740 {
1741 // flag was added, also add it to the saved 'cpo'
1742 t = alloc(STRLEN(si->sn_save_cpo) + 2);
1743 if (t != NULL)
1744 {
1745 *t = *f;
1746 STRCPY(t + 1, si->sn_save_cpo);
1747 vim_free(si->sn_save_cpo);
1748 si->sn_save_cpo = t;
1749 }
1750 }
1751 }
Bram Moolenaar37294bd2021-03-10 13:40:08 +01001752 set_option_value((char_u *)"cpo", 0L, si->sn_save_cpo, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 }
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001754 VIM_CLEAR(si->sn_save_cpo);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001756 restore_funccal();
1757# ifdef FEAT_PROFILE
1758 if (do_profiling == PROF_YES)
1759 prof_child_exit(&wait_start); // leaving a child now
1760# endif
1761#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001762 current_sctx = save_current_sctx;
1763
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001764 if (cookie.fp != NULL)
1765 fclose(cookie.fp);
1766 if (cookie.source_from_buf)
1767 ga_clear_strings(&cookie.buflines);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001768 vim_free(cookie.nextline);
1769 vim_free(firstline);
1770 convert_setup(&cookie.conv, NULL, NULL);
1771
1772 if (trigger_source_post)
1773 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1774
1775theend:
1776 vim_free(fname_exp);
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001777 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001778#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001779 estack_compiling = save_estack_compiling;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001780#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001781 return retval;
1782}
1783
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001784 int
1785do_source(
1786 char_u *fname,
1787 int check_other, // check for .vimrc and _vimrc
1788 int is_vimrc, // DOSO_ value
1789 int *ret_sid UNUSED)
1790{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001791 return do_source_ext(fname, check_other, is_vimrc, ret_sid, NULL, FALSE);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001792}
1793
1794
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001795#if defined(FEAT_EVAL) || defined(PROTO)
1796
1797/*
1798 * ":scriptnames"
1799 */
1800 void
1801ex_scriptnames(exarg_T *eap)
1802{
1803 int i;
1804
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001805 if (eap->addr_count > 0 || *eap->arg != NUL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001806 {
1807 // :script {scriptId}: edit the script
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001808 if (eap->addr_count > 0 && !SCRIPT_ID_VALID(eap->line2))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001809 emsg(_(e_invalid_argument));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001810 else
1811 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001812 if (eap->addr_count > 0)
1813 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
1814 else
1815 {
1816 expand_env(eap->arg, NameBuff, MAXPATHL);
1817 eap->arg = NameBuff;
1818 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001819 do_exedit(eap, NULL);
1820 }
1821 return;
1822 }
1823
1824 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar6079da72022-01-18 14:16:59 +00001825 {
1826 scriptitem_T *si = SCRIPT_ITEM(i);
1827
1828 if (si->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001829 {
Bram Moolenaar6079da72022-01-18 14:16:59 +00001830 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
1831 vim_snprintf((char *)IObuff, IOSIZE, "%3d%s: %s",
1832 i,
1833 si->sn_state == SN_STATE_NOT_LOADED ? " A" : "",
1834 NameBuff);
Bram Moolenaar769f5892022-02-09 14:31:05 +00001835 if (!message_filtered(IObuff))
1836 {
1837 msg_putchar('\n');
1838 msg_outtrans(IObuff);
1839 out_flush(); // output one line at a time
1840 ui_breakcheck();
1841 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001842 }
Bram Moolenaar6079da72022-01-18 14:16:59 +00001843 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001844}
1845
1846# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1847/*
1848 * Fix slashes in the list of script names for 'shellslash'.
1849 */
1850 void
1851scriptnames_slash_adjust(void)
1852{
1853 int i;
1854
1855 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001856 if (SCRIPT_ITEM(i)->sn_name != NULL)
1857 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001858}
1859# endif
1860
1861/*
1862 * Get a pointer to a script name. Used for ":verbose set".
Bram Moolenaar74667062020-12-28 15:41:41 +01001863 * Message appended to "Last set from "
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001864 */
1865 char_u *
1866get_scriptname(scid_T id)
1867{
1868 if (id == SID_MODELINE)
1869 return (char_u *)_("modeline");
1870 if (id == SID_CMDARG)
1871 return (char_u *)_("--cmd argument");
1872 if (id == SID_CARG)
1873 return (char_u *)_("-c argument");
1874 if (id == SID_ENV)
1875 return (char_u *)_("environment variable");
1876 if (id == SID_ERROR)
1877 return (char_u *)_("error handler");
Bram Moolenaar74667062020-12-28 15:41:41 +01001878 if (id == SID_WINLAYOUT)
1879 return (char_u *)_("changed window size");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001880 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001881}
1882
1883# if defined(EXITFREE) || defined(PROTO)
1884 void
1885free_scriptnames(void)
1886{
1887 int i;
1888
1889 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02001890 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001891 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001893 // the variables themselves are cleared in evalvars_clear()
1894 vim_free(si->sn_vars);
1895
1896 vim_free(si->sn_name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001897 free_imports_and_script_vars(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001898 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02001899# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001900 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02001901# endif
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00001902 vim_free(si->sn_autoload_prefix);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001903 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02001904 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001905 ga_clear(&script_items);
1906}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001907
1908 void
1909free_autoload_scriptnames(void)
1910{
1911 ga_clear_strings(&ga_loaded);
1912}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001913# endif
1914
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001915 linenr_T
Bram Moolenaar66250c92020-08-20 15:02:42 +02001916get_sourced_lnum(
1917 char_u *(*fgetline)(int, void *, int, getline_opt_T),
1918 void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001919{
1920 return fgetline == getsourceline
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001921 ? ((source_cookie_T *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001922 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001923}
Dominique Pelle748b3082022-01-08 12:41:16 +00001924#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001925
1926 static char_u *
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001927get_one_sourceline(source_cookie_T *sp)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001928{
1929 garray_T ga;
1930 int len;
1931 int c;
1932 char_u *buf;
1933#ifdef USE_CRNL
1934 int has_cr; // CR-LF found
1935#endif
1936 int have_read = FALSE;
1937
1938 // use a growarray to store the sourced line
1939 ga_init2(&ga, 1, 250);
1940
1941 // Loop until there is a finished line (or end-of-file).
1942 ++sp->sourcing_lnum;
1943 for (;;)
1944 {
1945 // make room to read at least 120 (more) characters
1946 if (ga_grow(&ga, 120) == FAIL)
1947 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001948 if (sp->source_from_buf)
1949 {
1950 if (sp->buf_lnum >= sp->buflines.ga_len)
1951 break; // all the lines are processed
1952 ga_concat(&ga, ((char_u **)sp->buflines.ga_data)[sp->buf_lnum]);
1953 sp->buf_lnum++;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01001954 if (ga_grow(&ga, 1) == FAIL)
1955 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001956 buf = (char_u *)ga.ga_data;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01001957 buf[ga.ga_len++] = NUL;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001958 }
1959 else
1960 {
1961 buf = (char_u *)ga.ga_data;
1962 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
1963 sp->fp) == NULL)
1964 break;
1965 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001966 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
1967#ifdef USE_CRNL
1968 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
1969 // CTRL-Z by its own, or after a NL.
1970 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
1971 && sp->fileformat == EOL_DOS
1972 && buf[len - 1] == Ctrl_Z)
1973 {
1974 buf[len - 1] = NUL;
1975 break;
1976 }
1977#endif
1978
1979 have_read = TRUE;
1980 ga.ga_len = len;
1981
1982 // If the line was longer than the buffer, read more.
1983 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
1984 continue;
1985
1986 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
1987 {
1988#ifdef USE_CRNL
1989 has_cr = (len >= 2 && buf[len - 2] == '\r');
1990 if (sp->fileformat == EOL_UNKNOWN)
1991 {
1992 if (has_cr)
1993 sp->fileformat = EOL_DOS;
1994 else
1995 sp->fileformat = EOL_UNIX;
1996 }
1997
1998 if (sp->fileformat == EOL_DOS)
1999 {
2000 if (has_cr) // replace trailing CR
2001 {
2002 buf[len - 2] = '\n';
2003 --len;
2004 --ga.ga_len;
2005 }
2006 else // lines like ":map xx yy^M" will have failed
2007 {
2008 if (!sp->error)
2009 {
2010 msg_source(HL_ATTR(HLF_W));
2011 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
2012 }
2013 sp->error = TRUE;
2014 sp->fileformat = EOL_UNIX;
2015 }
2016 }
2017#endif
2018 // The '\n' is escaped if there is an odd number of ^V's just
2019 // before it, first set "c" just before the 'V's and then check
2020 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
2021 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
2022 ;
2023 if ((len & 1) != (c & 1)) // escaped NL, read more
2024 {
2025 ++sp->sourcing_lnum;
2026 continue;
2027 }
2028
2029 buf[len - 1] = NUL; // remove the NL
2030 }
2031
2032 // Check for ^C here now and then, so recursive :so can be broken.
2033 line_breakcheck();
2034 break;
2035 }
2036
2037 if (have_read)
2038 return (char_u *)ga.ga_data;
2039
2040 vim_free(ga.ga_data);
2041 return NULL;
2042}
2043
2044/*
2045 * Get one full line from a sourced file.
2046 * Called by do_cmdline() when it's called from do_source().
2047 *
2048 * Return a pointer to the line in allocated memory.
2049 * Return NULL for end-of-file or some error.
2050 */
2051 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02002052getsourceline(
2053 int c UNUSED,
2054 void *cookie,
2055 int indent UNUSED,
2056 getline_opt_T options)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002057{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002058 source_cookie_T *sp = (source_cookie_T *)cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002059 char_u *line;
2060 char_u *p;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002061 int do_vim9_all = in_vim9script()
2062 && options == GETLINE_CONCAT_ALL;
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002063 int do_bar_cont = do_vim9_all
2064 || options == GETLINE_CONCAT_CONTBAR;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002065
2066#ifdef FEAT_EVAL
2067 // If breakpoints have been added/deleted need to check for it.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002068 if ((sp->dbg_tick < debug_tick) && !sp->source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002069 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002070 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002071 sp->dbg_tick = debug_tick;
2072 }
2073# ifdef FEAT_PROFILE
2074 if (do_profiling == PROF_YES)
2075 script_line_end();
2076# endif
2077#endif
2078
2079 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002080 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002081
2082 // Get current line. If there is a read-ahead line, use it, otherwise get
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002083 // one now. "fp" is NULL if actually using a string.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002084 if (sp->finished || (!sp->source_from_buf && sp->fp == NULL))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002085 line = NULL;
2086 else if (sp->nextline == NULL)
2087 line = get_one_sourceline(sp);
2088 else
2089 {
2090 line = sp->nextline;
2091 sp->nextline = NULL;
2092 ++sp->sourcing_lnum;
2093 }
2094#ifdef FEAT_PROFILE
2095 if (line != NULL && do_profiling == PROF_YES)
2096 script_line_start();
2097#endif
2098
2099 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
2100 // contain the 'C' flag.
Bram Moolenaar66250c92020-08-20 15:02:42 +02002101 if (line != NULL && options != GETLINE_NONE
2102 && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002103 {
Bram Moolenaar5072b472021-06-03 21:56:10 +02002104 int comment_char = in_vim9script() ? '#' : '"';
2105
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002106 // compensate for the one line read-ahead
2107 --sp->sourcing_lnum;
2108
2109 // Get the next line and concatenate it when it starts with a
2110 // backslash. We always need to read the next line, keep it in
2111 // sp->nextline.
2112 /* Also check for a comment in between continuation lines: "\ */
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002113 // Also check for a Vim9 comment, empty line, line starting with '|',
2114 // but not "||".
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002115 sp->nextline = get_one_sourceline(sp);
2116 if (sp->nextline != NULL
2117 && (*(p = skipwhite(sp->nextline)) == '\\'
Bram Moolenaar5072b472021-06-03 21:56:10 +02002118 || (p[0] == comment_char
2119 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002120 || (do_vim9_all && (*p == NUL
2121 || vim9_comment_start(p)))
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002122 || (do_bar_cont && p[0] == '|' && p[1] != '|')))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002123 {
2124 garray_T ga;
2125
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002126 ga_init2(&ga, sizeof(char_u), 400);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002127 ga_concat(&ga, line);
2128 if (*p == '\\')
2129 ga_concat(&ga, p + 1);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002130 else if (*p == '|')
2131 {
2132 ga_concat(&ga, (char_u *)" ");
2133 ga_concat(&ga, p);
2134 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002135 for (;;)
2136 {
2137 vim_free(sp->nextline);
2138 sp->nextline = get_one_sourceline(sp);
2139 if (sp->nextline == NULL)
2140 break;
2141 p = skipwhite(sp->nextline);
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002142 if (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|'))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002143 {
2144 // Adjust the growsize to the current length to speed up
2145 // concatenating many lines.
2146 if (ga.ga_len > 400)
2147 {
2148 if (ga.ga_len > 8000)
2149 ga.ga_growsize = 8000;
2150 else
2151 ga.ga_growsize = ga.ga_len;
2152 }
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002153 if (*p == '\\')
2154 ga_concat(&ga, p + 1);
2155 else
2156 {
2157 ga_concat(&ga, (char_u *)" ");
2158 ga_concat(&ga, p);
2159 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002160 }
Bram Moolenaar5072b472021-06-03 21:56:10 +02002161 else if (!(p[0] == (comment_char)
Bram Moolenaar0f37e352021-06-02 15:28:15 +02002162 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002163 && !(do_vim9_all && (*p == NUL || vim9_comment_start(p))))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002164 break;
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002165 /* drop a # comment or "\ comment line */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002166 }
2167 ga_append(&ga, NUL);
2168 vim_free(line);
2169 line = ga.ga_data;
2170 }
2171 }
2172
2173 if (line != NULL && sp->conv.vc_type != CONV_NONE)
2174 {
2175 char_u *s;
2176
2177 // Convert the encoding of the script line.
2178 s = string_convert(&sp->conv, line, NULL);
2179 if (s != NULL)
2180 {
2181 vim_free(line);
2182 line = s;
2183 }
2184 }
2185
2186#ifdef FEAT_EVAL
2187 // Did we encounter a breakpoint?
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002188 if (!sp->source_from_buf && sp->breakpoint != 0
2189 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002190 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002191 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002192 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002193 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002194 sp->dbg_tick = debug_tick;
2195 }
2196#endif
2197
2198 return line;
2199}
2200
2201/*
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002202 * Returns TRUE if sourcing a script either from a file or a buffer.
2203 * Otherwise returns FALSE.
2204 */
2205 int
2206sourcing_a_script(exarg_T *eap)
2207{
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002208 return (getline_equal(eap->getline, eap->cookie, getsourceline));
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002209}
2210
2211/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002212 * ":scriptencoding": Set encoding conversion for a sourced script.
2213 */
2214 void
2215ex_scriptencoding(exarg_T *eap)
2216{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002217 source_cookie_T *sp;
2218 char_u *name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002219
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002220 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002221 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00002222 emsg(_(e_scriptencoding_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002223 return;
2224 }
2225
2226 if (*eap->arg != NUL)
2227 {
2228 name = enc_canonize(eap->arg);
2229 if (name == NULL) // out of memory
2230 return;
2231 }
2232 else
2233 name = eap->arg;
2234
2235 // Setup for conversion from the specified encoding to 'encoding'.
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002236 sp = (source_cookie_T *)getline_cookie(eap->getline, eap->cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002237 convert_setup(&sp->conv, name, p_enc);
2238
2239 if (name != eap->arg)
2240 vim_free(name);
2241}
2242
2243/*
2244 * ":scriptversion": Set Vim script version for a sourced script.
2245 */
2246 void
2247ex_scriptversion(exarg_T *eap UNUSED)
2248{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002249 int nr;
2250
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002251 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002252 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002253 emsg(_(e_scriptversion_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002254 return;
2255 }
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002256 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002258 emsg(_(e_cannot_use_scriptversion_after_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 return;
2260 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002261
2262 nr = getdigits(&eap->arg);
2263 if (nr == 0 || *eap->arg != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002264 emsg(_(e_invalid_argument));
Bram Moolenaar67979662020-06-20 22:50:47 +02002265 else if (nr > SCRIPT_VERSION_MAX)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002266 semsg(_(e_scriptversion_not_supported_nr), nr);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002267 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002268 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002269 current_sctx.sc_version = nr;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002270#ifdef FEAT_EVAL
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002271 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002272#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002273 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002274}
2275
2276#if defined(FEAT_EVAL) || defined(PROTO)
2277/*
2278 * ":finish": Mark a sourced file as finished.
2279 */
2280 void
2281ex_finish(exarg_T *eap)
2282{
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002283 if (sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002284 do_finish(eap, FALSE);
2285 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00002286 emsg(_(e_finish_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002287}
2288
2289/*
2290 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
2291 * Also called for a pending finish at the ":endtry" or after returning from
2292 * an extra do_cmdline(). "reanimate" is used in the latter case.
2293 */
2294 void
2295do_finish(exarg_T *eap, int reanimate)
2296{
2297 int idx;
2298
2299 if (reanimate)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002300 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002301 eap->cookie))->finished = FALSE;
2302
2303 // Cleanup (and inactivate) conditionals, but stop when a try conditional
2304 // not in its finally clause (which then is to be executed next) is found.
2305 // In this case, make the ":finish" pending for execution at the ":endtry".
2306 // Otherwise, finish normally.
2307 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
2308 if (idx >= 0)
2309 {
2310 eap->cstack->cs_pending[idx] = CSTP_FINISH;
2311 report_make_pending(CSTP_FINISH, NULL);
2312 }
2313 else
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002314 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002315 eap->cookie))->finished = TRUE;
2316}
2317
2318
2319/*
2320 * Return TRUE when a sourced file had the ":finish" command: Don't give error
2321 * message for missing ":endif".
2322 * Return FALSE when not sourcing a file.
2323 */
2324 int
2325source_finished(
Bram Moolenaar66250c92020-08-20 15:02:42 +02002326 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002327 void *cookie)
2328{
2329 return (getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002330 && ((source_cookie_T *)getline_cookie(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002331 fgetline, cookie))->finished);
2332}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002333
2334/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002335 * Find the path of a script below the "autoload" directory.
2336 * Returns NULL if there is no "/autoload/" in the script name.
2337 */
2338 char_u *
2339script_name_after_autoload(scriptitem_T *si)
2340{
2341 char_u *p = si->sn_name;
2342 char_u *res = NULL;
2343
2344 for (;;)
2345 {
2346 char_u *n = (char_u *)strstr((char *)p, "autoload");
2347
2348 if (n == NULL)
2349 break;
2350 if (n > p && vim_ispathsep(n[-1]) && vim_ispathsep(n[8]))
2351 res = n + 9;
2352 p = n + 8;
2353 }
2354 return res;
2355}
2356
2357/*
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002358 * For an autoload script "autoload/dir/script.vim" return the prefix
2359 * "dir#script#" in allocated memory.
2360 * Returns NULL if anything is wrong.
2361 */
2362 char_u *
2363get_autoload_prefix(scriptitem_T *si)
2364{
2365 char_u *p = script_name_after_autoload(si);
2366 char_u *prefix;
2367
2368 if (p == NULL)
2369 return NULL;
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002370 prefix = vim_strsave(p);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002371 if (prefix == NULL)
2372 return NULL;
2373
2374 // replace all '/' with '#' and locate ".vim" at the end
2375 for (p = prefix; *p != NUL; p += mb_ptr2len(p))
2376 {
2377 if (vim_ispathsep(*p))
2378 *p = '#';
2379 else if (STRCMP(p, ".vim") == 0)
2380 {
2381 p[0] = '#';
2382 p[1] = NUL;
2383 return prefix;
2384 }
2385 }
2386
2387 // did not find ".vim" at the end
2388 vim_free(prefix);
2389 return NULL;
2390}
2391
2392/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002393 * If in a Vim9 autoload script return "name" with the autoload prefix for the
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002394 * script. If successful the returned name is allocated.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002395 * Otherwise it returns "name" unmodified.
2396 */
2397 char_u *
2398may_prefix_autoload(char_u *name)
2399{
2400 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
2401 {
2402 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2403
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002404 if (si->sn_autoload_prefix != NULL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002405 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002406 char_u *basename = name;
2407 size_t len;
2408 char_u *res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002409
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002410 if (*name == K_SPECIAL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002411 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002412 char_u *p = vim_strchr(name, '_');
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002413
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002414 // skip over "<SNR>99_"
2415 if (p != NULL)
2416 basename = p + 1;
2417 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002418
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002419 len = STRLEN(si->sn_autoload_prefix) + STRLEN(basename) + 2;
2420 res = alloc(len);
2421 if (res != NULL)
2422 {
2423 vim_snprintf((char *)res, len, "%s%s",
2424 si->sn_autoload_prefix, basename);
2425 return res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002426 }
2427 }
2428 }
2429 return name;
2430}
2431
2432/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002433 * Return the autoload script name for a function or variable name.
2434 * Returns NULL when out of memory.
2435 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
2436 */
2437 char_u *
2438autoload_name(char_u *name)
2439{
2440 char_u *p, *q = NULL;
2441 char_u *scriptname;
2442
2443 // Get the script file name: replace '#' with '/', append ".vim".
2444 scriptname = alloc(STRLEN(name) + 14);
2445 if (scriptname == NULL)
2446 return NULL;
2447 STRCPY(scriptname, "autoload/");
Bram Moolenaara1773442020-08-12 15:21:22 +02002448 STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002449 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
2450 q = p, ++p)
2451 *p = '/';
2452 STRCPY(q, ".vim");
2453 return scriptname;
2454}
2455
2456/*
2457 * If "name" has a package name try autoloading the script for it.
2458 * Return TRUE if a package was loaded.
2459 */
2460 int
2461script_autoload(
2462 char_u *name,
2463 int reload) // load script again when already loaded
2464{
2465 char_u *p;
2466 char_u *scriptname, *tofree;
2467 int ret = FALSE;
2468 int i;
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002469 int ret_sid;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002470
Bram Moolenaar89445512022-04-14 12:58:23 +01002471 // If the name starts with "<SNR>123_" then "123" is the script ID.
2472 if (name[0] == K_SPECIAL && name[1] == KS_EXTRA && name[2] == KE_SNR)
2473 {
2474 p = name + 3;
2475 ret_sid = (int)getdigits(&p);
2476 if (*p == '_' && SCRIPT_ID_VALID(ret_sid))
2477 {
2478 may_load_script(ret_sid, &ret);
2479 return ret;
2480 }
2481 }
2482
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002483 // If there is no '#' after name[0] there is no package name.
2484 p = vim_strchr(name, AUTOLOAD_CHAR);
2485 if (p == NULL || p == name)
2486 return FALSE;
2487
2488 tofree = scriptname = autoload_name(name);
2489 if (scriptname == NULL)
2490 return FALSE;
2491
2492 // Find the name in the list of previously loaded package names. Skip
2493 // "autoload/", it's always the same.
2494 for (i = 0; i < ga_loaded.ga_len; ++i)
2495 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2496 break;
2497 if (!reload && i < ga_loaded.ga_len)
2498 ret = FALSE; // was loaded already
2499 else
2500 {
2501 // Remember the name if it wasn't loaded already.
2502 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2503 {
2504 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2505 tofree = NULL;
2506 }
2507
2508 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002509 // Use "ret_sid" to avoid loading the same script again.
=?UTF-8?q?Bj=C3=B6rn=20Linse?=223a9502022-01-31 17:26:05 +00002510 if (source_in_path(p_rtp, scriptname, DIP_START, &ret_sid) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002511 ret = TRUE;
2512 }
2513
2514 vim_free(tofree);
2515 return ret;
2516}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002517#endif