From d937cee71bcc29491ed04fc37d8f44ccc7e1a842 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Wed, 10 Oct 2018 09:45:47 +0000 Subject: [PATCH] 'main': Add :alias: state. --- highlighters/main/main-highlighter.zsh | 29 +++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 5f23357..1e0af52 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -319,6 +319,7 @@ _zsh_highlight_main_highlighter_highlight_list() # # The states are: # - :start: Command word + # - :alias: :start: and alias expansion is allowed # - :sudo_opt: A leading-dash option to sudo (such as "-u" or "-i") # - :sudo_arg: The argument to a sudo leading-dash option that takes one, # when given as a separate word; i.e., "foo" in "-u foo" (two @@ -351,7 +352,7 @@ _zsh_highlight_main_highlighter_highlight_list() # $in_redirection. The value of $next_word from the iteration that processed # the operator is discarded. # - local this_word=':start:' next_word + local this_word=':start::alias:' next_word integer in_redirection # Processing buffer local proc_buf="$buf" @@ -386,7 +387,7 @@ _zsh_highlight_main_highlighter_highlight_list() # the string's color. integer already_added=0 style=unknown-token - if [[ $this_word == *':start:'* ]]; then + if [[ $this_word == *':alias:'* ]]; then in_array_assignment=false if [[ $arg == 'noglob' ]]; then highlight_glob=false @@ -485,9 +486,11 @@ _zsh_highlight_main_highlighter_highlight_list() case "$arg" in # Flag that requires an argument '-'[Cgprtu]) this_word=${this_word//:start:/}; + this_word=${this_word//:alias:/}; next_word=':sudo_arg:';; # This prevents misbehavior with sudo -u -otherargument '-'*) this_word=${this_word//:start:/}; + this_word=${this_word//:alias:/}; next_word+=':start:'; next_word+=':sudo_opt:';; *) ;; @@ -502,13 +505,14 @@ _zsh_highlight_main_highlighter_highlight_list() if [[ $this_word == *':always:'* && $arg == 'always' ]]; then # try-always construct style=reserved-word # de facto a reserved word, although not de jure - next_word=':start:' + next_word=':start:' # :alias: will be added to $next_word when $this_word is \x7b. elif [[ $this_word == *':start:'* ]] && (( in_redirection == 0 )); then # $arg is the command word if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]]; then style=precommand elif [[ "$arg" = "sudo" ]] && { _zsh_highlight_main__type sudo; [[ -n $REPLY && $REPLY != "none" ]] }; then style=precommand next_word=${next_word//:regular:/} + next_word=${next_word//:alias:/} next_word+=':sudo_opt:' next_word+=':start:' else @@ -541,6 +545,7 @@ _zsh_highlight_main_highlighter_highlight_list() case $arg in ($'\x7b') braces_stack='Y'"$braces_stack" + next_word+=':alias:' ;; ($'\x7d') # We're at command word, so no need to check $right_brace_is_recognised_everywhere @@ -627,6 +632,7 @@ _zsh_highlight_main_highlighter_highlight_list() # assignment to a scalar parameter. # (For array assignments, the command doesn't start until the ")" token.) next_word+=':start:' + next_word+=':alias:' if (( start_pos + i <= end_pos )); then () { local highlight_glob=false @@ -704,18 +710,21 @@ _zsh_highlight_main_highlighter_highlight_list() style=assign in_array_assignment=false next_word+=':start:' + next_word+=':alias:' else if _zsh_highlight_main__stack_pop 'S'; then REPLY=$start_pos reply=($list_highlights) return 0 fi + # TODO: next_word can only be a command separator now _zsh_highlight_main__stack_pop 'R' reserved-word fi;; $'\x28\x29') # possibly a function definition if [[ $zsyh_user_options[multifuncdef] == on ]] || false # TODO: or if the previous word was a command word then next_word+=':start:' + next_word+=':alias:' fi style=reserved-word ;; @@ -755,12 +764,18 @@ _zsh_highlight_main_highlighter_highlight_list() next_word=':regular:' else next_word=':start:' + next_word+=':alias:' highlight_glob=true fi - elif - [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW:#"$arg"} && $this_word == *':start:'* ]] || - [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} && $this_word == *':start:'* ]]; then + elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW:#"$arg"} && $this_word == *':start:'* ]]; then next_word=':start:' + next_word+=':alias:' + elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} && $this_word == *':start:'* ]]; then + next_word=':start:' + # Special-case two reserved words that _can_ be followed by aliases + if [[ $arg == ('noglob'|'nocorrect') ]]; then + next_word+=':alias:' + fi elif [[ $arg == "repeat" && $this_word == *':start:'* ]]; then # skip the repeat-count word in_redirection=2 @@ -771,7 +786,7 @@ _zsh_highlight_main_highlighter_highlight_list() # or a command separator (`repeat 2; ls` or `repeat 2; do ls; done`). # # The repeat-count word will be handled like a redirection target. - this_word=':start::regular:' + this_word=':start::alias::regular:' fi start_pos=$end_pos if (( in_redirection == 0 )); then