-
Notifications
You must be signed in to change notification settings - Fork 7
/
fzf-fasd.plugin.zsh
68 lines (57 loc) · 1.82 KB
/
fzf-fasd.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# fzf-fasd integration
# author: @wookayin
# MIT License (c) 2017-2020
# vim: set ft=zsh ts=2 sts=2 ts=2:
# fasd+fzf integration (ZSH only)
__fzf_fasd_zsh_completion() {
local args cmd slug selected
args=(${(z)LBUFFER})
cmd=${args[1]}
# triggered only at the command 'z'; fallback to default
if [[ "$cmd" != "z" ]]; then
zle ${__fzf_fasd_default_completion:-expand-or-complete}
return
fi
if [[ "${#args}" -gt 1 ]]; then
eval "slug=${args[-1]}"
fi
# generate completion list from fasd
local matches_count
matches_count=$(__fzf_fasd_generate_matches "$slug" | head | wc -l)
if [[ "$matches_count" -gt 1 ]]; then
# >1 results, invoke fzf
selected=$(__fzf_fasd_generate_matches "$slug" \
| FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS $FZF_FASD_OPTS" \
fzf --query="$slug" --reverse --bind 'shift-tab:up,tab:down'
)
elif [[ "$matches_count" -eq 1 ]]; then
# 1 result, just complete it
selected=$(__fzf_fasd_generate_matches "$slug")
else;
# no result
return
fi
#echo [DEBUG] $selected $matches_count
# return completion result with $selected
if [[ -n "$selected" ]]; then
selected=$(printf %q "$selected")
if [[ "$selected" != */ ]]; then
selected="${selected}/"
fi
LBUFFER="$cmd $selected"
fi
zle redisplay
typeset -f zle-line-init >/dev/null && zle zle-line-init
}
__fzf_fasd_generate_matches() {
# currently only 'z' (fasd -d) is supported; list all dirs (without score)
# -R: make entries with higher score comes earlier
fasd -d -l -R "$@"
}
[ -z "$__fzf_fasd_default_completion" ] && {
binding=$(bindkey '^I')
[[ $binding =~ 'undefined-key' ]] || __fzf_fasd_default_completion=$binding[(s: :w)2]
unset binding
}
zle -N __fzf_fasd_zsh_completion
bindkey '^I' __fzf_fasd_zsh_completion