forked from zdharma-continuum/zinit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zinit-autoload.zsh
3581 lines (3190 loc) · 141 KB
/
zinit-autoload.zsh
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
#!/usr/bin/env zsh
#
# zdharma-continuum/zinit/zinit-autoload.zsh
# Copyright (c) 2016-2021 Sebastian Gniazdowski
# Copyright (c) 2021-2023 zdharma-continuum
# Homepage: https://github.com/zdharma-continuum/zinit
# License: MIT License
#
builtin source "${ZINIT[BIN_DIR]}/zinit-side.zsh" || { builtin print -P "${ZINIT[col-error]}ERROR:%f%b Couldn't find ${ZINIT[col-obj]}zinit-side.zsh%f%b."; return 1; }
ZINIT[EXTENDED_GLOB]=""
#
# Backend, low level functions
#
# FUNCTION: .zinit-unregister-plugin [[[
# Removes the plugin from ZINIT_REGISTERED_PLUGINS array and from the
# zsh_loaded_plugins array (managed according to the plugin standard)
.zinit-unregister-plugin() {
.zinit-any-to-user-plugin "$1" "$2"
local uspl2="${reply[-2]}${${reply[-2]:#(%|/)*}:+/}${reply[-1]}" \
teleid="$3"
# If not found, the index will be length+1
ZINIT_REGISTERED_PLUGINS[${ZINIT_REGISTERED_PLUGINS[(i)$uspl2]}]=()
# Support Zsh plugin standard
zsh_loaded_plugins[${zsh_loaded_plugins[(i)$teleid]}]=()
ZINIT[STATES__$uspl2]="0"
} # ]]]
# FUNCTION: .zinit-diff-functions-compute [[[
# Computes FUNCTIONS that holds new functions added by plugin.
# Uses data gathered earlier by .zinit-diff-functions().
#
# $1 - user/plugin
.zinit-diff-functions-compute() {
local uspl2="$1"
# Cannot run diff if *_BEFORE or *_AFTER variable is not set
# Following is paranoid for *_BEFORE and *_AFTER being only spaces
builtin setopt localoptions extendedglob nokshglob noksharrays
[[ "${ZINIT[FUNCTIONS_BEFORE__$uspl2]}" != *[$'! \t']* || "${ZINIT[FUNCTIONS_AFTER__$uspl2]}" != *[$'! \t']* ]] && return 1
typeset -A func
local i
# This includes new functions. Quoting is kept (i.e. no i=${(Q)i})
for i in "${(z)ZINIT[FUNCTIONS_AFTER__$uspl2]}"; do
func[$i]=1
done
# Remove duplicated entries, i.e. existing before. Quoting is kept
for i in "${(z)ZINIT[FUNCTIONS_BEFORE__$uspl2]}"; do
# if would do unset, then: func[opp+a\[]: invalid parameter name
func[$i]=0
done
# Store the functions, associating them with plugin ($uspl2)
ZINIT[FUNCTIONS__$uspl2]=""
for i in "${(onk)func[@]}"; do
[[ "${func[$i]}" = "1" ]] && ZINIT[FUNCTIONS__$uspl2]+="$i "
done
return 0
} # ]]]
# FUNCTION: .zinit-diff-options-compute [[[
# Computes OPTIONS that holds options changed by plugin.
# Uses data gathered earlier by .zinit-diff-options().
#
# $1 - user/plugin
.zinit-diff-options-compute() {
local uspl2="$1"
# Cannot run diff if *_BEFORE or *_AFTER variable is not set
# Following is paranoid for *_BEFORE and *_AFTER being only spaces
builtin setopt localoptions extendedglob nokshglob noksharrays
[[ "${ZINIT[OPTIONS_BEFORE__$uspl2]}" != *[$'! \t']* || "${ZINIT[OPTIONS_AFTER__$uspl2]}" != *[$'! \t']* ]] && return 1
typeset -A opts_before opts_after opts
opts_before=( "${(z)ZINIT[OPTIONS_BEFORE__$uspl2]}" )
opts_after=( "${(z)ZINIT[OPTIONS_AFTER__$uspl2]}" )
opts=( )
# Iterate through first array (keys the same
# on both of them though) and test for a change
local key
for key in "${(k)opts_before[@]}"; do
if [[ "${opts_before[$key]}" != "${opts_after[$key]}" ]]; then
opts[$key]="${opts_before[$key]}"
fi
done
# Serialize for reporting
local IFS=" "
ZINIT[OPTIONS__$uspl2]="${(kv)opts[@]}"
return 0
} # ]]]
# FUNCTION: .zinit-diff-env-compute [[[
# Computes ZINIT_PATH, ZINIT_FPATH that hold (f)path components
# added by plugin. Uses data gathered earlier by .zinit-diff-env().
#
# $1 - user/plugin
.zinit-diff-env-compute() {
local uspl2="$1"
typeset -a tmp
# Cannot run diff if *_BEFORE or *_AFTER variable is not set
# Following is paranoid for *_BEFORE and *_AFTER being only spaces
builtin setopt localoptions extendedglob nokshglob noksharrays
[[ "${ZINIT[PATH_BEFORE__$uspl2]}" != *[$'! \t']* || "${ZINIT[PATH_AFTER__$uspl2]}" != *[$'! \t']* ]] && return 1
[[ "${ZINIT[FPATH_BEFORE__$uspl2]}" != *[$'! \t']* || "${ZINIT[FPATH_AFTER__$uspl2]}" != *[$'! \t']* ]] && return 1
typeset -A path_state fpath_state
local i
#
# PATH processing
#
# This includes new path elements
for i in "${(z)ZINIT[PATH_AFTER__$uspl2]}"; do
path_state[${(Q)i}]=1
done
# Remove duplicated entries, i.e. existing before
for i in "${(z)ZINIT[PATH_BEFORE__$uspl2]}"; do
unset "path_state[${(Q)i}]"
done
# Store the path elements, associating them with plugin ($uspl2)
ZINIT[PATH__$uspl2]=""
for i in "${(onk)path_state[@]}"; do
ZINIT[PATH__$uspl2]+="${(q)i} "
done
#
# FPATH processing
#
# This includes new path elements
for i in "${(z)ZINIT[FPATH_AFTER__$uspl2]}"; do
fpath_state[${(Q)i}]=1
done
# Remove duplicated entries, i.e. existing before
for i in "${(z)ZINIT[FPATH_BEFORE__$uspl2]}"; do
unset "fpath_state[${(Q)i}]"
done
# Store the path elements, associating them with plugin ($uspl2)
ZINIT[FPATH__$uspl2]=""
for i in "${(onk)fpath_state[@]}"; do
ZINIT[FPATH__$uspl2]+="${(q)i} "
done
return 0
} # ]]]
# FUNCTION: .zinit-diff-parameter-compute [[[
# Computes ZINIT_PARAMETERS_PRE, ZINIT_PARAMETERS_POST that hold
# parameters created or changed (their type) by plugin. Uses
# data gathered earlier by .zinit-diff-parameter().
#
# $1 - user/plugin
.zinit-diff-parameter-compute() {
local uspl2="$1"
typeset -a tmp
# Cannot run diff if *_BEFORE or *_AFTER variable is not set
# Following is paranoid for *_BEFORE and *_AFTER being only spaces
builtin setopt localoptions extendedglob nokshglob noksharrays
[[ "${ZINIT[PARAMETERS_BEFORE__$uspl2]}" != *[$'! \t']* || "${ZINIT[PARAMETERS_AFTER__$uspl2]}" != *[$'! \t']* ]] && return 1
# Un-concatenated parameters from moment of diff start and of diff end
typeset -A params_before params_after
params_before=( "${(z)ZINIT[PARAMETERS_BEFORE__$uspl2]}" )
params_after=( "${(z)ZINIT[PARAMETERS_AFTER__$uspl2]}" )
# The parameters that changed, with save of what
# parameter was when diff started or when diff ended
typeset -A params_pre params_post
params_pre=( )
params_post=( )
# Iterate through all existing keys, before or after diff,
# i.e. after all variables that were somehow live across
# the diffing process
local key
typeset -aU keys
keys=( "${(k)params_after[@]}" );
keys=( "${keys[@]}" "${(k)params_before[@]}" );
for key in "${keys[@]}"; do
key="${(Q)key}"
[[ "${params_after[$key]}" = *local* ]] && continue
if [[ "${params_after[$key]}" != "${params_before[$key]}" ]]; then
# Empty for a new param, a type otherwise
[[ -z "${params_before[$key]}" ]] && params_before[$key]="\"\""
params_pre[$key]="${params_before[$key]}"
# Current type, can also be empty, when plugin
# unsets a parameter
[[ -z "${params_after[$key]}" ]] && params_after[$key]="\"\""
params_post[$key]="${params_after[$key]}"
fi
done
# Serialize for reporting
ZINIT[PARAMETERS_PRE__$uspl2]="${(j: :)${(qkv)params_pre[@]}}"
ZINIT[PARAMETERS_POST__$uspl2]="${(j: :)${(qkv)params_post[@]}}"
return 0
} # ]]]
# FUNCTION: .zinit-any-to-uspl2 [[[
# Converts given plugin-spec to format that's used in keys for hash tables.
# So basically, creates string "user/plugin" (this format is called: uspl2).
#
# $1 - plugin spec (4 formats: user---plugin, user/plugin, user, plugin)
# $2 - (optional) plugin (only when $1 - i.e. user - given)
.zinit-any-to-uspl2() {
.zinit-any-to-user-plugin "$1" "$2"
[[ "${reply[-2]}" = "%" ]] && REPLY="${reply[-2]}${reply[-1]}" || REPLY="${reply[-2]}${${reply[-2]:#(%|/)*}:+/}${reply[-1]//---//}"
} # ]]]
# FUNCTION: .zinit-save-set-extendedglob [[[
# Enables extendedglob-option first saving if it was already
# enabled, for restoration of this state later.
.zinit-save-set-extendedglob() {
[[ -o "extendedglob" ]] && ZINIT[EXTENDED_GLOB]="1" || ZINIT[EXTENDED_GLOB]="0"
builtin setopt extendedglob
} # ]]]
# FUNCTION: .zinit-restore-extendedglob [[[
# Restores extendedglob-option from state saved earlier.
.zinit-restore-extendedglob() {
[[ "${ZINIT[EXTENDED_GLOB]}" = "0" ]] && builtin unsetopt extendedglob || builtin setopt extendedglob
} # ]]]
# FUNCTION: .zinit-prepare-readlink [[[
# Prepares readlink command, used for establishing completion's owner.
#
# $REPLY = ":" or "readlink"
.zinit-prepare-readlink() {
REPLY=":"
if type readlink 2>/dev/null 1>&2; then
REPLY="readlink"
fi
} # ]]]
# FUNCTION: .zinit-clear-report-for [[[
# Clears all report data for given user/plugin. This is
# done by resetting all related global ZINIT_* hashes.
#
# $1 - plugin spec (4 formats: user---plugin, user/plugin, user, plugin)
# $2 - (optional) plugin (only when $1 - i.e. user - given)
.zinit-clear-report-for() {
.zinit-any-to-uspl2 "$1" "$2"
# Shadowing
ZINIT_REPORTS[$REPLY]=""
ZINIT[BINDKEYS__$REPLY]=""
ZINIT[ZSTYLES__$REPLY]=""
ZINIT[ALIASES__$REPLY]=""
ZINIT[WIDGETS_SAVED__$REPLY]=""
ZINIT[WIDGETS_DELETE__$REPLY]=""
# Function diffing
ZINIT[FUNCTIONS__$REPLY]=""
ZINIT[FUNCTIONS_BEFORE__$REPLY]=""
ZINIT[FUNCTIONS_AFTER__$REPLY]=""
# Option diffing
ZINIT[OPTIONS__$REPLY]=""
ZINIT[OPTIONS_BEFORE__$REPLY]=""
ZINIT[OPTIONS_AFTER__$REPLY]=""
# Environment diffing
ZINIT[PATH__$REPLY]=""
ZINIT[PATH_BEFORE__$REPLY]=""
ZINIT[PATH_AFTER__$REPLY]=""
ZINIT[FPATH__$REPLY]=""
ZINIT[FPATH_BEFORE__$REPLY]=""
ZINIT[FPATH_AFTER__$REPLY]=""
# Parameter diffing
ZINIT[PARAMETERS_PRE__$REPLY]=""
ZINIT[PARAMETERS_POST__$REPLY]=""
ZINIT[PARAMETERS_BEFORE__$REPLY]=""
ZINIT[PARAMETERS_AFTER__$REPLY]=""
} # ]]]
# FUNCTION: .zinit-exists-message [[[
# Checks if plugin is loaded. Testable. Also outputs error
# message if plugin is not loaded.
#
# $1 - plugin spec (4 formats: user---plugin, user/plugin, user, plugin)
# $2 - (optional) plugin (only when $1 - i.e. user - given)
.zinit-exists-message() {
.zinit-any-to-uspl2 "$1" "$2"
if [[ -z "${ZINIT_REGISTERED_PLUGINS[(r)$REPLY]}" ]]; then
.zinit-any-colorify-as-uspl2 "$1" "$2"
builtin print "${ZINIT[col-error]}No such plugin${ZINIT[col-rst]} $REPLY"
return 1
fi
return 0
} # ]]]
# FUNCTION: .zinit-at-eval [[[
.zinit-at-eval() {
local atclone="$2" atpull="$1"
integer retval
@zinit-substitute atclone atpull
[[ $atpull = "%atclone" ]] && { eval "$atclone"; retval=$?; } || { eval "$atpull"; retval=$?; }
return $retval
} # ]]]
#
# Format functions
#
# FUNCTION: .zinit-format-functions [[[
# Creates a one or two columns text with functions created
# by given plugin.
#
# $1 - user/plugin (i.e. uspl2 format of plugin-spec)
.zinit-format-functions() {
local uspl2="$1"
typeset -a func
func=( "${(z)ZINIT[FUNCTIONS__$uspl2]}" )
# Get length of longest left-right string pair,
# and length of longest left string
integer longest=0 longest_left=0 cur_left_len=0 count=1
local f
for f in "${(on)func[@]}"; do
[[ -z "${#f}" ]] && continue
f="${(Q)f}"
# Compute for elements in left column,
# ones that will be paded with spaces
if (( count ++ % 2 != 0 )); then
[[ "${#f}" -gt "$longest_left" ]] && longest_left="${#f}"
cur_left_len="${#f}"
else
cur_left_len+="${#f}"
cur_left_len+=1 # For separating space
[[ "$cur_left_len" -gt "$longest" ]] && longest="$cur_left_len"
fi
done
# Output in one or two columns
local answer=""
count=1
for f in "${(on)func[@]}"; do
[[ -z "$f" ]] && continue
f="${(Q)f}"
if (( COLUMNS >= longest )); then
if (( count ++ % 2 != 0 )); then
answer+="${(r:longest_left+1:: :)f}"
else
answer+="$f"$'\n'
fi
else
answer+="$f"$'\n'
fi
done
REPLY="$answer"
# == 0 is: next element would have newline (postfix addition in "count ++")
(( COLUMNS >= longest && count % 2 == 0 )) && REPLY="$REPLY"$'\n'
} # ]]]
# FUNCTION: .zinit-format-options [[[
# Creates one-column text about options that changed when
# plugin "$1" was loaded.
#
# $1 - user/plugin (i.e. uspl2 format of plugin-spec)
.zinit-format-options() {
local uspl2="$1"
REPLY=""
# Paranoid, don't want bad key/value pair error
integer empty=0
.zinit-save-set-extendedglob
[[ "${ZINIT[OPTIONS__$uspl2]}" != *[$'! \t']* ]] && empty=1
.zinit-restore-extendedglob
(( empty )) && return 0
typeset -A opts
opts=( "${(z)ZINIT[OPTIONS__$uspl2]}" )
# Get length of longest option
integer longest=0
local k
for k in "${(kon)opts[@]}"; do
[[ "${#k}" -gt "$longest" ]] && longest="${#k}"
done
# Output in one column
local txt
for k in "${(kon)opts[@]}"; do
[[ "${opts[$k]}" = "on" ]] && txt="was unset" || txt="was set"
REPLY+="${(r:longest+1:: :)k}$txt"$'\n'
done
} # ]]]
# FUNCTION: .zinit-format-env [[[
# Creates one-column text about FPATH or PATH elements
# added when given plugin was loaded.
#
# $1 - user/plugin (i.e. uspl2 format of plugin-spec)
# $2 - if 1, then examine PATH, if 2, then examine FPATH
.zinit-format-env() {
local uspl2="$1" which="$2"
# Format PATH?
if [[ "$which" = "1" ]]; then
typeset -a elem
elem=( "${(z@)ZINIT[PATH__$uspl2]}" )
elif [[ "$which" = "2" ]]; then
typeset -a elem
elem=( "${(z@)ZINIT[FPATH__$uspl2]}" )
fi
# Enumerate elements added
local answer="" e
for e in "${elem[@]}"; do
[[ -z "$e" ]] && continue
e="${(Q)e}"
answer+="$e"$'\n'
done
[[ -n "$answer" ]] && REPLY="$answer"
} # ]]]
# FUNCTION: .zinit-format-parameter [[[
# Creates one column text that lists global parameters that
# changed when the given plugin was loaded.
#
# $1 - user/plugin (i.e. uspl2 format of plugin-spec)
.zinit-format-parameter() {
local uspl2="$1" infoc="${ZINIT[col-info]}" k
builtin setopt localoptions extendedglob nokshglob noksharrays
REPLY=""
[[ "${ZINIT[PARAMETERS_PRE__$uspl2]}" != *[$'! \t']* || "${ZINIT[PARAMETERS_POST__$uspl2]}" != *[$'! \t']* ]] && return 0
typeset -A elem_pre elem_post
elem_pre=( "${(z)ZINIT[PARAMETERS_PRE__$uspl2]}" )
elem_post=( "${(z)ZINIT[PARAMETERS_POST__$uspl2]}" )
# Find longest key and longest value
integer longest=0 vlongest1=0 vlongest2=0
local v1 v2
for k in "${(k)elem_post[@]}"; do
k="${(Q)k}"
[[ "${#k}" -gt "$longest" ]] && longest="${#k}"
v1="${(Q)elem_pre[$k]}"
v2="${(Q)elem_post[$k]}"
[[ "${#v1}" -gt "$vlongest1" ]] && vlongest1="${#v1}"
[[ "${#v2}" -gt "$vlongest2" ]] && vlongest2="${#v2}"
done
# Enumerate parameters that changed. A key
# always exists in both of the arrays
local answer="" k
for k in "${(k)elem_post[@]}"; do
v1="${(Q)elem_pre[$k]}"
v2="${(Q)elem_post[$k]}"
k="${(Q)k}"
k="${(r:longest+1:: :)k}"
v1="${(l:vlongest1+1:: :)v1}"
v2="${(r:vlongest2+1:: :)v2}"
answer+="$k ${infoc}[$v1 -> $v2]${ZINIT[col-rst]}"$'\n'
done
[[ -n "$answer" ]] && REPLY="$answer"
return 0
} # ]]]
#
# Completion functions
#
# FUNCTION: .zinit-get-completion-owner [[[
# Returns "user---plugin" string (uspl1 format) of plugin that
# owns given completion.
#
# Both :A and readlink will be used, then readlink's output if
# results differ. Readlink might not be available.
#
# :A will read the link "twice" and give the final repository
# directory, possibly without username in the uspl format;
# readlink will read the link "once"
#
# $1 - absolute path to completion file (in COMPLETIONS_DIR)
# $2 - readlink command (":" or "readlink")
.zinit-get-completion-owner() {
setopt localoptions extendedglob nokshglob noksharrays noshwordsplit
local cpath="$1"
local readlink_cmd="$2"
local in_plugin_path tmp
# Try to go not too deep into resolving the symlink,
# to have the name as it is in .zinit/plugins
# :A goes deep, descends fully to origin directory
# Readlink just reads what symlink points to
in_plugin_path="${cpath:A}"
tmp=$( "$readlink_cmd" "$cpath" )
# This in effect works as: "if different, then readlink"
[[ -n "$tmp" ]] && in_plugin_path="$tmp"
if [[ "$in_plugin_path" != "$cpath" && -r "$in_plugin_path" ]]; then
# Get the user---plugin part of path
while [[ "$in_plugin_path" != ${ZINIT[PLUGINS_DIR]}/[^/]## && "$in_plugin_path" != "/" && "$in_plugin_path" != "." ]]; do
in_plugin_path="${in_plugin_path:h}"
done
in_plugin_path="${in_plugin_path:t}"
if [[ -z "$in_plugin_path" ]]; then
in_plugin_path="${tmp:h}"
fi
else
# readlink and :A have nothing
in_plugin_path="[unknown]"
fi
REPLY="$in_plugin_path"
} # ]]]
# FUNCTION: .zinit-get-completion-owner-uspl2col [[[
# For shortening of code - returns colorized plugin name
# that owns given completion.
#
# $1 - absolute path to completion file (in COMPLETIONS_DIR)
# $2 - readlink command (":" or "readlink")
.zinit-get-completion-owner-uspl2col() {
# "cpath" "readline_cmd"
.zinit-get-completion-owner "$1" "$2"
.zinit-any-colorify-as-uspl2 "$REPLY"
} # ]]]
# FUNCTION: .zinit-find-completions-of-plugin [[[
# Searches for completions owned by given plugin.
# Returns them in `reply' array.
#
# $1 - plugin spec (4 formats: user---plugin, user/plugin, user, plugin)
# $2 - plugin (only when $1 - i.e. user - given)
.zinit-find-completions-of-plugin() {
builtin setopt localoptions nullglob extendedglob nokshglob noksharrays
.zinit-any-to-user-plugin "$1" "$2"
local user="${reply[-2]}" plugin="${reply[-1]}" uspl
[[ "$user" = "%" ]] && uspl="${user}${plugin}" || uspl="${reply[-2]}${reply[-2]:+---}${reply[-1]//\//---}"
reply=( "${ZINIT[PLUGINS_DIR]}/$uspl"/**/_[^_.]*~*(*.zwc|*.html|*.txt|*.png|*.jpg|*.jpeg|*.js|*.md|*.yml|*.ri|_zsh_highlight*|/zsdoc/*|*.ps1)(DN) )
} # ]]]
# FUNCTION: .zinit-check-comp-consistency [[[
# Zinit creates symlink for each installed completion.
# This function checks whether given completion (i.e.
# file like "_mkdir") is indeed a symlink. Backup file
# is a completion that is disabled - has the leading "_"
# removed.
#
# $1 - path to completion within plugin's directory
# $2 - path to backup file within plugin's directory
.zinit-check-comp-consistency() {
local cfile="$1" bkpfile="$2"
integer error="$3"
# bkpfile must be a symlink
if [[ -e "$bkpfile" && ! -L "$bkpfile" ]]; then
builtin print "${ZINIT[col-error]}Warning: completion's backup file \`${bkpfile:t}' isn't a symlink${ZINIT[col-rst]}"
error=1
fi
# cfile must be a symlink
if [[ -e "$cfile" && ! -L "$cfile" ]]; then
builtin print "${ZINIT[col-error]}Warning: completion file \`${cfile:t}' isn't a symlink${ZINIT[col-rst]}"
error=1
fi
# Tell user that he can manually modify but should do it right
(( error )) && builtin print "${ZINIT[col-error]}Manual edit of ${ZINIT[COMPLETIONS_DIR]} occured?${ZINIT[col-rst]}"
} # ]]]
# FUNCTION: .zinit-check-which-completions-are-installed [[[
# For each argument that each should be a path to completion
# within a plugin's dir, it checks whether that completion
# is installed - returns 0 or 1 on corresponding positions
# in reply.
#
# $1, ... - path to completion within plugin's directory
.zinit-check-which-completions-are-installed() {
local i cfile bkpfile
reply=( )
for i in "$@"; do
cfile="${i:t}"
bkpfile="${cfile#_}"
if [[ -e "${ZINIT[COMPLETIONS_DIR]}"/"$cfile" || -e "${ZINIT[COMPLETIONS_DIR]}"/"$bkpfile" ]]; then
reply+=( "1" )
else
reply+=( "0" )
fi
done
} # ]]]
# FUNCTION: .zinit-check-which-completions-are-enabled [[[
# For each argument that each should be a path to completion
# within a plugin's dir, it checks whether that completion
# is disabled - returns 0 or 1 on corresponding positions
# in reply.
#
# Uninstalled completions will be reported as "0"
# - i.e. disabled
#
# $1, ... - path to completion within plugin's directory
.zinit-check-which-completions-are-enabled() {
local i cfile
reply=( )
for i in "$@"; do
cfile="${i:t}"
if [[ -e "${ZINIT[COMPLETIONS_DIR]}"/"$cfile" ]]; then
reply+=( "1" )
else
reply+=( "0" )
fi
done
} # ]]]
# FUNCTION: .zinit-uninstall-completions [[[
# Removes all completions of given plugin from Zshell (i.e. from FPATH).
# The FPATH is typically `~/.zinit/completions/'.
#
# $1 - plugin spec (4 formats: user---plugin, user/plugin, user, plugin)
# $2 - plugin (only when $1 - i.e. user - given)
.zinit-uninstall-completions() {
builtin emulate -LR zsh ${=${options[xtrace]:#off}:+-o xtrace}
builtin setopt nullglob extendedglob warncreateglobal typesetsilent noshortloops
typeset -a completions symlinked backup_comps
local c cfile bkpfile
integer action global_action=0
.zinit-get-path "$1" "$2"
[[ -e $REPLY ]] && {
completions=( $REPLY/**/_[^_.]*~*(*.zwc|*.html|*.txt|*.png|*.jpg|*.jpeg|*.js|*.md|*.yml|*.ri|_zsh_highlight*|/zsdoc/*|*.ps1)(DN) )
} || {
builtin print "No completions found for \`$1${${1:#(%|/)*}:+${2:+/}}$2'"
return 1
}
symlinked=( ${ZINIT[COMPLETIONS_DIR]}/_[^_.]*~*.zwc )
backup_comps=( ${ZINIT[COMPLETIONS_DIR]}/[^_.]*~*.zwc )
(( ${+functions[.zinit-forget-completion]} )) || builtin source ${ZINIT[BIN_DIR]}"/zinit-install.zsh"
# Delete completions if they are really there, either
# as completions (_fname) or backups (fname)
for c in ${completions[@]}; do
action=0
cfile=${c:t}
bkpfile=${cfile#_}
# Remove symlink to completion
if [[ -n ${symlinked[(r)*/$cfile]} ]]; then
command rm -f ${ZINIT[COMPLETIONS_DIR]}/$cfile
action=1
fi
# Remove backup symlink (created by cdisable)
if [[ -n ${backup_comps[(r)*/$bkpfile]} ]]; then
command rm -f ${ZINIT[COMPLETIONS_DIR]}/$bkpfile
action=1
fi
if (( action )); then
+zi-log "{info}Uninstalling completion \`{file}$cfile{info}'{…}{rst}"
# Make compinit notice the change
.zinit-forget-completion "$cfile"
(( global_action ++ ))
else
+zi-log "{info}Completion \`{file}$cfile{info}' not installed.{rst}"
fi
done
if (( global_action > 0 )); then
+zi-log "{info}Uninstalled {num}$global_action{info} completions.{rst}"
fi
.zinit-compinit >/dev/null
} # ]]]
#
# User-exposed functions
#
# FUNCTION: .zinit-pager [[[
# BusyBox less lacks the -X and -i options, so it can use more
.zinit-pager() {
setopt LOCAL_OPTIONS EQUALS
# Quiet mode ? → no pager.
if (( OPTS[opt_-n,--no-pager] )) {
cat
return 0
}
if [[ ${${:-=less}:A:t} = busybox* ]] {
more 2>/dev/null
(( ${+commands[more]} ))
} else {
less -FRXi 2>/dev/null
(( ${+commands[less]} ))
}
(( $? )) && cat
return 0
} # ]]]
# FUNCTION: .zinit-build-module [[[
# Performs ./configure && make on the module and displays information
# how to load the module in .zshrc.
.zinit-build-module() {
setopt localoptions localtraps
trap 'return 1' INT TERM
if command git -C "${ZINIT[MODULE_DIR]}" rev-parse 2>/dev/null; then
command git -C "${ZINIT[MODULE_DIR]}" clean -d -f -f
command git -C "${ZINIT[MODULE_DIR]}" reset --hard HEAD
command git -C "${ZINIT[MODULE_DIR]}" pull
else
command git clone "https://github.com/zdharma-continuum/zinit-module.git" "${ZINIT[MODULE_DIR]}" || {
builtin print "${ZINIT[col-error]}Failed to clone module repo${ZINIT[col-rst]}"
return 1
}
fi
( builtin cd -q "${ZINIT[MODULE_DIR]}"
+zi-log "{pname}== Building module zdharma-continuum/zinit-module, running: make clean, then ./configure and then make =={rst}"
+zi-log "{pname}== The module sources are located at: "${ZINIT[MODULE_DIR]}" =={rst}"
if [[ -f Makefile ]] {
if [[ "$1" = "--clean" ]] {
noglob +zi-log {p}-- make distclean --{rst}
make distclean
((1))
} else {
noglob +zi-log {p}-- make clean --{rst}
make clean
}
}
noglob +zi-log {p}-- ./configure --{rst}
CPPFLAGS=-I/usr/local/include CFLAGS="-g -Wall -O3" LDFLAGS=-L/usr/local/lib ./configure --disable-gdbm --without-tcsetpgrp && {
noglob +zi-log {p}-- make --{rst}
if { make } {
[[ -f Src/zdharma_continuum/zinit.so ]] && cp -vf Src/zdharma_continuum/zinit.{so,bundle}
noglob +zi-log "{info}Module has been built correctly.{rst}"
.zinit-module info
} else {
noglob +zi-log "{error}Module didn't build.{rst} "
.zinit-module info --link
}
}
builtin print $EPOCHSECONDS >! "${ZINIT[MAN_DIR]}/COMPILED_AT"
)
} # ]]]
# FUNCTION: .zinit-module [[[
# Function that has sub-commands passed as long-options (with two dashes, --).
# It's an attempt to plugin only this one function into `zinit' function
# defined in zinit.zsh, to not make this file longer than it's needed.
.zinit-module() {
if [[ "$1" = "build" ]]; then
.zinit-build-module "${@[2,-1]}"
elif [[ "$1" = "info" ]]; then
if [[ "$2" = "--link" ]]; then
builtin print -r "You can copy the error messages and submit"
builtin print -r "error-report at: https://github.com/zdharma-continuum/zinit-module/issues"
else
builtin print -r "To load the module, add following 2 lines to .zshrc, at top:"
builtin print -r " module_path+=( \"${ZINIT[MODULE_DIR]}/Src\" )"
builtin print -r " zmodload zdharma_continuum/zinit"
builtin print -r ""
builtin print -r "After loading, use command \`zpmod' to communicate with the module."
builtin print -r "See \`zpmod -h' for more information."
fi
elif [[ "$1" = (help|usage) ]]; then
builtin print -r "Usage: zinit module {build|info|help} [options]"
builtin print -r " zinit module build [--clean]"
builtin print -r " zinit module info [--link]"
builtin print -r ""
builtin print -r "To start using the zinit Zsh module run: \`zinit module build'"
builtin print -r "and follow the instructions. Option --clean causes \`make distclean'"
builtin print -r "to be run. To display the instructions on loading the module, run:"
builtin print -r "\`zinit module info'."
fi
} # ]]]
# FUNCTION: .zinit-cd [[[
# Jumps to plugin's directory (in Zinit's home directory).
#
# User-action entry point.
#
# $1 - plugin spec (4 formats: user---plugin, user/plugin, user, plugin)
# $2 - plugin (only when $1 - i.e. user - given)
.zinit-cd() {
builtin emulate -LR zsh ${=${options[xtrace]:#off}:+-o xtrace}
builtin setopt extendedglob warncreateglobal typesetsilent rcquotes
.zinit-get-path "$1" "$2" && {
if [[ -e $REPLY ]]; then
builtin pushd $REPLY
else
+zi-log "No such plugin or snippet"
return 1
fi
builtin print
} || {
+zi-log "No such plugin or snippet"
return 1
}
} # ]]]
# FUNCTION: .zinit-cdisable [[[
# Enables given installed completion.
#
# User-action entry point.
#
# $1 - e.g. "_mkdir" or "mkdir"
.zinit-cdisable() {
local c="$1"
c="${c#_}"
local cfile="${ZINIT[COMPLETIONS_DIR]}/_${c}"
local bkpfile="${cfile:h}/$c"
if [[ ! -e "$cfile" && ! -e "$bkpfile" ]]; then
builtin print "${ZINIT[col-error]}No such completion \`$c'${ZINIT[col-rst]}"
return 1
fi
# Check if it's already disabled
# Not existing "$cfile" says that
if [[ ! -e "$cfile" ]]; then
builtin print "Completion ${ZINIT[col-info]}$c${ZINIT[col-rst]} already disabled"
.zinit-check-comp-consistency "$cfile" "$bkpfile" 0
return 1
fi
# No disable, but bkpfile exists?
if [[ -e "$bkpfile" ]]; then
builtin print "${ZINIT[col-error]}Warning: completion's backup file \`${bkpfile:t}' already exists, will overwrite${ZINIT[col-rst]}"
.zinit-check-comp-consistency "$cfile" "$bkpfile" 1
command rm -f "$bkpfile"
else
.zinit-check-comp-consistency "$cfile" "$bkpfile" 0
fi
# Disable
command mv "$cfile" "$bkpfile"
# Prepare readlink command for establishing completion's owner
.zinit-prepare-readlink
# Get completion's owning plugin
.zinit-get-completion-owner-uspl2col "$bkpfile" "$REPLY"
builtin print "Disabled ${ZINIT[col-info]}$c${ZINIT[col-rst]} completion belonging to $REPLY"
return 0
} # ]]]
# FUNCTION: .zinit-cenable [[[
# Disables given installed completion.
#
# User-action entry point.
#
# $1 - e.g. "_mkdir" or "mkdir"
.zinit-cenable() {
local c="$1"
c="${c#_}"
local cfile="${ZINIT[COMPLETIONS_DIR]}/_${c}"
local bkpfile="${cfile:h}/$c"
if [[ ! -e "$cfile" && ! -e "$bkpfile" ]]; then
builtin print "${ZINIT[col-error]}No such completion \`$c'${ZINIT[col-rst]}"
return 1
fi
# Check if there is no backup file
# This is treated as if the completion is already enabled
if [[ ! -e "$bkpfile" ]]; then
builtin print "Completion ${ZINIT[col-info]}$c${ZINIT[col-rst]} already enabled"
.zinit-check-comp-consistency "$cfile" "$bkpfile" 0
return 1
fi
# Disabled, but completion file already exists?
if [[ -e "$cfile" ]]; then
builtin print "${ZINIT[col-error]}Warning: completion's file \`${cfile:t}' exists, will overwrite${ZINIT[col-rst]}"
builtin print "${ZINIT[col-error]}Completion is actually enabled and will re-enable it again${ZINIT[col-rst]}"
.zinit-check-comp-consistency "$cfile" "$bkpfile" 1
command rm -f "$cfile"
else
.zinit-check-comp-consistency "$cfile" "$bkpfile" 0
fi
# Enable
command mv "$bkpfile" "$cfile" # move completion's backup file created when disabling
# Prepare readlink command for establishing completion's owner
.zinit-prepare-readlink
# Get completion's owning plugin
.zinit-get-completion-owner-uspl2col "$cfile" "$REPLY"
builtin print "Enabled ${ZINIT[col-info]}$c${ZINIT[col-rst]} completion belonging to $REPLY"
return 0
} # ]]]
# FUNCTION: .zinit-changes [[[
# Shows `git log` of given plugin.
#
# User-action entry point.
#
# $1 - plugin spec (4 formats: user---plugin, user/plugin, user, plugin)
# $2 - plugin (only when $1 - i.e. user - given)
.zinit-changes() {
.zinit-any-to-user-plugin "$1" "$2"
local user="${reply[-2]}" plugin="${reply[-1]}"
.zinit-exists-physically-message "$user" "$plugin" || return 1
(
builtin cd -q "${ZINIT[PLUGINS_DIR]}/${user:+${user}---}${plugin//\//---}" && \
command git log -p --graph --decorate --date=relative -C -M
)
} # ]]]
# FUNCTION: .zinit-clear-completions [[[
# Delete stray and improper completions.
#
# Completions live even when plugin isn't loaded - if they are
# installed and enabled.
#
# User-action entry point.
.zinit-clear-completions() {
builtin setopt localoptions nullglob extendedglob nokshglob noksharrays
typeset -a completions
completions=( "${ZINIT[COMPLETIONS_DIR]}"/_[^_.]*~*.zwc "${ZINIT[COMPLETIONS_DIR]}"/[^_.]*~*.zwc )
# Find longest completion name
local cpath c
integer longest=0
for cpath in "${completions[@]}"; do
c="${cpath:t}"
c="${c#_}"
[[ "${#c}" -gt "$longest" ]] && longest="${#c}"
done
.zinit-prepare-readlink
local rdlink="$REPLY"
integer disabled unknown stray
for cpath in "${completions[@]}"; do
c="${cpath:t}"
[[ "${c#_}" = "${c}" ]] && disabled=1 || disabled=0
c="${c#_}"
# This will resolve completion's symlink to obtain
# information about the repository it comes from, i.e.
# about user and plugin, taken from directory name
.zinit-get-completion-owner "$cpath" "$rdlink"
[[ "$REPLY" = "[unknown]" ]] && unknown=1 || unknown=0
.zinit-any-colorify-as-uspl2 "$REPLY"
# If we successfully read a symlink (unknown == 0), test if it isn't broken
stray=0
if (( unknown == 0 )); then
[[ ! -f "$cpath" ]] && stray=1
fi
if (( unknown == 1 || stray == 1 )); then
builtin print -n "Removing completion: ${(r:longest+1:: :)c} $REPLY"
(( disabled )) && builtin print -n " ${ZINIT[col-error]}[disabled]${ZINIT[col-rst]}"
(( unknown )) && builtin print -n " ${ZINIT[col-error]}[unknown file]${ZINIT[col-rst]}"
(( stray )) && builtin print -n " ${ZINIT[col-error]}[stray]${ZINIT[col-rst]}"
builtin print
command rm -f "$cpath"
fi
done
} # ]]]
# FUNCTION: .zinit-compile-plugin [[[
# Compiles given plugin (its main source file, and also an
# additional "....zsh" file if it exists).
#
# $1 - plugin spec (4 formats: user---plugin, user/plugin, user, plugin)
# $2 - plugin (only when $1 - i.e. user - given)
.zinit-compile-plugin () {
emulate -LR zsh ${=${options[xtrace]:#off}:+-o xtrace}
setopt extendedglob warncreateglobal typesetsilent noshortloops rcquotes
local id_as=$1${2:+${${${(M)1:#%}:+$2}:-/$2}} first plugin_dir filename is_snippet
local -a list
local -A ICE
.zinit-compute-ice "$id_as" "pack" ICE plugin_dir filename is_snippet || return 1
if [[ ${ICE[from]} = gh-r ]] && (( ${+ICE[compile]} == 0 )); then
+zi-log "{dbg} $0: ${id_as} has from'gh-r', skip compile"
return 0
fi
__compile_header () {
(( $#quiet )) || +zi-log "{i} {b}${1}{rst}"
}
if [[ -n "${ICE[compile]}" ]]; then
local -aU pats list=()