Skip to content

Commit

Permalink
'main': Highlight escaped reserved words properly. Fixes zsh-users#701.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielshahaf committed Mar 19, 2020
1 parent f284041 commit 3997739
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 6 additions & 3 deletions highlighters/main/main-highlighter.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,12 @@ _zsh_highlight_main_calculate_fallback() {
#
# If $2 is 0, do not consider aliases.
#
# If $3 is 0, do not consider reserved words.
#
# The result will be stored in REPLY.
_zsh_highlight_main__type() {
integer -r aliases_allowed=${2-1}
integer -r resword_allowed=${3-1}
# We won't cache replies of anything that exists as an alias at all, to
# ensure the cached value is correct regardless of $aliases_allowed.
#
Expand Down Expand Up @@ -186,7 +189,7 @@ _zsh_highlight_main__type() {
REPLY=alias
elif [[ $1 == *.* && -n ${1%.*} ]] && (( $+saliases[(e)${1##*.}] )); then
REPLY='suffix alias'
elif (( $reswords[(Ie)$1] )); then
elif (( resword_allowed )) && (( $reswords[(Ie)$1] )); then
REPLY=reserved
elif (( $+functions[(e)$1] )); then
REPLY=function
Expand Down Expand Up @@ -616,7 +619,7 @@ _zsh_highlight_main_highlighter_highlight_list()
if [[ $this_word == *':start:'* ]] && ! (( in_redirection )); then
# Expand aliases.
# An alias is ineligible for expansion while it's being expanded (see #652/#653).
_zsh_highlight_main__type "$arg" "$(( ! ${+seen_alias[$arg]} ))"
_zsh_highlight_main__type "$arg" "$(( ! ${+seen_alias[$arg]} ))" 1
local res="$REPLY"
if [[ $res == "alias" ]]; then
# Mark insane aliases as unknown-token (cf. #263).
Expand Down Expand Up @@ -648,7 +651,7 @@ _zsh_highlight_main_highlighter_highlight_list()
continue
else
_zsh_highlight_main_highlighter_expand_path $arg
_zsh_highlight_main__type "$REPLY" 0
_zsh_highlight_main__type "$REPLY" 0 0
res="$REPLY"
fi
fi
Expand Down
4 changes: 3 additions & 1 deletion highlighters/main/test-data/escaped-reserved-word-isnt.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@
BUFFER=$'\\local a=( * )'

expected_region_highlight=(
'1 6 builtin "issue #701"' # \\local
'1 6 builtin' # \\local
'8 14 default' # a=( * )
'12 12 globbing' # *
)

0 comments on commit 3997739

Please sign in to comment.