fzf.plugin.zsh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. function setup_using_base_dir() {
  2. # Declare all variables local not no mess with outside env in any way
  3. local fzf_base
  4. local fzf_shell
  5. local fzfdirs
  6. local dir
  7. test -d "${FZF_BASE}" && fzf_base="${FZF_BASE}"
  8. if [[ -z "${fzf_base}" ]]; then
  9. fzfdirs=(
  10. "${HOME}/.fzf"
  11. "/usr/local/opt/fzf"
  12. "/usr/share/fzf"
  13. "/usr/local/share/examples/fzf"
  14. )
  15. for dir in ${fzfdirs}; do
  16. if [[ -d "${dir}" ]]; then
  17. fzf_base="${dir}"
  18. break
  19. fi
  20. done
  21. if [[ -z "${fzf_base}" ]]; then
  22. if (( ${+commands[brew]} )) && dir="$(brew --prefix fzf 2>/dev/null)"; then
  23. if [[ -d "${dir}" ]]; then
  24. fzf_base="${dir}"
  25. fi
  26. fi
  27. fi
  28. fi
  29. if [[ -d "${fzf_base}" ]]; then
  30. # Fix fzf shell directory for Archlinux package
  31. if [[ ! -d "${fzf_base}/shell" ]] && [[ -f /etc/arch-release ]]; 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 [[ ! "$DISABLE_FZF_AUTO_COMPLETION" == "true" ]]; then
  42. [[ $- == *i* ]] && 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. else
  49. return 1
  50. fi
  51. }
  52. function setup_using_debian_package() {
  53. (( $+commands[dpkg] )) && dpkg -s fzf &> /dev/null
  54. if (( $? )); then
  55. # Either not a debian based distro, or no fzf installed. In any case skip ahead
  56. return 1
  57. fi
  58. # NOTE: There is no need to configure PATH for debian package, all binaries
  59. # are installed to /usr/bin by default
  60. # Determine completion file path: first bullseye/sid, then buster/stretch
  61. local completions="/usr/share/doc/fzf/examples/completion.zsh"
  62. [[ -f "$completions" ]] || completions="/usr/share/zsh/vendor-completions/_fzf"
  63. local key_bindings="/usr/share/doc/fzf/examples/key-bindings.zsh"
  64. # Auto-completion
  65. if [[ $- == *i* ]] && [[ ! "$DISABLE_FZF_AUTO_COMPLETION" == "true" ]]; then
  66. source $completions 2> /dev/null
  67. fi
  68. # Key bindings
  69. if [[ ! "$DISABLE_FZF_KEY_BINDINGS" == "true" ]]; then
  70. source $key_bindings
  71. fi
  72. return 0
  73. }
  74. function indicate_error() {
  75. print "[oh-my-zsh] fzf plugin: Cannot find fzf installation directory.\n"\
  76. "Please add \`export FZF_BASE=/path/to/fzf/install/dir\` to your .zshrc" >&2
  77. }
  78. # Check for debian package first, because it easy to short cut
  79. # Indicate to user that fzf installation not found if nothing worked
  80. setup_using_debian_package || setup_using_base_dir || indicate_error
  81. unset -f setup_using_debian_package setup_using_base_dir indicate_error