43 Commits
0.1.1 ... 0.2.1

Author SHA1 Message Date
nicoulaj
3dc5741900 Merge pull request #140 from randomize/pr_fix_issue_#77
Pr fix issue #77
2014-09-24 22:58:20 +02:00
nicoulaj
10391d025a Merge pull request #134 from sonnym/enable_quick_history_substitution_highlighting
highlight ^old^new pattern
2014-09-24 22:54:21 +02:00
nicoulaj
443908b9a2 Merge pull request #142 from jcassee/master
Add 'line' highlighter for the whole buffer
2014-09-24 22:50:54 +02:00
nicoulaj
eb9870f4db Merge pull request #143 from vincentbernat/fix/yank-pop
zle: don't override yank/yank-pop
2014-09-24 22:49:21 +02:00
Vincent Bernat
74a183447d zle: don't override yank/yank-pop
`yank-pop` relies on the fact that the last zle command is `yank` or
`yank-pop` to work correctly. Rewriting them prevents this check to work
correctly breaking `yank-pop`.

This fix just disallow overriding of those two zle commands. As a
side-effect, syntax highlighting will not happen when using.

This fixes #99.
2014-06-29 11:52:32 +02:00
Joost Cassee
d7d4dff1c8 Add 'line' highlighter for the whole buffer 2014-06-08 00:04:00 +02:00
Randy
7edd08156e Chaned main highlighter alorithm to resolve issue #77 2014-03-29 22:52:10 +02:00
Randy
e8cc271314 Resolves slow performance in brackets highlighter 2014-03-24 08:45:34 +02:00
Julien Nicoulaud
5320f1e18d Merge pull request #135 from arcan1s/patch-1
Update README.md
2014-01-15 04:15:52 -08:00
Evgeniy Alexeev
dcb5da9457 Update README.md
Edited links to Archlinux packages (the link to `zsh-syntax-highlighting-git` is out-of-date, and `zsh-syntax-highlighting` is now available in [community])
2014-01-09 04:21:35 +03:00
Julien Nicoulaud
c83e266888 #110: Now using :A modifier, bump zsh version requirement 2013-12-24 12:40:35 +01:00
Julien Nicoulaud
072c064d2a Merge pull request #110 from xaocon/master
small change to find highlighting directory better
2013-12-24 03:39:37 -08:00
sonnym
04f9c8884f highlight ^old^new pattern
by extending the already in place mechanism of checking for the first
histchar to check for the second as well
2013-12-07 05:11:19 -05:00
Julien Nicoulaud
f289a9f8e7 Merge pull request #125 from acatton/fix-121
Fix #121: sudo syntax highlighting incorrectly
2013-11-19 11:42:03 -08:00
Julien Nicoulaud
8ac964186d Merge pull request #131 from MarkLodato/array-assignments
Properly highlight array assignments
2013-11-19 11:40:29 -08:00
Julien Nicoulaud
ffce2a0b20 Merge pull request #133 from lucc/master
protect alias expansion from interpreting aliases as options
2013-11-19 11:39:40 -08:00
Julien Nicoulaud
63a720af9b Merge pull request #128 from knu/patch-1
Do not leak a variable: cdpath_dir.
2013-11-19 11:38:50 -08:00
Lucas Hoffmann
784a5dc35b protect alias expansion from interpreting aliases as options
If one defines aliases like `++` the alias builtin tries to interprete these
as options so they have to be protected like this

    alias -- ++=true

