2
0

fzf.plugin.zsh 7.5 KB

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