fzf.plugin.zsh 6.8 KB

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