fzf.plugin.zsh 6.3 KB

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