forked from holman/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 1
/
antigen.zsh
2057 lines (1791 loc) · 57.1 KB
/
antigen.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
######################################################################
# This file was autogenerated by `make`. Do not edit it directly!
######################################################################
# Antigen: A simple plugin manager for zsh
# Authors: Shrikant Sharat Kandula
# and Contributors <https://github.com/zsh-users/antigen/contributors>
# Homepage: http://antigen.sharats.me
# License: MIT License <mitl.sharats.me>
zmodload zsh/parameter
autoload -U is-at-least
# While boot.zsh is part of the ext/cache functionallity it may be disabled
# with ANTIGEN_CACHE flag, and it's always compiled with antigen.zsh
if [[ $ANTIGEN_CACHE != false ]]; then
ANTIGEN_CACHE="${ANTIGEN_CACHE:-${ADOTDIR:-$HOME/.antigen}/init.zsh}"
ANTIGEN_RSRC="${ANTIGEN_RSRC:-${ADOTDIR:-$HOME/.antigen}/.resources}"
# It may not be necessary to check ANTIGEN_AUTO_CONFIG.
if [[ $ANTIGEN_AUTO_CONFIG != false && -f $ANTIGEN_RSRC ]]; then
# Check the list of files for configuration changes (uses -nt comp)
ANTIGEN_CHECK_FILES=$(cat $ANTIGEN_RSRC 2> /dev/null)
ANTIGEN_CHECK_FILES=(${(@f)ANTIGEN_CHECK_FILES})
for config in $ANTIGEN_CHECK_FILES; do
if [[ "$config" -nt "$config.zwc" ]]; then
# Flag configuration file as newer
{ zcompile "$config" } &!
# Kill cache file in order to force full loading (see a few lines below)
[[ -f "$ANTIGEN_CACHE" ]] && rm -f "$ANTIGEN_CACHE"
fi
done
fi
# If there is a cache file do load from it
if [[ -f $ANTIGEN_CACHE && ! $_ANTIGEN_CACHE_LOADED == true ]]; then
# Wrap antigen in order to defer cache source until `antigen-apply`
antigen() {
if [[ $1 == "apply" ]]; then
source "$ANTIGEN_CACHE"
# Handle `antigen-init` command properly
elif [[ $1 == "init" ]]; then
source "$2"
fi
}
# Do not continue loading antigen as cache bundle takes care of it.
return 0
fi
fi
[[ -z "$_ANTIGEN_INSTALL_DIR" ]] && _ANTIGEN_INSTALL_DIR=${0:A:h}
# Each line in this string has the following entries separated by a space
# character.
# <repo-url>, <plugin-location>, <bundle-type>, <has-local-clone>
[[ $_ANTIGEN_CACHE_LOADED != true ]] && typeset -aU _ANTIGEN_BUNDLE_RECORD
# Do not load anything if git is not available.
if (( ! $+commands[git] )); then
echo 'Antigen: Please install git to use Antigen.' >&2
return 1
fi
# Used to defer compinit/compdef
typeset -a __deferred_compdefs
compdef () { __deferred_compdefs=($__deferred_compdefs "$*") }
# A syntax sugar to avoid the `-` when calling antigen commands. With this
# function, you can write `antigen-bundle` as `antigen bundle` and so on.
antigen () {
local cmd="$1"
if [[ -z "$cmd" ]]; then
echo 'Antigen: Please give a command to run.' >&2
return 1
fi
shift
if (( $+functions[antigen-$cmd] )); then
"antigen-$cmd" "$@"
return $?
else
echo "Antigen: Unknown command: $cmd" >&2
return 1
fi
}
# Returns the bundle's git revision
#
# Usage
# -antigen-bundle-rev bundle-name [is_local_clone]
#
# Returns
# Bundle rev-parse output (branch name or short ref name)
-antigen-bundle-rev () {
local bundle=$1
local is_local_clone=$2
local bundle_path=$bundle
# Get bunde path inside $ADOTDIR if bundle was effectively cloned
if [[ "$is_local_clone" == "true" ]]; then
bundle_path=$(-antigen-get-clone-dir $bundle)
fi
local ref
ref=$(git --git-dir="$bundle_path/.git" rev-parse --abbrev-ref '@' 2>/dev/null)
# Avoid 'HEAD' when in detached mode
if [[ $ref == "HEAD" ]]; then
ref=$(git --git-dir="$bundle_path/.git" describe --tags --exact-match 2>/dev/null \
|| git --git-dir="$bundle_path/.git" rev-parse --short '@' 2>/dev/null || "-")
fi
echo $ref
}
# Usage:
# -antigen-bundle-short-name "https://github.com/user/repo.git[|*]" "[branch/name]"
# Returns:
# user/repo@branch/name
-antigen-bundle-short-name () {
local bundle_name="${1%|*}"
local bundle_branch="$2"
local match mbegin mend MATCH MBEGIN MEND
[[ "$bundle_name" =~ '.*/(.*/.*).*$' ]] && bundle_name=$match[1]
bundle_name="${bundle_name%.git*}"
if [[ -n $bundle_branch ]]; then
bundle_name="$bundle_name@$bundle_branch"
fi
echo $bundle_name
}
# Echo the bundle specs as in the record. The first line is not echoed since it
# is a blank line.
-antigen-echo-record () {
echo ${(j:\n:)_ANTIGEN_BUNDLE_RECORD}
}
# Filters _ANTIGEN_BUNDLE_RECORD for $1
#
# Usage
# -antigen-find-bundle example/bundle
#
# Returns
# String if bundle is found
-antigen-find-bundle () {
echo $(-antigen-find-record $1 | cut -d' ' -f1)
}
# Filters _ANTIGEN_BUNDLE_RECORD for $1
#
# Usage
# -antigen-find-record example/bundle
#
# Returns
# String if record is found
-antigen-find-record () {
local bundle=$1
if [[ $# -eq 0 ]]; then
return 1
fi
local record=${bundle/\|/\\\|}
echo "${_ANTIGEN_BUNDLE_RECORD[(r)*$record*]}"
}
# Returns bundle names from _ANTIGEN_BUNDLE_RECORD
#
# Usage
# -antigen-get-bundles [--short|--simple|--long]
#
# Returns
# List of bundles installed
-antigen-get-bundles () {
local mode revision url bundle_name bundle_entry loc no_local_clone
local record bundle make_local_clone
mode=${1:-"--short"}
for record in $_ANTIGEN_BUNDLE_RECORD; do
bundle=(${(@s/ /)record})
url=$bundle[1]
loc=$bundle[2]
make_local_clone=$bundle[4]
bundle_name=$(-antigen-bundle-short-name $url)
case "$mode" in
--short)
# Only check revision for bundle with a requested branch
if [[ $url == *\|* ]]; then
revision=$(-antigen-bundle-rev $url $make_local_clone)
else
revision="master"
fi
if [[ $loc != '/' ]]; then
bundle_name="$bundle_name ~ $loc"
fi
echo "$bundle_name @ $revision"
;;
--simple)
echo "$bundle_name"
;;
--long)
echo "$record"
;;
esac
done
}
# Usage:
# -antigen-get-clone-dir "https://github.com/zsh-users/zsh-syntax-highlighting.git[|feature/branch]"
# Returns:
# $ANTIGEN_BUNDLES/zsh-users/zsh-syntax-highlighting[-feature-branch]
-antigen-get-clone-dir () {
local bundle="$1"
local url="${bundle%|*}"
local branch match mbegin mend MATCH MBEGIN MEND
[[ "$bundle" =~ "\|" ]] && branch="${bundle#*|}"
# Takes a repo url and mangles it, giving the path that this url will be
# cloned to. Doesn't actually clone anything.
local clone_dir="$ANTIGEN_BUNDLES"
url=$(-antigen-bundle-short-name $url)
# Suffix with branch/tag name
[[ -n "$branch" ]] && url="$url-${branch//\//-}"
url=${url//\*/x}
echo "$clone_dir/$url"
}
# Returns bundles flagged as make_local_clone
#
# Usage
# -antigen-cloned-bundles
#
# Returns
# Bundle metadata
-antigen-get-cloned-bundles() {
-antigen-echo-record |
awk '$4 == "true" {print $1}' |
sort -u
}
# Returns a list of themes from a default library (omz)
#
# Usage
# -antigen-get-themes
#
# Returns
# List of themes by name
-antigen-get-themes () {
local library='robbyrussell/oh-my-zsh'
local bundle=$(-antigen-find-bundle $library)
if [[ -n "$bundle" ]]; then
local dir=$(-antigen-get-clone-dir $ANTIGEN_DEFAULT_REPO_URL)
echo $(ls $dir/themes/ | grep '.zsh-theme$' | sed 's/.zsh-theme//')
fi
return 0
}
# This function check ZSH_EVAL_CONTEXT to determine if running in interactive shell.
#
# Usage
# -antigen-interactive-mode
#
# Returns
# Either true or false depending if we are running in interactive mode
-antigen-interactive-mode () {
WARN "-antigen-interactive-mode: $ZSH_EVAL_CONTEXT \$_ANTIGEN_INTERACTIVE = $_ANTIGEN_INTERACTIVE"
if [[ $_ANTIGEN_INTERACTIVE != "" ]]; then
[[ $_ANTIGEN_INTERACTIVE == true ]];
return
fi
[[ "$ZSH_EVAL_CONTEXT" == toplevel* || "$ZSH_EVAL_CONTEXT" == cmdarg* ]];
}
# Parses and retrieves a remote branch given a branch name.
#
# If the branch name contains '*' it will retrieve remote branches
# and try to match against tags and heads, returning the latest matching.
#
# Usage
# -antigen-parse-branch https://github.com/user/repo.git x.y.z
#
# Returns
# Branch name
-antigen-parse-branch () {
local url="$1" branch="$2" branches
local match mbegin mend MATCH MBEGIN MEND
if [[ "$branch" =~ '\*' ]]; then
branches=$(git ls-remote --tags -q "$url" "$branch"|cut -d'/' -f3|sort -n|tail -1)
# There is no --refs flag in git 1.8 and below, this way we
# emulate this flag -- also git 1.8 ref order is undefined.
branch=${${branches#*/*/}%^*} # Why you are like this?
fi
echo $branch
}
-antigen-update-repos () {
local repo bundle url target
local log=/tmp/antigen-v2-migrate.log
echo "It seems you have bundles cloned with Antigen v1.x."
echo "We'll try to convert directory structure to v2."
echo
echo -n "Moving bundles to '\$ADOTDIR/bundles'... "
# Migrate old repos -> bundles
local errors=0
for repo in $ADOTDIR/repos/*; do
bundle=${repo/$ADOTDIR\/repos\//}
bundle=${bundle//-SLASH-/\/}
bundle=${bundle//-COLON-/\:}
bundle=${bundle//-STAR-/\*}
url=${bundle//-PIPE-/\|}
target=$(-antigen-get-clone-dir $url)
mkdir -p "${target:A:h}"
echo " ---> ${repo/$ADOTDIR\/} -> ${target/$ADOTDIR\/}" | tee > $log
mv "$repo" "$target" &> $log
if [[ $? != 0 ]]; then
echo "Failed to migrate '$repo'!."
errors+=1
fi
done
if [[ $errors == 0 ]]; then
echo "Done."
else
echo "An error ocurred!"
fi
echo
if [[ "$(ls -A $ADOTDIR/repos | wc -l | xargs)" == 0 ]]; then
echo "You can safely remove \$ADOTDIR/repos."
else
echo "Some bundles couldn't be migrated. See \$ADOTDIR/repos."
fi
echo
if [[ $errors == 0 ]]; then
echo "Bundles migrated successfuly."
rm $log
else
echo "Some errors occured. Review migration log in '$log'."
fi
antigen-reset
}
# Ensure that a clone exists for the given repo url and branch. If the first
# argument is `update` and if a clone already exists for the given repo
# and branch, it is pull-ed, i.e., updated.
#
# This function expects three arguments in order:
# - 'url=<url>'
# - 'update=true|false'
# - 'verbose=true|false'
#
# Returns true|false Whether cloning/pulling was succesful
-antigen-ensure-repo () {
# Argument defaults. Previously using ${1:?"missing url argument"} format
# but it seems to mess up with cram
if (( $# < 1 )); then
echo "Antigen: Missing url argument."
return 1
fi
# The url. No sane default for this, so just empty.
local url=$1
# Check if we have to update.
local update=${2:-false}
# Verbose output.
local verbose=${3:-false}
shift $#
# Get the clone's directory as per the given repo url and branch.
local clone_dir=$(-antigen-get-clone-dir $url)
if [[ -d "$clone_dir" && $update == false ]]; then
return true
fi
# A temporary function wrapping the `git` command with repeated arguments.
--plugin-git () {
(\cd -q "$clone_dir" && eval ${ANTIGEN_CLONE_ENV} git --git-dir="$clone_dir/.git" --no-pager "$@" &>>! $ANTIGEN_LOG)
}
local success=false
# If its a specific branch that we want, checkout that branch.
local branch="master" # TODO FIX THIS
if [[ $url == *\|* ]]; then
branch="$(-antigen-parse-branch ${url%|*} ${url#*|})"
fi
if [[ ! -d $clone_dir ]]; then
eval ${ANTIGEN_CLONE_ENV} git clone ${=ANTIGEN_CLONE_OPTS} --branch "$branch" -- "${url%|*}" "$clone_dir" &>> $ANTIGEN_LOG
success=$?
elif $update; then
# Save current revision.
local old_rev="$(--plugin-git rev-parse HEAD)"
# Pull changes if update requested.
--plugin-git checkout "$branch"
--plugin-git pull origin "$branch"
success=$?
# Update submodules.
--plugin-git submodule update ${=ANTIGEN_SUBMODULE_OPTS}
# Get the new revision.
local new_rev="$(--plugin-git rev-parse HEAD)"
fi
if [[ -n $old_rev && $old_rev != $new_rev ]]; then
echo Updated from $old_rev[0,7] to $new_rev[0,7].
if $verbose; then
--plugin-git log --oneline --reverse --no-merges --stat '@{1}..'
fi
fi
# Remove the temporary git wrapper function.
unfunction -- --plugin-git
return $success
}
# Helper function: Same as `$1=$2`, but will only happen if the name
# specified by `$1` is not already set.
-antigen-set-default () {
local arg_name="$1"
local arg_value="$2"
eval "test -z \"\$$arg_name\" && typeset -g $arg_name='$arg_value'"
}
-antigen-env-setup () {
typeset -gU fpath path
# Pre-startup initializations.
-antigen-set-default ANTIGEN_OMZ_REPO_URL \
https://github.com/robbyrussell/oh-my-zsh.git
-antigen-set-default ANTIGEN_PREZTO_REPO_URL \
https://github.com/sorin-ionescu/prezto.git
-antigen-set-default ANTIGEN_DEFAULT_REPO_URL $ANTIGEN_OMZ_REPO_URL
# Default Antigen directory.
-antigen-set-default ADOTDIR $HOME/.antigen
[[ ! -d $ADOTDIR ]] && mkdir -p $ADOTDIR
# Defaults bundles directory.
-antigen-set-default ANTIGEN_BUNDLES $ADOTDIR/bundles
# If there is no bundles directory, create it.
if [[ ! -d $ANTIGEN_BUNDLES ]]; then
mkdir -p $ANTIGEN_BUNDLES
# Check for v1 repos directory, transform it to v2 format.
[[ -d $ADOTDIR/repos ]] && -antigen-update-repos
fi
-antigen-set-default ANTIGEN_COMPDUMP "${ADOTDIR:-$HOME}/.zcompdump"
-antigen-set-default ANTIGEN_LOG /dev/null
# CLONE_OPTS uses ${=CLONE_OPTS} expansion so don't use spaces
# for arguments that can be passed as `--key=value`.
-antigen-set-default ANTIGEN_CLONE_ENV "GIT_TERMINAL_PROMPT=0"
-antigen-set-default ANTIGEN_CLONE_OPTS "--single-branch --recursive --depth=1"
-antigen-set-default ANTIGEN_SUBMODULE_OPTS "--recursive --depth=1"
# Complain when a bundle is already installed.
-antigen-set-default _ANTIGEN_WARN_DUPLICATES true
# Compatibility with oh-my-zsh themes.
-antigen-set-default _ANTIGEN_THEME_COMPAT true
# Add default built-in extensions to load at start up
-antigen-set-default _ANTIGEN_BUILTIN_EXTENSIONS 'lock parallel defer cache'
# Setup antigen's own completion.
if -antigen-interactive-mode; then
TRACE "Gonna create compdump file @ env-setup" COMPDUMP
autoload -Uz compinit
compinit -d "$ANTIGEN_COMPDUMP"
compdef _antigen antigen
else
(( $+functions[antigen-ext-init] )) && antigen-ext-init
fi
}
# Load a given bundle by sourcing it.
#
# The function also modifies fpath to add the bundle path.
#
# Usage
# -antigen-load "bundle-url" ["location"] ["make_local_clone"] ["btype"]
#
# Returns
# Integer. 0 if success 1 if an error ocurred.
-antigen-load () {
local bundle list
typeset -A bundle; bundle=($@)
typeset -Ua list; list=()
local location="${bundle[dir]}/${bundle[loc]}"
# Prioritize location when given.
if [[ -f "${location}" ]]; then
list=(${location})
else
# Directory locations must be suffixed with slash
location="$location/"
# Prioritize theme with antigen-theme
if [[ ${bundle[btype]} == "theme" ]]; then
list=(${location}*.zsh-theme(N[1]))
fi
# Common frameworks
if [[ $#list == 0 ]]; then
# dot-plugin, init and functions support (omz, prezto)
# Support prezto function loading. See https://github.com/zsh-users/antigen/pull/428
list=(${location}*.plugin.zsh(N[1]) ${location}init.zsh(N[1]) ${location}/functions(N[1]))
fi
# Default to zsh and sh
if [[ $#list == 0 ]]; then
list=(${location}*.zsh(N) ${location}*.sh(N))
fi
fi
-antigen-load-env ${(kv)bundle}
# If there is any sourceable try to load it
if ! -antigen-load-source "${list[@]}" && [[ ! -d ${location} ]]; then
return 1
fi
return 0
}
-antigen-load-env () {
typeset -A bundle; bundle=($@)
local location=${bundle[dir]}/${bundle[loc]}
# Load to path if there is no sourceable
if [[ -d ${location} ]]; then
PATH="$PATH:${location:A}"
fpath+=("${location:A}")
return
fi
PATH="$PATH:${location:A:h}"
fpath+=("${location:A:h}")
}
-antigen-load-source () {
typeset -a list
list=($@)
local src match mbegin mend MATCH MBEGIN MEND
# Return error when we're given an empty list
if [[ $#list == 0 ]]; then
return 1
fi
# Using a for rather than `source $list` as we need to check for zsh-themes
# In order to create antigen-compat file. This is only needed for interactive-mode
# theme switching, for static loading (cache) there is no need.
for src in $list; do
if [[ $_ANTIGEN_THEME_COMPAT == true && -f "$src" && "$src" == *.zsh-theme* ]]; then
local compat="${src:A}.antigen-compat"
echo "# Generated by Antigen. Do not edit!" >! "$compat"
cat $src | sed -Ee '/\{$/,/^\}/!{
s/^local //
}' >>! "$compat"
src="$compat"
fi
if ! source "$src" 2>/dev/null; then
return 1
fi
done
}
# Usage:
# -antigen-parse-args output_assoc_arr <args...>
-antigen-parse-args () {
local argkey key value index=0 args
local match mbegin mend MATCH MBEGIN MEND
local var=$1
shift
# Bundle spec arguments' default values.
#setopt XTRACE VERBOSE
builtin typeset -A args
args[url]="$ANTIGEN_DEFAULT_REPO_URL"
#unsetopt XTRACE VERBOSE
args[loc]=/
args[make_local_clone]=true
args[btype]=plugin
#args[branch]= # commented out as it may cause assoc array kv mismatch
while [[ $# -gt 0 ]]; do
argkey="${1%\=*}"
key="${argkey//--/}"
value="${1#*=}"
case "$argkey" in
--url|--loc|--branch|--btype)
if [[ "$value" == "$argkey" ]]; then
printf "Required argument for '%s' not provided.\n" $key >&2
else
args[$key]="$value"
fi
;;
--no-local-clone)
args[make_local_clone]=false
;;
--*)
printf "Unknown argument '%s'.\n" $key >&2
;;
*)
value=$key
case $index in
0)
key=url
local domain=""
local url_path=$value
# Full url with protocol or ssh github url (github.com:org/repo)
if [[ "$value" =~ "://" || "$value" =~ ":" ]]; then
if [[ "$value" =~ [@.][^/:]+[:]?[0-9]*[:/]?(.*)@?$ ]]; then
url_path=$match[1]
domain=${value/$url_path/}
fi
fi
if [[ "$url_path" =~ '@' ]]; then
args[branch]="${url_path#*@}"
value="$domain${url_path%@*}"
else
value="$domain$url_path"
fi
;;
1) key=loc ;;
esac
let index+=1
args[$key]="$value"
;;
esac
shift
done
# Check if url is just the plugin name. Super short syntax.
if [[ "${args[url]}" != */* ]]; then
case "$ANTIGEN_DEFAULT_REPO_URL" in
"$ANTIGEN_OMZ_REPO_URL")
args[loc]="plugins/${args[url]}"
;;
"$ANTIGEN_PREZTO_REPO_URL")
args[loc]="modules/${args[url]}"
;;
*)
args[loc]="${args[url]}"
;;
esac
args[url]="$ANTIGEN_DEFAULT_REPO_URL"
fi
# Resolve the url.
# Expand short github url syntax: `username/reponame`.
local url="${args[url]}"
if [[ $url != git://* &&
$url != https://* &&
$url != http://* &&
$url != ssh://* &&
$url != /* &&
$url != *github.com:*/*
]]; then
url="https://github.com/${url%.git}.git"
fi
args[url]="$url"
# Ignore local clone if url given is not a git directory
if [[ ${args[url]} == /* && ! -d ${args[url]}/.git ]]; then
args[make_local_clone]=false
fi
# Add the branch information to the url if we need to create a local clone.
# Format url in bundle-metadata format: url[|branch]
if [[ ! -z "${args[branch]}" && ${args[make_local_clone]} == true ]]; then
args[url]="${args[url]}|${args[branch]}"
fi
# Add the theme extension to `loc`, if this is a theme, but only
# if it's especified, ie, --loc=theme-name, in case when it's not
# specified antige-load-list will look for *.zsh-theme files
if [[ ${args[btype]} == "theme" &&
${args[loc]} != "/" && ${args[loc]} != *.zsh-theme ]]; then
args[loc]="${args[loc]}.zsh-theme"
fi
local name="${args[url]%|*}"
local branch="${args[branch]}"
# Extract bundle name.
if [[ "$name" =~ '.*/(.*/.*).*$' ]]; then
name="${match[1]}"
fi
name="${name%.git*}"
# Format bundle name with optional branch.
if [[ -n "${branch}" ]]; then
args[name]="${name}@${branch}"
else
args[name]="${name}"
fi
# Format bundle path.
if [[ ${args[make_local_clone]} == true ]]; then
local bpath="$name"
# Suffix with branch/tag name
if [[ -n "$branch" ]]; then
# bpath is in the form of repo/name@version => repo/name-version
# Replace / with - in bundle branch.
local bbranch=${branch//\//-}
# If branch/tag is semver-like do replace * by x.
bbranch=${bbranch//\*/x}
bpath="${name}-${bbranch}"
fi
bpath="$ANTIGEN_BUNDLES/$bpath"
args[dir]="${(qq)bpath}"
else
# if it's local then path is just the "url" argument, loc remains the same
args[dir]=${args[url]}
fi
# Escape url and branch (may contain semver-like and pipe characters)
args[url]="${(qq)args[url]}"
if [[ -n "${args[branch]}" ]]; then
args[branch]="${(qq)args[branch]}"
fi
# Escape bundle name (may contain semver-like characters)
args[name]="${(qq)args[name]}"
eval "${var}=(${(kv)args})"
return 0
}
# Updates revert-info data with git hash.
#
# This does process only cloned bundles.
#
# Usage
# -antigen-revert-info
#
# Returns
# Nothing. Generates/updates $ADOTDIR/revert-info.
-antigen-revert-info() {
local url
# Update your bundles, i.e., `git pull` in all the plugin repos.
date >! $ADOTDIR/revert-info
-antigen-get-cloned-bundles | while read url; do
local clone_dir="$(-antigen-get-clone-dir "$url")"
if [[ -d "$clone_dir" ]]; then
(echo -n "$clone_dir:"
\cd -q "$clone_dir"
git rev-parse HEAD) >> $ADOTDIR/revert-info
fi
done
}
-antigen-use-oh-my-zsh () {
typeset -g ZSH ZSH_CACHE_DIR
ANTIGEN_DEFAULT_REPO_URL=$ANTIGEN_OMZ_REPO_URL
if [[ -z "$ZSH" ]]; then
ZSH="$(-antigen-get-clone-dir "$ANTIGEN_DEFAULT_REPO_URL")"
fi
if [[ -z "$ZSH_CACHE_DIR" ]]; then
ZSH_CACHE_DIR="$ZSH/cache/"
fi
antigen-bundle --loc=lib
}
-antigen-use-prezto () {
ANTIGEN_DEFAULT_REPO_URL=$ANTIGEN_PREZTO_REPO_URL
antigen-bundle "$ANTIGEN_PREZTO_REPO_URL"
}
# Initialize completion
antigen-apply () {
LOG "Called antigen-apply"
# Load the compinit module. This will readefine the `compdef` function to
# the one that actually initializes completions.
TRACE "Gonna create compdump file @ apply" COMPDUMP
autoload -Uz compinit
compinit -d "$ANTIGEN_COMPDUMP"
# Apply all `compinit`s that have been deferred.
local cdef
for cdef in "${__deferred_compdefs[@]}"; do
compdef "$cdef"
done
{ zcompile "$ANTIGEN_COMPDUMP" } &!
unset __deferred_compdefs
}
# Syntaxes
# antigen-bundle <url> [<loc>=/]
# Keyword only arguments:
# branch - The branch of the repo to use for this bundle.
antigen-bundle () {
TRACE "Called antigen-bundle with $@" BUNDLE
if [[ -z "$1" ]]; then
printf "Antigen: Must provide a bundle url or name.\n" >&2
return 1
fi
builtin typeset -A bundle; -antigen-parse-args 'bundle' ${=@}
if [[ -z ${bundle[btype]} ]]; then
bundle[btype]=bundle
fi
local record="${bundle[url]} ${bundle[loc]} ${bundle[btype]} ${bundle[make_local_clone]}"
if [[ $_ANTIGEN_WARN_DUPLICATES == true && ! ${_ANTIGEN_BUNDLE_RECORD[(I)$record]} == 0 ]]; then
printf "Seems %s is already installed!\n" ${bundle[name]}
return 1
fi
# Clone bundle if we haven't done do already.
if [[ ! -d "${bundle[dir]}" ]]; then
if ! -antigen-bundle-install ${(kv)bundle}; then
return 1
fi
fi
# Load the plugin.
if ! -antigen-load ${(kv)bundle}; then
TRACE "-antigen-load failed to load ${bundle[name]}" BUNDLE
printf "Antigen: Failed to load %s.\n" ${bundle[btype]} >&2
return 1
fi
# Only add it to the record if it could be installed and loaded.
_ANTIGEN_BUNDLE_RECORD+=("$record")
}
#
# Usage:
# -antigen-bundle-install <record>
# Returns:
# 1 if it fails to install bundle
-antigen-bundle-install () {
typeset -A bundle; bundle=($@)
# Ensure a clone exists for this repo, if needed.
# Get the clone's directory as per the given repo url and branch.
local bpath="${bundle[dir]}"
# Clone if it doesn't already exist.
local start=$(date +'%s')
printf "Installing %s... " "${bundle[name]}"
if ! -antigen-ensure-repo "${bundle[url]}"; then
# Return immediately if there is an error cloning
TRACE "-antigen-bundle-instal failed to clone ${bundle[url]}" BUNDLE
printf "Error! Activate logging and try again.\n" >&2
return 1
fi
local took=$(( $(date +'%s') - $start ))
printf "Done. Took %ds.\n" $took
}
antigen-bundles () {
# Bulk add many bundles at one go. Empty lines and lines starting with a `#`
# are ignored. Everything else is given to `antigen-bundle` as is, no
# quoting rules applied.
local line
setopt localoptions no_extended_glob # See https://github.com/zsh-users/antigen/issues/456
grep '^[[:space:]]*[^[:space:]#]' | while read line; do
antigen-bundle ${=line%#*}
done
}
# Cleanup unused repositories.
antigen-cleanup () {
local force=false
if [[ $1 == --force ]]; then
force=true
fi
if [[ ! -d "$ANTIGEN_BUNDLES" || -z "$(\ls -A "$ANTIGEN_BUNDLES")" ]]; then
echo "You don't have any bundles."
return 0
fi
# Find directores in ANTIGEN_BUNDLES, that are not in the bundles record.
typeset -a unused_clones clones
local url record clone
for record in $(-antigen-get-cloned-bundles); do
url=${record% /*}
clones+=("$(-antigen-get-clone-dir $url)")
done
for clone in $ANTIGEN_BUNDLES/*/*(/); do
if [[ $clones[(I)$clone] == 0 ]]; then
unused_clones+=($clone)
fi
done
if [[ -z $unused_clones ]]; then
echo "You don't have any unidentified bundles."
return 0
fi
echo 'You have clones for the following repos, but are not used.'
echo "\n${(j:\n:)unused_clones}"
if $force || (echo -n '\nDelete them all? [y/N] '; read -q); then
echo
echo
for clone in $unused_clones; do
echo -n "Deleting clone \"$clone\"..."
\rm -rf "$clone"
echo ' done.'
done
else
echo
echo "Nothing deleted."
fi
}
antigen-help () {
antigen-version
cat <<EOF
Antigen is a plugin management system for zsh. It makes it easy to grab awesome
shell scripts and utilities, put up on Github.
Usage: antigen <command> [args]
Commands:
apply Must be called in the zshrc after all calls to 'antigen bundle'.
bundle Install and load a plugin.
cache-gen Generate Antigen's cache with currently loaded bundles.
cleanup Remove clones of repos not used by any loaded plugins.
init Use caching to quickly load bundles.
list List currently loaded plugins.
purge Remove a bundle from the filesystem.
reset Clean the generated cache.
restore Restore plugin state from a snapshot file.
revert Revert plugins to their state prior to the last time 'antigen
update' was run.
selfupdate Update antigen.
snapshot Create a snapshot of all active plugin repos and save it to a
snapshot file.
update Update plugins.
use Load a supported zsh pre-packaged framework.
For further details and complete documentation, visit the project's page at
'http://antigen.sharats.me'.
EOF
}
# Antigen command to load antigen configuration
#
# This method is slighlty more performing than using various antigen-* methods.
#
# Usage
# Referencing an antigen configuration file:
#
# antigen-init "/path/to/antigenrc"
#
# or using HEREDOCS:
#
# antigen-init <<EOBUNDLES
# antigen use oh-my-zsh
#
# antigen bundle zsh/bundle
# antigen bundle zsh/example
#
# antigen theme zsh/theme
#
# antigen apply
# EOBUNDLES
#
# Returns
# Nothing
antigen-init () {
local src="$1" line
# If we're given an argument it should be a path to a file
if [[ -n "$src" ]]; then
if [[ -f "$src" ]]; then
source "$src"
return
else
printf "Antigen: invalid argument provided.\n" >&2
return 1
fi
fi
# Otherwise we expect it to be a heredoc
grep '^[[:space:]]*[^[:space:]#]' | while read -r line; do
eval $line