Commit d91610f4695405d34ccc7c6b0a1318f90f8b4536
1 parent
98dc8bb90b
Please shutup!
Showing 1 changed file with 2 additions and 2 deletions Inline Diff
antigen.zsh
1 | # Antigen: A simple plugin manager for zsh | 1 | # Antigen: A simple plugin manager for zsh |
2 | # Authors: Shrikant Sharat Kandula | 2 | # Authors: Shrikant Sharat Kandula |
3 | # and Contributors <https://github.com/zsh-users/antigen/contributors> | 3 | # and Contributors <https://github.com/zsh-users/antigen/contributors> |
4 | # Homepage: http://antigen.sharats.me | 4 | # Homepage: http://antigen.sharats.me |
5 | # License: MIT License <mitl.sharats.me> | 5 | # License: MIT License <mitl.sharats.me> |
6 | 6 | ||
7 | # Each line in this string has the following entries separated by a space | 7 | # Each line in this string has the following entries separated by a space |
8 | # character. | 8 | # character. |
9 | # <repo-url>, <plugin-location>, <bundle-type>, <has-local-clone> | 9 | # <repo-url>, <plugin-location>, <bundle-type>, <has-local-clone> |
10 | # FIXME: Is not kept local by zsh! | 10 | # FIXME: Is not kept local by zsh! |
11 | local _ANTIGEN_BUNDLE_RECORD="" | 11 | local _ANTIGEN_BUNDLE_RECORD="" |
12 | local _ANTIGEN_INSTALL_DIR="$(cd "$(dirname "$0")" && pwd)" | 12 | local _ANTIGEN_INSTALL_DIR="$(cd "$(dirname "$0")" && pwd)" |
13 | 13 | ||
14 | # Used to defer compinit/compdef | 14 | # Used to defer compinit/compdef |
15 | typeset -a __deferred_compdefs | 15 | typeset -a __deferred_compdefs |
16 | compdef () { __deferred_compdefs=($__deferred_compdefs "$*") } | 16 | compdef () { __deferred_compdefs=($__deferred_compdefs "$*") } |
17 | 17 | ||
18 | # Syntaxes | 18 | # Syntaxes |
19 | # antigen-bundle <url> [<loc>=/] | 19 | # antigen-bundle <url> [<loc>=/] |
20 | # Keyword only arguments: | 20 | # Keyword only arguments: |
21 | # branch - The branch of the repo to use for this bundle. | 21 | # branch - The branch of the repo to use for this bundle. |
22 | antigen-bundle () { | 22 | antigen-bundle () { |
23 | 23 | ||
24 | # Bundle spec arguments' default values. | 24 | # Bundle spec arguments' default values. |
25 | local url="$ANTIGEN_DEFAULT_REPO_URL" | 25 | local url="$ANTIGEN_DEFAULT_REPO_URL" |
26 | local loc=/ | 26 | local loc=/ |
27 | local branch= | 27 | local branch= |
28 | local no_local_clone=false | 28 | local no_local_clone=false |
29 | local btype=plugin | 29 | local btype=plugin |
30 | 30 | ||
31 | # Parse the given arguments. (Will overwrite the above values). | 31 | # Parse the given arguments. (Will overwrite the above values). |
32 | eval "$(-antigen-parse-args \ | 32 | eval "$(-antigen-parse-args \ |
33 | 'url?, loc? ; branch:?, no-local-clone?, btype:?' \ | 33 | 'url?, loc? ; branch:?, no-local-clone?, btype:?' \ |
34 | "$@")" | 34 | "$@")" |
35 | 35 | ||
36 | # Check if url is just the plugin name. Super short syntax. | 36 | # Check if url is just the plugin name. Super short syntax. |
37 | if [[ "$url" != */* ]]; then | 37 | if [[ "$url" != */* ]]; then |
38 | loc="plugins/$url" | 38 | loc="plugins/$url" |
39 | url="$ANTIGEN_DEFAULT_REPO_URL" | 39 | url="$ANTIGEN_DEFAULT_REPO_URL" |
40 | fi | 40 | fi |
41 | 41 | ||
42 | # Resolve the url. | 42 | # Resolve the url. |
43 | url="$(-antigen-resolve-bundle-url "$url")" | 43 | url="$(-antigen-resolve-bundle-url "$url")" |
44 | 44 | ||
45 | # Add the branch information to the url. | 45 | # Add the branch information to the url. |
46 | if [[ ! -z $branch ]]; then | 46 | if [[ ! -z $branch ]]; then |
47 | url="$url|$branch" | 47 | url="$url|$branch" |
48 | fi | 48 | fi |
49 | 49 | ||
50 | # The `make_local_clone` variable better represents whether there should be | 50 | # The `make_local_clone` variable better represents whether there should be |
51 | # a local clone made. For cloning to be avoided, firstly, the `$url` should | 51 | # a local clone made. For cloning to be avoided, firstly, the `$url` should |
52 | # be an absolute local path and `$branch` should be empty. In addition to | 52 | # be an absolute local path and `$branch` should be empty. In addition to |
53 | # these two conditions, either the `--no-local-clone` option should be | 53 | # these two conditions, either the `--no-local-clone` option should be |
54 | # given, or `$url` should not a git repo. | 54 | # given, or `$url` should not a git repo. |
55 | local make_local_clone=true | 55 | local make_local_clone=true |
56 | if [[ $url == /* && -z $branch && | 56 | if [[ $url == /* && -z $branch && |
57 | ( $no_local_clone == true || ! -d $url/.git ) ]]; then | 57 | ( $no_local_clone == true || ! -d $url/.git ) ]]; then |
58 | make_local_clone=false | 58 | make_local_clone=false |
59 | fi | 59 | fi |
60 | 60 | ||
61 | # Add the theme extension to `loc`, if this is a theme. | 61 | # Add the theme extension to `loc`, if this is a theme. |
62 | if [[ $btype == theme && $loc != *.zsh-theme ]]; then | 62 | if [[ $btype == theme && $loc != *.zsh-theme ]]; then |
63 | loc="$loc.zsh-theme" | 63 | loc="$loc.zsh-theme" |
64 | fi | 64 | fi |
65 | 65 | ||
66 | # Add it to the record. | 66 | # Add it to the record. |
67 | _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype" | 67 | _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype" |
68 | _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD $make_local_clone" | 68 | _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD $make_local_clone" |
69 | 69 | ||
70 | # Ensure a clone exists for this repo, if needed. | 70 | # Ensure a clone exists for this repo, if needed. |
71 | if $make_local_clone; then | 71 | if $make_local_clone; then |
72 | -antigen-ensure-repo "$url" | 72 | -antigen-ensure-repo "$url" |
73 | fi | 73 | fi |
74 | 74 | ||
75 | # Load the plugin. | 75 | # Load the plugin. |
76 | -antigen-load "$url" "$loc" "$btype" "$make_local_clone" | 76 | -antigen-load "$url" "$loc" "$btype" "$make_local_clone" |
77 | 77 | ||
78 | } | 78 | } |
79 | 79 | ||
80 | -antigen-resolve-bundle-url () { | 80 | -antigen-resolve-bundle-url () { |
81 | # Given an acceptable short/full form of a bundle's repo url, this function | 81 | # Given an acceptable short/full form of a bundle's repo url, this function |
82 | # echoes the full form of the repo's clone url. | 82 | # echoes the full form of the repo's clone url. |
83 | 83 | ||
84 | local url="$1" | 84 | local url="$1" |
85 | 85 | ||
86 | # Expand short github url syntax: `username/reponame`. | 86 | # Expand short github url syntax: `username/reponame`. |
87 | if [[ $url != git://* && | 87 | if [[ $url != git://* && |
88 | $url != https://* && | 88 | $url != https://* && |
89 | $url != http://* && | 89 | $url != http://* && |
90 | $url != ssh://* && | 90 | $url != ssh://* && |
91 | $url != /* && | 91 | $url != /* && |
92 | $url != git@github.com:*/* | 92 | $url != git@github.com:*/* |
93 | ]]; then | 93 | ]]; then |
94 | url="https://github.com/${url%.git}.git" | 94 | url="https://github.com/${url%.git}.git" |
95 | fi | 95 | fi |
96 | 96 | ||
97 | echo "$url" | 97 | echo "$url" |
98 | } | 98 | } |
99 | 99 | ||
100 | antigen-bundles () { | 100 | antigen-bundles () { |
101 | # Bulk add many bundles at one go. Empty lines and lines starting with a `#` | 101 | # Bulk add many bundles at one go. Empty lines and lines starting with a `#` |
102 | # are ignored. Everything else is given to `antigen-bundle` as is, no | 102 | # are ignored. Everything else is given to `antigen-bundle` as is, no |
103 | # quoting rules applied. | 103 | # quoting rules applied. |
104 | 104 | ||
105 | local line | 105 | local line |
106 | 106 | ||
107 | grep '^[[:space:]]*[^[:space:]#]' | while read line; do | 107 | grep '^[[:space:]]*[^[:space:]#]' | while read line; do |
108 | # Using `eval` so that we can use the shell-style quoting in each line | 108 | # Using `eval` so that we can use the shell-style quoting in each line |
109 | # piped to `antigen-bundles`. | 109 | # piped to `antigen-bundles`. |
110 | eval "antigen-bundle $line" | 110 | eval "antigen-bundle $line" |
111 | done | 111 | done |
112 | } | 112 | } |
113 | 113 | ||
114 | antigen-update () { | 114 | antigen-update () { |
115 | # Update your bundles, i.e., `git pull` in all the plugin repos. | 115 | # Update your bundles, i.e., `git pull` in all the plugin repos. |
116 | 116 | ||
117 | date > $ADOTDIR/revert-info | 117 | date > $ADOTDIR/revert-info |
118 | 118 | ||
119 | -antigen-echo-record | | 119 | -antigen-echo-record | |
120 | awk '$4 == "true" {print $1}' | | 120 | awk '$4 == "true" {print $1}' | |
121 | sort -u | | 121 | sort -u | |
122 | while read url; do | 122 | while read url; do |
123 | echo "**** Pulling $url" | 123 | echo "**** Pulling $url" |
124 | 124 | ||
125 | local clone_dir="$(-antigen-get-clone-dir "$url")" | 125 | local clone_dir="$(-antigen-get-clone-dir "$url")" |
126 | if [[ -d "$clone_dir" ]]; then | 126 | if [[ -d "$clone_dir" ]]; then |
127 | (echo -n "$clone_dir:" | 127 | (echo -n "$clone_dir:" |
128 | cd "$clone_dir" | 128 | cd "$clone_dir" |
129 | git rev-parse HEAD) >> $ADOTDIR/revert-info | 129 | git rev-parse HEAD) >> $ADOTDIR/revert-info |
130 | fi | 130 | fi |
131 | 131 | ||
132 | -antigen-ensure-repo "$url" --update --verbose | 132 | -antigen-ensure-repo "$url" --update --verbose |
133 | 133 | ||
134 | echo | 134 | echo |
135 | done | 135 | done |
136 | } | 136 | } |
137 | 137 | ||
138 | antigen-revert () { | 138 | antigen-revert () { |
139 | if [[ -f $ADOTDIR/revert-info ]]; then | 139 | if [[ -f $ADOTDIR/revert-info ]]; then |
140 | cat $ADOTDIR/revert-info | sed '1!p' | while read line; do | 140 | cat $ADOTDIR/revert-info | sed '1!p' | while read line; do |
141 | dir="$(echo "$line" | cut -d: -f1)" | 141 | dir="$(echo "$line" | cut -d: -f1)" |
142 | git --git-dir="$dir/.git" --work-tree="$dir" \ | 142 | git --git-dir="$dir/.git" --work-tree="$dir" \ |
143 | checkout "$(echo "$line" | cut -d: -f2)" 2> /dev/null | 143 | checkout "$(echo "$line" | cut -d: -f2)" 2> /dev/null |
144 | 144 | ||
145 | done | 145 | done |
146 | 146 | ||
147 | echo "Reverted to state before running -update on $( | 147 | echo "Reverted to state before running -update on $( |
148 | cat $ADOTDIR/revert-info | sed -n 1p)." | 148 | cat $ADOTDIR/revert-info | sed -n 1p)." |
149 | 149 | ||
150 | else | 150 | else |
151 | echo 'No revert information available. Cannot revert.' >&2 | 151 | echo 'No revert information available. Cannot revert.' >&2 |
152 | fi | 152 | fi |
153 | 153 | ||
154 | 154 | ||
155 | } | 155 | } |
156 | 156 | ||
157 | -antigen-get-clone-dir () { | 157 | -antigen-get-clone-dir () { |
158 | # Takes a repo url and gives out the path that this url needs to be cloned | 158 | # Takes a repo url and gives out the path that this url needs to be cloned |
159 | # to. Doesn't actually clone anything. | 159 | # to. Doesn't actually clone anything. |
160 | echo -n $ADOTDIR/repos/ | 160 | echo -n $ADOTDIR/repos/ |
161 | 161 | ||
162 | if [[ "$1" == "https://github.com/sorin-ionescu/prezto.git" ]]; then | 162 | if [[ "$1" == "https://github.com/sorin-ionescu/prezto.git" ]]; then |
163 | # Prezto's directory *has* to be `.zprezto`. | 163 | # Prezto's directory *has* to be `.zprezto`. |
164 | echo .zprezto | 164 | echo .zprezto |
165 | 165 | ||
166 | else | 166 | else |
167 | echo "$1" | sed \ | 167 | echo "$1" | sed \ |
168 | -e 's./.-SLASH-.g' \ | 168 | -e 's./.-SLASH-.g' \ |
169 | -e 's.:.-COLON-.g' \ | 169 | -e 's.:.-COLON-.g' \ |
170 | -e 's.|.-PIPE-.g' | 170 | -e 's.|.-PIPE-.g' |
171 | 171 | ||
172 | fi | 172 | fi |
173 | } | 173 | } |
174 | 174 | ||
175 | -antigen-get-clone-url () { | 175 | -antigen-get-clone-url () { |
176 | # Takes a repo's clone dir and gives out the repo's original url that was | 176 | # Takes a repo's clone dir and gives out the repo's original url that was |
177 | # used to create the given directory path. | 177 | # used to create the given directory path. |
178 | 178 | ||
179 | if [[ "$1" == ".zprezto" ]]; then | 179 | if [[ "$1" == ".zprezto" ]]; then |
180 | # Prezto's (in `.zprezto`), is assumed to be from `sorin-ionescu`'s | 180 | # Prezto's (in `.zprezto`), is assumed to be from `sorin-ionescu`'s |
181 | # remote. | 181 | # remote. |
182 | echo https://github.com/sorin-ionescu/prezto.git | 182 | echo https://github.com/sorin-ionescu/prezto.git |
183 | 183 | ||
184 | else | 184 | else |
185 | echo "$1" | sed \ | 185 | echo "$1" | sed \ |
186 | -e "s:^$ADOTDIR/repos/::" \ | 186 | -e "s:^$ADOTDIR/repos/::" \ |
187 | -e 's.-SLASH-./.g' \ | 187 | -e 's.-SLASH-./.g' \ |
188 | -e 's.-COLON-.:.g' \ | 188 | -e 's.-COLON-.:.g' \ |
189 | -e 's.-PIPE-.|.g' | 189 | -e 's.-PIPE-.|.g' |
190 | 190 | ||
191 | fi | 191 | fi |
192 | } | 192 | } |
193 | 193 | ||
194 | -antigen-ensure-repo () { | 194 | -antigen-ensure-repo () { |
195 | 195 | ||
196 | # Ensure that a clone exists for the given repo url and branch. If the first | 196 | # Ensure that a clone exists for the given repo url and branch. If the first |
197 | # argument is `--update` and if a clone already exists for the given repo | 197 | # argument is `--update` and if a clone already exists for the given repo |
198 | # and branch, it is pull-ed, i.e., updated. | 198 | # and branch, it is pull-ed, i.e., updated. |
199 | 199 | ||
200 | # Argument defaults. | 200 | # Argument defaults. |
201 | # The url. No sane default for this, so just empty. | 201 | # The url. No sane default for this, so just empty. |
202 | local url= | 202 | local url= |
203 | # Check if we have to update. | 203 | # Check if we have to update. |
204 | local update=false | 204 | local update=false |
205 | # Verbose output. | 205 | # Verbose output. |
206 | local verbose=false | 206 | local verbose=false |
207 | 207 | ||
208 | eval "$(-antigen-parse-args 'url ; update?, verbose?' "$@")" | 208 | eval "$(-antigen-parse-args 'url ; update?, verbose?' "$@")" |
209 | shift $# | 209 | shift $# |
210 | 210 | ||
211 | # Get the clone's directory as per the given repo url and branch. | 211 | # Get the clone's directory as per the given repo url and branch. |
212 | local clone_dir="$(-antigen-get-clone-dir $url)" | 212 | local clone_dir="$(-antigen-get-clone-dir $url)" |
213 | 213 | ||
214 | # A temporary function wrapping the `git` command with repeated arguments. | 214 | # A temporary function wrapping the `git` command with repeated arguments. |
215 | --plugin-git () { | 215 | --plugin-git () { |
216 | (cd "$clone_dir" && git --no-pager "$@") | 216 | (cd "$clone_dir" && git --no-pager "$@") |
217 | } | 217 | } |
218 | 218 | ||
219 | # Clone if it doesn't already exist. | 219 | # Clone if it doesn't already exist. |
220 | if [[ ! -d $clone_dir ]]; then | 220 | if [[ ! -d $clone_dir ]]; then |
221 | git clone --recursive "${url%|*}" "$clone_dir" | 221 | git clone --recursive "${url%|*}" "$clone_dir" |
222 | elif $update; then | 222 | elif $update; then |
223 | # Save current revision. | 223 | # Save current revision. |
224 | local old_rev="$(--plugin-git rev-parse HEAD)" | 224 | local old_rev="$(--plugin-git rev-parse HEAD)" |
225 | # Pull changes if update requested. | 225 | # Pull changes if update requested. |
226 | --plugin-git pull | 226 | --plugin-git pull |
227 | # Update submodules. | 227 | # Update submodules. |
228 | --plugin-git submodule update --recursive | 228 | --plugin-git submodule update --recursive |
229 | # Get the new revision. | 229 | # Get the new revision. |
230 | local new_rev="$(--plugin-git rev-parse HEAD)" | 230 | local new_rev="$(--plugin-git rev-parse HEAD)" |
231 | fi | 231 | fi |
232 | 232 | ||
233 | # If its a specific branch that we want, checkout that branch. | 233 | # If its a specific branch that we want, checkout that branch. |
234 | if [[ $url == *\|* ]]; then | 234 | if [[ $url == *\|* ]]; then |
235 | local current_branch=${$(--plugin-git symbolic-ref HEAD)##refs/heads/} | 235 | local current_branch=${$(--plugin-git symbolic-ref HEAD)##refs/heads/} |
236 | local requested_branch="${url#*|}" | 236 | local requested_branch="${url#*|}" |
237 | # Only do the checkout when we are not already on the branch. | 237 | # Only do the checkout when we are not already on the branch. |
238 | [[ $requested_branch != $current_branch ]] && | 238 | [[ $requested_branch != $current_branch ]] && |
239 | --plugin-git checkout $requested_branch | 239 | --plugin-git checkout $requested_branch |
240 | fi | 240 | fi |
241 | 241 | ||
242 | if [[ -n $old_rev && $old_rev != $new_rev ]]; then | 242 | if [[ -n $old_rev && $old_rev != $new_rev ]]; then |
243 | echo Updated from ${old_rev:0:7} to ${new_rev:0:7}. | 243 | echo Updated from ${old_rev:0:7} to ${new_rev:0:7}. |
244 | if $verbose; then | 244 | if $verbose; then |
245 | --plugin-git log --oneline --reverse --no-merges --stat '@{1}..' | 245 | --plugin-git log --oneline --reverse --no-merges --stat '@{1}..' |
246 | fi | 246 | fi |
247 | fi | 247 | fi |
248 | 248 | ||
249 | # Remove the temporary git wrapper function. | 249 | # Remove the temporary git wrapper function. |
250 | unfunction -- --plugin-git | 250 | unfunction -- --plugin-git |
251 | 251 | ||
252 | } | 252 | } |
253 | 253 | ||
254 | -antigen-load () { | 254 | -antigen-load () { |
255 | 255 | ||
256 | local url="$1" | 256 | local url="$1" |
257 | local loc="$2" | 257 | local loc="$2" |
258 | local btype="$3" | 258 | local btype="$3" |
259 | local make_local_clone="$4" | 259 | local make_local_clone="$4" |
260 | 260 | ||
261 | # The full location where the plugin is located. | 261 | # The full location where the plugin is located. |
262 | local location | 262 | local location |
263 | if $make_local_clone; then | 263 | if $make_local_clone; then |
264 | location="$(-antigen-get-clone-dir "$url")/$loc" | 264 | location="$(-antigen-get-clone-dir "$url")/$loc" |
265 | else | 265 | else |
266 | location="$url" | 266 | location="$url" |
267 | fi | 267 | fi |
268 | 268 | ||
269 | if [[ $btype == theme ]]; then | 269 | if [[ $btype == theme ]]; then |
270 | 270 | ||
271 | # Of course, if its a theme, the location would point to the script | 271 | # Of course, if its a theme, the location would point to the script |
272 | # file. | 272 | # file. |
273 | source "$location" | 273 | source "$location" |
274 | 274 | ||
275 | else | 275 | else |
276 | 276 | ||
277 | # Source the plugin script. | 277 | # Source the plugin script. |
278 | # FIXME: I don't know. Looks very very ugly. Needs a better | 278 | # FIXME: I don't know. Looks very very ugly. Needs a better |
279 | # implementation once tests are ready. | 279 | # implementation once tests are ready. |
280 | local script_loc="$(ls "$location" | grep '\.plugin\.zsh$' | head -n1)" | 280 | local script_loc="$(ls "$location" | grep '\.plugin\.zsh$' | head -n1)" |
281 | 281 | ||
282 | if [[ -f $location/$script_loc ]]; then | 282 | if [[ -f $location/$script_loc ]]; then |
283 | # If we have a `*.plugin.zsh`, source it. | 283 | # If we have a `*.plugin.zsh`, source it. |
284 | source "$location/$script_loc" | 284 | source "$location/$script_loc" |
285 | 285 | ||
286 | elif [[ -f $location/init.zsh ]]; then | 286 | elif [[ -f $location/init.zsh ]]; then |
287 | # If we have a `init.zsh` | 287 | # If we have a `init.zsh` |
288 | if (( $+functions[pmodload] )); then | 288 | if (( $+functions[pmodload] )); then |
289 | # If pmodload is defined pmodload the module. Remove `modules/` | 289 | # If pmodload is defined pmodload the module. Remove `modules/` |
290 | # from loc to find module name. | 290 | # from loc to find module name. |
291 | pmodload "${loc#modules/}" | 291 | pmodload "${loc#modules/}" |
292 | else | 292 | else |
293 | # Otherwise source it. | 293 | # Otherwise source it. |
294 | source "$location/init.zsh" | 294 | source "$location/init.zsh" |
295 | fi | 295 | fi |
296 | 296 | ||
297 | elif ls "$location" | grep -l '\.zsh$' &> /dev/null; then | 297 | elif ls "$location" | grep -l '\.zsh$' &> /dev/null; then |
298 | # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` | 298 | # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` |
299 | # files. | 299 | # files. |
300 | for script ($location/*.zsh(N)) source "$script" | 300 | for script ($location/*.zsh(N)) source "$script" |
301 | 301 | ||
302 | elif ls "$location" | grep -l '\.sh$' &> /dev/null; then | 302 | elif ls "$location" | grep -l '\.sh$' &> /dev/null; then |
303 | # If there are no `*.zsh` files either, we look for and source any | 303 | # If there are no `*.zsh` files either, we look for and source any |
304 | # `*.sh` files instead. | 304 | # `*.sh` files instead. |
305 | for script ($location/*.sh(N)) source "$script" | 305 | for script ($location/*.sh(N)) source "$script" |
306 | 306 | ||
307 | fi | 307 | fi |
308 | 308 | ||
309 | # Add to $fpath, for completion(s). | 309 | # Add to $fpath, for completion(s). |
310 | fpath=($location $fpath) | 310 | fpath=($location $fpath) |
311 | 311 | ||
312 | fi | 312 | fi |
313 | 313 | ||
314 | } | 314 | } |
315 | 315 | ||
316 | # Update (with `git pull`) antigen itself. | 316 | # Update (with `git pull`) antigen itself. |
317 | # TODO: Once update is finished, show a summary of the new commits, as a kind of | 317 | # TODO: Once update is finished, show a summary of the new commits, as a kind of |
318 | # "what's new" message. | 318 | # "what's new" message. |
319 | antigen-selfupdate () { | 319 | antigen-selfupdate () { |
320 | ( cd $_ANTIGEN_INSTALL_DIR | 320 | ( cd $_ANTIGEN_INSTALL_DIR |
321 | if [[ ! ( -d .git || -f .git ) ]]; then | 321 | if [[ ! ( -d .git || -f .git ) ]]; then |
322 | echo "Your copy of antigen doesn't appear to be a git clone. " \ | 322 | echo "Your copy of antigen doesn't appear to be a git clone. " \ |
323 | "The 'selfupdate' command cannot work in this case." | 323 | "The 'selfupdate' command cannot work in this case." |
324 | return 1 | 324 | return 1 |
325 | fi | 325 | fi |
326 | local head="$(git rev-parse --abbrev-ref HEAD)" | 326 | local head="$(git rev-parse --abbrev-ref HEAD)" |
327 | if [[ $head == "HEAD" ]]; then | 327 | if [[ $head == "HEAD" ]]; then |
328 | # If current head is detached HEAD, checkout to master branch. | 328 | # If current head is detached HEAD, checkout to master branch. |
329 | git checkout master | 329 | git checkout master |
330 | fi | 330 | fi |
331 | git pull | 331 | git pull |
332 | ) | 332 | ) |
333 | } | 333 | } |
334 | 334 | ||
335 | antigen-cleanup () { | 335 | antigen-cleanup () { |
336 | 336 | ||
337 | # Cleanup unused repositories. | 337 | # Cleanup unused repositories. |
338 | 338 | ||
339 | local force=false | 339 | local force=false |
340 | if [[ $1 == --force ]]; then | 340 | if [[ $1 == --force ]]; then |
341 | force=true | 341 | force=true |
342 | fi | 342 | fi |
343 | 343 | ||
344 | if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then | 344 | if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then |
345 | echo "You don't have any bundles." | 345 | echo "You don't have any bundles." |
346 | return 0 | 346 | return 0 |
347 | fi | 347 | fi |
348 | 348 | ||
349 | # Find directores in ADOTDIR/repos, that are not in the bundles record. | 349 | # Find directores in ADOTDIR/repos, that are not in the bundles record. |
350 | local unused_clones="$(comm -13 \ | 350 | local unused_clones="$(comm -13 \ |
351 | <(-antigen-echo-record | | 351 | <(-antigen-echo-record | |
352 | awk '$4 == "true" {print $1}' | | 352 | awk '$4 == "true" {print $1}' | |
353 | while read line; do | 353 | while read line; do |
354 | -antigen-get-clone-dir "$line" | 354 | -antigen-get-clone-dir "$line" |
355 | done | | 355 | done | |
356 | sort -u) \ | 356 | sort -u) \ |
357 | <(ls -d "$ADOTDIR/repos/"* | sort -u))" | 357 | <(ls -d "$ADOTDIR/repos/"* | sort -u))" |
358 | 358 | ||
359 | if [[ -z $unused_clones ]]; then | 359 | if [[ -z $unused_clones ]]; then |
360 | echo "You don't have any unidentified bundles." | 360 | echo "You don't have any unidentified bundles." |
361 | return 0 | 361 | return 0 |
362 | fi | 362 | fi |
363 | 363 | ||
364 | echo 'You have clones for the following repos, but are not used.' | 364 | echo 'You have clones for the following repos, but are not used.' |
365 | echo "$unused_clones" | | 365 | echo "$unused_clones" | |
366 | while read line; do | 366 | while read line; do |
367 | -antigen-get-clone-url "$line" | 367 | -antigen-get-clone-url "$line" |
368 | done | | 368 | done | |
369 | sed -e 's/^/ /' -e 's/|/, branch /' | 369 | sed -e 's/^/ /' -e 's/|/, branch /' |
370 | 370 | ||
371 | if $force || (echo -n '\nDelete them all? [y/N] '; read -q); then | 371 | if $force || (echo -n '\nDelete them all? [y/N] '; read -q); then |
372 | echo | 372 | echo |
373 | echo | 373 | echo |
374 | echo "$unused_clones" | while read line; do | 374 | echo "$unused_clones" | while read line; do |
375 | echo -n "Deleting clone for $(-antigen-get-clone-url "$line")..." | 375 | echo -n "Deleting clone for $(-antigen-get-clone-url "$line")..." |
376 | rm -rf "$line" | 376 | rm -rf "$line" |
377 | echo ' done.' | 377 | echo ' done.' |
378 | done | 378 | done |
379 | else | 379 | else |
380 | echo | 380 | echo |
381 | echo Nothing deleted. | 381 | echo Nothing deleted. |
382 | fi | 382 | fi |
383 | } | 383 | } |
384 | 384 | ||
385 | antigen-use () { | 385 | antigen-use () { |
386 | if [[ $1 == oh-my-zsh ]]; then | 386 | if [[ $1 == oh-my-zsh ]]; then |
387 | -antigen-use-oh-my-zsh | 387 | -antigen-use-oh-my-zsh |
388 | elif [[ $1 == prezto ]]; then | 388 | elif [[ $1 == prezto ]]; then |
389 | -antigen-use-prezto | 389 | -antigen-use-prezto |
390 | else | 390 | else |
391 | echo 'Usage: antigen-use <library-name>' >&2 | 391 | echo 'Usage: antigen-use <library-name>' >&2 |
392 | echo 'Where <library-name> is any one of the following:' >&2 | 392 | echo 'Where <library-name> is any one of the following:' >&2 |
393 | echo ' * oh-my-zsh' >&2 | 393 | echo ' * oh-my-zsh' >&2 |
394 | echo ' * prezto' >&2 | 394 | echo ' * prezto' >&2 |
395 | return 1 | 395 | return 1 |
396 | fi | 396 | fi |
397 | } | 397 | } |
398 | 398 | ||
399 | -antigen-use-oh-my-zsh () { | 399 | -antigen-use-oh-my-zsh () { |
400 | if [[ -z "$ZSH" ]]; then | 400 | if [[ -z "$ZSH" ]]; then |
401 | export ZSH="$(-antigen-get-clone-dir "$ANTIGEN_DEFAULT_REPO_URL")" | 401 | export ZSH="$(-antigen-get-clone-dir "$ANTIGEN_DEFAULT_REPO_URL")" |
402 | fi | 402 | fi |
403 | antigen-bundle --loc=lib | 403 | antigen-bundle --loc=lib |
404 | } | 404 | } |
405 | 405 | ||
406 | -antigen-use-prezto () { | 406 | -antigen-use-prezto () { |
407 | antigen-bundle sorin-ionescu/prezto | 407 | antigen-bundle sorin-ionescu/prezto |
408 | export ZDOTDIR=$ADOTDIR/repos/ | 408 | export ZDOTDIR=$ADOTDIR/repos/ |
409 | } | 409 | } |
410 | 410 | ||
411 | # For backwards compatibility. | 411 | # For backwards compatibility. |
412 | antigen-lib () { | 412 | antigen-lib () { |
413 | -antigen-use-oh-my-zsh | 413 | -antigen-use-oh-my-zsh |
414 | echo '`antigen-lib` is deprecated and will soon be removed.' | 414 | echo '`antigen-lib` is deprecated and will soon be removed.' |
415 | echo 'Use `antigen-use oh-my-zsh` instead.' | 415 | echo 'Use `antigen-use oh-my-zsh` instead.' |
416 | } | 416 | } |
417 | 417 | ||
418 | # For backwards compatibility. | 418 | # For backwards compatibility. |
419 | antigen-prezto-lib () { | 419 | antigen-prezto-lib () { |
420 | -antigen-use-prezto | 420 | -antigen-use-prezto |
421 | echo '`antigen-prezto-lib` is deprecated and will soon be removed.' | 421 | echo '`antigen-prezto-lib` is deprecated and will soon be removed.' |
422 | echo 'Use `antigen-use prezto` instead.' | 422 | echo 'Use `antigen-use prezto` instead.' |
423 | } | 423 | } |
424 | 424 | ||
425 | antigen-theme () { | 425 | antigen-theme () { |
426 | 426 | ||
427 | if [[ "$1" != */* && "$1" != --* ]]; then | 427 | if [[ "$1" != */* && "$1" != --* ]]; then |
428 | # The first argument is just a name of the plugin, to be picked up from | 428 | # The first argument is just a name of the plugin, to be picked up from |
429 | # the default repo. | 429 | # the default repo. |
430 | local name="${1:-robbyrussell}" | 430 | local name="${1:-robbyrussell}" |
431 | antigen-bundle --loc=themes/$name --btype=theme | 431 | antigen-bundle --loc=themes/$name --btype=theme |
432 | 432 | ||
433 | else | 433 | else |
434 | antigen-bundle "$@" --btype=theme | 434 | antigen-bundle "$@" --btype=theme |
435 | 435 | ||
436 | fi | 436 | fi |
437 | 437 | ||
438 | } | 438 | } |
439 | 439 | ||
440 | antigen-apply () { | 440 | antigen-apply () { |
441 | 441 | ||
442 | # Initialize completion. | 442 | # Initialize completion. |
443 | local cdef | 443 | local cdef |
444 | 444 | ||
445 | # Load the compinit module. This will readefine the `compdef` function to | 445 | # Load the compinit module. This will readefine the `compdef` function to |
446 | # the one that actually initializes completions. | 446 | # the one that actually initializes completions. |
447 | autoload -U compinit | 447 | autoload -U compinit |
448 | compinit -i | 448 | compinit -i |
449 | 449 | ||
450 | # Apply all `compinit`s that have been deferred. | 450 | # Apply all `compinit`s that have been deferred. |
451 | eval "$(for cdef in $__deferred_compdefs; do | 451 | eval "$(for cdef in $__deferred_compdefs; do |
452 | echo compdef $cdef | 452 | echo compdef $cdef |
453 | done)" | 453 | done)" |
454 | 454 | ||
455 | unset __deferred_compdefs | 455 | unset __deferred_compdefs |
456 | 456 | ||
457 | } | 457 | } |
458 | 458 | ||
459 | antigen-list () { | 459 | antigen-list () { |
460 | # List all currently installed bundles. | 460 | # List all currently installed bundles. |
461 | if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then | 461 | if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then |
462 | echo "You don't have any bundles." >&2 | 462 | echo "You don't have any bundles." >&2 |
463 | return 1 | 463 | return 1 |
464 | else | 464 | else |
465 | -antigen-echo-record | sort -u | 465 | -antigen-echo-record | sort -u |
466 | fi | 466 | fi |
467 | } | 467 | } |
468 | 468 | ||
469 | antigen-snapshot () { | 469 | antigen-snapshot () { |
470 | 470 | ||
471 | local snapshot_file="${1:-antigen-shapshot}" | 471 | local snapshot_file="${1:-antigen-shapshot}" |
472 | 472 | ||
473 | # The snapshot content lines are pairs of repo-url and git version hash, in | 473 | # The snapshot content lines are pairs of repo-url and git version hash, in |
474 | # the form: | 474 | # the form: |
475 | # <version-hash> <repo-url> | 475 | # <version-hash> <repo-url> |
476 | local snapshot_content="$(-antigen-echo-record | | 476 | local snapshot_content="$(-antigen-echo-record | |
477 | grep 'true$' | | 477 | grep 'true$' | |
478 | sed 's/ .*$//' | | 478 | sed 's/ .*$//' | |
479 | sort -u | | 479 | sort -u | |
480 | while read url; do | 480 | while read url; do |
481 | local dir="$(-antigen-get-clone-dir "$url")" | 481 | local dir="$(-antigen-get-clone-dir "$url")" |
482 | local version_hash="$(cd "$dir" && git rev-parse HEAD)" | 482 | local version_hash="$(cd "$dir" && git rev-parse HEAD)" |
483 | echo "$version_hash $url" | 483 | echo "$version_hash $url" |
484 | done)" | 484 | done)" |
485 | 485 | ||
486 | { | 486 | { |
487 | # The first line in the snapshot file is for metadata, in the form: | 487 | # The first line in the snapshot file is for metadata, in the form: |
488 | # key='value'; key='value'; key='value'; | 488 | # key='value'; key='value'; key='value'; |
489 | # Where `key`s are valid shell variable names. | 489 | # Where `key`s are valid shell variable names. |
490 | 490 | ||
491 | # Snapshot version. Has no relation to antigen version. If the snapshot | 491 | # Snapshot version. Has no relation to antigen version. If the snapshot |
492 | # file format changes, this number can be incremented. | 492 | # file format changes, this number can be incremented. |
493 | echo -n "version='1';" | 493 | echo -n "version='1';" |
494 | 494 | ||
495 | # Snapshot creation date+time. | 495 | # Snapshot creation date+time. |
496 | echo -n " created_on='$(date)';" | 496 | echo -n " created_on='$(date)';" |
497 | 497 | ||
498 | # Add a checksum with the md5 checksum of all the snapshot lines. | 498 | # Add a checksum with the md5 checksum of all the snapshot lines. |
499 | chksum() { (md5sum; test $? = 127 && md5) 2>/dev/null | cut -d' ' -f1 } | 499 | chksum() { (md5sum; test $? = 127 && md5) 2>/dev/null | cut -d' ' -f1 } |
500 | local checksum="$(echo "$snapshot_content" | chksum)" | 500 | local checksum="$(echo "$snapshot_content" | chksum)" |
501 | unset -f chksum; | 501 | unset -f chksum; |
502 | echo -n " checksum='${checksum%% *}';" | 502 | echo -n " checksum='${checksum%% *}';" |
503 | 503 | ||
504 | # A newline after the metadata and then the snapshot lines. | 504 | # A newline after the metadata and then the snapshot lines. |
505 | echo "\n$snapshot_content" | 505 | echo "\n$snapshot_content" |
506 | 506 | ||
507 | } > "$snapshot_file" | 507 | } > "$snapshot_file" |
508 | 508 | ||
509 | } | 509 | } |
510 | 510 | ||
511 | antigen-restore () { | 511 | antigen-restore () { |
512 | 512 | ||
513 | if [[ $# == 0 ]]; then | 513 | if [[ $# == 0 ]]; then |
514 | echo 'Please provide a snapshot file to restore from.' >&2 | 514 | echo 'Please provide a snapshot file to restore from.' >&2 |
515 | return 1 | 515 | return 1 |
516 | fi | 516 | fi |
517 | 517 | ||
518 | local snapshot_file="$1" | 518 | local snapshot_file="$1" |
519 | 519 | ||
520 | # TODO: Before doing anything with the snapshot file, verify its checksum. | 520 | # TODO: Before doing anything with the snapshot file, verify its checksum. |
521 | # If it fails, notify this to the user and confirm if restore should | 521 | # If it fails, notify this to the user and confirm if restore should |
522 | # proceed. | 522 | # proceed. |
523 | 523 | ||
524 | echo -n "Restoring from $snapshot_file..." | 524 | echo -n "Restoring from $snapshot_file..." |
525 | 525 | ||
526 | sed -n '1!p' "$snapshot_file" | | 526 | sed -n '1!p' "$snapshot_file" | |
527 | while read line; do | 527 | while read line; do |
528 | 528 | ||
529 | local version_hash="${line%% *}" | 529 | local version_hash="${line%% *}" |
530 | local url="${line##* }" | 530 | local url="${line##* }" |
531 | local clone_dir="$(-antigen-get-clone-dir "$url")" | 531 | local clone_dir="$(-antigen-get-clone-dir "$url")" |
532 | 532 | ||
533 | if [[ ! -d $clone_dir ]]; then | 533 | if [[ ! -d $clone_dir ]]; then |
534 | git clone "$url" "$clone_dir" > /dev/null | 534 | git clone "$url" "$clone_dir" &> /dev/null |
535 | fi | 535 | fi |
536 | 536 | ||
537 | (cd "$clone_dir" && git checkout $version_hash) 2> /dev/null | 537 | (cd "$clone_dir" && git checkout $version_hash) &> /dev/null |
538 | 538 | ||
539 | done | 539 | done |
540 | 540 | ||
541 | echo ' done.' | 541 | echo ' done.' |
542 | echo 'Please open a new shell to get the restored changes.' | 542 | echo 'Please open a new shell to get the restored changes.' |
543 | } | 543 | } |
544 | 544 | ||
545 | antigen-help () { | 545 | antigen-help () { |
546 | cat <<EOF | 546 | cat <<EOF |
547 | Antigen is a plugin management system for zsh. It makes it easy to grab awesome | 547 | Antigen is a plugin management system for zsh. It makes it easy to grab awesome |
548 | shell scripts and utilities, put up on github. For further details and complete | 548 | shell scripts and utilities, put up on github. For further details and complete |
549 | documentation, visit the project's page at 'http://antigen.sharats.me'. | 549 | documentation, visit the project's page at 'http://antigen.sharats.me'. |
550 | EOF | 550 | EOF |
551 | } | 551 | } |
552 | 552 | ||
553 | # A syntax sugar to avoid the `-` when calling antigen commands. With this | 553 | # A syntax sugar to avoid the `-` when calling antigen commands. With this |
554 | # function, you can write `antigen-bundle` as `antigen bundle` and so on. | 554 | # function, you can write `antigen-bundle` as `antigen bundle` and so on. |
555 | antigen () { | 555 | antigen () { |
556 | local cmd="$1" | 556 | local cmd="$1" |
557 | if [[ -z "$cmd" ]]; then | 557 | if [[ -z "$cmd" ]]; then |
558 | echo 'Antigen: Please give a command to run.' >&2 | 558 | echo 'Antigen: Please give a command to run.' >&2 |
559 | return 1 | 559 | return 1 |
560 | fi | 560 | fi |
561 | shift | 561 | shift |
562 | 562 | ||
563 | if functions "antigen-$cmd" > /dev/null; then | 563 | if functions "antigen-$cmd" > /dev/null; then |
564 | "antigen-$cmd" "$@" | 564 | "antigen-$cmd" "$@" |
565 | else | 565 | else |
566 | echo "Antigen: Unknown command: $cmd" >&2 | 566 | echo "Antigen: Unknown command: $cmd" >&2 |
567 | fi | 567 | fi |
568 | } | 568 | } |
569 | 569 | ||
570 | -antigen-parse-args () { | 570 | -antigen-parse-args () { |
571 | # An argument parsing functionality to parse arguments the *antigen* way :). | 571 | # An argument parsing functionality to parse arguments the *antigen* way :). |
572 | # Takes one first argument (called spec), which dictates how to parse and | 572 | # Takes one first argument (called spec), which dictates how to parse and |
573 | # the rest of the arguments are parsed. Outputs a piece of valid shell code | 573 | # the rest of the arguments are parsed. Outputs a piece of valid shell code |
574 | # that can be passed to `eval` inside a function which creates the arguments | 574 | # that can be passed to `eval` inside a function which creates the arguments |
575 | # and their values as local variables. Suggested use is to set the defaults | 575 | # and their values as local variables. Suggested use is to set the defaults |
576 | # to all arguments first and then eval the output of this function. | 576 | # to all arguments first and then eval the output of this function. |
577 | 577 | ||
578 | # Spec: Only long argument supported. No support for parsing short options. | 578 | # Spec: Only long argument supported. No support for parsing short options. |
579 | # The spec must have two sections, separated by a `;`. | 579 | # The spec must have two sections, separated by a `;`. |
580 | # '<positional-arguments>;<keyword-only-arguments>' | 580 | # '<positional-arguments>;<keyword-only-arguments>' |
581 | # Positional arguments are passed as just values, like `command a b`. | 581 | # Positional arguments are passed as just values, like `command a b`. |
582 | # Keyword arguments are passed as a `--name=value` pair, like `command | 582 | # Keyword arguments are passed as a `--name=value` pair, like `command |
583 | # --arg1=a --arg2=b`. | 583 | # --arg1=a --arg2=b`. |
584 | 584 | ||
585 | # Each argument in the spec is separated by a `,`. Each keyword argument can | 585 | # Each argument in the spec is separated by a `,`. Each keyword argument can |
586 | # end in a `:` to specifiy that this argument wants a value, otherwise it | 586 | # end in a `:` to specifiy that this argument wants a value, otherwise it |
587 | # doesn't take a value. (The value in the output when the keyword argument | 587 | # doesn't take a value. (The value in the output when the keyword argument |
588 | # doesn't have a `:` is `true`). | 588 | # doesn't have a `:` is `true`). |
589 | 589 | ||
590 | # Arguments in either section can end with a `?` (should come after `:`, if | 590 | # Arguments in either section can end with a `?` (should come after `:`, if |
591 | # both are present), means optional. FIXME: Not yet implemented. | 591 | # both are present), means optional. FIXME: Not yet implemented. |
592 | 592 | ||
593 | # See the test file, tests/arg-parser.t for (working) examples. | 593 | # See the test file, tests/arg-parser.t for (working) examples. |
594 | 594 | ||
595 | local spec="$1" | 595 | local spec="$1" |
596 | shift | 596 | shift |
597 | 597 | ||
598 | # Sanitize the spec | 598 | # Sanitize the spec |
599 | spec="$(echo "$spec" | tr '\n' ' ' | sed 's/[[:space:]]//g')" | 599 | spec="$(echo "$spec" | tr '\n' ' ' | sed 's/[[:space:]]//g')" |
600 | 600 | ||
601 | local code='' | 601 | local code='' |
602 | 602 | ||
603 | --add-var () { | 603 | --add-var () { |
604 | test -z "$code" || code="$code\n" | 604 | test -z "$code" || code="$code\n" |
605 | code="${code}local $1='$2'" | 605 | code="${code}local $1='$2'" |
606 | } | 606 | } |
607 | 607 | ||
608 | local positional_args="$(echo "$spec" | cut -d\; -f1)" | 608 | local positional_args="$(echo "$spec" | cut -d\; -f1)" |
609 | local positional_args_count="$(echo $positional_args | | 609 | local positional_args_count="$(echo $positional_args | |
610 | awk -F, '{print NF}')" | 610 | awk -F, '{print NF}')" |
611 | 611 | ||
612 | # Set spec values based on the positional arguments. | 612 | # Set spec values based on the positional arguments. |
613 | local i=1 | 613 | local i=1 |
614 | while [[ -n $1 && $1 != --* ]]; do | 614 | while [[ -n $1 && $1 != --* ]]; do |
615 | 615 | ||
616 | if (( $i > $positional_args_count )); then | 616 | if (( $i > $positional_args_count )); then |
617 | echo "Only $positional_args_count positional arguments allowed." >&2 | 617 | echo "Only $positional_args_count positional arguments allowed." >&2 |
618 | echo "Found at least one more: '$1'" >&2 | 618 | echo "Found at least one more: '$1'" >&2 |
619 | return | 619 | return |
620 | fi | 620 | fi |
621 | 621 | ||
622 | local name_spec="$(echo "$positional_args" | cut -d, -f$i)" | 622 | local name_spec="$(echo "$positional_args" | cut -d, -f$i)" |
623 | local name="${${name_spec%\?}%:}" | 623 | local name="${${name_spec%\?}%:}" |
624 | local value="$1" | 624 | local value="$1" |
625 | 625 | ||
626 | if echo "$code" | grep -l "^local $name=" &> /dev/null; then | 626 | if echo "$code" | grep -l "^local $name=" &> /dev/null; then |
627 | echo "Argument '$name' repeated with the value '$value'". >&2 | 627 | echo "Argument '$name' repeated with the value '$value'". >&2 |
628 | return | 628 | return |
629 | fi | 629 | fi |
630 | 630 | ||
631 | --add-var $name "$value" | 631 | --add-var $name "$value" |
632 | 632 | ||
633 | shift | 633 | shift |
634 | i=$(($i + 1)) | 634 | i=$(($i + 1)) |
635 | done | 635 | done |
636 | 636 | ||
637 | local keyword_args="$( | 637 | local keyword_args="$( |
638 | # Positional arguments can double up as keyword arguments too. | 638 | # Positional arguments can double up as keyword arguments too. |
639 | echo "$positional_args" | tr , '\n' | | 639 | echo "$positional_args" | tr , '\n' | |
640 | while read line; do | 640 | while read line; do |
641 | if [[ $line == *\? ]]; then | 641 | if [[ $line == *\? ]]; then |
642 | echo "${line%?}:?" | 642 | echo "${line%?}:?" |
643 | else | 643 | else |
644 | echo "$line:" | 644 | echo "$line:" |
645 | fi | 645 | fi |
646 | done | 646 | done |
647 | 647 | ||
648 | # Specified keyword arguments. | 648 | # Specified keyword arguments. |
649 | echo "$spec" | cut -d\; -f2 | tr , '\n' | 649 | echo "$spec" | cut -d\; -f2 | tr , '\n' |
650 | )" | 650 | )" |
651 | local keyword_args_count="$(echo $keyword_args | awk -F, '{print NF}')" | 651 | local keyword_args_count="$(echo $keyword_args | awk -F, '{print NF}')" |
652 | 652 | ||
653 | # Set spec values from keyword arguments, if any. The remaining arguments | 653 | # Set spec values from keyword arguments, if any. The remaining arguments |
654 | # are all assumed to be keyword arguments. | 654 | # are all assumed to be keyword arguments. |
655 | while [[ $1 == --* ]]; do | 655 | while [[ $1 == --* ]]; do |
656 | # Remove the `--` at the start. | 656 | # Remove the `--` at the start. |
657 | local arg="${1#--}" | 657 | local arg="${1#--}" |
658 | 658 | ||
659 | # Get the argument name and value. | 659 | # Get the argument name and value. |
660 | if [[ $arg != *=* ]]; then | 660 | if [[ $arg != *=* ]]; then |
661 | local name="$arg" | 661 | local name="$arg" |
662 | local value='' | 662 | local value='' |
663 | else | 663 | else |
664 | local name="${arg%\=*}" | 664 | local name="${arg%\=*}" |
665 | local value="${arg#*=}" | 665 | local value="${arg#*=}" |
666 | fi | 666 | fi |
667 | 667 | ||
668 | if echo "$code" | grep -l "^local $name=" &> /dev/null; then | 668 | if echo "$code" | grep -l "^local $name=" &> /dev/null; then |
669 | echo "Argument '$name' repeated with the value '$value'". >&2 | 669 | echo "Argument '$name' repeated with the value '$value'". >&2 |
670 | return | 670 | return |
671 | fi | 671 | fi |
672 | 672 | ||
673 | # The specification for this argument, used for validations. | 673 | # The specification for this argument, used for validations. |
674 | local arg_line="$(echo "$keyword_args" | | 674 | local arg_line="$(echo "$keyword_args" | |
675 | egrep "^$name:?\??" | head -n1)" | 675 | egrep "^$name:?\??" | head -n1)" |
676 | 676 | ||
677 | # Validate argument and value. | 677 | # Validate argument and value. |
678 | if [[ -z $arg_line ]]; then | 678 | if [[ -z $arg_line ]]; then |
679 | # This argument is not known to us. | 679 | # This argument is not known to us. |
680 | echo "Unknown argument '$name'." >&2 | 680 | echo "Unknown argument '$name'." >&2 |
681 | return | 681 | return |
682 | 682 | ||
683 | elif (echo "$arg_line" | grep -l ':' &> /dev/null) && | 683 | elif (echo "$arg_line" | grep -l ':' &> /dev/null) && |
684 | [[ -z $value ]]; then | 684 | [[ -z $value ]]; then |
685 | # This argument needs a value, but is not provided. | 685 | # This argument needs a value, but is not provided. |
686 | echo "Required argument for '$name' not provided." >&2 | 686 | echo "Required argument for '$name' not provided." >&2 |
687 | return | 687 | return |
688 | 688 | ||
689 | elif (echo "$arg_line" | grep -vl ':' &> /dev/null) && | 689 | elif (echo "$arg_line" | grep -vl ':' &> /dev/null) && |
690 | [[ -n $value ]]; then | 690 | [[ -n $value ]]; then |
691 | # This argument doesn't need a value, but is provided. | 691 | # This argument doesn't need a value, but is provided. |
692 | echo "No argument required for '$name', but provided '$value'." >&2 | 692 | echo "No argument required for '$name', but provided '$value'." >&2 |
693 | return | 693 | return |
694 | 694 | ||
695 | fi | 695 | fi |
696 | 696 | ||
697 | if [[ -z $value ]]; then | 697 | if [[ -z $value ]]; then |
698 | value=true | 698 | value=true |
699 | fi | 699 | fi |
700 | 700 | ||
701 | --add-var "${name//-/_}" "$value" | 701 | --add-var "${name//-/_}" "$value" |
702 | shift | 702 | shift |
703 | done | 703 | done |
704 | 704 | ||
705 | echo "$code" | 705 | echo "$code" |
706 | 706 | ||
707 | unfunction -- --add-var | 707 | unfunction -- --add-var |
708 | 708 | ||
709 | } | 709 | } |
710 | 710 | ||
711 | # Echo the bundle specs as in the record. The first line is not echoed since it | 711 | # Echo the bundle specs as in the record. The first line is not echoed since it |
712 | # is a blank line. | 712 | # is a blank line. |
713 | -antigen-echo-record () { | 713 | -antigen-echo-record () { |
714 | echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p' | 714 | echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p' |
715 | } | 715 | } |
716 | 716 | ||
717 | -antigen-env-setup () { | 717 | -antigen-env-setup () { |
718 | 718 | ||
719 | # Helper function: Same as `export $1=$2`, but will only happen if the name | 719 | # Helper function: Same as `export $1=$2`, but will only happen if the name |
720 | # specified by `$1` is not already set. | 720 | # specified by `$1` is not already set. |
721 | -set-default () { | 721 | -set-default () { |
722 | local arg_name="$1" | 722 | local arg_name="$1" |
723 | local arg_value="$2" | 723 | local arg_value="$2" |
724 | eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" | 724 | eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" |
725 | } | 725 | } |
726 | 726 | ||
727 | # Pre-startup initializations. | 727 | # Pre-startup initializations. |
728 | -set-default ANTIGEN_DEFAULT_REPO_URL \ | 728 | -set-default ANTIGEN_DEFAULT_REPO_URL \ |
729 | https://github.com/robbyrussell/oh-my-zsh.git | 729 | https://github.com/robbyrussell/oh-my-zsh.git |
730 | -set-default ADOTDIR $HOME/.antigen | 730 | -set-default ADOTDIR $HOME/.antigen |
731 | 731 | ||
732 | # Setup antigen's own completion. | 732 | # Setup antigen's own completion. |
733 | compdef _antigen antigen | 733 | compdef _antigen antigen |
734 | 734 | ||
735 | # Remove private functions. | 735 | # Remove private functions. |
736 | unfunction -- -set-default | 736 | unfunction -- -set-default |
737 | } | 737 | } |
738 | 738 | ||
739 | # Setup antigen's autocompletion | 739 | # Setup antigen's autocompletion |
740 | _antigen () { | 740 | _antigen () { |
741 | compadd \ | 741 | compadd \ |
742 | bundle \ | 742 | bundle \ |
743 | bundles \ | 743 | bundles \ |
744 | update \ | 744 | update \ |
745 | revert \ | 745 | revert \ |
746 | list \ | 746 | list \ |
747 | cleanup \ | 747 | cleanup \ |
748 | use \ | 748 | use \ |
749 | selfupdate \ | 749 | selfupdate \ |
750 | theme \ | 750 | theme \ |
751 | apply \ | 751 | apply \ |
752 | snapshot \ | 752 | snapshot \ |
753 | restore \ | 753 | restore \ |
754 | help | 754 | help |
755 | } | 755 | } |
756 | 756 | ||
757 | -antigen-env-setup | 757 | -antigen-env-setup |
758 | 758 |