Add zsh-lookup sub-system
[grml-etc-core.git] / usr_share_grml / zsh / functions / Lookup / LOOKUP_encode
1 ### vim:ft=zsh:foldmethod=marker
2 ## url encoding for lookup queries, etc.
3
4 LOOKUP_guard || return 1
5 emulate -L zsh
6 setopt extendedglob
7 local input output ws=$'\t '
8 local -A opts
9
10 lu_parseopts_args=( s bool q bool )
11 LOOKUP_parseopts "$@" || return 1
12 if [[ ${opts[-q]} == 'yes' ]] ; then
13     # -q: QUERY is declared 'local -x QUERY' in backends.
14     #     That means, it gets handed down to us. Apply our filters to it.
15     unencQUERY="${QUERY}"
16     set -- "${QUERY}"
17 else
18     set -- "${args[@]}"
19 fi
20
21 input=( ${(s::)1} )
22 output=${(j::)input/(#b)([^A-Za-z0-9_${ws}.!~*\'\(\)+-])/%${(l:2::0:)$(([##16]#match))}}
23
24 if [[ ${opts[-s]} == 'yes' ]] ; then
25     output=${${output##[$ws]#}%%[$ws]#}
26     output="${output//+/%2B}"
27     output=${output//[$ws]##/+}
28 else
29     output="${output//[$ws]/%20}"
30 fi
31
32 if [[ ${opts[-q]} == 'yes' ]] ; then
33     QUERY=${output}
34 else
35     printf '%s' ${output}
36 fi