emacsclient.sh 1005 B

1234567891011121314151617181920212223242526272829
  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 --tmpdir emacs-stdin-$USERNAME.XXXXXXX 2>/dev/null \
  20. || mktemp -t emacs-stdin-$USERNAME)" # support BSD mktemp
  21. cat - > "$tempfile"
  22. _emacsfun --no-wait $tempfile
  23. else
  24. _emacsfun "$@"
  25. fi