From ae66485a179d069fd0f2e79cc2224a915c6f2892 Mon Sep 17 00:00:00 2001 From: m0viefreak Date: Thu, 14 Nov 2013 03:52:58 +0100 Subject: [PATCH] prevent cursor highlighting from remaining visible after an accept-line This fixes #109. --- highlighters/cursor/README.md | 6 ++++++ highlighters/cursor/cursor-highlighter.zsh | 8 +++++++- zsh-syntax-highlighting.zsh | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/highlighters/cursor/README.md b/highlighters/cursor/README.md index d28ac1908..88b2fc56f 100644 --- a/highlighters/cursor/README.md +++ b/highlighters/cursor/README.md @@ -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. diff --git a/highlighters/cursor/cursor-highlighter.zsh b/highlighters/cursor/cursor-highlighter.zsh index b5868a46e..a945c52f0 100644 --- a/highlighters/cursor/cursor-highlighter.zsh +++ b/highlighters/cursor/cursor-highlighter.zsh @@ -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]") } diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index 26ae673e2..0970705bc 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -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. @@ -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 @@ -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