Add zsh-lookup sub-system
[grml-etc-core.git] / usr_share_grml / zsh / functions / Lookup / Backends / LOOKUP_be_zsh_mla
1 ### vim:ft=zsh:foldmethod=marker
2 ## zsh mla search backend for lookup
3 ## Copyright: 2009, Frank Terbeck <ft@bewatermyfriend.org>
4
5 LOOKUP_guard || return 1
6 [[ -n ${lookup_describe} ]] &&
7     printf '%s' 'search interface for zsh'\''s ML archive' &&
8     return 0
9
10 local lookup_context
11 local -a comp_args
12
13 lookup_context="$(LOOKUP_context)"
14
15 LOOKUP_guard -fd LOOKUP_help_${backend} ||
16 function LOOKUP_help_${backend}() {
17     LOOKUP_guard || return 1
18     printf 'usage: %s [-{n,u,w}] [-{l,y} <arg>] <query>\n' ${backend}
19     printf '  -n            search the archives for keywords ('\''normal'\'' mode)\n'
20     printf '  -u            search user archive by X-Seq: header\n'
21     printf '  -w            search workers archive by X-Seq: header\n'
22     printf '  -l <list>     limit search to a specific list\n'
23     printf '  -y <year>     limit search to a specific year\n'
24     printf '\n Query zsh[1]'\''s Mailing List Arhive[2].'
25     printf '\n The normal keyword search is done via google'\''s '\''site:'\'' feature.\n'
26     printf ' In X-Seq modes (-u and -w), all non-digits are stripped from query.\n'
27     printf '\n The default mode is '\''normal'\'', which can be modified via the\n'
28     printf ' '\''default-mode'\'' style in this context: %s\n' ${lookup_context}
29     printf ' Valid values are: normal, users, workers\n'
30     printf '\nExamples:\n'
31     printf ' %% zstyle '\'':lookup:*:%s:*'\'' default-mode users\n' ${backend}
32     printf ' %% lookup %s released\n' ${backend}
33     printf ' %% lookup %s -u 12345\n' ${backend}
34     printf ' %% lookup %s -w 12345\n' ${backend}
35     printf '\n[1] <http://zsh.sourceforge.net>\n'
36     printf '[2] <http://www.zsh.org/mla/>\n'
37 }
38 LOOKUP_help && return 0
39
40 if [[ -n ${lookup_complete} ]] ; then
41     comp_args=(
42         '-u[users x-seq search]:users\: X-Seq header number:'
43         '-w[workers x-seq search]:workers\: X-Seq header number:'
44         '-l[limit search to a specific list]:list name:(users workers "*")'
45         '-y[limit search to a specific year]:year - "*" means all years:'
46         '*:zsh mla search:true'
47     )
48
49     _arguments -s -w -A '-*' ${comp_args} && return 0
50     _message 'zsh mla search'
51     return 0
52 fi
53
54 local list year mode
55 local -A opts
56 local -x QUERY
57
58 list='*'
59 year='*'
60 lu_parseopts_args=(
61     n   bool
62     u   bool
63     w   bool
64     l   string
65     y   string
66 )
67 LOOKUP_parseopts "$@" || return 1
68 [[ ${opts[-u]} == 'yes' ]] && mode='users'
69 [[ ${opts[-w]} == 'yes' ]] && mode='workers'
70 [[ ${opts[-n]} == 'yes' ]] && mode='normal'
71 [[ -n ${opts[-l]} ]] && list="${opts[-l]}"
72 [[ -n ${opts[-y]} ]] && year="${opts[-y]}"
73
74 if [[ -z ${mode} ]] ; then
75     zstyle -s "${lookup_context}" default-mode mode || mode='normal'
76 fi
77
78 QUERY="${args[*]}"
79 LOOKUP_query_handler || return 1
80 if [[ -z ${QUERY} ]] ; then
81     LOOKUP_help -f
82     return 1
83 fi
84
85 case ${mode} in
86 (users|workers)
87     QUERY="${QUERY//[^0-9]/}"
88     if [[ ${mode} == 'users' ]] ; then
89         LOOKUP_browser "http://www.zsh.org/cgi-bin/mla/redirect?USERNUMBER=${QUERY}"
90         return $?
91     else
92         LOOKUP_browser "http://www.zsh.org/cgi-bin/mla/redirect?WORKERNUMBER=${QUERY}"
93         return $?
94     fi
95     ;;
96 (*)
97     LOOKUP_encode -s -q
98     LOOKUP_browser "http://www.google.com/search?q=site:www.zsh.org/mla/${list}/${year}/+${QUERY}"
99     return $?
100     ;;
101 esac