blob: 27d0eb2a52270aa1464e05ebc650ac86ae28a09a [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 Moolenaara5d04232020-07-26 15:37:02 +0200114 * "is_sfile" is TRUE for <sfile> itself.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100115 */
116 char_u *
Bram Moolenaarcb4f69c2020-07-26 15:51:06 +0200117estack_sfile(int is_sfile 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 Moolenaara5d04232020-07-26 15:37:02 +0200130 if (is_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 Moolenaara5d04232020-07-26 15:37:02 +0200147 len = STRLEN(entry->es_name) + 15;
148 type_name = "";
149 if (entry->es_type != last_type)
150 {
151 switch (entry->es_type)
152 {
153 case ETYPE_SCRIPT: type_name = "script "; break;
154 case ETYPE_UFUNC: type_name = "function "; break;
155 default: type_name = ""; break;
156 }
157 last_type = entry->es_type;
158 }
159 len += STRLEN(type_name);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200160 if (ga_grow(&ga, (int)len) == FAIL)
Bram Moolenaara5d04232020-07-26 15:37:02 +0200161 break;
162 if (idx == exestack.ga_len - 1 || entry->es_lnum == 0)
163 // For the bottom entry: do not add the line number, it is used
164 // in <slnum>. Also leave it out when the number is not set.
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200165 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s%s",
Bram Moolenaara5d04232020-07-26 15:37:02 +0200166 type_name, entry->es_name,
167 idx == exestack.ga_len - 1 ? "" : "..");
168 else
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200169 vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s[%ld]..",
Bram Moolenaara5d04232020-07-26 15:37:02 +0200170 type_name, entry->es_name, entry->es_lnum);
Bram Moolenaard3bb6a82020-07-26 15:55:25 +0200171 ga.ga_len += (int)STRLEN((char *)ga.ga_data + ga.ga_len);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100172 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100173 }
174
Bram Moolenaara5d04232020-07-26 15:37:02 +0200175 return (char_u *)ga.ga_data;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100176#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100177}
178
179/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200180 * ":runtime [what] {name}"
181 */
182 void
183ex_runtime(exarg_T *eap)
184{
185 char_u *arg = eap->arg;
186 char_u *p = skiptowhite(arg);
187 int len = (int)(p - arg);
188 int flags = eap->forceit ? DIP_ALL : 0;
189
190 if (STRNCMP(arg, "START", len) == 0)
191 {
192 flags += DIP_START + DIP_NORTP;
193 arg = skipwhite(arg + len);
194 }
195 else if (STRNCMP(arg, "OPT", len) == 0)
196 {
197 flags += DIP_OPT + DIP_NORTP;
198 arg = skipwhite(arg + len);
199 }
200 else if (STRNCMP(arg, "PACK", len) == 0)
201 {
202 flags += DIP_START + DIP_OPT + DIP_NORTP;
203 arg = skipwhite(arg + len);
204 }
205 else if (STRNCMP(arg, "ALL", len) == 0)
206 {
207 flags += DIP_START + DIP_OPT;
208 arg = skipwhite(arg + len);
209 }
210
211 source_runtime(arg, flags);
212}
213
214 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100215source_callback(char_u *fname, void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200216{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200218}
219
220/*
221 * Find the file "name" in all directories in "path" and invoke
222 * "callback(fname, cookie)".
223 * "name" can contain wildcards.
224 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
225 * When "flags" has DIP_DIR: find directories instead of files.
226 * When "flags" has DIP_ERR: give an error message if there is no match.
227 *
228 * return FAIL when no file could be sourced, OK otherwise.
229 */
230 int
231do_in_path(
232 char_u *path,
233 char_u *name,
234 int flags,
235 void (*callback)(char_u *fname, void *ck),
236 void *cookie)
237{
238 char_u *rtp;
239 char_u *np;
240 char_u *buf;
241 char_u *rtp_copy;
242 char_u *tail;
243 int num_files;
244 char_u **files;
245 int i;
246 int did_one = FALSE;
247#ifdef AMIGA
248 struct Process *proc = (struct Process *)FindTask(0L);
249 APTR save_winptr = proc->pr_WindowPtr;
250
251 // Avoid a requester here for a volume that doesn't exist.
252 proc->pr_WindowPtr = (APTR)-1L;
253#endif
254
255 // Make a copy of 'runtimepath'. Invoking the callback may change the
256 // value.
257 rtp_copy = vim_strsave(path);
258 buf = alloc(MAXPATHL);
259 if (buf != NULL && rtp_copy != NULL)
260 {
Bram Moolenaar647a5302020-05-03 17:01:24 +0200261 if (p_verbose > 10 && name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200262 {
263 verbose_enter();
264 smsg(_("Searching for \"%s\" in \"%s\""),
265 (char *)name, (char *)path);
266 verbose_leave();
267 }
268
269 // Loop over all entries in 'runtimepath'.
270 rtp = rtp_copy;
271 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
272 {
273 size_t buflen;
274
275 // Copy the path from 'runtimepath' to buf[].
276 copy_option_part(&rtp, buf, MAXPATHL, ",");
277 buflen = STRLEN(buf);
278
279 // Skip after or non-after directories.
280 if (flags & (DIP_NOAFTER | DIP_AFTER))
281 {
282 int is_after = buflen >= 5
283 && STRCMP(buf + buflen - 5, "after") == 0;
284
285 if ((is_after && (flags & DIP_NOAFTER))
286 || (!is_after && (flags & DIP_AFTER)))
287 continue;
288 }
289
290 if (name == NULL)
291 {
292 (*callback)(buf, (void *) &cookie);
293 if (!did_one)
294 did_one = (cookie == NULL);
295 }
296 else if (buflen + STRLEN(name) + 2 < MAXPATHL)
297 {
298 add_pathsep(buf);
299 tail = buf + STRLEN(buf);
300
301 // Loop over all patterns in "name"
302 np = name;
303 while (*np != NUL && ((flags & DIP_ALL) || !did_one))
304 {
305 // Append the pattern from "name" to buf[].
306 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
307 "\t ");
308
Bram Moolenaar647a5302020-05-03 17:01:24 +0200309 if (p_verbose > 10)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200310 {
311 verbose_enter();
312 smsg(_("Searching for \"%s\""), buf);
313 verbose_leave();
314 }
315
316 // Expand wildcards, invoke the callback for each match.
317 if (gen_expand_wildcards(1, &buf, &num_files, &files,
318 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
319 {
320 for (i = 0; i < num_files; ++i)
321 {
322 (*callback)(files[i], cookie);
323 did_one = TRUE;
324 if (!(flags & DIP_ALL))
325 break;
326 }
327 FreeWild(num_files, files);
328 }
329 }
330 }
331 }
332 }
333 vim_free(buf);
334 vim_free(rtp_copy);
335 if (!did_one && name != NULL)
336 {
337 char *basepath = path == p_rtp ? "runtimepath" : "packpath";
338
339 if (flags & DIP_ERR)
340 semsg(_(e_dirnotf), basepath, name);
341 else if (p_verbose > 0)
342 {
343 verbose_enter();
344 smsg(_("not found in '%s': \"%s\""), basepath, name);
345 verbose_leave();
346 }
347 }
348
349#ifdef AMIGA
350 proc->pr_WindowPtr = save_winptr;
351#endif
352
353 return did_one ? OK : FAIL;
354}
355
356/*
357 * Find "name" in "path". When found, invoke the callback function for
358 * it: callback(fname, "cookie")
359 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first
360 * one is used.
361 * Returns OK when at least one match found, FAIL otherwise.
362 *
363 * If "name" is NULL calls callback for each entry in "path". Cookie is
364 * passed by reference in this case, setting it to NULL indicates that callback
365 * has done its job.
366 */
367 static int
368do_in_path_and_pp(
369 char_u *path,
370 char_u *name,
371 int flags,
372 void (*callback)(char_u *fname, void *ck),
373 void *cookie)
374{
375 int done = FAIL;
376 char_u *s;
377 int len;
378 char *start_dir = "pack/*/start/*/%s";
379 char *opt_dir = "pack/*/opt/*/%s";
380
381 if ((flags & DIP_NORTP) == 0)
382 done = do_in_path(path, name, flags, callback, cookie);
383
384 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
385 {
386 len = (int)(STRLEN(start_dir) + STRLEN(name));
387 s = alloc(len);
388 if (s == NULL)
389 return FAIL;
390 vim_snprintf((char *)s, len, start_dir, name);
391 done = do_in_path(p_pp, s, flags, callback, cookie);
392 vim_free(s);
393 }
394
395 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
396 {
397 len = (int)(STRLEN(opt_dir) + STRLEN(name));
398 s = alloc(len);
399 if (s == NULL)
400 return FAIL;
401 vim_snprintf((char *)s, len, opt_dir, name);
402 done = do_in_path(p_pp, s, flags, callback, cookie);
403 vim_free(s);
404 }
405
406 return done;
407}
408
409/*
410 * Just like do_in_path_and_pp(), using 'runtimepath' for "path".
411 */
412 int
413do_in_runtimepath(
414 char_u *name,
415 int flags,
416 void (*callback)(char_u *fname, void *ck),
417 void *cookie)
418{
419 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
420}
421
422/*
423 * Source the file "name" from all directories in 'runtimepath'.
424 * "name" can contain wildcards.
425 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
426 *
427 * return FAIL when no file could be sourced, OK otherwise.
428 */
429 int
430source_runtime(char_u *name, int flags)
431{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100432 return source_in_path(p_rtp, name, flags, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200433}
434
435/*
436 * Just like source_runtime(), but use "path" instead of 'runtimepath'.
437 */
438 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100439source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200440{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100441 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200442}
443
444
445#if defined(FEAT_EVAL) || defined(PROTO)
446
447/*
448 * Expand wildcards in "pat" and invoke do_source() for each match.
449 */
450 static void
451source_all_matches(char_u *pat)
452{
453 int num_files;
454 char_u **files;
455 int i;
456
457 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK)
458 {
459 for (i = 0; i < num_files; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100460 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200461 FreeWild(num_files, files);
462 }
463}
464
465/*
466 * Add the package directory to 'runtimepath'.
467 */
468 static int
469add_pack_dir_to_rtp(char_u *fname)
470{
471 char_u *p4, *p3, *p2, *p1, *p;
472 char_u *entry;
473 char_u *insp = NULL;
474 int c;
475 char_u *new_rtp;
476 int keep;
477 size_t oldlen;
478 size_t addlen;
479 size_t new_rtp_len;
480 char_u *afterdir = NULL;
481 size_t afterlen = 0;
482 char_u *after_insp = NULL;
483 char_u *ffname = NULL;
484 size_t fname_len;
485 char_u *buf = NULL;
486 char_u *rtp_ffname;
487 int match;
488 int retval = FAIL;
489
490 p4 = p3 = p2 = p1 = get_past_head(fname);
491 for (p = p1; *p; MB_PTR_ADV(p))
492 if (vim_ispathsep_nocolon(*p))
493 {
494 p4 = p3; p3 = p2; p2 = p1; p1 = p;
495 }
496
497 // now we have:
498 // rtp/pack/name/start/name
499 // p4 p3 p2 p1
500 //
501 // find the part up to "pack" in 'runtimepath'
502 c = *++p4; // append pathsep in order to expand symlink
503 *p4 = NUL;
504 ffname = fix_fname(fname);
505 *p4 = c;
506 if (ffname == NULL)
507 return FAIL;
508
509 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
510 // Also stop at the first "after" directory.
511 fname_len = STRLEN(ffname);
512 buf = alloc(MAXPATHL);
513 if (buf == NULL)
514 goto theend;
515 for (entry = p_rtp; *entry != NUL; )
516 {
517 char_u *cur_entry = entry;
518
519 copy_option_part(&entry, buf, MAXPATHL, ",");
520 if (insp == NULL)
521 {
522 add_pathsep(buf);
523 rtp_ffname = fix_fname(buf);
524 if (rtp_ffname == NULL)
525 goto theend;
526 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
527 vim_free(rtp_ffname);
528 if (match)
529 // Insert "ffname" after this entry (and comma).
530 insp = entry;
531 }
532
533 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
534 && p > buf
535 && vim_ispathsep(p[-1])
536 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
537 {
538 if (insp == NULL)
539 // Did not find "ffname" before the first "after" directory,
540 // insert it before this entry.
541 insp = cur_entry;
542 after_insp = cur_entry;
543 break;
544 }
545 }
546
547 if (insp == NULL)
548 // Both "fname" and "after" not found, append at the end.
549 insp = p_rtp + STRLEN(p_rtp);
550
551 // check if rtp/pack/name/start/name/after exists
552 afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
553 if (afterdir != NULL && mch_isdir(afterdir))
554 afterlen = STRLEN(afterdir) + 1; // add one for comma
555
556 oldlen = STRLEN(p_rtp);
557 addlen = STRLEN(fname) + 1; // add one for comma
558 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
559 if (new_rtp == NULL)
560 goto theend;
561
562 // We now have 'rtp' parts: {keep}{keep_after}{rest}.
563 // Create new_rtp, first: {keep},{fname}
564 keep = (int)(insp - p_rtp);
565 mch_memmove(new_rtp, p_rtp, keep);
566 new_rtp_len = keep;
567 if (*insp == NUL)
568 new_rtp[new_rtp_len++] = ','; // add comma before
569 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
570 new_rtp_len += addlen - 1;
571 if (*insp != NUL)
572 new_rtp[new_rtp_len++] = ','; // add comma after
573
574 if (afterlen > 0 && after_insp != NULL)
575 {
576 int keep_after = (int)(after_insp - p_rtp);
577
578 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
579 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
580 keep_after - keep);
581 new_rtp_len += keep_after - keep;
582 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
583 new_rtp_len += afterlen - 1;
584 new_rtp[new_rtp_len++] = ',';
585 keep = keep_after;
586 }
587
588 if (p_rtp[keep] != NUL)
589 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
590 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
591 else
592 new_rtp[new_rtp_len] = NUL;
593
594 if (afterlen > 0 && after_insp == NULL)
595 {
596 // Append afterdir when "after" was not found:
597 // {keep},{fname}{rest},{afterdir}
598 STRCAT(new_rtp, ",");
599 STRCAT(new_rtp, afterdir);
600 }
601
602 set_option_value((char_u *)"rtp", 0L, new_rtp, 0);
603 vim_free(new_rtp);
604 retval = OK;
605
606theend:
607 vim_free(buf);
608 vim_free(ffname);
609 vim_free(afterdir);
610 return retval;
611}
612
613/*
614 * Load scripts in "plugin" and "ftdetect" directories of the package.
615 */
616 static int
617load_pack_plugin(char_u *fname)
618{
619 static char *plugpat = "%s/plugin/**/*.vim";
620 static char *ftpat = "%s/ftdetect/*.vim";
621 int len;
622 char_u *ffname = fix_fname(fname);
623 char_u *pat = NULL;
624 int retval = FAIL;
625
626 if (ffname == NULL)
627 return FAIL;
628 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
629 pat = alloc(len);
630 if (pat == NULL)
631 goto theend;
632 vim_snprintf((char *)pat, len, plugpat, ffname);
633 source_all_matches(pat);
634
635 {
636 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
637
638 // If runtime/filetype.vim wasn't loaded yet, the scripts will be
639 // found when it loads.
640 if (cmd != NULL && eval_to_number(cmd) > 0)
641 {
642 do_cmdline_cmd((char_u *)"augroup filetypedetect");
643 vim_snprintf((char *)pat, len, ftpat, ffname);
644 source_all_matches(pat);
645 do_cmdline_cmd((char_u *)"augroup END");
646 }
647 vim_free(cmd);
648 }
649 vim_free(pat);
650 retval = OK;
651
652theend:
653 vim_free(ffname);
654 return retval;
655}
656
657// used for "cookie" of add_pack_plugin()
658static int APP_ADD_DIR;
659static int APP_LOAD;
660static int APP_BOTH;
661
662 static void
663add_pack_plugin(char_u *fname, void *cookie)
664{
665 if (cookie != &APP_LOAD)
666 {
667 char_u *buf = alloc(MAXPATHL);
668 char_u *p;
669 int found = FALSE;
670
671 if (buf == NULL)
672 return;
673 p = p_rtp;
674 while (*p != NUL)
675 {
676 copy_option_part(&p, buf, MAXPATHL, ",");
677 if (pathcmp((char *)buf, (char *)fname, -1) == 0)
678 {
679 found = TRUE;
680 break;
681 }
682 }
683 vim_free(buf);
684 if (!found)
685 // directory is not yet in 'runtimepath', add it
686 if (add_pack_dir_to_rtp(fname) == FAIL)
687 return;
688 }
689
690 if (cookie != &APP_ADD_DIR)
691 load_pack_plugin(fname);
692}
693
694/*
695 * Add all packages in the "start" directory to 'runtimepath'.
696 */
697 void
698add_pack_start_dirs(void)
699{
700 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
701 add_pack_plugin, &APP_ADD_DIR);
702}
703
704/*
705 * Load plugins from all packages in the "start" directory.
706 */
707 void
708load_start_packages(void)
709{
710 did_source_packages = TRUE;
711 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
712 add_pack_plugin, &APP_LOAD);
713}
714
715/*
716 * ":packloadall"
717 * Find plugins in the package directories and source them.
718 */
719 void
720ex_packloadall(exarg_T *eap)
721{
722 if (!did_source_packages || eap->forceit)
723 {
724 // First do a round to add all directories to 'runtimepath', then load
725 // the plugins. This allows for plugins to use an autoload directory
726 // of another plugin.
727 add_pack_start_dirs();
728 load_start_packages();
729 }
730}
731
732/*
733 * ":packadd[!] {name}"
734 */
735 void
736ex_packadd(exarg_T *eap)
737{
738 static char *plugpat = "pack/*/%s/%s";
739 int len;
740 char *pat;
741 int round;
742 int res = OK;
743
744 // Round 1: use "start", round 2: use "opt".
745 for (round = 1; round <= 2; ++round)
746 {
747 // Only look under "start" when loading packages wasn't done yet.
748 if (round == 1 && did_source_packages)
749 continue;
750
751 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
752 pat = alloc(len);
753 if (pat == NULL)
754 return;
755 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
756 // The first round don't give a "not found" error, in the second round
757 // only when nothing was found in the first round.
758 res = do_in_path(p_pp, (char_u *)pat,
759 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
760 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
761 vim_free(pat);
762 }
763}
764#endif
765
766/*
Bram Moolenaar26262f82019-09-04 20:59:15 +0200767 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
768 * list of file names in allocated memory.
769 */
770 void
771remove_duplicates(garray_T *gap)
772{
773 int i;
774 int j;
775 char_u **fnames = (char_u **)gap->ga_data;
776
777 sort_strings(fnames, gap->ga_len);
778 for (i = gap->ga_len - 1; i > 0; --i)
779 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
780 {
781 vim_free(fnames[i]);
782 for (j = i + 1; j < gap->ga_len; ++j)
783 fnames[j - 1] = fnames[j];
784 --gap->ga_len;
785 }
786}
787
788/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200789 * Expand color scheme, compiler or filetype names.
790 * Search from 'runtimepath':
791 * 'runtimepath'/{dirnames}/{pat}.vim
792 * When "flags" has DIP_START: search also from 'start' of 'packpath':
793 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
794 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
795 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
796 * "dirnames" is an array with one or more directory names.
797 */
798 int
799ExpandRTDir(
800 char_u *pat,
801 int flags,
802 int *num_file,
803 char_u ***file,
804 char *dirnames[])
805{
806 char_u *s;
807 char_u *e;
808 char_u *match;
809 garray_T ga;
810 int i;
811 int pat_len;
812
813 *num_file = 0;
814 *file = NULL;
815 pat_len = (int)STRLEN(pat);
816 ga_init2(&ga, (int)sizeof(char *), 10);
817
818 for (i = 0; dirnames[i] != NULL; ++i)
819 {
820 s = alloc(STRLEN(dirnames[i]) + pat_len + 7);
821 if (s == NULL)
822 {
823 ga_clear_strings(&ga);
824 return FAIL;
825 }
826 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
827 globpath(p_rtp, s, &ga, 0);
828 vim_free(s);
829 }
830
831 if (flags & DIP_START) {
832 for (i = 0; dirnames[i] != NULL; ++i)
833 {
834 s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
835 if (s == NULL)
836 {
837 ga_clear_strings(&ga);
838 return FAIL;
839 }
840 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
841 globpath(p_pp, s, &ga, 0);
842 vim_free(s);
843 }
844 }
845
846 if (flags & DIP_OPT) {
847 for (i = 0; dirnames[i] != NULL; ++i)
848 {
849 s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
850 if (s == NULL)
851 {
852 ga_clear_strings(&ga);
853 return FAIL;
854 }
855 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
856 globpath(p_pp, s, &ga, 0);
857 vim_free(s);
858 }
859 }
860
861 for (i = 0; i < ga.ga_len; ++i)
862 {
863 match = ((char_u **)ga.ga_data)[i];
864 s = match;
865 e = s + STRLEN(s);
866 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
867 {
868 e -= 4;
869 for (s = e; s > match; MB_PTR_BACK(match, s))
870 if (s < match || vim_ispathsep(*s))
871 break;
872 ++s;
873 *e = NUL;
874 mch_memmove(match, s, e - s + 1);
875 }
876 }
877
878 if (ga.ga_len == 0)
879 return FAIL;
880
881 // Sort and remove duplicates which can happen when specifying multiple
882 // directories in dirnames.
883 remove_duplicates(&ga);
884
885 *file = ga.ga_data;
886 *num_file = ga.ga_len;
887 return OK;
888}
889
890/*
891 * Expand loadplugin names:
892 * 'packpath'/pack/ * /opt/{pat}
893 */
894 int
895ExpandPackAddDir(
896 char_u *pat,
897 int *num_file,
898 char_u ***file)
899{
900 char_u *s;
901 char_u *e;
902 char_u *match;
903 garray_T ga;
904 int i;
905 int pat_len;
906
907 *num_file = 0;
908 *file = NULL;
909 pat_len = (int)STRLEN(pat);
910 ga_init2(&ga, (int)sizeof(char *), 10);
911
912 s = alloc(pat_len + 26);
913 if (s == NULL)
914 {
915 ga_clear_strings(&ga);
916 return FAIL;
917 }
918 sprintf((char *)s, "pack/*/opt/%s*", pat);
919 globpath(p_pp, s, &ga, 0);
920 vim_free(s);
921
922 for (i = 0; i < ga.ga_len; ++i)
923 {
924 match = ((char_u **)ga.ga_data)[i];
925 s = gettail(match);
926 e = s + STRLEN(s);
927 mch_memmove(match, s, e - s + 1);
928 }
929
930 if (ga.ga_len == 0)
931 return FAIL;
932
933 // Sort and remove duplicates which can happen when specifying multiple
934 // directories in dirnames.
935 remove_duplicates(&ga);
936
937 *file = ga.ga_data;
938 *num_file = ga.ga_len;
939 return OK;
940}
941
942 static void
943cmd_source(char_u *fname, exarg_T *eap)
944{
945 if (*fname == NUL)
946 emsg(_(e_argreq));
947
948 else if (eap != NULL && eap->forceit)
949 // ":source!": read Normal mode commands
950 // Need to execute the commands directly. This is required at least
951 // for:
952 // - ":g" command busy
953 // - after ":argdo", ":windo" or ":bufdo"
954 // - another command follows
955 // - inside a loop
956 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
957#ifdef FEAT_EVAL
958 || eap->cstack->cs_idx >= 0
959#endif
960 );
961
962 // ":source" read ex commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200964 semsg(_(e_notopen), fname);
965}
966
967/*
968 * ":source {fname}"
969 */
970 void
971ex_source(exarg_T *eap)
972{
973#ifdef FEAT_BROWSE
974 if (cmdmod.browse)
975 {
976 char_u *fname = NULL;
977
978 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
979 NULL, NULL,
980 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
981 if (fname != NULL)
982 {
983 cmd_source(fname, eap);
984 vim_free(fname);
985 }
986 }
987 else
988#endif
989 cmd_source(eap->arg, eap);
990}
991
992#if defined(FEAT_EVAL) || defined(PROTO)
993/*
994 * ":options"
995 */
996 void
997ex_options(
998 exarg_T *eap UNUSED)
999{
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001000 char_u buf[500];
1001 int multi_mods = 0;
1002
1003 buf[0] = NUL;
1004 (void)add_win_cmd_modifers(buf, &multi_mods);
1005
1006 vim_setenv((char_u *)"OPTWIN_CMD", buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001007 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
1008}
1009#endif
1010
1011/*
1012 * ":source" and associated commands.
1013 */
1014/*
1015 * Structure used to store info for each sourced file.
1016 * It is shared between do_source() and getsourceline().
1017 * This is required, because it needs to be handed to do_cmdline() and
1018 * sourcing can be done recursively.
1019 */
1020struct source_cookie
1021{
1022 FILE *fp; // opened file for sourcing
1023 char_u *nextline; // if not NULL: line that was read ahead
1024 linenr_T sourcing_lnum; // line number of the source file
1025 int finished; // ":finish" used
1026#ifdef USE_CRNL
1027 int fileformat; // EOL_UNKNOWN, EOL_UNIX or EOL_DOS
1028 int error; // TRUE if LF found after CR-LF
1029#endif
1030#ifdef FEAT_EVAL
1031 linenr_T breakpoint; // next line with breakpoint or zero
1032 char_u *fname; // name of sourced file
1033 int dbg_tick; // debug_tick when breakpoint was set
1034 int level; // top nesting level of sourced file
1035#endif
1036 vimconv_T conv; // type of conversion
1037};
1038
1039#ifdef FEAT_EVAL
1040/*
1041 * Return the address holding the next breakpoint line for a source cookie.
1042 */
1043 linenr_T *
1044source_breakpoint(void *cookie)
1045{
1046 return &((struct source_cookie *)cookie)->breakpoint;
1047}
1048
1049/*
1050 * Return the address holding the debug tick for a source cookie.
1051 */
1052 int *
1053source_dbg_tick(void *cookie)
1054{
1055 return &((struct source_cookie *)cookie)->dbg_tick;
1056}
1057
1058/*
1059 * Return the nesting level for a source cookie.
1060 */
1061 int
1062source_level(void *cookie)
1063{
1064 return ((struct source_cookie *)cookie)->level;
1065}
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001066
1067/*
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02001068 * Return the readahead line. Note that the pointer may become invalid when
1069 * getting the next line, if it's concatenated with the next one.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001070 */
1071 char_u *
1072source_nextline(void *cookie)
1073{
1074 return ((struct source_cookie *)cookie)->nextline;
1075}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001076#endif
1077
1078#if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC)
1079# define USE_FOPEN_NOINH
1080/*
1081 * Special function to open a file without handle inheritance.
1082 * When possible the handle is closed on exec().
1083 */
1084 static FILE *
1085fopen_noinh_readbin(char *filename)
1086{
1087# ifdef MSWIN
1088 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
1089# else
1090 int fd_tmp = mch_open(filename, O_RDONLY, 0);
1091# endif
1092
1093 if (fd_tmp == -1)
1094 return NULL;
1095
1096# ifdef HAVE_FD_CLOEXEC
1097 {
1098 int fdflags = fcntl(fd_tmp, F_GETFD);
1099 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1100 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
1101 }
1102# endif
1103
1104 return fdopen(fd_tmp, READBIN);
1105}
1106#endif
1107
1108/*
1109 * do_source: Read the file "fname" and execute its lines as EX commands.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1111 * again.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001112 *
1113 * This function may be called recursively!
1114 *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001115 * Return FAIL if file could not be opened, OK otherwise.
1116 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001117 */
1118 int
1119do_source(
1120 char_u *fname,
1121 int check_other, // check for .vimrc and _vimrc
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122 int is_vimrc, // DOSO_ value
1123 int *ret_sid UNUSED)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001124{
1125 struct source_cookie cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001126 char_u *p;
1127 char_u *fname_exp;
1128 char_u *firstline = NULL;
1129 int retval = FAIL;
1130#ifdef FEAT_EVAL
1131 sctx_T save_current_sctx;
1132 static scid_T last_current_SID = 0;
1133 static int last_current_SID_seq = 0;
1134 funccal_entry_T funccalp_entry;
1135 int save_debug_break_level = debug_break_level;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001136 int sid;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001137 scriptitem_T *si = NULL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001138#endif
1139#ifdef STARTUPTIME
1140 struct timeval tv_rel;
1141 struct timeval tv_start;
1142#endif
1143#ifdef FEAT_PROFILE
1144 proftime_T wait_start;
1145#endif
1146 int trigger_source_post = FALSE;
Bram Moolenaare31ee862020-01-07 20:59:34 +01001147 ESTACK_CHECK_DECLARATION
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001148
1149 p = expand_env_save(fname);
1150 if (p == NULL)
1151 return retval;
1152 fname_exp = fix_fname(p);
1153 vim_free(p);
1154 if (fname_exp == NULL)
1155 return retval;
1156 if (mch_isdir(fname_exp))
1157 {
1158 smsg(_("Cannot source a directory: \"%s\""), fname);
1159 goto theend;
1160 }
1161
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162#ifdef FEAT_EVAL
1163 // See if we loaded this script before.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001164 for (sid = script_items.ga_len; sid > 0; --sid)
1165 {
Bram Moolenaar978d1702020-01-26 17:38:12 +01001166 // We used to check inode here, but that doesn't work:
1167 // - If a script is edited and written, it may get a different
1168 // inode number, even though to the user it is the same script.
1169 // - If a script is deleted and another script is written, with a
1170 // different name, the inode may be re-used.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001171 si = SCRIPT_ITEM(sid);
Bram Moolenaar978d1702020-01-26 17:38:12 +01001172 if (si->sn_name != NULL && fnamecmp(si->sn_name, fname_exp) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001173 // Found it!
1174 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 }
1176 if (sid > 0 && ret_sid != NULL)
1177 {
1178 // Already loaded and no need to load again, return here.
1179 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001180 retval = OK;
1181 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 }
1183#endif
1184
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001185 // Apply SourceCmd autocommands, they should get the file and source it.
1186 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1187 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1188 FALSE, curbuf))
1189 {
1190#ifdef FEAT_EVAL
1191 retval = aborting() ? FAIL : OK;
1192#else
1193 retval = OK;
1194#endif
1195 if (retval == OK)
1196 // Apply SourcePost autocommands.
1197 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1198 FALSE, curbuf);
1199 goto theend;
1200 }
1201
1202 // Apply SourcePre autocommands, they may get the file.
1203 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1204
1205#ifdef USE_FOPEN_NOINH
1206 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1207#else
1208 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1209#endif
1210 if (cookie.fp == NULL && check_other)
1211 {
1212 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1213 // and ".exrc" by "_exrc" or vice versa.
1214 p = gettail(fname_exp);
1215 if ((*p == '.' || *p == '_')
1216 && (STRICMP(p + 1, "vimrc") == 0
1217 || STRICMP(p + 1, "gvimrc") == 0
1218 || STRICMP(p + 1, "exrc") == 0))
1219 {
1220 if (*p == '_')
1221 *p = '.';
1222 else
1223 *p = '_';
1224#ifdef USE_FOPEN_NOINH
1225 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1226#else
1227 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1228#endif
1229 }
1230 }
1231
1232 if (cookie.fp == NULL)
1233 {
1234 if (p_verbose > 0)
1235 {
1236 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001237 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001238 smsg(_("could not source \"%s\""), fname);
1239 else
1240 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001241 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001242 verbose_leave();
1243 }
1244 goto theend;
1245 }
1246
1247 // The file exists.
1248 // - In verbose mode, give a message.
1249 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1250 if (p_verbose > 1)
1251 {
1252 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001253 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001254 smsg(_("sourcing \"%s\""), fname);
1255 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001256 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001257 verbose_leave();
1258 }
1259 if (is_vimrc == DOSO_VIMRC)
1260 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1261 else if (is_vimrc == DOSO_GVIMRC)
1262 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1263
1264#ifdef USE_CRNL
1265 // If no automatic file format: Set default to CR-NL.
1266 if (*p_ffs == NUL)
1267 cookie.fileformat = EOL_DOS;
1268 else
1269 cookie.fileformat = EOL_UNKNOWN;
1270 cookie.error = FALSE;
1271#endif
1272
1273 cookie.nextline = NULL;
1274 cookie.sourcing_lnum = 0;
1275 cookie.finished = FALSE;
1276
1277#ifdef FEAT_EVAL
1278 // Check if this script has a breakpoint.
1279 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1280 cookie.fname = fname_exp;
1281 cookie.dbg_tick = debug_tick;
1282
1283 cookie.level = ex_nesting_level;
1284#endif
1285
1286 // Keep the sourcing name/lnum, for recursive calls.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001287 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001288 ESTACK_CHECK_SETUP
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001289
1290#ifdef STARTUPTIME
1291 if (time_fd != NULL)
1292 time_push(&tv_rel, &tv_start);
1293#endif
1294
1295#ifdef FEAT_EVAL
1296# ifdef FEAT_PROFILE
1297 if (do_profiling == PROF_YES)
1298 prof_child_enter(&wait_start); // entering a child now
1299# endif
1300
1301 // Don't use local function variables, if called from a function.
1302 // Also starts profiling timer for nested script.
1303 save_funccal(&funccalp_entry);
1304
1305 save_current_sctx = current_sctx;
1306 current_sctx.sc_lnum = 0;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001307 current_sctx.sc_version = 1; // default script version
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001308
1309 // Check if this script was sourced before to finds its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001310 // Always use a new sequence number.
1311 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001313 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001314 hashtab_T *ht;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001315 int is_vim9 = si->sn_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316
1317 // loading the same script again
1318 si->sn_had_command = FALSE;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01001319 si->sn_version = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320 current_sctx.sc_sid = sid;
1321
Bram Moolenaar2eec3792020-05-25 20:33:55 +02001322 // In Vim9 script all script-local variables are removed when reloading
1323 // the same script. In legacy script they remain but "const" can be
1324 // set again.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325 ht = &SCRIPT_VARS(sid);
Bram Moolenaar89483d42020-05-10 15:24:44 +02001326 if (is_vim9)
1327 hashtab_free_contents(ht);
1328 else
1329 {
1330 int todo = (int)ht->ht_used;
1331 hashitem_T *hi;
1332 dictitem_T *di;
1333
1334 for (hi = ht->ht_array; todo > 0; ++hi)
1335 if (!HASHITEM_EMPTY(hi))
1336 {
1337 --todo;
1338 di = HI2DI(hi);
1339 di->di_flags |= DI_FLAGS_RELOAD;
1340 }
1341 }
Bram Moolenaar750802b2020-02-23 18:08:33 +01001342
1343 // old imports are no longer valid
1344 free_imports(sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001345
1346 // in Vim9 script functions are marked deleted
1347 if (is_vim9)
1348 delete_script_functions(sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001349 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001351 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 // It's new, generate a new SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001353 current_sctx.sc_sid = ++last_current_SID;
1354 if (ga_grow(&script_items,
1355 (int)(current_sctx.sc_sid - script_items.ga_len)) == FAIL)
1356 goto almosttheend;
1357 while (script_items.ga_len < current_sctx.sc_sid)
1358 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001359 si = ALLOC_CLEAR_ONE(scriptitem_T);
1360 if (si == NULL)
1361 goto almosttheend;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001362 ++script_items.ga_len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001363 SCRIPT_ITEM(script_items.ga_len) = si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364 si->sn_name = NULL;
1365 si->sn_version = 1;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001366
1367 // Allocate the local script variables to use for this script.
1368 new_script_vars(script_items.ga_len);
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +02001369 ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
1371 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001372# ifdef FEAT_PROFILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001373 si->sn_prof_on = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001374# endif
1375 }
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001376 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001377 si->sn_name = fname_exp;
1378 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 if (ret_sid != NULL)
1380 *ret_sid = current_sctx.sc_sid;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001381 }
1382
1383# ifdef FEAT_PROFILE
1384 if (do_profiling == PROF_YES)
1385 {
1386 int forceit;
1387
1388 // Check if we do profiling for this script.
1389 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
1390 {
1391 script_do_profile(si);
1392 si->sn_pr_force = forceit;
1393 }
1394 if (si->sn_prof_on)
1395 {
1396 ++si->sn_pr_count;
1397 profile_start(&si->sn_pr_start);
1398 profile_zero(&si->sn_pr_children);
1399 }
1400 }
1401# endif
1402#endif
1403
1404 cookie.conv.vc_type = CONV_NONE; // no conversion
1405
1406 // Read the first line so we can check for a UTF-8 BOM.
1407 firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1408 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1409 && firstline[1] == 0xbb && firstline[2] == 0xbf)
1410 {
1411 // Found BOM; setup conversion, skip over BOM and recode the line.
1412 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1413 p = string_convert(&cookie.conv, firstline + 3, NULL);
1414 if (p == NULL)
1415 p = vim_strsave(firstline + 3);
1416 if (p != NULL)
1417 {
1418 vim_free(firstline);
1419 firstline = p;
1420 }
1421 }
1422
1423 // Call do_cmdline, which will call getsourceline() to get the lines.
1424 do_cmdline(firstline, getsourceline, (void *)&cookie,
1425 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1426 retval = OK;
1427
1428#ifdef FEAT_PROFILE
1429 if (do_profiling == PROF_YES)
1430 {
1431 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001432 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001433 if (si->sn_prof_on)
1434 {
1435 profile_end(&si->sn_pr_start);
1436 profile_sub_wait(&wait_start, &si->sn_pr_start);
1437 profile_add(&si->sn_pr_total, &si->sn_pr_start);
1438 profile_self(&si->sn_pr_self, &si->sn_pr_start,
1439 &si->sn_pr_children);
1440 }
1441 }
1442#endif
1443
1444 if (got_int)
1445 emsg(_(e_interr));
Bram Moolenaare31ee862020-01-07 20:59:34 +01001446 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001447 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001448 if (p_verbose > 1)
1449 {
1450 verbose_enter();
1451 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001452 if (SOURCING_NAME != NULL)
1453 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001454 verbose_leave();
1455 }
1456#ifdef STARTUPTIME
1457 if (time_fd != NULL)
1458 {
1459 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1460 time_msg((char *)IObuff, &tv_start);
1461 time_pop(&tv_rel);
1462 }
1463#endif
1464
1465 if (!got_int)
1466 trigger_source_post = TRUE;
1467
1468#ifdef FEAT_EVAL
1469 // After a "finish" in debug mode, need to break at first command of next
1470 // sourced file.
1471 if (save_debug_break_level > ex_nesting_level
1472 && debug_break_level == ex_nesting_level)
1473 ++debug_break_level;
1474#endif
1475
1476#ifdef FEAT_EVAL
1477almosttheend:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001479 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001480 if (si->sn_save_cpo != NULL)
1481 {
1482 free_string_option(p_cpo);
1483 p_cpo = si->sn_save_cpo;
1484 si->sn_save_cpo = NULL;
1485 }
1486
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001487 current_sctx = save_current_sctx;
1488 restore_funccal();
1489# ifdef FEAT_PROFILE
1490 if (do_profiling == PROF_YES)
1491 prof_child_exit(&wait_start); // leaving a child now
1492# endif
1493#endif
1494 fclose(cookie.fp);
1495 vim_free(cookie.nextline);
1496 vim_free(firstline);
1497 convert_setup(&cookie.conv, NULL, NULL);
1498
1499 if (trigger_source_post)
1500 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1501
1502theend:
1503 vim_free(fname_exp);
1504 return retval;
1505}
1506
1507#if defined(FEAT_EVAL) || defined(PROTO)
1508
1509/*
1510 * ":scriptnames"
1511 */
1512 void
1513ex_scriptnames(exarg_T *eap)
1514{
1515 int i;
1516
1517 if (eap->addr_count > 0)
1518 {
1519 // :script {scriptId}: edit the script
1520 if (eap->line2 < 1 || eap->line2 > script_items.ga_len)
1521 emsg(_(e_invarg));
1522 else
1523 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001524 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001525 do_exedit(eap, NULL);
1526 }
1527 return;
1528 }
1529
1530 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001531 if (SCRIPT_ITEM(i)->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001532 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001533 home_replace(NULL, SCRIPT_ITEM(i)->sn_name,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001534 NameBuff, MAXPATHL, TRUE);
1535 smsg("%3d: %s", i, NameBuff);
1536 }
1537}
1538
1539# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1540/*
1541 * Fix slashes in the list of script names for 'shellslash'.
1542 */
1543 void
1544scriptnames_slash_adjust(void)
1545{
1546 int i;
1547
1548 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001549 if (SCRIPT_ITEM(i)->sn_name != NULL)
1550 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001551}
1552# endif
1553
1554/*
1555 * Get a pointer to a script name. Used for ":verbose set".
1556 */
1557 char_u *
1558get_scriptname(scid_T id)
1559{
1560 if (id == SID_MODELINE)
1561 return (char_u *)_("modeline");
1562 if (id == SID_CMDARG)
1563 return (char_u *)_("--cmd argument");
1564 if (id == SID_CARG)
1565 return (char_u *)_("-c argument");
1566 if (id == SID_ENV)
1567 return (char_u *)_("environment variable");
1568 if (id == SID_ERROR)
1569 return (char_u *)_("error handler");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001570 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001571}
1572
1573# if defined(EXITFREE) || defined(PROTO)
1574 void
1575free_scriptnames(void)
1576{
1577 int i;
1578
1579 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02001580 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001581 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001583 // the variables themselves are cleared in evalvars_clear()
1584 vim_free(si->sn_vars);
1585
1586 vim_free(si->sn_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01001587 free_imports(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001588 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02001589# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001590 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02001591# endif
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001592 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02001593 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001594 ga_clear(&script_items);
1595}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001596
1597 void
1598free_autoload_scriptnames(void)
1599{
1600 ga_clear_strings(&ga_loaded);
1601}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001602# endif
1603
1604#endif
1605
1606 linenr_T
Bram Moolenaar66250c92020-08-20 15:02:42 +02001607get_sourced_lnum(
1608 char_u *(*fgetline)(int, void *, int, getline_opt_T),
1609 void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001610{
1611 return fgetline == getsourceline
1612 ? ((struct source_cookie *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001613 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001614}
1615
1616 static char_u *
1617get_one_sourceline(struct source_cookie *sp)
1618{
1619 garray_T ga;
1620 int len;
1621 int c;
1622 char_u *buf;
1623#ifdef USE_CRNL
1624 int has_cr; // CR-LF found
1625#endif
1626 int have_read = FALSE;
1627
1628 // use a growarray to store the sourced line
1629 ga_init2(&ga, 1, 250);
1630
1631 // Loop until there is a finished line (or end-of-file).
1632 ++sp->sourcing_lnum;
1633 for (;;)
1634 {
1635 // make room to read at least 120 (more) characters
1636 if (ga_grow(&ga, 120) == FAIL)
1637 break;
1638 buf = (char_u *)ga.ga_data;
1639
1640 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
1641 sp->fp) == NULL)
1642 break;
1643 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
1644#ifdef USE_CRNL
1645 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
1646 // CTRL-Z by its own, or after a NL.
1647 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
1648 && sp->fileformat == EOL_DOS
1649 && buf[len - 1] == Ctrl_Z)
1650 {
1651 buf[len - 1] = NUL;
1652 break;
1653 }
1654#endif
1655
1656 have_read = TRUE;
1657 ga.ga_len = len;
1658
1659 // If the line was longer than the buffer, read more.
1660 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
1661 continue;
1662
1663 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
1664 {
1665#ifdef USE_CRNL
1666 has_cr = (len >= 2 && buf[len - 2] == '\r');
1667 if (sp->fileformat == EOL_UNKNOWN)
1668 {
1669 if (has_cr)
1670 sp->fileformat = EOL_DOS;
1671 else
1672 sp->fileformat = EOL_UNIX;
1673 }
1674
1675 if (sp->fileformat == EOL_DOS)
1676 {
1677 if (has_cr) // replace trailing CR
1678 {
1679 buf[len - 2] = '\n';
1680 --len;
1681 --ga.ga_len;
1682 }
1683 else // lines like ":map xx yy^M" will have failed
1684 {
1685 if (!sp->error)
1686 {
1687 msg_source(HL_ATTR(HLF_W));
1688 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
1689 }
1690 sp->error = TRUE;
1691 sp->fileformat = EOL_UNIX;
1692 }
1693 }
1694#endif
1695 // The '\n' is escaped if there is an odd number of ^V's just
1696 // before it, first set "c" just before the 'V's and then check
1697 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
1698 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
1699 ;
1700 if ((len & 1) != (c & 1)) // escaped NL, read more
1701 {
1702 ++sp->sourcing_lnum;
1703 continue;
1704 }
1705
1706 buf[len - 1] = NUL; // remove the NL
1707 }
1708
1709 // Check for ^C here now and then, so recursive :so can be broken.
1710 line_breakcheck();
1711 break;
1712 }
1713
1714 if (have_read)
1715 return (char_u *)ga.ga_data;
1716
1717 vim_free(ga.ga_data);
1718 return NULL;
1719}
1720
1721/*
1722 * Get one full line from a sourced file.
1723 * Called by do_cmdline() when it's called from do_source().
1724 *
1725 * Return a pointer to the line in allocated memory.
1726 * Return NULL for end-of-file or some error.
1727 */
1728 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02001729getsourceline(
1730 int c UNUSED,
1731 void *cookie,
1732 int indent UNUSED,
1733 getline_opt_T options)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001734{
1735 struct source_cookie *sp = (struct source_cookie *)cookie;
1736 char_u *line;
1737 char_u *p;
1738
1739#ifdef FEAT_EVAL
1740 // If breakpoints have been added/deleted need to check for it.
1741 if (sp->dbg_tick < debug_tick)
1742 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001743 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001744 sp->dbg_tick = debug_tick;
1745 }
1746# ifdef FEAT_PROFILE
1747 if (do_profiling == PROF_YES)
1748 script_line_end();
1749# endif
1750#endif
1751
1752 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001753 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001754
1755 // Get current line. If there is a read-ahead line, use it, otherwise get
1756 // one now.
1757 if (sp->finished)
1758 line = NULL;
1759 else if (sp->nextline == NULL)
1760 line = get_one_sourceline(sp);
1761 else
1762 {
1763 line = sp->nextline;
1764 sp->nextline = NULL;
1765 ++sp->sourcing_lnum;
1766 }
1767#ifdef FEAT_PROFILE
1768 if (line != NULL && do_profiling == PROF_YES)
1769 script_line_start();
1770#endif
1771
1772 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
1773 // contain the 'C' flag.
Bram Moolenaar66250c92020-08-20 15:02:42 +02001774 if (line != NULL && options != GETLINE_NONE
1775 && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001776 {
1777 // compensate for the one line read-ahead
1778 --sp->sourcing_lnum;
1779
1780 // Get the next line and concatenate it when it starts with a
1781 // backslash. We always need to read the next line, keep it in
1782 // sp->nextline.
1783 /* Also check for a comment in between continuation lines: "\ */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001784 // Also check for a Vim9 comment and empty line.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001785 sp->nextline = get_one_sourceline(sp);
1786 if (sp->nextline != NULL
1787 && (*(p = skipwhite(sp->nextline)) == '\\'
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001788 || (p[0] == '"' && p[1] == '\\' && p[2] == ' ')
Bram Moolenaarf3982382020-07-19 16:32:09 +02001789#ifdef FEAT_EVAL
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001790 || (in_vim9script()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001791 && options == GETLINE_CONCAT_ALL
1792 && (*p == NUL || vim9_comment_start(p)))
Bram Moolenaarf3982382020-07-19 16:32:09 +02001793#endif
1794 ))
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001795 {
1796 garray_T ga;
1797
1798 ga_init2(&ga, (int)sizeof(char_u), 400);
1799 ga_concat(&ga, line);
1800 if (*p == '\\')
1801 ga_concat(&ga, p + 1);
1802 for (;;)
1803 {
1804 vim_free(sp->nextline);
1805 sp->nextline = get_one_sourceline(sp);
1806 if (sp->nextline == NULL)
1807 break;
1808 p = skipwhite(sp->nextline);
1809 if (*p == '\\')
1810 {
1811 // Adjust the growsize to the current length to speed up
1812 // concatenating many lines.
1813 if (ga.ga_len > 400)
1814 {
1815 if (ga.ga_len > 8000)
1816 ga.ga_growsize = 8000;
1817 else
1818 ga.ga_growsize = ga.ga_len;
1819 }
1820 ga_concat(&ga, p + 1);
1821 }
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001822 else if (!(p[0] == '"' && p[1] == '\\' && p[2] == ' ')
Bram Moolenaarf3982382020-07-19 16:32:09 +02001823#ifdef FEAT_EVAL
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001824 && !(in_vim9script()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001825 && options == GETLINE_CONCAT_ALL
1826 && (*p == NUL || vim9_comment_start(p)))
Bram Moolenaarf3982382020-07-19 16:32:09 +02001827#endif
1828 )
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001829 break;
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001830 /* drop a # comment or "\ comment line */
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001831 }
1832 ga_append(&ga, NUL);
1833 vim_free(line);
1834 line = ga.ga_data;
1835 }
1836 }
1837
1838 if (line != NULL && sp->conv.vc_type != CONV_NONE)
1839 {
1840 char_u *s;
1841
1842 // Convert the encoding of the script line.
1843 s = string_convert(&sp->conv, line, NULL);
1844 if (s != NULL)
1845 {
1846 vim_free(line);
1847 line = s;
1848 }
1849 }
1850
1851#ifdef FEAT_EVAL
1852 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001853 if (sp->breakpoint != 0 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001854 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001855 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001856 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001857 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001858 sp->dbg_tick = debug_tick;
1859 }
1860#endif
1861
1862 return line;
1863}
1864
1865/*
1866 * ":scriptencoding": Set encoding conversion for a sourced script.
1867 */
1868 void
1869ex_scriptencoding(exarg_T *eap)
1870{
1871 struct source_cookie *sp;
1872 char_u *name;
1873
1874 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
1875 {
1876 emsg(_("E167: :scriptencoding used outside of a sourced file"));
1877 return;
1878 }
1879
1880 if (*eap->arg != NUL)
1881 {
1882 name = enc_canonize(eap->arg);
1883 if (name == NULL) // out of memory
1884 return;
1885 }
1886 else
1887 name = eap->arg;
1888
1889 // Setup for conversion from the specified encoding to 'encoding'.
1890 sp = (struct source_cookie *)getline_cookie(eap->getline, eap->cookie);
1891 convert_setup(&sp->conv, name, p_enc);
1892
1893 if (name != eap->arg)
1894 vim_free(name);
1895}
1896
1897/*
1898 * ":scriptversion": Set Vim script version for a sourced script.
1899 */
1900 void
1901ex_scriptversion(exarg_T *eap UNUSED)
1902{
1903#ifdef FEAT_EVAL
1904 int nr;
1905
1906 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
1907 {
1908 emsg(_("E984: :scriptversion used outside of a sourced file"));
1909 return;
1910 }
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001911 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001912 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001913 emsg(_(e_cannot_use_scriptversion_after_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001914 return;
1915 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001916
1917 nr = getdigits(&eap->arg);
1918 if (nr == 0 || *eap->arg != NUL)
1919 emsg(_(e_invarg));
Bram Moolenaar67979662020-06-20 22:50:47 +02001920 else if (nr > SCRIPT_VERSION_MAX)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001921 semsg(_("E999: scriptversion not supported: %d"), nr);
1922 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001923 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001924 current_sctx.sc_version = nr;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001925 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001926 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001927#endif
1928}
1929
1930#if defined(FEAT_EVAL) || defined(PROTO)
1931/*
1932 * ":finish": Mark a sourced file as finished.
1933 */
1934 void
1935ex_finish(exarg_T *eap)
1936{
1937 if (getline_equal(eap->getline, eap->cookie, getsourceline))
1938 do_finish(eap, FALSE);
1939 else
1940 emsg(_("E168: :finish used outside of a sourced file"));
1941}
1942
1943/*
1944 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
1945 * Also called for a pending finish at the ":endtry" or after returning from
1946 * an extra do_cmdline(). "reanimate" is used in the latter case.
1947 */
1948 void
1949do_finish(exarg_T *eap, int reanimate)
1950{
1951 int idx;
1952
1953 if (reanimate)
1954 ((struct source_cookie *)getline_cookie(eap->getline,
1955 eap->cookie))->finished = FALSE;
1956
1957 // Cleanup (and inactivate) conditionals, but stop when a try conditional
1958 // not in its finally clause (which then is to be executed next) is found.
1959 // In this case, make the ":finish" pending for execution at the ":endtry".
1960 // Otherwise, finish normally.
1961 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
1962 if (idx >= 0)
1963 {
1964 eap->cstack->cs_pending[idx] = CSTP_FINISH;
1965 report_make_pending(CSTP_FINISH, NULL);
1966 }
1967 else
1968 ((struct source_cookie *)getline_cookie(eap->getline,
1969 eap->cookie))->finished = TRUE;
1970}
1971
1972
1973/*
1974 * Return TRUE when a sourced file had the ":finish" command: Don't give error
1975 * message for missing ":endif".
1976 * Return FALSE when not sourcing a file.
1977 */
1978 int
1979source_finished(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001980 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001981 void *cookie)
1982{
1983 return (getline_equal(fgetline, cookie, getsourceline)
1984 && ((struct source_cookie *)getline_cookie(
1985 fgetline, cookie))->finished);
1986}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001987
1988/*
1989 * Return the autoload script name for a function or variable name.
1990 * Returns NULL when out of memory.
1991 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
1992 */
1993 char_u *
1994autoload_name(char_u *name)
1995{
1996 char_u *p, *q = NULL;
1997 char_u *scriptname;
1998
1999 // Get the script file name: replace '#' with '/', append ".vim".
2000 scriptname = alloc(STRLEN(name) + 14);
2001 if (scriptname == NULL)
2002 return NULL;
2003 STRCPY(scriptname, "autoload/");
Bram Moolenaara1773442020-08-12 15:21:22 +02002004 STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002005 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
2006 q = p, ++p)
2007 *p = '/';
2008 STRCPY(q, ".vim");
2009 return scriptname;
2010}
2011
2012/*
2013 * If "name" has a package name try autoloading the script for it.
2014 * Return TRUE if a package was loaded.
2015 */
2016 int
2017script_autoload(
2018 char_u *name,
2019 int reload) // load script again when already loaded
2020{
2021 char_u *p;
2022 char_u *scriptname, *tofree;
2023 int ret = FALSE;
2024 int i;
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002025 int ret_sid;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002026
2027 // If there is no '#' after name[0] there is no package name.
2028 p = vim_strchr(name, AUTOLOAD_CHAR);
2029 if (p == NULL || p == name)
2030 return FALSE;
2031
2032 tofree = scriptname = autoload_name(name);
2033 if (scriptname == NULL)
2034 return FALSE;
2035
2036 // Find the name in the list of previously loaded package names. Skip
2037 // "autoload/", it's always the same.
2038 for (i = 0; i < ga_loaded.ga_len; ++i)
2039 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2040 break;
2041 if (!reload && i < ga_loaded.ga_len)
2042 ret = FALSE; // was loaded already
2043 else
2044 {
2045 // Remember the name if it wasn't loaded already.
2046 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2047 {
2048 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2049 tofree = NULL;
2050 }
2051
2052 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
Bram Moolenaardaa2f362020-08-08 21:33:21 +02002053 // Use "ret_sid" to avoid loading the same script again.
2054 if (source_in_path(p_rtp, scriptname, 0, &ret_sid) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002055 ret = TRUE;
2056 }
2057
2058 vim_free(tofree);
2059 return ret;
2060}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002061#endif