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
46 LOOKUP_parseopts "$@" || return 1
48 if [[ -n ${opts[-l]} ]] ; then
49 printf 'Available backends:\n\n'
51 for backend in ${LOOKUP_backends} ; do
52 printf '%16s - %s\n' ${backend} "$(LOOKUP_be_${backend})"
58 if [[ -n ${opts[-L]} ]] ; then
59 if (( ${#LOOKUP_aliases} == 0 )) ; then
60 printf 'lookup: No aliases defined.\n'
63 printf 'Defined backend aliases:\n\n'
64 for al in ${(k)LOOKUP_aliases}; do
65 printf '%16s=%s\n' ${al} ${LOOKUP_aliases[$al]}
71 if [[ -n ${opts[-i]} ]] ; then
77 LOOKUP_browser LOOKUP_context LOOKUP_encode LOOKUP_guard
78 LOOKUP_help LOOKUP_hook LOOKUP_query_handler
81 for file in ${^fpath}/LOOKUP_be_*~*(\~|.zwc)(N.) ; do
83 : ${file:#(#b)LOOKUP_be_(*)}
86 [[ -n ${(M)LOOKUP_backends:#${backend}} ]] && continue
87 LOOKUP_backends+=(${backend})
88 (( ${+functions[LOOKUP_be_$backend]} )) ||
89 autoload -Uz LOOKUP_be_${backend}
93 (( ${+functions[$f]} )) || autoload -Uz $f
98 if [[ -n ${opts[-a]} ]] ; then
102 : ${${opts[-a]}/(#b)(*)=(*)}
105 if [[ -z ${al} ]] || [[ -z ${val} ]] ; then
106 printf 'An alias definition *must* look like this: aliasname="originalname -options"\n'
110 LOOKUP_aliases[$al]="${val}"
114 if [[ -n ${opts[-d]} ]] ; then
115 if [[ -n ${LOOKUP_aliases[${opts[-d]}]} ]] ; then
116 unset "LOOKUP_aliases[${opts[-d]}]"
119 printf 'No such backend alias defined: "%s"\n' ${opts[-d]}
120 printf 'Use lookup -L to get a list.\n'
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]}
131 if (( ${#args} == 0 )) ; then
132 printf 'usage: lookup [-{i,a,d,Q,l,L,P,R}] [-{h,q} <arg>] <backend> OPTION(s)...\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
149 if (( ${+LOOKUP_aliases[${args[1]}]} )) ; then
150 alias_words=( ${(z)${LOOKUP_aliases[${args[1]}]}} )
152 backend=${alias_words[1]}
154 backend_args=( "${alias_words[@]}" "${args[@]}" )
158 backend_args=( "${args[@]}" )
161 if [[ -z ${(M)LOOKUP_backends:#$backend} ]] ; then
162 printf 'Unknown backend '\''%s'\''.\n' ${backend}
168 LOOKUP_hook -- "${backend_args[@]}"
169 lookup_ei='-backend-'
171 LOOKUP_be_${backend} "${backend_args[@]}"
179 # initialize the system
180 autoload -Uz LOOKUP_parseopts LOOKUP_guard # lookup -i needs this