]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/fzf/fzf.plugin.zsh
...
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / fzf / fzf.plugin.zsh
1 function fzf_setup_using_base_dir() {
2   local fzf_base fzf_shell fzfdirs dir
3
4   test -d "${FZF_BASE}" && fzf_base="${FZF_BASE}"
5
6   if [[ -z "${fzf_base}" ]]; then
7     fzfdirs=(
8       "${HOME}/.fzf"
9       "${HOME}/.nix-profile/share/fzf"
10       "${XDG_DATA_HOME:-$HOME/.local/share}/fzf"
11       "/usr/local/opt/fzf"
12       "/usr/share/fzf"
13       "/usr/local/share/examples/fzf"
14     )
15     for dir in ${fzfdirs}; do
16       if [[ -d "${dir}" ]]; then
17         fzf_base="${dir}"
18         break
19       fi
20     done
21
22     if [[ -z "${fzf_base}" ]]; then
23       if (( ${+commands[fzf-share]} )) && dir="$(fzf-share)" && [[ -d "${dir}" ]]; then
24         fzf_base="${dir}"
25       elif (( ${+commands[brew]} )) && dir="$(brew --prefix fzf 2>/dev/null)"; then
26         if [[ -d "${dir}" ]]; then
27           fzf_base="${dir}"
28         fi
29       fi
30     fi
31   fi
32
33   if [[ ! -d "${fzf_base}" ]]; then
34     return 1
35   fi
36
37   # Fix fzf shell directory for Arch Linux, NixOS or Void Linux packages
38   if [[ ! -d "${fzf_base}/shell" ]]; then
39     fzf_shell="${fzf_base}"
40   else
41     fzf_shell="${fzf_base}/shell"
42   fi
43
44   # Setup fzf binary path
45   if (( ! ${+commands[fzf]} )) && [[ "$PATH" != *$fzf_base/bin* ]]; then
46     export PATH="$PATH:$fzf_base/bin"
47   fi
48
49   # Auto-completion
50   if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
51     source "${fzf_shell}/completion.zsh" 2> /dev/null
52   fi
53
54   # Key bindings
55   if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
56     source "${fzf_shell}/key-bindings.zsh"
57   fi
58 }
59
60
61 function fzf_setup_using_debian() {
62   if (( ! $+commands[dpkg] )) || ! dpkg -s fzf &>/dev/null; then
63     # Either not a debian based distro, or no fzf installed
64     return 1
65   fi
66
67   # NOTE: There is no need to configure PATH for debian package, all binaries
68   # are installed to /usr/bin by default
69
70   local completions key_bindings
71
72   case $PREFIX in
73     *com.termux*)
74       # Support Termux package
75       completions="${PREFIX}/share/fzf/completion.zsh"
76       key_bindings="${PREFIX}/share/fzf/key-bindings.zsh"
77       ;;
78     *)
79       # Determine completion file path: first bullseye/sid, then buster/stretch
80       completions="/usr/share/doc/fzf/examples/completion.zsh"
81       [[ -f "$completions" ]] || completions="/usr/share/zsh/vendor-completions/_fzf"
82       key_bindings="/usr/share/doc/fzf/examples/key-bindings.zsh"
83       ;;
84   esac
85
86   # Auto-completion
87   if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
88     source $completions 2> /dev/null
89   fi
90
91   # Key bindings
92   if [[ ! "$DISABLE_FZF_KEY_BINDINGS" == "true" ]]; then
93     source $key_bindings
94   fi
95
96   return 0
97 }
98
99 function fzf_setup_using_opensuse() {
100   # OpenSUSE installs fzf in /usr/bin/fzf
101   # If the command is not found, the package isn't installed
102   (( $+commands[fzf] )) || return 1
103
104   # The fzf-zsh-completion package installs the auto-completion in
105   local completions="/usr/share/zsh/site-functions/_fzf"
106   # The fzf-zsh-completion package installs the key-bindings file in
107   local key_bindings="/etc/zsh_completion.d/fzf-key-bindings"
108
109   # If these are not found: (1) maybe we're not on OpenSUSE, or
110   # (2) maybe the fzf-zsh-completion package isn't installed.
111   if [[ ! -f "$completions" || ! -f "$key_bindings" ]]; then
112     return 1
113   fi
114
115   # Auto-completion
116   if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
117     source "$completions" 2>/dev/null
118   fi
119
120   # Key bindings
121   if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
122     source "$key_bindings" 2>/dev/null
123   fi
124
125   return 0
126 }
127
128 function fzf_setup_using_openbsd() {
129   # openBSD installs fzf in /usr/local/bin/fzf
130   if [[ "$OSTYPE" != openbsd* ]] || (( ! $+commands[fzf] )); then
131     return 1
132   fi
133
134   # The fzf package installs the auto-completion in
135   local completions="/usr/local/share/zsh/site-functions/_fzf_completion"
136   # The fzf package installs the key-bindings file in
137   local key_bindings="/usr/local/share/zsh/site-functions/_fzf_key_bindings"
138
139   # Auto-completion
140   if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
141     source "$completions" 2>/dev/null
142   fi
143
144   # Key bindings
145   if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
146     source "$key_bindings" 2>/dev/null
147   fi
148
149   return 0
150 }
151
152 function fzf_setup_using_cygwin() {
153   # Cygwin installs fzf in /usr/local/bin/fzf
154   if [[ "$OSTYPE" != cygwin* ]] || (( ! $+commands[fzf] )); then
155     return 1
156   fi
157
158   # The fzf-zsh-completion package installs the auto-completion in
159   local completions="/etc/profile.d/fzf-completion.zsh"
160   # The fzf-zsh package installs the key-bindings file in
161   local key_bindings="/etc/profile.d/fzf.zsh"
162
163   # Auto-completion
164   if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
165     source "$completions" 2>/dev/null
166   fi
167
168   # Key bindings
169   if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
170     source "$key_bindings" 2>/dev/null
171   fi
172
173   return 0
174 }
175
176 function fzf_setup_using_macports() {
177   # If the command is not found, the package isn't installed
178   (( $+commands[fzf] )) || return 1
179
180   # The fzf-zsh-completion package installs the auto-completion in
181   local completions="/opt/local/share/zsh/site-functions/fzf"
182   # The fzf-zsh-completion package installs the key-bindings file in
183   local key_bindings="/opt/local/share/fzf/shell/key-bindings.zsh"
184
185   if [[ ! -f "$completions" || ! -f "$key_bindings" ]]; then
186     return 1
187   fi
188
189   # Auto-completion
190   if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
191     source "$completions" 2>/dev/null
192   fi
193
194   # Key bindings
195   if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
196     source "$key_bindings" 2>/dev/null
197   fi
198
199   return 0
200 }
201
202 # Indicate to user that fzf installation not found if nothing worked
203 function fzf_setup_error() {
204   cat >&2 <<'EOF'
205 [oh-my-zsh] fzf plugin: Cannot find fzf installation directory.
206 Please add `export FZF_BASE=/path/to/fzf/install/dir` to your .zshrc
207 EOF
208 }
209
210 fzf_setup_using_openbsd \
211   || fzf_setup_using_debian \
212   || fzf_setup_using_opensuse \
213   || fzf_setup_using_cygwin \
214   || fzf_setup_using_macports \
215   || fzf_setup_using_base_dir \
216   || fzf_setup_error
217
218 unset -f -m 'fzf_setup_*'
219
220 if [[ -z "$FZF_DEFAULT_COMMAND" ]]; then
221   if (( $+commands[fd] )); then
222     export FZF_DEFAULT_COMMAND='fd --type f --hidden --exclude .git'
223   elif (( $+commands[rg] )); then
224     export FZF_DEFAULT_COMMAND='rg --files --hidden --glob "!.git/*"'
225   elif (( $+commands[ag] )); then
226     export FZF_DEFAULT_COMMAND='ag -l --hidden -g "" --ignore .git'
227   fi
228 fi