fzf.plugin.zsh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. "${HOME}/.nix-profile/share/fzf"
  12. "/usr/local/opt/fzf"
  13. "/usr/share/fzf"
  14. "/usr/local/share/examples/fzf"
  15. )
  16. for dir in ${fzfdirs}; do
  17. if [[ -d "${dir}" ]]; then
  18. fzf_base="${dir}"
  19. break
  20. fi
  21. done
  22. if [[ -z "${fzf_base}" ]]; then
  23. if (( ${+commands[brew]} )) && dir="$(brew --prefix fzf 2>/dev/null)"; then
  24. if [[ -d "${dir}" ]]; then
  25. fzf_base="${dir}"
  26. fi
  27. fi
  28. fi
  29. fi
  30. if [[ -d "${fzf_base}" ]]; then
  31. # Fix fzf shell directory for Arch Linux, NixOS or Void Linux packages
  32. if [[ ! -d "${fzf_base}/shell" ]]; then
  33. fzf_shell="${fzf_base}"
  34. else
  35. fzf_shell="${fzf_base}/shell"
  36. fi
  37. # Setup fzf binary path
  38. if ! (( ${+commands[fzf]} )) && [[ ! "$PATH" == *$fzf_base/bin* ]]; then
  39. export PATH="$PATH:$fzf_base/bin"
  40. fi
  41. # Auto-completion
  42. if [[ ! "$DISABLE_FZF_AUTO_COMPLETION" == "true" ]]; then
  43. [[ $- == *i* ]] && source "${fzf_shell}/completion.zsh" 2> /dev/null
  44. fi
  45. # Key bindings
  46. if [[ ! "$DISABLE_FZF_KEY_BINDINGS" == "true" ]]; then
  47. source "${fzf_shell}/key-bindings.zsh"
  48. fi
  49. else
  50. return 1
  51. fi
  52. }
  53. function setup_using_debian_package() {
  54. (( $+commands[dpkg] )) && dpkg -s fzf &> /dev/null
  55. if (( $? )); then
  56. # Either not a debian based distro, or no fzf installed. In any case skip ahead
  57. return 1
  58. fi
  59. # NOTE: There is no need to configure PATH for debian package, all binaries
  60. # are installed to /usr/bin by default
  61. # Determine completion file path: first bullseye/sid, then buster/stretch
  62. local completions="/usr/share/doc/fzf/examples/completion.zsh"
  63. [[ -f "$completions" ]] || completions="/usr/share/zsh/vendor-completions/_fzf"
  64. local key_bindings="/usr/share/doc/fzf/examples/key-bindings.zsh"
  65. # Auto-completion
  66. if [[ $- == *i* ]] && [[ ! "$DISABLE_FZF_AUTO_COMPLETION" == "true" ]]; then
  67. source $completions 2> /dev/null
  68. fi
  69. # Key bindings
  70. if [[ ! "$DISABLE_FZF_KEY_BINDINGS" == "true" ]]; then
  71. source $key_bindings
  72. fi
  73. return 0
  74. }
  75. function indicate_error() {
  76. print "[oh-my-zsh] fzf plugin: Cannot find fzf installation directory.\n"\
  77. "Please add \`export FZF_BASE=/path/to/fzf/install/dir\` to your .zshrc" >&2
  78. }
  79. # Check for debian package first, because it easy to short cut
  80. # Indicate to user that fzf installation not found if nothing worked
  81. setup_using_debian_package || setup_using_base_dir || indicate_error
  82. unset -f setup_using_debian_package setup_using_base_dir indicate_error
  83. if [[ -z "$FZF_DEFAULT_COMMAND" ]]; then
  84. if (( $+commands[rg] )); then
  85. export FZF_DEFAULT_COMMAND='rg --files --hidden'
  86. elif (( $+commands[fd] )); then
  87. export FZF_DEFAULT_COMMAND='fd --type f --hidden --exclude .git'
  88. elif (( $+commands[ag] )); then
  89. export FZF_DEFAULT_COMMAND='ag -l --hidden -g ""'
  90. fi
  91. fi