Add zsh-lookup sub-system
[grml-etc-core.git] / usr_share_grml / zsh / functions / Lookup / Backends / LOOKUP_be_wikipedia
1 ### vim:ft=zsh:foldmethod=marker
2 ## wikipedia 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 wikipedia' &&
8     return 0
9
10 local lookup_context
11 local -a comp_args private_completions
12 local -A known_locations
13
14 lookup_context="$(LOOKUP_context)"
15
16 known_locations=(
17     # yeah, there are more... a lot more.
18     ca      'Catalan'
19     cs      'Czech'
20     da      'Danish'
21     de      'German'
22     en      'English'
23     eo      'Esperanto'
24     es      'Spanish'
25     'fi'    'Finnish'
26     fr      'French'
27     hu      'Hungarian'
28     it      'Italian'
29     ja      'Japanese'
30     nl      'Dutch'
31     no      'Norwegian'
32     pt      'Portuguese'
33     pl      'Polish'
34     ro      'Romanian'
35     ru      'Russian'
36     sk      'Slovenian'
37     sv      'Swedish'
38     tr      'Turkish'
39     uk      'Ukrainian'
40 )
41
42 LOOKUP_guard -fd LOOKUP_help_${backend} ||
43 function LOOKUP_help_${backend}() {
44     LOOKUP_guard || return 1
45     local l
46     printf 'usage: %s [-l <arg>] <query>\n' ${backend}
47     printf '  -l <location>     use a specific non-default wikipedia location\n'
48     printf '\n Query wikipedia.org about stuff you don'\''t know. :-)\n'
49     printf '\nKnown location codes:\n'
50     for l in ${(kon)known_locations}; do
51         printf '%6s - %s\n' $l ${known_locations[$l]}
52     done
53     printf '\n There are more than that, obviously. Inclusion of codes had to stop\n'
54     printf ' somewhere, so I settled for codes, that had 100.000+ articles at the time\n'
55     printf ' of writing. The default location is '\''en'\'', which can be modified via the\n'
56     printf ' '\''default-location'\'' style in this context:\n'
57     printf '        %s\n' ${lookup_context}
58     printf '\n The completion code only completes the known locations by default. If your\n'
59     printf ' code is missing you can add it via the '\''my-completions'\'' list style,\n'
60     printf ' looked up in this context: '\'':completion:*:lookup-%s:'\''\n' ${backend}
61     printf '\nExamples:\n'
62     printf ' %% zstyle '\'':lookup:*:%s:*'\''           default-location de\n' ${backend}
63     printf ' %% zstyle '\'':completion:*:lookup-%s:'\'' my-completions   gr:Greek\n' ${backend}
64     printf ' %% lookup %s zsh\n' ${backend}
65     printf ' %% lookup %s -l de zsh\n' ${backend}
66     printf ' %% lookup %s -l no zsh\n\n' ${backend}
67 }
68 LOOKUP_help && return 0
69
70 if [[ -n ${lookup_complete} ]] ; then
71
72     zstyle -a ":completion:${curcontext}" my-completions private_completions
73
74     LOOKUP_guard -fd __lookup_${backend}_known_locations ||
75     function __lookup_${backend}_known_locations() {
76         local l
77         local -a ls
78
79         ls=(${private_completions})
80         for l in ${(k)known_locations}; do
81             ls+=("$l:${known_locations[$l]}")
82         done
83         _describe -t leo_langs 'known locations' ls
84     }
85
86     comp_args=(
87         '-l[choose a wikipedia flavour]:known locations:__lookup_'${backend}'_known_locations'
88         '*:wikipedia search:true'
89     )
90
91     _arguments -s -w -A '-*' ${comp_args} && return 0
92     _message 'wikipedia search'
93     return 0
94 fi
95
96 local loc
97 local -A opts
98 local -x QUERY
99
100 zstyle -s "${lookup_context}" default-location loc || loc='en'
101
102 lu_parseopts_args=( l string )
103 LOOKUP_parseopts "$@" || return 1
104 [[ -n ${opts[-l]} ]] && loc="${opts[-l]}"
105
106 QUERY="${args[*]}"
107 LOOKUP_query_handler || return 1
108 if [[ -z ${QUERY} ]] ; then
109     LOOKUP_help -f
110     return 1
111 fi
112
113 LOOKUP_encode -q
114 LOOKUP_browser "http://${loc}.wikipedia.org/w/wiki.phtml?search=${QUERY}&go=Go"
115 return $?