blob: 75182ca511487322186de6ff54c053942b671c31 [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
Bram Moolenaar307c5a52019-08-25 15:41:00 +020021/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010022 * Initialize the execution stack.
23 */
24 void
25estack_init(void)
26{
27 estack_T *entry;
28
29 if (ga_grow(&exestack, 10) == FAIL)
30 mch_exit(0);
31 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
32 entry->es_type = ETYPE_TOP;
33 entry->es_name = NULL;
34 entry->es_lnum = 0;
Bram Moolenaar09d46402019-12-29 23:53:01 +010035#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010036 entry->es_info.ufunc = NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +010037#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010038 ++exestack.ga_len;
39}
40
41/*
42 * Add an item to the execution stack.
43 * Returns the new entry or NULL when out of memory.
44 */
45 estack_T *
46estack_push(etype_T type, char_u *name, long lnum)
47{
48 estack_T *entry;
49
50 // If memory allocation fails then we'll pop more than we push, eventually
51 // at the top level it will be OK again.
52 if (ga_grow(&exestack, 1) == OK)
53 {
54 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
55 entry->es_type = type;
56 entry->es_name = name;
57 entry->es_lnum = lnum;
Bram Moolenaar09d46402019-12-29 23:53:01 +010058#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010059 entry->es_info.ufunc = NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +010060#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010061 ++exestack.ga_len;
62 return entry;
63 }
64 return NULL;
65}
66
Bram Moolenaar09d46402019-12-29 23:53:01 +010067#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010068/*
69 * Add a user function to the execution stack.
70 */
Bram Moolenaarc620c052020-07-08 15:16:19 +020071 estack_T *
Bram Moolenaar25e0f582020-05-25 22:36:50 +020072estack_push_ufunc(ufunc_T *ufunc, long lnum)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010073{
Bram Moolenaar25e0f582020-05-25 22:36:50 +020074 estack_T *entry = estack_push(ETYPE_UFUNC,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010075 ufunc->uf_name_exp != NULL
76 ? ufunc->uf_name_exp : ufunc->uf_name, lnum);
77 if (entry != NULL)
78 entry->es_info.ufunc = ufunc;
Bram Moolenaarc620c052020-07-08 15:16:19 +020079 return entry;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010080}
Bram Moolenaar25e0f582020-05-25 22:36:50 +020081
82/*
83 * Return TRUE if "ufunc" with "lnum" is already at the top of the exe stack.
84 */
85 int
86estack_top_is_ufunc(ufunc_T *ufunc, long lnum)
87{
88 estack_T *entry;
89
90 if (exestack.ga_len == 0)
91 return FALSE;
92 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
93 return entry->es_type == ETYPE_UFUNC
94 && STRCMP( entry->es_name, ufunc->uf_name_exp != NULL
95 ? ufunc->uf_name_exp : ufunc->uf_name) == 0
96 && entry->es_lnum == lnum;
97}
Bram Moolenaar09d46402019-12-29 23:53:01 +010098#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010099
100/*
Bram Moolenaarc620c052020-07-08 15:16:19 +0200101 * Take an item off of the execution stack and return it.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100102 */
Bram Moolenaarc620c052020-07-08 15:16:19 +0200103 estack_T *
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100104estack_pop(void)
105{
Bram Moolenaarc620c052020-07-08 15:16:19 +0200106 if (exestack.ga_len == 0)
107 return NULL;
108 --exestack.ga_len;
109 return ((estack_T *)exestack.ga_data) + exestack.ga_len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100110}
111
112/*
113 * Get the current value for <sfile> in allocated memory.
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200114 * "which" is ESTACK_SFILE for <sfile> and ESTACK_STACK for <stack>.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100115 */
116 char_u *
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200117estack_sfile(estack_arg_T which UNUSED)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100118{
Bram Moolenaar85b09572019-12-30 10:57:00 +0100119 estack_T *entry;
120#ifdef FEAT_EVAL
Bram Moolenaara5d04232020-07-26 15:37:02 +0200121 garray_T ga;
Bram Moolenaar4d7a2482020-01-06 19:53:43 +0100122 size_t len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100123 int idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200124 etype_T last_type = ETYPE_SCRIPT;
125 char *type_name;
Bram Moolenaar85b09572019-12-30 10:57:00 +0100126#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100127
128 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100129#ifdef FEAT_EVAL
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200130 if (which == ESTACK_SFILE && entry->es_type != ETYPE_UFUNC)
Bram Moolenaar09d46402019-12-29 23:53:01 +0100131#endif
Bram Moolenaara5d04232020-07-26 15:37:02 +0200132 {
133 if (entry->es_name == NULL)
134 return NULL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100135 return vim_strsave(entry->es_name);
Bram Moolenaara5d04232020-07-26 15:37:02 +0200136 }
Bram Moolenaar09d46402019-12-29 23:53:01 +0100137#ifdef FEAT_EVAL
Bram Moolenaara5d04232020-07-26 15:37:02 +0200138 // Give information about each stack entry up to the root.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100139 // For a function we compose the call stack, as it was done in the past:
140 // "function One[123]..Two[456]..Three"
Bram Moolenaara5d04232020-07-26 15:37:02 +0200141 ga_init2(&ga, sizeof(char), 100);
142 for (idx = 0; idx < exestack.ga_len; ++idx)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100143 {
144 entry = ((estack_T *)exestack.ga_data) + idx;
Bram Moolenaara5d04232020-07-26 15:37:02 +0200145 if (entry->es_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100146 {
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200147 long lnum = 0;
148
Bram Moolenaara5d04232020-07-26 15:37:02 +0200149 len = STRLEN(entry->es_name) + 15;
150 type_name = "";
151 if (entry->es_type != last_type)
152 {
153 switch (entry->es_type)
154 {
155 case ETYPE_SCRIPT: type_name = "script "; break;
156 case ETYPE_UFUNC: type_name = "function "; break;
157 default: type_name = ""; break;
158 }
159 last_type = entry->es_type;
160 }
161 len += STRLEN(type_name);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200162 if (ga_grow(&ga, (int)len) == FAIL)
Bram Moolenaara5d04232020-07-26 15:37:02 +0200163 break;
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200164 if (idx == exestack.ga_len - 1)
165 lnum = which == ESTACK_STACK ? SOURCING_LNUM : 0;
166 else
167 lnum = entry->es_lnum;
168 if (lnum == 0)
169 // For the bottom entry of <sfile>: do not add the line number,
170 // it is used in <slnum>. Also leave it out when the number is
171 // not set.
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200172 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s%s",
Bram Moolenaara5d04232020-07-26 15:37:02 +0200173 type_name, entry->es_name,
174 idx == exestack.ga_len - 1 ? "" : "..");
175 else
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200176 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s[%ld]..",
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200177 type_name, entry->es_name, lnum);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200178 ga.ga_len += (int)STRLEN((char *)ga.ga_data + ga.ga_len);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100179 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100180 }
181
Bram Moolenaara5d04232020-07-26 15:37:02 +0200182 return (char_u *)ga.ga_data;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100183#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100184}
185
186/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200187 * ":runtime [what] {name}"
188 */
189 void
190ex_runtime(exarg_T *eap)
191{
192 char_u *arg = eap->arg;
193 char_u *p = skiptowhite(arg);
194 int len = (int)(p - arg);
195 int flags = eap->forceit ? DIP_ALL : 0;
196
197 if (STRNCMP(arg, "START", len) == 0)
198 {
199 flags += DIP_START + DIP_NORTP;
200 arg = skipwhite(arg + len);
201 }
202 else if (STRNCMP(arg, "OPT", len) == 0)
203 {
204 flags += DIP_OPT + DIP_NORTP;
205 arg = skipwhite(arg + len);
206 }
207 else if (STRNCMP(arg, "PACK", len) == 0)
208 {
209 flags += DIP_START + DIP_OPT + DIP_NORTP;
210 arg = skipwhite(arg + len);
211 }
212 else if (STRNCMP(arg, "ALL", len) == 0)
213 {
214 flags += DIP_START + DIP_OPT;
215 arg = skipwhite(arg + len);
216 }
217
218 source_runtime(arg, flags);
219}
220
221 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222source_callback(char_u *fname, void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200223{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200225}
226
227/*
228 * Find the file "name" in all directories in "path" and invoke
229 * "callback(fname, cookie)".
230 * "name" can contain wildcards.
231 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
232 * When "flags" has DIP_DIR: find directories instead of files.
233 * When "flags" has DIP_ERR: give an error message if there is no match.
234 *
235 * return FAIL when no file could be sourced, OK otherwise.
236 */
237 int
238do_in_path(
239 char_u *path,
240 char_u *name,
241 int flags,
242 void (*callback)(char_u *fname, void *ck),
243 void *cookie)
244{
245 char_u *rtp;
246 char_u *np;
247 char_u *buf;
248 char_u *rtp_copy;
249 char_u *tail;
250 int num_files;
251 char_u **files;
252 int i;
253 int did_one = FALSE;
254#ifdef AMIGA
255 struct Process *proc = (struct Process *)FindTask(0L);
256 APTR save_winptr = proc->pr_WindowPtr;
257
258 // Avoid a requester here for a volume that doesn't exist.
259 proc->pr_WindowPtr = (APTR)-1L;
260#endif
261
262 // Make a copy of 'runtimepath'. Invoking the callback may change the
263 // value.
264 rtp_copy = vim_strsave(path);
265 buf = alloc(MAXPATHL);
266 if (buf != NULL && rtp_copy != NULL)
267 {
Bram Moolenaar647a5302020-05-03 17:01:24 +0200268 if (p_verbose > 10 && name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200269 {
270 verbose_enter();
271 smsg(_("Searching for \"%s\" in \"%s\""),
272 (char *)name, (char *)path);
273 verbose_leave();
274 }
275
276 // Loop over all entries in 'runtimepath'.
277 rtp = rtp_copy;
278 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
279 {
280 size_t buflen;
281
282 // Copy the path from 'runtimepath' to buf[].
283 copy_option_part(&rtp, buf, MAXPATHL, ",");
284 buflen = STRLEN(buf);
285
286 // Skip after or non-after directories.
287 if (flags & (DIP_NOAFTER | DIP_AFTER))
288 {
289 int is_after = buflen >= 5
290 && STRCMP(buf + buflen - 5, "after") == 0;
291
292 if ((is_after && (flags & DIP_NOAFTER))
293 || (!is_after && (flags & DIP_AFTER)))
294 continue;
295 }
296
297 if (name == NULL)
298 {
299 (*callback)(buf, (void *) &cookie);
300 if (!did_one)
301 did_one = (cookie == NULL);
302 }
303 else if (buflen + STRLEN(name) + 2 < MAXPATHL)
304 {
305 add_pathsep(buf);
306 tail = buf + STRLEN(buf);
307
308 // Loop over all patterns in "name"
309 np = name;
310 while (*np != NUL && ((flags & DIP_ALL) || !did_one))
311 {
312 // Append the pattern from "name" to buf[].
313 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
314 "\t ");
315
Bram Moolenaar647a5302020-05-03 17:01:24 +0200316 if (p_verbose > 10)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200317 {
318 verbose_enter();
319 smsg(_("Searching for \"%s\""), buf);
320 verbose_leave();
321 }
322
323 // Expand wildcards, invoke the callback for each match.
324 if (gen_expand_wildcards(1, &buf, &num_files, &files,
325 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
326 {
327 for (i = 0; i < num_files; ++i)
328 {
329 (*callback)(files[i], cookie);
330 did_one = TRUE;
331 if (!(flags & DIP_ALL))
332 break;
333 }
334 FreeWild(num_files, files);
335 }
336 }
337 }
338 }
339 }
340 vim_free(buf);
341 vim_free(rtp_copy);
342 if (!did_one && name != NULL)
343 {
344 char *basepath = path == p_rtp ? "runtimepath" : "packpath";
345
346 if (flags & DIP_ERR)
347 semsg(_(e_dirnotf), basepath, name);
348 else if (p_verbose > 0)
349 {
350 verbose_enter();
351 smsg(_("not found in '%s': \"%s\""), basepath, name);
352 verbose_leave();
353 }
354 }
355
356#ifdef AMIGA
357 proc->pr_WindowPtr = save_winptr;
358#endif
359
360 return did_one ? OK : FAIL;
361}
362
363/*
364 * Find "name" in "path". When found, invoke the callback function for
365 * it: callback(fname, "cookie")
366 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first
367 * one is used.
368 * Returns OK when at least one match found, FAIL otherwise.
369 *
370 * If "name" is NULL calls callback for each entry in "path". Cookie is
371 * passed by reference in this case, setting it to NULL indicates that callback
372 * has done its job.
373 */
374 static int
375do_in_path_and_pp(
376 char_u *path,
377 char_u *name,
378 int flags,
379 void (*callback)(char_u *fname, void *ck),
380 void *cookie)
381{
382 int done = FAIL;
383 char_u *s;
384 int len;
385 char *start_dir = "pack/*/start/*/%s";
386 char *opt_dir = "pack/*/opt/*/%s";
387
388 if ((flags & DIP_NORTP) == 0)
389 done = do_in_path(path, name, flags, callback, cookie);
390
391 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
392 {
393 len = (int)(STRLEN(start_dir) + STRLEN(name));
394 s = alloc(len);
395 if (s == NULL)
396 return FAIL;
397 vim_snprintf((char *)s, len, start_dir, name);
398 done = do_in_path(p_pp, s, flags, callback, cookie);
399 vim_free(s);
400 }
401
402 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
403 {
404 len = (int)(STRLEN(opt_dir) + STRLEN(name));
405 s = alloc(len);
406 if (s == NULL)
407 return FAIL;
408 vim_snprintf((char *)s, len, opt_dir, name);
409 done = do_in_path(p_pp, s, flags, callback, cookie);
410 vim_free(s);
411 }
412
413 return done;
414}
415
416/*
417 * Just like do_in_path_and_pp(), using 'runtimepath' for "path".
418 */
419 int
420do_in_runtimepath(
421 char_u *name,
422 int flags,
423 void (*callback)(char_u *fname, void *ck),
424 void *cookie)
425{
426 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
427}
428
429/*
430 * Source the file "name" from all directories in 'runtimepath'.
431 * "name" can contain wildcards.
432 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
433 *
434 * return FAIL when no file could be sourced, OK otherwise.
435 */
436 int
437source_runtime(char_u *name, int flags)
438{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100439 return source_in_path(p_rtp, name, flags, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200440}
441
442/*
443 * Just like source_runtime(), but use "path" instead of 'runtimepath'.
444 */
445 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100446source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200447{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100448 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200449}
450
451
452#if defined(FEAT_EVAL) || defined(PROTO)
453
454/*
455 * Expand wildcards in "pat" and invoke do_source() for each match.
456 */
457 static void
458source_all_matches(char_u *pat)
459{
460 int num_files;
461 char_u **files;
462 int i;
463
464 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK)
465 {
466 for (i = 0; i < num_files; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100467 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200468 FreeWild(num_files, files);
469 }
470}
471
472/*
473 * Add the package directory to 'runtimepath'.
474 */
475 static int
476add_pack_dir_to_rtp(char_u *fname)
477{
478 char_u *p4, *p3, *p2, *p1, *p;
479 char_u *entry;
480 char_u *insp = NULL;
481 int c;
482 char_u *new_rtp;
483 int keep;
484 size_t oldlen;
485 size_t addlen;
486 size_t new_rtp_len;
487 char_u *afterdir = NULL;
488 size_t afterlen = 0;
489 char_u *after_insp = NULL;
490 char_u *ffname = NULL;
491 size_t fname_len;
492 char_u *buf = NULL;
493 char_u *rtp_ffname;
494 int match;
495 int retval = FAIL;
496
497 p4 = p3 = p2 = p1 = get_past_head(fname);
498 for (p = p1; *p; MB_PTR_ADV(p))
499 if (vim_ispathsep_nocolon(*p))
500 {
501 p4 = p3; p3 = p2; p2 = p1; p1 = p;
502 }
503
504 // now we have:
505 // rtp/pack/name/start/name
506 // p4 p3 p2 p1
507 //
508 // find the part up to "pack" in 'runtimepath'
509 c = *++p4; // append pathsep in order to expand symlink
510 *p4 = NUL;
511 ffname = fix_fname(fname);
512 *p4 = c;
513 if (ffname == NULL)
514 return FAIL;
515
516 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
517 // Also stop at the first "after" directory.
518 fname_len = STRLEN(ffname);
519 buf = alloc(MAXPATHL);
520 if (buf == NULL)
521 goto theend;
522 for (entry = p_rtp; *entry != NUL; )
523 {
524 char_u *cur_entry = entry;
525
526 copy_option_part(&entry, buf, MAXPATHL, ",");
527 if (insp == NULL)
528 {
529 add_pathsep(buf);
530 rtp_ffname = fix_fname(buf);
531 if (rtp_ffname == NULL)
532 goto theend;
533 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
534 vim_free(rtp_ffname);
535 if (match)
536 // Insert "ffname" after this entry (and comma).
537 insp = entry;
538 }
539
540 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
541 && p > buf
542 && vim_ispathsep(p[-1])
543 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
544 {
545 if (insp == NULL)
546 // Did not find "ffname" before the first "after" directory,
547 // insert it before this entry.
548 insp = cur_entry;
549 after_insp = cur_entry;
550 break;
551 }
552 }
553
554 if (insp == NULL)
555 // Both "fname" and "after" not found, append at the end.
556 insp = p_rtp + STRLEN(p_rtp);
557
558 // check if rtp/pack/name/start/name/after exists
559 afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
560 if (afterdir != NULL && mch_isdir(afterdir))
561 afterlen = STRLEN(afterdir) + 1; // add one for comma
562
563 oldlen = STRLEN(p_rtp);
564 addlen = STRLEN(fname) + 1; // add one for comma
565 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
566 if (new_rtp == NULL)
567 goto theend;
568
569 // We now have 'rtp' parts: {keep}{keep_after}{rest}.
570 // Create new_rtp, first: {keep},{fname}
571 keep = (int)(insp - p_rtp);
572 mch_memmove(new_rtp, p_rtp, keep);
573 new_rtp_len = keep;
574 if (*insp == NUL)
575 new_rtp[new_rtp_len++] = ','; // add comma before
576 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
577 new_rtp_len += addlen - 1;
578 if (*insp != NUL)
579 new_rtp[new_rtp_len++] = ','; // add comma after
580
581 if (afterlen > 0 && after_insp != NULL)
582 {
583 int keep_after = (int)(after_insp - p_rtp);
584
585 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
586 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
587 keep_after - keep);
588 new_rtp_len += keep_after - keep;
589 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
590 new_rtp_len += afterlen - 1;
591 new_rtp[new_rtp_len++] = ',';
592 keep = keep_after;
593 }
594
595 if (p_rtp[keep] != NUL)
596 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
597 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
598 else
599 new_rtp[new_rtp_len] = NUL;
600
601 if (afterlen > 0 && after_insp == NULL)
602 {
603 // Append afterdir when "after" was not found:
604 // {keep},{fname}{rest},{afterdir}
605 STRCAT(new_rtp, ",");
606 STRCAT(new_rtp, afterdir);
607 }
608
609 set_option_value((char_u *)"rtp", 0L, new_rtp, 0);
610 vim_free(new_rtp);
611 retval = OK;
612
613theend:
614 vim_free(buf);
615 vim_free(ffname);
616 vim_free(afterdir);
617 return retval;
618}
619
620/*
621 * Load scripts in "plugin" and "ftdetect" directories of the package.
622 */
623 static int
624load_pack_plugin(char_u *fname)
625{
626 static char *plugpat = "%s/plugin/**/*.vim";
627 static char *ftpat = "%s/ftdetect/*.vim";
628 int len;
629 char_u *ffname = fix_fname(fname);
630 char_u *pat = NULL;
631 int retval = FAIL;
632
633 if (ffname == NULL)
634 return FAIL;
635 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
636 pat = alloc(len);
637 if (pat == NULL)
638 goto theend;
639 vim_snprintf((char *)pat, len, plugpat, ffname);
640 source_all_matches(pat);
641
642 {
643 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
644
645 // If runtime/filetype.vim wasn't loaded yet, the scripts will be
646 // found when it loads.
647 if (cmd != NULL && eval_to_number(cmd) > 0)
648 {
649 do_cmdline_cmd((char_u *)"augroup filetypedetect");
650 vim_snprintf((char *)pat, len, ftpat, ffname);
651 source_all_matches(pat);
652 do_cmdline_cmd((char_u *)"augroup END");
653 }
654 vim_free(cmd);
655 }
656 vim_free(pat);
657 retval = OK;
658
659theend:
660 vim_free(ffname);
661 return retval;
662}
663
664// used for "cookie" of add_pack_plugin()
665static int APP_ADD_DIR;
666static int APP_LOAD;
667static int APP_BOTH;
668
669 static void
670add_pack_plugin(char_u *fname, void *cookie)
671{
672 if (cookie != &APP_LOAD)
673 {
674 char_u *buf = alloc(MAXPATHL);
675 char_u *p;
676 int found = FALSE;
677
678 if (buf == NULL)
679 return;
680 p = p_rtp;
681 while (*p != NUL)
682 {
683 copy_option_part(&p, buf, MAXPATHL, ",");
684 if (pathcmp((char *)buf, (char *)fname, -1) == 0)
685 {
686 found = TRUE;
687 break;
688 }
689 }
690 vim_free(buf);
691 if (!found)
692 // directory is not yet in 'runtimepath', add it
693 if (add_pack_dir_to_rtp(fname) == FAIL)
694 return;
695 }
696
697 if (cookie != &APP_ADD_DIR)
698 load_pack_plugin(fname);
699}
700
701/*
702 * Add all packages in the "start" directory to 'runtimepath'.
703 */
704 void
705add_pack_start_dirs(void)
706{
707 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
708 add_pack_plugin, &APP_ADD_DIR);
709}
710
711/*
712 * Load plugins from all packages in the "start" directory.
713 */
714 void
715load_start_packages(void)
716{
717 did_source_packages = TRUE;
718 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
719 add_pack_plugin, &APP_LOAD);
720}
721
722/*
723 * ":packloadall"
724 * Find plugins in the package directories and source them.
725 */
726 void
727ex_packloadall(exarg_T *eap)
728{
729 if (!did_source_packages || eap->forceit)
730 {
731 // First do a round to add all directories to 'runtimepath', then load
732 // the plugins. This allows for plugins to use an autoload directory
733 // of another plugin.
734 add_pack_start_dirs();
735 load_start_packages();
736 }
737}
738
739/*
740 * ":packadd[!] {name}"
741 */
742 void
743ex_packadd(exarg_T *eap)
744{
745 static char *plugpat = "pack/*/%s/%s";
746 int len;
747 char *pat;
748 int round;
749 int res = OK;
750
751 // Round 1: use "start", round 2: use "opt".
752 for (round = 1; round <= 2; ++round)
753 {
754 // Only look under "start" when loading packages wasn't done yet.
755 if (round == 1 && did_source_packages)
756 continue;
757
758 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
759 pat = alloc(len);
760 if (pat == NULL)
761 return;
762 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
763 // The first round don't give a "not found" error, in the second round
764 // only when nothing was found in the first round.
765 res = do_in_path(p_pp, (char_u *)pat,
766 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
767 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
768 vim_free(pat);
769 }
770}
771#endif
772
773/*
Bram Moolenaar26262f82019-09-04 20:59:15 +0200774 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
775 * list of file names in allocated memory.
776 */
777 void
778remove_duplicates(garray_T *gap)
779{
780 int i;
781 int j;
782 char_u **fnames = (char_u **)gap->ga_data;
783
784 sort_strings(fnames, gap->ga_len);
785 for (i = gap->ga_len - 1; i > 0; --i)
786 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
787 {
788 vim_free(fnames[i]);
789 for (j = i + 1; j < gap->ga_len; ++j)
790 fnames[j - 1] = fnames[j];
791 --gap->ga_len;
792 }
793}
794
795/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200796 * Expand color scheme, compiler or filetype names.
797 * Search from 'runtimepath':
798 * 'runtimepath'/{dirnames}/{pat}.vim
799 * When "flags" has DIP_START: search also from 'start' of 'packpath':
800 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
801 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
802 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
803 * "dirnames" is an array with one or more directory names.
804 */
805 int
806ExpandRTDir(
807 char_u *pat,
808 int flags,
809 int *num_file,
810 char_u ***file,
811 char *dirnames[])
812{
813 char_u *s;
814 char_u *e;
815 char_u *match;
816 garray_T ga;
817 int i;
818 int pat_len;
819
820 *num_file = 0;
821 *file = NULL;
822 pat_len = (int)STRLEN(pat);
823 ga_init2(&ga, (int)sizeof(char *), 10);
824
825 for (i = 0; dirnames[i] != NULL; ++i)
826 {
827 s = alloc(STRLEN(dirnames[i]) + pat_len + 7);
828 if (s == NULL)
829 {
830 ga_clear_strings(&ga);
831 return FAIL;
832 }
833 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
834 globpath(p_rtp, s, &ga, 0);
835 vim_free(s);
836 }
837
838 if (flags & DIP_START) {
839 for (i = 0; dirnames[i] != NULL; ++i)
840 {
841 s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
842 if (s == NULL)
843 {
844 ga_clear_strings(&ga);
845 return FAIL;
846 }
847 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
848 globpath(p_pp, s, &ga, 0);
849 vim_free(s);
850 }
851 }
852
853 if (flags & DIP_OPT) {
854 for (i = 0; dirnames[i] != NULL; ++i)
855 {
856 s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
857 if (s == NULL)
858 {
859 ga_clear_strings(&ga);
860 return FAIL;
861 }
862 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
863 globpath(p_pp, s, &ga, 0);
864 vim_free(s);
865 }
866 }
867
868 for (i = 0; i < ga.ga_len; ++i)
869 {
870 match = ((char_u **)ga.ga_data)[i];
871 s = match;
872 e = s + STRLEN(s);
873 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
874 {
875 e -= 4;
876 for (s = e; s > match; MB_PTR_BACK(match, s))
877 if (s < match || vim_ispathsep(*s))
878 break;
879 ++s;
880 *e = NUL;
881 mch_memmove(match, s, e - s + 1);
882 }
883 }
884
885 if (ga.ga_len == 0)
886 return FAIL;
887
888 // Sort and remove duplicates which can happen when specifying multiple
889 // directories in dirnames.
890 remove_duplicates(&ga);
891
892 *file = ga.ga_data;
893 *num_file = ga.ga_len;
894 return OK;
895}
896
897/*
898 * Expand loadplugin names:
899 * 'packpath'/pack/ * /opt/{pat}
900 */
901 int
902ExpandPackAddDir(
903 char_u *pat,
904 int *num_file,
905 char_u ***file)
906{
907 char_u *s;
908 char_u *e;
909 char_u *match;
910 garray_T ga;
911 int i;
912 int pat_len;
913
914 *num_file = 0;
915 *file = NULL;
916 pat_len = (int)STRLEN(pat);
917 ga_init2(&ga, (int)sizeof(char *), 10);
918
919 s = alloc(pat_len + 26);
920 if (s == NULL)
921 {
922 ga_clear_strings(&ga);
923 return FAIL;
924 }
925 sprintf((char *)s, "pack/*/opt/%s*", pat);
926 globpath(p_pp, s, &ga, 0);
927 vim_free(s);
928
929 for (i = 0; i < ga.ga_len; ++i)
930 {
931 match = ((char_u **)ga.ga_data)[i];
932 s = gettail(match);
933 e = s + STRLEN(s);
934 mch_memmove(match, s, e - s + 1);
935 }
936
937 if (ga.ga_len == 0)
938 return FAIL;
939
940 // Sort and remove duplicates which can happen when specifying multiple
941 // directories in dirnames.
942 remove_duplicates(&ga);
943
944 *file = ga.ga_data;
945 *num_file = ga.ga_len;
946 return OK;
947}
948
949 static void
950cmd_source(char_u *fname, exarg_T *eap)
951{
952 if (*fname == NUL)
953 emsg(_(e_argreq));
954
955 else if (eap != NULL && eap->forceit)
956 // ":source!": read Normal mode commands
957 // Need to execute the commands directly. This is required at least
958 // for:
959 // - ":g" command busy
960 // - after ":argdo", ":windo" or ":bufdo"
961 // - another command follows
962 // - inside a loop
963 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
964#ifdef FEAT_EVAL
965 || eap->cstack->cs_idx >= 0
966#endif
967 );
968
969 // ":source" read ex commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200971 semsg(_(e_notopen), fname);
972}
973
974/*
975 * ":source {fname}"
976 */
977 void
978ex_source(exarg_T *eap)
979{
980#ifdef FEAT_BROWSE
981 if (cmdmod.browse)
982 {
983 char_u *fname = NULL;
984
985 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
986 NULL, NULL,
987 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
988 if (fname != NULL)
989 {
990 cmd_source(fname, eap);
991 vim_free(fname);
992 }
993 }
994 else
995#endif
996 cmd_source(eap->arg, eap);
997}
998
999#if defined(FEAT_EVAL) || defined(PROTO)
1000/*
1001 * ":options"
1002 */
1003 void
1004ex_options(
1005 exarg_T *eap UNUSED)
1006{
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001007 char_u buf[500];
1008 int multi_mods = 0;
1009
1010 buf[0] = NUL;
1011 (void)add_win_cmd_modifers(buf, &multi_mods);
1012
1013 vim_setenv((char_u *)"OPTWIN_CMD", buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001014 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
1015}
1016#endif
1017
1018/*
1019 * ":source" and associated commands.
1020 */
1021/*
1022 * Structure used to store info for each sourced file.
1023 * It is shared between do_source() and getsourceline().
1024 * This is required, because it needs to be handed to do_cmdline() and
1025 * sourcing can be done recursively.
1026 */
1027struct source_cookie
1028{
1029 FILE *fp; // opened file for sourcing
1030 char_u *nextline; // if not NULL: line that was read ahead
1031 linenr_T sourcing_lnum; // line number of the source file
1032 int finished; // ":finish" used
1033#ifdef USE_CRNL
1034 int fileformat; // EOL_UNKNOWN, EOL_UNIX or EOL_DOS
1035 int error; // TRUE if LF found after CR-LF
1036#endif
1037#ifdef FEAT_EVAL
1038 linenr_T breakpoint; // next line with breakpoint or zero
1039 char_u *fname; // name of sourced file
1040 int dbg_tick; // debug_tick when breakpoint was set
1041 int level; // top nesting level of sourced file
1042#endif
1043 vimconv_T conv; // type of conversion
1044};
1045
1046#ifdef FEAT_EVAL
1047/*
1048 * Return the address holding the next breakpoint line for a source cookie.
1049 */
1050 linenr_T *
1051source_breakpoint(void *cookie)
1052{
1053 return &((struct source_cookie *)cookie)->breakpoint;
1054}
1055
1056/*
1057 * Return the address holding the debug tick for a source cookie.
1058 */
1059 int *
1060source_dbg_tick(void *cookie)
1061{
1062 return &((struct source_cookie *)cookie)->dbg_tick;
1063}
1064
1065/*
1066 * Return the nesting level for a source cookie.
1067 */
1068 int
1069source_level(void *cookie)
1070{
1071 return ((struct source_cookie *)cookie)->level;
1072}
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001073
1074/*
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02001075 * Return the readahead line. Note that the pointer may become invalid when
1076 * getting the next line, if it's concatenated with the next one.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001077 */
1078 char_u *
1079source_nextline(void *cookie)
1080{
1081 return ((struct source_cookie *)cookie)->nextline;
1082}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001083#endif
1084
1085#if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC)
1086# define USE_FOPEN_NOINH
1087/*
1088 * Special function to open a file without handle inheritance.
1089 * When possible the handle is closed on exec().
1090 */
1091 static FILE *
1092fopen_noinh_readbin(char *filename)
1093{
1094# ifdef MSWIN
1095 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
1096# else
1097 int fd_tmp = mch_open(filename, O_RDONLY, 0);
1098# endif
1099
1100 if (fd_tmp == -1)
1101 return NULL;
1102
1103# ifdef HAVE_FD_CLOEXEC
1104 {
1105 int fdflags = fcntl(fd_tmp, F_GETFD);
1106 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1107 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
1108 }
1109# endif
1110
1111 return fdopen(fd_tmp, READBIN);
1112}
1113#endif
1114
1115/*
1116 * do_source: Read the file "fname" and execute its lines as EX commands.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001117 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1118 * again.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001119 *
1120 * This function may be called recursively!
1121 *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122 * Return FAIL if file could not be opened, OK otherwise.
1123 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001124 */
1125 int
1126do_source(
1127 char_u *fname,
1128 int check_other, // check for .vimrc and _vimrc
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129 int is_vimrc, // DOSO_ value
1130 int *ret_sid UNUSED)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001131{
1132 struct source_cookie cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001133 char_u *p;
1134 char_u *fname_exp;
1135 char_u *firstline = NULL;
1136 int retval = FAIL;
1137#ifdef FEAT_EVAL
1138 sctx_T save_current_sctx;
1139 static scid_T last_current_SID = 0;
1140 static int last_current_SID_seq = 0;
1141 funccal_entry_T funccalp_entry;
1142 int save_debug_break_level = debug_break_level;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001143 int sid;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001144 scriptitem_T *si = NULL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001145#endif
1146#ifdef STARTUPTIME
1147 struct timeval tv_rel;
1148 struct timeval tv_start;
1149#endif
1150#ifdef FEAT_PROFILE
1151 proftime_T wait_start;
1152#endif
1153 int trigger_source_post = FALSE;
Bram Moolenaare31ee862020-01-07 20:59:34 +01001154 ESTACK_CHECK_DECLARATION
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001155
1156 p = expand_env_save(fname);
1157 if (p == NULL)
1158 return retval;
1159 fname_exp = fix_fname(p);
1160 vim_free(p);
1161 if (fname_exp == NULL)
1162 return retval;
1163 if (mch_isdir(fname_exp))
1164 {
1165 smsg(_("Cannot source a directory: \"%s\""), fname);
1166 goto theend;
1167 }
1168
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001169#ifdef FEAT_EVAL
1170 // See if we loaded this script before.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171 for (sid = script_items.ga_len; sid > 0; --sid)
1172 {
Bram Moolenaar978d1702020-01-26 17:38:12 +01001173 // We used to check inode here, but that doesn't work:
1174 // - If a script is edited and written, it may get a different
1175 // inode number, even though to the user it is the same script.
1176 // - If a script is deleted and another script is written, with a
1177 // different name, the inode may be re-used.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001178 si = SCRIPT_ITEM(sid);
Bram Moolenaar978d1702020-01-26 17:38:12 +01001179 if (si->sn_name != NULL && fnamecmp(si->sn_name, fname_exp) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 // Found it!
1181 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 }
1183 if (sid > 0 && ret_sid != NULL)
1184 {
1185 // Already loaded and no need to load again, return here.
1186 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001187 retval = OK;
1188 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189 }
1190#endif
1191
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001192 // Apply SourceCmd autocommands, they should get the file and source it.
1193 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1194 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1195 FALSE, curbuf))
1196 {
1197#ifdef FEAT_EVAL
1198 retval = aborting() ? FAIL : OK;
1199#else
1200 retval = OK;
1201#endif
1202 if (retval == OK)
1203 // Apply SourcePost autocommands.
1204 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1205 FALSE, curbuf);
1206 goto theend;
1207 }
1208
1209 // Apply SourcePre autocommands, they may get the file.
1210 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1211
1212#ifdef USE_FOPEN_NOINH
1213 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1214#else
1215 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1216#endif
1217 if (cookie.fp == NULL && check_other)
1218 {
1219 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1220 // and ".exrc" by "_exrc" or vice versa.
1221 p = gettail(fname_exp);
1222 if ((*p == '.' || *p == '_')
1223 && (STRICMP(p + 1, "vimrc") == 0
1224 || STRICMP(p + 1, "gvimrc") == 0
1225 || STRICMP(p + 1, "exrc") == 0))
1226 {
1227 if (*p == '_')
1228 *p = '.';
1229 else
1230 *p = '_';
1231#ifdef USE_FOPEN_NOINH
1232 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1233#else
1234 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1235#endif
1236 }
1237 }
1238
1239 if (cookie.fp == NULL)
1240 {
1241 if (p_verbose > 0)
1242 {
1243 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001244 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001245 smsg(_("could not source \"%s\""), fname);
1246 else
1247 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001248 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001249 verbose_leave();
1250 }
1251 goto theend;
1252 }
1253
1254 // The file exists.
1255 // - In verbose mode, give a message.
1256 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1257 if (p_verbose > 1)
1258 {
1259 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001260 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001261 smsg(_("sourcing \"%s\""), fname);
1262 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001263 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001264 verbose_leave();
1265 }
1266 if (is_vimrc == DOSO_VIMRC)
1267 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1268 else if (is_vimrc == DOSO_GVIMRC)
1269 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1270
1271#ifdef USE_CRNL
1272 // If no automatic file format: Set default to CR-NL.
1273 if (*p_ffs == NUL)
1274 cookie.fileformat = EOL_DOS;
1275 else
1276 cookie.fileformat = EOL_UNKNOWN;
1277 cookie.error = FALSE;
1278#endif
1279
1280 cookie.nextline = NULL;
1281 cookie.sourcing_lnum = 0;
1282 cookie.finished = FALSE;
1283
1284#ifdef FEAT_EVAL
1285 // Check if this script has a breakpoint.
1286 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1287 cookie.fname = fname_exp;
1288 cookie.dbg_tick = debug_tick;
1289
1290 cookie.level = ex_nesting_level;
1291#endif
1292
1293 // Keep the sourcing name/lnum, for recursive calls.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001294 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001295 ESTACK_CHECK_SETUP
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001296
1297#ifdef STARTUPTIME
1298 if (time_fd != NULL)
1299 time_push(&tv_rel, &tv_start);
1300#endif
1301
1302#ifdef FEAT_EVAL
1303# ifdef FEAT_PROFILE
1304 if (do_profiling == PROF_YES)
1305 prof_child_enter(&wait_start); // entering a child now
1306# endif
1307
1308 // Don't use local function variables, if called from a function.
1309 // Also starts profiling timer for nested script.
1310 save_funccal(&funccalp_entry);
1311
1312 save_current_sctx = current_sctx;
1313 current_sctx.sc_lnum = 0;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001314 current_sctx.sc_version = 1; // default script version
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001315
1316 // Check if this script was sourced before to finds its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001317 // Always use a new sequence number.
1318 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001320 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 hashtab_T *ht;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001322 int is_vim9 = si->sn_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001323
1324 // loading the same script again
1325 si->sn_had_command = FALSE;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01001326 si->sn_version = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327 current_sctx.sc_sid = sid;
1328
Bram Moolenaar2eec3792020-05-25 20:33:55 +02001329 // In Vim9 script all script-local variables are removed when reloading
1330 // the same script. In legacy script they remain but "const" can be
1331 // set again.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332 ht = &SCRIPT_VARS(sid);
Bram Moolenaar89483d42020-05-10 15:24:44 +02001333 if (is_vim9)
1334 hashtab_free_contents(ht);
1335 else
1336 {
1337 int todo = (int)ht->ht_used;
1338 hashitem_T *hi;
1339 dictitem_T *di;
1340
1341 for (hi = ht->ht_array; todo > 0; ++hi)
1342 if (!HASHITEM_EMPTY(hi))
1343 {
1344 --todo;
1345 di = HI2DI(hi);
1346 di->di_flags |= DI_FLAGS_RELOAD;
1347 }
1348 }
Bram Moolenaar750802b2020-02-23 18:08:33 +01001349
1350 // old imports are no longer valid
1351 free_imports(sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001352
1353 // in Vim9 script functions are marked deleted
1354 if (is_vim9)
1355 delete_script_functions(sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001356 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001358 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 // It's new, generate a new SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001360 current_sctx.sc_sid = ++last_current_SID;
1361 if (ga_grow(&script_items,
1362 (int)(current_sctx.sc_sid - script_items.ga_len)) == FAIL)
1363 goto almosttheend;
1364 while (script_items.ga_len < current_sctx.sc_sid)
1365 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001366 si = ALLOC_CLEAR_ONE(scriptitem_T);
1367 if (si == NULL)
1368 goto almosttheend;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001369 ++script_items.ga_len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001370 SCRIPT_ITEM(script_items.ga_len) = si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371 si->sn_name = NULL;
1372 si->sn_version = 1;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001373
1374 // Allocate the local script variables to use for this script.
1375 new_script_vars(script_items.ga_len);
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +02001376 ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
1378 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001379# ifdef FEAT_PROFILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 si->sn_prof_on = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001381# endif
1382 }
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001383 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001384 si->sn_name = fname_exp;
1385 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001386 if (ret_sid != NULL)
1387 *ret_sid = current_sctx.sc_sid;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001388 }
1389
1390# ifdef FEAT_PROFILE
1391 if (do_profiling == PROF_YES)
1392 {
1393 int forceit;
1394
1395 // Check if we do profiling for this script.
1396 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
1397 {
1398 script_do_profile(si);
1399 si->sn_pr_force = forceit;
1400 }
1401 if (si->sn_prof_on)
1402 {
1403 ++si->sn_pr_count;
1404 profile_start(&si->sn_pr_start);
1405 profile_zero(&si->sn_pr_children);
1406 }
1407 }
1408# endif
1409#endif
1410
1411 cookie.conv.vc_type = CONV_NONE; // no conversion
1412
1413 // Read the first line so we can check for a UTF-8 BOM.
1414 firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1415 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1416 && firstline[1] == 0xbb && firstline[2] == 0xbf)
1417 {
1418 // Found BOM; setup conversion, skip over BOM and recode the line.
1419 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1420 p = string_convert(&cookie.conv, firstline + 3, NULL);
1421 if (p == NULL)
1422 p = vim_strsave(firstline + 3);
1423 if (p != NULL)
1424 {
1425 vim_free(firstline);
1426 firstline = p;
1427 }
1428 }
1429
1430 // Call do_cmdline, which will call getsourceline() to get the lines.
1431 do_cmdline(firstline, getsourceline, (void *)&cookie,
1432 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1433 retval = OK;
1434
1435#ifdef FEAT_PROFILE
1436 if (do_profiling == PROF_YES)
1437 {
1438 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001439 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001440 if (si->sn_prof_on)
1441 {
1442 profile_end(&si->sn_pr_start);
1443 profile_sub_wait(&wait_start, &si->sn_pr_start);
1444 profile_add(&si->sn_pr_total, &si->sn_pr_start);
1445 profile_self(&si->sn_pr_self, &si->sn_pr_start,
1446 &si->sn_pr_children);
1447 }
1448 }
1449#endif
1450
1451 if (got_int)
1452 emsg(_(e_interr));
Bram Moolenaare31ee862020-01-07 20:59:34 +01001453 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001454 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001455 if (p_verbose > 1)
1456 {
1457 verbose_enter();
1458 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001459 if (SOURCING_NAME != NULL)
1460 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001461 verbose_leave();
1462 }
1463#ifdef STARTUPTIME
1464 if (time_fd != NULL)
1465 {
1466 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1467 time_msg((char *)IObuff, &tv_start);
1468 time_pop(&tv_rel);
1469 }
1470#endif
1471
1472 if (!got_int)
1473 trigger_source_post = TRUE;
1474
1475#ifdef FEAT_EVAL
1476 // After a "finish" in debug mode, need to break at first command of next
1477 // sourced file.
1478 if (save_debug_break_level > ex_nesting_level
1479 && debug_break_level == ex_nesting_level)
1480 ++debug_break_level;
1481#endif
1482
1483#ifdef FEAT_EVAL
1484almosttheend:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001486 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 if (si->sn_save_cpo != NULL)
1488 {
1489 free_string_option(p_cpo);
1490 p_cpo = si->sn_save_cpo;
1491 si->sn_save_cpo = NULL;
1492 }
1493
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001494 current_sctx = save_current_sctx;
1495 restore_funccal();
1496# ifdef FEAT_PROFILE
1497 if (do_profiling == PROF_YES)
1498 prof_child_exit(&wait_start); // leaving a child now
1499# endif
1500#endif
1501 fclose(cookie.fp);
1502 vim_free(cookie.nextline);
1503 vim_free(firstline);
1504 convert_setup(&cookie.conv, NULL, NULL);
1505
1506 if (trigger_source_post)
1507 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1508
1509theend:
1510 vim_free(fname_exp);
1511 return retval;
1512}
1513
1514#if defined(FEAT_EVAL) || defined(PROTO)
1515
1516/*
1517 * ":scriptnames"
1518 */
1519 void
1520ex_scriptnames(exarg_T *eap)
1521{
1522 int i;
1523
1524 if (eap->addr_count > 0)
1525 {
1526 // :script {scriptId}: edit the script
Bram Moolenaare3d46852020-08-29 13:39:17 +02001527 if (!SCRIPT_ID_VALID(eap->line2))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001528 emsg(_(e_invarg));
1529 else
1530 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001531 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001532 do_exedit(eap, NULL);
1533 }
1534 return;
1535 }
1536
1537 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001538 if (SCRIPT_ITEM(i)->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001539 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001540 home_replace(NULL, SCRIPT_ITEM(i)->sn_name,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001541 NameBuff, MAXPATHL, TRUE);
1542 smsg("%3d: %s", i, NameBuff);
1543 }
1544}
1545
1546# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1547/*
1548 * Fix slashes in the list of script names for 'shellslash'.
1549 */
1550 void
1551scriptnames_slash_adjust(void)
1552{
1553 int i;
1554
1555 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001556 if (SCRIPT_ITEM(i)->sn_name != NULL)
1557 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001558}
1559# endif
1560
1561/*
1562 * Get a pointer to a script name. Used for ":verbose set".
1563 */
1564 char_u *
1565get_scriptname(scid_T id)
1566{
1567 if (id == SID_MODELINE)
1568 return (char_u *)_("modeline");
1569 if (id == SID_CMDARG)
1570 return (char_u *)_("--cmd argument");
1571 if (id == SID_CARG)
1572 return (char_u *)_("-c argument");
1573 if (id == SID_ENV)
1574 return (char_u *)_("environment variable");
1575 if (id == SID_ERROR)
1576 return (char_u *)_("error handler");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001577 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001578}
1579
1580# if defined(EXITFREE) || defined(PROTO)
1581 void
1582free_scriptnames(void)
1583{
1584 int i;
1585
1586 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02001587 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001588 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001590 // the variables themselves are cleared in evalvars_clear()
1591 vim_free(si->sn_vars);
1592
1593 vim_free(si->sn_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01001594 free_imports(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001595 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02001596# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001597 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02001598# endif
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001599 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02001600 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001601 ga_clear(&script_items);
1602}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001603
1604 void
1605free_autoload_scriptnames(void)
1606{
1607 ga_clear_strings(&ga_loaded);
1608}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001609# endif
1610
1611#endif
1612
1613 linenr_T
Bram Moolenaar66250c92020-08-20 15:02:42 +02001614get_sourced_lnum(
1615 char_u *(*fgetline)(int, void *, int, getline_opt_T),
1616 void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001617{
1618 return fgetline == getsourceline
1619 ? ((struct source_cookie *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001620 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001621}
1622
1623 static char_u *
1624get_one_sourceline(struct source_cookie *sp)
1625{
1626 garray_T ga;
1627 int len;
1628 int c;
1629 char_u *buf;
1630#ifdef USE_CRNL
1631 int has_cr; // CR-LF found
1632#endif
1633 int have_read = FALSE;
1634
1635 // use a growarray to store the sourced line
1636 ga_init2(&ga, 1, 250);
1637
1638 // Loop until there is a finished line (or end-of-file).
1639 ++sp->sourcing_lnum;
1640 for (;;)
1641 {
1642 // make room to read at least 120 (more) characters
1643 if (ga_grow(&ga, 120) == FAIL)
1644 break;
1645 buf = (char_u *)ga.ga_data;
1646
1647 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
1648 sp->fp) == NULL)
1649 break;
1650 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
1651#ifdef USE_CRNL
1652 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
1653 // CTRL-Z by its own, or after a NL.
1654 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
1655 && sp->fileformat == EOL_DOS
1656 && buf[len - 1] == Ctrl_Z)
1657 {
1658 buf[len - 1] = NUL;
1659 break;
1660 }
1661#endif
1662
1663 have_read = TRUE;
1664 ga.ga_len = len;
1665
1666 // If the line was longer than the buffer, read more.
1667 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
1668 continue;
1669
1670 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
1671 {
1672#ifdef USE_CRNL
1673 has_cr = (len >= 2 && buf[len - 2] == '\r');
1674 if (sp->fileformat == EOL_UNKNOWN)
1675 {
1676 if (has_cr)
1677 sp->fileformat = EOL_DOS;
1678 else
1679 sp->fileformat = EOL_UNIX;
1680 }
1681
1682 if (sp->fileformat == EOL_DOS)
1683 {
1684 if (has_cr) // replace trailing CR
1685 {
1686 buf[len - 2] = '\n';
1687 --len;
1688 --ga.ga_len;
1689 }
1690 else // lines like ":map xx yy^M" will have failed
1691 {
1692 if (!sp->error)
1693 {
1694 msg_source(HL_ATTR(HLF_W));
1695 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
1696 }
1697 sp->error = TRUE;
1698 sp->fileformat = EOL_UNIX;
1699 }
1700 }
1701#endif
1702 // The '\n' is escaped if there is an odd number of ^V's just
1703 // before it, first set "c" just before the 'V's and then check
1704 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
1705 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
1706 ;
1707 if ((len & 1) != (c & 1)) // escaped NL, read more
1708 {
1709 ++sp->sourcing_lnum;
1710 continue;
1711 }
1712
1713 buf[len - 1] = NUL; // remove the NL
1714 }
1715
1716 // Check for ^C here now and then, so recursive :so can be broken.
1717 line_breakcheck();
1718 break;
1719 }
1720
1721 if (have_read)
1722 return (char_u *)ga.ga_data;
1723
1724 vim_free(ga.ga_data);
1725 return NULL;
1726}
1727
1728/*
1729 * Get one full line from a sourced file.
1730 * Called by do_cmdline() when it's called from do_source().
1731 *
1732 * Return a pointer to the line in allocated memory.
1733 * Return NULL for end-of-file or some error.
1734 */
1735 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02001736getsourceline(
1737 int c UNUSED,
1738 void *cookie,
1739 int indent UNUSED,
1740 getline_opt_T options)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001741{
1742 struct source_cookie *sp = (struct source_cookie *)cookie;
1743 char_u *line;
1744 char_u *p;
1745
1746#ifdef FEAT_EVAL
1747 // If breakpoints have been added/deleted need to check for it.
1748 if (sp->dbg_tick < debug_tick)
1749 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001750 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001751 sp->dbg_tick = debug_tick;
1752 }
1753# ifdef FEAT_PROFILE
1754 if (do_profiling == PROF_YES)
1755 script_line_end();
1756# endif
1757#endif
1758
1759 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001760 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001761
1762 // Get current line. If there is a read-ahead line, use it, otherwise get
1763 // one now.
1764 if (sp->finished)
1765 line = NULL;
1766 else if (sp->nextline == NULL)
1767 line = get_one_sourceline(sp);
1768 else
1769 {
1770 line = sp->nextline;
1771 sp->nextline = NULL;
1772 ++sp->sourcing_lnum;
1773 }
1774#ifdef FEAT_PROFILE
1775 if (line != NULL && do_profiling == PROF_YES)
1776 script_line_start();
1777#endif
1778
1779 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
1780 // contain the 'C' flag.
Bram Moolenaar66250c92020-08-20 15:02:42 +02001781 if (line != NULL && options != GETLINE_NONE
1782 && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001783 {
1784 // compensate for the one line read-ahead
1785 --sp->sourcing_lnum;
1786
1787 // Get the next line and concatenate it when it starts with a
1788 // backslash. We always need to read the next line, keep it in
1789 // sp->nextline.
1790 /* Also check for a comment in between continuation lines: "\ */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001791 // Also check for a Vim9 comment and empty line.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001792 sp->nextline = get_one_sourceline(sp);
1793 if (sp->nextline != NULL
1794 && (*(p = skipwhite(sp->nextline)) == '\\'
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001795 || (p[0] == '"' && p[1] == '\\' && p[2] == ' ')
Bram Moolenaarf3982382020-07-19 16:32:09 +02001796#ifdef FEAT_EVAL
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001797 || (in_vim9script()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001798 && options == GETLINE_CONCAT_ALL
1799 && (*p == NUL || vim9_comment_start(p)))
Bram Moolenaarf3982382020-07-19 16:32:09 +02001800#endif
1801 ))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001802 {
1803 garray_T ga;
1804
1805 ga_init2(&ga, (int)sizeof(char_u), 400);
1806 ga_concat(&ga, line);
1807 if (*p == '\\')
1808 ga_concat(&ga, p + 1);
1809 for (;;)
1810 {
1811 vim_free(sp->nextline);
1812 sp->nextline = get_one_sourceline(sp);
1813 if (sp->nextline == NULL)
1814 break;
1815 p = skipwhite(sp->nextline);
1816 if (*p == '\\')
1817 {
1818 // Adjust the growsize to the current length to speed up
1819 // concatenating many lines.
1820 if (ga.ga_len > 400)
1821 {
1822 if (ga.ga_len > 8000)
1823 ga.ga_growsize = 8000;
1824 else
1825 ga.ga_growsize = ga.ga_len;
1826 }
1827 ga_concat(&ga, p + 1);
1828 }
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001829 else if (!(p[0] == '"' && p[1] == '\\' && p[2] == ' ')
Bram Moolenaarf3982382020-07-19 16:32:09 +02001830#ifdef FEAT_EVAL
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001831 && !(in_vim9script()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001832 && options == GETLINE_CONCAT_ALL
1833 && (*p == NUL || vim9_comment_start(p)))
Bram Moolenaarf3982382020-07-19 16:32:09 +02001834#endif
1835 )
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001836 break;
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001837 /* drop a # comment or "\ comment line */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001838 }
1839 ga_append(&ga, NUL);
1840 vim_free(line);
1841 line = ga.ga_data;
1842 }
1843 }
1844
1845 if (line != NULL && sp->conv.vc_type != CONV_NONE)
1846 {
1847 char_u *s;
1848
1849 // Convert the encoding of the script line.
1850 s = string_convert(&sp->conv, line, NULL);
1851 if (s != NULL)
1852 {
1853 vim_free(line);
1854 line = s;
1855 }
1856 }
1857
1858#ifdef FEAT_EVAL
1859 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001860 if (sp->breakpoint != 0 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001861 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001862 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001863 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001864 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001865 sp->dbg_tick = debug_tick;
1866 }
1867#endif
1868
1869 return line;
1870}
1871
1872/*
1873 * ":scriptencoding": Set encoding conversion for a sourced script.
1874 */
1875 void
1876ex_scriptencoding(exarg_T *eap)
1877{
1878 struct source_cookie *sp;
1879 char_u *name;
1880
1881 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
1882 {
1883 emsg(_("E167: :scriptencoding used outside of a sourced file"));
1884 return;
1885 }
1886
1887 if (*eap->arg != NUL)
1888 {
1889 name = enc_canonize(eap->arg);
1890 if (name == NULL) // out of memory
1891 return;
1892 }
1893 else
1894 name = eap->arg;
1895
1896 // Setup for conversion from the specified encoding to 'encoding'.
1897 sp = (struct source_cookie *)getline_cookie(eap->getline, eap->cookie);
1898 convert_setup(&sp->conv, name, p_enc);
1899
1900 if (name != eap->arg)
1901 vim_free(name);
1902}
1903
1904/*
1905 * ":scriptversion": Set Vim script version for a sourced script.
1906 */
1907 void
1908ex_scriptversion(exarg_T *eap UNUSED)
1909{
1910#ifdef FEAT_EVAL
1911 int nr;
1912
1913 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
1914 {
1915 emsg(_("E984: :scriptversion used outside of a sourced file"));
1916 return;
1917 }
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001918 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001920 emsg(_(e_cannot_use_scriptversion_after_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 return;
1922 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001923
1924 nr = getdigits(&eap->arg);
1925 if (nr == 0 || *eap->arg != NUL)
1926 emsg(_(e_invarg));
Bram Moolenaar67979662020-06-20 22:50:47 +02001927 else if (nr > SCRIPT_VERSION_MAX)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001928 semsg(_("E999: scriptversion not supported: %d"), nr);
1929 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001930 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001931 current_sctx.sc_version = nr;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001932 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001933 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001934#endif
1935}
1936
1937#if defined(FEAT_EVAL) || defined(PROTO)
1938/*
1939 * ":finish": Mark a sourced file as finished.
1940 */
1941 void
1942ex_finish(exarg_T *eap)
1943{
1944 if (getline_equal(eap->getline, eap->cookie, getsourceline))
1945 do_finish(eap, FALSE);
1946 else
1947 emsg(_("E168: :finish used outside of a sourced file"));
1948}
1949
1950/*
1951 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
1952 * Also called for a pending finish at the ":endtry" or after returning from
1953 * an extra do_cmdline(). "reanimate" is used in the latter case.
1954 */
1955 void
1956do_finish(exarg_T *eap, int reanimate)
1957{
1958 int idx;
1959
1960 if (reanimate)
1961 ((struct source_cookie *)getline_cookie(eap->getline,
1962 eap->cookie))->finished = FALSE;
1963
1964 // Cleanup (and inactivate) conditionals, but stop when a try conditional
1965 // not in its finally clause (which then is to be executed next) is found.
1966 // In this case, make the ":finish" pending for execution at the ":endtry".
1967 // Otherwise, finish normally.
1968 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
1969 if (idx >= 0)
1970 {
1971 eap->cstack->cs_pending[idx] = CSTP_FINISH;
1972 report_make_pending(CSTP_FINISH, NULL);
1973 }
1974 else
1975 ((struct source_cookie *)getline_cookie(eap->getline,
1976 eap->cookie))->finished = TRUE;
1977}
1978
1979
1980/*
1981 * Return TRUE when a sourced file had the ":finish" command: Don't give error
1982 * message for missing ":endif".
1983 * Return FALSE when not sourcing a file.
1984 */
1985 int
1986source_finished(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001987 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001988 void *cookie)
1989{
1990 return (getline_equal(fgetline, cookie, getsourceline)
1991 && ((struct source_cookie *)getline_cookie(
1992 fgetline, cookie))->finished);
1993}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001994
1995/*
1996 * Return the autoload script name for a function or variable name.
1997 * Returns NULL when out of memory.
1998 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
1999 */
2000 char_u *
2001autoload_name(char_u *name)
2002{
2003 char_u *p, *q = NULL;
2004 char_u *scriptname;
2005
2006 // Get the script file name: replace '#' with '/', append ".vim".
2007 scriptname = alloc(STRLEN(name) + 14);
2008 if (scriptname == NULL)
2009 return NULL;
2010 STRCPY(scriptname, "autoload/");
Bram Moolenaara1773442020-08-12 15:21:22 +02002011 STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002012 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
2013 q = p, ++p)
2014 *p = '/';
2015 STRCPY(q, ".vim");
2016 return scriptname;
2017}
2018
2019/*
2020 * If "name" has a package name try autoloading the script for it.
2021 * Return TRUE if a package was loaded.
2022 */
2023 int
2024script_autoload(
2025 char_u *name,
2026 int reload) // load script again when already loaded
2027{
2028 char_u *p;
2029 char_u *scriptname, *tofree;
2030 int ret = FALSE;
2031 int i;
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002032 int ret_sid;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002033
2034 // If there is no '#' after name[0] there is no package name.
2035 p = vim_strchr(name, AUTOLOAD_CHAR);
2036 if (p == NULL || p == name)
2037 return FALSE;
2038
2039 tofree = scriptname = autoload_name(name);
2040 if (scriptname == NULL)
2041 return FALSE;
2042
2043 // Find the name in the list of previously loaded package names. Skip
2044 // "autoload/", it's always the same.
2045 for (i = 0; i < ga_loaded.ga_len; ++i)
2046 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2047 break;
2048 if (!reload && i < ga_loaded.ga_len)
2049 ret = FALSE; // was loaded already
2050 else
2051 {
2052 // Remember the name if it wasn't loaded already.
2053 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2054 {
2055 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2056 tofree = NULL;
2057 }
2058
2059 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002060 // Use "ret_sid" to avoid loading the same script again.
2061 if (source_in_path(p_rtp, scriptname, 0, &ret_sid) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002062 ret = TRUE;
2063 }
2064
2065 vim_free(tofree);
2066 return ret;
2067}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002068#endif