Skip to content

Commit

Permalink
Fix bugs in insertion logic
Browse files Browse the repository at this point in the history
  • Loading branch information
marlonrichert committed Jun 29, 2023
1 parent ece9083 commit 6d059a3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 61 deletions.
77 changes: 35 additions & 42 deletions Functions/Widgets/.autocomplete:complete-word:post
Original file line number Diff line number Diff line change
@@ -1,64 +1,57 @@
#autoload

local -a match=() mbegin=() mend=() # `zstyle` for some reason assigns these.

unset MENUMODE MENUSELECT
compstate[insert]=
compstate[list]=
compstate[restore]=

if [[ $_completer == _prefix ]]; then
compstate[to_end]=
else
compstate[to_end]='always'
fi

if _autocomplete__should_insert_unambiguous; then

[[ $WIDGETSTYLE == (|*-)menu(|-*) ]] &&
compstate[insert]='automenu-'

if [[ $WIDGETSTYLE == (|*-)menu(|-*) ]]; then
compstate[insert]='automenu-'
else
compstate[insert]=
fi
compstate[insert]+='unambiguous'

compstate[list]='list force'
unset _autocomplete__unambiguous

return # !
fi

local -Pa comptags=()
local -Pi nmatches=0

if [[ $compstate[old_list] == keep ]]; then
comptags=( $=_lastcomp[tags] )
nmatches=$_lastcomp[nmatches]
else
comptags=( $=_comp_tags )
nmatches=$compstate[nmatches]
fi

if (( nmatches > 1 )); then
if [[ $WIDGETSTYLE == (|*-)menu(|-*) ]]; then
compstate[insert]='menu:'
if (( nmatches <= LINES - BUFFERLINES - 1 )); then
compstate[list]='list packed rows'
else
builtin zle -Rc
if [[ $WIDGETSTYLE == (|*-)select(|-*) ]]; then
typeset -gi MENUSELECT=0
if [[ $WIDGET == (|*-)search(|-*) ]]; then
typeset -g MENUMODE=search-forward
fi
fi
compstate[insert]='menu:'
compstate[list]='list force'
else
compstate[insert]=
compstate[list]=
zle -Rc
fi
if [[ $WIDGET == (|*-)search(|-*) ]]; then
MENUMODE=search-forward
if [[ $WIDGET == (|.)reverse-* || $WIDGETSTYLE == (|.)reverse-menu-complete ]]; then
compstate[insert]+='0'
else
compstate[insert]+='1'
fi
if [[ $WIDGETSTYLE == (|*-)select(|-*) ]]; then
typeset -gi MENUSELECT=0

local -Pa comptags=()
if [[ $compstate[old_list] == keep ]]; then
comptags=( $=_lastcomp[tags] )
else
comptags=( $=_comp_tags )
fi
fi

if [[ $WIDGET == (|.)reverse-* || $WIDGETSTYLE == (|.)reverse-menu-complete ]]; then
compstate[insert]+='0'
else
compstate[insert]+='1'
fi
local -a spacetags=()
builtin zstyle -a :autocomplete: add-space spacetags ||
spacetags=( executables aliases functions builtins reserved-words commands )

local -a spacetags=()
builtin zstyle -a :autocomplete: add-space spacetags ||
spacetags=( executables aliases functions builtins reserved-words commands )
[[ -n ${comptags:*spacetags} ]] &&
compstate[insert]+=' '
[[ -n ${comptags:*spacetags} ]] &&
compstate[insert]+=' '
fi
38 changes: 19 additions & 19 deletions Tests/complete-word.post.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,31 @@ automenu-unambiguous 0
%
```

If we have only one match, always insert it.
Default inserts first match.
```zsh
% compstate[old_list]=keep _lastcomp[tags]=
% compstate[nmatches]=0 _lastcomp[nmatches]=1
% WIDGETSTYLE=menu-select WIDGET=
% compstate[nmatches]=0 _lastcomp[nmatches]=2
% WIDGETSTYLE= WIDGET=
% _autocomplete__unambiguous=
% .autocomplete:complete-word:post
% print -r -- ${(q+)compstate[insert]} $+MENUSELECT $MENUMODE
1 0
% print -r -- ${(q+)compstate[insert]}
1
%
```

Add a space for certain tags.
```zsh
% compstate[old_list]=keep _lastcomp[tags]='BAR BAZ'
% compstate[nmatches]=0 _lastcomp[nmatches]=1
% WIDGETSTYLE=menu-select WIDGET=
% WIDGETSTYLE= WIDGET=
% _autocomplete__unambiguous=
% comptags=( BAR ) .autocomplete:complete-word:post
% print -r -- ${(q+)compstate[insert]} $+MENUSELECT $MENUMODE
'1 ' 0
% print -r -- ${(q+)compstate[insert]}
'1 '
%
```

When there's more than one match, `menu-complete` inserts `menu:`.
`menu-` widgets insert `menu:`.
```zsh
% compstate[old_list]=keep _lastcomp[tags]='BAR BAZ'
% compstate[nmatches]=0 _lastcomp[nmatches]=2
Expand All @@ -68,38 +68,38 @@ When there's more than one match, `menu-complete` inserts `menu:`.
%
```

Reverse inserts the last match.
`menu-select` widgets add `MENUSELECT`
```zsh
% compstate[old_list]=keep _lastcomp[tags]='BAR BAZ'
% compstate[nmatches]=0 _lastcomp[nmatches]=2
% WIDGETSTYLE=reverse-menu-complete WIDGET=
% WIDGETSTYLE=menu-select WIDGET=
% _autocomplete__unambiguous=
% .autocomplete:complete-word:post
% print -r -- ${(q+)compstate[insert]} $+MENUSELECT $MENUMODE
'menu:0 ' 0
'menu:1 ' 1
%
```

`select` adds `MENUSELECT`
`menu-select` widgets with `search` in name add `MENUMODE=search-forward`.
```zsh
% compstate[old_list]=keep _lastcomp[tags]='BAR BAZ'
% compstate[nmatches]=0 _lastcomp[nmatches]=2
% WIDGETSTYLE=menu-select WIDGET=
% WIDGETSTYLE=menu-select WIDGET=incremental-history-search-forward
% _autocomplete__unambiguous=
% .autocomplete:complete-word:post
% print -r -- ${(q+)compstate[insert]} $+MENUSELECT $MENUMODE
'menu:1 ' 1
'menu:1 ' 1 search-forward
%
```

`search` adds `MENUMODE=search-forward`.
Reverse inserts the last match.
```zsh
% compstate[old_list]=keep _lastcomp[tags]='BAR BAZ'
% compstate[nmatches]=0 _lastcomp[nmatches]=2
% WIDGETSTYLE=menu-select WIDGET=incremental-history-search-forward
% WIDGETSTYLE=reverse-menu-complete WIDGET=
% _autocomplete__unambiguous=
% .autocomplete:complete-word:post
% print -r -- ${(q+)compstate[insert]} $+MENUSELECT $MENUMODE
'menu:1 ' 1 search-forward
% print -r -- ${(q+)compstate[insert]}
'menu:0 '
%
```

0 comments on commit 6d059a3

Please sign in to comment.