Update hl and _hl_complete to work with current ‘highlight’ versions
authorFrank Terbeck <ft@grml.org>
Sat, 21 Dec 2013 12:35:50 +0000 (13:35 +0100)
committerFrank Terbeck <ft@grml.org>
Sat, 21 Dec 2013 12:35:50 +0000 (13:35 +0100)
usr_share_grml/zsh/completion/unix/_hl_complete
usr_share_grml/zsh/functions/hl

index 28363d4..fad3ad8 100644 (file)
@@ -1,17 +1,38 @@
 #compdef hl
 
-function _hl_genarg()  {
+# Taken from:
+# https://dev.0x50.de/projects/ftzsh/repository/revisions/master/entry/functions/_hl_complete
+
+function _hl_themes() {
     local expl
-    if [[ -prefix 1 *: ]] ; then
-        local themes
-        themes=(${${${(f)"$(LC_ALL=C highlight --list-themes)"}/ #/}:#*(Installed|Use name)*})
-        compset -P 1 '*:'
-        _wanted -C list themes expl theme compadd ${themes}
-    else
-        local langs
-        langs=(${${${(f)"$(LC_ALL=C highlight --list-langs)"}/ #/}:#*(Installed|Use name)*})
-        _wanted -C list languages expl languages compadd -S ':' -q ${langs}
-    fi
+    local -a themes
+    themes=(${${${(f)"$(LC_ALL=C highlight --list-themes)"}/ #/}:#*(Installed|Use name)*})
+    _wanted -C list themes expl theme compadd ${themes}
+}
+
+function _hl_languages() {
+    local dir expl curcontext="$curcontext"
+    local -a langs
+    zstyle -s ":completion:${curcontext}:" theme-directory dir \
+        || dir='/usr/share/highlight/langDefs/'
+    langs=( $dir/*(.:t:r) )
+    _wanted -C list languages expl languages compadd ${langs}
+}
+
+function _hl_complete () {
+    local -a args
+    args=(
+        '(--help -h)'{--help,-h}'[display help text]'
+        '(-c --cat --no-pager)'{--no-pager,--cat,-c}'[do not use a pager]'
+        '(--format -F)'{--format,-F}'[specify output format]'
+        '(--list -l)'{--list,-l}'[list available languages]'
+        '(--pager -P)'{--pager,-P}'[specify which pager to use]'
+        '(--syntax -s)'{--syntax,-s}'[specify which syntax to assume]:languages:_hl_languages'
+        '(--themes -t)'{--themes,-t}'[list available themes]'
+        '(--theme -T)'{--theme,-T}'[specify which theme to use]:themes:_hl_themes'
+        '*:files:_path_files'
+    )
+    _arguments -s $args
 }
 
-_arguments -s '1: :_hl_genarg' '2:files:_path_files'
+_hl_complete "$@"
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