fzf.plugin.zsh 6.9 KB

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