__fzf_open.fish 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. function __fzf_open -d "Open files and directories."
  2. function __fzf_open_get_open_cmd -d "Find appropriate open command."
  3. if type -q xdg-open
  4. echo "xdg-open"
  5. else if type -q open
  6. echo "open"
  7. end
  8. end
  9. set -l commandline (__fzf_parse_commandline)
  10. set -l dir $commandline[1]
  11. set -l fzf_query $commandline[2]
  12. if not type -q argparse
  13. set created_argparse
  14. function argparse
  15. functions -e argparse # deletes itself
  16. end
  17. if contains -- --editor $argv; or contains -- -e $argv
  18. set _flag_editor "yes"
  19. end
  20. if contains -- --preview $argv; or contains -- -p $argv
  21. set _flag_preview "yes"
  22. end
  23. end
  24. set -l options "e/editor" "p/preview=?"
  25. argparse $options -- $argv
  26. set -l preview_cmd
  27. if set -q FZF_ENABLE_OPEN_PREVIEW
  28. set preview_cmd "--preview-window=right:wrap --preview='fish -c \"__fzf_complete_preview {}\"'"
  29. end
  30. set -q FZF_OPEN_COMMAND
  31. or set -l FZF_OPEN_COMMAND "
  32. command find -L \$dir -mindepth 1 \\( -path \$dir'*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' \\) -prune \
  33. -o -type f -print \
  34. -o -type d -print \
  35. -o -type l -print 2> /dev/null | sed 's@^\./@@'"
  36. eval "$FZF_OPEN_COMMAND | "(__fzfcmd) $preview_cmd "-m $FZF_DEFAULT_OPTS $FZF_OPEN_OPTS --query \"$fzf_query\"" | read -l select
  37. # set how to open
  38. set -l open_cmd
  39. if set -q _flag_editor
  40. set open_cmd "$EDITOR"
  41. else
  42. set open_cmd (__fzf_open_get_open_cmd)
  43. if test -z "$open_cmd"
  44. echo "Couldn't find appropriate open command to use. Do you have 'xdg-open' or 'open' installed?"; and return 1
  45. end
  46. end
  47. set -l open_status 0
  48. if not test -z "$select"
  49. commandline "$open_cmd \"$select\"" ;and commandline -f execute
  50. set open_status $status
  51. end
  52. commandline -f repaint
  53. return $open_status
  54. end