emacsclient.sh 915 B

12345678910111213141516171819202122232425262728
  1. #!/bin/sh
  2. _emacsfun()
  3. {
  4. # get list of emacs frames.
  5. frameslist=`emacsclient --alternate-editor '' --eval '(frame-list)' 2>/dev/null | egrep -o '(frame)+'`
  6. if [ "$(echo "$frameslist" | sed -n '$=')" -ge 2 ] ;then
  7. # prevent creating another X frame if there is at least one present.
  8. emacsclient --alternate-editor "" "$@"
  9. else
  10. # Create one if there is no X window yet.
  11. emacsclient --alternate-editor "" --create-frame "$@"
  12. fi
  13. }
  14. # adopted from https://github.com/davidshepherd7/emacs-read-stdin/blob/master/emacs-read-stdin.sh
  15. # If the second argument is - then write stdin to a tempfile and open the
  16. # tempfile. (first argument will be `--no-wait` passed in by the plugin.zsh)
  17. if [ "$#" -ge "2" -a "$2" = "-" ]
  18. then
  19. tempfile="$(mktemp emacs-stdin-$USER.XXXXXXX --tmpdir)"
  20. cat - > "$tempfile"
  21. _emacsfun --no-wait $tempfile
  22. else
  23. _emacsfun "$@"
  24. fi