diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index b0c5c92..91be6ca 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -73,8 +73,10 @@ _zsh_highlight_main_add_region_highlight() { integer start=$1 end=$2 shift 2 - (( highlighted_alias )) && return - (( in_alias )) && highlighted_alias=1 + if (( in_alias )); then + [[ $1 == unknown-token ]] && alias_style=unknown-token + return + fi # The calculation was relative to $buf but region_highlight is relative to $BUFFER. (( start += buf_offset )) @@ -377,15 +379,16 @@ _zsh_highlight_highlighter_main_paint() _zsh_highlight_main_highlighter_highlight_list() { integer start_pos end_pos=0 buf_offset=$1 has_end=$3 + # alias_style is the style to apply to an alias once in_alias=0 + # Usually 'alias' but set to 'unknown-token' if any word expanded from + # the alias would be highlighted as unknown-token # last_alias is the last alias arg (lhs) expanded (if in an alias). # This allows for expanding alias ls='ls -l' while avoiding loops. - local arg buf=$4 highlight_glob=true last_alias style + local alias_style arg buf=$4 highlight_glob=true last_alias style local in_array_assignment=false # true between 'a=(' and the matching ')' - # highlighted_alias is 1 when the alias arg has been highlighted with a non-alias style. - # E.g. alias x=ls; x has been highlighted as alias AND command. # in_alias is equal to the number of shifts needed until arg=args[1] pops an # arg from BUFFER and not added by an alias. - integer highlighted_alias=0 in_alias=0 len=$#buf + integer in_alias=0 len=$#buf local -a match mbegin mend list_highlights # seen_alias is a map of aliases already seen to avoid loops like alias a=b b=a local -A seen_alias @@ -458,7 +461,11 @@ _zsh_highlight_main_highlighter_highlight_list() shift args if (( in_alias )); then (( in_alias-- )) - (( in_alias == 0 )) && highlighted_alias=0 last_alias= seen_alias=() + if (( in_alias == 0 )); then + last_alias= seen_alias=() + # start_pos and end_pos are of the alias (previous $arg) here + _zsh_highlight_main_add_region_highlight $start_pos $end_pos $alias_style + fi fi # Initialize this_word and next_word. @@ -535,6 +542,7 @@ _zsh_highlight_main_highlighter_highlight_list() # Avoid looping forever on alias a=b b=c c=b, but allow alias foo='foo bar' # Also mark insane aliases as unknown-token (cf. #263). if (( $+seen_alias[$arg] )) || [[ $arg == ?*=* ]]; then + (( in_alias == 0 )) && in_alias=1 _zsh_highlight_main_add_region_highlight $start_pos $end_pos unknown-token continue fi @@ -550,7 +558,7 @@ _zsh_highlight_main_highlighter_highlight_list() fi args=( $alias_args $args ) if (( in_alias == 0 )); then - _zsh_highlight_main_add_region_highlight $start_pos $end_pos alias + alias_style=alias # Add one because we will in_alias-- on the next loop iteration so # this iteration should be considered in in_alias as well (( in_alias += $#alias_args + 1 )) @@ -909,6 +917,7 @@ _zsh_highlight_main_highlighter_highlight_list() fi _zsh_highlight_main_add_region_highlight $start_pos $end_pos $style done + (( in_alias == 1 )) && in_alias=0 _zsh_highlight_main_add_region_highlight $start_pos $end_pos $alias_style [[ "$proc_buf" = (#b)(#s)(([[:space:]]|\\$'\n')#) ]] REPLY=$(( end_pos + ${#match[1]} - 1 )) reply=($list_highlights) diff --git a/highlighters/main/test-data/alias-comment1.zsh b/highlighters/main/test-data/alias-comment1.zsh index 0c449e1..dd5068f 100644 --- a/highlighters/main/test-data/alias-comment1.zsh +++ b/highlighters/main/test-data/alias-comment1.zsh @@ -33,6 +33,5 @@ alias x=$'# foo\npwd' BUFFER='x' expected_region_highlight=( - '1 1 alias' # x - '1 1 comment' # x (#) + '1 1 alias "issue #616"' # x ) diff --git a/highlighters/main/test-data/alias-comment2.zsh b/highlighters/main/test-data/alias-comment2.zsh index 8bdc5a8..5a49822 100644 --- a/highlighters/main/test-data/alias-comment2.zsh +++ b/highlighters/main/test-data/alias-comment2.zsh @@ -33,6 +33,5 @@ alias x=$'# foo\npwd' BUFFER='x' expected_region_highlight=( - '1 1 alias' # x '1 1 unknown-token' # x (#) ) diff --git a/highlighters/main/test-data/alias-complex.zsh b/highlighters/main/test-data/alias-complex.zsh index 7a9626e..5ad7c0a 100644 --- a/highlighters/main/test-data/alias-complex.zsh +++ b/highlighters/main/test-data/alias-complex.zsh @@ -33,7 +33,6 @@ BUFFER='x file echo' expected_region_highlight=( '1 1 alias' # x - '1 1 builtin' # x (echo) '3 6 default' # file '8 11 builtin' # echo ) diff --git a/highlighters/main/test-data/alias-loop.zsh b/highlighters/main/test-data/alias-loop.zsh index b36d1c9..baa122b 100644 --- a/highlighters/main/test-data/alias-loop.zsh +++ b/highlighters/main/test-data/alias-loop.zsh @@ -33,7 +33,6 @@ alias a=b b=c c=b BUFFER='a foo; :' expected_region_highlight=( - '1 1 alias' # a '1 1 unknown-token' # a (invalid alias loop) '3 5 default' # foo '6 6 commandseparator' # ; diff --git a/highlighters/main/test-data/alias-nested-precommand.zsh b/highlighters/main/test-data/alias-nested-precommand.zsh index 7c2eeeb..6d4172b 100644 --- a/highlighters/main/test-data/alias-nested-precommand.zsh +++ b/highlighters/main/test-data/alias-nested-precommand.zsh @@ -35,7 +35,6 @@ BUFFER='a -u phy1729 echo; :' expected_region_highlight=( '1 1 alias' # a - '1 1 precommand' # a (sudo) '3 4 single-hyphen-option' # -u '6 12 default' # phy1729 '14 17 builtin' # echo diff --git a/highlighters/main/test-data/alias-nested.zsh b/highlighters/main/test-data/alias-nested.zsh index 44ab22b..b24b496 100644 --- a/highlighters/main/test-data/alias-nested.zsh +++ b/highlighters/main/test-data/alias-nested.zsh @@ -34,7 +34,6 @@ BUFFER='a foo; :' expected_region_highlight=( '1 1 alias' # a - '1 1 builtin' # a (:) '3 5 default' # foo '6 6 commandseparator' # ; '8 8 builtin' # : diff --git a/highlighters/main/test-data/alias-precommand-option-argument1.zsh b/highlighters/main/test-data/alias-precommand-option-argument1.zsh index ad16962..ecdd9f9 100644 --- a/highlighters/main/test-data/alias-precommand-option-argument1.zsh +++ b/highlighters/main/test-data/alias-precommand-option-argument1.zsh @@ -35,7 +35,6 @@ BUFFER='sdu phy1729 echo foo' expected_region_highlight=( '1 3 alias' # sdu - '1 3 precommand' # sdu (sudo) '5 11 default' # phy1729 '13 16 commmand "issue #540"' # echo (not builtin) '18 20 default' # foo diff --git a/highlighters/main/test-data/alias-precommand-option-argument2.zsh b/highlighters/main/test-data/alias-precommand-option-argument2.zsh index ebbb17b..95d71f8 100644 --- a/highlighters/main/test-data/alias-precommand-option-argument2.zsh +++ b/highlighters/main/test-data/alias-precommand-option-argument2.zsh @@ -36,7 +36,6 @@ BUFFER='sbu phy1729 echo foo' expected_region_highlight=( '1 3 alias' # sbu - '1 3 precommand' # sbu (sudo) '5 11 default' # phy1729 '13 16 commmand "issue #540"' # echo (not builtin) '18 20 default' # foo diff --git a/highlighters/main/test-data/alias-redirect.zsh b/highlighters/main/test-data/alias-redirect.zsh index a6a0aab..acc3dcb 100644 --- a/highlighters/main/test-data/alias-redirect.zsh +++ b/highlighters/main/test-data/alias-redirect.zsh @@ -32,7 +32,6 @@ BUFFER='x foo echo bar' expected_region_highlight=( '1 1 alias' # x - '1 1 redirection' # x (>) '3 5 default' # foo '7 10 builtin' # echo '12 14 default' # bar diff --git a/highlighters/main/test-data/alias-self.zsh b/highlighters/main/test-data/alias-self.zsh index 88ed3c8..c6f12b1 100644 --- a/highlighters/main/test-data/alias-self.zsh +++ b/highlighters/main/test-data/alias-self.zsh @@ -34,6 +34,5 @@ BUFFER='echo bar' expected_region_highlight=( '1 4 alias' # echo - '1 4 builtin' # echo '6 8 default' # bar ) diff --git a/highlighters/main/test-data/alias-to-dir.zsh b/highlighters/main/test-data/alias-to-dir.zsh index 93aaa62..767d3c7 100644 --- a/highlighters/main/test-data/alias-to-dir.zsh +++ b/highlighters/main/test-data/alias-to-dir.zsh @@ -32,6 +32,5 @@ alias x=/ BUFFER=$'x' expected_region_highlight=( - '1 1 alias' # x '1 1 unknown-token "issue #202"' # x (/) ) diff --git a/highlighters/main/test-data/alias.zsh b/highlighters/main/test-data/alias.zsh index 03ca0be..4cafa80 100644 --- a/highlighters/main/test-data/alias.zsh +++ b/highlighters/main/test-data/alias.zsh @@ -48,7 +48,6 @@ fi expected_region_highlight+=( "9 9 commandseparator" # ; "11 16 alias" # alias1 - "11 16 command" # alias1 (ls) "17 17 commandseparator" # ; "19 24 unknown-token" # alias2 ) diff --git a/highlighters/main/test-data/noglob-alias.zsh b/highlighters/main/test-data/noglob-alias.zsh index 5676b5a..2f96445 100644 --- a/highlighters/main/test-data/noglob-alias.zsh +++ b/highlighters/main/test-data/noglob-alias.zsh @@ -32,6 +32,5 @@ BUFFER='x ls' expected_region_highlight=( "1 1 alias" # x - "1 1 precommand" # x (command) "3 4 command" # ls ) diff --git a/highlighters/main/test-data/off-by-one.zsh b/highlighters/main/test-data/off-by-one.zsh index 7d6961a..872143a 100644 --- a/highlighters/main/test-data/off-by-one.zsh +++ b/highlighters/main/test-data/off-by-one.zsh @@ -34,7 +34,6 @@ BUFFER='a;f;' expected_region_highlight=( "1 1 alias" # a - "1 1 builtin" # a (:) "2 2 commandseparator" # ; "3 3 function" # f "4 4 commandseparator" # ;