Add zsh-lookup sub-system
[grml-etc-core.git] / usr_share_grml / zsh / functions / Lookup / Backends / LOOKUP_be_koders
1 ### vim:ft=zsh:foldmethod=marker
2 ## koders.com source code 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' 'source code search via koders.com' &&
8     return 0
9
10 local lookup_context
11 local -a comp_args known_langs
12 local -A known_licences
13
14 lookup_context="$(LOOKUP_context)"
15
16 known_langs=(
17 #{{{
18     "ActionScript"
19     "Ada"
20     "ASP"
21     "ASP.NET"
22     "Assembler"
23     "C"
24     "C#"
25     "C++"
26     "Cobol"
27     "ColdFusion"
28     "Delphi"
29     "Eiffel"
30     "Erlang"
31     "Fortran"
32     "Java"
33     "JavaScript"
34     "JSP"
35     "Lisp"
36     "Lua"
37     "Mathematica"
38     "Matlab"
39     "ObjectiveC"
40     "Perl"
41     "PHP"
42     "Prolog"
43     "Python"
44     "Ruby"
45     "Scheme"
46     "Smalltalk"
47     "SQL"
48     "Tcl"
49     "VB"
50     "VB.NET"
51     "*"
52 #}}}
53 )
54
55 known_licences=(
56 #{{{
57     AFL     'Academic Free Licence'
58     AGPLv3  'Affero General Public Licence v3'
59     AL20    'Apache Licence, Version 2.0'
60     ASL     'Apache Software Licence'
61     APSL    'Apple Public Source Licence'
62     BSD     'Berkeley Software Distribution Licence'
63     CPL     'Common Public Licence'
64     EPL10   'Eclipse Public Licence v1.0'
65     GTPL    'Globus Toolkit Public Licence'
66     GPL     'GNU General Public Licence v1 v2 v3'
67     LGPL    'GNU Lesser General Public Licence'
68     IBMPL   'IBM Public Licence'
69     IBMILA  'International Licence Agreement for Non-Warranted Programs'
70     IOSL    'Intel Open Source Licence'
71     MSCL    'Microsoft Community Licence'
72     MSLCL   'Microsoft Limited Community Licence'
73     MSPL    'Microsoft Permissive Licence'
74     MSLPL   'Microsoft Limited Permissive Licence'
75     MSRL    'Microsoft Reference Licence'
76     MSVSSDK 'Microsoft Visual Studio SDK Licence'
77     MITD    'MIT Derived Licence'
78     MPL10   'Mozilla Public Licence Version 1.0'
79     MPL11   'Mozilla Public Licence Version 1.1'
80     NPL10   'Netscape Public Licence, Version 1.0'
81     NPL11   'Netscape Public Licence, Version 1.1'
82     OSL     'Open Software Licence'
83     PHPL    'PHP Licence'
84     CNRIPL  'Python Licence'
85     PSFL    'Python Software Foundation Licence'
86     SL      'Sleepycat Software Product Licence'
87     SISSL   'Sun Industry Standards Source Licence'
88     SPL     'Sun Public Licence'
89     W3C     'W3C Software Licence'
90     WXWLL   'wxWindows Library Licence'
91     ZLL     'zlib/libpng Licence'
92     ZPL     'Zope Public Licence'
93     "*"     'Match all licences'
94 #}}}
95 )
96
97 LOOKUP_guard -fd LOOKUP_help_${backend} ||
98 function LOOKUP_help_${backend}() {
99     LOOKUP_guard || return 1
100     local l
101     local -i i
102     local -a ls
103
104     i=8
105     ls=(${(on)known_langs})
106
107     printf 'usage: %s <query>\n' ${backend}
108     printf '  -l <lang>     limit search to specific language\n'
109     printf '  -L <licence>  limit search to specific licence\n'
110     printf '\n Search source code via koders.com\n'
111     printf '\nKnown languages:\n'
112     while (( ${#ls} > 0 )) ; do
113         if (( ${#ls} > i )) ; then
114             print ' ' ${ls[1,$i]}
115             shift $i ls
116         else
117             print ' ' ${ls}
118             ls=()
119         fi
120     done
121     printf '\nKnown licences:\n'
122     for l in ${(kon)known_licences} ; do
123         printf '%8s - %s\n' $l ${known_licences[$l]}
124     done
125     printf '\n Per default this backend makes unlimited searches.\n'
126     printf ' Results include code from all languages in all licences.\n'
127     printf '\n You may specify a default language and a default licence via\n'
128     printf ' the default-lang and default-licence styles (both default to '\''*'\''\n'
129     printf ' which means unlimited) in this context: %s\n' ${lookup_context}
130     printf '\nExamples:\n'
131     printf ' %% zstyle '\'':lookup:*:%s:*'\'' default-lang    C\n' ${backend}
132     printf ' %% zstyle '\'':lookup:*:%s:*'\'' default-licence BSD\n\n' ${backend}
133     printf ' %% lookup %s strlcpy\n' ${backend}
134     printf ' %% lookup %s -l C handle_keys\n' ${backend}
135     printf ' %% lookup %s -L GPL -l C strlcpy\n\n' ${backend}
136 }
137 LOOKUP_help && return 0
138
139 if [[ -n ${lookup_complete} ]] ; then
140
141     LOOKUP_guard -fd __lookup_${backend}_langs ||
142     function __lookup_${backend}_langs() {
143         _describe -t koders_langs 'known languages' known_langs
144     }
145
146     LOOKUP_guard -fd __lookup_${backend}_licences ||
147     function __lookup_${backend}_licences() {
148         local l
149         local -a ls
150
151         ls=()
152         for l in ${(k)known_licences}; do
153             ls+=("$l:${known_licences[$l]}")
154         done
155         _describe -t koders_licences 'known licences' ls
156     }
157
158     comp_args=(
159         '-l[limit search to specific language]:language:__lookup_'${backend}'_langs'
160         '-L[limit search to specific licence]:licence:__lookup_'${backend}'_licences'
161         '*:source code search string:true'
162     )
163
164     _arguments -s -w -A '-*' ${comp_args} && return 0
165     _message 'source code search string'
166     return 0
167 fi
168
169 local lang licence
170 local -x QUERY
171
172 zstyle -s "${lookup_context}" default-lang    lang    || lang='*'
173 zstyle -s "${lookup_context}" default-licence licence || licence='*'
174
175 lu_parseopts_args=( l string L string )
176 LOOKUP_parseopts "$@" || return 1
177 [[ -n ${opts[-l]} ]] && lang="${opts[-l]}"
178 [[ -n ${opts[-L]} ]] && licence="${opts[-L]}"
179
180 QUERY="${args[*]}"
181 LOOKUP_query_handler || return 1
182 if [[ -z ${QUERY} ]] ; then
183     LOOKUP_help -f
184     return 1
185 fi
186
187 LOOKUP_encode -s -q
188 LOOKUP_browser "http://koders.com/default.aspx?s=${QUERY}&la=${lang}&li=${licence}"
189 return $?