From 1ee27bd79f2b8eae519f6890bc43f5feca13a261 Mon Sep 17 00:00:00 2001 From: Frank Terbeck Date: Thu, 4 Nov 2010 13:48:18 +0100 Subject: [PATCH] Add zsh-lookup sub-system In the grml-etc-core package. Not in the grmlzshrc file. Not going to happen. :) --- etc/zsh/zshrc | 1 + usr_share_grml/zsh/completion/unix/_lookup | 91 +++++++ .../zsh/functions/Lookup/Backends/LOOKUP_be_cpan | 34 +++ .../functions/Lookup/Backends/LOOKUP_be_deb_bts | 65 +++++ .../functions/Lookup/Backends/LOOKUP_be_deb_msgid | 61 +++++ .../Lookup/Backends/LOOKUP_be_deb_patchtrack | 42 ++++ .../functions/Lookup/Backends/LOOKUP_be_deb_pts | 43 ++++ .../zsh/functions/Lookup/Backends/LOOKUP_be_dsc | 34 +++ .../Lookup/Backends/LOOKUP_be_filewatcher | 39 +++ .../functions/Lookup/Backends/LOOKUP_be_freshmeat | 39 +++ .../zsh/functions/Lookup/Backends/LOOKUP_be_gmane | 271 +++++++++++++++++++++ .../zsh/functions/Lookup/Backends/LOOKUP_be_google | 62 +++++ .../zsh/functions/Lookup/Backends/LOOKUP_be_koders | 189 ++++++++++++++ .../zsh/functions/Lookup/Backends/LOOKUP_be_leo | 190 +++++++++++++++ .../functions/Lookup/Backends/LOOKUP_be_letssingit | 89 +++++++ .../zsh/functions/Lookup/Backends/LOOKUP_be_rfc | 138 +++++++++++ .../Lookup/Backends/LOOKUP_be_sourceforge | 39 +++ .../zsh/functions/Lookup/Backends/LOOKUP_be_usenet | 100 ++++++++ .../functions/Lookup/Backends/LOOKUP_be_wikipedia | 115 +++++++++ .../functions/Lookup/Backends/LOOKUP_be_zsh_mla | 101 ++++++++ usr_share_grml/zsh/functions/Lookup/LOOKUP_browser | 46 ++++ usr_share_grml/zsh/functions/Lookup/LOOKUP_context | 22 ++ usr_share_grml/zsh/functions/Lookup/LOOKUP_encode | 36 +++ usr_share_grml/zsh/functions/Lookup/LOOKUP_guard | 52 ++++ usr_share_grml/zsh/functions/Lookup/LOOKUP_help | 48 ++++ usr_share_grml/zsh/functions/Lookup/LOOKUP_hook | 37 +++ .../zsh/functions/Lookup/LOOKUP_parseopts | 101 ++++++++ .../zsh/functions/Lookup/LOOKUP_query_handler | 52 ++++ usr_share_grml/zsh/functions/Lookup/lookupinit | 181 ++++++++++++++ 29 files changed, 2318 insertions(+) create mode 100644 usr_share_grml/zsh/completion/unix/_lookup create mode 100644 usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_cpan create mode 100644 usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_deb_bts create mode 100644 usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_deb_msgid create mode 100644 usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_deb_patchtrack create mode 100644 usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_deb_pts create mode 100644 usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_dsc create mode 100644 usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_filewatcher create mode 100644 usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_freshmeat create mode 100644 usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_gmane create mode 100644 usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_google create mode 100644 usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_koders create mode 100644 usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_leo create mode 100644 usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_letssingit create mode 100644 usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_rfc create mode 100644 usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_sourceforge create mode 100644 usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_usenet create mode 100644 usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_wikipedia create mode 100644 usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_zsh_mla create mode 100644 usr_share_grml/zsh/functions/Lookup/LOOKUP_browser create mode 100644 usr_share_grml/zsh/functions/Lookup/LOOKUP_context create mode 100644 usr_share_grml/zsh/functions/Lookup/LOOKUP_encode create mode 100644 usr_share_grml/zsh/functions/Lookup/LOOKUP_guard create mode 100644 usr_share_grml/zsh/functions/Lookup/LOOKUP_help create mode 100644 usr_share_grml/zsh/functions/Lookup/LOOKUP_hook create mode 100644 usr_share_grml/zsh/functions/Lookup/LOOKUP_parseopts create mode 100644 usr_share_grml/zsh/functions/Lookup/LOOKUP_query_handler create mode 100644 usr_share_grml/zsh/functions/Lookup/lookupinit diff --git a/etc/zsh/zshrc b/etc/zsh/zshrc index 83dc68b..da60016 100644 --- a/etc/zsh/zshrc +++ b/etc/zsh/zshrc @@ -3143,6 +3143,7 @@ if (( GRML_ALWAYS_LOAD_ALL == 0 )) && (( $EUID == 0 )) ; then return 0 fi +zrcautoload lookupinit && lookupinit # variables {{{ diff --git a/usr_share_grml/zsh/completion/unix/_lookup b/usr_share_grml/zsh/completion/unix/_lookup new file mode 100644 index 0000000..af48f6d --- /dev/null +++ b/usr_share_grml/zsh/completion/unix/_lookup @@ -0,0 +1,91 @@ +#compdef lookup lu +### vim:ft=zsh:foldmethod=marker +## Copyright: 2009, Frank Terbeck + +function _lookup_aliases() { + local al + local -a aliases + + aliases=() + for al in ${(k)LOOKUP_aliases}; do + aliases+=("${al}:alias for ${LOOKUP_aliases[$al]}") + done + + _describe -t aliases 'backend alias' aliases +} + +function _lookup_backends() { + local be + local -i hidealiased + local -a backends + local -x lookup_describe + lookup_describe='yes' + + if [[ -z ${lookup_nohide} ]] && + zstyle -t ":completion:${curcontext}" hide-aliased-backends ; then + hidealiased=1 + else + hidealiased=0 + fi + + backends=() + for be in ${LOOKUP_backends}; do + (( hidealiased > 0 )) && [[ -n ${(Mv)LOOKUP_aliases:#$be} ]] && continue + backends+=("${be}:$(LOOKUP_be_${be})") + done + + _describe -t backends 'backend' backends +} + +function _lookup_backends_nohide() { + local -x lookup_nohide='yes' + _lookup_backends +} + +function _lookup_backends_and_aliases() { + _alternative \ + 'aliases: :_lookup_aliases' \ + 'commands: :_lookup_backends' +} + +function _lookup() { + local context curcontext state line ret + local -a args aw + local -x backend + local -ix lookup_complete + + lookup_complete=1 + args=( + '-a[add a backend alias]:alias definition:' + '-d[remove backand alias]:alias name:_lookup_aliases' + '-h[provide help]:lookup backend:_lookup_backends_nohide' + '-i[(re)initialize lookup]:' + '-l[list available backends]:' + '-L[list defined backend aliases]:' + '-Q[let a handler create the QUERY string]' + '-q[let a handler create the QUERY string, with arg]:argument for query handler:' + '-P[print which browser command would be used]' + '-R[send url to remote browser]' + '*:: :->be_or_options' + ) + _arguments -C -s -w ${args} && return + + if [[ ${state} == "be_or_options" ]]; then + if (( CURRENT == 1 )) ; then + _lookup_backends_and_aliases + else + backend="${words[1]}" + if [[ -n ${LOOKUP_aliases[$backend]} ]] ; then + aw=( ${(z)${LOOKUP_aliases[$backend]}} ) + (( CURRENT = CURRENT + ${#aw} - 1 )) + words[1]=( ${aw} ) + aw=() + backend="${words[1]}" + fi + curcontext="${curcontext%:*:*}:lookup-${backend}:" + _call_function ret LOOKUP_be_${backend} + fi + fi +} + +_lookup diff --git a/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_cpan b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_cpan new file mode 100644 index 0000000..65175b8 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_cpan @@ -0,0 +1,34 @@ +### vim:ft=zsh:foldmethod=marker +## search.cpan.org backend for lookup +## Copyright: 2009, Frank Terbeck + +LOOKUP_guard || return 1 +[[ -n ${lookup_describe} ]] && + printf '%s' 'search CPAN via search.cpan.org' && return 0 + +LOOKUP_guard -fd LOOKUP_help_${backend} || +function LOOKUP_help_${backend}() { + LOOKUP_guard || return 1 + printf 'usage: %s \n' ${backend} + printf '\n Searches CPAN via search.cpan.org.\n' + printf '\nExamples:\n' + printf ' %% lookup %s Audio::Scan\n' ${backend} +} +LOOKUP_help && return 0 + +if [[ -n ${lookup_complete} ]] ; then + _message 'search.cpan.org query' + return 0 +fi + +local -x QUERY +QUERY="$*" +LOOKUP_query_handler || return 1 +if [[ -z ${QUERY} ]] ; then + LOOKUP_help -f + return 1 +fi + +LOOKUP_encode -q +LOOKUP_browser "http://search.cpan.org/search?query=${QUERY}" +return $? diff --git a/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_deb_bts b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_deb_bts new file mode 100644 index 0000000..f171170 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_deb_bts @@ -0,0 +1,65 @@ +### vim:ft=zsh:foldmethod=marker +## bugs.debian.org backend for lookup +## Copyright: 2009, Frank Terbeck + +LOOKUP_guard || return 1 +[[ -n ${lookup_describe} ]] && + printf '%s' 'get information about debian bugs by #id' && + return 0 + +local -a comp_args + +LOOKUP_guard -fd LOOKUP_help_${backend} || +function LOOKUP_help_${backend}() { + LOOKUP_guard || return 1 + printf 'usage: %s [-p] \n' ${backend} + printf ' -p go to the bts page of a given package\n' + printf '\n Query Debian[1]'\''s Bug Tracking System[2].\n' + printf '\n By default, this looks up bugs by bug id. In this mode, all non-digit\n' + printf ' characters are automatically stripped off of the query.\n' + printf '\nExamples:\n' + printf ' %% lookup %s 123456\n' ${backend} + printf ' %% lookup %s "#123456"\n' ${backend} + printf ' %% lookup %s -p zsh\n' ${backend} + printf '\n[1] \n' + printf '[2] \n' +} +LOOKUP_help && return 0 + +if [[ -n ${lookup_complete} ]] ; then + comp_args=( + '-p[go to package'\''s bts page]:package:_deb_packages avail' + '*:debian bts query:true' + ) + + _arguments -s -w -A '-*' ${comp_args} && return 0 + _message 'debian bts query' + return 0 +fi + +local mode +local -A opts +local -x QUERY + +mode='id' +lu_parseopts_args=( p bool ) +LOOKUP_parseopts "$@" || return 1 +[[ ${opts[-p]} == 'yes' ]] && mode='package' + +QUERY="${args[*]}" +LOOKUP_query_handler || return 1 +if [[ -z ${QUERY} ]] ; then + LOOKUP_help -f + return 1 +fi + +if [[ ${mode} == 'id' ]] ; then + QUERY="${QUERY//[^0-9]/}" + LOOKUP_encode -q + LOOKUP_browser "http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=${QUERY}" + return $? +else + LOOKUP_encode -q + LOOKUP_browser "http://bugs.debian.org/${QUERY}" + return $? +fi diff --git a/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_deb_msgid b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_deb_msgid new file mode 100644 index 0000000..5edea06 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_deb_msgid @@ -0,0 +1,61 @@ +### vim:ft=zsh:foldmethod=marker +## lists.debian.org message-id search backend for lookup +## Copyright: 2009, Frank Terbeck + +LOOKUP_guard || return 1 +[[ -n ${lookup_describe} ]] && + printf '%s' 'search lists.debian.org by message-ids' && + return 0 + +local lookup_context +local -a comp_args + +lookup_context="$(LOOKUP_context)" + +LOOKUP_guard -fd LOOKUP_help_${backend} || +function LOOKUP_help_${backend}() { + LOOKUP_guard || return 1 + printf 'usage: %s [-1] \n' ${backend} + printf ' -1 use the search'\''s first-hit option\n' + printf '\n Search Debian[1]'\''s Mailinglist Archive[2] my Message-ID: header.\n' + printf ' If you always want to use the -1 option, you may set the use-first-hit\n' + printf ' style in this context: %s\n' ${lookup_context} + printf '\nExamples:\n' + printf ' %% lookup %s 20090215095848.GA21661@schmehl.info\n' ${backend} + printf ' %% lookup %s -1 20090215095848.GA21661@schmehl.info\n' ${backend} + printf ' %% zstyle '\'':lookup:*:%s:*'\'' use-first-hit true\n' ${backend} + printf '\n[1] \n' + printf '[2] \n' +} +LOOKUP_help && return 0 + +if [[ -n ${lookup_complete} ]] ; then + comp_args=( + '-1[use /firsthit option]' + '*:message-id for l.d.o:true' + ) + + _arguments -s -w -A '-*' ${comp_args} && return 0 + _message 'message-id for l.d.o' + return 0 +fi + +local end +local -A opts +local -x QUERY + +lu_parseopts_args=( 1 bool ) +LOOKUP_parseopts "$@" || return 1 +[[ ${opts[-1]} == 'yes' ]] && end='/firsthit' || end='/links' +zstyle -t "${lookup_context}" use-first-hit && end='/firsthit' + +QUERY="${args[*]}" +LOOKUP_query_handler || return 1 +if [[ -z ${QUERY} ]] ; then + LOOKUP_help -f + return 1 +fi + +LOOKUP_encode -q +LOOKUP_browser "http://lists.debian.org/msgid-search/${QUERY}${end}" +return $? diff --git a/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_deb_patchtrack b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_deb_patchtrack new file mode 100644 index 0000000..ddae74b --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_deb_patchtrack @@ -0,0 +1,42 @@ +### vim:ft=zsh:foldmethod=marker +## patch-tracking.debian.net backend for lookup +## Copyright: 2009, Frank Terbeck + +LOOKUP_guard || return 1 +[[ -n ${lookup_describe} ]] && + printf '%s' 'check patch information of debian packages' && + return 0 + +local -a comp_args + +LOOKUP_guard -fd LOOKUP_help_${backend} || +function LOOKUP_help_${backend}() { + LOOKUP_guard || return 1 + printf 'usage: %s \n' ${backend} + printf '\n Warps you to a package'\''s page in Debian[1]'\''s Patch Tracking System[2].\n' + printf '\nExamples\n' + printf ' %% lookup %s fdm\n' ${backend} + printf ' %% lookup %s zsh\n' ${backend} + printf '\n[1] \n' + printf '[2] \n' +} +LOOKUP_help && return 0 + +if [[ -n ${lookup_complete} ]] ; then + _message 'debian package' + _deb_packages avail + return 0 +fi + +local -x QUERY + +QUERY="$*" +LOOKUP_query_handler || return 1 +if [[ -z ${QUERY} ]] ; then + LOOKUP_help -f + return 1 +fi + +LOOKUP_encode -q +LOOKUP_browser "http://patch-tracker.debian.org/package/${QUERY}" +return $? diff --git a/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_deb_pts b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_deb_pts new file mode 100644 index 0000000..9fed264 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_deb_pts @@ -0,0 +1,43 @@ +### vim:ft=zsh:foldmethod=marker +## packages.qa.debian.org backend for lookup +## Copyright: 2009, Frank Terbeck + +LOOKUP_guard || return 1 +[[ -n ${lookup_describe} ]] && + printf '%s' 'debian PTS information about a package' && + return 0 + +local -a comp_args + +LOOKUP_guard -fd LOOKUP_help_${backend} || +function LOOKUP_help_${backend}() { + LOOKUP_guard || return 1 + printf 'usage: %s \n' ${backend} + printf '\n Warps you to a package'\''s page in Debian[1]'\''s Package\n' + printf ' Tracking System[2].\n' + printf '\nExamples\n' + printf ' %% lookup %s wesnoth\n' ${backend} + printf ' %% lookup %s zsh\n' ${backend} + printf '\n[1] \n' + printf '[2] \n' +} +LOOKUP_help && return 0 + +if [[ -n ${lookup_complete} ]] ; then + _message 'debian package' + _deb_packages avail + return 0 +fi + +local -x QUERY + +QUERY="$*" +LOOKUP_query_handler || return 1 +if [[ -z ${QUERY} ]] ; then + LOOKUP_help -f + return 1 +fi + +LOOKUP_encode -q +LOOKUP_browser "http://packages.qa.debian.org/${QUERY}" +return $? diff --git a/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_dsc b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_dsc new file mode 100644 index 0000000..cbf4296 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_dsc @@ -0,0 +1,34 @@ +### vim:ft=zsh:foldmethod=marker +## datasheetcatalog.net backend for lookup +## Copyright: 2009, Frank Terbeck + +LOOKUP_guard || return 1 +[[ -n ${lookup_describe} ]] && + printf '%s' 'search for datasheets via datasheetcatalog.net' && return 0 + +LOOKUP_guard -fd LOOKUP_help_${backend} || +function LOOKUP_help_${backend}() { + LOOKUP_guard || return 1 + printf 'usage: %s \n' ${backend} + printf '\n Searches for datasheets via datasheetcatalog.net.\n' + printf '\nExamples:\n' + printf ' %% lookup %s 7447\n' ${backend} +} +LOOKUP_help && return 0 + +if [[ -n ${lookup_complete} ]] ; then + _message 'datasheetcatalog.net query' + return 0 +fi + +local -x QUERY +QUERY="$*" +LOOKUP_query_handler || return 1 +if [[ -z ${QUERY} ]] ; then + LOOKUP_help -f + return 1 +fi + +LOOKUP_encode -q +LOOKUP_browser "http://search.datasheetcatalog.net/key/${QUERY}" +return $? diff --git a/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_filewatcher b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_filewatcher new file mode 100644 index 0000000..5eaa777 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_filewatcher @@ -0,0 +1,39 @@ +### vim:ft=zsh:foldmethod=marker +## filewatcher.com search backend for lookup +## Copyright: 2009, Frank Terbeck + +LOOKUP_guard || return 1 +[[ -n ${lookup_describe} ]] && + printf '%s' 'search filewatcher.com' && + return 0 + +local -a comp_args + +LOOKUP_guard -fd LOOKUP_help_${backend} || +function LOOKUP_help_${backend}() { + LOOKUP_guard || return 1 + printf 'usage: %s \n' ${backend} + printf '\n Use filewatcher.com to search FTP server contents.\n' + printf '\nExamples:\n' + printf ' %% lookup %s '\''zsh*.tar.gz'\''\n' ${backend} + printf ' %% lookup %s '\''*.jpg'\''\n\n' ${backend} +} +LOOKUP_help && return 0 + +if [[ -n ${lookup_complete} ]] ; then + _message 'filewatcher search string' + return 0 +fi + +local -x QUERY + +QUERY="$*" +LOOKUP_query_handler || return 1 +if [[ -z ${QUERY} ]] ; then + LOOKUP_help -f + return 1 +fi + +LOOKUP_encode -s -q +LOOKUP_browser "http://www.filewatcher.com/_/?q=${QUERY}" +return $? diff --git a/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_freshmeat b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_freshmeat new file mode 100644 index 0000000..e043979 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_freshmeat @@ -0,0 +1,39 @@ +### vim:ft=zsh:foldmethod=marker +## freshmeat.net search backend for lookup +## Copyright: 2009, Frank Terbeck + +LOOKUP_guard || return 1 +[[ -n ${lookup_describe} ]] && + printf '%s' 'search freshmeat.net' && + return 0 + +local -a comp_args + +LOOKUP_guard -fd LOOKUP_help_${backend} || +function LOOKUP_help_${backend}() { + LOOKUP_guard || return 1 + printf 'usage: %s \n' ${backend} + printf '\n Query freshmeat.net for software projects.\n' + printf '\nExamples:\n' + printf ' %% lookup %s zsh\n' ${backend} + printf ' %% lookup %s gnu screen\n\n' ${backend} +} +LOOKUP_help && return 0 + +if [[ -n ${lookup_complete} ]] ; then + _message 'freshmeat search string' + return 0 +fi + +local -x QUERY + +QUERY="$*" +LOOKUP_query_handler || return 1 +if [[ -z ${QUERY} ]] ; then + LOOKUP_help -f + return 1 +fi + +LOOKUP_encode -s -q +LOOKUP_browser "http://freshmeat.net/search/?q=${QUERY}§ion=projects" +return $? diff --git a/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_gmane b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_gmane new file mode 100644 index 0000000..bda727d --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_gmane @@ -0,0 +1,271 @@ +### vim:ft=zsh:foldmethod=marker +## gmane.org backend for lookup +## Copyright: 2009, Frank Terbeck + +LOOKUP_guard || return 1 +[[ -n ${lookup_describe} ]] && printf '%s' 'query gmane.org' && return 0 + +local lookup_context bookmarks +local -A known_sorts known_opers +lookup_context="$(LOOKUP_context)" + +# Set up the path name of the bookmark file. Looked up in the :lookup:* +# context because it may not be limited to compsys in the future +# (maybe someone will write bookmark managing via zsh/curses...). :-) +zstyle -s "${lookup_context}" group-bookmarks bookmarks || + bookmarks="${HOME}/.zgmane.bookmarks" + +known_sorts=( + date 'sort by date' + revdate 'sort by reverse date' + relevance 'sort by relevance' +) + +known_opers=( + and 'require all words' + or 'require one word' +) + +LOOKUP_guard -fd LOOKUP_help_${backend} || +function LOOKUP_help_${backend}() { + # gmane backend help {{{ + LOOKUP_guard || return 1 + printf 'usage: %s \n' ${backend} + printf ' -l search for original list names (like zsh-workers)\n' + printf ' -s match gmane group names (like comp.shell)\n' + printf ' -L list-mode; do not open a browser, just print the results\n' + printf ' valid with -l and -s.\n' + printf ' -B browser-mode; opposite of -L\n' + printf ' -m search for Message-Id: header\n' + printf ' -a match messages only by a certain author\n' + printf ' -g match messages only from a certain gmane group\n' + printf ' -o use defined operation in search (%s)\n' "${(j:/:)${(@kon)known_opers}}" + printf ' -S use given sorting method (%s)\n' "${(j:/:)${(@kon)known_sorts}}" + printf '\n Search the archives of the gmane.org mail2news gateway.\n' + printf '\n Default sort method: relevance\n' + printf ' Default query logic: and\n' + printf ' List-mode (-L and -B options) defaults to: off\n' + printf ' Default group bookmark file: %s\n' "${HOME}/.zgmane.bookmarks" + printf ' All defaults may be altered by the use of styles.\n' + printf '\nStyles:\n' + printf ' All styles are looked up in this context:\n' + printf ' %s\n\n' ${lookup_context} + printf ' group-bookmarks: file that contains group bookmarks (see below)\n' + printf ' list-default: boolean, use list-mode by default if true\n' + printf ' default-sort: change default sorting method\n' + printf ' default-oper: change default query logic\n' + printf '\nBookmarks:\n' + printf '\n Often you will want to limit your queries to a certain gmane group.\n' + printf ' Since these are lengthy at times, it is awful to type them out.\n' + printf ' Therefore, this backend gives you a bookmark file. Its format is:\n' + printf ' gname.group.name:description\n' + printf ' The '\'':description'\'' part is optional.\n' + printf ' That way you can search for group names (using -L, -l and -s) and put\n' + printf ' your prefered groups into that file. That way you get rid of a lot of\n' + printf ' typing by using completion: %% lu gmane -g \n' + printf '\nExamples:\n' + printf ' %% zstyle '\'':lookup:*:%s:*'\'' list-default true\n' ${backend} + printf ' %% zstyle '\'':lookup:*:%s:*'\'' default-sort date\n' ${backend} + printf ' %% zstyle '\'':lookup:*:%s:*'\'' default-oper or\n' ${backend} + printf ' %% zstyle '\'':lookup:*:%s:*'\'' group-bookmarks ~/.my_gmane_bookmarks\n' ${backend} + printf ' %% lookup %s zsh\n' ${backend} + printf ' %% lookup %s -m 20090215095848.GA21661@schmehl.info\n' ${backend} + printf ' %% lookup %s -g gmane.comp.shells.fish.user zsh\n' ${backend} + printf ' %% lookup %s -L -l zsh-workers\n' ${backend} + printf ' %% lookup %s -L -s openbsd\n' ${backend} + #}}} +} +LOOKUP_help && return 0 + +if [[ -n ${lookup_complete} ]] ; then + # gmane backend completion code {{{ + local -a comp_args + + LOOKUP_guard -fd __lookup_${backend}_known_operations || + function __lookup_${backend}_known_operations() { + local o + local -a os + + os=() + for o in ${(k)known_opers}; do + os+=("$o:${known_opers[$o]}") + done + _describe -t gmane_opers 'known operations' os + } + + LOOKUP_guard -fd __lookup_${backend}_known_sorts || + function __lookup_${backend}_known_sorts() { + local s + local -a as + + as=() + for s in ${(k)known_sorts}; do + as+=("$s:${known_sorts[$s]}") + done + _describe -t gmane_sorts 'known sorting methods' as + } + + LOOKUP_guard -fd __lookup_${backend}_group_bookmarks || + function __lookup_${backend}_group_bookmarks() { + local -a bms + + if [[ -r ${bookmarks} ]] ; then + bms=(${(f)"$(< $bookmarks)"}) + fi + _describe -t gmane_bookmarks 'bookmarked gmane groups' bms + } + + comp_args=( + '-l[search for original list names]:list name:' + '-s[match gmane group names]:group name:' + '-L[switch to list-mode]' + '-m[query for message ids]:Message-Id\::' + '-a[author name]:author:' + '-g[group name]:group:__lookup_'${backend}'_group_bookmarks' + '-o[operations]:operation:__lookup_'${backend}'_known_operations' + '-S[sorting method]:sorting method:__lookup_'${backend}'_known_sorts' + '*:wikipedia search:true' + ) + + _arguments -s -w -A '-*' ${comp_args} && return 0 + _message 'gmane query' + return 0 + #}}} +fi + +local author group operation sort ret list_mode +local -x QUERY + +# set some defaults via zstyle +zstyle -t "${lookup_context}" list-default && list_mode='yes' +zstyle -s "${lookup_context}" default-sort sort || sort='relevance' +zstyle -s "${lookup_context}" default-oper operation || operation='and' + +# parse options +lu_parseopts_args=( + l bool + m bool + s bool + B bool + L bool + a string + g string + o string + S string +) +LOOKUP_parseopts "$@" || return 1 +[[ -n ${opts[-a]} ]] && author="$(LOOKUP_encode ${opts[-a]})" +[[ -n ${opts[-g]} ]] && group="$(LOOKUP_encode ${opts[-g]})" +[[ -n ${opts[-o]} ]] && operation="${opts[-o]}" +[[ -n ${opts[-S]} ]] && sort="${opts[-S]}" +[[ -n ${opts[-L]} ]] && list_mode='yes' +[[ -n ${opts[-B]} ]] && list_mode='' + +# set QUERY and export parameters for query handlers via $lookup_communicate[] +QUERY="${args[*]}" +lookup_communicate[author]="${author}" +lookup_communicate[group]="${group}" +lookup_communicate[operation]="${operation}" +lookup_communicate[sort]="${sort}" +LOOKUP_query_handler || return 1 +[[ ${lookup_communicate[author]} != ${author} ]] && author="${lookup_communicate[author]}" +[[ ${lookup_communicate[group]} != ${group} ]] && group="${lookup_communicate[group]}" +[[ ${lookup_communicate[operation]} != ${operation} ]] && operation="${lookup_communicate[operation]}" +[[ ${lookup_communicate[sort]} != ${sort} ]] && sort="${lookup_communicate[sort]}" + +# validate arguments +if [[ -z ${(Mk)known_opers:#$operation} ]] ; then + printf 'Unknown operation: '\''%s'\''.\n\n' ${mode} + QUERY='' +elif [[ -z ${(Mk)known_sorts:#$sort} ]] ; then + printf 'Unknown sorting method: '\''%s'\''.\n\n' ${mode} + QUERY='' +fi +if [[ -z ${QUERY} ]] && + ( [[ ${opts[-l]} == 'yes' ]] || [[ ${opts[-s]} == 'yes' ]] || [[ ${opts[-m]} == 'yes' ]] ) + then + + LOOKUP_help -f + return 1 +fi +if [[ -z ${QUERY} ]] && [[ -z ${group} ]] && [[ -z ${author} ]] ; then + LOOKUP_help -f + return 1 +fi + +LOOKUP_guard -fd LOOKUP_beh_${backend}_list || +function LOOKUP_beh_${backend}_list() { + # list mode via tcp_{open,send,close} {{{ + LOOKUP_guard || return 1 + setopt localoptions no_warncreateglobal + local mode="$1" q="$2" + + LOOKUP_guard -fd tcp_open || autoload -Uz tcp_open + LOOKUP_guard -fd tcp_send || autoload -Uz tcp_send + + case ${mode} in + (list) + tcp_open -q gmane.org 80 gmane || return 1 + tcp_send -s gmane -- "GET /find.php?list=${q} HTTP/1.1" + tcp_send -s gmane -- 'HOST: gmane.org' + ;; + (match) + tcp_open -q dir.gmane.org 80 gmane || return 1 + tcp_send -s gmane -- "GET /search.php?match=${q} HTTP/1.1" + tcp_send -s gmane -- 'HOST: dir.gmane.org' + ;; + (*) + return 1 + ;; + esac + + tcp_send -s gmane -- 'Connection: close' + tcp_send -s gmane -- '' + + TCP_SILENT='yes' + tcp_read -b -d -s gmane + TCP_SILENT='' + + tcp_close -q -s gmane + + results=() + for line in ${tcp_lines} ; do + [[ ${line} != *'dir.gmane.org'* ]] && continue + line=${line//(#b)*\"(http:\/\/dir.gmane.org\/[^\"]#)\"*/${match[1]}} + line=${line#http://dir.gmane.org/} + [[ -z ${line} ]] && continue + [[ ${line} != gmane.* ]] && continue + results+=( ${line} ) + done + + print -l ${results} + return 0 + #}}} +} + +# make the actual queries +LOOKUP_encode -q +if [[ ${opts[-l]} == 'yes' ]] ; then + if [[ ${list_mode} == 'yes' ]] ; then + LOOKUP_beh_${backend}_list 'list' ${QUERY} + ret="$?" + else + LOOKUP_browser "http://gmane.org/find.php?list=${QUERY}" + ret="$?" + fi +elif [[ ${opts[-s]} == 'yes' ]] ; then + if [[ ${list_mode} == 'yes' ]] ; then + LOOKUP_beh_${backend}_list 'match' ${QUERY} + ret="$?" + else + LOOKUP_browser "http://dir.gmane.org/search.php?match=${QUERY}" + ret="$?" + fi +elif [[ ${opts[-m]} == 'yes' ]] ; then + LOOKUP_browser "http://mid.gmane.org/${QUERY}" + ret="$?" +else + LOOKUP_browser "http://search.gmane.org/?query=${QUERY}&author=${author}&group=${group}&DEFAULTOP=${operation}&sort=${sort}" + ret="$?" +fi +return ${ret} diff --git a/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_google b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_google new file mode 100644 index 0000000..dbe91e3 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_google @@ -0,0 +1,62 @@ +### vim:ft=zsh:foldmethod=marker +## google search backend for lookup +## Copyright: 2009, Frank Terbeck + +LOOKUP_guard || return 1 +[[ -n ${lookup_describe} ]] && printf '%s' 'google websearch' && return 0 + +if [[ -n ${lookup_complete} ]] ; then + local -a comp_args + comp_args=( + '-1[I'\''m Feeling Lucky!]' + '*:dict.leo.org query:true' + ) + + _arguments -s -w -A '-*' ${comp_args} && return 0 + _message 'google query' + return 0 +fi + +local mode lookup_context +local -x QUERY + +lookup_context="$(LOOKUP_context)" + +LOOKUP_guard -fd LOOKUP_help_${backend} || +function LOOKUP_help_${backend}() { + LOOKUP_guard || return 1 + printf 'usage: %s [-1] \n' ${backend} + printf ' -1 Go directly to the first hit using "I'\''m Feeling Lucky"\n' + printf '\n Make web searches via google.com\n' + printf ' If you always want to use the -1 option, you may set the use-first-hit\n' + printf ' style in this context: %s\n' ${lookup_context} + printf '\nExamples:\n' + printf ' %% lookup %s zsh\n' ${backend} + printf ' %% lookup %s -1 openbsd\n' ${backend} + printf ' %% zstyle '\'':lookup:*:%s:*'\'' use-first-hit true\n\n' ${backend} +} +LOOKUP_help && return 0 + +lu_parseopts_args=( 1 bool ) +LOOKUP_parseopts "$@" || return 1 + +if [[ ${opts[-1]} == 'yes' ]] || + zstyle -t "${lookup_context}" use-first-hit ; then + + mode='I'\''m Feeling Lucky' + mode="$(LOOKUP_encode ${mode})" + mode="&btnI=${mode}" +else + mode='' +fi + +QUERY="${args[*]}" +LOOKUP_query_handler || return 1 +if [[ -z ${QUERY} ]] ; then + LOOKUP_help -f + return 1 +fi + +LOOKUP_encode -s -q +LOOKUP_browser "http://www.google.com/search?q=${QUERY}${mode}" +return $? diff --git a/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_koders b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_koders new file mode 100644 index 0000000..0a41d64 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_koders @@ -0,0 +1,189 @@ +### vim:ft=zsh:foldmethod=marker +## koders.com source code search backend for lookup +## Copyright: 2009, Frank Terbeck + +LOOKUP_guard || return 1 +[[ -n ${lookup_describe} ]] && + printf '%s' 'source code search via koders.com' && + return 0 + +local lookup_context +local -a comp_args known_langs +local -A known_licences + +lookup_context="$(LOOKUP_context)" + +known_langs=( +#{{{ + "ActionScript" + "Ada" + "ASP" + "ASP.NET" + "Assembler" + "C" + "C#" + "C++" + "Cobol" + "ColdFusion" + "Delphi" + "Eiffel" + "Erlang" + "Fortran" + "Java" + "JavaScript" + "JSP" + "Lisp" + "Lua" + "Mathematica" + "Matlab" + "ObjectiveC" + "Perl" + "PHP" + "Prolog" + "Python" + "Ruby" + "Scheme" + "Smalltalk" + "SQL" + "Tcl" + "VB" + "VB.NET" + "*" +#}}} +) + +known_licences=( +#{{{ + AFL 'Academic Free Licence' + AGPLv3 'Affero General Public Licence v3' + AL20 'Apache Licence, Version 2.0' + ASL 'Apache Software Licence' + APSL 'Apple Public Source Licence' + BSD 'Berkeley Software Distribution Licence' + CPL 'Common Public Licence' + EPL10 'Eclipse Public Licence v1.0' + GTPL 'Globus Toolkit Public Licence' + GPL 'GNU General Public Licence v1 v2 v3' + LGPL 'GNU Lesser General Public Licence' + IBMPL 'IBM Public Licence' + IBMILA 'International Licence Agreement for Non-Warranted Programs' + IOSL 'Intel Open Source Licence' + MSCL 'Microsoft Community Licence' + MSLCL 'Microsoft Limited Community Licence' + MSPL 'Microsoft Permissive Licence' + MSLPL 'Microsoft Limited Permissive Licence' + MSRL 'Microsoft Reference Licence' + MSVSSDK 'Microsoft Visual Studio SDK Licence' + MITD 'MIT Derived Licence' + MPL10 'Mozilla Public Licence Version 1.0' + MPL11 'Mozilla Public Licence Version 1.1' + NPL10 'Netscape Public Licence, Version 1.0' + NPL11 'Netscape Public Licence, Version 1.1' + OSL 'Open Software Licence' + PHPL 'PHP Licence' + CNRIPL 'Python Licence' + PSFL 'Python Software Foundation Licence' + SL 'Sleepycat Software Product Licence' + SISSL 'Sun Industry Standards Source Licence' + SPL 'Sun Public Licence' + W3C 'W3C Software Licence' + WXWLL 'wxWindows Library Licence' + ZLL 'zlib/libpng Licence' + ZPL 'Zope Public Licence' + "*" 'Match all licences' +#}}} +) + +LOOKUP_guard -fd LOOKUP_help_${backend} || +function LOOKUP_help_${backend}() { + LOOKUP_guard || return 1 + local l + local -i i + local -a ls + + i=8 + ls=(${(on)known_langs}) + + printf 'usage: %s \n' ${backend} + printf ' -l limit search to specific language\n' + printf ' -L limit search to specific licence\n' + printf '\n Search source code via koders.com\n' + printf '\nKnown languages:\n' + while (( ${#ls} > 0 )) ; do + if (( ${#ls} > i )) ; then + print ' ' ${ls[1,$i]} + shift $i ls + else + print ' ' ${ls} + ls=() + fi + done + printf '\nKnown licences:\n' + for l in ${(kon)known_licences} ; do + printf '%8s - %s\n' $l ${known_licences[$l]} + done + printf '\n Per default this backend makes unlimited searches.\n' + printf ' Results include code from all languages in all licences.\n' + printf '\n You may specify a default language and a default licence via\n' + printf ' the default-lang and default-licence styles (both default to '\''*'\''\n' + printf ' which means unlimited) in this context: %s\n' ${lookup_context} + printf '\nExamples:\n' + printf ' %% zstyle '\'':lookup:*:%s:*'\'' default-lang C\n' ${backend} + printf ' %% zstyle '\'':lookup:*:%s:*'\'' default-licence BSD\n\n' ${backend} + printf ' %% lookup %s strlcpy\n' ${backend} + printf ' %% lookup %s -l C handle_keys\n' ${backend} + printf ' %% lookup %s -L GPL -l C strlcpy\n\n' ${backend} +} +LOOKUP_help && return 0 + +if [[ -n ${lookup_complete} ]] ; then + + LOOKUP_guard -fd __lookup_${backend}_langs || + function __lookup_${backend}_langs() { + _describe -t koders_langs 'known languages' known_langs + } + + LOOKUP_guard -fd __lookup_${backend}_licences || + function __lookup_${backend}_licences() { + local l + local -a ls + + ls=() + for l in ${(k)known_licences}; do + ls+=("$l:${known_licences[$l]}") + done + _describe -t koders_licences 'known licences' ls + } + + comp_args=( + '-l[limit search to specific language]:language:__lookup_'${backend}'_langs' + '-L[limit search to specific licence]:licence:__lookup_'${backend}'_licences' + '*:source code search string:true' + ) + + _arguments -s -w -A '-*' ${comp_args} && return 0 + _message 'source code search string' + return 0 +fi + +local lang licence +local -x QUERY + +zstyle -s "${lookup_context}" default-lang lang || lang='*' +zstyle -s "${lookup_context}" default-licence licence || licence='*' + +lu_parseopts_args=( l string L string ) +LOOKUP_parseopts "$@" || return 1 +[[ -n ${opts[-l]} ]] && lang="${opts[-l]}" +[[ -n ${opts[-L]} ]] && licence="${opts[-L]}" + +QUERY="${args[*]}" +LOOKUP_query_handler || return 1 +if [[ -z ${QUERY} ]] ; then + LOOKUP_help -f + return 1 +fi + +LOOKUP_encode -s -q +LOOKUP_browser "http://koders.com/default.aspx?s=${QUERY}&la=${lang}&li=${licence}" +return $? diff --git a/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_leo b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_leo new file mode 100644 index 0000000..73a4744 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_leo @@ -0,0 +1,190 @@ +### vim:ft=zsh:foldmethod=marker +## dict.leo.org search backend for lookup +## Copyright: 2009, Frank Terbeck +## +## This was the first non-trivial backend for lookup. Therefore, +## it contains some guidelines on how to write backends and what they +## are expected to do. +## +## For consistency in usage among different backends, please try +## to follow the general style of this one. +## + +# This should be the first thing a backend does. +# It makes sure a backend function is not run directly by a user, but +# only via lookup(), lu() or _lookup(). +LOOKUP_guard || return 1 + +# This check is required and it should be the second thing the backend does. +# If $lookup_describe is not empty, only print a short description +# of what the backend does. Without a newline at the end! +[[ -n ${lookup_describe} ]] && + printf '%s' 'translations via dict.leo.org' && + return 0 + +# declare variables. Assume warncreateglobal is enabled. +local lang lookup_context +local -a comp_args +local -A opts supported_langs interface_lang + +# For the actual query string, always use QUERY and make it 'local -x'! +# Parts of the lookup system depend on $QUERY to be declared like this! +local -x QUERY + +supported_langs=( + ende 'english german' + frde 'french german' + esde 'spanish german' + itde 'italian german' + chde 'chinese german' +) + +# To find out the current context string you can use the LOOKUP_context() +# function like this. Please don't use $context or $curcontext to store +# the result (that may clash with compsys). If you use lookup_context +# you cannot go wrong. For modifying the local-part of the context, see +# the comment in the LOOKUP_context file. +lookup_context="$(LOOKUP_context)" + +# Lookup backends should be self-documenting. +# You need to provide a function that prints a useful message that helps +# the user use the backend. +# DO NOT hardcode the name of the function. Always use LOOKUP_help_${backend} +# and guard its definition by 'LOOKUP_guard -fd LOOKUP_help_${backend}' just +# like it is done here. +LOOKUP_guard -fd LOOKUP_help_${backend} || +function LOOKUP_help_${backend}() { + LOOKUP_guard || return 1 + #-80characters-----------------------------------------------------------------# + printf 'usage: %s [-l lang] \n' ${backend} + printf ' -l specify a language definition string\n' + printf '\nAvailable language definition strings:\n' + for lang in ${(k)supported_langs} ; do + printf '%6s - %s\n' ${lang} ${supported_langs[$lang]} + done + printf '\n Makes dictionary lookups via dict.leo.org.\n\n' + printf ' Default language code: ende (german <-> english).\n' + printf ' The interface language used by the website defaults to: en\n' + printf '\n These can be modified via the '\''default-lang'\'' and '\''interface-lang'\''\n' + printf ' styles in this context: %s\n\n' ${lookup_context} + printf 'Examples:\n' + printf ' %% zstyle '\'':lookup:*:%s:*'\'' default-lang frde\n' ${backend} + printf ' %% zstyle '\'':lookup:*:%s:*'\'' interface-lang de\n\n' ${backend} + printf ' %% lookup %s sugar\n' ${backend} + printf ' %% lookup %s -l frde Zucker\n\n' ${backend} +} + +# This call must be here, too. Just after you defined the help function. +# It handles the global -h option of lookup correctly. +LOOKUP_help && return 0 + +# This test is needed, too. +# Backends are supposed to bring there own completion. +if [[ -n ${lookup_complete} ]] ; then + # During development, you can just put a + #return 0 + # here. That will disable completion for the backend and + # prevent breakage for other backends. + + # completion sub-functions should be called: + # __lookup_${backend}_() + # Again, DO NOT hardcode the name. Use ${backend}! + # guard their definition by checking the $functions[] entry + # that belongs to your sub-function. + LOOKUP_guard -fd __lookup_${backend}_supported_langs || + function __lookup_${backend}_supported_langs() { + local l + local -a ls + + ls=() + for l in ${(k)supported_langs}; do + ls+=("$l:${supported_langs[$l]}") + done + _describe -t leo_langs 'supported languages' ls + } + + comp_args=( + '-l[set languages]:supported languages:__lookup_'${backend}'_supported_langs' + '*:dict.leo.org query:true' + ) + + _arguments -s -w -A '-*' ${comp_args} && return 0 + _message 'dict.leo.org query' + + return 0 + # end of completion code # +fi + +# parse possible options of the backend. +# 'LOOKUP_parseopts' is the recommended way of doing that. +# See the top of that function's file for an explained example. +# Since LOOKUP_parseopts() returns its findings via $opt[] and $args[], you +# should *never* use variables of those names in your backends. +lu_parseopts_args=( l string ) +LOOKUP_parseopts "$@" || return 1 +lang=${opts[-l]} + +# all configuration should be done via zstyle. The context should always be +# the result of what LOOKUP_context() tells us (see the $lookup_context comment +# earlier, too). Also set reasonable defaults. +if [[ -z ${lang} ]] ; then + zstyle -s "${lookup_context}" default-lang lang || lang='ende' +fi + +zstyle -s "${lookup_context}" interface-lang interface_lang || interface_lang='en' + +# If applicable, use the remaining arguments as QUERY. +# If the backend used LOOKUP_parseopts() before, the remaining arguments +# are in $args[]. If not, they can be assigned like this: +# QUERY="$*" +# +# Since we do use LOOKUP_parseopts(), we'll do it like this: +QUERY="${args[*]}" + +# Call LOOKUP_query_handler() to support the -Q and -q options of lookup. +# This should also be called like this in every case, as this supports +# a return value from handler functions that indicates unrecoverable errors. +# If you want to support return values from handlers, use the +# $lookup_communicate[] hash, which is meant to be used for that. It can +# also be used to get additional information into the handler. +# See LOOKUP_be_letssingit for an example. +LOOKUP_query_handler || return 1 + +# Do appropriate error checking and give usage information +# if the query is empty. +if [[ -z ${(Mk)supported_langs:#$lang} ]] ; then + printf 'Unknown language definition: '\''%s'\''.\n\n' ${lang} + # Clear out $QUERY, so we get the usage message below. + QUERY='' +fi +if [[ -z ${QUERY} ]] ; then + # -f means, "display the help text, no matter what." + LOOKUP_help -f + return 1 +fi + +# You may also put in hooks into your backend. You don't need to though. :) +# To do so, use the LOOKUP_hook function. The first argument should be a name +# for the hook. That will adjust the local-part of the context to that name. +# If you don't want that, use -- as the first argument. Then the context +# remains unchanged. +# The rest of the arguments are handed over to the hook function unchanged. +# There is no convention as to what arguments should be provided or whether +# $lookup_communicate[] should be used. Everything is possible; just make +# sure you document your hooks properly in the help function earlier. +# +# A sample hook call may look like this: +#LOOKUP_hook "pre-encode" $QUERY $lang $interface_lang + +# Use LOOKUP_encode() for proper url encoding of the QUERY. +# Since, QUERY is the most common use case of LOOKUP_encode, it has +# a special option to handle that variable. +# For other variables you want to encode, do: +# foo="$(LOOKUP_encode ${foo})" +LOOKUP_encode -q + +# Use our browser wrapper to open the URI, it'll handle everything just right. +LOOKUP_browser "http://dict.leo.org?lang=${interface_lang}&lp=${lang}&search=${QUERY}" + +# Explicitly return $?. +return $? diff --git a/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_letssingit b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_letssingit new file mode 100644 index 0000000..357cb93 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_letssingit @@ -0,0 +1,89 @@ +### vim:ft=zsh:foldmethod=marker +## letssingit.com search backend for lookup +## Copyright: 2009, Frank Terbeck + +LOOKUP_guard || return 1 +[[ -n ${lookup_describe} ]] && + printf '%s' 'search letssingit.com' && + return 0 + +local mode lookup_context +local -a comp_args +local -A opts supported_modes +local -x QUERY + +lookup_context="$(LOOKUP_context)" + +supported_modes=( + artist 'search by artist' + album 'search by album' + song 'search by song name' + lyrics 'search contents of lyrics' +) + +LOOKUP_guard -fd LOOKUP_help_${backend} || +function LOOKUP_help_${backend}() { + LOOKUP_guard || return 1 + printf 'usage: %s [-m mode] \n' ${backend} + printf ' -m specify search mode\n' + printf '\n Make music related queries via letssingit.com\n' + printf '\nAvailable modes are::\n' + for mode in ${(kon)supported_modes} ; do + printf '%8s - %s\n' ${mode} ${supported_modes[$mode]} + done + printf '\n The default mode is '\''artist'\'', which can be modified via the\n' + printf ' '\''default-mode'\'' style in this context: %s\n\n' ${lookup_context} + printf 'Examples:\n' + printf ' %% zstyle '\'':lookup:*:%s:*'\'' default-mode song\n' ${backend} + printf ' %% lookup %s Led Zeppelin\n' ${backend} + printf ' %% lookup %s -m album Machine Head\n' ${backend} + printf ' %% lookup %s -m song Paint it Black\n' ${backend} +} +LOOKUP_help && return 0 + +if [[ -n ${lookup_complete} ]] ; then + LOOKUP_guard -fd __lookup_${backend}_supported_modes || + function __lookup_${backend}_supported_modes() { + local m + local -a ms + + ms=() + for m in ${(k)supported_modes}; do + ms+=("$m:${supported_modes[$m]}") + done + _describe -t letssingit_modes 'supported search modes' ms + } + + comp_args=( + '-m[set search mode]:supported search modes:__lookup_'${backend}'_supported_modes' + '*:letssingit.com query:true' + ) + + _arguments -s -w -A '-*' ${comp_args} && return 0 + _message 'letssingit.com query' + return 0 +fi + +lu_parseopts_args=( m string ) +LOOKUP_parseopts "$@" || return 1 +mode=${opts[-m]} + +if [[ -z ${mode} ]] ; then + zstyle -s "${lookup_context}" default-mode mode || mode='artist' +fi + +QUERY="${args[*]}" +lookup_communicate[mode]=${mode} +LOOKUP_query_handler || return 1 +if [[ -z ${(Mk)supported_modes:#$mode} ]] ; then + printf 'Unknown search mode: '\''%s'\''.\n\n' ${mode} + QUERY='' +fi +if [[ -z ${QUERY} ]] ; then + LOOKUP_help -f + return 1 +fi + +LOOKUP_encode -q +LOOKUP_browser "http://search.letssingit.com/cgi-exe/am.cgi?a=search_quick&l=${mode}&s=${QUERY}" +return $? diff --git a/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_rfc b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_rfc new file mode 100644 index 0000000..88e4fd9 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_rfc @@ -0,0 +1,138 @@ +### vim:ft=zsh:foldmethod=marker +## RFC backend for lookup +## Copyright: 2009, Frank Terbeck + +# TODO: This is currently just a real lookup. rfc-editor.com supports searching. +# ...would be nice... + +LOOKUP_guard || return 1 +[[ -n ${lookup_describe} ]] && + printf '%s' 'RFC lookup' && + return 0 + +local lookup_context +local -a comp_args + +lookup_context="$(LOOKUP_context)" + +LOOKUP_guard -fd LOOKUP_help_${backend} || +function LOOKUP_help_${backend}() { + LOOKUP_guard || return 1 + #-80characters-----------------------------------------------------------------# + printf 'usage: %s [-{d,w}] \n' ${backend} + printf ' -d ask to download RFC before reading\n' + printf ' -w just watch. do not ask to download\n' + printf '\n Read RFCs by number.\n' + printf ' Supports local files and remote files (via rfc-editor.com).\n' + printf '\n First, it looks for a local version of the RFC (within a given path, see\n' + printf ' Styles below); if a local version is found, it is opened in a browser. If it\n' + printf ' cannot be found (per default) the user is asked if we should try to download\n' + printf ' the RFC in question via FTP from rfc-editor.com (only works if the zsh/zftp\n' + printf ' module can be loaded). If the answer is yes, the file is downloaded. If that\n' + printf ' was successful the *local* file is viewed. If the answer is no, the remote\n' + printf ' URL is opened in a browser.\n' + printf '\nStyles:\n' + printf ' The context in which styles are looked up is:\n' + printf ' %s\n' ${lookup_context} + LOOKUP_context -l local-files + lookup_context="$(LOOKUP_context)" + printf ' If a local file is viewed, that changes to:\n' + printf ' %s\n\n' ${lookup_context} + printf ' search-path: a list style, defining where to look for local RFCs\n' + printf ' (Default: one entry: $HOME - "%s")\n' ${HOME} + printf ' save-path: when downloading RFCs, save them to this directory\n' + printf ' (Default: $HOME - "%s")\n' ${HOME} + printf ' download: boolean; changes the default downloading behaviour\n' + printf ' (Default: true: always ask to download non-local RFCs)\n' + printf '\n When looking for local RFCs, this backend looks for files named like this:\n' + printf ' rfc<->.txt(|.gz|.bz2) (eg: rfc123.txt, rfc234.txt.gz or rfc345.txt.bz2)\n' + printf '\nExamples:\n' + printf ' %% zstyle '\'':lookup:*:%s:*'\'' search-path /usr/share/doc/RFC/links ~/myrfcs\n' ${backend} + printf ' %% zstyle '\'':lookup:*:%s:*'\'' save-path ~/myrfcs\n' ${backend} + printf ' %% zstyle '\'':lookup:*:%s:*'\'' download false\n' ${backend} + printf ' %% lookup %s 881\n' ${backend} + printf ' %% lookup %s -w 881\n\n' ${backend} +} +LOOKUP_help && return 0 + +if [[ -n ${lookup_complete} ]] ; then + comp_args=( + '-d[attempt do download RFCs]:' + '-w[never attempt do download RFCs]:' + '*:RFC number:true' + ) + + _arguments -s -w -A '-*' ${comp_args} && return 0 + _message 'RFC number' + return 0 +fi + +local dir file save_path search_path yesno +local -i justwatch +local -A opts +local -x QUERY + +zstyle -s "${lookup_context}" save-path save_path || save_path=${HOME} +zstyle -a "${lookup_context}" search-path search_path || search_path=(${HOME}) + +if zstyle -t "${lookup_context}" download ; then + justwatch='0' +else + justwatch='1' +fi + +lu_parseopts_args=( d bool w bool ) +LOOKUP_parseopts "$@" || return 1 +[[ ${opts[-d]} == 'yes' ]] && justwatch=0 +[[ ${opts[-w]} == 'yes' ]] && justwatch=1 + +QUERY="${args[*]}" +LOOKUP_query_handler || return 1 +if [[ -z ${QUERY} ]] ; then + LOOKUP_help -f + return 1 +fi + +for dir in ${search_path} ; do + file="${dir}/rfc${QUERY}.txt" + if [[ -r ${file} ]] ; then + LOOKUP_browser ${file} + return $? + elif [[ -r ${file}.gz ]] ; then + LOOKUP_context -l local-files + LOOKUP_browser ${file}.gz + return $? + elif [[ -r ${file}.bz2 ]] ; then + LOOKUP_context -l local-files + LOOKUP_browser ${file}.bz2 + return $? + fi +done + +if (( lookup_printout == 0 )) && (( justwatch == 0 )) && [[ -n ${save_path} ]] ; then + if [[ -z ${builtins[zftp]} ]] ; then + if ! zmodload zsh/zftp ; then + printf 'Module zsh/zftp could not be loaded, cannot download.\n' + return 1 + fi + fi + printf 'RFC: %s, save-path: %s\n' ${QUERY} ${save_path} + printf 'Shall I download the rfc in question? [y/n] ' + read -q yesno + if [[ ${yesno} == 'y' ]] ; then + LOOKUP_context -l local-files + file="${save_path}/rfc${QUERY}.txt" + + zftp open ftp.rfc-editor.org "anonymous" "zsh_lookup@somewhere.tld" + zftp cd "/in-notes" + zftp binary + zftp get "rfc${QUERY}.txt" > ${file} + quit + + LOOKUP_browser ${file} + return $? + fi +fi + +LOOKUP_browser "ftp://ftp.rfc-editor.org/in-notes/rfc${QUERY}.txt" +return $? diff --git a/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_sourceforge b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_sourceforge new file mode 100644 index 0000000..e3fc0a8 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_sourceforge @@ -0,0 +1,39 @@ +### vim:ft=zsh:foldmethod=marker +## sf.net search backend for lookup +## Copyright: 2009, Frank Terbeck + +LOOKUP_guard || return 1 +[[ -n ${lookup_describe} ]] && + printf '%s' 'search sourceforge.net' && + return 0 + +local -a comp_args + +LOOKUP_guard -fd LOOKUP_help_${backend} || +function LOOKUP_help_${backend}() { + LOOKUP_guard || return 1 + printf 'usage: %s \n' ${backend} + printf '\n Query sourceforge.net for software projects.\n' + printf '\nExamples:\n' + printf ' %% lookup %s zsh\n' ${backend} + printf ' %% lookup %s fdm\n\n' ${backend} +} +LOOKUP_help && return 0 + +if [[ -n ${lookup_complete} ]] ; then + _message 'sf.net search string' + return 0 +fi + +local -x QUERY + +QUERY="$*" +LOOKUP_query_handler || return 1 +if [[ -z ${QUERY} ]] ; then + LOOKUP_help -f + return 1 +fi + +LOOKUP_encode -s -q +LOOKUP_browser "http://sourceforge.net/search/?words=${QUERY}&type_of_search=soft" +return $? diff --git a/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_usenet b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_usenet new file mode 100644 index 0000000..f092041 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_usenet @@ -0,0 +1,100 @@ +### vim:ft=zsh:foldmethod=marker +## usenet search backend for lookup via groups.google.com +## Copyright: 2009, Frank Terbeck + +LOOKUP_guard || return 1 +[[ -n ${lookup_describe} ]] && + printf '%s' 'usenet search' && + return 0 + +local lookup_context +local -a comp_args +local -A known_modes + +lookup_context="$(LOOKUP_context)" + +known_modes=( + text 'Free text search' + msgid 'Search for Message-ID: header' + author 'Search for a specific author' +) + +LOOKUP_guard -fd LOOKUP_help_${backend} || +function LOOKUP_help_${backend}() { + LOOKUP_guard || return 1 + local m + printf 'usage: %s [-{t,m}] [-a ] \n' ${backend} + printf ' -m search for message id\n' + printf ' -t free text search\n' + printf ' -a search for messages by \n' + printf '\n Search google'\''s usenet archives via groups.google.com.\n' + printf '\n Per default, free text searches are done, which can be modified via the\n' + printf ' '\''default-mode'\'' style in this context: %s\n\n' ${lookup_context} + for m in ${(k)known_modes} ; do + printf '%8s - %s\n' $m ${known_modes[$m]} + done + printf '\n As an exception the argument may be empty in author mode.\n' + printf ' If you set default-mode to '\''author'\'', you may also set the\n' + printf ' '\''default-author'\'' style (which defaults to '\''Joe User'\'').\n' + printf '\nExamples:\n' + printf ' %% zstyle '\'':lookup:*:%s:*'\'' default-mode author\n' ${backend} + printf ' %% zstyle '\'':lookup:*:%s:*'\'' default-author "Dr. Foo Bar"\n' ${backend} + printf ' %% lookup %s zsh prompt colours\n' ${backend} + printf ' %% lookup %s -a pws Bugs in zsh\n\n' ${backend} +} +LOOKUP_help && return 0 + +if [[ -n ${lookup_complete} ]] ; then + comp_args=( + '-a[specify author]:author:' + '-m[search for message id]:message id:' + '-t[free text search]:text:' + '*:usenet search string:true' + ) + + _arguments -s -w -A '-*' ${comp_args} && return 0 + _message 'usenet search string' + return 0 +fi + +local mode author +local -A opts +local -x QUERY + +lu_parseopts_args=( m bool t bool a string ) +LOOKUP_parseopts "$@" || return 1 +[[ ${opts[-t]} == 'yes' ]] && mode='text' +[[ ${opts[-m]} == 'yes' ]] && mode='msgid' +if [[ -n ${opts[-a]} ]] ; then + mode='author' + author="&as_uauthors=$(LOOKUP_encode ${opts[-a]})" +fi + +if [[ -z ${mode} ]] ; then + zstyle -s "${lookup_context}" default-mode mode || mode='text' + if [[ mode == 'author' ]] && [[ -z ${author} ]] ; then + local a + zstyle -s "${lookup_context}" default-author a || a='Joe User' + author="&as_uauthors=$(LOOKUP_encode $a})" + fi +fi + +QUERY="${args[*]}" +LOOKUP_query_handler || return 1 +if [[ -z ${(Mk)known_modes:#$mode} ]] ; then + printf 'Unknown mode: '\''%s'\''.\n\n' ${mode} + QUERY='' +fi +if [[ -z ${QUERY} ]] && [[ ${mode} != 'author' ]] ; then + LOOKUP_help -f + return 1 +fi + +LOOKUP_encode -s -q +if [[ ${mode} == 'msgid' ]] ; then + LOOKUP_browser "http://groups.google.com/groups?selm=${QUERY}" + return $? +fi + +LOOKUP_browser "http://groups.google.com/groups?q=${QUERY}${author}" +return $? diff --git a/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_wikipedia b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_wikipedia new file mode 100644 index 0000000..4135666 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_wikipedia @@ -0,0 +1,115 @@ +### vim:ft=zsh:foldmethod=marker +## wikipedia backend for lookup +## Copyright: 2009, Frank Terbeck + +LOOKUP_guard || return 1 +[[ -n ${lookup_describe} ]] && + printf '%s' 'search wikipedia' && + return 0 + +local lookup_context +local -a comp_args private_completions +local -A known_locations + +lookup_context="$(LOOKUP_context)" + +known_locations=( + # yeah, there are more... a lot more. + ca 'Catalan' + cs 'Czech' + da 'Danish' + de 'German' + en 'English' + eo 'Esperanto' + es 'Spanish' + 'fi' 'Finnish' + fr 'French' + hu 'Hungarian' + it 'Italian' + ja 'Japanese' + nl 'Dutch' + no 'Norwegian' + pt 'Portuguese' + pl 'Polish' + ro 'Romanian' + ru 'Russian' + sk 'Slovenian' + sv 'Swedish' + tr 'Turkish' + uk 'Ukrainian' +) + +LOOKUP_guard -fd LOOKUP_help_${backend} || +function LOOKUP_help_${backend}() { + LOOKUP_guard || return 1 + local l + printf 'usage: %s [-l ] \n' ${backend} + printf ' -l use a specific non-default wikipedia location\n' + printf '\n Query wikipedia.org about stuff you don'\''t know. :-)\n' + printf '\nKnown location codes:\n' + for l in ${(kon)known_locations}; do + printf '%6s - %s\n' $l ${known_locations[$l]} + done + printf '\n There are more than that, obviously. Inclusion of codes had to stop\n' + printf ' somewhere, so I settled for codes, that had 100.000+ articles at the time\n' + printf ' of writing. The default location is '\''en'\'', which can be modified via the\n' + printf ' '\''default-location'\'' style in this context:\n' + printf ' %s\n' ${lookup_context} + printf '\n The completion code only completes the known locations by default. If your\n' + printf ' code is missing you can add it via the '\''my-completions'\'' list style,\n' + printf ' looked up in this context: '\'':completion:*:lookup-%s:'\''\n' ${backend} + printf '\nExamples:\n' + printf ' %% zstyle '\'':lookup:*:%s:*'\'' default-location de\n' ${backend} + printf ' %% zstyle '\'':completion:*:lookup-%s:'\'' my-completions gr:Greek\n' ${backend} + printf ' %% lookup %s zsh\n' ${backend} + printf ' %% lookup %s -l de zsh\n' ${backend} + printf ' %% lookup %s -l no zsh\n\n' ${backend} +} +LOOKUP_help && return 0 + +if [[ -n ${lookup_complete} ]] ; then + + zstyle -a ":completion:${curcontext}" my-completions private_completions + + LOOKUP_guard -fd __lookup_${backend}_known_locations || + function __lookup_${backend}_known_locations() { + local l + local -a ls + + ls=(${private_completions}) + for l in ${(k)known_locations}; do + ls+=("$l:${known_locations[$l]}") + done + _describe -t leo_langs 'known locations' ls + } + + comp_args=( + '-l[choose a wikipedia flavour]:known locations:__lookup_'${backend}'_known_locations' + '*:wikipedia search:true' + ) + + _arguments -s -w -A '-*' ${comp_args} && return 0 + _message 'wikipedia search' + return 0 +fi + +local loc +local -A opts +local -x QUERY + +zstyle -s "${lookup_context}" default-location loc || loc='en' + +lu_parseopts_args=( l string ) +LOOKUP_parseopts "$@" || return 1 +[[ -n ${opts[-l]} ]] && loc="${opts[-l]}" + +QUERY="${args[*]}" +LOOKUP_query_handler || return 1 +if [[ -z ${QUERY} ]] ; then + LOOKUP_help -f + return 1 +fi + +LOOKUP_encode -q +LOOKUP_browser "http://${loc}.wikipedia.org/w/wiki.phtml?search=${QUERY}&go=Go" +return $? diff --git a/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_zsh_mla b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_zsh_mla new file mode 100644 index 0000000..09595e5 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/Backends/LOOKUP_be_zsh_mla @@ -0,0 +1,101 @@ +### vim:ft=zsh:foldmethod=marker +## zsh mla search backend for lookup +## Copyright: 2009, Frank Terbeck + +LOOKUP_guard || return 1 +[[ -n ${lookup_describe} ]] && + printf '%s' 'search interface for zsh'\''s ML archive' && + return 0 + +local lookup_context +local -a comp_args + +lookup_context="$(LOOKUP_context)" + +LOOKUP_guard -fd LOOKUP_help_${backend} || +function LOOKUP_help_${backend}() { + LOOKUP_guard || return 1 + printf 'usage: %s [-{n,u,w}] [-{l,y} ] \n' ${backend} + printf ' -n search the archives for keywords ('\''normal'\'' mode)\n' + printf ' -u search user archive by X-Seq: header\n' + printf ' -w search workers archive by X-Seq: header\n' + printf ' -l limit search to a specific list\n' + printf ' -y limit search to a specific year\n' + printf '\n Query zsh[1]'\''s Mailing List Arhive[2].' + printf '\n The normal keyword search is done via google'\''s '\''site:'\'' feature.\n' + printf ' In X-Seq modes (-u and -w), all non-digits are stripped from query.\n' + printf '\n The default mode is '\''normal'\'', which can be modified via the\n' + printf ' '\''default-mode'\'' style in this context: %s\n' ${lookup_context} + printf ' Valid values are: normal, users, workers\n' + printf '\nExamples:\n' + printf ' %% zstyle '\'':lookup:*:%s:*'\'' default-mode users\n' ${backend} + printf ' %% lookup %s released\n' ${backend} + printf ' %% lookup %s -u 12345\n' ${backend} + printf ' %% lookup %s -w 12345\n' ${backend} + printf '\n[1] \n' + printf '[2] \n' +} +LOOKUP_help && return 0 + +if [[ -n ${lookup_complete} ]] ; then + comp_args=( + '-u[users x-seq search]:users\: X-Seq header number:' + '-w[workers x-seq search]:workers\: X-Seq header number:' + '-l[limit search to a specific list]:list name:(users workers "*")' + '-y[limit search to a specific year]:year - "*" means all years:' + '*:zsh mla search:true' + ) + + _arguments -s -w -A '-*' ${comp_args} && return 0 + _message 'zsh mla search' + return 0 +fi + +local list year mode +local -A opts +local -x QUERY + +list='*' +year='*' +lu_parseopts_args=( + n bool + u bool + w bool + l string + y string +) +LOOKUP_parseopts "$@" || return 1 +[[ ${opts[-u]} == 'yes' ]] && mode='users' +[[ ${opts[-w]} == 'yes' ]] && mode='workers' +[[ ${opts[-n]} == 'yes' ]] && mode='normal' +[[ -n ${opts[-l]} ]] && list="${opts[-l]}" +[[ -n ${opts[-y]} ]] && year="${opts[-y]}" + +if [[ -z ${mode} ]] ; then + zstyle -s "${lookup_context}" default-mode mode || mode='normal' +fi + +QUERY="${args[*]}" +LOOKUP_query_handler || return 1 +if [[ -z ${QUERY} ]] ; then + LOOKUP_help -f + return 1 +fi + +case ${mode} in +(users|workers) + QUERY="${QUERY//[^0-9]/}" + if [[ ${mode} == 'users' ]] ; then + LOOKUP_browser "http://www.zsh.org/cgi-bin/mla/redirect?USERNUMBER=${QUERY}" + return $? + else + LOOKUP_browser "http://www.zsh.org/cgi-bin/mla/redirect?WORKERNUMBER=${QUERY}" + return $? + fi + ;; +(*) + LOOKUP_encode -s -q + LOOKUP_browser "http://www.google.com/search?q=site:www.zsh.org/mla/${list}/${year}/+${QUERY}" + return $? + ;; +esac diff --git a/usr_share_grml/zsh/functions/Lookup/LOOKUP_browser b/usr_share_grml/zsh/functions/Lookup/LOOKUP_browser new file mode 100644 index 0000000..9b2fc35 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/LOOKUP_browser @@ -0,0 +1,46 @@ +### vim:ft=zsh:foldmethod=marker +## +## Spawn an appropriate browser. +## Copyright: 2009, Frank Terbeck +## + +LOOKUP_guard || return 1 +local arg debug format lookup_browser uri i lookup_context +local -a lookup_browser_args + +lookup_ei='-browser-' +uri="$1" + +lookup_context="$(LOOKUP_context)" + +LOOKUP_hook -- "$@" +zstyle -t "${lookup_context}" debug && + printf 'LOOKUP_browser: current context: "%s"\n' "${lookup_context}" + +if (( lookup_printout > 0 )) || zstyle -t "${lookup_context}" debug ; then + debug='yes' +fi + +if (( lookup_remote > 0 )) ; then + zstyle -s "${lookup_context}" gui-browser lookup_browser + zstyle -a "${lookup_context}" gui-formats lookup_browser_args +else + zstyle -s "${lookup_context}" txt-browser lookup_browser + zstyle -a "${lookup_context}" txt-formats lookup_browser_args +fi + +[[ -z ${lookup_browser} ]] && lookup_browser=${BROWSER:-w3m} +(( ${#lookup_browser_args} == 0 )) && lookup_browser_args=( "%s" ) + +for i in {1..${#lookup_browser_args}} ; do + zformat -f format ${lookup_browser_args[$i]} s:${uri} + lookup_browser_args[$i]="${format}" +done + +if [[ -n ${debug} ]] ; then + print -- ${lookup_browser} ${(qq)lookup_browser_args} + return 0 +else + ${lookup_browser} ${lookup_browser_args} + return $? +fi diff --git a/usr_share_grml/zsh/functions/Lookup/LOOKUP_context b/usr_share_grml/zsh/functions/Lookup/LOOKUP_context new file mode 100644 index 0000000..60f72be --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/LOOKUP_context @@ -0,0 +1,22 @@ +### vim:ft=zsh:foldmethod=marker +## get and set the current context +## Copyright: 2009, Frank Terbeck + +# If called without arguments, it spits out the current context. +# If called with -l it sets the of the context +# to . If called with -d, it sets the to its default. + +LOOKUP_guard || return 1 +local opt +local -A opts + +lu_parseopts_args=( d bool l string ) +LOOKUP_parseopts "$@" || return 1 +if [[ ${opts[-d]} == 'yes' ]] ; then + lookup_lp="-default-" +elif [[ -n ${opts[-l]} ]] ; then + lookup_lp="${opts[-l]}" +else + printf ':lookup:%s:%s:%s' ${lookup_ei} ${backend} ${lookup_lp} +fi +return 0 diff --git a/usr_share_grml/zsh/functions/Lookup/LOOKUP_encode b/usr_share_grml/zsh/functions/Lookup/LOOKUP_encode new file mode 100644 index 0000000..713e9e2 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/LOOKUP_encode @@ -0,0 +1,36 @@ +### vim:ft=zsh:foldmethod=marker +## url encoding for lookup queries, etc. + +LOOKUP_guard || return 1 +emulate -L zsh +setopt extendedglob +local input output ws=$'\t ' +local -A opts + +lu_parseopts_args=( s bool q bool ) +LOOKUP_parseopts "$@" || return 1 +if [[ ${opts[-q]} == 'yes' ]] ; then + # -q: QUERY is declared 'local -x QUERY' in backends. + # That means, it gets handed down to us. Apply our filters to it. + unencQUERY="${QUERY}" + set -- "${QUERY}" +else + set -- "${args[@]}" +fi + +input=( ${(s::)1} ) +output=${(j::)input/(#b)([^A-Za-z0-9_${ws}.!~*\'\(\)+-])/%${(l:2::0:)$(([##16]#match))}} + +if [[ ${opts[-s]} == 'yes' ]] ; then + output=${${output##[$ws]#}%%[$ws]#} + output="${output//+/%2B}" + output=${output//[$ws]##/+} +else + output="${output//[$ws]/%20}" +fi + +if [[ ${opts[-q]} == 'yes' ]] ; then + QUERY=${output} +else + printf '%s' ${output} +fi diff --git a/usr_share_grml/zsh/functions/Lookup/LOOKUP_guard b/usr_share_grml/zsh/functions/Lookup/LOOKUP_guard new file mode 100644 index 0000000..230a05a --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/LOOKUP_guard @@ -0,0 +1,52 @@ +### vim:ft=zsh:foldmethod=marker +## Copyright: 2009, Frank Terbeck + +### Usage: +## Check if we're called from the lookup subsystem. +# This way you can guard your functions from being called by hand +# by the user. Every function in lookup should do this as soon as +# possible. +# +# LOOKUP_guard || return 1 +# +## Check if a function is defined already. +# This is used to avoid nested functions to get defined over and +# over again. Everytime you introduce a function in a backend, you +# should use this idom. No matter if it's a helper function +# (LOOKU_beh_${backend}_my_helper) or the function, that prints +# the backend's documentation (LOOKUP_help_${backend}). +# +# LOOKUP_guard -fd LOOKUP_help_${backend} || +# function LOOKUP_help_${backend}() { +# LOOKUP_guard || return 1 +# ... +# } + +local -i fail +fail=1 + +if [[ $1 == '-fd' ]] ; then + # This function cannot use LOOKUP_parseopts() because that function + # already uses LOOKUP_guard(). That would be an endless loop. + # Since this function does not alter any 'local -x' variables, it + # should be the one that doesn't require any other lookup() function. + shift + (( ${+functions[$1]} )) && return 0 + return 1 +fi + +(( lookup_complete > 0 )) && fail=0 +[[ -n ${backend} ]] && [[ -n ${lookup_ei} ]] && [[ -n ${lookup_help} ]] && fail=0 + +if (( fail > 0 )) ; then + printf '\nThis function is part of the lookup subsystem.\n' + printf 'It is not supposed to be run by you, the user directly.\n' + printf 'Use the appropriate backend via: lookup \n' + printf 'See:\n' + printf ' lookup -h\n' + printf ' and\n' + printf ' lookup -h \n' + printf 'for details.\n\n' + return 1 +fi +return 0 diff --git a/usr_share_grml/zsh/functions/Lookup/LOOKUP_help b/usr_share_grml/zsh/functions/Lookup/LOOKUP_help new file mode 100644 index 0000000..6ae0174 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/LOOKUP_help @@ -0,0 +1,48 @@ +### vim:ft=zsh:foldmethod=marker +## Copyright: 2009, Frank Terbeck + +LOOKUP_guard || return 1 + +if [[ $1 != '-f' ]] ; then + (( lookup_help == 0 )) && return 1 +else + shift +fi + +local line use_pager pager_auto pager +local -i prompt_height +local -a lines + +zstyle -t "${lookup_context}" use-pager && use_pager='yes' || use_pager='no' +zstyle -t "${lookup_context}" pager-auto && pager_auto='yes' || pager_auto='no' +zstyle -s "${lookup_context}" pager pager || pager=${PAGER:-more} +zstyle -s "${lookup_context}" prompt-height prompt_height || prompt_height=1 +(( prompt_height == 0 )) && prompt_height=1 + +if [[ ${LINES} != [0-9]## ]] || [[ ${LINES} -eq 0 ]] ; then + # $LINES is either 0, empty or other rubbish that's not a positive integer. + pager_auto='no' +fi + +if [[ ${use_pager} == 'no' ]] ; then + LOOKUP_help_${backend} + return 0 +fi + +if [[ ${use_pager} == 'yes' ]] && [[ ${pager_auto} == 'no' ]] ; then + LOOKUP_help_${backend} | ${=pager} + return 0 +fi + +# use-pager: true, pager-auto: true +lines=() +LOOKUP_help_${backend} | while IFS='' read -r line; do + lines+=( "${line}" ) +done + +if (( ${#lines} > LINES - prompt_height )); then + print -l -- "${lines[@]}" | ${=pager} +else + print -l -- "${lines[@]}" +fi +return 0 diff --git a/usr_share_grml/zsh/functions/Lookup/LOOKUP_hook b/usr_share_grml/zsh/functions/Lookup/LOOKUP_hook new file mode 100644 index 0000000..b8d037a --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/LOOKUP_hook @@ -0,0 +1,37 @@ +### vim:ft=zsh:foldmethod=marker +## run hooks +## Copyright: 2009, Frank Terbeck + +LOOKUP_guard || return 1 +local hook obackend old_lookup_ei ret lookup_context +local -a hooks + +lookup_context="$(LOOKUP_context)" + +obackend="${backend}" +if [[ $1 != '--' ]] ; then + backend="$1" +fi +shift + +zstyle -t "${lookup_context}" debug && + printf 'LOOKUP_hook: current context: "%s"\n' "${lookup_context}" + +zstyle -a "${lookup_context}" hooks hooks || return 0 + +old_lookup_ei=${lookup_ei} +lookup_ei='-hook-' +for hook in ${hooks} ; do + (( ${+functions[$hook]} == 0 )) && continue + ${hook} "$@" + case $? in + (1) + break + ;; + (0|*) + ;; + esac +done +lookup_ei="${old_lookup_ei}" +backend="${obackend}" +return 0 diff --git a/usr_share_grml/zsh/functions/Lookup/LOOKUP_parseopts b/usr_share_grml/zsh/functions/Lookup/LOOKUP_parseopts new file mode 100644 index 0000000..f080e30 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/LOOKUP_parseopts @@ -0,0 +1,101 @@ +### vim:ft=zsh:foldmethod=marker +## parse cmdline options +## Copyright: 2009, Frank Terbeck + +# This is lookup's option parsing slave. +# To use it, you'll have to follow the following idiom: +# lu_parseopts_args=( +# l bool +# h bool +# x string +# i int +# ) +# LOOKUP_parseopts "$@" || return 1 +# +# ...the result of LOOKUP_parseopts is now stored in $opts[] and $args[]. +# $args[] is an array containing the remaining args from "$@" after parsing. +# $opts[] is an associative array containing key value pairs in the following +# form: +# -x "a string" +# -i 12345 +# For boolean args an entry is only added if the was given and will look +# like this: +# -l "yes" +# -h "yes" +# +# If you define integer arguments in $lu_parseopts_args[], LOOKUP_parseopts() +# will check its value and return with an error if the argument is not a +# signed integer. +# Hence, in the above example the following calls are valid: +# foo -i 123 +# foo -i -123 +# foo -i +123 +# while this is not: +# foo -i bar + +LOOKUP_guard || return 1 + +local char o dashdash firstarg who +local -a largs match + +if [[ ${lookup_ei} == '-backend-' ]] ; then + who="${backend}" +else + who="lookup" +fi + +if (( ${#lu_parseopts_args} == 0 )) ; then + printf '%s: $lu_parseopts_args is emtpy.\n' ${who} + printf '%s: Read the example on top of the LOOKUP_parseopts file.\n' ${who} + return 1 +fi + +args=() +opts=() +for o in ${(k)lu_parseopts_args} ; do + case ${lu_parseopts_args[$o]} in + (bool) largs+=($o) ;; + (string|int) largs+=($o:) ;; + (*) + printf '%s: Unknown option type (%s: %s)!\n' ${who} $o ${lu_parseopts_args[$o]} + return 1 + ;; + esac +done + +for o in "$@" ; do + case $o in + (-|--) + dashdash='found' + continue + ;; + esac + if [[ ${dashdash} == 'found' ]] ; then + firstarg="$o" + break + fi +done +zparseopts -A opts -D "${largs[@]}" || return 1 +if [[ $1 == -* ]] && [[ $1 != ${firstarg} ]] ; then + printf '%s: Unknown option: %s\n' ${who} "$1" + return 1 +fi +for o in ${(k)opts} ; do + match=() + : ${o/(#b)-(*)/} + char="${match[1]}" + case ${lu_parseopts_args[$char]} in + (bool) + [[ -z ${opts[$o]} ]] && opts[$o]='yes' + ;; + (int) + opts[$o]=${${opts[$o]}/(#s)+/} + if [[ ${${opts[$o]}/(#s)-/} == (-|)*[^0-9]* ]]; then + printf '%s: %s requires an integer argument.\n' ${who} $o + return 1 + fi + ;; + esac +done + +args=( "$@" ) diff --git a/usr_share_grml/zsh/functions/Lookup/LOOKUP_query_handler b/usr_share_grml/zsh/functions/Lookup/LOOKUP_query_handler new file mode 100644 index 0000000..07afc55 --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/LOOKUP_query_handler @@ -0,0 +1,52 @@ +### vim:ft=zsh:foldmethod=marker +## run query handlers +## Copyright: 2009, Frank Terbeck + +LOOKUP_guard || return 1 +local old_lookup_ei qh qh_return ret +local -a qhs + +zstyle -t "${lookup_context}" debug && + printf 'LOOKUP_query_handler: current context: "%s"\n' "${lookup_context}" + +(( lookup_use_qh == 0 )) && return 0 +zstyle -a "${lookup_context}" query-handlers qhs || return 0 + +qh_return=0 +old_lookup_ei=${lookup_ei} +lookup_ei='-queryhandler-' +for qh in ${qhs} ; do + if (( ${+functions[LOOKUP_qh_$qh]} == 0 )) ; then + printf 'query-handler %s requires this function: %s\n' "${qa}" "LOOKUP_qh_${qh}" + printf 'Hit any key to continue...\n' + read -k 1 + else + if [[ ${lookup_qh_arg-wasunset} == 'wasunset' ]] ; then + LOOKUP_qh_${qh} + ret=$? + else + LOOKUP_qh_${qh} ${lookup_qh_arg} + ret=$? + fi + case ${ret} in + (1) # stop processing + break + ;; + (2) # clear QUERY and stop processing + QUERY='' + break + ;; + (3) # clear QUERY and stop processing and return 1 + # which means, the backends are supposed to return 1, too. + # thus, no browser will be called. + QUERY='' + qh_return=1 + break + ;; + (0|*) # just continue + ;; + esac + fi +done +lookup_ei=${old_lookup_ei} +return ${qh_return} diff --git a/usr_share_grml/zsh/functions/Lookup/lookupinit b/usr_share_grml/zsh/functions/Lookup/lookupinit new file mode 100644 index 0000000..802a9eb --- /dev/null +++ b/usr_share_grml/zsh/functions/Lookup/lookupinit @@ -0,0 +1,181 @@ +### vim:ft=zsh:foldmethod=marker +## +## zsh function that looks up an argument in various webservices. +## Copyright: 2009, Frank Terbeck +## +## This file, all LOOKUP_* files and all backends in the Backends/ subdirectory +## are distributed under the same licence as zsh itself (BSD-like). +## + +typeset -ga LOOKUP_backends +typeset -gA LOOKUP_aliases + +function lookup() { + emulate -L zsh + setopt extendedglob + setopt warncreateglobal + + local file opt + local -a alias_words backend_args + local -A lu_aliases + + local -x backend lookup_describe lookup_qh_arg lookup_lp lookup_ei unencQUERY + local -ix lookup_remote lookup_printout lookup_use_qh lookup_help + local -ax args + local -Ax lookup_communicate lu_parseopts_args opts + + backend='-init-' + lookup_ei='-main-' + lookup_help=0 + lookup_use_qh=0 + lookup_remote=0 + lookup_printout=0 + + lu_parseopts_args=( + a string + d string + q string + h bool + i bool + l bool + L bool + P bool + Q bool + R bool + ) + LOOKUP_parseopts "$@" || return 1 + + if [[ -n ${opts[-l]} ]] ; then + printf 'Available backends:\n\n' + lookup_describe="yes" + for backend in ${LOOKUP_backends} ; do + printf '%16s - %s\n' ${backend} "$(LOOKUP_be_${backend})" + done + printf '\n' + return 0 + fi + + if [[ -n ${opts[-L]} ]] ; then + if (( ${#LOOKUP_aliases} == 0 )) ; then + printf 'lookup: No aliases defined.\n' + return 0 + fi + printf 'Defined backend aliases:\n\n' + for al in ${(k)LOOKUP_aliases}; do + printf '%16s=%s\n' ${al} ${LOOKUP_aliases[$al]} + done + printf '\n' + return 0 + fi + + if [[ -n ${opts[-i]} ]] ; then + local f + local -a fcts + + LOOKUP_backends=() + fcts=( + LOOKUP_browser LOOKUP_context LOOKUP_encode LOOKUP_guard + LOOKUP_help LOOKUP_hook LOOKUP_query_handler + ) + + for file in ${^fpath}/LOOKUP_be_*~*(\~|.zwc)(N.) ; do + file=${file:t} + : ${file:#(#b)LOOKUP_be_(*)} + backend=${match[1]} + + [[ -n ${(M)LOOKUP_backends:#${backend}} ]] && continue + LOOKUP_backends+=(${backend}) + (( ${+functions[LOOKUP_be_$backend]} )) || + autoload -Uz LOOKUP_be_${backend} + done + + for f in ${fcts} ; do + (( ${+functions[$f]} )) || autoload -Uz $f + done + return 0 + fi + + if [[ -n ${opts[-a]} ]] ; then + local al val + + match=() + : ${${opts[-a]}/(#b)(*)=(*)} + al="${match[1]}" + val="${match[2]}" + if [[ -z ${al} ]] || [[ -z ${val} ]] ; then + printf 'An alias definition *must* look like this: aliasname="originalname -options"\n' + return 1 + fi + + LOOKUP_aliases[$al]="${val}" + return 0 + fi + + if [[ -n ${opts[-d]} ]] ; then + if [[ -n ${LOOKUP_aliases[${opts[-d]}]} ]] ; then + unset "LOOKUP_aliases[${opts[-d]}]" + return 0 + else + printf 'No such backend alias defined: "%s"\n' ${opts[-d]} + printf 'Use lookup -L to get a list.\n' + return 1 + fi + fi + + [[ -n ${opts[-h]} ]] && lookup_help=1 + [[ -n ${opts[-R]} ]] && lookup_remote=1 + [[ -n ${opts[-P]} ]] && lookup_printout=1 + [[ -n ${opts[-Q]} ]] && lookup_use_qh=1 + [[ -n ${opts[-q]} ]] && lookup_use_qh=1 && lookup_qh_arg=${opts[-q]} + + if (( ${#args} == 0 )) ; then + printf 'usage: lookup [-{i,a,d,Q,l,L,P,R}] [-{h,q} ] OPTION(s)...\n' + printf ' Options:\n' + printf ' -h [backend] print this text or help for '\''backend'\''\n\n' + printf ' -i (re)initialize lookup\n\n' + printf ' -a add a backend alias\n' + printf ' -d remove an alias for a backend\n\n' + printf ' -Q let a handler create the QUERY string\n' + printf ' -q same as -Q, but let'\''s you give an argument, too\n\n' + printf ' -l list available backends\n' + printf ' -L list defined backend aliases\n\n' + printf ' -P print which browser command would be used\n' + printf ' -R send url to remote browser\n' + (( ${+functions[lu]} )) && + printf '\n Instead of '\''lookup'\'' the shorter '\''lu'\'' may be used.\n' + (( lookup_help > 0 )) && return 0 || return 1 + fi + + if (( ${+LOOKUP_aliases[${args[1]}]} )) ; then + alias_words=( ${(z)${LOOKUP_aliases[${args[1]}]}} ) + shift args + backend=${alias_words[1]} + shift alias_words + backend_args=( "${alias_words[@]}" "${args[@]}" ) + else + backend=${args[1]} + shift args + backend_args=( "${args[@]}" ) + fi + + if [[ -z ${(M)LOOKUP_backends:#$backend} ]] ; then + printf 'Unknown backend '\''%s'\''.\n' ${backend} + return 1 + fi + + args=() + opts=() + LOOKUP_hook -- "${backend_args[@]}" + lookup_ei='-backend-' + LOOKUP_context -d + LOOKUP_be_${backend} "${backend_args[@]}" + return $? +} + +function lu() { + lookup "$@" +} + +# initialize the system +autoload -Uz LOOKUP_parseopts LOOKUP_guard # lookup -i needs this +lookup -i -- 2.1.4