-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_urls
234 lines (213 loc) · 7.56 KB
/
_urls
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
#compdef -value-,WWW_HOME,-default- -P -value-,(ftp|http(|s))_proxy,-default-
# Configuration styles used:
#
# urls
# The path to a directory containing a URL database, such as:
#
# % cd ~/.zsh/urls
# % find . -ls
# ... drwxr-xr-x ... 512 Sep 3 02:46 .
# ... drwxr-xr-x ... 512 Sep 3 02:48 ./http
# ... drwxr-xr-x ... 512 Sep 3 02:52 ./http/www.zsh.org
# ... drwxr-xr-x ... 512 Sep 3 03:01 ./http/www.zsh.org/mla
# ... drwxr-xr-x ... 512 Sep 3 03:01 ./http/www.zsh.org/mla/workers
# ... drwxr-xr-x ... 512 Sep 3 03:01 ./http/www.zsh.org/mla/workers/1999
# ... -rw-r--r-- ... 0 Sep 3 03:01 ./http/www.zsh.org/mla/workers/1999/index.html
# ... drwxr-xr-x ... 512 Sep 3 02:48 ./http/sunsite.dk
# ... drwxr-xr-x ... 512 Sep 3 02:48 ./http/sunsite.dk/zsh
# ... drwxr-xr-x ... 512 Sep 3 02:47 ./bookmark
# ... drwxr-xr-x ... 512 Sep 3 02:48 ./bookmark/zsh
# ... -rw-r--r-- ... 27 Sep 3 02:47 ./bookmark/zsh/home
# ... -rw-r--r-- ... 20 Sep 3 02:48 ./bookmark/zsh/meta
# % cat bookmark/zsh/home
# http://sunsite.dk/zsh/
# % cat bookmark/zsh/meta
# http://www.zsh.org/
#
# Alternatively it may contain the path to a normal file and the URLs will
# be taken from that. Or the style may contain the URLs as its values.
#
# local
# Specify a local web server as an array with three elements:
# <hostname> <doc root> <user area>
# where hostname is the name of the web server, doc root is the path to
# the default web pages for the server and user area is the directory
# name used by a user placing web pages within their home area.
# E.g.:
# zstyle ':completion:*:urls' local www /usr/local/apache/htdocs public_html
local ipre scheme host user uhosts ret=1 expl match glob suf
local localhttp
zstyle -a ":completion:${curcontext}:urls" local localhttp
local localhttp_servername="$localhttp[1]"
local localhttp_documentroot="$localhttp[2]"
local localhttp_userdir="$localhttp[3]"
zstyle -a ":completion:${curcontext}:urls" urls urls
if [[ $#urls -gt 1 || ( $#urls -eq 1 && ! -d $urls[1] ) ]]; then
[[ $#urls -eq 1 && -f $urls[1] ]] && urls=( $(< $urls[1]) )
_wanted urls expl 'URL' compadd "$@" -a urls && return 0
urls=()
fi
urls="$urls[1]"
glob=(-g '*(^/)')
zparseopts -D -K -E 'g:=glob'
ipre="$IPREFIX"
if ! compset -P '(#b)([-+.a-z0-9]#):'; then
_tags -C argument prefixes
while _tags; do
while _next_label prefixes expl 'URL prefix' -S '' "$@"; do
compset -S '[^:/]*' && compstate[to_end]=''
[[ -d $urls/bookmark ]] &&
compadd "$expl[@]" bookmark: && ret=0
compadd "$expl[@]" file: ftp:// gopher:// http:// https:// && ret=0
done
(( ret )) || return 0
done
return 1
fi
scheme="$match[1]"
case "$scheme" in
http(|s)|ftp|gopher)
if ! compset -P //; then
_wanted -C "$scheme" prefixes expl 'end of prefix' compadd -S '' "$@" //
return
fi
;;
file)
[[ -prefix //(127.0.0.1|localhost)/ ]] && compset -P '//(127.0.0.1|localhost)'
[[ -prefix /// ]] && compset -P //
if ! compset -P //; then
_tags -C file files
while _tags; do
while _next_label files expl 'local file'; do
if [[ -prefix / ]]; then
_path_files "$expl[@]" -S '' "${glob[@]}" && ret=0
_path_files "$expl[@]" -S/ -r '/' -/ && ret=0
elif [[ -z "$PREFIX" ]]; then
compadd -S '/' -r '/' "$expl[@]" "$@" - "${PWD%/}" && ret=0
fi
done
(( ret )) || return 0
done
return 1
fi
;;
bookmark)
if [[ -f "$urls/$scheme/${(Q)PREFIX}${(Q)SUFFIX}" &&
-s "$urls/$scheme/${(Q)PREFIX}${(Q)SUFFIX}" ]]; then
_wanted -C bookmark bookmarks expl bookmarks \
compadd "$@" -U - \
"$ipre$(<"$urls/$scheme/${(Q)PREFIX}${(Q)SUFFIX}")" && ret=0
else
_tags -C bookmark files
while _tags; do
while _next_label files expl 'bookmark'; do
_path_files -W "$urls/$scheme" "$expl[@]" -S '' "${glob[@]}" &&
ret=0
_path_files -W "$urls/$scheme" -S/ -r '/' "$expl[@]" -/ && ret=0
done
(( ret )) || return 0
done
fi
return ret
;;
esac
# Complete hosts
if ! compset -P '(#b)([^:/]#)([:/])'; then
uhosts=($urls/$scheme/$PREFIX*$SUFFIX(/:t))
_tags hosts
while _tags; do
while _next_label hosts expl host; do
compset -S '[:/]*' || suf="/"
(( $#uhosts )) || _hosts -S "$suf" -r '/:' "$expl[@]" && ret=0
[[ "$scheme" = http ]] && uhosts=($uhosts $localhttp_servername)
compadd -S "$suf" -r '/:' "$expl[@]" -a uhosts && ret=0
done
(( ret )) || return 0
done
return 1
fi
host="$match[1]"
if [[ $match[2] == ':' ]]; then
! compset -P '(#b)(<->)/' && _message -e ports 'port number' && return 0
port="$match[1]"
else
port=80
fi
# check if this is http, and not a local thing. also check for a GNU ping
if [[ "$localhttp_servername" != "$host" && "$scheme" == "http" ]]; then
# Can we do http completion?
if ping -c 1 -w 1 "$host" > /dev/null 2>/dev/null ; then
# this is required for the urldecode below
setopt localoptions histsubstpattern
# get path up until last /
if compset -P '(#b)([-a-zA-Z0-9\!%)(/._]#/)'; then
httppath=${(Q)match[1]}
else
httppath='/'
fi
# fetch an url listing (only intended for http file listings!)
urls=( ${(f)"$(wget --timeout=3 -O - -q "http://$host:$port/$httppath" | grep -o -E 'href="[^"]+"' | cut -d'"' -f2)"} )
dirurls=( ${${(M)urls:#*/}:#(../|/)} )
fileurls=( ${${urls:#*/}:#\?*} )
dirout=()
# need the for loop until zsh 4.3.12 / urldecode by Mikachu
for url in "${(@)dirurls}"; do dirout+=("$(printf %b ${url:gs/(#b)%(??)/\\x$match})"); done
fileout=()
# need the for loop until zsh 4.3.12 / urldecode by Mikachu
for url in "${(@)fileurls}"; do fileout+=("$(printf %b ${url:gs/(#b)%(??)/\\x$match})"); done
# Yes. Yes, we can.
_tags -C httplink httpdir httpfile
while _tags; do
while _next_label httpdir expl 'HTTP Listing Directory'; do
# treat directories
if [[ $#dirurls > 0 ]]; then
compadd -J 'directories' -S '' "$expl[@]" -d dirout -a dirurls && ret=0
fi
done
while _next_label httpfile expl 'HTTP Listing File'; do
# treat files
if [[ $#fileurls > 0 ]]; then
compadd -J 'files' -S ' ' "$expl[@]" -d fileout -a fileurls && ret=0
fi
done
(( ret )) || return 0
done
else
_message -e httplink 'could not complete from host - ping timeout' && return 0
fi
fi
# Complete part after hostname
_tags -C local files || return 1
if [[ "$localhttp_servername" = "$host" ]]; then
if compset -P \~; then
if ! compset -P '(#b)([^/]#)/'; then
_users -S/ "$@"
return
fi
user="$match[1]"
while _tags; do
while _next_label files expl 'local file'; do
_path_files "$expl[@]" "$@" -W ~$user/$localhttp_userdir "${glob[@]}" && ret=0
_path_files -S/ -r '/' "$expl[@]" -W ~$user/$localhttp_userdir-/ && ret=0
done
(( ret )) || return 0
done
else
while _tags; do
while _next_label files expl 'local file'; do
_path_files "$expl[@]" "$@" -W $localhttp_documentroot "${glob[@]}" && ret=0
_path_files -S/ -r '/' "$expl[@]" -W $localhttp_documentroot -/ && ret=0
done
(( ret )) || return 0
done
fi
else
while _tags; do
while _next_label files expl 'local file'; do
_path_files "$expl[@]" "$@" -W $urls/$scheme/$host "${glob[@]}" && ret=0
_path_files -S/ -r '/' "$expl[@]" -W $urls/$scheme/$host -/ && ret=0
done
(( ret )) || return 0
done
fi
return $ret