From fcbdb897546f0c6868fddf9ab2be0e9a23e4ba90 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Sun, 7 Aug 2022 17:02:26 +0200 Subject: [PATCH] Use new zsh 'memo=' feature to improve interoperability with others See https://github.com/zsh-users/zsh-syntax-highlighting/commit/810c2dcedebcfef5458c0bbd5e2e01d63adc7059 I'm not sure if it should be used even in our dummy _zsh_highlight or not and, to be honest, I couldn't figure out why is this dummy _zsh_highlight needed at all, so I left it unchanged. --- zsh-history-substring-search.zsh | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/zsh-history-substring-search.zsh b/zsh-history-substring-search.zsh index e4e3503..b0a0a8d 100644 --- a/zsh-history-substring-search.zsh +++ b/zsh-history-substring-search.zsh @@ -65,6 +65,7 @@ typeset -g -i _history_substring_search_raw_match_index typeset -g -a _history_substring_search_matches typeset -g -i _history_substring_search_match_index typeset -g -A _history_substring_search_unique_filter +typeset -g -i _history_substring_search_zsh_5_9 #----------------------------------------------------------------------------- # the main ZLE widgets @@ -98,6 +99,11 @@ zle -N history-substring-search-down #----------------------------------------------------------------------------- zmodload -F zsh/parameter +autoload -Uz is-at-least + +if is-at-least 5.9 $ZSH_VERSION; then + _history_substring_search_zsh_5_9=1 +fi # # We have to "override" some keys and widgets if the @@ -106,8 +112,6 @@ zmodload -F zsh/parameter # https://github.com/nicoulaj/zsh-syntax-highlighting # if [[ $+functions[_zsh_highlight] -eq 0 ]]; then - autoload -U is-at-least - # # Dummy implementation of _zsh_highlight() that # simply removes any existing highlights when the @@ -144,7 +148,7 @@ if [[ $+functions[_zsh_highlight] -eq 0 ]]; then # of the legacy "bind all widgets" if 1) zsh has the memo= feature (added in # version 5.9) and 2) add-zle-hook-widget is available. # - if is-at-least 5.9 $ZSH_VERSION && _history-substring-search-function-callable add-zle-hook-widget; then + if [[ $_history_substring_search_zsh_5_9 -eq 1 ]] && _history-substring-search-function-callable add-zle-hook-widget; then # # The following code is based on the zsh-syntax-highlighting plugin. # @@ -359,12 +363,21 @@ _history-substring-search-begin() { _history-substring-search-end() { setopt localoptions extendedglob + local highlight_memo= _history_substring_search_result=$BUFFER + if [[ $_history_substring_search_zsh_5_9 -eq 1 ]]; then + highlight_memo='memo=history-substring-search' + fi + # the search was successful so display the result properly by clearing away # existing highlights and moving the cursor to the end of the result buffer if [[ $_history_substring_search_refresh_display -eq 1 ]]; then - region_highlight=() + if [[ -n $highlight_memo ]]; then + region_highlight=( "${(@)region_highlight:#*${highlight_memo}*}" ) + else + region_highlight=() + fi CURSOR=${#BUFFER} fi @@ -384,7 +397,9 @@ _history-substring-search-end() { if [[ $query_part_match_index -le ${#BUFFER:$highlight_start_index} ]]; then highlight_start_index=$(( $highlight_start_index + $query_part_match_index )) highlight_end_index=$(( $highlight_start_index + ${#query_part} )) - region_highlight+=("$(($highlight_start_index - 1)) $(($highlight_end_index - 1)) $_history_substring_search_query_highlight") + region_highlight+=( + "$(($highlight_start_index - 1)) $(($highlight_end_index - 1)) ${_history_substring_search_query_highlight}${highlight_memo:+,$highlight_memo}" + ) fi done fi