Parent directory

fzf.plugin.zsh

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