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