blob: ec47edf72d2f4fb2807e99fec377f233efc127b5 [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/*
120 * Get the current value for <sfile> in allocated memory.
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200121 * "which" is ESTACK_SFILE for <sfile> and ESTACK_STACK for <stack>.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100122 */
123 char_u *
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200124estack_sfile(estack_arg_T which UNUSED)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100125{
Bram Moolenaar85b09572019-12-30 10:57:00 +0100126 estack_T *entry;
127#ifdef FEAT_EVAL
Bram Moolenaara5d04232020-07-26 15:37:02 +0200128 garray_T ga;
Bram Moolenaar4d7a2482020-01-06 19:53:43 +0100129 size_t len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100130 int idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200131 etype_T last_type = ETYPE_SCRIPT;
132 char *type_name;
Bram Moolenaar85b09572019-12-30 10:57:00 +0100133#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100134
135 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100136#ifdef FEAT_EVAL
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200137 if (which == ESTACK_SFILE && entry->es_type != ETYPE_UFUNC)
Bram Moolenaar09d46402019-12-29 23:53:01 +0100138#endif
Bram Moolenaara5d04232020-07-26 15:37:02 +0200139 {
140 if (entry->es_name == NULL)
141 return NULL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100142 return vim_strsave(entry->es_name);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200143 }
Bram Moolenaar09d46402019-12-29 23:53:01 +0100144#ifdef FEAT_EVAL
Bram Moolenaarc4492712021-11-22 15:37:15 +0000145 // expand('<sfile>') works in a function for backwards compatibility, but
146 // may give an unexpected result. Disallow it in Vim 9 script.
147 if (which == ESTACK_SFILE && in_vim9script())
148 {
149 int save_emsg_off = emsg_off;
150
151 if (emsg_off == 1)
152 // f_expand() silences errors but we do want this one
153 emsg_off = 0;
154 emsg(_(e_cannot_expand_sfile_in_vim9_function));
155 emsg_off = save_emsg_off;
156 return NULL;
157 }
158
Bram Moolenaara5d04232020-07-26 15:37:02 +0200159 // Give information about each stack entry up to the root.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100160 // For a function we compose the call stack, as it was done in the past:
161 // "function One[123]..Two[456]..Three"
Bram Moolenaara5d04232020-07-26 15:37:02 +0200162 ga_init2(&ga, sizeof(char), 100);
163 for (idx = 0; idx < exestack.ga_len; ++idx)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100164 {
165 entry = ((estack_T *)exestack.ga_data) + idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200166 if (entry->es_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100167 {
Bram Moolenaara810db32020-09-11 17:59:23 +0200168 long lnum = 0;
169 char *dots;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200170
Bram Moolenaara5d04232020-07-26 15:37:02 +0200171 len = STRLEN(entry->es_name) + 15;
172 type_name = "";
173 if (entry->es_type != last_type)
174 {
175 switch (entry->es_type)
176 {
177 case ETYPE_SCRIPT: type_name = "script "; break;
178 case ETYPE_UFUNC: type_name = "function "; break;
179 default: type_name = ""; break;
180 }
181 last_type = entry->es_type;
182 }
183 len += STRLEN(type_name);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200184 if (ga_grow(&ga, (int)len) == FAIL)
Bram Moolenaara5d04232020-07-26 15:37:02 +0200185 break;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200186 if (idx == exestack.ga_len - 1)
187 lnum = which == ESTACK_STACK ? SOURCING_LNUM : 0;
188 else
189 lnum = entry->es_lnum;
Bram Moolenaara810db32020-09-11 17:59:23 +0200190 dots = idx == exestack.ga_len - 1 ? "" : "..";
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200191 if (lnum == 0)
192 // For the bottom entry of <sfile>: do not add the line number,
193 // it is used in <slnum>. Also leave it out when the number is
194 // not set.
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200195 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s%s",
Bram Moolenaara810db32020-09-11 17:59:23 +0200196 type_name, entry->es_name, dots);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200197 else
Bram Moolenaara810db32020-09-11 17:59:23 +0200198 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s[%ld]%s",
199 type_name, entry->es_name, lnum, dots);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200200 ga.ga_len += (int)STRLEN((char *)ga.ga_data + ga.ga_len);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100201 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100202 }
203
Bram Moolenaara5d04232020-07-26 15:37:02 +0200204 return (char_u *)ga.ga_data;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100205#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100206}
207
208/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200209 * ":runtime [what] {name}"
210 */
211 void
212ex_runtime(exarg_T *eap)
213{
214 char_u *arg = eap->arg;
215 char_u *p = skiptowhite(arg);
216 int len = (int)(p - arg);
217 int flags = eap->forceit ? DIP_ALL : 0;
218
219 if (STRNCMP(arg, "START", len) == 0)
220 {
221 flags += DIP_START + DIP_NORTP;
222 arg = skipwhite(arg + len);
223 }
224 else if (STRNCMP(arg, "OPT", len) == 0)
225 {
226 flags += DIP_OPT + DIP_NORTP;
227 arg = skipwhite(arg + len);
228 }
229 else if (STRNCMP(arg, "PACK", len) == 0)
230 {
231 flags += DIP_START + DIP_OPT + DIP_NORTP;
232 arg = skipwhite(arg + len);
233 }
234 else if (STRNCMP(arg, "ALL", len) == 0)
235 {
236 flags += DIP_START + DIP_OPT;
237 arg = skipwhite(arg + len);
238 }
239
240 source_runtime(arg, flags);
241}
242
243 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244source_callback(char_u *fname, void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200245{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200247}
248
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000249#ifdef FEAT_EVAL
250/*
251 * Find an already loaded script "name".
252 * If found returns its script ID. If not found returns -1.
253 */
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100254 int
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000255find_script_by_name(char_u *name)
256{
257 int sid;
258 scriptitem_T *si;
259
260 for (sid = script_items.ga_len; sid > 0; --sid)
261 {
262 // We used to check inode here, but that doesn't work:
263 // - If a script is edited and written, it may get a different
264 // inode number, even though to the user it is the same script.
265 // - If a script is deleted and another script is written, with a
266 // different name, the inode may be re-used.
267 si = SCRIPT_ITEM(sid);
268 if (si->sn_name != NULL && fnamecmp(si->sn_name, name) == 0)
269 return sid;
270 }
271 return -1;
272}
273
274/*
275 * Add a new scriptitem with all items initialized.
276 * When running out of memory "error" is set to FAIL.
277 * Returns the script ID.
278 */
279 static int
280get_new_scriptitem(int *error)
281{
282 static scid_T last_current_SID = 0;
283 int sid = ++last_current_SID;
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000284 scriptitem_T *si = NULL;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000285
286 if (ga_grow(&script_items, (int)(sid - script_items.ga_len)) == FAIL)
287 {
288 *error = FAIL;
289 return sid;
290 }
291 while (script_items.ga_len < sid)
292 {
293 si = ALLOC_CLEAR_ONE(scriptitem_T);
294 if (si == NULL)
295 {
296 *error = FAIL;
297 return sid;
298 }
299 ++script_items.ga_len;
300 SCRIPT_ITEM(script_items.ga_len) = si;
301 si->sn_name = NULL;
302 si->sn_version = 1;
303
304 // Allocate the local script variables to use for this script.
305 new_script_vars(script_items.ga_len);
306 ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
307 hash_init(&si->sn_all_vars.dv_hashtab);
308 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
309 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
310# ifdef FEAT_PROFILE
311 si->sn_prof_on = FALSE;
312# endif
313 }
314
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000315 // "si" can't be NULL, check only to avoid a compiler warning
316 if (si != NULL)
317 // Used to check script variable index is still valid.
318 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000319
320 return sid;
321}
322
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100323 int
324get_new_scriptitem_for_fname(int *error, char_u *fname)
325{
326 int sid = get_new_scriptitem(error);
327
328 if (*error == OK)
329 {
330 scriptitem_T *si = SCRIPT_ITEM(sid);
331
332 si->sn_name = vim_strsave(fname);
333 si->sn_state = SN_STATE_NOT_LOADED;
334 }
335 return sid;
336}
337
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000338 static void
339find_script_callback(char_u *fname, void *cookie)
340{
341 int sid;
342 int error = OK;
343 int *ret_sid = cookie;
344
345 sid = find_script_by_name(fname);
346 if (sid < 0)
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000347 // script does not exist yet, create a new scriptitem
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100348 sid = get_new_scriptitem_for_fname(&error, fname);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000349 *ret_sid = sid;
350}
351#endif
352
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200353/*
354 * Find the file "name" in all directories in "path" and invoke
355 * "callback(fname, cookie)".
356 * "name" can contain wildcards.
357 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
358 * When "flags" has DIP_DIR: find directories instead of files.
359 * When "flags" has DIP_ERR: give an error message if there is no match.
360 *
361 * return FAIL when no file could be sourced, OK otherwise.
362 */
363 int
364do_in_path(
365 char_u *path,
366 char_u *name,
367 int flags,
368 void (*callback)(char_u *fname, void *ck),
369 void *cookie)
370{
371 char_u *rtp;
372 char_u *np;
373 char_u *buf;
374 char_u *rtp_copy;
375 char_u *tail;
376 int num_files;
377 char_u **files;
378 int i;
379 int did_one = FALSE;
380#ifdef AMIGA
381 struct Process *proc = (struct Process *)FindTask(0L);
382 APTR save_winptr = proc->pr_WindowPtr;
383
384 // Avoid a requester here for a volume that doesn't exist.
385 proc->pr_WindowPtr = (APTR)-1L;
386#endif
387
388 // Make a copy of 'runtimepath'. Invoking the callback may change the
389 // value.
390 rtp_copy = vim_strsave(path);
391 buf = alloc(MAXPATHL);
392 if (buf != NULL && rtp_copy != NULL)
393 {
Bram Moolenaar647a5302020-05-03 17:01:24 +0200394 if (p_verbose > 10 && name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200395 {
396 verbose_enter();
397 smsg(_("Searching for \"%s\" in \"%s\""),
398 (char *)name, (char *)path);
399 verbose_leave();
400 }
401
402 // Loop over all entries in 'runtimepath'.
403 rtp = rtp_copy;
404 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
405 {
406 size_t buflen;
407
408 // Copy the path from 'runtimepath' to buf[].
409 copy_option_part(&rtp, buf, MAXPATHL, ",");
410 buflen = STRLEN(buf);
411
412 // Skip after or non-after directories.
413 if (flags & (DIP_NOAFTER | DIP_AFTER))
414 {
415 int is_after = buflen >= 5
416 && STRCMP(buf + buflen - 5, "after") == 0;
417
418 if ((is_after && (flags & DIP_NOAFTER))
419 || (!is_after && (flags & DIP_AFTER)))
420 continue;
421 }
422
423 if (name == NULL)
424 {
425 (*callback)(buf, (void *) &cookie);
426 if (!did_one)
427 did_one = (cookie == NULL);
428 }
429 else if (buflen + STRLEN(name) + 2 < MAXPATHL)
430 {
431 add_pathsep(buf);
432 tail = buf + STRLEN(buf);
433
434 // Loop over all patterns in "name"
435 np = name;
436 while (*np != NUL && ((flags & DIP_ALL) || !did_one))
437 {
438 // Append the pattern from "name" to buf[].
439 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
440 "\t ");
441
Bram Moolenaar647a5302020-05-03 17:01:24 +0200442 if (p_verbose > 10)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200443 {
444 verbose_enter();
445 smsg(_("Searching for \"%s\""), buf);
446 verbose_leave();
447 }
448
449 // Expand wildcards, invoke the callback for each match.
450 if (gen_expand_wildcards(1, &buf, &num_files, &files,
451 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
452 {
453 for (i = 0; i < num_files; ++i)
454 {
455 (*callback)(files[i], cookie);
456 did_one = TRUE;
457 if (!(flags & DIP_ALL))
458 break;
459 }
460 FreeWild(num_files, files);
461 }
462 }
463 }
464 }
465 }
466 vim_free(buf);
467 vim_free(rtp_copy);
468 if (!did_one && name != NULL)
469 {
470 char *basepath = path == p_rtp ? "runtimepath" : "packpath";
471
472 if (flags & DIP_ERR)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000473 semsg(_(e_directory_not_found_in_str_str), basepath, name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200474 else if (p_verbose > 0)
475 {
476 verbose_enter();
477 smsg(_("not found in '%s': \"%s\""), basepath, name);
478 verbose_leave();
479 }
480 }
481
482#ifdef AMIGA
483 proc->pr_WindowPtr = save_winptr;
484#endif
485
486 return did_one ? OK : FAIL;
487}
488
489/*
490 * Find "name" in "path". When found, invoke the callback function for
491 * it: callback(fname, "cookie")
492 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first
493 * one is used.
494 * Returns OK when at least one match found, FAIL otherwise.
495 *
496 * If "name" is NULL calls callback for each entry in "path". Cookie is
497 * passed by reference in this case, setting it to NULL indicates that callback
498 * has done its job.
499 */
500 static int
501do_in_path_and_pp(
502 char_u *path,
503 char_u *name,
504 int flags,
505 void (*callback)(char_u *fname, void *ck),
506 void *cookie)
507{
508 int done = FAIL;
509 char_u *s;
510 int len;
511 char *start_dir = "pack/*/start/*/%s";
512 char *opt_dir = "pack/*/opt/*/%s";
513
514 if ((flags & DIP_NORTP) == 0)
515 done = do_in_path(path, name, flags, callback, cookie);
516
517 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
518 {
519 len = (int)(STRLEN(start_dir) + STRLEN(name));
520 s = alloc(len);
521 if (s == NULL)
522 return FAIL;
523 vim_snprintf((char *)s, len, start_dir, name);
524 done = do_in_path(p_pp, s, flags, callback, cookie);
525 vim_free(s);
526 }
527
528 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
529 {
530 len = (int)(STRLEN(opt_dir) + STRLEN(name));
531 s = alloc(len);
532 if (s == NULL)
533 return FAIL;
534 vim_snprintf((char *)s, len, opt_dir, name);
535 done = do_in_path(p_pp, s, flags, callback, cookie);
536 vim_free(s);
537 }
538
539 return done;
540}
541
542/*
543 * Just like do_in_path_and_pp(), using 'runtimepath' for "path".
544 */
545 int
546do_in_runtimepath(
547 char_u *name,
548 int flags,
549 void (*callback)(char_u *fname, void *ck),
550 void *cookie)
551{
552 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
553}
554
555/*
556 * Source the file "name" from all directories in 'runtimepath'.
557 * "name" can contain wildcards.
558 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
559 *
560 * return FAIL when no file could be sourced, OK otherwise.
561 */
562 int
563source_runtime(char_u *name, int flags)
564{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100565 return source_in_path(p_rtp, name, flags, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200566}
567
568/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000569 * Just like source_runtime(), but use "path" instead of 'runtimepath'
570 * and return the script ID in "ret_sid".
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200571 */
572 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200574{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200576}
577
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200578#if defined(FEAT_EVAL) || defined(PROTO)
579
580/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000581 * Find "name" in 'runtimepath'. If found a new scriptitem is created for it
582 * and it's script ID is returned.
583 * If not found returns -1.
584 */
585 int
586find_script_in_rtp(char_u *name)
587{
588 int sid = -1;
589
590 (void)do_in_path_and_pp(p_rtp, name, DIP_NOAFTER,
591 find_script_callback, &sid);
592 return sid;
593}
594
595/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200596 * Expand wildcards in "pat" and invoke do_source() for each match.
597 */
598 static void
599source_all_matches(char_u *pat)
600{
601 int num_files;
602 char_u **files;
603 int i;
604
605 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK)
606 {
607 for (i = 0; i < num_files; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200609 FreeWild(num_files, files);
610 }
611}
612
613/*
614 * Add the package directory to 'runtimepath'.
615 */
616 static int
617add_pack_dir_to_rtp(char_u *fname)
618{
619 char_u *p4, *p3, *p2, *p1, *p;
620 char_u *entry;
621 char_u *insp = NULL;
622 int c;
623 char_u *new_rtp;
624 int keep;
625 size_t oldlen;
626 size_t addlen;
627 size_t new_rtp_len;
628 char_u *afterdir = NULL;
629 size_t afterlen = 0;
630 char_u *after_insp = NULL;
631 char_u *ffname = NULL;
632 size_t fname_len;
633 char_u *buf = NULL;
634 char_u *rtp_ffname;
635 int match;
636 int retval = FAIL;
637
638 p4 = p3 = p2 = p1 = get_past_head(fname);
639 for (p = p1; *p; MB_PTR_ADV(p))
640 if (vim_ispathsep_nocolon(*p))
641 {
642 p4 = p3; p3 = p2; p2 = p1; p1 = p;
643 }
644
645 // now we have:
646 // rtp/pack/name/start/name
647 // p4 p3 p2 p1
648 //
649 // find the part up to "pack" in 'runtimepath'
650 c = *++p4; // append pathsep in order to expand symlink
651 *p4 = NUL;
652 ffname = fix_fname(fname);
653 *p4 = c;
654 if (ffname == NULL)
655 return FAIL;
656
657 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
658 // Also stop at the first "after" directory.
659 fname_len = STRLEN(ffname);
660 buf = alloc(MAXPATHL);
661 if (buf == NULL)
662 goto theend;
663 for (entry = p_rtp; *entry != NUL; )
664 {
665 char_u *cur_entry = entry;
666
667 copy_option_part(&entry, buf, MAXPATHL, ",");
668 if (insp == NULL)
669 {
670 add_pathsep(buf);
671 rtp_ffname = fix_fname(buf);
672 if (rtp_ffname == NULL)
673 goto theend;
674 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
675 vim_free(rtp_ffname);
676 if (match)
677 // Insert "ffname" after this entry (and comma).
678 insp = entry;
679 }
680
681 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
682 && p > buf
683 && vim_ispathsep(p[-1])
684 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
685 {
686 if (insp == NULL)
687 // Did not find "ffname" before the first "after" directory,
688 // insert it before this entry.
689 insp = cur_entry;
690 after_insp = cur_entry;
691 break;
692 }
693 }
694
695 if (insp == NULL)
696 // Both "fname" and "after" not found, append at the end.
697 insp = p_rtp + STRLEN(p_rtp);
698
699 // check if rtp/pack/name/start/name/after exists
700 afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
701 if (afterdir != NULL && mch_isdir(afterdir))
702 afterlen = STRLEN(afterdir) + 1; // add one for comma
703
704 oldlen = STRLEN(p_rtp);
705 addlen = STRLEN(fname) + 1; // add one for comma
706 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
707 if (new_rtp == NULL)
708 goto theend;
709
710 // We now have 'rtp' parts: {keep}{keep_after}{rest}.
711 // Create new_rtp, first: {keep},{fname}
712 keep = (int)(insp - p_rtp);
713 mch_memmove(new_rtp, p_rtp, keep);
714 new_rtp_len = keep;
715 if (*insp == NUL)
716 new_rtp[new_rtp_len++] = ','; // add comma before
717 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
718 new_rtp_len += addlen - 1;
719 if (*insp != NUL)
720 new_rtp[new_rtp_len++] = ','; // add comma after
721
722 if (afterlen > 0 && after_insp != NULL)
723 {
724 int keep_after = (int)(after_insp - p_rtp);
725
726 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
727 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
728 keep_after - keep);
729 new_rtp_len += keep_after - keep;
730 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
731 new_rtp_len += afterlen - 1;
732 new_rtp[new_rtp_len++] = ',';
733 keep = keep_after;
734 }
735
736 if (p_rtp[keep] != NUL)
737 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
738 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
739 else
740 new_rtp[new_rtp_len] = NUL;
741
742 if (afterlen > 0 && after_insp == NULL)
743 {
744 // Append afterdir when "after" was not found:
745 // {keep},{fname}{rest},{afterdir}
746 STRCAT(new_rtp, ",");
747 STRCAT(new_rtp, afterdir);
748 }
749
750 set_option_value((char_u *)"rtp", 0L, new_rtp, 0);
751 vim_free(new_rtp);
752 retval = OK;
753
754theend:
755 vim_free(buf);
756 vim_free(ffname);
757 vim_free(afterdir);
758 return retval;
759}
760
761/*
762 * Load scripts in "plugin" and "ftdetect" directories of the package.
763 */
764 static int
765load_pack_plugin(char_u *fname)
766{
767 static char *plugpat = "%s/plugin/**/*.vim";
768 static char *ftpat = "%s/ftdetect/*.vim";
769 int len;
770 char_u *ffname = fix_fname(fname);
771 char_u *pat = NULL;
772 int retval = FAIL;
773
774 if (ffname == NULL)
775 return FAIL;
776 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
777 pat = alloc(len);
778 if (pat == NULL)
779 goto theend;
780 vim_snprintf((char *)pat, len, plugpat, ffname);
781 source_all_matches(pat);
782
783 {
784 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
785
786 // If runtime/filetype.vim wasn't loaded yet, the scripts will be
787 // found when it loads.
788 if (cmd != NULL && eval_to_number(cmd) > 0)
789 {
790 do_cmdline_cmd((char_u *)"augroup filetypedetect");
791 vim_snprintf((char *)pat, len, ftpat, ffname);
792 source_all_matches(pat);
793 do_cmdline_cmd((char_u *)"augroup END");
794 }
795 vim_free(cmd);
796 }
797 vim_free(pat);
798 retval = OK;
799
800theend:
801 vim_free(ffname);
802 return retval;
803}
804
805// used for "cookie" of add_pack_plugin()
806static int APP_ADD_DIR;
807static int APP_LOAD;
808static int APP_BOTH;
809
810 static void
811add_pack_plugin(char_u *fname, void *cookie)
812{
813 if (cookie != &APP_LOAD)
814 {
815 char_u *buf = alloc(MAXPATHL);
816 char_u *p;
817 int found = FALSE;
818
819 if (buf == NULL)
820 return;
821 p = p_rtp;
822 while (*p != NUL)
823 {
824 copy_option_part(&p, buf, MAXPATHL, ",");
825 if (pathcmp((char *)buf, (char *)fname, -1) == 0)
826 {
827 found = TRUE;
828 break;
829 }
830 }
831 vim_free(buf);
832 if (!found)
833 // directory is not yet in 'runtimepath', add it
834 if (add_pack_dir_to_rtp(fname) == FAIL)
835 return;
836 }
837
838 if (cookie != &APP_ADD_DIR)
839 load_pack_plugin(fname);
840}
841
842/*
843 * Add all packages in the "start" directory to 'runtimepath'.
844 */
845 void
846add_pack_start_dirs(void)
847{
848 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
849 add_pack_plugin, &APP_ADD_DIR);
850}
851
852/*
853 * Load plugins from all packages in the "start" directory.
854 */
855 void
856load_start_packages(void)
857{
858 did_source_packages = TRUE;
859 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
860 add_pack_plugin, &APP_LOAD);
861}
862
863/*
864 * ":packloadall"
865 * Find plugins in the package directories and source them.
866 */
867 void
868ex_packloadall(exarg_T *eap)
869{
870 if (!did_source_packages || eap->forceit)
871 {
872 // First do a round to add all directories to 'runtimepath', then load
873 // the plugins. This allows for plugins to use an autoload directory
874 // of another plugin.
875 add_pack_start_dirs();
876 load_start_packages();
877 }
878}
879
880/*
881 * ":packadd[!] {name}"
882 */
883 void
884ex_packadd(exarg_T *eap)
885{
886 static char *plugpat = "pack/*/%s/%s";
887 int len;
888 char *pat;
889 int round;
890 int res = OK;
891
892 // Round 1: use "start", round 2: use "opt".
893 for (round = 1; round <= 2; ++round)
894 {
895 // Only look under "start" when loading packages wasn't done yet.
896 if (round == 1 && did_source_packages)
897 continue;
898
899 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
900 pat = alloc(len);
901 if (pat == NULL)
902 return;
903 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
904 // The first round don't give a "not found" error, in the second round
905 // only when nothing was found in the first round.
906 res = do_in_path(p_pp, (char_u *)pat,
907 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
908 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
909 vim_free(pat);
910 }
911}
912#endif
913
914/*
Bram Moolenaar26262f82019-09-04 20:59:15 +0200915 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
916 * list of file names in allocated memory.
917 */
918 void
919remove_duplicates(garray_T *gap)
920{
921 int i;
922 int j;
923 char_u **fnames = (char_u **)gap->ga_data;
924
925 sort_strings(fnames, gap->ga_len);
926 for (i = gap->ga_len - 1; i > 0; --i)
927 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
928 {
929 vim_free(fnames[i]);
930 for (j = i + 1; j < gap->ga_len; ++j)
931 fnames[j - 1] = fnames[j];
932 --gap->ga_len;
933 }
934}
935
936/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200937 * Expand color scheme, compiler or filetype names.
938 * Search from 'runtimepath':
939 * 'runtimepath'/{dirnames}/{pat}.vim
940 * When "flags" has DIP_START: search also from 'start' of 'packpath':
941 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
942 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
943 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
944 * "dirnames" is an array with one or more directory names.
945 */
946 int
947ExpandRTDir(
948 char_u *pat,
949 int flags,
950 int *num_file,
951 char_u ***file,
952 char *dirnames[])
953{
954 char_u *s;
955 char_u *e;
956 char_u *match;
957 garray_T ga;
958 int i;
959 int pat_len;
960
961 *num_file = 0;
962 *file = NULL;
963 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000964 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200965
966 for (i = 0; dirnames[i] != NULL; ++i)
967 {
968 s = alloc(STRLEN(dirnames[i]) + pat_len + 7);
969 if (s == NULL)
970 {
971 ga_clear_strings(&ga);
972 return FAIL;
973 }
974 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
975 globpath(p_rtp, s, &ga, 0);
976 vim_free(s);
977 }
978
979 if (flags & DIP_START) {
980 for (i = 0; dirnames[i] != NULL; ++i)
981 {
982 s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
983 if (s == NULL)
984 {
985 ga_clear_strings(&ga);
986 return FAIL;
987 }
988 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
989 globpath(p_pp, s, &ga, 0);
990 vim_free(s);
991 }
992 }
993
994 if (flags & DIP_OPT) {
995 for (i = 0; dirnames[i] != NULL; ++i)
996 {
997 s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
998 if (s == NULL)
999 {
1000 ga_clear_strings(&ga);
1001 return FAIL;
1002 }
1003 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
1004 globpath(p_pp, s, &ga, 0);
1005 vim_free(s);
1006 }
1007 }
1008
1009 for (i = 0; i < ga.ga_len; ++i)
1010 {
1011 match = ((char_u **)ga.ga_data)[i];
1012 s = match;
1013 e = s + STRLEN(s);
1014 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
1015 {
1016 e -= 4;
1017 for (s = e; s > match; MB_PTR_BACK(match, s))
1018 if (s < match || vim_ispathsep(*s))
1019 break;
1020 ++s;
1021 *e = NUL;
1022 mch_memmove(match, s, e - s + 1);
1023 }
1024 }
1025
1026 if (ga.ga_len == 0)
1027 return FAIL;
1028
1029 // Sort and remove duplicates which can happen when specifying multiple
1030 // directories in dirnames.
1031 remove_duplicates(&ga);
1032
1033 *file = ga.ga_data;
1034 *num_file = ga.ga_len;
1035 return OK;
1036}
1037
1038/*
1039 * Expand loadplugin names:
1040 * 'packpath'/pack/ * /opt/{pat}
1041 */
1042 int
1043ExpandPackAddDir(
1044 char_u *pat,
1045 int *num_file,
1046 char_u ***file)
1047{
1048 char_u *s;
1049 char_u *e;
1050 char_u *match;
1051 garray_T ga;
1052 int i;
1053 int pat_len;
1054
1055 *num_file = 0;
1056 *file = NULL;
1057 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001058 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001059
1060 s = alloc(pat_len + 26);
1061 if (s == NULL)
1062 {
1063 ga_clear_strings(&ga);
1064 return FAIL;
1065 }
1066 sprintf((char *)s, "pack/*/opt/%s*", pat);
1067 globpath(p_pp, s, &ga, 0);
1068 vim_free(s);
1069
1070 for (i = 0; i < ga.ga_len; ++i)
1071 {
1072 match = ((char_u **)ga.ga_data)[i];
1073 s = gettail(match);
1074 e = s + STRLEN(s);
1075 mch_memmove(match, s, e - s + 1);
1076 }
1077
1078 if (ga.ga_len == 0)
1079 return FAIL;
1080
1081 // Sort and remove duplicates which can happen when specifying multiple
1082 // directories in dirnames.
1083 remove_duplicates(&ga);
1084
1085 *file = ga.ga_data;
1086 *num_file = ga.ga_len;
1087 return OK;
1088}
1089
1090 static void
1091cmd_source(char_u *fname, exarg_T *eap)
1092{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001093 int clearvars = FALSE;
1094
1095 if (*fname != NUL && STRNCMP(fname, "++clear", 7) == 0)
1096 {
1097 // ++clear argument is supplied
1098 clearvars = TRUE;
1099 fname = fname + 7;
1100 if (*fname != NUL)
1101 {
1102 semsg(_(e_invalid_argument_str), eap->arg);
1103 return;
1104 }
1105 }
1106
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001107 if (*fname != NUL && eap != NULL && eap->addr_count > 0)
1108 {
1109 // if a filename is specified to :source, then a range is not allowed
1110 emsg(_(e_no_range_allowed));
1111 return;
1112 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001113
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001114 if (eap != NULL && *fname == NUL)
1115 {
1116 if (eap->forceit)
1117 // a file name is needed to source normal mode commands
1118 emsg(_(e_argument_required));
1119 else
1120 // source ex commands from the current buffer
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001121 do_source_ext(NULL, FALSE, FALSE, NULL, eap, clearvars);
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001122 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001123 else if (eap != NULL && eap->forceit)
1124 // ":source!": read Normal mode commands
1125 // Need to execute the commands directly. This is required at least
1126 // for:
1127 // - ":g" command busy
1128 // - after ":argdo", ":windo" or ":bufdo"
1129 // - another command follows
1130 // - inside a loop
1131 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
1132#ifdef FEAT_EVAL
1133 || eap->cstack->cs_idx >= 0
1134#endif
1135 );
1136
1137 // ":source" read ex commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001138 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001139 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001140}
1141
1142/*
1143 * ":source {fname}"
1144 */
1145 void
1146ex_source(exarg_T *eap)
1147{
1148#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02001149 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001150 {
1151 char_u *fname = NULL;
1152
1153 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
1154 NULL, NULL,
1155 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1156 if (fname != NULL)
1157 {
1158 cmd_source(fname, eap);
1159 vim_free(fname);
1160 }
1161 }
1162 else
1163#endif
1164 cmd_source(eap->arg, eap);
1165}
1166
1167#if defined(FEAT_EVAL) || defined(PROTO)
1168/*
1169 * ":options"
1170 */
1171 void
1172ex_options(
1173 exarg_T *eap UNUSED)
1174{
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001175 char_u buf[500];
1176 int multi_mods = 0;
1177
1178 buf[0] = NUL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001179 (void)add_win_cmd_modifers(buf, &cmdmod, &multi_mods);
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001180
1181 vim_setenv((char_u *)"OPTWIN_CMD", buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001182 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
1183}
1184#endif
1185
1186/*
1187 * ":source" and associated commands.
1188 */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001189
1190#ifdef FEAT_EVAL
1191/*
1192 * Return the address holding the next breakpoint line for a source cookie.
1193 */
1194 linenr_T *
1195source_breakpoint(void *cookie)
1196{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001197 return &((source_cookie_T *)cookie)->breakpoint;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001198}
1199
1200/*
1201 * Return the address holding the debug tick for a source cookie.
1202 */
1203 int *
1204source_dbg_tick(void *cookie)
1205{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001206 return &((source_cookie_T *)cookie)->dbg_tick;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001207}
1208
1209/*
1210 * Return the nesting level for a source cookie.
1211 */
1212 int
1213source_level(void *cookie)
1214{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001215 return ((source_cookie_T *)cookie)->level;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001216}
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001217
1218/*
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02001219 * Return the readahead line. Note that the pointer may become invalid when
1220 * getting the next line, if it's concatenated with the next one.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001221 */
1222 char_u *
1223source_nextline(void *cookie)
1224{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001225 return ((source_cookie_T *)cookie)->nextline;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001226}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001227#endif
1228
1229#if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC)
1230# define USE_FOPEN_NOINH
1231/*
1232 * Special function to open a file without handle inheritance.
1233 * When possible the handle is closed on exec().
1234 */
1235 static FILE *
1236fopen_noinh_readbin(char *filename)
1237{
1238# ifdef MSWIN
1239 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
1240# else
1241 int fd_tmp = mch_open(filename, O_RDONLY, 0);
1242# endif
1243
1244 if (fd_tmp == -1)
1245 return NULL;
1246
1247# ifdef HAVE_FD_CLOEXEC
1248 {
1249 int fdflags = fcntl(fd_tmp, F_GETFD);
1250 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1251 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
1252 }
1253# endif
1254
1255 return fdopen(fd_tmp, READBIN);
1256}
1257#endif
1258
1259/*
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001260 * Initialization for sourcing lines from the current buffer. Reads all the
1261 * lines from the buffer and stores it in the cookie grow array.
1262 * Returns a pointer to the name ":source buffer=<n>" on success and NULL on
1263 * failure.
1264 */
1265 static char_u *
1266do_source_buffer_init(source_cookie_T *sp, exarg_T *eap)
1267{
1268 linenr_T curr_lnum;
1269 char_u *line = NULL;
1270 char_u *fname;
1271
1272 CLEAR_FIELD(*sp);
1273
1274 if (curbuf == NULL)
1275 return NULL;
1276
1277 // Use ":source buffer=<num>" as the script name
1278 vim_snprintf((char *)IObuff, IOSIZE, ":source buffer=%d", curbuf->b_fnum);
1279 fname = vim_strsave(IObuff);
1280 if (fname == NULL)
1281 return NULL;
1282
1283 ga_init2(&sp->buflines, sizeof(char_u *), 100);
1284
1285 // Copy the lines from the buffer into a grow array
1286 for (curr_lnum = eap->line1; curr_lnum <= eap->line2; curr_lnum++)
1287 {
1288 line = vim_strsave(ml_get(curr_lnum));
1289 if (line == NULL)
1290 goto errret;
1291 if (ga_add_string(&sp->buflines, line) == FAIL)
1292 goto errret;
1293 line = NULL;
1294 }
1295 sp->buf_lnum = 0;
1296 sp->source_from_buf = TRUE;
1297
1298 return fname;
1299
1300errret:
1301 vim_free(fname);
1302 vim_free(line);
1303 ga_clear_strings(&sp->buflines);
1304 return NULL;
1305}
1306
1307/*
1308 * Read the file "fname" and execute its lines as EX commands.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1310 * again.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001311 *
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001312 * The 'eap' argument is used when sourcing lines from a buffer instead of a
1313 * file.
1314 *
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001315 * If 'clearvars' is TRUE, then for scripts which are loaded more than
1316 * once, clear all the functions and variables previously defined in that
1317 * script.
1318 *
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001319 * This function may be called recursively!
1320 *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 * Return FAIL if file could not be opened, OK otherwise.
1322 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001323 */
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001324 static int
1325do_source_ext(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001326 char_u *fname,
1327 int check_other, // check for .vimrc and _vimrc
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 int is_vimrc, // DOSO_ value
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001329 int *ret_sid UNUSED,
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001330 exarg_T *eap,
1331 int clearvars UNUSED)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001332{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001333 source_cookie_T cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001334 char_u *p;
1335 char_u *fname_exp;
1336 char_u *firstline = NULL;
1337 int retval = FAIL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001338 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001339#ifdef FEAT_EVAL
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001340 funccal_entry_T funccalp_entry;
1341 int save_debug_break_level = debug_break_level;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 int sid;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001343 scriptitem_T *si = NULL;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001344 int save_estack_compiling = estack_compiling;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001345#endif
1346#ifdef STARTUPTIME
1347 struct timeval tv_rel;
1348 struct timeval tv_start;
1349#endif
1350#ifdef FEAT_PROFILE
1351 proftime_T wait_start;
1352#endif
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001353 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001354 int trigger_source_post = FALSE;
Bram Moolenaare31ee862020-01-07 20:59:34 +01001355 ESTACK_CHECK_DECLARATION
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001356
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001357 CLEAR_FIELD(cookie);
1358 if (fname == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001359 {
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001360 // sourcing lines from a buffer
1361 fname_exp = do_source_buffer_init(&cookie, eap);
1362 if (fname_exp == NULL)
1363 return FAIL;
1364 }
1365 else
1366 {
1367 p = expand_env_save(fname);
1368 if (p == NULL)
1369 return retval;
1370 fname_exp = fix_fname(p);
1371 vim_free(p);
1372 if (fname_exp == NULL)
1373 return retval;
1374 if (mch_isdir(fname_exp))
1375 {
1376 smsg(_("Cannot source a directory: \"%s\""), fname);
1377 goto theend;
1378 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001379 }
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001380#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001381 estack_compiling = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001382
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383 // See if we loaded this script before.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001384 sid = find_script_by_name(fname_exp);
Bram Moolenaarf479cac2022-01-12 12:54:55 +00001385 if (sid > 0 && ret_sid != NULL
1386 && SCRIPT_ITEM(sid)->sn_state != SN_STATE_NOT_LOADED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 {
1388 // Already loaded and no need to load again, return here.
1389 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001390 retval = OK;
1391 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001392 }
1393#endif
1394
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001395 // Apply SourceCmd autocommands, they should get the file and source it.
1396 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1397 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1398 FALSE, curbuf))
1399 {
1400#ifdef FEAT_EVAL
1401 retval = aborting() ? FAIL : OK;
1402#else
1403 retval = OK;
1404#endif
1405 if (retval == OK)
1406 // Apply SourcePost autocommands.
1407 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1408 FALSE, curbuf);
1409 goto theend;
1410 }
1411
1412 // Apply SourcePre autocommands, they may get the file.
1413 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1414
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001415 if (!cookie.source_from_buf)
1416 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001417#ifdef USE_FOPEN_NOINH
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001418 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001419#else
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001420 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001421#endif
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001422 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001423 if (cookie.fp == NULL && check_other)
1424 {
1425 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1426 // and ".exrc" by "_exrc" or vice versa.
1427 p = gettail(fname_exp);
1428 if ((*p == '.' || *p == '_')
1429 && (STRICMP(p + 1, "vimrc") == 0
1430 || STRICMP(p + 1, "gvimrc") == 0
1431 || STRICMP(p + 1, "exrc") == 0))
1432 {
1433 if (*p == '_')
1434 *p = '.';
1435 else
1436 *p = '_';
1437#ifdef USE_FOPEN_NOINH
1438 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1439#else
1440 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1441#endif
1442 }
1443 }
1444
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001445 if (cookie.fp == NULL && !cookie.source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001446 {
1447 if (p_verbose > 0)
1448 {
1449 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001450 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001451 smsg(_("could not source \"%s\""), fname);
1452 else
1453 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001454 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001455 verbose_leave();
1456 }
1457 goto theend;
1458 }
1459
1460 // The file exists.
1461 // - In verbose mode, give a message.
1462 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1463 if (p_verbose > 1)
1464 {
1465 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001466 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001467 smsg(_("sourcing \"%s\""), fname);
1468 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001469 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001470 verbose_leave();
1471 }
1472 if (is_vimrc == DOSO_VIMRC)
1473 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1474 else if (is_vimrc == DOSO_GVIMRC)
1475 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1476
1477#ifdef USE_CRNL
1478 // If no automatic file format: Set default to CR-NL.
1479 if (*p_ffs == NUL)
1480 cookie.fileformat = EOL_DOS;
1481 else
1482 cookie.fileformat = EOL_UNKNOWN;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001483#endif
1484
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001485 if (fname == NULL)
1486 // When sourcing a range of lines from a buffer, use the buffer line
1487 // number.
1488 cookie.sourcing_lnum = eap->line1 - 1;
1489 else
1490 cookie.sourcing_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001491
1492#ifdef FEAT_EVAL
1493 // Check if this script has a breakpoint.
1494 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1495 cookie.fname = fname_exp;
1496 cookie.dbg_tick = debug_tick;
1497
1498 cookie.level = ex_nesting_level;
1499#endif
1500
1501 // Keep the sourcing name/lnum, for recursive calls.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001502 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001503 ESTACK_CHECK_SETUP
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001504
1505#ifdef STARTUPTIME
1506 if (time_fd != NULL)
1507 time_push(&tv_rel, &tv_start);
1508#endif
1509
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001510 // "legacy" does not apply to commands in the script
1511 sticky_cmdmod_flags = 0;
1512
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001513 save_current_sctx = current_sctx;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001514 if (cmdmod.cmod_flags & CMOD_VIM9CMD)
1515 // When the ":vim9cmd" command modifier is used, source the script as a
1516 // Vim9 script.
1517 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1518 else
1519 current_sctx.sc_version = 1; // default script version
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001520
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001521#ifdef FEAT_EVAL
1522# ifdef FEAT_PROFILE
1523 if (do_profiling == PROF_YES)
1524 prof_child_enter(&wait_start); // entering a child now
1525# endif
1526
1527 // Don't use local function variables, if called from a function.
1528 // Also starts profiling timer for nested script.
1529 save_funccal(&funccalp_entry);
1530
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001531 current_sctx.sc_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001532
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001533 // Check if this script was sourced before to find its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001534 // Always use a new sequence number.
1535 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001537 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538 hashtab_T *ht;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001539 int todo;
1540 hashitem_T *hi;
1541 dictitem_T *di;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001542
1543 // loading the same script again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001545 si = SCRIPT_ITEM(sid);
1546 if (si->sn_state == SN_STATE_NOT_LOADED)
1547 {
1548 // this script was found but not loaded yet
1549 si->sn_state = SN_STATE_NEW;
1550 }
1551 else
1552 {
1553 si->sn_state = SN_STATE_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001555 if (!clearvars)
1556 {
1557 // Script-local variables remain but "const" can be set again.
1558 // In Vim9 script variables will be cleared when "vim9script"
1559 // is encountered without the "noclear" argument.
1560 ht = &SCRIPT_VARS(sid);
1561 todo = (int)ht->ht_used;
1562 for (hi = ht->ht_array; todo > 0; ++hi)
1563 if (!HASHITEM_EMPTY(hi))
1564 {
1565 --todo;
1566 di = HI2DI(hi);
1567 di->di_flags |= DI_FLAGS_RELOAD;
1568 }
1569 // imports can be redefined once
1570 mark_imports_for_reload(sid);
1571 }
1572 else
1573 clear_vim9_scriptlocal_vars(sid);
Bram Moolenaar0123cc12021-02-07 17:17:58 +01001574
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001575 // reset version, "vim9script" may have been added or removed.
1576 si->sn_version = 1;
1577 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001578 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001580 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001581 int error = OK;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001582
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001583 // It's new, generate a new SID and initialize the scriptitem.
1584 current_sctx.sc_sid = get_new_scriptitem(&error);
1585 if (error == FAIL)
1586 goto almosttheend;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001587 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001588 si->sn_name = fname_exp;
1589 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 if (ret_sid != NULL)
1591 *ret_sid = current_sctx.sc_sid;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001592
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001593 // Remember the "is_vimrc" flag for when the file is sourced again.
1594 si->sn_is_vimrc = is_vimrc;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001595 }
1596
1597# ifdef FEAT_PROFILE
1598 if (do_profiling == PROF_YES)
1599 {
1600 int forceit;
1601
1602 // Check if we do profiling for this script.
1603 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
1604 {
1605 script_do_profile(si);
1606 si->sn_pr_force = forceit;
1607 }
1608 if (si->sn_prof_on)
1609 {
1610 ++si->sn_pr_count;
1611 profile_start(&si->sn_pr_start);
1612 profile_zero(&si->sn_pr_children);
1613 }
1614 }
1615# endif
1616#endif
1617
1618 cookie.conv.vc_type = CONV_NONE; // no conversion
1619
1620 // Read the first line so we can check for a UTF-8 BOM.
1621 firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1622 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1623 && firstline[1] == 0xbb && firstline[2] == 0xbf)
1624 {
1625 // Found BOM; setup conversion, skip over BOM and recode the line.
1626 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1627 p = string_convert(&cookie.conv, firstline + 3, NULL);
1628 if (p == NULL)
1629 p = vim_strsave(firstline + 3);
1630 if (p != NULL)
1631 {
1632 vim_free(firstline);
1633 firstline = p;
1634 }
1635 }
1636
1637 // Call do_cmdline, which will call getsourceline() to get the lines.
1638 do_cmdline(firstline, getsourceline, (void *)&cookie,
1639 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1640 retval = OK;
1641
1642#ifdef FEAT_PROFILE
1643 if (do_profiling == PROF_YES)
1644 {
1645 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001646 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001647 if (si->sn_prof_on)
1648 {
1649 profile_end(&si->sn_pr_start);
1650 profile_sub_wait(&wait_start, &si->sn_pr_start);
1651 profile_add(&si->sn_pr_total, &si->sn_pr_start);
1652 profile_self(&si->sn_pr_self, &si->sn_pr_start,
1653 &si->sn_pr_children);
1654 }
1655 }
1656#endif
1657
1658 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001659 emsg(_(e_interrupted));
Bram Moolenaare31ee862020-01-07 20:59:34 +01001660 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001661 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001662 if (p_verbose > 1)
1663 {
1664 verbose_enter();
1665 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001666 if (SOURCING_NAME != NULL)
1667 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001668 verbose_leave();
1669 }
1670#ifdef STARTUPTIME
1671 if (time_fd != NULL)
1672 {
1673 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1674 time_msg((char *)IObuff, &tv_start);
1675 time_pop(&tv_rel);
1676 }
1677#endif
1678
1679 if (!got_int)
1680 trigger_source_post = TRUE;
1681
1682#ifdef FEAT_EVAL
1683 // After a "finish" in debug mode, need to break at first command of next
1684 // sourced file.
1685 if (save_debug_break_level > ex_nesting_level
1686 && debug_break_level == ex_nesting_level)
1687 ++debug_break_level;
1688#endif
1689
1690#ifdef FEAT_EVAL
1691almosttheend:
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001692 // If "sn_save_cpo" is set that means we encountered "vim9script": restore
1693 // 'cpoptions', unless in the main .vimrc file.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001695 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001696 if (si->sn_save_cpo != NULL && si->sn_is_vimrc == DOSO_NONE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 {
Bram Moolenaar3e191692021-03-17 17:46:00 +01001698 if (STRCMP(p_cpo, CPO_VIM) != 0)
1699 {
1700 char_u *f;
1701 char_u *t;
1702
1703 // 'cpo' was changed in the script. Apply the same change to the
1704 // saved value, if possible.
1705 for (f = (char_u *)CPO_VIM; *f != NUL; ++f)
1706 if (vim_strchr(p_cpo, *f) == NULL
1707 && (t = vim_strchr(si->sn_save_cpo, *f)) != NULL)
1708 // flag was removed, also remove it from the saved 'cpo'
1709 mch_memmove(t, t + 1, STRLEN(t));
1710 for (f = p_cpo; *f != NUL; ++f)
1711 if (vim_strchr((char_u *)CPO_VIM, *f) == NULL
1712 && vim_strchr(si->sn_save_cpo, *f) == NULL)
1713 {
1714 // flag was added, also add it to the saved 'cpo'
1715 t = alloc(STRLEN(si->sn_save_cpo) + 2);
1716 if (t != NULL)
1717 {
1718 *t = *f;
1719 STRCPY(t + 1, si->sn_save_cpo);
1720 vim_free(si->sn_save_cpo);
1721 si->sn_save_cpo = t;
1722 }
1723 }
1724 }
Bram Moolenaar37294bd2021-03-10 13:40:08 +01001725 set_option_value((char_u *)"cpo", 0L, si->sn_save_cpo, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001726 }
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001727 VIM_CLEAR(si->sn_save_cpo);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001729 restore_funccal();
1730# ifdef FEAT_PROFILE
1731 if (do_profiling == PROF_YES)
1732 prof_child_exit(&wait_start); // leaving a child now
1733# endif
1734#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001735 current_sctx = save_current_sctx;
1736
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001737 if (cookie.fp != NULL)
1738 fclose(cookie.fp);
1739 if (cookie.source_from_buf)
1740 ga_clear_strings(&cookie.buflines);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001741 vim_free(cookie.nextline);
1742 vim_free(firstline);
1743 convert_setup(&cookie.conv, NULL, NULL);
1744
1745 if (trigger_source_post)
1746 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1747
1748theend:
1749 vim_free(fname_exp);
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001750 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001751#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001752 estack_compiling = save_estack_compiling;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001753#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001754 return retval;
1755}
1756
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001757 int
1758do_source(
1759 char_u *fname,
1760 int check_other, // check for .vimrc and _vimrc
1761 int is_vimrc, // DOSO_ value
1762 int *ret_sid UNUSED)
1763{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001764 return do_source_ext(fname, check_other, is_vimrc, ret_sid, NULL, FALSE);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001765}
1766
1767
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001768#if defined(FEAT_EVAL) || defined(PROTO)
1769
1770/*
1771 * ":scriptnames"
1772 */
1773 void
1774ex_scriptnames(exarg_T *eap)
1775{
1776 int i;
1777
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001778 if (eap->addr_count > 0 || *eap->arg != NUL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001779 {
1780 // :script {scriptId}: edit the script
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001781 if (eap->addr_count > 0 && !SCRIPT_ID_VALID(eap->line2))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001782 emsg(_(e_invalid_argument));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001783 else
1784 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001785 if (eap->addr_count > 0)
1786 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
1787 else
1788 {
1789 expand_env(eap->arg, NameBuff, MAXPATHL);
1790 eap->arg = NameBuff;
1791 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001792 do_exedit(eap, NULL);
1793 }
1794 return;
1795 }
1796
1797 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar6079da72022-01-18 14:16:59 +00001798 {
1799 scriptitem_T *si = SCRIPT_ITEM(i);
1800
1801 if (si->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001802 {
Bram Moolenaar6079da72022-01-18 14:16:59 +00001803 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
1804 vim_snprintf((char *)IObuff, IOSIZE, "%3d%s: %s",
1805 i,
1806 si->sn_state == SN_STATE_NOT_LOADED ? " A" : "",
1807 NameBuff);
Bram Moolenaar769f5892022-02-09 14:31:05 +00001808 if (!message_filtered(IObuff))
1809 {
1810 msg_putchar('\n');
1811 msg_outtrans(IObuff);
1812 out_flush(); // output one line at a time
1813 ui_breakcheck();
1814 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001815 }
Bram Moolenaar6079da72022-01-18 14:16:59 +00001816 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001817}
1818
1819# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1820/*
1821 * Fix slashes in the list of script names for 'shellslash'.
1822 */
1823 void
1824scriptnames_slash_adjust(void)
1825{
1826 int i;
1827
1828 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001829 if (SCRIPT_ITEM(i)->sn_name != NULL)
1830 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001831}
1832# endif
1833
1834/*
1835 * Get a pointer to a script name. Used for ":verbose set".
Bram Moolenaar74667062020-12-28 15:41:41 +01001836 * Message appended to "Last set from "
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001837 */
1838 char_u *
1839get_scriptname(scid_T id)
1840{
1841 if (id == SID_MODELINE)
1842 return (char_u *)_("modeline");
1843 if (id == SID_CMDARG)
1844 return (char_u *)_("--cmd argument");
1845 if (id == SID_CARG)
1846 return (char_u *)_("-c argument");
1847 if (id == SID_ENV)
1848 return (char_u *)_("environment variable");
1849 if (id == SID_ERROR)
1850 return (char_u *)_("error handler");
Bram Moolenaar74667062020-12-28 15:41:41 +01001851 if (id == SID_WINLAYOUT)
1852 return (char_u *)_("changed window size");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001853 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001854}
1855
1856# if defined(EXITFREE) || defined(PROTO)
1857 void
1858free_scriptnames(void)
1859{
1860 int i;
1861
1862 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02001863 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001864 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001866 // the variables themselves are cleared in evalvars_clear()
1867 vim_free(si->sn_vars);
1868
1869 vim_free(si->sn_name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001870 free_imports_and_script_vars(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001871 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02001872# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001873 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02001874# endif
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00001875 vim_free(si->sn_autoload_prefix);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001876 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02001877 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001878 ga_clear(&script_items);
1879}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001880
1881 void
1882free_autoload_scriptnames(void)
1883{
1884 ga_clear_strings(&ga_loaded);
1885}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001886# endif
1887
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001888 linenr_T
Bram Moolenaar66250c92020-08-20 15:02:42 +02001889get_sourced_lnum(
1890 char_u *(*fgetline)(int, void *, int, getline_opt_T),
1891 void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001892{
1893 return fgetline == getsourceline
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001894 ? ((source_cookie_T *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001895 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001896}
Dominique Pelle748b3082022-01-08 12:41:16 +00001897#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001898
1899 static char_u *
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001900get_one_sourceline(source_cookie_T *sp)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001901{
1902 garray_T ga;
1903 int len;
1904 int c;
1905 char_u *buf;
1906#ifdef USE_CRNL
1907 int has_cr; // CR-LF found
1908#endif
1909 int have_read = FALSE;
1910
1911 // use a growarray to store the sourced line
1912 ga_init2(&ga, 1, 250);
1913
1914 // Loop until there is a finished line (or end-of-file).
1915 ++sp->sourcing_lnum;
1916 for (;;)
1917 {
1918 // make room to read at least 120 (more) characters
1919 if (ga_grow(&ga, 120) == FAIL)
1920 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001921 if (sp->source_from_buf)
1922 {
1923 if (sp->buf_lnum >= sp->buflines.ga_len)
1924 break; // all the lines are processed
1925 ga_concat(&ga, ((char_u **)sp->buflines.ga_data)[sp->buf_lnum]);
1926 sp->buf_lnum++;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01001927 if (ga_grow(&ga, 1) == FAIL)
1928 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001929 buf = (char_u *)ga.ga_data;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01001930 buf[ga.ga_len++] = NUL;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001931 }
1932 else
1933 {
1934 buf = (char_u *)ga.ga_data;
1935 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
1936 sp->fp) == NULL)
1937 break;
1938 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001939 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
1940#ifdef USE_CRNL
1941 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
1942 // CTRL-Z by its own, or after a NL.
1943 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
1944 && sp->fileformat == EOL_DOS
1945 && buf[len - 1] == Ctrl_Z)
1946 {
1947 buf[len - 1] = NUL;
1948 break;
1949 }
1950#endif
1951
1952 have_read = TRUE;
1953 ga.ga_len = len;
1954
1955 // If the line was longer than the buffer, read more.
1956 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
1957 continue;
1958
1959 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
1960 {
1961#ifdef USE_CRNL
1962 has_cr = (len >= 2 && buf[len - 2] == '\r');
1963 if (sp->fileformat == EOL_UNKNOWN)
1964 {
1965 if (has_cr)
1966 sp->fileformat = EOL_DOS;
1967 else
1968 sp->fileformat = EOL_UNIX;
1969 }
1970
1971 if (sp->fileformat == EOL_DOS)
1972 {
1973 if (has_cr) // replace trailing CR
1974 {
1975 buf[len - 2] = '\n';
1976 --len;
1977 --ga.ga_len;
1978 }
1979 else // lines like ":map xx yy^M" will have failed
1980 {
1981 if (!sp->error)
1982 {
1983 msg_source(HL_ATTR(HLF_W));
1984 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
1985 }
1986 sp->error = TRUE;
1987 sp->fileformat = EOL_UNIX;
1988 }
1989 }
1990#endif
1991 // The '\n' is escaped if there is an odd number of ^V's just
1992 // before it, first set "c" just before the 'V's and then check
1993 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
1994 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
1995 ;
1996 if ((len & 1) != (c & 1)) // escaped NL, read more
1997 {
1998 ++sp->sourcing_lnum;
1999 continue;
2000 }
2001
2002 buf[len - 1] = NUL; // remove the NL
2003 }
2004
2005 // Check for ^C here now and then, so recursive :so can be broken.
2006 line_breakcheck();
2007 break;
2008 }
2009
2010 if (have_read)
2011 return (char_u *)ga.ga_data;
2012
2013 vim_free(ga.ga_data);
2014 return NULL;
2015}
2016
2017/*
2018 * Get one full line from a sourced file.
2019 * Called by do_cmdline() when it's called from do_source().
2020 *
2021 * Return a pointer to the line in allocated memory.
2022 * Return NULL for end-of-file or some error.
2023 */
2024 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02002025getsourceline(
2026 int c UNUSED,
2027 void *cookie,
2028 int indent UNUSED,
2029 getline_opt_T options)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002030{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002031 source_cookie_T *sp = (source_cookie_T *)cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002032 char_u *line;
2033 char_u *p;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002034 int do_vim9_all = in_vim9script()
2035 && options == GETLINE_CONCAT_ALL;
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002036 int do_bar_cont = do_vim9_all
2037 || options == GETLINE_CONCAT_CONTBAR;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002038
2039#ifdef FEAT_EVAL
2040 // If breakpoints have been added/deleted need to check for it.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002041 if ((sp->dbg_tick < debug_tick) && !sp->source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002042 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002043 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002044 sp->dbg_tick = debug_tick;
2045 }
2046# ifdef FEAT_PROFILE
2047 if (do_profiling == PROF_YES)
2048 script_line_end();
2049# endif
2050#endif
2051
2052 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002053 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002054
2055 // Get current line. If there is a read-ahead line, use it, otherwise get
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002056 // one now. "fp" is NULL if actually using a string.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002057 if (sp->finished || (!sp->source_from_buf && sp->fp == NULL))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002058 line = NULL;
2059 else if (sp->nextline == NULL)
2060 line = get_one_sourceline(sp);
2061 else
2062 {
2063 line = sp->nextline;
2064 sp->nextline = NULL;
2065 ++sp->sourcing_lnum;
2066 }
2067#ifdef FEAT_PROFILE
2068 if (line != NULL && do_profiling == PROF_YES)
2069 script_line_start();
2070#endif
2071
2072 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
2073 // contain the 'C' flag.
Bram Moolenaar66250c92020-08-20 15:02:42 +02002074 if (line != NULL && options != GETLINE_NONE
2075 && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002076 {
Bram Moolenaar5072b472021-06-03 21:56:10 +02002077 int comment_char = in_vim9script() ? '#' : '"';
2078
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002079 // compensate for the one line read-ahead
2080 --sp->sourcing_lnum;
2081
2082 // Get the next line and concatenate it when it starts with a
2083 // backslash. We always need to read the next line, keep it in
2084 // sp->nextline.
2085 /* Also check for a comment in between continuation lines: "\ */
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002086 // Also check for a Vim9 comment, empty line, line starting with '|',
2087 // but not "||".
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002088 sp->nextline = get_one_sourceline(sp);
2089 if (sp->nextline != NULL
2090 && (*(p = skipwhite(sp->nextline)) == '\\'
Bram Moolenaar5072b472021-06-03 21:56:10 +02002091 || (p[0] == comment_char
2092 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002093 || (do_vim9_all && (*p == NUL
2094 || vim9_comment_start(p)))
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002095 || (do_bar_cont && p[0] == '|' && p[1] != '|')))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002096 {
2097 garray_T ga;
2098
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002099 ga_init2(&ga, sizeof(char_u), 400);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002100 ga_concat(&ga, line);
2101 if (*p == '\\')
2102 ga_concat(&ga, p + 1);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002103 else if (*p == '|')
2104 {
2105 ga_concat(&ga, (char_u *)" ");
2106 ga_concat(&ga, p);
2107 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002108 for (;;)
2109 {
2110 vim_free(sp->nextline);
2111 sp->nextline = get_one_sourceline(sp);
2112 if (sp->nextline == NULL)
2113 break;
2114 p = skipwhite(sp->nextline);
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002115 if (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|'))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002116 {
2117 // Adjust the growsize to the current length to speed up
2118 // concatenating many lines.
2119 if (ga.ga_len > 400)
2120 {
2121 if (ga.ga_len > 8000)
2122 ga.ga_growsize = 8000;
2123 else
2124 ga.ga_growsize = ga.ga_len;
2125 }
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002126 if (*p == '\\')
2127 ga_concat(&ga, p + 1);
2128 else
2129 {
2130 ga_concat(&ga, (char_u *)" ");
2131 ga_concat(&ga, p);
2132 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002133 }
Bram Moolenaar5072b472021-06-03 21:56:10 +02002134 else if (!(p[0] == (comment_char)
Bram Moolenaar0f37e352021-06-02 15:28:15 +02002135 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002136 && !(do_vim9_all && (*p == NUL || vim9_comment_start(p))))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002137 break;
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002138 /* drop a # comment or "\ comment line */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002139 }
2140 ga_append(&ga, NUL);
2141 vim_free(line);
2142 line = ga.ga_data;
2143 }
2144 }
2145
2146 if (line != NULL && sp->conv.vc_type != CONV_NONE)
2147 {
2148 char_u *s;
2149
2150 // Convert the encoding of the script line.
2151 s = string_convert(&sp->conv, line, NULL);
2152 if (s != NULL)
2153 {
2154 vim_free(line);
2155 line = s;
2156 }
2157 }
2158
2159#ifdef FEAT_EVAL
2160 // Did we encounter a breakpoint?
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002161 if (!sp->source_from_buf && sp->breakpoint != 0
2162 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002163 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002164 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002165 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002166 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002167 sp->dbg_tick = debug_tick;
2168 }
2169#endif
2170
2171 return line;
2172}
2173
2174/*
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002175 * Returns TRUE if sourcing a script either from a file or a buffer.
2176 * Otherwise returns FALSE.
2177 */
2178 int
2179sourcing_a_script(exarg_T *eap)
2180{
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002181 return (getline_equal(eap->getline, eap->cookie, getsourceline));
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002182}
2183
2184/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002185 * ":scriptencoding": Set encoding conversion for a sourced script.
2186 */
2187 void
2188ex_scriptencoding(exarg_T *eap)
2189{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002190 source_cookie_T *sp;
2191 char_u *name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002192
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002193 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002194 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00002195 emsg(_(e_scriptencoding_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002196 return;
2197 }
2198
2199 if (*eap->arg != NUL)
2200 {
2201 name = enc_canonize(eap->arg);
2202 if (name == NULL) // out of memory
2203 return;
2204 }
2205 else
2206 name = eap->arg;
2207
2208 // Setup for conversion from the specified encoding to 'encoding'.
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002209 sp = (source_cookie_T *)getline_cookie(eap->getline, eap->cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002210 convert_setup(&sp->conv, name, p_enc);
2211
2212 if (name != eap->arg)
2213 vim_free(name);
2214}
2215
2216/*
2217 * ":scriptversion": Set Vim script version for a sourced script.
2218 */
2219 void
2220ex_scriptversion(exarg_T *eap UNUSED)
2221{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002222 int nr;
2223
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002224 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002225 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002226 emsg(_(e_scriptversion_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002227 return;
2228 }
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002229 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002231 emsg(_(e_cannot_use_scriptversion_after_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002232 return;
2233 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002234
2235 nr = getdigits(&eap->arg);
2236 if (nr == 0 || *eap->arg != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002237 emsg(_(e_invalid_argument));
Bram Moolenaar67979662020-06-20 22:50:47 +02002238 else if (nr > SCRIPT_VERSION_MAX)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002239 semsg(_(e_scriptversion_not_supported_nr), nr);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002240 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002241 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002242 current_sctx.sc_version = nr;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002243#ifdef FEAT_EVAL
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002244 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002245#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002246 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002247}
2248
2249#if defined(FEAT_EVAL) || defined(PROTO)
2250/*
2251 * ":finish": Mark a sourced file as finished.
2252 */
2253 void
2254ex_finish(exarg_T *eap)
2255{
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002256 if (sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002257 do_finish(eap, FALSE);
2258 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00002259 emsg(_(e_finish_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002260}
2261
2262/*
2263 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
2264 * Also called for a pending finish at the ":endtry" or after returning from
2265 * an extra do_cmdline(). "reanimate" is used in the latter case.
2266 */
2267 void
2268do_finish(exarg_T *eap, int reanimate)
2269{
2270 int idx;
2271
2272 if (reanimate)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002273 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002274 eap->cookie))->finished = FALSE;
2275
2276 // Cleanup (and inactivate) conditionals, but stop when a try conditional
2277 // not in its finally clause (which then is to be executed next) is found.
2278 // In this case, make the ":finish" pending for execution at the ":endtry".
2279 // Otherwise, finish normally.
2280 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
2281 if (idx >= 0)
2282 {
2283 eap->cstack->cs_pending[idx] = CSTP_FINISH;
2284 report_make_pending(CSTP_FINISH, NULL);
2285 }
2286 else
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002287 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002288 eap->cookie))->finished = TRUE;
2289}
2290
2291
2292/*
2293 * Return TRUE when a sourced file had the ":finish" command: Don't give error
2294 * message for missing ":endif".
2295 * Return FALSE when not sourcing a file.
2296 */
2297 int
2298source_finished(
Bram Moolenaar66250c92020-08-20 15:02:42 +02002299 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002300 void *cookie)
2301{
2302 return (getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002303 && ((source_cookie_T *)getline_cookie(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002304 fgetline, cookie))->finished);
2305}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002306
2307/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002308 * Find the path of a script below the "autoload" directory.
2309 * Returns NULL if there is no "/autoload/" in the script name.
2310 */
2311 char_u *
2312script_name_after_autoload(scriptitem_T *si)
2313{
2314 char_u *p = si->sn_name;
2315 char_u *res = NULL;
2316
2317 for (;;)
2318 {
2319 char_u *n = (char_u *)strstr((char *)p, "autoload");
2320
2321 if (n == NULL)
2322 break;
2323 if (n > p && vim_ispathsep(n[-1]) && vim_ispathsep(n[8]))
2324 res = n + 9;
2325 p = n + 8;
2326 }
2327 return res;
2328}
2329
2330/*
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002331 * For an autoload script "autoload/dir/script.vim" return the prefix
2332 * "dir#script#" in allocated memory.
2333 * Returns NULL if anything is wrong.
2334 */
2335 char_u *
2336get_autoload_prefix(scriptitem_T *si)
2337{
2338 char_u *p = script_name_after_autoload(si);
2339 char_u *prefix;
2340
2341 if (p == NULL)
2342 return NULL;
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002343 prefix = vim_strsave(p);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002344 if (prefix == NULL)
2345 return NULL;
2346
2347 // replace all '/' with '#' and locate ".vim" at the end
2348 for (p = prefix; *p != NUL; p += mb_ptr2len(p))
2349 {
2350 if (vim_ispathsep(*p))
2351 *p = '#';
2352 else if (STRCMP(p, ".vim") == 0)
2353 {
2354 p[0] = '#';
2355 p[1] = NUL;
2356 return prefix;
2357 }
2358 }
2359
2360 // did not find ".vim" at the end
2361 vim_free(prefix);
2362 return NULL;
2363}
2364
2365/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002366 * If in a Vim9 autoload script return "name" with the autoload prefix for the
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002367 * script. If successful the returned name is allocated.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002368 * Otherwise it returns "name" unmodified.
2369 */
2370 char_u *
2371may_prefix_autoload(char_u *name)
2372{
2373 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
2374 {
2375 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2376
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002377 if (si->sn_autoload_prefix != NULL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002378 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002379 char_u *basename = name;
2380 size_t len;
2381 char_u *res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002382
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002383 if (*name == K_SPECIAL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002384 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002385 char_u *p = vim_strchr(name, '_');
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002386
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002387 // skip over "<SNR>99_"
2388 if (p != NULL)
2389 basename = p + 1;
2390 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002391
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002392 len = STRLEN(si->sn_autoload_prefix) + STRLEN(basename) + 2;
2393 res = alloc(len);
2394 if (res != NULL)
2395 {
2396 vim_snprintf((char *)res, len, "%s%s",
2397 si->sn_autoload_prefix, basename);
2398 return res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002399 }
2400 }
2401 }
2402 return name;
2403}
2404
2405/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002406 * Return the autoload script name for a function or variable name.
2407 * Returns NULL when out of memory.
2408 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
2409 */
2410 char_u *
2411autoload_name(char_u *name)
2412{
2413 char_u *p, *q = NULL;
2414 char_u *scriptname;
2415
2416 // Get the script file name: replace '#' with '/', append ".vim".
2417 scriptname = alloc(STRLEN(name) + 14);
2418 if (scriptname == NULL)
2419 return NULL;
2420 STRCPY(scriptname, "autoload/");
Bram Moolenaara1773442020-08-12 15:21:22 +02002421 STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002422 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
2423 q = p, ++p)
2424 *p = '/';
2425 STRCPY(q, ".vim");
2426 return scriptname;
2427}
2428
2429/*
2430 * If "name" has a package name try autoloading the script for it.
2431 * Return TRUE if a package was loaded.
2432 */
2433 int
2434script_autoload(
2435 char_u *name,
2436 int reload) // load script again when already loaded
2437{
2438 char_u *p;
2439 char_u *scriptname, *tofree;
2440 int ret = FALSE;
2441 int i;
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002442 int ret_sid;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002443
2444 // If there is no '#' after name[0] there is no package name.
2445 p = vim_strchr(name, AUTOLOAD_CHAR);
2446 if (p == NULL || p == name)
2447 return FALSE;
2448
2449 tofree = scriptname = autoload_name(name);
2450 if (scriptname == NULL)
2451 return FALSE;
2452
2453 // Find the name in the list of previously loaded package names. Skip
2454 // "autoload/", it's always the same.
2455 for (i = 0; i < ga_loaded.ga_len; ++i)
2456 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2457 break;
2458 if (!reload && i < ga_loaded.ga_len)
2459 ret = FALSE; // was loaded already
2460 else
2461 {
2462 // Remember the name if it wasn't loaded already.
2463 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2464 {
2465 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2466 tofree = NULL;
2467 }
2468
2469 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002470 // Use "ret_sid" to avoid loading the same script again.
=?UTF-8?q?Bj=C3=B6rn=20Linse?=223a9502022-01-31 17:26:05 +00002471 if (source_in_path(p_rtp, scriptname, DIP_START, &ret_sid) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002472 ret = TRUE;
2473 }
2474
2475 vim_free(tofree);
2476 return ret;
2477}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002478#endif