fzf.plugin.zsh 5.2 KB

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