Commit 43bb2cef16c92712e5c72df5624e5c71edc13c05
1 parent
8685c068da
Variable declaration refactorings to use `local`.
Showing 1 changed file with 11 additions and 12 deletions Inline Diff
antigen.zsh
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 | # <bundle-name>, <repo-url>, <plugin-location>, <repo-local-clone-dir> | 5 | # <bundle-name>, <repo-url>, <plugin-location>, <repo-local-clone-dir> |
6 | # FIXME: Is not kept local by zsh! | 6 | # FIXME: Is not kept local by zsh! |
7 | local bundles="" | 7 | local bundles="" |
8 | 8 | ||
9 | # Syntaxes | 9 | # Syntaxes |
10 | # bundle <url> [<loc>=/] [<name>] | 10 | # bundle <url> [<loc>=/] [<name>] |
11 | bundle () { | 11 | bundle () { |
12 | 12 | ||
13 | # Bundle spec arguments' default values. | 13 | # Bundle spec arguments' default values. |
14 | local url="$ANTIGEN_DEFAULT_REPO_URL" | 14 | local url="$ANTIGEN_DEFAULT_REPO_URL" |
15 | local loc=/ | 15 | local loc=/ |
16 | local name= | 16 | local name= |
17 | local load=true | 17 | local load=true |
18 | 18 | ||
19 | # Set spec values based on the positional arguments. | 19 | # Set spec values based on the positional arguments. |
20 | local position_args='url loc name' | 20 | local position_args='url loc name' |
21 | local i=1 | 21 | local i=1 |
22 | while ! [[ -z $1 || $1 == --*=* ]]; do | 22 | while ! [[ -z $1 || $1 == --*=* ]]; do |
23 | arg_name="$(echo "$position_args" | cut -d\ -f$i)" | 23 | local arg_name="$(echo "$position_args" | cut -d\ -f$i)" |
24 | arg_value="$1" | 24 | local arg_value="$1" |
25 | eval "local $arg_name='$arg_value'" | 25 | eval "local $arg_name='$arg_value'" |
26 | shift | 26 | shift |
27 | i=$(($i + 1)) | 27 | i=$(($i + 1)) |
28 | done | 28 | done |
29 | 29 | ||
30 | # Check if url is just the plugin name. Super short syntax. | 30 | # Check if url is just the plugin name. Super short syntax. |
31 | if [[ "$url" != */* ]]; then | 31 | if [[ "$url" != */* ]]; then |
32 | loc="plugins/$url" | 32 | loc="plugins/$url" |
33 | url="$ANTIGEN_DEFAULT_REPO_URL" | 33 | url="$ANTIGEN_DEFAULT_REPO_URL" |
34 | fi | 34 | fi |
35 | 35 | ||
36 | # Set spec values from keyword arguments, if any. The remaining arguments | 36 | # Set spec values from keyword arguments, if any. The remaining arguments |
37 | # are all assumed to be keyword arguments. | 37 | # are all assumed to be keyword arguments. |
38 | while [[ $1 == --*=* ]]; do | 38 | while [[ $1 == --*=* ]]; do |
39 | arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')" | 39 | local arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')" |
40 | arg_value="$(echo "$1" | cut -d= -f2)" | 40 | local arg_value="$(echo "$1" | cut -d= -f2)" |
41 | eval "local $arg_name='$arg_value'" | 41 | eval "local $arg_name='$arg_value'" |
42 | shift | 42 | shift |
43 | done | 43 | done |
44 | 44 | ||
45 | # Resolve the url. | 45 | # Resolve the url. |
46 | if [[ $url != git://* && $url != https://* ]]; then | 46 | if [[ $url != git://* && $url != https://* ]]; then |
47 | url="${url%.git}" | 47 | url="${url%.git}" |
48 | name="$(basename "$url")" | 48 | name="$(basename "$url")" |
49 | url="https://github.com/$url.git" | 49 | url="https://github.com/$url.git" |
50 | fi | 50 | fi |
51 | 51 | ||
52 | # Plugin's repo will be cloned here. | 52 | # Plugin's repo will be cloned here. |
53 | local clone_dir="$ANTIGEN_REPO_CACHE/$(echo "$url" \ | 53 | local clone_dir="$ANTIGEN_REPO_CACHE/$(echo "$url" \ |
54 | | sed -e 's/\.git$//' -e 's./.-SLASH-.g' -e 's.:.-COLON-.g')" | 54 | | sed -e 's/\.git$//' -e 's./.-SLASH-.g' -e 's.:.-COLON-.g')" |
55 | 55 | ||
56 | # Make an intelligent guess about the name of the plugin, if not already | 56 | # Make an intelligent guess about the name of the plugin, if not already |
57 | # done or is explicitly specified. | 57 | # done or is explicitly specified. |
58 | if [[ -z $name ]]; then | 58 | if [[ -z $name ]]; then |
59 | name="$(basename $url/$loc)" | 59 | name="$(basename $url/$loc)" |
60 | fi | 60 | fi |
61 | 61 | ||
62 | # Add it to the record. | 62 | # Add it to the record. |
63 | bundles="$bundles\n$name $url $loc $clone_dir" | 63 | bundles="$bundles\n$name $url $loc $clone_dir" |
64 | 64 | ||
65 | # Load it, unless specified otherwise. | 65 | # Load it, unless specified otherwise. |
66 | if $load; then | 66 | if $load; then |
67 | bundle-load "$name" | 67 | bundle-load "$name" |
68 | fi | 68 | fi |
69 | } | 69 | } |
70 | 70 | ||
71 | bundle-install () { | 71 | bundle-install () { |
72 | 72 | ||
73 | local update=false | ||
73 | if [[ $1 == --update ]]; then | 74 | if [[ $1 == --update ]]; then |
74 | local update=true | 75 | update=true |
75 | shift | 76 | shift |
76 | else | ||
77 | local update=false | ||
78 | fi | 77 | fi |
79 | 78 | ||
80 | mkdir -p "$ANTIGEN_BUNDLE_DIR" | 79 | mkdir -p "$ANTIGEN_BUNDLE_DIR" |
81 | 80 | ||
82 | local handled_repos="" | 81 | local handled_repos="" |
83 | local install_bundles="" | 82 | local install_bundles="" |
84 | 83 | ||
85 | if [[ $# != 0 ]]; then | 84 | if [[ $# != 0 ]]; then |
86 | # Record and install just the given plugin here and now. | 85 | # Record and install just the given plugin here and now. |
87 | bundle "$@" | 86 | bundle "$@" |
88 | install_bundles="$(echo "$bundles" | tail -1)" | 87 | install_bundles="$(echo "$bundles" | tail -1)" |
89 | else | 88 | else |
90 | # Install all the plugins, previously recorded. | 89 | # Install all the plugins, previously recorded. |
91 | install_bundles="$(echo-non-empty "$bundles")" | 90 | install_bundles="$(echo-non-empty "$bundles")" |
92 | fi | 91 | fi |
93 | 92 | ||
94 | # If the above `if` is directly piped to the below `while`, the contents | 93 | # If the above `if` is directly piped to the below `while`, the contents |
95 | # inside the `if` construct are run in a new subshell, so changes to the | 94 | # inside the `if` construct are run in a new subshell, so changes to the |
96 | # `$bundles` variable are lost after the `if` construct finishes. So, we | 95 | # `$bundles` variable are lost after the `if` construct finishes. So, we |
97 | # need the temporary `$install_bundles` variable. | 96 | # need the temporary `$install_bundles` variable. |
98 | echo "$install_bundles" | while read spec; do | 97 | echo "$install_bundles" | while read spec; do |
99 | 98 | ||
100 | local name="$(echo "$spec" | awk '{print $1}')" | 99 | local name="$(echo "$spec" | awk '{print $1}')" |
101 | local url="$(echo "$spec" | awk '{print $2}')" | 100 | local url="$(echo "$spec" | awk '{print $2}')" |
102 | local loc="$(echo "$spec" | awk '{print $3}')" | 101 | local loc="$(echo "$spec" | awk '{print $3}')" |
103 | local clone_dir="$(echo "$spec" | awk '{print $4}')" | 102 | local clone_dir="$(echo "$spec" | awk '{print $4}')" |
104 | 103 | ||
105 | if [[ -z "$(echo "$handled_repos" | grep -Fm1 "$url")" ]]; then | 104 | if [[ -z "$(echo "$handled_repos" | grep -Fm1 "$url")" ]]; then |
106 | if [[ ! -d $clone_dir ]]; then | 105 | if [[ ! -d $clone_dir ]]; then |
107 | git clone "$url" "$clone_dir" | 106 | git clone "$url" "$clone_dir" |
108 | elif $update; then | 107 | elif $update; then |
109 | git --git-dir "$clone_dir/.git" pull | 108 | git --git-dir "$clone_dir/.git" pull |
110 | fi | 109 | fi |
111 | 110 | ||
112 | handled_repos="$handled_repos\n$url" | 111 | handled_repos="$handled_repos\n$url" |
113 | fi | 112 | fi |
114 | 113 | ||
115 | if [[ $name != *.theme ]]; then | 114 | if [[ $name != *.theme ]]; then |
116 | echo Installing $name | 115 | echo Installing $name |
117 | local bundle_dest="$ANTIGEN_BUNDLE_DIR/$name" | 116 | local bundle_dest="$ANTIGEN_BUNDLE_DIR/$name" |
118 | test -e "$bundle_dest" && rm -rf "$bundle_dest" | 117 | test -e "$bundle_dest" && rm -rf "$bundle_dest" |
119 | ln -s "$clone_dir/$loc" "$bundle_dest" | 118 | ln -s "$clone_dir/$loc" "$bundle_dest" |
120 | else | 119 | else |
121 | mkdir -p "$ANTIGEN_BUNDLE_DIR/$name" | 120 | mkdir -p "$ANTIGEN_BUNDLE_DIR/$name" |
122 | cp "$clone_dir/$loc" "$ANTIGEN_BUNDLE_DIR/$name" | 121 | cp "$clone_dir/$loc" "$ANTIGEN_BUNDLE_DIR/$name" |
123 | fi | 122 | fi |
124 | 123 | ||
125 | bundle-load "$name" | 124 | bundle-load "$name" |
126 | 125 | ||
127 | done | 126 | done |
128 | 127 | ||
129 | # Initialize completions after installing | 128 | # Initialize completions after installing |
130 | bundle-apply | 129 | bundle-apply |
131 | 130 | ||
132 | } | 131 | } |
133 | 132 | ||
134 | bundle-install! () { | 133 | bundle-install! () { |
135 | bundle-install --update | 134 | bundle-install --update |
136 | } | 135 | } |
137 | 136 | ||
138 | bundle-cleanup () { | 137 | bundle-cleanup () { |
139 | 138 | ||
140 | # Find directores in ANTIGEN_BUNDLE_DIR, that are not in the bundles record. | 139 | # Find directores in ANTIGEN_BUNDLE_DIR, that are not in the bundles record. |
141 | local unidentified_bundles="$(comm -13 \ | 140 | local unidentified_bundles="$(comm -13 \ |
142 | <(echo-non-empty "$bundles" | awk '{print $1}' | sort) \ | 141 | <(echo-non-empty "$bundles" | awk '{print $1}' | sort) \ |
143 | <(ls -1 "$ANTIGEN_BUNDLE_DIR"))" | 142 | <(ls -1 "$ANTIGEN_BUNDLE_DIR"))" |
144 | 143 | ||
145 | if [[ -z $unidentified_bundles ]]; then | 144 | if [[ -z $unidentified_bundles ]]; then |
146 | echo "You don't have any unidentified bundles." | 145 | echo "You don't have any unidentified bundles." |
147 | return 0 | 146 | return 0 |
148 | fi | 147 | fi |
149 | 148 | ||
150 | echo The following bundles are not recorded: | 149 | echo The following bundles are not recorded: |
151 | echo "$unidentified_bundles" | sed 's/^/ /' | 150 | echo "$unidentified_bundles" | sed 's/^/ /' |
152 | 151 | ||
153 | echo -n '\nDelete them all? [y/N] ' | 152 | echo -n '\nDelete them all? [y/N] ' |
154 | if read -q; then | 153 | if read -q; then |
155 | echo | 154 | echo |
156 | echo | 155 | echo |
157 | echo "$unidentified_bundles" | while read name; do | 156 | echo "$unidentified_bundles" | while read name; do |
158 | echo -n Deleting $name... | 157 | echo -n Deleting $name... |
159 | rm -rf "$ANTIGEN_BUNDLE_DIR/$name" | 158 | rm -rf "$ANTIGEN_BUNDLE_DIR/$name" |
160 | echo ' done.' | 159 | echo ' done.' |
161 | done | 160 | done |
162 | else | 161 | else |
163 | echo | 162 | echo |
164 | echo Nothing deleted. | 163 | echo Nothing deleted. |
165 | fi | 164 | fi |
166 | } | 165 | } |
167 | 166 | ||
168 | bundle-load () { | 167 | bundle-load () { |
169 | 168 | ||
170 | name="$1" | 169 | local name="$1" |
171 | bundle_dir="$ANTIGEN_BUNDLE_DIR/$name" | 170 | local bundle_dir="$ANTIGEN_BUNDLE_DIR/$name" |
172 | 171 | ||
173 | # Source the plugin script | 172 | # Source the plugin script |
174 | script_loc="$bundle_dir/$name.plugin.zsh" | 173 | local script_loc="$bundle_dir/$name.plugin.zsh" |
175 | if [[ -f $script_loc ]]; then | 174 | if [[ -f $script_loc ]]; then |
176 | source "$script_loc" | 175 | source "$script_loc" |
177 | fi | 176 | fi |
178 | 177 | ||
179 | # If the name of the plugin ends with `.lib`, all the *.zsh files in it are | 178 | # If the name of the plugin ends with `.lib`, all the *.zsh files in it are |
180 | # sourced. This is kind of a hack to source the libraries of oh-my-zsh. | 179 | # sourced. This is kind of a hack to source the libraries of oh-my-zsh. |
181 | if [[ $name == *.lib ]]; then | 180 | if [[ $name == *.lib ]]; then |
182 | # FIXME: This throws an error if no files match the given glob pattern. | 181 | # FIXME: This throws an error if no files match the given glob pattern. |
183 | for lib ($bundle_dir/*.zsh) source $lib | 182 | for lib ($bundle_dir/*.zsh) source $lib |
184 | fi | 183 | fi |
185 | 184 | ||
186 | # If the name ends with `.theme`, it is handled as if it were a zsh-theme | 185 | # If the name ends with `.theme`, it is handled as if it were a zsh-theme |
187 | # plugin. | 186 | # plugin. |
188 | if [[ $name == *.theme ]]; then | 187 | if [[ $name == *.theme ]]; then |
189 | local theme_file="$bundle_dir/${name%.theme}.zsh-theme" | 188 | local theme_file="$bundle_dir/${name%.theme}.zsh-theme" |
190 | test -f "$theme_file" && source "$theme_file" | 189 | test -f "$theme_file" && source "$theme_file" |
191 | fi | 190 | fi |
192 | 191 | ||
193 | # Add to $fpath, for completion(s) | 192 | # Add to $fpath, for completion(s) |
194 | fpath=($bundle_dir $fpath) | 193 | fpath=($bundle_dir $fpath) |
195 | 194 | ||
196 | } | 195 | } |
197 | 196 | ||
198 | bundle-lib () { | 197 | bundle-lib () { |
199 | bundle --name=oh-my-zsh.lib --loc=lib | 198 | bundle --name=oh-my-zsh.lib --loc=lib |
200 | } | 199 | } |
201 | 200 | ||
202 | bundle-theme () { | 201 | bundle-theme () { |
203 | local url="$ANTIGEN_DEFAULT_REPO_URL" | 202 | local url="$ANTIGEN_DEFAULT_REPO_URL" |
204 | local name="${1:-robbyrussell}" | 203 | local name="${1:-robbyrussell}" |
205 | bundle-install "$url" --name=$name.theme --loc=themes/$name.zsh-theme | 204 | bundle-install "$url" --name=$name.theme --loc=themes/$name.zsh-theme |
206 | } | 205 | } |
207 | 206 | ||
208 | bundle-apply () { | 207 | bundle-apply () { |
209 | # Initialize completion. | 208 | # Initialize completion. |
210 | compinit -i | 209 | compinit -i |
211 | } | 210 | } |
212 | 211 | ||
213 | # Does what it says. | 212 | # Does what it says. |
214 | echo-non-empty () { | 213 | echo-non-empty () { |
215 | echo "$@" | while read line; do | 214 | echo "$@" | while read line; do |
216 | [[ $line != "" ]] && echo $line | 215 | [[ $line != "" ]] && echo $line |
217 | done | 216 | done |
218 | } | 217 | } |
219 | 218 | ||
220 | -bundle-env-setup () { | 219 | -bundle-env-setup () { |
221 | # Pre-startup initializations | 220 | # Pre-startup initializations |
222 | -set-default ANTIGEN_DEFAULT_REPO_URL \ | 221 | -set-default ANTIGEN_DEFAULT_REPO_URL \ |
223 | https://github.com/robbyrussell/oh-my-zsh.git | 222 | https://github.com/robbyrussell/oh-my-zsh.git |
224 | -set-default ANTIGEN_REPO_CACHE $HOME/.antigen/cache | 223 | -set-default ANTIGEN_REPO_CACHE $HOME/.antigen/cache |
225 | -set-default ANTIGEN_BUNDLE_DIR $HOME/.antigen/bundles | 224 | -set-default ANTIGEN_BUNDLE_DIR $HOME/.antigen/bundles |
226 | 225 | ||
227 | # Load the compinit module | 226 | # Load the compinit module |
228 | autoload -U compinit | 227 | autoload -U compinit |
229 | 228 | ||
230 | # Without the following, `compdef` function is not defined. | 229 | # Without the following, `compdef` function is not defined. |
231 | compinit -i | 230 | compinit -i |
232 | } | 231 | } |
233 | 232 | ||
234 | # Same as `export $1=$2`, but will only happen if the name specified by `$1` is | 233 | # Same as `export $1=$2`, but will only happen if the name specified by `$1` is |
235 | # not already set. | 234 | # not already set. |
236 | -set-default () { | 235 | -set-default () { |
237 | arg_name="$1" | 236 | local arg_name="$1" |
238 | arg_value="$2" | 237 | local arg_value="$2" |
239 | eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" | 238 | eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" |
240 | } | 239 | } |
241 | 240 | ||
242 | -bundle-env-setup | 241 | -bundle-env-setup |