blob: bdb7922363ac34a72c9dd4362e1787898093fa72 [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
LemonBoyeca7c602022-04-14 15:39:43 +0100160 // If evaluated in a function or autocommand, return the path of the script
161 // where it is defined, at script level the current script path is returned
LemonBoy6013d002022-04-09 21:42:10 +0100162 // instead.
163 if (which == ESTACK_SCRIPT)
164 {
LemonBoyeca7c602022-04-14 15:39:43 +0100165 // Walk the stack backwards, starting from the current frame.
166 for (idx = exestack.ga_len - 1; idx >= 0; --idx, --entry)
LemonBoy6013d002022-04-09 21:42:10 +0100167 {
zeertzjqa2471422022-08-23 19:26:05 +0100168 if (entry->es_type == ETYPE_UFUNC || entry->es_type == ETYPE_AUCMD)
LemonBoy6013d002022-04-09 21:42:10 +0100169 {
zeertzjqa2471422022-08-23 19:26:05 +0100170 sctx_T *def_ctx = entry->es_type == ETYPE_UFUNC
171 ? &entry->es_info.ufunc->uf_script_ctx
172 : acp_script_ctx(entry->es_info.aucmd);
LemonBoy6013d002022-04-09 21:42:10 +0100173
zeertzjqa2471422022-08-23 19:26:05 +0100174 return def_ctx->sc_sid > 0
175 ? vim_strsave(SCRIPT_ITEM(def_ctx->sc_sid)->sn_name)
176 : NULL;
LemonBoyeca7c602022-04-14 15:39:43 +0100177 }
178 else if (entry->es_type == ETYPE_SCRIPT)
LemonBoyeca7c602022-04-14 15:39:43 +0100179 return vim_strsave(entry->es_name);
LemonBoy6013d002022-04-09 21:42:10 +0100180 }
181 return NULL;
182 }
183
Bram Moolenaara5d04232020-07-26 15:37:02 +0200184 // Give information about each stack entry up to the root.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100185 // For a function we compose the call stack, as it was done in the past:
186 // "function One[123]..Two[456]..Three"
Bram Moolenaara5d04232020-07-26 15:37:02 +0200187 ga_init2(&ga, sizeof(char), 100);
188 for (idx = 0; idx < exestack.ga_len; ++idx)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100189 {
190 entry = ((estack_T *)exestack.ga_data) + idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200191 if (entry->es_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100192 {
Bram Moolenaara810db32020-09-11 17:59:23 +0200193 long lnum = 0;
194 char *dots;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200195
Bram Moolenaara5d04232020-07-26 15:37:02 +0200196 len = STRLEN(entry->es_name) + 15;
197 type_name = "";
198 if (entry->es_type != last_type)
199 {
200 switch (entry->es_type)
201 {
202 case ETYPE_SCRIPT: type_name = "script "; break;
203 case ETYPE_UFUNC: type_name = "function "; break;
204 default: type_name = ""; break;
205 }
206 last_type = entry->es_type;
207 }
208 len += STRLEN(type_name);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200209 if (ga_grow(&ga, (int)len) == FAIL)
Bram Moolenaara5d04232020-07-26 15:37:02 +0200210 break;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200211 if (idx == exestack.ga_len - 1)
212 lnum = which == ESTACK_STACK ? SOURCING_LNUM : 0;
213 else
214 lnum = entry->es_lnum;
Bram Moolenaara810db32020-09-11 17:59:23 +0200215 dots = idx == exestack.ga_len - 1 ? "" : "..";
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200216 if (lnum == 0)
217 // For the bottom entry of <sfile>: do not add the line number,
218 // it is used in <slnum>. Also leave it out when the number is
219 // not set.
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200220 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s%s",
Bram Moolenaara810db32020-09-11 17:59:23 +0200221 type_name, entry->es_name, dots);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200222 else
Bram Moolenaara810db32020-09-11 17:59:23 +0200223 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s[%ld]%s",
224 type_name, entry->es_name, lnum, dots);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200225 ga.ga_len += (int)STRLEN((char *)ga.ga_data + ga.ga_len);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100226 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100227 }
228
Bram Moolenaara5d04232020-07-26 15:37:02 +0200229 return (char_u *)ga.ga_data;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100230#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100231}
232
233/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200234 * ":runtime [what] {name}"
235 */
236 void
237ex_runtime(exarg_T *eap)
238{
239 char_u *arg = eap->arg;
240 char_u *p = skiptowhite(arg);
241 int len = (int)(p - arg);
242 int flags = eap->forceit ? DIP_ALL : 0;
243
244 if (STRNCMP(arg, "START", len) == 0)
245 {
246 flags += DIP_START + DIP_NORTP;
247 arg = skipwhite(arg + len);
248 }
249 else if (STRNCMP(arg, "OPT", len) == 0)
250 {
251 flags += DIP_OPT + DIP_NORTP;
252 arg = skipwhite(arg + len);
253 }
254 else if (STRNCMP(arg, "PACK", len) == 0)
255 {
256 flags += DIP_START + DIP_OPT + DIP_NORTP;
257 arg = skipwhite(arg + len);
258 }
259 else if (STRNCMP(arg, "ALL", len) == 0)
260 {
261 flags += DIP_START + DIP_OPT;
262 arg = skipwhite(arg + len);
263 }
264
265 source_runtime(arg, flags);
266}
267
268 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100269source_callback(char_u *fname, void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200270{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200272}
273
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000274#ifdef FEAT_EVAL
275/*
276 * Find an already loaded script "name".
277 * If found returns its script ID. If not found returns -1.
278 */
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100279 int
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000280find_script_by_name(char_u *name)
281{
282 int sid;
283 scriptitem_T *si;
284
285 for (sid = script_items.ga_len; sid > 0; --sid)
286 {
287 // We used to check inode here, but that doesn't work:
288 // - If a script is edited and written, it may get a different
289 // inode number, even though to the user it is the same script.
290 // - If a script is deleted and another script is written, with a
291 // different name, the inode may be re-used.
292 si = SCRIPT_ITEM(sid);
293 if (si->sn_name != NULL && fnamecmp(si->sn_name, name) == 0)
294 return sid;
295 }
296 return -1;
297}
298
299/*
300 * Add a new scriptitem with all items initialized.
301 * When running out of memory "error" is set to FAIL.
302 * Returns the script ID.
303 */
304 static int
305get_new_scriptitem(int *error)
306{
307 static scid_T last_current_SID = 0;
308 int sid = ++last_current_SID;
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000309 scriptitem_T *si = NULL;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000310
311 if (ga_grow(&script_items, (int)(sid - script_items.ga_len)) == FAIL)
312 {
313 *error = FAIL;
314 return sid;
315 }
316 while (script_items.ga_len < sid)
317 {
318 si = ALLOC_CLEAR_ONE(scriptitem_T);
319 if (si == NULL)
320 {
321 *error = FAIL;
322 return sid;
323 }
324 ++script_items.ga_len;
325 SCRIPT_ITEM(script_items.ga_len) = si;
326 si->sn_name = NULL;
327 si->sn_version = 1;
328
329 // Allocate the local script variables to use for this script.
330 new_script_vars(script_items.ga_len);
331 ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
332 hash_init(&si->sn_all_vars.dv_hashtab);
333 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
334 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
335# ifdef FEAT_PROFILE
336 si->sn_prof_on = FALSE;
337# endif
338 }
339
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000340 // "si" can't be NULL, check only to avoid a compiler warning
341 if (si != NULL)
342 // Used to check script variable index is still valid.
343 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000344
345 return sid;
346}
347
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100348 int
349get_new_scriptitem_for_fname(int *error, char_u *fname)
350{
351 int sid = get_new_scriptitem(error);
352
353 if (*error == OK)
354 {
355 scriptitem_T *si = SCRIPT_ITEM(sid);
356
357 si->sn_name = vim_strsave(fname);
358 si->sn_state = SN_STATE_NOT_LOADED;
359 }
360 return sid;
361}
362
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000363 static void
364find_script_callback(char_u *fname, void *cookie)
365{
366 int sid;
367 int error = OK;
368 int *ret_sid = cookie;
369
370 sid = find_script_by_name(fname);
371 if (sid < 0)
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000372 // script does not exist yet, create a new scriptitem
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100373 sid = get_new_scriptitem_for_fname(&error, fname);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000374 *ret_sid = sid;
375}
376#endif
377
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200378/*
379 * Find the file "name" in all directories in "path" and invoke
380 * "callback(fname, cookie)".
381 * "name" can contain wildcards.
382 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
383 * When "flags" has DIP_DIR: find directories instead of files.
384 * When "flags" has DIP_ERR: give an error message if there is no match.
385 *
386 * return FAIL when no file could be sourced, OK otherwise.
387 */
388 int
389do_in_path(
390 char_u *path,
391 char_u *name,
392 int flags,
393 void (*callback)(char_u *fname, void *ck),
394 void *cookie)
395{
396 char_u *rtp;
397 char_u *np;
398 char_u *buf;
399 char_u *rtp_copy;
400 char_u *tail;
401 int num_files;
402 char_u **files;
403 int i;
404 int did_one = FALSE;
405#ifdef AMIGA
406 struct Process *proc = (struct Process *)FindTask(0L);
407 APTR save_winptr = proc->pr_WindowPtr;
408
409 // Avoid a requester here for a volume that doesn't exist.
410 proc->pr_WindowPtr = (APTR)-1L;
411#endif
412
413 // Make a copy of 'runtimepath'. Invoking the callback may change the
414 // value.
415 rtp_copy = vim_strsave(path);
416 buf = alloc(MAXPATHL);
417 if (buf != NULL && rtp_copy != NULL)
418 {
Bram Moolenaar647a5302020-05-03 17:01:24 +0200419 if (p_verbose > 10 && name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200420 {
421 verbose_enter();
422 smsg(_("Searching for \"%s\" in \"%s\""),
423 (char *)name, (char *)path);
424 verbose_leave();
425 }
426
427 // Loop over all entries in 'runtimepath'.
428 rtp = rtp_copy;
429 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
430 {
431 size_t buflen;
432
433 // Copy the path from 'runtimepath' to buf[].
434 copy_option_part(&rtp, buf, MAXPATHL, ",");
435 buflen = STRLEN(buf);
436
437 // Skip after or non-after directories.
438 if (flags & (DIP_NOAFTER | DIP_AFTER))
439 {
440 int is_after = buflen >= 5
441 && STRCMP(buf + buflen - 5, "after") == 0;
442
443 if ((is_after && (flags & DIP_NOAFTER))
444 || (!is_after && (flags & DIP_AFTER)))
445 continue;
446 }
447
448 if (name == NULL)
449 {
450 (*callback)(buf, (void *) &cookie);
451 if (!did_one)
452 did_one = (cookie == NULL);
453 }
454 else if (buflen + STRLEN(name) + 2 < MAXPATHL)
455 {
456 add_pathsep(buf);
457 tail = buf + STRLEN(buf);
458
459 // Loop over all patterns in "name"
460 np = name;
461 while (*np != NUL && ((flags & DIP_ALL) || !did_one))
462 {
463 // Append the pattern from "name" to buf[].
464 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
465 "\t ");
466
Bram Moolenaar647a5302020-05-03 17:01:24 +0200467 if (p_verbose > 10)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200468 {
469 verbose_enter();
470 smsg(_("Searching for \"%s\""), buf);
471 verbose_leave();
472 }
473
474 // Expand wildcards, invoke the callback for each match.
475 if (gen_expand_wildcards(1, &buf, &num_files, &files,
476 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
477 {
478 for (i = 0; i < num_files; ++i)
479 {
480 (*callback)(files[i], cookie);
481 did_one = TRUE;
482 if (!(flags & DIP_ALL))
483 break;
484 }
485 FreeWild(num_files, files);
486 }
487 }
488 }
489 }
490 }
491 vim_free(buf);
492 vim_free(rtp_copy);
493 if (!did_one && name != NULL)
494 {
495 char *basepath = path == p_rtp ? "runtimepath" : "packpath";
496
497 if (flags & DIP_ERR)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000498 semsg(_(e_directory_not_found_in_str_str), basepath, name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200499 else if (p_verbose > 0)
500 {
501 verbose_enter();
502 smsg(_("not found in '%s': \"%s\""), basepath, name);
503 verbose_leave();
504 }
505 }
506
507#ifdef AMIGA
508 proc->pr_WindowPtr = save_winptr;
509#endif
510
511 return did_one ? OK : FAIL;
512}
513
514/*
515 * Find "name" in "path". When found, invoke the callback function for
516 * it: callback(fname, "cookie")
517 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first
518 * one is used.
519 * Returns OK when at least one match found, FAIL otherwise.
520 *
521 * If "name" is NULL calls callback for each entry in "path". Cookie is
522 * passed by reference in this case, setting it to NULL indicates that callback
523 * has done its job.
524 */
525 static int
526do_in_path_and_pp(
527 char_u *path,
528 char_u *name,
529 int flags,
530 void (*callback)(char_u *fname, void *ck),
531 void *cookie)
532{
533 int done = FAIL;
534 char_u *s;
535 int len;
536 char *start_dir = "pack/*/start/*/%s";
537 char *opt_dir = "pack/*/opt/*/%s";
538
539 if ((flags & DIP_NORTP) == 0)
540 done = do_in_path(path, name, flags, callback, cookie);
541
542 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
543 {
544 len = (int)(STRLEN(start_dir) + STRLEN(name));
545 s = alloc(len);
546 if (s == NULL)
547 return FAIL;
548 vim_snprintf((char *)s, len, start_dir, name);
549 done = do_in_path(p_pp, s, flags, callback, cookie);
550 vim_free(s);
551 }
552
553 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
554 {
555 len = (int)(STRLEN(opt_dir) + STRLEN(name));
556 s = alloc(len);
557 if (s == NULL)
558 return FAIL;
559 vim_snprintf((char *)s, len, opt_dir, name);
560 done = do_in_path(p_pp, s, flags, callback, cookie);
561 vim_free(s);
562 }
563
564 return done;
565}
566
567/*
568 * Just like do_in_path_and_pp(), using 'runtimepath' for "path".
569 */
570 int
571do_in_runtimepath(
572 char_u *name,
573 int flags,
574 void (*callback)(char_u *fname, void *ck),
575 void *cookie)
576{
577 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
578}
579
580/*
581 * Source the file "name" from all directories in 'runtimepath'.
582 * "name" can contain wildcards.
583 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
584 *
585 * return FAIL when no file could be sourced, OK otherwise.
586 */
587 int
588source_runtime(char_u *name, int flags)
589{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590 return source_in_path(p_rtp, name, flags, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200591}
592
593/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000594 * Just like source_runtime(), but use "path" instead of 'runtimepath'
595 * and return the script ID in "ret_sid".
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200596 */
597 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100598source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200599{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200601}
602
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200603#if defined(FEAT_EVAL) || defined(PROTO)
604
605/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000606 * Find "name" in 'runtimepath'. If found a new scriptitem is created for it
607 * and it's script ID is returned.
608 * If not found returns -1.
609 */
610 int
611find_script_in_rtp(char_u *name)
612{
613 int sid = -1;
614
615 (void)do_in_path_and_pp(p_rtp, name, DIP_NOAFTER,
616 find_script_callback, &sid);
617 return sid;
618}
619
620/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200621 * Expand wildcards in "pat" and invoke do_source() for each match.
622 */
623 static void
624source_all_matches(char_u *pat)
625{
626 int num_files;
627 char_u **files;
628 int i;
629
630 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK)
631 {
632 for (i = 0; i < num_files; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100633 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200634 FreeWild(num_files, files);
635 }
636}
637
638/*
639 * Add the package directory to 'runtimepath'.
640 */
641 static int
642add_pack_dir_to_rtp(char_u *fname)
643{
644 char_u *p4, *p3, *p2, *p1, *p;
645 char_u *entry;
646 char_u *insp = NULL;
647 int c;
648 char_u *new_rtp;
649 int keep;
650 size_t oldlen;
651 size_t addlen;
652 size_t new_rtp_len;
653 char_u *afterdir = NULL;
654 size_t afterlen = 0;
655 char_u *after_insp = NULL;
656 char_u *ffname = NULL;
657 size_t fname_len;
658 char_u *buf = NULL;
659 char_u *rtp_ffname;
660 int match;
661 int retval = FAIL;
662
663 p4 = p3 = p2 = p1 = get_past_head(fname);
664 for (p = p1; *p; MB_PTR_ADV(p))
665 if (vim_ispathsep_nocolon(*p))
666 {
667 p4 = p3; p3 = p2; p2 = p1; p1 = p;
668 }
669
670 // now we have:
671 // rtp/pack/name/start/name
672 // p4 p3 p2 p1
673 //
674 // find the part up to "pack" in 'runtimepath'
675 c = *++p4; // append pathsep in order to expand symlink
676 *p4 = NUL;
677 ffname = fix_fname(fname);
678 *p4 = c;
679 if (ffname == NULL)
680 return FAIL;
681
682 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
683 // Also stop at the first "after" directory.
684 fname_len = STRLEN(ffname);
685 buf = alloc(MAXPATHL);
686 if (buf == NULL)
687 goto theend;
688 for (entry = p_rtp; *entry != NUL; )
689 {
690 char_u *cur_entry = entry;
691
692 copy_option_part(&entry, buf, MAXPATHL, ",");
693 if (insp == NULL)
694 {
695 add_pathsep(buf);
696 rtp_ffname = fix_fname(buf);
697 if (rtp_ffname == NULL)
698 goto theend;
699 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
700 vim_free(rtp_ffname);
701 if (match)
702 // Insert "ffname" after this entry (and comma).
703 insp = entry;
704 }
705
706 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
707 && p > buf
708 && vim_ispathsep(p[-1])
709 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
710 {
711 if (insp == NULL)
712 // Did not find "ffname" before the first "after" directory,
713 // insert it before this entry.
714 insp = cur_entry;
715 after_insp = cur_entry;
716 break;
717 }
718 }
719
720 if (insp == NULL)
721 // Both "fname" and "after" not found, append at the end.
722 insp = p_rtp + STRLEN(p_rtp);
723
724 // check if rtp/pack/name/start/name/after exists
725 afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
726 if (afterdir != NULL && mch_isdir(afterdir))
727 afterlen = STRLEN(afterdir) + 1; // add one for comma
728
729 oldlen = STRLEN(p_rtp);
730 addlen = STRLEN(fname) + 1; // add one for comma
731 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
732 if (new_rtp == NULL)
733 goto theend;
734
735 // We now have 'rtp' parts: {keep}{keep_after}{rest}.
736 // Create new_rtp, first: {keep},{fname}
737 keep = (int)(insp - p_rtp);
738 mch_memmove(new_rtp, p_rtp, keep);
739 new_rtp_len = keep;
740 if (*insp == NUL)
741 new_rtp[new_rtp_len++] = ','; // add comma before
742 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
743 new_rtp_len += addlen - 1;
744 if (*insp != NUL)
745 new_rtp[new_rtp_len++] = ','; // add comma after
746
747 if (afterlen > 0 && after_insp != NULL)
748 {
749 int keep_after = (int)(after_insp - p_rtp);
750
751 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
752 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
753 keep_after - keep);
754 new_rtp_len += keep_after - keep;
755 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
756 new_rtp_len += afterlen - 1;
757 new_rtp[new_rtp_len++] = ',';
758 keep = keep_after;
759 }
760
761 if (p_rtp[keep] != NUL)
762 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
763 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
764 else
765 new_rtp[new_rtp_len] = NUL;
766
767 if (afterlen > 0 && after_insp == NULL)
768 {
769 // Append afterdir when "after" was not found:
770 // {keep},{fname}{rest},{afterdir}
771 STRCAT(new_rtp, ",");
772 STRCAT(new_rtp, afterdir);
773 }
774
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100775 set_option_value_give_err((char_u *)"rtp", 0L, new_rtp, 0);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200776 vim_free(new_rtp);
777 retval = OK;
778
779theend:
780 vim_free(buf);
781 vim_free(ffname);
782 vim_free(afterdir);
783 return retval;
784}
785
786/*
787 * Load scripts in "plugin" and "ftdetect" directories of the package.
788 */
789 static int
790load_pack_plugin(char_u *fname)
791{
792 static char *plugpat = "%s/plugin/**/*.vim";
793 static char *ftpat = "%s/ftdetect/*.vim";
794 int len;
795 char_u *ffname = fix_fname(fname);
796 char_u *pat = NULL;
797 int retval = FAIL;
798
799 if (ffname == NULL)
800 return FAIL;
801 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
802 pat = alloc(len);
803 if (pat == NULL)
804 goto theend;
805 vim_snprintf((char *)pat, len, plugpat, ffname);
806 source_all_matches(pat);
807
808 {
809 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
810
811 // If runtime/filetype.vim wasn't loaded yet, the scripts will be
812 // found when it loads.
813 if (cmd != NULL && eval_to_number(cmd) > 0)
814 {
815 do_cmdline_cmd((char_u *)"augroup filetypedetect");
816 vim_snprintf((char *)pat, len, ftpat, ffname);
817 source_all_matches(pat);
818 do_cmdline_cmd((char_u *)"augroup END");
819 }
820 vim_free(cmd);
821 }
822 vim_free(pat);
823 retval = OK;
824
825theend:
826 vim_free(ffname);
827 return retval;
828}
829
830// used for "cookie" of add_pack_plugin()
831static int APP_ADD_DIR;
832static int APP_LOAD;
833static int APP_BOTH;
834
835 static void
836add_pack_plugin(char_u *fname, void *cookie)
837{
838 if (cookie != &APP_LOAD)
839 {
840 char_u *buf = alloc(MAXPATHL);
841 char_u *p;
842 int found = FALSE;
843
844 if (buf == NULL)
845 return;
846 p = p_rtp;
847 while (*p != NUL)
848 {
849 copy_option_part(&p, buf, MAXPATHL, ",");
850 if (pathcmp((char *)buf, (char *)fname, -1) == 0)
851 {
852 found = TRUE;
853 break;
854 }
855 }
856 vim_free(buf);
857 if (!found)
858 // directory is not yet in 'runtimepath', add it
859 if (add_pack_dir_to_rtp(fname) == FAIL)
860 return;
861 }
862
863 if (cookie != &APP_ADD_DIR)
864 load_pack_plugin(fname);
865}
866
867/*
868 * Add all packages in the "start" directory to 'runtimepath'.
869 */
870 void
871add_pack_start_dirs(void)
872{
873 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
874 add_pack_plugin, &APP_ADD_DIR);
875}
876
877/*
878 * Load plugins from all packages in the "start" directory.
879 */
880 void
881load_start_packages(void)
882{
883 did_source_packages = TRUE;
884 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
885 add_pack_plugin, &APP_LOAD);
886}
887
888/*
889 * ":packloadall"
890 * Find plugins in the package directories and source them.
891 */
892 void
893ex_packloadall(exarg_T *eap)
894{
895 if (!did_source_packages || eap->forceit)
896 {
897 // First do a round to add all directories to 'runtimepath', then load
898 // the plugins. This allows for plugins to use an autoload directory
899 // of another plugin.
900 add_pack_start_dirs();
901 load_start_packages();
902 }
903}
904
905/*
906 * ":packadd[!] {name}"
907 */
908 void
909ex_packadd(exarg_T *eap)
910{
911 static char *plugpat = "pack/*/%s/%s";
912 int len;
913 char *pat;
914 int round;
915 int res = OK;
916
917 // Round 1: use "start", round 2: use "opt".
918 for (round = 1; round <= 2; ++round)
919 {
920 // Only look under "start" when loading packages wasn't done yet.
921 if (round == 1 && did_source_packages)
922 continue;
923
924 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
925 pat = alloc(len);
926 if (pat == NULL)
927 return;
928 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
929 // The first round don't give a "not found" error, in the second round
930 // only when nothing was found in the first round.
931 res = do_in_path(p_pp, (char_u *)pat,
932 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
933 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
934 vim_free(pat);
935 }
936}
937#endif
938
939/*
Bram Moolenaar26262f82019-09-04 20:59:15 +0200940 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
941 * list of file names in allocated memory.
942 */
943 void
944remove_duplicates(garray_T *gap)
945{
946 int i;
947 int j;
948 char_u **fnames = (char_u **)gap->ga_data;
949
950 sort_strings(fnames, gap->ga_len);
951 for (i = gap->ga_len - 1; i > 0; --i)
952 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
953 {
954 vim_free(fnames[i]);
955 for (j = i + 1; j < gap->ga_len; ++j)
956 fnames[j - 1] = fnames[j];
957 --gap->ga_len;
958 }
959}
960
961/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200962 * Expand color scheme, compiler or filetype names.
963 * Search from 'runtimepath':
964 * 'runtimepath'/{dirnames}/{pat}.vim
965 * When "flags" has DIP_START: search also from 'start' of 'packpath':
966 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
967 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
968 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
969 * "dirnames" is an array with one or more directory names.
970 */
971 int
972ExpandRTDir(
973 char_u *pat,
974 int flags,
975 int *num_file,
976 char_u ***file,
977 char *dirnames[])
978{
979 char_u *s;
980 char_u *e;
981 char_u *match;
982 garray_T ga;
983 int i;
984 int pat_len;
985
986 *num_file = 0;
987 *file = NULL;
988 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000989 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200990
991 for (i = 0; dirnames[i] != NULL; ++i)
992 {
993 s = alloc(STRLEN(dirnames[i]) + pat_len + 7);
994 if (s == NULL)
995 {
996 ga_clear_strings(&ga);
997 return FAIL;
998 }
999 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
1000 globpath(p_rtp, s, &ga, 0);
1001 vim_free(s);
1002 }
1003
1004 if (flags & DIP_START) {
1005 for (i = 0; dirnames[i] != NULL; ++i)
1006 {
1007 s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
1008 if (s == NULL)
1009 {
1010 ga_clear_strings(&ga);
1011 return FAIL;
1012 }
1013 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
1014 globpath(p_pp, s, &ga, 0);
1015 vim_free(s);
1016 }
1017 }
1018
1019 if (flags & DIP_OPT) {
1020 for (i = 0; dirnames[i] != NULL; ++i)
1021 {
1022 s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
1023 if (s == NULL)
1024 {
1025 ga_clear_strings(&ga);
1026 return FAIL;
1027 }
1028 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
1029 globpath(p_pp, s, &ga, 0);
1030 vim_free(s);
1031 }
1032 }
1033
1034 for (i = 0; i < ga.ga_len; ++i)
1035 {
1036 match = ((char_u **)ga.ga_data)[i];
1037 s = match;
1038 e = s + STRLEN(s);
1039 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
1040 {
1041 e -= 4;
1042 for (s = e; s > match; MB_PTR_BACK(match, s))
1043 if (s < match || vim_ispathsep(*s))
1044 break;
1045 ++s;
1046 *e = NUL;
1047 mch_memmove(match, s, e - s + 1);
1048 }
1049 }
1050
1051 if (ga.ga_len == 0)
1052 return FAIL;
1053
1054 // Sort and remove duplicates which can happen when specifying multiple
1055 // directories in dirnames.
1056 remove_duplicates(&ga);
1057
1058 *file = ga.ga_data;
1059 *num_file = ga.ga_len;
1060 return OK;
1061}
1062
1063/*
1064 * Expand loadplugin names:
1065 * 'packpath'/pack/ * /opt/{pat}
1066 */
1067 int
1068ExpandPackAddDir(
1069 char_u *pat,
1070 int *num_file,
1071 char_u ***file)
1072{
1073 char_u *s;
1074 char_u *e;
1075 char_u *match;
1076 garray_T ga;
1077 int i;
1078 int pat_len;
1079
1080 *num_file = 0;
1081 *file = NULL;
1082 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001083 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001084
1085 s = alloc(pat_len + 26);
1086 if (s == NULL)
1087 {
1088 ga_clear_strings(&ga);
1089 return FAIL;
1090 }
1091 sprintf((char *)s, "pack/*/opt/%s*", pat);
1092 globpath(p_pp, s, &ga, 0);
1093 vim_free(s);
1094
1095 for (i = 0; i < ga.ga_len; ++i)
1096 {
1097 match = ((char_u **)ga.ga_data)[i];
1098 s = gettail(match);
1099 e = s + STRLEN(s);
1100 mch_memmove(match, s, e - s + 1);
1101 }
1102
1103 if (ga.ga_len == 0)
1104 return FAIL;
1105
1106 // Sort and remove duplicates which can happen when specifying multiple
1107 // directories in dirnames.
1108 remove_duplicates(&ga);
1109
1110 *file = ga.ga_data;
1111 *num_file = ga.ga_len;
1112 return OK;
1113}
1114
1115 static void
1116cmd_source(char_u *fname, exarg_T *eap)
1117{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001118 int clearvars = FALSE;
1119
1120 if (*fname != NUL && STRNCMP(fname, "++clear", 7) == 0)
1121 {
1122 // ++clear argument is supplied
1123 clearvars = TRUE;
1124 fname = fname + 7;
1125 if (*fname != NUL)
1126 {
1127 semsg(_(e_invalid_argument_str), eap->arg);
1128 return;
1129 }
1130 }
1131
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001132 if (*fname != NUL && eap != NULL && eap->addr_count > 0)
1133 {
1134 // if a filename is specified to :source, then a range is not allowed
1135 emsg(_(e_no_range_allowed));
1136 return;
1137 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001138
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001139 if (eap != NULL && *fname == NUL)
1140 {
1141 if (eap->forceit)
1142 // a file name is needed to source normal mode commands
1143 emsg(_(e_argument_required));
1144 else
1145 // source ex commands from the current buffer
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001146 do_source_ext(NULL, FALSE, FALSE, NULL, eap, clearvars);
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001147 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001148 else if (eap != NULL && eap->forceit)
1149 // ":source!": read Normal mode commands
1150 // Need to execute the commands directly. This is required at least
1151 // for:
1152 // - ":g" command busy
1153 // - after ":argdo", ":windo" or ":bufdo"
1154 // - another command follows
1155 // - inside a loop
1156 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
1157#ifdef FEAT_EVAL
1158 || eap->cstack->cs_idx >= 0
1159#endif
1160 );
1161
1162 // ":source" read ex commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001163 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001164 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001165}
1166
1167/*
1168 * ":source {fname}"
1169 */
1170 void
1171ex_source(exarg_T *eap)
1172{
1173#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02001174 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001175 {
1176 char_u *fname = NULL;
1177
1178 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
1179 NULL, NULL,
1180 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1181 if (fname != NULL)
1182 {
1183 cmd_source(fname, eap);
1184 vim_free(fname);
1185 }
1186 }
1187 else
1188#endif
1189 cmd_source(eap->arg, eap);
1190}
1191
1192#if defined(FEAT_EVAL) || defined(PROTO)
1193/*
1194 * ":options"
1195 */
1196 void
1197ex_options(
1198 exarg_T *eap UNUSED)
1199{
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001200 char_u buf[500];
1201 int multi_mods = 0;
1202
1203 buf[0] = NUL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001204 (void)add_win_cmd_modifers(buf, &cmdmod, &multi_mods);
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001205
1206 vim_setenv((char_u *)"OPTWIN_CMD", buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001207 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
1208}
1209#endif
1210
1211/*
1212 * ":source" and associated commands.
1213 */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001214
1215#ifdef FEAT_EVAL
1216/*
1217 * Return the address holding the next breakpoint line for a source cookie.
1218 */
1219 linenr_T *
1220source_breakpoint(void *cookie)
1221{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001222 return &((source_cookie_T *)cookie)->breakpoint;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001223}
1224
1225/*
1226 * Return the address holding the debug tick for a source cookie.
1227 */
1228 int *
1229source_dbg_tick(void *cookie)
1230{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001231 return &((source_cookie_T *)cookie)->dbg_tick;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001232}
1233
1234/*
1235 * Return the nesting level for a source cookie.
1236 */
1237 int
1238source_level(void *cookie)
1239{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001240 return ((source_cookie_T *)cookie)->level;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001241}
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001242
1243/*
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02001244 * Return the readahead line. Note that the pointer may become invalid when
1245 * getting the next line, if it's concatenated with the next one.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001246 */
1247 char_u *
1248source_nextline(void *cookie)
1249{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001250 return ((source_cookie_T *)cookie)->nextline;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001251}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001252#endif
1253
1254#if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC)
1255# define USE_FOPEN_NOINH
1256/*
1257 * Special function to open a file without handle inheritance.
1258 * When possible the handle is closed on exec().
1259 */
1260 static FILE *
1261fopen_noinh_readbin(char *filename)
1262{
1263# ifdef MSWIN
1264 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
1265# else
1266 int fd_tmp = mch_open(filename, O_RDONLY, 0);
1267# endif
1268
1269 if (fd_tmp == -1)
1270 return NULL;
1271
1272# ifdef HAVE_FD_CLOEXEC
1273 {
1274 int fdflags = fcntl(fd_tmp, F_GETFD);
1275 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1276 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
1277 }
1278# endif
1279
1280 return fdopen(fd_tmp, READBIN);
1281}
1282#endif
1283
1284/*
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001285 * Initialization for sourcing lines from the current buffer. Reads all the
1286 * lines from the buffer and stores it in the cookie grow array.
1287 * Returns a pointer to the name ":source buffer=<n>" on success and NULL on
1288 * failure.
1289 */
1290 static char_u *
1291do_source_buffer_init(source_cookie_T *sp, exarg_T *eap)
1292{
1293 linenr_T curr_lnum;
1294 char_u *line = NULL;
1295 char_u *fname;
1296
1297 CLEAR_FIELD(*sp);
1298
1299 if (curbuf == NULL)
1300 return NULL;
1301
1302 // Use ":source buffer=<num>" as the script name
1303 vim_snprintf((char *)IObuff, IOSIZE, ":source buffer=%d", curbuf->b_fnum);
1304 fname = vim_strsave(IObuff);
1305 if (fname == NULL)
1306 return NULL;
1307
1308 ga_init2(&sp->buflines, sizeof(char_u *), 100);
1309
1310 // Copy the lines from the buffer into a grow array
1311 for (curr_lnum = eap->line1; curr_lnum <= eap->line2; curr_lnum++)
1312 {
1313 line = vim_strsave(ml_get(curr_lnum));
1314 if (line == NULL)
1315 goto errret;
1316 if (ga_add_string(&sp->buflines, line) == FAIL)
1317 goto errret;
1318 line = NULL;
1319 }
1320 sp->buf_lnum = 0;
1321 sp->source_from_buf = TRUE;
1322
1323 return fname;
1324
1325errret:
1326 vim_free(fname);
1327 vim_free(line);
1328 ga_clear_strings(&sp->buflines);
1329 return NULL;
1330}
1331
1332/*
1333 * Read the file "fname" and execute its lines as EX commands.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001334 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1335 * again.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001336 *
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001337 * The 'eap' argument is used when sourcing lines from a buffer instead of a
1338 * file.
1339 *
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001340 * If 'clearvars' is TRUE, then for scripts which are loaded more than
1341 * once, clear all the functions and variables previously defined in that
1342 * script.
1343 *
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001344 * This function may be called recursively!
1345 *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 * Return FAIL if file could not be opened, OK otherwise.
1347 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001348 */
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001349 static int
1350do_source_ext(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001351 char_u *fname,
1352 int check_other, // check for .vimrc and _vimrc
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001353 int is_vimrc, // DOSO_ value
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001354 int *ret_sid UNUSED,
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001355 exarg_T *eap,
1356 int clearvars UNUSED)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001357{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001358 source_cookie_T cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001359 char_u *p;
Bram Moolenaar753885b2022-08-24 16:30:36 +01001360 char_u *fname_not_fixed = NULL;
Bram Moolenaar5214b292022-08-24 17:32:35 +01001361 char_u *fname_exp = NULL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001362 char_u *firstline = NULL;
1363 int retval = FAIL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001364 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001365#ifdef FEAT_EVAL
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001366 funccal_entry_T funccalp_entry;
1367 int save_debug_break_level = debug_break_level;
Bram Moolenaar5214b292022-08-24 17:32:35 +01001368 int sid = -1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001369 scriptitem_T *si = NULL;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001370 int save_estack_compiling = estack_compiling;
Bram Moolenaar88774872022-08-16 20:24:29 +01001371 ESTACK_CHECK_DECLARATION
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;
1382
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001383 CLEAR_FIELD(cookie);
1384 if (fname == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001385 {
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001386 // sourcing lines from a buffer
1387 fname_exp = do_source_buffer_init(&cookie, eap);
1388 if (fname_exp == NULL)
1389 return FAIL;
1390 }
1391 else
1392 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01001393 fname_not_fixed = expand_env_save(fname);
1394 if (fname_not_fixed == NULL)
1395 goto theend;
1396 fname_exp = fix_fname(fname_not_fixed);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001397 if (fname_exp == NULL)
Bram Moolenaar753885b2022-08-24 16:30:36 +01001398 goto theend;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001399 if (mch_isdir(fname_exp))
1400 {
1401 smsg(_("Cannot source a directory: \"%s\""), fname);
1402 goto theend;
1403 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001404 }
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001405#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001406 estack_compiling = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001407
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408 // See if we loaded this script before.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001409 sid = find_script_by_name(fname_exp);
Bram Moolenaarf479cac2022-01-12 12:54:55 +00001410 if (sid > 0 && ret_sid != NULL
1411 && SCRIPT_ITEM(sid)->sn_state != SN_STATE_NOT_LOADED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 {
1413 // Already loaded and no need to load again, return here.
1414 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001415 retval = OK;
1416 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417 }
1418#endif
1419
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001420 // Apply SourceCmd autocommands, they should get the file and source it.
1421 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1422 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1423 FALSE, curbuf))
1424 {
1425#ifdef FEAT_EVAL
1426 retval = aborting() ? FAIL : OK;
1427#else
1428 retval = OK;
1429#endif
1430 if (retval == OK)
1431 // Apply SourcePost autocommands.
1432 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1433 FALSE, curbuf);
1434 goto theend;
1435 }
1436
1437 // Apply SourcePre autocommands, they may get the file.
1438 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1439
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001440 if (!cookie.source_from_buf)
1441 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001442#ifdef USE_FOPEN_NOINH
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001443 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001444#else
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001445 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001446#endif
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001447 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001448 if (cookie.fp == NULL && check_other)
1449 {
1450 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1451 // and ".exrc" by "_exrc" or vice versa.
1452 p = gettail(fname_exp);
1453 if ((*p == '.' || *p == '_')
1454 && (STRICMP(p + 1, "vimrc") == 0
1455 || STRICMP(p + 1, "gvimrc") == 0
1456 || STRICMP(p + 1, "exrc") == 0))
1457 {
1458 if (*p == '_')
1459 *p = '.';
1460 else
1461 *p = '_';
1462#ifdef USE_FOPEN_NOINH
1463 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1464#else
1465 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1466#endif
1467 }
1468 }
1469
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001470 if (cookie.fp == NULL && !cookie.source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001471 {
1472 if (p_verbose > 0)
1473 {
1474 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001475 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001476 smsg(_("could not source \"%s\""), fname);
1477 else
1478 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001479 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001480 verbose_leave();
1481 }
1482 goto theend;
1483 }
1484
1485 // The file exists.
1486 // - In verbose mode, give a message.
1487 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1488 if (p_verbose > 1)
1489 {
1490 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001491 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001492 smsg(_("sourcing \"%s\""), fname);
1493 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001494 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001495 verbose_leave();
1496 }
1497 if (is_vimrc == DOSO_VIMRC)
1498 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1499 else if (is_vimrc == DOSO_GVIMRC)
1500 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1501
1502#ifdef USE_CRNL
1503 // If no automatic file format: Set default to CR-NL.
1504 if (*p_ffs == NUL)
1505 cookie.fileformat = EOL_DOS;
1506 else
1507 cookie.fileformat = EOL_UNKNOWN;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001508#endif
1509
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001510 if (fname == NULL)
1511 // When sourcing a range of lines from a buffer, use the buffer line
1512 // number.
1513 cookie.sourcing_lnum = eap->line1 - 1;
1514 else
1515 cookie.sourcing_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001516
1517#ifdef FEAT_EVAL
1518 // Check if this script has a breakpoint.
1519 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1520 cookie.fname = fname_exp;
1521 cookie.dbg_tick = debug_tick;
1522
1523 cookie.level = ex_nesting_level;
1524#endif
1525
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001526#ifdef STARTUPTIME
1527 if (time_fd != NULL)
1528 time_push(&tv_rel, &tv_start);
1529#endif
1530
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001531 // "legacy" does not apply to commands in the script
1532 sticky_cmdmod_flags = 0;
1533
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001534 save_current_sctx = current_sctx;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001535 if (cmdmod.cmod_flags & CMOD_VIM9CMD)
1536 // When the ":vim9cmd" command modifier is used, source the script as a
1537 // Vim9 script.
1538 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1539 else
1540 current_sctx.sc_version = 1; // default script version
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001541
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001542#ifdef FEAT_EVAL
1543# ifdef FEAT_PROFILE
1544 if (do_profiling == PROF_YES)
1545 prof_child_enter(&wait_start); // entering a child now
1546# endif
1547
1548 // Don't use local function variables, if called from a function.
1549 // Also starts profiling timer for nested script.
1550 save_funccal(&funccalp_entry);
1551
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001552 current_sctx.sc_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001553
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001554 // Check if this script was sourced before to find its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001555 // Always use a new sequence number.
1556 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001558 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 hashtab_T *ht;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001560 int todo;
1561 hashitem_T *hi;
1562 dictitem_T *di;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563
1564 // loading the same script again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001566 si = SCRIPT_ITEM(sid);
1567 if (si->sn_state == SN_STATE_NOT_LOADED)
1568 {
1569 // this script was found but not loaded yet
1570 si->sn_state = SN_STATE_NEW;
1571 }
1572 else
1573 {
1574 si->sn_state = SN_STATE_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001576 if (!clearvars)
1577 {
1578 // Script-local variables remain but "const" can be set again.
1579 // In Vim9 script variables will be cleared when "vim9script"
1580 // is encountered without the "noclear" argument.
1581 ht = &SCRIPT_VARS(sid);
1582 todo = (int)ht->ht_used;
1583 for (hi = ht->ht_array; todo > 0; ++hi)
1584 if (!HASHITEM_EMPTY(hi))
1585 {
1586 --todo;
1587 di = HI2DI(hi);
1588 di->di_flags |= DI_FLAGS_RELOAD;
1589 }
1590 // imports can be redefined once
1591 mark_imports_for_reload(sid);
1592 }
1593 else
1594 clear_vim9_scriptlocal_vars(sid);
Bram Moolenaar0123cc12021-02-07 17:17:58 +01001595
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001596 // reset version, "vim9script" may have been added or removed.
1597 si->sn_version = 1;
1598 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001599 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001601 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001602 int error = OK;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001603
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001604 // It's new, generate a new SID and initialize the scriptitem.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001605 sid = get_new_scriptitem(&error);
1606 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001607 if (error == FAIL)
1608 goto almosttheend;
Bram Moolenaar753885b2022-08-24 16:30:36 +01001609 si = SCRIPT_ITEM(sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001610 si->sn_name = fname_exp;
1611 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612 if (ret_sid != NULL)
Bram Moolenaar753885b2022-08-24 16:30:36 +01001613 *ret_sid = sid;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001614
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001615 // Remember the "is_vimrc" flag for when the file is sourced again.
1616 si->sn_is_vimrc = is_vimrc;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001617 }
1618
zeertzjq5a4fff42022-08-15 17:53:55 +01001619 // Keep the sourcing name/lnum, for recursive calls.
1620 estack_push(ETYPE_SCRIPT, si->sn_name, 0);
1621 ESTACK_CHECK_SETUP
1622
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001623# ifdef FEAT_PROFILE
1624 if (do_profiling == PROF_YES)
1625 {
1626 int forceit;
1627
1628 // Check if we do profiling for this script.
1629 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
1630 {
1631 script_do_profile(si);
1632 si->sn_pr_force = forceit;
1633 }
1634 if (si->sn_prof_on)
1635 {
1636 ++si->sn_pr_count;
1637 profile_start(&si->sn_pr_start);
1638 profile_zero(&si->sn_pr_children);
1639 }
1640 }
1641# endif
Bram Moolenaaraeef1f72022-09-15 12:20:18 +01001642#else
1643 // Keep the sourcing name/lnum, for recursive calls.
1644 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001645#endif
1646
1647 cookie.conv.vc_type = CONV_NONE; // no conversion
1648
1649 // Read the first line so we can check for a UTF-8 BOM.
1650 firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1651 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1652 && firstline[1] == 0xbb && firstline[2] == 0xbf)
1653 {
1654 // Found BOM; setup conversion, skip over BOM and recode the line.
1655 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1656 p = string_convert(&cookie.conv, firstline + 3, NULL);
1657 if (p == NULL)
1658 p = vim_strsave(firstline + 3);
1659 if (p != NULL)
1660 {
1661 vim_free(firstline);
1662 firstline = p;
1663 }
1664 }
1665
1666 // Call do_cmdline, which will call getsourceline() to get the lines.
1667 do_cmdline(firstline, getsourceline, (void *)&cookie,
1668 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1669 retval = OK;
1670
1671#ifdef FEAT_PROFILE
1672 if (do_profiling == PROF_YES)
1673 {
1674 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001675 si = SCRIPT_ITEM(sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001676 if (si->sn_prof_on)
1677 {
1678 profile_end(&si->sn_pr_start);
1679 profile_sub_wait(&wait_start, &si->sn_pr_start);
1680 profile_add(&si->sn_pr_total, &si->sn_pr_start);
1681 profile_self(&si->sn_pr_self, &si->sn_pr_start,
1682 &si->sn_pr_children);
1683 }
1684 }
1685#endif
1686
1687 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001688 emsg(_(e_interrupted));
Bram Moolenaar88774872022-08-16 20:24:29 +01001689#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +01001690 ESTACK_CHECK_NOW
Bram Moolenaar88774872022-08-16 20:24:29 +01001691#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001692 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001693 if (p_verbose > 1)
1694 {
1695 verbose_enter();
1696 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001697 if (SOURCING_NAME != NULL)
1698 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001699 verbose_leave();
1700 }
1701#ifdef STARTUPTIME
1702 if (time_fd != NULL)
1703 {
1704 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1705 time_msg((char *)IObuff, &tv_start);
1706 time_pop(&tv_rel);
1707 }
1708#endif
1709
1710 if (!got_int)
1711 trigger_source_post = TRUE;
1712
1713#ifdef FEAT_EVAL
1714 // After a "finish" in debug mode, need to break at first command of next
1715 // sourced file.
1716 if (save_debug_break_level > ex_nesting_level
1717 && debug_break_level == ex_nesting_level)
1718 ++debug_break_level;
1719#endif
1720
1721#ifdef FEAT_EVAL
1722almosttheend:
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001723 // If "sn_save_cpo" is set that means we encountered "vim9script": restore
1724 // 'cpoptions', unless in the main .vimrc file.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001726 si = SCRIPT_ITEM(sid);
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001727 if (si->sn_save_cpo != NULL && si->sn_is_vimrc == DOSO_NONE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728 {
Bram Moolenaar3e191692021-03-17 17:46:00 +01001729 if (STRCMP(p_cpo, CPO_VIM) != 0)
1730 {
1731 char_u *f;
1732 char_u *t;
1733
1734 // 'cpo' was changed in the script. Apply the same change to the
1735 // saved value, if possible.
1736 for (f = (char_u *)CPO_VIM; *f != NUL; ++f)
1737 if (vim_strchr(p_cpo, *f) == NULL
1738 && (t = vim_strchr(si->sn_save_cpo, *f)) != NULL)
1739 // flag was removed, also remove it from the saved 'cpo'
1740 mch_memmove(t, t + 1, STRLEN(t));
1741 for (f = p_cpo; *f != NUL; ++f)
1742 if (vim_strchr((char_u *)CPO_VIM, *f) == NULL
1743 && vim_strchr(si->sn_save_cpo, *f) == NULL)
1744 {
1745 // flag was added, also add it to the saved 'cpo'
1746 t = alloc(STRLEN(si->sn_save_cpo) + 2);
1747 if (t != NULL)
1748 {
1749 *t = *f;
1750 STRCPY(t + 1, si->sn_save_cpo);
1751 vim_free(si->sn_save_cpo);
1752 si->sn_save_cpo = t;
1753 }
1754 }
1755 }
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001756 set_option_value_give_err((char_u *)"cpo",
1757 0L, si->sn_save_cpo, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001758 }
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001759 VIM_CLEAR(si->sn_save_cpo);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001760
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001761 restore_funccal();
1762# ifdef FEAT_PROFILE
1763 if (do_profiling == PROF_YES)
1764 prof_child_exit(&wait_start); // leaving a child now
1765# endif
1766#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001767 current_sctx = save_current_sctx;
1768
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001769 if (cookie.fp != NULL)
1770 fclose(cookie.fp);
1771 if (cookie.source_from_buf)
1772 ga_clear_strings(&cookie.buflines);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001773 vim_free(cookie.nextline);
1774 vim_free(firstline);
1775 convert_setup(&cookie.conv, NULL, NULL);
1776
1777 if (trigger_source_post)
1778 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1779
1780theend:
Bram Moolenaar0af2ecf2022-08-24 17:05:56 +01001781#ifdef FEAT_EVAL
Bram Moolenaar753885b2022-08-24 16:30:36 +01001782 if (sid > 0 && ret_sid != NULL
1783 && fname_not_fixed != NULL && fname_exp != NULL)
1784 {
1785 int not_fixed_sid = find_script_by_name(fname_not_fixed);
1786
1787 // If "fname_not_fixed" is a symlink then we source the linked file.
1788 // If the original name is in the script list we add the ID of the
1789 // script that was actually sourced.
1790 if (SCRIPT_ID_VALID(not_fixed_sid) && not_fixed_sid != sid)
1791 SCRIPT_ITEM(not_fixed_sid)->sn_sourced_sid = sid;
1792 }
Bram Moolenaar0af2ecf2022-08-24 17:05:56 +01001793#endif
Bram Moolenaar753885b2022-08-24 16:30:36 +01001794
1795 vim_free(fname_not_fixed);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001796 vim_free(fname_exp);
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001797 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001798#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001799 estack_compiling = save_estack_compiling;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001800#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001801 return retval;
1802}
1803
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001804 int
1805do_source(
1806 char_u *fname,
1807 int check_other, // check for .vimrc and _vimrc
1808 int is_vimrc, // DOSO_ value
Bram Moolenaar753885b2022-08-24 16:30:36 +01001809 int *ret_sid)
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001810{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001811 return do_source_ext(fname, check_other, is_vimrc, ret_sid, NULL, FALSE);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001812}
1813
1814
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001815#if defined(FEAT_EVAL) || defined(PROTO)
1816
1817/*
1818 * ":scriptnames"
1819 */
1820 void
1821ex_scriptnames(exarg_T *eap)
1822{
1823 int i;
1824
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001825 if (eap->addr_count > 0 || *eap->arg != NUL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001826 {
1827 // :script {scriptId}: edit the script
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001828 if (eap->addr_count > 0 && !SCRIPT_ID_VALID(eap->line2))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001829 emsg(_(e_invalid_argument));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001830 else
1831 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001832 if (eap->addr_count > 0)
1833 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
1834 else
1835 {
1836 expand_env(eap->arg, NameBuff, MAXPATHL);
1837 eap->arg = NameBuff;
1838 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001839 do_exedit(eap, NULL);
1840 }
1841 return;
1842 }
1843
1844 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar6079da72022-01-18 14:16:59 +00001845 {
1846 scriptitem_T *si = SCRIPT_ITEM(i);
1847
1848 if (si->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001849 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01001850 char sourced_buf[20];
1851
Bram Moolenaar6079da72022-01-18 14:16:59 +00001852 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar753885b2022-08-24 16:30:36 +01001853 if (si->sn_sourced_sid > 0)
1854 vim_snprintf(sourced_buf, 20, "->%d", si->sn_sourced_sid);
1855 else
1856 sourced_buf[0] = NUL;
1857 vim_snprintf((char *)IObuff, IOSIZE, "%3d%s%s: %s",
Bram Moolenaar6079da72022-01-18 14:16:59 +00001858 i,
Bram Moolenaar753885b2022-08-24 16:30:36 +01001859 sourced_buf,
Bram Moolenaar6079da72022-01-18 14:16:59 +00001860 si->sn_state == SN_STATE_NOT_LOADED ? " A" : "",
1861 NameBuff);
Bram Moolenaar769f5892022-02-09 14:31:05 +00001862 if (!message_filtered(IObuff))
1863 {
1864 msg_putchar('\n');
1865 msg_outtrans(IObuff);
1866 out_flush(); // output one line at a time
1867 ui_breakcheck();
1868 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001869 }
Bram Moolenaar6079da72022-01-18 14:16:59 +00001870 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001871}
1872
1873# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1874/*
1875 * Fix slashes in the list of script names for 'shellslash'.
1876 */
1877 void
1878scriptnames_slash_adjust(void)
1879{
1880 int i;
1881
1882 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001883 if (SCRIPT_ITEM(i)->sn_name != NULL)
1884 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001885}
1886# endif
1887
1888/*
1889 * Get a pointer to a script name. Used for ":verbose set".
Bram Moolenaar74667062020-12-28 15:41:41 +01001890 * Message appended to "Last set from "
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001891 */
1892 char_u *
1893get_scriptname(scid_T id)
1894{
1895 if (id == SID_MODELINE)
1896 return (char_u *)_("modeline");
1897 if (id == SID_CMDARG)
1898 return (char_u *)_("--cmd argument");
1899 if (id == SID_CARG)
1900 return (char_u *)_("-c argument");
1901 if (id == SID_ENV)
1902 return (char_u *)_("environment variable");
1903 if (id == SID_ERROR)
1904 return (char_u *)_("error handler");
Bram Moolenaar74667062020-12-28 15:41:41 +01001905 if (id == SID_WINLAYOUT)
1906 return (char_u *)_("changed window size");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001907 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001908}
1909
1910# if defined(EXITFREE) || defined(PROTO)
1911 void
1912free_scriptnames(void)
1913{
1914 int i;
1915
1916 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02001917 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001918 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001920 // the variables themselves are cleared in evalvars_clear()
1921 vim_free(si->sn_vars);
1922
1923 vim_free(si->sn_name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001924 free_imports_and_script_vars(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001925 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02001926# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001927 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02001928# endif
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00001929 vim_free(si->sn_autoload_prefix);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001930 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02001931 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001932 ga_clear(&script_items);
1933}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001934
1935 void
1936free_autoload_scriptnames(void)
1937{
1938 ga_clear_strings(&ga_loaded);
1939}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001940# endif
1941
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001942 linenr_T
Bram Moolenaar66250c92020-08-20 15:02:42 +02001943get_sourced_lnum(
1944 char_u *(*fgetline)(int, void *, int, getline_opt_T),
1945 void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001946{
1947 return fgetline == getsourceline
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001948 ? ((source_cookie_T *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001949 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001950}
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01001951
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01001952/*
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01001953 * Return a List of script-local functions defined in the script with id
1954 * 'sid'.
1955 */
1956 static list_T *
1957get_script_local_funcs(scid_T sid)
1958{
1959 hashtab_T *functbl;
1960 hashitem_T *hi;
1961 long_u todo;
1962 list_T *l;
1963
1964 l = list_alloc();
1965 if (l == NULL)
1966 return NULL;
1967
1968 // Iterate through all the functions in the global function hash table
1969 // looking for functions with script ID 'sid'.
1970 functbl = func_tbl_get();
1971 todo = functbl->ht_used;
1972 for (hi = functbl->ht_array; todo > 0; ++hi)
1973 {
1974 ufunc_T *fp;
1975
1976 if (HASHITEM_EMPTY(hi))
1977 continue;
1978
1979 --todo;
1980 fp = HI2UF(hi);
1981
1982 // Add active functions with script id == 'sid'
1983 if (!(fp->uf_flags & FC_DEAD) && (fp->uf_script_ctx.sc_sid == sid))
1984 {
1985 char_u *name;
1986
1987 if (fp->uf_name_exp != NULL)
1988 name = fp->uf_name_exp;
1989 else
1990 name = fp->uf_name;
1991
1992 list_append_string(l, name, -1);
1993 }
1994 }
1995
1996 return l;
1997}
1998
1999/*
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002000 * getscriptinfo() function
2001 */
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002002 void
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002003f_getscriptinfo(typval_T *argvars, typval_T *rettv)
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002004{
2005 int i;
2006 list_T *l;
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002007 char_u *pat = NULL;
2008 regmatch_T regmatch;
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002009 int filterpat = FALSE;
2010 scid_T sid = -1;
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002011
2012 if (rettv_list_alloc(rettv) == FAIL)
2013 return;
2014
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002015 if (check_for_opt_dict_arg(argvars, 0) == FAIL)
2016 return;
2017
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002018 l = rettv->vval.v_list;
2019
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002020 regmatch.regprog = NULL;
2021 regmatch.rm_ic = p_ic;
2022
2023 if (argvars[0].v_type == VAR_DICT)
2024 {
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002025 sid = dict_get_number_def(argvars[0].vval.v_dict, "sid", -1);
2026 if (sid == -1)
2027 {
2028 pat = dict_get_string(argvars[0].vval.v_dict, "name", TRUE);
2029 if (pat != NULL)
2030 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2031 if (regmatch.regprog != NULL)
2032 filterpat = TRUE;
2033 }
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002034 }
2035
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002036 for (i = 1; i <= script_items.ga_len; ++i)
2037 {
2038 scriptitem_T *si = SCRIPT_ITEM(i);
2039 dict_T *d;
2040
2041 if (si->sn_name == NULL)
2042 continue;
2043
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002044 if (filterpat && !vim_regexec(&regmatch, si->sn_name, (colnr_T)0))
2045 continue;
2046
2047 if (sid != -1 && sid != i)
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002048 continue;
2049
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002050 if ((d = dict_alloc()) == NULL
2051 || list_append_dict(l, d) == FAIL
2052 || dict_add_string(d, "name", si->sn_name) == FAIL
2053 || dict_add_number(d, "sid", i) == FAIL
Bram Moolenaar753885b2022-08-24 16:30:36 +01002054 || dict_add_number(d, "sourced", si->sn_sourced_sid) == FAIL
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002055 || dict_add_number(d, "version", si->sn_version) == FAIL
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002056 || dict_add_bool(d, "autoload",
2057 si->sn_state == SN_STATE_NOT_LOADED) == FAIL)
2058 return;
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002059
2060 // When a filter pattern is specified to return information about only
2061 // specific script(s), also add the script-local variables and
2062 // functions.
2063 if (sid != -1)
2064 {
2065 dict_T *var_dict;
2066
2067 var_dict = dict_copy(&si->sn_vars->sv_dict, TRUE, TRUE,
2068 get_copyID());
2069 if (var_dict == NULL
2070 || dict_add_dict(d, "variables", var_dict) == FAIL
2071 || dict_add_list(d, "functions",
2072 get_script_local_funcs(sid)) == FAIL)
2073 return;
2074 }
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002075 }
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002076
2077 vim_regfree(regmatch.regprog);
2078 vim_free(pat);
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002079}
2080
Dominique Pelle748b3082022-01-08 12:41:16 +00002081#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002082
2083 static char_u *
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002084get_one_sourceline(source_cookie_T *sp)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002085{
2086 garray_T ga;
2087 int len;
2088 int c;
2089 char_u *buf;
2090#ifdef USE_CRNL
2091 int has_cr; // CR-LF found
2092#endif
2093 int have_read = FALSE;
2094
2095 // use a growarray to store the sourced line
2096 ga_init2(&ga, 1, 250);
2097
2098 // Loop until there is a finished line (or end-of-file).
2099 ++sp->sourcing_lnum;
2100 for (;;)
2101 {
2102 // make room to read at least 120 (more) characters
2103 if (ga_grow(&ga, 120) == FAIL)
2104 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002105 if (sp->source_from_buf)
2106 {
2107 if (sp->buf_lnum >= sp->buflines.ga_len)
2108 break; // all the lines are processed
2109 ga_concat(&ga, ((char_u **)sp->buflines.ga_data)[sp->buf_lnum]);
2110 sp->buf_lnum++;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01002111 if (ga_grow(&ga, 1) == FAIL)
2112 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002113 buf = (char_u *)ga.ga_data;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01002114 buf[ga.ga_len++] = NUL;
Bram Moolenaar4748c4b2022-05-17 17:47:07 +01002115 len = ga.ga_len;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002116 }
2117 else
2118 {
2119 buf = (char_u *)ga.ga_data;
2120 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
2121 sp->fp) == NULL)
2122 break;
Bram Moolenaar4748c4b2022-05-17 17:47:07 +01002123 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002124 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002125#ifdef USE_CRNL
2126 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
2127 // CTRL-Z by its own, or after a NL.
2128 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
2129 && sp->fileformat == EOL_DOS
2130 && buf[len - 1] == Ctrl_Z)
2131 {
2132 buf[len - 1] = NUL;
2133 break;
2134 }
2135#endif
2136
2137 have_read = TRUE;
2138 ga.ga_len = len;
2139
2140 // If the line was longer than the buffer, read more.
2141 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
2142 continue;
2143
2144 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
2145 {
2146#ifdef USE_CRNL
2147 has_cr = (len >= 2 && buf[len - 2] == '\r');
2148 if (sp->fileformat == EOL_UNKNOWN)
2149 {
2150 if (has_cr)
2151 sp->fileformat = EOL_DOS;
2152 else
2153 sp->fileformat = EOL_UNIX;
2154 }
2155
2156 if (sp->fileformat == EOL_DOS)
2157 {
2158 if (has_cr) // replace trailing CR
2159 {
2160 buf[len - 2] = '\n';
2161 --len;
2162 --ga.ga_len;
2163 }
2164 else // lines like ":map xx yy^M" will have failed
2165 {
2166 if (!sp->error)
2167 {
2168 msg_source(HL_ATTR(HLF_W));
2169 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
2170 }
2171 sp->error = TRUE;
2172 sp->fileformat = EOL_UNIX;
2173 }
2174 }
2175#endif
2176 // The '\n' is escaped if there is an odd number of ^V's just
2177 // before it, first set "c" just before the 'V's and then check
2178 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
2179 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
2180 ;
2181 if ((len & 1) != (c & 1)) // escaped NL, read more
2182 {
2183 ++sp->sourcing_lnum;
2184 continue;
2185 }
2186
2187 buf[len - 1] = NUL; // remove the NL
2188 }
2189
2190 // Check for ^C here now and then, so recursive :so can be broken.
2191 line_breakcheck();
2192 break;
2193 }
2194
2195 if (have_read)
2196 return (char_u *)ga.ga_data;
2197
2198 vim_free(ga.ga_data);
2199 return NULL;
2200}
2201
2202/*
2203 * Get one full line from a sourced file.
2204 * Called by do_cmdline() when it's called from do_source().
2205 *
2206 * Return a pointer to the line in allocated memory.
2207 * Return NULL for end-of-file or some error.
2208 */
2209 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02002210getsourceline(
2211 int c UNUSED,
2212 void *cookie,
2213 int indent UNUSED,
2214 getline_opt_T options)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002215{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002216 source_cookie_T *sp = (source_cookie_T *)cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002217 char_u *line;
2218 char_u *p;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002219 int do_vim9_all = in_vim9script()
2220 && options == GETLINE_CONCAT_ALL;
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002221 int do_bar_cont = do_vim9_all
2222 || options == GETLINE_CONCAT_CONTBAR;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002223
2224#ifdef FEAT_EVAL
2225 // If breakpoints have been added/deleted need to check for it.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002226 if ((sp->dbg_tick < debug_tick) && !sp->source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002227 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002228 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002229 sp->dbg_tick = debug_tick;
2230 }
2231# ifdef FEAT_PROFILE
2232 if (do_profiling == PROF_YES)
2233 script_line_end();
2234# endif
2235#endif
2236
2237 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002238 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002239
2240 // Get current line. If there is a read-ahead line, use it, otherwise get
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002241 // one now. "fp" is NULL if actually using a string.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002242 if (sp->finished || (!sp->source_from_buf && sp->fp == NULL))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002243 line = NULL;
2244 else if (sp->nextline == NULL)
2245 line = get_one_sourceline(sp);
2246 else
2247 {
2248 line = sp->nextline;
2249 sp->nextline = NULL;
2250 ++sp->sourcing_lnum;
2251 }
2252#ifdef FEAT_PROFILE
2253 if (line != NULL && do_profiling == PROF_YES)
2254 script_line_start();
2255#endif
2256
2257 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
2258 // contain the 'C' flag.
Bram Moolenaar66250c92020-08-20 15:02:42 +02002259 if (line != NULL && options != GETLINE_NONE
2260 && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002261 {
Bram Moolenaar5072b472021-06-03 21:56:10 +02002262 int comment_char = in_vim9script() ? '#' : '"';
2263
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002264 // compensate for the one line read-ahead
2265 --sp->sourcing_lnum;
2266
2267 // Get the next line and concatenate it when it starts with a
2268 // backslash. We always need to read the next line, keep it in
2269 // sp->nextline.
2270 /* Also check for a comment in between continuation lines: "\ */
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002271 // Also check for a Vim9 comment, empty line, line starting with '|',
2272 // but not "||".
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002273 sp->nextline = get_one_sourceline(sp);
2274 if (sp->nextline != NULL
2275 && (*(p = skipwhite(sp->nextline)) == '\\'
Bram Moolenaar5072b472021-06-03 21:56:10 +02002276 || (p[0] == comment_char
2277 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002278 || (do_vim9_all && (*p == NUL
2279 || vim9_comment_start(p)))
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002280 || (do_bar_cont && p[0] == '|' && p[1] != '|')))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002281 {
2282 garray_T ga;
2283
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002284 ga_init2(&ga, sizeof(char_u), 400);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002285 ga_concat(&ga, line);
2286 if (*p == '\\')
2287 ga_concat(&ga, p + 1);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002288 else if (*p == '|')
2289 {
2290 ga_concat(&ga, (char_u *)" ");
2291 ga_concat(&ga, p);
2292 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002293 for (;;)
2294 {
2295 vim_free(sp->nextline);
2296 sp->nextline = get_one_sourceline(sp);
2297 if (sp->nextline == NULL)
2298 break;
2299 p = skipwhite(sp->nextline);
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002300 if (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|'))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002301 {
2302 // Adjust the growsize to the current length to speed up
2303 // concatenating many lines.
2304 if (ga.ga_len > 400)
2305 {
2306 if (ga.ga_len > 8000)
2307 ga.ga_growsize = 8000;
2308 else
2309 ga.ga_growsize = ga.ga_len;
2310 }
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002311 if (*p == '\\')
2312 ga_concat(&ga, p + 1);
2313 else
2314 {
2315 ga_concat(&ga, (char_u *)" ");
2316 ga_concat(&ga, p);
2317 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002318 }
Bram Moolenaar5072b472021-06-03 21:56:10 +02002319 else if (!(p[0] == (comment_char)
Bram Moolenaar0f37e352021-06-02 15:28:15 +02002320 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002321 && !(do_vim9_all && (*p == NUL || vim9_comment_start(p))))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002322 break;
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002323 /* drop a # comment or "\ comment line */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002324 }
2325 ga_append(&ga, NUL);
2326 vim_free(line);
2327 line = ga.ga_data;
2328 }
2329 }
2330
2331 if (line != NULL && sp->conv.vc_type != CONV_NONE)
2332 {
2333 char_u *s;
2334
2335 // Convert the encoding of the script line.
2336 s = string_convert(&sp->conv, line, NULL);
2337 if (s != NULL)
2338 {
2339 vim_free(line);
2340 line = s;
2341 }
2342 }
2343
2344#ifdef FEAT_EVAL
2345 // Did we encounter a breakpoint?
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002346 if (!sp->source_from_buf && sp->breakpoint != 0
2347 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002348 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002349 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002350 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002351 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002352 sp->dbg_tick = debug_tick;
2353 }
2354#endif
2355
2356 return line;
2357}
2358
2359/*
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002360 * Returns TRUE if sourcing a script either from a file or a buffer.
2361 * Otherwise returns FALSE.
2362 */
2363 int
2364sourcing_a_script(exarg_T *eap)
2365{
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002366 return (getline_equal(eap->getline, eap->cookie, getsourceline));
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002367}
2368
2369/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002370 * ":scriptencoding": Set encoding conversion for a sourced script.
2371 */
2372 void
2373ex_scriptencoding(exarg_T *eap)
2374{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002375 source_cookie_T *sp;
2376 char_u *name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002377
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002378 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002379 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00002380 emsg(_(e_scriptencoding_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002381 return;
2382 }
2383
2384 if (*eap->arg != NUL)
2385 {
2386 name = enc_canonize(eap->arg);
2387 if (name == NULL) // out of memory
2388 return;
2389 }
2390 else
2391 name = eap->arg;
2392
2393 // Setup for conversion from the specified encoding to 'encoding'.
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002394 sp = (source_cookie_T *)getline_cookie(eap->getline, eap->cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002395 convert_setup(&sp->conv, name, p_enc);
2396
2397 if (name != eap->arg)
2398 vim_free(name);
2399}
2400
2401/*
2402 * ":scriptversion": Set Vim script version for a sourced script.
2403 */
2404 void
2405ex_scriptversion(exarg_T *eap UNUSED)
2406{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002407 int nr;
2408
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002409 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002410 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002411 emsg(_(e_scriptversion_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002412 return;
2413 }
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002414 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002416 emsg(_(e_cannot_use_scriptversion_after_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 return;
2418 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002419
2420 nr = getdigits(&eap->arg);
2421 if (nr == 0 || *eap->arg != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002422 emsg(_(e_invalid_argument));
Bram Moolenaar67979662020-06-20 22:50:47 +02002423 else if (nr > SCRIPT_VERSION_MAX)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002424 semsg(_(e_scriptversion_not_supported_nr), nr);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002425 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002426 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002427 current_sctx.sc_version = nr;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002428#ifdef FEAT_EVAL
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002429 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002430#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002431 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002432}
2433
2434#if defined(FEAT_EVAL) || defined(PROTO)
2435/*
2436 * ":finish": Mark a sourced file as finished.
2437 */
2438 void
2439ex_finish(exarg_T *eap)
2440{
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002441 if (sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002442 do_finish(eap, FALSE);
2443 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00002444 emsg(_(e_finish_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002445}
2446
2447/*
2448 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
2449 * Also called for a pending finish at the ":endtry" or after returning from
2450 * an extra do_cmdline(). "reanimate" is used in the latter case.
2451 */
2452 void
2453do_finish(exarg_T *eap, int reanimate)
2454{
2455 int idx;
2456
2457 if (reanimate)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002458 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002459 eap->cookie))->finished = FALSE;
2460
2461 // Cleanup (and inactivate) conditionals, but stop when a try conditional
2462 // not in its finally clause (which then is to be executed next) is found.
2463 // In this case, make the ":finish" pending for execution at the ":endtry".
2464 // Otherwise, finish normally.
2465 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
2466 if (idx >= 0)
2467 {
2468 eap->cstack->cs_pending[idx] = CSTP_FINISH;
2469 report_make_pending(CSTP_FINISH, NULL);
2470 }
2471 else
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002472 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002473 eap->cookie))->finished = TRUE;
2474}
2475
2476
2477/*
2478 * Return TRUE when a sourced file had the ":finish" command: Don't give error
2479 * message for missing ":endif".
2480 * Return FALSE when not sourcing a file.
2481 */
2482 int
2483source_finished(
Bram Moolenaar66250c92020-08-20 15:02:42 +02002484 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002485 void *cookie)
2486{
2487 return (getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002488 && ((source_cookie_T *)getline_cookie(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002489 fgetline, cookie))->finished);
2490}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002491
2492/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002493 * Find the path of a script below the "autoload" directory.
2494 * Returns NULL if there is no "/autoload/" in the script name.
2495 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01002496 static char_u *
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002497script_name_after_autoload(scriptitem_T *si)
2498{
2499 char_u *p = si->sn_name;
2500 char_u *res = NULL;
2501
2502 for (;;)
2503 {
2504 char_u *n = (char_u *)strstr((char *)p, "autoload");
2505
2506 if (n == NULL)
2507 break;
2508 if (n > p && vim_ispathsep(n[-1]) && vim_ispathsep(n[8]))
2509 res = n + 9;
2510 p = n + 8;
2511 }
2512 return res;
2513}
2514
2515/*
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002516 * For an autoload script "autoload/dir/script.vim" return the prefix
2517 * "dir#script#" in allocated memory.
2518 * Returns NULL if anything is wrong.
2519 */
2520 char_u *
2521get_autoload_prefix(scriptitem_T *si)
2522{
2523 char_u *p = script_name_after_autoload(si);
2524 char_u *prefix;
2525
2526 if (p == NULL)
2527 return NULL;
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002528 prefix = vim_strsave(p);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002529 if (prefix == NULL)
2530 return NULL;
2531
2532 // replace all '/' with '#' and locate ".vim" at the end
2533 for (p = prefix; *p != NUL; p += mb_ptr2len(p))
2534 {
2535 if (vim_ispathsep(*p))
2536 *p = '#';
2537 else if (STRCMP(p, ".vim") == 0)
2538 {
2539 p[0] = '#';
2540 p[1] = NUL;
2541 return prefix;
2542 }
2543 }
2544
2545 // did not find ".vim" at the end
2546 vim_free(prefix);
2547 return NULL;
2548}
2549
2550/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002551 * If in a Vim9 autoload script return "name" with the autoload prefix for the
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002552 * script. If successful the returned name is allocated.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002553 * Otherwise it returns "name" unmodified.
2554 */
2555 char_u *
2556may_prefix_autoload(char_u *name)
2557{
2558 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
2559 {
2560 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2561
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002562 if (si->sn_autoload_prefix != NULL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002563 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002564 char_u *basename = name;
2565 size_t len;
2566 char_u *res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002567
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002568 if (*name == K_SPECIAL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002569 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002570 char_u *p = vim_strchr(name, '_');
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002571
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002572 // skip over "<SNR>99_"
2573 if (p != NULL)
2574 basename = p + 1;
2575 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002576
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002577 len = STRLEN(si->sn_autoload_prefix) + STRLEN(basename) + 2;
2578 res = alloc(len);
2579 if (res != NULL)
2580 {
2581 vim_snprintf((char *)res, len, "%s%s",
2582 si->sn_autoload_prefix, basename);
2583 return res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002584 }
2585 }
2586 }
2587 return name;
2588}
2589
2590/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002591 * Return the autoload script name for a function or variable name.
2592 * Returns NULL when out of memory.
2593 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
2594 */
2595 char_u *
2596autoload_name(char_u *name)
2597{
2598 char_u *p, *q = NULL;
2599 char_u *scriptname;
2600
2601 // Get the script file name: replace '#' with '/', append ".vim".
2602 scriptname = alloc(STRLEN(name) + 14);
2603 if (scriptname == NULL)
2604 return NULL;
2605 STRCPY(scriptname, "autoload/");
Bram Moolenaara1773442020-08-12 15:21:22 +02002606 STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002607 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
2608 q = p, ++p)
2609 *p = '/';
2610 STRCPY(q, ".vim");
2611 return scriptname;
2612}
2613
2614/*
2615 * If "name" has a package name try autoloading the script for it.
2616 * Return TRUE if a package was loaded.
2617 */
2618 int
2619script_autoload(
2620 char_u *name,
2621 int reload) // load script again when already loaded
2622{
2623 char_u *p;
2624 char_u *scriptname, *tofree;
2625 int ret = FALSE;
2626 int i;
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002627 int ret_sid;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002628
Bram Moolenaar89445512022-04-14 12:58:23 +01002629 // If the name starts with "<SNR>123_" then "123" is the script ID.
2630 if (name[0] == K_SPECIAL && name[1] == KS_EXTRA && name[2] == KE_SNR)
2631 {
2632 p = name + 3;
2633 ret_sid = (int)getdigits(&p);
2634 if (*p == '_' && SCRIPT_ID_VALID(ret_sid))
2635 {
2636 may_load_script(ret_sid, &ret);
2637 return ret;
2638 }
2639 }
2640
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002641 // If there is no '#' after name[0] there is no package name.
2642 p = vim_strchr(name, AUTOLOAD_CHAR);
2643 if (p == NULL || p == name)
2644 return FALSE;
2645
2646 tofree = scriptname = autoload_name(name);
2647 if (scriptname == NULL)
2648 return FALSE;
2649
2650 // Find the name in the list of previously loaded package names. Skip
2651 // "autoload/", it's always the same.
2652 for (i = 0; i < ga_loaded.ga_len; ++i)
2653 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2654 break;
2655 if (!reload && i < ga_loaded.ga_len)
2656 ret = FALSE; // was loaded already
2657 else
2658 {
2659 // Remember the name if it wasn't loaded already.
2660 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2661 {
2662 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2663 tofree = NULL;
2664 }
2665
2666 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002667 // Use "ret_sid" to avoid loading the same script again.
=?UTF-8?q?Bj=C3=B6rn=20Linse?=223a9502022-01-31 17:26:05 +00002668 if (source_in_path(p_rtp, scriptname, DIP_START, &ret_sid) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002669 ret = TRUE;
2670 }
2671
2672 vim_free(tofree);
2673 return ret;
2674}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002675#endif