Skip to content

Commit

Permalink
Squashed 'plugins/zsh-syntax-highlighting/' changes from 732b7d6..dbd…
Browse files Browse the repository at this point in the history
…27cb

dbd27cb ohmyzsh#155: document path_prefix/path_approx
08b8002 Merge pull request ohmyzsh#123 from Shura0/master
a7ee059 Update main-highlighter.zsh
3677b75 Merge pull request ohmyzsh#115 from Valodim/master
3f2c76f Merge pull request ohmyzsh#116 from jameskyle/feature/Issue-#92-silence-system-bell-in-osx
6fcdb06 Merge pull request ohmyzsh#122 from shockone/master
57c01d1 Update main-highlighter.zsh
8abcf18 dollar variable and backslash codes highlighting
93827ed Issue ohmyzsh#92 Eliminates system bell errors on osx.
228f5a6 add separate path_prefix and path_approx hilights
e5d8a50 Merge pull request ohmyzsh#106 from cknadler/master
612e493 Fix relative links in highlighters readme
408b100 Fix relative link in main readme
a086205 Merge pull request ohmyzsh#100 from hchbaw/for-nicoulaj/pattern-local
b4e6677 make $pattern local to _zsh_highlight_pattern_highlighter ohmyzsh#97
4519467 Merge pull request ohmyzsh#91 from xaocon/master
19981ef added sudo as a precommand

git-subtree-dir: plugins/zsh-syntax-highlighting
git-subtree-split: dbd27cb
  • Loading branch information
habnabit committed Nov 17, 2013
1 parent e1833b5 commit 843c136
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
10 changes: 5 additions & 5 deletions highlighters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions highlighters/main/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 42 additions & 7 deletions highlighters/main/main-highlighter.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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=(
Expand All @@ -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}))
Expand Down Expand Up @@ -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
Expand All @@ -158,27 +163,57 @@ _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
}

# Highlight special chars inside double-quoted strings
_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[dollar-double-quoted-argument]
(( varflag = 1))
;;
"\\") style=$ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]
(( k += 1 )) # Color following char too.
(( i += 1 )) # Skip parsing the escaped char.
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
;;
*) continue;;
([^a-zA-Z0-9_]))
(( varflag = 0 )) # End of variable
continue
;;
*) [[ $varflag -eq 0 ]] && continue ;;

esac
region_highlight+=("$j $k $style")
done
Expand Down
1 change: 1 addition & 0 deletions highlighters/pattern/pattern-highlighter.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion highlighters/root/root-highlighter.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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]") }
}

0 comments on commit 843c136

Please sign in to comment.