mirror of
https://github.com/zsh-users/zsh-syntax-highlighting.git
synced 2025-02-13 10:05:31 +08:00
Merge remote-tracking branch 'upstream/pr/356'
Fixes #356 (the pull request being merged). Fixes #245 (the meta-issue for the following redrawhook issues). Fixes #40. Fixes #90. Fixes #150. Fixes #154. Fixes #183. Fixes #377. * upstream/pr/356: test harness: Actually test the new code. driver: Rewrite without a state variable noop: Make a whitespace-only change to reduce noise in the next commit. docs: Rewrap. docs: Update FAQ answer per changes on this branch. redo _zsh_highlight__function_callable_p driver: Use a different way of checking whether add-zle-hook-widget is present. changelog: Use a more specific link. changelog: Note the effect of fixing #245/#90 and an alternative. driver: Pass zle-line-finish arguments on to _zsh_highlight. driver: Hook zle-line-finish. driver: Reimplement using 'add-zle-hook-widget zle-line-pre-redraw' wrappers: Reimplement using Mikachu's zle-line-pre-redraw hook (workers/36650).
This commit is contained in:
commit
8dbe1209ca
18
README.md
18
README.md
@ -27,11 +27,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
|
Highlighting the command line during an incremental history search
|
||||||
|
22
changelog.md
22
changelog.md
@ -3,6 +3,28 @@ up to 952a97dbc99a54bd86141b7a57625e748941a937 + 0.4.1
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 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"
|
||||||
|
@ -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."
|
||||||
|
@ -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."
|
||||||
|
@ -42,6 +42,50 @@ 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 (( ${+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
|
# Core highlighting update system
|
||||||
# -------------------------------------------------------------------------------------------------
|
# -------------------------------------------------------------------------------------------------
|
||||||
@ -249,9 +293,26 @@ _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
|
||||||
|
_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
|
setopt localoptions noksharrays
|
||||||
typeset -F SECONDS
|
typeset -F SECONDS
|
||||||
local prefix=orig-s$SECONDS-r$RANDOM # unique each time, in case we're sourced more than once
|
local prefix=orig-s$SECONDS-r$RANDOM # unique each time, in case we're sourced more than once
|
||||||
@ -313,7 +374,8 @@ _zsh_highlight_bind_widgets()
|
|||||||
fi
|
fi
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
fi
|
||||||
|
|
||||||
# Load highlighters from directory.
|
# Load highlighters from directory.
|
||||||
#
|
#
|
||||||
|
Loading…
Reference in New Issue
Block a user