Commit 382d9209623921567828bf4406503397ee7ee55c

Authored by Shrikant Sharat
1 parent 062a7b830d

Use local variables in functions.

Showing 1 changed file with 2 additions and 2 deletions Inline Diff

1 #!/bin/zsh 1 #!/bin/zsh
2 2
3 # Each line in this string has the following entries separated by a space 3 # Each line in this string has the following entries separated by a space
4 # character. 4 # character.
5 # <repo-url>, <plugin-location>, <bundle-type> 5 # <repo-url>, <plugin-location>, <bundle-type>
6 # FIXME: Is not kept local by zsh! 6 # FIXME: Is not kept local by zsh!
7 local _ANTIGEN_BUNDLE_RECORD="" 7 local _ANTIGEN_BUNDLE_RECORD=""
8 8
9 # Syntaxes 9 # Syntaxes
10 # antigen-bundle <url> [<loc>=/] 10 # antigen-bundle <url> [<loc>=/]
11 # Keyword only arguments: 11 # Keyword only arguments:
12 # branch - The branch of the repo to use for this bundle. 12 # branch - The branch of the repo to use for this bundle.
13 antigen-bundle () { 13 antigen-bundle () {
14 14
15 # Bundle spec arguments' default values. 15 # Bundle spec arguments' default values.
16 local url="$ANTIGEN_DEFAULT_REPO_URL" 16 local url="$ANTIGEN_DEFAULT_REPO_URL"
17 local loc=/ 17 local loc=/
18 local branch= 18 local branch=
19 local no_local_clone=false 19 local no_local_clone=false
20 local btype=plugin 20 local btype=plugin
21 21
22 # Set spec values based on the positional arguments. 22 # Set spec values based on the positional arguments.
23 local position_args 23 local position_args
24 position_args=(url loc) 24 position_args=(url loc)
25 local i=1 25 local i=1
26 while ! [[ -z $1 || $1 == --* ]]; do 26 while ! [[ -z $1 || $1 == --* ]]; do
27 local arg_name="${position_args[$i]}" 27 local arg_name="${position_args[$i]}"
28 local arg_value="$1" 28 local arg_value="$1"
29 eval "local $arg_name='$arg_value'" 29 eval "local $arg_name='$arg_value'"
30 shift 30 shift
31 i=$(($i + 1)) 31 i=$(($i + 1))
32 done 32 done
33 33
34 # Set spec values from keyword arguments, if any. The remaining arguments 34 # Set spec values from keyword arguments, if any. The remaining arguments
35 # are all assumed to be keyword arguments. 35 # are all assumed to be keyword arguments.
36 while [[ $1 == --* ]]; do 36 while [[ $1 == --* ]]; do
37 # Remove the `--` at the start. 37 # Remove the `--` at the start.
38 local arg="${1#--}" 38 local arg="${1#--}"
39 39
40 if [[ $arg != *=* ]]; then 40 if [[ $arg != *=* ]]; then
41 arg="$arg=true" 41 arg="$arg=true"
42 fi 42 fi
43 43
44 # Get the name of the arg and replace the `-`'s to `_`'s. 44 # Get the name of the arg and replace the `-`'s to `_`'s.
45 local arg_name="${${arg%\=*}//-/_}" 45 local arg_name="${${arg%\=*}//-/_}"
46 46
47 # Get the value of the arg. 47 # Get the value of the arg.
48 local arg_value="${arg#*\=}" 48 local arg_value="${arg#*\=}"
49 49
50 eval "local $arg_name='$arg_value'" 50 eval "local $arg_name='$arg_value'"
51 shift 51 shift
52 done 52 done
53 53
54 # Check if url is just the plugin name. Super short syntax. 54 # Check if url is just the plugin name. Super short syntax.
55 if [[ "$url" != */* ]]; then 55 if [[ "$url" != */* ]]; then
56 loc="plugins/$url" 56 loc="plugins/$url"
57 url="$ANTIGEN_DEFAULT_REPO_URL" 57 url="$ANTIGEN_DEFAULT_REPO_URL"
58 fi 58 fi
59 59
60 # Resolve the url. 60 # Resolve the url.
61 url="$(-antigen-resolve-bundle-url "$url")" 61 url="$(-antigen-resolve-bundle-url "$url")"
62 62
63 # Add the branch information to the url. 63 # Add the branch information to the url.
64 if [[ ! -z $branch ]]; then 64 if [[ ! -z $branch ]]; then
65 url="$url|$branch" 65 url="$url|$branch"
66 fi 66 fi
67 67
68 # The `make_local_clone` variable better represents whether there should be 68 # The `make_local_clone` variable better represents whether there should be
69 # a local clone made. For cloning to be avoided, firstly, the `$url` should 69 # a local clone made. For cloning to be avoided, firstly, the `$url` should
70 # be an absolute local path and `$branch` should be empty. In addition to 70 # be an absolute local path and `$branch` should be empty. In addition to
71 # these two conditions, either the `--no-local-clone` option should be 71 # these two conditions, either the `--no-local-clone` option should be
72 # given, or `$url` should not a git repo. 72 # given, or `$url` should not a git repo.
73 local make_local_clone=true 73 local make_local_clone=true
74 if [[ $url == /* && -z $branch \ 74 if [[ $url == /* && -z $branch \
75 && ( $no_local_clone == true || ! -d $url/.git ) ]]; then 75 && ( $no_local_clone == true || ! -d $url/.git ) ]]; then
76 make_local_clone=false 76 make_local_clone=false
77 fi 77 fi
78 78
79 # Add it to the record. 79 # Add it to the record.
80 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype $make_local_clone" 80 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype $make_local_clone"
81 81
82 # Ensure a clone exists for this repo, if needed. 82 # Ensure a clone exists for this repo, if needed.
83 if $make_local_clone; then 83 if $make_local_clone; then
84 -antigen-ensure-repo "$url" 84 -antigen-ensure-repo "$url"
85 fi 85 fi
86 86
87 # Load the plugin. 87 # Load the plugin.
88 -antigen-load "$url" "$loc" "$btype" "$make_local_clone" 88 -antigen-load "$url" "$loc" "$btype" "$make_local_clone"
89 89
90 } 90 }
91 91
92 -antigen-resolve-bundle-url () { 92 -antigen-resolve-bundle-url () {
93 # Given an acceptable short/full form of a bundle's repo url, this function 93 # Given an acceptable short/full form of a bundle's repo url, this function
94 # echoes the full form of the repo's clone url. 94 # echoes the full form of the repo's clone url.
95 95
96 local url="$1" 96 local url="$1"
97 97
98 # Expand short github url syntax: `username/reponame` 98 # Expand short github url syntax: `username/reponame`
99 if [[ $url != git://* && \ 99 if [[ $url != git://* && \
100 $url != https://* && \ 100 $url != https://* && \
101 $url != /* && \ 101 $url != /* && \
102 $url != git@github.com:*/* 102 $url != git@github.com:*/*
103 ]]; then 103 ]]; then
104 url="https://github.com/${url%.git}.git" 104 url="https://github.com/${url%.git}.git"
105 fi 105 fi
106 106
107 echo "$url" 107 echo "$url"
108 } 108 }
109 109
110 antigen-bundles () { 110 antigen-bundles () {
111 # Bulk add many bundles at one go. Empty lines and lines starting with a `#` 111 # Bulk add many bundles at one go. Empty lines and lines starting with a `#`
112 # are ignored. Everything else is given to `antigen-bundle` as is, no 112 # are ignored. Everything else is given to `antigen-bundle` as is, no
113 # quoting rules applied. 113 # quoting rules applied.
114 114
115 local line 115 local line
116 116
117 grep -v '^\s*$\|^#' | while read line; do 117 grep -v '^\s*$\|^#' | while read line; do
118 # Using `eval` so that we can use the shell-style quoting in each line 118 # Using `eval` so that we can use the shell-style quoting in each line
119 # piped to `antigen-bundles`. 119 # piped to `antigen-bundles`.
120 eval "antigen-bundle $line" 120 eval "antigen-bundle $line"
121 done 121 done
122 } 122 }
123 123
124 antigen-update () { 124 antigen-update () {
125 # Update your bundles, i.e., `git pull` in all the plugin repos. 125 # Update your bundles, i.e., `git pull` in all the plugin repos.
126 -antigen-echo-record \ 126 -antigen-echo-record \
127 | awk '{print $1}' \ 127 | awk '{print $1}' \
128 | sort -u \ 128 | sort -u \
129 | while read url; do 129 | while read url; do
130 echo "**** Pulling $url" 130 echo "**** Pulling $url"
131 -antigen-ensure-repo --update --verbose "$url" 131 -antigen-ensure-repo --update --verbose "$url"
132 echo 132 echo
133 done 133 done
134 } 134 }
135 135
136 -antigen-get-clone-dir () { 136 -antigen-get-clone-dir () {
137 # Takes a repo url and gives out the path that this url needs to be cloned 137 # Takes a repo url and gives out the path that this url needs to be cloned
138 # to. Doesn't actually clone anything. 138 # to. Doesn't actually clone anything.
139 # TODO: Memoize? 139 # TODO: Memoize?
140 140
141 # The url given. 141 # The url given.
142 local url="$1" 142 local url="$1"
143 143
144 # Echo the full path to the clone directory. 144 # Echo the full path to the clone directory.
145 echo -n $ADOTDIR/repos/ 145 echo -n $ADOTDIR/repos/
146 echo "$url" | sed \ 146 echo "$url" | sed \
147 -e 's./.-SLASH-.g' \ 147 -e 's./.-SLASH-.g' \
148 -e 's.:.-COLON-.g' \ 148 -e 's.:.-COLON-.g' \
149 -e 's.|.-PIPE-.g' 149 -e 's.|.-PIPE-.g'
150 } 150 }
151 151
152 -antigen-get-clone-url () { 152 -antigen-get-clone-url () {
153 # Takes a repo's clone dir and gives out the repo's original url that was 153 # Takes a repo's clone dir and gives out the repo's original url that was
154 # used to create the given directory path. 154 # used to create the given directory path.
155 # TODO: Memoize? 155 # TODO: Memoize?
156 echo "$1" | sed \ 156 echo "$1" | sed \
157 -e "s:^$ADOTDIR/repos/::" \ 157 -e "s:^$ADOTDIR/repos/::" \
158 -e 's.-SLASH-./.g' \ 158 -e 's.-SLASH-./.g' \
159 -e 's.-COLON-.:.g' \ 159 -e 's.-COLON-.:.g' \
160 -e 's.-PIPE-.|.g' 160 -e 's.-PIPE-.|.g'
161 } 161 }
162 162
163 -antigen-ensure-repo () { 163 -antigen-ensure-repo () {
164 164
165 # Ensure that a clone exists for the given repo url and branch. If the first 165 # Ensure that a clone exists for the given repo url and branch. If the first
166 # argument is `--update` and if a clone already exists for the given repo 166 # argument is `--update` and if a clone already exists for the given repo
167 # and branch, it is pull-ed, i.e., updated. 167 # and branch, it is pull-ed, i.e., updated.
168 168
169 # Argument defaults. 169 # Argument defaults.
170 # Check if we have to update. 170 # Check if we have to update.
171 local update=false 171 local update=false
172 # Verbose output. 172 # Verbose output.
173 local verbose=false 173 local verbose=false
174 174
175 # Load any boolean arguments specified. 175 # Load any boolean arguments specified.
176 while [[ $1 == --* ]]; do 176 while [[ $1 == --* ]]; do
177 eval "local '${1#--}=true'" 177 eval "local '${1#--}=true'"
178 shift 178 shift
179 done 179 done
180 180
181 # Get the clone's directory as per the given repo url and branch. 181 # Get the clone's directory as per the given repo url and branch.
182 local url="$1" 182 local url="$1"
183 local clone_dir="$(-antigen-get-clone-dir $url)" 183 local clone_dir="$(-antigen-get-clone-dir $url)"
184 184
185 # A temporary function wrapping the `git` command with repeated arguments. 185 # A temporary function wrapping the `git` command with repeated arguments.
186 --plugin-git () { 186 --plugin-git () {
187 eval git --git-dir=$clone_dir/.git --work-tree=$clone_dir "$@" 187 eval git --git-dir=$clone_dir/.git --work-tree=$clone_dir "$@"
188 } 188 }
189 189
190 # Clone if it doesn't already exist. 190 # Clone if it doesn't already exist.
191 if [[ ! -d $clone_dir ]]; then 191 if [[ ! -d $clone_dir ]]; then
192 git clone "${url%|*}" "$clone_dir" 192 git clone "${url%|*}" "$clone_dir"
193 elif $update; then 193 elif $update; then
194 # Save current revision. 194 # Save current revision.
195 old_rev="$(--plugin-git rev-parse HEAD)" 195 local old_rev="$(--plugin-git rev-parse HEAD)"
196 # Pull changes if update requested. 196 # Pull changes if update requested.
197 --plugin-git pull 197 --plugin-git pull
198 # Get the new revision. 198 # Get the new revision.
199 new_rev="$(--plugin-git rev-parse HEAD)" 199 local new_rev="$(--plugin-git rev-parse HEAD)"
200 fi 200 fi
201 201
202 # If its a specific branch that we want, checkout that branch. 202 # If its a specific branch that we want, checkout that branch.
203 if [[ $url == *\|* ]]; then 203 if [[ $url == *\|* ]]; then
204 --plugin-git checkout "${url#*|}" 204 --plugin-git checkout "${url#*|}"
205 fi 205 fi
206 206
207 if ! [[ -z $old_rev || $old_rev == $new_rev ]]; then 207 if ! [[ -z $old_rev || $old_rev == $new_rev ]]; then
208 echo Updated from ${old_rev:0:7} to ${new_rev:0:7}. 208 echo Updated from ${old_rev:0:7} to ${new_rev:0:7}.
209 if $verbose; then 209 if $verbose; then
210 --plugin-git log --oneline --reverse --no-merges --stat '@{1}..' 210 --plugin-git log --oneline --reverse --no-merges --stat '@{1}..'
211 fi 211 fi
212 fi 212 fi
213 213
214 unfunction -- --plugin-git 214 unfunction -- --plugin-git
215 215
216 } 216 }
217 217
218 -antigen-load () { 218 -antigen-load () {
219 219
220 local url="$1" 220 local url="$1"
221 local loc="$2" 221 local loc="$2"
222 local btype="$3" 222 local btype="$3"
223 local make_local_clone="$4" 223 local make_local_clone="$4"
224 224
225 # The full location where the plugin is located. 225 # The full location where the plugin is located.
226 local location 226 local location
227 if $make_local_clone; then 227 if $make_local_clone; then
228 location="$(-antigen-get-clone-dir "$url")/$loc" 228 location="$(-antigen-get-clone-dir "$url")/$loc"
229 else 229 else
230 location="$url" 230 location="$url"
231 fi 231 fi
232 232
233 if [[ $btype == theme ]]; then 233 if [[ $btype == theme ]]; then
234 234
235 # Of course, if its a theme, the location would point to the script 235 # Of course, if its a theme, the location would point to the script
236 # file. 236 # file.
237 source "$location" 237 source "$location"
238 238
239 else 239 else
240 240
241 # Source the plugin script 241 # Source the plugin script
242 # FIXME: I don't know. Looks very very ugly. Needs a better 242 # FIXME: I don't know. Looks very very ugly. Needs a better
243 # implementation once tests are ready. 243 # implementation once tests are ready.
244 local script_loc="$(ls "$location" | grep -m1 '\.plugin\.zsh$')" 244 local script_loc="$(ls "$location" | grep -m1 '\.plugin\.zsh$')"
245 245
246 if [[ -f $script_loc ]]; then 246 if [[ -f $script_loc ]]; then
247 # If we have a `*.plugin.zsh`, source it. 247 # If we have a `*.plugin.zsh`, source it.
248 source "$script_loc" 248 source "$script_loc"
249 249
250 elif [[ ! -z "$(ls "$location" | grep -m1 '\.zsh$')" ]]; then 250 elif [[ ! -z "$(ls "$location" | grep -m1 '\.zsh$')" ]]; then
251 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` 251 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh`
252 # files. 252 # files.
253 for script ($location/*.zsh(N)) source "$script" 253 for script ($location/*.zsh(N)) source "$script"
254 254
255 elif [[ ! -z "$(ls "$location" | grep -m1 '\.sh$')" ]]; then 255 elif [[ ! -z "$(ls "$location" | grep -m1 '\.sh$')" ]]; then
256 # If there are no `*.zsh` files either, we look for and source any 256 # If there are no `*.zsh` files either, we look for and source any
257 # `*.sh` files instead. 257 # `*.sh` files instead.
258 for script ($location/*.sh(N)) source "$script" 258 for script ($location/*.sh(N)) source "$script"
259 259
260 fi 260 fi
261 261
262 # Add to $fpath, for completion(s). 262 # Add to $fpath, for completion(s).
263 fpath=($location $fpath) 263 fpath=($location $fpath)
264 264
265 fi 265 fi
266 266
267 } 267 }
268 268
269 antigen-cleanup () { 269 antigen-cleanup () {
270 270
271 # Cleanup unused repositories. 271 # Cleanup unused repositories.
272 272
273 local force=false 273 local force=false
274 if [[ $1 == --force ]]; then 274 if [[ $1 == --force ]]; then
275 force=true 275 force=true
276 fi 276 fi
277 277
278 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then 278 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then
279 echo "You don't have any bundles." 279 echo "You don't have any bundles."
280 return 0 280 return 0
281 fi 281 fi
282 282
283 # Find directores in ADOTDIR/repos, that are not in the bundles record. 283 # Find directores in ADOTDIR/repos, that are not in the bundles record.
284 local unused_clones="$(comm -13 \ 284 local unused_clones="$(comm -13 \
285 <(-antigen-echo-record \ 285 <(-antigen-echo-record \
286 | awk '$4 == "true" {print $1}' \ 286 | awk '$4 == "true" {print $1}' \
287 | while read line; do 287 | while read line; do
288 -antigen-get-clone-dir "$line" 288 -antigen-get-clone-dir "$line"
289 done \ 289 done \
290 | sort -u) \ 290 | sort -u) \
291 <(ls -d "$ADOTDIR/repos/"* | sort -u))" 291 <(ls -d "$ADOTDIR/repos/"* | sort -u))"
292 292
293 if [[ -z $unused_clones ]]; then 293 if [[ -z $unused_clones ]]; then
294 echo "You don't have any unidentified bundles." 294 echo "You don't have any unidentified bundles."
295 return 0 295 return 0
296 fi 296 fi
297 297
298 echo 'You have clones for the following repos, but are not used.' 298 echo 'You have clones for the following repos, but are not used.'
299 echo "$unused_clones" \ 299 echo "$unused_clones" \
300 | while read line; do 300 | while read line; do
301 -antigen-get-clone-url "$line" 301 -antigen-get-clone-url "$line"
302 done \ 302 done \
303 | sed -e 's/^/ /' -e 's/|/, branch /' 303 | sed -e 's/^/ /' -e 's/|/, branch /'
304 304
305 if $force || (echo -n '\nDelete them all? [y/N] '; read -q); then 305 if $force || (echo -n '\nDelete them all? [y/N] '; read -q); then
306 echo 306 echo
307 echo 307 echo
308 echo "$unused_clones" | while read line; do 308 echo "$unused_clones" | while read line; do
309 echo -n "Deleting clone for $(-antigen-get-clone-url "$line")..." 309 echo -n "Deleting clone for $(-antigen-get-clone-url "$line")..."
310 rm -rf "$line" 310 rm -rf "$line"
311 echo ' done.' 311 echo ' done.'
312 done 312 done
313 else 313 else
314 echo 314 echo
315 echo Nothing deleted. 315 echo Nothing deleted.
316 fi 316 fi
317 } 317 }
318 318
319 antigen-lib () { 319 antigen-lib () {
320 antigen-bundle --loc=lib 320 antigen-bundle --loc=lib
321 } 321 }
322 322
323 antigen-theme () { 323 antigen-theme () {
324 local name="${1:-robbyrussell}" 324 local name="${1:-robbyrussell}"
325 antigen-bundle --loc=themes/$name.zsh-theme --btype=theme 325 antigen-bundle --loc=themes/$name.zsh-theme --btype=theme
326 } 326 }
327 327
328 antigen-apply () { 328 antigen-apply () {
329 # Initialize completion. 329 # Initialize completion.
330 # TODO: Only load completions if there are any changes to the bundle 330 # TODO: Only load completions if there are any changes to the bundle
331 # repositories. 331 # repositories.
332 compinit -i 332 compinit -i
333 } 333 }
334 334
335 antigen-list () { 335 antigen-list () {
336 # List all currently installed bundles 336 # List all currently installed bundles
337 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then 337 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then
338 echo "You don't have any bundles." >&2 338 echo "You don't have any bundles." >&2
339 return 1 339 return 1
340 else 340 else
341 -antigen-echo-record | sort -u 341 -antigen-echo-record | sort -u
342 fi 342 fi
343 } 343 }
344 344
345 antigen-help () { 345 antigen-help () {
346 cat <<EOF 346 cat <<EOF
347 Antigen is a plugin management system for zsh. It makes it easy to grab awesome 347 Antigen is a plugin management system for zsh. It makes it easy to grab awesome
348 shell scripts and utilities, put up on github. For further details and complete 348 shell scripts and utilities, put up on github. For further details and complete
349 documentation, visit the project's page at 'http://antigen.sharats.me'. 349 documentation, visit the project's page at 'http://antigen.sharats.me'.
350 EOF 350 EOF
351 } 351 }
352 352
353 # A syntax sugar to avoid the `-` when calling antigen commands. With this 353 # A syntax sugar to avoid the `-` when calling antigen commands. With this
354 # function, you can write `antigen-bundle` as `antigen bundle` and so on. 354 # function, you can write `antigen-bundle` as `antigen bundle` and so on.
355 antigen () { 355 antigen () {
356 local cmd="$1" 356 local cmd="$1"
357 shift 357 shift
358 "antigen-$cmd" "$@" 358 "antigen-$cmd" "$@"
359 } 359 }
360 360
361 # Echo the bundle specs as in the record. The first line is not echoed since it 361 # Echo the bundle specs as in the record. The first line is not echoed since it
362 # is a blank line. 362 # is a blank line.
363 -antigen-echo-record () { 363 -antigen-echo-record () {
364 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p' 364 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p'
365 } 365 }
366 366
367 -antigen-env-setup () { 367 -antigen-env-setup () {
368 # Pre-startup initializations 368 # Pre-startup initializations
369 -set-default ANTIGEN_DEFAULT_REPO_URL \ 369 -set-default ANTIGEN_DEFAULT_REPO_URL \
370 https://github.com/robbyrussell/oh-my-zsh.git 370 https://github.com/robbyrussell/oh-my-zsh.git
371 -set-default ADOTDIR $HOME/.antigen 371 -set-default ADOTDIR $HOME/.antigen
372 372
373 # Load the compinit module 373 # Load the compinit module
374 autoload -U compinit 374 autoload -U compinit
375 375
376 # Without the following, `compdef` function is not defined. 376 # Without the following, `compdef` function is not defined.
377 compinit -i 377 compinit -i
378 } 378 }
379 379
380 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is 380 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is
381 # not already set. 381 # not already set.
382 -set-default () { 382 -set-default () {
383 local arg_name="$1" 383 local arg_name="$1"
384 local arg_value="$2" 384 local arg_value="$2"
385 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" 385 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'"
386 } 386 }
387 387
388 -antigen-env-setup 388 -antigen-env-setup
389 389