Isolate setup code in functions to ease messing with zsh-syntax-highlighting

This commit is contained in:
Julien Nicoulaud 2011-07-31 17:59:55 +02:00
parent 5003fa7c9b
commit 0e331cd654

View File

@ -119,24 +119,21 @@ _zsh_highlight_cursor_moved()
# ------------------------------------------------------------------------------------------------- # -------------------------------------------------------------------------------------------------
# Setup # Setup functions
# ------------------------------------------------------------------------------------------------- # -------------------------------------------------------------------------------------------------
# Load ZSH module zsh/zleparameter, needed to override user defined widgets. # Rebind all ZLE widgets to make them invoke _zsh_highlights.
zmodload zsh/zleparameter 2>/dev/null || { _zsh_highlight_bind_widgets()
echo 'zsh-syntax-highlighting: failed loading zsh/zleparameter, exiting.' >&2 {
return -1 # 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
}
# Resolve highlighters directory location. # Override ZLE widgets to make them invoke _zsh_highlight.
highlighters_dir="${ZSH_HIGHLIGHT_HIGHLIGHTERS_DIR:-${0:h}/highlighters}" local cur_widget
[[ -d $highlighters_dir ]] || { for cur_widget in ${${(f)"$(builtin zle -la)"}:#(.*|orig-*|run-help|which-command|beep)}; do
echo "zsh-syntax-highlighting: highlighters directory '$highlighters_dir' not found, exiting." >&2
return -1
}
# Override ZLE widgets to make them invoke _zsh_highlight.
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
# Already rebound event: do nothing. # Already rebound event: do nothing.
@ -159,11 +156,24 @@ for cur_widget in ${${(f)"$(builtin zle -la)"}:#(.*|orig-*|run-help|which-comman
# Default: unhandled case. # Default: unhandled case.
*) 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 highlighters directory and check they define required functions. # Load highlighters from directory.
for highlighter_dir ($highlighters_dir/*/); do #
# 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.
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"
@ -172,8 +182,25 @@ for highlighter_dir ($highlighters_dir/*/); do
echo "zsh-syntax-highlighting: '${highlighter}' highlighter should define both required functions '_zsh_highlight_${highlighter}_highlighter' and '_zsh_highlight_${highlighter}_highlighter_predicate' in '${highlighter_dir}/${highlighter}-highlighter.zsh'." >&2 echo "zsh-syntax-highlighting: '${highlighter}' highlighter should define both required functions '_zsh_highlight_${highlighter}_highlighter' and '_zsh_highlight_${highlighter}_highlighter_predicate' in '${highlighter_dir}/${highlighter}-highlighter.zsh'." >&2
} }
} }
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)