forked from zdharma-continuum/zinit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zinit-install.zsh
2283 lines (2045 loc) · 95.1 KB
/
zinit-install.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; }
# FUNCTION: .zinit-parse-json [[[
# Retrievies the ice-list from given profile from
# the JSON of the package.json.
.zinit-parse-json() {
emulate -LR zsh
setopt extendedglob warncreateglobal typesetsilent
local -A ___pos_to_level ___level_to_pos ___pair_map \
___final_pairs ___Strings ___Counts
local ___input=$1 ___workbuf=$1 ___key=$2 ___varname=$3 \
___style ___quoting
integer ___nest=${4:-1} ___idx=0 ___pair_idx ___level=0 \
___start ___end ___sidx=1 ___had_quoted_value=0
local -a match mbegin mend ___pair_order
(( ${(P)+___varname} )) || typeset -gA "$___varname"
___pair_map=( "{" "}" "[" "]" )
while [[ $___workbuf = (#b)[^"{}[]\\\"'":,]#((["{[]}\"'":,])|[\\](*))(*) ]]; do
if [[ -n ${match[3]} ]] {
___idx+=${mbegin[1]}
[[ $___quoting = \' ]] && \
{ ___workbuf=${match[3]}; } || \
{ ___workbuf=${match[3]:1}; (( ++ ___idx )); }
} else {
___idx+=${mbegin[1]}
if [[ -z $___quoting ]] {
if [[ ${match[1]} = ["({["] ]]; then
___Strings[$___level/${___Counts[$___level]}]+=" $'\0'--object--$'\0'"
___pos_to_level[$___idx]=$(( ++ ___level ))
___level_to_pos[$___level]=$___idx
(( ___Counts[$___level] += 1 ))
___sidx=___idx+1
___had_quoted_value=0
elif [[ ${match[1]} = ["]})"] ]]; then
(( !___had_quoted_value )) && \
___Strings[$___level/${___Counts[$___level]}]+=" ${(q)___input[___sidx,___idx-1]//((#s)[[:blank:]]##|([[:blank:]]##(#e)))}"
___had_quoted_value=1
if (( ___level > 0 )); then
___pair_idx=${___level_to_pos[$___level]}
___pos_to_level[$___idx]=$(( ___level -- ))
if [[ ${___pair_map[${___input[___pair_idx]}]} = ${___input[___idx]} ]] {
___final_pairs[$___idx]=$___pair_idx
___final_pairs[$___pair_idx]=$___idx
___pair_order+=( $___idx )
}
else
___pos_to_level[$___idx]=-1
fi
fi
}
[[ ${match[1]} = \" && $___quoting != \' ]] && \
if [[ $___quoting = '"' ]]; then
___Strings[$___level/${___Counts[$___level]}]+=" ${(q)___input[___sidx,___idx-1]}"
___quoting=""
else
___had_quoted_value=1
___sidx=___idx+1
___quoting='"'
fi
[[ ${match[1]} = , && -z $___quoting ]] && \
{
(( !___had_quoted_value )) && \
___Strings[$___level/${___Counts[$___level]}]+=" ${(q)___input[___sidx,___idx-1]//((#s)[[:blank:]]##|([[:blank:]]##(#e)))}"
___sidx=___idx+1
___had_quoted_value=0
}
[[ ${match[1]} = : && -z $___quoting ]] && \
{
___had_quoted_value=0
___sidx=___idx+1
}
[[ ${match[1]} = \' && $___quoting != \" ]] && \
if [[ $___quoting = "'" ]]; then
___Strings[$___level/${___Counts[$___level]}]+=" ${(q)___input[___sidx,___idx-1]}"
___quoting=""
else
___had_quoted_value=1
___sidx=___idx+1
___quoting="'"
fi
___workbuf=${match[4]}
}
done
local ___text ___found
if (( ___nest != 2 )) {
integer ___pair_a ___pair_b
for ___pair_a ( "${___pair_order[@]}" ) {
___pair_b="${___final_pairs[$___pair_a]}"
___text="${___input[___pair_b,___pair_a]}"
if [[ $___text = [[:space:]]#\{[[:space:]]#[\"\']${___key}[\"\']* ]]; then
___found="$___text"
fi
}
}
if [[ -n $___found && $___nest -lt 2 ]] {
.zinit-parse-json "$___found" "$___key" "$___varname" 2
}
if (( ___nest == 2 )) {
: ${(PAA)___varname::="${(kv)___Strings[@]}"}
}
}
# ]]]
# FUNCTION: .zinit-get-package [[[
.zinit-get-package() {
emulate -LR zsh
setopt extendedglob warncreateglobal typesetsilent noshortloops rcquotes
local user=$1 pkg=$2 plugin=$2 id_as=$3 dir=$4 profile=$5 \
local_path=${ZINIT[PLUGINS_DIR]}/${3//\//---} pkgjson \
tmpfile=${$(mktemp):-${TMPDIR:-/tmp}/zsh.xYzAbc123} \
URL=https://raw.githubusercontent.com/Zsh-Packages/$2/master/package.json
local pro_sep="{rst}, {profile}" epro_sep="{error}, {profile}" \
tool_sep="{rst}, {cmd}" \
lhi_hl="{lhi}" profile_hl="{profile}"
trap "rmdir ${(qqq)local_path} 2>/dev/null; return 1" INT TERM QUIT HUP
trap "rmdir ${(qqq)local_path} 2>/dev/null" EXIT
if [[ $profile != ./* ]] {
if { ! .zinit-download-file-stdout $URL 0 1 2>/dev/null > $tmpfile } {
rm -f $tmpfile; .zinit-download-file-stdout $URL 1 1 2>/dev/null >1 $tmpfile
}
} else {
tmpfile=${profile%:*}
profile=${${${(M)profile:#*:*}:+${profile#*:}}:-default}
}
pkgjson="$(<$tmpfile)"
if [[ -z $pkgjson ]]; then
+zinit-message "{u-warn}Error{b-warn}:{error} the package {apo}\`{pid}$id_as{apo}\`"\
"{error}couldn't be found.{rst}"
return 1
fi
local -A Strings
.zinit-parse-json "$pkgjson" "plugin-info" Strings
local -A jsondata1
jsondata1=( ${(@Q)${(@z)Strings[2/1]}} )
local user=${jsondata1[user]} plugin=${jsondata1[plugin]} \
url=${jsondata1[url]} message=${jsondata1[message]} \
required=${jsondata1[required]:-${jsondata1[requires]}}
local -a profiles
local key value
integer pos
profiles=( ${(@Q)${(@z)Strings[2/2]}} )
profiles=( ${profiles[@]:#$'\0'--object--$'\0'} )
pos=${${(@Q)${(@z)Strings[2/2]}}[(I)$profile]}
if (( pos )) {
for key value ( "${(@Q)${(@z)Strings[3/$(( (pos + 1) / 2 ))]}}" ) {
(( ${+ICE[$key]} )) && [[ ${ICE[$key]} != +* ]] && continue
ICE[$key]=$value${ICE[$key]#+}
}
ICE=( "${(kv)ICE[@]//\\\"/\"}" )
[[ ${ICE[as]} = program ]] && ICE[as]="command"
[[ -n ${ICE[on-update-of]} ]] && ICE[subscribe]="${ICE[subscribe]:-${ICE[on-update-of]}}"
[[ -n ${ICE[pick]} ]] && ICE[pick]="${ICE[pick]//\$ZPFX/${ZPFX%/}}"
if [[ -n ${ICE[id-as]} ]] {
@zinit-substitute 'ICE[id-as]'
local -A map
map=( "\"" "\\\"" "\\" "\\" )
eval "ICE[id-as]=\"${ICE[id-as]//(#m)[\"\\]/${map[$MATCH]}}\""
}
} else {
# Założenie: profil domyślny jest pierwszy w tablicy (patrz: inny kolor).
+zinit-message "{u-warn}Error{b-warn}:{error} the profile {apo}\`{hi}$profile{apo}\`" \
"{error}couldn't be found, aborting. Available profiles are:" \
"{lhi}${(pj:$epro_sep:)profiles[@]}{error}.{rst}"
return 1
}
+zinit-message "{info3}Package{ehi}:{rst} {pid}$pkg{rst}. Selected" \
"profile{ehi}:{rst} {hi}$profile{rst}. Available" \
"profiles:${${${(M)profile:#default}:+$lhi_hl}:-$profile_hl}" \
"${(pj:$pro_sep:)profiles[@]}{rst}."
if [[ $profile != *bgn* && -n ${(M)profiles[@]:#*bgn*} ]] {
+zinit-message "{note}Note:{rst} The {apo}\`{profile}bgn{glob}*{apo}\`{rst}" \
"profiles (if any are available) are the recommended ones (the reason" \
"is that they expose the binaries provided by the package without" \
"altering (i.e.: {slight}cluttering{rst}{…}) the {var}\$PATH{rst}" \
"environment variable)."
}
ICE[required]=${ICE[required]:-$ICE[requires]}
local -a req
req=( ${(s.;.)${:-${required:+$required\;}${ICE[required]}}} )
for required ( $req ) {
if [[ $required == (bgn|dl|monitor) ]]; then
if [[ ( $required == bgn && -z ${(k)ZINIT_EXTS[(r)<-> z-annex-data: z-a-bin-gem-node *]} ) || \
( $required == dl && -z ${(k)ZINIT_EXTS[(r)<-> z-annex-data: z-a-patch-dl *]} ) || \
( $required == monitor && -z ${(k)ZINIT_EXTS[(r)<-> z-annex-data: z-a-readurl *]} )
]]; then
local -A namemap
namemap=( bgn Bin-Gem-Node dl Patch-Dl monitor readurl )
+zinit-message -n "{u-warn}ERROR{b-warn}: {error}the "
if [[ -z ${(MS)ICE[required]##(\;|(#s))$required(\;|(#e))} ]]; then
+zinit-message -n "{error}requested profile {apo}\`{hi}$profile{apo}\`{error} "
else
+zinit-message -n "{error}package {pid}$pkg{error} "
fi
+zinit-message '{error}requires the {apo}`{annex}'${namemap[$required]}'{apo}`' \
"{error}annex, which is currently not installed." \
"{nl}{nl}If you'd like to install it, you can visit its homepage:" \
"{nl}– {url}https://github.com/zinit-zsh/z-a-${(L)namemap[$required]}{rst}" \
"{nl}for instructions."
(( ${#profiles[@]:#$profile} > 0 )) && \
+zinit-message "{nl}Other available profiles are:" \
"{profile}${(pj:$pro_sep:)${profiles[@]:#$profile}}{rst}."
return 1
fi
else
if ! command -v $required &>/dev/null; then
+zinit-message -n "{u-warn}ERROR{b-warn}: {error}the "
if [[ -n ${(MS)ICE[required]##(\;|(#s))$required(\;|(#e))} ]]; then
+zinit-message -n "{error}requested profile {apo}\`{hi}$profile{apo}\`{error} "
else
+zinit-message -n "{error}package {pid}$pkg{error} "
fi
+zinit-message '{error}requires a {apo}`{cmd}'$required'{apo}`{error}' \
"command to be available in {var}\$PATH{error}.{rst}" \
"{nl}{error}The package cannot be installed unless the" \
"command will be available."
(( ${#profiles[@]:#$profile} > 0 )) && \
+zinit-message "{nl}Other available profiles are:" \
"{profile}${(pj:$pro_sep:)${profiles[@]:#$profile}}{rst}."
return 1
fi
fi
}
if [[ -n ${ICE[dl]} && -z ${(k)ZINIT_EXTS[(r)<-> z-annex-data: z-a-patch-dl *]} ]] {
+zinit-message "{nl}{u-warn}WARNING{b-warn}:{rst} the profile uses" \
"{ice}dl''{rst} ice however there's currently no {annex}z-a-patch-dl{rst}" \
"annex loaded, which provides it."
+zinit-message "The ice will be inactive, i.e.: no additional" \
"files will become downloaded (the ice downloads the given URLs)." \
"The package should still work, as it doesn't indicate to" \
"{u}{slight}require{rst} the annex."
+zinit-message "{nl}You can download the" \
"annex from its homepage at {url}https://github.com/zinit-zsh/z-a-patch-dl{rst}."
}
[[ -n ${jsondata1[message]} ]] && \
+zinit-message "{info}${jsondata1[message]}{rst}"
if (( ${+ICE[is-snippet]} )) {
reply=( "" "$url" )
REPLY=snippet
return 0
}
if (( !${+ICE[git]} && !${+ICE[from]} )) {
(
.zinit-parse-json "$pkgjson" "_from" Strings
local -A jsondata
jsondata=( "${(@Q)${(@z)Strings[1/1]}}" )
local URL=${jsondata[_resolved]}
local fname="${${URL%%\?*}:t}"
command mkdir -p $dir || {
+zinit-message "{u-warn}Error{b-warn}:{error} Couldn't create directory:" \
"{dir}$dir{error}, aborting.{rst}"
return 1
}
builtin cd -q $dir || return 1
+zinit-message "Downloading tarball for {pid}$plugin{rst}{…}"
if { ! .zinit-download-file-stdout "$URL" 0 1 >! "$fname" } {
if { ! .zinit-download-file-stdout "$URL" 1 1 >! "$fname" } {
command rm -f "$fname"
+zinit-message "Download of the file {apo}\`{file}$fname{apo}\`{rst}" \
"failed. No available download tool? One of:" \
"{cmd}${(pj:$tool_sep:)${=:-curl wget lftp lynx}}{rst}."
return 1
}
}
ziextract "$fname" --move
return 0
) && {
reply=( "$user" "$plugin" )
REPLY=tarball
}
} else {
reply=( "${ICE[user]:-$user}" "${ICE[plugin]:-$plugin}" )
if [[ ${ICE[from]} = (|gh-r|github-rel) ]]; then
REPLY=github
else
REPLY=unknown
fi
}
return $?
}
# ]]]
# FUNCTION: .zinit-setup-plugin-dir [[[
# Clones given plugin into PLUGIN_DIR. Supports multiple
# sites (respecting `from' and `proto' ice modifiers).
# Invokes compilation of plugin's main file.
#
# $1 - user
# $2 - plugin
.zinit-setup-plugin-dir() {
emulate -LR zsh
setopt extendedglob warncreateglobal noshortloops rcquotes
local user=$1 plugin=$2 id_as=$3 remote_url_path=${1:+$1/}$2 \
local_path tpe=$4 update=$5 version=$6
if .zinit-get-object-path plugin "$id_as" && [[ -z $update ]] {
+zinit-message "{u-warn}ERROR{b-warn}:{error} A plugin named {pid}$id_as{error}" \
"already exists, aborting."
return 1
}
local_path=$REPLY
trap "rmdir ${(qqq)local_path}/._zinit ${(qqq)local_path} 2>/dev/null" EXIT
trap "rmdir ${(qqq)local_path}/._zinit ${(qqq)local_path} 2>/dev/null; return 1" INT TERM QUIT HUP
local -A sites
sites=(
github github.com
gh github.com
bitbucket bitbucket.org
bb bitbucket.org
gitlab gitlab.com
gl gitlab.com
notabug notabug.org
nb notabug.org
github-rel github.com/$remote_url_path/releases
gh-r github.com/$remote_url_path/releases
cygwin cygwin
)
ZINIT[annex-multi-flag:pull-active]=${${${(M)update:#-u}:+${ZINIT[annex-multi-flag:pull-active]}}:-2}
local -a arr
if [[ $user = _local ]]; then
builtin print "Warning: no local plugin \`$plugin\'."
builtin print "(should be located at: $local_path)"
return 1
fi
command rm -f ${TMPDIR:-/tmp}/zinit-execs.$$.lst ${TMPDIR:-/tmp}/zinit.installed_comps.$$.lst \
${TMPDIR:-/tmp}/zinit.skipped_comps.$$.lst ${TMPDIR:-/tmp}/zinit.compiled.$$.lst
if [[ $tpe != tarball ]] {
if [[ -z $update ]] {
.zinit-any-colorify-as-uspl2 "$user" "$plugin"
local pid_hl='{pid}' id_msg_part=" (at label{ehi}:{rst} {id-as}$id_as{rst}{…})"
(( $+ICE[pack] )) && local infix_m="({b}{ice}pack{apo}''{rst}) "
+zinit-message "{nl}Downloading $infix_m{pid}$user${user:+/}$plugin{…}${${${id_as:#$user/$plugin}}:+$id_msg_part}"
}
local site
[[ -n ${ICE[from]} ]] && site=${sites[${ICE[from]}]}
if [[ -z $site && ${ICE[from]} = *(gh-r|github-rel)* ]] {
site=${ICE[from]/(gh-r|github-re)/${sites[gh-r]}}
}
}
(
if [[ $site = */releases ]] {
local url=$site/${ICE[ver]}
.zinit-get-latest-gh-r-url-part "$user" "$plugin" "$url" || return $?
command mkdir -p "$local_path"
[[ -d "$local_path" ]] || return 1
(
() { setopt localoptions noautopushd; builtin cd -q "$local_path"; } || return 1
integer count
for REPLY ( $reply ) {
count+=1
url="https://github.com${REPLY}"
if [[ -d $local_path/._zinit ]] {
{ local old_version="$(<$local_path/._zinit/is_release${count:#1})"; } 2>/dev/null
old_version=${old_version/(#b)(\/[^\/]##)(#c4,4)\/([^\/]##)*/${match[2]}}
}
+zinit-message "(Requesting \`${REPLY:t}'${version:+, version $version}{…}${old_version:+ Current version: $old_version.})"
if { ! .zinit-download-file-stdout "$url" 0 1 >! "${REPLY:t}" } {
if { ! .zinit-download-file-stdout "$url" 1 1 >! "${REPLY:t}" } {
command rm -f "${REPLY:t}"
+zinit-message "Download of release for \`$remote_url_path' " \
"failed.{nl}Tried url: $url."
return 1
}
}
if .zinit-download-file-stdout "$url.sig" 2>/dev/null >! "${REPLY:t}.sig"; then
:
else
command rm -f "${REPLY:t}.sig"
fi
command mkdir -p ._zinit
[[ -d ._zinit ]] || return 2
builtin print -r -- $url >! ._zinit/url || return 3
builtin print -r -- ${REPLY} >! ._zinit/is_release${count:#1} || return 4
ziextract ${REPLY:t} ${${${#reply}:#1}:+--nobkp}
}
return $?
) || {
return 1
}
} elif [[ $site = cygwin ]] {
command mkdir -p "$local_path/._zinit"
[[ -d "$local_path" ]] || return 1
(
() { setopt localoptions noautopushd; builtin cd -q "$local_path"; } || return 1
.zinit-get-cygwin-package "$remote_url_path" || return 1
builtin print -r -- $REPLY >! ._zinit/is_release
ziextract "$REPLY"
) || return $?
} elif [[ $tpe = github ]] {
case ${ICE[proto]} in
(|https|git|http|ftp|ftps|rsync|ssh)
:zinit-git-clone() {
command git clone --progress ${(s: :)ICE[cloneopts]---recursive} \
${(s: :)ICE[depth]:+--depth ${ICE[depth]}} \
"${ICE[proto]:-https}://${site:-${ICE[from]:-github.com}}/$remote_url_path" \
"$local_path" \
--config transfer.fsckobjects=false \
--config receive.fsckobjects=false \
--config fetch.fsckobjects=false \
--config pull.rebase=false
integer retval=$?
unfunction :zinit-git-clone
return $retval
}
:zinit-git-clone |& { command ${ZINIT[BIN_DIR]}/git-process-output.zsh || cat; }
if (( pipestatus[1] == 141 )) {
:zinit-git-clone
integer retval=$?
if (( retval )) {
builtin print -Pr -- "$ZINIT[col-error]Clone failed (code: $ZINIT[col-obj]$retval$ZINIT[col-error]).%f%b"
return 1
}
} elif (( pipestatus[1] )) {
builtin print -Pr -- "$ZINIT[col-error]Clone failed (code: $ZINIT[col-obj]$pipestatus[1]$ZINIT[col-error]).%f%b"
return 1
}
;;
(*)
builtin print -Pr "${ZINIT[col-error]}Unknown protocol:%f%b ${ICE[proto]}."
return 1
esac
if [[ -n ${ICE[ver]} ]] {
command git -C "$local_path" checkout "${ICE[ver]}"
}
}
if [[ $update != -u ]] {
# Store ices at clone of a plugin
.zinit-store-ices "$local_path/._zinit" ICE "" "" "" ""
reply=(
${(on)ZINIT_EXTS2[(I)zinit hook:\\\!atclone-pre <->]}
${(on)ZINIT_EXTS[(I)z-annex hook:\\\!atclone-<-> <->]}
${(on)ZINIT_EXTS2[(I)zinit hook:\\\!atclone-post <->]}
)
for key in "${reply[@]}"; do
arr=( "${(Q)${(z@)ZINIT_EXTS[$key]:-$ZINIT_EXTS2[$key]}[@]}" )
"${arr[5]}" plugin "$user" "$plugin" "$id_as" "$local_path" "${${key##(zinit|z-annex) hook:}%% <->}" load
done
# Run annexes' atclone hooks (the after atclone-ice ones)
reply=(
${(on)ZINIT_EXTS2[(I)zinit hook:atclone-pre <->]}
${(on)ZINIT_EXTS[(I)z-annex hook:atclone-<-> <->]}
${(on)ZINIT_EXTS2[(I)zinit hook:atclone-post <->]}
)
for key in "${reply[@]}"; do
arr=( "${(Q)${(z@)ZINIT_EXTS[$key]:-$ZINIT_EXTS2[$key]}[@]}" )
"${arr[5]}" plugin "$user" "$plugin" "$id_as" "$local_path" "${${key##(zinit|z-annex) hook:}%% <->}"
done
}
((1))
) || return $?
typeset -ga INSTALLED_EXECS
{ INSTALLED_EXECS=( "${(@f)$(<${TMPDIR:-/tmp}/zinit-execs.$$.lst)}" ) } 2>/dev/null
# After additional executions like atclone'' - install completions (1 - plugins)
local -A OPTS
OPTS[opt_-q,--quiet]=1
[[ 0 = ${+ICE[nocompletions]} && ${ICE[as]} != null && ${+ICE[null]} -eq 0 ]] && \
.zinit-install-completions "$id_as" "" "0"
if [[ -e ${TMPDIR:-/tmp}/zinit.skipped_comps.$$.lst || -e ${TMPDIR:-/tmp}/zinit.installed_comps.$$.lst ]] {
typeset -ga INSTALLED_COMPS SKIPPED_COMPS
{ INSTALLED_COMPS=( "${(@f)$(<${TMPDIR:-/tmp}/zinit.installed_comps.$$.lst)}" ) } 2>/dev/null
{ SKIPPED_COMPS=( "${(@f)$(<${TMPDIR:-/tmp}/zinit.skipped_comps.$$.lst)}" ) } 2>/dev/null
}
if [[ -e ${TMPDIR:-/tmp}/zinit.compiled.$$.lst ]] {
typeset -ga ADD_COMPILED
{ ADD_COMPILED=( "${(@f)$(<${TMPDIR:-/tmp}/zinit.compiled.$$.lst)}" ) } 2>/dev/null
}
# After any download – rehash the command table
# This will however miss the as"program" binaries
# as their PATH gets extended - and it is done
# later. It will however work for sbin'' ice.
(( !OPTS[opt_-p,--parallel] )) && rehash
return 0
} # ]]]
# FUNCTION: .zinit-install-completions [[[
# Installs all completions of given plugin. After that they are
# visible to `compinit'. Visible completions can be selectively
# disabled and enabled. User can access completion data with
# `clist' or `completions' subcommand.
#
# $1 - plugin spec (4 formats: user---plugin, user/plugin, user, plugin)
# $2 - plugin (only when $1 - i.e. user - given)
# $3 - if 1, then reinstall, otherwise only install completions that aren't there
.zinit-install-completions() {
builtin emulate -LR zsh
setopt nullglob extendedglob warncreateglobal typesetsilent noshortloops
local id_as=$1${2:+${${${(M)1:#%}:+$2}:-/$2}}
local reinstall=${3:-0} quiet=${${4:+1}:-0}
(( OPTS[opt_-q,--quiet] )) && quiet=1
[[ $4 = -Q ]] && quiet=2
typeset -ga INSTALLED_COMPS SKIPPED_COMPS
INSTALLED_COMPS=() SKIPPED_COMPS=()
.zinit-any-to-user-plugin "$id_as" ""
local user=${reply[-2]}
local plugin=${reply[-1]}
.zinit-any-colorify-as-uspl2 "$user" "$plugin"
local abbrev_pspec=$REPLY
.zinit-exists-physically-message "$id_as" "" || return 1
# Symlink any completion files included in plugin's directory
typeset -a completions already_symlinked backup_comps
local c cfile bkpfile
# The plugin == . is a semi-hack/trick to handle `creinstall .' properly
[[ $user == % || ( -z $user && $plugin == . ) ]] && \
completions=( "${plugin}"/**/_[^_.]*~*(*.zwc|*.html|*.txt|*.png|*.jpg|*.jpeg|*.js|*.md|*.yml|*.ri|_zsh_highlight*|/zsdoc/*|*.ps1)(DN^/) ) || \
completions=( "${ZINIT[PLUGINS_DIR]}/${id_as//\//---}"/**/_[^_.]*~*(*.zwc|*.html|*.txt|*.png|*.jpg|*.jpeg|*.js|*.md|*.yml|*.ri|_zsh_highlight*|/zsdoc/*|*.ps1)(DN^/) )
already_symlinked=( "${ZINIT[COMPLETIONS_DIR]}"/_[^_.]*~*.zwc(DN) )
backup_comps=( "${ZINIT[COMPLETIONS_DIR]}"/[^_.]*~*.zwc(DN) )
# Symlink completions if they are not already there
# either as completions (_fname) or as backups (fname)
# OR - if it's a reinstall
for c in "${completions[@]}"; do
cfile="${c:t}"
bkpfile="${cfile#_}"
if [[ ( -z ${already_symlinked[(r)*/$cfile]} || $reinstall = 1 ) &&
-z ${backup_comps[(r)*/$bkpfile]}
]]; then
if [[ $reinstall = 1 ]]; then
# Remove old files
command rm -f "${ZINIT[COMPLETIONS_DIR]}/$cfile" "${ZINIT[COMPLETIONS_DIR]}/$bkpfile"
fi
INSTALLED_COMPS+=( $cfile )
(( quiet )) || builtin print -Pr "Symlinking completion ${ZINIT[col-uname]}$cfile%f%b to completions directory."
command ln -fs "$c" "${ZINIT[COMPLETIONS_DIR]}/$cfile"
# Make compinit notice the change
.zinit-forget-completion "$cfile" "$quiet"
else
SKIPPED_COMPS+=( $cfile )
(( quiet )) || builtin print -Pr "Not symlinking completion \`${ZINIT[col-obj]}$cfile%f%b', it already exists."
(( quiet )) || builtin print -Pr "${ZINIT[col-info2]}Use \`${ZINIT[col-pname]}zinit creinstall $abbrev_pspec${ZINIT[col-info2]}' to force install.%f%b"
fi
done
if (( quiet == 1 && (${#INSTALLED_COMPS} || ${#SKIPPED_COMPS}) )) {
+zinit-message "{msg}Installed {num}${#INSTALLED_COMPS}" \
"{msg}completions. They are stored in{var}" \
"\$INSTALLED_COMPS{msg} array."
if (( ${#SKIPPED_COMPS} )) {
+zinit-message "{msg}Skipped installing" \
"{num}${#SKIPPED_COMPS}{msg} completions." \
"They are stored in {var}\$SKIPPED_COMPS{msg} array."
}
}
if (( ZSH_SUBSHELL )) {
builtin print -rl -- $INSTALLED_COMPS >! ${TMPDIR:-/tmp}/zinit.installed_comps.$$.lst
builtin print -rl -- $SKIPPED_COMPS >! ${TMPDIR:-/tmp}/zinit.skipped_comps.$$.lst
}
.zinit-compinit 1 1 &>/dev/null
} # ]]]
# FUNCTION: .zinit-compinit [[[
# User-exposed `compinit' frontend which first ensures that all
# completions managed by Zinit are forgotten by Zshell. After
# that it runs normal `compinit', which should more easily detect
# Zinit's completions.
#
# No arguments.
.zinit-compinit() {
[[ -n ${OPTS[opt_-p,--parallel]} && $1 != 1 ]] && return
emulate -LR zsh
builtin setopt nullglob extendedglob warncreateglobal typesetsilent
integer use_C=$2
typeset -a symlinked backup_comps
local c cfile bkpfile action
symlinked=( "${ZINIT[COMPLETIONS_DIR]}"/_[^_.]*~*.zwc )
backup_comps=( "${ZINIT[COMPLETIONS_DIR]}"/[^_.]*~*.zwc )
# Delete completions if they are really there, either
# as completions (_fname) or backups (fname)
for c in "${symlinked[@]}" "${backup_comps[@]}"; do
action=0
cfile="${c:t}"
cfile="_${cfile#_}"
bkpfile="${cfile#_}"
#print -Pr "${ZINIT[col-info]}Processing completion $cfile%f%b"
.zinit-forget-completion "$cfile"
done
+zinit-message "Initializing completion ({func}compinit{rst}){…}"
command rm -f ${ZINIT[ZCOMPDUMP_PATH]:-${ZDOTDIR:-$HOME}/.zcompdump}
# Workaround for a nasty trick in _vim
(( ${+functions[_vim_files]} )) && unfunction _vim_files
builtin autoload -Uz compinit
compinit ${${(M)use_C:#1}:+-C} -d ${ZINIT[ZCOMPDUMP_PATH]:-${ZDOTDIR:-$HOME}/.zcompdump} "${(Q@)${(z@)ZINIT[COMPINIT_OPTS]}}"
} # ]]]
# FUNCTION: .zinit-download-file-stdout [[[
# Downloads file to stdout. Supports following backend commands:
# curl, wget, lftp, lynx. Used by snippet loading.
.zinit-download-file-stdout() {
local url="$1" restart="$2" progress="${(M)3:#1}"
emulate -LR zsh
setopt localtraps extendedglob
if (( restart )) {
(( ${path[(I)/usr/local/bin]} )) || \
{
path+=( "/usr/local/bin" );
trap "path[-1]=()" EXIT
}
if (( ${+commands[curl]} )); then
if [[ -n $progress ]]; then
command curl --progress-bar -fSL "$url" 2> >($ZINIT[BIN_DIR]/share/single-line.zsh >&2) || return 1
else
command curl -fsSL "$url" || return 1
fi
elif (( ${+commands[wget]} )); then
command wget ${${progress:--q}:#1} "$url" -O - || return 1
elif (( ${+commands[lftp]} )); then
command lftp -c "cat $url" || return 1
elif (( ${+commands[lynx]} )); then
command lynx -source "$url" || return 1
else
+zinit-message "{u-warn}ERROR{b-warn}:{rst}No download tool detected" \
"(one of: {cmd}curl{rst}, {cmd}wget{rst}, {cmd}lftp{rst}," \
"{cmd}lynx{rst})."
return 2
fi
} else {
if type curl 2>/dev/null 1>&2; then
if [[ -n $progress ]]; then
command curl --progress-bar -fSL "$url" 2> >($ZINIT[BIN_DIR]/share/single-line.zsh >&2) || return 1
else
command curl -fsSL "$url" || return 1
fi
elif type wget 2>/dev/null 1>&2; then
command wget ${${progress:--q}:#1} "$url" -O - || return 1
elif type lftp 2>/dev/null 1>&2; then
command lftp -c "cat $url" || return 1
else
.zinit-download-file-stdout "$url" "1" "$progress"
return $?
fi
}
return 0
} # ]]]
# FUNCTION: .zinit-get-url-mtime [[[
# For the given URL returns the date in the Last-Modified
# header as a time stamp
.zinit-get-url-mtime() {
local url="$1" IFS line header
local -a cmd
setopt localoptions localtraps
(( !${path[(I)/usr/local/bin]} )) && \
{
path+=( "/usr/local/bin" );
trap "path[-1]=()" EXIT
}
if (( ${+commands[curl]} )) || type curl 2>/dev/null 1>&2; then
cmd=(command curl -sIL "$url")
elif (( ${+commands[wget]} )) || type wget 2>/dev/null 1>&2; then
cmd=(command wget --server-response --spider -q "$url" -O -)
else
REPLY=$(( $(date +"%s") ))
return 2
fi
"${cmd[@]}" |& command grep -i Last-Modified: | while read -r line; do
header="${${line#*, }//$'\r'}"
done
if [[ -z $header ]] {
REPLY=$(( $(date +"%s") ))
return 3
}
LANG=C TZ=UTC strftime -r -s REPLY "%d %b %Y %H:%M:%S GMT" "$header" &>/dev/null || {
REPLY=$(( $(date +"%s") ))
return 4
}
return 0
} # ]]]
# FUNCTION: .zinit-mirror-using-svn [[[
# Used to clone subdirectories from Github. If in update mode
# (see $2), then invokes `svn update', in normal mode invokes
# `svn checkout --non-interactive -q <URL>'. In test mode only
# compares remote and local revision and outputs true if update
# is needed.
#
# $1 - URL
# $2 - mode, "" - normal, "-u" - update, "-t" - test
# $3 - subdirectory (not path) with working copy, needed for -t and -u
.zinit-mirror-using-svn() {
setopt localoptions extendedglob warncreateglobal
local url="$1" update="$2" directory="$3"
(( ${+commands[svn]} )) || \
builtin print -Pr -- "${ZINIT[col-error]}Warning:%f%b Subversion not found" \
", please install it to use \`${ZINIT[col-obj]}svn%f%b' ice."
if [[ "$update" = "-t" ]]; then
(
() { setopt localoptions noautopushd; builtin cd -q "$directory"; }
local -a out1 out2
out1=( "${(f@)"$(LANG=C svn info -r HEAD)"}" )
out2=( "${(f@)"$(LANG=C svn info)"}" )
out1=( "${(M)out1[@]:#Revision:*}" )
out2=( "${(M)out2[@]:#Revision:*}" )
[[ "${out1[1]##[^0-9]##}" != "${out2[1]##[^0-9]##}" ]] && return 0
return 1
)
return $?
fi
if [[ "$update" = "-u" && -d "$directory" && -d "$directory/.svn" ]]; then
( () { setopt localoptions noautopushd; builtin cd -q "$directory"; }
command svn update
return $? )
else
command svn checkout --non-interactive -q "$url" "$directory"
fi
return $?
}
# ]]]
# FUNCTION: .zinit-forget-completion [[[
# Implements alternation of Zsh state so that already initialized
# completion stops being visible to Zsh.
#
# $1 - completion function name, e.g. "_cp"; can also be "cp"
.zinit-forget-completion() {
emulate -LR zsh
setopt extendedglob typesetsilent warncreateglobal
local f="$1" quiet="$2"
typeset -a commands
commands=( ${(k)_comps[(Re)$f]} )
[[ "${#commands}" -gt 0 ]] && (( quiet == 0 )) && builtin print -Prn "Forgetting commands completed by \`${ZINIT[col-obj]}$f%f%b': "
local k
integer first=1
for k ( $commands ) {
unset "_comps[$k]"
(( quiet )) || builtin print -Prn "${${first:#1}:+, }${ZINIT[col-info]}$k%f%b"
first=0
}
(( quiet || first )) || builtin print
unfunction -- 2>/dev/null "$f"
} # ]]]
# 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() {
builtin emulate -LR zsh
builtin 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[pick]} != /dev/null && ${ICE[as]} != null && \
${+ICE[null]} -eq 0 && ${ICE[as]} != command && ${+ICE[binary]} -eq 0 && \
( ${+ICE[nocompile]} = 0 || ${ICE[nocompile]} = \! )
]] {
reply=()
if [[ -n ${ICE[pick]} ]]; then
list=( ${~${(M)ICE[pick]:#/*}:-$plugin_dir/$ICE[pick]}(DN) )
if [[ ${#list} -eq 0 ]] {
builtin print "No files for compilation found (pick-ice didn't match)."
return 1
}
reply=( "${list[1]:h}" "${list[1]}" )
else
if (( is_snippet )) {
if [[ -f $plugin_dir/$filename ]] {
reply=( "$plugin_dir" $plugin_dir/$filename )
} elif { ! .zinit-first % "$plugin_dir" } {
+zinit-message "No files for compilation found."
return 1
}
} else {
.zinit-first "$1" "$2" || {
+zinit-message "No files for compilation found."
return 1
}
}
fi
local pdir_path=${reply[-2]}
first=${reply[-1]}
local fname=${first#$pdir_path/}
+zinit-message -n "{note}Note:{rst} Compiling{ehi}:{rst} {b}{file}$fname{rst}{…}"
if [[ -z ${ICE[(i)(\!|)(sh|bash|ksh|csh)]} ]] {
() {
builtin emulate -LR zsh -o extendedglob
if { ! zcompile -U "$first" } {
+zinit-message "{msg2}Warning:{rst} Compilation failed. Don't worry, the plugin will work also without compilation."
+zinit-message "{msg2}Warning:{rst} Consider submitting an error report to Zinit or to the plugin's author."
} else {
+zinit-message " {ok}OK{rst}."
}
# Try to catch possible additional file
zcompile -U "${${first%.plugin.zsh}%.zsh-theme}.zsh" 2>/dev/null
}
}
}
if [[ -n "${ICE[compile]}" ]]; then
local -a pats
pats=( ${(s.;.)ICE[compile]} )
local pat
list=()
for pat ( $pats ) {
eval "list+=( \$plugin_dir/$~pat(N) )"
}
if [[ ${#list} -eq 0 ]] {
+zinit-message "{u-warn}Warning{b-warn}:{rst} ice {ice}compile{apo}''{rst} didn't match any files."
} else {
integer retval
for first in $list; do
() {
builtin emulate -LR zsh -o extendedglob
zcompile -U "$first"; retval+=$?
}
done
builtin print -rl -- ${list[@]#$plugin_dir/} >! ${TMPDIR:-/tmp}/zinit.compiled.$$.lst
if (( retval )) {
+zinit-message "{note}Note:{rst} The additional {num}${#list}{rst} compiled files" \
"are listed in the {var}\$ADD_COMPILED{rst} array (operation exit" \
"code: {ehi}$retval{rst})."
} else {
+zinit-message "{note}Note:{rst} The additional {num}${#list}{rst} compiled files" \
"are listed in the {var}\$ADD_COMPILED{rst} array."
}
}
fi
return 0
} # ]]]
# FUNCTION: .zinit-download-snippet [[[
# Downloads snippet – either a file – with curl, wget, lftp or lynx,
# or a directory, with Subversion – when svn-ICE is active. Github
# supports Subversion protocol and allows to clone subdirectories.
# This is used to provide a layer of support for Oh-My-Zsh and Prezto.
.zinit-download-snippet() {
emulate -LR zsh
setopt extendedglob warncreateglobal typesetsilent
local save_url=$1 url=$2 id_as=$3 local_dir=$4 dirname=$5 filename=$6 update=$7
trap "command rmdir ${(qqq)local_dir}/${(qqq)dirname} 2>/dev/null; return 1;" INT TERM QUIT HUP
local -a list arr
integer retval
local teleid_clean=${ICE[teleid]%%\?*}
[[ $teleid_clean == *://* ]] && \
local sname=${(M)teleid_clean##*://[^/]##(/[^/]##)(#c0,4)} || \
local sname=${${teleid_clean:h}:t}/${teleid_clean:t}
[[ $sname = */trunk* ]] && sname=${${ICE[teleid]%%/trunk*}:t}/${ICE[teleid]:t}
sname=${sname#./}
if (( ${+ICE[svn]} )) {
[[ $url = *(${(~kj.|.)${(Mk)ZINIT_1MAP:#OMZ*}}|robbyrussell*oh-my-zsh|ohmyzsh/ohmyzsh)* ]] && local ZSH=${ZINIT[SNIPPETS_DIR]}
url=${url/(#s)(#m)(${(~kj.|.)ZINIT_1MAP})/$ZINIT_1MAP[$MATCH]}
} else {
url=${url/(#s)(#m)(${(~kj.|.)ZINIT_2MAP})/$ZINIT_2MAP[$MATCH]}
if [[ $save_url == (${(~kj.|.)${(Mk)ZINIT_1MAP:#OMZ*}})* ]] {
if [[ $url != *.zsh(|-theme) && $url != */_[^/]## ]] {
if [[ $save_url == OMZT::* ]] {
url+=.zsh-theme
} else {
url+=/${${url#*::}:t}.plugin.zsh
}
}
} elif [[ $save_url = (${(~kj.|.)${(kM)ZINIT_1MAP:#PZT*}})* ]] {
if [[ $url != *.zsh && $url != */_[^/]## ]] {
url+=/init.zsh
}
}
}
# Change the url to point to raw github content if it isn't like that
if [[ "$url" = *github.com* && ! "$url" = */raw/* && "${+ICE[svn]}" = "0" ]] {
url="${${url/\/blob\///raw/}/\/tree\///raw/}"
}
command rm -f ${TMPDIR:-/tmp}/zinit-execs.$$.lst ${TMPDIR:-/tmp}/zinit.installed_comps.$$.lst \
${TMPDIR:-/tmp}/zinit.skipped_comps.$$.lst ${TMPDIR:-/tmp}/zinit.compiled.$$.lst
if [[ ! -d $local_dir/$dirname ]]; then
local id_msg_part="{…} (at label{ehi}:{rst} {id-as}$id_as{rst})"
[[ $update != -u ]] && +zinit-message "{nl}{info}Setting up snippet:" \
"{url}$sname{rst}${ICE[id-as]:+$id_msg_part}"
command mkdir -p "$local_dir"
fi
if [[ $update = -u && ${OPTS[opt_-q,--quiet]} != 1 ]]; then
local id_msg_part="{…} (identified as{ehi}:{rst} {id-as}$id_as{rst})"
+zinit-message "{nl}{info2}Updating snippet: {url}$sname{rst}${ICE[id-as]:+$id_msg_part}"
fi
# A flag for the annexes. 0 – no new commits, 1 - run-atpull mode,
# 2 – full update/there are new commits to download, 3 - full but
# a forced download (i.e.: the medium doesn't allow to peek update)
#
# The below inherits the flag if it's an update call (i.e.: -u given),
# otherwise it sets it to 2 – a new download is treated like a full
# update.
ZINIT[annex-multi-flag:pull-active]=${${${(M)update:#-u}:+${ZINIT[annex-multi-flag:pull-active]}}:-2}
(
if [[ $url = (http|https|ftp|ftps|scp)://* ]] {
# URL
(
() { setopt localoptions noautopushd; builtin cd -q "$local_dir"; } || return 4
(( !OPTS[opt_-q,--quiet] )) && +zinit-message "Downloading {apo}\`{url}$sname{apo}\`{rst}${${ICE[svn]+" (with Subversion)"}:-" (with curl, wget, lftp)"}{…}"
if (( ${+ICE[svn]} )) {
if [[ $update = -u ]] {
# Test if update available
if ! .zinit-mirror-using-svn "$url" "-t" "$dirname"; then