Skip to content

Commit

Permalink
prevent cursor highlighting from remaining visible after an accept-line
Browse files Browse the repository at this point in the history
This fixes zsh-users#109.
  • Loading branch information
m0vie committed Jan 31, 2014
1 parent d6a9f36 commit ae66485
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
6 changes: 6 additions & 0 deletions highlighters/cursor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ To override one of those styles, change its entry in `ZSH_HIGHLIGHT_STYLES`, for
ZSH_HIGHLIGHT_STYLES[cursor]='bg=blue'

The syntax for declaring styles is [documented here](http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#SEC135).

Usually it is not desired to have the cursor highlighting remain visible after a line was accepted. Thus for some accept-line style widgets the cursor highlighting should be disabled. This can be done with the variable `ZSH_HIGHLIGHT_ACCEPTING_WIDGETS` which defaults to

ZSH_HIGHLIGHT_ACCEPTING_WIDGETS=(accept-line accept-line-and-down-history accept-and-hold accept-and-infer-next-history pound-insert)

In case of any custom, userdefined widgets that call zle internally and accept a line, make sure to adjust this variable accordingly to include them.
8 changes: 7 additions & 1 deletion highlighters/cursor/cursor-highlighter.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@
# Whether the cursor highlighter should be called or not.
_zsh_highlight_cursor_highlighter_predicate()
{
_zsh_highlight_cursor_moved
_zsh_highlight_cursor_moved || _zsh_highlight_is_accept_line_type_widget
}

# Cursor highlighting function.
_zsh_highlight_cursor_highlighter()
{
# In case the current widget is one of the accept* widgets, don't do
# any highlighting. This prevents a stray cursor imprint to remain on
# the executed line.
if _zsh_highlight_is_accept_line_type_widget; then
return
fi
region_highlight+=("$CURSOR $(( $CURSOR + 1 )) $ZSH_HIGHLIGHT_STYLES[cursor]")
}
14 changes: 14 additions & 0 deletions zsh-syntax-highlighting.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
# Array declaring active highlighters names.
typeset -ga ZSH_HIGHLIGHT_HIGHLIGHTERS

# Array declaring accept-style widget names.
typeset -ga ZSH_HIGHLIGHT_ACCEPTING_WIDGETS

# Update ZLE buffer syntax highlighting.
#
# Invokes each highlighter that needs updating.
Expand Down Expand Up @@ -120,6 +123,14 @@ _zsh_highlight_cursor_moved()
[[ -n $CURSOR ]] && [[ -n ${_ZSH_HIGHLIGHT_PRIOR_CURSOR-} ]] && (($_ZSH_HIGHLIGHT_PRIOR_CURSOR != $CURSOR))
}

# Whether the current widget is an accept-line type widget.
#
# Returns 0 if the the current widget is an accept-line type widget.
_zsh_highlight_is_accept_line_type_widget()
{
[[ $WIDGET == ${ZSH_HIGHLIGHT_ACCEPTING_WIDGETS[(r)$WIDGET]} ]]
}


# -------------------------------------------------------------------------------------------------
# Setup functions
Expand Down Expand Up @@ -218,3 +229,6 @@ add-zsh-hook preexec _zsh_highlight_preexec_hook 2>/dev/null || {

# Initialize the array of active highlighters if needed.
[[ $#ZSH_HIGHLIGHT_HIGHLIGHTERS -eq 0 ]] && ZSH_HIGHLIGHT_HIGHLIGHTERS=(main) || true

# Initialize the array of accept-style widgets if needed.
[[ $#ZSH_HIGHLIGHT_ACCEPTING_WIDGETS -eq 0 ]] && ZSH_HIGHLIGHT_ACCEPTING_WIDGETS=(accept-line accept-line-and-down-history accept-and-hold accept-and-infer-next-history pound-insert) || true

0 comments on commit ae66485

Please sign in to comment.