blob: 3dfb1c43cc0ea8c312b730375c3318ac626714ba [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 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
166 // Walk the stack backwards, starting from the current frame.
167 for (idx = exestack.ga_len - 1; idx >= 0; --idx, --entry)
LemonBoy6013d002022-04-09 21:42:10 +0100168 {
LemonBoyeca7c602022-04-14 15:39:43 +0100169 if (entry->es_type == ETYPE_UFUNC)
LemonBoy6013d002022-04-09 21:42:10 +0100170 {
LemonBoyeca7c602022-04-14 15:39:43 +0100171 sctx_T *def_ctx = &entry->es_info.ufunc->uf_script_ctx;
LemonBoy6013d002022-04-09 21:42:10 +0100172
LemonBoyeca7c602022-04-14 15:39:43 +0100173 if (def_ctx->sc_sid > 0)
174 return vim_strsave(SCRIPT_ITEM(def_ctx->sc_sid)->sn_name);
175 else
176 return NULL;
177 }
178 else if (entry->es_type == ETYPE_AUCMD)
179 {
180 sctx_T *def_ctx = acp_script_ctx(entry->es_info.aucmd);
181
182 if (def_ctx->sc_sid > 0)
183 return vim_strsave(SCRIPT_ITEM(def_ctx->sc_sid)->sn_name);
184 else
185 return NULL;
186 }
187 else if (entry->es_type == ETYPE_SCRIPT)
188 {
189 return vim_strsave(entry->es_name);
LemonBoy6013d002022-04-09 21:42:10 +0100190 }
191 }
192 return NULL;
193 }
194
Bram Moolenaara5d04232020-07-26 15:37:02 +0200195 // Give information about each stack entry up to the root.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100196 // For a function we compose the call stack, as it was done in the past:
197 // "function One[123]..Two[456]..Three"
Bram Moolenaara5d04232020-07-26 15:37:02 +0200198 ga_init2(&ga, sizeof(char), 100);
199 for (idx = 0; idx < exestack.ga_len; ++idx)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100200 {
201 entry = ((estack_T *)exestack.ga_data) + idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200202 if (entry->es_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100203 {
Bram Moolenaara810db32020-09-11 17:59:23 +0200204 long lnum = 0;
205 char *dots;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200206
Bram Moolenaara5d04232020-07-26 15:37:02 +0200207 len = STRLEN(entry->es_name) + 15;
208 type_name = "";
209 if (entry->es_type != last_type)
210 {
211 switch (entry->es_type)
212 {
213 case ETYPE_SCRIPT: type_name = "script "; break;
214 case ETYPE_UFUNC: type_name = "function "; break;
215 default: type_name = ""; break;
216 }
217 last_type = entry->es_type;
218 }
219 len += STRLEN(type_name);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200220 if (ga_grow(&ga, (int)len) == FAIL)
Bram Moolenaara5d04232020-07-26 15:37:02 +0200221 break;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200222 if (idx == exestack.ga_len - 1)
223 lnum = which == ESTACK_STACK ? SOURCING_LNUM : 0;
224 else
225 lnum = entry->es_lnum;
Bram Moolenaara810db32020-09-11 17:59:23 +0200226 dots = idx == exestack.ga_len - 1 ? "" : "..";
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200227 if (lnum == 0)
228 // For the bottom entry of <sfile>: do not add the line number,
229 // it is used in <slnum>. Also leave it out when the number is
230 // not set.
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200231 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s%s",
Bram Moolenaara810db32020-09-11 17:59:23 +0200232 type_name, entry->es_name, dots);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200233 else
Bram Moolenaara810db32020-09-11 17:59:23 +0200234 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s[%ld]%s",
235 type_name, entry->es_name, lnum, dots);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200236 ga.ga_len += (int)STRLEN((char *)ga.ga_data + ga.ga_len);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100237 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100238 }
239
Bram Moolenaara5d04232020-07-26 15:37:02 +0200240 return (char_u *)ga.ga_data;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100241#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100242}
243
244/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200245 * ":runtime [what] {name}"
246 */
247 void
248ex_runtime(exarg_T *eap)
249{
250 char_u *arg = eap->arg;
251 char_u *p = skiptowhite(arg);
252 int len = (int)(p - arg);
253 int flags = eap->forceit ? DIP_ALL : 0;
254
255 if (STRNCMP(arg, "START", len) == 0)
256 {
257 flags += DIP_START + DIP_NORTP;
258 arg = skipwhite(arg + len);
259 }
260 else if (STRNCMP(arg, "OPT", len) == 0)
261 {
262 flags += DIP_OPT + DIP_NORTP;
263 arg = skipwhite(arg + len);
264 }
265 else if (STRNCMP(arg, "PACK", len) == 0)
266 {
267 flags += DIP_START + DIP_OPT + DIP_NORTP;
268 arg = skipwhite(arg + len);
269 }
270 else if (STRNCMP(arg, "ALL", len) == 0)
271 {
272 flags += DIP_START + DIP_OPT;
273 arg = skipwhite(arg + len);
274 }
275
276 source_runtime(arg, flags);
277}
278
279 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100280source_callback(char_u *fname, void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200281{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200283}
284
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000285#ifdef FEAT_EVAL
286/*
287 * Find an already loaded script "name".
288 * If found returns its script ID. If not found returns -1.
289 */
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100290 int
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000291find_script_by_name(char_u *name)
292{
293 int sid;
294 scriptitem_T *si;
295
296 for (sid = script_items.ga_len; sid > 0; --sid)
297 {
298 // We used to check inode here, but that doesn't work:
299 // - If a script is edited and written, it may get a different
300 // inode number, even though to the user it is the same script.
301 // - If a script is deleted and another script is written, with a
302 // different name, the inode may be re-used.
303 si = SCRIPT_ITEM(sid);
304 if (si->sn_name != NULL && fnamecmp(si->sn_name, name) == 0)
305 return sid;
306 }
307 return -1;
308}
309
310/*
311 * Add a new scriptitem with all items initialized.
312 * When running out of memory "error" is set to FAIL.
313 * Returns the script ID.
314 */
315 static int
316get_new_scriptitem(int *error)
317{
318 static scid_T last_current_SID = 0;
319 int sid = ++last_current_SID;
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000320 scriptitem_T *si = NULL;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000321
322 if (ga_grow(&script_items, (int)(sid - script_items.ga_len)) == FAIL)
323 {
324 *error = FAIL;
325 return sid;
326 }
327 while (script_items.ga_len < sid)
328 {
329 si = ALLOC_CLEAR_ONE(scriptitem_T);
330 if (si == NULL)
331 {
332 *error = FAIL;
333 return sid;
334 }
335 ++script_items.ga_len;
336 SCRIPT_ITEM(script_items.ga_len) = si;
337 si->sn_name = NULL;
338 si->sn_version = 1;
339
340 // Allocate the local script variables to use for this script.
341 new_script_vars(script_items.ga_len);
342 ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
343 hash_init(&si->sn_all_vars.dv_hashtab);
344 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
345 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
346# ifdef FEAT_PROFILE
347 si->sn_prof_on = FALSE;
348# endif
349 }
350
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000351 // "si" can't be NULL, check only to avoid a compiler warning
352 if (si != NULL)
353 // Used to check script variable index is still valid.
354 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000355
356 return sid;
357}
358
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100359 int
360get_new_scriptitem_for_fname(int *error, char_u *fname)
361{
362 int sid = get_new_scriptitem(error);
363
364 if (*error == OK)
365 {
366 scriptitem_T *si = SCRIPT_ITEM(sid);
367
368 si->sn_name = vim_strsave(fname);
369 si->sn_state = SN_STATE_NOT_LOADED;
370 }
371 return sid;
372}
373
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000374 static void
375find_script_callback(char_u *fname, void *cookie)
376{
377 int sid;
378 int error = OK;
379 int *ret_sid = cookie;
380
381 sid = find_script_by_name(fname);
382 if (sid < 0)
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000383 // script does not exist yet, create a new scriptitem
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100384 sid = get_new_scriptitem_for_fname(&error, fname);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000385 *ret_sid = sid;
386}
387#endif
388
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200389/*
390 * Find the file "name" in all directories in "path" and invoke
391 * "callback(fname, cookie)".
392 * "name" can contain wildcards.
393 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
394 * When "flags" has DIP_DIR: find directories instead of files.
395 * When "flags" has DIP_ERR: give an error message if there is no match.
396 *
397 * return FAIL when no file could be sourced, OK otherwise.
398 */
399 int
400do_in_path(
401 char_u *path,
402 char_u *name,
403 int flags,
404 void (*callback)(char_u *fname, void *ck),
405 void *cookie)
406{
407 char_u *rtp;
408 char_u *np;
409 char_u *buf;
410 char_u *rtp_copy;
411 char_u *tail;
412 int num_files;
413 char_u **files;
414 int i;
415 int did_one = FALSE;
416#ifdef AMIGA
417 struct Process *proc = (struct Process *)FindTask(0L);
418 APTR save_winptr = proc->pr_WindowPtr;
419
420 // Avoid a requester here for a volume that doesn't exist.
421 proc->pr_WindowPtr = (APTR)-1L;
422#endif
423
424 // Make a copy of 'runtimepath'. Invoking the callback may change the
425 // value.
426 rtp_copy = vim_strsave(path);
427 buf = alloc(MAXPATHL);
428 if (buf != NULL && rtp_copy != NULL)
429 {
Bram Moolenaar647a5302020-05-03 17:01:24 +0200430 if (p_verbose > 10 && name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200431 {
432 verbose_enter();
433 smsg(_("Searching for \"%s\" in \"%s\""),
434 (char *)name, (char *)path);
435 verbose_leave();
436 }
437
438 // Loop over all entries in 'runtimepath'.
439 rtp = rtp_copy;
440 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
441 {
442 size_t buflen;
443
444 // Copy the path from 'runtimepath' to buf[].
445 copy_option_part(&rtp, buf, MAXPATHL, ",");
446 buflen = STRLEN(buf);
447
448 // Skip after or non-after directories.
449 if (flags & (DIP_NOAFTER | DIP_AFTER))
450 {
451 int is_after = buflen >= 5
452 && STRCMP(buf + buflen - 5, "after") == 0;
453
454 if ((is_after && (flags & DIP_NOAFTER))
455 || (!is_after && (flags & DIP_AFTER)))
456 continue;
457 }
458
459 if (name == NULL)
460 {
461 (*callback)(buf, (void *) &cookie);
462 if (!did_one)
463 did_one = (cookie == NULL);
464 }
465 else if (buflen + STRLEN(name) + 2 < MAXPATHL)
466 {
467 add_pathsep(buf);
468 tail = buf + STRLEN(buf);
469
470 // Loop over all patterns in "name"
471 np = name;
472 while (*np != NUL && ((flags & DIP_ALL) || !did_one))
473 {
474 // Append the pattern from "name" to buf[].
475 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
476 "\t ");
477
Bram Moolenaar647a5302020-05-03 17:01:24 +0200478 if (p_verbose > 10)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200479 {
480 verbose_enter();
481 smsg(_("Searching for \"%s\""), buf);
482 verbose_leave();
483 }
484
485 // Expand wildcards, invoke the callback for each match.
486 if (gen_expand_wildcards(1, &buf, &num_files, &files,
487 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
488 {
489 for (i = 0; i < num_files; ++i)
490 {
491 (*callback)(files[i], cookie);
492 did_one = TRUE;
493 if (!(flags & DIP_ALL))
494 break;
495 }
496 FreeWild(num_files, files);
497 }
498 }
499 }
500 }
501 }
502 vim_free(buf);
503 vim_free(rtp_copy);
504 if (!did_one && name != NULL)
505 {
506 char *basepath = path == p_rtp ? "runtimepath" : "packpath";
507
508 if (flags & DIP_ERR)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000509 semsg(_(e_directory_not_found_in_str_str), basepath, name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200510 else if (p_verbose > 0)
511 {
512 verbose_enter();
513 smsg(_("not found in '%s': \"%s\""), basepath, name);
514 verbose_leave();
515 }
516 }
517
518#ifdef AMIGA
519 proc->pr_WindowPtr = save_winptr;
520#endif
521
522 return did_one ? OK : FAIL;
523}
524
525/*
526 * Find "name" in "path". When found, invoke the callback function for
527 * it: callback(fname, "cookie")
528 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first
529 * one is used.
530 * Returns OK when at least one match found, FAIL otherwise.
531 *
532 * If "name" is NULL calls callback for each entry in "path". Cookie is
533 * passed by reference in this case, setting it to NULL indicates that callback
534 * has done its job.
535 */
536 static int
537do_in_path_and_pp(
538 char_u *path,
539 char_u *name,
540 int flags,
541 void (*callback)(char_u *fname, void *ck),
542 void *cookie)
543{
544 int done = FAIL;
545 char_u *s;
546 int len;
547 char *start_dir = "pack/*/start/*/%s";
548 char *opt_dir = "pack/*/opt/*/%s";
549
550 if ((flags & DIP_NORTP) == 0)
551 done = do_in_path(path, name, flags, callback, cookie);
552
553 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
554 {
555 len = (int)(STRLEN(start_dir) + STRLEN(name));
556 s = alloc(len);
557 if (s == NULL)
558 return FAIL;
559 vim_snprintf((char *)s, len, start_dir, name);
560 done = do_in_path(p_pp, s, flags, callback, cookie);
561 vim_free(s);
562 }
563
564 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
565 {
566 len = (int)(STRLEN(opt_dir) + STRLEN(name));
567 s = alloc(len);
568 if (s == NULL)
569 return FAIL;
570 vim_snprintf((char *)s, len, opt_dir, name);
571 done = do_in_path(p_pp, s, flags, callback, cookie);
572 vim_free(s);
573 }
574
575 return done;
576}
577
578/*
579 * Just like do_in_path_and_pp(), using 'runtimepath' for "path".
580 */
581 int
582do_in_runtimepath(
583 char_u *name,
584 int flags,
585 void (*callback)(char_u *fname, void *ck),
586 void *cookie)
587{
588 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
589}
590
591/*
592 * Source the file "name" from all directories in 'runtimepath'.
593 * "name" can contain wildcards.
594 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
595 *
596 * return FAIL when no file could be sourced, OK otherwise.
597 */
598 int
599source_runtime(char_u *name, int flags)
600{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 return source_in_path(p_rtp, name, flags, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200602}
603
604/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000605 * Just like source_runtime(), but use "path" instead of 'runtimepath'
606 * and return the script ID in "ret_sid".
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200607 */
608 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200610{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100611 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200612}
613
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200614#if defined(FEAT_EVAL) || defined(PROTO)
615
616/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000617 * Find "name" in 'runtimepath'. If found a new scriptitem is created for it
618 * and it's script ID is returned.
619 * If not found returns -1.
620 */
621 int
622find_script_in_rtp(char_u *name)
623{
624 int sid = -1;
625
626 (void)do_in_path_and_pp(p_rtp, name, DIP_NOAFTER,
627 find_script_callback, &sid);
628 return sid;
629}
630
631/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200632 * Expand wildcards in "pat" and invoke do_source() for each match.
633 */
634 static void
635source_all_matches(char_u *pat)
636{
637 int num_files;
638 char_u **files;
639 int i;
640
641 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK)
642 {
643 for (i = 0; i < num_files; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100644 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200645 FreeWild(num_files, files);
646 }
647}
648
649/*
650 * Add the package directory to 'runtimepath'.
651 */
652 static int
653add_pack_dir_to_rtp(char_u *fname)
654{
655 char_u *p4, *p3, *p2, *p1, *p;
656 char_u *entry;
657 char_u *insp = NULL;
658 int c;
659 char_u *new_rtp;
660 int keep;
661 size_t oldlen;
662 size_t addlen;
663 size_t new_rtp_len;
664 char_u *afterdir = NULL;
665 size_t afterlen = 0;
666 char_u *after_insp = NULL;
667 char_u *ffname = NULL;
668 size_t fname_len;
669 char_u *buf = NULL;
670 char_u *rtp_ffname;
671 int match;
672 int retval = FAIL;
673
674 p4 = p3 = p2 = p1 = get_past_head(fname);
675 for (p = p1; *p; MB_PTR_ADV(p))
676 if (vim_ispathsep_nocolon(*p))
677 {
678 p4 = p3; p3 = p2; p2 = p1; p1 = p;
679 }
680
681 // now we have:
682 // rtp/pack/name/start/name
683 // p4 p3 p2 p1
684 //
685 // find the part up to "pack" in 'runtimepath'
686 c = *++p4; // append pathsep in order to expand symlink
687 *p4 = NUL;
688 ffname = fix_fname(fname);
689 *p4 = c;
690 if (ffname == NULL)
691 return FAIL;
692
693 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
694 // Also stop at the first "after" directory.
695 fname_len = STRLEN(ffname);
696 buf = alloc(MAXPATHL);
697 if (buf == NULL)
698 goto theend;
699 for (entry = p_rtp; *entry != NUL; )
700 {
701 char_u *cur_entry = entry;
702
703 copy_option_part(&entry, buf, MAXPATHL, ",");
704 if (insp == NULL)
705 {
706 add_pathsep(buf);
707 rtp_ffname = fix_fname(buf);
708 if (rtp_ffname == NULL)
709 goto theend;
710 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
711 vim_free(rtp_ffname);
712 if (match)
713 // Insert "ffname" after this entry (and comma).
714 insp = entry;
715 }
716
717 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
718 && p > buf
719 && vim_ispathsep(p[-1])
720 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
721 {
722 if (insp == NULL)
723 // Did not find "ffname" before the first "after" directory,
724 // insert it before this entry.
725 insp = cur_entry;
726 after_insp = cur_entry;
727 break;
728 }
729 }
730
731 if (insp == NULL)
732 // Both "fname" and "after" not found, append at the end.
733 insp = p_rtp + STRLEN(p_rtp);
734
735 // check if rtp/pack/name/start/name/after exists
736 afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
737 if (afterdir != NULL && mch_isdir(afterdir))
738 afterlen = STRLEN(afterdir) + 1; // add one for comma
739
740 oldlen = STRLEN(p_rtp);
741 addlen = STRLEN(fname) + 1; // add one for comma
742 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
743 if (new_rtp == NULL)
744 goto theend;
745
746 // We now have 'rtp' parts: {keep}{keep_after}{rest}.
747 // Create new_rtp, first: {keep},{fname}
748 keep = (int)(insp - p_rtp);
749 mch_memmove(new_rtp, p_rtp, keep);
750 new_rtp_len = keep;
751 if (*insp == NUL)
752 new_rtp[new_rtp_len++] = ','; // add comma before
753 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
754 new_rtp_len += addlen - 1;
755 if (*insp != NUL)
756 new_rtp[new_rtp_len++] = ','; // add comma after
757
758 if (afterlen > 0 && after_insp != NULL)
759 {
760 int keep_after = (int)(after_insp - p_rtp);
761
762 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
763 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
764 keep_after - keep);
765 new_rtp_len += keep_after - keep;
766 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
767 new_rtp_len += afterlen - 1;
768 new_rtp[new_rtp_len++] = ',';
769 keep = keep_after;
770 }
771
772 if (p_rtp[keep] != NUL)
773 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
774 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
775 else
776 new_rtp[new_rtp_len] = NUL;
777
778 if (afterlen > 0 && after_insp == NULL)
779 {
780 // Append afterdir when "after" was not found:
781 // {keep},{fname}{rest},{afterdir}
782 STRCAT(new_rtp, ",");
783 STRCAT(new_rtp, afterdir);
784 }
785
786 set_option_value((char_u *)"rtp", 0L, new_rtp, 0);
787 vim_free(new_rtp);
788 retval = OK;
789
790theend:
791 vim_free(buf);
792 vim_free(ffname);
793 vim_free(afterdir);
794 return retval;
795}
796
797/*
798 * Load scripts in "plugin" and "ftdetect" directories of the package.
799 */
800 static int
801load_pack_plugin(char_u *fname)
802{
803 static char *plugpat = "%s/plugin/**/*.vim";
804 static char *ftpat = "%s/ftdetect/*.vim";
805 int len;
806 char_u *ffname = fix_fname(fname);
807 char_u *pat = NULL;
808 int retval = FAIL;
809
810 if (ffname == NULL)
811 return FAIL;
812 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
813 pat = alloc(len);
814 if (pat == NULL)
815 goto theend;
816 vim_snprintf((char *)pat, len, plugpat, ffname);
817 source_all_matches(pat);
818
819 {
820 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
821
822 // If runtime/filetype.vim wasn't loaded yet, the scripts will be
823 // found when it loads.
824 if (cmd != NULL && eval_to_number(cmd) > 0)
825 {
826 do_cmdline_cmd((char_u *)"augroup filetypedetect");
827 vim_snprintf((char *)pat, len, ftpat, ffname);
828 source_all_matches(pat);
829 do_cmdline_cmd((char_u *)"augroup END");
830 }
831 vim_free(cmd);
832 }
833 vim_free(pat);
834 retval = OK;
835
836theend:
837 vim_free(ffname);
838 return retval;
839}
840
841// used for "cookie" of add_pack_plugin()
842static int APP_ADD_DIR;
843static int APP_LOAD;
844static int APP_BOTH;
845
846 static void
847add_pack_plugin(char_u *fname, void *cookie)
848{
849 if (cookie != &APP_LOAD)
850 {
851 char_u *buf = alloc(MAXPATHL);
852 char_u *p;
853 int found = FALSE;
854
855 if (buf == NULL)
856 return;
857 p = p_rtp;
858 while (*p != NUL)
859 {
860 copy_option_part(&p, buf, MAXPATHL, ",");
861 if (pathcmp((char *)buf, (char *)fname, -1) == 0)
862 {
863 found = TRUE;
864 break;
865 }
866 }
867 vim_free(buf);
868 if (!found)
869 // directory is not yet in 'runtimepath', add it
870 if (add_pack_dir_to_rtp(fname) == FAIL)
871 return;
872 }
873
874 if (cookie != &APP_ADD_DIR)
875 load_pack_plugin(fname);
876}
877
878/*
879 * Add all packages in the "start" directory to 'runtimepath'.
880 */
881 void
882add_pack_start_dirs(void)
883{
884 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
885 add_pack_plugin, &APP_ADD_DIR);
886}
887
888/*
889 * Load plugins from all packages in the "start" directory.
890 */
891 void
892load_start_packages(void)
893{
894 did_source_packages = TRUE;
895 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
896 add_pack_plugin, &APP_LOAD);
897}
898
899/*
900 * ":packloadall"
901 * Find plugins in the package directories and source them.
902 */
903 void
904ex_packloadall(exarg_T *eap)
905{
906 if (!did_source_packages || eap->forceit)
907 {
908 // First do a round to add all directories to 'runtimepath', then load
909 // the plugins. This allows for plugins to use an autoload directory
910 // of another plugin.
911 add_pack_start_dirs();
912 load_start_packages();
913 }
914}
915
916/*
917 * ":packadd[!] {name}"
918 */
919 void
920ex_packadd(exarg_T *eap)
921{
922 static char *plugpat = "pack/*/%s/%s";
923 int len;
924 char *pat;
925 int round;
926 int res = OK;
927
928 // Round 1: use "start", round 2: use "opt".
929 for (round = 1; round <= 2; ++round)
930 {
931 // Only look under "start" when loading packages wasn't done yet.
932 if (round == 1 && did_source_packages)
933 continue;
934
935 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
936 pat = alloc(len);
937 if (pat == NULL)
938 return;
939 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
940 // The first round don't give a "not found" error, in the second round
941 // only when nothing was found in the first round.
942 res = do_in_path(p_pp, (char_u *)pat,
943 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
944 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
945 vim_free(pat);
946 }
947}
948#endif
949
950/*
Bram Moolenaar26262f82019-09-04 20:59:15 +0200951 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
952 * list of file names in allocated memory.
953 */
954 void
955remove_duplicates(garray_T *gap)
956{
957 int i;
958 int j;
959 char_u **fnames = (char_u **)gap->ga_data;
960
961 sort_strings(fnames, gap->ga_len);
962 for (i = gap->ga_len - 1; i > 0; --i)
963 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
964 {
965 vim_free(fnames[i]);
966 for (j = i + 1; j < gap->ga_len; ++j)
967 fnames[j - 1] = fnames[j];
968 --gap->ga_len;
969 }
970}
971
972/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200973 * Expand color scheme, compiler or filetype names.
974 * Search from 'runtimepath':
975 * 'runtimepath'/{dirnames}/{pat}.vim
976 * When "flags" has DIP_START: search also from 'start' of 'packpath':
977 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
978 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
979 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
980 * "dirnames" is an array with one or more directory names.
981 */
982 int
983ExpandRTDir(
984 char_u *pat,
985 int flags,
986 int *num_file,
987 char_u ***file,
988 char *dirnames[])
989{
990 char_u *s;
991 char_u *e;
992 char_u *match;
993 garray_T ga;
994 int i;
995 int pat_len;
996
997 *num_file = 0;
998 *file = NULL;
999 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001000 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001001
1002 for (i = 0; dirnames[i] != NULL; ++i)
1003 {
1004 s = alloc(STRLEN(dirnames[i]) + pat_len + 7);
1005 if (s == NULL)
1006 {
1007 ga_clear_strings(&ga);
1008 return FAIL;
1009 }
1010 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
1011 globpath(p_rtp, s, &ga, 0);
1012 vim_free(s);
1013 }
1014
1015 if (flags & DIP_START) {
1016 for (i = 0; dirnames[i] != NULL; ++i)
1017 {
1018 s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
1019 if (s == NULL)
1020 {
1021 ga_clear_strings(&ga);
1022 return FAIL;
1023 }
1024 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
1025 globpath(p_pp, s, &ga, 0);
1026 vim_free(s);
1027 }
1028 }
1029
1030 if (flags & DIP_OPT) {
1031 for (i = 0; dirnames[i] != NULL; ++i)
1032 {
1033 s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
1034 if (s == NULL)
1035 {
1036 ga_clear_strings(&ga);
1037 return FAIL;
1038 }
1039 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
1040 globpath(p_pp, s, &ga, 0);
1041 vim_free(s);
1042 }
1043 }
1044
1045 for (i = 0; i < ga.ga_len; ++i)
1046 {
1047 match = ((char_u **)ga.ga_data)[i];
1048 s = match;
1049 e = s + STRLEN(s);
1050 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
1051 {
1052 e -= 4;
1053 for (s = e; s > match; MB_PTR_BACK(match, s))
1054 if (s < match || vim_ispathsep(*s))
1055 break;
1056 ++s;
1057 *e = NUL;
1058 mch_memmove(match, s, e - s + 1);
1059 }
1060 }
1061
1062 if (ga.ga_len == 0)
1063 return FAIL;
1064
1065 // Sort and remove duplicates which can happen when specifying multiple
1066 // directories in dirnames.
1067 remove_duplicates(&ga);
1068
1069 *file = ga.ga_data;
1070 *num_file = ga.ga_len;
1071 return OK;
1072}
1073
1074/*
1075 * Expand loadplugin names:
1076 * 'packpath'/pack/ * /opt/{pat}
1077 */
1078 int
1079ExpandPackAddDir(
1080 char_u *pat,
1081 int *num_file,
1082 char_u ***file)
1083{
1084 char_u *s;
1085 char_u *e;
1086 char_u *match;
1087 garray_T ga;
1088 int i;
1089 int pat_len;
1090
1091 *num_file = 0;
1092 *file = NULL;
1093 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001094 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001095
1096 s = alloc(pat_len + 26);
1097 if (s == NULL)
1098 {
1099 ga_clear_strings(&ga);
1100 return FAIL;
1101 }
1102 sprintf((char *)s, "pack/*/opt/%s*", pat);
1103 globpath(p_pp, s, &ga, 0);
1104 vim_free(s);
1105
1106 for (i = 0; i < ga.ga_len; ++i)
1107 {
1108 match = ((char_u **)ga.ga_data)[i];
1109 s = gettail(match);
1110 e = s + STRLEN(s);
1111 mch_memmove(match, s, e - s + 1);
1112 }
1113
1114 if (ga.ga_len == 0)
1115 return FAIL;
1116
1117 // Sort and remove duplicates which can happen when specifying multiple
1118 // directories in dirnames.
1119 remove_duplicates(&ga);
1120
1121 *file = ga.ga_data;
1122 *num_file = ga.ga_len;
1123 return OK;
1124}
1125
1126 static void
1127cmd_source(char_u *fname, exarg_T *eap)
1128{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001129 int clearvars = FALSE;
1130
1131 if (*fname != NUL && STRNCMP(fname, "++clear", 7) == 0)
1132 {
1133 // ++clear argument is supplied
1134 clearvars = TRUE;
1135 fname = fname + 7;
1136 if (*fname != NUL)
1137 {
1138 semsg(_(e_invalid_argument_str), eap->arg);
1139 return;
1140 }
1141 }
1142
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001143 if (*fname != NUL && eap != NULL && eap->addr_count > 0)
1144 {
1145 // if a filename is specified to :source, then a range is not allowed
1146 emsg(_(e_no_range_allowed));
1147 return;
1148 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001149
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001150 if (eap != NULL && *fname == NUL)
1151 {
1152 if (eap->forceit)
1153 // a file name is needed to source normal mode commands
1154 emsg(_(e_argument_required));
1155 else
1156 // source ex commands from the current buffer
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001157 do_source_ext(NULL, FALSE, FALSE, NULL, eap, clearvars);
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001158 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001159 else if (eap != NULL && eap->forceit)
1160 // ":source!": read Normal mode commands
1161 // Need to execute the commands directly. This is required at least
1162 // for:
1163 // - ":g" command busy
1164 // - after ":argdo", ":windo" or ":bufdo"
1165 // - another command follows
1166 // - inside a loop
1167 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
1168#ifdef FEAT_EVAL
1169 || eap->cstack->cs_idx >= 0
1170#endif
1171 );
1172
1173 // ":source" read ex commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001175 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001176}
1177
1178/*
1179 * ":source {fname}"
1180 */
1181 void
1182ex_source(exarg_T *eap)
1183{
1184#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02001185 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001186 {
1187 char_u *fname = NULL;
1188
1189 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
1190 NULL, NULL,
1191 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1192 if (fname != NULL)
1193 {
1194 cmd_source(fname, eap);
1195 vim_free(fname);
1196 }
1197 }
1198 else
1199#endif
1200 cmd_source(eap->arg, eap);
1201}
1202
1203#if defined(FEAT_EVAL) || defined(PROTO)
1204/*
1205 * ":options"
1206 */
1207 void
1208ex_options(
1209 exarg_T *eap UNUSED)
1210{
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001211 char_u buf[500];
1212 int multi_mods = 0;
1213
1214 buf[0] = NUL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001215 (void)add_win_cmd_modifers(buf, &cmdmod, &multi_mods);
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001216
1217 vim_setenv((char_u *)"OPTWIN_CMD", buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001218 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
1219}
1220#endif
1221
1222/*
1223 * ":source" and associated commands.
1224 */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001225
1226#ifdef FEAT_EVAL
1227/*
1228 * Return the address holding the next breakpoint line for a source cookie.
1229 */
1230 linenr_T *
1231source_breakpoint(void *cookie)
1232{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001233 return &((source_cookie_T *)cookie)->breakpoint;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001234}
1235
1236/*
1237 * Return the address holding the debug tick for a source cookie.
1238 */
1239 int *
1240source_dbg_tick(void *cookie)
1241{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001242 return &((source_cookie_T *)cookie)->dbg_tick;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001243}
1244
1245/*
1246 * Return the nesting level for a source cookie.
1247 */
1248 int
1249source_level(void *cookie)
1250{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001251 return ((source_cookie_T *)cookie)->level;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001252}
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001253
1254/*
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02001255 * Return the readahead line. Note that the pointer may become invalid when
1256 * getting the next line, if it's concatenated with the next one.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001257 */
1258 char_u *
1259source_nextline(void *cookie)
1260{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001261 return ((source_cookie_T *)cookie)->nextline;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001262}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001263#endif
1264
1265#if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC)
1266# define USE_FOPEN_NOINH
1267/*
1268 * Special function to open a file without handle inheritance.
1269 * When possible the handle is closed on exec().
1270 */
1271 static FILE *
1272fopen_noinh_readbin(char *filename)
1273{
1274# ifdef MSWIN
1275 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
1276# else
1277 int fd_tmp = mch_open(filename, O_RDONLY, 0);
1278# endif
1279
1280 if (fd_tmp == -1)
1281 return NULL;
1282
1283# ifdef HAVE_FD_CLOEXEC
1284 {
1285 int fdflags = fcntl(fd_tmp, F_GETFD);
1286 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1287 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
1288 }
1289# endif
1290
1291 return fdopen(fd_tmp, READBIN);
1292}
1293#endif
1294
1295/*
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001296 * Initialization for sourcing lines from the current buffer. Reads all the
1297 * lines from the buffer and stores it in the cookie grow array.
1298 * Returns a pointer to the name ":source buffer=<n>" on success and NULL on
1299 * failure.
1300 */
1301 static char_u *
1302do_source_buffer_init(source_cookie_T *sp, exarg_T *eap)
1303{
1304 linenr_T curr_lnum;
1305 char_u *line = NULL;
1306 char_u *fname;
1307
1308 CLEAR_FIELD(*sp);
1309
1310 if (curbuf == NULL)
1311 return NULL;
1312
1313 // Use ":source buffer=<num>" as the script name
1314 vim_snprintf((char *)IObuff, IOSIZE, ":source buffer=%d", curbuf->b_fnum);
1315 fname = vim_strsave(IObuff);
1316 if (fname == NULL)
1317 return NULL;
1318
1319 ga_init2(&sp->buflines, sizeof(char_u *), 100);
1320
1321 // Copy the lines from the buffer into a grow array
1322 for (curr_lnum = eap->line1; curr_lnum <= eap->line2; curr_lnum++)
1323 {
1324 line = vim_strsave(ml_get(curr_lnum));
1325 if (line == NULL)
1326 goto errret;
1327 if (ga_add_string(&sp->buflines, line) == FAIL)
1328 goto errret;
1329 line = NULL;
1330 }
1331 sp->buf_lnum = 0;
1332 sp->source_from_buf = TRUE;
1333
1334 return fname;
1335
1336errret:
1337 vim_free(fname);
1338 vim_free(line);
1339 ga_clear_strings(&sp->buflines);
1340 return NULL;
1341}
1342
1343/*
1344 * Read the file "fname" and execute its lines as EX commands.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1346 * again.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001347 *
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001348 * The 'eap' argument is used when sourcing lines from a buffer instead of a
1349 * file.
1350 *
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001351 * If 'clearvars' is TRUE, then for scripts which are loaded more than
1352 * once, clear all the functions and variables previously defined in that
1353 * script.
1354 *
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001355 * This function may be called recursively!
1356 *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 * Return FAIL if file could not be opened, OK otherwise.
1358 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001359 */
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001360 static int
1361do_source_ext(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001362 char_u *fname,
1363 int check_other, // check for .vimrc and _vimrc
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364 int is_vimrc, // DOSO_ value
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001365 int *ret_sid UNUSED,
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001366 exarg_T *eap,
1367 int clearvars UNUSED)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001368{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001369 source_cookie_T cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001370 char_u *p;
1371 char_u *fname_exp;
1372 char_u *firstline = NULL;
1373 int retval = FAIL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001374 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001375#ifdef FEAT_EVAL
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001376 funccal_entry_T funccalp_entry;
1377 int save_debug_break_level = debug_break_level;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 int sid;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001379 scriptitem_T *si = NULL;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001380 int save_estack_compiling = estack_compiling;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001381#endif
1382#ifdef STARTUPTIME
1383 struct timeval tv_rel;
1384 struct timeval tv_start;
1385#endif
1386#ifdef FEAT_PROFILE
1387 proftime_T wait_start;
1388#endif
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001389 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001390 int trigger_source_post = FALSE;
Bram Moolenaare31ee862020-01-07 20:59:34 +01001391 ESTACK_CHECK_DECLARATION
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001392
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001393 CLEAR_FIELD(cookie);
1394 if (fname == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001395 {
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001396 // sourcing lines from a buffer
1397 fname_exp = do_source_buffer_init(&cookie, eap);
1398 if (fname_exp == NULL)
1399 return FAIL;
1400 }
1401 else
1402 {
1403 p = expand_env_save(fname);
1404 if (p == NULL)
1405 return retval;
1406 fname_exp = fix_fname(p);
1407 vim_free(p);
1408 if (fname_exp == NULL)
1409 return retval;
1410 if (mch_isdir(fname_exp))
1411 {
1412 smsg(_("Cannot source a directory: \"%s\""), fname);
1413 goto theend;
1414 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001415 }
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001416#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001417 estack_compiling = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419 // See if we loaded this script before.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001420 sid = find_script_by_name(fname_exp);
Bram Moolenaarf479cac2022-01-12 12:54:55 +00001421 if (sid > 0 && ret_sid != NULL
1422 && SCRIPT_ITEM(sid)->sn_state != SN_STATE_NOT_LOADED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 {
1424 // Already loaded and no need to load again, return here.
1425 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001426 retval = OK;
1427 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001428 }
1429#endif
1430
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001431 // Apply SourceCmd autocommands, they should get the file and source it.
1432 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1433 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1434 FALSE, curbuf))
1435 {
1436#ifdef FEAT_EVAL
1437 retval = aborting() ? FAIL : OK;
1438#else
1439 retval = OK;
1440#endif
1441 if (retval == OK)
1442 // Apply SourcePost autocommands.
1443 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1444 FALSE, curbuf);
1445 goto theend;
1446 }
1447
1448 // Apply SourcePre autocommands, they may get the file.
1449 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1450
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001451 if (!cookie.source_from_buf)
1452 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001453#ifdef USE_FOPEN_NOINH
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001454 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001455#else
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001456 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001457#endif
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001458 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001459 if (cookie.fp == NULL && check_other)
1460 {
1461 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1462 // and ".exrc" by "_exrc" or vice versa.
1463 p = gettail(fname_exp);
1464 if ((*p == '.' || *p == '_')
1465 && (STRICMP(p + 1, "vimrc") == 0
1466 || STRICMP(p + 1, "gvimrc") == 0
1467 || STRICMP(p + 1, "exrc") == 0))
1468 {
1469 if (*p == '_')
1470 *p = '.';
1471 else
1472 *p = '_';
1473#ifdef USE_FOPEN_NOINH
1474 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1475#else
1476 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1477#endif
1478 }
1479 }
1480
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001481 if (cookie.fp == NULL && !cookie.source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001482 {
1483 if (p_verbose > 0)
1484 {
1485 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001486 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001487 smsg(_("could not source \"%s\""), fname);
1488 else
1489 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001490 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001491 verbose_leave();
1492 }
1493 goto theend;
1494 }
1495
1496 // The file exists.
1497 // - In verbose mode, give a message.
1498 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1499 if (p_verbose > 1)
1500 {
1501 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001502 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001503 smsg(_("sourcing \"%s\""), fname);
1504 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001505 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001506 verbose_leave();
1507 }
1508 if (is_vimrc == DOSO_VIMRC)
1509 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1510 else if (is_vimrc == DOSO_GVIMRC)
1511 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1512
1513#ifdef USE_CRNL
1514 // If no automatic file format: Set default to CR-NL.
1515 if (*p_ffs == NUL)
1516 cookie.fileformat = EOL_DOS;
1517 else
1518 cookie.fileformat = EOL_UNKNOWN;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001519#endif
1520
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001521 if (fname == NULL)
1522 // When sourcing a range of lines from a buffer, use the buffer line
1523 // number.
1524 cookie.sourcing_lnum = eap->line1 - 1;
1525 else
1526 cookie.sourcing_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001527
1528#ifdef FEAT_EVAL
1529 // Check if this script has a breakpoint.
1530 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1531 cookie.fname = fname_exp;
1532 cookie.dbg_tick = debug_tick;
1533
1534 cookie.level = ex_nesting_level;
1535#endif
1536
1537 // Keep the sourcing name/lnum, for recursive calls.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001538 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001539 ESTACK_CHECK_SETUP
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001540
1541#ifdef STARTUPTIME
1542 if (time_fd != NULL)
1543 time_push(&tv_rel, &tv_start);
1544#endif
1545
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001546 // "legacy" does not apply to commands in the script
1547 sticky_cmdmod_flags = 0;
1548
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001549 save_current_sctx = current_sctx;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001550 if (cmdmod.cmod_flags & CMOD_VIM9CMD)
1551 // When the ":vim9cmd" command modifier is used, source the script as a
1552 // Vim9 script.
1553 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1554 else
1555 current_sctx.sc_version = 1; // default script version
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001556
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001557#ifdef FEAT_EVAL
1558# ifdef FEAT_PROFILE
1559 if (do_profiling == PROF_YES)
1560 prof_child_enter(&wait_start); // entering a child now
1561# endif
1562
1563 // Don't use local function variables, if called from a function.
1564 // Also starts profiling timer for nested script.
1565 save_funccal(&funccalp_entry);
1566
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001567 current_sctx.sc_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001568
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001569 // Check if this script was sourced before to find its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001570 // Always use a new sequence number.
1571 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001573 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574 hashtab_T *ht;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001575 int todo;
1576 hashitem_T *hi;
1577 dictitem_T *di;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578
1579 // loading the same script again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001581 si = SCRIPT_ITEM(sid);
1582 if (si->sn_state == SN_STATE_NOT_LOADED)
1583 {
1584 // this script was found but not loaded yet
1585 si->sn_state = SN_STATE_NEW;
1586 }
1587 else
1588 {
1589 si->sn_state = SN_STATE_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001591 if (!clearvars)
1592 {
1593 // Script-local variables remain but "const" can be set again.
1594 // In Vim9 script variables will be cleared when "vim9script"
1595 // is encountered without the "noclear" argument.
1596 ht = &SCRIPT_VARS(sid);
1597 todo = (int)ht->ht_used;
1598 for (hi = ht->ht_array; todo > 0; ++hi)
1599 if (!HASHITEM_EMPTY(hi))
1600 {
1601 --todo;
1602 di = HI2DI(hi);
1603 di->di_flags |= DI_FLAGS_RELOAD;
1604 }
1605 // imports can be redefined once
1606 mark_imports_for_reload(sid);
1607 }
1608 else
1609 clear_vim9_scriptlocal_vars(sid);
Bram Moolenaar0123cc12021-02-07 17:17:58 +01001610
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001611 // reset version, "vim9script" may have been added or removed.
1612 si->sn_version = 1;
1613 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001614 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001616 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001617 int error = OK;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001618
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001619 // It's new, generate a new SID and initialize the scriptitem.
1620 current_sctx.sc_sid = get_new_scriptitem(&error);
1621 if (error == FAIL)
1622 goto almosttheend;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001623 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001624 si->sn_name = fname_exp;
1625 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626 if (ret_sid != NULL)
1627 *ret_sid = current_sctx.sc_sid;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001628
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001629 // Remember the "is_vimrc" flag for when the file is sourced again.
1630 si->sn_is_vimrc = is_vimrc;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001631 }
1632
1633# ifdef FEAT_PROFILE
1634 if (do_profiling == PROF_YES)
1635 {
1636 int forceit;
1637
1638 // Check if we do profiling for this script.
1639 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
1640 {
1641 script_do_profile(si);
1642 si->sn_pr_force = forceit;
1643 }
1644 if (si->sn_prof_on)
1645 {
1646 ++si->sn_pr_count;
1647 profile_start(&si->sn_pr_start);
1648 profile_zero(&si->sn_pr_children);
1649 }
1650 }
1651# endif
1652#endif
1653
1654 cookie.conv.vc_type = CONV_NONE; // no conversion
1655
1656 // Read the first line so we can check for a UTF-8 BOM.
1657 firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1658 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1659 && firstline[1] == 0xbb && firstline[2] == 0xbf)
1660 {
1661 // Found BOM; setup conversion, skip over BOM and recode the line.
1662 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1663 p = string_convert(&cookie.conv, firstline + 3, NULL);
1664 if (p == NULL)
1665 p = vim_strsave(firstline + 3);
1666 if (p != NULL)
1667 {
1668 vim_free(firstline);
1669 firstline = p;
1670 }
1671 }
1672
1673 // Call do_cmdline, which will call getsourceline() to get the lines.
1674 do_cmdline(firstline, getsourceline, (void *)&cookie,
1675 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1676 retval = OK;
1677
1678#ifdef FEAT_PROFILE
1679 if (do_profiling == PROF_YES)
1680 {
1681 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001682 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001683 if (si->sn_prof_on)
1684 {
1685 profile_end(&si->sn_pr_start);
1686 profile_sub_wait(&wait_start, &si->sn_pr_start);
1687 profile_add(&si->sn_pr_total, &si->sn_pr_start);
1688 profile_self(&si->sn_pr_self, &si->sn_pr_start,
1689 &si->sn_pr_children);
1690 }
1691 }
1692#endif
1693
1694 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001695 emsg(_(e_interrupted));
Bram Moolenaare31ee862020-01-07 20:59:34 +01001696 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001697 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001698 if (p_verbose > 1)
1699 {
1700 verbose_enter();
1701 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001702 if (SOURCING_NAME != NULL)
1703 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001704 verbose_leave();
1705 }
1706#ifdef STARTUPTIME
1707 if (time_fd != NULL)
1708 {
1709 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1710 time_msg((char *)IObuff, &tv_start);
1711 time_pop(&tv_rel);
1712 }
1713#endif
1714
1715 if (!got_int)
1716 trigger_source_post = TRUE;
1717
1718#ifdef FEAT_EVAL
1719 // After a "finish" in debug mode, need to break at first command of next
1720 // sourced file.
1721 if (save_debug_break_level > ex_nesting_level
1722 && debug_break_level == ex_nesting_level)
1723 ++debug_break_level;
1724#endif
1725
1726#ifdef FEAT_EVAL
1727almosttheend:
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001728 // If "sn_save_cpo" is set that means we encountered "vim9script": restore
1729 // 'cpoptions', unless in the main .vimrc file.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001731 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001732 if (si->sn_save_cpo != NULL && si->sn_is_vimrc == DOSO_NONE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 {
Bram Moolenaar3e191692021-03-17 17:46:00 +01001734 if (STRCMP(p_cpo, CPO_VIM) != 0)
1735 {
1736 char_u *f;
1737 char_u *t;
1738
1739 // 'cpo' was changed in the script. Apply the same change to the
1740 // saved value, if possible.
1741 for (f = (char_u *)CPO_VIM; *f != NUL; ++f)
1742 if (vim_strchr(p_cpo, *f) == NULL
1743 && (t = vim_strchr(si->sn_save_cpo, *f)) != NULL)
1744 // flag was removed, also remove it from the saved 'cpo'
1745 mch_memmove(t, t + 1, STRLEN(t));
1746 for (f = p_cpo; *f != NUL; ++f)
1747 if (vim_strchr((char_u *)CPO_VIM, *f) == NULL
1748 && vim_strchr(si->sn_save_cpo, *f) == NULL)
1749 {
1750 // flag was added, also add it to the saved 'cpo'
1751 t = alloc(STRLEN(si->sn_save_cpo) + 2);
1752 if (t != NULL)
1753 {
1754 *t = *f;
1755 STRCPY(t + 1, si->sn_save_cpo);
1756 vim_free(si->sn_save_cpo);
1757 si->sn_save_cpo = t;
1758 }
1759 }
1760 }
Bram Moolenaar37294bd2021-03-10 13:40:08 +01001761 set_option_value((char_u *)"cpo", 0L, si->sn_save_cpo, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762 }
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001763 VIM_CLEAR(si->sn_save_cpo);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001764
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001765 restore_funccal();
1766# ifdef FEAT_PROFILE
1767 if (do_profiling == PROF_YES)
1768 prof_child_exit(&wait_start); // leaving a child now
1769# endif
1770#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001771 current_sctx = save_current_sctx;
1772
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001773 if (cookie.fp != NULL)
1774 fclose(cookie.fp);
1775 if (cookie.source_from_buf)
1776 ga_clear_strings(&cookie.buflines);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001777 vim_free(cookie.nextline);
1778 vim_free(firstline);
1779 convert_setup(&cookie.conv, NULL, NULL);
1780
1781 if (trigger_source_post)
1782 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1783
1784theend:
1785 vim_free(fname_exp);
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001786 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001787#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001788 estack_compiling = save_estack_compiling;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001789#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001790 return retval;
1791}
1792
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001793 int
1794do_source(
1795 char_u *fname,
1796 int check_other, // check for .vimrc and _vimrc
1797 int is_vimrc, // DOSO_ value
1798 int *ret_sid UNUSED)
1799{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001800 return do_source_ext(fname, check_other, is_vimrc, ret_sid, NULL, FALSE);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001801}
1802
1803
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001804#if defined(FEAT_EVAL) || defined(PROTO)
1805
1806/*
1807 * ":scriptnames"
1808 */
1809 void
1810ex_scriptnames(exarg_T *eap)
1811{
1812 int i;
1813
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001814 if (eap->addr_count > 0 || *eap->arg != NUL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001815 {
1816 // :script {scriptId}: edit the script
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001817 if (eap->addr_count > 0 && !SCRIPT_ID_VALID(eap->line2))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001818 emsg(_(e_invalid_argument));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001819 else
1820 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001821 if (eap->addr_count > 0)
1822 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
1823 else
1824 {
1825 expand_env(eap->arg, NameBuff, MAXPATHL);
1826 eap->arg = NameBuff;
1827 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001828 do_exedit(eap, NULL);
1829 }
1830 return;
1831 }
1832
1833 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar6079da72022-01-18 14:16:59 +00001834 {
1835 scriptitem_T *si = SCRIPT_ITEM(i);
1836
1837 if (si->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001838 {
Bram Moolenaar6079da72022-01-18 14:16:59 +00001839 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
1840 vim_snprintf((char *)IObuff, IOSIZE, "%3d%s: %s",
1841 i,
1842 si->sn_state == SN_STATE_NOT_LOADED ? " A" : "",
1843 NameBuff);
Bram Moolenaar769f5892022-02-09 14:31:05 +00001844 if (!message_filtered(IObuff))
1845 {
1846 msg_putchar('\n');
1847 msg_outtrans(IObuff);
1848 out_flush(); // output one line at a time
1849 ui_breakcheck();
1850 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001851 }
Bram Moolenaar6079da72022-01-18 14:16:59 +00001852 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001853}
1854
1855# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1856/*
1857 * Fix slashes in the list of script names for 'shellslash'.
1858 */
1859 void
1860scriptnames_slash_adjust(void)
1861{
1862 int i;
1863
1864 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001865 if (SCRIPT_ITEM(i)->sn_name != NULL)
1866 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001867}
1868# endif
1869
1870/*
1871 * Get a pointer to a script name. Used for ":verbose set".
Bram Moolenaar74667062020-12-28 15:41:41 +01001872 * Message appended to "Last set from "
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001873 */
1874 char_u *
1875get_scriptname(scid_T id)
1876{
1877 if (id == SID_MODELINE)
1878 return (char_u *)_("modeline");
1879 if (id == SID_CMDARG)
1880 return (char_u *)_("--cmd argument");
1881 if (id == SID_CARG)
1882 return (char_u *)_("-c argument");
1883 if (id == SID_ENV)
1884 return (char_u *)_("environment variable");
1885 if (id == SID_ERROR)
1886 return (char_u *)_("error handler");
Bram Moolenaar74667062020-12-28 15:41:41 +01001887 if (id == SID_WINLAYOUT)
1888 return (char_u *)_("changed window size");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001889 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001890}
1891
1892# if defined(EXITFREE) || defined(PROTO)
1893 void
1894free_scriptnames(void)
1895{
1896 int i;
1897
1898 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02001899 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001900 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001901
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001902 // the variables themselves are cleared in evalvars_clear()
1903 vim_free(si->sn_vars);
1904
1905 vim_free(si->sn_name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001906 free_imports_and_script_vars(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001907 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02001908# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001909 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02001910# endif
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00001911 vim_free(si->sn_autoload_prefix);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001912 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02001913 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001914 ga_clear(&script_items);
1915}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001916
1917 void
1918free_autoload_scriptnames(void)
1919{
1920 ga_clear_strings(&ga_loaded);
1921}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001922# endif
1923
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001924 linenr_T
Bram Moolenaar66250c92020-08-20 15:02:42 +02001925get_sourced_lnum(
1926 char_u *(*fgetline)(int, void *, int, getline_opt_T),
1927 void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001928{
1929 return fgetline == getsourceline
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001930 ? ((source_cookie_T *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001931 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001932}
Dominique Pelle748b3082022-01-08 12:41:16 +00001933#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001934
1935 static char_u *
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001936get_one_sourceline(source_cookie_T *sp)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001937{
1938 garray_T ga;
1939 int len;
1940 int c;
1941 char_u *buf;
1942#ifdef USE_CRNL
1943 int has_cr; // CR-LF found
1944#endif
1945 int have_read = FALSE;
1946
1947 // use a growarray to store the sourced line
1948 ga_init2(&ga, 1, 250);
1949
1950 // Loop until there is a finished line (or end-of-file).
1951 ++sp->sourcing_lnum;
1952 for (;;)
1953 {
1954 // make room to read at least 120 (more) characters
1955 if (ga_grow(&ga, 120) == FAIL)
1956 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001957 if (sp->source_from_buf)
1958 {
1959 if (sp->buf_lnum >= sp->buflines.ga_len)
1960 break; // all the lines are processed
1961 ga_concat(&ga, ((char_u **)sp->buflines.ga_data)[sp->buf_lnum]);
1962 sp->buf_lnum++;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01001963 if (ga_grow(&ga, 1) == FAIL)
1964 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001965 buf = (char_u *)ga.ga_data;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01001966 buf[ga.ga_len++] = NUL;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001967 }
1968 else
1969 {
1970 buf = (char_u *)ga.ga_data;
1971 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
1972 sp->fp) == NULL)
1973 break;
1974 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001975 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
1976#ifdef USE_CRNL
1977 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
1978 // CTRL-Z by its own, or after a NL.
1979 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
1980 && sp->fileformat == EOL_DOS
1981 && buf[len - 1] == Ctrl_Z)
1982 {
1983 buf[len - 1] = NUL;
1984 break;
1985 }
1986#endif
1987
1988 have_read = TRUE;
1989 ga.ga_len = len;
1990
1991 // If the line was longer than the buffer, read more.
1992 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
1993 continue;
1994
1995 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
1996 {
1997#ifdef USE_CRNL
1998 has_cr = (len >= 2 && buf[len - 2] == '\r');
1999 if (sp->fileformat == EOL_UNKNOWN)
2000 {
2001 if (has_cr)
2002 sp->fileformat = EOL_DOS;
2003 else
2004 sp->fileformat = EOL_UNIX;
2005 }
2006
2007 if (sp->fileformat == EOL_DOS)
2008 {
2009 if (has_cr) // replace trailing CR
2010 {
2011 buf[len - 2] = '\n';
2012 --len;
2013 --ga.ga_len;
2014 }
2015 else // lines like ":map xx yy^M" will have failed
2016 {
2017 if (!sp->error)
2018 {
2019 msg_source(HL_ATTR(HLF_W));
2020 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
2021 }
2022 sp->error = TRUE;
2023 sp->fileformat = EOL_UNIX;
2024 }
2025 }
2026#endif
2027 // The '\n' is escaped if there is an odd number of ^V's just
2028 // before it, first set "c" just before the 'V's and then check
2029 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
2030 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
2031 ;
2032 if ((len & 1) != (c & 1)) // escaped NL, read more
2033 {
2034 ++sp->sourcing_lnum;
2035 continue;
2036 }
2037
2038 buf[len - 1] = NUL; // remove the NL
2039 }
2040
2041 // Check for ^C here now and then, so recursive :so can be broken.
2042 line_breakcheck();
2043 break;
2044 }
2045
2046 if (have_read)
2047 return (char_u *)ga.ga_data;
2048
2049 vim_free(ga.ga_data);
2050 return NULL;
2051}
2052
2053/*
2054 * Get one full line from a sourced file.
2055 * Called by do_cmdline() when it's called from do_source().
2056 *
2057 * Return a pointer to the line in allocated memory.
2058 * Return NULL for end-of-file or some error.
2059 */
2060 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02002061getsourceline(
2062 int c UNUSED,
2063 void *cookie,
2064 int indent UNUSED,
2065 getline_opt_T options)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002066{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002067 source_cookie_T *sp = (source_cookie_T *)cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002068 char_u *line;
2069 char_u *p;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002070 int do_vim9_all = in_vim9script()
2071 && options == GETLINE_CONCAT_ALL;
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002072 int do_bar_cont = do_vim9_all
2073 || options == GETLINE_CONCAT_CONTBAR;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002074
2075#ifdef FEAT_EVAL
2076 // If breakpoints have been added/deleted need to check for it.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002077 if ((sp->dbg_tick < debug_tick) && !sp->source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002078 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002079 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002080 sp->dbg_tick = debug_tick;
2081 }
2082# ifdef FEAT_PROFILE
2083 if (do_profiling == PROF_YES)
2084 script_line_end();
2085# endif
2086#endif
2087
2088 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002089 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002090
2091 // Get current line. If there is a read-ahead line, use it, otherwise get
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002092 // one now. "fp" is NULL if actually using a string.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002093 if (sp->finished || (!sp->source_from_buf && sp->fp == NULL))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002094 line = NULL;
2095 else if (sp->nextline == NULL)
2096 line = get_one_sourceline(sp);
2097 else
2098 {
2099 line = sp->nextline;
2100 sp->nextline = NULL;
2101 ++sp->sourcing_lnum;
2102 }
2103#ifdef FEAT_PROFILE
2104 if (line != NULL && do_profiling == PROF_YES)
2105 script_line_start();
2106#endif
2107
2108 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
2109 // contain the 'C' flag.
Bram Moolenaar66250c92020-08-20 15:02:42 +02002110 if (line != NULL && options != GETLINE_NONE
2111 && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002112 {
Bram Moolenaar5072b472021-06-03 21:56:10 +02002113 int comment_char = in_vim9script() ? '#' : '"';
2114
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002115 // compensate for the one line read-ahead
2116 --sp->sourcing_lnum;
2117
2118 // Get the next line and concatenate it when it starts with a
2119 // backslash. We always need to read the next line, keep it in
2120 // sp->nextline.
2121 /* Also check for a comment in between continuation lines: "\ */
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002122 // Also check for a Vim9 comment, empty line, line starting with '|',
2123 // but not "||".
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002124 sp->nextline = get_one_sourceline(sp);
2125 if (sp->nextline != NULL
2126 && (*(p = skipwhite(sp->nextline)) == '\\'
Bram Moolenaar5072b472021-06-03 21:56:10 +02002127 || (p[0] == comment_char
2128 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002129 || (do_vim9_all && (*p == NUL
2130 || vim9_comment_start(p)))
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002131 || (do_bar_cont && p[0] == '|' && p[1] != '|')))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002132 {
2133 garray_T ga;
2134
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002135 ga_init2(&ga, sizeof(char_u), 400);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002136 ga_concat(&ga, line);
2137 if (*p == '\\')
2138 ga_concat(&ga, p + 1);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002139 else if (*p == '|')
2140 {
2141 ga_concat(&ga, (char_u *)" ");
2142 ga_concat(&ga, p);
2143 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002144 for (;;)
2145 {
2146 vim_free(sp->nextline);
2147 sp->nextline = get_one_sourceline(sp);
2148 if (sp->nextline == NULL)
2149 break;
2150 p = skipwhite(sp->nextline);
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002151 if (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|'))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002152 {
2153 // Adjust the growsize to the current length to speed up
2154 // concatenating many lines.
2155 if (ga.ga_len > 400)
2156 {
2157 if (ga.ga_len > 8000)
2158 ga.ga_growsize = 8000;
2159 else
2160 ga.ga_growsize = ga.ga_len;
2161 }
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002162 if (*p == '\\')
2163 ga_concat(&ga, p + 1);
2164 else
2165 {
2166 ga_concat(&ga, (char_u *)" ");
2167 ga_concat(&ga, p);
2168 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002169 }
Bram Moolenaar5072b472021-06-03 21:56:10 +02002170 else if (!(p[0] == (comment_char)
Bram Moolenaar0f37e352021-06-02 15:28:15 +02002171 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002172 && !(do_vim9_all && (*p == NUL || vim9_comment_start(p))))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002173 break;
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002174 /* drop a # comment or "\ comment line */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002175 }
2176 ga_append(&ga, NUL);
2177 vim_free(line);
2178 line = ga.ga_data;
2179 }
2180 }
2181
2182 if (line != NULL && sp->conv.vc_type != CONV_NONE)
2183 {
2184 char_u *s;
2185
2186 // Convert the encoding of the script line.
2187 s = string_convert(&sp->conv, line, NULL);
2188 if (s != NULL)
2189 {
2190 vim_free(line);
2191 line = s;
2192 }
2193 }
2194
2195#ifdef FEAT_EVAL
2196 // Did we encounter a breakpoint?
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002197 if (!sp->source_from_buf && sp->breakpoint != 0
2198 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002199 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002200 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002201 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002202 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002203 sp->dbg_tick = debug_tick;
2204 }
2205#endif
2206
2207 return line;
2208}
2209
2210/*
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002211 * Returns TRUE if sourcing a script either from a file or a buffer.
2212 * Otherwise returns FALSE.
2213 */
2214 int
2215sourcing_a_script(exarg_T *eap)
2216{
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002217 return (getline_equal(eap->getline, eap->cookie, getsourceline));
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002218}
2219
2220/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002221 * ":scriptencoding": Set encoding conversion for a sourced script.
2222 */
2223 void
2224ex_scriptencoding(exarg_T *eap)
2225{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002226 source_cookie_T *sp;
2227 char_u *name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002228
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002229 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002230 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00002231 emsg(_(e_scriptencoding_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002232 return;
2233 }
2234
2235 if (*eap->arg != NUL)
2236 {
2237 name = enc_canonize(eap->arg);
2238 if (name == NULL) // out of memory
2239 return;
2240 }
2241 else
2242 name = eap->arg;
2243
2244 // Setup for conversion from the specified encoding to 'encoding'.
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002245 sp = (source_cookie_T *)getline_cookie(eap->getline, eap->cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002246 convert_setup(&sp->conv, name, p_enc);
2247
2248 if (name != eap->arg)
2249 vim_free(name);
2250}
2251
2252/*
2253 * ":scriptversion": Set Vim script version for a sourced script.
2254 */
2255 void
2256ex_scriptversion(exarg_T *eap UNUSED)
2257{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002258 int nr;
2259
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002260 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002261 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002262 emsg(_(e_scriptversion_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002263 return;
2264 }
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002265 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002267 emsg(_(e_cannot_use_scriptversion_after_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 return;
2269 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002270
2271 nr = getdigits(&eap->arg);
2272 if (nr == 0 || *eap->arg != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002273 emsg(_(e_invalid_argument));
Bram Moolenaar67979662020-06-20 22:50:47 +02002274 else if (nr > SCRIPT_VERSION_MAX)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002275 semsg(_(e_scriptversion_not_supported_nr), nr);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002276 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002277 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002278 current_sctx.sc_version = nr;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002279#ifdef FEAT_EVAL
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002280 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002281#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002282 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002283}
2284
2285#if defined(FEAT_EVAL) || defined(PROTO)
2286/*
2287 * ":finish": Mark a sourced file as finished.
2288 */
2289 void
2290ex_finish(exarg_T *eap)
2291{
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002292 if (sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002293 do_finish(eap, FALSE);
2294 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00002295 emsg(_(e_finish_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002296}
2297
2298/*
2299 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
2300 * Also called for a pending finish at the ":endtry" or after returning from
2301 * an extra do_cmdline(). "reanimate" is used in the latter case.
2302 */
2303 void
2304do_finish(exarg_T *eap, int reanimate)
2305{
2306 int idx;
2307
2308 if (reanimate)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002309 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002310 eap->cookie))->finished = FALSE;
2311
2312 // Cleanup (and inactivate) conditionals, but stop when a try conditional
2313 // not in its finally clause (which then is to be executed next) is found.
2314 // In this case, make the ":finish" pending for execution at the ":endtry".
2315 // Otherwise, finish normally.
2316 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
2317 if (idx >= 0)
2318 {
2319 eap->cstack->cs_pending[idx] = CSTP_FINISH;
2320 report_make_pending(CSTP_FINISH, NULL);
2321 }
2322 else
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002323 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002324 eap->cookie))->finished = TRUE;
2325}
2326
2327
2328/*
2329 * Return TRUE when a sourced file had the ":finish" command: Don't give error
2330 * message for missing ":endif".
2331 * Return FALSE when not sourcing a file.
2332 */
2333 int
2334source_finished(
Bram Moolenaar66250c92020-08-20 15:02:42 +02002335 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002336 void *cookie)
2337{
2338 return (getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002339 && ((source_cookie_T *)getline_cookie(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002340 fgetline, cookie))->finished);
2341}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002342
2343/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002344 * Find the path of a script below the "autoload" directory.
2345 * Returns NULL if there is no "/autoload/" in the script name.
2346 */
2347 char_u *
2348script_name_after_autoload(scriptitem_T *si)
2349{
2350 char_u *p = si->sn_name;
2351 char_u *res = NULL;
2352
2353 for (;;)
2354 {
2355 char_u *n = (char_u *)strstr((char *)p, "autoload");
2356
2357 if (n == NULL)
2358 break;
2359 if (n > p && vim_ispathsep(n[-1]) && vim_ispathsep(n[8]))
2360 res = n + 9;
2361 p = n + 8;
2362 }
2363 return res;
2364}
2365
2366/*
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002367 * For an autoload script "autoload/dir/script.vim" return the prefix
2368 * "dir#script#" in allocated memory.
2369 * Returns NULL if anything is wrong.
2370 */
2371 char_u *
2372get_autoload_prefix(scriptitem_T *si)
2373{
2374 char_u *p = script_name_after_autoload(si);
2375 char_u *prefix;
2376
2377 if (p == NULL)
2378 return NULL;
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002379 prefix = vim_strsave(p);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002380 if (prefix == NULL)
2381 return NULL;
2382
2383 // replace all '/' with '#' and locate ".vim" at the end
2384 for (p = prefix; *p != NUL; p += mb_ptr2len(p))
2385 {
2386 if (vim_ispathsep(*p))
2387 *p = '#';
2388 else if (STRCMP(p, ".vim") == 0)
2389 {
2390 p[0] = '#';
2391 p[1] = NUL;
2392 return prefix;
2393 }
2394 }
2395
2396 // did not find ".vim" at the end
2397 vim_free(prefix);
2398 return NULL;
2399}
2400
2401/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002402 * If in a Vim9 autoload script return "name" with the autoload prefix for the
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002403 * script. If successful the returned name is allocated.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002404 * Otherwise it returns "name" unmodified.
2405 */
2406 char_u *
2407may_prefix_autoload(char_u *name)
2408{
2409 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
2410 {
2411 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2412
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002413 if (si->sn_autoload_prefix != NULL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002414 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002415 char_u *basename = name;
2416 size_t len;
2417 char_u *res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002418
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002419 if (*name == K_SPECIAL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002420 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002421 char_u *p = vim_strchr(name, '_');
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002422
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002423 // skip over "<SNR>99_"
2424 if (p != NULL)
2425 basename = p + 1;
2426 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002427
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002428 len = STRLEN(si->sn_autoload_prefix) + STRLEN(basename) + 2;
2429 res = alloc(len);
2430 if (res != NULL)
2431 {
2432 vim_snprintf((char *)res, len, "%s%s",
2433 si->sn_autoload_prefix, basename);
2434 return res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002435 }
2436 }
2437 }
2438 return name;
2439}
2440
2441/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002442 * Return the autoload script name for a function or variable name.
2443 * Returns NULL when out of memory.
2444 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
2445 */
2446 char_u *
2447autoload_name(char_u *name)
2448{
2449 char_u *p, *q = NULL;
2450 char_u *scriptname;
2451
2452 // Get the script file name: replace '#' with '/', append ".vim".
2453 scriptname = alloc(STRLEN(name) + 14);
2454 if (scriptname == NULL)
2455 return NULL;
2456 STRCPY(scriptname, "autoload/");
Bram Moolenaara1773442020-08-12 15:21:22 +02002457 STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002458 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
2459 q = p, ++p)
2460 *p = '/';
2461 STRCPY(q, ".vim");
2462 return scriptname;
2463}
2464
2465/*
2466 * If "name" has a package name try autoloading the script for it.
2467 * Return TRUE if a package was loaded.
2468 */
2469 int
2470script_autoload(
2471 char_u *name,
2472 int reload) // load script again when already loaded
2473{
2474 char_u *p;
2475 char_u *scriptname, *tofree;
2476 int ret = FALSE;
2477 int i;
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002478 int ret_sid;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002479
Bram Moolenaar89445512022-04-14 12:58:23 +01002480 // If the name starts with "<SNR>123_" then "123" is the script ID.
2481 if (name[0] == K_SPECIAL && name[1] == KS_EXTRA && name[2] == KE_SNR)
2482 {
2483 p = name + 3;
2484 ret_sid = (int)getdigits(&p);
2485 if (*p == '_' && SCRIPT_ID_VALID(ret_sid))
2486 {
2487 may_load_script(ret_sid, &ret);
2488 return ret;
2489 }
2490 }
2491
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002492 // If there is no '#' after name[0] there is no package name.
2493 p = vim_strchr(name, AUTOLOAD_CHAR);
2494 if (p == NULL || p == name)
2495 return FALSE;
2496
2497 tofree = scriptname = autoload_name(name);
2498 if (scriptname == NULL)
2499 return FALSE;
2500
2501 // Find the name in the list of previously loaded package names. Skip
2502 // "autoload/", it's always the same.
2503 for (i = 0; i < ga_loaded.ga_len; ++i)
2504 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2505 break;
2506 if (!reload && i < ga_loaded.ga_len)
2507 ret = FALSE; // was loaded already
2508 else
2509 {
2510 // Remember the name if it wasn't loaded already.
2511 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2512 {
2513 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2514 tofree = NULL;
2515 }
2516
2517 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002518 // Use "ret_sid" to avoid loading the same script again.
=?UTF-8?q?Bj=C3=B6rn=20Linse?=223a9502022-01-31 17:26:05 +00002519 if (source_in_path(p_rtp, scriptname, DIP_START, &ret_sid) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002520 ret = TRUE;
2521 }
2522
2523 vim_free(tofree);
2524 return ret;
2525}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002526#endif