1 ### vim:ft=zsh:foldmethod=marker
3 ## zsh function that looks up an argument in various webservices.
4 ## Copyright: 2009, Frank Terbeck <ft@bewatermyfriend.org>
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).
10 typeset -ga LOOKUP_backends
11 typeset -gA LOOKUP_aliases
16 setopt warncreateglobal
19 local -a alias_words backend_args
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
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
50 LOOKUP_parseopts "$@" || return 1
52 if [[ -n ${opts[-l]} ]] ; then
53 printf 'Available backends:\n\n'
55 for backend in ${LOOKUP_backends} ; do
56 printf '%16s - %s\n' ${backend} "$(LOOKUP_be_${backend})"
62 if [[ -n ${opts[-L]} ]] ; then
63 if (( ${#LOOKUP_aliases} == 0 )) ; then
64 printf 'lookup: No aliases defined.\n'
67 printf 'Defined backend aliases:\n\n'
68 for al in ${(k)LOOKUP_aliases}; do
69 printf '%16s=%s\n' ${al} ${LOOKUP_aliases[$al]}
75 if [[ -n ${opts[-i]} ]] ; then
81 LOOKUP_browser LOOKUP_context LOOKUP_encode LOOKUP_guard
82 LOOKUP_help LOOKUP_hook LOOKUP_query_handler
85 for file in ${^fpath}/LOOKUP_be_*~*(\~|.zwc)(N.) ; do
87 : ${file:#(#b)LOOKUP_be_(*)}
90 [[ -n ${(M)LOOKUP_backends:#${backend}} ]] && continue
91 LOOKUP_backends+=(${backend})
92 (( ${+functions[LOOKUP_be_$backend]} )) ||
93 autoload -Uz LOOKUP_be_${backend}
97 (( ${+functions[$f]} )) || autoload -Uz $f
102 if [[ -n ${opts[-a]} ]] ; then
106 : ${${opts[-a]}/(#b)(*)=(*)}
109 if [[ -z ${al} ]] || [[ -z ${val} ]] ; then
110 printf 'An alias definition *must* look like this: aliasname="originalname -options"\n'
114 LOOKUP_aliases[$al]="${val}"
118 if [[ -n ${opts[-d]} ]] ; then
119 if [[ -n ${LOOKUP_aliases[${opts[-d]}]} ]] ; then
120 unset "LOOKUP_aliases[${opts[-d]}]"
123 printf 'No such backend alias defined: "%s"\n' ${opts[-d]}
124 printf 'Use lookup -L to get a list.\n'
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]}
135 if (( ${#args} == 0 )) ; then
136 printf 'usage: lookup [-{i,a,d,Q,l,L,P,R}] [-{h,q} <arg>] <backend> OPTION(s)...\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
153 if (( ${+LOOKUP_aliases[${args[1]}]} )) ; then
154 alias_words=( ${(z)${LOOKUP_aliases[${args[1]}]}} )
156 backend=${alias_words[1]}
158 backend_args=( "${alias_words[@]}" "${args[@]}" )
162 backend_args=( "${args[@]}" )
165 if [[ -z ${(M)LOOKUP_backends:#$backend} ]] ; then
166 printf 'Unknown backend '\''%s'\''.\n' ${backend}
172 LOOKUP_hook -- "${backend_args[@]}"
173 lookup_ei='-backend-'
175 LOOKUP_be_${backend} "${backend_args[@]}"
183 # initialize the system
184 autoload -Uz LOOKUP_parseopts LOOKUP_guard # lookup -i needs this