Skip to content

Commit

Permalink
Apply some changes from #57
Browse files Browse the repository at this point in the history
You can now use _approximate, but there is no fzf support.
And a better stability.
  • Loading branch information
Aloxaf committed Mar 10, 2020
1 parent 56adfa4 commit adcd317
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ See [zsh's doc](http://zsh.sourceforge.net/Doc/Release/Zsh-Modules.html#The-zsh_

You can use <kbd>C-x h</kbd> to get possible context for a command:

**NOTE:** This command will break fzf-tab totally, you need to restart zsh to re-enable fzf-tab.

```zsh
❯ rg -- # Press `C-x h` here
tags in context :completion::complete:rg::
Expand Down
2 changes: 0 additions & 2 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ zstyle 的更多信息详见 [zsh's doc](http://zsh.sourceforge.net/Doc/Release/

你可以使用 <kbd>C-x h</kbd> 来获取补全一个命令时可能的 context。

**注意:** 这个命令会完全破坏掉 fzf-tab 的加载状态,只能通过重启 zsh 来再次激活。

```zsh
❯ rg -- # 按下 `C-x h`
tags in context :completion::complete:rg::
Expand Down
52 changes: 33 additions & 19 deletions fzf-tab.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
zmodload zsh/zutil

# thanks Valodim/zsh-capture-completion
compadd() {
_fzf_tab_compadd() {
# parse all options
local -A apre hpre dscrs _oad expl
local -a isfile _opts __
Expand Down Expand Up @@ -284,9 +284,7 @@ _fzf_tab_complete() {
local -Ua _fzf_tab_groups
local choice choices _fzf_tab_curcontext continuous_trigger

IN_FZF_TAB=1
_fzf_tab_orig_main_complete # must run with user options; don't move `emulate -L zsh` above this line
IN_FZF_TAB=0
_fzf_tab__main_complete # must run with user options; don't move `emulate -L zsh` above this line

emulate -L zsh -o extended_glob

Expand Down Expand Up @@ -349,23 +347,17 @@ _fzf_tab_complete() {
fi
}

zle -C _fzf_tab_complete complete-word _fzf_tab_complete

fzf-tab-complete() {
# this name must be ugly to avoid clashes
local -i _fzf_tab_continue=1
while (( _fzf_tab_continue )); do
_fzf_tab_continue=0
if (( ${+functions[_main_complete]} )); then
# hack: hook _main_complete to trigger fzf-tab
functions[_fzf_tab_orig_main_complete]=${functions[_main_complete]}
function _main_complete() { _fzf_tab_complete }
{
zle .fzf-tab-orig-$_fzf_tab_orig_widget
} always {
functions[_main_complete]=$functions[_fzf_tab_orig_main_complete]
}
fi
IN_FZF_TAB=1
{
zle .fzf-tab-orig-$_fzf_tab_orig_widget
} always {
IN_FZF_TAB=0
}
zle redisplay
done
}
Expand All @@ -384,6 +376,12 @@ disable-fzf-tab() {
esac
unset _fzf_tab_orig_widget _fzf_tab_orig_list_groupded

# unhook compadd so that _approximate can work properply
unfunction compadd

functions -c _fzf_tab__main_complete _main_complete
functions -c _fzf_tab__approximate _approximate

# Don't remove .fzf-tab-orig-$_fzf_tab_orig_widget as we won't be able to reliably
# create it if enable-fzf-tab is called again.
}
Expand Down Expand Up @@ -417,14 +415,30 @@ enable-fzf-tab() {
fi
fi

# Make sure _main_complete has been loaded because we will then hook it.
autoload +X _main_complete

zstyle -t ':completion:*' list-grouped false
typeset -g _fzf_tab_orig_list_grouped=$?

zstyle ':completion:*' list-grouped false
bindkey '^I' fzf-tab-complete

# hook compadd
functions -c _fzf_tab_compadd compadd

# hook _main_complete to trigger fzf-tab
functions -c _main_complete _fzf_tab__main_complete
function _main_complete() { _fzf_tab_complete }

# TODO: This is not a full support, see #47
# _approximate will also hook compadd
# let it call _fzf_tab_compadd instead of builtin compadd so that fzf-tab can capture result
# make sure _approximate has been loaded.
functions -c _approximate _fzf_tab__approximate
function _approximate() {
# if not called by fzf-tab, don't do anything with compadd
(( ! IN_FZF_TAB )) || unfunction compadd
_fzf_tab__approximate
(( ! IN_FZF_TAB )) || functions -c _fzf_tab_compadd compadd
}
}

toggle-fzf-tab() {
Expand Down

0 comments on commit adcd317

Please sign in to comment.