blob: 109e13e5c602e54c21e430a15822e54354469d5b [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;
Bram Moolenaar85b09572019-12-30 10:57:00 +0100132#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100133
134 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100135#ifdef FEAT_EVAL
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200136 if (which == ESTACK_SFILE && entry->es_type != ETYPE_UFUNC)
Bram Moolenaar09d46402019-12-29 23:53:01 +0100137#endif
Bram Moolenaara5d04232020-07-26 15:37:02 +0200138 {
139 if (entry->es_name == NULL)
140 return NULL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100141 return vim_strsave(entry->es_name);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200142 }
Bram Moolenaar09d46402019-12-29 23:53:01 +0100143#ifdef FEAT_EVAL
Bram Moolenaarc4492712021-11-22 15:37:15 +0000144 // expand('<sfile>') works in a function for backwards compatibility, but
145 // may give an unexpected result. Disallow it in Vim 9 script.
146 if (which == ESTACK_SFILE && in_vim9script())
147 {
148 int save_emsg_off = emsg_off;
149
150 if (emsg_off == 1)
151 // f_expand() silences errors but we do want this one
152 emsg_off = 0;
153 emsg(_(e_cannot_expand_sfile_in_vim9_function));
154 emsg_off = save_emsg_off;
155 return NULL;
156 }
157
LemonBoyeca7c602022-04-14 15:39:43 +0100158 // If evaluated in a function or autocommand, return the path of the script
159 // where it is defined, at script level the current script path is returned
LemonBoy6013d002022-04-09 21:42:10 +0100160 // instead.
161 if (which == ESTACK_SCRIPT)
162 {
LemonBoyeca7c602022-04-14 15:39:43 +0100163 // Walk the stack backwards, starting from the current frame.
164 for (idx = exestack.ga_len - 1; idx >= 0; --idx, --entry)
LemonBoy6013d002022-04-09 21:42:10 +0100165 {
zeertzjqa2471422022-08-23 19:26:05 +0100166 if (entry->es_type == ETYPE_UFUNC || entry->es_type == ETYPE_AUCMD)
LemonBoy6013d002022-04-09 21:42:10 +0100167 {
zeertzjqa2471422022-08-23 19:26:05 +0100168 sctx_T *def_ctx = entry->es_type == ETYPE_UFUNC
169 ? &entry->es_info.ufunc->uf_script_ctx
170 : acp_script_ctx(entry->es_info.aucmd);
LemonBoy6013d002022-04-09 21:42:10 +0100171
zeertzjqa2471422022-08-23 19:26:05 +0100172 return def_ctx->sc_sid > 0
173 ? vim_strsave(SCRIPT_ITEM(def_ctx->sc_sid)->sn_name)
174 : NULL;
LemonBoyeca7c602022-04-14 15:39:43 +0100175 }
176 else if (entry->es_type == ETYPE_SCRIPT)
LemonBoyeca7c602022-04-14 15:39:43 +0100177 return vim_strsave(entry->es_name);
LemonBoy6013d002022-04-09 21:42:10 +0100178 }
179 return NULL;
180 }
181
Bram Moolenaara5d04232020-07-26 15:37:02 +0200182 // Give information about each stack entry up to the root.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100183 // For a function we compose the call stack, as it was done in the past:
184 // "function One[123]..Two[456]..Three"
Bram Moolenaara5d04232020-07-26 15:37:02 +0200185 ga_init2(&ga, sizeof(char), 100);
186 for (idx = 0; idx < exestack.ga_len; ++idx)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100187 {
188 entry = ((estack_T *)exestack.ga_data) + idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200189 if (entry->es_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100190 {
Bram Moolenaara810db32020-09-11 17:59:23 +0200191 long lnum = 0;
LemonBoy0ffc17a2023-08-20 18:09:11 +0200192 char_u *type_name = (char_u *)"";
193 char_u *class_name = (char_u *)"";
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200194
Bram Moolenaara5d04232020-07-26 15:37:02 +0200195 if (entry->es_type != last_type)
196 {
197 switch (entry->es_type)
198 {
LemonBoy0ffc17a2023-08-20 18:09:11 +0200199 case ETYPE_SCRIPT: type_name = (char_u *)"script "; break;
200 case ETYPE_UFUNC: type_name = (char_u *)"function "; break;
201 default: type_name = (char_u *)""; break;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200202 }
203 last_type = entry->es_type;
204 }
LemonBoy0ffc17a2023-08-20 18:09:11 +0200205 if (entry->es_type == ETYPE_UFUNC && entry->es_info.ufunc->uf_class != NULL)
206 class_name = entry->es_info.ufunc->uf_class->class_name;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200207 if (idx == exestack.ga_len - 1)
208 lnum = which == ESTACK_STACK ? SOURCING_LNUM : 0;
209 else
210 lnum = entry->es_lnum;
LemonBoy0ffc17a2023-08-20 18:09:11 +0200211 len = STRLEN(entry->es_name) + STRLEN(type_name) + STRLEN(class_name) + 26;
212 if (ga_grow(&ga, (int)len) == FAIL)
213 break;
214 ga_concat(&ga, type_name);
215 if (*class_name != NUL)
216 {
217 // For class methods prepend "<class name>." to the function name.
Ernie Rael16cdfa62024-04-02 19:05:39 +0200218 ga_concat(&ga, (char_u *)"<SNR>");
219 ga.ga_len += vim_snprintf((char *)ga.ga_data + ga.ga_len, 23,
220 "%d_", entry->es_info.ufunc->uf_script_ctx.sc_sid);
LemonBoy0ffc17a2023-08-20 18:09:11 +0200221 ga_concat(&ga, class_name);
222 ga_append(&ga, '.');
223 }
224 ga_concat(&ga, entry->es_name);
225 // For the bottom entry of <sfile>: do not add the line number, it is used in
226 // <slnum>. Also leave it out when the number is not set.
227 if (lnum != 0)
228 ga.ga_len += vim_snprintf((char *)ga.ga_data + ga.ga_len, 23, "[%ld]",
229 lnum);
230 if (idx != exestack.ga_len - 1)
231 ga_concat(&ga, (char_u *)"..");
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100232 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100233 }
234
LemonBoy0ffc17a2023-08-20 18:09:11 +0200235 ga_append(&ga, '\0');
Bram Moolenaara5d04232020-07-26 15:37:02 +0200236 return (char_u *)ga.ga_data;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100237#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100238}
239
240/*
zeertzjq5c8771b2023-01-24 12:34:03 +0000241 * Get DIP_ flags from the [where] argument of a :runtime command.
242 * "*argp" is advanced to after the [where] argument if it is found.
zeertzjq3770f4c2023-01-22 18:38:51 +0000243 */
244 static int
zeertzjq5c8771b2023-01-24 12:34:03 +0000245get_runtime_cmd_flags(char_u **argp, size_t where_len)
zeertzjq3770f4c2023-01-22 18:38:51 +0000246{
247 char_u *arg = *argp;
zeertzjq3770f4c2023-01-22 18:38:51 +0000248
zeertzjq5c8771b2023-01-24 12:34:03 +0000249 if (where_len == 0)
zeertzjq3770f4c2023-01-22 18:38:51 +0000250 return 0;
251
zeertzjq5c8771b2023-01-24 12:34:03 +0000252 if (STRNCMP(arg, "START", where_len) == 0)
zeertzjq3770f4c2023-01-22 18:38:51 +0000253 {
zeertzjq5c8771b2023-01-24 12:34:03 +0000254 *argp = skipwhite(arg + where_len);
zeertzjq3770f4c2023-01-22 18:38:51 +0000255 return DIP_START + DIP_NORTP;
256 }
zeertzjq5c8771b2023-01-24 12:34:03 +0000257 if (STRNCMP(arg, "OPT", where_len) == 0)
zeertzjq3770f4c2023-01-22 18:38:51 +0000258 {
zeertzjq5c8771b2023-01-24 12:34:03 +0000259 *argp = skipwhite(arg + where_len);
zeertzjq3770f4c2023-01-22 18:38:51 +0000260 return DIP_OPT + DIP_NORTP;
261 }
zeertzjq5c8771b2023-01-24 12:34:03 +0000262 if (STRNCMP(arg, "PACK", where_len) == 0)
zeertzjq3770f4c2023-01-22 18:38:51 +0000263 {
zeertzjq5c8771b2023-01-24 12:34:03 +0000264 *argp = skipwhite(arg + where_len);
zeertzjq3770f4c2023-01-22 18:38:51 +0000265 return DIP_START + DIP_OPT + DIP_NORTP;
266 }
zeertzjq5c8771b2023-01-24 12:34:03 +0000267 if (STRNCMP(arg, "ALL", where_len) == 0)
zeertzjq3770f4c2023-01-22 18:38:51 +0000268 {
zeertzjq5c8771b2023-01-24 12:34:03 +0000269 *argp = skipwhite(arg + where_len);
zeertzjq3770f4c2023-01-22 18:38:51 +0000270 return DIP_START + DIP_OPT;
271 }
272
273 return 0;
274}
275
276/*
zeertzjq5c8771b2023-01-24 12:34:03 +0000277 * ":runtime [where] {name}"
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200278 */
279 void
280ex_runtime(exarg_T *eap)
281{
282 char_u *arg = eap->arg;
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200283 int flags = eap->forceit ? DIP_ALL : 0;
zeertzjq5c8771b2023-01-24 12:34:03 +0000284 char_u *p = skiptowhite(arg);
285 flags += get_runtime_cmd_flags(&arg, p - arg);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200286 source_runtime(arg, flags);
287}
288
zeertzjq3770f4c2023-01-22 18:38:51 +0000289static int runtime_expand_flags;
290
291/*
292 * Set the completion context for the :runtime command.
293 */
294 void
295set_context_in_runtime_cmd(expand_T *xp, char_u *arg)
296{
zeertzjq5c8771b2023-01-24 12:34:03 +0000297 char_u *p = skiptowhite(arg);
298 runtime_expand_flags
299 = *p != NUL ? get_runtime_cmd_flags(&arg, p - arg) : 0;
zeertzjqbe5cdd12023-08-17 23:48:58 +0200300 // Skip to the last argument.
301 while (*(p = skiptowhite_esc(arg)) != NUL)
302 {
303 if (runtime_expand_flags == 0)
304 // When there are multiple arguments and [where] is not specified,
305 // use an unrelated non-zero flag to avoid expanding [where].
306 runtime_expand_flags = DIP_ALL;
307 arg = skipwhite(p);
308 }
zeertzjq3770f4c2023-01-22 18:38:51 +0000309 xp->xp_context = EXPAND_RUNTIME;
310 xp->xp_pattern = arg;
311}
312
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200313 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100314source_callback(char_u *fname, void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200315{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100316 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200317}
318
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000319#ifdef FEAT_EVAL
320/*
321 * Find an already loaded script "name".
322 * If found returns its script ID. If not found returns -1.
323 */
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100324 int
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000325find_script_by_name(char_u *name)
326{
327 int sid;
328 scriptitem_T *si;
329
330 for (sid = script_items.ga_len; sid > 0; --sid)
331 {
332 // We used to check inode here, but that doesn't work:
333 // - If a script is edited and written, it may get a different
334 // inode number, even though to the user it is the same script.
335 // - If a script is deleted and another script is written, with a
336 // different name, the inode may be re-used.
337 si = SCRIPT_ITEM(sid);
338 if (si->sn_name != NULL && fnamecmp(si->sn_name, name) == 0)
339 return sid;
340 }
341 return -1;
342}
343
344/*
345 * Add a new scriptitem with all items initialized.
346 * When running out of memory "error" is set to FAIL.
347 * Returns the script ID.
348 */
349 static int
350get_new_scriptitem(int *error)
351{
352 static scid_T last_current_SID = 0;
353 int sid = ++last_current_SID;
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000354 scriptitem_T *si = NULL;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000355
356 if (ga_grow(&script_items, (int)(sid - script_items.ga_len)) == FAIL)
357 {
358 *error = FAIL;
359 return sid;
360 }
361 while (script_items.ga_len < sid)
362 {
363 si = ALLOC_CLEAR_ONE(scriptitem_T);
364 if (si == NULL)
365 {
366 *error = FAIL;
367 return sid;
368 }
369 ++script_items.ga_len;
370 SCRIPT_ITEM(script_items.ga_len) = si;
371 si->sn_name = NULL;
372 si->sn_version = 1;
373
374 // Allocate the local script variables to use for this script.
375 new_script_vars(script_items.ga_len);
376 ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
377 hash_init(&si->sn_all_vars.dv_hashtab);
378 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
379 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
380# ifdef FEAT_PROFILE
381 si->sn_prof_on = FALSE;
382# endif
383 }
384
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000385 // "si" can't be NULL, check only to avoid a compiler warning
386 if (si != NULL)
387 // Used to check script variable index is still valid.
388 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000389
390 return sid;
391}
392
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100393 int
394get_new_scriptitem_for_fname(int *error, char_u *fname)
395{
396 int sid = get_new_scriptitem(error);
397
398 if (*error == OK)
399 {
400 scriptitem_T *si = SCRIPT_ITEM(sid);
401
402 si->sn_name = vim_strsave(fname);
403 si->sn_state = SN_STATE_NOT_LOADED;
404 }
405 return sid;
406}
407
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000408 static void
409find_script_callback(char_u *fname, void *cookie)
410{
411 int sid;
412 int error = OK;
413 int *ret_sid = cookie;
414
415 sid = find_script_by_name(fname);
416 if (sid < 0)
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000417 // script does not exist yet, create a new scriptitem
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100418 sid = get_new_scriptitem_for_fname(&error, fname);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000419 *ret_sid = sid;
420}
421#endif
422
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200423/*
zeertzjq008c9152023-08-17 23:08:53 +0200424 * Find the patterns in "name" in all directories in "path" and invoke
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200425 * "callback(fname, cookie)".
zeertzjq008c9152023-08-17 23:08:53 +0200426 * "prefix" is prepended to each pattern in "name".
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200427 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
428 * When "flags" has DIP_DIR: find directories instead of files.
429 * When "flags" has DIP_ERR: give an error message if there is no match.
430 *
zeertzjq008c9152023-08-17 23:08:53 +0200431 * Return FAIL when no file could be sourced, OK otherwise.
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200432 */
433 int
434do_in_path(
435 char_u *path,
zeertzjq008c9152023-08-17 23:08:53 +0200436 char *prefix,
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200437 char_u *name,
438 int flags,
439 void (*callback)(char_u *fname, void *ck),
440 void *cookie)
441{
442 char_u *rtp;
443 char_u *np;
444 char_u *buf;
445 char_u *rtp_copy;
446 char_u *tail;
447 int num_files;
448 char_u **files;
449 int i;
450 int did_one = FALSE;
451#ifdef AMIGA
452 struct Process *proc = (struct Process *)FindTask(0L);
453 APTR save_winptr = proc->pr_WindowPtr;
454
455 // Avoid a requester here for a volume that doesn't exist.
456 proc->pr_WindowPtr = (APTR)-1L;
457#endif
458
459 // Make a copy of 'runtimepath'. Invoking the callback may change the
460 // value.
461 rtp_copy = vim_strsave(path);
462 buf = alloc(MAXPATHL);
463 if (buf != NULL && rtp_copy != NULL)
464 {
Bram Moolenaar647a5302020-05-03 17:01:24 +0200465 if (p_verbose > 10 && name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200466 {
467 verbose_enter();
zeertzjq008c9152023-08-17 23:08:53 +0200468 if (*prefix != NUL)
469 smsg(_("Searching for \"%s\" under \"%s\" in \"%s\""),
470 (char *)name, prefix, (char *)path);
471 else
472 smsg(_("Searching for \"%s\" in \"%s\""),
473 (char *)name, (char *)path);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200474 verbose_leave();
475 }
476
477 // Loop over all entries in 'runtimepath'.
478 rtp = rtp_copy;
479 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
480 {
481 size_t buflen;
482
483 // Copy the path from 'runtimepath' to buf[].
484 copy_option_part(&rtp, buf, MAXPATHL, ",");
485 buflen = STRLEN(buf);
486
487 // Skip after or non-after directories.
488 if (flags & (DIP_NOAFTER | DIP_AFTER))
489 {
490 int is_after = buflen >= 5
491 && STRCMP(buf + buflen - 5, "after") == 0;
492
493 if ((is_after && (flags & DIP_NOAFTER))
494 || (!is_after && (flags & DIP_AFTER)))
495 continue;
496 }
497
498 if (name == NULL)
499 {
500 (*callback)(buf, (void *) &cookie);
501 if (!did_one)
502 did_one = (cookie == NULL);
503 }
zeertzjq008c9152023-08-17 23:08:53 +0200504 else if (buflen + 2 + STRLEN(prefix) + STRLEN(name) < MAXPATHL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200505 {
506 add_pathsep(buf);
zeertzjq008c9152023-08-17 23:08:53 +0200507 STRCAT(buf, prefix);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200508 tail = buf + STRLEN(buf);
509
510 // Loop over all patterns in "name"
511 np = name;
512 while (*np != NUL && ((flags & DIP_ALL) || !did_one))
513 {
514 // Append the pattern from "name" to buf[].
515 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
516 "\t ");
517
Bram Moolenaar647a5302020-05-03 17:01:24 +0200518 if (p_verbose > 10)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200519 {
520 verbose_enter();
521 smsg(_("Searching for \"%s\""), buf);
522 verbose_leave();
523 }
524
525 // Expand wildcards, invoke the callback for each match.
526 if (gen_expand_wildcards(1, &buf, &num_files, &files,
527 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
528 {
529 for (i = 0; i < num_files; ++i)
530 {
531 (*callback)(files[i], cookie);
532 did_one = TRUE;
533 if (!(flags & DIP_ALL))
534 break;
535 }
536 FreeWild(num_files, files);
537 }
538 }
539 }
540 }
541 }
542 vim_free(buf);
543 vim_free(rtp_copy);
544 if (!did_one && name != NULL)
545 {
546 char *basepath = path == p_rtp ? "runtimepath" : "packpath";
547
548 if (flags & DIP_ERR)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000549 semsg(_(e_directory_not_found_in_str_str), basepath, name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200550 else if (p_verbose > 0)
551 {
552 verbose_enter();
553 smsg(_("not found in '%s': \"%s\""), basepath, name);
554 verbose_leave();
555 }
556 }
557
558#ifdef AMIGA
559 proc->pr_WindowPtr = save_winptr;
560#endif
561
562 return did_one ? OK : FAIL;
563}
564
565/*
566 * Find "name" in "path". When found, invoke the callback function for
567 * it: callback(fname, "cookie")
568 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first
569 * one is used.
570 * Returns OK when at least one match found, FAIL otherwise.
571 *
572 * If "name" is NULL calls callback for each entry in "path". Cookie is
573 * passed by reference in this case, setting it to NULL indicates that callback
574 * has done its job.
575 */
576 static int
577do_in_path_and_pp(
578 char_u *path,
579 char_u *name,
580 int flags,
581 void (*callback)(char_u *fname, void *ck),
582 void *cookie)
583{
584 int done = FAIL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200585
586 if ((flags & DIP_NORTP) == 0)
zeertzjq008c9152023-08-17 23:08:53 +0200587 done = do_in_path(path, "", name, flags, callback, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200588
589 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
zeertzjq008c9152023-08-17 23:08:53 +0200590 done = do_in_path(p_pp, "pack/*/start/*/", name, flags, callback,
591 cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200592
593 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
zeertzjq008c9152023-08-17 23:08:53 +0200594 done = do_in_path(p_pp, "pack/*/opt/*/", name, flags, callback,
595 cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200596
597 return done;
598}
599
600/*
601 * Just like do_in_path_and_pp(), using 'runtimepath' for "path".
602 */
603 int
604do_in_runtimepath(
605 char_u *name,
606 int flags,
607 void (*callback)(char_u *fname, void *ck),
608 void *cookie)
609{
610 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
611}
612
613/*
614 * Source the file "name" from all directories in 'runtimepath'.
615 * "name" can contain wildcards.
616 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
617 *
618 * return FAIL when no file could be sourced, OK otherwise.
619 */
620 int
621source_runtime(char_u *name, int flags)
622{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 return source_in_path(p_rtp, name, flags, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200624}
625
626/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000627 * Just like source_runtime(), but use "path" instead of 'runtimepath'
628 * and return the script ID in "ret_sid".
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200629 */
630 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100631source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200632{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100633 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200634}
635
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200636#if defined(FEAT_EVAL) || defined(PROTO)
637
638/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000639 * Find "name" in 'runtimepath'. If found a new scriptitem is created for it
640 * and it's script ID is returned.
641 * If not found returns -1.
642 */
643 int
644find_script_in_rtp(char_u *name)
645{
646 int sid = -1;
647
648 (void)do_in_path_and_pp(p_rtp, name, DIP_NOAFTER,
649 find_script_callback, &sid);
650 return sid;
651}
652
653/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200654 * Expand wildcards in "pat" and invoke do_source() for each match.
655 */
656 static void
657source_all_matches(char_u *pat)
658{
659 int num_files;
660 char_u **files;
661 int i;
662
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000663 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) != OK)
664 return;
665
666 for (i = 0; i < num_files; ++i)
667 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
668 FreeWild(num_files, files);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200669}
670
671/*
672 * Add the package directory to 'runtimepath'.
673 */
674 static int
675add_pack_dir_to_rtp(char_u *fname)
676{
677 char_u *p4, *p3, *p2, *p1, *p;
678 char_u *entry;
679 char_u *insp = NULL;
680 int c;
681 char_u *new_rtp;
682 int keep;
683 size_t oldlen;
684 size_t addlen;
685 size_t new_rtp_len;
686 char_u *afterdir = NULL;
687 size_t afterlen = 0;
688 char_u *after_insp = NULL;
689 char_u *ffname = NULL;
690 size_t fname_len;
691 char_u *buf = NULL;
692 char_u *rtp_ffname;
693 int match;
694 int retval = FAIL;
695
696 p4 = p3 = p2 = p1 = get_past_head(fname);
697 for (p = p1; *p; MB_PTR_ADV(p))
698 if (vim_ispathsep_nocolon(*p))
699 {
700 p4 = p3; p3 = p2; p2 = p1; p1 = p;
701 }
702
703 // now we have:
704 // rtp/pack/name/start/name
705 // p4 p3 p2 p1
706 //
707 // find the part up to "pack" in 'runtimepath'
708 c = *++p4; // append pathsep in order to expand symlink
709 *p4 = NUL;
710 ffname = fix_fname(fname);
711 *p4 = c;
712 if (ffname == NULL)
713 return FAIL;
714
715 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
716 // Also stop at the first "after" directory.
717 fname_len = STRLEN(ffname);
718 buf = alloc(MAXPATHL);
719 if (buf == NULL)
720 goto theend;
721 for (entry = p_rtp; *entry != NUL; )
722 {
723 char_u *cur_entry = entry;
724
725 copy_option_part(&entry, buf, MAXPATHL, ",");
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200726
727 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
728 && p > buf
729 && vim_ispathsep(p[-1])
730 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
731 {
732 if (insp == NULL)
733 // Did not find "ffname" before the first "after" directory,
734 // insert it before this entry.
735 insp = cur_entry;
736 after_insp = cur_entry;
737 break;
738 }
zeertzjq39c9ec12023-04-01 13:52:03 +0100739
740 if (insp == NULL)
741 {
742 add_pathsep(buf);
743 rtp_ffname = fix_fname(buf);
744 if (rtp_ffname == NULL)
745 goto theend;
746 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
747 vim_free(rtp_ffname);
748 if (match)
749 // Insert "ffname" after this entry (and comma).
750 insp = entry;
751 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200752 }
753
754 if (insp == NULL)
755 // Both "fname" and "after" not found, append at the end.
756 insp = p_rtp + STRLEN(p_rtp);
757
758 // check if rtp/pack/name/start/name/after exists
759 afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
760 if (afterdir != NULL && mch_isdir(afterdir))
761 afterlen = STRLEN(afterdir) + 1; // add one for comma
762
763 oldlen = STRLEN(p_rtp);
764 addlen = STRLEN(fname) + 1; // add one for comma
765 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
766 if (new_rtp == NULL)
767 goto theend;
768
769 // We now have 'rtp' parts: {keep}{keep_after}{rest}.
770 // Create new_rtp, first: {keep},{fname}
771 keep = (int)(insp - p_rtp);
772 mch_memmove(new_rtp, p_rtp, keep);
773 new_rtp_len = keep;
774 if (*insp == NUL)
775 new_rtp[new_rtp_len++] = ','; // add comma before
776 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
777 new_rtp_len += addlen - 1;
778 if (*insp != NUL)
779 new_rtp[new_rtp_len++] = ','; // add comma after
780
781 if (afterlen > 0 && after_insp != NULL)
782 {
783 int keep_after = (int)(after_insp - p_rtp);
784
785 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
786 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
787 keep_after - keep);
788 new_rtp_len += keep_after - keep;
789 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
790 new_rtp_len += afterlen - 1;
791 new_rtp[new_rtp_len++] = ',';
792 keep = keep_after;
793 }
794
795 if (p_rtp[keep] != NUL)
796 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
797 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
798 else
799 new_rtp[new_rtp_len] = NUL;
800
801 if (afterlen > 0 && after_insp == NULL)
802 {
803 // Append afterdir when "after" was not found:
804 // {keep},{fname}{rest},{afterdir}
805 STRCAT(new_rtp, ",");
806 STRCAT(new_rtp, afterdir);
807 }
808
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100809 set_option_value_give_err((char_u *)"rtp", 0L, new_rtp, 0);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200810 vim_free(new_rtp);
811 retval = OK;
812
813theend:
814 vim_free(buf);
815 vim_free(ffname);
816 vim_free(afterdir);
817 return retval;
818}
819
820/*
821 * Load scripts in "plugin" and "ftdetect" directories of the package.
822 */
823 static int
824load_pack_plugin(char_u *fname)
825{
826 static char *plugpat = "%s/plugin/**/*.vim";
827 static char *ftpat = "%s/ftdetect/*.vim";
828 int len;
829 char_u *ffname = fix_fname(fname);
830 char_u *pat = NULL;
831 int retval = FAIL;
832
833 if (ffname == NULL)
834 return FAIL;
835 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
836 pat = alloc(len);
837 if (pat == NULL)
838 goto theend;
839 vim_snprintf((char *)pat, len, plugpat, ffname);
840 source_all_matches(pat);
841
842 {
843 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
844
845 // If runtime/filetype.vim wasn't loaded yet, the scripts will be
846 // found when it loads.
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100847 if (cmd != NULL && eval_to_number(cmd, FALSE) > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200848 {
849 do_cmdline_cmd((char_u *)"augroup filetypedetect");
850 vim_snprintf((char *)pat, len, ftpat, ffname);
851 source_all_matches(pat);
852 do_cmdline_cmd((char_u *)"augroup END");
853 }
854 vim_free(cmd);
855 }
856 vim_free(pat);
857 retval = OK;
858
859theend:
860 vim_free(ffname);
861 return retval;
862}
863
864// used for "cookie" of add_pack_plugin()
865static int APP_ADD_DIR;
866static int APP_LOAD;
867static int APP_BOTH;
868
869 static void
870add_pack_plugin(char_u *fname, void *cookie)
871{
872 if (cookie != &APP_LOAD)
873 {
874 char_u *buf = alloc(MAXPATHL);
875 char_u *p;
876 int found = FALSE;
877
878 if (buf == NULL)
879 return;
880 p = p_rtp;
881 while (*p != NUL)
882 {
883 copy_option_part(&p, buf, MAXPATHL, ",");
884 if (pathcmp((char *)buf, (char *)fname, -1) == 0)
885 {
886 found = TRUE;
887 break;
888 }
889 }
890 vim_free(buf);
891 if (!found)
892 // directory is not yet in 'runtimepath', add it
893 if (add_pack_dir_to_rtp(fname) == FAIL)
894 return;
895 }
896
897 if (cookie != &APP_ADD_DIR)
898 load_pack_plugin(fname);
899}
900
901/*
902 * Add all packages in the "start" directory to 'runtimepath'.
903 */
904 void
905add_pack_start_dirs(void)
906{
zeertzjq008c9152023-08-17 23:08:53 +0200907 do_in_path(p_pp, "", (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200908 add_pack_plugin, &APP_ADD_DIR);
909}
910
911/*
912 * Load plugins from all packages in the "start" directory.
913 */
914 void
915load_start_packages(void)
916{
917 did_source_packages = TRUE;
zeertzjq008c9152023-08-17 23:08:53 +0200918 do_in_path(p_pp, "", (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200919 add_pack_plugin, &APP_LOAD);
920}
921
922/*
923 * ":packloadall"
924 * Find plugins in the package directories and source them.
925 */
926 void
927ex_packloadall(exarg_T *eap)
928{
929 if (!did_source_packages || eap->forceit)
930 {
931 // First do a round to add all directories to 'runtimepath', then load
932 // the plugins. This allows for plugins to use an autoload directory
933 // of another plugin.
934 add_pack_start_dirs();
935 load_start_packages();
936 }
937}
938
939/*
940 * ":packadd[!] {name}"
941 */
942 void
943ex_packadd(exarg_T *eap)
944{
945 static char *plugpat = "pack/*/%s/%s";
946 int len;
947 char *pat;
948 int round;
949 int res = OK;
950
951 // Round 1: use "start", round 2: use "opt".
952 for (round = 1; round <= 2; ++round)
953 {
954 // Only look under "start" when loading packages wasn't done yet.
955 if (round == 1 && did_source_packages)
956 continue;
957
958 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
959 pat = alloc(len);
960 if (pat == NULL)
961 return;
962 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
963 // The first round don't give a "not found" error, in the second round
964 // only when nothing was found in the first round.
zeertzjq008c9152023-08-17 23:08:53 +0200965 res = do_in_path(p_pp, "", (char_u *)pat,
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200966 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
967 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
968 vim_free(pat);
969 }
970}
971#endif
972
973/*
Bram Moolenaar26262f82019-09-04 20:59:15 +0200974 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
975 * list of file names in allocated memory.
976 */
977 void
978remove_duplicates(garray_T *gap)
979{
980 int i;
981 int j;
982 char_u **fnames = (char_u **)gap->ga_data;
983
984 sort_strings(fnames, gap->ga_len);
985 for (i = gap->ga_len - 1; i > 0; --i)
986 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
987 {
988 vim_free(fnames[i]);
989 for (j = i + 1; j < gap->ga_len; ++j)
990 fnames[j - 1] = fnames[j];
991 --gap->ga_len;
992 }
993}
994
zeertzjq5c8771b2023-01-24 12:34:03 +0000995 static void
996ExpandRTDir_int(
997 char_u *pat,
998 size_t pat_len,
999 int flags,
1000 int keep_ext,
1001 garray_T *gap,
1002 char *dirnames[])
1003{
1004 for (int i = 0; dirnames[i] != NULL; ++i)
1005 {
1006 size_t buf_len = STRLEN(dirnames[i]) + pat_len + 22;
1007 char *buf = alloc(buf_len);
1008 if (buf == NULL)
1009 {
1010 ga_clear_strings(gap);
1011 return;
1012 }
1013 char *tail = buf + 15;
1014 size_t tail_buflen = buf_len - 15;
1015 int glob_flags = 0;
1016 int expand_dirs = FALSE;
1017
1018 if (*(dirnames[i]) == NUL) // empty dir used for :runtime
1019 vim_snprintf(tail, tail_buflen, "%s*.vim", pat);
1020 else
1021 vim_snprintf(tail, tail_buflen, "%s/%s*.vim", dirnames[i], pat);
1022
1023expand:
1024 if ((flags & DIP_NORTP) == 0)
1025 globpath(p_rtp, (char_u *)tail, gap, glob_flags, expand_dirs);
1026
1027 if (flags & DIP_START)
1028 {
1029 memcpy(tail - 15, "pack/*/start/*/", 15);
1030 globpath(p_pp, (char_u *)tail - 15, gap, glob_flags, expand_dirs);
1031 }
1032
1033 if (flags & DIP_OPT)
1034 {
1035 memcpy(tail - 13, "pack/*/opt/*/", 13);
1036 globpath(p_pp, (char_u *)tail - 13, gap, glob_flags, expand_dirs);
1037 }
1038
1039 if (*(dirnames[i]) == NUL && !expand_dirs)
1040 {
1041 // expand dir names in another round
1042 vim_snprintf(tail, tail_buflen, "%s*", pat);
1043 glob_flags = WILD_ADD_SLASH;
1044 expand_dirs = TRUE;
1045 goto expand;
1046 }
1047
1048 vim_free(buf);
1049 }
1050
1051 int pat_pathsep_cnt = 0;
1052 for (size_t i = 0; i < pat_len; ++i)
1053 if (vim_ispathsep(pat[i]))
1054 ++pat_pathsep_cnt;
1055
1056 for (int i = 0; i < gap->ga_len; ++i)
1057 {
1058 char_u *match = ((char_u **)gap->ga_data)[i];
1059 char_u *s = match;
1060 char_u *e = s + STRLEN(s);
1061 if (e - 4 > s && !keep_ext && STRNICMP(e - 4, ".vim", 4) == 0)
1062 {
1063 e -= 4;
1064 *e = NUL;
1065 }
1066
1067 int match_pathsep_cnt = (e > s && e[-1] == '/') ? -1 : 0;
1068 for (s = e; s > match; MB_PTR_BACK(match, s))
1069 if (s < match || (vim_ispathsep(*s)
1070 && ++match_pathsep_cnt > pat_pathsep_cnt))
1071 break;
1072 ++s;
1073 if (s != match)
1074 mch_memmove(match, s, e - s + 1);
1075 }
1076
1077 if (gap->ga_len == 0)
1078 return;
1079
1080 // Sort and remove duplicates which can happen when specifying multiple
1081 // directories in dirnames.
1082 remove_duplicates(gap);
1083}
1084
Bram Moolenaar26262f82019-09-04 20:59:15 +02001085/*
zeertzjq3770f4c2023-01-22 18:38:51 +00001086 * Expand runtime file names.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001087 * Search from 'runtimepath':
1088 * 'runtimepath'/{dirnames}/{pat}.vim
zeertzjq3770f4c2023-01-22 18:38:51 +00001089 * When "flags" has DIP_START: search also from "start" of 'packpath':
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001090 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
zeertzjq3770f4c2023-01-22 18:38:51 +00001091 * When "flags" has DIP_OPT: search also from "opt" of 'packpath':
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001092 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
1093 * "dirnames" is an array with one or more directory names.
1094 */
1095 int
1096ExpandRTDir(
1097 char_u *pat,
1098 int flags,
1099 int *num_file,
1100 char_u ***file,
1101 char *dirnames[])
1102{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001103 *num_file = 0;
1104 *file = NULL;
zeertzjq5c8771b2023-01-24 12:34:03 +00001105
1106 garray_T ga;
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001107 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001108
zeertzjq5c8771b2023-01-24 12:34:03 +00001109 ExpandRTDir_int(pat, STRLEN(pat), flags, FALSE, &ga, dirnames);
1110
1111 if (ga.ga_len == 0)
1112 return FAIL;
1113
1114 *file = ga.ga_data;
1115 *num_file = ga.ga_len;
1116 return OK;
1117}
1118
1119/*
1120 * Handle command line completion for :runtime command.
1121 */
1122 int
1123expand_runtime_cmd(char_u *pat, int *numMatches, char_u ***matches)
1124{
1125 *numMatches = 0;
1126 *matches = NULL;
1127
1128 garray_T ga;
1129 ga_init2(&ga, sizeof(char *), 10);
1130
1131 size_t pat_len = (int)STRLEN(pat);
1132 char *dirnames[] = {"", NULL};
1133 ExpandRTDir_int(pat, pat_len, runtime_expand_flags, TRUE, &ga, dirnames);
1134
1135 // Try to complete values for [where] argument when none was found.
1136 if (runtime_expand_flags == 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001137 {
zeertzjq5c8771b2023-01-24 12:34:03 +00001138 char *where_values[] = {"START", "OPT", "PACK", "ALL"};
1139 for (size_t i = 0; i < ARRAY_LENGTH(where_values); ++i)
1140 if (STRNCMP(pat, where_values[i], pat_len) == 0)
1141 {
1142 char_u *p = vim_strsave((char_u *)where_values[i]);
1143 if (p != NULL && ga_add_string(&ga, p) == FAIL)
1144 vim_free(p);
1145 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001146 }
1147
1148 if (ga.ga_len == 0)
1149 return FAIL;
1150
zeertzjq5c8771b2023-01-24 12:34:03 +00001151 *matches = ga.ga_data;
1152 *numMatches = ga.ga_len;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001153 return OK;
1154}
1155
1156/*
1157 * Expand loadplugin names:
1158 * 'packpath'/pack/ * /opt/{pat}
1159 */
1160 int
1161ExpandPackAddDir(
1162 char_u *pat,
1163 int *num_file,
1164 char_u ***file)
1165{
1166 char_u *s;
1167 char_u *e;
1168 char_u *match;
1169 garray_T ga;
1170 int i;
1171 int pat_len;
1172
1173 *num_file = 0;
1174 *file = NULL;
1175 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001176 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001177
1178 s = alloc(pat_len + 26);
1179 if (s == NULL)
1180 {
1181 ga_clear_strings(&ga);
1182 return FAIL;
1183 }
1184 sprintf((char *)s, "pack/*/opt/%s*", pat);
zeertzjq3770f4c2023-01-22 18:38:51 +00001185 globpath(p_pp, s, &ga, 0, TRUE);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001186 vim_free(s);
1187
1188 for (i = 0; i < ga.ga_len; ++i)
1189 {
1190 match = ((char_u **)ga.ga_data)[i];
1191 s = gettail(match);
1192 e = s + STRLEN(s);
1193 mch_memmove(match, s, e - s + 1);
1194 }
1195
1196 if (ga.ga_len == 0)
1197 return FAIL;
1198
1199 // Sort and remove duplicates which can happen when specifying multiple
1200 // directories in dirnames.
1201 remove_duplicates(&ga);
1202
1203 *file = ga.ga_data;
1204 *num_file = ga.ga_len;
1205 return OK;
1206}
1207
1208 static void
1209cmd_source(char_u *fname, exarg_T *eap)
1210{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001211 int clearvars = FALSE;
1212
1213 if (*fname != NUL && STRNCMP(fname, "++clear", 7) == 0)
1214 {
1215 // ++clear argument is supplied
1216 clearvars = TRUE;
1217 fname = fname + 7;
1218 if (*fname != NUL)
1219 {
1220 semsg(_(e_invalid_argument_str), eap->arg);
1221 return;
1222 }
1223 }
1224
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001225 if (*fname != NUL && eap != NULL && eap->addr_count > 0)
1226 {
1227 // if a filename is specified to :source, then a range is not allowed
1228 emsg(_(e_no_range_allowed));
1229 return;
1230 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001231
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001232 if (eap != NULL && *fname == NUL)
1233 {
1234 if (eap->forceit)
1235 // a file name is needed to source normal mode commands
1236 emsg(_(e_argument_required));
1237 else
1238 // source ex commands from the current buffer
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001239 do_source_ext(NULL, FALSE, FALSE, NULL, eap, clearvars);
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001240 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001241 else if (eap != NULL && eap->forceit)
1242 // ":source!": read Normal mode commands
1243 // Need to execute the commands directly. This is required at least
1244 // for:
1245 // - ":g" command busy
1246 // - after ":argdo", ":windo" or ":bufdo"
1247 // - another command follows
1248 // - inside a loop
1249 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
1250#ifdef FEAT_EVAL
1251 || eap->cstack->cs_idx >= 0
1252#endif
1253 );
1254
1255 // ":source" read ex commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001256 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001257 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001258}
1259
1260/*
1261 * ":source {fname}"
1262 */
1263 void
1264ex_source(exarg_T *eap)
1265{
1266#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02001267 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001268 {
1269 char_u *fname = NULL;
1270
1271 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
1272 NULL, NULL,
1273 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1274 if (fname != NULL)
1275 {
1276 cmd_source(fname, eap);
1277 vim_free(fname);
1278 }
1279 }
1280 else
1281#endif
1282 cmd_source(eap->arg, eap);
1283}
1284
1285#if defined(FEAT_EVAL) || defined(PROTO)
1286/*
1287 * ":options"
1288 */
1289 void
1290ex_options(
1291 exarg_T *eap UNUSED)
1292{
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001293 char_u buf[500];
1294 int multi_mods = 0;
1295
1296 buf[0] = NUL;
dundargocc57b5bc2022-11-02 13:30:51 +00001297 (void)add_win_cmd_modifiers(buf, &cmdmod, &multi_mods);
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001298
1299 vim_setenv((char_u *)"OPTWIN_CMD", buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001300 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
1301}
1302#endif
1303
1304/*
1305 * ":source" and associated commands.
1306 */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001307
zeertzjq2d68b722023-03-30 21:50:37 +01001308#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001309/*
1310 * Return the address holding the next breakpoint line for a source cookie.
1311 */
1312 linenr_T *
1313source_breakpoint(void *cookie)
1314{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001315 return &((source_cookie_T *)cookie)->breakpoint;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001316}
1317
1318/*
1319 * Return the address holding the debug tick for a source cookie.
1320 */
1321 int *
1322source_dbg_tick(void *cookie)
1323{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001324 return &((source_cookie_T *)cookie)->dbg_tick;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001325}
1326
1327/*
1328 * Return the nesting level for a source cookie.
1329 */
1330 int
1331source_level(void *cookie)
1332{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001333 return ((source_cookie_T *)cookie)->level;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001334}
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001335
1336/*
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02001337 * Return the readahead line. Note that the pointer may become invalid when
1338 * getting the next line, if it's concatenated with the next one.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001339 */
1340 char_u *
1341source_nextline(void *cookie)
1342{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001343 return ((source_cookie_T *)cookie)->nextline;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001344}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001345#endif
1346
1347#if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC)
1348# define USE_FOPEN_NOINH
1349/*
1350 * Special function to open a file without handle inheritance.
1351 * When possible the handle is closed on exec().
1352 */
1353 static FILE *
1354fopen_noinh_readbin(char *filename)
1355{
1356# ifdef MSWIN
1357 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
1358# else
1359 int fd_tmp = mch_open(filename, O_RDONLY, 0);
1360# endif
1361
1362 if (fd_tmp == -1)
1363 return NULL;
1364
1365# ifdef HAVE_FD_CLOEXEC
1366 {
1367 int fdflags = fcntl(fd_tmp, F_GETFD);
1368 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1369 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
1370 }
1371# endif
1372
1373 return fdopen(fd_tmp, READBIN);
1374}
1375#endif
1376
1377/*
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001378 * Initialization for sourcing lines from the current buffer. Reads all the
1379 * lines from the buffer and stores it in the cookie grow array.
1380 * Returns a pointer to the name ":source buffer=<n>" on success and NULL on
1381 * failure.
1382 */
1383 static char_u *
1384do_source_buffer_init(source_cookie_T *sp, exarg_T *eap)
1385{
1386 linenr_T curr_lnum;
1387 char_u *line = NULL;
1388 char_u *fname;
1389
1390 CLEAR_FIELD(*sp);
1391
1392 if (curbuf == NULL)
1393 return NULL;
1394
1395 // Use ":source buffer=<num>" as the script name
1396 vim_snprintf((char *)IObuff, IOSIZE, ":source buffer=%d", curbuf->b_fnum);
1397 fname = vim_strsave(IObuff);
1398 if (fname == NULL)
1399 return NULL;
1400
1401 ga_init2(&sp->buflines, sizeof(char_u *), 100);
1402
1403 // Copy the lines from the buffer into a grow array
1404 for (curr_lnum = eap->line1; curr_lnum <= eap->line2; curr_lnum++)
1405 {
1406 line = vim_strsave(ml_get(curr_lnum));
1407 if (line == NULL)
1408 goto errret;
1409 if (ga_add_string(&sp->buflines, line) == FAIL)
1410 goto errret;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001411 }
1412 sp->buf_lnum = 0;
1413 sp->source_from_buf = TRUE;
1414
1415 return fname;
1416
1417errret:
1418 vim_free(fname);
1419 vim_free(line);
1420 ga_clear_strings(&sp->buflines);
1421 return NULL;
1422}
1423
1424/*
1425 * Read the file "fname" and execute its lines as EX commands.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001426 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1427 * again.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001428 *
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001429 * The "eap" argument is used when sourcing lines from a buffer instead of a
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001430 * file.
1431 *
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001432 * If "clearvars" is TRUE, then for scripts which are loaded more than
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001433 * once, clear all the functions and variables previously defined in that
1434 * script.
1435 *
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001436 * This function may be called recursively!
1437 *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438 * Return FAIL if file could not be opened, OK otherwise.
1439 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001440 */
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001441 static int
1442do_source_ext(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001443 char_u *fname,
1444 int check_other, // check for .vimrc and _vimrc
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445 int is_vimrc, // DOSO_ value
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001446 int *ret_sid UNUSED,
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001447 exarg_T *eap,
1448 int clearvars UNUSED)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001449{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001450 source_cookie_T cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001451 char_u *p;
Bram Moolenaar753885b2022-08-24 16:30:36 +01001452 char_u *fname_not_fixed = NULL;
Bram Moolenaar5214b292022-08-24 17:32:35 +01001453 char_u *fname_exp = NULL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001454 char_u *firstline = NULL;
1455 int retval = FAIL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001456 sctx_T save_current_sctx;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001457#ifdef STARTUPTIME
1458 struct timeval tv_rel;
1459 struct timeval tv_start;
1460#endif
1461#ifdef FEAT_PROFILE
1462 proftime_T wait_start;
1463#endif
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001464 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001465 int trigger_source_post = FALSE;
ichizok7e5fe382023-04-15 13:17:50 +01001466#ifdef FEAT_EVAL
1467 funccal_entry_T funccalp_entry;
1468 int save_debug_break_level = debug_break_level;
1469 int sid = -1;
1470 scriptitem_T *si = NULL;
1471 int save_estack_compiling = estack_compiling;
1472 ESTACK_CHECK_DECLARATION;
1473#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001474
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001475 CLEAR_FIELD(cookie);
1476 if (fname == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001477 {
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001478 // sourcing lines from a buffer
1479 fname_exp = do_source_buffer_init(&cookie, eap);
1480 if (fname_exp == NULL)
1481 return FAIL;
1482 }
1483 else
1484 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01001485 fname_not_fixed = expand_env_save(fname);
1486 if (fname_not_fixed == NULL)
1487 goto theend;
1488 fname_exp = fix_fname(fname_not_fixed);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001489 if (fname_exp == NULL)
Bram Moolenaar753885b2022-08-24 16:30:36 +01001490 goto theend;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001491 if (mch_isdir(fname_exp))
1492 {
1493 smsg(_("Cannot source a directory: \"%s\""), fname);
1494 goto theend;
1495 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001496 }
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001497#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001498 estack_compiling = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001499
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500 // See if we loaded this script before.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001501 sid = find_script_by_name(fname_exp);
Bram Moolenaarf479cac2022-01-12 12:54:55 +00001502 if (sid > 0 && ret_sid != NULL
1503 && SCRIPT_ITEM(sid)->sn_state != SN_STATE_NOT_LOADED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 {
1505 // Already loaded and no need to load again, return here.
1506 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001507 retval = OK;
1508 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509 }
1510#endif
1511
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001512 // Apply SourceCmd autocommands, they should get the file and source it.
1513 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1514 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1515 FALSE, curbuf))
1516 {
1517#ifdef FEAT_EVAL
1518 retval = aborting() ? FAIL : OK;
1519#else
1520 retval = OK;
1521#endif
1522 if (retval == OK)
1523 // Apply SourcePost autocommands.
1524 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1525 FALSE, curbuf);
1526 goto theend;
1527 }
1528
1529 // Apply SourcePre autocommands, they may get the file.
1530 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1531
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001532 if (!cookie.source_from_buf)
1533 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001534#ifdef USE_FOPEN_NOINH
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001535 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001536#else
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001537 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001538#endif
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001539 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001540 if (cookie.fp == NULL && check_other)
1541 {
1542 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1543 // and ".exrc" by "_exrc" or vice versa.
1544 p = gettail(fname_exp);
1545 if ((*p == '.' || *p == '_')
1546 && (STRICMP(p + 1, "vimrc") == 0
1547 || STRICMP(p + 1, "gvimrc") == 0
1548 || STRICMP(p + 1, "exrc") == 0))
1549 {
1550 if (*p == '_')
1551 *p = '.';
1552 else
1553 *p = '_';
1554#ifdef USE_FOPEN_NOINH
1555 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1556#else
1557 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1558#endif
1559 }
1560 }
1561
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001562 if (cookie.fp == NULL && !cookie.source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001563 {
1564 if (p_verbose > 0)
1565 {
1566 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001567 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001568 smsg(_("could not source \"%s\""), fname);
1569 else
1570 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001571 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001572 verbose_leave();
1573 }
1574 goto theend;
1575 }
1576
1577 // The file exists.
1578 // - In verbose mode, give a message.
1579 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1580 if (p_verbose > 1)
1581 {
1582 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001583 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001584 smsg(_("sourcing \"%s\""), fname);
1585 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001586 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001587 verbose_leave();
1588 }
1589 if (is_vimrc == DOSO_VIMRC)
1590 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1591 else if (is_vimrc == DOSO_GVIMRC)
1592 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1593
1594#ifdef USE_CRNL
1595 // If no automatic file format: Set default to CR-NL.
1596 if (*p_ffs == NUL)
1597 cookie.fileformat = EOL_DOS;
1598 else
1599 cookie.fileformat = EOL_UNKNOWN;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001600#endif
1601
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001602 if (fname == NULL)
1603 // When sourcing a range of lines from a buffer, use the buffer line
1604 // number.
1605 cookie.sourcing_lnum = eap->line1 - 1;
1606 else
1607 cookie.sourcing_lnum = 0;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001608
1609#ifdef FEAT_EVAL
1610 // Check if this script has a breakpoint.
1611 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1612 cookie.fname = fname_exp;
1613 cookie.dbg_tick = debug_tick;
1614
1615 cookie.level = ex_nesting_level;
1616#endif
1617
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001618#ifdef STARTUPTIME
1619 if (time_fd != NULL)
1620 time_push(&tv_rel, &tv_start);
1621#endif
1622
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001623 // "legacy" does not apply to commands in the script
1624 sticky_cmdmod_flags = 0;
1625
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001626 save_current_sctx = current_sctx;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001627 if (cmdmod.cmod_flags & CMOD_VIM9CMD)
1628 // When the ":vim9cmd" command modifier is used, source the script as a
1629 // Vim9 script.
1630 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1631 else
1632 current_sctx.sc_version = 1; // default script version
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001633
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001634#ifdef FEAT_EVAL
Bram Moolenaara2942c72023-01-02 13:41:49 +00001635 current_sctx.sc_lnum = 0;
1636
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001637# ifdef FEAT_PROFILE
1638 if (do_profiling == PROF_YES)
1639 prof_child_enter(&wait_start); // entering a child now
1640# endif
1641
1642 // Don't use local function variables, if called from a function.
1643 // Also starts profiling timer for nested script.
1644 save_funccal(&funccalp_entry);
1645
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001646 // Reset "KeyTyped" to avoid some commands thinking they are invoked
1647 // interactively. E.g. defining a function would output indent.
1648 int save_KeyTyped = KeyTyped;
1649 KeyTyped = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001650
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001651 // Check if this script was sourced before to find its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001652 // Always use a new sequence number.
1653 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001655 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001656 hashtab_T *ht;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001657 int todo;
1658 hashitem_T *hi;
1659 dictitem_T *di;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001660
1661 // loading the same script again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001662 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001663 si = SCRIPT_ITEM(sid);
1664 if (si->sn_state == SN_STATE_NOT_LOADED)
1665 {
1666 // this script was found but not loaded yet
1667 si->sn_state = SN_STATE_NEW;
1668 }
1669 else
1670 {
1671 si->sn_state = SN_STATE_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001673 if (!clearvars)
1674 {
1675 // Script-local variables remain but "const" can be set again.
1676 // In Vim9 script variables will be cleared when "vim9script"
1677 // is encountered without the "noclear" argument.
1678 ht = &SCRIPT_VARS(sid);
1679 todo = (int)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001680 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001681 if (!HASHITEM_EMPTY(hi))
1682 {
1683 --todo;
1684 di = HI2DI(hi);
1685 di->di_flags |= DI_FLAGS_RELOAD;
1686 }
1687 // imports can be redefined once
1688 mark_imports_for_reload(sid);
1689 }
1690 else
1691 clear_vim9_scriptlocal_vars(sid);
Bram Moolenaar0123cc12021-02-07 17:17:58 +01001692
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001693 // reset version, "vim9script" may have been added or removed.
1694 si->sn_version = 1;
1695 }
Yegappan Lakshmananfa630082024-03-10 19:22:38 +01001696 if (ret_sid != NULL)
1697 *ret_sid = sid;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001698 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001700 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001701 int error = OK;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001702
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001703 // It's new, generate a new SID and initialize the scriptitem.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001704 sid = get_new_scriptitem(&error);
1705 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001706 if (error == FAIL)
1707 goto almosttheend;
Bram Moolenaar753885b2022-08-24 16:30:36 +01001708 si = SCRIPT_ITEM(sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001709 si->sn_name = fname_exp;
1710 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 if (ret_sid != NULL)
Bram Moolenaar753885b2022-08-24 16:30:36 +01001712 *ret_sid = sid;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001713
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001714 // Remember the "is_vimrc" flag for when the file is sourced again.
1715 si->sn_is_vimrc = is_vimrc;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001716 }
1717
zeertzjq5a4fff42022-08-15 17:53:55 +01001718 // Keep the sourcing name/lnum, for recursive calls.
1719 estack_push(ETYPE_SCRIPT, si->sn_name, 0);
ichizok7e5fe382023-04-15 13:17:50 +01001720 ESTACK_CHECK_SETUP;
zeertzjq5a4fff42022-08-15 17:53:55 +01001721
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001722# ifdef FEAT_PROFILE
1723 if (do_profiling == PROF_YES)
1724 {
1725 int forceit;
1726
1727 // Check if we do profiling for this script.
Ernie Rael21d32122023-09-02 15:09:18 +02001728 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit, NULL))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001729 {
1730 script_do_profile(si);
1731 si->sn_pr_force = forceit;
1732 }
1733 if (si->sn_prof_on)
1734 {
1735 ++si->sn_pr_count;
1736 profile_start(&si->sn_pr_start);
1737 profile_zero(&si->sn_pr_children);
1738 }
1739 }
1740# endif
Bram Moolenaaraeef1f72022-09-15 12:20:18 +01001741#else
1742 // Keep the sourcing name/lnum, for recursive calls.
1743 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001744#endif
1745
1746 cookie.conv.vc_type = CONV_NONE; // no conversion
1747
1748 // Read the first line so we can check for a UTF-8 BOM.
1749 firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1750 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1751 && firstline[1] == 0xbb && firstline[2] == 0xbf)
1752 {
1753 // Found BOM; setup conversion, skip over BOM and recode the line.
1754 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1755 p = string_convert(&cookie.conv, firstline + 3, NULL);
1756 if (p == NULL)
1757 p = vim_strsave(firstline + 3);
1758 if (p != NULL)
1759 {
1760 vim_free(firstline);
1761 firstline = p;
1762 }
1763 }
1764
1765 // Call do_cmdline, which will call getsourceline() to get the lines.
1766 do_cmdline(firstline, getsourceline, (void *)&cookie,
1767 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1768 retval = OK;
1769
1770#ifdef FEAT_PROFILE
1771 if (do_profiling == PROF_YES)
1772 {
1773 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001774 si = SCRIPT_ITEM(sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001775 if (si->sn_prof_on)
1776 {
1777 profile_end(&si->sn_pr_start);
1778 profile_sub_wait(&wait_start, &si->sn_pr_start);
1779 profile_add(&si->sn_pr_total, &si->sn_pr_start);
1780 profile_self(&si->sn_pr_self, &si->sn_pr_start,
1781 &si->sn_pr_children);
1782 }
1783 }
1784#endif
1785
1786 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001787 emsg(_(e_interrupted));
Bram Moolenaar88774872022-08-16 20:24:29 +01001788#ifdef FEAT_EVAL
ichizok7e5fe382023-04-15 13:17:50 +01001789 ESTACK_CHECK_NOW;
Bram Moolenaar88774872022-08-16 20:24:29 +01001790#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001791 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001792 if (p_verbose > 1)
1793 {
1794 verbose_enter();
1795 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001796 if (SOURCING_NAME != NULL)
1797 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001798 verbose_leave();
1799 }
1800#ifdef STARTUPTIME
1801 if (time_fd != NULL)
1802 {
1803 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1804 time_msg((char *)IObuff, &tv_start);
1805 time_pop(&tv_rel);
1806 }
1807#endif
1808
1809 if (!got_int)
1810 trigger_source_post = TRUE;
1811
1812#ifdef FEAT_EVAL
1813 // After a "finish" in debug mode, need to break at first command of next
1814 // sourced file.
1815 if (save_debug_break_level > ex_nesting_level
1816 && debug_break_level == ex_nesting_level)
1817 ++debug_break_level;
1818#endif
1819
1820#ifdef FEAT_EVAL
1821almosttheend:
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001822 // If "sn_save_cpo" is set that means we encountered "vim9script": restore
1823 // 'cpoptions', unless in the main .vimrc file.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001825 si = SCRIPT_ITEM(sid);
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001826 if (si->sn_save_cpo != NULL && si->sn_is_vimrc == DOSO_NONE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827 {
Bram Moolenaar3e191692021-03-17 17:46:00 +01001828 if (STRCMP(p_cpo, CPO_VIM) != 0)
1829 {
1830 char_u *f;
1831 char_u *t;
1832
1833 // 'cpo' was changed in the script. Apply the same change to the
1834 // saved value, if possible.
1835 for (f = (char_u *)CPO_VIM; *f != NUL; ++f)
1836 if (vim_strchr(p_cpo, *f) == NULL
1837 && (t = vim_strchr(si->sn_save_cpo, *f)) != NULL)
1838 // flag was removed, also remove it from the saved 'cpo'
1839 mch_memmove(t, t + 1, STRLEN(t));
1840 for (f = p_cpo; *f != NUL; ++f)
1841 if (vim_strchr((char_u *)CPO_VIM, *f) == NULL
1842 && vim_strchr(si->sn_save_cpo, *f) == NULL)
1843 {
1844 // flag was added, also add it to the saved 'cpo'
1845 t = alloc(STRLEN(si->sn_save_cpo) + 2);
1846 if (t != NULL)
1847 {
1848 *t = *f;
1849 STRCPY(t + 1, si->sn_save_cpo);
1850 vim_free(si->sn_save_cpo);
1851 si->sn_save_cpo = t;
1852 }
1853 }
1854 }
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001855 set_option_value_give_err((char_u *)"cpo",
1856 0L, si->sn_save_cpo, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857 }
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001858 VIM_CLEAR(si->sn_save_cpo);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001859
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001860 restore_funccal();
1861# ifdef FEAT_PROFILE
1862 if (do_profiling == PROF_YES)
1863 prof_child_exit(&wait_start); // leaving a child now
1864# endif
Bram Moolenaara2942c72023-01-02 13:41:49 +00001865
1866 KeyTyped = save_KeyTyped;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001867#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001868 current_sctx = save_current_sctx;
1869
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001870 if (cookie.fp != NULL)
1871 fclose(cookie.fp);
1872 if (cookie.source_from_buf)
1873 ga_clear_strings(&cookie.buflines);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001874 vim_free(cookie.nextline);
1875 vim_free(firstline);
1876 convert_setup(&cookie.conv, NULL, NULL);
1877
1878 if (trigger_source_post)
1879 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1880
1881theend:
Bram Moolenaar0af2ecf2022-08-24 17:05:56 +01001882#ifdef FEAT_EVAL
Bram Moolenaar753885b2022-08-24 16:30:36 +01001883 if (sid > 0 && ret_sid != NULL
1884 && fname_not_fixed != NULL && fname_exp != NULL)
1885 {
1886 int not_fixed_sid = find_script_by_name(fname_not_fixed);
1887
1888 // If "fname_not_fixed" is a symlink then we source the linked file.
1889 // If the original name is in the script list we add the ID of the
1890 // script that was actually sourced.
1891 if (SCRIPT_ID_VALID(not_fixed_sid) && not_fixed_sid != sid)
1892 SCRIPT_ITEM(not_fixed_sid)->sn_sourced_sid = sid;
1893 }
Bram Moolenaar0af2ecf2022-08-24 17:05:56 +01001894#endif
Bram Moolenaar753885b2022-08-24 16:30:36 +01001895
1896 vim_free(fname_not_fixed);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001897 vim_free(fname_exp);
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001898 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001899#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001900 estack_compiling = save_estack_compiling;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001901#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001902 return retval;
1903}
1904
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001905 int
1906do_source(
1907 char_u *fname,
1908 int check_other, // check for .vimrc and _vimrc
1909 int is_vimrc, // DOSO_ value
Bram Moolenaar753885b2022-08-24 16:30:36 +01001910 int *ret_sid)
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001911{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001912 return do_source_ext(fname, check_other, is_vimrc, ret_sid, NULL, FALSE);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001913}
1914
1915
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001916#if defined(FEAT_EVAL) || defined(PROTO)
1917
1918/*
1919 * ":scriptnames"
1920 */
1921 void
1922ex_scriptnames(exarg_T *eap)
1923{
1924 int i;
1925
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001926 if (eap->addr_count > 0 || *eap->arg != NUL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001927 {
1928 // :script {scriptId}: edit the script
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001929 if (eap->addr_count > 0 && !SCRIPT_ID_VALID(eap->line2))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001930 emsg(_(e_invalid_argument));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001931 else
1932 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001933 if (eap->addr_count > 0)
1934 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
1935 else
1936 {
1937 expand_env(eap->arg, NameBuff, MAXPATHL);
1938 eap->arg = NameBuff;
1939 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001940 do_exedit(eap, NULL);
1941 }
1942 return;
1943 }
1944
1945 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar6079da72022-01-18 14:16:59 +00001946 {
1947 scriptitem_T *si = SCRIPT_ITEM(i);
1948
1949 if (si->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001950 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01001951 char sourced_buf[20];
1952
Bram Moolenaar6079da72022-01-18 14:16:59 +00001953 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar753885b2022-08-24 16:30:36 +01001954 if (si->sn_sourced_sid > 0)
1955 vim_snprintf(sourced_buf, 20, "->%d", si->sn_sourced_sid);
1956 else
1957 sourced_buf[0] = NUL;
1958 vim_snprintf((char *)IObuff, IOSIZE, "%3d%s%s: %s",
Bram Moolenaar6079da72022-01-18 14:16:59 +00001959 i,
Bram Moolenaar753885b2022-08-24 16:30:36 +01001960 sourced_buf,
Bram Moolenaar6079da72022-01-18 14:16:59 +00001961 si->sn_state == SN_STATE_NOT_LOADED ? " A" : "",
1962 NameBuff);
Bram Moolenaar769f5892022-02-09 14:31:05 +00001963 if (!message_filtered(IObuff))
1964 {
1965 msg_putchar('\n');
1966 msg_outtrans(IObuff);
1967 out_flush(); // output one line at a time
1968 ui_breakcheck();
1969 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001970 }
Bram Moolenaar6079da72022-01-18 14:16:59 +00001971 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001972}
1973
1974# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1975/*
1976 * Fix slashes in the list of script names for 'shellslash'.
1977 */
1978 void
1979scriptnames_slash_adjust(void)
1980{
1981 int i;
1982
1983 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001984 if (SCRIPT_ITEM(i)->sn_name != NULL)
1985 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001986}
1987# endif
1988
1989/*
1990 * Get a pointer to a script name. Used for ":verbose set".
Bram Moolenaar74667062020-12-28 15:41:41 +01001991 * Message appended to "Last set from "
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001992 */
1993 char_u *
1994get_scriptname(scid_T id)
1995{
1996 if (id == SID_MODELINE)
1997 return (char_u *)_("modeline");
1998 if (id == SID_CMDARG)
1999 return (char_u *)_("--cmd argument");
2000 if (id == SID_CARG)
2001 return (char_u *)_("-c argument");
2002 if (id == SID_ENV)
2003 return (char_u *)_("environment variable");
2004 if (id == SID_ERROR)
2005 return (char_u *)_("error handler");
Bram Moolenaar74667062020-12-28 15:41:41 +01002006 if (id == SID_WINLAYOUT)
2007 return (char_u *)_("changed window size");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002008 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002009}
2010
2011# if defined(EXITFREE) || defined(PROTO)
2012 void
2013free_scriptnames(void)
2014{
2015 int i;
2016
2017 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02002018 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002019 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002021 // the variables themselves are cleared in evalvars_clear()
2022 vim_free(si->sn_vars);
2023
2024 vim_free(si->sn_name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02002025 free_imports_and_script_vars(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002026 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02002027# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002028 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02002029# endif
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002030 vim_free(si->sn_autoload_prefix);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002031 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02002032 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002033 ga_clear(&script_items);
2034}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002035
2036 void
2037free_autoload_scriptnames(void)
2038{
2039 ga_clear_strings(&ga_loaded);
2040}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002041# endif
2042
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002043 linenr_T
Bram Moolenaar66250c92020-08-20 15:02:42 +02002044get_sourced_lnum(
2045 char_u *(*fgetline)(int, void *, int, getline_opt_T),
2046 void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002047{
2048 return fgetline == getsourceline
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002049 ? ((source_cookie_T *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002050 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002051}
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002052
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002053/*
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002054 * Return a List of script-local functions defined in the script with id
2055 * 'sid'.
2056 */
2057 static list_T *
2058get_script_local_funcs(scid_T sid)
2059{
2060 hashtab_T *functbl;
2061 hashitem_T *hi;
2062 long_u todo;
2063 list_T *l;
2064
2065 l = list_alloc();
2066 if (l == NULL)
2067 return NULL;
2068
2069 // Iterate through all the functions in the global function hash table
2070 // looking for functions with script ID 'sid'.
2071 functbl = func_tbl_get();
2072 todo = functbl->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002073 FOR_ALL_HASHTAB_ITEMS(functbl, hi, todo)
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002074 {
2075 ufunc_T *fp;
2076
2077 if (HASHITEM_EMPTY(hi))
2078 continue;
2079
2080 --todo;
2081 fp = HI2UF(hi);
2082
2083 // Add active functions with script id == 'sid'
2084 if (!(fp->uf_flags & FC_DEAD) && (fp->uf_script_ctx.sc_sid == sid))
2085 {
2086 char_u *name;
2087
2088 if (fp->uf_name_exp != NULL)
2089 name = fp->uf_name_exp;
2090 else
2091 name = fp->uf_name;
2092
2093 list_append_string(l, name, -1);
2094 }
2095 }
2096
2097 return l;
2098}
2099
2100/*
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002101 * getscriptinfo() function
2102 */
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002103 void
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002104f_getscriptinfo(typval_T *argvars, typval_T *rettv)
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002105{
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002106 list_T *l;
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002107 char_u *pat = NULL;
2108 regmatch_T regmatch;
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002109 int filterpat = FALSE;
2110 scid_T sid = -1;
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002111
2112 if (rettv_list_alloc(rettv) == FAIL)
2113 return;
2114
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002115 if (check_for_opt_dict_arg(argvars, 0) == FAIL)
2116 return;
2117
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002118 l = rettv->vval.v_list;
2119
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002120 regmatch.regprog = NULL;
2121 regmatch.rm_ic = p_ic;
2122
2123 if (argvars[0].v_type == VAR_DICT)
2124 {
zeertzjq2d68b722023-03-30 21:50:37 +01002125 dictitem_T *sid_di = dict_find(argvars[0].vval.v_dict,
2126 (char_u *)"sid", 3);
2127 if (sid_di != NULL)
2128 {
2129 int error = FALSE;
2130 sid = tv_get_number_chk(&sid_di->di_tv, &error);
2131 if (error)
2132 return;
2133 if (sid <= 0)
2134 {
zeertzjq276410e2023-05-07 21:59:33 +01002135 semsg(_(e_invalid_value_for_argument_str_str), "sid",
zeertzjq2d68b722023-03-30 21:50:37 +01002136 tv_get_string(&sid_di->di_tv));
2137 return;
2138 }
2139 }
2140 else
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002141 {
2142 pat = dict_get_string(argvars[0].vval.v_dict, "name", TRUE);
2143 if (pat != NULL)
2144 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2145 if (regmatch.regprog != NULL)
2146 filterpat = TRUE;
2147 }
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002148 }
2149
zeertzjq2d68b722023-03-30 21:50:37 +01002150 for (varnumber_T i = sid > 0 ? sid : 1;
2151 (i == sid || sid <= 0) && i <= script_items.ga_len; ++i)
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002152 {
2153 scriptitem_T *si = SCRIPT_ITEM(i);
2154 dict_T *d;
2155
2156 if (si->sn_name == NULL)
2157 continue;
2158
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002159 if (filterpat && !vim_regexec(&regmatch, si->sn_name, (colnr_T)0))
2160 continue;
2161
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002162 if ((d = dict_alloc()) == NULL
2163 || list_append_dict(l, d) == FAIL
2164 || dict_add_string(d, "name", si->sn_name) == FAIL
2165 || dict_add_number(d, "sid", i) == FAIL
Bram Moolenaar753885b2022-08-24 16:30:36 +01002166 || dict_add_number(d, "sourced", si->sn_sourced_sid) == FAIL
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002167 || dict_add_number(d, "version", si->sn_version) == FAIL
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002168 || dict_add_bool(d, "autoload",
2169 si->sn_state == SN_STATE_NOT_LOADED) == FAIL)
2170 return;
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002171
zeertzjq2d68b722023-03-30 21:50:37 +01002172 // When a script ID is specified, return information about only the
2173 // specified script, and add the script-local variables and functions.
2174 if (sid > 0)
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002175 {
2176 dict_T *var_dict;
2177
2178 var_dict = dict_copy(&si->sn_vars->sv_dict, TRUE, TRUE,
2179 get_copyID());
2180 if (var_dict == NULL
2181 || dict_add_dict(d, "variables", var_dict) == FAIL
2182 || dict_add_list(d, "functions",
2183 get_script_local_funcs(sid)) == FAIL)
2184 return;
2185 }
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002186 }
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002187
2188 vim_regfree(regmatch.regprog);
2189 vim_free(pat);
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002190}
2191
Dominique Pelle748b3082022-01-08 12:41:16 +00002192#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002193
2194 static char_u *
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002195get_one_sourceline(source_cookie_T *sp)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002196{
2197 garray_T ga;
2198 int len;
2199 int c;
2200 char_u *buf;
2201#ifdef USE_CRNL
2202 int has_cr; // CR-LF found
2203#endif
2204 int have_read = FALSE;
2205
2206 // use a growarray to store the sourced line
2207 ga_init2(&ga, 1, 250);
2208
2209 // Loop until there is a finished line (or end-of-file).
2210 ++sp->sourcing_lnum;
2211 for (;;)
2212 {
2213 // make room to read at least 120 (more) characters
2214 if (ga_grow(&ga, 120) == FAIL)
2215 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002216 if (sp->source_from_buf)
2217 {
2218 if (sp->buf_lnum >= sp->buflines.ga_len)
2219 break; // all the lines are processed
2220 ga_concat(&ga, ((char_u **)sp->buflines.ga_data)[sp->buf_lnum]);
2221 sp->buf_lnum++;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01002222 if (ga_grow(&ga, 1) == FAIL)
2223 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002224 buf = (char_u *)ga.ga_data;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01002225 buf[ga.ga_len++] = NUL;
Bram Moolenaar4748c4b2022-05-17 17:47:07 +01002226 len = ga.ga_len;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002227 }
2228 else
2229 {
2230 buf = (char_u *)ga.ga_data;
2231 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
2232 sp->fp) == NULL)
2233 break;
Bram Moolenaar4748c4b2022-05-17 17:47:07 +01002234 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002235 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002236#ifdef USE_CRNL
2237 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
2238 // CTRL-Z by its own, or after a NL.
2239 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
2240 && sp->fileformat == EOL_DOS
2241 && buf[len - 1] == Ctrl_Z)
2242 {
2243 buf[len - 1] = NUL;
2244 break;
2245 }
2246#endif
2247
2248 have_read = TRUE;
2249 ga.ga_len = len;
2250
2251 // If the line was longer than the buffer, read more.
2252 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
2253 continue;
2254
2255 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
2256 {
2257#ifdef USE_CRNL
2258 has_cr = (len >= 2 && buf[len - 2] == '\r');
2259 if (sp->fileformat == EOL_UNKNOWN)
2260 {
2261 if (has_cr)
2262 sp->fileformat = EOL_DOS;
2263 else
2264 sp->fileformat = EOL_UNIX;
2265 }
2266
2267 if (sp->fileformat == EOL_DOS)
2268 {
2269 if (has_cr) // replace trailing CR
2270 {
2271 buf[len - 2] = '\n';
2272 --len;
2273 --ga.ga_len;
2274 }
2275 else // lines like ":map xx yy^M" will have failed
2276 {
2277 if (!sp->error)
2278 {
2279 msg_source(HL_ATTR(HLF_W));
2280 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
2281 }
2282 sp->error = TRUE;
2283 sp->fileformat = EOL_UNIX;
2284 }
2285 }
2286#endif
2287 // The '\n' is escaped if there is an odd number of ^V's just
2288 // before it, first set "c" just before the 'V's and then check
2289 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
2290 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
2291 ;
2292 if ((len & 1) != (c & 1)) // escaped NL, read more
2293 {
2294 ++sp->sourcing_lnum;
2295 continue;
2296 }
2297
2298 buf[len - 1] = NUL; // remove the NL
2299 }
2300
2301 // Check for ^C here now and then, so recursive :so can be broken.
2302 line_breakcheck();
2303 break;
2304 }
2305
2306 if (have_read)
2307 return (char_u *)ga.ga_data;
2308
2309 vim_free(ga.ga_data);
2310 return NULL;
2311}
2312
2313/*
2314 * Get one full line from a sourced file.
2315 * Called by do_cmdline() when it's called from do_source().
2316 *
2317 * Return a pointer to the line in allocated memory.
2318 * Return NULL for end-of-file or some error.
2319 */
2320 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02002321getsourceline(
2322 int c UNUSED,
2323 void *cookie,
2324 int indent UNUSED,
2325 getline_opt_T options)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002326{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002327 source_cookie_T *sp = (source_cookie_T *)cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002328 char_u *line;
2329 char_u *p;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002330 int do_vim9_all = in_vim9script()
2331 && options == GETLINE_CONCAT_ALL;
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002332 int do_bar_cont = do_vim9_all
2333 || options == GETLINE_CONCAT_CONTBAR;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002334
2335#ifdef FEAT_EVAL
2336 // If breakpoints have been added/deleted need to check for it.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002337 if ((sp->dbg_tick < debug_tick) && !sp->source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002338 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002339 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002340 sp->dbg_tick = debug_tick;
2341 }
2342# ifdef FEAT_PROFILE
2343 if (do_profiling == PROF_YES)
2344 script_line_end();
2345# endif
2346#endif
2347
2348 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002349 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002350
2351 // Get current line. If there is a read-ahead line, use it, otherwise get
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002352 // one now. "fp" is NULL if actually using a string.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002353 if (sp->finished || (!sp->source_from_buf && sp->fp == NULL))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002354 line = NULL;
2355 else if (sp->nextline == NULL)
2356 line = get_one_sourceline(sp);
2357 else
2358 {
2359 line = sp->nextline;
2360 sp->nextline = NULL;
2361 ++sp->sourcing_lnum;
2362 }
2363#ifdef FEAT_PROFILE
2364 if (line != NULL && do_profiling == PROF_YES)
2365 script_line_start();
2366#endif
2367
2368 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
2369 // contain the 'C' flag.
Bram Moolenaar66250c92020-08-20 15:02:42 +02002370 if (line != NULL && options != GETLINE_NONE
2371 && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002372 {
Bram Moolenaar5072b472021-06-03 21:56:10 +02002373 int comment_char = in_vim9script() ? '#' : '"';
2374
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002375 // compensate for the one line read-ahead
2376 --sp->sourcing_lnum;
2377
2378 // Get the next line and concatenate it when it starts with a
2379 // backslash. We always need to read the next line, keep it in
2380 // sp->nextline.
2381 /* Also check for a comment in between continuation lines: "\ */
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002382 // Also check for a Vim9 comment, empty line, line starting with '|',
2383 // but not "||".
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002384 sp->nextline = get_one_sourceline(sp);
2385 if (sp->nextline != NULL
2386 && (*(p = skipwhite(sp->nextline)) == '\\'
Bram Moolenaar5072b472021-06-03 21:56:10 +02002387 || (p[0] == comment_char
2388 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002389 || (do_vim9_all && (*p == NUL
2390 || vim9_comment_start(p)))
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002391 || (do_bar_cont && p[0] == '|' && p[1] != '|')))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002392 {
2393 garray_T ga;
2394
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002395 ga_init2(&ga, sizeof(char_u), 400);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002396 ga_concat(&ga, line);
2397 if (*p == '\\')
2398 ga_concat(&ga, p + 1);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002399 else if (*p == '|')
2400 {
2401 ga_concat(&ga, (char_u *)" ");
2402 ga_concat(&ga, p);
2403 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002404 for (;;)
2405 {
2406 vim_free(sp->nextline);
2407 sp->nextline = get_one_sourceline(sp);
2408 if (sp->nextline == NULL)
2409 break;
2410 p = skipwhite(sp->nextline);
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002411 if (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|'))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002412 {
2413 // Adjust the growsize to the current length to speed up
2414 // concatenating many lines.
2415 if (ga.ga_len > 400)
2416 {
2417 if (ga.ga_len > 8000)
2418 ga.ga_growsize = 8000;
2419 else
2420 ga.ga_growsize = ga.ga_len;
2421 }
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002422 if (*p == '\\')
2423 ga_concat(&ga, p + 1);
2424 else
2425 {
2426 ga_concat(&ga, (char_u *)" ");
2427 ga_concat(&ga, p);
2428 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002429 }
Bram Moolenaar5072b472021-06-03 21:56:10 +02002430 else if (!(p[0] == (comment_char)
Bram Moolenaar0f37e352021-06-02 15:28:15 +02002431 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002432 && !(do_vim9_all && (*p == NUL || vim9_comment_start(p))))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002433 break;
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002434 /* drop a # comment or "\ comment line */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002435 }
2436 ga_append(&ga, NUL);
2437 vim_free(line);
2438 line = ga.ga_data;
2439 }
2440 }
2441
2442 if (line != NULL && sp->conv.vc_type != CONV_NONE)
2443 {
2444 char_u *s;
2445
2446 // Convert the encoding of the script line.
2447 s = string_convert(&sp->conv, line, NULL);
2448 if (s != NULL)
2449 {
2450 vim_free(line);
2451 line = s;
2452 }
2453 }
2454
2455#ifdef FEAT_EVAL
2456 // Did we encounter a breakpoint?
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002457 if (!sp->source_from_buf && sp->breakpoint != 0
2458 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002459 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002460 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002461 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002462 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002463 sp->dbg_tick = debug_tick;
2464 }
2465#endif
2466
2467 return line;
2468}
2469
2470/*
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002471 * Returns TRUE if sourcing a script either from a file or a buffer.
2472 * Otherwise returns FALSE.
2473 */
2474 int
2475sourcing_a_script(exarg_T *eap)
2476{
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002477 return (getline_equal(eap->ea_getline, eap->cookie, getsourceline));
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002478}
2479
2480/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002481 * ":scriptencoding": Set encoding conversion for a sourced script.
2482 */
2483 void
2484ex_scriptencoding(exarg_T *eap)
2485{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002486 source_cookie_T *sp;
2487 char_u *name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002488
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002489 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002490 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00002491 emsg(_(e_scriptencoding_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002492 return;
2493 }
2494
2495 if (*eap->arg != NUL)
2496 {
2497 name = enc_canonize(eap->arg);
2498 if (name == NULL) // out of memory
2499 return;
2500 }
2501 else
2502 name = eap->arg;
2503
2504 // Setup for conversion from the specified encoding to 'encoding'.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002505 sp = (source_cookie_T *)getline_cookie(eap->ea_getline, eap->cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002506 convert_setup(&sp->conv, name, p_enc);
2507
2508 if (name != eap->arg)
2509 vim_free(name);
2510}
2511
2512/*
2513 * ":scriptversion": Set Vim script version for a sourced script.
2514 */
2515 void
2516ex_scriptversion(exarg_T *eap UNUSED)
2517{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002518 int nr;
2519
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002520 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002521 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002522 emsg(_(e_scriptversion_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002523 return;
2524 }
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002525 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002527 emsg(_(e_cannot_use_scriptversion_after_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 return;
2529 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002530
2531 nr = getdigits(&eap->arg);
2532 if (nr == 0 || *eap->arg != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002533 emsg(_(e_invalid_argument));
Bram Moolenaar67979662020-06-20 22:50:47 +02002534 else if (nr > SCRIPT_VERSION_MAX)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002535 semsg(_(e_scriptversion_not_supported_nr), nr);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002536 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002537 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002538 current_sctx.sc_version = nr;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002539#ifdef FEAT_EVAL
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002540 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002541#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002542 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002543}
2544
2545#if defined(FEAT_EVAL) || defined(PROTO)
2546/*
2547 * ":finish": Mark a sourced file as finished.
2548 */
2549 void
2550ex_finish(exarg_T *eap)
2551{
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002552 if (sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002553 do_finish(eap, FALSE);
2554 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00002555 emsg(_(e_finish_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002556}
2557
2558/*
2559 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
2560 * Also called for a pending finish at the ":endtry" or after returning from
2561 * an extra do_cmdline(). "reanimate" is used in the latter case.
2562 */
2563 void
2564do_finish(exarg_T *eap, int reanimate)
2565{
2566 int idx;
2567
2568 if (reanimate)
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002569 ((source_cookie_T *)getline_cookie(eap->ea_getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002570 eap->cookie))->finished = FALSE;
2571
2572 // Cleanup (and inactivate) conditionals, but stop when a try conditional
2573 // not in its finally clause (which then is to be executed next) is found.
2574 // In this case, make the ":finish" pending for execution at the ":endtry".
2575 // Otherwise, finish normally.
2576 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
2577 if (idx >= 0)
2578 {
2579 eap->cstack->cs_pending[idx] = CSTP_FINISH;
2580 report_make_pending(CSTP_FINISH, NULL);
2581 }
2582 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002583 ((source_cookie_T *)getline_cookie(eap->ea_getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002584 eap->cookie))->finished = TRUE;
2585}
2586
2587
2588/*
2589 * Return TRUE when a sourced file had the ":finish" command: Don't give error
2590 * message for missing ":endif".
2591 * Return FALSE when not sourcing a file.
2592 */
2593 int
2594source_finished(
Bram Moolenaar66250c92020-08-20 15:02:42 +02002595 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002596 void *cookie)
2597{
2598 return (getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002599 && ((source_cookie_T *)getline_cookie(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002600 fgetline, cookie))->finished);
2601}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002602
2603/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002604 * Find the path of a script below the "autoload" directory.
2605 * Returns NULL if there is no "/autoload/" in the script name.
2606 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01002607 static char_u *
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002608script_name_after_autoload(scriptitem_T *si)
2609{
2610 char_u *p = si->sn_name;
2611 char_u *res = NULL;
2612
2613 for (;;)
2614 {
2615 char_u *n = (char_u *)strstr((char *)p, "autoload");
2616
2617 if (n == NULL)
2618 break;
2619 if (n > p && vim_ispathsep(n[-1]) && vim_ispathsep(n[8]))
2620 res = n + 9;
2621 p = n + 8;
2622 }
2623 return res;
2624}
2625
2626/*
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002627 * For an autoload script "autoload/dir/script.vim" return the prefix
2628 * "dir#script#" in allocated memory.
2629 * Returns NULL if anything is wrong.
2630 */
2631 char_u *
2632get_autoload_prefix(scriptitem_T *si)
2633{
2634 char_u *p = script_name_after_autoload(si);
2635 char_u *prefix;
2636
2637 if (p == NULL)
2638 return NULL;
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002639 prefix = vim_strsave(p);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002640 if (prefix == NULL)
2641 return NULL;
2642
2643 // replace all '/' with '#' and locate ".vim" at the end
2644 for (p = prefix; *p != NUL; p += mb_ptr2len(p))
2645 {
2646 if (vim_ispathsep(*p))
2647 *p = '#';
2648 else if (STRCMP(p, ".vim") == 0)
2649 {
2650 p[0] = '#';
2651 p[1] = NUL;
2652 return prefix;
2653 }
2654 }
2655
2656 // did not find ".vim" at the end
2657 vim_free(prefix);
2658 return NULL;
2659}
2660
2661/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002662 * If in a Vim9 autoload script return "name" with the autoload prefix for the
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002663 * script. If successful the returned name is allocated.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002664 * Otherwise it returns "name" unmodified.
2665 */
2666 char_u *
2667may_prefix_autoload(char_u *name)
2668{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002669 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2670 return name;
2671
2672 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2673
2674 if (si->sn_autoload_prefix == NULL)
2675 return name;
2676
2677 char_u *basename = name;
2678 size_t len;
2679 char_u *res;
2680
2681 if (*name == K_SPECIAL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002682 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002683 char_u *p = vim_strchr(name, '_');
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002684
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002685 // skip over "<SNR>99_"
2686 if (p != NULL)
2687 basename = p + 1;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002688 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002689
2690 len = STRLEN(si->sn_autoload_prefix) + STRLEN(basename) + 2;
2691 res = alloc(len);
2692 if (res == NULL)
2693 return NULL;
2694
2695 vim_snprintf((char *)res, len, "%s%s", si->sn_autoload_prefix, basename);
2696 return res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002697}
2698
2699/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002700 * Return the autoload script name for a function or variable name.
2701 * Returns NULL when out of memory.
2702 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
2703 */
2704 char_u *
2705autoload_name(char_u *name)
2706{
2707 char_u *p, *q = NULL;
2708 char_u *scriptname;
2709
2710 // Get the script file name: replace '#' with '/', append ".vim".
2711 scriptname = alloc(STRLEN(name) + 14);
2712 if (scriptname == NULL)
2713 return NULL;
2714 STRCPY(scriptname, "autoload/");
Bram Moolenaara1773442020-08-12 15:21:22 +02002715 STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002716 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
2717 q = p, ++p)
2718 *p = '/';
2719 STRCPY(q, ".vim");
2720 return scriptname;
2721}
2722
2723/*
2724 * If "name" has a package name try autoloading the script for it.
2725 * Return TRUE if a package was loaded.
2726 */
2727 int
2728script_autoload(
2729 char_u *name,
2730 int reload) // load script again when already loaded
2731{
2732 char_u *p;
2733 char_u *scriptname, *tofree;
2734 int ret = FALSE;
2735 int i;
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002736 int ret_sid;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002737
Bram Moolenaar89445512022-04-14 12:58:23 +01002738 // If the name starts with "<SNR>123_" then "123" is the script ID.
2739 if (name[0] == K_SPECIAL && name[1] == KS_EXTRA && name[2] == KE_SNR)
2740 {
2741 p = name + 3;
2742 ret_sid = (int)getdigits(&p);
2743 if (*p == '_' && SCRIPT_ID_VALID(ret_sid))
2744 {
2745 may_load_script(ret_sid, &ret);
2746 return ret;
2747 }
2748 }
2749
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002750 // If there is no '#' after name[0] there is no package name.
2751 p = vim_strchr(name, AUTOLOAD_CHAR);
2752 if (p == NULL || p == name)
2753 return FALSE;
2754
2755 tofree = scriptname = autoload_name(name);
2756 if (scriptname == NULL)
2757 return FALSE;
2758
2759 // Find the name in the list of previously loaded package names. Skip
2760 // "autoload/", it's always the same.
2761 for (i = 0; i < ga_loaded.ga_len; ++i)
2762 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2763 break;
2764 if (!reload && i < ga_loaded.ga_len)
2765 ret = FALSE; // was loaded already
2766 else
2767 {
2768 // Remember the name if it wasn't loaded already.
2769 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2770 {
2771 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2772 tofree = NULL;
2773 }
2774
2775 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002776 // Use "ret_sid" to avoid loading the same script again.
=?UTF-8?q?Bj=C3=B6rn=20Linse?=223a9502022-01-31 17:26:05 +00002777 if (source_in_path(p_rtp, scriptname, DIP_START, &ret_sid) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002778 ret = TRUE;
2779 }
2780
2781 vim_free(tofree);
2782 return ret;
2783}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002784#endif