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 15, 2020
1 parent b105be6 commit 7c69fab
Showing 1 changed file with 37 additions and 37 deletions.
74 changes: 37 additions & 37 deletions zsh-autocomplete.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
[[ ! -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
no_CASE_GLOB no_COMPLETE_IN_WORD no_LIST_BEEP
ALWAYS_TO_END COMPLETE_ALIASES GLOB_DOTS GLOB_STAR_SHORT LIST_PACKED
no_CASE_GLOB no_COMPLETE_IN_WORD no_GLOB_COMPLETE no_LIST_BEEP
)

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

zstyle -e ':completion:*' tag-order '
reply=( '${(qq@)option_tags}' )
Expand All @@ -98,38 +96,29 @@ _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:*' 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 @@ -148,10 +137,9 @@ _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:*' glob true

zstyle ':completion:list-expand:*' completer _expand _complete _ignored _approximate
zstyle ':completion:list-expand:complete:*' matcher-list \
Expand All @@ -163,8 +151,6 @@ _autocomplete.main.hook() {
local punct=${(M)suffix##[[:punct:]]##}
reply=( "${(b)prefix}${punct}[[:punct:]]*" )'
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 @@ -252,7 +238,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 @@ -318,18 +304,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 @@ -578,7 +564,6 @@ _autocomplete.warning() {

_autocomplete.correct-word.completion-widget() {
setopt localoptions noshortloops warncreateglobal extendedglob $_autocomplete__options
unsetopt GLOB_COMPLETE

if [[ ${LBUFFER[-1]} != [[:IDENT:]] || ${RBUFFER[1]} != [[:IFS:]]# ]]; then
return 1
Expand Down Expand Up @@ -695,10 +680,18 @@ _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
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
fi

zle fzf-completion
Expand All @@ -707,10 +700,17 @@ _autocomplete.expand-or-complete.zle-widget() {
_autocomplete.expand-word.completion-widget() {
setopt localoptions noshortloops warncreateglobal extendedglob $_autocomplete__options

local curcontext
_autocomplete._main_complete expand-word
(( compstate[nmatches] == 1)) && compstate[insert]='1'
(( compstate[nmatches] > 0))
if [[ ${_lastcomp[completer]} == expand ]]; then
local curcontext
_autocomplete._main_complete expand-word _expand
if (( ${compstate[unambiguous_cursor]} > (${#:-$PREFIX$SUFFIX} + 1) )); then
compstate[insert]='unambiguous'
else
compstate[insert]=''
fi
else
_autocomplete._main_complete expand-word _expand_alias
fi
}

_autocomplete.curcontext() {
Expand Down

0 comments on commit 7c69fab

Please sign in to comment.