Update hl and _hl_complete to work with current ‘highlight’ versions
[grml-etc-core.git] / usr_share_grml / zsh / functions / hl
index 258ea7e..b7a8711 100644 (file)
-# hl() highlighted less
-emulate -L zsh
-
-local theme lang
-theme=${HL_THEME:-""}
-case ${1} in
-(-l|--list)
-    ( printf 'available languages (syntax parameter):\n\n' ;
-        highlight --list-langs ; ) | less -SMr
-    ;;
-(-t|--themes)
-    ( printf 'available themes (style parameter):\n\n' ;
-        highlight --list-themes ; ) | less -SMr
-    ;;
-(-h|--help)
-    printf 'usage: hl <syntax[:theme]> <file>\n'
-    printf '    available options: --list (-l), --themes (-t), --help (-h)\n\n'
-    printf '  Example: hl c main.c\n'
-    ;;
-(*)
-    if [[ -z ${2} ]] || (( ${#argv} > 2 )) ; then
-        printf 'usage: hl <syntax[:theme]> <file>\n'
-        printf '    available options: --list (-l), --themes (-t), --help (-h)\n'
-        (( ${#argv} > 2 )) && printf '  Too many arguments.\n'
+# -*- shell-script -*-
+
+# Taken from:
+# https://dev.0x50.de/projects/ftzsh/repository/revisions/master/entry/functions/hl
+
+if ! [[ -x ${commands[highlight]} ]]; then
+    printf 'hl: Could not find `highlight'\'' binary!\n'
+    return 1
+fi
+
+local context file format lang suffix syntax theme o
+local -i nopager=0
+local -a pager hl_opts
+local -A opt syntaxmap
+
+while [[ $1 == -* ]]; do
+    case $1 in
+    (-c|--cat|--no-pager)
+        nopager=1
+        shift
+        ;;
+    (-F|--format)
+        opt[format]=$2
+        shift
+        shift
+        ;;
+    (-h|--help)
+        printf 'usage: hl [OPTION(s)] <file(s)>\n'
+        return 0
+        ;;
+    (-l|--list)
+        (   printf 'available languages (syntax parameter):\n\n' ;
+            highlight --list-langs ; ) | less -SM
+        return 0
+        ;;
+    (-P|--pager)
+        opt[pager]=$2
+        shift
+        shift
+        ;;
+    (-s|--syntax)
+        opt[syntax]=$2
+        shift
+        shift
+        ;;
+    (-t|--themes)
+        (   printf 'available themes (style parameter):\n\n' ;
+            highlight --list-themes ; ) | less -SM
+        return 0
+        ;;
+    (-T|--theme)
+        opt[theme]=$2
+        shift
+        shift
+        ;;
+    (*)
+        printf 'hl: Unknown option `%s'\''!\n' $1
+        printf 'usage: hl [OPTION(s)] <file(s)>\n'
         return 1
+        ;;
+    esac
+done
+
+if (( ${#argv} < 1 )) ; then
+    printf 'usage: hl [OPTION(s)] <file(s)>\n'
+    printf '    available options: --list (-l), --themes (-t), --help (-h)\n'
+    printf '                       --no-pager (--cat, -c), --syntax (-s)\n'
+    (( ${#argv} > 2 )) && printf '  Too many arguments.\n'
+    return 1
+fi
+
+syntaxmap=(
+    scm lisp
+)
+
+for file in "$@"; do
+    suffix=${file:e}
+    context=":functions:hl:$OSTYPE:$TERM:$suffix"
+    hl_opts=()
+    syntax=''
+
+    if (( ${+opt[format]} )); then
+        format=${opt[format]}
+    else
+        if ! zstyle -a $context format format; then
+            case $TERM in
+            ((screen|xterm)-256color)
+                format=xterm256
+                ;;
+            (*)
+                format=ansi
+                ;;
+            esac
+        fi
     fi
-    lang=${1%:*}
-    [[ ${1} == *:* ]] && [[ -n ${1#*:} ]] && theme=${1#*:}
-    if [[ -n ${theme} ]] ; then
-        highlight -O xterm256 --syntax ${lang} --style ${theme} ${2} | less -SMr
+
+    if (( ${+opt[theme]} )); then
+        theme=${opt[theme]}
     else
-        highlight -O ansi --syntax ${lang} ${2} | less -SMr
+        zstyle -s $context theme theme || theme=solarized-dark
     fi
-    ;;
-esac
+
+    if (( nopager )); then
+        pager=cat
+    elif (( ${+opt[pager]} )); then
+        pager=${(z)opt[pager]}
+    else
+        zstyle -a $context pager pager || pager=( less -Mr )
+    fi
+
+    if (( ${+opt[syntax]} )); then
+        syntax=${opt[syntax]}
+    else
+        if ! zstyle -s $context syntax syntax; then
+            (( ${+syntaxmap[$suffix]} )) && syntax=${syntaxmap[$suffix]}
+        fi
+    fi
+    [[ -n $syntax ]] && hl_opts=( --syntax=$syntax )
+
+    highlight --out-format=$format "${hl_opts[@]}" --style=$theme $file \
+        | "${pager[@]}"
+done
+
 return 0