Skip to content

Commit

Permalink
Simplify ancestor/recent dirs completion
Browse files Browse the repository at this point in the history
Fixes part of #363.
  • Loading branch information
marlonrichert committed Oct 31, 2021
1 parent 33979b2 commit b41b563
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 108 deletions.
26 changes: 10 additions & 16 deletions functions/completion/_autocomplete.ancestor_dirs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#autoload

(( compstate[nmatches] )) &&
return 1

[[ $_comp_tags == *( | local-)'directories '* ]] ||
[[ $_comp_tags != (|* )(|local-)directories(| *) ]] &&
return

local -P tag=ancestor-directories
_tags $tag
_tags ||
return
_requested $tag ||
return
local displ= expl=
_description -V ancestor-directories expl 'ancestor directory'

local -Pa ancestors=()
local -P parent=$PWD:h
Expand All @@ -19,17 +16,14 @@ while (( $#parent > 1 )); do
parent=$parent:h
done

local displ= expl=
local -Pi ret=1
local -P a=
while _next_label -V $tag expl 'ancestor directory'; do
for a in ${(aO)ancestors[@]}; do
displ=( "$a" )
[[ -z $PREFIX$SUFFIX ]] &&
displ=( "$a:t" )
compadd "$expl[@]" -d displ -P "${${(D)a:h}%/}/" -fW "${${a:h}%/}/" - "$a:t" &&
ret=0
done
for a in ${(aO)ancestors[@]}; do
displ=( "$a" )
[[ -z $PREFIX$SUFFIX ]] &&
displ=( "$a:t" )
compadd "$expl[@]" -d displ -P "${${(D)a:h}%/}/" -fW "${${a:h}%/}/" - "$a:t" &&
ret=0
done

return ret
33 changes: 9 additions & 24 deletions functions/completion/_autocomplete.recent_paths
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,17 @@ _autocomplete.recent_paths() {
(( compstate[nmatches] > 1 )) &&
return 1

local -PaU dirs=()
local -PaU files=()
local -Pi ret=1
local -P tag=
_tags recent-directories recent-files
while _tags; do

tag=recent-directories
if _requested $tag &&
[[ -v functions[_autocomplete.recent_dirs] && $_comp_tags == *( | local-)directories* ]] &&
_autocomplete.recent_dirs "$PREFIX$SUFFIX"; then
dirs=( "$reply[@]" )
while _next_label -V $tag expl 'recent directory'; do
.autocomplete.recent_paths.add $dirs[@] &&
ret=0
done
fi

tag=recent-files
if _requested $tag &&
[[ -v functions[_autocomplete.recent_files] && $_comp_tags == *( | all-)'files '* ]] &&
_autocomplete.recent_files "$PREFIX$SUFFIX"; then
dirs=( "$reply[@]" )
while _next_label -v $tag expl 'recent file'; do
.autocomplete.recent_paths.add $dirs[@] &&
ret=0
done
for tag in directories files; do
if [[ -v functions[_autocomplete.recent_$tag] &&
$_comp_tags == (|* )(|(all|local)-)$tag(| *) ]] &&
_autocomplete.recent_$tag "$PREFIX$SUFFIX"; then
files=( "$reply[@]" )
_description -V recent-$tag expl 'recent directory'
.autocomplete.recent_paths.add $files[@] &&
ret=0
fi
done

Expand Down
10 changes: 0 additions & 10 deletions functions/completion/_autocomplete.sort_by_length

This file was deleted.

15 changes: 3 additions & 12 deletions scripts/.autocomplete.compinit
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,9 @@ EOF

.autocomplete.patch _complete
_complete() {
.autocomplete._complete "$@"
local -Pi __ret__=$?

[[ $compstate[context] == command && CURRENT -eq 1 && -z $PREFIX$SUFFIX ]] &&
return __ret__

_autocomplete.ancestor_dirs "$@" &&
__ret__=0
_autocomplete.recent_paths "$@" &&
__ret__=0

return __ret__
.autocomplete._complete "$@" ||
_autocomplete.ancestor_dirs "$@" ||
_autocomplete.recent_paths "$@"
}
# functions -T _complete

Expand Down
37 changes: 11 additions & 26 deletions scripts/.autocomplete.config
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,15 @@ builtin zstyle ':completion:list-expand:*' completer \
builtin zstyle ':completion:history-incremental-*search-*:*' completer \
_autocomplete.history_lines

builtin zstyle -e ':completion:*' range _autocomplete.config.range
_autocomplete.config.range() {
[[ -z $PREFIX$SUFFIX ]] &&
reply=( 16 )
}
builtin zstyle ':completion:*' remove-all-dups yes

builtin zstyle ':completion:*:expand:*' tag-order '! all-expansions original' -
builtin zstyle -e ':completion:*:-command-:*' tag-order _autocomplete.config.tag-order
_autocomplete.config.tag-order() {
local -PaU tags=()
[[ -n "$path[(r).]" ]] &&
tags+=( globbed-files executables directories )
[[ -o autocd ]] &&
tags+=( '(|local-)directories' )
[[ -n $tags ]] &&
tags+=( suffix-aliases )
reply=( "$tags" )
if [[ -z $PREFIX$SUFFIX ]]; then
reply+=( - )
else
reply+=(
'aliases functions builtins reserved-words commands'
)
fi
builtin zstyle -e ':completion:*:-command-:*' tag-order _autocomplete.config.tag-order.command
_autocomplete.config.tag-order.command() {
reply=( '(|*-)directories executables suffix-aliases' )
if [[ -z $PREFIX$SUFFIX ]]; then
reply+=( - )
else
reply+=( 'aliases functions builtins reserved-words commands' )
fi
}
builtin zstyle ':completion:list-expand:*:-command-:*' tag-order '*'
builtin zstyle ':completion:*:-tilde-:*' tag-order directory-stack
Expand All @@ -70,7 +54,8 @@ builtin zstyle ':completion:list-expand:*:git-*:(|*-)argument-*:*' tag-order '*'

# Order of matchers matters: m should come before r, which should come before l.
# Otherwise, the results are not as expected.
builtin zstyle ':completion:*' matcher-list ''
builtin zstyle ':completion:*' matcher-list \
'm:{[:lower:]-}={[:upper:]_} r:|[.]=**'
builtin zstyle ':completion:*-loose:*' matcher-list \
'm:{[:lower:]-}={[:upper:]_} r:|[.]=** l:?|=[-_\\ ] r:?||[-_]=*'
builtin zstyle ':completion:*-fuzzy:*' matcher-list \
Expand Down Expand Up @@ -105,7 +90,7 @@ builtin zstyle ':completion:*' complete-options yes
builtin zstyle ':completion:*' list-dirs-first yes

builtin zstyle ':completion:*:descriptions' format $'%{\e[1;2;39m%}%d%{\e[0m%}'
builtin zstyle ':completion:*:warnings' format '%F{11}No %d matches found.%f'
builtin zstyle ':completion:*:warnings' format $'%{\e[1;2;39m%}no such %d%{\e[0m%}'
builtin zstyle ':completion:*:messages' format '%F{9}%d%f'
builtin zstyle ':completion:*:history-lines' format ''

Expand Down
32 changes: 12 additions & 20 deletions scripts/.autocomplete.recent-dirs
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,41 @@ zmodload -F zsh/parameter p:commands p:dirstack p:functions

if [[ -v precmd_functions && $precmd_functions[(I)_zshz_precmd] != 0 ]] &&
builtin zstyle -T ':autocomplete:' recent-dirs 'zsh-z'; then
_autocomplete.recent_dirs() {
_autocomplete.recent_directories() {
reply=( ${(f)"$( zshz --complete -l $1 2> /dev/null )"} )
}

elif [[ -v chpwd_functions && $chpwd_functions[(I)__zoxide_hook] != 0 ]] &&
builtin zstyle -T ':autocomplete:' recent-dirs 'zoxide'; then
_autocomplete.recent_dirs() {
_autocomplete.recent_directories() {
reply=( ${(f)"$( zoxide query --list $1 2> /dev/null )"} )
}

elif [[ -v precmd_functions && $precmd_functions[(I)_zlua_precmd] != 0 ]] &&
builtin zstyle -T ':autocomplete:' recent-dirs 'z.lua'; then
_autocomplete.recent_dirs() {
_autocomplete.recent_directories() {
reply=( ${${(f)"$( _zlua --complete $1 2> /dev/null )"}##<->[[:space:]]##} )
}

elif [[ -v precmd_functions && $precmd_functions[(I)_z_precmd] != 0 ]] &&
builtin zstyle -T ':autocomplete:' recent-dirs 'z.sh'; then
_autocomplete.recent_dirs() {
_autocomplete.recent_directories() {
reply=( ${${(fOa)"$( _z -l $1 2>&1 )"}##(common:|<->)[[:space:]]##} )
}

elif [[ -v chpwd_functions && $chpwd_functions[(I)autojump_chpwd] != 0 ]] &&
builtin zstyle -T ':autocomplete:' recent-dirs 'autojump'; then
_autocomplete.recent_dirs() {
_autocomplete.recent_directories() {
reply=( ${${(f)"$( autojump --complete $1 2> /dev/null )"}##${1}__<->__} )
}

elif [[ -v preexec_functions && $preexec_functions[(I)_fasd_preexec] != 0 ]] &&
builtin zstyle -T ':autocomplete:' recent-dirs 'fasd'; then
_autocomplete.recent_dirs() {
_autocomplete.recent_directories() {
reply=( ${(f)"$( fasd -dlR $1 2> /dev/null )"} )
}

elif builtin autoload -RUz chpwd_recent_dirs chpwd_recent_filehandler &&
elif builtin autoload -RUz chpwd_recent_filehandler &&
builtin zstyle -T ':autocomplete:' recent-dirs 'cdr'; then
setopt autopushd pushdignoredups

Expand All @@ -61,28 +61,20 @@ zmodload -F zsh/parameter p:commands p:dirstack p:functions
dirstack=( ${^reply[@]:#$PWD}(N-/) )
fi

_autocomplete.recent_dirs.save() {
_autocomplete.recent_directories.save() {
chpwd_recent_filehandler $PWD $dirstack[@]
}
add-zsh-hook chpwd _autocomplete.recent_dirs.save
add-zsh-hook chpwd _autocomplete.recent_directories.save

_autocomplete.recent_dirs() {
local -Pa dirs=( ${^dirstack[@]:#$PWD(|/[^/]#)}(N) )
_autocomplete.recent_directories() {
reply=( ${^dirstack[@]:#$PWD(|/[^/]#)}(N) )

local -P ancestor=$PWD:h
while [[ $ancestor != / ]]; do
dirs=( ${dirs[@]:#$ancestor} )
reply=( ${reply[@]:#$ancestor} )
ancestor=$ancestor:h
done

local -Pa displ=( "${dirs[@]:t}" )
local -PA displ_to_dirs=( "${(@)displ:^dirs}" )
_autocomplete.sort_by_length "$displ[@]"

local -Pi MBEGIN= MEND=
local -P MATCH=
reply=( ${reply[@]:/(#m)*/${displ_to_dirs[$MATCH]}} )

(( $#reply[@] ))
}
fi
Expand Down

0 comments on commit b41b563

Please sign in to comment.