Skip to content

Commit

Permalink
Add glob completion & improve fzf integration
Browse files Browse the repository at this point in the history
Address issue #51.
  • Loading branch information
marlonrichert committed Jun 12, 2020
1 parent b58f843 commit a25cc8d
Showing 1 changed file with 26 additions and 32 deletions.
58 changes: 26 additions & 32 deletions zsh-autocomplete.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
[[ ! -v ZLE_REMOVE_SUFFIX_CHARS ]] && export ZLE_REMOVE_SUFFIX_CHARS=$' \t\n;&'

[[ ! -v _autocomplete__options ]] && export _autocomplete__options=(
ALWAYS_TO_END COMPLETE_ALIASES GLOB_COMPLETE GLOB_DOTS LIST_PACKED
ALWAYS_TO_END COMPLETE_ALIASES GLOB_COMPLETE GLOB_DOTS GLOB_STAR_SHORT LIST_PACKED
no_CASE_GLOB no_COMPLETE_IN_WORD no_LIST_BEEP
)

Expand Down Expand Up @@ -105,8 +105,6 @@ _autocomplete.main.hook() {
reply=( "^(${prefix}${punct}${suffix[1]}*)" )
fi
fi'
zstyle -e ':completion:*' glob '
[[ $PREFIX$SUFFIX == *[\*\(\|\<\[\?\^\#]* ]] && reply=( "true" ) || reply=( "false" )'

zstyle -e ':completion:*' tag-order '
reply=( '${(qq@)option_tags}' )
Expand All @@ -116,40 +114,31 @@ _autocomplete.main.hook() {
reply+=( "! *remote*" )
fi'
zstyle ':completion:*:(-command-|cd|z):*' tag-order '! users' '-'

zstyle ':completion:*:expand:*' tag-order '! all-expansions original'

zstyle -e ':completion:*:correct:*' max-errors '
reply=( $(( min(2, (${#PREFIX} + ${#SUFFIX}) / 2 - 1) )) numeric )'
zstyle -e ':completion:*:approximate:*' max-errors '
reply=( $(( min(7, (${#PREFIX} + ${#SUFFIX}) / 2 - 1) )) numeric )'

zstyle -e ':completion:*' glob '
[[ $PREFIX$SUFFIX == *[\*\(\|\<\[\?\^\#]* ]] && reply=( "true" ) || reply=( "false" )'
zstyle ':completion:*' expand prefix suffix
zstyle ':completion:*' list-suffixes false
zstyle ':completion:*' path-completion false
zstyle ':completion:*:(-command-|cd|z):*' list-suffixes true
zstyle ':completion:*:(-command-|cd|z):*' path-completion true

zstyle ':completion:*' file-patterns \
'*(#q^-/):all-files:file *(-/):directories:directory' '%p:globbed-files:"file or directory"'
zstyle ':completion:*:-command-:*' file-patterns \
'*(-/):directories:directory %p(#q^-/):globbed-files:executable' '*:all-files:file'
zstyle ':completion:*:z:*' file-patterns '%p(-/):directories:directory'
zstyle ':completion:*' list-suffixes true
zstyle ':completion:*' path-completion true
zstyle ':completion:*' list-dirs-first true
zstyle ':completion:*:z:*' file-patterns '%p(-/):directories'

local directory_tags=( local-directories directory-stack named-directories directories )
zstyle ':completion:*' group-order all-files directories globbed-files
zstyle ':completion:*:(-command-|cd|z):*' group-order globbed-files directories all-files
zstyle ':completion:*:(all-files|globbed-files)' group-name ''
zstyle ':completion:*:cd|z:*:globbed-files' group-name 'directories'
zstyle ':completion:*:('${(j:|:)directory_tags}')' group-name 'directories'
zstyle ':completion:*:('${(j:|:)directory_tags}')' matcher 'm:{[:lower:]}={[:upper:]}'
zstyle ':completion:*:('${(j:|:)directory_tags}')' matcher \
'm:{[:lower:][:upper:]-_}={[:upper:][:lower:]_-}'

if zstyle -t ':autocomplete:' groups 'always'; then
zstyle ':completion:*' format '%F{yellow}%d:%f'
zstyle ':completion:*' group-name ''
fi

zstyle ':completion:*:expansions' format '%F{yellow}%d:%f'
zstyle ':completion:*:expansions' group-name ''
zstyle ':completion:*:messages' format '%F{blue}%d%f'
zstyle ':completion:*:warnings' format '%F{red}%d%f'
Expand All @@ -166,10 +155,10 @@ _autocomplete.main.hook() {
zstyle -e ':completion:correct-word:complete:*' ignored-patterns 'reply=( "^($PREFIX$SUFFIX)" )'
zstyle ':completion:correct-word:*:git-*:argument-*:*' tag-order '-'

zstyle ':completion:list-choices:*' glob false
zstyle ':completion:list-choices:*' menu ''

zstyle ':completion:expand-word:*' completer _expand_alias _expand
zstyle ':completion:expand-word:*' completer _expand_alias
zstyle ':completion:expand-word:*' glob false

zstyle ':completion:list-expand:*' completer _expand _complete:-fuzzy _approximate _ignored
zstyle -e ':completion:*:complete-fuzzy:*' ignored-patterns '
Expand All @@ -183,8 +172,6 @@ _autocomplete.main.hook() {
reply=( "${prefix}([[:punct:]]*~${punct}*)" )
fi'
zstyle ':completion:list-expand:*' tag-order '*'
zstyle ':completion:list-expand:*' list-suffixes true
zstyle ':completion:list-expand:*' path-completion true
zstyle ':completion:list-expand:*' format '%F{yellow}%d:%f'
zstyle ':completion:list-expand:*' group-name ''
zstyle ':completion:list-expand:*' extra-verbose true
Expand Down Expand Up @@ -272,7 +259,7 @@ _autocomplete.main.hook() {
fi
zle -C correct-word complete-word _autocomplete.correct-word.completion-widget
bindkey '^[ ' self-insert-unmeta

local tab_completion
zstyle -s ":autocomplete:tab:" completion tab_completion || tab_completion='accept'
case $tab_completion in
Expand Down Expand Up @@ -338,18 +325,18 @@ _autocomplete.main.hook() {

[[ -v ZSH_AUTOSUGGEST_IGNORE_WIDGETS ]] && ZSH_AUTOSUGGEST_IGNORE_WIDGETS+=(
prompt_\*
user:_zsh_highlight_widget_\*-zle-line-finish
user:_zsh_highlight_widget_\*-zle-line-finish
)
[[ -v ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS ]] && ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS+=(
forward-char
forward-char
vi-forward-char
)

_autocomplete.no-op() { :; }
if [[ ! -v functions[_zsh_highlight] ]]; then
if [[ ! -v functions[_zsh_highlight] ]]; then
functions[_zsh_highlight]=$functions[_autocomplete.no-op]
fi
if [[ ! -v functions[_zsh_autosuggest_highlight_apply] ]]; then
if [[ ! -v functions[_zsh_autosuggest_highlight_apply] ]]; then
functions[_zsh_autosuggest_highlight_apply]=$functions[_autocomplete.no-op]
fi

Expand Down Expand Up @@ -715,12 +702,20 @@ _autocomplete.expand-or-complete.zle-widget() {
if [[ ${LBUFFER[-1]} != [[:IFS:]]# || ${RBUFFER[1]} != [[:IFS:]]# ]]; then
zle .select-in-shell-word
local lbuffer=$LBUFFER
if ! zle expand-word && [[ $lbuffer != $LBUFFER ]]; then
zle expand-word
if [[ $lbuffer != $LBUFFER ]]; then
zle .auto-suffix-remove
return 0
fi
return 0
fi

local lword=${${(Az)LBUFFER}[-1]}
local rword=${${(Az)RBUFFER}[1]}
local word=$lword$rword
word=${${word/[\^\(\<\[\*\?]*[\*\?\)\>\]\#]}//[\^\*\?\|\#]}
LBUFFER=${LBUFFER%$lword}$word
RBUFFER=${RBUFFER#$rword}
zle -R
zle fzf-completion
}

Expand All @@ -730,7 +725,6 @@ _autocomplete.expand-word.completion-widget() {
local curcontext
_autocomplete._main_complete expand-word
(( compstate[nmatches] == 1)) && compstate[insert]='1'
(( compstate[nmatches] > 0))
}

_autocomplete.curcontext() {
Expand Down

0 comments on commit a25cc8d

Please sign in to comment.