mirror of
https://github.com/zsh-users/zsh-syntax-highlighting.git
synced 2025-12-04 21:56:45 +01:00
synced up to latest
This commit is contained in:
commit
b46769b181
7 changed files with 172 additions and 67 deletions
|
|
@ -42,6 +42,50 @@ if true; then
|
|||
fi
|
||||
fi
|
||||
|
||||
# This function takes a single argument F and returns True iff F is an autoload stub.
|
||||
_zsh_highlight__function_is_autoload_stub_p() {
|
||||
if (( ${+functions} )); then
|
||||
## zsh/parameter is available
|
||||
#(( ${+functions[$1]} )) &&
|
||||
[[ "$functions[$1]" == *"builtin autoload -X" ]]
|
||||
else
|
||||
## zsh/parameter isn't available
|
||||
#[[ $(type -wa -- "$1") == *'function'* ]] &&
|
||||
[[ "${${(@f)"$(which -- "$1")"}[2]}" == $'\t'$histchars[3]' undefined' ]]
|
||||
fi
|
||||
# Do nothing here: return the exit code of the if.
|
||||
}
|
||||
|
||||
# Return True iff the argument denotes a function name.
|
||||
_zsh_highlight__is_function_p() {
|
||||
(( ${+functions[$1]} )) || [[ $(type -wa -- "$1") == *'function'* ]]
|
||||
}
|
||||
|
||||
# This function takes a single argument F and returns True iff F denotes the
|
||||
# name of a callable function. A function is callable if it is fully defined
|
||||
# or if it is marked for autoloading and autoloading it at the first call to it
|
||||
# will succeed. In particular, if a function has been marked for autoloading
|
||||
# but is not available in $fpath, then this function will return False therefor.
|
||||
#
|
||||
# See users/21671 http://www.zsh.org/cgi-bin/mla/redirect?USERNUMBER=21671
|
||||
_zsh_highlight__function_callable_p() {
|
||||
if _zsh_highlight__is_function_p "$1" &&
|
||||
! _zsh_highlight__function_is_autoload_stub_p "$1"
|
||||
then
|
||||
# Already fully loaded.
|
||||
return 0 # true
|
||||
else
|
||||
# "$1" is either an autoload stub, or not a function at all.
|
||||
#
|
||||
# Use a subshell to avoid affecting the calling shell.
|
||||
#
|
||||
# We expect 'autoload +X' to return non-zero if it fails to fully load
|
||||
# the function.
|
||||
( autoload -U +X -- "$1" 2>/dev/null )
|
||||
return $?
|
||||
fi
|
||||
}
|
||||
|
||||
# -------------------------------------------------------------------------------------------------
|
||||
# Core highlighting update system
|
||||
# -------------------------------------------------------------------------------------------------
|
||||
|
|
@ -255,71 +299,89 @@ _zsh_highlight_call_widget()
|
|||
_zsh_highlight
|
||||
}
|
||||
|
||||
# Rebind all ZLE widgets to make them invoke _zsh_highlights.
|
||||
_zsh_highlight_bind_widgets()
|
||||
{
|
||||
setopt localoptions noksharrays unset
|
||||
typeset -F SECONDS
|
||||
local prefix=orig-s$SECONDS-r$RANDOM # unique each time, in case we're sourced more than once
|
||||
|
||||
# Load ZSH module zsh/zleparameter, needed to override user defined widgets.
|
||||
zmodload zsh/zleparameter 2>/dev/null || {
|
||||
print -r -- >&2 'zsh-syntax-highlighting: failed loading zsh/zleparameter.'
|
||||
return 1
|
||||
if _zsh_highlight__function_callable_p add-zle-hook-widget
|
||||
then
|
||||
autoload -U add-zle-hook-widget
|
||||
_zsh_highlight__zle-line-finish() {
|
||||
# Reset $WIDGET since the 'main' highlighter depends on it.
|
||||
#
|
||||
# A nested function is required to hide zle parameters; see
|
||||
# "User-defined widgets" in zshall.
|
||||
() {
|
||||
local -h +r WIDGET=zle-line-finish
|
||||
_zsh_highlight "$@"
|
||||
} "$@"
|
||||
}
|
||||
_zsh_highlight_bind_widgets(){}
|
||||
add-zle-hook-widget zle-line-pre-redraw _zsh_highlight
|
||||
add-zle-hook-widget zle-line-finish _zsh_highlight__zle-line-finish
|
||||
else
|
||||
# Rebind all ZLE widgets to make them invoke _zsh_highlights.
|
||||
_zsh_highlight_bind_widgets()
|
||||
{
|
||||
setopt localoptions noksharrays
|
||||
typeset -F SECONDS
|
||||
local prefix=orig-s$SECONDS-r$RANDOM # unique each time, in case we're sourced more than once
|
||||
|
||||
# Load ZSH module zsh/zleparameter, needed to override user defined widgets.
|
||||
zmodload zsh/zleparameter 2>/dev/null || {
|
||||
print -r -- >&2 'zsh-syntax-highlighting: failed loading zsh/zleparameter.'
|
||||
return 1
|
||||
}
|
||||
|
||||
# Override ZLE widgets to make them invoke _zsh_highlight.
|
||||
local -U widgets_to_bind
|
||||
widgets_to_bind=(${${(k)widgets}:#(.*|run-help|which-command|beep|set-local-history|yank)})
|
||||
|
||||
# Always wrap special zle-line-finish widget. This is needed to decide if the
|
||||
# current line ends and special highlighting logic needs to be applied.
|
||||
# E.g. remove cursor imprint, don't highlight partial paths, ...
|
||||
widgets_to_bind+=(zle-line-finish)
|
||||
|
||||
# Always wrap special zle-isearch-update widget to be notified of updates in isearch.
|
||||
# This is needed because we need to disable highlighting in that case.
|
||||
widgets_to_bind+=(zle-isearch-update)
|
||||
|
||||
local cur_widget
|
||||
for cur_widget in $widgets_to_bind; do
|
||||
case $widgets[$cur_widget] in # requires no_unset be unset
|
||||
|
||||
# Already rebound event: do nothing.
|
||||
user:_zsh_highlight_widget_*);;
|
||||
|
||||
# The "eval"'s are required to make $cur_widget a closure: the value of the parameter at function
|
||||
# definition time is used.
|
||||
#
|
||||
# We can't use ${0/_zsh_highlight_widget_} because these widgets are always invoked with
|
||||
# NO_function_argzero, regardless of the option's setting here.
|
||||
|
||||
# User defined widget: override and rebind old one with prefix "orig-".
|
||||
user:*) zle -N $prefix-$cur_widget ${widgets[$cur_widget]#*:}
|
||||
eval "_zsh_highlight_widget_${(q)prefix}-${(q)cur_widget}() { _zsh_highlight_call_widget ${(q)prefix}-${(q)cur_widget} -- \"\$@\" }"
|
||||
zle -N $cur_widget _zsh_highlight_widget_$prefix-$cur_widget;;
|
||||
|
||||
# Completion widget: override and rebind old one with prefix "orig-".
|
||||
completion:*) zle -C $prefix-$cur_widget ${${(s.:.)widgets[$cur_widget]}[2,3]}
|
||||
eval "_zsh_highlight_widget_${(q)prefix}-${(q)cur_widget}() { _zsh_highlight_call_widget ${(q)prefix}-${(q)cur_widget} -- \"\$@\" }"
|
||||
zle -N $cur_widget _zsh_highlight_widget_$prefix-$cur_widget;;
|
||||
|
||||
# Builtin widget: override and make it call the builtin ".widget".
|
||||
builtin) eval "_zsh_highlight_widget_${(q)prefix}-${(q)cur_widget}() { _zsh_highlight_call_widget .${(q)cur_widget} -- \"\$@\" }"
|
||||
zle -N $cur_widget _zsh_highlight_widget_$prefix-$cur_widget;;
|
||||
|
||||
# Incomplete or nonexistent widget: Bind to z-sy-h directly.
|
||||
*)
|
||||
if [[ $cur_widget == zle-* ]] && [[ -z $widgets[$cur_widget] ]]; then
|
||||
_zsh_highlight_widget_${cur_widget}() { :; _zsh_highlight }
|
||||
zle -N $cur_widget _zsh_highlight_widget_$cur_widget
|
||||
else
|
||||
# Default: unhandled case.
|
||||
print -r -- >&2 "zsh-syntax-highlighting: unhandled ZLE widget ${(qq)cur_widget}"
|
||||
fi
|
||||
esac
|
||||
done
|
||||
}
|
||||
# Override ZLE widgets to make them invoke _zsh_highlight.
|
||||
local -U widgets_to_bind
|
||||
widgets_to_bind=(${${(k)widgets}:#(.*|run-help|which-command|beep|set-local-history|yank)})
|
||||
|
||||
# Always wrap special zle-line-finish widget. This is needed to decide if the
|
||||
# current line ends and special highlighting logic needs to be applied.
|
||||
# E.g. remove cursor imprint, don't highlight partial paths, ...
|
||||
widgets_to_bind+=(zle-line-finish)
|
||||
|
||||
# Always wrap special zle-isearch-update widget to be notified of updates in isearch.
|
||||
# This is needed because we need to disable highlighting in that case.
|
||||
widgets_to_bind+=(zle-isearch-update)
|
||||
|
||||
local cur_widget
|
||||
for cur_widget in $widgets_to_bind; do
|
||||
case $widgets[$cur_widget] in
|
||||
|
||||
# Already rebound event: do nothing.
|
||||
user:_zsh_highlight_widget_*);;
|
||||
|
||||
# The "eval"'s are required to make $cur_widget a closure: the value of the parameter at function
|
||||
# definition time is used.
|
||||
#
|
||||
# We can't use ${0/_zsh_highlight_widget_} because these widgets are always invoked with
|
||||
# NO_function_argzero, regardless of the option's setting here.
|
||||
|
||||
# User defined widget: override and rebind old one with prefix "orig-".
|
||||
user:*) zle -N $prefix-$cur_widget ${widgets[$cur_widget]#*:}
|
||||
eval "_zsh_highlight_widget_${(q)prefix}-${(q)cur_widget}() { _zsh_highlight_call_widget ${(q)prefix}-${(q)cur_widget} -- \"\$@\" }"
|
||||
zle -N $cur_widget _zsh_highlight_widget_$prefix-$cur_widget;;
|
||||
|
||||
# Completion widget: override and rebind old one with prefix "orig-".
|
||||
completion:*) zle -C $prefix-$cur_widget ${${(s.:.)widgets[$cur_widget]}[2,3]}
|
||||
eval "_zsh_highlight_widget_${(q)prefix}-${(q)cur_widget}() { _zsh_highlight_call_widget ${(q)prefix}-${(q)cur_widget} -- \"\$@\" }"
|
||||
zle -N $cur_widget _zsh_highlight_widget_$prefix-$cur_widget;;
|
||||
|
||||
# Builtin widget: override and make it call the builtin ".widget".
|
||||
builtin) eval "_zsh_highlight_widget_${(q)prefix}-${(q)cur_widget}() { _zsh_highlight_call_widget .${(q)cur_widget} -- \"\$@\" }"
|
||||
zle -N $cur_widget _zsh_highlight_widget_$prefix-$cur_widget;;
|
||||
|
||||
# Incomplete or nonexistent widget: Bind to z-sy-h directly.
|
||||
*)
|
||||
if [[ $cur_widget == zle-* ]] && [[ -z $widgets[$cur_widget] ]]; then
|
||||
_zsh_highlight_widget_${cur_widget}() { :; _zsh_highlight }
|
||||
zle -N $cur_widget _zsh_highlight_widget_$cur_widget
|
||||
else
|
||||
# Default: unhandled case.
|
||||
print -r -- >&2 "zsh-syntax-highlighting: unhandled ZLE widget ${(qq)cur_widget}"
|
||||
fi
|
||||
esac
|
||||
done
|
||||
}
|
||||
fi
|
||||
|
||||
# Load highlighters from directory.
|
||||
#
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue