fzf.plugin.zsh 6.3 KB

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