Add zsh-lookup sub-system
[grml-etc-core.git] / usr_share_grml / zsh / completion / unix / _lookup
1 #compdef lookup lu
2 ### vim:ft=zsh:foldmethod=marker
3 ## Copyright: 2009, Frank Terbeck <ft@bewatermyfriend.org>
4
5 function _lookup_aliases() {
6     local al
7     local -a aliases
8
9     aliases=()
10     for al in ${(k)LOOKUP_aliases}; do
11         aliases+=("${al}:alias for ${LOOKUP_aliases[$al]}")
12     done
13
14     _describe  -t aliases 'backend alias' aliases
15 }
16
17 function _lookup_backends() {
18     local be
19     local -i hidealiased
20     local -a backends
21     local -x lookup_describe
22     lookup_describe='yes'
23
24     if [[ -z ${lookup_nohide} ]] &&
25        zstyle -t ":completion:${curcontext}" hide-aliased-backends ; then
26         hidealiased=1
27     else
28         hidealiased=0
29     fi
30
31     backends=()
32     for be in ${LOOKUP_backends}; do
33         (( hidealiased > 0 )) && [[ -n ${(Mv)LOOKUP_aliases:#$be} ]] && continue
34         backends+=("${be}:$(LOOKUP_be_${be})")
35     done
36
37     _describe -t backends 'backend' backends
38 }
39
40 function _lookup_backends_nohide() {
41     local -x lookup_nohide='yes'
42     _lookup_backends
43 }
44
45 function _lookup_backends_and_aliases() {
46     _alternative \
47         'aliases: :_lookup_aliases' \
48         'commands: :_lookup_backends'
49 }
50
51 function _lookup() {
52     local context curcontext state line ret
53     local -a args aw
54     local -x backend
55     local -ix lookup_complete
56
57     lookup_complete=1
58     args=(
59         '-a[add a backend alias]:alias definition:'
60         '-d[remove backand alias]:alias name:_lookup_aliases'
61         '-h[provide help]:lookup backend:_lookup_backends_nohide'
62         '-i[(re)initialize lookup]:'
63         '-l[list available backends]:'
64         '-L[list defined backend aliases]:'
65         '-Q[let a handler create the QUERY string]'
66         '-q[let a handler create the QUERY string, with arg]:argument for query handler:'
67         '-P[print which browser command would be used]'
68         '-R[send url to remote browser]'
69         '*:: :->be_or_options'
70     )
71     _arguments -C -s -w ${args} && return
72
73     if [[ ${state} == "be_or_options" ]]; then
74         if (( CURRENT == 1 )) ; then
75             _lookup_backends_and_aliases
76         else
77             backend="${words[1]}"
78             if [[ -n ${LOOKUP_aliases[$backend]} ]] ; then
79                 aw=( ${(z)${LOOKUP_aliases[$backend]}} )
80                 (( CURRENT = CURRENT + ${#aw} - 1 ))
81                 words[1]=( ${aw} )
82                 aw=()
83                 backend="${words[1]}"
84             fi
85             curcontext="${curcontext%:*:*}:lookup-${backend}:"
86             _call_function ret LOOKUP_be_${backend}
87         fi
88     fi
89 }
90
91 _lookup