Sync to zsh-lookup upstream repository
[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     # Silence a few warnings for now.
27     # See http://www.zsh.org/mla/workers/2015/msg03364.html for details.
28     local -a match mbegin mend
29     local MATCH; integer MBEGIN MEND
30
31     backend='-init-'
32     lookup_ei='-main-'
33     lookup_help=0
34     lookup_use_qh=0
35     lookup_remote=0
36     lookup_printout=0
37
38     lu_parseopts_args=(
39         a   string
40         d   string
41         q   string
42         h   bool
43         i   bool
44         l   bool
45         L   bool
46         P   bool
47         Q   bool
48         R   bool
49     )
50     LOOKUP_parseopts "$@" || return 1
51
52     if [[ -n ${opts[-l]} ]] ; then
53         printf 'Available backends:\n\n'
54         lookup_describe="yes"
55         for backend in ${LOOKUP_backends} ; do
56             printf '%16s - %s\n' ${backend} "$(LOOKUP_be_${backend})"
57         done
58         printf '\n'
59         return 0
60     fi
61
62     if [[ -n ${opts[-L]} ]] ; then
63         if (( ${#LOOKUP_aliases} == 0 )) ; then
64             printf 'lookup: No aliases defined.\n'
65             return 0
66         fi
67         printf 'Defined backend aliases:\n\n'
68         for al in ${(k)LOOKUP_aliases}; do
69             printf '%16s=%s\n' ${al} ${LOOKUP_aliases[$al]}
70         done
71         printf '\n'
72         return 0
73     fi
74
75     if [[ -n ${opts[-i]} ]] ; then
76         local f
77         local -a fcts
78
79         LOOKUP_backends=()
80         fcts=(
81             LOOKUP_browser LOOKUP_context LOOKUP_encode LOOKUP_guard
82             LOOKUP_help LOOKUP_hook LOOKUP_query_handler
83         )
84
85         for file in ${^fpath}/LOOKUP_be_*~*(\~|.zwc)(N.) ; do
86             file=${file:t}
87             : ${file:#(#b)LOOKUP_be_(*)}
88             backend=${match[1]}
89
90             [[ -n ${(M)LOOKUP_backends:#${backend}} ]] && continue
91             LOOKUP_backends+=(${backend})
92             (( ${+functions[LOOKUP_be_$backend]} )) ||
93                 autoload -Uz LOOKUP_be_${backend}
94         done
95
96         for f in ${fcts} ; do
97             (( ${+functions[$f]} )) || autoload -Uz $f
98         done
99         return 0
100     fi
101
102     if [[ -n ${opts[-a]} ]] ; then
103         local al val
104
105         match=()
106         : ${${opts[-a]}/(#b)(*)=(*)}
107         al="${match[1]}"
108         val="${match[2]}"
109         if [[ -z ${al} ]] || [[ -z ${val} ]] ; then
110             printf 'An alias definition *must* look like this: aliasname="originalname -options"\n'
111             return 1
112         fi
113
114         LOOKUP_aliases[$al]="${val}"
115         return 0
116     fi
117
118     if [[ -n ${opts[-d]} ]] ; then
119         if [[ -n ${LOOKUP_aliases[${opts[-d]}]} ]] ; then
120             unset "LOOKUP_aliases[${opts[-d]}]"
121             return 0
122         else
123             printf 'No such backend alias defined: "%s"\n' ${opts[-d]}
124             printf 'Use lookup -L to get a list.\n'
125             return 1
126         fi
127     fi
128
129     [[ -n ${opts[-h]} ]] && lookup_help=1
130     [[ -n ${opts[-R]} ]] && lookup_remote=1
131     [[ -n ${opts[-P]} ]] && lookup_printout=1
132     [[ -n ${opts[-Q]} ]] && lookup_use_qh=1
133     [[ -n ${opts[-q]} ]] && lookup_use_qh=1 && lookup_qh_arg=${opts[-q]}
134
135     if (( ${#args} == 0 )) ; then
136         printf 'usage: lookup [-{i,a,d,Q,l,L,P,R}] [-{h,q} <arg>] <backend> OPTION(s)...\n'
137         printf '  Options:\n'
138         printf '    -h [backend] print this text or help for '\''backend'\''\n\n'
139         printf '    -i           (re)initialize lookup\n\n'
140         printf '    -a           add a backend alias\n'
141         printf '    -d           remove an alias for a backend\n\n'
142         printf '    -Q           let a handler create the QUERY string\n'
143         printf '    -q <arg>     same as -Q, but let'\''s you give an argument, too\n\n'
144         printf '    -l           list available backends\n'
145         printf '    -L           list defined backend aliases\n\n'
146         printf '    -P           print which browser command would be used\n'
147         printf '    -R           send url to remote browser\n'
148         (( ${+functions[lu]} )) &&
149             printf '\n Instead of '\''lookup'\'' the shorter '\''lu'\'' may be used.\n'
150         (( lookup_help > 0 )) && return 0 || return 1
151     fi
152
153     if (( ${+LOOKUP_aliases[${args[1]}]} )) ; then
154         alias_words=( ${(z)${LOOKUP_aliases[${args[1]}]}} )
155         shift args
156         backend=${alias_words[1]}
157         shift alias_words
158         backend_args=( "${alias_words[@]}" "${args[@]}" )
159     else
160         backend=${args[1]}
161         shift args
162         backend_args=( "${args[@]}" )
163     fi
164
165     if [[ -z ${(M)LOOKUP_backends:#$backend} ]] ; then
166         printf 'Unknown backend '\''%s'\''.\n' ${backend}
167         return 1
168     fi
169
170     args=()
171     opts=()
172     LOOKUP_hook -- "${backend_args[@]}"
173     lookup_ei='-backend-'
174     LOOKUP_context -d
175     LOOKUP_be_${backend} "${backend_args[@]}"
176     return $?
177 }
178
179 function lu() {
180     lookup "$@"
181 }
182
183 # initialize the system
184 autoload -Uz LOOKUP_parseopts LOOKUP_guard  # lookup -i needs this
185 lookup -i