-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscriptfile.c
2778 lines (2471 loc) · 69 KB
/
scriptfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* scriptfile.c: functions for dealing with the runtime directories/files
*/
#include "vim.h"
#if defined(FEAT_EVAL) || defined(PROTO)
// The names of packages that once were loaded are remembered.
static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
#endif
// last used sequence number for sourcing scripts (current_sctx.sc_seq)
#ifdef FEAT_EVAL
static int last_current_SID_seq = 0;
#endif
static int do_source_ext(char_u *fname, int check_other, int is_vimrc, int *ret_sid, exarg_T *eap, int clearvars);
/*
* Initialize the execution stack.
*/
void
estack_init(void)
{
estack_T *entry;
if (ga_grow(&exestack, 10) == FAIL)
mch_exit(0);
entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
entry->es_type = ETYPE_TOP;
entry->es_name = NULL;
entry->es_lnum = 0;
#ifdef FEAT_EVAL
entry->es_info.ufunc = NULL;
#endif
++exestack.ga_len;
}
/*
* Add an item to the execution stack.
* Returns the new entry or NULL when out of memory.
*/
estack_T *
estack_push(etype_T type, char_u *name, long lnum)
{
estack_T *entry;
// If memory allocation fails then we'll pop more than we push, eventually
// at the top level it will be OK again.
if (ga_grow(&exestack, 1) == FAIL)
return NULL;
entry = ((estack_T *)exestack.ga_data) + exestack.ga_len;
entry->es_type = type;
entry->es_name = name;
entry->es_lnum = lnum;
#ifdef FEAT_EVAL
entry->es_info.ufunc = NULL;
#endif
++exestack.ga_len;
return entry;
}
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* Add a user function to the execution stack.
*/
estack_T *
estack_push_ufunc(ufunc_T *ufunc, long lnum)
{
estack_T *entry = estack_push(ETYPE_UFUNC,
ufunc->uf_name_exp != NULL
? ufunc->uf_name_exp : ufunc->uf_name, lnum);
if (entry != NULL)
entry->es_info.ufunc = ufunc;
return entry;
}
/*
* Return TRUE if "ufunc" with "lnum" is already at the top of the exe stack.
*/
int
estack_top_is_ufunc(ufunc_T *ufunc, long lnum)
{
estack_T *entry;
if (exestack.ga_len == 0)
return FALSE;
entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
return entry->es_type == ETYPE_UFUNC
&& STRCMP( entry->es_name, ufunc->uf_name_exp != NULL
? ufunc->uf_name_exp : ufunc->uf_name) == 0
&& entry->es_lnum == lnum;
}
#endif
/*
* Take an item off of the execution stack and return it.
*/
estack_T *
estack_pop(void)
{
if (exestack.ga_len == 0)
return NULL;
--exestack.ga_len;
return ((estack_T *)exestack.ga_data) + exestack.ga_len;
}
/*
* Get the current value for "which" in allocated memory.
* "which" is ESTACK_SFILE for <sfile>, ESTACK_STACK for <stack> or
* ESTACK_SCRIPT for <script>.
*/
char_u *
estack_sfile(estack_arg_T which UNUSED)
{
estack_T *entry;
#ifdef FEAT_EVAL
garray_T ga;
size_t len;
int idx;
etype_T last_type = ETYPE_SCRIPT;
char *type_name;
#endif
entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
#ifdef FEAT_EVAL
if (which == ESTACK_SFILE && entry->es_type != ETYPE_UFUNC)
#endif
{
if (entry->es_name == NULL)
return NULL;
return vim_strsave(entry->es_name);
}
#ifdef FEAT_EVAL
// expand('<sfile>') works in a function for backwards compatibility, but
// may give an unexpected result. Disallow it in Vim 9 script.
if (which == ESTACK_SFILE && in_vim9script())
{
int save_emsg_off = emsg_off;
if (emsg_off == 1)
// f_expand() silences errors but we do want this one
emsg_off = 0;
emsg(_(e_cannot_expand_sfile_in_vim9_function));
emsg_off = save_emsg_off;
return NULL;
}
// If evaluated in a function or autocommand, return the path of the script
// where it is defined, at script level the current script path is returned
// instead.
if (which == ESTACK_SCRIPT)
{
// Walk the stack backwards, starting from the current frame.
for (idx = exestack.ga_len - 1; idx >= 0; --idx, --entry)
{
if (entry->es_type == ETYPE_UFUNC || entry->es_type == ETYPE_AUCMD)
{
sctx_T *def_ctx = entry->es_type == ETYPE_UFUNC
? &entry->es_info.ufunc->uf_script_ctx
: acp_script_ctx(entry->es_info.aucmd);
return def_ctx->sc_sid > 0
? vim_strsave(SCRIPT_ITEM(def_ctx->sc_sid)->sn_name)
: NULL;
}
else if (entry->es_type == ETYPE_SCRIPT)
return vim_strsave(entry->es_name);
}
return NULL;
}
// Give information about each stack entry up to the root.
// For a function we compose the call stack, as it was done in the past:
// "function One[123]..Two[456]..Three"
ga_init2(&ga, sizeof(char), 100);
for (idx = 0; idx < exestack.ga_len; ++idx)
{
entry = ((estack_T *)exestack.ga_data) + idx;
if (entry->es_name != NULL)
{
long lnum = 0;
char *dots;
len = STRLEN(entry->es_name) + 15;
type_name = "";
if (entry->es_type != last_type)
{
switch (entry->es_type)
{
case ETYPE_SCRIPT: type_name = "script "; break;
case ETYPE_UFUNC: type_name = "function "; break;
default: type_name = ""; break;
}
last_type = entry->es_type;
}
len += STRLEN(type_name);
if (ga_grow(&ga, (int)len) == FAIL)
break;
if (idx == exestack.ga_len - 1)
lnum = which == ESTACK_STACK ? SOURCING_LNUM : 0;
else
lnum = entry->es_lnum;
dots = idx == exestack.ga_len - 1 ? "" : "..";
if (lnum == 0)
// For the bottom entry of <sfile>: do not add the line number,
// it is used in <slnum>. Also leave it out when the number is
// not set.
vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s%s",
type_name, entry->es_name, dots);
else
vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s[%ld]%s",
type_name, entry->es_name, lnum, dots);
ga.ga_len += (int)STRLEN((char *)ga.ga_data + ga.ga_len);
}
}
return (char_u *)ga.ga_data;
#endif
}
/*
* Get DIP_ flags from the [where] argument of a :runtime command.
* "*argp" is advanced to after the [where] argument if it is found.
*/
static int
get_runtime_cmd_flags(char_u **argp, size_t where_len)
{
char_u *arg = *argp;
if (where_len == 0)
return 0;
if (STRNCMP(arg, "START", where_len) == 0)
{
*argp = skipwhite(arg + where_len);
return DIP_START + DIP_NORTP;
}
if (STRNCMP(arg, "OPT", where_len) == 0)
{
*argp = skipwhite(arg + where_len);
return DIP_OPT + DIP_NORTP;
}
if (STRNCMP(arg, "PACK", where_len) == 0)
{
*argp = skipwhite(arg + where_len);
return DIP_START + DIP_OPT + DIP_NORTP;
}
if (STRNCMP(arg, "ALL", where_len) == 0)
{
*argp = skipwhite(arg + where_len);
return DIP_START + DIP_OPT;
}
return 0;
}
/*
* ":runtime [where] {name}"
*/
void
ex_runtime(exarg_T *eap)
{
char_u *arg = eap->arg;
int flags = eap->forceit ? DIP_ALL : 0;
char_u *p = skiptowhite(arg);
flags += get_runtime_cmd_flags(&arg, p - arg);
source_runtime(arg, flags);
}
static int runtime_expand_flags;
/*
* Set the completion context for the :runtime command.
*/
void
set_context_in_runtime_cmd(expand_T *xp, char_u *arg)
{
char_u *p = skiptowhite(arg);
runtime_expand_flags
= *p != NUL ? get_runtime_cmd_flags(&arg, p - arg) : 0;
xp->xp_context = EXPAND_RUNTIME;
xp->xp_pattern = arg;
}
static void
source_callback(char_u *fname, void *cookie)
{
(void)do_source(fname, FALSE, DOSO_NONE, cookie);
}
#ifdef FEAT_EVAL
/*
* Find an already loaded script "name".
* If found returns its script ID. If not found returns -1.
*/
int
find_script_by_name(char_u *name)
{
int sid;
scriptitem_T *si;
for (sid = script_items.ga_len; sid > 0; --sid)
{
// We used to check inode here, but that doesn't work:
// - If a script is edited and written, it may get a different
// inode number, even though to the user it is the same script.
// - If a script is deleted and another script is written, with a
// different name, the inode may be re-used.
si = SCRIPT_ITEM(sid);
if (si->sn_name != NULL && fnamecmp(si->sn_name, name) == 0)
return sid;
}
return -1;
}
/*
* Add a new scriptitem with all items initialized.
* When running out of memory "error" is set to FAIL.
* Returns the script ID.
*/
static int
get_new_scriptitem(int *error)
{
static scid_T last_current_SID = 0;
int sid = ++last_current_SID;
scriptitem_T *si = NULL;
if (ga_grow(&script_items, (int)(sid - script_items.ga_len)) == FAIL)
{
*error = FAIL;
return sid;
}
while (script_items.ga_len < sid)
{
si = ALLOC_CLEAR_ONE(scriptitem_T);
if (si == NULL)
{
*error = FAIL;
return sid;
}
++script_items.ga_len;
SCRIPT_ITEM(script_items.ga_len) = si;
si->sn_name = NULL;
si->sn_version = 1;
// Allocate the local script variables to use for this script.
new_script_vars(script_items.ga_len);
ga_init2(&si->sn_var_vals, sizeof(svar_T), 10);
hash_init(&si->sn_all_vars.dv_hashtab);
ga_init2(&si->sn_imports, sizeof(imported_T), 10);
ga_init2(&si->sn_type_list, sizeof(type_T), 10);
# ifdef FEAT_PROFILE
si->sn_prof_on = FALSE;
# endif
}
// "si" can't be NULL, check only to avoid a compiler warning
if (si != NULL)
// Used to check script variable index is still valid.
si->sn_script_seq = current_sctx.sc_seq;
return sid;
}
int
get_new_scriptitem_for_fname(int *error, char_u *fname)
{
int sid = get_new_scriptitem(error);
if (*error == OK)
{
scriptitem_T *si = SCRIPT_ITEM(sid);
si->sn_name = vim_strsave(fname);
si->sn_state = SN_STATE_NOT_LOADED;
}
return sid;
}
static void
find_script_callback(char_u *fname, void *cookie)
{
int sid;
int error = OK;
int *ret_sid = cookie;
sid = find_script_by_name(fname);
if (sid < 0)
// script does not exist yet, create a new scriptitem
sid = get_new_scriptitem_for_fname(&error, fname);
*ret_sid = sid;
}
#endif
/*
* Find the file "name" in all directories in "path" and invoke
* "callback(fname, cookie)".
* "name" can contain wildcards.
* When "flags" has DIP_ALL: source all files, otherwise only the first one.
* When "flags" has DIP_DIR: find directories instead of files.
* When "flags" has DIP_ERR: give an error message if there is no match.
*
* return FAIL when no file could be sourced, OK otherwise.
*/
int
do_in_path(
char_u *path,
char_u *name,
int flags,
void (*callback)(char_u *fname, void *ck),
void *cookie)
{
char_u *rtp;
char_u *np;
char_u *buf;
char_u *rtp_copy;
char_u *tail;
int num_files;
char_u **files;
int i;
int did_one = FALSE;
#ifdef AMIGA
struct Process *proc = (struct Process *)FindTask(0L);
APTR save_winptr = proc->pr_WindowPtr;
// Avoid a requester here for a volume that doesn't exist.
proc->pr_WindowPtr = (APTR)-1L;
#endif
// Make a copy of 'runtimepath'. Invoking the callback may change the
// value.
rtp_copy = vim_strsave(path);
buf = alloc(MAXPATHL);
if (buf != NULL && rtp_copy != NULL)
{
if (p_verbose > 10 && name != NULL)
{
verbose_enter();
smsg(_("Searching for \"%s\" in \"%s\""),
(char *)name, (char *)path);
verbose_leave();
}
// Loop over all entries in 'runtimepath'.
rtp = rtp_copy;
while (*rtp != NUL && ((flags & DIP_ALL) || !did_one))
{
size_t buflen;
// Copy the path from 'runtimepath' to buf[].
copy_option_part(&rtp, buf, MAXPATHL, ",");
buflen = STRLEN(buf);
// Skip after or non-after directories.
if (flags & (DIP_NOAFTER | DIP_AFTER))
{
int is_after = buflen >= 5
&& STRCMP(buf + buflen - 5, "after") == 0;
if ((is_after && (flags & DIP_NOAFTER))
|| (!is_after && (flags & DIP_AFTER)))
continue;
}
if (name == NULL)
{
(*callback)(buf, (void *) &cookie);
if (!did_one)
did_one = (cookie == NULL);
}
else if (buflen + STRLEN(name) + 2 < MAXPATHL)
{
add_pathsep(buf);
tail = buf + STRLEN(buf);
// Loop over all patterns in "name"
np = name;
while (*np != NUL && ((flags & DIP_ALL) || !did_one))
{
// Append the pattern from "name" to buf[].
copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
"\t ");
if (p_verbose > 10)
{
verbose_enter();
smsg(_("Searching for \"%s\""), buf);
verbose_leave();
}
// Expand wildcards, invoke the callback for each match.
if (gen_expand_wildcards(1, &buf, &num_files, &files,
(flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK)
{
for (i = 0; i < num_files; ++i)
{
(*callback)(files[i], cookie);
did_one = TRUE;
if (!(flags & DIP_ALL))
break;
}
FreeWild(num_files, files);
}
}
}
}
}
vim_free(buf);
vim_free(rtp_copy);
if (!did_one && name != NULL)
{
char *basepath = path == p_rtp ? "runtimepath" : "packpath";
if (flags & DIP_ERR)
semsg(_(e_directory_not_found_in_str_str), basepath, name);
else if (p_verbose > 0)
{
verbose_enter();
smsg(_("not found in '%s': \"%s\""), basepath, name);
verbose_leave();
}
}
#ifdef AMIGA
proc->pr_WindowPtr = save_winptr;
#endif
return did_one ? OK : FAIL;
}
/*
* Find "name" in "path". When found, invoke the callback function for
* it: callback(fname, "cookie")
* When "flags" has DIP_ALL repeat for all matches, otherwise only the first
* one is used.
* Returns OK when at least one match found, FAIL otherwise.
*
* If "name" is NULL calls callback for each entry in "path". Cookie is
* passed by reference in this case, setting it to NULL indicates that callback
* has done its job.
*/
static int
do_in_path_and_pp(
char_u *path,
char_u *name,
int flags,
void (*callback)(char_u *fname, void *ck),
void *cookie)
{
int done = FAIL;
char_u *s;
int len;
char *start_dir = "pack/*/start/*/%s";
char *opt_dir = "pack/*/opt/*/%s";
if ((flags & DIP_NORTP) == 0)
done = do_in_path(path, name, flags, callback, cookie);
if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START))
{
len = (int)(STRLEN(start_dir) + STRLEN(name));
s = alloc(len);
if (s == NULL)
return FAIL;
vim_snprintf((char *)s, len, start_dir, name);
done = do_in_path(p_pp, s, flags, callback, cookie);
vim_free(s);
}
if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT))
{
len = (int)(STRLEN(opt_dir) + STRLEN(name));
s = alloc(len);
if (s == NULL)
return FAIL;
vim_snprintf((char *)s, len, opt_dir, name);
done = do_in_path(p_pp, s, flags, callback, cookie);
vim_free(s);
}
return done;
}
/*
* Just like do_in_path_and_pp(), using 'runtimepath' for "path".
*/
int
do_in_runtimepath(
char_u *name,
int flags,
void (*callback)(char_u *fname, void *ck),
void *cookie)
{
return do_in_path_and_pp(p_rtp, name, flags, callback, cookie);
}
/*
* Source the file "name" from all directories in 'runtimepath'.
* "name" can contain wildcards.
* When "flags" has DIP_ALL: source all files, otherwise only the first one.
*
* return FAIL when no file could be sourced, OK otherwise.
*/
int
source_runtime(char_u *name, int flags)
{
return source_in_path(p_rtp, name, flags, NULL);
}
/*
* Just like source_runtime(), but use "path" instead of 'runtimepath'
* and return the script ID in "ret_sid".
*/
int
source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
{
return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
}
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* Find "name" in 'runtimepath'. If found a new scriptitem is created for it
* and it's script ID is returned.
* If not found returns -1.
*/
int
find_script_in_rtp(char_u *name)
{
int sid = -1;
(void)do_in_path_and_pp(p_rtp, name, DIP_NOAFTER,
find_script_callback, &sid);
return sid;
}
/*
* Expand wildcards in "pat" and invoke do_source() for each match.
*/
static void
source_all_matches(char_u *pat)
{
int num_files;
char_u **files;
int i;
if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) != OK)
return;
for (i = 0; i < num_files; ++i)
(void)do_source(files[i], FALSE, DOSO_NONE, NULL);
FreeWild(num_files, files);
}
/*
* Add the package directory to 'runtimepath'.
*/
static int
add_pack_dir_to_rtp(char_u *fname)
{
char_u *p4, *p3, *p2, *p1, *p;
char_u *entry;
char_u *insp = NULL;
int c;
char_u *new_rtp;
int keep;
size_t oldlen;
size_t addlen;
size_t new_rtp_len;
char_u *afterdir = NULL;
size_t afterlen = 0;
char_u *after_insp = NULL;
char_u *ffname = NULL;
size_t fname_len;
char_u *buf = NULL;
char_u *rtp_ffname;
int match;
int retval = FAIL;
p4 = p3 = p2 = p1 = get_past_head(fname);
for (p = p1; *p; MB_PTR_ADV(p))
if (vim_ispathsep_nocolon(*p))
{
p4 = p3; p3 = p2; p2 = p1; p1 = p;
}
// now we have:
// rtp/pack/name/start/name
// p4 p3 p2 p1
//
// find the part up to "pack" in 'runtimepath'
c = *++p4; // append pathsep in order to expand symlink
*p4 = NUL;
ffname = fix_fname(fname);
*p4 = c;
if (ffname == NULL)
return FAIL;
// Find "ffname" in "p_rtp", ignoring '/' vs '\' differences.
// Also stop at the first "after" directory.
fname_len = STRLEN(ffname);
buf = alloc(MAXPATHL);
if (buf == NULL)
goto theend;
for (entry = p_rtp; *entry != NUL; )
{
char_u *cur_entry = entry;
copy_option_part(&entry, buf, MAXPATHL, ",");
if ((p = (char_u *)strstr((char *)buf, "after")) != NULL
&& p > buf
&& vim_ispathsep(p[-1])
&& (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ','))
{
if (insp == NULL)
// Did not find "ffname" before the first "after" directory,
// insert it before this entry.
insp = cur_entry;
after_insp = cur_entry;
break;
}
if (insp == NULL)
{
add_pathsep(buf);
rtp_ffname = fix_fname(buf);
if (rtp_ffname == NULL)
goto theend;
match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0;
vim_free(rtp_ffname);
if (match)
// Insert "ffname" after this entry (and comma).
insp = entry;
}
}
if (insp == NULL)
// Both "fname" and "after" not found, append at the end.
insp = p_rtp + STRLEN(p_rtp);
// check if rtp/pack/name/start/name/after exists
afterdir = concat_fnames(fname, (char_u *)"after", TRUE);
if (afterdir != NULL && mch_isdir(afterdir))
afterlen = STRLEN(afterdir) + 1; // add one for comma
oldlen = STRLEN(p_rtp);
addlen = STRLEN(fname) + 1; // add one for comma
new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL
if (new_rtp == NULL)
goto theend;
// We now have 'rtp' parts: {keep}{keep_after}{rest}.
// Create new_rtp, first: {keep},{fname}
keep = (int)(insp - p_rtp);
mch_memmove(new_rtp, p_rtp, keep);
new_rtp_len = keep;
if (*insp == NUL)
new_rtp[new_rtp_len++] = ','; // add comma before
mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1);
new_rtp_len += addlen - 1;
if (*insp != NUL)
new_rtp[new_rtp_len++] = ','; // add comma after
if (afterlen > 0 && after_insp != NULL)
{
int keep_after = (int)(after_insp - p_rtp);
// Add to new_rtp: {keep},{fname}{keep_after},{afterdir}
mch_memmove(new_rtp + new_rtp_len, p_rtp + keep,
keep_after - keep);
new_rtp_len += keep_after - keep;
mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1);
new_rtp_len += afterlen - 1;
new_rtp[new_rtp_len++] = ',';
keep = keep_after;
}
if (p_rtp[keep] != NUL)
// Append rest: {keep},{fname}{keep_after},{afterdir}{rest}
mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1);
else
new_rtp[new_rtp_len] = NUL;
if (afterlen > 0 && after_insp == NULL)
{
// Append afterdir when "after" was not found:
// {keep},{fname}{rest},{afterdir}
STRCAT(new_rtp, ",");
STRCAT(new_rtp, afterdir);
}
set_option_value_give_err((char_u *)"rtp", 0L, new_rtp, 0);
vim_free(new_rtp);
retval = OK;
theend:
vim_free(buf);
vim_free(ffname);
vim_free(afterdir);
return retval;
}
/*
* Load scripts in "plugin" and "ftdetect" directories of the package.
*/
static int
load_pack_plugin(char_u *fname)
{
static char *plugpat = "%s/plugin/**/*.vim";
static char *ftpat = "%s/ftdetect/*.vim";
int len;
char_u *ffname = fix_fname(fname);
char_u *pat = NULL;
int retval = FAIL;
if (ffname == NULL)
return FAIL;
len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
pat = alloc(len);
if (pat == NULL)
goto theend;
vim_snprintf((char *)pat, len, plugpat, ffname);
source_all_matches(pat);
{
char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
// If runtime/filetype.vim wasn't loaded yet, the scripts will be
// found when it loads.
if (cmd != NULL && eval_to_number(cmd, FALSE) > 0)
{
do_cmdline_cmd((char_u *)"augroup filetypedetect");
vim_snprintf((char *)pat, len, ftpat, ffname);
source_all_matches(pat);
do_cmdline_cmd((char_u *)"augroup END");
}
vim_free(cmd);
}
vim_free(pat);
retval = OK;
theend:
vim_free(ffname);
return retval;
}
// used for "cookie" of add_pack_plugin()
static int APP_ADD_DIR;
static int APP_LOAD;
static int APP_BOTH;
static void
add_pack_plugin(char_u *fname, void *cookie)
{
if (cookie != &APP_LOAD)
{
char_u *buf = alloc(MAXPATHL);
char_u *p;
int found = FALSE;
if (buf == NULL)
return;
p = p_rtp;
while (*p != NUL)
{
copy_option_part(&p, buf, MAXPATHL, ",");
if (pathcmp((char *)buf, (char *)fname, -1) == 0)
{
found = TRUE;
break;
}
}
vim_free(buf);
if (!found)
// directory is not yet in 'runtimepath', add it
if (add_pack_dir_to_rtp(fname) == FAIL)
return;
}
if (cookie != &APP_ADD_DIR)
load_pack_plugin(fname);
}
/*
* Add all packages in the "start" directory to 'runtimepath'.
*/
void
add_pack_start_dirs(void)
{
do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
add_pack_plugin, &APP_ADD_DIR);
}
/*
* Load plugins from all packages in the "start" directory.
*/
void
load_start_packages(void)
{
did_source_packages = TRUE;
do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR,
add_pack_plugin, &APP_LOAD);
}
/*
* ":packloadall"
* Find plugins in the package directories and source them.
*/
void
ex_packloadall(exarg_T *eap)
{
if (!did_source_packages || eap->forceit)
{
// First do a round to add all directories to 'runtimepath', then load
// the plugins. This allows for plugins to use an autoload directory
// of another plugin.
add_pack_start_dirs();
load_start_packages();
}
}
/*
* ":packadd[!] {name}"
*/
void
ex_packadd(exarg_T *eap)
{
static char *plugpat = "pack/*/%s/%s";
int len;
char *pat;
int round;
int res = OK;
// Round 1: use "start", round 2: use "opt".
for (round = 1; round <= 2; ++round)
{
// Only look under "start" when loading packages wasn't done yet.
if (round == 1 && did_source_packages)
continue;
len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5;
pat = alloc(len);
if (pat == NULL)
return;
vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg);
// The first round don't give a "not found" error, in the second round
// only when nothing was found in the first round.
res = do_in_path(p_pp, (char_u *)pat,
DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0),
add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH);
vim_free(pat);
}
}
#endif
/*
* Sort "gap" and remove duplicate entries. "gap" is expected to contain a
* list of file names in allocated memory.
*/
void
remove_duplicates(garray_T *gap)
{
int i;
int j;
char_u **fnames = (char_u **)gap->ga_data;
sort_strings(fnames, gap->ga_len);
for (i = gap->ga_len - 1; i > 0; --i)
if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
{
vim_free(fnames[i]);
for (j = i + 1; j < gap->ga_len; ++j)
fnames[j - 1] = fnames[j];
--gap->ga_len;
}
}
static void
ExpandRTDir_int(
char_u *pat,
size_t pat_len,
int flags,
int keep_ext,
garray_T *gap,
char *dirnames[])
{
for (int i = 0; dirnames[i] != NULL; ++i)
{