blob: ce4bafadaf95649a3f4f606af85f506bd75f9bc2 [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.
114 */
115 char_u *
116estack_sfile(void)
117{
Bram Moolenaar85b09572019-12-30 10:57:00 +0100118 estack_T *entry;
119#ifdef FEAT_EVAL
Bram Moolenaar4d7a2482020-01-06 19:53:43 +0100120 size_t len;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100121 int idx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100122 char *res;
Bram Moolenaar4d7a2482020-01-06 19:53:43 +0100123 size_t done;
Bram Moolenaar85b09572019-12-30 10:57:00 +0100124#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100125
126 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
127 if (entry->es_name == NULL)
128 return NULL;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100129#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100130 if (entry->es_info.ufunc == NULL)
Bram Moolenaar09d46402019-12-29 23:53:01 +0100131#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100132 return vim_strsave(entry->es_name);
133
Bram Moolenaar09d46402019-12-29 23:53:01 +0100134#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100135 // For a function we compose the call stack, as it was done in the past:
136 // "function One[123]..Two[456]..Three"
137 len = STRLEN(entry->es_name) + 10;
138 for (idx = exestack.ga_len - 2; idx >= 0; --idx)
139 {
140 entry = ((estack_T *)exestack.ga_data) + idx;
141 if (entry->es_name == NULL || entry->es_info.ufunc == NULL)
142 {
143 ++idx;
144 break;
145 }
146 len += STRLEN(entry->es_name) + 15;
147 }
148
Bram Moolenaar4d7a2482020-01-06 19:53:43 +0100149 res = (char *)alloc((int)len);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100150 if (res != NULL)
151 {
152 STRCPY(res, "function ");
153 while (idx < exestack.ga_len - 1)
154 {
155 done = STRLEN(res);
156 entry = ((estack_T *)exestack.ga_data) + idx;
157 vim_snprintf(res + done, len - done, "%s[%ld]..",
158 entry->es_name, entry->es_lnum);
159 ++idx;
160 }
161 done = STRLEN(res);
162 entry = ((estack_T *)exestack.ga_data) + idx;
163 vim_snprintf(res + done, len - done, "%s", entry->es_name);
164 }
165 return (char_u *)res;
Bram Moolenaar09d46402019-12-29 23:53:01 +0100166#endif
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100167}
168
169/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200170 * ":runtime [what] {name}"
171 */
172 void
173ex_runtime(exarg_T *eap)
174{
175 char_u *arg = eap->arg;
176 char_u *p = skiptowhite(arg);
177 int len = (int)(p - arg);
178 int flags = eap->forceit ? DIP_ALL : 0;
179
180 if (STRNCMP(arg, "START", len) == 0)
181 {
182 flags += DIP_START + DIP_NORTP;
183 arg = skipwhite(arg + len);
184 }
185 else if (STRNCMP(arg, "OPT", len) == 0)
186 {
187 flags += DIP_OPT + DIP_NORTP;
188 arg = skipwhite(arg + len);
189 }
190 else if (STRNCMP(arg, "PACK", len) == 0)
191 {
192 flags += DIP_START + DIP_OPT + DIP_NORTP;
193 arg = skipwhite(arg + len);
194 }
195 else if (STRNCMP(arg, "ALL", len) == 0)
196 {
197 flags += DIP_START + DIP_OPT;
198 arg = skipwhite(arg + len);
199 }
200
201 source_runtime(arg, flags);
202}
203
204 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205source_callback(char_u *fname, void *cookie)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200206{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200208}
209
210/*
211 * Find the file "name" in all directories in "path" and invoke
212 * "callback(fname, cookie)".
213 * "name" can contain wildcards.
214 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
215 * When "flags" has DIP_DIR: find directories instead of files.
216 * When "flags" has DIP_ERR: give an error message if there is no match.
217 *
218 * return FAIL when no file could be sourced, OK otherwise.
219 */
220 int
221do_in_path(
222 char_u *path,
223 char_u *name,
224 int flags,
225 void (*callback)(char_u *fname, void *ck),
226 void *cookie)
227{
228 char_u *rtp;
229 char_u *np;
230 char_u *buf;
231 char_u *rtp_copy;
232 char_u *tail;
233 int num_files;
234 char_u **files;
235 int i;
236 int did_one = FALSE;
237#ifdef AMIGA
238 struct Process *proc = (struct Process *)FindTask(0L);
239 APTR save_winptr = proc->pr_WindowPtr;
240
241 // Avoid a requester here for a volume that doesn't exist.
242 proc->pr_WindowPtr = (APTR)-1L;
243#endif
244
245 // Make a copy of 'runtimepath'. Invoking the callback may change the
246 // value.
247 rtp_copy = vim_strsave(path);
248 buf = alloc(MAXPATHL);
249 if (buf != NULL && rtp_copy != NULL)
250 {
Bram Moolenaar647a5302020-05-03 17:01:24 +0200251 if (p_verbose > 10 && name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200252 {
253 verbose_enter();
254 smsg(_("Searching for \"%s\" in \"%s\""),
255 (char *)name, (char *)path);
256 verbose_leave();
257 }
258
259 // Loop over all entries in 'runtimepath'.
260 rtp = rtp_copy;
261 while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
262 {
263 size_t buflen;
264
265 // Copy the path from 'runtimepath' to buf[].
266 copy_option_part(&rtp, buf, MAXPATHL, ",");
267 buflen = STRLEN(buf);
268
269 // Skip after or non-after directories.
270 if (flags & (DIP_NOAFTER | DIP_AFTER))
271 {
272 int is_after = buflen >= 5
273 && STRCMP(buf + buflen - 5, "after") == 0;
274
275 if ((is_after && (flags & DIP_NOAFTER))
276 || (!is_after && (flags & DIP_AFTER)))
277 continue;
278 }
279
280 if (name == NULL)
281 {
282 (*callback)(buf, (void *) &cookie);
283 if (!did_one)
284 did_one = (cookie == NULL);
285 }
286 else if (buflen + STRLEN(name) + 2 < MAXPATHL)
287 {
288 add_pathsep(buf);
289 tail = buf + STRLEN(buf);
290
291 // Loop over all patterns in "name"
292 np = name;
293 while (*np != NUL && ((flags & DIP_ALL) || !did_one))
294 {
295 // Append the pattern from "name" to buf[].
296 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
297 "\t ");
298
Bram Moolenaar647a5302020-05-03 17:01:24 +0200299 if (p_verbose > 10)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200300 {
301 verbose_enter();
302 smsg(_("Searching for \"%s\""), buf);
303 verbose_leave();
304 }
305
306 // Expand wildcards, invoke the callback for each match.
307 if (gen_expand_wildcards(1, &buf, &num_files, &files,
308 (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
309 {
310 for (i = 0; i < num_files; ++i)
311 {
312 (*callback)(files[i], cookie);
313 did_one = TRUE;
314 if (!(flags & DIP_ALL))
315 break;
316 }
317 FreeWild(num_files, files);
318 }
319 }
320 }
321 }
322 }
323 vim_free(buf);
324 vim_free(rtp_copy);
325 if (!did_one && name != NULL)
326 {
327 char *basepath = path == p_rtp ? "runtimepath" : "packpath";
328
329 if (flags & DIP_ERR)
330 semsg(_(e_dirnotf), basepath, name);
331 else if (p_verbose > 0)
332 {
333 verbose_enter();
334 smsg(_("not found in '%s': \"%s\""), basepath, name);
335 verbose_leave();
336 }
337 }
338
339#ifdef AMIGA
340 proc->pr_WindowPtr = save_winptr;
341#endif
342
343 return did_one ? OK : FAIL;
344}
345
346/*
347 * Find "name" in "path". When found, invoke the callback function for
348 * it: callback(fname, "cookie")
349 * When "flags" has DIP_ALL repeat for all matches, otherwise only the first
350 * one is used.
351 * Returns OK when at least one match found, FAIL otherwise.
352 *
353 * If "name" is NULL calls callback for each entry in "path". Cookie is
354 * passed by reference in this case, setting it to NULL indicates that callback
355 * has done its job.
356 */
357 static int
358do_in_path_and_pp(
359 char_u *path,
360 char_u *name,
361 int flags,
362 void (*callback)(char_u *fname, void *ck),
363 void *cookie)
364{
365 int done = FAIL;
366 char_u *s;
367 int len;
368 char *start_dir = "pack/*/start/*/%s";
369 char *opt_dir = "pack/*/opt/*/%s";
370
371 if ((flags & DIP_NORTP) == 0)
372 done = do_in_path(path, name, flags, callback, cookie);
373
374 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
375 {
376 len = (int)(STRLEN(start_dir) + STRLEN(name));
377 s = alloc(len);
378 if (s == NULL)
379 return FAIL;
380 vim_snprintf((char *)s, len, start_dir, name);
381 done = do_in_path(p_pp, s, flags, callback, cookie);
382 vim_free(s);
383 }
384
385 if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
386 {
387 len = (int)(STRLEN(opt_dir) + STRLEN(name));
388 s = alloc(len);
389 if (s == NULL)
390 return FAIL;
391 vim_snprintf((char *)s, len, opt_dir, name);
392 done = do_in_path(p_pp, s, flags, callback, cookie);
393 vim_free(s);
394 }
395
396 return done;
397}
398
399/*
400 * Just like do_in_path_and_pp(), using 'runtimepath' for "path".
401 */
402 int
403do_in_runtimepath(
404 char_u *name,
405 int flags,
406 void (*callback)(char_u *fname, void *ck),
407 void *cookie)
408{
409 return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
410}
411
412/*
413 * Source the file "name" from all directories in 'runtimepath'.
414 * "name" can contain wildcards.
415 * When "flags" has DIP_ALL: source all files, otherwise only the first one.
416 *
417 * return FAIL when no file could be sourced, OK otherwise.
418 */
419 int
420source_runtime(char_u *name, int flags)
421{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100422 return source_in_path(p_rtp, name, flags, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200423}
424
425/*
426 * Just like source_runtime(), but use "path" instead of 'runtimepath'.
427 */
428 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100429source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200430{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100431 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200432}
433
434
435#if defined(FEAT_EVAL) || defined(PROTO)
436
437/*
438 * Expand wildcards in "pat" and invoke do_source() for each match.
439 */
440 static void
441source_all_matches(char_u *pat)
442{
443 int num_files;
444 char_u **files;
445 int i;
446
447 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK)
448 {
449 for (i = 0; i < num_files; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100450 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200451 FreeWild(num_files, files);
452 }
453}
454
455/*
456 * Add the package directory to 'runtimepath'.
457 */
458 static int
459add_pack_dir_to_rtp(char_u *fname)
460{
461 char_u *p4, *p3, *p2, *p1, *p;
462 char_u *entry;
463 char_u *insp = NULL;
464 int c;
465 char_u *new_rtp;
466 int keep;
467 size_t oldlen;
468 size_t addlen;
469 size_t new_rtp_len;
470 char_u *afterdir = NULL;
471 size_t afterlen = 0;
472 char_u *after_insp = NULL;
473 char_u *ffname = NULL;
474 size_t fname_len;
475 char_u *buf = NULL;
476 char_u *rtp_ffname;
477 int match;
478 int retval = FAIL;
479
480 p4 = p3 = p2 = p1 = get_past_head(fname);
481 for (p = p1; *p; MB_PTR_ADV(p))
482 if (vim_ispathsep_nocolon(*p))
483 {
484 p4 = p3; p3 = p2; p2 = p1; p1 = p;
485 }
486
487 // now we have:
488 // rtp/pack/name/start/name
489 // p4 p3 p2 p1
490 //
491 // find the part up to "pack" in 'runtimepath'
492 c = *++p4; // append pathsep in order to expand symlink
493 *p4 = NUL;
494 ffname = fix_fname(fname);
495 *p4 = c;
496 if (ffname == NULL)
497 return FAIL;
498
499 // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
500 // Also stop at the first "after" directory.
501 fname_len = STRLEN(ffname);
502 buf = alloc(MAXPATHL);
503 if (buf == NULL)
504 goto theend;
505 for (entry = p_rtp; *entry != NUL; )
506 {
507 char_u *cur_entry = entry;
508
509 copy_option_part(&entry, buf, MAXPATHL, ",");
510 if (insp == NULL)
511 {
512 add_pathsep(buf);
513 rtp_ffname = fix_fname(buf);
514 if (rtp_ffname == NULL)
515 goto theend;
516 match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
517 vim_free(rtp_ffname);
518 if (match)
519 // Insert "ffname" after this entry (and comma).
520 insp = entry;
521 }
522
523 if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
524 && p > buf
525 && vim_ispathsep(p[-1])
526 && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
527 {
528 if (insp == NULL)
529 // Did not find "ffname" before the first "after" directory,
530 // insert it before this entry.
531 insp = cur_entry;
532 after_insp = cur_entry;
533 break;
534 }
535 }
536
537 if (insp == NULL)
538 // Both "fname" and "after" not found, append at the end.
539 insp = p_rtp + STRLEN(p_rtp);
540
541 // check if rtp/pack/name/start/name/after exists
542 afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
543 if (afterdir != NULL && mch_isdir(afterdir))
544 afterlen = STRLEN(afterdir) + 1; // add one for comma
545
546 oldlen = STRLEN(p_rtp);
547 addlen = STRLEN(fname) + 1; // add one for comma
548 new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
549 if (new_rtp == NULL)
550 goto theend;
551
552 // We now have 'rtp' parts: {keep}{keep_after}{rest}.
553 // Create new_rtp, first: {keep},{fname}
554 keep = (int)(insp - p_rtp);
555 mch_memmove(new_rtp, p_rtp, keep);
556 new_rtp_len = keep;
557 if (*insp == NUL)
558 new_rtp[new_rtp_len++] = ','; // add comma before
559 mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
560 new_rtp_len += addlen - 1;
561 if (*insp != NUL)
562 new_rtp[new_rtp_len++] = ','; // add comma after
563
564 if (afterlen > 0 && after_insp != NULL)
565 {
566 int keep_after = (int)(after_insp - p_rtp);
567
568 // Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
569 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
570 keep_after - keep);
571 new_rtp_len += keep_after - keep;
572 mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
573 new_rtp_len += afterlen - 1;
574 new_rtp[new_rtp_len++] = ',';
575 keep = keep_after;
576 }
577
578 if (p_rtp[keep] != NUL)
579 // Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
580 mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
581 else
582 new_rtp[new_rtp_len] = NUL;
583
584 if (afterlen > 0 && after_insp == NULL)
585 {
586 // Append afterdir when "after" was not found:
587 // {keep},{fname}{rest},{afterdir}
588 STRCAT(new_rtp, ",");
589 STRCAT(new_rtp, afterdir);
590 }
591
592 set_option_value((char_u *)"rtp", 0L, new_rtp, 0);
593 vim_free(new_rtp);
594 retval = OK;
595
596theend:
597 vim_free(buf);
598 vim_free(ffname);
599 vim_free(afterdir);
600 return retval;
601}
602
603/*
604 * Load scripts in "plugin" and "ftdetect" directories of the package.
605 */
606 static int
607load_pack_plugin(char_u *fname)
608{
609 static char *plugpat = "%s/plugin/**/*.vim";
610 static char *ftpat = "%s/ftdetect/*.vim";
611 int len;
612 char_u *ffname = fix_fname(fname);
613 char_u *pat = NULL;
614 int retval = FAIL;
615
616 if (ffname == NULL)
617 return FAIL;
618 len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
619 pat = alloc(len);
620 if (pat == NULL)
621 goto theend;
622 vim_snprintf((char *)pat, len, plugpat, ffname);
623 source_all_matches(pat);
624
625 {
626 char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
627
628 // If runtime/filetype.vim wasn't loaded yet, the scripts will be
629 // found when it loads.
630 if (cmd != NULL && eval_to_number(cmd) > 0)
631 {
632 do_cmdline_cmd((char_u *)"augroup filetypedetect");
633 vim_snprintf((char *)pat, len, ftpat, ffname);
634 source_all_matches(pat);
635 do_cmdline_cmd((char_u *)"augroup END");
636 }
637 vim_free(cmd);
638 }
639 vim_free(pat);
640 retval = OK;
641
642theend:
643 vim_free(ffname);
644 return retval;
645}
646
647// used for "cookie" of add_pack_plugin()
648static int APP_ADD_DIR;
649static int APP_LOAD;
650static int APP_BOTH;
651
652 static void
653add_pack_plugin(char_u *fname, void *cookie)
654{
655 if (cookie != &APP_LOAD)
656 {
657 char_u *buf = alloc(MAXPATHL);
658 char_u *p;
659 int found = FALSE;
660
661 if (buf == NULL)
662 return;
663 p = p_rtp;
664 while (*p != NUL)
665 {
666 copy_option_part(&p, buf, MAXPATHL, ",");
667 if (pathcmp((char *)buf, (char *)fname, -1) == 0)
668 {
669 found = TRUE;
670 break;
671 }
672 }
673 vim_free(buf);
674 if (!found)
675 // directory is not yet in 'runtimepath', add it
676 if (add_pack_dir_to_rtp(fname) == FAIL)
677 return;
678 }
679
680 if (cookie != &APP_ADD_DIR)
681 load_pack_plugin(fname);
682}
683
684/*
685 * Add all packages in the "start" directory to 'runtimepath'.
686 */
687 void
688add_pack_start_dirs(void)
689{
690 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
691 add_pack_plugin, &APP_ADD_DIR);
692}
693
694/*
695 * Load plugins from all packages in the "start" directory.
696 */
697 void
698load_start_packages(void)
699{
700 did_source_packages = TRUE;
701 do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
702 add_pack_plugin, &APP_LOAD);
703}
704
705/*
706 * ":packloadall"
707 * Find plugins in the package directories and source them.
708 */
709 void
710ex_packloadall(exarg_T *eap)
711{
712 if (!did_source_packages || eap->forceit)
713 {
714 // First do a round to add all directories to 'runtimepath', then load
715 // the plugins. This allows for plugins to use an autoload directory
716 // of another plugin.
717 add_pack_start_dirs();
718 load_start_packages();
719 }
720}
721
722/*
723 * ":packadd[!] {name}"
724 */
725 void
726ex_packadd(exarg_T *eap)
727{
728 static char *plugpat = "pack/*/%s/%s";
729 int len;
730 char *pat;
731 int round;
732 int res = OK;
733
734 // Round 1: use "start", round 2: use "opt".
735 for (round = 1; round <= 2; ++round)
736 {
737 // Only look under "start" when loading packages wasn't done yet.
738 if (round == 1 && did_source_packages)
739 continue;
740
741 len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
742 pat = alloc(len);
743 if (pat == NULL)
744 return;
745 vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
746 // The first round don't give a "not found" error, in the second round
747 // only when nothing was found in the first round.
748 res = do_in_path(p_pp, (char_u *)pat,
749 DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
750 add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
751 vim_free(pat);
752 }
753}
754#endif
755
756/*
Bram Moolenaar26262f82019-09-04 20:59:15 +0200757 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
758 * list of file names in allocated memory.
759 */
760 void
761remove_duplicates(garray_T *gap)
762{
763 int i;
764 int j;
765 char_u **fnames = (char_u **)gap->ga_data;
766
767 sort_strings(fnames, gap->ga_len);
768 for (i = gap->ga_len - 1; i > 0; --i)
769 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
770 {
771 vim_free(fnames[i]);
772 for (j = i + 1; j < gap->ga_len; ++j)
773 fnames[j - 1] = fnames[j];
774 --gap->ga_len;
775 }
776}
777
778/*
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200779 * Expand color scheme, compiler or filetype names.
780 * Search from 'runtimepath':
781 * 'runtimepath'/{dirnames}/{pat}.vim
782 * When "flags" has DIP_START: search also from 'start' of 'packpath':
783 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
784 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
785 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
786 * "dirnames" is an array with one or more directory names.
787 */
788 int
789ExpandRTDir(
790 char_u *pat,
791 int flags,
792 int *num_file,
793 char_u ***file,
794 char *dirnames[])
795{
796 char_u *s;
797 char_u *e;
798 char_u *match;
799 garray_T ga;
800 int i;
801 int pat_len;
802
803 *num_file = 0;
804 *file = NULL;
805 pat_len = (int)STRLEN(pat);
806 ga_init2(&ga, (int)sizeof(char *), 10);
807
808 for (i = 0; dirnames[i] != NULL; ++i)
809 {
810 s = alloc(STRLEN(dirnames[i]) + pat_len + 7);
811 if (s == NULL)
812 {
813 ga_clear_strings(&ga);
814 return FAIL;
815 }
816 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
817 globpath(p_rtp, s, &ga, 0);
818 vim_free(s);
819 }
820
821 if (flags & DIP_START) {
822 for (i = 0; dirnames[i] != NULL; ++i)
823 {
824 s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
825 if (s == NULL)
826 {
827 ga_clear_strings(&ga);
828 return FAIL;
829 }
830 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
831 globpath(p_pp, s, &ga, 0);
832 vim_free(s);
833 }
834 }
835
836 if (flags & DIP_OPT) {
837 for (i = 0; dirnames[i] != NULL; ++i)
838 {
839 s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
840 if (s == NULL)
841 {
842 ga_clear_strings(&ga);
843 return FAIL;
844 }
845 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
846 globpath(p_pp, s, &ga, 0);
847 vim_free(s);
848 }
849 }
850
851 for (i = 0; i < ga.ga_len; ++i)
852 {
853 match = ((char_u **)ga.ga_data)[i];
854 s = match;
855 e = s + STRLEN(s);
856 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
857 {
858 e -= 4;
859 for (s = e; s > match; MB_PTR_BACK(match, s))
860 if (s < match || vim_ispathsep(*s))
861 break;
862 ++s;
863 *e = NUL;
864 mch_memmove(match, s, e - s + 1);
865 }
866 }
867
868 if (ga.ga_len == 0)
869 return FAIL;
870
871 // Sort and remove duplicates which can happen when specifying multiple
872 // directories in dirnames.
873 remove_duplicates(&ga);
874
875 *file = ga.ga_data;
876 *num_file = ga.ga_len;
877 return OK;
878}
879
880/*
881 * Expand loadplugin names:
882 * 'packpath'/pack/ * /opt/{pat}
883 */
884 int
885ExpandPackAddDir(
886 char_u *pat,
887 int *num_file,
888 char_u ***file)
889{
890 char_u *s;
891 char_u *e;
892 char_u *match;
893 garray_T ga;
894 int i;
895 int pat_len;
896
897 *num_file = 0;
898 *file = NULL;
899 pat_len = (int)STRLEN(pat);
900 ga_init2(&ga, (int)sizeof(char *), 10);
901
902 s = alloc(pat_len + 26);
903 if (s == NULL)
904 {
905 ga_clear_strings(&ga);
906 return FAIL;
907 }
908 sprintf((char *)s, "pack/*/opt/%s*", pat);
909 globpath(p_pp, s, &ga, 0);
910 vim_free(s);
911
912 for (i = 0; i < ga.ga_len; ++i)
913 {
914 match = ((char_u **)ga.ga_data)[i];
915 s = gettail(match);
916 e = s + STRLEN(s);
917 mch_memmove(match, s, e - s + 1);
918 }
919
920 if (ga.ga_len == 0)
921 return FAIL;
922
923 // Sort and remove duplicates which can happen when specifying multiple
924 // directories in dirnames.
925 remove_duplicates(&ga);
926
927 *file = ga.ga_data;
928 *num_file = ga.ga_len;
929 return OK;
930}
931
932 static void
933cmd_source(char_u *fname, exarg_T *eap)
934{
935 if (*fname == NUL)
936 emsg(_(e_argreq));
937
938 else if (eap != NULL && eap->forceit)
939 // ":source!": read Normal mode commands
940 // Need to execute the commands directly. This is required at least
941 // for:
942 // - ":g" command busy
943 // - after ":argdo", ":windo" or ":bufdo"
944 // - another command follows
945 // - inside a loop
946 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
947#ifdef FEAT_EVAL
948 || eap->cstack->cs_idx >= 0
949#endif
950 );
951
952 // ":source" read ex commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100953 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200954 semsg(_(e_notopen), fname);
955}
956
957/*
958 * ":source {fname}"
959 */
960 void
961ex_source(exarg_T *eap)
962{
963#ifdef FEAT_BROWSE
964 if (cmdmod.browse)
965 {
966 char_u *fname = NULL;
967
968 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
969 NULL, NULL,
970 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
971 if (fname != NULL)
972 {
973 cmd_source(fname, eap);
974 vim_free(fname);
975 }
976 }
977 else
978#endif
979 cmd_source(eap->arg, eap);
980}
981
982#if defined(FEAT_EVAL) || defined(PROTO)
983/*
984 * ":options"
985 */
986 void
987ex_options(
988 exarg_T *eap UNUSED)
989{
Bram Moolenaar7a1637f2020-04-13 21:16:21 +0200990 char_u buf[500];
991 int multi_mods = 0;
992
993 buf[0] = NUL;
994 (void)add_win_cmd_modifers(buf, &multi_mods);
995
996 vim_setenv((char_u *)"OPTWIN_CMD", buf);
Bram Moolenaar307c5a52019-08-25 15:41:00 +0200997 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
998}
999#endif
1000
1001/*
1002 * ":source" and associated commands.
1003 */
1004/*
1005 * Structure used to store info for each sourced file.
1006 * It is shared between do_source() and getsourceline().
1007 * This is required, because it needs to be handed to do_cmdline() and
1008 * sourcing can be done recursively.
1009 */
1010struct source_cookie
1011{
1012 FILE *fp; // opened file for sourcing
1013 char_u *nextline; // if not NULL: line that was read ahead
1014 linenr_T sourcing_lnum; // line number of the source file
1015 int finished; // ":finish" used
1016#ifdef USE_CRNL
1017 int fileformat; // EOL_UNKNOWN, EOL_UNIX or EOL_DOS
1018 int error; // TRUE if LF found after CR-LF
1019#endif
1020#ifdef FEAT_EVAL
1021 linenr_T breakpoint; // next line with breakpoint or zero
1022 char_u *fname; // name of sourced file
1023 int dbg_tick; // debug_tick when breakpoint was set
1024 int level; // top nesting level of sourced file
1025#endif
1026 vimconv_T conv; // type of conversion
1027};
1028
1029#ifdef FEAT_EVAL
1030/*
1031 * Return the address holding the next breakpoint line for a source cookie.
1032 */
1033 linenr_T *
1034source_breakpoint(void *cookie)
1035{
1036 return &((struct source_cookie *)cookie)->breakpoint;
1037}
1038
1039/*
1040 * Return the address holding the debug tick for a source cookie.
1041 */
1042 int *
1043source_dbg_tick(void *cookie)
1044{
1045 return &((struct source_cookie *)cookie)->dbg_tick;
1046}
1047
1048/*
1049 * Return the nesting level for a source cookie.
1050 */
1051 int
1052source_level(void *cookie)
1053{
1054 return ((struct source_cookie *)cookie)->level;
1055}
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001056
1057/*
1058 * Return the readahead line.
1059 */
1060 char_u *
1061source_nextline(void *cookie)
1062{
1063 return ((struct source_cookie *)cookie)->nextline;
1064}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001065#endif
1066
1067#if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC)
1068# define USE_FOPEN_NOINH
1069/*
1070 * Special function to open a file without handle inheritance.
1071 * When possible the handle is closed on exec().
1072 */
1073 static FILE *
1074fopen_noinh_readbin(char *filename)
1075{
1076# ifdef MSWIN
1077 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
1078# else
1079 int fd_tmp = mch_open(filename, O_RDONLY, 0);
1080# endif
1081
1082 if (fd_tmp == -1)
1083 return NULL;
1084
1085# ifdef HAVE_FD_CLOEXEC
1086 {
1087 int fdflags = fcntl(fd_tmp, F_GETFD);
1088 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1089 (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
1090 }
1091# endif
1092
1093 return fdopen(fd_tmp, READBIN);
1094}
1095#endif
1096
1097/*
1098 * do_source: Read the file "fname" and execute its lines as EX commands.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001099 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1100 * again.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001101 *
1102 * This function may be called recursively!
1103 *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001104 * Return FAIL if file could not be opened, OK otherwise.
1105 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001106 */
1107 int
1108do_source(
1109 char_u *fname,
1110 int check_other, // check for .vimrc and _vimrc
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001111 int is_vimrc, // DOSO_ value
1112 int *ret_sid UNUSED)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001113{
1114 struct source_cookie cookie;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001115 char_u *p;
1116 char_u *fname_exp;
1117 char_u *firstline = NULL;
1118 int retval = FAIL;
1119#ifdef FEAT_EVAL
1120 sctx_T save_current_sctx;
1121 static scid_T last_current_SID = 0;
1122 static int last_current_SID_seq = 0;
1123 funccal_entry_T funccalp_entry;
1124 int save_debug_break_level = debug_break_level;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 int sid;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001126 scriptitem_T *si = NULL;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001127#endif
1128#ifdef STARTUPTIME
1129 struct timeval tv_rel;
1130 struct timeval tv_start;
1131#endif
1132#ifdef FEAT_PROFILE
1133 proftime_T wait_start;
1134#endif
1135 int trigger_source_post = FALSE;
Bram Moolenaare31ee862020-01-07 20:59:34 +01001136 ESTACK_CHECK_DECLARATION
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001137
1138 p = expand_env_save(fname);
1139 if (p == NULL)
1140 return retval;
1141 fname_exp = fix_fname(p);
1142 vim_free(p);
1143 if (fname_exp == NULL)
1144 return retval;
1145 if (mch_isdir(fname_exp))
1146 {
1147 smsg(_("Cannot source a directory: \"%s\""), fname);
1148 goto theend;
1149 }
1150
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001151#ifdef FEAT_EVAL
1152 // See if we loaded this script before.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001153 for (sid = script_items.ga_len; sid > 0; --sid)
1154 {
Bram Moolenaar978d1702020-01-26 17:38:12 +01001155 // We used to check inode here, but that doesn't work:
1156 // - If a script is edited and written, it may get a different
1157 // inode number, even though to the user it is the same script.
1158 // - If a script is deleted and another script is written, with a
1159 // different name, the inode may be re-used.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001160 si = SCRIPT_ITEM(sid);
Bram Moolenaar978d1702020-01-26 17:38:12 +01001161 if (si->sn_name != NULL && fnamecmp(si->sn_name, fname_exp) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 // Found it!
1163 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001164 }
1165 if (sid > 0 && ret_sid != NULL)
1166 {
1167 // Already loaded and no need to load again, return here.
1168 *ret_sid = sid;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001169 retval = OK;
1170 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171 }
1172#endif
1173
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001174 // Apply SourceCmd autocommands, they should get the file and source it.
1175 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1176 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1177 FALSE, curbuf))
1178 {
1179#ifdef FEAT_EVAL
1180 retval = aborting() ? FAIL : OK;
1181#else
1182 retval = OK;
1183#endif
1184 if (retval == OK)
1185 // Apply SourcePost autocommands.
1186 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1187 FALSE, curbuf);
1188 goto theend;
1189 }
1190
1191 // Apply SourcePre autocommands, they may get the file.
1192 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1193
1194#ifdef USE_FOPEN_NOINH
1195 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1196#else
1197 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1198#endif
1199 if (cookie.fp == NULL && check_other)
1200 {
1201 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1202 // and ".exrc" by "_exrc" or vice versa.
1203 p = gettail(fname_exp);
1204 if ((*p == '.' || *p == '_')
1205 && (STRICMP(p + 1, "vimrc") == 0
1206 || STRICMP(p + 1, "gvimrc") == 0
1207 || STRICMP(p + 1, "exrc") == 0))
1208 {
1209 if (*p == '_')
1210 *p = '.';
1211 else
1212 *p = '_';
1213#ifdef USE_FOPEN_NOINH
1214 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1215#else
1216 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1217#endif
1218 }
1219 }
1220
1221 if (cookie.fp == NULL)
1222 {
1223 if (p_verbose > 0)
1224 {
1225 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001226 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001227 smsg(_("could not source \"%s\""), fname);
1228 else
1229 smsg(_("line %ld: could not source \"%s\""),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001230 SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001231 verbose_leave();
1232 }
1233 goto theend;
1234 }
1235
1236 // The file exists.
1237 // - In verbose mode, give a message.
1238 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1239 if (p_verbose > 1)
1240 {
1241 verbose_enter();
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001242 if (SOURCING_NAME == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001243 smsg(_("sourcing \"%s\""), fname);
1244 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001245 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001246 verbose_leave();
1247 }
1248 if (is_vimrc == DOSO_VIMRC)
1249 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1250 else if (is_vimrc == DOSO_GVIMRC)
1251 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1252
1253#ifdef USE_CRNL
1254 // If no automatic file format: Set default to CR-NL.
1255 if (*p_ffs == NUL)
1256 cookie.fileformat = EOL_DOS;
1257 else
1258 cookie.fileformat = EOL_UNKNOWN;
1259 cookie.error = FALSE;
1260#endif
1261
1262 cookie.nextline = NULL;
1263 cookie.sourcing_lnum = 0;
1264 cookie.finished = FALSE;
1265
1266#ifdef FEAT_EVAL
1267 // Check if this script has a breakpoint.
1268 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1269 cookie.fname = fname_exp;
1270 cookie.dbg_tick = debug_tick;
1271
1272 cookie.level = ex_nesting_level;
1273#endif
1274
1275 // Keep the sourcing name/lnum, for recursive calls.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001276 estack_push(ETYPE_SCRIPT, fname_exp, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001277 ESTACK_CHECK_SETUP
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001278
1279#ifdef STARTUPTIME
1280 if (time_fd != NULL)
1281 time_push(&tv_rel, &tv_start);
1282#endif
1283
1284#ifdef FEAT_EVAL
1285# ifdef FEAT_PROFILE
1286 if (do_profiling == PROF_YES)
1287 prof_child_enter(&wait_start); // entering a child now
1288# endif
1289
1290 // Don't use local function variables, if called from a function.
1291 // Also starts profiling timer for nested script.
1292 save_funccal(&funccalp_entry);
1293
1294 save_current_sctx = current_sctx;
1295 current_sctx.sc_lnum = 0;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001296 current_sctx.sc_version = 1; // default script version
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001297
1298 // Check if this script was sourced before to finds its SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001299 // Always use a new sequence number.
1300 current_sctx.sc_seq = ++last_current_SID_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001301 if (sid > 0)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001302 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 hashtab_T *ht;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001304 int is_vim9 = si->sn_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305
1306 // loading the same script again
1307 si->sn_had_command = FALSE;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01001308 si->sn_version = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 current_sctx.sc_sid = sid;
1310
Bram Moolenaar2eec3792020-05-25 20:33:55 +02001311 // In Vim9 script all script-local variables are removed when reloading
1312 // the same script. In legacy script they remain but "const" can be
1313 // set again.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001314 ht = &SCRIPT_VARS(sid);
Bram Moolenaar89483d42020-05-10 15:24:44 +02001315 if (is_vim9)
1316 hashtab_free_contents(ht);
1317 else
1318 {
1319 int todo = (int)ht->ht_used;
1320 hashitem_T *hi;
1321 dictitem_T *di;
1322
1323 for (hi = ht->ht_array; todo > 0; ++hi)
1324 if (!HASHITEM_EMPTY(hi))
1325 {
1326 --todo;
1327 di = HI2DI(hi);
1328 di->di_flags |= DI_FLAGS_RELOAD;
1329 }
1330 }
Bram Moolenaar750802b2020-02-23 18:08:33 +01001331
1332 // old imports are no longer valid
1333 free_imports(sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001334
1335 // in Vim9 script functions are marked deleted
1336 if (is_vim9)
1337 delete_script_functions(sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001338 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 else
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001340 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 // It's new, generate a new SID.
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001342 current_sctx.sc_sid = ++last_current_SID;
1343 if (ga_grow(&script_items,
1344 (int)(current_sctx.sc_sid - script_items.ga_len)) == FAIL)
1345 goto almosttheend;
1346 while (script_items.ga_len < current_sctx.sc_sid)
1347 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001348 si = ALLOC_CLEAR_ONE(scriptitem_T);
1349 if (si == NULL)
1350 goto almosttheend;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001351 ++script_items.ga_len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001352 SCRIPT_ITEM(script_items.ga_len) = si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001353 si->sn_name = NULL;
1354 si->sn_version = 1;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001355
1356 // Allocate the local script variables to use for this script.
1357 new_script_vars(script_items.ga_len);
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +02001358 ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
1360 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001361# ifdef FEAT_PROFILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 si->sn_prof_on = FALSE;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001363# endif
1364 }
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001365 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001366 si->sn_name = fname_exp;
1367 fname_exp = vim_strsave(si->sn_name); // used for autocmd
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368 if (ret_sid != NULL)
1369 *ret_sid = current_sctx.sc_sid;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001370 }
1371
1372# ifdef FEAT_PROFILE
1373 if (do_profiling == PROF_YES)
1374 {
1375 int forceit;
1376
1377 // Check if we do profiling for this script.
1378 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
1379 {
1380 script_do_profile(si);
1381 si->sn_pr_force = forceit;
1382 }
1383 if (si->sn_prof_on)
1384 {
1385 ++si->sn_pr_count;
1386 profile_start(&si->sn_pr_start);
1387 profile_zero(&si->sn_pr_children);
1388 }
1389 }
1390# endif
1391#endif
1392
1393 cookie.conv.vc_type = CONV_NONE; // no conversion
1394
1395 // Read the first line so we can check for a UTF-8 BOM.
1396 firstline = getsourceline(0, (void *)&cookie, 0, TRUE);
1397 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
1398 && firstline[1] == 0xbb && firstline[2] == 0xbf)
1399 {
1400 // Found BOM; setup conversion, skip over BOM and recode the line.
1401 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
1402 p = string_convert(&cookie.conv, firstline + 3, NULL);
1403 if (p == NULL)
1404 p = vim_strsave(firstline + 3);
1405 if (p != NULL)
1406 {
1407 vim_free(firstline);
1408 firstline = p;
1409 }
1410 }
1411
1412 // Call do_cmdline, which will call getsourceline() to get the lines.
1413 do_cmdline(firstline, getsourceline, (void *)&cookie,
1414 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
1415 retval = OK;
1416
1417#ifdef FEAT_PROFILE
1418 if (do_profiling == PROF_YES)
1419 {
1420 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001421 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001422 if (si->sn_prof_on)
1423 {
1424 profile_end(&si->sn_pr_start);
1425 profile_sub_wait(&wait_start, &si->sn_pr_start);
1426 profile_add(&si->sn_pr_total, &si->sn_pr_start);
1427 profile_self(&si->sn_pr_self, &si->sn_pr_start,
1428 &si->sn_pr_children);
1429 }
1430 }
1431#endif
1432
1433 if (got_int)
1434 emsg(_(e_interr));
Bram Moolenaare31ee862020-01-07 20:59:34 +01001435 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001436 estack_pop();
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001437 if (p_verbose > 1)
1438 {
1439 verbose_enter();
1440 smsg(_("finished sourcing %s"), fname);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001441 if (SOURCING_NAME != NULL)
1442 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001443 verbose_leave();
1444 }
1445#ifdef STARTUPTIME
1446 if (time_fd != NULL)
1447 {
1448 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname);
1449 time_msg((char *)IObuff, &tv_start);
1450 time_pop(&tv_rel);
1451 }
1452#endif
1453
1454 if (!got_int)
1455 trigger_source_post = TRUE;
1456
1457#ifdef FEAT_EVAL
1458 // After a "finish" in debug mode, need to break at first command of next
1459 // sourced file.
1460 if (save_debug_break_level > ex_nesting_level
1461 && debug_break_level == ex_nesting_level)
1462 ++debug_break_level;
1463#endif
1464
1465#ifdef FEAT_EVAL
1466almosttheend:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467 // Get "si" again, "script_items" may have been reallocated.
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001468 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 if (si->sn_save_cpo != NULL)
1470 {
1471 free_string_option(p_cpo);
1472 p_cpo = si->sn_save_cpo;
1473 si->sn_save_cpo = NULL;
1474 }
1475
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001476 current_sctx = save_current_sctx;
1477 restore_funccal();
1478# ifdef FEAT_PROFILE
1479 if (do_profiling == PROF_YES)
1480 prof_child_exit(&wait_start); // leaving a child now
1481# endif
1482#endif
1483 fclose(cookie.fp);
1484 vim_free(cookie.nextline);
1485 vim_free(firstline);
1486 convert_setup(&cookie.conv, NULL, NULL);
1487
1488 if (trigger_source_post)
1489 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf);
1490
1491theend:
1492 vim_free(fname_exp);
1493 return retval;
1494}
1495
1496#if defined(FEAT_EVAL) || defined(PROTO)
1497
1498/*
1499 * ":scriptnames"
1500 */
1501 void
1502ex_scriptnames(exarg_T *eap)
1503{
1504 int i;
1505
1506 if (eap->addr_count > 0)
1507 {
1508 // :script {scriptId}: edit the script
1509 if (eap->line2 < 1 || eap->line2 > script_items.ga_len)
1510 emsg(_(e_invarg));
1511 else
1512 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001513 eap->arg = SCRIPT_ITEM(eap->line2)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001514 do_exedit(eap, NULL);
1515 }
1516 return;
1517 }
1518
1519 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001520 if (SCRIPT_ITEM(i)->sn_name != NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001521 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001522 home_replace(NULL, SCRIPT_ITEM(i)->sn_name,
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001523 NameBuff, MAXPATHL, TRUE);
1524 smsg("%3d: %s", i, NameBuff);
1525 }
1526}
1527
1528# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1529/*
1530 * Fix slashes in the list of script names for 'shellslash'.
1531 */
1532 void
1533scriptnames_slash_adjust(void)
1534{
1535 int i;
1536
1537 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001538 if (SCRIPT_ITEM(i)->sn_name != NULL)
1539 slash_adjust(SCRIPT_ITEM(i)->sn_name);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001540}
1541# endif
1542
1543/*
1544 * Get a pointer to a script name. Used for ":verbose set".
1545 */
1546 char_u *
1547get_scriptname(scid_T id)
1548{
1549 if (id == SID_MODELINE)
1550 return (char_u *)_("modeline");
1551 if (id == SID_CMDARG)
1552 return (char_u *)_("--cmd argument");
1553 if (id == SID_CARG)
1554 return (char_u *)_("-c argument");
1555 if (id == SID_ENV)
1556 return (char_u *)_("environment variable");
1557 if (id == SID_ERROR)
1558 return (char_u *)_("error handler");
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001559 return SCRIPT_ITEM(id)->sn_name;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001560}
1561
1562# if defined(EXITFREE) || defined(PROTO)
1563 void
1564free_scriptnames(void)
1565{
1566 int i;
1567
1568 for (i = script_items.ga_len; i > 0; --i)
Bram Moolenaar86173482019-10-01 17:02:16 +02001569 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001570 scriptitem_T *si = SCRIPT_ITEM(i);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001572 // the variables themselves are cleared in evalvars_clear()
1573 vim_free(si->sn_vars);
1574
1575 vim_free(si->sn_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01001576 free_imports(i);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001577 free_string_option(si->sn_save_cpo);
Bram Moolenaara720be72019-10-22 21:45:19 +02001578# ifdef FEAT_PROFILE
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001579 ga_clear(&si->sn_prl_ga);
Bram Moolenaara720be72019-10-22 21:45:19 +02001580# endif
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001581 vim_free(si);
Bram Moolenaar86173482019-10-01 17:02:16 +02001582 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001583 ga_clear(&script_items);
1584}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001585
1586 void
1587free_autoload_scriptnames(void)
1588{
1589 ga_clear_strings(&ga_loaded);
1590}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001591# endif
1592
1593#endif
1594
1595 linenr_T
1596get_sourced_lnum(char_u *(*fgetline)(int, void *, int, int), void *cookie)
1597{
1598 return fgetline == getsourceline
1599 ? ((struct source_cookie *)cookie)->sourcing_lnum
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001600 : SOURCING_LNUM;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001601}
1602
1603 static char_u *
1604get_one_sourceline(struct source_cookie *sp)
1605{
1606 garray_T ga;
1607 int len;
1608 int c;
1609 char_u *buf;
1610#ifdef USE_CRNL
1611 int has_cr; // CR-LF found
1612#endif
1613 int have_read = FALSE;
1614
1615 // use a growarray to store the sourced line
1616 ga_init2(&ga, 1, 250);
1617
1618 // Loop until there is a finished line (or end-of-file).
1619 ++sp->sourcing_lnum;
1620 for (;;)
1621 {
1622 // make room to read at least 120 (more) characters
1623 if (ga_grow(&ga, 120) == FAIL)
1624 break;
1625 buf = (char_u *)ga.ga_data;
1626
1627 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
1628 sp->fp) == NULL)
1629 break;
1630 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
1631#ifdef USE_CRNL
1632 // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
1633 // CTRL-Z by its own, or after a NL.
1634 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
1635 && sp->fileformat == EOL_DOS
1636 && buf[len - 1] == Ctrl_Z)
1637 {
1638 buf[len - 1] = NUL;
1639 break;
1640 }
1641#endif
1642
1643 have_read = TRUE;
1644 ga.ga_len = len;
1645
1646 // If the line was longer than the buffer, read more.
1647 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
1648 continue;
1649
1650 if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL
1651 {
1652#ifdef USE_CRNL
1653 has_cr = (len >= 2 && buf[len - 2] == '\r');
1654 if (sp->fileformat == EOL_UNKNOWN)
1655 {
1656 if (has_cr)
1657 sp->fileformat = EOL_DOS;
1658 else
1659 sp->fileformat = EOL_UNIX;
1660 }
1661
1662 if (sp->fileformat == EOL_DOS)
1663 {
1664 if (has_cr) // replace trailing CR
1665 {
1666 buf[len - 2] = '\n';
1667 --len;
1668 --ga.ga_len;
1669 }
1670 else // lines like ":map xx yy^M" will have failed
1671 {
1672 if (!sp->error)
1673 {
1674 msg_source(HL_ATTR(HLF_W));
1675 emsg(_("W15: Warning: Wrong line separator, ^M may be missing"));
1676 }
1677 sp->error = TRUE;
1678 sp->fileformat = EOL_UNIX;
1679 }
1680 }
1681#endif
1682 // The '\n' is escaped if there is an odd number of ^V's just
1683 // before it, first set "c" just before the 'V's and then check
1684 // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo
1685 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
1686 ;
1687 if ((len & 1) != (c & 1)) // escaped NL, read more
1688 {
1689 ++sp->sourcing_lnum;
1690 continue;
1691 }
1692
1693 buf[len - 1] = NUL; // remove the NL
1694 }
1695
1696 // Check for ^C here now and then, so recursive :so can be broken.
1697 line_breakcheck();
1698 break;
1699 }
1700
1701 if (have_read)
1702 return (char_u *)ga.ga_data;
1703
1704 vim_free(ga.ga_data);
1705 return NULL;
1706}
1707
1708/*
1709 * Get one full line from a sourced file.
1710 * Called by do_cmdline() when it's called from do_source().
1711 *
1712 * Return a pointer to the line in allocated memory.
1713 * Return NULL for end-of-file or some error.
1714 */
1715 char_u *
1716getsourceline(int c UNUSED, void *cookie, int indent UNUSED, int do_concat)
1717{
1718 struct source_cookie *sp = (struct source_cookie *)cookie;
1719 char_u *line;
1720 char_u *p;
1721
1722#ifdef FEAT_EVAL
1723 // If breakpoints have been added/deleted need to check for it.
1724 if (sp->dbg_tick < debug_tick)
1725 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001726 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001727 sp->dbg_tick = debug_tick;
1728 }
1729# ifdef FEAT_PROFILE
1730 if (do_profiling == PROF_YES)
1731 script_line_end();
1732# endif
1733#endif
1734
1735 // Set the current sourcing line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001736 SOURCING_LNUM = sp->sourcing_lnum + 1;
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001737
1738 // Get current line. If there is a read-ahead line, use it, otherwise get
1739 // one now.
1740 if (sp->finished)
1741 line = NULL;
1742 else if (sp->nextline == NULL)
1743 line = get_one_sourceline(sp);
1744 else
1745 {
1746 line = sp->nextline;
1747 sp->nextline = NULL;
1748 ++sp->sourcing_lnum;
1749 }
1750#ifdef FEAT_PROFILE
1751 if (line != NULL && do_profiling == PROF_YES)
1752 script_line_start();
1753#endif
1754
1755 // Only concatenate lines starting with a \ when 'cpoptions' doesn't
1756 // contain the 'C' flag.
Bram Moolenaar2eec3792020-05-25 20:33:55 +02001757 if (line != NULL && do_concat && vim_strchr(p_cpo, CPO_CONCAT) == NULL)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001758 {
1759 // compensate for the one line read-ahead
1760 --sp->sourcing_lnum;
1761
1762 // Get the next line and concatenate it when it starts with a
1763 // backslash. We always need to read the next line, keep it in
1764 // sp->nextline.
1765 /* Also check for a comment in between continuation lines: "\ */
1766 sp->nextline = get_one_sourceline(sp);
1767 if (sp->nextline != NULL
1768 && (*(p = skipwhite(sp->nextline)) == '\\'
1769 || (p[0] == '"' && p[1] == '\\' && p[2] == ' ')))
1770 {
1771 garray_T ga;
1772
1773 ga_init2(&ga, (int)sizeof(char_u), 400);
1774 ga_concat(&ga, line);
1775 if (*p == '\\')
1776 ga_concat(&ga, p + 1);
1777 for (;;)
1778 {
1779 vim_free(sp->nextline);
1780 sp->nextline = get_one_sourceline(sp);
1781 if (sp->nextline == NULL)
1782 break;
1783 p = skipwhite(sp->nextline);
1784 if (*p == '\\')
1785 {
1786 // Adjust the growsize to the current length to speed up
1787 // concatenating many lines.
1788 if (ga.ga_len > 400)
1789 {
1790 if (ga.ga_len > 8000)
1791 ga.ga_growsize = 8000;
1792 else
1793 ga.ga_growsize = ga.ga_len;
1794 }
1795 ga_concat(&ga, p + 1);
1796 }
1797 else if (p[0] != '"' || p[1] != '\\' || p[2] != ' ')
1798 break;
1799 }
1800 ga_append(&ga, NUL);
1801 vim_free(line);
1802 line = ga.ga_data;
1803 }
1804 }
1805
1806 if (line != NULL && sp->conv.vc_type != CONV_NONE)
1807 {
1808 char_u *s;
1809
1810 // Convert the encoding of the script line.
1811 s = string_convert(&sp->conv, line, NULL);
1812 if (s != NULL)
1813 {
1814 vim_free(line);
1815 line = s;
1816 }
1817 }
1818
1819#ifdef FEAT_EVAL
1820 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001821 if (sp->breakpoint != 0 && sp->breakpoint <= SOURCING_LNUM)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001822 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001823 dbg_breakpoint(sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001824 // Find next breakpoint.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001825 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM);
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001826 sp->dbg_tick = debug_tick;
1827 }
1828#endif
1829
1830 return line;
1831}
1832
1833/*
1834 * ":scriptencoding": Set encoding conversion for a sourced script.
1835 */
1836 void
1837ex_scriptencoding(exarg_T *eap)
1838{
1839 struct source_cookie *sp;
1840 char_u *name;
1841
1842 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
1843 {
1844 emsg(_("E167: :scriptencoding used outside of a sourced file"));
1845 return;
1846 }
1847
1848 if (*eap->arg != NUL)
1849 {
1850 name = enc_canonize(eap->arg);
1851 if (name == NULL) // out of memory
1852 return;
1853 }
1854 else
1855 name = eap->arg;
1856
1857 // Setup for conversion from the specified encoding to 'encoding'.
1858 sp = (struct source_cookie *)getline_cookie(eap->getline, eap->cookie);
1859 convert_setup(&sp->conv, name, p_enc);
1860
1861 if (name != eap->arg)
1862 vim_free(name);
1863}
1864
1865/*
1866 * ":scriptversion": Set Vim script version for a sourced script.
1867 */
1868 void
1869ex_scriptversion(exarg_T *eap UNUSED)
1870{
1871#ifdef FEAT_EVAL
1872 int nr;
1873
1874 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
1875 {
1876 emsg(_("E984: :scriptversion used outside of a sourced file"));
1877 return;
1878 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001879 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
1880 {
1881 emsg(_("E1040: Cannot use :scriptversion after :vim9script"));
1882 return;
1883 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001884
1885 nr = getdigits(&eap->arg);
1886 if (nr == 0 || *eap->arg != NUL)
1887 emsg(_(e_invarg));
Bram Moolenaar67979662020-06-20 22:50:47 +02001888 else if (nr > SCRIPT_VERSION_MAX)
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001889 semsg(_("E999: scriptversion not supported: %d"), nr);
1890 else
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001891 {
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001892 current_sctx.sc_version = nr;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001893 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr;
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01001894 }
Bram Moolenaar307c5a52019-08-25 15:41:00 +02001895#endif
1896}
1897
1898#if defined(FEAT_EVAL) || defined(PROTO)
1899/*
1900 * ":finish": Mark a sourced file as finished.
1901 */
1902 void
1903ex_finish(exarg_T *eap)
1904{
1905 if (getline_equal(eap->getline, eap->cookie, getsourceline))
1906 do_finish(eap, FALSE);
1907 else
1908 emsg(_("E168: :finish used outside of a sourced file"));
1909}
1910
1911/*
1912 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
1913 * Also called for a pending finish at the ":endtry" or after returning from
1914 * an extra do_cmdline(). "reanimate" is used in the latter case.
1915 */
1916 void
1917do_finish(exarg_T *eap, int reanimate)
1918{
1919 int idx;
1920
1921 if (reanimate)
1922 ((struct source_cookie *)getline_cookie(eap->getline,
1923 eap->cookie))->finished = FALSE;
1924
1925 // Cleanup (and inactivate) conditionals, but stop when a try conditional
1926 // not in its finally clause (which then is to be executed next) is found.
1927 // In this case, make the ":finish" pending for execution at the ":endtry".
1928 // Otherwise, finish normally.
1929 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
1930 if (idx >= 0)
1931 {
1932 eap->cstack->cs_pending[idx] = CSTP_FINISH;
1933 report_make_pending(CSTP_FINISH, NULL);
1934 }
1935 else
1936 ((struct source_cookie *)getline_cookie(eap->getline,
1937 eap->cookie))->finished = TRUE;
1938}
1939
1940
1941/*
1942 * Return TRUE when a sourced file had the ":finish" command: Don't give error
1943 * message for missing ":endif".
1944 * Return FALSE when not sourcing a file.
1945 */
1946 int
1947source_finished(
1948 char_u *(*fgetline)(int, void *, int, int),
1949 void *cookie)
1950{
1951 return (getline_equal(fgetline, cookie, getsourceline)
1952 && ((struct source_cookie *)getline_cookie(
1953 fgetline, cookie))->finished);
1954}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001955
1956/*
1957 * Return the autoload script name for a function or variable name.
1958 * Returns NULL when out of memory.
1959 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
1960 */
1961 char_u *
1962autoload_name(char_u *name)
1963{
1964 char_u *p, *q = NULL;
1965 char_u *scriptname;
1966
1967 // Get the script file name: replace '#' with '/', append ".vim".
1968 scriptname = alloc(STRLEN(name) + 14);
1969 if (scriptname == NULL)
1970 return NULL;
1971 STRCPY(scriptname, "autoload/");
1972 STRCAT(scriptname, name);
1973 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
1974 q = p, ++p)
1975 *p = '/';
1976 STRCPY(q, ".vim");
1977 return scriptname;
1978}
1979
1980/*
1981 * If "name" has a package name try autoloading the script for it.
1982 * Return TRUE if a package was loaded.
1983 */
1984 int
1985script_autoload(
1986 char_u *name,
1987 int reload) // load script again when already loaded
1988{
1989 char_u *p;
1990 char_u *scriptname, *tofree;
1991 int ret = FALSE;
1992 int i;
1993
1994 // If there is no '#' after name[0] there is no package name.
1995 p = vim_strchr(name, AUTOLOAD_CHAR);
1996 if (p == NULL || p == name)
1997 return FALSE;
1998
1999 tofree = scriptname = autoload_name(name);
2000 if (scriptname == NULL)
2001 return FALSE;
2002
2003 // Find the name in the list of previously loaded package names. Skip
2004 // "autoload/", it's always the same.
2005 for (i = 0; i < ga_loaded.ga_len; ++i)
2006 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
2007 break;
2008 if (!reload && i < ga_loaded.ga_len)
2009 ret = FALSE; // was loaded already
2010 else
2011 {
2012 // Remember the name if it wasn't loaded already.
2013 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
2014 {
2015 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
2016 tofree = NULL;
2017 }
2018
2019 // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
2020 if (source_runtime(scriptname, 0) == OK)
2021 ret = TRUE;
2022 }
2023
2024 vim_free(tofree);
2025 return ret;
2026}
Bram Moolenaar307c5a52019-08-25 15:41:00 +02002027#endif