Update hl and _hl_complete to work with current ‘highlight’ versions
[grml-etc-core.git] / usr_share_grml / zsh / functions / hl
1 # -*- shell-script -*-
2
3 # Taken from:
4 # https://dev.0x50.de/projects/ftzsh/repository/revisions/master/entry/functions/hl
5
6 if ! [[ -x ${commands[highlight]} ]]; then
7     printf 'hl: Could not find `highlight'\'' binary!\n'
8     return 1
9 fi
10
11 local context file format lang suffix syntax theme o
12 local -i nopager=0
13 local -a pager hl_opts
14 local -A opt syntaxmap
15
16 while [[ $1 == -* ]]; do
17     case $1 in
18     (-c|--cat|--no-pager)
19         nopager=1
20         shift
21         ;;
22     (-F|--format)
23         opt[format]=$2
24         shift
25         shift
26         ;;
27     (-h|--help)
28         printf 'usage: hl [OPTION(s)] <file(s)>\n'
29         return 0
30         ;;
31     (-l|--list)
32         (   printf 'available languages (syntax parameter):\n\n' ;
33             highlight --list-langs ; ) | less -SM
34         return 0
35         ;;
36     (-P|--pager)
37         opt[pager]=$2
38         shift
39         shift
40         ;;
41     (-s|--syntax)
42         opt[syntax]=$2
43         shift
44         shift
45         ;;
46     (-t|--themes)
47         (   printf 'available themes (style parameter):\n\n' ;
48             highlight --list-themes ; ) | less -SM
49         return 0
50         ;;
51     (-T|--theme)
52         opt[theme]=$2
53         shift
54         shift
55         ;;
56     (*)
57         printf 'hl: Unknown option `%s'\''!\n' $1
58         printf 'usage: hl [OPTION(s)] <file(s)>\n'
59         return 1
60         ;;
61     esac
62 done
63
64 if (( ${#argv} < 1 )) ; then
65     printf 'usage: hl [OPTION(s)] <file(s)>\n'
66     printf '    available options: --list (-l), --themes (-t), --help (-h)\n'
67     printf '                       --no-pager (--cat, -c), --syntax (-s)\n'
68     (( ${#argv} > 2 )) && printf '  Too many arguments.\n'
69     return 1
70 fi
71
72 syntaxmap=(
73     scm lisp
74 )
75
76 for file in "$@"; do
77     suffix=${file:e}
78     context=":functions:hl:$OSTYPE:$TERM:$suffix"
79     hl_opts=()
80     syntax=''
81
82     if (( ${+opt[format]} )); then
83         format=${opt[format]}
84     else
85         if ! zstyle -a $context format format; then
86             case $TERM in
87             ((screen|xterm)-256color)
88                 format=xterm256
89                 ;;
90             (*)
91                 format=ansi
92                 ;;
93             esac
94         fi
95     fi
96
97     if (( ${+opt[theme]} )); then
98         theme=${opt[theme]}
99     else
100         zstyle -s $context theme theme || theme=solarized-dark
101     fi
102
103     if (( nopager )); then
104         pager=cat
105     elif (( ${+opt[pager]} )); then
106         pager=${(z)opt[pager]}
107     else
108         zstyle -a $context pager pager || pager=( less -Mr )
109     fi
110
111     if (( ${+opt[syntax]} )); then
112         syntax=${opt[syntax]}
113     else
114         if ! zstyle -s $context syntax syntax; then
115             (( ${+syntaxmap[$suffix]} )) && syntax=${syntaxmap[$suffix]}
116         fi
117     fi
118     [[ -n $syntax ]] && hl_opts=( --syntax=$syntax )
119
120     highlight --out-format=$format "${hl_opts[@]}" --style=$theme $file \
121         | "${pager[@]}"
122 done
123
124 return 0