forked from zdharma-continuum/zinit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zinit-autoload.zsh
3446 lines (3019 loc) · 131 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
# -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# Copyright (c) 2016-2020 Sebastian Gniazdowski and contributors.
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" ]]; then
# Get the user---plugin part of path
while [[ "$in_plugin_path" != ${ZINIT[PLUGINS_DIR]}/[^/]## && "$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
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
+zinit-message "{info}Uninstalling completion \`{file}$cfile{info}'{…}{rst}"
# Make compinit notice the change
.zinit-forget-completion "$cfile"
(( global_action ++ ))
else
+zinit-message "{info}Completion \`{file}$cfile{info}' not installed.{rst}"
fi
done
if (( global_action > 0 )); then
+zinit-message "{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-self-update [[[
# Updates Zinit code (does a git pull).
#
# User-action entry point.
.zinit-self-update() {
emulate -LR zsh
setopt extendedglob typesetsilent warncreateglobal
[[ $1 = -q ]] && +zinit-message "{info2}Updating Zinit{…}{rst}"
local nl=$'\n' escape=$'\x1b['
local -a lines
( builtin cd -q "$ZINIT[BIN_DIR]" && \
command git checkout master &>/dev/null && \
command git checkout main &>/dev/null && \
command git fetch --quiet && \
lines=( ${(f)"$(command git log --color --date=short --pretty=format:'%Cgreen%cd %h %Creset%s %Cred%d%Creset || %b' ..FETCH_HEAD)"} )
if (( ${#lines} > 0 )); then
# Remove the (origin/master ...) segments, to expect only tags to appear
lines=( "${(S)lines[@]//\(([,[:blank:]]#(origin|HEAD|master|main)[^a-zA-Z]##(HEAD|origin|master|main)[,[:blank:]]#)#\)/}" )
# Remove " ||" if it ends the line (i.e. no additional text from the body)
lines=( "${lines[@]/ \|\|[[:blank:]]#(#e)/}" )
# If there's no ref-name, 2 consecutive spaces occur - fix this
lines=( "${lines[@]/(#b)[[:space:]]#\|\|[[:space:]]#(*)(#e)/|| ${match[1]}}" )
lines=( "${lines[@]/(#b)$escape([0-9]##)m[[:space:]]##${escape}m/$escape${match[1]}m${escape}m}" )
# Replace what follows "|| ..." with the same thing but with no newlines,
# and also only first 10 words (the (w)-flag enables word-indexing)
lines=( "${lines[@]/(#b)[[:blank:]]#\|\|(*)(#e)/| ${${match[1]//$nl/ }[(w)1,(w)10]}}" )
builtin print -rl -- "${lines[@]}" | .zinit-pager
builtin print
fi
if [[ $1 != -q ]] {
command git pull --no-stat --ff-only origin master
} else {
command git pull --no-stat --quiet --ff-only origin master
}
)
if [[ $1 != -q ]] {
+zinit-message "Compiling Zinit (zcompile){…}"
}
command rm -f $ZINIT[BIN_DIR]/*.zwc(DN)
zcompile -U $ZINIT[BIN_DIR]/zinit.zsh
zcompile -U $ZINIT[BIN_DIR]/zinit-side.zsh
zcompile -U $ZINIT[BIN_DIR]/zinit-install.zsh
zcompile -U $ZINIT[BIN_DIR]/zinit-autoload.zsh
zcompile -U $ZINIT[BIN_DIR]/zinit-additional.zsh
zcompile -U $ZINIT[BIN_DIR]/git-process-output.zsh
# Load for the current session
[[ $1 != -q ]] && +zinit-message "Reloading Zinit for the current session{…}"
source $ZINIT[BIN_DIR]/zinit.zsh
source $ZINIT[BIN_DIR]/zinit-side.zsh
source $ZINIT[BIN_DIR]/zinit-install.zsh
source $ZINIT[BIN_DIR]/zinit-autoload.zsh
# Read and remember the new modification timestamps
local file
for file ( "" -side -install -autoload ) {
.zinit-get-mtime-into "${ZINIT[BIN_DIR]}/zinit$file.zsh" "ZINIT[mtime$file]"
}
} # ]]]
# FUNCTION: .zinit-show-registered-plugins [[[
# Lists loaded plugins (subcommands list, lodaded).
#
# User-action entry point.
.zinit-show-registered-plugins() {
emulate -LR zsh
setopt extendedglob warncreateglobal typesetsilent noshortloops
typeset -a filtered
local keyword="$1"
keyword="${keyword## ##}"
keyword="${keyword%% ##}"
if [[ -n "$keyword" ]]; then
builtin print "Installed plugins matching ${ZINIT[col-info]}$keyword${ZINIT[col-rst]}:"
filtered=( "${(M)ZINIT_REGISTERED_PLUGINS[@]:#*$keyword*}" )
else
filtered=( "${ZINIT_REGISTERED_PLUGINS[@]}" )
fi
local i
for i in "${filtered[@]}"; do
[[ "$i" = "_local/zinit" ]] && continue
.zinit-any-colorify-as-uspl2 "$i"
# Mark light loads
[[ "${ZINIT[STATES__$i]}" = "1" ]] && REPLY="$REPLY ${ZINIT[col-info]}*${ZINIT[col-rst]}"
builtin print -r -- "$REPLY"
done
} # ]]]
# FUNCTION: .zinit-unload [[[
# 0. Call the Zsh Plugin's Standard *_plugin_unload function
# 0. Call the code provided by the Zsh Plugin's Standard @zsh-plugin-run-at-update
# 1. Delete bindkeys (...)
# 2. Delete Zstyles
# 3. Restore options
# 4. Remove aliases
# 5. Restore Zle state
# 6. Unfunction functions (created by plugin)
# 7. Clean-up FPATH and PATH
# 8. Delete created variables
# 9. Forget the 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-unload() {
.zinit-any-to-user-plugin "$1" "$2"
local uspl2="${reply[-2]}${${reply[-2]:#(%|/)*}:+/}${reply[-1]}" user="${reply[-2]}" plugin="${reply[-1]}" quiet="${${3:+1}:-0}"
local k
.zinit-any-colorify-as-uspl2 "$uspl2"
(( quiet )) || builtin print -r -- "${ZINIT[col-bar]}---${ZINIT[col-rst]} Unloading plugin: $REPLY ${ZINIT[col-bar]}---${ZINIT[col-rst]}"
local ___dir
[[ "$user" = "%" ]] && ___dir="$plugin" || ___dir="${ZINIT[PLUGINS_DIR]}/${user:+${user}---}${plugin//\//---}"
# KSH_ARRAYS immunity
integer correct=0
[[ -o "KSH_ARRAYS" ]] && correct=1
# Allow unload for debug user
if [[ "$uspl2" != "_dtrace/_dtrace" ]]; then
.zinit-exists-message "$1" "$2" || return 1
fi
.zinit-any-colorify-as-uspl2 "$1" "$2"
local uspl2col="$REPLY"
# Store report of the plugin in variable LASTREPORT
typeset -g LASTREPORT
LASTREPORT=`.zinit-show-report "$1" "$2"`
#
# Call the Zsh Plugin's Standard *_plugin_unload function
#
(( ${+functions[${plugin}_plugin_unload]} )) && ${plugin}_plugin_unload
#
# Call the code provided by the Zsh Plugin's Standard @zsh-plugin-run-at-update
#
local -a tmp
local -A sice
tmp=( "${(z@)ZINIT_SICE[$uspl2]}" )
(( ${#tmp} > 1 && ${#tmp} % 2 == 0 )) && sice=( "${(Q)tmp[@]}" ) || sice=()
if [[ -n ${sice[ps-on-unload]} ]]; then
(( quiet )) || builtin print -r "Running plugin's provided unload code: ${ZINIT[col-info]}${sice[ps-on-unload][1,50]}${sice[ps-on-unload][51]:+…}${ZINIT[col-rst]}"
local ___oldcd="$PWD"
() { setopt localoptions noautopushd; builtin cd -q "$___dir"; }
eval "${sice[ps-on-unload]}"
() { setopt localoptions noautopushd; builtin cd -q "$___oldcd"; }
fi
#
# 1. Delete done bindkeys
#
typeset -a string_widget
string_widget=( "${(z)ZINIT[BINDKEYS__$uspl2]}" )
local sw
for sw in "${(Oa)string_widget[@]}"; do
[[ -z "$sw" ]] && continue
# Remove one level of quoting to split using (z)
sw="${(Q)sw}"
typeset -a sw_arr
sw_arr=( "${(z)sw}" )
# Remove one level of quoting to pass to bindkey
local sw_arr1="${(Q)sw_arr[1-correct]}" # Keys
local sw_arr2="${(Q)sw_arr[2-correct]}" # Widget
local sw_arr3="${(Q)sw_arr[3-correct]}" # Optional previous-bound widget
local sw_arr4="${(Q)sw_arr[4-correct]}" # Optional -M or -A or -N
local sw_arr5="${(Q)sw_arr[5-correct]}" # Optional map name
local sw_arr6="${(Q)sw_arr[6-correct]}" # Optional -R (not with -A, -N)
if [[ "$sw_arr4" = "-M" && "$sw_arr6" != "-R" ]]; then
if [[ -n "$sw_arr3" ]]; then
() {
emulate -LR zsh -o extendedglob
(( quiet )) || builtin print -r "Restoring bindkey ${${(q)sw_arr1}//(#m)\\[\^\?\]\[\)\(\'\"\}\{\`]/${MATCH#\\}} $sw_arr3 ${ZINIT[col-info]}in map ${ZINIT[col-rst]}$sw_arr5"
}
bindkey -M "$sw_arr5" "$sw_arr1" "$sw_arr3"
else
(( quiet )) || builtin print -r "Deleting bindkey ${(q)sw_arr1} $sw_arr2 ${ZINIT[col-info]}in map ${ZINIT[col-rst]}$sw_arr5"
bindkey -M "$sw_arr5" -r "$sw_arr1"
fi
elif [[ "$sw_arr4" = "-M" && "$sw_arr6" = "-R" ]]; then
if [[ -n "$sw_arr3" ]]; then
(( quiet )) || builtin print -r "Restoring ${ZINIT[col-info]}range${ZINIT[col-rst]} bindkey ${(q)sw_arr1} $sw_arr3 ${ZINIT[col-info]}in map ${ZINIT[col-rst]}$sw_arr5"
bindkey -RM "$sw_arr5" "$sw_arr1" "$sw_arr3"
else
(( quiet )) || builtin print -r "Deleting ${ZINIT[col-info]}range${ZINIT[col-rst]} bindkey ${(q)sw_arr1} $sw_arr2 ${ZINIT[col-info]}in map ${ZINIT[col-rst]}$sw_arr5"
bindkey -M "$sw_arr5" -Rr "$sw_arr1"
fi
elif [[ "$sw_arr4" != "-M" && "$sw_arr6" = "-R" ]]; then
if [[ -n "$sw_arr3" ]]; then
(( quiet )) || builtin print -r "Restoring ${ZINIT[col-info]}range${ZINIT[col-rst]} bindkey ${(q)sw_arr1} $sw_arr3"
bindkey -R "$sw_arr1" "$sw_arr3"
else
(( quiet )) || builtin print -r "Deleting ${ZINIT[col-info]}range${ZINIT[col-rst]} bindkey ${(q)sw_arr1} $sw_arr2"
bindkey -Rr "$sw_arr1"
fi
elif [[ "$sw_arr4" = "-A" ]]; then
(( quiet )) || builtin print -r "Linking backup-\`main' keymap \`$sw_arr5' back to \`main'"
bindkey -A "$sw_arr5" "main"
elif [[ "$sw_arr4" = "-N" ]]; then
(( quiet )) || builtin print -r "Deleting keymap \`$sw_arr5'"
bindkey -D "$sw_arr5"
else
if [[ -n "$sw_arr3" ]]; then
() {
emulate -LR zsh -o extendedglob
(( quiet )) || builtin print -r "Restoring bindkey ${${(q)sw_arr1}//(#m)\\[\^\?\]\[\)\(\'\"\}\{\`]/${MATCH#\\}} $sw_arr3"
}
bindkey "$sw_arr1" "$sw_arr3"
else
(( quiet )) || builtin print -r "Deleting bindkey ${(q)sw_arr1} $sw_arr2"
bindkey -r "$sw_arr1"
fi
fi
done
#
# 2. Delete created Zstyles
#
typeset -a pattern_style
pattern_style=( "${(z)ZINIT[ZSTYLES__$uspl2]}" )
local ps
for ps in "${(Oa)pattern_style[@]}"; do
[[ -z "$ps" ]] && continue
# Remove one level of quoting to split using (z)
ps="${(Q)ps}"
typeset -a ps_arr
ps_arr=( "${(z)ps}" )
# Remove one level of quoting to pass to zstyle
local ps_arr1="${(Q)ps_arr[1-correct]}"
local ps_arr2="${(Q)ps_arr[2-correct]}"
(( quiet )) || builtin print "Deleting zstyle $ps_arr1 $ps_arr2"
zstyle -d "$ps_arr1" "$ps_arr2"
done
#
# 3. Restore changed options
#
# Paranoid, don't want bad key/value pair error
.zinit-diff-options-compute "$uspl2"
integer empty=0
.zinit-save-set-extendedglob
[[ "${ZINIT[OPTIONS__$uspl2]}" != *[$'! \t']* ]] && empty=1
.zinit-restore-extendedglob
if (( empty != 1 )); then
typeset -A opts
opts=( "${(z)ZINIT[OPTIONS__$uspl2]}" )
for k in "${(kon)opts[@]}"; do
# Internal options
[[ "$k" = "physical" ]] && continue
if [[ "${opts[$k]}" = "on" ]]; then
(( quiet )) || builtin print "Setting option $k"
builtin setopt "$k"
else
(( quiet )) || builtin print "Unsetting option $k"
builtin unsetopt "$k"
fi
done
fi
#
# 4. Delete aliases
#
typeset -a aname_avalue
aname_avalue=( "${(z)ZINIT[ALIASES__$uspl2]}" )
local nv
for nv in "${(Oa)aname_avalue[@]}"; do
[[ -z "$nv" ]] && continue
# Remove one level of quoting to split using (z)
nv="${(Q)nv}"
typeset -a nv_arr
nv_arr=( "${(z)nv}" )
# Remove one level of quoting to pass to unalias
local nv_arr1="${(Q)nv_arr[1-correct]}"
local nv_arr2="${(Q)nv_arr[2-correct]}"
local nv_arr3="${(Q)nv_arr[3-correct]}"
if [[ "$nv_arr3" = "-s" ]]; then