epipe 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/bin/sh
  2. EPIPE_EMACS=${EPIPE_EMACS:-emacs}
  3. show="NO"
  4. buffer_name=""
  5. interval=1
  6. usage() {
  7. self=$(basename $0)
  8. echo "$self redirects stdin to an emacs buffer"
  9. echo -e "Example: 'echo \"foo\" | epipe'"
  10. echo
  11. echo -e "\t-e CMD\t Use emacs CMD."
  12. echo -e "\t-b NAME\t Piped-to buffer should have NAME."
  13. echo -e "\t-I INT\t Use INT as revert interval."
  14. echo -e "\t-v\t Verbose mode also prints output to stdout."
  15. echo -e "\t-h\t Shows this message."
  16. }
  17. while [ $# -gt 0 ]
  18. do
  19. case $1 in
  20. -v) show="YES"; shift;;
  21. -e) shift; EPIPE_EMACS=$1; shift;;
  22. -b) shift; buffer_name=$1; shift;;
  23. -I) shift; interval=$1; shift;;
  24. -h) usage; exit 0; shift;;
  25. *) echo "Unknown option: '$1'"; usage; exit 1;;
  26. esac
  27. done
  28. tmpfile=$(mktemp --tmpdir=/tmp epipe.XXXXXX)
  29. elisp="
  30. (let ((find-file-hook
  31. '((lambda ()
  32. (turn-on-auto-revert-tail-mode)
  33. (setq auto-revert-interval ${interval})
  34. (auto-revert-set-timer)
  35. (goto-char (point-max))
  36. (cd \"$(pwd)\")
  37. (unless (string-empty-p \"$buffer_name\")
  38. (rename-buffer \"$buffer_name\"))))))
  39. (find-file \"$tmpfile\"))"
  40. ${EPIPE_EMACS} --eval "$elisp" > /dev/null &
  41. if [ "$show" = "NO" ]
  42. then
  43. cat > $tmpfile
  44. else
  45. tee $tmpfile
  46. fi