blob: f42c3d04b06048bff50549c76b5e4bacd1062586 [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 */
71 void
72estack_push_ufunc(etype_T type, ufunc_T *ufunc, long lnum)
73{
74 estack_T *entry = estack_push(type,
75 ufunc->uf_name_exp != NULL
76 ? ufunc->uf_name_exp : ufunc->uf_name, lnum);
77 if (entry != NULL)
78 entry->es_info.ufunc = ufunc;
79}
Bram Moolenaar09d46402019-12-29 23:53:01 +010080#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010081
82/*
83 * Take an item off of the execution stack.
84 */
85 void
86estack_pop(void)
87{
88 if (exestack.ga_len > 1)
89 --exestack.ga_len;
90}
91
92/*
93 * Get the current value for <sfile> in allocated memory.
94 */
95 char_u *
96estack_sfile(void)
97{
Bram Moolenaar85b09572019-12-30 10:57:00 +010098 estack_T *entry;
99#ifdef FEAT_EVAL
Bram Moolenaar4d7a2482020-01-06 19:53:43 +0100100 size_t len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100101 int idx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100102 char *res;
Bram Moolenaar4d7a2482020-01-06 19:53:43 +0100103 size_t done;
Bram Moolenaar85b09572019-12-30 10:57:00 +0100104#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100105
106 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
107 if (entry->es_name == NULL)
108 return NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100109#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100110 if (entry->es_info.ufunc == NULL)
Bram Moolenaar09d46402019-12-29 23:53:01 +0100111#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100112 return vim_strsave(entry->es_name);
113
Bram Moolenaar09d46402019-12-29 23:53:01 +0100114#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100115 // For a function we compose the call stack, as it was done in the past:
116 // "function One[123]..Two[456]..Three"
117 len = STRLEN(entry->es_name) + 10;
118 for (idx = exestack.ga_len - 2; idx >= 0; --idx)
119 {
120 entry = ((estack_T *)exestack.ga_data) + idx;
121 if (entry->es_name == NULL || entry->es_info.ufunc == NULL)
122 {
123 ++idx;
124 break;
125 }
126 len += STRLEN(entry->es_name) + 15;
127 }
128
Bram Moolenaar4d7a2482020-01-06 19:53:43 +0100129 res = (char *)alloc((int)len);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100130 if (res != NULL)
131 {
132 STRCPY(res, "function ");
133 while (idx < exestack.ga_len - 1)
134 {
135 done = STRLEN(res);
136 entry = ((estack_T *)exestack.ga_data) + idx;
137 vim_snprintf(res + done, len - done, "%s[%ld]..",
138 entry->es_name, entry->es_lnum);
139 ++idx;
140 }
141 done = STRLEN(res);
142 entry = ((estack_T *)exestack.ga_data) + idx;
143 vim_snprintf(res + done, len - done, "%s", entry->es_name);
144 }
145 return (char_u *)res;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100146#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100147}
148
149/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200150 * ":runtime [what] {name}"
151 */
152 void
153ex_runtime(exarg_T *eap)
154{
155 char_u *arg = eap->arg;
156 char_u *p = skiptowhite(arg);
157 int len = (int)(p - arg);
158 int flags = eap->forceit ? DIP_ALL : 0;
159
160 if (STRNCMP(arg, "START", len) == 0)
161 {
162 flags += DIP_START + DIP_NORTP;
163 arg = skipwhite(arg + len);
164 }
165 else if (STRNCMP(arg, "OPT", len) == 0)
166 {
167 flags += DIP_OPT + DIP_NORTP;
168 arg = skipwhite(arg + len);
169 }
170 else if (STRNCMP(arg, "PACK", len) == 0)
171 {
172 flags += DIP_START + DIP_OPT + DIP_NORTP;
173 arg = skipwhite(arg + len);
174 }
175 else if (STRNCMP(arg, "ALL", len) == 0)
176 {
177 flags += DIP_START + DIP_OPT;
178 arg = skipwhite(arg + len);
179 }
180
181 source_runtime(arg, flags);
182}
183
184 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100185source_callback(char_u *fname, void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200186{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100187 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200188}
189
190/*
191 * Find the file "name" in all directories in "path" and invoke
192 * "callback(fname, cookie)".
193 * "name" can contain wildcards.
194 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
195 * When "flags" has DIP_DIR: find directories instead of files.
196 * When "flags" has DIP_ERR: give an error message if there is no match.
197 *
198 * return FAIL when no file could be sourced, OK otherwise.
199 */
200 int
201do_in_path(
202 char_u *path,
203 char_u *name,
204 int flags,
205 void (*callback)(char_u *fname, void *ck),
206 void *cookie)
207{
208 char_u *rtp;
209 char_u *np;
210 char_u *buf;
211 char_u *rtp_copy;
212 char_u *tail;
213 int num_files;
214 char_u **files;
215 int i;
216 int did_one = FALSE;
217#ifdef AMIGA
218 struct Process *proc = (struct Process *)FindTask(0L);
219 APTR save_winptr = proc->pr_WindowPtr;
220
221 // Avoid a requester here for a volume that doesn't exist.
222 proc->pr_WindowPtr = (APTR)-1L;
223#endif
224
225 // Make a copy of 'runtimepath'. Invoking the callback may change the
226 // value.
227 rtp_copy = vim_strsave(path);
228 buf = alloc(MAXPATHL);
229 if (buf != NULL && rtp_copy != NULL)
230 {
Bram Moolenaar647a5302020-05-03 17:01:24 +0200231 if (p_verbose > 10 && name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200232 {
233 verbose_enter();
234 smsg(_("Searching for \"%s\" in \"%s\""),
235 (char *)name, (char *)path);
236 verbose_leave();
237 }
238
239 // Loop over all entries in 'runtimepath'.
240 rtp = rtp_copy;
241 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
242 {
243 size_t buflen;
244
245 // Copy the path from 'runtimepath' to buf[].
246 copy_option_part(&rtp, buf, MAXPATHL, ",");
247 buflen = STRLEN(buf);
248
249 // Skip after or non-after directories.
250 if (flags & (DIP_NOAFTER | DIP_AFTER))
251 {
252 int is_after = buflen >= 5
253 && STRCMP(buf + buflen - 5, "after") == 0;
254
255 if ((is_after && (flags & DIP_NOAFTER))
256 || (!is_after && (flags & DIP_AFTER)))
257 continue;
258 }
259
260 if (name == NULL)
261 {
262 (*callback)(buf, (void *) &cookie);
263 if (!did_one)
264 did_one = (cookie == NULL);
265 }
266 else if (buflen + STRLEN(name) + 2 < MAXPATHL)
267 {
268 add_pathsep(buf);
269 tail = buf + STRLEN(buf);
270
271 // Loop over all patterns in "name"
272 np = name;
273 while (*np != NUL && ((flags & DIP_ALL) || !did_one))
274 {
275 // Append the pattern from "name" to buf[].
276 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
277 "\t ");
278
Bram Moolenaar647a5302020-05-03 17:01:24 +0200279 if (p_verbose > 10)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200280 {
281 verbose_enter();
282 smsg(_("Searching for \"%s\""), buf);
283 verbose_leave();
284 }
285
286 // Expand wildcards, invoke the callback for each match.
287 if (gen_expand_wildcards(1, &buf, &num_files, &files,
288 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
289 {
290 for (i = 0; i < num_files; ++i)
291 {
292 (*callback)(files[i], cookie);
293 did_one = TRUE;
294 if (!(flags & DIP_ALL))
295 break;
296 }
297 FreeWild(num_files, files);
298 }
299 }
300 }
301 }
302 }
303 vim_free(buf);
304 vim_free(rtp_copy);
305 if (!did_one && name != NULL)
306 {
307 char *basepath = path == p_rtp ? "runtimepath" : "packpath";
308
309 if (flags & DIP_ERR)
310 semsg(_(e_dirnotf), basepath, name);
311 else if (p_verbose > 0)
312 {
313 verbose_enter();
314 smsg(_("not found in '%s': \"%s\""), basepath, name);
315 verbose_leave();
316 }
317 }
318
319#ifdef AMIGA
320 proc->pr_WindowPtr = save_winptr;
321#endif
322
323 return did_one ? OK : FAIL;
324}
325
326/*
327 * Find "name" in "path". When found, invoke the callback function for
328 * it: callback(fname, "cookie")
329 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first
330 * one is used.
331 * Returns OK when at least one match found, FAIL otherwise.
332 *
333 * If "name" is NULL calls callback for each entry in "path". Cookie is
334 * passed by reference in this case, setting it to NULL indicates that callback
335 * has done its job.
336 */
337 static int
338do_in_path_and_pp(
339 char_u *path,
340 char_u *name,
341 int flags,
342 void (*callback)(char_u *fname, void *ck),
343 void *cookie)
344{
345 int done = FAIL;
346 char_u *s;
347 int len;
348 char *start_dir = "pack/*/start/*/%s";
349 char *opt_dir = "pack/*/opt/*/%s";
350
351 if ((flags & DIP_NORTP) == 0)
352 done = do_in_path(path, name, flags, callback, cookie);
353
354 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
355 {
356 len = (int)(STRLEN(start_dir) + STRLEN(name));
357 s = alloc(len);
358 if (s == NULL)
359 return FAIL;
360 vim_snprintf((char *)s, len, start_dir, name);
361 done = do_in_path(p_pp, s, flags, callback, cookie);
362 vim_free(s);
363 }
364
365 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
366 {
367 len = (int)(STRLEN(opt_dir) + STRLEN(name));
368 s = alloc(len);
369 if (s == NULL)
370 return FAIL;
371 vim_snprintf((char *)s, len, opt_dir, name);
372 done = do_in_path(p_pp, s, flags, callback, cookie);
373 vim_free(s);
374 }
375
376 return done;
377}
378
379/*
380 * Just like do_in_path_and_pp(), using 'runtimepath' for "path".
381 */
382 int
383do_in_runtimepath(
384 char_u *name,
385 int flags,
386 void (*callback)(char_u *fname, void *ck),
387 void *cookie)
388{
389 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
390}
391
392/*
393 * Source the file "name" from all directories in 'runtimepath'.
394 * "name" can contain wildcards.
395 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
396 *
397 * return FAIL when no file could be sourced, OK otherwise.
398 */
399 int
400source_runtime(char_u *name, int flags)
401{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100402 return source_in_path(p_rtp, name, flags, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200403}
404
405/*
406 * Just like source_runtime(), but use "path" instead of 'runtimepath'.
407 */
408 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100409source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200410{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100411 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200412}
413
414
415#if defined(FEAT_EVAL) || defined(PROTO)
416
417/*
418 * Expand wildcards in "pat" and invoke do_source() for each match.
419 */
420 static void
421source_all_matches(char_u *pat)
422{
423 int num_files;
424 char_u **files;
425 int i;
426
427 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK)
428 {
429 for (i = 0; i < num_files; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100430 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200431 FreeWild(num_files, files);
432 }
433}
434
435/*
436 * Add the package directory to 'runtimepath'.
437 */
438 static int
439add_pack_dir_to_rtp(char_u *fname)
440{
441 char_u *p4, *p3, *p2, *p1, *p;
442 char_u *entry;
443 char_u *insp = NULL;
444 int c;
445 char_u *new_rtp;
446 int keep;
447 size_t oldlen;
448 size_t addlen;
449 size_t new_rtp_len;
450 char_u *afterdir = NULL;
451 size_t afterlen = 0;
452 char_u *after_insp = NULL;
453 char_u *ffname = NULL;
454 size_t fname_len;
455 char_u *buf = NULL;
456 char_u *rtp_ffname;
457 int match;
458 int retval = FAIL;
459
460 p4 = p3 = p2 = p1 = get_past_head(fname);
461 for (p = p1; *p; MB_PTR_ADV(p))
462 if (vim_ispathsep_nocolon(*p))
463 {
464 p4 = p3; p3 = p2; p2 = p1; p1 = p;
465 }
466
467 // now we have:
468 // rtp/pack/name/start/name
469 // p4 p3 p2 p1
470 //
471 // find the part up to "pack" in 'runtimepath'
472 c = *++p4; // append pathsep in order to expand symlink
473 *p4 = NUL;
474 ffname = fix_fname(fname);
475 *p4 = c;
476 if (ffname == NULL)
477 return FAIL;
478
479 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
480 // Also stop at the first "after" directory.
481 fname_len = STRLEN(ffname);
482 buf = alloc(MAXPATHL);
483 if (buf == NULL)
484 goto theend;
485 for (entry = p_rtp; *entry != NUL; )
486 {
487 char_u *cur_entry = entry;
488
489 copy_option_part(&entry, buf, MAXPATHL, ",");
490 if (insp == NULL)
491 {
492 add_pathsep(buf);
493 rtp_ffname = fix_fname(buf);
494 if (rtp_ffname == NULL)
495 goto theend;
496 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
497 vim_free(rtp_ffname);
498 if (match)
499 // Insert "ffname" after this entry (and comma).
500 insp = entry;
501 }
502
503 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
504 && p > buf
505 && vim_ispathsep(p[-1])
506 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
507 {
508 if (insp == NULL)
509 // Did not find "ffname" before the first "after" directory,
510 // insert it before this entry.
511 insp = cur_entry;
512 after_insp = cur_entry;
513 break;
514 }
515 }
516
517 if (insp == NULL)
518 // Both "fname" and "after" not found, append at the end.
519 insp = p_rtp + STRLEN(p_rtp);
520
521 // check if rtp/pack/name/start/name/after exists
522 afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
523 if (afterdir != NULL && mch_isdir(afterdir))
524 afterlen = STRLEN(afterdir) + 1; // add one for comma
525
526 oldlen = STRLEN(p_rtp);
527 addlen = STRLEN(fname) + 1; // add one for comma
528 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
529 if (new_rtp == NULL)
530 goto theend;
531
532 // We now have 'rtp' parts: {keep}{keep_after}{rest}.
533 // Create new_rtp, first: {keep},{fname}
534 keep = (int)(insp - p_rtp);
535 mch_memmove(new_rtp, p_rtp, keep);
536 new_rtp_len = keep;
537 if (*insp == NUL)
538 new_rtp[new_rtp_len++] = ','; // add comma before
539 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
540 new_rtp_len += addlen - 1;
541 if (*insp != NUL)
542 new_rtp[new_rtp_len++] = ','; // add comma after
543
544 if (afterlen > 0 && after_insp != NULL)
545 {
546 int keep_after = (int)(after_insp - p_rtp);
547
548 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
549 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
550 keep_after - keep);
551 new_rtp_len += keep_after - keep;
552 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
553 new_rtp_len += afterlen - 1;
554 new_rtp[new_rtp_len++] = ',';
555 keep = keep_after;
556 }
557
558 if (p_rtp[keep] != NUL)
559 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
560 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
561 else
562 new_rtp[new_rtp_len] = NUL;
563
564 if (afterlen > 0 && after_insp == NULL)
565 {
566 // Append afterdir when "after" was not found:
567 // {keep},{fname}{rest},{afterdir}
568 STRCAT(new_rtp, ",");
569 STRCAT(new_rtp, afterdir);
570 }
571
572 set_option_value((char_u *)"rtp", 0L, new_rtp, 0);
573 vim_free(new_rtp);
574 retval = OK;
575
576theend:
577 vim_free(buf);
578 vim_free(ffname);
579 vim_free(afterdir);
580 return retval;
581}
582
583/*
584 * Load scripts in "plugin" and "ftdetect" directories of the package.
585 */
586 static int
587load_pack_plugin(char_u *fname)
588{
589 static char *plugpat = "%s/plugin/**/*.vim";
590 static char *ftpat = "%s/ftdetect/*.vim";
591 int len;
592 char_u *ffname = fix_fname(fname);
593 char_u *pat = NULL;
594 int retval = FAIL;
595
596 if (ffname == NULL)
597 return FAIL;
598 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
599 pat = alloc(len);
600 if (pat == NULL)
601 goto theend;
602 vim_snprintf((char *)pat, len, plugpat, ffname);
603 source_all_matches(pat);
604
605 {
606 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
607
608 // If runtime/filetype.vim wasn't loaded yet, the scripts will be
609 // found when it loads.
610 if (cmd != NULL && eval_to_number(cmd) > 0)
611 {
612 do_cmdline_cmd((char_u *)"augroup filetypedetect");
613 vim_snprintf((char *)pat, len, ftpat, ffname);
614 source_all_matches(pat);
615 do_cmdline_cmd((char_u *)"augroup END");
616 }
617 vim_free(cmd);
618 }
619 vim_free(pat);
620 retval = OK;
621
622theend:
623 vim_free(ffname);
624 return retval;
625}
626
627// used for "cookie" of add_pack_plugin()
628static int APP_ADD_DIR;
629static int APP_LOAD;
630static int APP_BOTH;
631
632 static void
633add_pack_plugin(char_u *fname, void *cookie)
634{
635 if (cookie != &APP_LOAD)
636 {
637 char_u *buf = alloc(MAXPATHL);
638 char_u *p;
639 int found = FALSE;
640
641 if (buf == NULL)
642 return;
643 p = p_rtp;
644 while (*p != NUL)
645 {
646 copy_option_part(&p, buf, MAXPATHL, ",");
647 if (pathcmp((char *)buf, (char *)fname, -1) == 0)
648 {
649 found = TRUE;
650 break;
651 }
652 }
653 vim_free(buf);
654 if (!found)
655 // directory is not yet in 'runtimepath', add it
656 if (add_pack_dir_to_rtp(fname) == FAIL)
657 return;
658 }
659
660 if (cookie != &APP_ADD_DIR)
661 load_pack_plugin(fname);
662}
663
664/*
665 * Add all packages in the "start" directory to 'runtimepath'.
666 */
667 void
668add_pack_start_dirs(void)
669{
670 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
671 add_pack_plugin, &APP_ADD_DIR);
672}
673
674/*
675 * Load plugins from all packages in the "start" directory.
676 */
677 void
678load_start_packages(void)
679{
680 did_source_packages = TRUE;
681 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
682 add_pack_plugin, &APP_LOAD);
683}
684
685/*
686 * ":packloadall"
687 * Find plugins in the package directories and source them.
688 */
689 void
690ex_packloadall(exarg_T *eap)
691{
692 if (!did_source_packages || eap->forceit)
693 {
694 // First do a round to add all directories to 'runtimepath', then load
695 // the plugins. This allows for plugins to use an autoload directory
696 // of another plugin.
697 add_pack_start_dirs();
698 load_start_packages();
699 }
700}
701
702/*
703 * ":packadd[!] {name}"
704 */
705 void
706ex_packadd(exarg_T *eap)
707{
708 static char *plugpat = "pack/*/%s/%s";
709 int len;
710 char *pat;
711 int round;
712 int res = OK;
713
714 // Round 1: use "start", round 2: use "opt".
715 for (round = 1; round <= 2; ++round)
716 {
717 // Only look under "start" when loading packages wasn't done yet.
718 if (round == 1 && did_source_packages)
719 continue;
720
721 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
722 pat = alloc(len);
723 if (pat == NULL)
724 return;
725 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
726 // The first round don't give a "not found" error, in the second round
727 // only when nothing was found in the first round.
728 res = do_in_path(p_pp, (char_u *)pat,
729 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
730 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
731 vim_free(pat);
732 }
733}
734#endif
735
736/*
Bram Moolenaar26262f82019-09-04 20:59:15 +0200737 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
738 * list of file names in allocated memory.
739 */
740 void
741remove_duplicates(garray_T *gap)
742{
743 int i;
744 int j;
745 char_u **fnames = (char_u **)gap->ga_data;
746
747 sort_strings(fnames, gap->ga_len);
748 for (i = gap->ga_len - 1; i > 0; --i)
749 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
750 {
751 vim_free(fnames[i]);
752 for (j = i + 1; j < gap->ga_len; ++j)
753 fnames[j - 1] = fnames[j];
754 --gap->ga_len;
755 }
756}
757
758/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200759 * Expand color scheme, compiler or filetype names.
760 * Search from 'runtimepath':
761 * 'runtimepath'/{dirnames}/{pat}.vim
762 * When "flags" has DIP_START: search also from 'start' of 'packpath':
763 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
764 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
765 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
766 * "dirnames" is an array with one or more directory names.
767 */
768 int
769ExpandRTDir(
770 char_u *pat,
771 int flags,
772 int *num_file,
773 char_u ***file,
774 char *dirnames[])
775{
776 char_u *s;
777 char_u *e;
778 char_u *match;
779 garray_T ga;
780 int i;
781 int pat_len;
782
783 *num_file = 0;
784 *file = NULL;
785 pat_len = (int)STRLEN(pat);
786 ga_init2(&ga, (int)sizeof(char *), 10);
787
788 for (i = 0; dirnames[i] != NULL; ++i)
789 {
790 s = alloc(STRLEN(dirnames[i]) + pat_len + 7);
791 if (s == NULL)
792 {
793 ga_clear_strings(&ga);
794 return FAIL;
795 }
796 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
797 globpath(p_rtp, s, &ga, 0);
798 vim_free(s);
799 }
800
801 if (flags & DIP_START) {
802 for (i = 0; dirnames[i] != NULL; ++i)
803 {
804 s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
805 if (s == NULL)
806 {
807 ga_clear_strings(&ga);
808 return FAIL;
809 }
810 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
811 globpath(p_pp, s, &ga, 0);
812 vim_free(s);
813 }
814 }
815
816 if (flags & DIP_OPT) {
817 for (i = 0; dirnames[i] != NULL; ++i)
818 {
819 s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
820 if (s == NULL)
821 {
822 ga_clear_strings(&ga);
823 return FAIL;
824 }
825 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
826 globpath(p_pp, s, &ga, 0);
827 vim_free(s);
828 }
829 }
830
831 for (i = 0; i < ga.ga_len; ++i)
832 {
833 match = ((char_u **)ga.ga_data)[i];
834 s = match;
835 e = s + STRLEN(s);
836 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
837 {
838 e -= 4;
839 for (s = e; s > match; MB_PTR_BACK(match, s))
840 if (s < match || vim_ispathsep(*s))
841 break;
842 ++s;
843 *e = NUL;
844 mch_memmove(match, s, e - s + 1);
845 }
846 }
847
848 if (ga.ga_len == 0)
849 return FAIL;
850
851 // Sort and remove duplicates which can happen when specifying multiple
852 // directories in dirnames.
853 remove_duplicates(&ga);
854
855 *file = ga.ga_data;
856 *num_file = ga.ga_len;
857 return OK;
858}
859
860/*
861 * Expand loadplugin names:
862 * 'packpath'/pack/ * /opt/{pat}
863 */
864 int
865ExpandPackAddDir(
866 char_u *pat,
867 int *num_file,
868 char_u ***file)
869{
870 char_u *s;
871 char_u *e;
872 char_u *match;
873 garray_T ga;
874 int i;
875 int pat_len;
876
877 *num_file = 0;
878 *file = NULL;
879 pat_len = (int)STRLEN(pat);
880 ga_init2(&ga, (int)sizeof(char *), 10);
881
882 s = alloc(pat_len + 26);
883 if (s == NULL)
884 {
885 ga_clear_strings(&ga);
886 return FAIL;
887 }
888 sprintf((char *)s, "pack/*/opt/%s*", pat);
889 globpath(p_pp, s, &ga, 0);
890 vim_free(s);
891
892 for (i = 0; i < ga.ga_len; ++i)
893 {
894 match = ((char_u **)ga.ga_data)[i];
895 s = gettail(match);
896 e = s + STRLEN(s);
897 mch_memmove(match, s, e - s + 1);
898 }
899
900 if (ga.ga_len == 0)
901 return FAIL;
902
903 // Sort and remove duplicates which can happen when specifying multiple
904 // directories in dirnames.
905 remove_duplicates(&ga);
906
907 *file = ga.ga_data;
908 *num_file = ga.ga_len;
909 return OK;
910}
911
912 static void
913cmd_source(char_u *fname, exarg_T *eap)
914{
915 if (*fname == NUL)
916 emsg(_(e_argreq));
917
918 else if (eap != NULL && eap->forceit)
919 // ":source!": read Normal mode commands
920 // Need to execute the commands directly. This is required at least
921 // for:
922 // - ":g" command busy
923 // - after ":argdo", ":windo" or ":bufdo"
924 // - another command follows
925 // - inside a loop
926 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
927#ifdef FEAT_EVAL
928 || eap->cstack->cs_idx >= 0
929#endif
930 );
931
932 // ":source" read ex commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200934 semsg(_(e_notopen), fname);
935}
936
937/*
938 * ":source {fname}"
939 */
940 void
941ex_source(exarg_T *eap)
942{
943#ifdef FEAT_BROWSE
944 if (cmdmod.browse)
945 {
946 char_u *fname = NULL;
947
948 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
949 NULL, NULL,
950 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
951 if (fname != NULL)
952 {
953 cmd_source(fname, eap);
954 vim_free(fname);
955 }
956 }
957 else
958#endif
959 cmd_source(eap->arg, eap);
960}
961
962#if defined(FEAT_EVAL) || defined(PROTO)
963/*
964 * ":options"
965 */
966 void
967ex_options(
968 exarg_T *eap UNUSED)
969{
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200970 char_u buf[500];
971 int multi_mods = 0;
972
973 buf[0] = NUL;
974 (void)add_win_cmd_modifers(buf, &multi_mods);
975
976 vim_setenv((char_u *)"OPTWIN_CMD", buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200977 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
978}
979#endif
980
981/*
982 * ":source" and associated commands.
983 */
984/*
985 * Structure used to store info for each sourced file.
986 * It is shared between do_source() and getsourceline().
987 * This is required, because it needs to be handed to do_cmdline() and
988 * sourcing can be done recursively.
989 */
990struct source_cookie
991{
992 FILE *fp; // opened file for sourcing
993 char_u *nextline; // if not NULL: line that was read ahead
994 linenr_T sourcing_lnum; // line number of the source file
995 int finished; // ":finish" used
996#ifdef USE_CRNL
997 int fileformat; // EOL_UNKNOWN, EOL_UNIX or EOL_DOS
998 int error; // TRUE if LF found after CR-LF
999#endif
1000#ifdef FEAT_EVAL
1001 linenr_T breakpoint; // next line with breakpoint or zero
1002 char_u *fname; // name of sourced file
1003 int dbg_tick; // debug_tick when breakpoint was set
1004 int level; // top nesting level of sourced file
1005#endif
1006 vimconv_T conv; // type of conversion
1007};
1008
1009#ifdef FEAT_EVAL
1010/*
1011 * Return the address holding the next breakpoint line for a source cookie.
1012 */
1013 linenr_T *
1014source_breakpoint(void *cookie)
1015{
1016 return &((struct source_cookie *)cookie)->breakpoint;
1017}
1018
1019/*
1020 * Return the address holding the debug tick for a source cookie.
1021 */
1022 int *
1023source_dbg_tick(void *cookie)
1024{
1025 return &((struct source_cookie *)cookie)->dbg_tick;
1026}
1027
1028/*
1029 * Return the nesting level for a source cookie.
1030 */
1031 int
1032source_level(void *cookie)
1033{
1034 return ((struct source_cookie *)cookie)->level;
1035}
1036#endif
1037
1038#if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC)
1039# define USE_FOPEN_NOINH
1040/*
1041 * Special function to open a file without handle inheritance.
1042 * When possible the handle is closed on exec().
1043 */
1044 static FILE *
1045fopen_noinh_readbin(char *filename)
1046{
1047# ifdef MSWIN
1048 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
1049# else
1050 int fd_tmp = mch_open(filename, O_RDONLY, 0);
1051# endif
1052
1053 if (fd_tmp == -1)
1054 return NULL;
1055
1056# ifdef HAVE_FD_CLOEXEC
1057 {
1058 int fdflags = fcntl(fd_tmp, F_GETFD);
1059 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1060 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
1061 }
1062# endif
1063
1064 return fdopen(fd_tmp, READBIN);
1065}
1066#endif
1067
1068/*
1069 * do_source: Read the file "fname" and execute its lines as EX commands.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001070 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1071 * again.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001072 *
1073 * This function may be called recursively!
1074 *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075 * Return FAIL if file could not be opened, OK otherwise.
1076 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001077 */
1078 int
1079do_source(
1080 char_u *fname,
1081 int check_other, // check for .vimrc and _vimrc
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001082 int is_vimrc, // DOSO_ value
1083 int *ret_sid UNUSED)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001084{
1085 struct source_cookie cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001086 char_u *p;
1087 char_u *fname_exp;
1088 char_u *firstline = NULL;
1089 int retval = FAIL;
1090#ifdef FEAT_EVAL
1091 sctx_T save_current_sctx;
1092 static scid_T last_current_SID = 0;
1093 static int last_current_SID_seq = 0;
1094 funccal_entry_T funccalp_entry;
1095 int save_debug_break_level = debug_break_level;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096 int sid;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001097 scriptitem_T *si = NULL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001098#endif
1099#ifdef STARTUPTIME
1100 struct timeval tv_rel;
1101 struct timeval tv_start;
1102#endif
1103#ifdef FEAT_PROFILE
1104 proftime_T wait_start;
1105#endif
1106 int trigger_source_post = FALSE;
Bram Moolenaare31ee862020-01-07 20:59:34 +01001107 ESTACK_CHECK_DECLARATION
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001108
1109 p = expand_env_save(fname);
1110 if (p == NULL)
1111 return retval;
1112 fname_exp = fix_fname(p);
1113 vim_free(p);
1114 if (fname_exp == NULL)
1115 return retval;
1116 if (mch_isdir(fname_exp))
1117 {
1118 smsg(_("Cannot source a directory: \"%s\""), fname);
1119 goto theend;
1120 }
1121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122#ifdef FEAT_EVAL
1123 // See if we loaded this script before.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001124 for (sid = script_items.ga_len; sid > 0; --sid)
1125 {
Bram Moolenaar978d1702020-01-26 17:38:12 +01001126 // We used to check inode here, but that doesn't work:
1127 // - If a script is edited and written, it may get a different
1128 // inode number, even though to the user it is the same script.
1129 // - If a script is deleted and another script is written, with a
1130 // different name, the inode may be re-used.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001131 si = SCRIPT_ITEM(sid);
Bram Moolenaar978d1702020-01-26 17:38:12 +01001132 if (si->sn_name != NULL && fnamecmp(si->sn_name, fname_exp) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001133 // Found it!
1134 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135 }
1136 if (sid > 0 && ret_sid != NULL)
1137 {
1138 // Already loaded and no need to load again, return here.
1139 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001140 retval = OK;
1141 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 }
1143#endif
1144
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001145 // Apply SourceCmd autocommands, they should get the file and source it.
1146 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1147 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1148 FALSE, curbuf))
1149 {
1150#ifdef FEAT_EVAL
1151 retval = aborting() ? FAIL : OK;
1152#else
1153 retval = OK;
1154#endif
1155 if (retval == OK)
1156 // Apply SourcePost autocommands.
1157 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1158 FALSE, curbuf);
1159 goto theend;
1160 }
1161
1162 // Apply SourcePre autocommands, they may get the file.
1163 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1164
1165#ifdef USE_FOPEN_NOINH
1166 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1167#else
1168 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1169#endif
1170 if (cookie.fp == NULL && check_other)
1171 {
1172 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1173 // and ".exrc" by "_exrc" or vice versa.
1174 p = gettail(fname_exp);
1175 if ((*p == '.' || *p == '_')
1176 && (STRICMP(p + 1, "vimrc") == 0
1177 || STRICMP(p + 1, "gvimrc") == 0
1178 || STRICMP(p + 1, "exrc") == 0))
1179 {
1180 if (*p == '_')
1181 *p = '.';
1182 else
1183 *p = '_';
1184#ifdef USE_FOPEN_NOINH
1185 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1186#else
1187 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1188#endif
1189 }
1190 }
1191
1192 if (cookie.fp == NULL)
1193 {
1194 if (p_verbose > 0)
1195 {
1196 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001197 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001198 smsg(_("could not source \"%s\""), fname);
1199 else
1200 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001201 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001202 verbose_leave();
1203 }
1204 goto theend;
1205 }
1206
1207 // The file exists.
1208 // - In verbose mode, give a message.
1209 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1210 if (p_verbose > 1)
1211 {
1212 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001213 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001214 smsg(_("sourcing \"%s\""), fname);
1215 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001216 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001217 verbose_leave();
1218 }
1219 if (is_vimrc == DOSO_VIMRC)
1220 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1221 else if (is_vimrc == DOSO_GVIMRC)
1222 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1223
1224#ifdef USE_CRNL
1225 // If no automatic file format: Set default to CR-NL.
1226 if (*p_ffs == NUL)
1227 cookie.fileformat = EOL_DOS;
1228 else
1229 cookie.fileformat = EOL_UNKNOWN;
1230 cookie.error = FALSE;
1231#endif
1232
1233 cookie.nextline = NULL;
1234 cookie.sourcing_lnum = 0;
1235 cookie.finished = FALSE;
1236
1237#ifdef FEAT_EVAL
1238 // Check if this script has a breakpoint.
1239 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1240 cookie.fname = fname_exp;
1241 cookie.dbg_tick = debug_tick;
1242
1243 cookie.level = ex_nesting_level;
1244#endif
1245
1246 // Keep the sourcing name/lnum, for recursive calls.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001247 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001248 ESTACK_CHECK_SETUP
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001249
1250#ifdef STARTUPTIME
1251 if (time_fd != NULL)
1252 time_push(&tv_rel, &tv_start);
1253#endif
1254
1255#ifdef FEAT_EVAL
1256# ifdef FEAT_PROFILE
1257 if (do_profiling == PROF_YES)
1258 prof_child_enter(&wait_start); // entering a child now
1259# endif
1260
1261 // Don't use local function variables, if called from a function.
1262 // Also starts profiling timer for nested script.
1263 save_funccal(&funccalp_entry);
1264
1265 save_current_sctx = current_sctx;
1266 current_sctx.sc_lnum = 0;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001267 current_sctx.sc_version = 1; // default script version
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001268
1269 // Check if this script was sourced before to finds its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001270 // Always use a new sequence number.
1271 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001272 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001273 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274 hashtab_T *ht;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001275 int is_vim9 = si->sn_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276
1277 // loading the same script again
1278 si->sn_had_command = FALSE;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01001279 si->sn_version = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001280 current_sctx.sc_sid = sid;
1281
Bram Moolenaar2eec3792020-05-25 20:33:55 +02001282 // In Vim9 script all script-local variables are removed when reloading
1283 // the same script. In legacy script they remain but "const" can be
1284 // set again.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285 ht = &SCRIPT_VARS(sid);
Bram Moolenaar89483d42020-05-10 15:24:44 +02001286 if (is_vim9)
1287 hashtab_free_contents(ht);
1288 else
1289 {
1290 int todo = (int)ht->ht_used;
1291 hashitem_T *hi;
1292 dictitem_T *di;
1293
1294 for (hi = ht->ht_array; todo > 0; ++hi)
1295 if (!HASHITEM_EMPTY(hi))
1296 {
1297 --todo;
1298 di = HI2DI(hi);
1299 di->di_flags |= DI_FLAGS_RELOAD;
1300 }
1301 }
Bram Moolenaar750802b2020-02-23 18:08:33 +01001302
1303 // old imports are no longer valid
1304 free_imports(sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001305
1306 // in Vim9 script functions are marked deleted
1307 if (is_vim9)
1308 delete_script_functions(sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001309 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001310 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001311 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312 // It's new, generate a new SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001313 current_sctx.sc_sid = ++last_current_SID;
1314 if (ga_grow(&script_items,
1315 (int)(current_sctx.sc_sid - script_items.ga_len)) == FAIL)
1316 goto almosttheend;
1317 while (script_items.ga_len < current_sctx.sc_sid)
1318 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001319 si = ALLOC_CLEAR_ONE(scriptitem_T);
1320 if (si == NULL)
1321 goto almosttheend;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001322 ++script_items.ga_len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001323 SCRIPT_ITEM(script_items.ga_len) = si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324 si->sn_name = NULL;
1325 si->sn_version = 1;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001326
1327 // Allocate the local script variables to use for this script.
1328 new_script_vars(script_items.ga_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001329 ga_init2(&si->sn_var_vals, sizeof(typval_T), 10);
1330 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
1331 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001332# ifdef FEAT_PROFILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333 si->sn_prof_on = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001334# endif
1335 }
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001336 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001337 si->sn_name = fname_exp;
1338 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 if (ret_sid != NULL)
1340 *ret_sid = current_sctx.sc_sid;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001341 }
1342
1343# ifdef FEAT_PROFILE
1344 if (do_profiling == PROF_YES)
1345 {
1346 int forceit;
1347
1348 // Check if we do profiling for this script.
1349 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
1350 {
1351 script_do_profile(si);
1352 si->sn_pr_force = forceit;
1353 }
1354 if (si->sn_prof_on)
1355 {
1356 ++si->sn_pr_count;
1357 profile_start(&si->sn_pr_start);
1358 profile_zero(&si->sn_pr_children);
1359 }
1360 }
1361# endif
1362#endif
1363
1364 cookie.conv.vc_type = CONV_NONE; // no conversion
1365
1366 // Read the first line so we can check for a UTF-8 BOM.
1367 firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1368 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1369 && firstline[1] == 0xbb && firstline[2] == 0xbf)
1370 {
1371 // Found BOM; setup conversion, skip over BOM and recode the line.
1372 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1373 p = string_convert(&cookie.conv, firstline + 3, NULL);
1374 if (p == NULL)
1375 p = vim_strsave(firstline + 3);
1376 if (p != NULL)
1377 {
1378 vim_free(firstline);
1379 firstline = p;
1380 }
1381 }
1382
1383 // Call do_cmdline, which will call getsourceline() to get the lines.
1384 do_cmdline(firstline, getsourceline, (void *)&cookie,
1385 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1386 retval = OK;
1387
1388#ifdef FEAT_PROFILE
1389 if (do_profiling == PROF_YES)
1390 {
1391 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001392 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001393 if (si->sn_prof_on)
1394 {
1395 profile_end(&si->sn_pr_start);
1396 profile_sub_wait(&wait_start, &si->sn_pr_start);
1397 profile_add(&si->sn_pr_total, &si->sn_pr_start);
1398 profile_self(&si->sn_pr_self, &si->sn_pr_start,
1399 &si->sn_pr_children);
1400 }
1401 }
1402#endif
1403
1404 if (got_int)
1405 emsg(_(e_interr));
Bram Moolenaare31ee862020-01-07 20:59:34 +01001406 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001407 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001408 if (p_verbose > 1)
1409 {
1410 verbose_enter();
1411 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001412 if (SOURCING_NAME != NULL)
1413 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001414 verbose_leave();
1415 }
1416#ifdef STARTUPTIME
1417 if (time_fd != NULL)
1418 {
1419 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1420 time_msg((char *)IObuff, &tv_start);
1421 time_pop(&tv_rel);
1422 }
1423#endif
1424
1425 if (!got_int)
1426 trigger_source_post = TRUE;
1427
1428#ifdef FEAT_EVAL
1429 // After a "finish" in debug mode, need to break at first command of next
1430 // sourced file.
1431 if (save_debug_break_level > ex_nesting_level
1432 && debug_break_level == ex_nesting_level)
1433 ++debug_break_level;
1434#endif
1435
1436#ifdef FEAT_EVAL
1437almosttheend:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438 // 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 Moolenaar8a7d6542020-01-26 15:56:19 +01001440 if (si->sn_save_cpo != NULL)
1441 {
1442 free_string_option(p_cpo);
1443 p_cpo = si->sn_save_cpo;
1444 si->sn_save_cpo = NULL;
1445 }
1446
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001447 current_sctx = save_current_sctx;
1448 restore_funccal();
1449# ifdef FEAT_PROFILE
1450 if (do_profiling == PROF_YES)
1451 prof_child_exit(&wait_start); // leaving a child now
1452# endif
1453#endif
1454 fclose(cookie.fp);
1455 vim_free(cookie.nextline);
1456 vim_free(firstline);
1457 convert_setup(&cookie.conv, NULL, NULL);
1458
1459 if (trigger_source_post)
1460 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1461
1462theend:
1463 vim_free(fname_exp);
1464 return retval;
1465}
1466
1467#if defined(FEAT_EVAL) || defined(PROTO)
1468
1469/*
1470 * ":scriptnames"
1471 */
1472 void
1473ex_scriptnames(exarg_T *eap)
1474{
1475 int i;
1476
1477 if (eap->addr_count > 0)
1478 {
1479 // :script {scriptId}: edit the script
1480 if (eap->line2 < 1 || eap->line2 > script_items.ga_len)
1481 emsg(_(e_invarg));
1482 else
1483 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001484 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001485 do_exedit(eap, NULL);
1486 }
1487 return;
1488 }
1489
1490 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001491 if (SCRIPT_ITEM(i)->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001492 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001493 home_replace(NULL, SCRIPT_ITEM(i)->sn_name,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001494 NameBuff, MAXPATHL, TRUE);
1495 smsg("%3d: %s", i, NameBuff);
1496 }
1497}
1498
1499# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1500/*
1501 * Fix slashes in the list of script names for 'shellslash'.
1502 */
1503 void
1504scriptnames_slash_adjust(void)
1505{
1506 int i;
1507
1508 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001509 if (SCRIPT_ITEM(i)->sn_name != NULL)
1510 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001511}
1512# endif
1513
1514/*
1515 * Get a pointer to a script name. Used for ":verbose set".
1516 */
1517 char_u *
1518get_scriptname(scid_T id)
1519{
1520 if (id == SID_MODELINE)
1521 return (char_u *)_("modeline");
1522 if (id == SID_CMDARG)
1523 return (char_u *)_("--cmd argument");
1524 if (id == SID_CARG)
1525 return (char_u *)_("-c argument");
1526 if (id == SID_ENV)
1527 return (char_u *)_("environment variable");
1528 if (id == SID_ERROR)
1529 return (char_u *)_("error handler");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001530 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001531}
1532
1533# if defined(EXITFREE) || defined(PROTO)
1534 void
1535free_scriptnames(void)
1536{
1537 int i;
1538
1539 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02001540 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001541 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001542
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001543 // the variables themselves are cleared in evalvars_clear()
1544 vim_free(si->sn_vars);
1545
1546 vim_free(si->sn_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01001547 free_imports(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001548 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02001549# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001550 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02001551# endif
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001552 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02001553 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001554 ga_clear(&script_items);
1555}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001556
1557 void
1558free_autoload_scriptnames(void)
1559{
1560 ga_clear_strings(&ga_loaded);
1561}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001562# endif
1563
1564#endif
1565
1566 linenr_T
1567get_sourced_lnum(char_u *(*fgetline)(int, void *, int, int), void *cookie)
1568{
1569 return fgetline == getsourceline
1570 ? ((struct source_cookie *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001571 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001572}
1573
1574 static char_u *
1575get_one_sourceline(struct source_cookie *sp)
1576{
1577 garray_T ga;
1578 int len;
1579 int c;
1580 char_u *buf;
1581#ifdef USE_CRNL
1582 int has_cr; // CR-LF found
1583#endif
1584 int have_read = FALSE;
1585
1586 // use a growarray to store the sourced line
1587 ga_init2(&ga, 1, 250);
1588
1589 // Loop until there is a finished line (or end-of-file).
1590 ++sp->sourcing_lnum;
1591 for (;;)
1592 {
1593 // make room to read at least 120 (more) characters
1594 if (ga_grow(&ga, 120) == FAIL)
1595 break;
1596 buf = (char_u *)ga.ga_data;
1597
1598 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
1599 sp->fp) == NULL)
1600 break;
1601 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
1602#ifdef USE_CRNL
1603 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
1604 // CTRL-Z by its own, or after a NL.
1605 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
1606 && sp->fileformat == EOL_DOS
1607 && buf[len - 1] == Ctrl_Z)
1608 {
1609 buf[len - 1] = NUL;
1610 break;
1611 }
1612#endif
1613
1614 have_read = TRUE;
1615 ga.ga_len = len;
1616
1617 // If the line was longer than the buffer, read more.
1618 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
1619 continue;
1620
1621 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
1622 {
1623#ifdef USE_CRNL
1624 has_cr = (len >= 2 && buf[len - 2] == '\r');
1625 if (sp->fileformat == EOL_UNKNOWN)
1626 {
1627 if (has_cr)
1628 sp->fileformat = EOL_DOS;
1629 else
1630 sp->fileformat = EOL_UNIX;
1631 }
1632
1633 if (sp->fileformat == EOL_DOS)
1634 {
1635 if (has_cr) // replace trailing CR
1636 {
1637 buf[len - 2] = '\n';
1638 --len;
1639 --ga.ga_len;
1640 }
1641 else // lines like ":map xx yy^M" will have failed
1642 {
1643 if (!sp->error)
1644 {
1645 msg_source(HL_ATTR(HLF_W));
1646 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
1647 }
1648 sp->error = TRUE;
1649 sp->fileformat = EOL_UNIX;
1650 }
1651 }
1652#endif
1653 // The '\n' is escaped if there is an odd number of ^V's just
1654 // before it, first set "c" just before the 'V's and then check
1655 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
1656 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
1657 ;
1658 if ((len & 1) != (c & 1)) // escaped NL, read more
1659 {
1660 ++sp->sourcing_lnum;
1661 continue;
1662 }
1663
1664 buf[len - 1] = NUL; // remove the NL
1665 }
1666
1667 // Check for ^C here now and then, so recursive :so can be broken.
1668 line_breakcheck();
1669 break;
1670 }
1671
1672 if (have_read)
1673 return (char_u *)ga.ga_data;
1674
1675 vim_free(ga.ga_data);
1676 return NULL;
1677}
1678
1679/*
1680 * Get one full line from a sourced file.
1681 * Called by do_cmdline() when it's called from do_source().
1682 *
1683 * Return a pointer to the line in allocated memory.
1684 * Return NULL for end-of-file or some error.
1685 */
1686 char_u *
1687getsourceline(int c UNUSED, void *cookie, int indent UNUSED, int do_concat)
1688{
1689 struct source_cookie *sp = (struct source_cookie *)cookie;
1690 char_u *line;
1691 char_u *p;
1692
1693#ifdef FEAT_EVAL
1694 // If breakpoints have been added/deleted need to check for it.
1695 if (sp->dbg_tick < debug_tick)
1696 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001697 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001698 sp->dbg_tick = debug_tick;
1699 }
1700# ifdef FEAT_PROFILE
1701 if (do_profiling == PROF_YES)
1702 script_line_end();
1703# endif
1704#endif
1705
1706 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001707 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001708
1709 // Get current line. If there is a read-ahead line, use it, otherwise get
1710 // one now.
1711 if (sp->finished)
1712 line = NULL;
1713 else if (sp->nextline == NULL)
1714 line = get_one_sourceline(sp);
1715 else
1716 {
1717 line = sp->nextline;
1718 sp->nextline = NULL;
1719 ++sp->sourcing_lnum;
1720 }
1721#ifdef FEAT_PROFILE
1722 if (line != NULL && do_profiling == PROF_YES)
1723 script_line_start();
1724#endif
1725
1726 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
1727 // contain the 'C' flag.
Bram Moolenaar2eec3792020-05-25 20:33:55 +02001728 if (line != NULL && do_concat && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001729 {
1730 // compensate for the one line read-ahead
1731 --sp->sourcing_lnum;
1732
1733 // Get the next line and concatenate it when it starts with a
1734 // backslash. We always need to read the next line, keep it in
1735 // sp->nextline.
1736 /* Also check for a comment in between continuation lines: "\ */
1737 sp->nextline = get_one_sourceline(sp);
1738 if (sp->nextline != NULL
1739 && (*(p = skipwhite(sp->nextline)) == '\\'
1740 || (p[0] == '"' && p[1] == '\\' && p[2] == ' ')))
1741 {
1742 garray_T ga;
1743
1744 ga_init2(&ga, (int)sizeof(char_u), 400);
1745 ga_concat(&ga, line);
1746 if (*p == '\\')
1747 ga_concat(&ga, p + 1);
1748 for (;;)
1749 {
1750 vim_free(sp->nextline);
1751 sp->nextline = get_one_sourceline(sp);
1752 if (sp->nextline == NULL)
1753 break;
1754 p = skipwhite(sp->nextline);
1755 if (*p == '\\')
1756 {
1757 // Adjust the growsize to the current length to speed up
1758 // concatenating many lines.
1759 if (ga.ga_len > 400)
1760 {
1761 if (ga.ga_len > 8000)
1762 ga.ga_growsize = 8000;
1763 else
1764 ga.ga_growsize = ga.ga_len;
1765 }
1766 ga_concat(&ga, p + 1);
1767 }
1768 else if (p[0] != '"' || p[1] != '\\' || p[2] != ' ')
1769 break;
1770 }
1771 ga_append(&ga, NUL);
1772 vim_free(line);
1773 line = ga.ga_data;
1774 }
1775 }
1776
1777 if (line != NULL && sp->conv.vc_type != CONV_NONE)
1778 {
1779 char_u *s;
1780
1781 // Convert the encoding of the script line.
1782 s = string_convert(&sp->conv, line, NULL);
1783 if (s != NULL)
1784 {
1785 vim_free(line);
1786 line = s;
1787 }
1788 }
1789
1790#ifdef FEAT_EVAL
1791 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001792 if (sp->breakpoint != 0 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001793 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001794 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001795 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001796 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001797 sp->dbg_tick = debug_tick;
1798 }
1799#endif
1800
1801 return line;
1802}
1803
1804/*
1805 * ":scriptencoding": Set encoding conversion for a sourced script.
1806 */
1807 void
1808ex_scriptencoding(exarg_T *eap)
1809{
1810 struct source_cookie *sp;
1811 char_u *name;
1812
1813 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
1814 {
1815 emsg(_("E167: :scriptencoding used outside of a sourced file"));
1816 return;
1817 }
1818
1819 if (*eap->arg != NUL)
1820 {
1821 name = enc_canonize(eap->arg);
1822 if (name == NULL) // out of memory
1823 return;
1824 }
1825 else
1826 name = eap->arg;
1827
1828 // Setup for conversion from the specified encoding to 'encoding'.
1829 sp = (struct source_cookie *)getline_cookie(eap->getline, eap->cookie);
1830 convert_setup(&sp->conv, name, p_enc);
1831
1832 if (name != eap->arg)
1833 vim_free(name);
1834}
1835
1836/*
1837 * ":scriptversion": Set Vim script version for a sourced script.
1838 */
1839 void
1840ex_scriptversion(exarg_T *eap UNUSED)
1841{
1842#ifdef FEAT_EVAL
1843 int nr;
1844
1845 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
1846 {
1847 emsg(_("E984: :scriptversion used outside of a sourced file"));
1848 return;
1849 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
1851 {
1852 emsg(_("E1040: Cannot use :scriptversion after :vim9script"));
1853 return;
1854 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001855
1856 nr = getdigits(&eap->arg);
1857 if (nr == 0 || *eap->arg != NUL)
1858 emsg(_(e_invarg));
Bram Moolenaar60a8de22019-09-15 14:33:22 +02001859 else if (nr > 4)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001860 semsg(_("E999: scriptversion not supported: %d"), nr);
1861 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001862 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001863 current_sctx.sc_version = nr;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001864 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001865 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001866#endif
1867}
1868
1869#if defined(FEAT_EVAL) || defined(PROTO)
1870/*
1871 * ":finish": Mark a sourced file as finished.
1872 */
1873 void
1874ex_finish(exarg_T *eap)
1875{
1876 if (getline_equal(eap->getline, eap->cookie, getsourceline))
1877 do_finish(eap, FALSE);
1878 else
1879 emsg(_("E168: :finish used outside of a sourced file"));
1880}
1881
1882/*
1883 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
1884 * Also called for a pending finish at the ":endtry" or after returning from
1885 * an extra do_cmdline(). "reanimate" is used in the latter case.
1886 */
1887 void
1888do_finish(exarg_T *eap, int reanimate)
1889{
1890 int idx;
1891
1892 if (reanimate)
1893 ((struct source_cookie *)getline_cookie(eap->getline,
1894 eap->cookie))->finished = FALSE;
1895
1896 // Cleanup (and inactivate) conditionals, but stop when a try conditional
1897 // not in its finally clause (which then is to be executed next) is found.
1898 // In this case, make the ":finish" pending for execution at the ":endtry".
1899 // Otherwise, finish normally.
1900 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
1901 if (idx >= 0)
1902 {
1903 eap->cstack->cs_pending[idx] = CSTP_FINISH;
1904 report_make_pending(CSTP_FINISH, NULL);
1905 }
1906 else
1907 ((struct source_cookie *)getline_cookie(eap->getline,
1908 eap->cookie))->finished = TRUE;
1909}
1910
1911
1912/*
1913 * Return TRUE when a sourced file had the ":finish" command: Don't give error
1914 * message for missing ":endif".
1915 * Return FALSE when not sourcing a file.
1916 */
1917 int
1918source_finished(
1919 char_u *(*fgetline)(int, void *, int, int),
1920 void *cookie)
1921{
1922 return (getline_equal(fgetline, cookie, getsourceline)
1923 && ((struct source_cookie *)getline_cookie(
1924 fgetline, cookie))->finished);
1925}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001926
1927/*
1928 * Return the autoload script name for a function or variable name.
1929 * Returns NULL when out of memory.
1930 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
1931 */
1932 char_u *
1933autoload_name(char_u *name)
1934{
1935 char_u *p, *q = NULL;
1936 char_u *scriptname;
1937
1938 // Get the script file name: replace '#' with '/', append ".vim".
1939 scriptname = alloc(STRLEN(name) + 14);
1940 if (scriptname == NULL)
1941 return NULL;
1942 STRCPY(scriptname, "autoload/");
1943 STRCAT(scriptname, name);
1944 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
1945 q = p, ++p)
1946 *p = '/';
1947 STRCPY(q, ".vim");
1948 return scriptname;
1949}
1950
1951/*
1952 * If "name" has a package name try autoloading the script for it.
1953 * Return TRUE if a package was loaded.
1954 */
1955 int
1956script_autoload(
1957 char_u *name,
1958 int reload) // load script again when already loaded
1959{
1960 char_u *p;
1961 char_u *scriptname, *tofree;
1962 int ret = FALSE;
1963 int i;
1964
1965 // If there is no '#' after name[0] there is no package name.
1966 p = vim_strchr(name, AUTOLOAD_CHAR);
1967 if (p == NULL || p == name)
1968 return FALSE;
1969
1970 tofree = scriptname = autoload_name(name);
1971 if (scriptname == NULL)
1972 return FALSE;
1973
1974 // Find the name in the list of previously loaded package names. Skip
1975 // "autoload/", it's always the same.
1976 for (i = 0; i < ga_loaded.ga_len; ++i)
1977 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
1978 break;
1979 if (!reload && i < ga_loaded.ga_len)
1980 ret = FALSE; // was loaded already
1981 else
1982 {
1983 // Remember the name if it wasn't loaded already.
1984 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
1985 {
1986 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
1987 tofree = NULL;
1988 }
1989
1990 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
1991 if (source_runtime(scriptname, 0) == OK)
1992 ret = TRUE;
1993 }
1994
1995 vim_free(tofree);
1996 return ret;
1997}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001998#endif