ec 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/bin/bash
  2. # This script starts emacs daemon if it is not running, opens whatever file
  3. # you pass in and changes the focus to emacs. Without any arguments, it just
  4. # opens the current buffer or *scratch* if nothing else is open. The following
  5. # example will open ~/.bashrc
  6. # ec ~/.bashrc
  7. # You can also pass it multiple files, it will open them all. Unbury-buffer
  8. # will cycle through those files in order
  9. # The compliment to the script is et, which opens emacs in the terminal
  10. # attached to a daemon
  11. # If you want to execute elisp, pass in -e whatever.
  12. # You may also want to stop the output from returning to the terminal, like
  13. # ec -e "(message \"Hello\")" > /dev/null
  14. # emacsclient options for reference
  15. # -a "" starts emacs daemon and reattaches
  16. # -c creates a new frame
  17. # -n returns control back to the terminal
  18. # -e eval the script
  19. # Number of current visible frames,
  20. # Emacs daemon always has a visible frame called F1
  21. visible_frames() {
  22. emacsclient -a "" -e '(length (visible-frame-list))'
  23. }
  24. change_focus() {
  25. emacsclient -n -e "(select-frame-set-input-focus (selected-frame))" > /dev/null
  26. }
  27. # with macport of emacs, this has issues.
  28. # https://github.com/railwaycat/emacs-mac-port/issues/49
  29. #Let's just open one and then start the server
  30. #open -a Emacs.app
  31. # try switching to the frame incase it is just minimized
  32. # will start a server if not running
  33. test "$(visible_frames)" -eq "1" && change_focus
  34. if [ "$(visible_frames)" -lt "2" ]; then # need to create a frame
  35. # -c $@ with no args just opens the scratch buffer
  36. emacsclient -n -c "$@" && change_focus
  37. else # there is already a visible frame besides the daemon, so
  38. change_focus
  39. # -n $@ errors if there are no args
  40. test "$#" -ne "0" && emacsclient -n "$@"
  41. fi