blob: 008c7b3d41e8e2fd847b9810aaf7a2933f19399f [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.
Yegappan Lakshmananfadc02a2023-01-27 21:03:12 +000059 if (ga_grow(&exestack, 1) == FAIL)
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +000060 return NULL;
61
62 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
63 entry->es_type = type;
64 entry->es_name = name;
65 entry->es_lnum = lnum;
Bram Moolenaar09d46402019-12-29 23:53:01 +010066#ifdef FEAT_EVAL
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +000067 entry->es_info.ufunc = NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +010068#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +000069 ++exestack.ga_len;
70 return entry;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010071}
72
Bram Moolenaar09d46402019-12-29 23:53:01 +010073#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010074/*
75 * Add a user function to the execution stack.
76 */
Bram Moolenaarc620c052020-07-08 15:16:19 +020077 estack_T *
Bram Moolenaar25e0f582020-05-25 22:36:50 +020078estack_push_ufunc(ufunc_T *ufunc, long lnum)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010079{
Bram Moolenaar25e0f582020-05-25 22:36:50 +020080 estack_T *entry = estack_push(ETYPE_UFUNC,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010081 ufunc->uf_name_exp != NULL
82 ? ufunc->uf_name_exp : ufunc->uf_name, lnum);
83 if (entry != NULL)
84 entry->es_info.ufunc = ufunc;
Bram Moolenaarc620c052020-07-08 15:16:19 +020085 return entry;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010086}
Bram Moolenaar25e0f582020-05-25 22:36:50 +020087
88/*
89 * Return TRUE if "ufunc" with "lnum" is already at the top of the exe stack.
90 */
91 int
92estack_top_is_ufunc(ufunc_T *ufunc, long lnum)
93{
94 estack_T *entry;
95
96 if (exestack.ga_len == 0)
97 return FALSE;
98 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
99 return entry->es_type == ETYPE_UFUNC
100 && STRCMP( entry->es_name, ufunc->uf_name_exp != NULL
101 ? ufunc->uf_name_exp : ufunc->uf_name) == 0
102 && entry->es_lnum == lnum;
103}
Bram Moolenaar09d46402019-12-29 23:53:01 +0100104#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100105
106/*
Bram Moolenaarc620c052020-07-08 15:16:19 +0200107 * Take an item off of the execution stack and return it.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100108 */
Bram Moolenaarc620c052020-07-08 15:16:19 +0200109 estack_T *
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100110estack_pop(void)
111{
Bram Moolenaarc620c052020-07-08 15:16:19 +0200112 if (exestack.ga_len == 0)
113 return NULL;
114 --exestack.ga_len;
115 return ((estack_T *)exestack.ga_data) + exestack.ga_len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100116}
117
118/*
Bram Moolenaar89445512022-04-14 12:58:23 +0100119 * Get the current value for "which" in allocated memory.
LemonBoy6013d002022-04-09 21:42:10 +0100120 * "which" is ESTACK_SFILE for <sfile>, ESTACK_STACK for <stack> or
121 * ESTACK_SCRIPT for <script>.
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
LemonBoyeca7c602022-04-14 15:39:43 +0100159 // If evaluated in a function or autocommand, return the path of the script
160 // where it is defined, at script level the current script path is returned
LemonBoy6013d002022-04-09 21:42:10 +0100161 // instead.
162 if (which == ESTACK_SCRIPT)
163 {
LemonBoyeca7c602022-04-14 15:39:43 +0100164 // Walk the stack backwards, starting from the current frame.
165 for (idx = exestack.ga_len - 1; idx >= 0; --idx, --entry)
LemonBoy6013d002022-04-09 21:42:10 +0100166 {
zeertzjqa2471422022-08-23 19:26:05 +0100167 if (entry->es_type == ETYPE_UFUNC || entry->es_type == ETYPE_AUCMD)
LemonBoy6013d002022-04-09 21:42:10 +0100168 {
zeertzjqa2471422022-08-23 19:26:05 +0100169 sctx_T *def_ctx = entry->es_type == ETYPE_UFUNC
170 ? &entry->es_info.ufunc->uf_script_ctx
171 : acp_script_ctx(entry->es_info.aucmd);
LemonBoy6013d002022-04-09 21:42:10 +0100172
zeertzjqa2471422022-08-23 19:26:05 +0100173 return def_ctx->sc_sid > 0
174 ? vim_strsave(SCRIPT_ITEM(def_ctx->sc_sid)->sn_name)
175 : NULL;
LemonBoyeca7c602022-04-14 15:39:43 +0100176 }
177 else if (entry->es_type == ETYPE_SCRIPT)
LemonBoyeca7c602022-04-14 15:39:43 +0100178 return vim_strsave(entry->es_name);
LemonBoy6013d002022-04-09 21:42:10 +0100179 }
180 return NULL;
181 }
182
Bram Moolenaara5d04232020-07-26 15:37:02 +0200183 // Give information about each stack entry up to the root.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100184 // For a function we compose the call stack, as it was done in the past:
185 // "function One[123]..Two[456]..Three"
Bram Moolenaara5d04232020-07-26 15:37:02 +0200186 ga_init2(&ga, sizeof(char), 100);
187 for (idx = 0; idx < exestack.ga_len; ++idx)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100188 {
189 entry = ((estack_T *)exestack.ga_data) + idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200190 if (entry->es_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100191 {
Bram Moolenaara810db32020-09-11 17:59:23 +0200192 long lnum = 0;
193 char *dots;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200194
Bram Moolenaara5d04232020-07-26 15:37:02 +0200195 len = STRLEN(entry->es_name) + 15;
196 type_name = "";
197 if (entry->es_type != last_type)
198 {
199 switch (entry->es_type)
200 {
201 case ETYPE_SCRIPT: type_name = "script "; break;
202 case ETYPE_UFUNC: type_name = "function "; break;
203 default: type_name = ""; break;
204 }
205 last_type = entry->es_type;
206 }
207 len += STRLEN(type_name);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200208 if (ga_grow(&ga, (int)len) == FAIL)
Bram Moolenaara5d04232020-07-26 15:37:02 +0200209 break;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200210 if (idx == exestack.ga_len - 1)
211 lnum = which == ESTACK_STACK ? SOURCING_LNUM : 0;
212 else
213 lnum = entry->es_lnum;
Bram Moolenaara810db32020-09-11 17:59:23 +0200214 dots = idx == exestack.ga_len - 1 ? "" : "..";
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200215 if (lnum == 0)
216 // For the bottom entry of <sfile>: do not add the line number,
217 // it is used in <slnum>. Also leave it out when the number is
218 // not set.
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200219 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s%s",
Bram Moolenaara810db32020-09-11 17:59:23 +0200220 type_name, entry->es_name, dots);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200221 else
Bram Moolenaara810db32020-09-11 17:59:23 +0200222 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s[%ld]%s",
223 type_name, entry->es_name, lnum, dots);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200224 ga.ga_len += (int)STRLEN((char *)ga.ga_data + ga.ga_len);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100225 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100226 }
227
Bram Moolenaara5d04232020-07-26 15:37:02 +0200228 return (char_u *)ga.ga_data;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100229#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100230}
231
232/*
zeertzjq5c8771b2023-01-24 12:34:03 +0000233 * Get DIP_ flags from the [where] argument of a :runtime command.
234 * "*argp" is advanced to after the [where] argument if it is found.
zeertzjq3770f4c2023-01-22 18:38:51 +0000235 */
236 static int
zeertzjq5c8771b2023-01-24 12:34:03 +0000237get_runtime_cmd_flags(char_u **argp, size_t where_len)
zeertzjq3770f4c2023-01-22 18:38:51 +0000238{
239 char_u *arg = *argp;
zeertzjq3770f4c2023-01-22 18:38:51 +0000240
zeertzjq5c8771b2023-01-24 12:34:03 +0000241 if (where_len == 0)
zeertzjq3770f4c2023-01-22 18:38:51 +0000242 return 0;
243
zeertzjq5c8771b2023-01-24 12:34:03 +0000244 if (STRNCMP(arg, "START", where_len) == 0)
zeertzjq3770f4c2023-01-22 18:38:51 +0000245 {
zeertzjq5c8771b2023-01-24 12:34:03 +0000246 *argp = skipwhite(arg + where_len);
zeertzjq3770f4c2023-01-22 18:38:51 +0000247 return DIP_START + DIP_NORTP;
248 }
zeertzjq5c8771b2023-01-24 12:34:03 +0000249 if (STRNCMP(arg, "OPT", where_len) == 0)
zeertzjq3770f4c2023-01-22 18:38:51 +0000250 {
zeertzjq5c8771b2023-01-24 12:34:03 +0000251 *argp = skipwhite(arg + where_len);
zeertzjq3770f4c2023-01-22 18:38:51 +0000252 return DIP_OPT + DIP_NORTP;
253 }
zeertzjq5c8771b2023-01-24 12:34:03 +0000254 if (STRNCMP(arg, "PACK", where_len) == 0)
zeertzjq3770f4c2023-01-22 18:38:51 +0000255 {
zeertzjq5c8771b2023-01-24 12:34:03 +0000256 *argp = skipwhite(arg + where_len);
zeertzjq3770f4c2023-01-22 18:38:51 +0000257 return DIP_START + DIP_OPT + DIP_NORTP;
258 }
zeertzjq5c8771b2023-01-24 12:34:03 +0000259 if (STRNCMP(arg, "ALL", where_len) == 0)
zeertzjq3770f4c2023-01-22 18:38:51 +0000260 {
zeertzjq5c8771b2023-01-24 12:34:03 +0000261 *argp = skipwhite(arg + where_len);
zeertzjq3770f4c2023-01-22 18:38:51 +0000262 return DIP_START + DIP_OPT;
263 }
264
265 return 0;
266}
267
268/*
zeertzjq5c8771b2023-01-24 12:34:03 +0000269 * ":runtime [where] {name}"
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200270 */
271 void
272ex_runtime(exarg_T *eap)
273{
274 char_u *arg = eap->arg;
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200275 int flags = eap->forceit ? DIP_ALL : 0;
zeertzjq5c8771b2023-01-24 12:34:03 +0000276 char_u *p = skiptowhite(arg);
277 flags += get_runtime_cmd_flags(&arg, p - arg);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200278 source_runtime(arg, flags);
279}
280
zeertzjq3770f4c2023-01-22 18:38:51 +0000281static int runtime_expand_flags;
282
283/*
284 * Set the completion context for the :runtime command.
285 */
286 void
287set_context_in_runtime_cmd(expand_T *xp, char_u *arg)
288{
zeertzjq5c8771b2023-01-24 12:34:03 +0000289 char_u *p = skiptowhite(arg);
290 runtime_expand_flags
291 = *p != NUL ? get_runtime_cmd_flags(&arg, p - arg) : 0;
zeertzjq3770f4c2023-01-22 18:38:51 +0000292 xp->xp_context = EXPAND_RUNTIME;
293 xp->xp_pattern = arg;
294}
295
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200296 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297source_callback(char_u *fname, void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200298{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200300}
301
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000302#ifdef FEAT_EVAL
303/*
304 * Find an already loaded script "name".
305 * If found returns its script ID. If not found returns -1.
306 */
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100307 int
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000308find_script_by_name(char_u *name)
309{
310 int sid;
311 scriptitem_T *si;
312
313 for (sid = script_items.ga_len; sid > 0; --sid)
314 {
315 // We used to check inode here, but that doesn't work:
316 // - If a script is edited and written, it may get a different
317 // inode number, even though to the user it is the same script.
318 // - If a script is deleted and another script is written, with a
319 // different name, the inode may be re-used.
320 si = SCRIPT_ITEM(sid);
321 if (si->sn_name != NULL && fnamecmp(si->sn_name, name) == 0)
322 return sid;
323 }
324 return -1;
325}
326
327/*
328 * Add a new scriptitem with all items initialized.
329 * When running out of memory "error" is set to FAIL.
330 * Returns the script ID.
331 */
332 static int
333get_new_scriptitem(int *error)
334{
335 static scid_T last_current_SID = 0;
336 int sid = ++last_current_SID;
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000337 scriptitem_T *si = NULL;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000338
339 if (ga_grow(&script_items, (int)(sid - script_items.ga_len)) == FAIL)
340 {
341 *error = FAIL;
342 return sid;
343 }
344 while (script_items.ga_len < sid)
345 {
346 si = ALLOC_CLEAR_ONE(scriptitem_T);
347 if (si == NULL)
348 {
349 *error = FAIL;
350 return sid;
351 }
352 ++script_items.ga_len;
353 SCRIPT_ITEM(script_items.ga_len) = si;
354 si->sn_name = NULL;
355 si->sn_version = 1;
356
357 // Allocate the local script variables to use for this script.
358 new_script_vars(script_items.ga_len);
359 ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
360 hash_init(&si->sn_all_vars.dv_hashtab);
361 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
362 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
363# ifdef FEAT_PROFILE
364 si->sn_prof_on = FALSE;
365# endif
366 }
367
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000368 // "si" can't be NULL, check only to avoid a compiler warning
369 if (si != NULL)
370 // Used to check script variable index is still valid.
371 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000372
373 return sid;
374}
375
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100376 int
377get_new_scriptitem_for_fname(int *error, char_u *fname)
378{
379 int sid = get_new_scriptitem(error);
380
381 if (*error == OK)
382 {
383 scriptitem_T *si = SCRIPT_ITEM(sid);
384
385 si->sn_name = vim_strsave(fname);
386 si->sn_state = SN_STATE_NOT_LOADED;
387 }
388 return sid;
389}
390
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000391 static void
392find_script_callback(char_u *fname, void *cookie)
393{
394 int sid;
395 int error = OK;
396 int *ret_sid = cookie;
397
398 sid = find_script_by_name(fname);
399 if (sid < 0)
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000400 // script does not exist yet, create a new scriptitem
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100401 sid = get_new_scriptitem_for_fname(&error, fname);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000402 *ret_sid = sid;
403}
404#endif
405
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200406/*
407 * Find the file "name" in all directories in "path" and invoke
408 * "callback(fname, cookie)".
409 * "name" can contain wildcards.
410 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
411 * When "flags" has DIP_DIR: find directories instead of files.
412 * When "flags" has DIP_ERR: give an error message if there is no match.
413 *
414 * return FAIL when no file could be sourced, OK otherwise.
415 */
416 int
417do_in_path(
418 char_u *path,
419 char_u *name,
420 int flags,
421 void (*callback)(char_u *fname, void *ck),
422 void *cookie)
423{
424 char_u *rtp;
425 char_u *np;
426 char_u *buf;
427 char_u *rtp_copy;
428 char_u *tail;
429 int num_files;
430 char_u **files;
431 int i;
432 int did_one = FALSE;
433#ifdef AMIGA
434 struct Process *proc = (struct Process *)FindTask(0L);
435 APTR save_winptr = proc->pr_WindowPtr;
436
437 // Avoid a requester here for a volume that doesn't exist.
438 proc->pr_WindowPtr = (APTR)-1L;
439#endif
440
441 // Make a copy of 'runtimepath'. Invoking the callback may change the
442 // value.
443 rtp_copy = vim_strsave(path);
444 buf = alloc(MAXPATHL);
445 if (buf != NULL && rtp_copy != NULL)
446 {
Bram Moolenaar647a5302020-05-03 17:01:24 +0200447 if (p_verbose > 10 && name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200448 {
449 verbose_enter();
450 smsg(_("Searching for \"%s\" in \"%s\""),
451 (char *)name, (char *)path);
452 verbose_leave();
453 }
454
455 // Loop over all entries in 'runtimepath'.
456 rtp = rtp_copy;
457 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
458 {
459 size_t buflen;
460
461 // Copy the path from 'runtimepath' to buf[].
462 copy_option_part(&rtp, buf, MAXPATHL, ",");
463 buflen = STRLEN(buf);
464
465 // Skip after or non-after directories.
466 if (flags & (DIP_NOAFTER | DIP_AFTER))
467 {
468 int is_after = buflen >= 5
469 && STRCMP(buf + buflen - 5, "after") == 0;
470
471 if ((is_after && (flags & DIP_NOAFTER))
472 || (!is_after && (flags & DIP_AFTER)))
473 continue;
474 }
475
476 if (name == NULL)
477 {
478 (*callback)(buf, (void *) &cookie);
479 if (!did_one)
480 did_one = (cookie == NULL);
481 }
482 else if (buflen + STRLEN(name) + 2 < MAXPATHL)
483 {
484 add_pathsep(buf);
485 tail = buf + STRLEN(buf);
486
487 // Loop over all patterns in "name"
488 np = name;
489 while (*np != NUL && ((flags & DIP_ALL) || !did_one))
490 {
491 // Append the pattern from "name" to buf[].
492 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
493 "\t ");
494
Bram Moolenaar647a5302020-05-03 17:01:24 +0200495 if (p_verbose > 10)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200496 {
497 verbose_enter();
498 smsg(_("Searching for \"%s\""), buf);
499 verbose_leave();
500 }
501
502 // Expand wildcards, invoke the callback for each match.
503 if (gen_expand_wildcards(1, &buf, &num_files, &files,
504 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
505 {
506 for (i = 0; i < num_files; ++i)
507 {
508 (*callback)(files[i], cookie);
509 did_one = TRUE;
510 if (!(flags & DIP_ALL))
511 break;
512 }
513 FreeWild(num_files, files);
514 }
515 }
516 }
517 }
518 }
519 vim_free(buf);
520 vim_free(rtp_copy);
521 if (!did_one && name != NULL)
522 {
523 char *basepath = path == p_rtp ? "runtimepath" : "packpath";
524
525 if (flags & DIP_ERR)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000526 semsg(_(e_directory_not_found_in_str_str), basepath, name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200527 else if (p_verbose > 0)
528 {
529 verbose_enter();
530 smsg(_("not found in '%s': \"%s\""), basepath, name);
531 verbose_leave();
532 }
533 }
534
535#ifdef AMIGA
536 proc->pr_WindowPtr = save_winptr;
537#endif
538
539 return did_one ? OK : FAIL;
540}
541
542/*
543 * Find "name" in "path". When found, invoke the callback function for
544 * it: callback(fname, "cookie")
545 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first
546 * one is used.
547 * Returns OK when at least one match found, FAIL otherwise.
548 *
549 * If "name" is NULL calls callback for each entry in "path". Cookie is
550 * passed by reference in this case, setting it to NULL indicates that callback
551 * has done its job.
552 */
553 static int
554do_in_path_and_pp(
555 char_u *path,
556 char_u *name,
557 int flags,
558 void (*callback)(char_u *fname, void *ck),
559 void *cookie)
560{
561 int done = FAIL;
562 char_u *s;
563 int len;
564 char *start_dir = "pack/*/start/*/%s";
565 char *opt_dir = "pack/*/opt/*/%s";
566
567 if ((flags & DIP_NORTP) == 0)
568 done = do_in_path(path, name, flags, callback, cookie);
569
570 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
571 {
572 len = (int)(STRLEN(start_dir) + STRLEN(name));
573 s = alloc(len);
574 if (s == NULL)
575 return FAIL;
576 vim_snprintf((char *)s, len, start_dir, name);
577 done = do_in_path(p_pp, s, flags, callback, cookie);
578 vim_free(s);
579 }
580
581 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
582 {
583 len = (int)(STRLEN(opt_dir) + STRLEN(name));
584 s = alloc(len);
585 if (s == NULL)
586 return FAIL;
587 vim_snprintf((char *)s, len, opt_dir, name);
588 done = do_in_path(p_pp, s, flags, callback, cookie);
589 vim_free(s);
590 }
591
592 return done;
593}
594
595/*
596 * Just like do_in_path_and_pp(), using 'runtimepath' for "path".
597 */
598 int
599do_in_runtimepath(
600 char_u *name,
601 int flags,
602 void (*callback)(char_u *fname, void *ck),
603 void *cookie)
604{
605 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
606}
607
608/*
609 * Source the file "name" from all directories in 'runtimepath'.
610 * "name" can contain wildcards.
611 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
612 *
613 * return FAIL when no file could be sourced, OK otherwise.
614 */
615 int
616source_runtime(char_u *name, int flags)
617{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618 return source_in_path(p_rtp, name, flags, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200619}
620
621/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000622 * Just like source_runtime(), but use "path" instead of 'runtimepath'
623 * and return the script ID in "ret_sid".
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200624 */
625 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200627{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100628 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200629}
630
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200631#if defined(FEAT_EVAL) || defined(PROTO)
632
633/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000634 * Find "name" in 'runtimepath'. If found a new scriptitem is created for it
635 * and it's script ID is returned.
636 * If not found returns -1.
637 */
638 int
639find_script_in_rtp(char_u *name)
640{
641 int sid = -1;
642
643 (void)do_in_path_and_pp(p_rtp, name, DIP_NOAFTER,
644 find_script_callback, &sid);
645 return sid;
646}
647
648/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200649 * Expand wildcards in "pat" and invoke do_source() for each match.
650 */
651 static void
652source_all_matches(char_u *pat)
653{
654 int num_files;
655 char_u **files;
656 int i;
657
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000658 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) != OK)
659 return;
660
661 for (i = 0; i < num_files; ++i)
662 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
663 FreeWild(num_files, files);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200664}
665
666/*
667 * Add the package directory to 'runtimepath'.
668 */
669 static int
670add_pack_dir_to_rtp(char_u *fname)
671{
672 char_u *p4, *p3, *p2, *p1, *p;
673 char_u *entry;
674 char_u *insp = NULL;
675 int c;
676 char_u *new_rtp;
677 int keep;
678 size_t oldlen;
679 size_t addlen;
680 size_t new_rtp_len;
681 char_u *afterdir = NULL;
682 size_t afterlen = 0;
683 char_u *after_insp = NULL;
684 char_u *ffname = NULL;
685 size_t fname_len;
686 char_u *buf = NULL;
687 char_u *rtp_ffname;
688 int match;
689 int retval = FAIL;
690
691 p4 = p3 = p2 = p1 = get_past_head(fname);
692 for (p = p1; *p; MB_PTR_ADV(p))
693 if (vim_ispathsep_nocolon(*p))
694 {
695 p4 = p3; p3 = p2; p2 = p1; p1 = p;
696 }
697
698 // now we have:
699 // rtp/pack/name/start/name
700 // p4 p3 p2 p1
701 //
702 // find the part up to "pack" in 'runtimepath'
703 c = *++p4; // append pathsep in order to expand symlink
704 *p4 = NUL;
705 ffname = fix_fname(fname);
706 *p4 = c;
707 if (ffname == NULL)
708 return FAIL;
709
710 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
711 // Also stop at the first "after" directory.
712 fname_len = STRLEN(ffname);
713 buf = alloc(MAXPATHL);
714 if (buf == NULL)
715 goto theend;
716 for (entry = p_rtp; *entry != NUL; )
717 {
718 char_u *cur_entry = entry;
719
720 copy_option_part(&entry, buf, MAXPATHL, ",");
721 if (insp == NULL)
722 {
723 add_pathsep(buf);
724 rtp_ffname = fix_fname(buf);
725 if (rtp_ffname == NULL)
726 goto theend;
727 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
728 vim_free(rtp_ffname);
729 if (match)
730 // Insert "ffname" after this entry (and comma).
731 insp = entry;
732 }
733
734 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
735 && p > buf
736 && vim_ispathsep(p[-1])
737 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
738 {
739 if (insp == NULL)
740 // Did not find "ffname" before the first "after" directory,
741 // insert it before this entry.
742 insp = cur_entry;
743 after_insp = cur_entry;
744 break;
745 }
746 }
747
748 if (insp == NULL)
749 // Both "fname" and "after" not found, append at the end.
750 insp = p_rtp + STRLEN(p_rtp);
751
752 // check if rtp/pack/name/start/name/after exists
753 afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
754 if (afterdir != NULL && mch_isdir(afterdir))
755 afterlen = STRLEN(afterdir) + 1; // add one for comma
756
757 oldlen = STRLEN(p_rtp);
758 addlen = STRLEN(fname) + 1; // add one for comma
759 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
760 if (new_rtp == NULL)
761 goto theend;
762
763 // We now have 'rtp' parts: {keep}{keep_after}{rest}.
764 // Create new_rtp, first: {keep},{fname}
765 keep = (int)(insp - p_rtp);
766 mch_memmove(new_rtp, p_rtp, keep);
767 new_rtp_len = keep;
768 if (*insp == NUL)
769 new_rtp[new_rtp_len++] = ','; // add comma before
770 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
771 new_rtp_len += addlen - 1;
772 if (*insp != NUL)
773 new_rtp[new_rtp_len++] = ','; // add comma after
774
775 if (afterlen > 0 && after_insp != NULL)
776 {
777 int keep_after = (int)(after_insp - p_rtp);
778
779 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
780 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
781 keep_after - keep);
782 new_rtp_len += keep_after - keep;
783 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
784 new_rtp_len += afterlen - 1;
785 new_rtp[new_rtp_len++] = ',';
786 keep = keep_after;
787 }
788
789 if (p_rtp[keep] != NUL)
790 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
791 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
792 else
793 new_rtp[new_rtp_len] = NUL;
794
795 if (afterlen > 0 && after_insp == NULL)
796 {
797 // Append afterdir when "after" was not found:
798 // {keep},{fname}{rest},{afterdir}
799 STRCAT(new_rtp, ",");
800 STRCAT(new_rtp, afterdir);
801 }
802
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100803 set_option_value_give_err((char_u *)"rtp", 0L, new_rtp, 0);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200804 vim_free(new_rtp);
805 retval = OK;
806
807theend:
808 vim_free(buf);
809 vim_free(ffname);
810 vim_free(afterdir);
811 return retval;
812}
813
814/*
815 * Load scripts in "plugin" and "ftdetect" directories of the package.
816 */
817 static int
818load_pack_plugin(char_u *fname)
819{
820 static char *plugpat = "%s/plugin/**/*.vim";
821 static char *ftpat = "%s/ftdetect/*.vim";
822 int len;
823 char_u *ffname = fix_fname(fname);
824 char_u *pat = NULL;
825 int retval = FAIL;
826
827 if (ffname == NULL)
828 return FAIL;
829 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
830 pat = alloc(len);
831 if (pat == NULL)
832 goto theend;
833 vim_snprintf((char *)pat, len, plugpat, ffname);
834 source_all_matches(pat);
835
836 {
837 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
838
839 // If runtime/filetype.vim wasn't loaded yet, the scripts will be
840 // found when it loads.
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100841 if (cmd != NULL && eval_to_number(cmd, FALSE) > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200842 {
843 do_cmdline_cmd((char_u *)"augroup filetypedetect");
844 vim_snprintf((char *)pat, len, ftpat, ffname);
845 source_all_matches(pat);
846 do_cmdline_cmd((char_u *)"augroup END");
847 }
848 vim_free(cmd);
849 }
850 vim_free(pat);
851 retval = OK;
852
853theend:
854 vim_free(ffname);
855 return retval;
856}
857
858// used for "cookie" of add_pack_plugin()
859static int APP_ADD_DIR;
860static int APP_LOAD;
861static int APP_BOTH;
862
863 static void
864add_pack_plugin(char_u *fname, void *cookie)
865{
866 if (cookie != &APP_LOAD)
867 {
868 char_u *buf = alloc(MAXPATHL);
869 char_u *p;
870 int found = FALSE;
871
872 if (buf == NULL)
873 return;
874 p = p_rtp;
875 while (*p != NUL)
876 {
877 copy_option_part(&p, buf, MAXPATHL, ",");
878 if (pathcmp((char *)buf, (char *)fname, -1) == 0)
879 {
880 found = TRUE;
881 break;
882 }
883 }
884 vim_free(buf);
885 if (!found)
886 // directory is not yet in 'runtimepath', add it
887 if (add_pack_dir_to_rtp(fname) == FAIL)
888 return;
889 }
890
891 if (cookie != &APP_ADD_DIR)
892 load_pack_plugin(fname);
893}
894
895/*
896 * Add all packages in the "start" directory to 'runtimepath'.
897 */
898 void
899add_pack_start_dirs(void)
900{
901 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
902 add_pack_plugin, &APP_ADD_DIR);
903}
904
905/*
906 * Load plugins from all packages in the "start" directory.
907 */
908 void
909load_start_packages(void)
910{
911 did_source_packages = TRUE;
912 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
913 add_pack_plugin, &APP_LOAD);
914}
915
916/*
917 * ":packloadall"
918 * Find plugins in the package directories and source them.
919 */
920 void
921ex_packloadall(exarg_T *eap)
922{
923 if (!did_source_packages || eap->forceit)
924 {
925 // First do a round to add all directories to 'runtimepath', then load
926 // the plugins. This allows for plugins to use an autoload directory
927 // of another plugin.
928 add_pack_start_dirs();
929 load_start_packages();
930 }
931}
932
933/*
934 * ":packadd[!] {name}"
935 */
936 void
937ex_packadd(exarg_T *eap)
938{
939 static char *plugpat = "pack/*/%s/%s";
940 int len;
941 char *pat;
942 int round;
943 int res = OK;
944
945 // Round 1: use "start", round 2: use "opt".
946 for (round = 1; round <= 2; ++round)
947 {
948 // Only look under "start" when loading packages wasn't done yet.
949 if (round == 1 && did_source_packages)
950 continue;
951
952 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
953 pat = alloc(len);
954 if (pat == NULL)
955 return;
956 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
957 // The first round don't give a "not found" error, in the second round
958 // only when nothing was found in the first round.
959 res = do_in_path(p_pp, (char_u *)pat,
960 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
961 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
962 vim_free(pat);
963 }
964}
965#endif
966
967/*
Bram Moolenaar26262f82019-09-04 20:59:15 +0200968 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
969 * list of file names in allocated memory.
970 */
971 void
972remove_duplicates(garray_T *gap)
973{
974 int i;
975 int j;
976 char_u **fnames = (char_u **)gap->ga_data;
977
978 sort_strings(fnames, gap->ga_len);
979 for (i = gap->ga_len - 1; i > 0; --i)
980 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
981 {
982 vim_free(fnames[i]);
983 for (j = i + 1; j < gap->ga_len; ++j)
984 fnames[j - 1] = fnames[j];
985 --gap->ga_len;
986 }
987}
988
zeertzjq5c8771b2023-01-24 12:34:03 +0000989 static void
990ExpandRTDir_int(
991 char_u *pat,
992 size_t pat_len,
993 int flags,
994 int keep_ext,
995 garray_T *gap,
996 char *dirnames[])
997{
998 for (int i = 0; dirnames[i] != NULL; ++i)
999 {
1000 size_t buf_len = STRLEN(dirnames[i]) + pat_len + 22;
1001 char *buf = alloc(buf_len);
1002 if (buf == NULL)
1003 {
1004 ga_clear_strings(gap);
1005 return;
1006 }
1007 char *tail = buf + 15;
1008 size_t tail_buflen = buf_len - 15;
1009 int glob_flags = 0;
1010 int expand_dirs = FALSE;
1011
1012 if (*(dirnames[i]) == NUL) // empty dir used for :runtime
1013 vim_snprintf(tail, tail_buflen, "%s*.vim", pat);
1014 else
1015 vim_snprintf(tail, tail_buflen, "%s/%s*.vim", dirnames[i], pat);
1016
1017expand:
1018 if ((flags & DIP_NORTP) == 0)
1019 globpath(p_rtp, (char_u *)tail, gap, glob_flags, expand_dirs);
1020
1021 if (flags & DIP_START)
1022 {
1023 memcpy(tail - 15, "pack/*/start/*/", 15);
1024 globpath(p_pp, (char_u *)tail - 15, gap, glob_flags, expand_dirs);
1025 }
1026
1027 if (flags & DIP_OPT)
1028 {
1029 memcpy(tail - 13, "pack/*/opt/*/", 13);
1030 globpath(p_pp, (char_u *)tail - 13, gap, glob_flags, expand_dirs);
1031 }
1032
1033 if (*(dirnames[i]) == NUL && !expand_dirs)
1034 {
1035 // expand dir names in another round
1036 vim_snprintf(tail, tail_buflen, "%s*", pat);
1037 glob_flags = WILD_ADD_SLASH;
1038 expand_dirs = TRUE;
1039 goto expand;
1040 }
1041
1042 vim_free(buf);
1043 }
1044
1045 int pat_pathsep_cnt = 0;
1046 for (size_t i = 0; i < pat_len; ++i)
1047 if (vim_ispathsep(pat[i]))
1048 ++pat_pathsep_cnt;
1049
1050 for (int i = 0; i < gap->ga_len; ++i)
1051 {
1052 char_u *match = ((char_u **)gap->ga_data)[i];
1053 char_u *s = match;
1054 char_u *e = s + STRLEN(s);
1055 if (e - 4 > s && !keep_ext && STRNICMP(e - 4, ".vim", 4) == 0)
1056 {
1057 e -= 4;
1058 *e = NUL;
1059 }
1060
1061 int match_pathsep_cnt = (e > s && e[-1] == '/') ? -1 : 0;
1062 for (s = e; s > match; MB_PTR_BACK(match, s))
1063 if (s < match || (vim_ispathsep(*s)
1064 && ++match_pathsep_cnt > pat_pathsep_cnt))
1065 break;
1066 ++s;
1067 if (s != match)
1068 mch_memmove(match, s, e - s + 1);
1069 }
1070
1071 if (gap->ga_len == 0)
1072 return;
1073
1074 // Sort and remove duplicates which can happen when specifying multiple
1075 // directories in dirnames.
1076 remove_duplicates(gap);
1077}
1078
Bram Moolenaar26262f82019-09-04 20:59:15 +02001079/*
zeertzjq3770f4c2023-01-22 18:38:51 +00001080 * Expand runtime file names.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001081 * Search from 'runtimepath':
1082 * 'runtimepath'/{dirnames}/{pat}.vim
zeertzjq3770f4c2023-01-22 18:38:51 +00001083 * When "flags" has DIP_START: search also from "start" of 'packpath':
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001084 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
zeertzjq3770f4c2023-01-22 18:38:51 +00001085 * When "flags" has DIP_OPT: search also from "opt" of 'packpath':
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001086 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
1087 * "dirnames" is an array with one or more directory names.
1088 */
1089 int
1090ExpandRTDir(
1091 char_u *pat,
1092 int flags,
1093 int *num_file,
1094 char_u ***file,
1095 char *dirnames[])
1096{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001097 *num_file = 0;
1098 *file = NULL;
zeertzjq5c8771b2023-01-24 12:34:03 +00001099
1100 garray_T ga;
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001101 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001102
zeertzjq5c8771b2023-01-24 12:34:03 +00001103 ExpandRTDir_int(pat, STRLEN(pat), flags, FALSE, &ga, dirnames);
1104
1105 if (ga.ga_len == 0)
1106 return FAIL;
1107
1108 *file = ga.ga_data;
1109 *num_file = ga.ga_len;
1110 return OK;
1111}
1112
1113/*
1114 * Handle command line completion for :runtime command.
1115 */
1116 int
1117expand_runtime_cmd(char_u *pat, int *numMatches, char_u ***matches)
1118{
1119 *numMatches = 0;
1120 *matches = NULL;
1121
1122 garray_T ga;
1123 ga_init2(&ga, sizeof(char *), 10);
1124
1125 size_t pat_len = (int)STRLEN(pat);
1126 char *dirnames[] = {"", NULL};
1127 ExpandRTDir_int(pat, pat_len, runtime_expand_flags, TRUE, &ga, dirnames);
1128
1129 // Try to complete values for [where] argument when none was found.
1130 if (runtime_expand_flags == 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001131 {
zeertzjq5c8771b2023-01-24 12:34:03 +00001132 char *where_values[] = {"START", "OPT", "PACK", "ALL"};
1133 for (size_t i = 0; i < ARRAY_LENGTH(where_values); ++i)
1134 if (STRNCMP(pat, where_values[i], pat_len) == 0)
1135 {
1136 char_u *p = vim_strsave((char_u *)where_values[i]);
1137 if (p != NULL && ga_add_string(&ga, p) == FAIL)
1138 vim_free(p);
1139 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001140 }
1141
1142 if (ga.ga_len == 0)
1143 return FAIL;
1144
zeertzjq5c8771b2023-01-24 12:34:03 +00001145 *matches = ga.ga_data;
1146 *numMatches = ga.ga_len;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001147 return OK;
1148}
1149
1150/*
1151 * Expand loadplugin names:
1152 * 'packpath'/pack/ * /opt/{pat}
1153 */
1154 int
1155ExpandPackAddDir(
1156 char_u *pat,
1157 int *num_file,
1158 char_u ***file)
1159{
1160 char_u *s;
1161 char_u *e;
1162 char_u *match;
1163 garray_T ga;
1164 int i;
1165 int pat_len;
1166
1167 *num_file = 0;
1168 *file = NULL;
1169 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001170 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001171
1172 s = alloc(pat_len + 26);
1173 if (s == NULL)
1174 {
1175 ga_clear_strings(&ga);
1176 return FAIL;
1177 }
1178 sprintf((char *)s, "pack/*/opt/%s*", pat);
zeertzjq3770f4c2023-01-22 18:38:51 +00001179 globpath(p_pp, s, &ga, 0, TRUE);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001180 vim_free(s);
1181
1182 for (i = 0; i < ga.ga_len; ++i)
1183 {
1184 match = ((char_u **)ga.ga_data)[i];
1185 s = gettail(match);
1186 e = s + STRLEN(s);
1187 mch_memmove(match, s, e - s + 1);
1188 }
1189
1190 if (ga.ga_len == 0)
1191 return FAIL;
1192
1193 // Sort and remove duplicates which can happen when specifying multiple
1194 // directories in dirnames.
1195 remove_duplicates(&ga);
1196
1197 *file = ga.ga_data;
1198 *num_file = ga.ga_len;
1199 return OK;
1200}
1201
1202 static void
1203cmd_source(char_u *fname, exarg_T *eap)
1204{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001205 int clearvars = FALSE;
1206
1207 if (*fname != NUL && STRNCMP(fname, "++clear", 7) == 0)
1208 {
1209 // ++clear argument is supplied
1210 clearvars = TRUE;
1211 fname = fname + 7;
1212 if (*fname != NUL)
1213 {
1214 semsg(_(e_invalid_argument_str), eap->arg);
1215 return;
1216 }
1217 }
1218
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001219 if (*fname != NUL && eap != NULL && eap->addr_count > 0)
1220 {
1221 // if a filename is specified to :source, then a range is not allowed
1222 emsg(_(e_no_range_allowed));
1223 return;
1224 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001225
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001226 if (eap != NULL && *fname == NUL)
1227 {
1228 if (eap->forceit)
1229 // a file name is needed to source normal mode commands
1230 emsg(_(e_argument_required));
1231 else
1232 // source ex commands from the current buffer
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001233 do_source_ext(NULL, FALSE, FALSE, NULL, eap, clearvars);
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001234 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001235 else if (eap != NULL && eap->forceit)
1236 // ":source!": read Normal mode commands
1237 // Need to execute the commands directly. This is required at least
1238 // for:
1239 // - ":g" command busy
1240 // - after ":argdo", ":windo" or ":bufdo"
1241 // - another command follows
1242 // - inside a loop
1243 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
1244#ifdef FEAT_EVAL
1245 || eap->cstack->cs_idx >= 0
1246#endif
1247 );
1248
1249 // ":source" read ex commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001250 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001251 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001252}
1253
1254/*
1255 * ":source {fname}"
1256 */
1257 void
1258ex_source(exarg_T *eap)
1259{
1260#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02001261 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001262 {
1263 char_u *fname = NULL;
1264
1265 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
1266 NULL, NULL,
1267 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1268 if (fname != NULL)
1269 {
1270 cmd_source(fname, eap);
1271 vim_free(fname);
1272 }
1273 }
1274 else
1275#endif
1276 cmd_source(eap->arg, eap);
1277}
1278
1279#if defined(FEAT_EVAL) || defined(PROTO)
1280/*
1281 * ":options"
1282 */
1283 void
1284ex_options(
1285 exarg_T *eap UNUSED)
1286{
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001287 char_u buf[500];
1288 int multi_mods = 0;
1289
1290 buf[0] = NUL;
dundargocc57b5bc2022-11-02 13:30:51 +00001291 (void)add_win_cmd_modifiers(buf, &cmdmod, &multi_mods);
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001292
1293 vim_setenv((char_u *)"OPTWIN_CMD", buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001294 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
1295}
1296#endif
1297
1298/*
1299 * ":source" and associated commands.
1300 */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001301
1302#ifdef FEAT_EVAL
1303/*
1304 * Return the address holding the next breakpoint line for a source cookie.
1305 */
1306 linenr_T *
1307source_breakpoint(void *cookie)
1308{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001309 return &((source_cookie_T *)cookie)->breakpoint;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001310}
1311
1312/*
1313 * Return the address holding the debug tick for a source cookie.
1314 */
1315 int *
1316source_dbg_tick(void *cookie)
1317{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001318 return &((source_cookie_T *)cookie)->dbg_tick;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001319}
1320
1321/*
1322 * Return the nesting level for a source cookie.
1323 */
1324 int
1325source_level(void *cookie)
1326{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001327 return ((source_cookie_T *)cookie)->level;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001328}
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001329
1330/*
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02001331 * Return the readahead line. Note that the pointer may become invalid when
1332 * getting the next line, if it's concatenated with the next one.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001333 */
1334 char_u *
1335source_nextline(void *cookie)
1336{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001337 return ((source_cookie_T *)cookie)->nextline;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001338}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001339#endif
1340
1341#if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC)
1342# define USE_FOPEN_NOINH
1343/*
1344 * Special function to open a file without handle inheritance.
1345 * When possible the handle is closed on exec().
1346 */
1347 static FILE *
1348fopen_noinh_readbin(char *filename)
1349{
1350# ifdef MSWIN
1351 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
1352# else
1353 int fd_tmp = mch_open(filename, O_RDONLY, 0);
1354# endif
1355
1356 if (fd_tmp == -1)
1357 return NULL;
1358
1359# ifdef HAVE_FD_CLOEXEC
1360 {
1361 int fdflags = fcntl(fd_tmp, F_GETFD);
1362 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1363 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
1364 }
1365# endif
1366
1367 return fdopen(fd_tmp, READBIN);
1368}
1369#endif
1370
1371/*
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001372 * Initialization for sourcing lines from the current buffer. Reads all the
1373 * lines from the buffer and stores it in the cookie grow array.
1374 * Returns a pointer to the name ":source buffer=<n>" on success and NULL on
1375 * failure.
1376 */
1377 static char_u *
1378do_source_buffer_init(source_cookie_T *sp, exarg_T *eap)
1379{
1380 linenr_T curr_lnum;
1381 char_u *line = NULL;
1382 char_u *fname;
1383
1384 CLEAR_FIELD(*sp);
1385
1386 if (curbuf == NULL)
1387 return NULL;
1388
1389 // Use ":source buffer=<num>" as the script name
1390 vim_snprintf((char *)IObuff, IOSIZE, ":source buffer=%d", curbuf->b_fnum);
1391 fname = vim_strsave(IObuff);
1392 if (fname == NULL)
1393 return NULL;
1394
1395 ga_init2(&sp->buflines, sizeof(char_u *), 100);
1396
1397 // Copy the lines from the buffer into a grow array
1398 for (curr_lnum = eap->line1; curr_lnum <= eap->line2; curr_lnum++)
1399 {
1400 line = vim_strsave(ml_get(curr_lnum));
1401 if (line == NULL)
1402 goto errret;
1403 if (ga_add_string(&sp->buflines, line) == FAIL)
1404 goto errret;
1405 line = NULL;
1406 }
1407 sp->buf_lnum = 0;
1408 sp->source_from_buf = TRUE;
1409
1410 return fname;
1411
1412errret:
1413 vim_free(fname);
1414 vim_free(line);
1415 ga_clear_strings(&sp->buflines);
1416 return NULL;
1417}
1418
1419/*
1420 * Read the file "fname" and execute its lines as EX commands.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1422 * again.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001423 *
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001424 * The "eap" argument is used when sourcing lines from a buffer instead of a
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001425 * file.
1426 *
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001427 * If "clearvars" is TRUE, then for scripts which are loaded more than
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001428 * once, clear all the functions and variables previously defined in that
1429 * script.
1430 *
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001431 * This function may be called recursively!
1432 *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433 * Return FAIL if file could not be opened, OK otherwise.
1434 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001435 */
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001436 static int
1437do_source_ext(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001438 char_u *fname,
1439 int check_other, // check for .vimrc and _vimrc
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001440 int is_vimrc, // DOSO_ value
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001441 int *ret_sid UNUSED,
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001442 exarg_T *eap,
1443 int clearvars UNUSED)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001444{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001445 source_cookie_T cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001446 char_u *p;
Bram Moolenaar753885b2022-08-24 16:30:36 +01001447 char_u *fname_not_fixed = NULL;
Bram Moolenaar5214b292022-08-24 17:32:35 +01001448 char_u *fname_exp = NULL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001449 char_u *firstline = NULL;
1450 int retval = FAIL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001451 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001452#ifdef FEAT_EVAL
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001453 funccal_entry_T funccalp_entry;
1454 int save_debug_break_level = debug_break_level;
Bram Moolenaar5214b292022-08-24 17:32:35 +01001455 int sid = -1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001456 scriptitem_T *si = NULL;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001457 int save_estack_compiling = estack_compiling;
Bram Moolenaar88774872022-08-16 20:24:29 +01001458 ESTACK_CHECK_DECLARATION
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001459#endif
1460#ifdef STARTUPTIME
1461 struct timeval tv_rel;
1462 struct timeval tv_start;
1463#endif
1464#ifdef FEAT_PROFILE
1465 proftime_T wait_start;
1466#endif
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001467 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001468 int trigger_source_post = FALSE;
1469
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001470 CLEAR_FIELD(cookie);
1471 if (fname == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001472 {
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001473 // sourcing lines from a buffer
1474 fname_exp = do_source_buffer_init(&cookie, eap);
1475 if (fname_exp == NULL)
1476 return FAIL;
1477 }
1478 else
1479 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01001480 fname_not_fixed = expand_env_save(fname);
1481 if (fname_not_fixed == NULL)
1482 goto theend;
1483 fname_exp = fix_fname(fname_not_fixed);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001484 if (fname_exp == NULL)
Bram Moolenaar753885b2022-08-24 16:30:36 +01001485 goto theend;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001486 if (mch_isdir(fname_exp))
1487 {
1488 smsg(_("Cannot source a directory: \"%s\""), fname);
1489 goto theend;
1490 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001491 }
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001492#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001493 estack_compiling = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495 // See if we loaded this script before.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001496 sid = find_script_by_name(fname_exp);
Bram Moolenaarf479cac2022-01-12 12:54:55 +00001497 if (sid > 0 && ret_sid != NULL
1498 && SCRIPT_ITEM(sid)->sn_state != SN_STATE_NOT_LOADED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499 {
1500 // Already loaded and no need to load again, return here.
1501 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001502 retval = OK;
1503 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 }
1505#endif
1506
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001507 // Apply SourceCmd autocommands, they should get the file and source it.
1508 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1509 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1510 FALSE, curbuf))
1511 {
1512#ifdef FEAT_EVAL
1513 retval = aborting() ? FAIL : OK;
1514#else
1515 retval = OK;
1516#endif
1517 if (retval == OK)
1518 // Apply SourcePost autocommands.
1519 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1520 FALSE, curbuf);
1521 goto theend;
1522 }
1523
1524 // Apply SourcePre autocommands, they may get the file.
1525 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1526
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001527 if (!cookie.source_from_buf)
1528 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001529#ifdef USE_FOPEN_NOINH
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001530 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001531#else
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001532 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001533#endif
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001534 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001535 if (cookie.fp == NULL && check_other)
1536 {
1537 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1538 // and ".exrc" by "_exrc" or vice versa.
1539 p = gettail(fname_exp);
1540 if ((*p == '.' || *p == '_')
1541 && (STRICMP(p + 1, "vimrc") == 0
1542 || STRICMP(p + 1, "gvimrc") == 0
1543 || STRICMP(p + 1, "exrc") == 0))
1544 {
1545 if (*p == '_')
1546 *p = '.';
1547 else
1548 *p = '_';
1549#ifdef USE_FOPEN_NOINH
1550 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1551#else
1552 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1553#endif
1554 }
1555 }
1556
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001557 if (cookie.fp == NULL && !cookie.source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001558 {
1559 if (p_verbose > 0)
1560 {
1561 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001562 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001563 smsg(_("could not source \"%s\""), fname);
1564 else
1565 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001566 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001567 verbose_leave();
1568 }
1569 goto theend;
1570 }
1571
1572 // The file exists.
1573 // - In verbose mode, give a message.
1574 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1575 if (p_verbose > 1)
1576 {
1577 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001578 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001579 smsg(_("sourcing \"%s\""), fname);
1580 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001581 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001582 verbose_leave();
1583 }
1584 if (is_vimrc == DOSO_VIMRC)
1585 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1586 else if (is_vimrc == DOSO_GVIMRC)
1587 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1588
1589#ifdef USE_CRNL
1590 // If no automatic file format: Set default to CR-NL.
1591 if (*p_ffs == NUL)
1592 cookie.fileformat = EOL_DOS;
1593 else
1594 cookie.fileformat = EOL_UNKNOWN;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001595#endif
1596
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001597 if (fname == NULL)
1598 // When sourcing a range of lines from a buffer, use the buffer line
1599 // number.
1600 cookie.sourcing_lnum = eap->line1 - 1;
1601 else
1602 cookie.sourcing_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001603
1604#ifdef FEAT_EVAL
1605 // Check if this script has a breakpoint.
1606 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1607 cookie.fname = fname_exp;
1608 cookie.dbg_tick = debug_tick;
1609
1610 cookie.level = ex_nesting_level;
1611#endif
1612
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001613#ifdef STARTUPTIME
1614 if (time_fd != NULL)
1615 time_push(&tv_rel, &tv_start);
1616#endif
1617
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001618 // "legacy" does not apply to commands in the script
1619 sticky_cmdmod_flags = 0;
1620
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001621 save_current_sctx = current_sctx;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001622 if (cmdmod.cmod_flags & CMOD_VIM9CMD)
1623 // When the ":vim9cmd" command modifier is used, source the script as a
1624 // Vim9 script.
1625 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1626 else
1627 current_sctx.sc_version = 1; // default script version
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001628
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001629#ifdef FEAT_EVAL
Bram Moolenaara2942c72023-01-02 13:41:49 +00001630 current_sctx.sc_lnum = 0;
1631
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001632# ifdef FEAT_PROFILE
1633 if (do_profiling == PROF_YES)
1634 prof_child_enter(&wait_start); // entering a child now
1635# endif
1636
1637 // Don't use local function variables, if called from a function.
1638 // Also starts profiling timer for nested script.
1639 save_funccal(&funccalp_entry);
1640
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001641 // Reset "KeyTyped" to avoid some commands thinking they are invoked
1642 // interactively. E.g. defining a function would output indent.
1643 int save_KeyTyped = KeyTyped;
1644 KeyTyped = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001645
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001646 // Check if this script was sourced before to find its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001647 // Always use a new sequence number.
1648 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001650 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001651 hashtab_T *ht;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001652 int todo;
1653 hashitem_T *hi;
1654 dictitem_T *di;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655
1656 // loading the same script again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001658 si = SCRIPT_ITEM(sid);
1659 if (si->sn_state == SN_STATE_NOT_LOADED)
1660 {
1661 // this script was found but not loaded yet
1662 si->sn_state = SN_STATE_NEW;
1663 }
1664 else
1665 {
1666 si->sn_state = SN_STATE_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001668 if (!clearvars)
1669 {
1670 // Script-local variables remain but "const" can be set again.
1671 // In Vim9 script variables will be cleared when "vim9script"
1672 // is encountered without the "noclear" argument.
1673 ht = &SCRIPT_VARS(sid);
1674 todo = (int)ht->ht_used;
1675 for (hi = ht->ht_array; todo > 0; ++hi)
1676 if (!HASHITEM_EMPTY(hi))
1677 {
1678 --todo;
1679 di = HI2DI(hi);
1680 di->di_flags |= DI_FLAGS_RELOAD;
1681 }
1682 // imports can be redefined once
1683 mark_imports_for_reload(sid);
1684 }
1685 else
1686 clear_vim9_scriptlocal_vars(sid);
Bram Moolenaar0123cc12021-02-07 17:17:58 +01001687
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001688 // reset version, "vim9script" may have been added or removed.
1689 si->sn_version = 1;
1690 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001691 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001693 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001694 int error = OK;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001695
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001696 // It's new, generate a new SID and initialize the scriptitem.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001697 sid = get_new_scriptitem(&error);
1698 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001699 if (error == FAIL)
1700 goto almosttheend;
Bram Moolenaar753885b2022-08-24 16:30:36 +01001701 si = SCRIPT_ITEM(sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001702 si->sn_name = fname_exp;
1703 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 if (ret_sid != NULL)
Bram Moolenaar753885b2022-08-24 16:30:36 +01001705 *ret_sid = sid;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001706
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001707 // Remember the "is_vimrc" flag for when the file is sourced again.
1708 si->sn_is_vimrc = is_vimrc;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001709 }
1710
zeertzjq5a4fff42022-08-15 17:53:55 +01001711 // Keep the sourcing name/lnum, for recursive calls.
1712 estack_push(ETYPE_SCRIPT, si->sn_name, 0);
1713 ESTACK_CHECK_SETUP
1714
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001715# ifdef FEAT_PROFILE
1716 if (do_profiling == PROF_YES)
1717 {
1718 int forceit;
1719
1720 // Check if we do profiling for this script.
1721 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
1722 {
1723 script_do_profile(si);
1724 si->sn_pr_force = forceit;
1725 }
1726 if (si->sn_prof_on)
1727 {
1728 ++si->sn_pr_count;
1729 profile_start(&si->sn_pr_start);
1730 profile_zero(&si->sn_pr_children);
1731 }
1732 }
1733# endif
Bram Moolenaaraeef1f72022-09-15 12:20:18 +01001734#else
1735 // Keep the sourcing name/lnum, for recursive calls.
1736 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001737#endif
1738
1739 cookie.conv.vc_type = CONV_NONE; // no conversion
1740
1741 // Read the first line so we can check for a UTF-8 BOM.
1742 firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1743 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1744 && firstline[1] == 0xbb && firstline[2] == 0xbf)
1745 {
1746 // Found BOM; setup conversion, skip over BOM and recode the line.
1747 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1748 p = string_convert(&cookie.conv, firstline + 3, NULL);
1749 if (p == NULL)
1750 p = vim_strsave(firstline + 3);
1751 if (p != NULL)
1752 {
1753 vim_free(firstline);
1754 firstline = p;
1755 }
1756 }
1757
1758 // Call do_cmdline, which will call getsourceline() to get the lines.
1759 do_cmdline(firstline, getsourceline, (void *)&cookie,
1760 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1761 retval = OK;
1762
1763#ifdef FEAT_PROFILE
1764 if (do_profiling == PROF_YES)
1765 {
1766 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001767 si = SCRIPT_ITEM(sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001768 if (si->sn_prof_on)
1769 {
1770 profile_end(&si->sn_pr_start);
1771 profile_sub_wait(&wait_start, &si->sn_pr_start);
1772 profile_add(&si->sn_pr_total, &si->sn_pr_start);
1773 profile_self(&si->sn_pr_self, &si->sn_pr_start,
1774 &si->sn_pr_children);
1775 }
1776 }
1777#endif
1778
1779 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001780 emsg(_(e_interrupted));
Bram Moolenaar88774872022-08-16 20:24:29 +01001781#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +01001782 ESTACK_CHECK_NOW
Bram Moolenaar88774872022-08-16 20:24:29 +01001783#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001784 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001785 if (p_verbose > 1)
1786 {
1787 verbose_enter();
1788 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001789 if (SOURCING_NAME != NULL)
1790 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001791 verbose_leave();
1792 }
1793#ifdef STARTUPTIME
1794 if (time_fd != NULL)
1795 {
1796 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1797 time_msg((char *)IObuff, &tv_start);
1798 time_pop(&tv_rel);
1799 }
1800#endif
1801
1802 if (!got_int)
1803 trigger_source_post = TRUE;
1804
1805#ifdef FEAT_EVAL
1806 // After a "finish" in debug mode, need to break at first command of next
1807 // sourced file.
1808 if (save_debug_break_level > ex_nesting_level
1809 && debug_break_level == ex_nesting_level)
1810 ++debug_break_level;
1811#endif
1812
1813#ifdef FEAT_EVAL
1814almosttheend:
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001815 // If "sn_save_cpo" is set that means we encountered "vim9script": restore
1816 // 'cpoptions', unless in the main .vimrc file.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001818 si = SCRIPT_ITEM(sid);
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001819 if (si->sn_save_cpo != NULL && si->sn_is_vimrc == DOSO_NONE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 {
Bram Moolenaar3e191692021-03-17 17:46:00 +01001821 if (STRCMP(p_cpo, CPO_VIM) != 0)
1822 {
1823 char_u *f;
1824 char_u *t;
1825
1826 // 'cpo' was changed in the script. Apply the same change to the
1827 // saved value, if possible.
1828 for (f = (char_u *)CPO_VIM; *f != NUL; ++f)
1829 if (vim_strchr(p_cpo, *f) == NULL
1830 && (t = vim_strchr(si->sn_save_cpo, *f)) != NULL)
1831 // flag was removed, also remove it from the saved 'cpo'
1832 mch_memmove(t, t + 1, STRLEN(t));
1833 for (f = p_cpo; *f != NUL; ++f)
1834 if (vim_strchr((char_u *)CPO_VIM, *f) == NULL
1835 && vim_strchr(si->sn_save_cpo, *f) == NULL)
1836 {
1837 // flag was added, also add it to the saved 'cpo'
1838 t = alloc(STRLEN(si->sn_save_cpo) + 2);
1839 if (t != NULL)
1840 {
1841 *t = *f;
1842 STRCPY(t + 1, si->sn_save_cpo);
1843 vim_free(si->sn_save_cpo);
1844 si->sn_save_cpo = t;
1845 }
1846 }
1847 }
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001848 set_option_value_give_err((char_u *)"cpo",
1849 0L, si->sn_save_cpo, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 }
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001851 VIM_CLEAR(si->sn_save_cpo);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001853 restore_funccal();
1854# ifdef FEAT_PROFILE
1855 if (do_profiling == PROF_YES)
1856 prof_child_exit(&wait_start); // leaving a child now
1857# endif
Bram Moolenaara2942c72023-01-02 13:41:49 +00001858
1859 KeyTyped = save_KeyTyped;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001860#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001861 current_sctx = save_current_sctx;
1862
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001863 if (cookie.fp != NULL)
1864 fclose(cookie.fp);
1865 if (cookie.source_from_buf)
1866 ga_clear_strings(&cookie.buflines);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001867 vim_free(cookie.nextline);
1868 vim_free(firstline);
1869 convert_setup(&cookie.conv, NULL, NULL);
1870
1871 if (trigger_source_post)
1872 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1873
1874theend:
Bram Moolenaar0af2ecf2022-08-24 17:05:56 +01001875#ifdef FEAT_EVAL
Bram Moolenaar753885b2022-08-24 16:30:36 +01001876 if (sid > 0 && ret_sid != NULL
1877 && fname_not_fixed != NULL && fname_exp != NULL)
1878 {
1879 int not_fixed_sid = find_script_by_name(fname_not_fixed);
1880
1881 // If "fname_not_fixed" is a symlink then we source the linked file.
1882 // If the original name is in the script list we add the ID of the
1883 // script that was actually sourced.
1884 if (SCRIPT_ID_VALID(not_fixed_sid) && not_fixed_sid != sid)
1885 SCRIPT_ITEM(not_fixed_sid)->sn_sourced_sid = sid;
1886 }
Bram Moolenaar0af2ecf2022-08-24 17:05:56 +01001887#endif
Bram Moolenaar753885b2022-08-24 16:30:36 +01001888
1889 vim_free(fname_not_fixed);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001890 vim_free(fname_exp);
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001891 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001892#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001893 estack_compiling = save_estack_compiling;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001894#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001895 return retval;
1896}
1897
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001898 int
1899do_source(
1900 char_u *fname,
1901 int check_other, // check for .vimrc and _vimrc
1902 int is_vimrc, // DOSO_ value
Bram Moolenaar753885b2022-08-24 16:30:36 +01001903 int *ret_sid)
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001904{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001905 return do_source_ext(fname, check_other, is_vimrc, ret_sid, NULL, FALSE);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001906}
1907
1908
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001909#if defined(FEAT_EVAL) || defined(PROTO)
1910
1911/*
1912 * ":scriptnames"
1913 */
1914 void
1915ex_scriptnames(exarg_T *eap)
1916{
1917 int i;
1918
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001919 if (eap->addr_count > 0 || *eap->arg != NUL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001920 {
1921 // :script {scriptId}: edit the script
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001922 if (eap->addr_count > 0 && !SCRIPT_ID_VALID(eap->line2))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001923 emsg(_(e_invalid_argument));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001924 else
1925 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001926 if (eap->addr_count > 0)
1927 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
1928 else
1929 {
1930 expand_env(eap->arg, NameBuff, MAXPATHL);
1931 eap->arg = NameBuff;
1932 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001933 do_exedit(eap, NULL);
1934 }
1935 return;
1936 }
1937
1938 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar6079da72022-01-18 14:16:59 +00001939 {
1940 scriptitem_T *si = SCRIPT_ITEM(i);
1941
1942 if (si->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001943 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01001944 char sourced_buf[20];
1945
Bram Moolenaar6079da72022-01-18 14:16:59 +00001946 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar753885b2022-08-24 16:30:36 +01001947 if (si->sn_sourced_sid > 0)
1948 vim_snprintf(sourced_buf, 20, "->%d", si->sn_sourced_sid);
1949 else
1950 sourced_buf[0] = NUL;
1951 vim_snprintf((char *)IObuff, IOSIZE, "%3d%s%s: %s",
Bram Moolenaar6079da72022-01-18 14:16:59 +00001952 i,
Bram Moolenaar753885b2022-08-24 16:30:36 +01001953 sourced_buf,
Bram Moolenaar6079da72022-01-18 14:16:59 +00001954 si->sn_state == SN_STATE_NOT_LOADED ? " A" : "",
1955 NameBuff);
Bram Moolenaar769f5892022-02-09 14:31:05 +00001956 if (!message_filtered(IObuff))
1957 {
1958 msg_putchar('\n');
1959 msg_outtrans(IObuff);
1960 out_flush(); // output one line at a time
1961 ui_breakcheck();
1962 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001963 }
Bram Moolenaar6079da72022-01-18 14:16:59 +00001964 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001965}
1966
1967# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1968/*
1969 * Fix slashes in the list of script names for 'shellslash'.
1970 */
1971 void
1972scriptnames_slash_adjust(void)
1973{
1974 int i;
1975
1976 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001977 if (SCRIPT_ITEM(i)->sn_name != NULL)
1978 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001979}
1980# endif
1981
1982/*
1983 * Get a pointer to a script name. Used for ":verbose set".
Bram Moolenaar74667062020-12-28 15:41:41 +01001984 * Message appended to "Last set from "
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001985 */
1986 char_u *
1987get_scriptname(scid_T id)
1988{
1989 if (id == SID_MODELINE)
1990 return (char_u *)_("modeline");
1991 if (id == SID_CMDARG)
1992 return (char_u *)_("--cmd argument");
1993 if (id == SID_CARG)
1994 return (char_u *)_("-c argument");
1995 if (id == SID_ENV)
1996 return (char_u *)_("environment variable");
1997 if (id == SID_ERROR)
1998 return (char_u *)_("error handler");
Bram Moolenaar74667062020-12-28 15:41:41 +01001999 if (id == SID_WINLAYOUT)
2000 return (char_u *)_("changed window size");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002001 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002002}
2003
2004# if defined(EXITFREE) || defined(PROTO)
2005 void
2006free_scriptnames(void)
2007{
2008 int i;
2009
2010 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02002011 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002012 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002014 // the variables themselves are cleared in evalvars_clear()
2015 vim_free(si->sn_vars);
2016
2017 vim_free(si->sn_name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02002018 free_imports_and_script_vars(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002019 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02002020# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002021 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02002022# endif
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002023 vim_free(si->sn_autoload_prefix);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002024 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02002025 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002026 ga_clear(&script_items);
2027}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002028
2029 void
2030free_autoload_scriptnames(void)
2031{
2032 ga_clear_strings(&ga_loaded);
2033}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002034# endif
2035
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002036 linenr_T
Bram Moolenaar66250c92020-08-20 15:02:42 +02002037get_sourced_lnum(
2038 char_u *(*fgetline)(int, void *, int, getline_opt_T),
2039 void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002040{
2041 return fgetline == getsourceline
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002042 ? ((source_cookie_T *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002043 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002044}
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002045
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002046/*
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002047 * Return a List of script-local functions defined in the script with id
2048 * 'sid'.
2049 */
2050 static list_T *
2051get_script_local_funcs(scid_T sid)
2052{
2053 hashtab_T *functbl;
2054 hashitem_T *hi;
2055 long_u todo;
2056 list_T *l;
2057
2058 l = list_alloc();
2059 if (l == NULL)
2060 return NULL;
2061
2062 // Iterate through all the functions in the global function hash table
2063 // looking for functions with script ID 'sid'.
2064 functbl = func_tbl_get();
2065 todo = functbl->ht_used;
2066 for (hi = functbl->ht_array; todo > 0; ++hi)
2067 {
2068 ufunc_T *fp;
2069
2070 if (HASHITEM_EMPTY(hi))
2071 continue;
2072
2073 --todo;
2074 fp = HI2UF(hi);
2075
2076 // Add active functions with script id == 'sid'
2077 if (!(fp->uf_flags & FC_DEAD) && (fp->uf_script_ctx.sc_sid == sid))
2078 {
2079 char_u *name;
2080
2081 if (fp->uf_name_exp != NULL)
2082 name = fp->uf_name_exp;
2083 else
2084 name = fp->uf_name;
2085
2086 list_append_string(l, name, -1);
2087 }
2088 }
2089
2090 return l;
2091}
2092
2093/*
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002094 * getscriptinfo() function
2095 */
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002096 void
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002097f_getscriptinfo(typval_T *argvars, typval_T *rettv)
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002098{
2099 int i;
2100 list_T *l;
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002101 char_u *pat = NULL;
2102 regmatch_T regmatch;
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002103 int filterpat = FALSE;
2104 scid_T sid = -1;
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002105
2106 if (rettv_list_alloc(rettv) == FAIL)
2107 return;
2108
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002109 if (check_for_opt_dict_arg(argvars, 0) == FAIL)
2110 return;
2111
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002112 l = rettv->vval.v_list;
2113
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002114 regmatch.regprog = NULL;
2115 regmatch.rm_ic = p_ic;
2116
2117 if (argvars[0].v_type == VAR_DICT)
2118 {
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002119 sid = dict_get_number_def(argvars[0].vval.v_dict, "sid", -1);
2120 if (sid == -1)
2121 {
2122 pat = dict_get_string(argvars[0].vval.v_dict, "name", TRUE);
2123 if (pat != NULL)
2124 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2125 if (regmatch.regprog != NULL)
2126 filterpat = TRUE;
2127 }
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002128 }
2129
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002130 for (i = 1; i <= script_items.ga_len; ++i)
2131 {
2132 scriptitem_T *si = SCRIPT_ITEM(i);
2133 dict_T *d;
2134
2135 if (si->sn_name == NULL)
2136 continue;
2137
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002138 if (filterpat && !vim_regexec(&regmatch, si->sn_name, (colnr_T)0))
2139 continue;
2140
2141 if (sid != -1 && sid != i)
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002142 continue;
2143
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002144 if ((d = dict_alloc()) == NULL
2145 || list_append_dict(l, d) == FAIL
2146 || dict_add_string(d, "name", si->sn_name) == FAIL
2147 || dict_add_number(d, "sid", i) == FAIL
Bram Moolenaar753885b2022-08-24 16:30:36 +01002148 || dict_add_number(d, "sourced", si->sn_sourced_sid) == FAIL
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002149 || dict_add_number(d, "version", si->sn_version) == FAIL
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002150 || dict_add_bool(d, "autoload",
2151 si->sn_state == SN_STATE_NOT_LOADED) == FAIL)
2152 return;
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002153
2154 // When a filter pattern is specified to return information about only
2155 // specific script(s), also add the script-local variables and
2156 // functions.
2157 if (sid != -1)
2158 {
2159 dict_T *var_dict;
2160
2161 var_dict = dict_copy(&si->sn_vars->sv_dict, TRUE, TRUE,
2162 get_copyID());
2163 if (var_dict == NULL
2164 || dict_add_dict(d, "variables", var_dict) == FAIL
2165 || dict_add_list(d, "functions",
2166 get_script_local_funcs(sid)) == FAIL)
2167 return;
2168 }
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002169 }
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002170
2171 vim_regfree(regmatch.regprog);
2172 vim_free(pat);
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002173}
2174
Dominique Pelle748b3082022-01-08 12:41:16 +00002175#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002176
2177 static char_u *
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002178get_one_sourceline(source_cookie_T *sp)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002179{
2180 garray_T ga;
2181 int len;
2182 int c;
2183 char_u *buf;
2184#ifdef USE_CRNL
2185 int has_cr; // CR-LF found
2186#endif
2187 int have_read = FALSE;
2188
2189 // use a growarray to store the sourced line
2190 ga_init2(&ga, 1, 250);
2191
2192 // Loop until there is a finished line (or end-of-file).
2193 ++sp->sourcing_lnum;
2194 for (;;)
2195 {
2196 // make room to read at least 120 (more) characters
2197 if (ga_grow(&ga, 120) == FAIL)
2198 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002199 if (sp->source_from_buf)
2200 {
2201 if (sp->buf_lnum >= sp->buflines.ga_len)
2202 break; // all the lines are processed
2203 ga_concat(&ga, ((char_u **)sp->buflines.ga_data)[sp->buf_lnum]);
2204 sp->buf_lnum++;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01002205 if (ga_grow(&ga, 1) == FAIL)
2206 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002207 buf = (char_u *)ga.ga_data;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01002208 buf[ga.ga_len++] = NUL;
Bram Moolenaar4748c4b2022-05-17 17:47:07 +01002209 len = ga.ga_len;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002210 }
2211 else
2212 {
2213 buf = (char_u *)ga.ga_data;
2214 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
2215 sp->fp) == NULL)
2216 break;
Bram Moolenaar4748c4b2022-05-17 17:47:07 +01002217 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002218 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002219#ifdef USE_CRNL
2220 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
2221 // CTRL-Z by its own, or after a NL.
2222 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
2223 && sp->fileformat == EOL_DOS
2224 && buf[len - 1] == Ctrl_Z)
2225 {
2226 buf[len - 1] = NUL;
2227 break;
2228 }
2229#endif
2230
2231 have_read = TRUE;
2232 ga.ga_len = len;
2233
2234 // If the line was longer than the buffer, read more.
2235 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
2236 continue;
2237
2238 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
2239 {
2240#ifdef USE_CRNL
2241 has_cr = (len >= 2 && buf[len - 2] == '\r');
2242 if (sp->fileformat == EOL_UNKNOWN)
2243 {
2244 if (has_cr)
2245 sp->fileformat = EOL_DOS;
2246 else
2247 sp->fileformat = EOL_UNIX;
2248 }
2249
2250 if (sp->fileformat == EOL_DOS)
2251 {
2252 if (has_cr) // replace trailing CR
2253 {
2254 buf[len - 2] = '\n';
2255 --len;
2256 --ga.ga_len;
2257 }
2258 else // lines like ":map xx yy^M" will have failed
2259 {
2260 if (!sp->error)
2261 {
2262 msg_source(HL_ATTR(HLF_W));
2263 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
2264 }
2265 sp->error = TRUE;
2266 sp->fileformat = EOL_UNIX;
2267 }
2268 }
2269#endif
2270 // The '\n' is escaped if there is an odd number of ^V's just
2271 // before it, first set "c" just before the 'V's and then check
2272 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
2273 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
2274 ;
2275 if ((len & 1) != (c & 1)) // escaped NL, read more
2276 {
2277 ++sp->sourcing_lnum;
2278 continue;
2279 }
2280
2281 buf[len - 1] = NUL; // remove the NL
2282 }
2283
2284 // Check for ^C here now and then, so recursive :so can be broken.
2285 line_breakcheck();
2286 break;
2287 }
2288
2289 if (have_read)
2290 return (char_u *)ga.ga_data;
2291
2292 vim_free(ga.ga_data);
2293 return NULL;
2294}
2295
2296/*
2297 * Get one full line from a sourced file.
2298 * Called by do_cmdline() when it's called from do_source().
2299 *
2300 * Return a pointer to the line in allocated memory.
2301 * Return NULL for end-of-file or some error.
2302 */
2303 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02002304getsourceline(
2305 int c UNUSED,
2306 void *cookie,
2307 int indent UNUSED,
2308 getline_opt_T options)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002309{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002310 source_cookie_T *sp = (source_cookie_T *)cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002311 char_u *line;
2312 char_u *p;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002313 int do_vim9_all = in_vim9script()
2314 && options == GETLINE_CONCAT_ALL;
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002315 int do_bar_cont = do_vim9_all
2316 || options == GETLINE_CONCAT_CONTBAR;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002317
2318#ifdef FEAT_EVAL
2319 // If breakpoints have been added/deleted need to check for it.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002320 if ((sp->dbg_tick < debug_tick) && !sp->source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002321 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002322 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002323 sp->dbg_tick = debug_tick;
2324 }
2325# ifdef FEAT_PROFILE
2326 if (do_profiling == PROF_YES)
2327 script_line_end();
2328# endif
2329#endif
2330
2331 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002332 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002333
2334 // Get current line. If there is a read-ahead line, use it, otherwise get
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002335 // one now. "fp" is NULL if actually using a string.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002336 if (sp->finished || (!sp->source_from_buf && sp->fp == NULL))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002337 line = NULL;
2338 else if (sp->nextline == NULL)
2339 line = get_one_sourceline(sp);
2340 else
2341 {
2342 line = sp->nextline;
2343 sp->nextline = NULL;
2344 ++sp->sourcing_lnum;
2345 }
2346#ifdef FEAT_PROFILE
2347 if (line != NULL && do_profiling == PROF_YES)
2348 script_line_start();
2349#endif
2350
2351 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
2352 // contain the 'C' flag.
Bram Moolenaar66250c92020-08-20 15:02:42 +02002353 if (line != NULL && options != GETLINE_NONE
2354 && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002355 {
Bram Moolenaar5072b472021-06-03 21:56:10 +02002356 int comment_char = in_vim9script() ? '#' : '"';
2357
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002358 // compensate for the one line read-ahead
2359 --sp->sourcing_lnum;
2360
2361 // Get the next line and concatenate it when it starts with a
2362 // backslash. We always need to read the next line, keep it in
2363 // sp->nextline.
2364 /* Also check for a comment in between continuation lines: "\ */
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002365 // Also check for a Vim9 comment, empty line, line starting with '|',
2366 // but not "||".
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002367 sp->nextline = get_one_sourceline(sp);
2368 if (sp->nextline != NULL
2369 && (*(p = skipwhite(sp->nextline)) == '\\'
Bram Moolenaar5072b472021-06-03 21:56:10 +02002370 || (p[0] == comment_char
2371 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002372 || (do_vim9_all && (*p == NUL
2373 || vim9_comment_start(p)))
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002374 || (do_bar_cont && p[0] == '|' && p[1] != '|')))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002375 {
2376 garray_T ga;
2377
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002378 ga_init2(&ga, sizeof(char_u), 400);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002379 ga_concat(&ga, line);
2380 if (*p == '\\')
2381 ga_concat(&ga, p + 1);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002382 else if (*p == '|')
2383 {
2384 ga_concat(&ga, (char_u *)" ");
2385 ga_concat(&ga, p);
2386 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002387 for (;;)
2388 {
2389 vim_free(sp->nextline);
2390 sp->nextline = get_one_sourceline(sp);
2391 if (sp->nextline == NULL)
2392 break;
2393 p = skipwhite(sp->nextline);
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002394 if (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|'))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002395 {
2396 // Adjust the growsize to the current length to speed up
2397 // concatenating many lines.
2398 if (ga.ga_len > 400)
2399 {
2400 if (ga.ga_len > 8000)
2401 ga.ga_growsize = 8000;
2402 else
2403 ga.ga_growsize = ga.ga_len;
2404 }
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002405 if (*p == '\\')
2406 ga_concat(&ga, p + 1);
2407 else
2408 {
2409 ga_concat(&ga, (char_u *)" ");
2410 ga_concat(&ga, p);
2411 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002412 }
Bram Moolenaar5072b472021-06-03 21:56:10 +02002413 else if (!(p[0] == (comment_char)
Bram Moolenaar0f37e352021-06-02 15:28:15 +02002414 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002415 && !(do_vim9_all && (*p == NUL || vim9_comment_start(p))))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002416 break;
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002417 /* drop a # comment or "\ comment line */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002418 }
2419 ga_append(&ga, NUL);
2420 vim_free(line);
2421 line = ga.ga_data;
2422 }
2423 }
2424
2425 if (line != NULL && sp->conv.vc_type != CONV_NONE)
2426 {
2427 char_u *s;
2428
2429 // Convert the encoding of the script line.
2430 s = string_convert(&sp->conv, line, NULL);
2431 if (s != NULL)
2432 {
2433 vim_free(line);
2434 line = s;
2435 }
2436 }
2437
2438#ifdef FEAT_EVAL
2439 // Did we encounter a breakpoint?
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002440 if (!sp->source_from_buf && sp->breakpoint != 0
2441 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002442 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002443 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002444 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002445 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002446 sp->dbg_tick = debug_tick;
2447 }
2448#endif
2449
2450 return line;
2451}
2452
2453/*
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002454 * Returns TRUE if sourcing a script either from a file or a buffer.
2455 * Otherwise returns FALSE.
2456 */
2457 int
2458sourcing_a_script(exarg_T *eap)
2459{
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002460 return (getline_equal(eap->getline, eap->cookie, getsourceline));
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002461}
2462
2463/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002464 * ":scriptencoding": Set encoding conversion for a sourced script.
2465 */
2466 void
2467ex_scriptencoding(exarg_T *eap)
2468{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002469 source_cookie_T *sp;
2470 char_u *name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002471
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002472 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002473 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00002474 emsg(_(e_scriptencoding_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002475 return;
2476 }
2477
2478 if (*eap->arg != NUL)
2479 {
2480 name = enc_canonize(eap->arg);
2481 if (name == NULL) // out of memory
2482 return;
2483 }
2484 else
2485 name = eap->arg;
2486
2487 // Setup for conversion from the specified encoding to 'encoding'.
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002488 sp = (source_cookie_T *)getline_cookie(eap->getline, eap->cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002489 convert_setup(&sp->conv, name, p_enc);
2490
2491 if (name != eap->arg)
2492 vim_free(name);
2493}
2494
2495/*
2496 * ":scriptversion": Set Vim script version for a sourced script.
2497 */
2498 void
2499ex_scriptversion(exarg_T *eap UNUSED)
2500{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002501 int nr;
2502
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002503 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002504 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002505 emsg(_(e_scriptversion_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002506 return;
2507 }
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002508 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002510 emsg(_(e_cannot_use_scriptversion_after_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 return;
2512 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002513
2514 nr = getdigits(&eap->arg);
2515 if (nr == 0 || *eap->arg != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002516 emsg(_(e_invalid_argument));
Bram Moolenaar67979662020-06-20 22:50:47 +02002517 else if (nr > SCRIPT_VERSION_MAX)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002518 semsg(_(e_scriptversion_not_supported_nr), nr);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002519 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002520 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002521 current_sctx.sc_version = nr;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002522#ifdef FEAT_EVAL
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002523 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002524#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002525 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002526}
2527
2528#if defined(FEAT_EVAL) || defined(PROTO)
2529/*
2530 * ":finish": Mark a sourced file as finished.
2531 */
2532 void
2533ex_finish(exarg_T *eap)
2534{
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002535 if (sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002536 do_finish(eap, FALSE);
2537 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00002538 emsg(_(e_finish_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002539}
2540
2541/*
2542 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
2543 * Also called for a pending finish at the ":endtry" or after returning from
2544 * an extra do_cmdline(). "reanimate" is used in the latter case.
2545 */
2546 void
2547do_finish(exarg_T *eap, int reanimate)
2548{
2549 int idx;
2550
2551 if (reanimate)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002552 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002553 eap->cookie))->finished = FALSE;
2554
2555 // Cleanup (and inactivate) conditionals, but stop when a try conditional
2556 // not in its finally clause (which then is to be executed next) is found.
2557 // In this case, make the ":finish" pending for execution at the ":endtry".
2558 // Otherwise, finish normally.
2559 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
2560 if (idx >= 0)
2561 {
2562 eap->cstack->cs_pending[idx] = CSTP_FINISH;
2563 report_make_pending(CSTP_FINISH, NULL);
2564 }
2565 else
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002566 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002567 eap->cookie))->finished = TRUE;
2568}
2569
2570
2571/*
2572 * Return TRUE when a sourced file had the ":finish" command: Don't give error
2573 * message for missing ":endif".
2574 * Return FALSE when not sourcing a file.
2575 */
2576 int
2577source_finished(
Bram Moolenaar66250c92020-08-20 15:02:42 +02002578 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002579 void *cookie)
2580{
2581 return (getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002582 && ((source_cookie_T *)getline_cookie(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002583 fgetline, cookie))->finished);
2584}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002585
2586/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002587 * Find the path of a script below the "autoload" directory.
2588 * Returns NULL if there is no "/autoload/" in the script name.
2589 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01002590 static char_u *
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002591script_name_after_autoload(scriptitem_T *si)
2592{
2593 char_u *p = si->sn_name;
2594 char_u *res = NULL;
2595
2596 for (;;)
2597 {
2598 char_u *n = (char_u *)strstr((char *)p, "autoload");
2599
2600 if (n == NULL)
2601 break;
2602 if (n > p && vim_ispathsep(n[-1]) && vim_ispathsep(n[8]))
2603 res = n + 9;
2604 p = n + 8;
2605 }
2606 return res;
2607}
2608
2609/*
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002610 * For an autoload script "autoload/dir/script.vim" return the prefix
2611 * "dir#script#" in allocated memory.
2612 * Returns NULL if anything is wrong.
2613 */
2614 char_u *
2615get_autoload_prefix(scriptitem_T *si)
2616{
2617 char_u *p = script_name_after_autoload(si);
2618 char_u *prefix;
2619
2620 if (p == NULL)
2621 return NULL;
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002622 prefix = vim_strsave(p);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002623 if (prefix == NULL)
2624 return NULL;
2625
2626 // replace all '/' with '#' and locate ".vim" at the end
2627 for (p = prefix; *p != NUL; p += mb_ptr2len(p))
2628 {
2629 if (vim_ispathsep(*p))
2630 *p = '#';
2631 else if (STRCMP(p, ".vim") == 0)
2632 {
2633 p[0] = '#';
2634 p[1] = NUL;
2635 return prefix;
2636 }
2637 }
2638
2639 // did not find ".vim" at the end
2640 vim_free(prefix);
2641 return NULL;
2642}
2643
2644/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002645 * If in a Vim9 autoload script return "name" with the autoload prefix for the
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002646 * script. If successful the returned name is allocated.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002647 * Otherwise it returns "name" unmodified.
2648 */
2649 char_u *
2650may_prefix_autoload(char_u *name)
2651{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002652 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2653 return name;
2654
2655 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2656
2657 if (si->sn_autoload_prefix == NULL)
2658 return name;
2659
2660 char_u *basename = name;
2661 size_t len;
2662 char_u *res;
2663
2664 if (*name == K_SPECIAL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002665 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002666 char_u *p = vim_strchr(name, '_');
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002667
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002668 // skip over "<SNR>99_"
2669 if (p != NULL)
2670 basename = p + 1;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002671 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002672
2673 len = STRLEN(si->sn_autoload_prefix) + STRLEN(basename) + 2;
2674 res = alloc(len);
2675 if (res == NULL)
2676 return NULL;
2677
2678 vim_snprintf((char *)res, len, "%s%s", si->sn_autoload_prefix, basename);
2679 return res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002680}
2681
2682/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002683 * Return the autoload script name for a function or variable name.
2684 * Returns NULL when out of memory.
2685 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
2686 */
2687 char_u *
2688autoload_name(char_u *name)
2689{
2690 char_u *p, *q = NULL;
2691 char_u *scriptname;
2692
2693 // Get the script file name: replace '#' with '/', append ".vim".
2694 scriptname = alloc(STRLEN(name) + 14);
2695 if (scriptname == NULL)
2696 return NULL;
2697 STRCPY(scriptname, "autoload/");
Bram Moolenaara1773442020-08-12 15:21:22 +02002698 STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002699 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
2700 q = p, ++p)
2701 *p = '/';
2702 STRCPY(q, ".vim");
2703 return scriptname;
2704}
2705
2706/*
2707 * If "name" has a package name try autoloading the script for it.
2708 * Return TRUE if a package was loaded.
2709 */
2710 int
2711script_autoload(
2712 char_u *name,
2713 int reload) // load script again when already loaded
2714{
2715 char_u *p;
2716 char_u *scriptname, *tofree;
2717 int ret = FALSE;
2718 int i;
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002719 int ret_sid;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002720
Bram Moolenaar89445512022-04-14 12:58:23 +01002721 // If the name starts with "<SNR>123_" then "123" is the script ID.
2722 if (name[0] == K_SPECIAL && name[1] == KS_EXTRA && name[2] == KE_SNR)
2723 {
2724 p = name + 3;
2725 ret_sid = (int)getdigits(&p);
2726 if (*p == '_' && SCRIPT_ID_VALID(ret_sid))
2727 {
2728 may_load_script(ret_sid, &ret);
2729 return ret;
2730 }
2731 }
2732
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002733 // If there is no '#' after name[0] there is no package name.
2734 p = vim_strchr(name, AUTOLOAD_CHAR);
2735 if (p == NULL || p == name)
2736 return FALSE;
2737
2738 tofree = scriptname = autoload_name(name);
2739 if (scriptname == NULL)
2740 return FALSE;
2741
2742 // Find the name in the list of previously loaded package names. Skip
2743 // "autoload/", it's always the same.
2744 for (i = 0; i < ga_loaded.ga_len; ++i)
2745 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2746 break;
2747 if (!reload && i < ga_loaded.ga_len)
2748 ret = FALSE; // was loaded already
2749 else
2750 {
2751 // Remember the name if it wasn't loaded already.
2752 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2753 {
2754 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2755 tofree = NULL;
2756 }
2757
2758 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002759 // Use "ret_sid" to avoid loading the same script again.
=?UTF-8?q?Bj=C3=B6rn=20Linse?=223a9502022-01-31 17:26:05 +00002760 if (source_in_path(p_rtp, scriptname, DIP_START, &ret_sid) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002761 ret = TRUE;
2762 }
2763
2764 vim_free(tofree);
2765 return ret;
2766}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002767#endif