fzf.plugin.zsh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/opt/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[apt] && ! $+commands[apt-get] )); then
  54. # Not a debian based distro
  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. if [[ ! -f "${PREFIX}/bin/fzf" ]]; then
  63. # fzf not installed
  64. return 1
  65. fi
  66. # Support Termux package
  67. completions="${PREFIX}/share/fzf/completion.zsh"
  68. key_bindings="${PREFIX}/share/fzf/key-bindings.zsh"
  69. ;;
  70. *)
  71. if [[ ! -d /usr/share/doc/fzf/examples ]]; then
  72. # fzf not installed
  73. return 1
  74. fi
  75. # Determine completion file path: first bullseye/sid, then buster/stretch
  76. completions="/usr/share/doc/fzf/examples/completion.zsh"
  77. [[ -f "$completions" ]] || completions="/usr/share/zsh/vendor-completions/_fzf"
  78. key_bindings="/usr/share/doc/fzf/examples/key-bindings.zsh"
  79. ;;
  80. esac
  81. # Auto-completion
  82. if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
  83. source $completions 2> /dev/null
  84. fi
  85. # Key bindings
  86. if [[ ! "$DISABLE_FZF_KEY_BINDINGS" == "true" ]]; then
  87. source $key_bindings
  88. fi
  89. return 0
  90. }
  91. function fzf_setup_using_opensuse() {
  92. # OpenSUSE installs fzf in /usr/bin/fzf
  93. # If the command is not found, the package isn't installed
  94. (( $+commands[fzf] )) || return 1
  95. # The fzf-zsh-completion package installs the auto-completion in
  96. local completions="/usr/share/zsh/site-functions/_fzf"
  97. # The fzf-zsh-completion package installs the key-bindings file in
  98. local key_bindings="/etc/zsh_completion.d/fzf-key-bindings"
  99. # If these are not found: (1) maybe we're not on OpenSUSE, or
  100. # (2) maybe the fzf-zsh-completion package isn't installed.
  101. if [[ ! -f "$completions" || ! -f "$key_bindings" ]]; then
  102. return 1
  103. fi
  104. # Auto-completion
  105. if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
  106. source "$completions" 2>/dev/null
  107. fi
  108. # Key bindings
  109. if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
  110. source "$key_bindings" 2>/dev/null
  111. fi
  112. return 0
  113. }
  114. function fzf_setup_using_openbsd() {
  115. # openBSD installs fzf in /usr/local/bin/fzf
  116. if [[ "$OSTYPE" != openbsd* ]] || (( ! $+commands[fzf] )); then
  117. return 1
  118. fi
  119. # The fzf package installs the auto-completion in
  120. local completions="/usr/local/share/zsh/site-functions/_fzf_completion"
  121. # The fzf package installs the key-bindings file in
  122. local key_bindings="/usr/local/share/zsh/site-functions/_fzf_key_bindings"
  123. # Auto-completion
  124. if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
  125. source "$completions" 2>/dev/null
  126. fi
  127. # Key bindings
  128. if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
  129. source "$key_bindings" 2>/dev/null
  130. fi
  131. return 0
  132. }
  133. function fzf_setup_using_cygwin() {
  134. # Cygwin installs fzf in /usr/local/bin/fzf
  135. if [[ "$OSTYPE" != cygwin* ]] || (( ! $+commands[fzf] )); then
  136. return 1
  137. fi
  138. # The fzf-zsh-completion package installs the auto-completion in
  139. local completions="/etc/profile.d/fzf-completion.zsh"
  140. # The fzf-zsh package installs the key-bindings file in
  141. local key_bindings="/etc/profile.d/fzf.zsh"
  142. # Auto-completion
  143. if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
  144. source "$completions" 2>/dev/null
  145. fi
  146. # Key bindings
  147. if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
  148. source "$key_bindings" 2>/dev/null
  149. fi
  150. return 0
  151. }
  152. function fzf_setup_using_macports() {
  153. # If the command is not found, the package isn't installed
  154. (( $+commands[fzf] )) || return 1
  155. # The fzf-zsh-completion package installs the auto-completion in
  156. local completions="/opt/local/share/fzf/shell/completion.zsh"
  157. # The fzf-zsh-completion package installs the key-bindings file in
  158. local key_bindings="/opt/local/share/fzf/shell/key-bindings.zsh"
  159. if [[ ! -f "$completions" || ! -f "$key_bindings" ]]; then
  160. return 1
  161. fi
  162. # Auto-completion
  163. if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
  164. source "$completions" 2>/dev/null
  165. fi
  166. # Key bindings
  167. if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
  168. source "$key_bindings" 2>/dev/null
  169. fi
  170. return 0
  171. }
  172. # Indicate to user that fzf installation not found if nothing worked
  173. function fzf_setup_error() {
  174. cat >&2 <<'EOF'
  175. [oh-my-zsh] fzf plugin: Cannot find fzf installation directory.
  176. Please add `export FZF_BASE=/path/to/fzf/install/dir` to your .zshrc
  177. EOF
  178. }
  179. fzf_setup_using_openbsd \
  180. || fzf_setup_using_debian \
  181. || fzf_setup_using_opensuse \
  182. || fzf_setup_using_cygwin \
  183. || fzf_setup_using_macports \
  184. || fzf_setup_using_base_dir \
  185. || fzf_setup_error
  186. unset -f -m 'fzf_setup_*'
  187. if [[ -z "$FZF_DEFAULT_COMMAND" ]]; then
  188. if (( $+commands[fd] )); then
  189. export FZF_DEFAULT_COMMAND='fd --type f --hidden --exclude .git'
  190. elif (( $+commands[rg] )); then
  191. export FZF_DEFAULT_COMMAND='rg --files --hidden --glob "!.git/*"'
  192. elif (( $+commands[ag] )); then
  193. export FZF_DEFAULT_COMMAND='ag -l --hidden -g "" --ignore .git'
  194. fi
  195. fi