Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dbd27cb30a | ||
|
|
08b80022ca | ||
|
|
a7ee0597ef | ||
|
|
3677b75731 | ||
|
|
3f2c76f393 | ||
|
|
6fcdb06b28 | ||
|
|
57c01d19de | ||
|
|
8abcf187f6 | ||
|
|
93827ed84d | ||
|
|
228f5a6aad | ||
|
|
e5d8a50d36 | ||
|
|
612e493cba | ||
|
|
408b100295 | ||
|
|
a0862053f5 | ||
|
|
b4e667795c | ||
|
|
45194671af | ||
|
|
19981ef9ea |
@@ -41,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,11 @@ 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.
|
||||
|
||||
|
||||
How to activate highlighters
|
||||
|
||||
@@ -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}
|
||||
@@ -73,7 +75,7 @@ _zsh_highlight_main_highlighter()
|
||||
'|' '||' ';' '&' '&&'
|
||||
)
|
||||
ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS=(
|
||||
'builtin' 'command' 'exec' 'nocorrect' 'noglob'
|
||||
'builtin' 'command' 'exec' 'nocorrect' 'noglob' 'sudo'
|
||||
)
|
||||
# Tokens that are always immediately followed by a command.
|
||||
ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS=(
|
||||
@@ -82,6 +84,7 @@ _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}))
|
||||
@@ -138,6 +141,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
|
||||
@@ -158,8 +163,20 @@ _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
|
||||
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
|
||||
}
|
||||
|
||||
@@ -167,18 +184,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
|
||||
|
||||
@@ -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]") }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user