Add zsh-lookup sub-system
[grml-etc-core.git] / usr_share_grml / zsh / functions / Lookup / lookupinit
1 ### vim:ft=zsh:foldmethod=marker
2 ##
3 ## zsh function that looks up an argument in various webservices.
4 ## Copyright: 2009, Frank Terbeck <ft@bewatermyfriend.org>
5 ##
6 ## This file, all LOOKUP_* files and all backends in the Backends/ subdirectory
7 ## are distributed under the same licence as zsh itself (BSD-like).
8 ##
9
10 typeset -ga LOOKUP_backends
11 typeset -gA LOOKUP_aliases
12
13 function lookup() {
14     emulate -L zsh
15     setopt extendedglob
16     setopt warncreateglobal
17
18     local file opt
19     local -a alias_words backend_args
20     local -A lu_aliases
21
22     local -x backend lookup_describe lookup_qh_arg lookup_lp lookup_ei unencQUERY
23     local -ix lookup_remote lookup_printout lookup_use_qh lookup_help
24     local -ax args
25     local -Ax lookup_communicate lu_parseopts_args opts
26
27     backend='-init-'
28     lookup_ei='-main-'
29     lookup_help=0
30     lookup_use_qh=0
31     lookup_remote=0
32     lookup_printout=0
33
34     lu_parseopts_args=(
35         a   string
36         d   string
37         q   string
38         h   bool
39         i   bool
40         l   bool
41         L   bool
42         P   bool
43         Q   bool
44         R   bool
45     )
46     LOOKUP_parseopts "$@" || return 1
47
48     if [[ -n ${opts[-l]} ]] ; then
49         printf 'Available backends:\n\n'
50         lookup_describe="yes"
51         for backend in ${LOOKUP_backends} ; do
52             printf '%16s - %s\n' ${backend} "$(LOOKUP_be_${backend})"
53         done
54         printf '\n'
55         return 0
56     fi
57
58     if [[ -n ${opts[-L]} ]] ; then
59         if (( ${#LOOKUP_aliases} == 0 )) ; then
60             printf 'lookup: No aliases defined.\n'
61             return 0
62         fi
63         printf 'Defined backend aliases:\n\n'
64         for al in ${(k)LOOKUP_aliases}; do
65             printf '%16s=%s\n' ${al} ${LOOKUP_aliases[$al]}
66         done
67         printf '\n'
68         return 0
69     fi
70
71     if [[ -n ${opts[-i]} ]] ; then
72         local f
73         local -a fcts
74
75         LOOKUP_backends=()
76         fcts=(
77             LOOKUP_browser LOOKUP_context LOOKUP_encode LOOKUP_guard
78             LOOKUP_help LOOKUP_hook LOOKUP_query_handler
79         )
80
81         for file in ${^fpath}/LOOKUP_be_*~*(\~|.zwc)(N.) ; do
82             file=${file:t}
83             : ${file:#(#b)LOOKUP_be_(*)}
84             backend=${match[1]}
85
86             [[ -n ${(M)LOOKUP_backends:#${backend}} ]] && continue
87             LOOKUP_backends+=(${backend})
88             (( ${+functions[LOOKUP_be_$backend]} )) ||
89                 autoload -Uz LOOKUP_be_${backend}
90         done
91
92         for f in ${fcts} ; do
93             (( ${+functions[$f]} )) || autoload -Uz $f
94         done
95         return 0
96     fi
97
98     if [[ -n ${opts[-a]} ]] ; then
99         local al val
100
101         match=()
102         : ${${opts[-a]}/(#b)(*)=(*)}
103         al="${match[1]}"
104         val="${match[2]}"
105         if [[ -z ${al} ]] || [[ -z ${val} ]] ; then
106             printf 'An alias definition *must* look like this: aliasname="originalname -options"\n'
107             return 1
108         fi
109
110         LOOKUP_aliases[$al]="${val}"
111         return 0
112     fi
113
114     if [[ -n ${opts[-d]} ]] ; then
115         if [[ -n ${LOOKUP_aliases[${opts[-d]}]} ]] ; then
116             unset "LOOKUP_aliases[${opts[-d]}]"
117             return 0
118         else
119             printf 'No such backend alias defined: "%s"\n' ${opts[-d]}
120             printf 'Use lookup -L to get a list.\n'
121             return 1
122         fi
123     fi
124
125     [[ -n ${opts[-h]} ]] && lookup_help=1
126     [[ -n ${opts[-R]} ]] && lookup_remote=1
127     [[ -n ${opts[-P]} ]] && lookup_printout=1
128     [[ -n ${opts[-Q]} ]] && lookup_use_qh=1
129     [[ -n ${opts[-q]} ]] && lookup_use_qh=1 && lookup_qh_arg=${opts[-q]}
130
131     if (( ${#args} == 0 )) ; then
132         printf 'usage: lookup [-{i,a,d,Q,l,L,P,R}] [-{h,q} <arg>] <backend> OPTION(s)...\n'
133         printf '  Options:\n'
134         printf '    -h [backend] print this text or help for '\''backend'\''\n\n'
135         printf '    -i           (re)initialize lookup\n\n'
136         printf '    -a           add a backend alias\n'
137         printf '    -d           remove an alias for a backend\n\n'
138         printf '    -Q           let a handler create the QUERY string\n'
139         printf '    -q <arg>     same as -Q, but let'\''s you give an argument, too\n\n'
140         printf '    -l           list available backends\n'
141         printf '    -L           list defined backend aliases\n\n'
142         printf '    -P           print which browser command would be used\n'
143         printf '    -R           send url to remote browser\n'
144         (( ${+functions[lu]} )) &&
145             printf '\n Instead of '\''lookup'\'' the shorter '\''lu'\'' may be used.\n'
146         (( lookup_help > 0 )) && return 0 || return 1
147     fi
148
149     if (( ${+LOOKUP_aliases[${args[1]}]} )) ; then
150         alias_words=( ${(z)${LOOKUP_aliases[${args[1]}]}} )
151         shift args
152         backend=${alias_words[1]}
153         shift alias_words
154         backend_args=( "${alias_words[@]}" "${args[@]}" )
155     else
156         backend=${args[1]}
157         shift args
158         backend_args=( "${args[@]}" )
159     fi
160
161     if [[ -z ${(M)LOOKUP_backends:#$backend} ]] ; then
162         printf 'Unknown backend '\''%s'\''.\n' ${backend}
163         return 1
164     fi
165
166     args=()
167     opts=()
168     LOOKUP_hook -- "${backend_args[@]}"
169     lookup_ei='-backend-'
170     LOOKUP_context -d
171     LOOKUP_be_${backend} "${backend_args[@]}"
172     return $?
173 }
174
175 function lu() {
176     lookup "$@"
177 }
178
179 # initialize the system
180 autoload -Uz LOOKUP_parseopts LOOKUP_guard  # lookup -i needs this
181 lookup -i