From 2ff771eaa933f8b36e67f66feeb1a097a7f5b66a Mon Sep 17 00:00:00 2001 From: Andreas Jaggi Date: Sat, 25 Dec 2010 20:51:55 +0100 Subject: [PATCH 1/4] Underline existing paths --- README.md | 1 + zsh-syntax-highlighting.zsh | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9f7bcfa..136704c 100644 --- a/README.md +++ b/README.md @@ -20,3 +20,4 @@ Just source the script from your `~/.zshrc`. * [Mounier Florian](http://github.com/paradoxxxzero) * [Jonathan Dahan](http://github.com/jedahan) * James Ahlborn + * [Andreas Jaggi](http://github.com/x-way) diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index f0ae62a..1d21e88 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -10,6 +10,7 @@ ZLE_ALIAS_STYLE='fg=magenta,bold' ZLE_BUILTIN_STYLE='fg=cyan,bold' ZLE_FUNCTION_STYLE='fg=blue,bold' ZLE_COMMAND_STYLE='fg=green,bold' +ZLE_PATH_STYLE='fg=white,underline' ZLE_COMMAND_UNKNOWN_TOKEN_STYLE='fg=red,bold' ZLE_HYPHEN_CLI_OPTION='fg=yellow,bold' @@ -40,7 +41,10 @@ colorize-zle-buffer() { *'shell builtin'*) style=$ZLE_BUILTIN_STYLE;; *'shell function'*) style=$ZLE_FUNCTION_STYLE;; *"$cmd is"*) style=$ZLE_COMMAND_STYLE;; - *) style=$ZLE_COMMAND_UNKNOWN_TOKEN_STYLE;; + *) + style=$ZLE_COMMAND_UNKNOWN_TOKEN_STYLE + [ -e "$arg" ] && style=$ZLE_PATH_STYLE + ;; esac else case $arg in @@ -50,7 +54,10 @@ colorize-zle-buffer() { '"'*'"') style=$ZLE_DOUBLE_QUOTED;; '`'*'`') style=$ZLE_BACK_QUOTED;; *"*"*) style=$ZLE_GLOBING;; - *) style=$ZLE_DEFAULT;; + *) + style=$ZLE_DEFAULT + [ -e "$arg" ] && style=$ZLE_PATH_STYLE + ;; esac fi region_highlight+=("$start_pos $end_pos $style") From 732faaa252ffdf50f62bc58883a518e2682604af Mon Sep 17 00:00:00 2001 From: Andreas Jaggi Date: Sat, 25 Dec 2010 20:52:44 +0100 Subject: [PATCH 2/4] Personalize colors --- zsh-syntax-highlighting.zsh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index 1d21e88..882b847 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -6,17 +6,17 @@ # Token types styles. # See http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#SEC135 ZLE_RESERVED_WORD_STYLE='fg=yellow,bold' -ZLE_ALIAS_STYLE='fg=magenta,bold' +ZLE_ALIAS_STYLE='fg=green,bold' ZLE_BUILTIN_STYLE='fg=cyan,bold' ZLE_FUNCTION_STYLE='fg=blue,bold' ZLE_COMMAND_STYLE='fg=green,bold' ZLE_PATH_STYLE='fg=white,underline' ZLE_COMMAND_UNKNOWN_TOKEN_STYLE='fg=red,bold' -ZLE_HYPHEN_CLI_OPTION='fg=yellow,bold' -ZLE_DOUBLE_HYPHEN_CLI_OPTION='fg=green,bold' -ZLE_SINGLE_QUOTED='fg=magenta,bold' -ZLE_DOUBLE_QUOTED='fg=red,bold' +ZLE_HYPHEN_CLI_OPTION='fg=white' +ZLE_DOUBLE_HYPHEN_CLI_OPTION='fg=white' +ZLE_SINGLE_QUOTED='fg=yellow' +ZLE_DOUBLE_QUOTED='fg=red' ZLE_BACK_QUOTED='fg=cyan,bold' ZLE_GLOBING='fg=blue,bold' From 899c83d0cd30431c796317d352233507c8360891 Mon Sep 17 00:00:00 2001 From: Andreas Jaggi Date: Sat, 25 Dec 2010 22:38:07 +0100 Subject: [PATCH 3/4] Underline text at curor if matching path exists --- zsh-syntax-highlighting.zsh | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index ba298be..7bf6172 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -24,6 +24,14 @@ ZLE_DEFAULT='fg=white,bold' ZLE_TOKENS_FOLLOWED_BY_COMMANDS=('|' '||' ';' '&' '&&' 'sudo' 'start' 'time' 'strace' 'ยง') +_check_path() { + [ -z "$arg" ] && return 1 + [ -e $arg ] && return 0 + [ ! -e "`dirname $arg`" ] && return 1 + [ ${#BUFFER} = $end_pos -a -n "`print $arg*`" ] && return 0 + return 1 +} + # Recolorize the current ZLE buffer. colorize-zle-buffer() { region_highlight=() @@ -43,7 +51,7 @@ colorize-zle-buffer() { *"$cmd is"*) style=$ZLE_COMMAND_STYLE;; *) style=$ZLE_COMMAND_UNKNOWN_TOKEN_STYLE - [ -e "$arg" ] && style=$ZLE_PATH_STYLE + _check_path && style=$ZLE_PATH_STYLE ;; esac else @@ -56,7 +64,7 @@ colorize-zle-buffer() { *"*"*) style=$ZLE_GLOBING;; *) style=$ZLE_DEFAULT - [ -e "$arg" ] && style=$ZLE_PATH_STYLE + _check_path && style=$ZLE_PATH_STYLE ;; esac fi From 48b389747942a689b3e37ec838e21956d7f8bb5d Mon Sep 17 00:00:00 2001 From: Andreas Jaggi Date: Mon, 27 Dec 2010 12:57:05 +0100 Subject: [PATCH 4/4] Highlight special chars in double-quoted strings --- zsh-syntax-highlighting.zsh | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index f35f251..f1dbfcf 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -16,7 +16,9 @@ ZLE_COMMAND_UNKNOWN_TOKEN_STYLE='fg=red,bold' ZLE_HYPHEN_CLI_OPTION='fg=white' ZLE_DOUBLE_HYPHEN_CLI_OPTION='fg=white' ZLE_SINGLE_QUOTED='fg=yellow' -ZLE_DOUBLE_QUOTED='fg=red' +ZLE_DOUBLE_QUOTED='fg=yellow' +ZLE_DOLLAR_DOUBLE_QUOTED='fg=cyan' +ZLE_BACK_DOUBLE_QUOTED='fg=magenta' ZLE_BACK_QUOTED='fg=cyan,bold' ZLE_GLOBING='fg=blue,bold' @@ -32,6 +34,24 @@ _check_path() { return 1 } +# hightlight special chars inside double-quoted strings +_hl_string() { + local i + local j + local k + local c + for (( i = 0 ; i < end_pos - start_pos ; i += 1 )) ; do + (( j = i + start_pos - 1 )) + (( k = j + 1 )) + c="$arg[$i]" + [[ "$c" = '$' ]] && region_highlight+=("$j $k $ZLE_DOLLAR_DOUBLE_QUOTED") + if [[ "$c" = '\' ]] ; then + (( k = k + 1 )) + region_highlight+=("$j $k $ZLE_BACK_DOUBLE_QUOTED") + fi + done +} + # Recolorize the current ZLE buffer. colorize-zle-buffer() { setopt localoptions extendedglob @@ -39,6 +59,7 @@ colorize-zle-buffer() { colorize=true start_pos=0 for arg in ${(z)BUFFER}; do + local substr_color=0 ((start_pos+=${#BUFFER[$start_pos+1,-1]}-${#${BUFFER[$start_pos+1,-1]##[[:space:]]#}})) ((end_pos=$start_pos+${#arg})) if $colorize; then @@ -60,7 +81,12 @@ colorize-zle-buffer() { '--'*) style=$ZLE_DOUBLE_HYPHEN_CLI_OPTION;; '-'*) style=$ZLE_HYPHEN_CLI_OPTION;; "'"*"'") style=$ZLE_SINGLE_QUOTED;; - '"'*'"') style=$ZLE_DOUBLE_QUOTED;; + '"'*'"') + style=$ZLE_DOUBLE_QUOTED + region_highlight+=("$start_pos $end_pos $style") + _hl_string + substr_color=1 + ;; '`'*'`') style=$ZLE_BACK_QUOTED;; *"*"*) style=$ZLE_GLOBING;; *) @@ -69,7 +95,7 @@ colorize-zle-buffer() { ;; esac fi - region_highlight+=("$start_pos $end_pos $style") + [[ $substr_color = 0 ]] && region_highlight+=("$start_pos $end_pos $style") [[ ${${ZLE_TOKENS_FOLLOWED_BY_COMMANDS[(r)${arg//|/\|}]:-}:+yes} = 'yes' ]] && colorize=true start_pos=$end_pos done