epipe 1.1 KB

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