fzf.plugin.zsh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. function 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[brew]} )) && dir="$(brew --prefix fzf 2>/dev/null)"; then
  21. if [[ -d "${dir}" ]]; then
  22. fzf_base="${dir}"
  23. fi
  24. fi
  25. fi
  26. fi
  27. if [[ ! -d "${fzf_base}" ]]; then
  28. return 1
  29. fi
  30. # Fix fzf shell directory for Arch Linux, NixOS or Void Linux packages
  31. if [[ ! -d "${fzf_base}/shell" ]]; then
  32. fzf_shell="${fzf_base}"
  33. else
  34. fzf_shell="${fzf_base}/shell"
  35. fi
  36. # Setup fzf binary path
  37. if (( ! ${+commands[fzf]} )) && [[ "$PATH" != *$fzf_base/bin* ]]; then
  38. export PATH="$PATH:$fzf_base/bin"
  39. fi
  40. # Auto-completion
  41. if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
  42. source "${fzf_shell}/completion.zsh" 2> /dev/null
  43. fi
  44. # Key bindings
  45. if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
  46. source "${fzf_shell}/key-bindings.zsh"
  47. fi
  48. }
  49. function setup_using_debian_package() {
  50. if (( ! $+commands[dpkg] )) || ! dpkg -s fzf &>/dev/null; then
  51. # Either not a debian based distro, or no fzf installed
  52. return 1
  53. fi
  54. # NOTE: There is no need to configure PATH for debian package, all binaries
  55. # are installed to /usr/bin by default
  56. local completions key_bindings
  57. case $PREFIX in
  58. *com.termux*)
  59. # Support Termux package
  60. completions="${PREFIX}/share/fzf/completion.zsh"
  61. key_bindings="${PREFIX}/share/fzf/key-bindings.zsh"
  62. ;;
  63. *)
  64. # Determine completion file path: first bullseye/sid, then buster/stretch
  65. completions="/usr/share/doc/fzf/examples/completion.zsh"
  66. [[ -f "$completions" ]] || completions="/usr/share/zsh/vendor-completions/_fzf"
  67. key_bindings="/usr/share/doc/fzf/examples/key-bindings.zsh"
  68. ;;
  69. esac
  70. # Auto-completion
  71. if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
  72. source $completions 2> /dev/null
  73. fi
  74. # Key bindings
  75. if [[ ! "$DISABLE_FZF_KEY_BINDINGS" == "true" ]]; then
  76. source $key_bindings
  77. fi
  78. return 0
  79. }
  80. function setup_using_opensuse_package() {
  81. # OpenSUSE installs fzf in /usr/bin/fzf
  82. # If the command is not found, the package isn't installed
  83. (( $+commands[fzf] )) || return 1
  84. # The fzf-zsh-completion package installs the auto-completion in
  85. local completions="/usr/share/zsh/site-functions/_fzf"
  86. # The fzf-zsh-completion package installs the key-bindings file in
  87. local key_bindings="/etc/zsh_completion.d/fzf-key-bindings"
  88. # If these are not found: (1) maybe we're not on OpenSUSE, or
  89. # (2) maybe the fzf-zsh-completion package isn't installed.
  90. if [[ ! -f "$completions" || ! -f "$key_bindings" ]]; then
  91. return 1
  92. fi
  93. # Auto-completion
  94. if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
  95. source "$completions" 2>/dev/null
  96. fi
  97. # Key bindings
  98. if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
  99. source "$key_bindings" 2>/dev/null
  100. fi
  101. return 0
  102. }
  103. function setup_using_openbsd_package() {
  104. # openBSD installs fzf in /usr/local/bin/fzf
  105. if [[ "$OSTYPE" != openbsd* ]] || (( ! $+commands[fzf] )); then
  106. return 1
  107. fi
  108. # The fzf package installs the auto-completion in
  109. local completions="/usr/local/share/zsh/site-functions/_fzf_completion"
  110. # The fzf package installs the key-bindings file in
  111. local key_bindings="/usr/local/share/zsh/site-functions/_fzf_key_bindings"
  112. # Auto-completion
  113. if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
  114. source "$completions" 2>/dev/null
  115. fi
  116. # Key bindings
  117. if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
  118. source "$key_bindings" 2>/dev/null
  119. fi
  120. return 0
  121. }
  122. function indicate_error() {
  123. cat >&2 <<EOF
  124. [oh-my-zsh] fzf plugin: Cannot find fzf installation directory.
  125. Please add \`export FZF_BASE=/path/to/fzf/install/dir\` to your .zshrc
  126. EOF
  127. }
  128. # Indicate to user that fzf installation not found if nothing worked
  129. setup_using_openbsd_package \
  130. || setup_using_debian_package \
  131. || setup_using_opensuse_package \
  132. || setup_using_base_dir \
  133. || indicate_error
  134. unset -f setup_using_opensuse_package setup_using_debian_package setup_using_base_dir indicate_error
  135. if [[ -z "$FZF_DEFAULT_COMMAND" ]]; then
  136. if (( $+commands[rg] )); then
  137. export FZF_DEFAULT_COMMAND='rg --files --hidden --glob "!.git/*"'
  138. elif (( $+commands[fd] )); then
  139. export FZF_DEFAULT_COMMAND='fd --type f --hidden --exclude .git'
  140. elif (( $+commands[ag] )); then
  141. export FZF_DEFAULT_COMMAND='ag -l --hidden -g "" --ignore .git'
  142. fi
  143. fi