fzf.plugin.zsh 2.8 KB

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