Add the function "zsh_highlight_main__test_type_builtin" also the change of PATH can't modify the behavior of the commands in "zsh_highlight_main__type"

In _zsh_highlight_main__type :
 - the calls to "builtin type -w -- $1" are replaced by call to "sh_highlight_main__test_type_builtin"
 - if ZSH_HIGHLIGHT_SPECIAL_PATH exists but is empty, "foo" in "sudo foo" is always red
 - if ZSH_HIGHLIGHT_SPECIAL_PATH is not empty, zsh searchs in the PATH "ZSH_HIGHLIGHT_SPECIAL_PATH" to know if the command "foo" in "sudo foo" exists
 - if "foo" is in ZSH_HIGHLIGHT_SPECIAL_COMMAND then "Admin" says that user has access to this command : foo in "sudo foo" is green
This commit is contained in:
raizo62 2019-03-31 19:52:19 +02:00
parent bc3f77f719
commit 70deca2e2c

View File

@ -127,6 +127,18 @@ _zsh_highlight_main_calculate_fallback() {
done done
} }
_zsh_highlight_main__test_type_builtin() {
if [[ -n ${ZSH_HIGHLIGHT_SPECIAL_PATH} ]]; then
if [ "$this_word" = ":sudo_opt::start:" ]; then
# ZSH_HIGHLIGHT_SPECIAL_PATH exist is not empty : "Admin" says that user has access to commands in these paths with sudo
local PATH="${ZSH_HIGHLIGHT_SPECIAL_PATH}"
fi
fi
builtin type -w -- $1
}
# Get the type of a command. # Get the type of a command.
# #
# Uses the zsh/parameter module if available to avoid forks, and a # Uses the zsh/parameter module if available to avoid forks, and a
@ -152,6 +164,24 @@ _zsh_highlight_main__type() {
fi fi
fi fi
if [[ -v ZSH_HIGHLIGHT_SPECIAL_COMMAND ]]; then
if (( ${ZSH_HIGHLIGHT_SPECIAL_COMMAND[(I)$1]} )); then
# ZSH_HIGHLIGHT_SPECIAL_COMMAND exists : if foo of "sudo foo" is in ZSH_HIGHLIGHT_SPECIAL_COMMAND then "Admin" says that user has access to this command
REPLY=command
return 0
fi
fi
if [[ -v ZSH_HIGHLIGHT_SPECIAL_PATH ]]; then
if [ "$this_word" = ":sudo_opt::start:" ]; then
if [ -z "${ZSH_HIGHLIGHT_SPECIAL_PATH}" ]; then
# ZSH_HIGHLIGHT_SPECIAL_PATH exist and is empty : "Admin" wants to say that user has no access to sudo : foo in "sudo foo" is red
REPLY=none
return 0
fi
fi
fi
# Main logic # Main logic
if (( $#options_to_set )); then if (( $#options_to_set )); then
setopt localoptions $options_to_set; setopt localoptions $options_to_set;
@ -186,7 +216,7 @@ _zsh_highlight_main__type() {
elif { [[ $1 != */* ]] || is-at-least 5.3 } && elif { [[ $1 != */* ]] || is-at-least 5.3 } &&
# Add a subshell to avoid a zsh upstream bug; see issue #606. # Add a subshell to avoid a zsh upstream bug; see issue #606.
# ### Remove the subshell when we stop supporting zsh 5.7.1 (I assume 5.8 will have the bugfix). # ### Remove the subshell when we stop supporting zsh 5.7.1 (I assume 5.8 will have the bugfix).
! (builtin type -w -- $1) >/dev/null 2>&1; then ! (_zsh_highlight_main__test_type_builtin $1) >/dev/null 2>&1; then
REPLY=none REPLY=none
fi fi
fi fi
@ -201,7 +231,7 @@ _zsh_highlight_main__type() {
# starts with an arithmetic expression [«((…))» as the first thing inside # starts with an arithmetic expression [«((…))» as the first thing inside
# «$(…)»], which is area that has had some parsing bugs before 5.6 # «$(…)»], which is area that has had some parsing bugs before 5.6
# (approximately). # (approximately).
REPLY="${$(:; (( aliases_allowed )) || unalias -- $1 2>/dev/null; LC_ALL=C builtin type -w -- $1 2>/dev/null)##*: }" REPLY="${$(:; (( aliases_allowed )) || unalias -- $1 2>/dev/null; LC_ALL=C _zsh_highlight_main__test_type_builtin $1 2>/dev/null)##*: }"
if [[ $REPLY == 'alias' ]]; then if [[ $REPLY == 'alias' ]]; then
may_cache=0 may_cache=0
fi fi