Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dbd27cb30a | ||
|
|
08b80022ca | ||
|
|
a7ee0597ef | ||
|
|
3677b75731 | ||
|
|
3f2c76f393 | ||
|
|
6fcdb06b28 | ||
|
|
57c01d19de | ||
|
|
8abcf187f6 | ||
|
|
93827ed84d | ||
|
|
228f5a6aad | ||
|
|
e5d8a50d36 | ||
|
|
612e493cba | ||
|
|
408b100295 | ||
|
|
a0862053f5 | ||
|
|
b4e667795c | ||
|
|
45194671af | ||
|
|
19981ef9ea | ||
|
|
732b7d6e65 | ||
|
|
966eb851ca | ||
|
|
bb0d575942 | ||
|
|
30c365e030 | ||
|
|
2c5c38144d | ||
|
|
d82eee5212 | ||
|
|
56b134f5d6 |
11
README.md
11
README.md
@@ -9,6 +9,11 @@ zsh-syntax-highlighting
|
||||
How to install
|
||||
--------------
|
||||
|
||||
### 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)
|
||||
* 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,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
|
||||
|
||||
@@ -52,18 +52,18 @@ _zsh_highlight_brackets_highlighter()
|
||||
|
||||
# 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
|
||||
|
||||
@@ -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,6 +63,7 @@ _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
|
||||
typeset -a ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR
|
||||
@@ -72,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=(
|
||||
@@ -81,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}))
|
||||
@@ -137,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
|
||||
@@ -157,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
|
||||
}
|
||||
|
||||
@@ -166,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]") }
|
||||
}
|
||||
|
||||
@@ -107,7 +107,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.
|
||||
@@ -142,16 +142,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.
|
||||
|
||||
Reference in New Issue
Block a user