fzf.plugin.zsh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. # Determine completion file path: first bullseye/sid, then buster/stretch
  56. local completions="/usr/share/doc/fzf/examples/completion.zsh"
  57. [[ -f "$completions" ]] || completions="/usr/share/zsh/vendor-completions/_fzf"
  58. local key_bindings="/usr/share/doc/fzf/examples/key-bindings.zsh"
  59. # Auto-completion
  60. if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
  61. source $completions 2> /dev/null
  62. fi
  63. # Key bindings
  64. if [[ ! "$DISABLE_FZF_KEY_BINDINGS" == "true" ]]; then
  65. source $key_bindings
  66. fi
  67. return 0
  68. }
  69. function setup_using_opensuse_package() {
  70. # OpenSUSE installs fzf in /usr/bin/fzf
  71. # If the command is not found, the package isn't installed
  72. (( $+commands[fzf] )) || return 1
  73. # The fzf-zsh-completion package installs the auto-completion in
  74. local completions="/usr/share/zsh/site-functions/_fzf"
  75. # The fzf-zsh-completion package installs the key-bindings file in
  76. local key_bindings="/etc/zsh_completion.d/fzf-key-bindings"
  77. # If these are not found: (1) maybe we're not on OpenSUSE, or
  78. # (2) maybe the fzf-zsh-completion package isn't installed.
  79. if [[ ! -f "$completions" || ! -f "$key_bindings" ]]; then
  80. return 1
  81. fi
  82. # Auto-completion
  83. if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
  84. source "$completions" 2>/dev/null
  85. fi
  86. # Key bindings
  87. if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
  88. source "$key_bindings" 2>/dev/null
  89. fi
  90. return 0
  91. }
  92. function setup_using_openbsd_package() {
  93. # openBSD installs fzf in /usr/local/bin/fzf
  94. if [[ "$OSTYPE" != openbsd* ]] || (( ! $+commands[fzf] )); then
  95. return 1
  96. fi
  97. # The fzf package installs the auto-completion in
  98. local completions="/usr/local/share/zsh/site-functions/_fzf_completion"
  99. # The fzf package installs the key-bindings file in
  100. local key_bindings="/usr/local/share/zsh/site-functions/_fzf_key_bindings"
  101. # Auto-completion
  102. if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
  103. source "$completions" 2>/dev/null
  104. fi
  105. # Key bindings
  106. if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
  107. source "$key_bindings" 2>/dev/null
  108. fi
  109. return 0
  110. }
  111. function indicate_error() {
  112. cat >&2 <<EOF
  113. [oh-my-zsh] fzf plugin: Cannot find fzf installation directory.
  114. Please add \`export FZF_BASE=/path/to/fzf/install/dir\` to your .zshrc
  115. EOF
  116. }
  117. # Indicate to user that fzf installation not found if nothing worked
  118. setup_using_openbsd_package \
  119. || setup_using_debian_package \
  120. || setup_using_opensuse_package \
  121. || setup_using_base_dir \
  122. || indicate_error
  123. unset -f setup_using_opensuse_package setup_using_debian_package setup_using_base_dir indicate_error
  124. if [[ -z "$FZF_DEFAULT_COMMAND" ]]; then
  125. if (( $+commands[rg] )); then
  126. export FZF_DEFAULT_COMMAND='rg --files --hidden --glob "!.git/*"'
  127. elif (( $+commands[fd] )); then
  128. export FZF_DEFAULT_COMMAND='fd --type f --hidden --exclude .git'
  129. elif (( $+commands[ag] )); then
  130. export FZF_DEFAULT_COMMAND='ag -l --hidden -g "" --ignore .git'
  131. fi
  132. fi