d41ac7fe3e52783054610c2cc57bbbfcdb2202b0
- Author
- Markus Faerevaag <mf@bitblueprint.com>
- Committer
- Markus Faerevaag <mf@bitblueprint.com>
- Date
Message
Diff
This diff is truncated to protect this page.
1diff --git a/plugins/wd/_wd.sh b/plugins/wd/_wd.sh
2index 9505644356e0146cbdb8dbd1e422de9bc156ab36..29df63520684f00cdb86adfea6c5785a816d6c9d 100644
3--- a/plugins/wd/_wd.sh
4+++ b/plugins/wd/_wd.sh
5@@ -1,48 +1,68 @@
6-#compdef wd.sh
7-
8-zstyle ":completion:*:descriptions" format "%B%d%b"
9-
10-CONFIG=$HOME/.warprc
11-
12-local -a main_commands
13-main_commands=(
14- add:'Adds the current working directory to your warp points'
15- #add'\!':'Overwrites existing warp point' # TODO: Fix
16- rm:'Removes the given warp point'
17- ls:'Outputs all stored warp points'
18- show:'Outputs warp points to current directory'
19-)
20-
21-local -a points
22-while read line
23-do
24- points+=$(awk "{ gsub(/\/Users\/$USER|\/home\/$USER/,\"~\"); print }" <<< $line)
25-done < $CONFIG
26-
27-_wd()
28-{
29- # init variables
30- local curcontext="$curcontext" state line
31- typeset -A opt_args
32-
33- # init state
34- _arguments \
35- '1: :->command' \
36- '2: :->argument'
37-
38- case $state in
39- command)
40- compadd "$@" add rm ls show
41- _describe -t warp-points 'Warp points:' points && ret=0
42- ;;
43- argument)
44- case $words[2] in
45- rm|add!)
46- _describe -t warp-points 'warp points' points && ret=0
47- ;;
48- *)
49- esac
50- esac
51+#compdef wd
52+
53+zstyle ':completion:*:descriptions' format '%B%d%b'
54+zstyle ':completion::complete:wd:*:commands' group-name commands
55+zstyle ':completion::complete:wd:*:warp_points' group-name warp_points
56+zstyle ':completion::complete:wd::' list-grouped
57+
58+# Call `_wd()` when when trying to complete the command `wd`
59+
60+zmodload zsh/mapfile
61+function _wd() {
62+ local ret=1
63+ local CONFIG=$HOME/.warprc
64+
65+ # Stolen from
66+ # http://stackoverflow.com/questions/9000698/completion-when-program-has-sub-commands
67+
68+ # local curcontext="$curcontext" state line
69+ # typeset -A opt_args
70+
71+ local -a commands
72+ local -a warp_points
73+ warp_points=( "${(f)mapfile[$CONFIG]}" )
74+ # LIST="${mapfile[$FNAME]}" # Not required unless stuff uses it
75+
76+ commands=(
77+ 'add:Adds the current working directory to your warp points'
78+ 'add!:Overwrites existing warp point'
79+ 'rm:Removes the given warp point'
80+ 'ls:Outputs all stored warp points'
81+ 'show:Outputs all warp points that point to the current directory'
82+ 'help:Show this extremely helpful text'
83+ '..:Go back to last directory'
84+ )
85+
86+ _arguments -C \
87+ '1: :->first_arg' \
88+ '2: :->second_arg' && ret=0
89+
90+ case $state in
91+ first_arg)
92+ _describe -t warp_points "Warp points" warp_points && ret=0
93+ _describe -t commands "Commands" commands && ret=0
94+ ;;
95+ second_arg)
96+ case $words[2] in
97+ add\!|rm)
98+ _describe -t points "Warp points" warp_points && ret=0
99+ ;;
100+ add)
101+ _message 'Write the name of your warp point' && ret=0
102+ ;;
103+ esac
104+ ;;
105diff --git a/plugins/wd/wd.plugin.zsh b/plugins/wd/wd.plugin.zsh
106index bbec4a7154a58572d74e389251ee1d4747c1aa5a..9800335fc6584ea2ffaed1d81b766c5095a68e18 100755
107--- a/plugins/wd/wd.plugin.zsh
108+++ b/plugins/wd/wd.plugin.zsh
109@@ -6,4 +6,6 @@
110 #
111 # @github.com/mfaerevaag/wd
112
113-alias wd='. $ZSH/plugins/wd/wd.sh'
114+wd() {
115+ . $ZSH/plugins/wd/wd.sh
116+}
117diff --git a/plugins/wd/wd.sh b/plugins/wd/wd.sh
118index 744f58bc2039a5e1c6054a5ad0aab77fa2aa165f..96a3426fc500115d28bb6282ec673922b63e5503 100755
119--- a/plugins/wd/wd.sh
120+++ b/plugins/wd/wd.sh
121@@ -50,12 +50,12 @@ wd_warp()
122 wd_print_msg $YELLOW "Warping to current directory?"
123 else
124 (( n = $#1 - 1 ))
125- wd_print_msg $BLUE "Warping..."
126+ #wd_print_msg $BLUE "Warping..."
127 cd -$n > /dev/null
128 fi
129 elif [[ ${points[$1]} != "" ]]
130 then
131- wd_print_msg $BLUE "Warping..."
132+ #wd_print_msg $BLUE "Warping..."
133 cd ${points[$1]}
134 else
135 wd_print_msg $RED "Unkown warp point '$1'"
136@@ -64,16 +64,16 @@ wd_warp()
137
138 wd_add()
139 {
140- if [[ $1 =~ "^\.+$" ]]
141+ if [[ $2 =~ "^\.+$" || $2 =~ "^\s*$" ]]
142 then
143- wd_print_msg $RED "Illeagal warp point (see README)."
144- elif [[ ${points[$1]} == "" ]] || $2
145+ wd_print_msg $RED "Illegal warp point (see README)."
146+ elif [[ ${points[$2]} == "" ]] || $1
147 then
148- wd_remove $1 > /dev/null
149- print "$1:$PWD" >> $CONFIG
150+ wd_remove $2 > /dev/null
151+ print "$2:$PWD" >> $CONFIG
152 wd_print_msg $GREEN "Warp point added"
153 else
154- wd_print_msg $YELLOW "Warp point '$1' alredy exists. Use 'add!' to overwrite."
155+ wd_print_msg $YELLOW "Warp point '$2' already exists. Use 'add!' to overwrite."
156 fi
157 }
158
159@@ -83,7 +83,9 @@ wd_remove()
160 then
161 if wd_tmp=`sed "/^$1:/d" $CONFIG`
162 then
163- echo $wd_tmp > $CONFIG
164+ # `>!` forces overwrite
165+ # we need this if people use `setopt NO_CLOBBER`
166+ echo $wd_tmp >! $CONFIG
167 wd_print_msg $GREEN "Warp point removed"
168 else
169 wd_print_msg $RED "Warp point unsuccessfully removed. Sorry!"
170@@ -131,9 +133,9 @@ wd_print_usage()
171 print "\nCommands:"
172 print "\t add \t Adds the current working directory to your warp points"
173 print "\t add! \t Overwrites existing warp point"
174- print "\t remove Removes the given warp point"
175+ print "\t rm \t Removes the given warp point"
176 print "\t show \t Outputs warp points to current directory"
177- print "\t list \t Outputs all stored warp points"
178+ print "\t ls \t Outputs all stored warp points"
179 print "\t help \t Show this extremely helpful text"
180 }
181
182@@ -141,7 +143,7 @@ wd_print_usage()
183 ## run
184
185 # get opts
186-args=`getopt -o a:r:lhs -l add:,remove:,list,help,show -- $*`
187+args=`getopt -o a:r:lhs -l add:,rm:,ls,help,show -- $*`
188
189 # check if no arguments were given
190 if [[ $? -ne 0 || $#* -eq 0 ]]
191@@ -161,49 +163,40 @@ else
192
193 for i
194 do
195- case "$i"
196- in
197- -a|--add|add)
198- wd_add $2 false
199- shift
200- shift
201+ case "$i"
202+ in
203+ -a|--add|add)
204+ wd_add false $2
205 break
206 ;;
207 -a!|--add!|add!)
208- wd_add $2 true
209- shift
210- shift
211+ wd_add true $2
212 break
213 ;;
214- -r|--remove|rm)
215- wd_remove $2
216- shift
217- shift
218+ -r|--remove|rm)
219+ wd_remove $2
220 break