From 196810035992abea65e54852c4278af2069ee482 Mon Sep 17 00:00:00 2001 From: Marlon Richert Date: Thu, 4 Apr 2024 12:44:32 +0300 Subject: [PATCH] Fix bugs in `recent-paths` widget * Limit number of lines shown. * Don't show common substring. * Let lowercase match uppercase. * Let completion widgets insert recent paths even when the list isn't yet visible. --- Completions/_autocomplete__history_lines | 4 +-- Completions/_autocomplete__recent_paths | 21 ++++++++----- Completions/_autocomplete__unambiguous | 3 +- Functions/Init/.autocomplete__recent-dirs | 2 +- ...complete__complete-word__completion-widget | 2 ++ README.md | 30 ++++++++++++------- 6 files changed, 39 insertions(+), 23 deletions(-) diff --git a/Completions/_autocomplete__history_lines b/Completions/_autocomplete__history_lines index 82799ea..60dc11c 100644 --- a/Completions/_autocomplete__history_lines +++ b/Completions/_autocomplete__history_lines @@ -65,11 +65,11 @@ _autocomplete__history_lines() { ) local -Pi excess= index= max= list_lines= - if (( is_incremental )); then + if (( is_incremental )); then .autocomplete:async:list-choices:max-lines 16 (( list_lines = _autocomplete__max_lines )) (( max = 16 * list_lines )) # Buffer for bubbling up more relevant results. - else + else zstyle -s ":autocomplete:${curcontext}:" list-lines list_lines || (( list_lines = $LINES / 2 )) (( max = $list_lines )) diff --git a/Completions/_autocomplete__recent_paths b/Completions/_autocomplete__recent_paths index b18452e..cf4834b 100644 --- a/Completions/_autocomplete__recent_paths +++ b/Completions/_autocomplete__recent_paths @@ -1,19 +1,26 @@ #autoload zmodload -Fa zsh/parameter p:functions -local -aU files=() -local -a expl=() displ=() -local -Pi ret=1 i=0 -local -P singular= plural= +local -aU files +local -a expl displ +local -iP ret i +local -P singular plural tag + +ret=1 + +.autocomplete:async:list-choices:max-lines 16 for singular plural in directory directories file files; do if [[ -v functions[+autocomplete:recent-$plural] ]] && +autocomplete:recent-$plural "$PREFIX$SUFFIX"; then files=( "$reply[@]" ) - _description -V recent-$plural expl "recent $singular" - for i in {1..$#files}; do - displ=( "${${(D)files[i]:h}%/}/$files[i]:t" ) + tag=recent-$plural + _comp_tags+=" $tag" + _description -V "$tag" expl "recent $singular" + + for (( i = 1 ; i <= $#files, compstate[list_lines] < _autocomplete__max_lines ; i++ )); do + displ=( "$files[i]" ) compadd -U "$expl[@]" -d displ -P "${${displ[1]:h}%/}/" -fW "${${files[i]:h}%/}/" \ -- "$files[i]:t" && ret=0 diff --git a/Completions/_autocomplete__unambiguous b/Completions/_autocomplete__unambiguous index 684cbfe..0784349 100644 --- a/Completions/_autocomplete__unambiguous +++ b/Completions/_autocomplete__unambiguous @@ -1,7 +1,6 @@ #autoload -# Does not work correctly. -[[ $_comp_tags == history-lines ]] && +[[ $_comp_tags == (history-lines|* recent-*) ]] && return 1 # Is not going to be correct. diff --git a/Functions/Init/.autocomplete__recent-dirs b/Functions/Init/.autocomplete__recent-dirs index 4e000d1..07b1232 100644 --- a/Functions/Init/.autocomplete__recent-dirs +++ b/Functions/Init/.autocomplete__recent-dirs @@ -33,7 +33,7 @@ ${0}:precmd() { reply=( ${^dirstack[@]:#([/~]|$PWD(|/[^/]#))}(N) ) [[ -n $1 ]] && - reply=( ${(M)reply:#*${(~j:*:)${(s::)1}}*} ) + reply=( ${(M)reply:#(#l)*${(~j:*:)${(s::)1}}*} ) (( $#reply[@] )) } diff --git a/Functions/Widgets/.autocomplete__complete-word__completion-widget b/Functions/Widgets/.autocomplete__complete-word__completion-widget index 6b774a7..818f746 100644 --- a/Functions/Widgets/.autocomplete__complete-word__completion-widget +++ b/Functions/Widgets/.autocomplete__complete-word__completion-widget @@ -9,6 +9,8 @@ local +h -a comppostfuncs=( .autocomplete__complete-word__post "$comppostfuncs[@ if [[ -z $compstate[old_list] && $curcontext == history-incremental-search* ]]; then autocomplete:_main_complete:new - history-lines _autocomplete__history_lines +elif [[ -z $compstate[old_list] && $curcontext == recent-paths:* ]]; then + autocomplete:_main_complete:new - recent-paths _autocomplete__recent_paths elif [[ -z $compstate[old_list] ]] || [[ -v _autocomplete__partial_list && $WIDGETSTYLE == (|*-)(list|menu)(|-*) ]] || _autocomplete__should_insert_unambiguous; then diff --git a/README.md b/README.md index 33a23b0..0fa3031 100644 --- a/README.md +++ b/README.md @@ -232,24 +232,32 @@ For example, this will stop completions from showing whenever the current word c zstyle ':autocomplete:*' ignored-input '..##' ``` -## Limit the number of lines shown -By default, Autocomplete lets the history menu fill half of the screen, and limits autocompletion and history search -to a maximum of 16 lines. You can change these limits as follows: +## Change the max number of lines shown +By default, Autocomplete lets the history menu fill half of the screen, and limits all real-time +listings to a maximum of 16 lines. You can change these limits as follows: ```zsh -# Autocompletion -zstyle -e ':autocomplete:list-choices:*' list-lines 'reply=( $(( LINES / 3 )) )' +# Note: -e lets you specify a dynamically generated value. -# Override history search. +# Override default for all listings +# $LINES is the number of lines that fit on screen. +zstyle -e ':autocomplete:*:*' list-lines 'reply=( $(( LINES / 3 )) )' + +# Override for recent path search only +zstyle ':autocomplete:recent-paths:*' list-lines 10 + +# Override for history search only zstyle ':autocomplete:history-incremental-search-backward:*' list-lines 8 -# History menu. -zstyle ':autocomplete:history-search-backward:*' list-lines 256 +# Override for history menu only +zstyle ':autocomplete:history-search-backward:*' list-lines 2000 ``` -Note that for autocompletion and history search, the maximum number of lines is additionally capped to the number of -lines that fit on screen. However, there is no such limit for the history menu. If that generates more lines than fit -on screen, you can simply scroll upwards to see more. +Note that for all real-time listings, the maximum number of lines is additionally capped to the +number of lines that fit on screen. However, there is no such limit for the history menu. If that +generates more lines than fit on screen, you can simply use PgUp and PgDn to +scroll through the excess lines. (Note: On some terminals, you have to additionally hold +Shift or, otherwise, it will scroll the terminal buffer instead.) ### Use a custom backend for recent directories Autocomplete comes with its own backend for keeping track of and listing recent directories (which