blob: 9535cef553c198a4f6f5855a500e9b6412adc653 [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
ichizok663d18d2025-01-02 18:06:00 +0100240#ifdef FEAT_EVAL
241 static void
242stacktrace_push_item(
243 list_T *l,
244 ufunc_T *fp,
245 char_u *event,
246 linenr_T lnum,
247 char_u *filepath)
248{
249 dict_T *d;
250 typval_T tv;
251
252 d = dict_alloc_lock(VAR_FIXED);
253 if (d == NULL)
254 return;
255
256 tv.v_type = VAR_DICT;
257 tv.v_lock = VAR_LOCKED;
258 tv.vval.v_dict = d;
259
260 if (fp != NULL)
261 dict_add_func(d, "funcref", fp);
262 if (event != NULL)
263 dict_add_string(d, "event", event);
264 dict_add_number(d, "lnum", lnum);
265 dict_add_string(d, "filepath", filepath);
266
267 list_append_tv(l, &tv);
268}
269
270/*
271 * Create the stacktrace from exestack.
272 */
273 list_T *
274stacktrace_create(void)
275{
276 list_T *l;
277 int i;
278
279 l = list_alloc();
280 if (l == NULL)
281 return NULL;
282
283 for (i = 0; i < exestack.ga_len; ++i)
284 {
285 estack_T *entry = &((estack_T *)exestack.ga_data)[i];
286 linenr_T lnum = entry->es_lnum;
287
288 if (entry->es_type == ETYPE_SCRIPT)
289 stacktrace_push_item(l, NULL, NULL, lnum, entry->es_name);
290 else if (entry->es_type == ETYPE_UFUNC)
291 {
292 ufunc_T *fp = entry->es_info.ufunc;
293 sctx_T sctx = fp->uf_script_ctx;
294 char_u *filepath = sctx.sc_sid > 0 ?
295 get_scriptname(sctx.sc_sid) : (char_u *)"";
296
297 lnum += sctx.sc_lnum;
298 stacktrace_push_item(l, fp, NULL, lnum, filepath);
299 }
300 else if (entry->es_type == ETYPE_AUCMD)
301 {
302 sctx_T sctx = *acp_script_ctx(entry->es_info.aucmd);
303 char_u *filepath = sctx.sc_sid > 0 ?
304 get_scriptname(sctx.sc_sid) : (char_u *)"";
305
306 lnum += sctx.sc_lnum;
307 stacktrace_push_item(l, NULL, entry->es_name, lnum, filepath);
308 }
309 }
310 return l;
311}
312
313/*
314 * getstacktrace() function
315 */
316 void
317f_getstacktrace(typval_T *argvars UNUSED, typval_T *rettv)
318{
319 rettv_list_set(rettv, stacktrace_create());
320}
321#endif
322
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100323/*
zeertzjq5c8771b2023-01-24 12:34:03 +0000324 * Get DIP_ flags from the [where] argument of a :runtime command.
325 * "*argp" is advanced to after the [where] argument if it is found.
zeertzjq3770f4c2023-01-22 18:38:51 +0000326 */
327 static int
zeertzjq5c8771b2023-01-24 12:34:03 +0000328get_runtime_cmd_flags(char_u **argp, size_t where_len)
zeertzjq3770f4c2023-01-22 18:38:51 +0000329{
330 char_u *arg = *argp;
zeertzjq3770f4c2023-01-22 18:38:51 +0000331
zeertzjq5c8771b2023-01-24 12:34:03 +0000332 if (where_len == 0)
zeertzjq3770f4c2023-01-22 18:38:51 +0000333 return 0;
334
zeertzjq5c8771b2023-01-24 12:34:03 +0000335 if (STRNCMP(arg, "START", where_len) == 0)
zeertzjq3770f4c2023-01-22 18:38:51 +0000336 {
zeertzjq5c8771b2023-01-24 12:34:03 +0000337 *argp = skipwhite(arg + where_len);
zeertzjq3770f4c2023-01-22 18:38:51 +0000338 return DIP_START + DIP_NORTP;
339 }
zeertzjq5c8771b2023-01-24 12:34:03 +0000340 if (STRNCMP(arg, "OPT", where_len) == 0)
zeertzjq3770f4c2023-01-22 18:38:51 +0000341 {
zeertzjq5c8771b2023-01-24 12:34:03 +0000342 *argp = skipwhite(arg + where_len);
zeertzjq3770f4c2023-01-22 18:38:51 +0000343 return DIP_OPT + DIP_NORTP;
344 }
zeertzjq5c8771b2023-01-24 12:34:03 +0000345 if (STRNCMP(arg, "PACK", where_len) == 0)
zeertzjq3770f4c2023-01-22 18:38:51 +0000346 {
zeertzjq5c8771b2023-01-24 12:34:03 +0000347 *argp = skipwhite(arg + where_len);
zeertzjq3770f4c2023-01-22 18:38:51 +0000348 return DIP_START + DIP_OPT + DIP_NORTP;
349 }
zeertzjq5c8771b2023-01-24 12:34:03 +0000350 if (STRNCMP(arg, "ALL", where_len) == 0)
zeertzjq3770f4c2023-01-22 18:38:51 +0000351 {
zeertzjq5c8771b2023-01-24 12:34:03 +0000352 *argp = skipwhite(arg + where_len);
zeertzjq3770f4c2023-01-22 18:38:51 +0000353 return DIP_START + DIP_OPT;
354 }
355
356 return 0;
357}
358
359/*
zeertzjq5c8771b2023-01-24 12:34:03 +0000360 * ":runtime [where] {name}"
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200361 */
362 void
363ex_runtime(exarg_T *eap)
364{
365 char_u *arg = eap->arg;
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200366 int flags = eap->forceit ? DIP_ALL : 0;
zeertzjq5c8771b2023-01-24 12:34:03 +0000367 char_u *p = skiptowhite(arg);
368 flags += get_runtime_cmd_flags(&arg, p - arg);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200369 source_runtime(arg, flags);
370}
371
zeertzjq3770f4c2023-01-22 18:38:51 +0000372static int runtime_expand_flags;
373
374/*
375 * Set the completion context for the :runtime command.
376 */
377 void
378set_context_in_runtime_cmd(expand_T *xp, char_u *arg)
379{
zeertzjq5c8771b2023-01-24 12:34:03 +0000380 char_u *p = skiptowhite(arg);
381 runtime_expand_flags
382 = *p != NUL ? get_runtime_cmd_flags(&arg, p - arg) : 0;
zeertzjqbe5cdd12023-08-17 23:48:58 +0200383 // Skip to the last argument.
384 while (*(p = skiptowhite_esc(arg)) != NUL)
385 {
386 if (runtime_expand_flags == 0)
387 // When there are multiple arguments and [where] is not specified,
388 // use an unrelated non-zero flag to avoid expanding [where].
389 runtime_expand_flags = DIP_ALL;
390 arg = skipwhite(p);
391 }
zeertzjq3770f4c2023-01-22 18:38:51 +0000392 xp->xp_context = EXPAND_RUNTIME;
393 xp->xp_pattern = arg;
394}
395
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200396 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100397source_callback(char_u *fname, void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200398{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200400}
401
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000402#ifdef FEAT_EVAL
403/*
404 * Find an already loaded script "name".
405 * If found returns its script ID. If not found returns -1.
406 */
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100407 int
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000408find_script_by_name(char_u *name)
409{
410 int sid;
411 scriptitem_T *si;
412
413 for (sid = script_items.ga_len; sid > 0; --sid)
414 {
415 // We used to check inode here, but that doesn't work:
416 // - If a script is edited and written, it may get a different
417 // inode number, even though to the user it is the same script.
418 // - If a script is deleted and another script is written, with a
419 // different name, the inode may be re-used.
420 si = SCRIPT_ITEM(sid);
421 if (si->sn_name != NULL && fnamecmp(si->sn_name, name) == 0)
422 return sid;
423 }
424 return -1;
425}
426
427/*
428 * Add a new scriptitem with all items initialized.
429 * When running out of memory "error" is set to FAIL.
430 * Returns the script ID.
431 */
432 static int
433get_new_scriptitem(int *error)
434{
435 static scid_T last_current_SID = 0;
436 int sid = ++last_current_SID;
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000437 scriptitem_T *si = NULL;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000438
439 if (ga_grow(&script_items, (int)(sid - script_items.ga_len)) == FAIL)
440 {
441 *error = FAIL;
442 return sid;
443 }
444 while (script_items.ga_len < sid)
445 {
446 si = ALLOC_CLEAR_ONE(scriptitem_T);
447 if (si == NULL)
448 {
449 *error = FAIL;
450 return sid;
451 }
452 ++script_items.ga_len;
453 SCRIPT_ITEM(script_items.ga_len) = si;
454 si->sn_name = NULL;
455 si->sn_version = 1;
456
457 // Allocate the local script variables to use for this script.
458 new_script_vars(script_items.ga_len);
459 ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
460 hash_init(&si->sn_all_vars.dv_hashtab);
461 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
462 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
463# ifdef FEAT_PROFILE
464 si->sn_prof_on = FALSE;
465# endif
466 }
467
Bram Moolenaarb06cfcf2022-01-10 11:26:33 +0000468 // "si" can't be NULL, check only to avoid a compiler warning
469 if (si != NULL)
470 // Used to check script variable index is still valid.
471 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000472
473 return sid;
474}
475
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100476 int
477get_new_scriptitem_for_fname(int *error, char_u *fname)
478{
479 int sid = get_new_scriptitem(error);
480
481 if (*error == OK)
482 {
483 scriptitem_T *si = SCRIPT_ITEM(sid);
484
485 si->sn_name = vim_strsave(fname);
486 si->sn_state = SN_STATE_NOT_LOADED;
487 }
488 return sid;
489}
490
Ernie Rael9a901792024-04-16 22:11:56 +0200491/*
492 * If the script for "sid" is a symlink and "sn_source_sid" is not set
493 * then initialize it. A new script_item is created if needed.
494 */
495 void
496check_script_symlink(int sid)
497{
498 scriptitem_T *si = SCRIPT_ITEM(sid);
499 if (si->sn_syml_checked || si->sn_sourced_sid > 0)
500 return;
501 si->sn_syml_checked = TRUE;
502
503 // If fname is a symbolic link, create an script_item for the real file.
504
505 char_u *real_fname = fix_fname(si->sn_name);
506 if (real_fname != NULL && STRCMP(real_fname, si->sn_name) != 0)
507 {
508 int real_sid = find_script_by_name(real_fname);
509 int error2 = OK;
510 int new_sid = FALSE;
511 if (real_sid < 0)
512 {
513 real_sid = get_new_scriptitem_for_fname(&error2, real_fname);
514 new_sid = TRUE;
515 }
516 if (error2 == OK)
517 {
518 si = SCRIPT_ITEM(sid);
519 si->sn_sourced_sid = real_sid;
520 if (new_sid)
nwounkn36b66762024-06-02 16:10:07 +0200521 {
Ernie Rael9a901792024-04-16 22:11:56 +0200522 SCRIPT_ITEM(real_sid)->sn_import_autoload
523 = si->sn_import_autoload;
nwounkn36b66762024-06-02 16:10:07 +0200524 if (si->sn_autoload_prefix != NULL)
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +0200525 SCRIPT_ITEM(real_sid)->sn_autoload_prefix =
526 vim_strsave(si->sn_autoload_prefix);
nwounkn36b66762024-06-02 16:10:07 +0200527 }
Ernie Rael9a901792024-04-16 22:11:56 +0200528 }
529 }
530 vim_free(real_fname);
531}
532
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000533 static void
534find_script_callback(char_u *fname, void *cookie)
535{
536 int sid;
537 int error = OK;
538 int *ret_sid = cookie;
539
540 sid = find_script_by_name(fname);
541 if (sid < 0)
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000542 // script does not exist yet, create a new scriptitem
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100543 sid = get_new_scriptitem_for_fname(&error, fname);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000544 *ret_sid = sid;
545}
546#endif
547
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200548/*
zeertzjq008c9152023-08-17 23:08:53 +0200549 * Find the patterns in "name" in all directories in "path" and invoke
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200550 * "callback(fname, cookie)".
zeertzjq008c9152023-08-17 23:08:53 +0200551 * "prefix" is prepended to each pattern in "name".
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200552 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
553 * When "flags" has DIP_DIR: find directories instead of files.
554 * When "flags" has DIP_ERR: give an error message if there is no match.
555 *
zeertzjq008c9152023-08-17 23:08:53 +0200556 * Return FAIL when no file could be sourced, OK otherwise.
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200557 */
558 int
559do_in_path(
560 char_u *path,
zeertzjq008c9152023-08-17 23:08:53 +0200561 char *prefix,
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200562 char_u *name,
563 int flags,
564 void (*callback)(char_u *fname, void *ck),
565 void *cookie)
566{
567 char_u *rtp;
568 char_u *np;
569 char_u *buf;
570 char_u *rtp_copy;
571 char_u *tail;
572 int num_files;
573 char_u **files;
574 int i;
575 int did_one = FALSE;
576#ifdef AMIGA
577 struct Process *proc = (struct Process *)FindTask(0L);
578 APTR save_winptr = proc->pr_WindowPtr;
579
580 // Avoid a requester here for a volume that doesn't exist.
581 proc->pr_WindowPtr = (APTR)-1L;
582#endif
583
584 // Make a copy of 'runtimepath'. Invoking the callback may change the
585 // value.
586 rtp_copy = vim_strsave(path);
587 buf = alloc(MAXPATHL);
588 if (buf != NULL && rtp_copy != NULL)
589 {
Bram Moolenaar647a5302020-05-03 17:01:24 +0200590 if (p_verbose > 10 && name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200591 {
592 verbose_enter();
zeertzjq008c9152023-08-17 23:08:53 +0200593 if (*prefix != NUL)
594 smsg(_("Searching for \"%s\" under \"%s\" in \"%s\""),
595 (char *)name, prefix, (char *)path);
596 else
597 smsg(_("Searching for \"%s\" in \"%s\""),
598 (char *)name, (char *)path);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200599 verbose_leave();
600 }
601
602 // Loop over all entries in 'runtimepath'.
603 rtp = rtp_copy;
604 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
605 {
606 size_t buflen;
607
608 // Copy the path from 'runtimepath' to buf[].
609 copy_option_part(&rtp, buf, MAXPATHL, ",");
610 buflen = STRLEN(buf);
611
612 // Skip after or non-after directories.
613 if (flags & (DIP_NOAFTER | DIP_AFTER))
614 {
615 int is_after = buflen >= 5
616 && STRCMP(buf + buflen - 5, "after") == 0;
617
618 if ((is_after && (flags & DIP_NOAFTER))
619 || (!is_after && (flags & DIP_AFTER)))
620 continue;
621 }
622
623 if (name == NULL)
624 {
625 (*callback)(buf, (void *) &cookie);
626 if (!did_one)
627 did_one = (cookie == NULL);
628 }
zeertzjq008c9152023-08-17 23:08:53 +0200629 else if (buflen + 2 + STRLEN(prefix) + STRLEN(name) < MAXPATHL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200630 {
631 add_pathsep(buf);
zeertzjq008c9152023-08-17 23:08:53 +0200632 STRCAT(buf, prefix);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200633 tail = buf + STRLEN(buf);
634
635 // Loop over all patterns in "name"
636 np = name;
637 while (*np != NUL && ((flags & DIP_ALL) || !did_one))
638 {
639 // Append the pattern from "name" to buf[].
640 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
641 "\t ");
642
Bram Moolenaar647a5302020-05-03 17:01:24 +0200643 if (p_verbose > 10)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200644 {
645 verbose_enter();
646 smsg(_("Searching for \"%s\""), buf);
647 verbose_leave();
648 }
649
650 // Expand wildcards, invoke the callback for each match.
651 if (gen_expand_wildcards(1, &buf, &num_files, &files,
652 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
653 {
654 for (i = 0; i < num_files; ++i)
655 {
656 (*callback)(files[i], cookie);
657 did_one = TRUE;
658 if (!(flags & DIP_ALL))
659 break;
660 }
661 FreeWild(num_files, files);
662 }
663 }
664 }
665 }
666 }
667 vim_free(buf);
668 vim_free(rtp_copy);
669 if (!did_one && name != NULL)
670 {
671 char *basepath = path == p_rtp ? "runtimepath" : "packpath";
672
673 if (flags & DIP_ERR)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000674 semsg(_(e_directory_not_found_in_str_str), basepath, name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200675 else if (p_verbose > 0)
676 {
677 verbose_enter();
678 smsg(_("not found in '%s': \"%s\""), basepath, name);
679 verbose_leave();
680 }
681 }
682
683#ifdef AMIGA
684 proc->pr_WindowPtr = save_winptr;
685#endif
686
687 return did_one ? OK : FAIL;
688}
689
690/*
691 * Find "name" in "path". When found, invoke the callback function for
692 * it: callback(fname, "cookie")
693 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first
694 * one is used.
695 * Returns OK when at least one match found, FAIL otherwise.
696 *
697 * If "name" is NULL calls callback for each entry in "path". Cookie is
698 * passed by reference in this case, setting it to NULL indicates that callback
699 * has done its job.
700 */
701 static int
702do_in_path_and_pp(
703 char_u *path,
704 char_u *name,
705 int flags,
706 void (*callback)(char_u *fname, void *ck),
707 void *cookie)
708{
709 int done = FAIL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200710
711 if ((flags & DIP_NORTP) == 0)
zeertzjq008c9152023-08-17 23:08:53 +0200712 done = do_in_path(path, "", name, flags, callback, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200713
714 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
zeertzjq008c9152023-08-17 23:08:53 +0200715 done = do_in_path(p_pp, "pack/*/start/*/", name, flags, callback,
716 cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200717
718 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
zeertzjq008c9152023-08-17 23:08:53 +0200719 done = do_in_path(p_pp, "pack/*/opt/*/", name, flags, callback,
720 cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200721
722 return done;
723}
724
725/*
726 * Just like do_in_path_and_pp(), using 'runtimepath' for "path".
727 */
728 int
729do_in_runtimepath(
730 char_u *name,
731 int flags,
732 void (*callback)(char_u *fname, void *ck),
733 void *cookie)
734{
735 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
736}
737
738/*
739 * Source the file "name" from all directories in 'runtimepath'.
740 * "name" can contain wildcards.
741 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
742 *
743 * return FAIL when no file could be sourced, OK otherwise.
744 */
745 int
746source_runtime(char_u *name, int flags)
747{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100748 return source_in_path(p_rtp, name, flags, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200749}
750
751/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000752 * Just like source_runtime(), but use "path" instead of 'runtimepath'
753 * and return the script ID in "ret_sid".
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200754 */
755 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200757{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200759}
760
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200761#if defined(FEAT_EVAL) || defined(PROTO)
762
763/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000764 * Find "name" in 'runtimepath'. If found a new scriptitem is created for it
765 * and it's script ID is returned.
766 * If not found returns -1.
767 */
768 int
769find_script_in_rtp(char_u *name)
770{
771 int sid = -1;
772
Yegappan Lakshmanan8c35c262024-05-10 13:10:54 +0200773 (void)do_in_path_and_pp(p_rtp, name, DIP_START | DIP_NOAFTER,
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000774 find_script_callback, &sid);
775 return sid;
776}
777
778/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200779 * Expand wildcards in "pat" and invoke do_source() for each match.
780 */
781 static void
782source_all_matches(char_u *pat)
783{
784 int num_files;
785 char_u **files;
786 int i;
787
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000788 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) != OK)
789 return;
790
791 for (i = 0; i < num_files; ++i)
792 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
793 FreeWild(num_files, files);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200794}
795
796/*
797 * Add the package directory to 'runtimepath'.
798 */
799 static int
800add_pack_dir_to_rtp(char_u *fname)
801{
802 char_u *p4, *p3, *p2, *p1, *p;
803 char_u *entry;
804 char_u *insp = NULL;
805 int c;
806 char_u *new_rtp;
807 int keep;
808 size_t oldlen;
809 size_t addlen;
810 size_t new_rtp_len;
811 char_u *afterdir = NULL;
812 size_t afterlen = 0;
813 char_u *after_insp = NULL;
814 char_u *ffname = NULL;
815 size_t fname_len;
816 char_u *buf = NULL;
817 char_u *rtp_ffname;
818 int match;
819 int retval = FAIL;
820
821 p4 = p3 = p2 = p1 = get_past_head(fname);
822 for (p = p1; *p; MB_PTR_ADV(p))
823 if (vim_ispathsep_nocolon(*p))
824 {
825 p4 = p3; p3 = p2; p2 = p1; p1 = p;
826 }
827
828 // now we have:
829 // rtp/pack/name/start/name
830 // p4 p3 p2 p1
831 //
832 // find the part up to "pack" in 'runtimepath'
833 c = *++p4; // append pathsep in order to expand symlink
834 *p4 = NUL;
835 ffname = fix_fname(fname);
836 *p4 = c;
837 if (ffname == NULL)
838 return FAIL;
839
840 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
841 // Also stop at the first "after" directory.
842 fname_len = STRLEN(ffname);
843 buf = alloc(MAXPATHL);
844 if (buf == NULL)
845 goto theend;
846 for (entry = p_rtp; *entry != NUL; )
847 {
848 char_u *cur_entry = entry;
849
850 copy_option_part(&entry, buf, MAXPATHL, ",");
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200851
852 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
853 && p > buf
854 && vim_ispathsep(p[-1])
855 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
856 {
857 if (insp == NULL)
858 // Did not find "ffname" before the first "after" directory,
859 // insert it before this entry.
860 insp = cur_entry;
861 after_insp = cur_entry;
862 break;
863 }
zeertzjq39c9ec12023-04-01 13:52:03 +0100864
865 if (insp == NULL)
866 {
867 add_pathsep(buf);
868 rtp_ffname = fix_fname(buf);
869 if (rtp_ffname == NULL)
870 goto theend;
871 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
872 vim_free(rtp_ffname);
873 if (match)
874 // Insert "ffname" after this entry (and comma).
875 insp = entry;
876 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200877 }
878
879 if (insp == NULL)
880 // Both "fname" and "after" not found, append at the end.
881 insp = p_rtp + STRLEN(p_rtp);
882
883 // check if rtp/pack/name/start/name/after exists
884 afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
885 if (afterdir != NULL && mch_isdir(afterdir))
886 afterlen = STRLEN(afterdir) + 1; // add one for comma
887
888 oldlen = STRLEN(p_rtp);
889 addlen = STRLEN(fname) + 1; // add one for comma
890 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
891 if (new_rtp == NULL)
892 goto theend;
893
894 // We now have 'rtp' parts: {keep}{keep_after}{rest}.
895 // Create new_rtp, first: {keep},{fname}
896 keep = (int)(insp - p_rtp);
897 mch_memmove(new_rtp, p_rtp, keep);
898 new_rtp_len = keep;
899 if (*insp == NUL)
900 new_rtp[new_rtp_len++] = ','; // add comma before
901 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
902 new_rtp_len += addlen - 1;
903 if (*insp != NUL)
904 new_rtp[new_rtp_len++] = ','; // add comma after
905
906 if (afterlen > 0 && after_insp != NULL)
907 {
908 int keep_after = (int)(after_insp - p_rtp);
909
910 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
911 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
912 keep_after - keep);
913 new_rtp_len += keep_after - keep;
914 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
915 new_rtp_len += afterlen - 1;
916 new_rtp[new_rtp_len++] = ',';
917 keep = keep_after;
918 }
919
920 if (p_rtp[keep] != NUL)
921 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
922 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
923 else
924 new_rtp[new_rtp_len] = NUL;
925
926 if (afterlen > 0 && after_insp == NULL)
927 {
928 // Append afterdir when "after" was not found:
929 // {keep},{fname}{rest},{afterdir}
930 STRCAT(new_rtp, ",");
931 STRCAT(new_rtp, afterdir);
932 }
933
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100934 set_option_value_give_err((char_u *)"rtp", 0L, new_rtp, 0);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200935 vim_free(new_rtp);
936 retval = OK;
937
938theend:
939 vim_free(buf);
940 vim_free(ffname);
941 vim_free(afterdir);
942 return retval;
943}
944
945/*
946 * Load scripts in "plugin" and "ftdetect" directories of the package.
947 */
948 static int
949load_pack_plugin(char_u *fname)
950{
951 static char *plugpat = "%s/plugin/**/*.vim";
952 static char *ftpat = "%s/ftdetect/*.vim";
953 int len;
954 char_u *ffname = fix_fname(fname);
955 char_u *pat = NULL;
956 int retval = FAIL;
957
958 if (ffname == NULL)
959 return FAIL;
960 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
961 pat = alloc(len);
962 if (pat == NULL)
963 goto theend;
964 vim_snprintf((char *)pat, len, plugpat, ffname);
965 source_all_matches(pat);
966
967 {
968 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
969
970 // If runtime/filetype.vim wasn't loaded yet, the scripts will be
971 // found when it loads.
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100972 if (cmd != NULL && eval_to_number(cmd, FALSE) > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200973 {
974 do_cmdline_cmd((char_u *)"augroup filetypedetect");
975 vim_snprintf((char *)pat, len, ftpat, ffname);
976 source_all_matches(pat);
977 do_cmdline_cmd((char_u *)"augroup END");
978 }
979 vim_free(cmd);
980 }
981 vim_free(pat);
982 retval = OK;
983
984theend:
985 vim_free(ffname);
986 return retval;
987}
988
989// used for "cookie" of add_pack_plugin()
990static int APP_ADD_DIR;
991static int APP_LOAD;
992static int APP_BOTH;
993
994 static void
995add_pack_plugin(char_u *fname, void *cookie)
996{
997 if (cookie != &APP_LOAD)
998 {
999 char_u *buf = alloc(MAXPATHL);
1000 char_u *p;
1001 int found = FALSE;
1002
1003 if (buf == NULL)
1004 return;
1005 p = p_rtp;
1006 while (*p != NUL)
1007 {
1008 copy_option_part(&p, buf, MAXPATHL, ",");
1009 if (pathcmp((char *)buf, (char *)fname, -1) == 0)
1010 {
1011 found = TRUE;
1012 break;
1013 }
1014 }
1015 vim_free(buf);
1016 if (!found)
1017 // directory is not yet in 'runtimepath', add it
1018 if (add_pack_dir_to_rtp(fname) == FAIL)
1019 return;
1020 }
1021
1022 if (cookie != &APP_ADD_DIR)
1023 load_pack_plugin(fname);
1024}
1025
1026/*
1027 * Add all packages in the "start" directory to 'runtimepath'.
1028 */
1029 void
1030add_pack_start_dirs(void)
1031{
zeertzjq008c9152023-08-17 23:08:53 +02001032 do_in_path(p_pp, "", (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001033 add_pack_plugin, &APP_ADD_DIR);
1034}
1035
1036/*
1037 * Load plugins from all packages in the "start" directory.
1038 */
1039 void
1040load_start_packages(void)
1041{
1042 did_source_packages = TRUE;
zeertzjq008c9152023-08-17 23:08:53 +02001043 do_in_path(p_pp, "", (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001044 add_pack_plugin, &APP_LOAD);
1045}
1046
1047/*
1048 * ":packloadall"
1049 * Find plugins in the package directories and source them.
1050 */
1051 void
1052ex_packloadall(exarg_T *eap)
1053{
1054 if (!did_source_packages || eap->forceit)
1055 {
1056 // First do a round to add all directories to 'runtimepath', then load
1057 // the plugins. This allows for plugins to use an autoload directory
1058 // of another plugin.
1059 add_pack_start_dirs();
1060 load_start_packages();
1061 }
1062}
1063
1064/*
1065 * ":packadd[!] {name}"
1066 */
1067 void
1068ex_packadd(exarg_T *eap)
1069{
1070 static char *plugpat = "pack/*/%s/%s";
1071 int len;
1072 char *pat;
1073 int round;
1074 int res = OK;
1075
1076 // Round 1: use "start", round 2: use "opt".
1077 for (round = 1; round <= 2; ++round)
1078 {
1079 // Only look under "start" when loading packages wasn't done yet.
1080 if (round == 1 && did_source_packages)
1081 continue;
1082
1083 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
1084 pat = alloc(len);
1085 if (pat == NULL)
1086 return;
1087 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
1088 // The first round don't give a "not found" error, in the second round
1089 // only when nothing was found in the first round.
zeertzjq008c9152023-08-17 23:08:53 +02001090 res = do_in_path(p_pp, "", (char_u *)pat,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001091 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
1092 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
1093 vim_free(pat);
1094 }
1095}
1096#endif
1097
1098/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02001099 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
1100 * list of file names in allocated memory.
1101 */
1102 void
1103remove_duplicates(garray_T *gap)
1104{
1105 int i;
1106 int j;
1107 char_u **fnames = (char_u **)gap->ga_data;
1108
1109 sort_strings(fnames, gap->ga_len);
1110 for (i = gap->ga_len - 1; i > 0; --i)
1111 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
1112 {
1113 vim_free(fnames[i]);
1114 for (j = i + 1; j < gap->ga_len; ++j)
1115 fnames[j - 1] = fnames[j];
1116 --gap->ga_len;
1117 }
1118}
1119
zeertzjq5c8771b2023-01-24 12:34:03 +00001120 static void
1121ExpandRTDir_int(
1122 char_u *pat,
1123 size_t pat_len,
1124 int flags,
1125 int keep_ext,
1126 garray_T *gap,
1127 char *dirnames[])
1128{
1129 for (int i = 0; dirnames[i] != NULL; ++i)
1130 {
glepnir21449ee2025-07-06 11:02:51 +02001131 const size_t buf_len = STRLEN(dirnames[i]) + pat_len + 64;
zeertzjq5c8771b2023-01-24 12:34:03 +00001132 char *buf = alloc(buf_len);
1133 if (buf == NULL)
1134 {
1135 ga_clear_strings(gap);
1136 return;
1137 }
zeertzjq5c8771b2023-01-24 12:34:03 +00001138 int glob_flags = 0;
1139 int expand_dirs = FALSE;
1140
glepnir21449ee2025-07-06 11:02:51 +02001141 // Build base pattern
1142 vim_snprintf(buf, buf_len, "%s%s%s%s",
1143 *dirnames[i] ? dirnames[i] : "", *dirnames[i] ? "/" : "",
1144 pat, "*.vim");
zeertzjq5c8771b2023-01-24 12:34:03 +00001145
1146expand:
1147 if ((flags & DIP_NORTP) == 0)
glepnir21449ee2025-07-06 11:02:51 +02001148 globpath(p_rtp, (char_u *)buf, gap, glob_flags, expand_dirs);
zeertzjq5c8771b2023-01-24 12:34:03 +00001149
1150 if (flags & DIP_START)
1151 {
glepnir21449ee2025-07-06 11:02:51 +02001152 // Build complete search path: pack/*/start/*/dirnames[i]/pat*.vim
1153 vim_snprintf(buf, buf_len, "pack/*/start/*/%s%s%s%s",
1154 *dirnames[i] ? dirnames[i] : "",
1155 *dirnames[i] ? "/" : "",
1156 pat,
1157 expand_dirs ? "*" : "*.vim");
1158 globpath(p_pp, (char_u *)buf, gap, glob_flags, expand_dirs);
zeertzjq5c8771b2023-01-24 12:34:03 +00001159 }
1160
1161 if (flags & DIP_OPT)
1162 {
glepnir21449ee2025-07-06 11:02:51 +02001163 // Build complete search path: pack/*/opt/*/dirnames[i]/pat*.vim
1164 vim_snprintf(buf, buf_len, "pack/*/opt/*/%s%s%s%s",
1165 *dirnames[i] ? dirnames[i] : "",
1166 *dirnames[i] ? "/" : "", pat,
1167 expand_dirs ? "*" : "*.vim");
1168 globpath(p_pp, (char_u *)buf, gap, glob_flags, expand_dirs);
zeertzjq5c8771b2023-01-24 12:34:03 +00001169 }
1170
glepnir21449ee2025-07-06 11:02:51 +02001171 // Second round for directories
1172 if (*dirnames[i] == NUL && !expand_dirs)
zeertzjq5c8771b2023-01-24 12:34:03 +00001173 {
1174 // expand dir names in another round
glepnir21449ee2025-07-06 11:02:51 +02001175 vim_snprintf(buf, buf_len, "%s*", pat);
zeertzjq5c8771b2023-01-24 12:34:03 +00001176 glob_flags = WILD_ADD_SLASH;
1177 expand_dirs = TRUE;
1178 goto expand;
1179 }
1180
1181 vim_free(buf);
1182 }
1183
1184 int pat_pathsep_cnt = 0;
1185 for (size_t i = 0; i < pat_len; ++i)
glepnir21449ee2025-07-06 11:02:51 +02001186 {
zeertzjq5c8771b2023-01-24 12:34:03 +00001187 if (vim_ispathsep(pat[i]))
1188 ++pat_pathsep_cnt;
glepnir21449ee2025-07-06 11:02:51 +02001189 }
zeertzjq5c8771b2023-01-24 12:34:03 +00001190
1191 for (int i = 0; i < gap->ga_len; ++i)
1192 {
1193 char_u *match = ((char_u **)gap->ga_data)[i];
1194 char_u *s = match;
1195 char_u *e = s + STRLEN(s);
1196 if (e - 4 > s && !keep_ext && STRNICMP(e - 4, ".vim", 4) == 0)
1197 {
1198 e -= 4;
1199 *e = NUL;
1200 }
1201
1202 int match_pathsep_cnt = (e > s && e[-1] == '/') ? -1 : 0;
1203 for (s = e; s > match; MB_PTR_BACK(match, s))
glepnir21449ee2025-07-06 11:02:51 +02001204 {
zeertzjq5c8771b2023-01-24 12:34:03 +00001205 if (s < match || (vim_ispathsep(*s)
1206 && ++match_pathsep_cnt > pat_pathsep_cnt))
1207 break;
glepnir21449ee2025-07-06 11:02:51 +02001208 }
zeertzjq5c8771b2023-01-24 12:34:03 +00001209 ++s;
1210 if (s != match)
1211 mch_memmove(match, s, e - s + 1);
1212 }
1213
1214 if (gap->ga_len == 0)
1215 return;
1216
1217 // Sort and remove duplicates which can happen when specifying multiple
1218 // directories in dirnames.
1219 remove_duplicates(gap);
1220}
1221
Bram Moolenaar26262f82019-09-04 20:59:15 +02001222/*
zeertzjq3770f4c2023-01-22 18:38:51 +00001223 * Expand runtime file names.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001224 * Search from 'runtimepath':
1225 * 'runtimepath'/{dirnames}/{pat}.vim
zeertzjq3770f4c2023-01-22 18:38:51 +00001226 * When "flags" has DIP_START: search also from "start" of 'packpath':
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001227 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
zeertzjq3770f4c2023-01-22 18:38:51 +00001228 * When "flags" has DIP_OPT: search also from "opt" of 'packpath':
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001229 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
1230 * "dirnames" is an array with one or more directory names.
1231 */
1232 int
1233ExpandRTDir(
1234 char_u *pat,
1235 int flags,
1236 int *num_file,
1237 char_u ***file,
1238 char *dirnames[])
1239{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001240 *num_file = 0;
1241 *file = NULL;
zeertzjq5c8771b2023-01-24 12:34:03 +00001242
1243 garray_T ga;
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001244 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001245
zeertzjq5c8771b2023-01-24 12:34:03 +00001246 ExpandRTDir_int(pat, STRLEN(pat), flags, FALSE, &ga, dirnames);
1247
1248 if (ga.ga_len == 0)
1249 return FAIL;
1250
1251 *file = ga.ga_data;
1252 *num_file = ga.ga_len;
1253 return OK;
1254}
1255
1256/*
1257 * Handle command line completion for :runtime command.
1258 */
1259 int
1260expand_runtime_cmd(char_u *pat, int *numMatches, char_u ***matches)
1261{
1262 *numMatches = 0;
1263 *matches = NULL;
1264
1265 garray_T ga;
1266 ga_init2(&ga, sizeof(char *), 10);
1267
1268 size_t pat_len = (int)STRLEN(pat);
1269 char *dirnames[] = {"", NULL};
1270 ExpandRTDir_int(pat, pat_len, runtime_expand_flags, TRUE, &ga, dirnames);
1271
1272 // Try to complete values for [where] argument when none was found.
1273 if (runtime_expand_flags == 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001274 {
zeertzjq5c8771b2023-01-24 12:34:03 +00001275 char *where_values[] = {"START", "OPT", "PACK", "ALL"};
1276 for (size_t i = 0; i < ARRAY_LENGTH(where_values); ++i)
1277 if (STRNCMP(pat, where_values[i], pat_len) == 0)
1278 {
1279 char_u *p = vim_strsave((char_u *)where_values[i]);
1280 if (p != NULL && ga_add_string(&ga, p) == FAIL)
1281 vim_free(p);
1282 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001283 }
1284
1285 if (ga.ga_len == 0)
1286 return FAIL;
1287
zeertzjq5c8771b2023-01-24 12:34:03 +00001288 *matches = ga.ga_data;
1289 *numMatches = ga.ga_len;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001290 return OK;
1291}
1292
1293/*
1294 * Expand loadplugin names:
1295 * 'packpath'/pack/ * /opt/{pat}
1296 */
1297 int
1298ExpandPackAddDir(
1299 char_u *pat,
1300 int *num_file,
1301 char_u ***file)
1302{
1303 char_u *s;
1304 char_u *e;
1305 char_u *match;
1306 garray_T ga;
1307 int i;
1308 int pat_len;
1309
1310 *num_file = 0;
1311 *file = NULL;
1312 pat_len = (int)STRLEN(pat);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001313 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001314
1315 s = alloc(pat_len + 26);
1316 if (s == NULL)
1317 {
1318 ga_clear_strings(&ga);
1319 return FAIL;
1320 }
1321 sprintf((char *)s, "pack/*/opt/%s*", pat);
zeertzjq3770f4c2023-01-22 18:38:51 +00001322 globpath(p_pp, s, &ga, 0, TRUE);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001323 vim_free(s);
1324
1325 for (i = 0; i < ga.ga_len; ++i)
1326 {
1327 match = ((char_u **)ga.ga_data)[i];
1328 s = gettail(match);
1329 e = s + STRLEN(s);
1330 mch_memmove(match, s, e - s + 1);
1331 }
1332
1333 if (ga.ga_len == 0)
1334 return FAIL;
1335
1336 // Sort and remove duplicates which can happen when specifying multiple
1337 // directories in dirnames.
1338 remove_duplicates(&ga);
1339
1340 *file = ga.ga_data;
1341 *num_file = ga.ga_len;
1342 return OK;
1343}
1344
1345 static void
1346cmd_source(char_u *fname, exarg_T *eap)
1347{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001348 int clearvars = FALSE;
1349
1350 if (*fname != NUL && STRNCMP(fname, "++clear", 7) == 0)
1351 {
1352 // ++clear argument is supplied
1353 clearvars = TRUE;
1354 fname = fname + 7;
1355 if (*fname != NUL)
1356 {
1357 semsg(_(e_invalid_argument_str), eap->arg);
1358 return;
1359 }
1360 }
1361
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001362 if (*fname != NUL && eap != NULL && eap->addr_count > 0)
1363 {
1364 // if a filename is specified to :source, then a range is not allowed
1365 emsg(_(e_no_range_allowed));
1366 return;
1367 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001368
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001369 if (eap != NULL && *fname == NUL)
1370 {
1371 if (eap->forceit)
1372 // a file name is needed to source normal mode commands
1373 emsg(_(e_argument_required));
1374 else
1375 // source ex commands from the current buffer
zeertzjqf68517c2024-04-25 21:34:10 +02001376 do_source_ext(NULL, FALSE, DOSO_NONE, NULL, eap, clearvars);
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00001377 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001378 else if (eap != NULL && eap->forceit)
1379 // ":source!": read Normal mode commands
1380 // Need to execute the commands directly. This is required at least
1381 // for:
1382 // - ":g" command busy
1383 // - after ":argdo", ":windo" or ":bufdo"
1384 // - another command follows
1385 // - inside a loop
1386 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
1387#ifdef FEAT_EVAL
1388 || eap->cstack->cs_idx >= 0
1389#endif
1390 );
1391
1392 // ":source" read ex commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001394 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001395}
1396
1397/*
1398 * ":source {fname}"
1399 */
1400 void
1401ex_source(exarg_T *eap)
1402{
1403#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02001404 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001405 {
1406 char_u *fname = NULL;
1407
1408 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
1409 NULL, NULL,
1410 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1411 if (fname != NULL)
1412 {
1413 cmd_source(fname, eap);
1414 vim_free(fname);
1415 }
1416 }
1417 else
1418#endif
1419 cmd_source(eap->arg, eap);
1420}
1421
1422#if defined(FEAT_EVAL) || defined(PROTO)
1423/*
1424 * ":options"
1425 */
1426 void
1427ex_options(
1428 exarg_T *eap UNUSED)
1429{
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001430 char_u buf[500];
1431 int multi_mods = 0;
1432
1433 buf[0] = NUL;
dundargocc57b5bc2022-11-02 13:30:51 +00001434 (void)add_win_cmd_modifiers(buf, &cmdmod, &multi_mods);
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001435
1436 vim_setenv((char_u *)"OPTWIN_CMD", buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001437 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
1438}
1439#endif
1440
1441/*
1442 * ":source" and associated commands.
1443 */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001444
zeertzjq2d68b722023-03-30 21:50:37 +01001445#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001446/*
1447 * Return the address holding the next breakpoint line for a source cookie.
1448 */
1449 linenr_T *
1450source_breakpoint(void *cookie)
1451{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001452 return &((source_cookie_T *)cookie)->breakpoint;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001453}
1454
1455/*
1456 * Return the address holding the debug tick for a source cookie.
1457 */
1458 int *
1459source_dbg_tick(void *cookie)
1460{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001461 return &((source_cookie_T *)cookie)->dbg_tick;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001462}
1463
1464/*
1465 * Return the nesting level for a source cookie.
1466 */
1467 int
1468source_level(void *cookie)
1469{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001470 return ((source_cookie_T *)cookie)->level;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001471}
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001472
1473/*
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02001474 * Return the readahead line. Note that the pointer may become invalid when
1475 * getting the next line, if it's concatenated with the next one.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001476 */
1477 char_u *
1478source_nextline(void *cookie)
1479{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001480 return ((source_cookie_T *)cookie)->nextline;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001481}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001482#endif
1483
1484#if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC)
1485# define USE_FOPEN_NOINH
1486/*
1487 * Special function to open a file without handle inheritance.
1488 * When possible the handle is closed on exec().
1489 */
1490 static FILE *
1491fopen_noinh_readbin(char *filename)
1492{
1493# ifdef MSWIN
1494 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
1495# else
1496 int fd_tmp = mch_open(filename, O_RDONLY, 0);
1497# endif
1498
1499 if (fd_tmp == -1)
1500 return NULL;
1501
1502# ifdef HAVE_FD_CLOEXEC
1503 {
1504 int fdflags = fcntl(fd_tmp, F_GETFD);
1505 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1506 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
1507 }
1508# endif
1509
1510 return fdopen(fd_tmp, READBIN);
1511}
1512#endif
1513
1514/*
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001515 * Initialization for sourcing lines from the current buffer. Reads all the
1516 * lines from the buffer and stores it in the cookie grow array.
1517 * Returns a pointer to the name ":source buffer=<n>" on success and NULL on
1518 * failure.
1519 */
1520 static char_u *
1521do_source_buffer_init(source_cookie_T *sp, exarg_T *eap)
1522{
1523 linenr_T curr_lnum;
1524 char_u *line = NULL;
1525 char_u *fname;
1526
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001527 if (curbuf == NULL)
1528 return NULL;
1529
1530 // Use ":source buffer=<num>" as the script name
Yegappan Lakshmananf135fa22024-04-20 18:31:21 +02001531 if (curbuf->b_ffname != NULL)
1532 fname = vim_strsave(curbuf->b_ffname);
1533 else
1534 {
1535 vim_snprintf((char *)IObuff, IOSIZE, ":source buffer=%d", curbuf->b_fnum);
1536 fname = vim_strsave(IObuff);
1537 }
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001538 if (fname == NULL)
1539 return NULL;
1540
1541 ga_init2(&sp->buflines, sizeof(char_u *), 100);
1542
1543 // Copy the lines from the buffer into a grow array
1544 for (curr_lnum = eap->line1; curr_lnum <= eap->line2; curr_lnum++)
1545 {
1546 line = vim_strsave(ml_get(curr_lnum));
1547 if (line == NULL)
1548 goto errret;
1549 if (ga_add_string(&sp->buflines, line) == FAIL)
1550 goto errret;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001551 }
1552 sp->buf_lnum = 0;
1553 sp->source_from_buf = TRUE;
zeertzjqf68517c2024-04-25 21:34:10 +02001554 // When sourcing a range of lines from a buffer, use buffer line number.
1555 sp->sourcing_lnum = eap->line1 - 1;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001556
1557 return fname;
1558
1559errret:
1560 vim_free(fname);
1561 vim_free(line);
1562 ga_clear_strings(&sp->buflines);
1563 return NULL;
1564}
1565
1566/*
1567 * Read the file "fname" and execute its lines as EX commands.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1569 * again.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001570 *
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001571 * The "eap" argument is used when sourcing lines from a buffer instead of a
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001572 * file.
1573 *
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001574 * If "clearvars" is TRUE, then for scripts which are loaded more than
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001575 * once, clear all the functions and variables previously defined in that
1576 * script.
1577 *
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001578 * This function may be called recursively!
1579 *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580 * Return FAIL if file could not be opened, OK otherwise.
1581 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001582 */
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001583 static int
1584do_source_ext(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001585 char_u *fname,
1586 int check_other, // check for .vimrc and _vimrc
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587 int is_vimrc, // DOSO_ value
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001588 int *ret_sid UNUSED,
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001589 exarg_T *eap,
1590 int clearvars UNUSED)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001591{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001592 source_cookie_T cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001593 char_u *p;
Bram Moolenaar753885b2022-08-24 16:30:36 +01001594 char_u *fname_not_fixed = NULL;
Bram Moolenaar5214b292022-08-24 17:32:35 +01001595 char_u *fname_exp = NULL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001596 char_u *firstline = NULL;
1597 int retval = FAIL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001598 sctx_T save_current_sctx;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001599#ifdef STARTUPTIME
1600 struct timeval tv_rel;
1601 struct timeval tv_start;
1602#endif
1603#ifdef FEAT_PROFILE
1604 proftime_T wait_start;
1605#endif
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001606 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001607 int trigger_source_post = FALSE;
ichizok7e5fe382023-04-15 13:17:50 +01001608#ifdef FEAT_EVAL
1609 funccal_entry_T funccalp_entry;
1610 int save_debug_break_level = debug_break_level;
1611 int sid = -1;
1612 scriptitem_T *si = NULL;
1613 int save_estack_compiling = estack_compiling;
1614 ESTACK_CHECK_DECLARATION;
1615#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001616
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001617 CLEAR_FIELD(cookie);
1618 if (fname == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001619 {
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001620 // sourcing lines from a buffer
1621 fname_exp = do_source_buffer_init(&cookie, eap);
1622 if (fname_exp == NULL)
1623 return FAIL;
1624 }
1625 else
1626 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01001627 fname_not_fixed = expand_env_save(fname);
1628 if (fname_not_fixed == NULL)
1629 goto theend;
1630 fname_exp = fix_fname(fname_not_fixed);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001631 if (fname_exp == NULL)
Bram Moolenaar753885b2022-08-24 16:30:36 +01001632 goto theend;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001633 if (mch_isdir(fname_exp))
1634 {
1635 smsg(_("Cannot source a directory: \"%s\""), fname);
1636 goto theend;
1637 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001638 }
Bram Moolenaar8de901e2021-06-11 22:21:24 +02001639#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02001640 estack_compiling = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 // See if we loaded this script before.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001643 sid = find_script_by_name(fname_exp);
Bram Moolenaarf479cac2022-01-12 12:54:55 +00001644 if (sid > 0 && ret_sid != NULL
1645 && SCRIPT_ITEM(sid)->sn_state != SN_STATE_NOT_LOADED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 {
1647 // Already loaded and no need to load again, return here.
1648 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001649 retval = OK;
1650 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001651 }
1652#endif
1653
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001654 // Apply SourceCmd autocommands, they should get the file and source it.
1655 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1656 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1657 FALSE, curbuf))
1658 {
1659#ifdef FEAT_EVAL
1660 retval = aborting() ? FAIL : OK;
1661#else
1662 retval = OK;
1663#endif
1664 if (retval == OK)
1665 // Apply SourcePost autocommands.
1666 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1667 FALSE, curbuf);
1668 goto theend;
1669 }
1670
1671 // Apply SourcePre autocommands, they may get the file.
1672 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1673
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001674 if (!cookie.source_from_buf)
1675 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001676#ifdef USE_FOPEN_NOINH
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001677 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001678#else
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001679 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001680#endif
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001681 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001682 if (cookie.fp == NULL && check_other)
1683 {
1684 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1685 // and ".exrc" by "_exrc" or vice versa.
1686 p = gettail(fname_exp);
1687 if ((*p == '.' || *p == '_')
1688 && (STRICMP(p + 1, "vimrc") == 0
1689 || STRICMP(p + 1, "gvimrc") == 0
1690 || STRICMP(p + 1, "exrc") == 0))
1691 {
1692 if (*p == '_')
1693 *p = '.';
1694 else
1695 *p = '_';
1696#ifdef USE_FOPEN_NOINH
1697 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1698#else
1699 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1700#endif
1701 }
1702 }
1703
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001704 if (cookie.fp == NULL && !cookie.source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001705 {
1706 if (p_verbose > 0)
1707 {
1708 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001709 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001710 smsg(_("could not source \"%s\""), fname);
1711 else
1712 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001713 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001714 verbose_leave();
1715 }
1716 goto theend;
1717 }
1718
1719 // The file exists.
1720 // - In verbose mode, give a message.
1721 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1722 if (p_verbose > 1)
1723 {
1724 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001725 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001726 smsg(_("sourcing \"%s\""), fname);
1727 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001728 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001729 verbose_leave();
1730 }
1731 if (is_vimrc == DOSO_VIMRC)
1732 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1733 else if (is_vimrc == DOSO_GVIMRC)
1734 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1735
1736#ifdef USE_CRNL
1737 // If no automatic file format: Set default to CR-NL.
1738 if (*p_ffs == NUL)
1739 cookie.fileformat = EOL_DOS;
1740 else
1741 cookie.fileformat = EOL_UNKNOWN;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001742#endif
1743
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001744#ifdef FEAT_EVAL
1745 // Check if this script has a breakpoint.
1746 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1747 cookie.fname = fname_exp;
1748 cookie.dbg_tick = debug_tick;
1749
1750 cookie.level = ex_nesting_level;
1751#endif
1752
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001753#ifdef STARTUPTIME
1754 if (time_fd != NULL)
1755 time_push(&tv_rel, &tv_start);
1756#endif
1757
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00001758 // "legacy" does not apply to commands in the script
1759 sticky_cmdmod_flags = 0;
1760
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001761 save_current_sctx = current_sctx;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00001762 if (cmdmod.cmod_flags & CMOD_VIM9CMD)
1763 // When the ":vim9cmd" command modifier is used, source the script as a
1764 // Vim9 script.
1765 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1766 else
1767 current_sctx.sc_version = 1; // default script version
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001768
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001769#ifdef FEAT_EVAL
Bram Moolenaara2942c72023-01-02 13:41:49 +00001770 current_sctx.sc_lnum = 0;
1771
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001772# ifdef FEAT_PROFILE
1773 if (do_profiling == PROF_YES)
1774 prof_child_enter(&wait_start); // entering a child now
1775# endif
1776
1777 // Don't use local function variables, if called from a function.
1778 // Also starts profiling timer for nested script.
1779 save_funccal(&funccalp_entry);
1780
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001781 // Reset "KeyTyped" to avoid some commands thinking they are invoked
1782 // interactively. E.g. defining a function would output indent.
1783 int save_KeyTyped = KeyTyped;
1784 KeyTyped = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001785
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001786 // Check if this script was sourced before to find its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001787 // Always use a new sequence number.
1788 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001790 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 hashtab_T *ht;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001792 int todo;
1793 hashitem_T *hi;
1794 dictitem_T *di;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795
1796 // loading the same script again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001798 si = SCRIPT_ITEM(sid);
1799 if (si->sn_state == SN_STATE_NOT_LOADED)
1800 {
1801 // this script was found but not loaded yet
1802 si->sn_state = SN_STATE_NEW;
1803 }
1804 else
1805 {
1806 si->sn_state = SN_STATE_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001808 if (!clearvars)
1809 {
1810 // Script-local variables remain but "const" can be set again.
1811 // In Vim9 script variables will be cleared when "vim9script"
1812 // is encountered without the "noclear" argument.
1813 ht = &SCRIPT_VARS(sid);
1814 todo = (int)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001815 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00001816 if (!HASHITEM_EMPTY(hi))
1817 {
1818 --todo;
1819 di = HI2DI(hi);
1820 di->di_flags |= DI_FLAGS_RELOAD;
1821 }
1822 // imports can be redefined once
1823 mark_imports_for_reload(sid);
1824 }
1825 else
1826 clear_vim9_scriptlocal_vars(sid);
Bram Moolenaar0123cc12021-02-07 17:17:58 +01001827
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001828 // reset version, "vim9script" may have been added or removed.
1829 si->sn_version = 1;
1830 }
Yegappan Lakshmananfa630082024-03-10 19:22:38 +01001831 if (ret_sid != NULL)
1832 *ret_sid = sid;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001833 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001835 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001836 int error = OK;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001837
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001838 // It's new, generate a new SID and initialize the scriptitem.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001839 sid = get_new_scriptitem(&error);
1840 current_sctx.sc_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001841 if (error == FAIL)
1842 goto almosttheend;
Bram Moolenaar753885b2022-08-24 16:30:36 +01001843 si = SCRIPT_ITEM(sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001844 si->sn_name = fname_exp;
1845 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 if (ret_sid != NULL)
Bram Moolenaar753885b2022-08-24 16:30:36 +01001847 *ret_sid = sid;
Bram Moolenaar2b327002020-12-26 15:39:31 +01001848
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001849 // Remember the "is_vimrc" flag for when the file is sourced again.
1850 si->sn_is_vimrc = is_vimrc;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001851 }
1852
zeertzjq5a4fff42022-08-15 17:53:55 +01001853 // Keep the sourcing name/lnum, for recursive calls.
1854 estack_push(ETYPE_SCRIPT, si->sn_name, 0);
ichizok7e5fe382023-04-15 13:17:50 +01001855 ESTACK_CHECK_SETUP;
zeertzjq5a4fff42022-08-15 17:53:55 +01001856
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001857# ifdef FEAT_PROFILE
1858 if (do_profiling == PROF_YES)
1859 {
1860 int forceit;
1861
1862 // Check if we do profiling for this script.
Ernie Rael21d32122023-09-02 15:09:18 +02001863 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit, NULL))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001864 {
1865 script_do_profile(si);
1866 si->sn_pr_force = forceit;
1867 }
1868 if (si->sn_prof_on)
1869 {
1870 ++si->sn_pr_count;
1871 profile_start(&si->sn_pr_start);
1872 profile_zero(&si->sn_pr_children);
1873 }
1874 }
1875# endif
Bram Moolenaaraeef1f72022-09-15 12:20:18 +01001876#else
1877 // Keep the sourcing name/lnum, for recursive calls.
1878 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001879#endif
1880
1881 cookie.conv.vc_type = CONV_NONE; // no conversion
1882
1883 // Read the first line so we can check for a UTF-8 BOM.
1884 firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1885 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1886 && firstline[1] == 0xbb && firstline[2] == 0xbf)
1887 {
1888 // Found BOM; setup conversion, skip over BOM and recode the line.
1889 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1890 p = string_convert(&cookie.conv, firstline + 3, NULL);
1891 if (p == NULL)
1892 p = vim_strsave(firstline + 3);
1893 if (p != NULL)
1894 {
1895 vim_free(firstline);
1896 firstline = p;
1897 }
1898 }
1899
1900 // Call do_cmdline, which will call getsourceline() to get the lines.
1901 do_cmdline(firstline, getsourceline, (void *)&cookie,
1902 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1903 retval = OK;
1904
1905#ifdef FEAT_PROFILE
1906 if (do_profiling == PROF_YES)
1907 {
1908 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001909 si = SCRIPT_ITEM(sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001910 if (si->sn_prof_on)
1911 {
1912 profile_end(&si->sn_pr_start);
1913 profile_sub_wait(&wait_start, &si->sn_pr_start);
1914 profile_add(&si->sn_pr_total, &si->sn_pr_start);
1915 profile_self(&si->sn_pr_self, &si->sn_pr_start,
1916 &si->sn_pr_children);
1917 }
1918 }
1919#endif
1920
1921 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001922 emsg(_(e_interrupted));
Bram Moolenaar88774872022-08-16 20:24:29 +01001923#ifdef FEAT_EVAL
ichizok7e5fe382023-04-15 13:17:50 +01001924 ESTACK_CHECK_NOW;
Bram Moolenaar88774872022-08-16 20:24:29 +01001925#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001926 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001927 if (p_verbose > 1)
1928 {
1929 verbose_enter();
1930 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001931 if (SOURCING_NAME != NULL)
1932 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001933 verbose_leave();
1934 }
1935#ifdef STARTUPTIME
1936 if (time_fd != NULL)
1937 {
1938 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1939 time_msg((char *)IObuff, &tv_start);
1940 time_pop(&tv_rel);
1941 }
1942#endif
1943
1944 if (!got_int)
1945 trigger_source_post = TRUE;
1946
1947#ifdef FEAT_EVAL
1948 // After a "finish" in debug mode, need to break at first command of next
1949 // sourced file.
1950 if (save_debug_break_level > ex_nesting_level
1951 && debug_break_level == ex_nesting_level)
1952 ++debug_break_level;
1953#endif
1954
1955#ifdef FEAT_EVAL
1956almosttheend:
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001957 // If "sn_save_cpo" is set that means we encountered "vim9script": restore
1958 // 'cpoptions', unless in the main .vimrc file.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar753885b2022-08-24 16:30:36 +01001960 si = SCRIPT_ITEM(sid);
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001961 if (si->sn_save_cpo != NULL && si->sn_is_vimrc == DOSO_NONE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962 {
Bram Moolenaar3e191692021-03-17 17:46:00 +01001963 if (STRCMP(p_cpo, CPO_VIM) != 0)
1964 {
1965 char_u *f;
1966 char_u *t;
1967
1968 // 'cpo' was changed in the script. Apply the same change to the
1969 // saved value, if possible.
1970 for (f = (char_u *)CPO_VIM; *f != NUL; ++f)
1971 if (vim_strchr(p_cpo, *f) == NULL
1972 && (t = vim_strchr(si->sn_save_cpo, *f)) != NULL)
1973 // flag was removed, also remove it from the saved 'cpo'
1974 mch_memmove(t, t + 1, STRLEN(t));
1975 for (f = p_cpo; *f != NUL; ++f)
1976 if (vim_strchr((char_u *)CPO_VIM, *f) == NULL
1977 && vim_strchr(si->sn_save_cpo, *f) == NULL)
1978 {
1979 // flag was added, also add it to the saved 'cpo'
1980 t = alloc(STRLEN(si->sn_save_cpo) + 2);
1981 if (t != NULL)
1982 {
1983 *t = *f;
1984 STRCPY(t + 1, si->sn_save_cpo);
1985 vim_free(si->sn_save_cpo);
1986 si->sn_save_cpo = t;
1987 }
1988 }
1989 }
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001990 set_option_value_give_err((char_u *)"cpo",
1991 0L, si->sn_save_cpo, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992 }
Bram Moolenaar71eb3ad2021-12-26 12:07:30 +00001993 VIM_CLEAR(si->sn_save_cpo);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001995 restore_funccal();
1996# ifdef FEAT_PROFILE
1997 if (do_profiling == PROF_YES)
1998 prof_child_exit(&wait_start); // leaving a child now
1999# endif
Bram Moolenaara2942c72023-01-02 13:41:49 +00002000
2001 KeyTyped = save_KeyTyped;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002002#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002003 current_sctx = save_current_sctx;
2004
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002005 if (cookie.fp != NULL)
2006 fclose(cookie.fp);
2007 if (cookie.source_from_buf)
2008 ga_clear_strings(&cookie.buflines);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002009 vim_free(cookie.nextline);
2010 vim_free(firstline);
2011 convert_setup(&cookie.conv, NULL, NULL);
2012
2013 if (trigger_source_post)
2014 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
2015
2016theend:
Bram Moolenaar0af2ecf2022-08-24 17:05:56 +01002017#ifdef FEAT_EVAL
Bram Moolenaar753885b2022-08-24 16:30:36 +01002018 if (sid > 0 && ret_sid != NULL
2019 && fname_not_fixed != NULL && fname_exp != NULL)
2020 {
2021 int not_fixed_sid = find_script_by_name(fname_not_fixed);
2022
2023 // If "fname_not_fixed" is a symlink then we source the linked file.
2024 // If the original name is in the script list we add the ID of the
2025 // script that was actually sourced.
2026 if (SCRIPT_ID_VALID(not_fixed_sid) && not_fixed_sid != sid)
2027 SCRIPT_ITEM(not_fixed_sid)->sn_sourced_sid = sid;
2028 }
Bram Moolenaar0af2ecf2022-08-24 17:05:56 +01002029#endif
Bram Moolenaar753885b2022-08-24 16:30:36 +01002030
2031 vim_free(fname_not_fixed);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002032 vim_free(fname_exp);
Bram Moolenaar2a9b62d2022-02-12 13:30:17 +00002033 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02002034#ifdef FEAT_EVAL
Bram Moolenaarf0a40692021-06-11 22:05:47 +02002035 estack_compiling = save_estack_compiling;
Bram Moolenaar8de901e2021-06-11 22:21:24 +02002036#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002037 return retval;
2038}
2039
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002040 int
2041do_source(
2042 char_u *fname,
2043 int check_other, // check for .vimrc and _vimrc
2044 int is_vimrc, // DOSO_ value
Bram Moolenaar753885b2022-08-24 16:30:36 +01002045 int *ret_sid)
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002046{
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +00002047 return do_source_ext(fname, check_other, is_vimrc, ret_sid, NULL, FALSE);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002048}
2049
2050
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002051#if defined(FEAT_EVAL) || defined(PROTO)
2052
2053/*
2054 * ":scriptnames"
2055 */
2056 void
2057ex_scriptnames(exarg_T *eap)
2058{
2059 int i;
2060
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002061 if (eap->addr_count > 0 || *eap->arg != NUL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002062 {
2063 // :script {scriptId}: edit the script
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002064 if (eap->addr_count > 0 && !SCRIPT_ID_VALID(eap->line2))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002065 emsg(_(e_invalid_argument));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002066 else
2067 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002068 if (eap->addr_count > 0)
2069 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
2070 else
2071 {
2072 expand_env(eap->arg, NameBuff, MAXPATHL);
2073 eap->arg = NameBuff;
2074 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002075 do_exedit(eap, NULL);
2076 }
2077 return;
2078 }
2079
2080 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar6079da72022-01-18 14:16:59 +00002081 {
2082 scriptitem_T *si = SCRIPT_ITEM(i);
2083
2084 if (si->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002085 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01002086 char sourced_buf[20];
2087
Bram Moolenaar6079da72022-01-18 14:16:59 +00002088 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar753885b2022-08-24 16:30:36 +01002089 if (si->sn_sourced_sid > 0)
2090 vim_snprintf(sourced_buf, 20, "->%d", si->sn_sourced_sid);
2091 else
2092 sourced_buf[0] = NUL;
2093 vim_snprintf((char *)IObuff, IOSIZE, "%3d%s%s: %s",
Bram Moolenaar6079da72022-01-18 14:16:59 +00002094 i,
Bram Moolenaar753885b2022-08-24 16:30:36 +01002095 sourced_buf,
Bram Moolenaar6079da72022-01-18 14:16:59 +00002096 si->sn_state == SN_STATE_NOT_LOADED ? " A" : "",
2097 NameBuff);
Bram Moolenaar769f5892022-02-09 14:31:05 +00002098 if (!message_filtered(IObuff))
2099 {
2100 msg_putchar('\n');
2101 msg_outtrans(IObuff);
2102 out_flush(); // output one line at a time
2103 ui_breakcheck();
2104 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002105 }
Bram Moolenaar6079da72022-01-18 14:16:59 +00002106 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002107}
2108
2109# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2110/*
2111 * Fix slashes in the list of script names for 'shellslash'.
2112 */
2113 void
2114scriptnames_slash_adjust(void)
2115{
2116 int i;
2117
2118 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002119 if (SCRIPT_ITEM(i)->sn_name != NULL)
2120 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002121}
2122# endif
2123
2124/*
2125 * Get a pointer to a script name. Used for ":verbose set".
Bram Moolenaar74667062020-12-28 15:41:41 +01002126 * Message appended to "Last set from "
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002127 */
2128 char_u *
2129get_scriptname(scid_T id)
2130{
2131 if (id == SID_MODELINE)
2132 return (char_u *)_("modeline");
2133 if (id == SID_CMDARG)
2134 return (char_u *)_("--cmd argument");
2135 if (id == SID_CARG)
2136 return (char_u *)_("-c argument");
2137 if (id == SID_ENV)
2138 return (char_u *)_("environment variable");
2139 if (id == SID_ERROR)
2140 return (char_u *)_("error handler");
Bram Moolenaar74667062020-12-28 15:41:41 +01002141 if (id == SID_WINLAYOUT)
2142 return (char_u *)_("changed window size");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002143 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002144}
2145
2146# if defined(EXITFREE) || defined(PROTO)
2147 void
2148free_scriptnames(void)
2149{
2150 int i;
2151
2152 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02002153 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002154 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002156 // the variables themselves are cleared in evalvars_clear()
2157 vim_free(si->sn_vars);
2158
2159 vim_free(si->sn_name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02002160 free_imports_and_script_vars(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002161 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02002162# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002163 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02002164# endif
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002165 vim_free(si->sn_autoload_prefix);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002166 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02002167 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002168 ga_clear(&script_items);
2169}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002170
2171 void
2172free_autoload_scriptnames(void)
2173{
2174 ga_clear_strings(&ga_loaded);
2175}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002176# endif
2177
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002178 linenr_T
Bram Moolenaar66250c92020-08-20 15:02:42 +02002179get_sourced_lnum(
2180 char_u *(*fgetline)(int, void *, int, getline_opt_T),
2181 void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002182{
2183 return fgetline == getsourceline
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002184 ? ((source_cookie_T *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002185 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002186}
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002187
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002188/*
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002189 * Return a List of script-local functions defined in the script with id
2190 * 'sid'.
2191 */
2192 static list_T *
2193get_script_local_funcs(scid_T sid)
2194{
2195 hashtab_T *functbl;
2196 hashitem_T *hi;
2197 long_u todo;
2198 list_T *l;
2199
2200 l = list_alloc();
2201 if (l == NULL)
2202 return NULL;
2203
2204 // Iterate through all the functions in the global function hash table
2205 // looking for functions with script ID 'sid'.
2206 functbl = func_tbl_get();
2207 todo = functbl->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002208 FOR_ALL_HASHTAB_ITEMS(functbl, hi, todo)
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002209 {
2210 ufunc_T *fp;
2211
2212 if (HASHITEM_EMPTY(hi))
2213 continue;
2214
2215 --todo;
2216 fp = HI2UF(hi);
2217
2218 // Add active functions with script id == 'sid'
2219 if (!(fp->uf_flags & FC_DEAD) && (fp->uf_script_ctx.sc_sid == sid))
2220 {
2221 char_u *name;
2222
2223 if (fp->uf_name_exp != NULL)
2224 name = fp->uf_name_exp;
2225 else
2226 name = fp->uf_name;
2227
2228 list_append_string(l, name, -1);
2229 }
2230 }
2231
2232 return l;
2233}
2234
2235/*
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002236 * getscriptinfo() function
2237 */
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002238 void
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002239f_getscriptinfo(typval_T *argvars, typval_T *rettv)
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002240{
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002241 list_T *l;
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002242 char_u *pat = NULL;
2243 regmatch_T regmatch;
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002244 int filterpat = FALSE;
2245 scid_T sid = -1;
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002246
2247 if (rettv_list_alloc(rettv) == FAIL)
2248 return;
2249
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002250 if (check_for_opt_dict_arg(argvars, 0) == FAIL)
2251 return;
2252
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002253 l = rettv->vval.v_list;
2254
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002255 regmatch.regprog = NULL;
2256 regmatch.rm_ic = p_ic;
2257
2258 if (argvars[0].v_type == VAR_DICT)
2259 {
zeertzjq2d68b722023-03-30 21:50:37 +01002260 dictitem_T *sid_di = dict_find(argvars[0].vval.v_dict,
2261 (char_u *)"sid", 3);
2262 if (sid_di != NULL)
2263 {
2264 int error = FALSE;
2265 sid = tv_get_number_chk(&sid_di->di_tv, &error);
2266 if (error)
2267 return;
2268 if (sid <= 0)
2269 {
zeertzjq276410e2023-05-07 21:59:33 +01002270 semsg(_(e_invalid_value_for_argument_str_str), "sid",
zeertzjq2d68b722023-03-30 21:50:37 +01002271 tv_get_string(&sid_di->di_tv));
2272 return;
2273 }
2274 }
2275 else
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002276 {
2277 pat = dict_get_string(argvars[0].vval.v_dict, "name", TRUE);
2278 if (pat != NULL)
2279 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2280 if (regmatch.regprog != NULL)
2281 filterpat = TRUE;
2282 }
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002283 }
2284
zeertzjq2d68b722023-03-30 21:50:37 +01002285 for (varnumber_T i = sid > 0 ? sid : 1;
2286 (i == sid || sid <= 0) && i <= script_items.ga_len; ++i)
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002287 {
2288 scriptitem_T *si = SCRIPT_ITEM(i);
2289 dict_T *d;
2290
2291 if (si->sn_name == NULL)
2292 continue;
2293
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002294 if (filterpat && !vim_regexec(&regmatch, si->sn_name, (colnr_T)0))
2295 continue;
2296
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002297 if ((d = dict_alloc()) == NULL
2298 || list_append_dict(l, d) == FAIL
2299 || dict_add_string(d, "name", si->sn_name) == FAIL
2300 || dict_add_number(d, "sid", i) == FAIL
Bram Moolenaar753885b2022-08-24 16:30:36 +01002301 || dict_add_number(d, "sourced", si->sn_sourced_sid) == FAIL
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002302 || dict_add_number(d, "version", si->sn_version) == FAIL
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002303 || dict_add_bool(d, "autoload",
2304 si->sn_state == SN_STATE_NOT_LOADED) == FAIL)
2305 return;
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002306
zeertzjq2d68b722023-03-30 21:50:37 +01002307 // When a script ID is specified, return information about only the
2308 // specified script, and add the script-local variables and functions.
2309 if (sid > 0)
Yegappan Lakshmanan2f892d82022-08-28 18:52:10 +01002310 {
2311 dict_T *var_dict;
2312
2313 var_dict = dict_copy(&si->sn_vars->sv_dict, TRUE, TRUE,
2314 get_copyID());
2315 if (var_dict == NULL
2316 || dict_add_dict(d, "variables", var_dict) == FAIL
2317 || dict_add_list(d, "functions",
2318 get_script_local_funcs(sid)) == FAIL)
2319 return;
2320 }
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002321 }
Yegappan Lakshmanan520f6ef2022-08-25 17:40:40 +01002322
2323 vim_regfree(regmatch.regprog);
2324 vim_free(pat);
Yegappan Lakshmananf768c3d2022-08-22 13:15:13 +01002325}
2326
Dominique Pelle748b3082022-01-08 12:41:16 +00002327#endif
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002328
2329 static char_u *
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002330get_one_sourceline(source_cookie_T *sp)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002331{
2332 garray_T ga;
2333 int len;
2334 int c;
2335 char_u *buf;
2336#ifdef USE_CRNL
2337 int has_cr; // CR-LF found
2338#endif
2339 int have_read = FALSE;
2340
2341 // use a growarray to store the sourced line
2342 ga_init2(&ga, 1, 250);
2343
2344 // Loop until there is a finished line (or end-of-file).
2345 ++sp->sourcing_lnum;
2346 for (;;)
2347 {
2348 // make room to read at least 120 (more) characters
2349 if (ga_grow(&ga, 120) == FAIL)
2350 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002351 if (sp->source_from_buf)
2352 {
2353 if (sp->buf_lnum >= sp->buflines.ga_len)
2354 break; // all the lines are processed
2355 ga_concat(&ga, ((char_u **)sp->buflines.ga_data)[sp->buf_lnum]);
2356 sp->buf_lnum++;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01002357 if (ga_grow(&ga, 1) == FAIL)
2358 break;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002359 buf = (char_u *)ga.ga_data;
Bram Moolenaar2bdad612022-03-29 19:52:12 +01002360 buf[ga.ga_len++] = NUL;
Bram Moolenaar4748c4b2022-05-17 17:47:07 +01002361 len = ga.ga_len;
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002362 }
2363 else
2364 {
2365 buf = (char_u *)ga.ga_data;
2366 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
2367 sp->fp) == NULL)
2368 break;
Bram Moolenaar4748c4b2022-05-17 17:47:07 +01002369 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002370 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002371#ifdef USE_CRNL
2372 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
2373 // CTRL-Z by its own, or after a NL.
2374 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
2375 && sp->fileformat == EOL_DOS
2376 && buf[len - 1] == Ctrl_Z)
2377 {
2378 buf[len - 1] = NUL;
2379 break;
2380 }
2381#endif
2382
2383 have_read = TRUE;
2384 ga.ga_len = len;
2385
2386 // If the line was longer than the buffer, read more.
2387 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
2388 continue;
2389
2390 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
2391 {
2392#ifdef USE_CRNL
2393 has_cr = (len >= 2 && buf[len - 2] == '\r');
2394 if (sp->fileformat == EOL_UNKNOWN)
2395 {
2396 if (has_cr)
2397 sp->fileformat = EOL_DOS;
2398 else
2399 sp->fileformat = EOL_UNIX;
2400 }
2401
2402 if (sp->fileformat == EOL_DOS)
2403 {
2404 if (has_cr) // replace trailing CR
2405 {
2406 buf[len - 2] = '\n';
2407 --len;
2408 --ga.ga_len;
2409 }
2410 else // lines like ":map xx yy^M" will have failed
2411 {
2412 if (!sp->error)
2413 {
2414 msg_source(HL_ATTR(HLF_W));
2415 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
2416 }
2417 sp->error = TRUE;
2418 sp->fileformat = EOL_UNIX;
2419 }
2420 }
2421#endif
2422 // The '\n' is escaped if there is an odd number of ^V's just
2423 // before it, first set "c" just before the 'V's and then check
2424 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
2425 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
2426 ;
2427 if ((len & 1) != (c & 1)) // escaped NL, read more
2428 {
2429 ++sp->sourcing_lnum;
2430 continue;
2431 }
2432
2433 buf[len - 1] = NUL; // remove the NL
2434 }
2435
2436 // Check for ^C here now and then, so recursive :so can be broken.
2437 line_breakcheck();
2438 break;
2439 }
2440
2441 if (have_read)
2442 return (char_u *)ga.ga_data;
2443
2444 vim_free(ga.ga_data);
2445 return NULL;
2446}
2447
2448/*
2449 * Get one full line from a sourced file.
2450 * Called by do_cmdline() when it's called from do_source().
2451 *
2452 * Return a pointer to the line in allocated memory.
2453 * Return NULL for end-of-file or some error.
2454 */
2455 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02002456getsourceline(
2457 int c UNUSED,
2458 void *cookie,
2459 int indent UNUSED,
2460 getline_opt_T options)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002461{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002462 source_cookie_T *sp = (source_cookie_T *)cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002463 char_u *line;
2464 char_u *p;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002465 int do_vim9_all = in_vim9script()
2466 && options == GETLINE_CONCAT_ALL;
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002467 int do_bar_cont = do_vim9_all
2468 || options == GETLINE_CONCAT_CONTBAR;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002469
2470#ifdef FEAT_EVAL
2471 // If breakpoints have been added/deleted need to check for it.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002472 if ((sp->dbg_tick < debug_tick) && !sp->source_from_buf)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002473 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002474 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002475 sp->dbg_tick = debug_tick;
2476 }
2477# ifdef FEAT_PROFILE
2478 if (do_profiling == PROF_YES)
2479 script_line_end();
2480# endif
2481#endif
2482
2483 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002484 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002485
2486 // Get current line. If there is a read-ahead line, use it, otherwise get
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002487 // one now. "fp" is NULL if actually using a string.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002488 if (sp->finished || (!sp->source_from_buf && sp->fp == NULL))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002489 line = NULL;
2490 else if (sp->nextline == NULL)
2491 line = get_one_sourceline(sp);
2492 else
2493 {
2494 line = sp->nextline;
2495 sp->nextline = NULL;
2496 ++sp->sourcing_lnum;
2497 }
2498#ifdef FEAT_PROFILE
2499 if (line != NULL && do_profiling == PROF_YES)
2500 script_line_start();
2501#endif
2502
2503 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
2504 // contain the 'C' flag.
Bram Moolenaar66250c92020-08-20 15:02:42 +02002505 if (line != NULL && options != GETLINE_NONE
2506 && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002507 {
Bram Moolenaar5072b472021-06-03 21:56:10 +02002508 int comment_char = in_vim9script() ? '#' : '"';
2509
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002510 // compensate for the one line read-ahead
2511 --sp->sourcing_lnum;
2512
2513 // Get the next line and concatenate it when it starts with a
2514 // backslash. We always need to read the next line, keep it in
2515 // sp->nextline.
2516 /* Also check for a comment in between continuation lines: "\ */
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002517 // Also check for a Vim9 comment, empty line, line starting with '|',
2518 // but not "||".
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002519 sp->nextline = get_one_sourceline(sp);
2520 if (sp->nextline != NULL
2521 && (*(p = skipwhite(sp->nextline)) == '\\'
Bram Moolenaar5072b472021-06-03 21:56:10 +02002522 || (p[0] == comment_char
2523 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002524 || (do_vim9_all && (*p == NUL
2525 || vim9_comment_start(p)))
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002526 || (do_bar_cont && p[0] == '|' && p[1] != '|')))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002527 {
2528 garray_T ga;
2529
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002530 ga_init2(&ga, sizeof(char_u), 400);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002531 ga_concat(&ga, line);
2532 if (*p == '\\')
2533 ga_concat(&ga, p + 1);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002534 else if (*p == '|')
2535 {
2536 ga_concat(&ga, (char_u *)" ");
2537 ga_concat(&ga, p);
2538 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002539 for (;;)
2540 {
2541 vim_free(sp->nextline);
2542 sp->nextline = get_one_sourceline(sp);
2543 if (sp->nextline == NULL)
2544 break;
2545 p = skipwhite(sp->nextline);
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01002546 if (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|'))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002547 {
2548 // Adjust the growsize to the current length to speed up
2549 // concatenating many lines.
2550 if (ga.ga_len > 400)
2551 {
2552 if (ga.ga_len > 8000)
2553 ga.ga_growsize = 8000;
2554 else
2555 ga.ga_growsize = ga.ga_len;
2556 }
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002557 if (*p == '\\')
2558 ga_concat(&ga, p + 1);
2559 else
2560 {
2561 ga_concat(&ga, (char_u *)" ");
2562 ga_concat(&ga, p);
2563 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002564 }
Bram Moolenaar5072b472021-06-03 21:56:10 +02002565 else if (!(p[0] == (comment_char)
Bram Moolenaar0f37e352021-06-02 15:28:15 +02002566 && p[1] == '\\' && p[2] == ' ')
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002567 && !(do_vim9_all && (*p == NUL || vim9_comment_start(p))))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002568 break;
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002569 /* drop a # comment or "\ comment line */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002570 }
2571 ga_append(&ga, NUL);
2572 vim_free(line);
2573 line = ga.ga_data;
2574 }
2575 }
2576
2577 if (line != NULL && sp->conv.vc_type != CONV_NONE)
2578 {
2579 char_u *s;
2580
2581 // Convert the encoding of the script line.
2582 s = string_convert(&sp->conv, line, NULL);
2583 if (s != NULL)
2584 {
2585 vim_free(line);
2586 line = s;
2587 }
2588 }
2589
2590#ifdef FEAT_EVAL
2591 // Did we encounter a breakpoint?
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002592 if (!sp->source_from_buf && sp->breakpoint != 0
2593 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002594 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002595 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002596 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002597 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002598 sp->dbg_tick = debug_tick;
2599 }
2600#endif
2601
2602 return line;
2603}
2604
2605/*
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002606 * Returns TRUE if sourcing a script either from a file or a buffer.
2607 * Otherwise returns FALSE.
2608 */
2609 int
2610sourcing_a_script(exarg_T *eap)
2611{
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002612 return (getline_equal(eap->ea_getline, eap->cookie, getsourceline));
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002613}
2614
2615/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002616 * ":scriptencoding": Set encoding conversion for a sourced script.
2617 */
2618 void
2619ex_scriptencoding(exarg_T *eap)
2620{
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002621 source_cookie_T *sp;
2622 char_u *name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002623
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002624 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002625 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00002626 emsg(_(e_scriptencoding_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002627 return;
2628 }
2629
2630 if (*eap->arg != NUL)
2631 {
2632 name = enc_canonize(eap->arg);
2633 if (name == NULL) // out of memory
2634 return;
2635 }
2636 else
2637 name = eap->arg;
2638
2639 // Setup for conversion from the specified encoding to 'encoding'.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002640 sp = (source_cookie_T *)getline_cookie(eap->ea_getline, eap->cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002641 convert_setup(&sp->conv, name, p_enc);
2642
2643 if (name != eap->arg)
2644 vim_free(name);
2645}
2646
2647/*
2648 * ":scriptversion": Set Vim script version for a sourced script.
2649 */
2650 void
2651ex_scriptversion(exarg_T *eap UNUSED)
2652{
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002653 int nr;
2654
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002655 if (!sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002656 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002657 emsg(_(e_scriptversion_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002658 return;
2659 }
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002660 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002662 emsg(_(e_cannot_use_scriptversion_after_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663 return;
2664 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002665
2666 nr = getdigits(&eap->arg);
2667 if (nr == 0 || *eap->arg != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002668 emsg(_(e_invalid_argument));
Bram Moolenaar67979662020-06-20 22:50:47 +02002669 else if (nr > SCRIPT_VERSION_MAX)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002670 semsg(_(e_scriptversion_not_supported_nr), nr);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002671 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002672 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002673 current_sctx.sc_version = nr;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002674#ifdef FEAT_EVAL
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002675 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002676#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002677 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002678}
2679
2680#if defined(FEAT_EVAL) || defined(PROTO)
2681/*
2682 * ":finish": Mark a sourced file as finished.
2683 */
2684 void
2685ex_finish(exarg_T *eap)
2686{
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +00002687 if (sourcing_a_script(eap))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002688 do_finish(eap, FALSE);
2689 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00002690 emsg(_(e_finish_used_outside_of_sourced_file));
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002691}
2692
2693/*
2694 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
2695 * Also called for a pending finish at the ":endtry" or after returning from
2696 * an extra do_cmdline(). "reanimate" is used in the latter case.
2697 */
2698 void
2699do_finish(exarg_T *eap, int reanimate)
2700{
2701 int idx;
2702
2703 if (reanimate)
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002704 ((source_cookie_T *)getline_cookie(eap->ea_getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002705 eap->cookie))->finished = FALSE;
2706
2707 // Cleanup (and inactivate) conditionals, but stop when a try conditional
2708 // not in its finally clause (which then is to be executed next) is found.
2709 // In this case, make the ":finish" pending for execution at the ":endtry".
2710 // Otherwise, finish normally.
2711 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
2712 if (idx >= 0)
2713 {
2714 eap->cstack->cs_pending[idx] = CSTP_FINISH;
2715 report_make_pending(CSTP_FINISH, NULL);
2716 }
2717 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002718 ((source_cookie_T *)getline_cookie(eap->ea_getline,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002719 eap->cookie))->finished = TRUE;
2720}
2721
2722
2723/*
2724 * Return TRUE when a sourced file had the ":finish" command: Don't give error
2725 * message for missing ":endif".
2726 * Return FALSE when not sourcing a file.
2727 */
2728 int
2729source_finished(
Bram Moolenaar66250c92020-08-20 15:02:42 +02002730 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002731 void *cookie)
2732{
2733 return (getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar9567efa2021-01-11 22:16:30 +01002734 && ((source_cookie_T *)getline_cookie(
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002735 fgetline, cookie))->finished);
2736}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002737
2738/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002739 * Find the path of a script below the "autoload" directory.
2740 * Returns NULL if there is no "/autoload/" in the script name.
2741 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01002742 static char_u *
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002743script_name_after_autoload(scriptitem_T *si)
2744{
2745 char_u *p = si->sn_name;
2746 char_u *res = NULL;
2747
2748 for (;;)
2749 {
2750 char_u *n = (char_u *)strstr((char *)p, "autoload");
2751
2752 if (n == NULL)
2753 break;
2754 if (n > p && vim_ispathsep(n[-1]) && vim_ispathsep(n[8]))
2755 res = n + 9;
2756 p = n + 8;
2757 }
2758 return res;
2759}
2760
2761/*
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002762 * For an autoload script "autoload/dir/script.vim" return the prefix
2763 * "dir#script#" in allocated memory.
2764 * Returns NULL if anything is wrong.
2765 */
2766 char_u *
2767get_autoload_prefix(scriptitem_T *si)
2768{
2769 char_u *p = script_name_after_autoload(si);
2770 char_u *prefix;
2771
2772 if (p == NULL)
2773 return NULL;
Bram Moolenaar3049fcf2022-01-13 19:25:50 +00002774 prefix = vim_strsave(p);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00002775 if (prefix == NULL)
2776 return NULL;
2777
2778 // replace all '/' with '#' and locate ".vim" at the end
2779 for (p = prefix; *p != NUL; p += mb_ptr2len(p))
2780 {
2781 if (vim_ispathsep(*p))
2782 *p = '#';
2783 else if (STRCMP(p, ".vim") == 0)
2784 {
2785 p[0] = '#';
2786 p[1] = NUL;
2787 return prefix;
2788 }
2789 }
2790
2791 // did not find ".vim" at the end
2792 vim_free(prefix);
2793 return NULL;
2794}
2795
2796/*
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002797 * If in a Vim9 autoload script return "name" with the autoload prefix for the
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002798 * script. If successful the returned name is allocated.
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002799 * Otherwise it returns "name" unmodified.
2800 */
2801 char_u *
2802may_prefix_autoload(char_u *name)
2803{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002804 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2805 return name;
2806
2807 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2808
2809 if (si->sn_autoload_prefix == NULL)
2810 return name;
2811
2812 char_u *basename = name;
2813 size_t len;
2814 char_u *res;
2815
2816 if (*name == K_SPECIAL)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002817 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002818 char_u *p = vim_strchr(name, '_');
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002819
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002820 // skip over "<SNR>99_"
2821 if (p != NULL)
2822 basename = p + 1;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002823 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002824
2825 len = STRLEN(si->sn_autoload_prefix) + STRLEN(basename) + 2;
2826 res = alloc(len);
2827 if (res == NULL)
2828 return NULL;
2829
2830 vim_snprintf((char *)res, len, "%s%s", si->sn_autoload_prefix, basename);
2831 return res;
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002832}
2833
2834/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002835 * Return the autoload script name for a function or variable name.
2836 * Returns NULL when out of memory.
2837 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
2838 */
2839 char_u *
2840autoload_name(char_u *name)
2841{
2842 char_u *p, *q = NULL;
2843 char_u *scriptname;
2844
2845 // Get the script file name: replace '#' with '/', append ".vim".
2846 scriptname = alloc(STRLEN(name) + 14);
2847 if (scriptname == NULL)
2848 return NULL;
2849 STRCPY(scriptname, "autoload/");
Bram Moolenaara1773442020-08-12 15:21:22 +02002850 STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002851 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
2852 q = p, ++p)
2853 *p = '/';
2854 STRCPY(q, ".vim");
2855 return scriptname;
2856}
2857
2858/*
2859 * If "name" has a package name try autoloading the script for it.
2860 * Return TRUE if a package was loaded.
2861 */
2862 int
2863script_autoload(
2864 char_u *name,
2865 int reload) // load script again when already loaded
2866{
2867 char_u *p;
2868 char_u *scriptname, *tofree;
2869 int ret = FALSE;
2870 int i;
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002871 int ret_sid;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002872
Bram Moolenaar89445512022-04-14 12:58:23 +01002873 // If the name starts with "<SNR>123_" then "123" is the script ID.
2874 if (name[0] == K_SPECIAL && name[1] == KS_EXTRA && name[2] == KE_SNR)
2875 {
2876 p = name + 3;
2877 ret_sid = (int)getdigits(&p);
2878 if (*p == '_' && SCRIPT_ID_VALID(ret_sid))
2879 {
2880 may_load_script(ret_sid, &ret);
2881 return ret;
2882 }
2883 }
2884
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002885 // If there is no '#' after name[0] there is no package name.
2886 p = vim_strchr(name, AUTOLOAD_CHAR);
2887 if (p == NULL || p == name)
2888 return FALSE;
2889
2890 tofree = scriptname = autoload_name(name);
2891 if (scriptname == NULL)
2892 return FALSE;
2893
2894 // Find the name in the list of previously loaded package names. Skip
2895 // "autoload/", it's always the same.
2896 for (i = 0; i < ga_loaded.ga_len; ++i)
2897 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2898 break;
2899 if (!reload && i < ga_loaded.ga_len)
2900 ret = FALSE; // was loaded already
2901 else
2902 {
2903 // Remember the name if it wasn't loaded already.
2904 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2905 {
2906 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2907 tofree = NULL;
2908 }
2909
2910 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002911 // Use "ret_sid" to avoid loading the same script again.
=?UTF-8?q?Bj=C3=B6rn=20Linse?=223a9502022-01-31 17:26:05 +00002912 if (source_in_path(p_rtp, scriptname, DIP_START, &ret_sid) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002913 ret = TRUE;
2914 }
2915
2916 vim_free(tofree);
2917 return ret;
2918}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002919#endif