#autoload autoload add-zsh-hook zmodload zsh/system || exit 1 typeset -g hlxtout hlxtin stderr oPS4="$PS4" coproc highlight -S sh -O xterm256 exec {hlxtout}<&p exec {hlxtin}>&p exec {stderr}>&2 # We don't want the commands that are managing highlighting to appear # in the trace output, so create them "sticky" with xtrace disabled emulate zsh +x -c ' hlxt_precmd() { local xtrace xt xc=0 # Unfortunately this has to use a one-second timeout because # "highlight" does not always write quickly enough. # # Manual says combining sysread -o with a param is valid, but # that does not work; neither output nor parameter is written. # while sysread -t 1 -i $hlxtout -c xc -o $stderr xtrace while sysread -t 1 -i $hlxtout -c xc xtrace do [[ -n $xtrace ]] && print -n -u $stderr -r -- "${xt::=$xtrace}" done # Need to handle error? (( $? < 4 && xc )) && [[ -n $xtrace ]] [[ -z $xt || $xt == *$'"'\\n'"' ]] || echo } hlxt() { setopt localoptions localtraps case $1 in (on|) add-zsh-hook precmd hlxt_precmd || exit 1 PS4= exec 2>&$hlxtin # Bypass local scope restore of xtrace trap "setopt xtrace" EXIT ;; (off) exec 2>&$stderr PS4="$oPS4" add-zsh-hook -d precmd hlxt_precmd trap "unsetopt xtrace" EXIT # Flush leftovers local stderr hlxt_precmd {stderr}>&/dev/null ;; (*) add-zsh-hook -L esac } '