blob: 2aca346d0fd99d502d457e5dc6d1f4aab54b97b7 [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;
zeertzjqbe5cdd12023-08-17 23:48:58 +0200292 // Skip to the last argument.
293 while (*(p = skiptowhite_esc(arg)) != NUL)
294 {
295 if (runtime_expand_flags == 0)
296 // When there are multiple arguments and [where] is not specified,
297 // use an unrelated non-zero flag to avoid expanding [where].
298 runtime_expand_flags = DIP_ALL;
299 arg = skipwhite(p);
300 }
zeertzjq3770f4c2023-01-22 18:38:51 +0000301 xp->xp_context = EXPAND_RUNTIME;
302 xp->xp_pattern = arg;
303}
304
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200305 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306source_callback(char_u *fname, void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200307{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200309}
310
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000311#ifdef FEAT_EVAL
312/*
313 * Find an already loaded script "name".
314 * If found returns its script ID. If not found returns -1.
315 */
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100316 int
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000317find_script_by_name(char_u *name)
318{
319 int sid;
320 scriptitem_T *si;
321
322 for (sid = script_items.ga_len; sid > 0; --sid)
323 {
324 // We used to check inode here, but that doesn't work:
325 // - If a script is edited and written, it may get a different
326 // inode number, even though to the user it is the same script.
327 // - If a script is deleted and another script is written, with a
328 // different name, the inode may be re-used.
329 si = SCRIPT_ITEM(sid);
330 if (si->sn_name != NULL && fnamecmp(si->sn_name, name) == 0)
331 return sid;
332 }
333 return -1;
334}
335
336/*
337 * Add a new scriptitem with all items initialized.
338 * When running out of memory "error" is set to FAIL.
339 * Returns the script ID.
340 */
341 static int
342get_new_scriptitem(int *error)
343{
344 static scid_T last_current_SID = 0;
345 int sid = ++last_current_SID;
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000346 scriptitem_T *si = NULL;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000347
348 if (ga_grow(&script_items, (int)(sid - script_items.ga_len)) == FAIL)
349 {
350 *error = FAIL;
351 return sid;
352 }
353 while (script_items.ga_len < sid)
354 {
355 si = ALLOC_CLEAR_ONE(scriptitem_T);
356 if (si == NULL)
357 {
358 *error = FAIL;
359 return sid;
360 }
361 ++script_items.ga_len;
362 SCRIPT_ITEM(script_items.ga_len) = si;
363 si->sn_name = NULL;
364 si->sn_version = 1;
365
366 // Allocate the local script variables to use for this script.
367 new_script_vars(script_items.ga_len);
368 ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
369 hash_init(&si->sn_all_vars.dv_hashtab);
370 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
371 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
372# ifdef FEAT_PROFILE
373 si->sn_prof_on = FALSE;
374# endif
375 }
376
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000377 // "si" can't be NULL, check only to avoid a compiler warning
378 if (si != NULL)
379 // Used to check script variable index is still valid.
380 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000381
382 return sid;
383}
384
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100385 int
386get_new_scriptitem_for_fname(int *error, char_u *fname)
387{
388 int sid = get_new_scriptitem(error);
389
390 if (*error == OK)
391 {
392 scriptitem_T *si = SCRIPT_ITEM(sid);
393
394 si->sn_name = vim_strsave(fname);
395 si->sn_state = SN_STATE_NOT_LOADED;
396 }
397 return sid;
398}
399
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000400 static void
401find_script_callback(char_u *fname, void *cookie)
402{
403 int sid;
404 int error = OK;
405 int *ret_sid = cookie;
406
407 sid = find_script_by_name(fname);
408 if (sid < 0)
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000409 // script does not exist yet, create a new scriptitem
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100410 sid = get_new_scriptitem_for_fname(&error, fname);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000411 *ret_sid = sid;
412}
413#endif
414
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200415/*
zeertzjq008c9152023-08-17 23:08:53 +0200416 * Find the patterns in "name" in all directories in "path" and invoke
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200417 * "callback(fname, cookie)".
zeertzjq008c9152023-08-17 23:08:53 +0200418 * "prefix" is prepended to each pattern in "name".
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200419 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
420 * When "flags" has DIP_DIR: find directories instead of files.
421 * When "flags" has DIP_ERR: give an error message if there is no match.
422 *
zeertzjq008c9152023-08-17 23:08:53 +0200423 * Return FAIL when no file could be sourced, OK otherwise.
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200424 */
425 int
426do_in_path(
427 char_u *path,
zeertzjq008c9152023-08-17 23:08:53 +0200428 char *prefix,
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200429 char_u *name,
430 int flags,
431 void (*callback)(char_u *fname, void *ck),
432 void *cookie)
433{
434 char_u *rtp;
435 char_u *np;
436 char_u *buf;
437 char_u *rtp_copy;
438 char_u *tail;
439 int num_files;
440 char_u **files;
441 int i;
442 int did_one = FALSE;
443#ifdef AMIGA
444 struct Process *proc = (struct Process *)FindTask(0L);
445 APTR save_winptr = proc->pr_WindowPtr;
446
447 // Avoid a requester here for a volume that doesn't exist.
448 proc->pr_WindowPtr = (APTR)-1L;
449#endif
450
451 // Make a copy of 'runtimepath'. Invoking the callback may change the
452 // value.
453 rtp_copy = vim_strsave(path);
454 buf = alloc(MAXPATHL);
455 if (buf != NULL && rtp_copy != NULL)
456 {
Bram Moolenaar647a5302020-05-03 17:01:24 +0200457 if (p_verbose > 10 && name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200458 {
459 verbose_enter();
zeertzjq008c9152023-08-17 23:08:53 +0200460 if (*prefix != NUL)
461 smsg(_("Searching for \"%s\" under \"%s\" in \"%s\""),
462 (char *)name, prefix, (char *)path);
463 else
464 smsg(_("Searching for \"%s\" in \"%s\""),
465 (char *)name, (char *)path);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200466 verbose_leave();
467 }
468
469 // Loop over all entries in 'runtimepath'.
470 rtp = rtp_copy;
471 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
472 {
473 size_t buflen;
474
475 // Copy the path from 'runtimepath' to buf[].
476 copy_option_part(&rtp, buf, MAXPATHL, ",");
477 buflen = STRLEN(buf);
478
479 // Skip after or non-after directories.
480 if (flags & (DIP_NOAFTER | DIP_AFTER))
481 {
482 int is_after = buflen >= 5
483 && STRCMP(buf + buflen - 5, "after") == 0;
484
485 if ((is_after && (flags & DIP_NOAFTER))
486 || (!is_after && (flags & DIP_AFTER)))
487 continue;
488 }
489
490 if (name == NULL)
491 {
492 (*callback)(buf, (void *) &cookie);
493 if (!did_one)
494 did_one = (cookie == NULL);
495 }
zeertzjq008c9152023-08-17 23:08:53 +0200496 else if (buflen + 2 + STRLEN(prefix) + STRLEN(name) < MAXPATHL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200497 {
498 add_pathsep(buf);
zeertzjq008c9152023-08-17 23:08:53 +0200499 STRCAT(buf, prefix);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200500 tail = buf + STRLEN(buf);
501
502 // Loop over all patterns in "name"
503 np = name;
504 while (*np != NUL && ((flags & DIP_ALL) || !did_one))
505 {
506 // Append the pattern from "name" to buf[].
507 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
508 "\t ");
509
Bram Moolenaar647a5302020-05-03 17:01:24 +0200510 if (p_verbose > 10)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200511 {
512 verbose_enter();
513 smsg(_("Searching for \"%s\""), buf);
514 verbose_leave();
515 }
516
517 // Expand wildcards, invoke the callback for each match.
518 if (gen_expand_wildcards(1, &buf, &num_files, &files,
519 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
520 {
521 for (i = 0; i < num_files; ++i)
522 {
523 (*callback)(files[i], cookie);
524 did_one = TRUE;
525 if (!(flags & DIP_ALL))
526 break;
527 }
528 FreeWild(num_files, files);
529 }
530 }
531 }
532 }
533 }
534 vim_free(buf);
535 vim_free(rtp_copy);
536 if (!did_one && name != NULL)
537 {
538 char *basepath = path == p_rtp ? "runtimepath" : "packpath";
539
540 if (flags & DIP_ERR)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000541 semsg(_(e_directory_not_found_in_str_str), basepath, name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200542 else if (p_verbose > 0)
543 {
544 verbose_enter();
545 smsg(_("not found in '%s': \"%s\""), basepath, name);
546 verbose_leave();
547 }
548 }
549
550#ifdef AMIGA
551 proc->pr_WindowPtr = save_winptr;
552#endif
553
554 return did_one ? OK : FAIL;
555}
556
557/*
558 * Find "name" in "path". When found, invoke the callback function for
559 * it: callback(fname, "cookie")
560 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first
561 * one is used.
562 * Returns OK when at least one match found, FAIL otherwise.
563 *
564 * If "name" is NULL calls callback for each entry in "path". Cookie is
565 * passed by reference in this case, setting it to NULL indicates that callback
566 * has done its job.
567 */
568 static int
569do_in_path_and_pp(
570 char_u *path,
571 char_u *name,
572 int flags,
573 void (*callback)(char_u *fname, void *ck),
574 void *cookie)
575{
576 int done = FAIL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200577
578 if ((flags & DIP_NORTP) == 0)
zeertzjq008c9152023-08-17 23:08:53 +0200579 done = do_in_path(path, "", name, flags, callback, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200580
581 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
zeertzjq008c9152023-08-17 23:08:53 +0200582 done = do_in_path(p_pp, "pack/*/start/*/", name, flags, callback,
583 cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200584
585 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
zeertzjq008c9152023-08-17 23:08:53 +0200586 done = do_in_path(p_pp, "pack/*/opt/*/", name, flags, callback,
587 cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200588
589 return done;
590}
591
592/*
593 * Just like do_in_path_and_pp(), using 'runtimepath' for "path".
594 */
595 int
596do_in_runtimepath(
597 char_u *name,
598 int flags,
599 void (*callback)(char_u *fname, void *ck),
600 void *cookie)
601{
602 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
603}
604
605/*
606 * Source the file "name" from all directories in 'runtimepath'.
607 * "name" can contain wildcards.
608 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
609 *
610 * return FAIL when no file could be sourced, OK otherwise.
611 */
612 int
613source_runtime(char_u *name, int flags)
614{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 return source_in_path(p_rtp, name, flags, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200616}
617
618/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000619 * Just like source_runtime(), but use "path" instead of 'runtimepath'
620 * and return the script ID in "ret_sid".
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200621 */
622 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200624{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200626}
627
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200628#if defined(FEAT_EVAL) || defined(PROTO)
629
630/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000631 * Find "name" in 'runtimepath'. If found a new scriptitem is created for it
632 * and it's script ID is returned.
633 * If not found returns -1.
634 */
635 int
636find_script_in_rtp(char_u *name)
637{
638 int sid = -1;
639
640 (void)do_in_path_and_pp(p_rtp, name, DIP_NOAFTER,
641 find_script_callback, &sid);
642 return sid;
643}
644
645/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200646 * Expand wildcards in "pat" and invoke do_source() for each match.
647 */
648 static void
649source_all_matches(char_u *pat)
650{
651 int num_files;
652 char_u **files;
653 int i;
654
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000655 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) != OK)
656 return;
657
658 for (i = 0; i < num_files; ++i)
659 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
660 FreeWild(num_files, files);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200661}
662
663/*
664 * Add the package directory to 'runtimepath'.
665 */
666 static int
667add_pack_dir_to_rtp(char_u *fname)
668{
669 char_u *p4, *p3, *p2, *p1, *p;
670 char_u *entry;
671 char_u *insp = NULL;
672 int c;
673 char_u *new_rtp;
674 int keep;
675 size_t oldlen;
676 size_t addlen;
677 size_t new_rtp_len;
678 char_u *afterdir = NULL;
679 size_t afterlen = 0;
680 char_u *after_insp = NULL;
681 char_u *ffname = NULL;
682 size_t fname_len;
683 char_u *buf = NULL;
684 char_u *rtp_ffname;
685 int match;
686 int retval = FAIL;
687
688 p4 = p3 = p2 = p1 = get_past_head(fname);
689 for (p = p1; *p; MB_PTR_ADV(p))
690 if (vim_ispathsep_nocolon(*p))
691 {
692 p4 = p3; p3 = p2; p2 = p1; p1 = p;
693 }
694
695 // now we have:
696 // rtp/pack/name/start/name
697 // p4 p3 p2 p1
698 //
699 // find the part up to "pack" in 'runtimepath'
700 c = *++p4; // append pathsep in order to expand symlink
701 *p4 = NUL;
702 ffname = fix_fname(fname);
703 *p4 = c;
704 if (ffname == NULL)
705 return FAIL;
706
707 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
708 // Also stop at the first "after" directory.
709 fname_len = STRLEN(ffname);
710 buf = alloc(MAXPATHL);
711 if (buf == NULL)
712 goto theend;
713 for (entry = p_rtp; *entry != NUL; )
714 {
715 char_u *cur_entry = entry;
716
717 copy_option_part(&entry, buf, MAXPATHL, ",");
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200718
719 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
720 && p > buf
721 && vim_ispathsep(p[-1])
722 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
723 {
724 if (insp == NULL)
725 // Did not find "ffname" before the first "after" directory,
726 // insert it before this entry.
727 insp = cur_entry;
728 after_insp = cur_entry;
729 break;
730 }
zeertzjq39c9ec12023-04-01 13:52:03 +0100731
732 if (insp == NULL)
733 {
734 add_pathsep(buf);
735 rtp_ffname = fix_fname(buf);
736 if (rtp_ffname == NULL)
737 goto theend;
738 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
739 vim_free(rtp_ffname);
740 if (match)
741 // Insert "ffname" after this entry (and comma).
742 insp = entry;
743 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200744 }
745
746 if (insp == NULL)
747 // Both "fname" and "after" not found, append at the end.
748 insp = p_rtp + STRLEN(p_rtp);
749
750 // check if rtp/pack/name/start/name/after exists
751 afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
752 if (afterdir != NULL && mch_isdir(afterdir))
753 afterlen = STRLEN(afterdir) + 1; // add one for comma
754
755 oldlen = STRLEN(p_rtp);
756 addlen = STRLEN(fname) + 1; // add one for comma
757 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
758 if (new_rtp == NULL)
759 goto theend;
760
761 // We now have 'rtp' parts: {keep}{keep_after}{rest}.
762 // Create new_rtp, first: {keep},{fname}
763 keep = (int)(insp - p_rtp);
764 mch_memmove(new_rtp, p_rtp, keep);
765 new_rtp_len = keep;
766 if (*insp == NUL)
767 new_rtp[new_rtp_len++] = ','; // add comma before
768 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
769 new_rtp_len += addlen - 1;
770 if (*insp != NUL)
771 new_rtp[new_rtp_len++] = ','; // add comma after
772
773 if (afterlen > 0 && after_insp != NULL)
774 {
775 int keep_after = (int)(after_insp - p_rtp);
776
777 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
778 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
779 keep_after - keep);
780 new_rtp_len += keep_after - keep;
781 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
782 new_rtp_len += afterlen - 1;
783 new_rtp[new_rtp_len++] = ',';
784 keep = keep_after;
785 }
786
787 if (p_rtp[keep] != NUL)
788 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
789 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
790 else
791 new_rtp[new_rtp_len] = NUL;
792
793 if (afterlen > 0 && after_insp == NULL)
794 {
795 // Append afterdir when "after" was not found:
796 // {keep},{fname}{rest},{afterdir}
797 STRCAT(new_rtp, ",");
798 STRCAT(new_rtp, afterdir);
799 }
800
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100801 set_option_value_give_err((char_u *)"rtp", 0L, new_rtp, 0);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200802 vim_free(new_rtp);
803 retval = OK;
804
805theend:
806 vim_free(buf);
807 vim_free(ffname);
808 vim_free(afterdir);
809 return retval;
810}
811
812/*
813 * Load scripts in "plugin" and "ftdetect" directories of the package.
814 */
815 static int
816load_pack_plugin(char_u *fname)
817{
818 static char *plugpat = "%s/plugin/**/*.vim";
819 static char *ftpat = "%s/ftdetect/*.vim";
820 int len;
821 char_u *ffname = fix_fname(fname);
822 char_u *pat = NULL;
823 int retval = FAIL;
824
825 if (ffname == NULL)
826 return FAIL;
827 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
828 pat = alloc(len);
829 if (pat == NULL)
830 goto theend;
831 vim_snprintf((char *)pat, len, plugpat, ffname);
832 source_all_matches(pat);
833
834 {
835 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
836
837 // If runtime/filetype.vim wasn't loaded yet, the scripts will be
838 // found when it loads.
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100839 if (cmd != NULL && eval_to_number(cmd, FALSE) > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200840 {
841 do_cmdline_cmd((char_u *)"augroup filetypedetect");
842 vim_snprintf((char *)pat, len, ftpat, ffname);
843 source_all_matches(pat);
844 do_cmdline_cmd((char_u *)"augroup END");
845 }
846 vim_free(cmd);
847 }
848 vim_free(pat);
849 retval = OK;
850
851theend:
852 vim_free(ffname);
853 return retval;
854}
855
856// used for "cookie" of add_pack_plugin()
857static int APP_ADD_DIR;
858static int APP_LOAD;
859static int APP_BOTH;
860
861 static void
862add_pack_plugin(char_u *fname, void *cookie)
863{
864 if (cookie != &APP_LOAD)
865 {
866 char_u *buf = alloc(MAXPATHL);
867 char_u *p;
868 int found = FALSE;
869
870 if (buf == NULL)
871 return;
872 p = p_rtp;
873 while (*p != NUL)
874 {
875 copy_option_part(&p, buf, MAXPATHL, ",");
876 if (pathcmp((char *)buf, (char *)fname, -1) == 0)
877 {
878 found = TRUE;
879 break;
880 }
881 }
882 vim_free(buf);
883 if (!found)
884 // directory is not yet in 'runtimepath', add it
885 if (add_pack_dir_to_rtp(fname) == FAIL)
886 return;
887 }
888
889 if (cookie != &APP_ADD_DIR)
890 load_pack_plugin(fname);
891}
892
893/*
894 * Add all packages in the "start" directory to 'runtimepath'.
895 */
896 void
897add_pack_start_dirs(void)
898{
zeertzjq008c9152023-08-17 23:08:53 +0200899 do_in_path(p_pp, "", (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200900 add_pack_plugin, &APP_ADD_DIR);
901}
902
903/*
904 * Load plugins from all packages in the "start" directory.
905 */
906 void
907load_start_packages(void)
908{
909 did_source_packages = TRUE;
zeertzjq008c9152023-08-17 23:08:53 +0200910 do_in_path(p_pp, "", (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200911 add_pack_plugin, &APP_LOAD);
912}
913
914/*
915 * ":packloadall"
916 * Find plugins in the package directories and source them.
917 */
918 void
919ex_packloadall(exarg_T *eap)
920{
921 if (!did_source_packages || eap->forceit)
922 {
923 // First do a round to add all directories to 'runtimepath', then load
924 // the plugins. This allows for plugins to use an autoload directory
925 // of another plugin.
926 add_pack_start_dirs();
927 load_start_packages();
928 }
929}
930
931/*
932 * ":packadd[!] {name}"
933 */
934 void
935ex_packadd(exarg_T *eap)
936{
937 static char *plugpat = "pack/*/%s/%s";
938 int len;
939 char *pat;
940 int round;
941 int res = OK;
942
943 // Round 1: use "start", round 2: use "opt".
944 for (round = 1; round <= 2; ++round)
945 {
946 // Only look under "start" when loading packages wasn't done yet.
947 if (round == 1 && did_source_packages)
948 continue;
949
950 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
951 pat = alloc(len);
952 if (pat == NULL)
953 return;
954 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
955 // The first round don't give a "not found" error, in the second round
956 // only when nothing was found in the first round.
zeertzjq008c9152023-08-17 23:08:53 +0200957 res = do_in_path(p_pp, "", (char_u *)pat,
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200958 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
959 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
960 vim_free(pat);
961 }
962}
963#endif
964
965/*
Bram Moolenaar26262f82019-09-04 20:59:15 +0200966 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
967 * list of file names in allocated memory.
968 */
969 void
970remove_duplicates(garray_T *gap)
971{
972 int i;
973 int j;
974 char_u **fnames = (char_u **)gap->ga_data;
975
976 sort_strings(fnames, gap->ga_len);
977 for (i = gap->ga_len - 1; i > 0; --i)
978 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
979 {
980 vim_free(fnames[i]);
981 for (j = i + 1; j < gap->ga_len; ++j)
982 fnames[j - 1] = fnames[j];
983 --gap->ga_len;
984 }
985}
986
zeertzjq5c8771b2023-01-24 12:34:03 +0000987 static void
988ExpandRTDir_int(
989 char_u *pat,
990 size_t pat_len,
991 int flags,
992 int keep_ext,
993 garray_T *gap,
994 char *dirnames[])
995{
996 for (int i = 0; dirnames[i] != NULL; ++i)
997 {
998 size_t buf_len = STRLEN(dirnames[i]) + pat_len + 22;
999 char *buf = alloc(buf_len);
1000 if (buf == NULL)
1001 {
1002 ga_clear_strings(gap);
1003 return;
1004 }
1005 char *tail = buf + 15;
1006 size_t tail_buflen = buf_len - 15;
1007 int glob_flags = 0;
1008 int expand_dirs = FALSE;
1009
1010 if (*(dirnames[i]) == NUL) // empty dir used for :runtime
1011 vim_snprintf(tail, tail_buflen, "%s*.vim", pat);
1012 else
1013 vim_snprintf(tail, tail_buflen, "%s/%s*.vim", dirnames[i], pat);
1014
1015expand:
1016 if ((flags & DIP_NORTP) == 0)
1017 globpath(p_rtp, (char_u *)tail, gap, glob_flags, expand_dirs);
1018
1019 if (flags & DIP_START)
1020 {
1021 memcpy(tail - 15, "pack/*/start/*/", 15);
1022 globpath(p_pp, (char_u *)tail - 15, gap, glob_flags, expand_dirs);
1023 }
1024
1025 if (flags & DIP_OPT)
1026 {
1027 memcpy(tail - 13, "pack/*/opt/*/", 13);
1028 globpath(p_pp, (char_u *)tail - 13, gap, glob_flags, expand_dirs);
1029 }
1030
1031 if (*(dirnames[i]) == NUL && !expand_dirs)
1032 {
1033 // expand dir names in another round
1034 vim_snprintf(tail, tail_buflen, "%s*", pat);
1035 glob_flags = WILD_ADD_SLASH;
1036 expand_dirs = TRUE;
1037 goto expand;
1038 }
1039
1040 vim_free(buf);
1041 }
1042
1043 int pat_pathsep_cnt = 0;
1044 for (size_t i = 0; i < pat_len; ++i)
1045 if (vim_ispathsep(pat[i]))
1046 ++pat_pathsep_cnt;
1047
1048 for (int i = 0; i < gap->ga_len; ++i)
1049 {
1050 char_u *match = ((char_u **)gap->ga_data)[i];
1051 char_u *s = match;
1052 char_u *e = s + STRLEN(s);
1053 if (e - 4 > s && !keep_ext && STRNICMP(e - 4, ".vim", 4) == 0)
1054 {
1055 e -= 4;
1056 *e = NUL;
1057 }
1058
1059 int match_pathsep_cnt = (e > s && e[-1] == '/') ? -1 : 0;
1060 for (s = e; s > match; MB_PTR_BACK(match, s))
1061 if (s < match || (vim_ispathsep(*s)
1062 && ++match_pathsep_cnt > pat_pathsep_cnt))
1063 break;
1064 ++s;
1065 if (s != match)
1066 mch_memmove(match, s, e - s + 1);
1067 }
1068
1069 if (gap->ga_len == 0)
1070 return;
1071
1072 // Sort and remove duplicates which can happen when specifying multiple
1073 // directories in dirnames.
1074 remove_duplicates(gap);
1075}
1076
Bram Moolenaar26262f82019-09-04 20:59:15 +02001077/*
zeertzjq3770f4c2023-01-22 18:38:51 +00001078 * Expand runtime file names.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001079 * Search from 'runtimepath':
1080 * 'runtimepath'/{dirnames}/{pat}.vim
zeertzjq3770f4c2023-01-22 18:38:51 +00001081 * When "flags" has DIP_START: search also from "start" of 'packpath':
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001082 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
zeertzjq3770f4c2023-01-22 18:38:51 +00001083 * When "flags" has DIP_OPT: search also from "opt" of 'packpath':
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001084 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
1085 * "dirnames" is an array with one or more directory names.
1086 */
1087 int
1088ExpandRTDir(
1089 char_u *pat,
1090 int flags,
1091 int *num_file,
1092 char_u ***file,
1093 char *dirnames[])
1094{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001095 *num_file = 0;
1096 *file = NULL;
zeertzjq5c8771b2023-01-24 12:34:03 +00001097
1098 garray_T ga;
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001099 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001100
zeertzjq5c8771b2023-01-24 12:34:03 +00001101 ExpandRTDir_int(pat, STRLEN(pat), flags, FALSE, &ga, dirnames);
1102
1103 if (ga.ga_len == 0)
1104 return FAIL;
1105
1106 *file = ga.ga_data;
1107 *num_file = ga.ga_len;
1108 return OK;
1109}
1110
1111/*
1112 * Handle command line completion for :runtime command.
1113 */
1114 int
1115expand_runtime_cmd(char_u *pat, int *numMatches, char_u ***matches)
1116{
1117 *numMatches = 0;
1118 *matches = NULL;
1119
1120 garray_T ga;
1121 ga_init2(&ga, sizeof(char *), 10);
1122
1123 size_t pat_len = (int)STRLEN(pat);
1124 char *dirnames[] = {"", NULL};
1125 ExpandRTDir_int(pat, pat_len, runtime_expand_flags, TRUE, &ga, dirnames);
1126
1127 // Try to complete values for [where] argument when none was found.
1128 if (runtime_expand_flags == 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001129 {
zeertzjq5c8771b2023-01-24 12:34:03 +00001130 char *where_values[] = {"START", "OPT", "PACK", "ALL"};
1131 for (size_t i = 0; i < ARRAY_LENGTH(where_values); ++i)
1132 if (STRNCMP(pat, where_values[i], pat_len) == 0)
1133 {
1134 char_u *p = vim_strsave((char_u *)where_values[i]);
1135 if (p != NULL && ga_add_string(&ga, p) == FAIL)
1136 vim_free(p);
1137 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001138 }
1139
1140 if (ga.ga_len == 0)
1141 return FAIL;
1142
zeertzjq5c8771b2023-01-24 12:34:03 +00001143 *matches = ga.ga_data;
1144 *numMatches = ga.ga_len;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001145 return OK;
1146}
1147
1148/*
1149 * Expand loadplugin names:
1150 * 'packpath'/pack/ * /opt/{pat}
1151 */
1152 int
1153ExpandPackAddDir(
1154 char_u *pat,
1155 int *num_file,
1156 char_u ***file)
1157{
1158 char_u *s;
1159 char_u *e;
1160 char_u *match;
1161 garray_T ga;
1162 int i;
1163 int pat_len;
1164
1165 *num_file = 0;
1166 *file = NULL;
1167 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001168 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001169
1170 s = alloc(pat_len + 26);
1171 if (s == NULL)
1172 {
1173 ga_clear_strings(&ga);
1174 return FAIL;
1175 }
1176 sprintf((char *)s, "pack/*/opt/%s*", pat);
zeertzjq3770f4c2023-01-22 18:38:51 +00001177 globpath(p_pp, s, &ga, 0, TRUE);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001178 vim_free(s);
1179
1180 for (i = 0; i < ga.ga_len; ++i)
1181 {
1182 match = ((char_u **)ga.ga_data)[i];
1183 s = gettail(match);
1184 e = s + STRLEN(s);
1185 mch_memmove(match, s, e - s + 1);
1186 }
1187
1188 if (ga.ga_len == 0)
1189 return FAIL;
1190
1191 // Sort and remove duplicates which can happen when specifying multiple
1192 // directories in dirnames.
1193 remove_duplicates(&ga);
1194
1195 *file = ga.ga_data;
1196 *num_file = ga.ga_len;
1197 return OK;
1198}
1199
1200 static void
1201cmd_source(char_u *fname, exarg_T *eap)
1202{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001203 int clearvars = FALSE;
1204
1205 if (*fname != NUL && STRNCMP(fname, "++clear", 7) == 0)
1206 {
1207 // ++clear argument is supplied
1208 clearvars = TRUE;
1209 fname = fname + 7;
1210 if (*fname != NUL)
1211 {
1212 semsg(_(e_invalid_argument_str), eap->arg);
1213 return;
1214 }
1215 }
1216
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001217 if (*fname != NUL && eap != NULL && eap->addr_count > 0)
1218 {
1219 // if a filename is specified to :source, then a range is not allowed
1220 emsg(_(e_no_range_allowed));
1221 return;
1222 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001223
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001224 if (eap != NULL && *fname == NUL)
1225 {
1226 if (eap->forceit)
1227 // a file name is needed to source normal mode commands
1228 emsg(_(e_argument_required));
1229 else
1230 // source ex commands from the current buffer
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001231 do_source_ext(NULL, FALSE, FALSE, NULL, eap, clearvars);
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001232 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001233 else if (eap != NULL && eap->forceit)
1234 // ":source!": read Normal mode commands
1235 // Need to execute the commands directly. This is required at least
1236 // for:
1237 // - ":g" command busy
1238 // - after ":argdo", ":windo" or ":bufdo"
1239 // - another command follows
1240 // - inside a loop
1241 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
1242#ifdef FEAT_EVAL
1243 || eap->cstack->cs_idx >= 0
1244#endif
1245 );
1246
1247 // ":source" read ex commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001248 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001249 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001250}
1251
1252/*
1253 * ":source {fname}"
1254 */
1255 void
1256ex_source(exarg_T *eap)
1257{
1258#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02001259 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001260 {
1261 char_u *fname = NULL;
1262
1263 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
1264 NULL, NULL,
1265 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1266 if (fname != NULL)
1267 {
1268 cmd_source(fname, eap);
1269 vim_free(fname);
1270 }
1271 }
1272 else
1273#endif
1274 cmd_source(eap->arg, eap);
1275}
1276
1277#if defined(FEAT_EVAL) || defined(PROTO)
1278/*
1279 * ":options"
1280 */
1281 void
1282ex_options(
1283 exarg_T *eap UNUSED)
1284{
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001285 char_u buf[500];
1286 int multi_mods = 0;
1287
1288 buf[0] = NUL;
dundargocc57b5bc2022-11-02 13:30:51 +00001289 (void)add_win_cmd_modifiers(buf, &cmdmod, &multi_mods);
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001290
1291 vim_setenv((char_u *)"OPTWIN_CMD", buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001292 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
1293}
1294#endif
1295
1296/*
1297 * ":source" and associated commands.
1298 */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001299
zeertzjq2d68b722023-03-30 21:50:37 +01001300#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001301/*
1302 * Return the address holding the next breakpoint line for a source cookie.
1303 */
1304 linenr_T *
1305source_breakpoint(void *cookie)
1306{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001307 return &((source_cookie_T *)cookie)->breakpoint;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001308}
1309
1310/*
1311 * Return the address holding the debug tick for a source cookie.
1312 */
1313 int *
1314source_dbg_tick(void *cookie)
1315{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001316 return &((source_cookie_T *)cookie)->dbg_tick;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001317}
1318
1319/*
1320 * Return the nesting level for a source cookie.
1321 */
1322 int
1323source_level(void *cookie)
1324{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001325 return ((source_cookie_T *)cookie)->level;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001326}
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001327
1328/*
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02001329 * Return the readahead line. Note that the pointer may become invalid when
1330 * getting the next line, if it's concatenated with the next one.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001331 */
1332 char_u *
1333source_nextline(void *cookie)
1334{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001335 return ((source_cookie_T *)cookie)->nextline;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001336}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001337#endif
1338
1339#if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC)
1340# define USE_FOPEN_NOINH
1341/*
1342 * Special function to open a file without handle inheritance.
1343 * When possible the handle is closed on exec().
1344 */
1345 static FILE *
1346fopen_noinh_readbin(char *filename)
1347{
1348# ifdef MSWIN
1349 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
1350# else
1351 int fd_tmp = mch_open(filename, O_RDONLY, 0);
1352# endif
1353
1354 if (fd_tmp == -1)
1355 return NULL;
1356
1357# ifdef HAVE_FD_CLOEXEC
1358 {
1359 int fdflags = fcntl(fd_tmp, F_GETFD);
1360 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1361 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
1362 }
1363# endif
1364
1365 return fdopen(fd_tmp, READBIN);
1366}
1367#endif
1368
1369/*
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001370 * Initialization for sourcing lines from the current buffer. Reads all the
1371 * lines from the buffer and stores it in the cookie grow array.
1372 * Returns a pointer to the name ":source buffer=<n>" on success and NULL on
1373 * failure.
1374 */
1375 static char_u *
1376do_source_buffer_init(source_cookie_T *sp, exarg_T *eap)
1377{
1378 linenr_T curr_lnum;
1379 char_u *line = NULL;
1380 char_u *fname;
1381
1382 CLEAR_FIELD(*sp);
1383
1384 if (curbuf == NULL)
1385 return NULL;
1386
1387 // Use ":source buffer=<num>" as the script name
1388 vim_snprintf((char *)IObuff, IOSIZE, ":source buffer=%d", curbuf->b_fnum);
1389 fname = vim_strsave(IObuff);
1390 if (fname == NULL)
1391 return NULL;
1392
1393 ga_init2(&sp->buflines, sizeof(char_u *), 100);
1394
1395 // Copy the lines from the buffer into a grow array
1396 for (curr_lnum = eap->line1; curr_lnum <= eap->line2; curr_lnum++)
1397 {
1398 line = vim_strsave(ml_get(curr_lnum));
1399 if (line == NULL)
1400 goto errret;
1401 if (ga_add_string(&sp->buflines, line) == FAIL)
1402 goto errret;
1403 line = NULL;
1404 }
1405 sp->buf_lnum = 0;
1406 sp->source_from_buf = TRUE;
1407
1408 return fname;
1409
1410errret:
1411 vim_free(fname);
1412 vim_free(line);
1413 ga_clear_strings(&sp->buflines);
1414 return NULL;
1415}
1416
1417/*
1418 * Read the file "fname" and execute its lines as EX commands.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1420 * again.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001421 *
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001422 * The "eap" argument is used when sourcing lines from a buffer instead of a
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001423 * file.
1424 *
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001425 * If "clearvars" is TRUE, then for scripts which are loaded more than
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001426 * once, clear all the functions and variables previously defined in that
1427 * script.
1428 *
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001429 * This function may be called recursively!
1430 *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001431 * Return FAIL if file could not be opened, OK otherwise.
1432 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001433 */
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001434 static int
1435do_source_ext(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001436 char_u *fname,
1437 int check_other, // check for .vimrc and _vimrc
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438 int is_vimrc, // DOSO_ value
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001439 int *ret_sid UNUSED,
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001440 exarg_T *eap,
1441 int clearvars UNUSED)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001442{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001443 source_cookie_T cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001444 char_u *p;
Bram Moolenaar753885b2022-08-24 16:30:36 +01001445 char_u *fname_not_fixed = NULL;
Bram Moolenaar5214b292022-08-24 17:32:35 +01001446 char_u *fname_exp = NULL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001447 char_u *firstline = NULL;
1448 int retval = FAIL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001449 sctx_T save_current_sctx;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001450#ifdef STARTUPTIME
1451 struct timeval tv_rel;
1452 struct timeval tv_start;
1453#endif
1454#ifdef FEAT_PROFILE
1455 proftime_T wait_start;
1456#endif
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001457 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001458 int trigger_source_post = FALSE;
ichizok7e5fe382023-04-15 13:17:50 +01001459#ifdef FEAT_EVAL
1460 funccal_entry_T funccalp_entry;
1461 int save_debug_break_level = debug_break_level;
1462 int sid = -1;
1463 scriptitem_T *si = NULL;
1464 int save_estack_compiling = estack_compiling;
1465 ESTACK_CHECK_DECLARATION;
1466#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001467
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001468 CLEAR_FIELD(cookie);
1469 if (fname == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001470 {
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001471 // sourcing lines from a buffer
1472 fname_exp = do_source_buffer_init(&cookie, eap);
1473 if (fname_exp == NULL)
1474 return FAIL;
1475 }
1476 else
1477 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01001478 fname_not_fixed = expand_env_save(fname);
1479 if (fname_not_fixed == NULL)
1480 goto theend;
1481 fname_exp = fix_fname(fname_not_fixed);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001482 if (fname_exp == NULL)
Bram Moolenaar753885b2022-08-24 16:30:36 +01001483 goto theend;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001484 if (mch_isdir(fname_exp))
1485 {
1486 smsg(_("Cannot source a directory: \"%s\""), fname);
1487 goto theend;
1488 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001489 }
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001490#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001491 estack_compiling = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001493 // See if we loaded this script before.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001494 sid = find_script_by_name(fname_exp);
Bram Moolenaarf479cac2022-01-12 12:54:55 +00001495 if (sid > 0 && ret_sid != NULL
1496 && SCRIPT_ITEM(sid)->sn_state != SN_STATE_NOT_LOADED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001497 {
1498 // Already loaded and no need to load again, return here.
1499 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001500 retval = OK;
1501 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001502 }
1503#endif
1504
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001505 // Apply SourceCmd autocommands, they should get the file and source it.
1506 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1507 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1508 FALSE, curbuf))
1509 {
1510#ifdef FEAT_EVAL
1511 retval = aborting() ? FAIL : OK;
1512#else
1513 retval = OK;
1514#endif
1515 if (retval == OK)
1516 // Apply SourcePost autocommands.
1517 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1518 FALSE, curbuf);
1519 goto theend;
1520 }
1521
1522 // Apply SourcePre autocommands, they may get the file.
1523 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1524
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001525 if (!cookie.source_from_buf)
1526 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001527#ifdef USE_FOPEN_NOINH
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001528 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001529#else
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001530 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001531#endif
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001532 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001533 if (cookie.fp == NULL && check_other)
1534 {
1535 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1536 // and ".exrc" by "_exrc" or vice versa.
1537 p = gettail(fname_exp);
1538 if ((*p == '.' || *p == '_')
1539 && (STRICMP(p + 1, "vimrc") == 0
1540 || STRICMP(p + 1, "gvimrc") == 0
1541 || STRICMP(p + 1, "exrc") == 0))
1542 {
1543 if (*p == '_')
1544 *p = '.';
1545 else
1546 *p = '_';
1547#ifdef USE_FOPEN_NOINH
1548 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1549#else
1550 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1551#endif
1552 }
1553 }
1554
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001555 if (cookie.fp == NULL && !cookie.source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001556 {
1557 if (p_verbose > 0)
1558 {
1559 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001560 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001561 smsg(_("could not source \"%s\""), fname);
1562 else
1563 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001564 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001565 verbose_leave();
1566 }
1567 goto theend;
1568 }
1569
1570 // The file exists.
1571 // - In verbose mode, give a message.
1572 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1573 if (p_verbose > 1)
1574 {
1575 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001576 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001577 smsg(_("sourcing \"%s\""), fname);
1578 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001579 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001580 verbose_leave();
1581 }
1582 if (is_vimrc == DOSO_VIMRC)
1583 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1584 else if (is_vimrc == DOSO_GVIMRC)
1585 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1586
1587#ifdef USE_CRNL
1588 // If no automatic file format: Set default to CR-NL.
1589 if (*p_ffs == NUL)
1590 cookie.fileformat = EOL_DOS;
1591 else
1592 cookie.fileformat = EOL_UNKNOWN;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001593#endif
1594
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001595 if (fname == NULL)
1596 // When sourcing a range of lines from a buffer, use the buffer line
1597 // number.
1598 cookie.sourcing_lnum = eap->line1 - 1;
1599 else
1600 cookie.sourcing_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001601
1602#ifdef FEAT_EVAL
1603 // Check if this script has a breakpoint.
1604 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1605 cookie.fname = fname_exp;
1606 cookie.dbg_tick = debug_tick;
1607
1608 cookie.level = ex_nesting_level;
1609#endif
1610
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001611#ifdef STARTUPTIME
1612 if (time_fd != NULL)
1613 time_push(&tv_rel, &tv_start);
1614#endif
1615
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001616 // "legacy" does not apply to commands in the script
1617 sticky_cmdmod_flags = 0;
1618
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001619 save_current_sctx = current_sctx;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001620 if (cmdmod.cmod_flags & CMOD_VIM9CMD)
1621 // When the ":vim9cmd" command modifier is used, source the script as a
1622 // Vim9 script.
1623 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1624 else
1625 current_sctx.sc_version = 1; // default script version
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001626
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001627#ifdef FEAT_EVAL
Bram Moolenaara2942c72023-01-02 13:41:49 +00001628 current_sctx.sc_lnum = 0;
1629
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001630# ifdef FEAT_PROFILE
1631 if (do_profiling == PROF_YES)
1632 prof_child_enter(&wait_start); // entering a child now
1633# endif
1634
1635 // Don't use local function variables, if called from a function.
1636 // Also starts profiling timer for nested script.
1637 save_funccal(&funccalp_entry);
1638
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001639 // Reset "KeyTyped" to avoid some commands thinking they are invoked
1640 // interactively. E.g. defining a function would output indent.
1641 int save_KeyTyped = KeyTyped;
1642 KeyTyped = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001643
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001644 // Check if this script was sourced before to find its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001645 // Always use a new sequence number.
1646 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001648 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 hashtab_T *ht;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001650 int todo;
1651 hashitem_T *hi;
1652 dictitem_T *di;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001653
1654 // loading the same script again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001656 si = SCRIPT_ITEM(sid);
1657 if (si->sn_state == SN_STATE_NOT_LOADED)
1658 {
1659 // this script was found but not loaded yet
1660 si->sn_state = SN_STATE_NEW;
1661 }
1662 else
1663 {
1664 si->sn_state = SN_STATE_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001666 if (!clearvars)
1667 {
1668 // Script-local variables remain but "const" can be set again.
1669 // In Vim9 script variables will be cleared when "vim9script"
1670 // is encountered without the "noclear" argument.
1671 ht = &SCRIPT_VARS(sid);
1672 todo = (int)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001673 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001674 if (!HASHITEM_EMPTY(hi))
1675 {
1676 --todo;
1677 di = HI2DI(hi);
1678 di->di_flags |= DI_FLAGS_RELOAD;
1679 }
1680 // imports can be redefined once
1681 mark_imports_for_reload(sid);
1682 }
1683 else
1684 clear_vim9_scriptlocal_vars(sid);
Bram Moolenaar0123cc12021-02-07 17:17:58 +01001685
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001686 // reset version, "vim9script" may have been added or removed.
1687 si->sn_version = 1;
1688 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001689 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001690 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001691 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001692 int error = OK;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001693
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001694 // It's new, generate a new SID and initialize the scriptitem.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001695 sid = get_new_scriptitem(&error);
1696 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001697 if (error == FAIL)
1698 goto almosttheend;
Bram Moolenaar753885b2022-08-24 16:30:36 +01001699 si = SCRIPT_ITEM(sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001700 si->sn_name = fname_exp;
1701 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 if (ret_sid != NULL)
Bram Moolenaar753885b2022-08-24 16:30:36 +01001703 *ret_sid = sid;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001704
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001705 // Remember the "is_vimrc" flag for when the file is sourced again.
1706 si->sn_is_vimrc = is_vimrc;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001707 }
1708
zeertzjq5a4fff42022-08-15 17:53:55 +01001709 // Keep the sourcing name/lnum, for recursive calls.
1710 estack_push(ETYPE_SCRIPT, si->sn_name, 0);
ichizok7e5fe382023-04-15 13:17:50 +01001711 ESTACK_CHECK_SETUP;
zeertzjq5a4fff42022-08-15 17:53:55 +01001712
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001713# ifdef FEAT_PROFILE
1714 if (do_profiling == PROF_YES)
1715 {
1716 int forceit;
1717
1718 // Check if we do profiling for this script.
1719 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
1720 {
1721 script_do_profile(si);
1722 si->sn_pr_force = forceit;
1723 }
1724 if (si->sn_prof_on)
1725 {
1726 ++si->sn_pr_count;
1727 profile_start(&si->sn_pr_start);
1728 profile_zero(&si->sn_pr_children);
1729 }
1730 }
1731# endif
Bram Moolenaaraeef1f72022-09-15 12:20:18 +01001732#else
1733 // Keep the sourcing name/lnum, for recursive calls.
1734 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001735#endif
1736
1737 cookie.conv.vc_type = CONV_NONE; // no conversion
1738
1739 // Read the first line so we can check for a UTF-8 BOM.
1740 firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1741 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1742 && firstline[1] == 0xbb && firstline[2] == 0xbf)
1743 {
1744 // Found BOM; setup conversion, skip over BOM and recode the line.
1745 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1746 p = string_convert(&cookie.conv, firstline + 3, NULL);
1747 if (p == NULL)
1748 p = vim_strsave(firstline + 3);
1749 if (p != NULL)
1750 {
1751 vim_free(firstline);
1752 firstline = p;
1753 }
1754 }
1755
1756 // Call do_cmdline, which will call getsourceline() to get the lines.
1757 do_cmdline(firstline, getsourceline, (void *)&cookie,
1758 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1759 retval = OK;
1760
1761#ifdef FEAT_PROFILE
1762 if (do_profiling == PROF_YES)
1763 {
1764 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001765 si = SCRIPT_ITEM(sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001766 if (si->sn_prof_on)
1767 {
1768 profile_end(&si->sn_pr_start);
1769 profile_sub_wait(&wait_start, &si->sn_pr_start);
1770 profile_add(&si->sn_pr_total, &si->sn_pr_start);
1771 profile_self(&si->sn_pr_self, &si->sn_pr_start,
1772 &si->sn_pr_children);
1773 }
1774 }
1775#endif
1776
1777 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001778 emsg(_(e_interrupted));
Bram Moolenaar88774872022-08-16 20:24:29 +01001779#ifdef FEAT_EVAL
ichizok7e5fe382023-04-15 13:17:50 +01001780 ESTACK_CHECK_NOW;
Bram Moolenaar88774872022-08-16 20:24:29 +01001781#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001782 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001783 if (p_verbose > 1)
1784 {
1785 verbose_enter();
1786 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001787 if (SOURCING_NAME != NULL)
1788 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001789 verbose_leave();
1790 }
1791#ifdef STARTUPTIME
1792 if (time_fd != NULL)
1793 {
1794 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1795 time_msg((char *)IObuff, &tv_start);
1796 time_pop(&tv_rel);
1797 }
1798#endif
1799
1800 if (!got_int)
1801 trigger_source_post = TRUE;
1802
1803#ifdef FEAT_EVAL
1804 // After a "finish" in debug mode, need to break at first command of next
1805 // sourced file.
1806 if (save_debug_break_level > ex_nesting_level
1807 && debug_break_level == ex_nesting_level)
1808 ++debug_break_level;
1809#endif
1810
1811#ifdef FEAT_EVAL
1812almosttheend:
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001813 // If "sn_save_cpo" is set that means we encountered "vim9script": restore
1814 // 'cpoptions', unless in the main .vimrc file.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001816 si = SCRIPT_ITEM(sid);
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001817 if (si->sn_save_cpo != NULL && si->sn_is_vimrc == DOSO_NONE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 {
Bram Moolenaar3e191692021-03-17 17:46:00 +01001819 if (STRCMP(p_cpo, CPO_VIM) != 0)
1820 {
1821 char_u *f;
1822 char_u *t;
1823
1824 // 'cpo' was changed in the script. Apply the same change to the
1825 // saved value, if possible.
1826 for (f = (char_u *)CPO_VIM; *f != NUL; ++f)
1827 if (vim_strchr(p_cpo, *f) == NULL
1828 && (t = vim_strchr(si->sn_save_cpo, *f)) != NULL)
1829 // flag was removed, also remove it from the saved 'cpo'
1830 mch_memmove(t, t + 1, STRLEN(t));
1831 for (f = p_cpo; *f != NUL; ++f)
1832 if (vim_strchr((char_u *)CPO_VIM, *f) == NULL
1833 && vim_strchr(si->sn_save_cpo, *f) == NULL)
1834 {
1835 // flag was added, also add it to the saved 'cpo'
1836 t = alloc(STRLEN(si->sn_save_cpo) + 2);
1837 if (t != NULL)
1838 {
1839 *t = *f;
1840 STRCPY(t + 1, si->sn_save_cpo);
1841 vim_free(si->sn_save_cpo);
1842 si->sn_save_cpo = t;
1843 }
1844 }
1845 }
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001846 set_option_value_give_err((char_u *)"cpo",
1847 0L, si->sn_save_cpo, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848 }
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001849 VIM_CLEAR(si->sn_save_cpo);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001851 restore_funccal();
1852# ifdef FEAT_PROFILE
1853 if (do_profiling == PROF_YES)
1854 prof_child_exit(&wait_start); // leaving a child now
1855# endif
Bram Moolenaara2942c72023-01-02 13:41:49 +00001856
1857 KeyTyped = save_KeyTyped;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001858#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001859 current_sctx = save_current_sctx;
1860
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001861 if (cookie.fp != NULL)
1862 fclose(cookie.fp);
1863 if (cookie.source_from_buf)
1864 ga_clear_strings(&cookie.buflines);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001865 vim_free(cookie.nextline);
1866 vim_free(firstline);
1867 convert_setup(&cookie.conv, NULL, NULL);
1868
1869 if (trigger_source_post)
1870 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1871
1872theend:
Bram Moolenaar0af2ecf2022-08-24 17:05:56 +01001873#ifdef FEAT_EVAL
Bram Moolenaar753885b2022-08-24 16:30:36 +01001874 if (sid > 0 && ret_sid != NULL
1875 && fname_not_fixed != NULL && fname_exp != NULL)
1876 {
1877 int not_fixed_sid = find_script_by_name(fname_not_fixed);
1878
1879 // If "fname_not_fixed" is a symlink then we source the linked file.
1880 // If the original name is in the script list we add the ID of the
1881 // script that was actually sourced.
1882 if (SCRIPT_ID_VALID(not_fixed_sid) && not_fixed_sid != sid)
1883 SCRIPT_ITEM(not_fixed_sid)->sn_sourced_sid = sid;
1884 }
Bram Moolenaar0af2ecf2022-08-24 17:05:56 +01001885#endif
Bram Moolenaar753885b2022-08-24 16:30:36 +01001886
1887 vim_free(fname_not_fixed);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001888 vim_free(fname_exp);
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001889 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001890#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001891 estack_compiling = save_estack_compiling;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001892#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001893 return retval;
1894}
1895
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001896 int
1897do_source(
1898 char_u *fname,
1899 int check_other, // check for .vimrc and _vimrc
1900 int is_vimrc, // DOSO_ value
Bram Moolenaar753885b2022-08-24 16:30:36 +01001901 int *ret_sid)
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001902{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001903 return do_source_ext(fname, check_other, is_vimrc, ret_sid, NULL, FALSE);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001904}
1905
1906
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001907#if defined(FEAT_EVAL) || defined(PROTO)
1908
1909/*
1910 * ":scriptnames"
1911 */
1912 void
1913ex_scriptnames(exarg_T *eap)
1914{
1915 int i;
1916
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001917 if (eap->addr_count > 0 || *eap->arg != NUL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001918 {
1919 // :script {scriptId}: edit the script
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001920 if (eap->addr_count > 0 && !SCRIPT_ID_VALID(eap->line2))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001921 emsg(_(e_invalid_argument));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001922 else
1923 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001924 if (eap->addr_count > 0)
1925 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
1926 else
1927 {
1928 expand_env(eap->arg, NameBuff, MAXPATHL);
1929 eap->arg = NameBuff;
1930 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001931 do_exedit(eap, NULL);
1932 }
1933 return;
1934 }
1935
1936 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar6079da72022-01-18 14:16:59 +00001937 {
1938 scriptitem_T *si = SCRIPT_ITEM(i);
1939
1940 if (si->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001941 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01001942 char sourced_buf[20];
1943
Bram Moolenaar6079da72022-01-18 14:16:59 +00001944 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar753885b2022-08-24 16:30:36 +01001945 if (si->sn_sourced_sid > 0)
1946 vim_snprintf(sourced_buf, 20, "->%d", si->sn_sourced_sid);
1947 else
1948 sourced_buf[0] = NUL;
1949 vim_snprintf((char *)IObuff, IOSIZE, "%3d%s%s: %s",
Bram Moolenaar6079da72022-01-18 14:16:59 +00001950 i,
Bram Moolenaar753885b2022-08-24 16:30:36 +01001951 sourced_buf,
Bram Moolenaar6079da72022-01-18 14:16:59 +00001952 si->sn_state == SN_STATE_NOT_LOADED ? " A" : "",
1953 NameBuff);
Bram Moolenaar769f5892022-02-09 14:31:05 +00001954 if (!message_filtered(IObuff))
1955 {
1956 msg_putchar('\n');
1957 msg_outtrans(IObuff);
1958 out_flush(); // output one line at a time
1959 ui_breakcheck();
1960 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001961 }
Bram Moolenaar6079da72022-01-18 14:16:59 +00001962 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001963}
1964
1965# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1966/*
1967 * Fix slashes in the list of script names for 'shellslash'.
1968 */
1969 void
1970scriptnames_slash_adjust(void)
1971{
1972 int i;
1973
1974 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001975 if (SCRIPT_ITEM(i)->sn_name != NULL)
1976 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001977}
1978# endif
1979
1980/*
1981 * Get a pointer to a script name. Used for ":verbose set".
Bram Moolenaar74667062020-12-28 15:41:41 +01001982 * Message appended to "Last set from "
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001983 */
1984 char_u *
1985get_scriptname(scid_T id)
1986{
1987 if (id == SID_MODELINE)
1988 return (char_u *)_("modeline");
1989 if (id == SID_CMDARG)
1990 return (char_u *)_("--cmd argument");
1991 if (id == SID_CARG)
1992 return (char_u *)_("-c argument");
1993 if (id == SID_ENV)
1994 return (char_u *)_("environment variable");
1995 if (id == SID_ERROR)
1996 return (char_u *)_("error handler");
Bram Moolenaar74667062020-12-28 15:41:41 +01001997 if (id == SID_WINLAYOUT)
1998 return (char_u *)_("changed window size");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001999 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002000}
2001
2002# if defined(EXITFREE) || defined(PROTO)
2003 void
2004free_scriptnames(void)
2005{
2006 int i;
2007
2008 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02002009 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002010 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002012 // the variables themselves are cleared in evalvars_clear()
2013 vim_free(si->sn_vars);
2014
2015 vim_free(si->sn_name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02002016 free_imports_and_script_vars(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002017 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02002018# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002019 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02002020# endif
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002021 vim_free(si->sn_autoload_prefix);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002022 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02002023 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002024 ga_clear(&script_items);
2025}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002026
2027 void
2028free_autoload_scriptnames(void)
2029{
2030 ga_clear_strings(&ga_loaded);
2031}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002032# endif
2033
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002034 linenr_T
Bram Moolenaar66250c92020-08-20 15:02:42 +02002035get_sourced_lnum(
2036 char_u *(*fgetline)(int, void *, int, getline_opt_T),
2037 void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002038{
2039 return fgetline == getsourceline
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002040 ? ((source_cookie_T *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002041 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002042}
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002043
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002044/*
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002045 * Return a List of script-local functions defined in the script with id
2046 * 'sid'.
2047 */
2048 static list_T *
2049get_script_local_funcs(scid_T sid)
2050{
2051 hashtab_T *functbl;
2052 hashitem_T *hi;
2053 long_u todo;
2054 list_T *l;
2055
2056 l = list_alloc();
2057 if (l == NULL)
2058 return NULL;
2059
2060 // Iterate through all the functions in the global function hash table
2061 // looking for functions with script ID 'sid'.
2062 functbl = func_tbl_get();
2063 todo = functbl->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002064 FOR_ALL_HASHTAB_ITEMS(functbl, hi, todo)
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002065 {
2066 ufunc_T *fp;
2067
2068 if (HASHITEM_EMPTY(hi))
2069 continue;
2070
2071 --todo;
2072 fp = HI2UF(hi);
2073
2074 // Add active functions with script id == 'sid'
2075 if (!(fp->uf_flags & FC_DEAD) && (fp->uf_script_ctx.sc_sid == sid))
2076 {
2077 char_u *name;
2078
2079 if (fp->uf_name_exp != NULL)
2080 name = fp->uf_name_exp;
2081 else
2082 name = fp->uf_name;
2083
2084 list_append_string(l, name, -1);
2085 }
2086 }
2087
2088 return l;
2089}
2090
2091/*
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002092 * getscriptinfo() function
2093 */
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002094 void
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002095f_getscriptinfo(typval_T *argvars, typval_T *rettv)
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002096{
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002097 list_T *l;
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002098 char_u *pat = NULL;
2099 regmatch_T regmatch;
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002100 int filterpat = FALSE;
2101 scid_T sid = -1;
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002102
2103 if (rettv_list_alloc(rettv) == FAIL)
2104 return;
2105
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002106 if (check_for_opt_dict_arg(argvars, 0) == FAIL)
2107 return;
2108
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002109 l = rettv->vval.v_list;
2110
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002111 regmatch.regprog = NULL;
2112 regmatch.rm_ic = p_ic;
2113
2114 if (argvars[0].v_type == VAR_DICT)
2115 {
zeertzjq2d68b722023-03-30 21:50:37 +01002116 dictitem_T *sid_di = dict_find(argvars[0].vval.v_dict,
2117 (char_u *)"sid", 3);
2118 if (sid_di != NULL)
2119 {
2120 int error = FALSE;
2121 sid = tv_get_number_chk(&sid_di->di_tv, &error);
2122 if (error)
2123 return;
2124 if (sid <= 0)
2125 {
zeertzjq276410e2023-05-07 21:59:33 +01002126 semsg(_(e_invalid_value_for_argument_str_str), "sid",
zeertzjq2d68b722023-03-30 21:50:37 +01002127 tv_get_string(&sid_di->di_tv));
2128 return;
2129 }
2130 }
2131 else
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002132 {
2133 pat = dict_get_string(argvars[0].vval.v_dict, "name", TRUE);
2134 if (pat != NULL)
2135 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2136 if (regmatch.regprog != NULL)
2137 filterpat = TRUE;
2138 }
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002139 }
2140
zeertzjq2d68b722023-03-30 21:50:37 +01002141 for (varnumber_T i = sid > 0 ? sid : 1;
2142 (i == sid || sid <= 0) && i <= script_items.ga_len; ++i)
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002143 {
2144 scriptitem_T *si = SCRIPT_ITEM(i);
2145 dict_T *d;
2146
2147 if (si->sn_name == NULL)
2148 continue;
2149
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002150 if (filterpat && !vim_regexec(&regmatch, si->sn_name, (colnr_T)0))
2151 continue;
2152
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002153 if ((d = dict_alloc()) == NULL
2154 || list_append_dict(l, d) == FAIL
2155 || dict_add_string(d, "name", si->sn_name) == FAIL
2156 || dict_add_number(d, "sid", i) == FAIL
Bram Moolenaar753885b2022-08-24 16:30:36 +01002157 || dict_add_number(d, "sourced", si->sn_sourced_sid) == FAIL
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002158 || dict_add_number(d, "version", si->sn_version) == FAIL
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002159 || dict_add_bool(d, "autoload",
2160 si->sn_state == SN_STATE_NOT_LOADED) == FAIL)
2161 return;
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002162
zeertzjq2d68b722023-03-30 21:50:37 +01002163 // When a script ID is specified, return information about only the
2164 // specified script, and add the script-local variables and functions.
2165 if (sid > 0)
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002166 {
2167 dict_T *var_dict;
2168
2169 var_dict = dict_copy(&si->sn_vars->sv_dict, TRUE, TRUE,
2170 get_copyID());
2171 if (var_dict == NULL
2172 || dict_add_dict(d, "variables", var_dict) == FAIL
2173 || dict_add_list(d, "functions",
2174 get_script_local_funcs(sid)) == FAIL)
2175 return;
2176 }
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002177 }
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002178
2179 vim_regfree(regmatch.regprog);
2180 vim_free(pat);
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002181}
2182
Dominique Pelle748b3082022-01-08 12:41:16 +00002183#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002184
2185 static char_u *
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002186get_one_sourceline(source_cookie_T *sp)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002187{
2188 garray_T ga;
2189 int len;
2190 int c;
2191 char_u *buf;
2192#ifdef USE_CRNL
2193 int has_cr; // CR-LF found
2194#endif
2195 int have_read = FALSE;
2196
2197 // use a growarray to store the sourced line
2198 ga_init2(&ga, 1, 250);
2199
2200 // Loop until there is a finished line (or end-of-file).
2201 ++sp->sourcing_lnum;
2202 for (;;)
2203 {
2204 // make room to read at least 120 (more) characters
2205 if (ga_grow(&ga, 120) == FAIL)
2206 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002207 if (sp->source_from_buf)
2208 {
2209 if (sp->buf_lnum >= sp->buflines.ga_len)
2210 break; // all the lines are processed
2211 ga_concat(&ga, ((char_u **)sp->buflines.ga_data)[sp->buf_lnum]);
2212 sp->buf_lnum++;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01002213 if (ga_grow(&ga, 1) == FAIL)
2214 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002215 buf = (char_u *)ga.ga_data;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01002216 buf[ga.ga_len++] = NUL;
Bram Moolenaar4748c4b2022-05-17 17:47:07 +01002217 len = ga.ga_len;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002218 }
2219 else
2220 {
2221 buf = (char_u *)ga.ga_data;
2222 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
2223 sp->fp) == NULL)
2224 break;
Bram Moolenaar4748c4b2022-05-17 17:47:07 +01002225 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002226 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002227#ifdef USE_CRNL
2228 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
2229 // CTRL-Z by its own, or after a NL.
2230 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
2231 && sp->fileformat == EOL_DOS
2232 && buf[len - 1] == Ctrl_Z)
2233 {
2234 buf[len - 1] = NUL;
2235 break;
2236 }
2237#endif
2238
2239 have_read = TRUE;
2240 ga.ga_len = len;
2241
2242 // If the line was longer than the buffer, read more.
2243 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
2244 continue;
2245
2246 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
2247 {
2248#ifdef USE_CRNL
2249 has_cr = (len >= 2 && buf[len - 2] == '\r');
2250 if (sp->fileformat == EOL_UNKNOWN)
2251 {
2252 if (has_cr)
2253 sp->fileformat = EOL_DOS;
2254 else
2255 sp->fileformat = EOL_UNIX;
2256 }
2257
2258 if (sp->fileformat == EOL_DOS)
2259 {
2260 if (has_cr) // replace trailing CR
2261 {
2262 buf[len - 2] = '\n';
2263 --len;
2264 --ga.ga_len;
2265 }
2266 else // lines like ":map xx yy^M" will have failed
2267 {
2268 if (!sp->error)
2269 {
2270 msg_source(HL_ATTR(HLF_W));
2271 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
2272 }
2273 sp->error = TRUE;
2274 sp->fileformat = EOL_UNIX;
2275 }
2276 }
2277#endif
2278 // The '\n' is escaped if there is an odd number of ^V's just
2279 // before it, first set "c" just before the 'V's and then check
2280 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
2281 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
2282 ;
2283 if ((len & 1) != (c & 1)) // escaped NL, read more
2284 {
2285 ++sp->sourcing_lnum;
2286 continue;
2287 }
2288
2289 buf[len - 1] = NUL; // remove the NL
2290 }
2291
2292 // Check for ^C here now and then, so recursive :so can be broken.
2293 line_breakcheck();
2294 break;
2295 }
2296
2297 if (have_read)
2298 return (char_u *)ga.ga_data;
2299
2300 vim_free(ga.ga_data);
2301 return NULL;
2302}
2303
2304/*
2305 * Get one full line from a sourced file.
2306 * Called by do_cmdline() when it's called from do_source().
2307 *
2308 * Return a pointer to the line in allocated memory.
2309 * Return NULL for end-of-file or some error.
2310 */
2311 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02002312getsourceline(
2313 int c UNUSED,
2314 void *cookie,
2315 int indent UNUSED,
2316 getline_opt_T options)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002317{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002318 source_cookie_T *sp = (source_cookie_T *)cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002319 char_u *line;
2320 char_u *p;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002321 int do_vim9_all = in_vim9script()
2322 && options == GETLINE_CONCAT_ALL;
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002323 int do_bar_cont = do_vim9_all
2324 || options == GETLINE_CONCAT_CONTBAR;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002325
2326#ifdef FEAT_EVAL
2327 // If breakpoints have been added/deleted need to check for it.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002328 if ((sp->dbg_tick < debug_tick) && !sp->source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002329 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002330 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002331 sp->dbg_tick = debug_tick;
2332 }
2333# ifdef FEAT_PROFILE
2334 if (do_profiling == PROF_YES)
2335 script_line_end();
2336# endif
2337#endif
2338
2339 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002340 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002341
2342 // Get current line. If there is a read-ahead line, use it, otherwise get
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002343 // one now. "fp" is NULL if actually using a string.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002344 if (sp->finished || (!sp->source_from_buf && sp->fp == NULL))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002345 line = NULL;
2346 else if (sp->nextline == NULL)
2347 line = get_one_sourceline(sp);
2348 else
2349 {
2350 line = sp->nextline;
2351 sp->nextline = NULL;
2352 ++sp->sourcing_lnum;
2353 }
2354#ifdef FEAT_PROFILE
2355 if (line != NULL && do_profiling == PROF_YES)
2356 script_line_start();
2357#endif
2358
2359 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
2360 // contain the 'C' flag.
Bram Moolenaar66250c92020-08-20 15:02:42 +02002361 if (line != NULL && options != GETLINE_NONE
2362 && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002363 {
Bram Moolenaar5072b472021-06-03 21:56:10 +02002364 int comment_char = in_vim9script() ? '#' : '"';
2365
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002366 // compensate for the one line read-ahead
2367 --sp->sourcing_lnum;
2368
2369 // Get the next line and concatenate it when it starts with a
2370 // backslash. We always need to read the next line, keep it in
2371 // sp->nextline.
2372 /* Also check for a comment in between continuation lines: "\ */
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002373 // Also check for a Vim9 comment, empty line, line starting with '|',
2374 // but not "||".
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002375 sp->nextline = get_one_sourceline(sp);
2376 if (sp->nextline != NULL
2377 && (*(p = skipwhite(sp->nextline)) == '\\'
Bram Moolenaar5072b472021-06-03 21:56:10 +02002378 || (p[0] == comment_char
2379 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002380 || (do_vim9_all && (*p == NUL
2381 || vim9_comment_start(p)))
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002382 || (do_bar_cont && p[0] == '|' && p[1] != '|')))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002383 {
2384 garray_T ga;
2385
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002386 ga_init2(&ga, sizeof(char_u), 400);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002387 ga_concat(&ga, line);
2388 if (*p == '\\')
2389 ga_concat(&ga, p + 1);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002390 else if (*p == '|')
2391 {
2392 ga_concat(&ga, (char_u *)" ");
2393 ga_concat(&ga, p);
2394 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002395 for (;;)
2396 {
2397 vim_free(sp->nextline);
2398 sp->nextline = get_one_sourceline(sp);
2399 if (sp->nextline == NULL)
2400 break;
2401 p = skipwhite(sp->nextline);
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002402 if (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|'))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002403 {
2404 // Adjust the growsize to the current length to speed up
2405 // concatenating many lines.
2406 if (ga.ga_len > 400)
2407 {
2408 if (ga.ga_len > 8000)
2409 ga.ga_growsize = 8000;
2410 else
2411 ga.ga_growsize = ga.ga_len;
2412 }
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002413 if (*p == '\\')
2414 ga_concat(&ga, p + 1);
2415 else
2416 {
2417 ga_concat(&ga, (char_u *)" ");
2418 ga_concat(&ga, p);
2419 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002420 }
Bram Moolenaar5072b472021-06-03 21:56:10 +02002421 else if (!(p[0] == (comment_char)
Bram Moolenaar0f37e352021-06-02 15:28:15 +02002422 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002423 && !(do_vim9_all && (*p == NUL || vim9_comment_start(p))))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002424 break;
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002425 /* drop a # comment or "\ comment line */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002426 }
2427 ga_append(&ga, NUL);
2428 vim_free(line);
2429 line = ga.ga_data;
2430 }
2431 }
2432
2433 if (line != NULL && sp->conv.vc_type != CONV_NONE)
2434 {
2435 char_u *s;
2436
2437 // Convert the encoding of the script line.
2438 s = string_convert(&sp->conv, line, NULL);
2439 if (s != NULL)
2440 {
2441 vim_free(line);
2442 line = s;
2443 }
2444 }
2445
2446#ifdef FEAT_EVAL
2447 // Did we encounter a breakpoint?
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002448 if (!sp->source_from_buf && sp->breakpoint != 0
2449 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002450 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002451 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002452 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002453 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002454 sp->dbg_tick = debug_tick;
2455 }
2456#endif
2457
2458 return line;
2459}
2460
2461/*
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002462 * Returns TRUE if sourcing a script either from a file or a buffer.
2463 * Otherwise returns FALSE.
2464 */
2465 int
2466sourcing_a_script(exarg_T *eap)
2467{
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002468 return (getline_equal(eap->getline, eap->cookie, getsourceline));
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002469}
2470
2471/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002472 * ":scriptencoding": Set encoding conversion for a sourced script.
2473 */
2474 void
2475ex_scriptencoding(exarg_T *eap)
2476{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002477 source_cookie_T *sp;
2478 char_u *name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002479
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002480 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002481 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00002482 emsg(_(e_scriptencoding_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002483 return;
2484 }
2485
2486 if (*eap->arg != NUL)
2487 {
2488 name = enc_canonize(eap->arg);
2489 if (name == NULL) // out of memory
2490 return;
2491 }
2492 else
2493 name = eap->arg;
2494
2495 // Setup for conversion from the specified encoding to 'encoding'.
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002496 sp = (source_cookie_T *)getline_cookie(eap->getline, eap->cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002497 convert_setup(&sp->conv, name, p_enc);
2498
2499 if (name != eap->arg)
2500 vim_free(name);
2501}
2502
2503/*
2504 * ":scriptversion": Set Vim script version for a sourced script.
2505 */
2506 void
2507ex_scriptversion(exarg_T *eap UNUSED)
2508{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002509 int nr;
2510
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002511 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002512 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002513 emsg(_(e_scriptversion_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002514 return;
2515 }
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002516 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002518 emsg(_(e_cannot_use_scriptversion_after_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 return;
2520 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002521
2522 nr = getdigits(&eap->arg);
2523 if (nr == 0 || *eap->arg != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002524 emsg(_(e_invalid_argument));
Bram Moolenaar67979662020-06-20 22:50:47 +02002525 else if (nr > SCRIPT_VERSION_MAX)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002526 semsg(_(e_scriptversion_not_supported_nr), nr);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002527 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002528 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002529 current_sctx.sc_version = nr;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002530#ifdef FEAT_EVAL
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002531 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002532#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002533 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002534}
2535
2536#if defined(FEAT_EVAL) || defined(PROTO)
2537/*
2538 * ":finish": Mark a sourced file as finished.
2539 */
2540 void
2541ex_finish(exarg_T *eap)
2542{
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002543 if (sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002544 do_finish(eap, FALSE);
2545 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00002546 emsg(_(e_finish_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002547}
2548
2549/*
2550 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
2551 * Also called for a pending finish at the ":endtry" or after returning from
2552 * an extra do_cmdline(). "reanimate" is used in the latter case.
2553 */
2554 void
2555do_finish(exarg_T *eap, int reanimate)
2556{
2557 int idx;
2558
2559 if (reanimate)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002560 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002561 eap->cookie))->finished = FALSE;
2562
2563 // Cleanup (and inactivate) conditionals, but stop when a try conditional
2564 // not in its finally clause (which then is to be executed next) is found.
2565 // In this case, make the ":finish" pending for execution at the ":endtry".
2566 // Otherwise, finish normally.
2567 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
2568 if (idx >= 0)
2569 {
2570 eap->cstack->cs_pending[idx] = CSTP_FINISH;
2571 report_make_pending(CSTP_FINISH, NULL);
2572 }
2573 else
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002574 ((source_cookie_T *)getline_cookie(eap->getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002575 eap->cookie))->finished = TRUE;
2576}
2577
2578
2579/*
2580 * Return TRUE when a sourced file had the ":finish" command: Don't give error
2581 * message for missing ":endif".
2582 * Return FALSE when not sourcing a file.
2583 */
2584 int
2585source_finished(
Bram Moolenaar66250c92020-08-20 15:02:42 +02002586 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002587 void *cookie)
2588{
2589 return (getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002590 && ((source_cookie_T *)getline_cookie(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002591 fgetline, cookie))->finished);
2592}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002593
2594/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002595 * Find the path of a script below the "autoload" directory.
2596 * Returns NULL if there is no "/autoload/" in the script name.
2597 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01002598 static char_u *
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002599script_name_after_autoload(scriptitem_T *si)
2600{
2601 char_u *p = si->sn_name;
2602 char_u *res = NULL;
2603
2604 for (;;)
2605 {
2606 char_u *n = (char_u *)strstr((char *)p, "autoload");
2607
2608 if (n == NULL)
2609 break;
2610 if (n > p && vim_ispathsep(n[-1]) && vim_ispathsep(n[8]))
2611 res = n + 9;
2612 p = n + 8;
2613 }
2614 return res;
2615}
2616
2617/*
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002618 * For an autoload script "autoload/dir/script.vim" return the prefix
2619 * "dir#script#" in allocated memory.
2620 * Returns NULL if anything is wrong.
2621 */
2622 char_u *
2623get_autoload_prefix(scriptitem_T *si)
2624{
2625 char_u *p = script_name_after_autoload(si);
2626 char_u *prefix;
2627
2628 if (p == NULL)
2629 return NULL;
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002630 prefix = vim_strsave(p);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002631 if (prefix == NULL)
2632 return NULL;
2633
2634 // replace all '/' with '#' and locate ".vim" at the end
2635 for (p = prefix; *p != NUL; p += mb_ptr2len(p))
2636 {
2637 if (vim_ispathsep(*p))
2638 *p = '#';
2639 else if (STRCMP(p, ".vim") == 0)
2640 {
2641 p[0] = '#';
2642 p[1] = NUL;
2643 return prefix;
2644 }
2645 }
2646
2647 // did not find ".vim" at the end
2648 vim_free(prefix);
2649 return NULL;
2650}
2651
2652/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002653 * If in a Vim9 autoload script return "name" with the autoload prefix for the
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002654 * script. If successful the returned name is allocated.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002655 * Otherwise it returns "name" unmodified.
2656 */
2657 char_u *
2658may_prefix_autoload(char_u *name)
2659{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002660 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2661 return name;
2662
2663 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2664
2665 if (si->sn_autoload_prefix == NULL)
2666 return name;
2667
2668 char_u *basename = name;
2669 size_t len;
2670 char_u *res;
2671
2672 if (*name == K_SPECIAL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002673 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002674 char_u *p = vim_strchr(name, '_');
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002675
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002676 // skip over "<SNR>99_"
2677 if (p != NULL)
2678 basename = p + 1;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002679 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002680
2681 len = STRLEN(si->sn_autoload_prefix) + STRLEN(basename) + 2;
2682 res = alloc(len);
2683 if (res == NULL)
2684 return NULL;
2685
2686 vim_snprintf((char *)res, len, "%s%s", si->sn_autoload_prefix, basename);
2687 return res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002688}
2689
2690/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002691 * Return the autoload script name for a function or variable name.
2692 * Returns NULL when out of memory.
2693 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
2694 */
2695 char_u *
2696autoload_name(char_u *name)
2697{
2698 char_u *p, *q = NULL;
2699 char_u *scriptname;
2700
2701 // Get the script file name: replace '#' with '/', append ".vim".
2702 scriptname = alloc(STRLEN(name) + 14);
2703 if (scriptname == NULL)
2704 return NULL;
2705 STRCPY(scriptname, "autoload/");
Bram Moolenaara1773442020-08-12 15:21:22 +02002706 STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002707 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
2708 q = p, ++p)
2709 *p = '/';
2710 STRCPY(q, ".vim");
2711 return scriptname;
2712}
2713
2714/*
2715 * If "name" has a package name try autoloading the script for it.
2716 * Return TRUE if a package was loaded.
2717 */
2718 int
2719script_autoload(
2720 char_u *name,
2721 int reload) // load script again when already loaded
2722{
2723 char_u *p;
2724 char_u *scriptname, *tofree;
2725 int ret = FALSE;
2726 int i;
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002727 int ret_sid;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002728
Bram Moolenaar89445512022-04-14 12:58:23 +01002729 // If the name starts with "<SNR>123_" then "123" is the script ID.
2730 if (name[0] == K_SPECIAL && name[1] == KS_EXTRA && name[2] == KE_SNR)
2731 {
2732 p = name + 3;
2733 ret_sid = (int)getdigits(&p);
2734 if (*p == '_' && SCRIPT_ID_VALID(ret_sid))
2735 {
2736 may_load_script(ret_sid, &ret);
2737 return ret;
2738 }
2739 }
2740
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002741 // If there is no '#' after name[0] there is no package name.
2742 p = vim_strchr(name, AUTOLOAD_CHAR);
2743 if (p == NULL || p == name)
2744 return FALSE;
2745
2746 tofree = scriptname = autoload_name(name);
2747 if (scriptname == NULL)
2748 return FALSE;
2749
2750 // Find the name in the list of previously loaded package names. Skip
2751 // "autoload/", it's always the same.
2752 for (i = 0; i < ga_loaded.ga_len; ++i)
2753 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2754 break;
2755 if (!reload && i < ga_loaded.ga_len)
2756 ret = FALSE; // was loaded already
2757 else
2758 {
2759 // Remember the name if it wasn't loaded already.
2760 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2761 {
2762 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2763 tofree = NULL;
2764 }
2765
2766 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002767 // Use "ret_sid" to avoid loading the same script again.
=?UTF-8?q?Bj=C3=B6rn=20Linse?=223a9502022-01-31 17:26:05 +00002768 if (source_in_path(p_rtp, scriptname, DIP_START, &ret_sid) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002769 ret = TRUE;
2770 }
2771
2772 vim_free(tofree);
2773 return ret;
2774}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002775#endif