Add zsh-lookup sub-system
[grml-etc-core.git] / usr_share_grml / zsh / functions / Lookup / Backends / LOOKUP_be_google
1 ### vim:ft=zsh:foldmethod=marker
2 ## google search backend for lookup
3 ## Copyright: 2009, Frank Terbeck <ft@bewatermyfriend.org>
4
5 LOOKUP_guard || return 1
6 [[ -n ${lookup_describe} ]] && printf '%s' 'google websearch' && return 0
7
8 if [[ -n ${lookup_complete} ]] ; then
9     local -a comp_args
10     comp_args=(
11         '-1[I'\''m Feeling Lucky!]'
12         '*:dict.leo.org query:true'
13     )
14
15     _arguments -s -w -A '-*' ${comp_args} && return 0
16     _message 'google query'
17     return 0
18 fi
19
20 local mode lookup_context
21 local -x QUERY
22
23 lookup_context="$(LOOKUP_context)"
24
25 LOOKUP_guard -fd LOOKUP_help_${backend} ||
26 function LOOKUP_help_${backend}() {
27     LOOKUP_guard || return 1
28     printf 'usage: %s [-1] <query>\n' ${backend}
29     printf '  -1    Go directly to the first hit using "I'\''m Feeling Lucky"\n'
30     printf '\n Make web searches via google.com\n'
31     printf ' If you always want to use the -1 option, you may set the use-first-hit\n'
32     printf ' style in this context: %s\n' ${lookup_context}
33     printf '\nExamples:\n'
34     printf ' %% lookup %s zsh\n' ${backend}
35     printf ' %% lookup %s -1 openbsd\n' ${backend}
36     printf ' %% zstyle '\'':lookup:*:%s:*'\'' use-first-hit true\n\n' ${backend}
37 }
38 LOOKUP_help && return 0
39
40 lu_parseopts_args=( 1 bool )
41 LOOKUP_parseopts "$@" || return 1
42
43 if [[ ${opts[-1]} == 'yes' ]] ||
44    zstyle -t "${lookup_context}" use-first-hit ; then
45
46     mode='I'\''m Feeling Lucky'
47     mode="$(LOOKUP_encode ${mode})"
48     mode="&btnI=${mode}"
49 else
50     mode=''
51 fi
52
53 QUERY="${args[*]}"
54 LOOKUP_query_handler || return 1
55 if [[ -z ${QUERY} ]] ; then
56     LOOKUP_help -f
57     return 1
58 fi
59
60 LOOKUP_encode -s -q
61 LOOKUP_browser "http://www.google.com/search?q=${QUERY}${mode}"
62 return $?