Isolate setup code in functions to ease messing with zsh-syntax-highlighting
This commit is contained in:
parent
5003fa7c9b
commit
0e331cd654
|
@ -119,23 +119,20 @@ _zsh_highlight_cursor_moved()
|
||||||
|
|
||||||
|
|
||||||
# -------------------------------------------------------------------------------------------------
|
# -------------------------------------------------------------------------------------------------
|
||||||
# Setup
|
# Setup functions
|
||||||
# -------------------------------------------------------------------------------------------------
|
# -------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# 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.
|
# Load ZSH module zsh/zleparameter, needed to override user defined widgets.
|
||||||
zmodload zsh/zleparameter 2>/dev/null || {
|
zmodload zsh/zleparameter 2>/dev/null || {
|
||||||
echo 'zsh-syntax-highlighting: failed loading zsh/zleparameter, exiting.' >&2
|
echo 'zsh-syntax-highlighting: failed loading zsh/zleparameter.' >&2
|
||||||
return -1
|
return 1
|
||||||
}
|
|
||||||
|
|
||||||
# Resolve highlighters directory location.
|
|
||||||
highlighters_dir="${ZSH_HIGHLIGHT_HIGHLIGHTERS_DIR:-${0:h}/highlighters}"
|
|
||||||
[[ -d $highlighters_dir ]] || {
|
|
||||||
echo "zsh-syntax-highlighting: highlighters directory '$highlighters_dir' not found, exiting." >&2
|
|
||||||
return -1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Override ZLE widgets to make them invoke _zsh_highlight.
|
# Override ZLE widgets to make them invoke _zsh_highlight.
|
||||||
|
local cur_widget
|
||||||
for cur_widget in ${${(f)"$(builtin zle -la)"}:#(.*|orig-*|run-help|which-command|beep)}; do
|
for cur_widget in ${${(f)"$(builtin zle -la)"}:#(.*|orig-*|run-help|which-command|beep)}; do
|
||||||
case $widgets[$cur_widget] in
|
case $widgets[$cur_widget] in
|
||||||
|
|
||||||
|
@ -160,10 +157,23 @@ for cur_widget in ${${(f)"$(builtin zle -la)"}:#(.*|orig-*|run-help|which-comman
|
||||||
*) echo "zsh-syntax-highlighting: unhandled ZLE widget '$cur_widget'" >&2 ;;
|
*) echo "zsh-syntax-highlighting: unhandled ZLE widget '$cur_widget'" >&2 ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
unset cur_widget
|
}
|
||||||
|
|
||||||
|
# Load highlighters from directory.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# 1) Path to the highlighters directory.
|
||||||
|
_zsh_highlight_load_highlighters()
|
||||||
|
{
|
||||||
|
# Check the directory exists.
|
||||||
|
[[ -d "$1" ]] || {
|
||||||
|
echo "zsh-syntax-highlighting: highlighters directory '$1' not found." >&2
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
# Load highlighters from highlighters directory and check they define required functions.
|
# Load highlighters from highlighters directory and check they define required functions.
|
||||||
for highlighter_dir ($highlighters_dir/*/); do
|
local highlighter highlighter_dir
|
||||||
|
for highlighter_dir ($1/*/); do
|
||||||
highlighter="${highlighter_dir:t}"
|
highlighter="${highlighter_dir:t}"
|
||||||
[[ -f "$highlighter_dir/${highlighter}-highlighter.zsh" ]] && {
|
[[ -f "$highlighter_dir/${highlighter}-highlighter.zsh" ]] && {
|
||||||
. "$highlighter_dir/${highlighter}-highlighter.zsh"
|
. "$highlighter_dir/${highlighter}-highlighter.zsh"
|
||||||
|
@ -173,7 +183,24 @@ for highlighter_dir ($highlighters_dir/*/); do
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
done
|
done
|
||||||
unset highlighter highlighter_dir highlighters_dir
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# -------------------------------------------------------------------------------------------------
|
||||||
|
# Setup
|
||||||
|
# -------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# Try binding widgets.
|
||||||
|
_zsh_highlight_bind_widgets || {
|
||||||
|
echo 'zsh-syntax-highlighting: failed binding ZLE widgets, exiting.' >&2
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Resolve highlighters directory location.
|
||||||
|
_zsh_highlight_load_highlighters "${ZSH_HIGHLIGHT_HIGHLIGHTERS_DIR:-${0:h}/highlighters}" || {
|
||||||
|
echo 'zsh-syntax-highlighting: failed loading highlighters, exiting.' >&2
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
# Initialize the array of active highlighters if needed.
|
# Initialize the array of active highlighters if needed.
|
||||||
[[ $#ZSH_HIGHLIGHT_HIGHLIGHTERS -eq 0 ]] && ZSH_HIGHLIGHT_HIGHLIGHTERS=(main)
|
[[ $#ZSH_HIGHLIGHT_HIGHLIGHTERS -eq 0 ]] && ZSH_HIGHLIGHT_HIGHLIGHTERS=(main)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user