Skip to content

Commit

Permalink
Fix bugs in recent-paths widget
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
marlonrichert committed Apr 4, 2024
1 parent 5cd2221 commit 1968100
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Completions/_autocomplete__history_lines
Original file line number Diff line number Diff line change
Expand Up @@ -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 ))
Expand Down
21 changes: 14 additions & 7 deletions Completions/_autocomplete__recent_paths
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 1 addition & 2 deletions Completions/_autocomplete__unambiguous
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Functions/Init/.autocomplete__recent-dirs
Original file line number Diff line number Diff line change
Expand Up @@ -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[@] ))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 19 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <kbd>PgUp</kbd> and <kbd>PgDn</kbd> to
scroll through the excess lines. (Note: On some terminals, you have to additionally hold
<kbd>Shift</kbd> 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
Expand Down

0 comments on commit 1968100

Please sign in to comment.