blob: 3faed4a700636e482491186779125a5243dca41c [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 Lakshmanan85b43c62022-03-21 19:45:17 +000026static int do_source_ext(char_u *fname, int check_other, int is_vimrc, int *ret_sid, exarg_T *eap);
27
Bram Moolenaar307c5a52019-08-25 15:41:00 +020028/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010029 * Initialize the execution stack.
30 */
31 void
32estack_init(void)
33{
34 estack_T *entry;
35
36 if (ga_grow(&exestack, 10) == FAIL)
37 mch_exit(0);
38 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
39 entry->es_type = ETYPE_TOP;
40 entry->es_name = NULL;
41 entry->es_lnum = 0;
Bram Moolenaar09d46402019-12-29 23:53:01 +010042#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010043 entry->es_info.ufunc = NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +010044#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010045 ++exestack.ga_len;
46}
47
48/*
49 * Add an item to the execution stack.
50 * Returns the new entry or NULL when out of memory.
51 */
52 estack_T *
53estack_push(etype_T type, char_u *name, long lnum)
54{
55 estack_T *entry;
56
57 // If memory allocation fails then we'll pop more than we push, eventually
58 // at the top level it will be OK again.
59 if (ga_grow(&exestack, 1) == OK)
60 {
61 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
62 entry->es_type = type;
63 entry->es_name = name;
64 entry->es_lnum = lnum;
Bram Moolenaar09d46402019-12-29 23:53:01 +010065#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010066 entry->es_info.ufunc = NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +010067#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010068 ++exestack.ga_len;
69 return entry;
70 }
71 return NULL;
72}
73
Bram Moolenaar09d46402019-12-29 23:53:01 +010074#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010075/*
76 * Add a user function to the execution stack.
77 */
Bram Moolenaarc620c052020-07-08 15:16:19 +020078 estack_T *
Bram Moolenaar25e0f582020-05-25 22:36:50 +020079estack_push_ufunc(ufunc_T *ufunc, long lnum)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010080{
Bram Moolenaar25e0f582020-05-25 22:36:50 +020081 estack_T *entry = estack_push(ETYPE_UFUNC,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010082 ufunc->uf_name_exp != NULL
83 ? ufunc->uf_name_exp : ufunc->uf_name, lnum);
84 if (entry != NULL)
85 entry->es_info.ufunc = ufunc;
Bram Moolenaarc620c052020-07-08 15:16:19 +020086 return entry;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010087}
Bram Moolenaar25e0f582020-05-25 22:36:50 +020088
89/*
90 * Return TRUE if "ufunc" with "lnum" is already at the top of the exe stack.
91 */
92 int
93estack_top_is_ufunc(ufunc_T *ufunc, long lnum)
94{
95 estack_T *entry;
96
97 if (exestack.ga_len == 0)
98 return FALSE;
99 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
100 return entry->es_type == ETYPE_UFUNC
101 && STRCMP( entry->es_name, ufunc->uf_name_exp != NULL
102 ? ufunc->uf_name_exp : ufunc->uf_name) == 0
103 && entry->es_lnum == lnum;
104}
Bram Moolenaar09d46402019-12-29 23:53:01 +0100105#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100106
107/*
Bram Moolenaarc620c052020-07-08 15:16:19 +0200108 * Take an item off of the execution stack and return it.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100109 */
Bram Moolenaarc620c052020-07-08 15:16:19 +0200110 estack_T *
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100111estack_pop(void)
112{
Bram Moolenaarc620c052020-07-08 15:16:19 +0200113 if (exestack.ga_len == 0)
114 return NULL;
115 --exestack.ga_len;
116 return ((estack_T *)exestack.ga_data) + exestack.ga_len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100117}
118
119/*
120 * Get the current value for <sfile> in allocated memory.
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200121 * "which" is ESTACK_SFILE for <sfile> and ESTACK_STACK for <stack>.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100122 */
123 char_u *
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200124estack_sfile(estack_arg_T which UNUSED)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100125{
Bram Moolenaar85b09572019-12-30 10:57:00 +0100126 estack_T *entry;
127#ifdef FEAT_EVAL
Bram Moolenaara5d04232020-07-26 15:37:02 +0200128 garray_T ga;
Bram Moolenaar4d7a2482020-01-06 19:53:43 +0100129 size_t len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100130 int idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200131 etype_T last_type = ETYPE_SCRIPT;
132 char *type_name;
Bram Moolenaar85b09572019-12-30 10:57:00 +0100133#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100134
135 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100136#ifdef FEAT_EVAL
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200137 if (which == ESTACK_SFILE && entry->es_type != ETYPE_UFUNC)
Bram Moolenaar09d46402019-12-29 23:53:01 +0100138#endif
Bram Moolenaara5d04232020-07-26 15:37:02 +0200139 {
140 if (entry->es_name == NULL)
141 return NULL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100142 return vim_strsave(entry->es_name);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200143 }
Bram Moolenaar09d46402019-12-29 23:53:01 +0100144#ifdef FEAT_EVAL
Bram Moolenaarc4492712021-11-22 15:37:15 +0000145 // expand('<sfile>') works in a function for backwards compatibility, but
146 // may give an unexpected result. Disallow it in Vim 9 script.
147 if (which == ESTACK_SFILE && in_vim9script())
148 {
149 int save_emsg_off = emsg_off;
150
151 if (emsg_off == 1)
152 // f_expand() silences errors but we do want this one
153 emsg_off = 0;
154 emsg(_(e_cannot_expand_sfile_in_vim9_function));
155 emsg_off = save_emsg_off;
156 return NULL;
157 }
158
Bram Moolenaara5d04232020-07-26 15:37:02 +0200159 // Give information about each stack entry up to the root.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100160 // For a function we compose the call stack, as it was done in the past:
161 // "function One[123]..Two[456]..Three"
Bram Moolenaara5d04232020-07-26 15:37:02 +0200162 ga_init2(&ga, sizeof(char), 100);
163 for (idx = 0; idx < exestack.ga_len; ++idx)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100164 {
165 entry = ((estack_T *)exestack.ga_data) + idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200166 if (entry->es_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100167 {
Bram Moolenaara810db32020-09-11 17:59:23 +0200168 long lnum = 0;
169 char *dots;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200170
Bram Moolenaara5d04232020-07-26 15:37:02 +0200171 len = STRLEN(entry->es_name) + 15;
172 type_name = "";
173 if (entry->es_type != last_type)
174 {
175 switch (entry->es_type)
176 {
177 case ETYPE_SCRIPT: type_name = "script "; break;
178 case ETYPE_UFUNC: type_name = "function "; break;
179 default: type_name = ""; break;
180 }
181 last_type = entry->es_type;
182 }
183 len += STRLEN(type_name);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200184 if (ga_grow(&ga, (int)len) == FAIL)
Bram Moolenaara5d04232020-07-26 15:37:02 +0200185 break;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200186 if (idx == exestack.ga_len - 1)
187 lnum = which == ESTACK_STACK ? SOURCING_LNUM : 0;
188 else
189 lnum = entry->es_lnum;
Bram Moolenaara810db32020-09-11 17:59:23 +0200190 dots = idx == exestack.ga_len - 1 ? "" : "..";
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200191 if (lnum == 0)
192 // For the bottom entry of <sfile>: do not add the line number,
193 // it is used in <slnum>. Also leave it out when the number is
194 // not set.
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200195 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s%s",
Bram Moolenaara810db32020-09-11 17:59:23 +0200196 type_name, entry->es_name, dots);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200197 else
Bram Moolenaara810db32020-09-11 17:59:23 +0200198 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s[%ld]%s",
199 type_name, entry->es_name, lnum, dots);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200200 ga.ga_len += (int)STRLEN((char *)ga.ga_data + ga.ga_len);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100201 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100202 }
203
Bram Moolenaara5d04232020-07-26 15:37:02 +0200204 return (char_u *)ga.ga_data;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100205#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100206}
207
208/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200209 * ":runtime [what] {name}"
210 */
211 void
212ex_runtime(exarg_T *eap)
213{
214 char_u *arg = eap->arg;
215 char_u *p = skiptowhite(arg);
216 int len = (int)(p - arg);
217 int flags = eap->forceit ? DIP_ALL : 0;
218
219 if (STRNCMP(arg, "START", len) == 0)
220 {
221 flags += DIP_START + DIP_NORTP;
222 arg = skipwhite(arg + len);
223 }
224 else if (STRNCMP(arg, "OPT", len) == 0)
225 {
226 flags += DIP_OPT + DIP_NORTP;
227 arg = skipwhite(arg + len);
228 }
229 else if (STRNCMP(arg, "PACK", len) == 0)
230 {
231 flags += DIP_START + DIP_OPT + DIP_NORTP;
232 arg = skipwhite(arg + len);
233 }
234 else if (STRNCMP(arg, "ALL", len) == 0)
235 {
236 flags += DIP_START + DIP_OPT;
237 arg = skipwhite(arg + len);
238 }
239
240 source_runtime(arg, flags);
241}
242
243 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244source_callback(char_u *fname, void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200245{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200247}
248
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000249#ifdef FEAT_EVAL
250/*
251 * Find an already loaded script "name".
252 * If found returns its script ID. If not found returns -1.
253 */
254 static int
255find_script_by_name(char_u *name)
256{
257 int sid;
258 scriptitem_T *si;
259
260 for (sid = script_items.ga_len; sid > 0; --sid)
261 {
262 // We used to check inode here, but that doesn't work:
263 // - If a script is edited and written, it may get a different
264 // inode number, even though to the user it is the same script.
265 // - If a script is deleted and another script is written, with a
266 // different name, the inode may be re-used.
267 si = SCRIPT_ITEM(sid);
268 if (si->sn_name != NULL && fnamecmp(si->sn_name, name) == 0)
269 return sid;
270 }
271 return -1;
272}
273
274/*
275 * Add a new scriptitem with all items initialized.
276 * When running out of memory "error" is set to FAIL.
277 * Returns the script ID.
278 */
279 static int
280get_new_scriptitem(int *error)
281{
282 static scid_T last_current_SID = 0;
283 int sid = ++last_current_SID;
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000284 scriptitem_T *si = NULL;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000285
286 if (ga_grow(&script_items, (int)(sid - script_items.ga_len)) == FAIL)
287 {
288 *error = FAIL;
289 return sid;
290 }
291 while (script_items.ga_len < sid)
292 {
293 si = ALLOC_CLEAR_ONE(scriptitem_T);
294 if (si == NULL)
295 {
296 *error = FAIL;
297 return sid;
298 }
299 ++script_items.ga_len;
300 SCRIPT_ITEM(script_items.ga_len) = si;
301 si->sn_name = NULL;
302 si->sn_version = 1;
303
304 // Allocate the local script variables to use for this script.
305 new_script_vars(script_items.ga_len);
306 ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
307 hash_init(&si->sn_all_vars.dv_hashtab);
308 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
309 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
310# ifdef FEAT_PROFILE
311 si->sn_prof_on = FALSE;
312# endif
313 }
314
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000315 // "si" can't be NULL, check only to avoid a compiler warning
316 if (si != NULL)
317 // Used to check script variable index is still valid.
318 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000319
320 return sid;
321}
322
323 static void
324find_script_callback(char_u *fname, void *cookie)
325{
326 int sid;
327 int error = OK;
328 int *ret_sid = cookie;
329
330 sid = find_script_by_name(fname);
331 if (sid < 0)
332 {
333 // script does not exist yet, create a new scriptitem
334 sid = get_new_scriptitem(&error);
335 if (error == OK)
336 {
337 scriptitem_T *si = SCRIPT_ITEM(sid);
338
339 si->sn_name = vim_strsave(fname);
340 si->sn_state = SN_STATE_NOT_LOADED;
341 }
342 }
343 *ret_sid = sid;
344}
345#endif
346
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200347/*
348 * Find the file "name" in all directories in "path" and invoke
349 * "callback(fname, cookie)".
350 * "name" can contain wildcards.
351 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
352 * When "flags" has DIP_DIR: find directories instead of files.
353 * When "flags" has DIP_ERR: give an error message if there is no match.
354 *
355 * return FAIL when no file could be sourced, OK otherwise.
356 */
357 int
358do_in_path(
359 char_u *path,
360 char_u *name,
361 int flags,
362 void (*callback)(char_u *fname, void *ck),
363 void *cookie)
364{
365 char_u *rtp;
366 char_u *np;
367 char_u *buf;
368 char_u *rtp_copy;
369 char_u *tail;
370 int num_files;
371 char_u **files;
372 int i;
373 int did_one = FALSE;
374#ifdef AMIGA
375 struct Process *proc = (struct Process *)FindTask(0L);
376 APTR save_winptr = proc->pr_WindowPtr;
377
378 // Avoid a requester here for a volume that doesn't exist.
379 proc->pr_WindowPtr = (APTR)-1L;
380#endif
381
382 // Make a copy of 'runtimepath'. Invoking the callback may change the
383 // value.
384 rtp_copy = vim_strsave(path);
385 buf = alloc(MAXPATHL);
386 if (buf != NULL && rtp_copy != NULL)
387 {
Bram Moolenaar647a5302020-05-03 17:01:24 +0200388 if (p_verbose > 10 && name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200389 {
390 verbose_enter();
391 smsg(_("Searching for \"%s\" in \"%s\""),
392 (char *)name, (char *)path);
393 verbose_leave();
394 }
395
396 // Loop over all entries in 'runtimepath'.
397 rtp = rtp_copy;
398 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
399 {
400 size_t buflen;
401
402 // Copy the path from 'runtimepath' to buf[].
403 copy_option_part(&rtp, buf, MAXPATHL, ",");
404 buflen = STRLEN(buf);
405
406 // Skip after or non-after directories.
407 if (flags & (DIP_NOAFTER | DIP_AFTER))
408 {
409 int is_after = buflen >= 5
410 && STRCMP(buf + buflen - 5, "after") == 0;
411
412 if ((is_after && (flags & DIP_NOAFTER))
413 || (!is_after && (flags & DIP_AFTER)))
414 continue;
415 }
416
417 if (name == NULL)
418 {
419 (*callback)(buf, (void *) &cookie);
420 if (!did_one)
421 did_one = (cookie == NULL);
422 }
423 else if (buflen + STRLEN(name) + 2 < MAXPATHL)
424 {
425 add_pathsep(buf);
426 tail = buf + STRLEN(buf);
427
428 // Loop over all patterns in "name"
429 np = name;
430 while (*np != NUL && ((flags & DIP_ALL) || !did_one))
431 {
432 // Append the pattern from "name" to buf[].
433 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
434 "\t ");
435
Bram Moolenaar647a5302020-05-03 17:01:24 +0200436 if (p_verbose > 10)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200437 {
438 verbose_enter();
439 smsg(_("Searching for \"%s\""), buf);
440 verbose_leave();
441 }
442
443 // Expand wildcards, invoke the callback for each match.
444 if (gen_expand_wildcards(1, &buf, &num_files, &files,
445 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
446 {
447 for (i = 0; i < num_files; ++i)
448 {
449 (*callback)(files[i], cookie);
450 did_one = TRUE;
451 if (!(flags & DIP_ALL))
452 break;
453 }
454 FreeWild(num_files, files);
455 }
456 }
457 }
458 }
459 }
460 vim_free(buf);
461 vim_free(rtp_copy);
462 if (!did_one && name != NULL)
463 {
464 char *basepath = path == p_rtp ? "runtimepath" : "packpath";
465
466 if (flags & DIP_ERR)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000467 semsg(_(e_directory_not_found_in_str_str), basepath, name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200468 else if (p_verbose > 0)
469 {
470 verbose_enter();
471 smsg(_("not found in '%s': \"%s\""), basepath, name);
472 verbose_leave();
473 }
474 }
475
476#ifdef AMIGA
477 proc->pr_WindowPtr = save_winptr;
478#endif
479
480 return did_one ? OK : FAIL;
481}
482
483/*
484 * Find "name" in "path". When found, invoke the callback function for
485 * it: callback(fname, "cookie")
486 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first
487 * one is used.
488 * Returns OK when at least one match found, FAIL otherwise.
489 *
490 * If "name" is NULL calls callback for each entry in "path". Cookie is
491 * passed by reference in this case, setting it to NULL indicates that callback
492 * has done its job.
493 */
494 static int
495do_in_path_and_pp(
496 char_u *path,
497 char_u *name,
498 int flags,
499 void (*callback)(char_u *fname, void *ck),
500 void *cookie)
501{
502 int done = FAIL;
503 char_u *s;
504 int len;
505 char *start_dir = "pack/*/start/*/%s";
506 char *opt_dir = "pack/*/opt/*/%s";
507
508 if ((flags & DIP_NORTP) == 0)
509 done = do_in_path(path, name, flags, callback, cookie);
510
511 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
512 {
513 len = (int)(STRLEN(start_dir) + STRLEN(name));
514 s = alloc(len);
515 if (s == NULL)
516 return FAIL;
517 vim_snprintf((char *)s, len, start_dir, name);
518 done = do_in_path(p_pp, s, flags, callback, cookie);
519 vim_free(s);
520 }
521
522 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
523 {
524 len = (int)(STRLEN(opt_dir) + STRLEN(name));
525 s = alloc(len);
526 if (s == NULL)
527 return FAIL;
528 vim_snprintf((char *)s, len, opt_dir, name);
529 done = do_in_path(p_pp, s, flags, callback, cookie);
530 vim_free(s);
531 }
532
533 return done;
534}
535
536/*
537 * Just like do_in_path_and_pp(), using 'runtimepath' for "path".
538 */
539 int
540do_in_runtimepath(
541 char_u *name,
542 int flags,
543 void (*callback)(char_u *fname, void *ck),
544 void *cookie)
545{
546 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
547}
548
549/*
550 * Source the file "name" from all directories in 'runtimepath'.
551 * "name" can contain wildcards.
552 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
553 *
554 * return FAIL when no file could be sourced, OK otherwise.
555 */
556 int
557source_runtime(char_u *name, int flags)
558{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100559 return source_in_path(p_rtp, name, flags, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200560}
561
562/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000563 * Just like source_runtime(), but use "path" instead of 'runtimepath'
564 * and return the script ID in "ret_sid".
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200565 */
566 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200568{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200570}
571
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200572#if defined(FEAT_EVAL) || defined(PROTO)
573
574/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000575 * Find "name" in 'runtimepath'. If found a new scriptitem is created for it
576 * and it's script ID is returned.
577 * If not found returns -1.
578 */
579 int
580find_script_in_rtp(char_u *name)
581{
582 int sid = -1;
583
584 (void)do_in_path_and_pp(p_rtp, name, DIP_NOAFTER,
585 find_script_callback, &sid);
586 return sid;
587}
588
589/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200590 * Expand wildcards in "pat" and invoke do_source() for each match.
591 */
592 static void
593source_all_matches(char_u *pat)
594{
595 int num_files;
596 char_u **files;
597 int i;
598
599 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK)
600 {
601 for (i = 0; i < num_files; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100602 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200603 FreeWild(num_files, files);
604 }
605}
606
607/*
608 * Add the package directory to 'runtimepath'.
609 */
610 static int
611add_pack_dir_to_rtp(char_u *fname)
612{
613 char_u *p4, *p3, *p2, *p1, *p;
614 char_u *entry;
615 char_u *insp = NULL;
616 int c;
617 char_u *new_rtp;
618 int keep;
619 size_t oldlen;
620 size_t addlen;
621 size_t new_rtp_len;
622 char_u *afterdir = NULL;
623 size_t afterlen = 0;
624 char_u *after_insp = NULL;
625 char_u *ffname = NULL;
626 size_t fname_len;
627 char_u *buf = NULL;
628 char_u *rtp_ffname;
629 int match;
630 int retval = FAIL;
631
632 p4 = p3 = p2 = p1 = get_past_head(fname);
633 for (p = p1; *p; MB_PTR_ADV(p))
634 if (vim_ispathsep_nocolon(*p))
635 {
636 p4 = p3; p3 = p2; p2 = p1; p1 = p;
637 }
638
639 // now we have:
640 // rtp/pack/name/start/name
641 // p4 p3 p2 p1
642 //
643 // find the part up to "pack" in 'runtimepath'
644 c = *++p4; // append pathsep in order to expand symlink
645 *p4 = NUL;
646 ffname = fix_fname(fname);
647 *p4 = c;
648 if (ffname == NULL)
649 return FAIL;
650
651 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
652 // Also stop at the first "after" directory.
653 fname_len = STRLEN(ffname);
654 buf = alloc(MAXPATHL);
655 if (buf == NULL)
656 goto theend;
657 for (entry = p_rtp; *entry != NUL; )
658 {
659 char_u *cur_entry = entry;
660
661 copy_option_part(&entry, buf, MAXPATHL, ",");
662 if (insp == NULL)
663 {
664 add_pathsep(buf);
665 rtp_ffname = fix_fname(buf);
666 if (rtp_ffname == NULL)
667 goto theend;
668 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
669 vim_free(rtp_ffname);
670 if (match)
671 // Insert "ffname" after this entry (and comma).
672 insp = entry;
673 }
674
675 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
676 && p > buf
677 && vim_ispathsep(p[-1])
678 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
679 {
680 if (insp == NULL)
681 // Did not find "ffname" before the first "after" directory,
682 // insert it before this entry.
683 insp = cur_entry;
684 after_insp = cur_entry;
685 break;
686 }
687 }
688
689 if (insp == NULL)
690 // Both "fname" and "after" not found, append at the end.
691 insp = p_rtp + STRLEN(p_rtp);
692
693 // check if rtp/pack/name/start/name/after exists
694 afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
695 if (afterdir != NULL && mch_isdir(afterdir))
696 afterlen = STRLEN(afterdir) + 1; // add one for comma
697
698 oldlen = STRLEN(p_rtp);
699 addlen = STRLEN(fname) + 1; // add one for comma
700 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
701 if (new_rtp == NULL)
702 goto theend;
703
704 // We now have 'rtp' parts: {keep}{keep_after}{rest}.
705 // Create new_rtp, first: {keep},{fname}
706 keep = (int)(insp - p_rtp);
707 mch_memmove(new_rtp, p_rtp, keep);
708 new_rtp_len = keep;
709 if (*insp == NUL)
710 new_rtp[new_rtp_len++] = ','; // add comma before
711 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
712 new_rtp_len += addlen - 1;
713 if (*insp != NUL)
714 new_rtp[new_rtp_len++] = ','; // add comma after
715
716 if (afterlen > 0 && after_insp != NULL)
717 {
718 int keep_after = (int)(after_insp - p_rtp);
719
720 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
721 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
722 keep_after - keep);
723 new_rtp_len += keep_after - keep;
724 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
725 new_rtp_len += afterlen - 1;
726 new_rtp[new_rtp_len++] = ',';
727 keep = keep_after;
728 }
729
730 if (p_rtp[keep] != NUL)
731 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
732 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
733 else
734 new_rtp[new_rtp_len] = NUL;
735
736 if (afterlen > 0 && after_insp == NULL)
737 {
738 // Append afterdir when "after" was not found:
739 // {keep},{fname}{rest},{afterdir}
740 STRCAT(new_rtp, ",");
741 STRCAT(new_rtp, afterdir);
742 }
743
744 set_option_value((char_u *)"rtp", 0L, new_rtp, 0);
745 vim_free(new_rtp);
746 retval = OK;
747
748theend:
749 vim_free(buf);
750 vim_free(ffname);
751 vim_free(afterdir);
752 return retval;
753}
754
755/*
756 * Load scripts in "plugin" and "ftdetect" directories of the package.
757 */
758 static int
759load_pack_plugin(char_u *fname)
760{
761 static char *plugpat = "%s/plugin/**/*.vim";
762 static char *ftpat = "%s/ftdetect/*.vim";
763 int len;
764 char_u *ffname = fix_fname(fname);
765 char_u *pat = NULL;
766 int retval = FAIL;
767
768 if (ffname == NULL)
769 return FAIL;
770 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
771 pat = alloc(len);
772 if (pat == NULL)
773 goto theend;
774 vim_snprintf((char *)pat, len, plugpat, ffname);
775 source_all_matches(pat);
776
777 {
778 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
779
780 // If runtime/filetype.vim wasn't loaded yet, the scripts will be
781 // found when it loads.
782 if (cmd != NULL && eval_to_number(cmd) > 0)
783 {
784 do_cmdline_cmd((char_u *)"augroup filetypedetect");
785 vim_snprintf((char *)pat, len, ftpat, ffname);
786 source_all_matches(pat);
787 do_cmdline_cmd((char_u *)"augroup END");
788 }
789 vim_free(cmd);
790 }
791 vim_free(pat);
792 retval = OK;
793
794theend:
795 vim_free(ffname);
796 return retval;
797}
798
799// used for "cookie" of add_pack_plugin()
800static int APP_ADD_DIR;
801static int APP_LOAD;
802static int APP_BOTH;
803
804 static void
805add_pack_plugin(char_u *fname, void *cookie)
806{
807 if (cookie != &APP_LOAD)
808 {
809 char_u *buf = alloc(MAXPATHL);
810 char_u *p;
811 int found = FALSE;
812
813 if (buf == NULL)
814 return;
815 p = p_rtp;
816 while (*p != NUL)
817 {
818 copy_option_part(&p, buf, MAXPATHL, ",");
819 if (pathcmp((char *)buf, (char *)fname, -1) == 0)
820 {
821 found = TRUE;
822 break;
823 }
824 }
825 vim_free(buf);
826 if (!found)
827 // directory is not yet in 'runtimepath', add it
828 if (add_pack_dir_to_rtp(fname) == FAIL)
829 return;
830 }
831
832 if (cookie != &APP_ADD_DIR)
833 load_pack_plugin(fname);
834}
835
836/*
837 * Add all packages in the "start" directory to 'runtimepath'.
838 */
839 void
840add_pack_start_dirs(void)
841{
842 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
843 add_pack_plugin, &APP_ADD_DIR);
844}
845
846/*
847 * Load plugins from all packages in the "start" directory.
848 */
849 void
850load_start_packages(void)
851{
852 did_source_packages = TRUE;
853 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
854 add_pack_plugin, &APP_LOAD);
855}
856
857/*
858 * ":packloadall"
859 * Find plugins in the package directories and source them.
860 */
861 void
862ex_packloadall(exarg_T *eap)
863{
864 if (!did_source_packages || eap->forceit)
865 {
866 // First do a round to add all directories to 'runtimepath', then load
867 // the plugins. This allows for plugins to use an autoload directory
868 // of another plugin.
869 add_pack_start_dirs();
870 load_start_packages();
871 }
872}
873
874/*
875 * ":packadd[!] {name}"
876 */
877 void
878ex_packadd(exarg_T *eap)
879{
880 static char *plugpat = "pack/*/%s/%s";
881 int len;
882 char *pat;
883 int round;
884 int res = OK;
885
886 // Round 1: use "start", round 2: use "opt".
887 for (round = 1; round <= 2; ++round)
888 {
889 // Only look under "start" when loading packages wasn't done yet.
890 if (round == 1 && did_source_packages)
891 continue;
892
893 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
894 pat = alloc(len);
895 if (pat == NULL)
896 return;
897 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
898 // The first round don't give a "not found" error, in the second round
899 // only when nothing was found in the first round.
900 res = do_in_path(p_pp, (char_u *)pat,
901 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
902 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
903 vim_free(pat);
904 }
905}
906#endif
907
908/*
Bram Moolenaar26262f82019-09-04 20:59:15 +0200909 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
910 * list of file names in allocated memory.
911 */
912 void
913remove_duplicates(garray_T *gap)
914{
915 int i;
916 int j;
917 char_u **fnames = (char_u **)gap->ga_data;
918
919 sort_strings(fnames, gap->ga_len);
920 for (i = gap->ga_len - 1; i > 0; --i)
921 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
922 {
923 vim_free(fnames[i]);
924 for (j = i + 1; j < gap->ga_len; ++j)
925 fnames[j - 1] = fnames[j];
926 --gap->ga_len;
927 }
928}
929
930/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200931 * Expand color scheme, compiler or filetype names.
932 * Search from 'runtimepath':
933 * 'runtimepath'/{dirnames}/{pat}.vim
934 * When "flags" has DIP_START: search also from 'start' of 'packpath':
935 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
936 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
937 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
938 * "dirnames" is an array with one or more directory names.
939 */
940 int
941ExpandRTDir(
942 char_u *pat,
943 int flags,
944 int *num_file,
945 char_u ***file,
946 char *dirnames[])
947{
948 char_u *s;
949 char_u *e;
950 char_u *match;
951 garray_T ga;
952 int i;
953 int pat_len;
954
955 *num_file = 0;
956 *file = NULL;
957 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000958 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200959
960 for (i = 0; dirnames[i] != NULL; ++i)
961 {
962 s = alloc(STRLEN(dirnames[i]) + pat_len + 7);
963 if (s == NULL)
964 {
965 ga_clear_strings(&ga);
966 return FAIL;
967 }
968 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
969 globpath(p_rtp, s, &ga, 0);
970 vim_free(s);
971 }
972
973 if (flags & DIP_START) {
974 for (i = 0; dirnames[i] != NULL; ++i)
975 {
976 s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
977 if (s == NULL)
978 {
979 ga_clear_strings(&ga);
980 return FAIL;
981 }
982 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
983 globpath(p_pp, s, &ga, 0);
984 vim_free(s);
985 }
986 }
987
988 if (flags & DIP_OPT) {
989 for (i = 0; dirnames[i] != NULL; ++i)
990 {
991 s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
992 if (s == NULL)
993 {
994 ga_clear_strings(&ga);
995 return FAIL;
996 }
997 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
998 globpath(p_pp, s, &ga, 0);
999 vim_free(s);
1000 }
1001 }
1002
1003 for (i = 0; i < ga.ga_len; ++i)
1004 {
1005 match = ((char_u **)ga.ga_data)[i];
1006 s = match;
1007 e = s + STRLEN(s);
1008 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
1009 {
1010 e -= 4;
1011 for (s = e; s > match; MB_PTR_BACK(match, s))
1012 if (s < match || vim_ispathsep(*s))
1013 break;
1014 ++s;
1015 *e = NUL;
1016 mch_memmove(match, s, e - s + 1);
1017 }
1018 }
1019
1020 if (ga.ga_len == 0)
1021 return FAIL;
1022
1023 // Sort and remove duplicates which can happen when specifying multiple
1024 // directories in dirnames.
1025 remove_duplicates(&ga);
1026
1027 *file = ga.ga_data;
1028 *num_file = ga.ga_len;
1029 return OK;
1030}
1031
1032/*
1033 * Expand loadplugin names:
1034 * 'packpath'/pack/ * /opt/{pat}
1035 */
1036 int
1037ExpandPackAddDir(
1038 char_u *pat,
1039 int *num_file,
1040 char_u ***file)
1041{
1042 char_u *s;
1043 char_u *e;
1044 char_u *match;
1045 garray_T ga;
1046 int i;
1047 int pat_len;
1048
1049 *num_file = 0;
1050 *file = NULL;
1051 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001052 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001053
1054 s = alloc(pat_len + 26);
1055 if (s == NULL)
1056 {
1057 ga_clear_strings(&ga);
1058 return FAIL;
1059 }
1060 sprintf((char *)s, "pack/*/opt/%s*", pat);
1061 globpath(p_pp, s, &ga, 0);
1062 vim_free(s);
1063
1064 for (i = 0; i < ga.ga_len; ++i)
1065 {
1066 match = ((char_u **)ga.ga_data)[i];
1067 s = gettail(match);
1068 e = s + STRLEN(s);
1069 mch_memmove(match, s, e - s + 1);
1070 }
1071
1072 if (ga.ga_len == 0)
1073 return FAIL;
1074
1075 // Sort and remove duplicates which can happen when specifying multiple
1076 // directories in dirnames.
1077 remove_duplicates(&ga);
1078
1079 *file = ga.ga_data;
1080 *num_file = ga.ga_len;
1081 return OK;
1082}
1083
1084 static void
1085cmd_source(char_u *fname, exarg_T *eap)
1086{
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001087 if (*fname != NUL && eap != NULL && eap->addr_count > 0)
1088 {
1089 // if a filename is specified to :source, then a range is not allowed
1090 emsg(_(e_no_range_allowed));
1091 return;
1092 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001093
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001094 if (eap != NULL && *fname == NUL)
1095 {
1096 if (eap->forceit)
1097 // a file name is needed to source normal mode commands
1098 emsg(_(e_argument_required));
1099 else
1100 // source ex commands from the current buffer
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001101 do_source_ext(NULL, FALSE, FALSE, NULL, eap);
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001102 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001103 else if (eap != NULL && eap->forceit)
1104 // ":source!": read Normal mode commands
1105 // Need to execute the commands directly. This is required at least
1106 // for:
1107 // - ":g" command busy
1108 // - after ":argdo", ":windo" or ":bufdo"
1109 // - another command follows
1110 // - inside a loop
1111 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
1112#ifdef FEAT_EVAL
1113 || eap->cstack->cs_idx >= 0
1114#endif
1115 );
1116
1117 // ":source" read ex commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001119 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001120}
1121
1122/*
1123 * ":source {fname}"
1124 */
1125 void
1126ex_source(exarg_T *eap)
1127{
1128#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02001129 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001130 {
1131 char_u *fname = NULL;
1132
1133 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
1134 NULL, NULL,
1135 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1136 if (fname != NULL)
1137 {
1138 cmd_source(fname, eap);
1139 vim_free(fname);
1140 }
1141 }
1142 else
1143#endif
1144 cmd_source(eap->arg, eap);
1145}
1146
1147#if defined(FEAT_EVAL) || defined(PROTO)
1148/*
1149 * ":options"
1150 */
1151 void
1152ex_options(
1153 exarg_T *eap UNUSED)
1154{
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001155 char_u buf[500];
1156 int multi_mods = 0;
1157
1158 buf[0] = NUL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001159 (void)add_win_cmd_modifers(buf, &cmdmod, &multi_mods);
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001160
1161 vim_setenv((char_u *)"OPTWIN_CMD", buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001162 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
1163}
1164#endif
1165
1166/*
1167 * ":source" and associated commands.
1168 */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001169
1170#ifdef FEAT_EVAL
1171/*
1172 * Return the address holding the next breakpoint line for a source cookie.
1173 */
1174 linenr_T *
1175source_breakpoint(void *cookie)
1176{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001177 return &((source_cookie_T *)cookie)->breakpoint;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001178}
1179
1180/*
1181 * Return the address holding the debug tick for a source cookie.
1182 */
1183 int *
1184source_dbg_tick(void *cookie)
1185{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001186 return &((source_cookie_T *)cookie)->dbg_tick;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001187}
1188
1189/*
1190 * Return the nesting level for a source cookie.
1191 */
1192 int
1193source_level(void *cookie)
1194{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001195 return ((source_cookie_T *)cookie)->level;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001196}
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001197
1198/*
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02001199 * Return the readahead line. Note that the pointer may become invalid when
1200 * getting the next line, if it's concatenated with the next one.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001201 */
1202 char_u *
1203source_nextline(void *cookie)
1204{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001205 return ((source_cookie_T *)cookie)->nextline;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001206}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001207#endif
1208
1209#if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC)
1210# define USE_FOPEN_NOINH
1211/*
1212 * Special function to open a file without handle inheritance.
1213 * When possible the handle is closed on exec().
1214 */
1215 static FILE *
1216fopen_noinh_readbin(char *filename)
1217{
1218# ifdef MSWIN
1219 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
1220# else
1221 int fd_tmp = mch_open(filename, O_RDONLY, 0);
1222# endif
1223
1224 if (fd_tmp == -1)
1225 return NULL;
1226
1227# ifdef HAVE_FD_CLOEXEC
1228 {
1229 int fdflags = fcntl(fd_tmp, F_GETFD);
1230 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1231 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
1232 }
1233# endif
1234
1235 return fdopen(fd_tmp, READBIN);
1236}
1237#endif
1238
1239/*
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001240 * Initialization for sourcing lines from the current buffer. Reads all the
1241 * lines from the buffer and stores it in the cookie grow array.
1242 * Returns a pointer to the name ":source buffer=<n>" on success and NULL on
1243 * failure.
1244 */
1245 static char_u *
1246do_source_buffer_init(source_cookie_T *sp, exarg_T *eap)
1247{
1248 linenr_T curr_lnum;
1249 char_u *line = NULL;
1250 char_u *fname;
1251
1252 CLEAR_FIELD(*sp);
1253
1254 if (curbuf == NULL)
1255 return NULL;
1256
1257 // Use ":source buffer=<num>" as the script name
1258 vim_snprintf((char *)IObuff, IOSIZE, ":source buffer=%d", curbuf->b_fnum);
1259 fname = vim_strsave(IObuff);
1260 if (fname == NULL)
1261 return NULL;
1262
1263 ga_init2(&sp->buflines, sizeof(char_u *), 100);
1264
1265 // Copy the lines from the buffer into a grow array
1266 for (curr_lnum = eap->line1; curr_lnum <= eap->line2; curr_lnum++)
1267 {
1268 line = vim_strsave(ml_get(curr_lnum));
1269 if (line == NULL)
1270 goto errret;
1271 if (ga_add_string(&sp->buflines, line) == FAIL)
1272 goto errret;
1273 line = NULL;
1274 }
1275 sp->buf_lnum = 0;
1276 sp->source_from_buf = TRUE;
1277
1278 return fname;
1279
1280errret:
1281 vim_free(fname);
1282 vim_free(line);
1283 ga_clear_strings(&sp->buflines);
1284 return NULL;
1285}
1286
1287/*
1288 * Read the file "fname" and execute its lines as EX commands.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001289 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1290 * again.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001291 *
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001292 * The 'eap' argument is used when sourcing lines from a buffer instead of a
1293 * file.
1294 *
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001295 * This function may be called recursively!
1296 *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001297 * Return FAIL if file could not be opened, OK otherwise.
1298 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001299 */
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001300 static int
1301do_source_ext(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001302 char_u *fname,
1303 int check_other, // check for .vimrc and _vimrc
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001304 int is_vimrc, // DOSO_ value
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001305 int *ret_sid UNUSED,
1306 exarg_T *eap)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001307{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001308 source_cookie_T cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001309 char_u *p;
1310 char_u *fname_exp;
1311 char_u *firstline = NULL;
1312 int retval = FAIL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001313 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001314#ifdef FEAT_EVAL
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001315 funccal_entry_T funccalp_entry;
1316 int save_debug_break_level = debug_break_level;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317 int sid;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001318 scriptitem_T *si = NULL;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001319 int save_estack_compiling = estack_compiling;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001320#endif
1321#ifdef STARTUPTIME
1322 struct timeval tv_rel;
1323 struct timeval tv_start;
1324#endif
1325#ifdef FEAT_PROFILE
1326 proftime_T wait_start;
1327#endif
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001328 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001329 int trigger_source_post = FALSE;
Bram Moolenaare31ee862020-01-07 20:59:34 +01001330 ESTACK_CHECK_DECLARATION
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001331
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001332 CLEAR_FIELD(cookie);
1333 if (fname == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001334 {
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001335 // sourcing lines from a buffer
1336 fname_exp = do_source_buffer_init(&cookie, eap);
1337 if (fname_exp == NULL)
1338 return FAIL;
1339 }
1340 else
1341 {
1342 p = expand_env_save(fname);
1343 if (p == NULL)
1344 return retval;
1345 fname_exp = fix_fname(p);
1346 vim_free(p);
1347 if (fname_exp == NULL)
1348 return retval;
1349 if (mch_isdir(fname_exp))
1350 {
1351 smsg(_("Cannot source a directory: \"%s\""), fname);
1352 goto theend;
1353 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001354 }
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001355#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001356 estack_compiling = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001357
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358 // See if we loaded this script before.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001359 sid = find_script_by_name(fname_exp);
Bram Moolenaarf479cac2022-01-12 12:54:55 +00001360 if (sid > 0 && ret_sid != NULL
1361 && SCRIPT_ITEM(sid)->sn_state != SN_STATE_NOT_LOADED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 {
1363 // Already loaded and no need to load again, return here.
1364 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001365 retval = OK;
1366 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001367 }
1368#endif
1369
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001370 // Apply SourceCmd autocommands, they should get the file and source it.
1371 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1372 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1373 FALSE, curbuf))
1374 {
1375#ifdef FEAT_EVAL
1376 retval = aborting() ? FAIL : OK;
1377#else
1378 retval = OK;
1379#endif
1380 if (retval == OK)
1381 // Apply SourcePost autocommands.
1382 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1383 FALSE, curbuf);
1384 goto theend;
1385 }
1386
1387 // Apply SourcePre autocommands, they may get the file.
1388 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1389
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001390 if (!cookie.source_from_buf)
1391 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001392#ifdef USE_FOPEN_NOINH
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001393 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001394#else
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001395 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001396#endif
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001397 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001398 if (cookie.fp == NULL && check_other)
1399 {
1400 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1401 // and ".exrc" by "_exrc" or vice versa.
1402 p = gettail(fname_exp);
1403 if ((*p == '.' || *p == '_')
1404 && (STRICMP(p + 1, "vimrc") == 0
1405 || STRICMP(p + 1, "gvimrc") == 0
1406 || STRICMP(p + 1, "exrc") == 0))
1407 {
1408 if (*p == '_')
1409 *p = '.';
1410 else
1411 *p = '_';
1412#ifdef USE_FOPEN_NOINH
1413 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1414#else
1415 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1416#endif
1417 }
1418 }
1419
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001420 if (cookie.fp == NULL && !cookie.source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001421 {
1422 if (p_verbose > 0)
1423 {
1424 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001425 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001426 smsg(_("could not source \"%s\""), fname);
1427 else
1428 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001429 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001430 verbose_leave();
1431 }
1432 goto theend;
1433 }
1434
1435 // The file exists.
1436 // - In verbose mode, give a message.
1437 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1438 if (p_verbose > 1)
1439 {
1440 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001441 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001442 smsg(_("sourcing \"%s\""), fname);
1443 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001444 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001445 verbose_leave();
1446 }
1447 if (is_vimrc == DOSO_VIMRC)
1448 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1449 else if (is_vimrc == DOSO_GVIMRC)
1450 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1451
1452#ifdef USE_CRNL
1453 // If no automatic file format: Set default to CR-NL.
1454 if (*p_ffs == NUL)
1455 cookie.fileformat = EOL_DOS;
1456 else
1457 cookie.fileformat = EOL_UNKNOWN;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001458#endif
1459
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001460 if (fname == NULL)
1461 // When sourcing a range of lines from a buffer, use the buffer line
1462 // number.
1463 cookie.sourcing_lnum = eap->line1 - 1;
1464 else
1465 cookie.sourcing_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001466
1467#ifdef FEAT_EVAL
1468 // Check if this script has a breakpoint.
1469 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1470 cookie.fname = fname_exp;
1471 cookie.dbg_tick = debug_tick;
1472
1473 cookie.level = ex_nesting_level;
1474#endif
1475
1476 // Keep the sourcing name/lnum, for recursive calls.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001477 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001478 ESTACK_CHECK_SETUP
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001479
1480#ifdef STARTUPTIME
1481 if (time_fd != NULL)
1482 time_push(&tv_rel, &tv_start);
1483#endif
1484
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001485 // "legacy" does not apply to commands in the script
1486 sticky_cmdmod_flags = 0;
1487
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001488 save_current_sctx = current_sctx;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001489 if (cmdmod.cmod_flags & CMOD_VIM9CMD)
1490 // When the ":vim9cmd" command modifier is used, source the script as a
1491 // Vim9 script.
1492 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1493 else
1494 current_sctx.sc_version = 1; // default script version
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001495
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001496#ifdef FEAT_EVAL
1497# ifdef FEAT_PROFILE
1498 if (do_profiling == PROF_YES)
1499 prof_child_enter(&wait_start); // entering a child now
1500# endif
1501
1502 // Don't use local function variables, if called from a function.
1503 // Also starts profiling timer for nested script.
1504 save_funccal(&funccalp_entry);
1505
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001506 current_sctx.sc_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001507
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001508 // Check if this script was sourced before to find its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001509 // Always use a new sequence number.
1510 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001512 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513 hashtab_T *ht;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001514 int todo;
1515 hashitem_T *hi;
1516 dictitem_T *di;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517
1518 // loading the same script again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001520 si = SCRIPT_ITEM(sid);
1521 if (si->sn_state == SN_STATE_NOT_LOADED)
1522 {
1523 // this script was found but not loaded yet
1524 si->sn_state = SN_STATE_NEW;
1525 }
1526 else
1527 {
1528 si->sn_state = SN_STATE_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001529
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001530 // Script-local variables remain but "const" can be set again.
1531 // In Vim9 script variables will be cleared when "vim9script" is
1532 // encountered without the "noclear" argument.
1533 ht = &SCRIPT_VARS(sid);
1534 todo = (int)ht->ht_used;
1535 for (hi = ht->ht_array; todo > 0; ++hi)
1536 if (!HASHITEM_EMPTY(hi))
1537 {
1538 --todo;
1539 di = HI2DI(hi);
1540 di->di_flags |= DI_FLAGS_RELOAD;
1541 }
1542 // imports can be redefined once
1543 mark_imports_for_reload(sid);
Bram Moolenaar0123cc12021-02-07 17:17:58 +01001544
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001545 // reset version, "vim9script" may have been added or removed.
1546 si->sn_version = 1;
1547 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001548 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001550 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001551 int error = OK;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001552
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001553 // It's new, generate a new SID and initialize the scriptitem.
1554 current_sctx.sc_sid = get_new_scriptitem(&error);
1555 if (error == FAIL)
1556 goto almosttheend;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001557 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001558 si->sn_name = fname_exp;
1559 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 if (ret_sid != NULL)
1561 *ret_sid = current_sctx.sc_sid;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001562
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001563 // Remember the "is_vimrc" flag for when the file is sourced again.
1564 si->sn_is_vimrc = is_vimrc;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001565 }
1566
1567# ifdef FEAT_PROFILE
1568 if (do_profiling == PROF_YES)
1569 {
1570 int forceit;
1571
1572 // Check if we do profiling for this script.
1573 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
1574 {
1575 script_do_profile(si);
1576 si->sn_pr_force = forceit;
1577 }
1578 if (si->sn_prof_on)
1579 {
1580 ++si->sn_pr_count;
1581 profile_start(&si->sn_pr_start);
1582 profile_zero(&si->sn_pr_children);
1583 }
1584 }
1585# endif
1586#endif
1587
1588 cookie.conv.vc_type = CONV_NONE; // no conversion
1589
1590 // Read the first line so we can check for a UTF-8 BOM.
1591 firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1592 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1593 && firstline[1] == 0xbb && firstline[2] == 0xbf)
1594 {
1595 // Found BOM; setup conversion, skip over BOM and recode the line.
1596 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1597 p = string_convert(&cookie.conv, firstline + 3, NULL);
1598 if (p == NULL)
1599 p = vim_strsave(firstline + 3);
1600 if (p != NULL)
1601 {
1602 vim_free(firstline);
1603 firstline = p;
1604 }
1605 }
1606
1607 // Call do_cmdline, which will call getsourceline() to get the lines.
1608 do_cmdline(firstline, getsourceline, (void *)&cookie,
1609 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1610 retval = OK;
1611
1612#ifdef FEAT_PROFILE
1613 if (do_profiling == PROF_YES)
1614 {
1615 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001616 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001617 if (si->sn_prof_on)
1618 {
1619 profile_end(&si->sn_pr_start);
1620 profile_sub_wait(&wait_start, &si->sn_pr_start);
1621 profile_add(&si->sn_pr_total, &si->sn_pr_start);
1622 profile_self(&si->sn_pr_self, &si->sn_pr_start,
1623 &si->sn_pr_children);
1624 }
1625 }
1626#endif
1627
1628 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001629 emsg(_(e_interrupted));
Bram Moolenaare31ee862020-01-07 20:59:34 +01001630 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001631 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001632 if (p_verbose > 1)
1633 {
1634 verbose_enter();
1635 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001636 if (SOURCING_NAME != NULL)
1637 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001638 verbose_leave();
1639 }
1640#ifdef STARTUPTIME
1641 if (time_fd != NULL)
1642 {
1643 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1644 time_msg((char *)IObuff, &tv_start);
1645 time_pop(&tv_rel);
1646 }
1647#endif
1648
1649 if (!got_int)
1650 trigger_source_post = TRUE;
1651
1652#ifdef FEAT_EVAL
1653 // After a "finish" in debug mode, need to break at first command of next
1654 // sourced file.
1655 if (save_debug_break_level > ex_nesting_level
1656 && debug_break_level == ex_nesting_level)
1657 ++debug_break_level;
1658#endif
1659
1660#ifdef FEAT_EVAL
1661almosttheend:
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001662 // If "sn_save_cpo" is set that means we encountered "vim9script": restore
1663 // 'cpoptions', unless in the main .vimrc file.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001665 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001666 if (si->sn_save_cpo != NULL && si->sn_is_vimrc == DOSO_NONE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 {
Bram Moolenaar3e191692021-03-17 17:46:00 +01001668 if (STRCMP(p_cpo, CPO_VIM) != 0)
1669 {
1670 char_u *f;
1671 char_u *t;
1672
1673 // 'cpo' was changed in the script. Apply the same change to the
1674 // saved value, if possible.
1675 for (f = (char_u *)CPO_VIM; *f != NUL; ++f)
1676 if (vim_strchr(p_cpo, *f) == NULL
1677 && (t = vim_strchr(si->sn_save_cpo, *f)) != NULL)
1678 // flag was removed, also remove it from the saved 'cpo'
1679 mch_memmove(t, t + 1, STRLEN(t));
1680 for (f = p_cpo; *f != NUL; ++f)
1681 if (vim_strchr((char_u *)CPO_VIM, *f) == NULL
1682 && vim_strchr(si->sn_save_cpo, *f) == NULL)
1683 {
1684 // flag was added, also add it to the saved 'cpo'
1685 t = alloc(STRLEN(si->sn_save_cpo) + 2);
1686 if (t != NULL)
1687 {
1688 *t = *f;
1689 STRCPY(t + 1, si->sn_save_cpo);
1690 vim_free(si->sn_save_cpo);
1691 si->sn_save_cpo = t;
1692 }
1693 }
1694 }
Bram Moolenaar37294bd2021-03-10 13:40:08 +01001695 set_option_value((char_u *)"cpo", 0L, si->sn_save_cpo, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001696 }
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001697 VIM_CLEAR(si->sn_save_cpo);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001699 restore_funccal();
1700# ifdef FEAT_PROFILE
1701 if (do_profiling == PROF_YES)
1702 prof_child_exit(&wait_start); // leaving a child now
1703# endif
1704#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001705 current_sctx = save_current_sctx;
1706
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001707 if (cookie.fp != NULL)
1708 fclose(cookie.fp);
1709 if (cookie.source_from_buf)
1710 ga_clear_strings(&cookie.buflines);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001711 vim_free(cookie.nextline);
1712 vim_free(firstline);
1713 convert_setup(&cookie.conv, NULL, NULL);
1714
1715 if (trigger_source_post)
1716 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1717
1718theend:
1719 vim_free(fname_exp);
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001720 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001721#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001722 estack_compiling = save_estack_compiling;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001723#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001724 return retval;
1725}
1726
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001727 int
1728do_source(
1729 char_u *fname,
1730 int check_other, // check for .vimrc and _vimrc
1731 int is_vimrc, // DOSO_ value
1732 int *ret_sid UNUSED)
1733{
1734 return do_source_ext(fname, check_other, is_vimrc, ret_sid, NULL);
1735}
1736
1737
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001738#if defined(FEAT_EVAL) || defined(PROTO)
1739
1740/*
1741 * ":scriptnames"
1742 */
1743 void
1744ex_scriptnames(exarg_T *eap)
1745{
1746 int i;
1747
1748 if (eap->addr_count > 0)
1749 {
1750 // :script {scriptId}: edit the script
Bram Moolenaare3d46852020-08-29 13:39:17 +02001751 if (!SCRIPT_ID_VALID(eap->line2))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001752 emsg(_(e_invalid_argument));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001753 else
1754 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001755 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001756 do_exedit(eap, NULL);
1757 }
1758 return;
1759 }
1760
1761 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar6079da72022-01-18 14:16:59 +00001762 {
1763 scriptitem_T *si = SCRIPT_ITEM(i);
1764
1765 if (si->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001766 {
Bram Moolenaar6079da72022-01-18 14:16:59 +00001767 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
1768 vim_snprintf((char *)IObuff, IOSIZE, "%3d%s: %s",
1769 i,
1770 si->sn_state == SN_STATE_NOT_LOADED ? " A" : "",
1771 NameBuff);
Bram Moolenaar769f5892022-02-09 14:31:05 +00001772 if (!message_filtered(IObuff))
1773 {
1774 msg_putchar('\n');
1775 msg_outtrans(IObuff);
1776 out_flush(); // output one line at a time
1777 ui_breakcheck();
1778 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001779 }
Bram Moolenaar6079da72022-01-18 14:16:59 +00001780 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001781}
1782
1783# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1784/*
1785 * Fix slashes in the list of script names for 'shellslash'.
1786 */
1787 void
1788scriptnames_slash_adjust(void)
1789{
1790 int i;
1791
1792 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001793 if (SCRIPT_ITEM(i)->sn_name != NULL)
1794 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001795}
1796# endif
1797
1798/*
1799 * Get a pointer to a script name. Used for ":verbose set".
Bram Moolenaar74667062020-12-28 15:41:41 +01001800 * Message appended to "Last set from "
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001801 */
1802 char_u *
1803get_scriptname(scid_T id)
1804{
1805 if (id == SID_MODELINE)
1806 return (char_u *)_("modeline");
1807 if (id == SID_CMDARG)
1808 return (char_u *)_("--cmd argument");
1809 if (id == SID_CARG)
1810 return (char_u *)_("-c argument");
1811 if (id == SID_ENV)
1812 return (char_u *)_("environment variable");
1813 if (id == SID_ERROR)
1814 return (char_u *)_("error handler");
Bram Moolenaar74667062020-12-28 15:41:41 +01001815 if (id == SID_WINLAYOUT)
1816 return (char_u *)_("changed window size");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001817 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001818}
1819
1820# if defined(EXITFREE) || defined(PROTO)
1821 void
1822free_scriptnames(void)
1823{
1824 int i;
1825
1826 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02001827 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001828 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001830 // the variables themselves are cleared in evalvars_clear()
1831 vim_free(si->sn_vars);
1832
1833 vim_free(si->sn_name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001834 free_imports_and_script_vars(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001835 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02001836# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001837 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02001838# endif
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00001839 vim_free(si->sn_autoload_prefix);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001840 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02001841 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001842 ga_clear(&script_items);
1843}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001844
1845 void
1846free_autoload_scriptnames(void)
1847{
1848 ga_clear_strings(&ga_loaded);
1849}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001850# endif
1851
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001852 linenr_T
Bram Moolenaar66250c92020-08-20 15:02:42 +02001853get_sourced_lnum(
1854 char_u *(*fgetline)(int, void *, int, getline_opt_T),
1855 void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001856{
1857 return fgetline == getsourceline
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001858 ? ((source_cookie_T *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001859 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001860}
Dominique Pelle748b3082022-01-08 12:41:16 +00001861#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001862
1863 static char_u *
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001864get_one_sourceline(source_cookie_T *sp)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001865{
1866 garray_T ga;
1867 int len;
1868 int c;
1869 char_u *buf;
1870#ifdef USE_CRNL
1871 int has_cr; // CR-LF found
1872#endif
1873 int have_read = FALSE;
1874
1875 // use a growarray to store the sourced line
1876 ga_init2(&ga, 1, 250);
1877
1878 // Loop until there is a finished line (or end-of-file).
1879 ++sp->sourcing_lnum;
1880 for (;;)
1881 {
1882 // make room to read at least 120 (more) characters
1883 if (ga_grow(&ga, 120) == FAIL)
1884 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001885 if (sp->source_from_buf)
1886 {
1887 if (sp->buf_lnum >= sp->buflines.ga_len)
1888 break; // all the lines are processed
1889 ga_concat(&ga, ((char_u **)sp->buflines.ga_data)[sp->buf_lnum]);
1890 sp->buf_lnum++;
1891 buf = (char_u *)ga.ga_data;
1892 }
1893 else
1894 {
1895 buf = (char_u *)ga.ga_data;
1896 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
1897 sp->fp) == NULL)
1898 break;
1899 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001900 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
1901#ifdef USE_CRNL
1902 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
1903 // CTRL-Z by its own, or after a NL.
1904 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
1905 && sp->fileformat == EOL_DOS
1906 && buf[len - 1] == Ctrl_Z)
1907 {
1908 buf[len - 1] = NUL;
1909 break;
1910 }
1911#endif
1912
1913 have_read = TRUE;
1914 ga.ga_len = len;
1915
1916 // If the line was longer than the buffer, read more.
1917 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
1918 continue;
1919
1920 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
1921 {
1922#ifdef USE_CRNL
1923 has_cr = (len >= 2 && buf[len - 2] == '\r');
1924 if (sp->fileformat == EOL_UNKNOWN)
1925 {
1926 if (has_cr)
1927 sp->fileformat = EOL_DOS;
1928 else
1929 sp->fileformat = EOL_UNIX;
1930 }
1931
1932 if (sp->fileformat == EOL_DOS)
1933 {
1934 if (has_cr) // replace trailing CR
1935 {
1936 buf[len - 2] = '\n';
1937 --len;
1938 --ga.ga_len;
1939 }
1940 else // lines like ":map xx yy^M" will have failed
1941 {
1942 if (!sp->error)
1943 {
1944 msg_source(HL_ATTR(HLF_W));
1945 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
1946 }
1947 sp->error = TRUE;
1948 sp->fileformat = EOL_UNIX;
1949 }
1950 }
1951#endif
1952 // The '\n' is escaped if there is an odd number of ^V's just
1953 // before it, first set "c" just before the 'V's and then check
1954 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
1955 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
1956 ;
1957 if ((len & 1) != (c & 1)) // escaped NL, read more
1958 {
1959 ++sp->sourcing_lnum;
1960 continue;
1961 }
1962
1963 buf[len - 1] = NUL; // remove the NL
1964 }
1965
1966 // Check for ^C here now and then, so recursive :so can be broken.
1967 line_breakcheck();
1968 break;
1969 }
1970
1971 if (have_read)
1972 return (char_u *)ga.ga_data;
1973
1974 vim_free(ga.ga_data);
1975 return NULL;
1976}
1977
1978/*
1979 * Get one full line from a sourced file.
1980 * Called by do_cmdline() when it's called from do_source().
1981 *
1982 * Return a pointer to the line in allocated memory.
1983 * Return NULL for end-of-file or some error.
1984 */
1985 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02001986getsourceline(
1987 int c UNUSED,
1988 void *cookie,
1989 int indent UNUSED,
1990 getline_opt_T options)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001991{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001992 source_cookie_T *sp = (source_cookie_T *)cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001993 char_u *line;
1994 char_u *p;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001995 int do_vim9_all = in_vim9script()
1996 && options == GETLINE_CONCAT_ALL;
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01001997 int do_bar_cont = do_vim9_all
1998 || options == GETLINE_CONCAT_CONTBAR;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001999
2000#ifdef FEAT_EVAL
2001 // If breakpoints have been added/deleted need to check for it.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002002 if ((sp->dbg_tick < debug_tick) && !sp->source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002003 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002004 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002005 sp->dbg_tick = debug_tick;
2006 }
2007# ifdef FEAT_PROFILE
2008 if (do_profiling == PROF_YES)
2009 script_line_end();
2010# endif
2011#endif
2012
2013 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002014 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002015
2016 // Get current line. If there is a read-ahead line, use it, otherwise get
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002017 // one now. "fp" is NULL if actually using a string.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002018 if (sp->finished || (!sp->source_from_buf && sp->fp == NULL))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002019 line = NULL;
2020 else if (sp->nextline == NULL)
2021 line = get_one_sourceline(sp);
2022 else
2023 {
2024 line = sp->nextline;
2025 sp->nextline = NULL;
2026 ++sp->sourcing_lnum;
2027 }
2028#ifdef FEAT_PROFILE
2029 if (line != NULL && do_profiling == PROF_YES)
2030 script_line_start();
2031#endif
2032
2033 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
2034 // contain the 'C' flag.
Bram Moolenaar66250c92020-08-20 15:02:42 +02002035 if (line != NULL && options != GETLINE_NONE
2036 && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002037 {
Bram Moolenaar5072b472021-06-03 21:56:10 +02002038 int comment_char = in_vim9script() ? '#' : '"';
2039
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002040 // compensate for the one line read-ahead
2041 --sp->sourcing_lnum;
2042
2043 // Get the next line and concatenate it when it starts with a
2044 // backslash. We always need to read the next line, keep it in
2045 // sp->nextline.
2046 /* Also check for a comment in between continuation lines: "\ */
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002047 // Also check for a Vim9 comment, empty line, line starting with '|',
2048 // but not "||".
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002049 sp->nextline = get_one_sourceline(sp);
2050 if (sp->nextline != NULL
2051 && (*(p = skipwhite(sp->nextline)) == '\\'
Bram Moolenaar5072b472021-06-03 21:56:10 +02002052 || (p[0] == comment_char
2053 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002054 || (do_vim9_all && (*p == NUL
2055 || vim9_comment_start(p)))
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002056 || (do_bar_cont && p[0] == '|' && p[1] != '|')))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002057 {
2058 garray_T ga;
2059
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002060 ga_init2(&ga, sizeof(char_u), 400);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002061 ga_concat(&ga, line);
2062 if (*p == '\\')
2063 ga_concat(&ga, p + 1);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002064 else if (*p == '|')
2065 {
2066 ga_concat(&ga, (char_u *)" ");
2067 ga_concat(&ga, p);
2068 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002069 for (;;)
2070 {
2071 vim_free(sp->nextline);
2072 sp->nextline = get_one_sourceline(sp);
2073 if (sp->nextline == NULL)
2074 break;
2075 p = skipwhite(sp->nextline);
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002076 if (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|'))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002077 {
2078 // Adjust the growsize to the current length to speed up
2079 // concatenating many lines.
2080 if (ga.ga_len > 400)
2081 {
2082 if (ga.ga_len > 8000)
2083 ga.ga_growsize = 8000;
2084 else
2085 ga.ga_growsize = ga.ga_len;
2086 }
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002087 if (*p == '\\')
2088 ga_concat(&ga, p + 1);
2089 else
2090 {
2091 ga_concat(&ga, (char_u *)" ");
2092 ga_concat(&ga, p);
2093 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002094 }
Bram Moolenaar5072b472021-06-03 21:56:10 +02002095 else if (!(p[0] == (comment_char)
Bram Moolenaar0f37e352021-06-02 15:28:15 +02002096 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002097 && !(do_vim9_all && (*p == NUL || vim9_comment_start(p))))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002098 break;
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002099 /* drop a # comment or "\ comment line */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002100 }
2101 ga_append(&ga, NUL);
2102 vim_free(line);
2103 line = ga.ga_data;
2104 }
2105 }
2106
2107 if (line != NULL && sp->conv.vc_type != CONV_NONE)
2108 {
2109 char_u *s;
2110
2111 // Convert the encoding of the script line.
2112 s = string_convert(&sp->conv, line, NULL);
2113 if (s != NULL)
2114 {
2115 vim_free(line);
2116 line = s;
2117 }
2118 }
2119
2120#ifdef FEAT_EVAL
2121 // Did we encounter a breakpoint?
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002122 if (!sp->source_from_buf && sp->breakpoint != 0
2123 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002124 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002125 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002126 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002127 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002128 sp->dbg_tick = debug_tick;
2129 }
2130#endif
2131
2132 return line;
2133}
2134
2135/*
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002136 * Returns TRUE if sourcing a script either from a file or a buffer.
2137 * Otherwise returns FALSE.
2138 */
2139 int
2140sourcing_a_script(exarg_T *eap)
2141{
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002142 return (getline_equal(eap->getline, eap->cookie, getsourceline));
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002143}
2144
2145/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002146 * ":scriptencoding": Set encoding conversion for a sourced script.
2147 */
2148 void
2149ex_scriptencoding(exarg_T *eap)
2150{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002151 source_cookie_T *sp;
2152 char_u *name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002153
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002154 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002155 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00002156 emsg(_(e_scriptencoding_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002157 return;
2158 }
2159
2160 if (*eap->arg != NUL)
2161 {
2162 name = enc_canonize(eap->arg);
2163 if (name == NULL) // out of memory
2164 return;
2165 }
2166 else
2167 name = eap->arg;
2168
2169 // Setup for conversion from the specified encoding to 'encoding'.
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002170 sp = (source_cookie_T *)getline_cookie(eap->getline, eap->cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002171 convert_setup(&sp->conv, name, p_enc);
2172
2173 if (name != eap->arg)
2174 vim_free(name);
2175}
2176
2177/*
2178 * ":scriptversion": Set Vim script version for a sourced script.
2179 */
2180 void
2181ex_scriptversion(exarg_T *eap UNUSED)
2182{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002183 int nr;
2184
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002185 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002186 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002187 emsg(_(e_scriptversion_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002188 return;
2189 }
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002190 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002192 emsg(_(e_cannot_use_scriptversion_after_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193 return;
2194 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002195
2196 nr = getdigits(&eap->arg);
2197 if (nr == 0 || *eap->arg != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002198 emsg(_(e_invalid_argument));
Bram Moolenaar67979662020-06-20 22:50:47 +02002199 else if (nr > SCRIPT_VERSION_MAX)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002200 semsg(_(e_scriptversion_not_supported_nr), nr);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002201 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002202 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002203 current_sctx.sc_version = nr;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002204#ifdef FEAT_EVAL
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002205 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002206#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002207 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002208}
2209
2210#if defined(FEAT_EVAL) || defined(PROTO)
2211/*
2212 * ":finish": Mark a sourced file as finished.
2213 */
2214 void
2215ex_finish(exarg_T *eap)
2216{
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002217 if (sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002218 do_finish(eap, FALSE);
2219 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00002220 emsg(_(e_finish_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002221}
2222
2223/*
2224 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
2225 * Also called for a pending finish at the ":endtry" or after returning from
2226 * an extra do_cmdline(). "reanimate" is used in the latter case.
2227 */
2228 void
2229do_finish(exarg_T *eap, int reanimate)
2230{
2231 int idx;
2232
2233 if (reanimate)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002234 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002235 eap->cookie))->finished = FALSE;
2236
2237 // Cleanup (and inactivate) conditionals, but stop when a try conditional
2238 // not in its finally clause (which then is to be executed next) is found.
2239 // In this case, make the ":finish" pending for execution at the ":endtry".
2240 // Otherwise, finish normally.
2241 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
2242 if (idx >= 0)
2243 {
2244 eap->cstack->cs_pending[idx] = CSTP_FINISH;
2245 report_make_pending(CSTP_FINISH, NULL);
2246 }
2247 else
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002248 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002249 eap->cookie))->finished = TRUE;
2250}
2251
2252
2253/*
2254 * Return TRUE when a sourced file had the ":finish" command: Don't give error
2255 * message for missing ":endif".
2256 * Return FALSE when not sourcing a file.
2257 */
2258 int
2259source_finished(
Bram Moolenaar66250c92020-08-20 15:02:42 +02002260 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002261 void *cookie)
2262{
2263 return (getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002264 && ((source_cookie_T *)getline_cookie(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002265 fgetline, cookie))->finished);
2266}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002267
2268/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002269 * Find the path of a script below the "autoload" directory.
2270 * Returns NULL if there is no "/autoload/" in the script name.
2271 */
2272 char_u *
2273script_name_after_autoload(scriptitem_T *si)
2274{
2275 char_u *p = si->sn_name;
2276 char_u *res = NULL;
2277
2278 for (;;)
2279 {
2280 char_u *n = (char_u *)strstr((char *)p, "autoload");
2281
2282 if (n == NULL)
2283 break;
2284 if (n > p && vim_ispathsep(n[-1]) && vim_ispathsep(n[8]))
2285 res = n + 9;
2286 p = n + 8;
2287 }
2288 return res;
2289}
2290
2291/*
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002292 * For an autoload script "autoload/dir/script.vim" return the prefix
2293 * "dir#script#" in allocated memory.
2294 * Returns NULL if anything is wrong.
2295 */
2296 char_u *
2297get_autoload_prefix(scriptitem_T *si)
2298{
2299 char_u *p = script_name_after_autoload(si);
2300 char_u *prefix;
2301
2302 if (p == NULL)
2303 return NULL;
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002304 prefix = vim_strsave(p);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002305 if (prefix == NULL)
2306 return NULL;
2307
2308 // replace all '/' with '#' and locate ".vim" at the end
2309 for (p = prefix; *p != NUL; p += mb_ptr2len(p))
2310 {
2311 if (vim_ispathsep(*p))
2312 *p = '#';
2313 else if (STRCMP(p, ".vim") == 0)
2314 {
2315 p[0] = '#';
2316 p[1] = NUL;
2317 return prefix;
2318 }
2319 }
2320
2321 // did not find ".vim" at the end
2322 vim_free(prefix);
2323 return NULL;
2324}
2325
2326/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002327 * If in a Vim9 autoload script return "name" with the autoload prefix for the
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002328 * script. If successful the returned name is allocated.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002329 * Otherwise it returns "name" unmodified.
2330 */
2331 char_u *
2332may_prefix_autoload(char_u *name)
2333{
2334 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
2335 {
2336 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2337
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002338 if (si->sn_autoload_prefix != NULL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002339 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002340 char_u *basename = name;
2341 size_t len;
2342 char_u *res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002343
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002344 if (*name == K_SPECIAL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002345 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002346 char_u *p = vim_strchr(name, '_');
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002347
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002348 // skip over "<SNR>99_"
2349 if (p != NULL)
2350 basename = p + 1;
2351 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002352
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002353 len = STRLEN(si->sn_autoload_prefix) + STRLEN(basename) + 2;
2354 res = alloc(len);
2355 if (res != NULL)
2356 {
2357 vim_snprintf((char *)res, len, "%s%s",
2358 si->sn_autoload_prefix, basename);
2359 return res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002360 }
2361 }
2362 }
2363 return name;
2364}
2365
2366/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002367 * Return the autoload script name for a function or variable name.
2368 * Returns NULL when out of memory.
2369 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
2370 */
2371 char_u *
2372autoload_name(char_u *name)
2373{
2374 char_u *p, *q = NULL;
2375 char_u *scriptname;
2376
2377 // Get the script file name: replace '#' with '/', append ".vim".
2378 scriptname = alloc(STRLEN(name) + 14);
2379 if (scriptname == NULL)
2380 return NULL;
2381 STRCPY(scriptname, "autoload/");
Bram Moolenaara1773442020-08-12 15:21:22 +02002382 STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002383 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
2384 q = p, ++p)
2385 *p = '/';
2386 STRCPY(q, ".vim");
2387 return scriptname;
2388}
2389
2390/*
2391 * If "name" has a package name try autoloading the script for it.
2392 * Return TRUE if a package was loaded.
2393 */
2394 int
2395script_autoload(
2396 char_u *name,
2397 int reload) // load script again when already loaded
2398{
2399 char_u *p;
2400 char_u *scriptname, *tofree;
2401 int ret = FALSE;
2402 int i;
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002403 int ret_sid;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002404
2405 // If there is no '#' after name[0] there is no package name.
2406 p = vim_strchr(name, AUTOLOAD_CHAR);
2407 if (p == NULL || p == name)
2408 return FALSE;
2409
2410 tofree = scriptname = autoload_name(name);
2411 if (scriptname == NULL)
2412 return FALSE;
2413
2414 // Find the name in the list of previously loaded package names. Skip
2415 // "autoload/", it's always the same.
2416 for (i = 0; i < ga_loaded.ga_len; ++i)
2417 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2418 break;
2419 if (!reload && i < ga_loaded.ga_len)
2420 ret = FALSE; // was loaded already
2421 else
2422 {
2423 // Remember the name if it wasn't loaded already.
2424 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2425 {
2426 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2427 tofree = NULL;
2428 }
2429
2430 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002431 // Use "ret_sid" to avoid loading the same script again.
=?UTF-8?q?Bj=C3=B6rn=20Linse?=223a9502022-01-31 17:26:05 +00002432 if (source_in_path(p_rtp, scriptname, DIP_START, &ret_sid) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002433 ret = TRUE;
2434 }
2435
2436 vim_free(tofree);
2437 return ret;
2438}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002439#endif