Compare commits
20 Commits
master
...
feature/re
Author | SHA1 | Date | |
---|---|---|---|
|
b08d508cd8 | ||
|
8d4c6355e6 | ||
|
56ba7f082d | ||
|
2cbb3fb24e | ||
|
f265ef0b9a | ||
|
d0fb0df4ff | ||
|
f665eec230 | ||
|
a868b6942e | ||
|
b5249f17ab | ||
|
d2594c1157 | ||
|
66ae59eccc | ||
|
1651137f5c | ||
|
d4ab7e51d2 | ||
|
38477f2a3d | ||
|
d98622dcd0 | ||
|
04fd6bbf53 | ||
|
30c6e70394 | ||
|
74a27de70d | ||
|
85e62a8171 | ||
|
aed99f6a3e |
18
README.md
18
README.md
@ -39,11 +39,23 @@ FAQ
|
|||||||
|
|
||||||
### Why must `zsh-syntax-highlighting.zsh` be sourced at the end of the `.zshrc` file?
|
### Why must `zsh-syntax-highlighting.zsh` be sourced at the end of the `.zshrc` file?
|
||||||
|
|
||||||
`zsh-syntax-highlighting.zsh` wraps ZLE widgets. It must be sourced after all
|
zsh-syntax-highlighting works by hooking into the Zsh Line Editor (ZLE) and
|
||||||
custom widgets have been created (i.e., after all `zle -N` calls and after
|
computing syntax highlighting for the command-line buffer as it stands at the
|
||||||
running `compinit`). Widgets created later will work, but will not update the
|
time z-sy-h's hook is invoked.
|
||||||
|
|
||||||
|
In zsh 5.2 and older,
|
||||||
|
`zsh-syntax-highlighting.zsh` hooks into ZLE by wrapping ZLE widgets. It must
|
||||||
|
be sourced after all custom widgets have been created (i.e., after all `zle -N`
|
||||||
|
calls and after running `compinit`) in order to be able to wrap all of them.
|
||||||
|
Widgets created after z-sy-h is sourced will work, but will not update the
|
||||||
syntax highlighting.
|
syntax highlighting.
|
||||||
|
|
||||||
|
In zsh 5.3 and newer,
|
||||||
|
zsh-syntax-highlighting uses the `add-zle-hook-widget` facility to install
|
||||||
|
a `zle-line-pre-redraw` hook. Hooks are run in order of registration,
|
||||||
|
therefore, z-sy-h must be sourced (and register its hook) after anything else
|
||||||
|
that adds hooks that modify the command-line buffer.
|
||||||
|
|
||||||
### Does syntax highlighting work during incremental history search?
|
### Does syntax highlighting work during incremental history search?
|
||||||
|
|
||||||
Highlighting the command line during an incremental history search (by default bound to
|
Highlighting the command line during an incremental history search (by default bound to
|
||||||
|
22
changelog.md
22
changelog.md
@ -292,6 +292,28 @@ in this area.
|
|||||||
(0a9b347483ae)
|
(0a9b347483ae)
|
||||||
|
|
||||||
|
|
||||||
|
# Changes in version 0.5.0
|
||||||
|
|
||||||
|
|
||||||
|
## Incompatible changes:
|
||||||
|
|
||||||
|
- An unsuccessful completion (a <kbd>⮀ Tab</kbd> press that doesn't change the
|
||||||
|
command line) no longer causes highlighting to be lost. Visual feedback can
|
||||||
|
alternatively be achieved by setting the `format` zstyle under the `warnings`
|
||||||
|
tag, for example,
|
||||||
|
|
||||||
|
zstyle ':completion:*:warnings' format '%F{red}No matches%f'
|
||||||
|
|
||||||
|
Refer to the [description of the `format` style in `zshcompsys(1)`]
|
||||||
|
[zshcompsys-Standard-Styles-format].
|
||||||
|
|
||||||
|
(#90, part of #245, XXXXXXXXXXXX)
|
||||||
|
|
||||||
|
[zshcompsys-Standard-Styles]: http://zsh.sourceforge.net/Doc/Release/Completion-System.html#Standard-Styles
|
||||||
|
[zshcompsys-Standard-Styles-format]: http://zsh.sourceforge.net/Doc/Release/Completion-System.html#index-format_002c-completion-style
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Changes in version 0.4.1
|
# Changes in version 0.4.1
|
||||||
|
|
||||||
## Fixes:
|
## Fixes:
|
||||||
|
@ -31,6 +31,9 @@
|
|||||||
emulate -LR zsh
|
emulate -LR zsh
|
||||||
setopt localoptions extendedglob
|
setopt localoptions extendedglob
|
||||||
|
|
||||||
|
# Required for add-zle-hook-widget.
|
||||||
|
zmodload zsh/zle
|
||||||
|
|
||||||
# Argument parsing.
|
# Argument parsing.
|
||||||
if (( $# != 3 )) || [[ $1 == -* ]]; then
|
if (( $# != 3 )) || [[ $1 == -* ]]; then
|
||||||
print -r -- >&2 "$0: usage: $0 BUFFER HIGHLIGHTER BASENAME"
|
print -r -- >&2 "$0: usage: $0 BUFFER HIGHLIGHTER BASENAME"
|
||||||
|
@ -31,6 +31,9 @@
|
|||||||
|
|
||||||
setopt NO_UNSET WARN_CREATE_GLOBAL
|
setopt NO_UNSET WARN_CREATE_GLOBAL
|
||||||
|
|
||||||
|
# Required for add-zle-hook-widget.
|
||||||
|
zmodload zsh/zle
|
||||||
|
|
||||||
# Check an highlighter was given as argument.
|
# Check an highlighter was given as argument.
|
||||||
[[ -n "$1" ]] || {
|
[[ -n "$1" ]] || {
|
||||||
echo >&2 "Bail out! You must provide the name of a valid highlighter as argument."
|
echo >&2 "Bail out! You must provide the name of a valid highlighter as argument."
|
||||||
|
@ -29,6 +29,9 @@
|
|||||||
# -------------------------------------------------------------------------------------------------
|
# -------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
# Required for add-zle-hook-widget.
|
||||||
|
zmodload zsh/zle
|
||||||
|
|
||||||
# Check an highlighter was given as argument.
|
# Check an highlighter was given as argument.
|
||||||
[[ -n "$1" ]] || {
|
[[ -n "$1" ]] || {
|
||||||
echo >&2 "Bail out! You must provide the name of a valid highlighter as argument."
|
echo >&2 "Bail out! You must provide the name of a valid highlighter as argument."
|
||||||
|
@ -49,6 +49,52 @@ if true; then
|
|||||||
fi
|
fi
|
||||||
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 zmodload -e zsh/parameter; then
|
||||||
|
#(( ${+functions[$1]} )) &&
|
||||||
|
[[ "$functions[$1]" == *"builtin autoload -X"* ]]
|
||||||
|
else
|
||||||
|
#[[ $(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() {
|
||||||
|
if zmodload -e zsh/parameter; then
|
||||||
|
(( ${+functions[$1]} ))
|
||||||
|
else
|
||||||
|
[[ $(type -wa -- "$1") == *'function'* ]]
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# 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
|
# Core highlighting update system
|
||||||
# -------------------------------------------------------------------------------------------------
|
# -------------------------------------------------------------------------------------------------
|
||||||
@ -290,72 +336,99 @@ _zsh_highlight_call_widget()
|
|||||||
_zsh_highlight
|
_zsh_highlight
|
||||||
}
|
}
|
||||||
|
|
||||||
# Rebind all ZLE widgets to make them invoke _zsh_highlights.
|
if _zsh_highlight__function_callable_p add-zle-hook-widget
|
||||||
_zsh_highlight_bind_widgets()
|
then
|
||||||
{
|
autoload -U add-zle-hook-widget
|
||||||
setopt localoptions noksharrays
|
_zsh_highlight__zle-line-finish() {
|
||||||
typeset -F SECONDS
|
# Reset $WIDGET since the 'main' highlighter depends on it.
|
||||||
local prefix=orig-s$SECONDS-r$RANDOM # unique each time, in case we're sourced more than once
|
#
|
||||||
|
# Since $WIDGET is declared by zle as read-only in this function's scope,
|
||||||
# Load ZSH module zsh/zleparameter, needed to override user defined widgets.
|
# a nested function is required in order to shadow its built-in value;
|
||||||
zmodload zsh/zleparameter 2>/dev/null || {
|
# see "User-defined widgets" in zshall.
|
||||||
print -r -- >&2 'zsh-syntax-highlighting: failed loading zsh/zleparameter.'
|
() {
|
||||||
return 1
|
local -h -r WIDGET=zle-line-finish
|
||||||
|
_zsh_highlight
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
_zsh_highlight__zle-line-pre-redraw() {
|
||||||
|
# Set $? to 0 for _zsh_highlight. Without this, subsequent
|
||||||
|
# zle-line-pre-redraw hooks won't run, since add-zle-hook-widget happens to
|
||||||
|
# call us with $? == 1 in the common case.
|
||||||
|
true && _zsh_highlight "$@"
|
||||||
|
}
|
||||||
|
_zsh_highlight_bind_widgets(){}
|
||||||
|
if [[ -o zle ]]; then
|
||||||
|
add-zle-hook-widget zle-line-pre-redraw _zsh_highlight__zle-line-pre-redraw
|
||||||
|
add-zle-hook-widget zle-line-finish _zsh_highlight__zle-line-finish
|
||||||
|
fi
|
||||||
|
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
|
||||||
|
|
||||||
# Override ZLE widgets to make them invoke _zsh_highlight.
|
# Load ZSH module zsh/zleparameter, needed to override user defined widgets.
|
||||||
local -U widgets_to_bind
|
zmodload zsh/zleparameter 2>/dev/null || {
|
||||||
widgets_to_bind=(${${(k)widgets}:#(.*|run-help|which-command|beep|set-local-history|yank|yank-pop)})
|
print -r -- >&2 'zsh-syntax-highlighting: failed loading zsh/zleparameter.'
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
# Always wrap special zle-line-finish widget. This is needed to decide if the
|
# Override ZLE widgets to make them invoke _zsh_highlight.
|
||||||
# current line ends and special highlighting logic needs to be applied.
|
local -U widgets_to_bind
|
||||||
# E.g. remove cursor imprint, don't highlight partial paths, ...
|
widgets_to_bind=(${${(k)widgets}:#(.*|run-help|which-command|beep|set-local-history|yank|yank-pop)})
|
||||||
widgets_to_bind+=(zle-line-finish)
|
|
||||||
|
|
||||||
# Always wrap special zle-isearch-update widget to be notified of updates in isearch.
|
# Always wrap special zle-line-finish widget. This is needed to decide if the
|
||||||
# This is needed because we need to disable highlighting in that case.
|
# current line ends and special highlighting logic needs to be applied.
|
||||||
widgets_to_bind+=(zle-isearch-update)
|
# E.g. remove cursor imprint, don't highlight partial paths, ...
|
||||||
|
widgets_to_bind+=(zle-line-finish)
|
||||||
|
|
||||||
local cur_widget
|
# Always wrap special zle-isearch-update widget to be notified of updates in isearch.
|
||||||
for cur_widget in $widgets_to_bind; do
|
# This is needed because we need to disable highlighting in that case.
|
||||||
case ${widgets[$cur_widget]:-""} in
|
widgets_to_bind+=(zle-isearch-update)
|
||||||
|
|
||||||
# Already rebound event: do nothing.
|
local cur_widget
|
||||||
user:_zsh_highlight_widget_*);;
|
for cur_widget in $widgets_to_bind; do
|
||||||
|
case ${widgets[$cur_widget]:-""} in
|
||||||
|
|
||||||
# The "eval"'s are required to make $cur_widget a closure: the value of the parameter at function
|
# Already rebound event: do nothing.
|
||||||
# definition time is used.
|
user:_zsh_highlight_widget_*);;
|
||||||
#
|
|
||||||
# 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-".
|
# The "eval"'s are required to make $cur_widget a closure: the value of the parameter at function
|
||||||
user:*) zle -N $prefix-$cur_widget ${widgets[$cur_widget]#*:}
|
# definition time is used.
|
||||||
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;;
|
# 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.
|
||||||
|
|
||||||
# Completion widget: override and rebind old one with prefix "orig-".
|
# User defined widget: override and rebind old one with prefix "orig-".
|
||||||
completion:*) zle -C $prefix-$cur_widget ${${(s.:.)widgets[$cur_widget]}[2,3]}
|
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} -- \"\$@\" }"
|
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;;
|
zle -N $cur_widget _zsh_highlight_widget_$prefix-$cur_widget;;
|
||||||
|
|
||||||
# Builtin widget: override and make it call the builtin ".widget".
|
# Completion widget: override and rebind old one with prefix "orig-".
|
||||||
builtin) eval "_zsh_highlight_widget_${(q)prefix}-${(q)cur_widget}() { _zsh_highlight_call_widget .${(q)cur_widget} -- \"\$@\" }"
|
completion:*) zle -C $prefix-$cur_widget ${${(s.:.)widgets[$cur_widget]}[2,3]}
|
||||||
zle -N $cur_widget _zsh_highlight_widget_$prefix-$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;;
|
||||||
|
|
||||||
# Incomplete or nonexistent widget: Bind to z-sy-h directly.
|
# 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} -- \"\$@\" }"
|
||||||
if [[ $cur_widget == zle-* ]] && (( ! ${+widgets[$cur_widget]} )); then
|
zle -N $cur_widget _zsh_highlight_widget_$prefix-$cur_widget;;
|
||||||
_zsh_highlight_widget_${cur_widget}() { :; _zsh_highlight }
|
|
||||||
zle -N $cur_widget _zsh_highlight_widget_$cur_widget
|
# Incomplete or nonexistent widget: Bind to z-sy-h directly.
|
||||||
else
|
*)
|
||||||
# Default: unhandled case.
|
if [[ $cur_widget == zle-* ]] && (( ! ${+widgets[$cur_widget]} )); then
|
||||||
print -r -- >&2 "zsh-syntax-highlighting: unhandled ZLE widget ${(qq)cur_widget}"
|
_zsh_highlight_widget_${cur_widget}() { :; _zsh_highlight }
|
||||||
print -r -- >&2 "zsh-syntax-highlighting: (This is sometimes caused by doing \`bindkey <keys> ${(q-)cur_widget}\` without creating the ${(qq)cur_widget} widget with \`zle -N\` or \`zle -C\`.)"
|
zle -N $cur_widget _zsh_highlight_widget_$cur_widget
|
||||||
fi
|
else
|
||||||
esac
|
# Default: unhandled case.
|
||||||
done
|
print -r -- >&2 "zsh-syntax-highlighting: unhandled ZLE widget ${(qq)cur_widget}"
|
||||||
}
|
print -r -- >&2 "zsh-syntax-highlighting: (This is sometimes caused by doing \`bindkey <keys> ${(q-)cur_widget}\` without creating the ${(qq)cur_widget} widget with \`zle -N\` or \`zle -C\`.)"
|
||||||
|
fi
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
}
|
||||||
|
fi
|
||||||
|
|
||||||
# Load highlighters from directory.
|
# Load highlighters from directory.
|
||||||
#
|
#
|
||||||
|
Loading…
Reference in New Issue
Block a user