How can I run Mac OSX graphical Emacs in daemon mode?

24

15

Short form: I want to run the Mac OSX version of Emacs found on http://emacsformacosx.com/ as a daemon, with graphical and text clients connecting to it as-needed.

In detail, here is the workflow I want:

  1. On login, run /Applications/Emacs.app/Contents/MacOS/Emacs --daemon to start emacs as a daemon. I already know how to do this.

  2. When running emacs from a local termnal, I want Either one of these:

    1. If I run emacs from a local command prompt, open a new frame with the selected file.
    2. If #1 is not possible, at least if I run emacsclient from the command line, open a new frame with the selected file.
  3. When running emacs from a remote terminal, I would like a text-mode emacs to start, connected to the daemon process.

  4. If I run Emacs.app re-use the emacs daemon that is running in the background instead of starting a new instance and open a new window on it.

So far, I am able to accomplish only #1 and #2.2 above, but those are the easy parts. #3 and #4 are the things that are most needed in order to make this work. Does anyone know how to accomplish these things?

Chris R

Posted 2009-10-02T16:25:50.003

Reputation: 1 751

1For others who find this question: you can also usr M-x server-start to put an emacs started without --daemon into server mode. Good questions, BTW. – dmckee --- ex-moderator kitten – 2009-10-02T17:47:31.353

1... or put (server-start) in your .emacs so that it starts when you launch emacsa – Doug Harris – 2009-10-03T12:24:09.127

Can someone explain how to do # 1? Would you just make it a login item? – Charlie Flowers – 2012-09-21T05:35:55.670

1

@Charlie Flowers http://www.emacswiki.org/emacs/EmacsAsDaemon#toc5

– Colin – 2014-06-09T00:32:38.243

Answers

15

  1. You've got this handled
  2. Yes, you'll want to use emacsclient. To get a new frame, add the -c option. I have an alias to make it quicker and to tell emacsclient not to wait for emacs server to return:

    alias em='emacsclient -n'
    
  3. emacsclient -nw
  4. I'm not sure about this. I start Emacs once and always load new files either within emacs or using my em alias.

Update:

Check out this tip documenting how to use Platypus to create an app wrapper around emacsclient. You could then use "Open With..." to launch using your new app wrapper.

Doug Harris

Posted 2009-10-02T16:25:50.003

Reputation: 23 578

6Is this solved? How did you get Emacs.app to connect to the daemon? – milkypostman – 2011-03-29T14:57:15.967

I also want to know the answer to question 4 :) – jkp – 2011-09-04T13:56:00.233

Yeah, that's the real trick, isn't it? Because what I want is for all of the OS open calls to use the running emacs, whether I'm operating within a terminal or a GUI context. I'll keep looking. – Chris R – 2009-10-04T15:11:55.700

Found a ref to Platypus and updated my answer. – Doug Harris – 2009-10-05T03:29:01.433

2

Yes, here is an pretty comprehensive explain about how to do this nicely. I tried several methods to get it working nicely, but no better than what they described here.

https://gist.github.com/304964

Cheers!

Spike

Posted 2009-10-02T16:25:50.003

Reputation: 784

1The link is dead – Nisba – 2017-07-12T11:11:07.590

1

For 4. put this in your .emacs:

(setq ns-pop-up-frames nil)

The solution of creating a Mac .app (with Platypus) does not work because Mac won't open the same application twice.

juanmirocks

Posted 2009-10-02T16:25:50.003

Reputation: 111

1If you use the "open -n" you can open a new instance of an application even if it's already running. Try this in the terminal: open -n -a Emacs.app somefile.txt – Jörgen Lundberg – 2013-04-04T09:35:49.010

Yeah, but it opens a new window, which I don't want – juanmirocks – 2013-04-04T14:01:15.900

Maybe I'm misunderstanding what you want, but if you don't want it to create a new window but use an existing one, skip the '-n'. – Jörgen Lundberg – 2013-04-04T17:54:30.720

Yes, I don't want to create a new window. open -a Emacs.app is certainly what I do, but to be opened in the same instance/daemon background of an existing emacs, I need to write (setq ns-pop-up-frames nil) in my .emacs as I wrote in my answer. Otherwise it creates a new window anyway, with or without -n. – juanmirocks – 2013-04-05T06:59:46.427