Update widget-remap code from zsh-syntax-highlighting

This brings in the several improvements since the code was initially
added for compatibility. In particular, this fixes widget conflicts with
existing functions and executables by prefixing all redefined widgets
with "_zsh_highlight_widget_".
This commit is contained in:
Aaron Hall 2012-01-10 21:18:57 -06:00 committed by Suraj N. Kurapati
parent ba2e1a76aa
commit 68904e1d72

View file

@ -109,12 +109,9 @@ if [[ $+functions[_zsh_highlight] -eq 0 ]]; then
zle -N self-insert ordinary-key-press zle -N self-insert ordinary-key-press
# #
# Override ZLE widgets to invoke _zsh_highlight() # The following snippet was taken from the zsh-syntax-highlighting project:
# #
# https://github.com/nicoulaj/zsh-syntax-highlighting/blob/ # https://github.com/zsh-users/zsh-syntax-highlighting/blob/56b134f5d62ae3d4e66c7f52bd0cc2595f9b305b/zsh-syntax-highlighting.zsh#L126-161
# bb7fcb79fad797a40077bebaf6f4e4a93c9d8163/zsh-syntax-highlighting.zsh#L121
#
#--------------8<-------------------8<-------------------8<-----------------
# #
# Copyright (c) 2010-2011 zsh-syntax-highlighting contributors # Copyright (c) 2010-2011 zsh-syntax-highlighting contributors
# All rights reserved. # All rights reserved.
@ -145,45 +142,47 @@ if [[ $+functions[_zsh_highlight] -eq 0 ]]; then
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#--------------8<-------------------8<-------------------8<-----------------
# Rebind all ZLE widgets to make them invoke _zsh_highlights.
_zsh_highlight_bind_widgets()
{
# Load ZSH module zsh/zleparameter, needed to override user defined widgets.
zmodload zsh/zleparameter 2>/dev/null || {
echo 'zsh-syntax-highlighting: failed loading zsh/zleparameter.' >&2
return 1
}
# Load ZSH module zsh/zleparameter, needed to override user defined widgets. # Override ZLE widgets to make them invoke _zsh_highlight.
zmodload zsh/zleparameter 2>/dev/null || { local cur_widget
echo 'zsh-syntax-highlighting: failed loading zsh/zleparameter, exiting.' >&2 for cur_widget in ${${(f)"$(builtin zle -la)"}:#(.*|_*|orig-*|run-help|which-command|beep)}; do
return -1 case $widgets[$cur_widget] in
}
# Override ZLE widgets to make them invoke _zsh_highlight. # Already rebound event: do nothing.
for event in ${${(f)"$(zle -la)"}:#(_*|orig-*|.run-help|.which-command)}; do user:$cur_widget|user:_zsh_highlight_widget_*);;
if [[ "$widgets[$event]" == completion:* ]]; then
eval "zle -C orig-$event ${${${widgets[$event]}#*:}/:/ } ; $event() { builtin zle orig-$event && _zsh_highlight } ; zle -N $event"
else
case $event in
accept-and-menu-complete)
eval "$event() { builtin zle .$event && _zsh_highlight } ; zle -N $event"
;;
# The following widgets should NOT remove any previously # User defined widget: override and rebind old one with prefix "orig-".
# applied highlighting. Therefore we do not remap them. user:*) eval "zle -N orig-$cur_widget ${widgets[$cur_widget]#*:}; \
.forward-char|.backward-char|.up-line-or-history|.down-line-or-history) _zsh_highlight_widget_$cur_widget() { builtin zle orig-$cur_widget -- \"\$@\" && _zsh_highlight }; \
;; zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
.*) # Completion widget: override and rebind old one with prefix "orig-".
clean_event=$event[2,${#event}] # Remove the leading dot in the event name completion:*) eval "zle -C orig-$cur_widget ${${widgets[$cur_widget]#*:}/:/ }; \
case ${widgets[$clean_event]-} in _zsh_highlight_widget_$cur_widget() { builtin zle orig-$cur_widget -- \"\$@\" && _zsh_highlight }; \
(completion|user):*) zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
;;
*) # Builtin widget: override and make it call the builtin ".widget".
eval "$clean_event() { builtin zle $event && _zsh_highlight } ; zle -N $clean_event" builtin) eval "_zsh_highlight_widget_$cur_widget() { builtin zle .$cur_widget -- \"\$@\" && _zsh_highlight }; \
;; zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
esac
;; # Default: unhandled case.
*) *) echo "zsh-syntax-highlighting: unhandled ZLE widget '$cur_widget'" >&2 ;;
;;
esac esac
fi done
done }
unset event clean_event
#-------------->8------------------->8------------------->8----------------- #-------------->8------------------->8------------------->8-----------------
_zsh_highlight_bind_widgets
fi fi
function _history-substring-search-begin() { function _history-substring-search-begin() {