emacsclient.sh 856 B

123456789101112131415161718192021222324252627
  1. #!/bin/sh
  2. emacsfun() {
  3. local frames="$(emacsclient --alternate-editor "" -n -e "(length (frame-list))" 2>/dev/null)"
  4. # Only create another X frame if there isn't one present
  5. if [ -z "$frames" -o "$frames" -lt 2 ]; then
  6. emacsclient --alternate-editor "" --create-frame "$@"
  7. return $?
  8. fi
  9. emacsclient --alternate-editor "" "$@"
  10. }
  11. # adopted from https://github.com/davidshepherd7/emacs-read-stdin/blob/master/emacs-read-stdin.sh
  12. # If the second argument is - then write stdin to a tempfile and open the
  13. # tempfile. (first argument will be `--no-wait` passed in by the plugin.zsh)
  14. if [ $# -ge 2 -a "$2" = "-" ]; then
  15. tempfile="$(mktemp --tmpdir emacs-stdin-$USERNAME.XXXXXXX 2>/dev/null \
  16. || mktemp -t emacs-stdin-$USERNAME)" # support BSD mktemp
  17. cat - > "$tempfile"
  18. emacsfun --no-wait "$tempfile"
  19. return $?
  20. fi
  21. emacsfun "$@"