Compare commits
48 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3dc5741900 | ||
|
|
10391d025a | ||
|
|
443908b9a2 | ||
|
|
eb9870f4db | ||
|
|
74a183447d | ||
|
|
d7d4dff1c8 | ||
|
|
7edd08156e | ||
|
|
e8cc271314 | ||
|
|
5320f1e18d | ||
|
|
dcb5da9457 | ||
|
|
c83e266888 | ||
|
|
072c064d2a | ||
|
|
04f9c8884f | ||
|
|
f289a9f8e7 | ||
|
|
8ac964186d | ||
|
|
ffce2a0b20 | ||
|
|
63a720af9b | ||
|
|
784a5dc35b | ||
|
|
441f1a8aad | ||
|
|
c62cb54e9d | ||
|
|
0fc6b07509 | ||
|
|
dffbab0c92 | ||
|
|
3b3a58be91 | ||
|
|
dbd27cb30a | ||
|
|
08b80022ca | ||
|
|
a7ee0597ef | ||
|
|
3677b75731 | ||
|
|
3f2c76f393 | ||
|
|
6fcdb06b28 | ||
|
|
57c01d19de | ||
|
|
8abcf187f6 | ||
|
|
93827ed84d | ||
|
|
228f5a6aad | ||
|
|
0fcc2629d2 | ||
|
|
e5d8a50d36 | ||
|
|
612e493cba | ||
|
|
408b100295 | ||
|
|
a0862053f5 | ||
|
|
b4e667795c | ||
|
|
45194671af | ||
|
|
19981ef9ea | ||
|
|
732b7d6e65 | ||
|
|
966eb851ca | ||
|
|
bb0d575942 | ||
|
|
30c365e030 | ||
|
|
2c5c38144d | ||
|
|
d82eee5212 | ||
|
|
56b134f5d6 |
13
README.md
13
README.md
@@ -3,12 +3,17 @@ zsh-syntax-highlighting
|
||||
|
||||
**[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
|
||||
--------------
|
||||
|
||||
### Using packages
|
||||
|
||||
* 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)
|
||||
|
||||
### In your ~/.zshrc
|
||||
|
||||
* Download the script or clone this repository:
|
||||
@@ -28,7 +33,7 @@ How to install
|
||||
|
||||
* Download the script or clone this repository in [oh-my-zsh](http://github.com/robbyrussell/oh-my-zsh) plugins directory:
|
||||
|
||||
cd ~/.oh-my-zsh/plugins/
|
||||
cd ~/.oh-my-zsh/custom/plugins
|
||||
git clone git://github.com/zsh-users/zsh-syntax-highlighting.git
|
||||
|
||||
* Activate the plugin in `~/.zshrc` (in **last** position):
|
||||
@@ -36,12 +41,12 @@ How to install
|
||||
plugins=( [plugins...] zsh-syntax-highlighting)
|
||||
|
||||
* Source `~/.zshrc` to take changes into account:
|
||||
|
||||
|
||||
source ~/.zshrc
|
||||
|
||||
|
||||
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.
|
||||
|
||||
@@ -3,11 +3,12 @@ zsh-syntax-highlighting / highlighters
|
||||
|
||||
Syntax highlighting is done by pluggable highlighters:
|
||||
|
||||
* [***main***](highlighters/main) - the base highlighter, and the only one active by default.
|
||||
* [***brackets***](highlighters/brackets) - matches brackets and parenthesis.
|
||||
* [***pattern***](highlighters/pattern) - matches user-defined patterns.
|
||||
* [***cursor***](highlighters/cursor) - matches the cursor position.
|
||||
* [***root***](highlighters/root) - triggered if the current user is root.
|
||||
* [***main***](main) - the base highlighter, and the only one active by default.
|
||||
* [***brackets***](brackets) - matches brackets and parenthesis.
|
||||
* [***pattern***](pattern) - matches user-defined patterns.
|
||||
* [***cursor***](cursor) - matches the cursor position.
|
||||
* [***root***](root) - triggered if the current user is root.
|
||||
* [***line***](line) - applied to the whole command line
|
||||
|
||||
|
||||
How to activate highlighters
|
||||
|
||||
@@ -49,21 +49,22 @@ _zsh_highlight_brackets_highlighter()
|
||||
{
|
||||
local level=0 pos
|
||||
local -A levelpos lastoflevel matching typepos
|
||||
region_highlight=()
|
||||
|
||||
# Find all brackets and remember which one is matching
|
||||
for (( pos = 0; $pos < ${#BUFFER}; pos++ )) ; do
|
||||
local char=$BUFFER[pos+1]
|
||||
local char="$BUFFER[pos+1]"
|
||||
case $char in
|
||||
["([{"])
|
||||
levelpos[$pos]=$((++level))
|
||||
lastoflevel[$level]=$pos
|
||||
_zsh_highlight_brackets_highlighter_brackettype $char
|
||||
_zsh_highlight_brackets_highlighter_brackettype "$char"
|
||||
;;
|
||||
[")]}"])
|
||||
matching[$lastoflevel[$level]]=$pos
|
||||
matching[$pos]=$lastoflevel[$level]
|
||||
levelpos[$pos]=$((level--))
|
||||
_zsh_highlight_brackets_highlighter_brackettype $char
|
||||
_zsh_highlight_brackets_highlighter_brackettype "$char"
|
||||
;;
|
||||
['"'\'])
|
||||
# Skip everything inside quotes
|
||||
|
||||
24
highlighters/line/README.md
Normal file
24
highlighters/line/README.md
Normal 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).
|
||||
45
highlighters/line/line-highlighter.zsh
Normal file
45
highlighters/line/line-highlighter.zsh
Normal 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]")
|
||||
}
|
||||
@@ -32,6 +32,8 @@ This highlighter defines the following styles:
|
||||
* `commandseparator` - command separation tokens
|
||||
* `hashed-command` - hashed commands
|
||||
* `path` - paths
|
||||
* `path_prefix` - path prefixes
|
||||
* `path_approx` - approximated paths
|
||||
* `globbing` - globbing expressions
|
||||
* `history-expansion` - history expansion expressions
|
||||
* `single-hyphen-option` - single hyphen options
|
||||
|
||||
@@ -41,6 +41,8 @@
|
||||
: ${ZSH_HIGHLIGHT_STYLES[commandseparator]:=none}
|
||||
: ${ZSH_HIGHLIGHT_STYLES[hashed-command]:=fg=green}
|
||||
: ${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[history-expansion]:=fg=blue}
|
||||
: ${ZSH_HIGHLIGHT_STYLES[single-hyphen-option]:=none}
|
||||
@@ -61,8 +63,9 @@ _zsh_highlight_main_highlighter_predicate()
|
||||
# Main syntax highlighting function.
|
||||
_zsh_highlight_main_highlighter()
|
||||
{
|
||||
emulate -L zsh
|
||||
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_PRECOMMANDS
|
||||
typeset -a ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS
|
||||
@@ -81,19 +84,39 @@ _zsh_highlight_main_highlighter()
|
||||
|
||||
for arg in ${(z)BUFFER}; do
|
||||
local substr_color=0
|
||||
local style_override=""
|
||||
[[ $start_pos -eq 0 && $arg = 'noglob' ]] && highlight_glob=false
|
||||
((start_pos+=${#BUFFER[$start_pos+1,-1]}-${#${BUFFER[$start_pos+1,-1]##[[:space:]]#}}))
|
||||
((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
|
||||
new_expression=false
|
||||
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]]; then
|
||||
style=$ZSH_HIGHLIGHT_STYLES[precommand]
|
||||
elif [[ "$arg" = "sudo" ]]; then
|
||||
style=$ZSH_HIGHLIGHT_STYLES[precommand]
|
||||
sudo=true
|
||||
else
|
||||
res=$(LC_ALL=C builtin type -w $arg 2>/dev/null)
|
||||
case $res in
|
||||
*': reserved') style=$ZSH_HIGHLIGHT_STYLES[reserved-word];;
|
||||
*': 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)
|
||||
;;
|
||||
*': builtin') style=$ZSH_HIGHLIGHT_STYLES[builtin];;
|
||||
@@ -105,7 +128,7 @@ _zsh_highlight_main_highlighter()
|
||||
new_expression=true
|
||||
elif _zsh_highlight_main_highlighter_check_path; then
|
||||
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]
|
||||
else
|
||||
style=$ZSH_HIGHLIGHT_STYLES[unknown-token]
|
||||
@@ -137,6 +160,8 @@ _zsh_highlight_main_highlighter()
|
||||
;;
|
||||
esac
|
||||
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")
|
||||
[[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS:#"$arg"} ]] && new_expression=true
|
||||
start_pos=$end_pos
|
||||
@@ -147,7 +172,7 @@ _zsh_highlight_main_highlighter()
|
||||
_zsh_highlight_main_highlighter_check_assign()
|
||||
{
|
||||
setopt localoptions extended_glob
|
||||
[[ ${(Q)arg} == [[:alpha:]_]([[:alnum:]_])#=* ]]
|
||||
[[ $arg == [[:alpha:]_][[:alnum:]_]#(|\[*\])=* ]]
|
||||
}
|
||||
|
||||
# Check if the argument is a path.
|
||||
@@ -157,8 +182,21 @@ _zsh_highlight_main_highlighter_check_path()
|
||||
local expanded_path; : ${expanded_path:=${(Q)~arg}}
|
||||
[[ -z $expanded_path ]] && return 1
|
||||
[[ -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
|
||||
[[ ${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
|
||||
}
|
||||
|
||||
@@ -166,18 +204,36 @@ _zsh_highlight_main_highlighter_check_path()
|
||||
_zsh_highlight_main_highlighter_highlight_string()
|
||||
{
|
||||
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.
|
||||
for (( i = 2 ; i < end_pos - start_pos ; i += 1 )) ; do
|
||||
(( j = i + start_pos - 1 ))
|
||||
(( k = j + 1 ))
|
||||
case "$arg[$i]" in
|
||||
'$') style=$ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument];;
|
||||
"\\") style=$ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]
|
||||
(( k += 1 )) # Color following char too.
|
||||
(( i += 1 )) # Skip parsing the escaped char.
|
||||
'$' ) style=$ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]
|
||||
(( varflag = 1))
|
||||
;;
|
||||
*) 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
|
||||
region_highlight+=("$j $k $style")
|
||||
done
|
||||
|
||||
39
highlighters/main/test-data/sudo-command.zsh
Normal file
39
highlighters/main/test-data/sudo-command.zsh
Normal 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]" # /
|
||||
)
|
||||
@@ -42,6 +42,7 @@ _zsh_highlight_pattern_highlighter_predicate()
|
||||
_zsh_highlight_pattern_highlighter()
|
||||
{
|
||||
setopt localoptions extendedglob
|
||||
local pattern
|
||||
for pattern in ${(k)ZSH_HIGHLIGHT_PATTERNS}; do
|
||||
_zsh_highlight_pattern_highlighter_loop "$BUFFER" "$pattern"
|
||||
done
|
||||
|
||||
@@ -41,5 +41,5 @@ _zsh_highlight_root_highlighter_predicate()
|
||||
# root highlighting function.
|
||||
_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]") }
|
||||
}
|
||||
|
||||
@@ -54,37 +54,42 @@ _zsh_highlight()
|
||||
# Do not highlight if there are pending inputs (copy/paste).
|
||||
[[ $PENDING -gt 0 ]] && return $ret
|
||||
|
||||
# Reset region highlight to build it from scratch
|
||||
region_highlight=();
|
||||
|
||||
{
|
||||
local -a selected_highlighters
|
||||
local cache_place
|
||||
local -a region_highlight_copy
|
||||
|
||||
# Select which highlighters in ZSH_HIGHLIGHT_HIGHLIGHTERS need to be invoked.
|
||||
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 "_zsh_highlight_${highlighter}_highlighter_predicate"; then
|
||||
|
||||
# Mark the highlighter as selected for update.
|
||||
selected_highlighters+=($highlighter)
|
||||
# save a copy, and cleanup region_highlight
|
||||
region_highlight_copy=("${region_highlight[@]}")
|
||||
region_highlight=()
|
||||
|
||||
# Remove what was stored in its cache from region_highlight.
|
||||
cache_place="_zsh_highlight_${highlighter}_highlighter_cache"
|
||||
typeset -ga ${cache_place}
|
||||
[[ ${#${(P)cache_place}} -gt 0 ]] && [[ ! -z ${region_highlight-} ]] && region_highlight=(${region_highlight:#(${(P~j.|.)cache_place})})
|
||||
# Execute highlighter and save result
|
||||
{
|
||||
"_zsh_highlight_${highlighter}_highlighter"
|
||||
} always {
|
||||
eval "${cache_place}=(\"\${region_highlight[@]}\")"
|
||||
}
|
||||
|
||||
# Restore saved region_highlight
|
||||
region_highlight=("${region_highlight_copy[@]}")
|
||||
|
||||
fi
|
||||
done
|
||||
|
||||
# Invoke each selected highlighter and store the result in its cache.
|
||||
local -a region_highlight_copy
|
||||
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})}}
|
||||
}
|
||||
# Use value form cache if any cached
|
||||
eval "region_highlight+=(\"\${${cache_place}[@]}\")"
|
||||
|
||||
done
|
||||
|
||||
} always {
|
||||
@@ -107,7 +112,7 @@ typeset -gA ZSH_HIGHLIGHT_STYLES
|
||||
# Returns 0 if the buffer has changed since _zsh_highlight was last called.
|
||||
_zsh_highlight_buffer_modified()
|
||||
{
|
||||
[[ ${_ZSH_HIGHLIGHT_PRIOR_BUFFER:-} != $BUFFER ]]
|
||||
[[ "${_ZSH_HIGHLIGHT_PRIOR_BUFFER:-}" != "$BUFFER" ]]
|
||||
}
|
||||
|
||||
# Whether the cursor has moved or not.
|
||||
@@ -134,7 +139,7 @@ _zsh_highlight_bind_widgets()
|
||||
|
||||
# Override ZLE widgets to make them invoke _zsh_highlight.
|
||||
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
|
||||
|
||||
# Already rebound event: do nothing.
|
||||
@@ -142,16 +147,16 @@ _zsh_highlight_bind_widgets()
|
||||
|
||||
# User defined widget: override and rebind old one with prefix "orig-".
|
||||
user:*) eval "zle -N orig-$cur_widget ${widgets[$cur_widget]#*:}; \
|
||||
_zsh_highlight_widget_$cur_widget() { builtin zle orig-$cur_widget \"\$@\" && _zsh_highlight }; \
|
||||
_zsh_highlight_widget_$cur_widget() { builtin zle orig-$cur_widget -- \"\$@\" && _zsh_highlight }; \
|
||||
zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
|
||||
|
||||
# Completion widget: override and rebind old one with prefix "orig-".
|
||||
completion:*) eval "zle -C orig-$cur_widget ${${widgets[$cur_widget]#*:}/:/ }; \
|
||||
_zsh_highlight_widget_$cur_widget() { builtin zle orig-$cur_widget \"\$@\" && _zsh_highlight }; \
|
||||
_zsh_highlight_widget_$cur_widget() { builtin zle orig-$cur_widget -- \"\$@\" && _zsh_highlight }; \
|
||||
zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
|
||||
|
||||
# Builtin widget: override and make it call the builtin ".widget".
|
||||
builtin) eval "_zsh_highlight_widget_$cur_widget() { builtin zle .$cur_widget \"\$@\" && _zsh_highlight }; \
|
||||
builtin) eval "_zsh_highlight_widget_$cur_widget() { builtin zle .$cur_widget -- \"\$@\" && _zsh_highlight }; \
|
||||
zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
|
||||
|
||||
# Default: unhandled case.
|
||||
@@ -198,7 +203,7 @@ _zsh_highlight_bind_widgets || {
|
||||
}
|
||||
|
||||
# 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
|
||||
return 1
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user