The same goes for a call to `alias` in order to expand the alias again.
2013-11-05 00:50:20 +01:00
Mark Lodato
441f1a8aad highlight array assignments of the form x[y]=...
This code is more lenient than bash.  Examples:

    $ x[y[]=
    zsh: no matches found: x[y[]=
    $ x[][]=
    zsh: no matches found: x[][]=

The proper solution is to look inside the [...] and make sure that all
unescaped/unquoted square brackes are matched, but that is a heck of
a lot more complicated than this simple 8-character patch.
2013-10-30 00:53:46 -04:00
Mark Lodato
c62cb54e9d do not remove quotes when checking assignments
Zsh does not allow the variable name or the equals sign to be quoted or
escaped.  The previous code incorrectly highlighted the following
examples as assignments:

    $ 'x=y'
    zsh: command not found: x=y
    $ x\=y
    zsh: command not found: x=y
    $ "x"=y
    zsh: command not found: x=y
    $ \x=y
    zsh: command not found: x=y
2013-10-30 00:00:16 -04:00
Akinori MUSHA
0fc6b07509 Do not leak a variable: cdpath_dir.
Leaking a variable that holds a path in CDPATH could easily end up appearing on your prompt like `~cdpath_dir/subdir`.
2013-10-21 21:18:38 +09:00
Evan Pitstick
dffbab0c92 a more simple and universal way (for zsh projects) to find highlight dir path 2013-10-09 12:02:34 -04:00
Antoine Catton
3b3a58be91 Fix #121: sudo syntax highlighting incorrectly
Parse sudo command line in order to highlight the options correctly
2013-09-28 19:33:34 -06:00
Julien Nicoulaud
dbd27cb30a #155: document path_prefix/path_approx 2013-08-09 09:56:28 +02:00
Julien Nicoulaud
08b80022ca Merge pull request #123 from Shura0/master
Highlighted dollar variable ver. 2
2013-08-09 00:47:32 -07:00
Shura
a7ee0597ef Update main-highlighter.zsh
Comments added
2013-08-09 10:24:14 +04:00
Julien Nicoulaud
3677b75731 Merge pull request #115 from Valodim/master
add separate path_prefix and path_approx hilights
2013-08-08 07:31:46 -07:00
Julien Nicoulaud
3f2c76f393 Merge pull request #116 from jameskyle/feature/Issue-#92-silence-system-bell-in-osx
Issue #92 Eliminates system bell errors on osx.
2013-08-08 07:26:22 -07:00
Julien Nicoulaud
6fcdb06b28 Merge pull request #122 from shockone/master
Update main-highlighter.zsh
2013-08-08 07:25:54 -07:00
Владимир
57c01d19de Update main-highlighter.zsh
Add support of CDPATH
2013-08-07 03:21:15 +03:00
Shura
8abcf187f6 dollar variable and backslash codes highlighting 2013-07-29 17:33:34 +04:00
James Kyle
93827ed84d Issue #92 Eliminates system bell errors on osx.
Prepended an 'if' to remove error bell on osx systems.
2013-07-25 11:39:21 -07:00
Vincent Breitmoser
228f5a6aad add separate path_prefix and path_approx hilights 2013-07-25 04:41:09 +02:00
Evan Pitstick
0fcc2629d2 Finds highlighters dir a little better 2013-03-08 00:57:16 -05:00
Julien Nicoulaud
e5d8a50d36 Merge pull request #106 from cknadler/master
Fix relative links in various project readmes
2013-02-27 04:42:09 -08:00
Chris Knadler
612e493cba Fix relative links in highlighters readme 2013-02-26 18:03:35 -08:00
Chris Knadler
408b100295 Fix relative link in main readme 2013-02-26 17:59:53 -08:00
Julien Nicoulaud
a0862053f5 Merge pull request #100 from hchbaw/for-nicoulaj/pattern-local
make $pattern local to _zsh_highlight_pattern_highlighter #97
2012-12-19 10:30:06 -08:00
Takeshi Banse
b4e667795c make $pattern local to _zsh_highlight_pattern_highlighter #97
Signed-off-by: Takeshi Banse <takebi@laafc.net>
2012-12-09 10:43:00 +09:00
Julien Nicoulaud
45194671af Merge pull request #91 from xaocon/master
Added sudo as a precommand
2012-08-26 03:33:34 -07:00
evan
19981ef9ea added sudo as a precommand 2012-08-17 15:52:09 -04:00
Jan M. Binder
732b7d6e65 Fix bracket highlighter 2012-04-07 17:09:15 +02:00
Jan M. Binder
966eb851ca Added quotes to suppress error 2012-04-07 11:48:11 +02:00
11 changed files with 219 additions and 46 deletions

View File

@@ -3,7 +3,7 @@ zsh-syntax-highlighting
**[Fish shell](http://www.fishshell.com) like syntax highlighting for [Zsh](http://www.zsh.org).** **[Fish shell](http://www.fishshell.com) like syntax highlighting for [Zsh](http://www.zsh.org).**
*Requirements: zsh 4.3.9+.* *Requirements: zsh 4.3.17+.*
How to install How to install
@@ -11,7 +11,7 @@ How to install
### Using packages ### Using packages
* Arch Linux: [AUR/zsh-syntax-highlighting](https://aur.archlinux.org/packages.php?ID=54171) / [AUR/zsh-syntax-highlighting-git](https://aur.archlinux.org/packages.php?ID=50867) * Arch Linux: [community/zsh-syntax-highlighting](https://www.archlinux.org/packages/zsh-syntax-highlighting) / [AUR/zsh-syntax-highlighting-git](https://aur.archlinux.org/packages/zsh-syntax-highlighting-git)
* Gentoo: [mv overlay](http://gpo.zugaina.org/app-shells/zsh-syntax-highlighting) * Gentoo: [mv overlay](http://gpo.zugaina.org/app-shells/zsh-syntax-highlighting)
### In your ~/.zshrc ### In your ~/.zshrc
@@ -41,12 +41,12 @@ How to install
plugins=( [plugins...] zsh-syntax-highlighting) plugins=( [plugins...] zsh-syntax-highlighting)
* Source `~/.zshrc` to take changes into account: * Source `~/.zshrc` to take changes into account:
source ~/.zshrc source ~/.zshrc
How to tweak How to tweak
------------ ------------
Syntax highlighting is done by pluggable highlighter scripts, see the [highlighters directory](zsh-syntax-highlighting/tree/master/highlighters) Syntax highlighting is done by pluggable highlighter scripts, see the [highlighters directory](highlighters)
for documentation and configuration settings. for documentation and configuration settings.

View File

@@ -3,11 +3,12 @@ zsh-syntax-highlighting / highlighters
Syntax highlighting is done by pluggable highlighters: Syntax highlighting is done by pluggable highlighters:
* [***main***](highlighters/main) - the base highlighter, and the only one active by default. * [***main***](main) - the base highlighter, and the only one active by default.
* [***brackets***](highlighters/brackets) - matches brackets and parenthesis. * [***brackets***](brackets) - matches brackets and parenthesis.
* [***pattern***](highlighters/pattern) - matches user-defined patterns. * [***pattern***](pattern) - matches user-defined patterns.
* [***cursor***](highlighters/cursor) - matches the cursor position. * [***cursor***](cursor) - matches the cursor position.
* [***root***](highlighters/root) - triggered if the current user is root. * [***root***](root) - triggered if the current user is root.
* [***line***](line) - applied to the whole command line
How to activate highlighters How to activate highlighters

View File

@@ -49,21 +49,22 @@ _zsh_highlight_brackets_highlighter()
{ {
local level=0 pos local level=0 pos
local -A levelpos lastoflevel matching typepos local -A levelpos lastoflevel matching typepos
region_highlight=()
# Find all brackets and remember which one is matching # Find all brackets and remember which one is matching
for (( pos = 0; $pos < ${#BUFFER}; pos++ )) ; do for (( pos = 0; $pos < ${#BUFFER}; pos++ )) ; do
local char=$BUFFER[pos+1] local char="$BUFFER[pos+1]"
case $char in case $char in
["([{"]) ["([{"])
levelpos[$pos]=$((++level)) levelpos[$pos]=$((++level))
lastoflevel[$level]=$pos lastoflevel[$level]=$pos
_zsh_highlight_brackets_highlighter_brackettype $char _zsh_highlight_brackets_highlighter_brackettype "$char"
;; ;;
[")]}"]) [")]}"])
matching[$lastoflevel[$level]]=$pos matching[$lastoflevel[$level]]=$pos
matching[$pos]=$lastoflevel[$level] matching[$pos]=$lastoflevel[$level]
levelpos[$pos]=$((level--)) levelpos[$pos]=$((level--))
_zsh_highlight_brackets_highlighter_brackettype $char _zsh_highlight_brackets_highlighter_brackettype "$char"
;; ;;
['"'\']) ['"'\'])
# Skip everything inside quotes # Skip everything inside quotes

View File

@@ -0,0 +1,24 @@
zsh-syntax-highlighting / highlighters / line
=================================================
This is the ***line*** highlighter, that highlights the whole line.
How to activate it
------------------
To activate it, add it to `ZSH_HIGHLIGHT_HIGHLIGHTERS`:
ZSH_HIGHLIGHT_HIGHLIGHTERS=( [...] line)
How to tweak it
---------------
This highlighter defines the following styles:
* `line` - the style for the whole line
To override one of those styles, change its entry in `ZSH_HIGHLIGHT_STYLES`, for example in `~/.zshrc`:
ZSH_HIGHLIGHT_STYLES[line]='bold'
The syntax for declaring styles is [documented here](http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#SEC135).

View File

@@ -0,0 +1,45 @@
#!/usr/bin/env zsh
# -------------------------------------------------------------------------------------------------
# Copyright (c) 2010-2011 zsh-syntax-highlighting contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted
# provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this list of conditions
# and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list of
# conditions and the following disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
# may be used to endorse or promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -------------------------------------------------------------------------------------------------
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------
# Define default styles.
: ${ZSH_HIGHLIGHT_STYLES[line]:=}
# Whether the root highlighter should be called or not.
_zsh_highlight_line_highlighter_predicate()
{
_zsh_highlight_buffer_modified
}
# root highlighting function.
_zsh_highlight_line_highlighter()
{
region_highlight+=("0 $#BUFFER $ZSH_HIGHLIGHT_STYLES[line]")
}

View File

@@ -32,6 +32,8 @@ This highlighter defines the following styles:
* `commandseparator` - command separation tokens * `commandseparator` - command separation tokens
* `hashed-command` - hashed commands * `hashed-command` - hashed commands
* `path` - paths * `path` - paths
* `path_prefix` - path prefixes
* `path_approx` - approximated paths
* `globbing` - globbing expressions * `globbing` - globbing expressions
* `history-expansion` - history expansion expressions * `history-expansion` - history expansion expressions
* `single-hyphen-option` - single hyphen options * `single-hyphen-option` - single hyphen options

View File

@@ -41,6 +41,8 @@
: ${ZSH_HIGHLIGHT_STYLES[commandseparator]:=none} : ${ZSH_HIGHLIGHT_STYLES[commandseparator]:=none}
: ${ZSH_HIGHLIGHT_STYLES[hashed-command]:=fg=green} : ${ZSH_HIGHLIGHT_STYLES[hashed-command]:=fg=green}
: ${ZSH_HIGHLIGHT_STYLES[path]:=underline} : ${ZSH_HIGHLIGHT_STYLES[path]:=underline}
: ${ZSH_HIGHLIGHT_STYLES[path_prefix]:=underline}
: ${ZSH_HIGHLIGHT_STYLES[path_approx]:=fg=yellow,underline}
: ${ZSH_HIGHLIGHT_STYLES[globbing]:=fg=blue} : ${ZSH_HIGHLIGHT_STYLES[globbing]:=fg=blue}
: ${ZSH_HIGHLIGHT_STYLES[history-expansion]:=fg=blue} : ${ZSH_HIGHLIGHT_STYLES[history-expansion]:=fg=blue}
: ${ZSH_HIGHLIGHT_STYLES[single-hyphen-option]:=none} : ${ZSH_HIGHLIGHT_STYLES[single-hyphen-option]:=none}
@@ -61,9 +63,9 @@ _zsh_highlight_main_highlighter_predicate()
# Main syntax highlighting function. # Main syntax highlighting function.
_zsh_highlight_main_highlighter() _zsh_highlight_main_highlighter()
{ {
emulate -L zsh emulate -L zsh
setopt localoptions extendedglob bareglobqual setopt localoptions extendedglob bareglobqual
local start_pos=0 end_pos highlight_glob=true new_expression=true arg style local start_pos=0 end_pos highlight_glob=true new_expression=true arg style sudo=false sudo_arg=false
typeset -a ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR typeset -a ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR
typeset -a ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS typeset -a ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS
typeset -a ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS typeset -a ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS
@@ -82,19 +84,39 @@ _zsh_highlight_main_highlighter()
for arg in ${(z)BUFFER}; do for arg in ${(z)BUFFER}; do
local substr_color=0 local substr_color=0
local style_override=""
[[ $start_pos -eq 0 && $arg = 'noglob' ]] && highlight_glob=false [[ $start_pos -eq 0 && $arg = 'noglob' ]] && highlight_glob=false
((start_pos+=${#BUFFER[$start_pos+1,-1]}-${#${BUFFER[$start_pos+1,-1]##[[:space:]]#}})) ((start_pos+=${#BUFFER[$start_pos+1,-1]}-${#${BUFFER[$start_pos+1,-1]##[[:space:]]#}}))
((end_pos=$start_pos+${#arg})) ((end_pos=$start_pos+${#arg}))
# Parse the sudo command line
if $sudo; then
case "$arg" in
# Flag that requires an argument
'-'[Cgprtu]) sudo_arg=true;;
# This prevents misbehavior with sudo -u -otherargument
'-'*) sudo_arg=false;;
*) if $sudo_arg; then
sudo_arg=false
else
sudo=false
new_expression=true
fi
;;
esac
fi
if $new_expression; then if $new_expression; then
new_expression=false new_expression=false
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]]; then if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]]; then
style=$ZSH_HIGHLIGHT_STYLES[precommand] style=$ZSH_HIGHLIGHT_STYLES[precommand]
elif [[ "$arg" = "sudo" ]]; then
style=$ZSH_HIGHLIGHT_STYLES[precommand]
sudo=true
else else
res=$(LC_ALL=C builtin type -w $arg 2>/dev/null) res=$(LC_ALL=C builtin type -w $arg 2>/dev/null)
case $res in case $res in
*': reserved') style=$ZSH_HIGHLIGHT_STYLES[reserved-word];; *': reserved') style=$ZSH_HIGHLIGHT_STYLES[reserved-word];;
*': alias') style=$ZSH_HIGHLIGHT_STYLES[alias] *': alias') style=$ZSH_HIGHLIGHT_STYLES[alias]
local aliased_command="${"$(alias $arg)"#*=}" local aliased_command="${"$(alias -- $arg)"#*=}"
[[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS:#"$aliased_command"} && -z ${(M)ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS:#"$arg"} ]] && ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS+=($arg) [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS:#"$aliased_command"} && -z ${(M)ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS:#"$arg"} ]] && ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS+=($arg)
;; ;;
*': builtin') style=$ZSH_HIGHLIGHT_STYLES[builtin];; *': builtin') style=$ZSH_HIGHLIGHT_STYLES[builtin];;
@@ -106,7 +128,7 @@ _zsh_highlight_main_highlighter()
new_expression=true new_expression=true
elif _zsh_highlight_main_highlighter_check_path; then elif _zsh_highlight_main_highlighter_check_path; then
style=$ZSH_HIGHLIGHT_STYLES[path] style=$ZSH_HIGHLIGHT_STYLES[path]
elif [[ $arg[0,1] = $histchars[0,1] ]]; then elif [[ $arg[0,1] == $histchars[0,1] || $arg[0,1] == $histchars[2,2] ]]; then
style=$ZSH_HIGHLIGHT_STYLES[history-expansion] style=$ZSH_HIGHLIGHT_STYLES[history-expansion]
else else
style=$ZSH_HIGHLIGHT_STYLES[unknown-token] style=$ZSH_HIGHLIGHT_STYLES[unknown-token]
@@ -138,6 +160,8 @@ _zsh_highlight_main_highlighter()
;; ;;
esac esac
fi fi
# if a style_override was set (eg in _zsh_highlight_main_highlighter_check_path), use it
[[ -n $style_override ]] && style=$ZSH_HIGHLIGHT_STYLES[$style_override]
[[ $substr_color = 0 ]] && region_highlight+=("$start_pos $end_pos $style") [[ $substr_color = 0 ]] && region_highlight+=("$start_pos $end_pos $style")
[[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS:#"$arg"} ]] && new_expression=true [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS:#"$arg"} ]] && new_expression=true
start_pos=$end_pos start_pos=$end_pos
@@ -148,7 +172,7 @@ _zsh_highlight_main_highlighter()
_zsh_highlight_main_highlighter_check_assign() _zsh_highlight_main_highlighter_check_assign()
{ {
setopt localoptions extended_glob setopt localoptions extended_glob
[[ ${(Q)arg} == [[:alpha:]_]([[:alnum:]_])#=* ]] [[ $arg == [[:alpha:]_][[:alnum:]_]#(|\[*\])=* ]]
} }
# Check if the argument is a path. # Check if the argument is a path.
@@ -158,8 +182,21 @@ _zsh_highlight_main_highlighter_check_path()
local expanded_path; : ${expanded_path:=${(Q)~arg}} local expanded_path; : ${expanded_path:=${(Q)~arg}}
[[ -z $expanded_path ]] && return 1 [[ -z $expanded_path ]] && return 1
[[ -e $expanded_path ]] && return 0 [[ -e $expanded_path ]] && return 0
# Search the path in CDPATH
local cdpath_dir
for cdpath_dir in $cdpath ; do
[[ -e "$cdpath_dir/$expanded_path" ]] && return 0
done
[[ ! -e ${expanded_path:h} ]] && return 1 [[ ! -e ${expanded_path:h} ]] && return 1
[[ ${BUFFER[1]} != "-" && ${#BUFFER} == $end_pos && -n $(print ${expanded_path}*(N)) ]] && return 0 if [[ ${BUFFER[1]} != "-" && ${#BUFFER} == $end_pos ]]; then
local -a tmp
# got a path prefix?
tmp=( ${expanded_path}*(N) )
(( $#tmp > 0 )) && style_override=path_prefix && return 0
# or maybe an approximate path?
tmp=( (#a1)${expanded_path}*(N) )
(( $#tmp > 0 )) && style_override=path_approx && return 0
fi
return 1 return 1
} }
@@ -167,18 +204,36 @@ _zsh_highlight_main_highlighter_check_path()
_zsh_highlight_main_highlighter_highlight_string() _zsh_highlight_main_highlighter_highlight_string()
{ {
setopt localoptions noksharrays setopt localoptions noksharrays
local i j k style local i j k style varflag
# Starting quote is at 1, so start parsing at offset 2 in the string. # Starting quote is at 1, so start parsing at offset 2 in the string.
for (( i = 2 ; i < end_pos - start_pos ; i += 1 )) ; do for (( i = 2 ; i < end_pos - start_pos ; i += 1 )) ; do
(( j = i + start_pos - 1 )) (( j = i + start_pos - 1 ))
(( k = j + 1 )) (( k = j + 1 ))
case "$arg[$i]" in case "$arg[$i]" in
'$') style=$ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument];; '$' ) style=$ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]
"\\") style=$ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument] (( varflag = 1))
(( k += 1 )) # Color following char too.
(( i += 1 )) # Skip parsing the escaped char.
;; ;;
*) continue;; "\\") style=$ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]
for (( c = i + 1 ; c < end_pos - start_pos ; c += 1 )); do
[[ "$arg[$c]" != ([0-9,xX,a-f,A-F]) ]] && break
done
AA=$arg[$i+1,$c-1]
# Matching for HEX and OCT values like \0xA6, \xA6 or \012
if [[ "$AA" =~ "^(0*(x|X)[0-9,a-f,A-F]{1,2})" || "$AA" =~ "^(0[0-7]{1,3})" ]];then
(( k += $#MATCH ))
(( i += $#MATCH ))
else
(( k += 1 )) # Color following char too.
(( i += 1 )) # Skip parsing the escaped char.
fi
(( varflag = 0 )) # End of variable
;;
([^a-zA-Z0-9_]))
(( varflag = 0 )) # End of variable
continue
;;
*) [[ $varflag -eq 0 ]] && continue ;;
esac esac
region_highlight+=("$j $k $style") region_highlight+=("$j $k $style")
done done

View File

@@ -0,0 +1,39 @@
#!/usr/bin/env zsh
# -------------------------------------------------------------------------------------------------
# Copyright (c) 2010-2013 zsh-syntax-highlighting contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted
# provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this list of conditions
# and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list of
# conditions and the following disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
# may be used to endorse or promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -------------------------------------------------------------------------------------------------
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------
BUFFER='sudo -u otheruser ls /'
expected_region_highlight=(
"1 4 $ZSH_HIGHLIGHT_STYLES[precommand]" # sudo
"6 7 $ZSH_HIGHLIGHT_STYLES[single-hyphen-option]" # -u
"9 17 $ZSH_HIGHLIGHT_STYLES[default]" # otheruser
"19 20 $ZSH_HIGHLIGHT_STYLES[command]" # ls
"22 22 $ZSH_HIGHLIGHT_STYLES[path]" # /
)

View File

@@ -42,6 +42,7 @@ _zsh_highlight_pattern_highlighter_predicate()
_zsh_highlight_pattern_highlighter() _zsh_highlight_pattern_highlighter()
{ {
setopt localoptions extendedglob setopt localoptions extendedglob
local pattern
for pattern in ${(k)ZSH_HIGHLIGHT_PATTERNS}; do for pattern in ${(k)ZSH_HIGHLIGHT_PATTERNS}; do
_zsh_highlight_pattern_highlighter_loop "$BUFFER" "$pattern" _zsh_highlight_pattern_highlighter_loop "$BUFFER" "$pattern"
done done

View File

@@ -41,5 +41,5 @@ _zsh_highlight_root_highlighter_predicate()
# root highlighting function. # root highlighting function.
_zsh_highlight_root_highlighter() _zsh_highlight_root_highlighter()
{ {
[[ $(command id -u) -eq 0 ]] && region_highlight+=("0 $#BUFFER $ZSH_HIGHLIGHT_STYLES[root]") if [[ $(command id -u) -eq 0 ]] { region_highlight+=("0 $#BUFFER $ZSH_HIGHLIGHT_STYLES[root]") }
} }

View File

@@ -54,37 +54,42 @@ _zsh_highlight()
# Do not highlight if there are pending inputs (copy/paste). # Do not highlight if there are pending inputs (copy/paste).
[[ $PENDING -gt 0 ]] && return $ret [[ $PENDING -gt 0 ]] && return $ret
# Reset region highlight to build it from scratch
region_highlight=();
{ {
local -a selected_highlighters
local cache_place local cache_place
local -a region_highlight_copy
# Select which highlighters in ZSH_HIGHLIGHT_HIGHLIGHTERS need to be invoked. # Select which highlighters in ZSH_HIGHLIGHT_HIGHLIGHTERS need to be invoked.
local highlighter; for highlighter in $ZSH_HIGHLIGHT_HIGHLIGHTERS; do local highlighter; for highlighter in $ZSH_HIGHLIGHT_HIGHLIGHTERS; do
# eval cache place for current highlighter and prepare it
cache_place="_zsh_highlight_${highlighter}_highlighter_cache"
typeset -ga ${cache_place}
# If highlighter needs to be invoked # If highlighter needs to be invoked
if "_zsh_highlight_${highlighter}_highlighter_predicate"; then if "_zsh_highlight_${highlighter}_highlighter_predicate"; then
# Mark the highlighter as selected for update. # save a copy, and cleanup region_highlight
selected_highlighters+=($highlighter) region_highlight_copy=("${region_highlight[@]}")
region_highlight=()
# Remove what was stored in its cache from region_highlight. # Execute highlighter and save result
cache_place="_zsh_highlight_${highlighter}_highlighter_cache" {
typeset -ga ${cache_place} "_zsh_highlight_${highlighter}_highlighter"
[[ ${#${(P)cache_place}} -gt 0 ]] && [[ ! -z ${region_highlight-} ]] && region_highlight=(${region_highlight:#(${(P~j.|.)cache_place})}) } always {
eval "${cache_place}=(\"\${region_highlight[@]}\")"
}
# Restore saved region_highlight
region_highlight=("${region_highlight_copy[@]}")
fi fi
done
# Invoke each selected highlighter and store the result in its cache. # Use value form cache if any cached
local -a region_highlight_copy eval "region_highlight+=(\"\${${cache_place}[@]}\")"
for highlighter in $selected_highlighters; do
cache_place="_zsh_highlight_${highlighter}_highlighter_cache"
region_highlight_copy=($region_highlight)
{
"_zsh_highlight_${highlighter}_highlighter"
} always {
[[ ! -z ${region_highlight-} ]] && : ${(PA)cache_place::=${region_highlight:#(${(~j.|.)region_highlight_copy})}}
}
done done
} always { } always {
@@ -107,7 +112,7 @@ typeset -gA ZSH_HIGHLIGHT_STYLES
# Returns 0 if the buffer has changed since _zsh_highlight was last called. # Returns 0 if the buffer has changed since _zsh_highlight was last called.
_zsh_highlight_buffer_modified() _zsh_highlight_buffer_modified()
{ {
[[ ${_ZSH_HIGHLIGHT_PRIOR_BUFFER:-} != $BUFFER ]] [[ "${_ZSH_HIGHLIGHT_PRIOR_BUFFER:-}" != "$BUFFER" ]]
} }
# Whether the cursor has moved or not. # Whether the cursor has moved or not.
@@ -134,7 +139,7 @@ _zsh_highlight_bind_widgets()
# Override ZLE widgets to make them invoke _zsh_highlight. # Override ZLE widgets to make them invoke _zsh_highlight.
local cur_widget local cur_widget
for cur_widget in ${${(f)"$(builtin zle -la)"}:#(.*|_*|orig-*|run-help|which-command|beep)}; do for cur_widget in ${${(f)"$(builtin zle -la)"}:#(.*|_*|orig-*|run-help|which-command|beep|yank*)}; do
case $widgets[$cur_widget] in case $widgets[$cur_widget] in
# Already rebound event: do nothing. # Already rebound event: do nothing.
@@ -198,7 +203,7 @@ _zsh_highlight_bind_widgets || {
} }
# Resolve highlighters directory location. # Resolve highlighters directory location.
_zsh_highlight_load_highlighters "${ZSH_HIGHLIGHT_HIGHLIGHTERS_DIR:-${0:h}/highlighters}" || { _zsh_highlight_load_highlighters "${ZSH_HIGHLIGHT_HIGHLIGHTERS_DIR:-${${0:A}:h}/highlighters}" || {
echo 'zsh-syntax-highlighting: failed loading highlighters, exiting.' >&2 echo 'zsh-syntax-highlighting: failed loading highlighters, exiting.' >&2
return 1 return 1
} }