From 9b4316a92a3c1632d460462cafe0539b1e8e804a Mon Sep 17 00:00:00 2001 From: qinfanpeng Date: Wed, 5 Mar 2014 20:51:24 +0800 Subject: [PATCH] [qfp] Add highlight command plugin --- .../highlight-command.plugin.zsh | 6 ++++ .../highlight-command/highlight-command.sh | 34 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 plugins/highlight-command/highlight-command.plugin.zsh create mode 100644 plugins/highlight-command/highlight-command.sh diff --git a/plugins/highlight-command/highlight-command.plugin.zsh b/plugins/highlight-command/highlight-command.plugin.zsh new file mode 100644 index 00000000..3ee681a3 --- /dev/null +++ b/plugins/highlight-command/highlight-command.plugin.zsh @@ -0,0 +1,6 @@ +_load_highlight_command() { + source $1/highlight-command.sh +} + +[[ -f $ZSH_CUSTOM/plugins/highlight-command/highlight-command.plugin.zsh ]] && _load_highlight_command $ZSH_CUSTOM/plugins/highlight-command +[[ -f $ZSH/plugins/highlight-command/highlight-command.plugin.zsh ]] && _load_highlight_command $ZSH/plugins/highlight-command diff --git a/plugins/highlight-command/highlight-command.sh b/plugins/highlight-command/highlight-command.sh new file mode 100644 index 00000000..159f64c1 --- /dev/null +++ b/plugins/highlight-command/highlight-command.sh @@ -0,0 +1,34 @@ + +setopt extended_glob + TOKENS_FOLLOWED_BY_COMMANDS=('|' '||' ';' '&' '&&' 'sudo' 'do' 'time' 'strace') + + recolor-cmd() { + region_highlight=() + colorize=true + start_pos=0 + for arg in ${(z)BUFFER}; do + ((start_pos+=${#BUFFER[$start_pos+1,-1]}-${#${BUFFER[$start_pos+1,-1]## #}})) + ((end_pos=$start_pos+${#arg})) + if $colorize; then + colorize=false + res=$(LC_ALL=C builtin type $arg 2>/dev/null) + case $res in + *'reserved word'*) style="fg=magenta,bold";; + *'alias for'*) style="fg=cyan,bold";; + *'shell builtin'*) style="fg=yellow,bold";; + *'shell function'*) style='fg=green,bold';; + *"$arg is"*) + [[ $arg = 'sudo' ]] && style="fg=red,bold" || style="fg=blue,bold";; + *) style='none,bold';; + esac + region_highlight+=("$start_pos $end_pos $style") + fi + [[ ${${TOKENS_FOLLOWED_BY_COMMANDS[(r)${arg//|/\|}]}:+yes} = 'yes' ]] && colorize=true + start_pos=$end_pos + done + } +check-cmd-self-insert() { zle .self-insert && recolor-cmd } +check-cmd-backward-delete-char() { zle .backward-delete-char && recolor-cmd } + +zle -N self-insert check-cmd-self-insert +zle -N backward-delete-char check-cmd-backward-delete-char \ No newline at end of file