diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 1e0af52..f3cfe43 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -127,20 +127,43 @@ _zsh_highlight_main_calculate_fallback() { # # Takes a single argument. # +# Uses the following caller variables: [[ $this_word == *':alias:'* ]] +# # The result will be stored in REPLY. _zsh_highlight_main__type() { + # Our caller knows whether aliases are allowed at this point. Compare: + # % ls + # % sudo ls + if [[ $this_word == *':alias:'* ]]; then + integer -r aliases_allowed=1 + else + integer -r aliases_allowed=0 + fi + # For the same reason, we won't cache replies of anything that exists as an + # alias at all, regardless of $aliases_allowed. + # + # ### We probably _should_ cache them in a cache that's keyed on the value of + # ### $aliases_allowed, on the assumption that aliases are the common case. + integer may_cache=1 + + # Cache lookup if (( $+_zsh_highlight_main__command_type_cache )); then REPLY=$_zsh_highlight_main__command_type_cache[(e)$1] if [[ -n "$REPLY" ]]; then return fi fi + + # Main logic if (( $#options_to_set )); then setopt localoptions $options_to_set; fi unset REPLY if zmodload -e zsh/parameter; then if (( $+aliases[(e)$1] )); then + may_cache=0 + fi + if (( $+aliases[(e)$1] )) && (( aliases_allowed )); then REPLY=alias elif (( $+saliases[(e)${1##*.}] )); then REPLY='suffix alias' @@ -165,9 +188,21 @@ _zsh_highlight_main__type() { fi if ! (( $+REPLY )); then # Note that 'type -w' will run 'rehash' implicitly. - REPLY="${$(LC_ALL=C builtin type -w -- $1 2>/dev/null)##*: }" + # + # We 'unalias' in a subshell, so the parent shell is not affected. + # + # The colon command is there just to avoid a command substitution that + # starts with an arithmetic expression [«((…))» as the first thing inside + # «$(…)»], which is area that has had some parsing bugs before 5.6 + # (approximately). + REPLY="${$(:; (( aliases_allowed )) || unalias -- $1;LC_ALL=C builtin type -w -- $1 2>/dev/null)##*: }" + if [[ $REPLY == 'alias' ]]; then + may_cache=0 + fi fi - if (( $+_zsh_highlight_main__command_type_cache )); then + + # Cache population + if (( may_cache )) && (( $+_zsh_highlight_main__command_type_cache )); then _zsh_highlight_main__command_type_cache[(e)$1]=$REPLY fi } diff --git a/highlighters/main/test-data/alias-after-precommand.zsh b/highlighters/main/test-data/alias-after-precommand.zsh new file mode 100644 index 0000000..6a71759 --- /dev/null +++ b/highlighters/main/test-data/alias-after-precommand.zsh @@ -0,0 +1,42 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2018 zsh-syntax-highlighting contributors +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, are permitted +# provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, this list of conditions +# and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, this list of +# conditions and the following disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors +# may be used to endorse or promote products derived from this software without specific prior +# written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# ------------------------------------------------------------------------------------------------- +# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*- +# vim: ft=zsh sw=2 ts=2 et +# ------------------------------------------------------------------------------------------------- + +alias ls='ls -l' +BUFFER=$'ls; sudo ls; noglob ls' + +expected_region_highlight=( + '1 2 alias' # ls + '3 3 commandseparator' # ; + '5 8 precommand' # sudo + '10 11 command' # ls + '12 12 commandseparator' # ; + '14 19 precommand' # noglob + '21 22 alias' # ls +)