简体   繁体   中英

Use GUI Emacs on OS X when opening files with emacsclient

I'm trying to setup Emacs (which I installed via brew install emacs --cocoa ) in a way that I can use it the in following way:

  1. emacsclient file.txt creates a new GUI Frame if there is none already
  2. emacsclient file2.txt replaces the contents of the existing frame if there is one
  3. emacsclient should always return immediately and not block the shell it has been called from
  4. At any given point in time there should basically be one Emacs window and one emacs --daemon process

As I said I installed Emacs via homebrew. To have a daemon starting when I log in I added ~/Library/LaunchAgents/org.gnu.emacs.plist with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>org.gnu.emacs</string>
    <key>ProgramArguments</key>
    <array>
      <string>/usr/local/Cellar/emacs/HEAD/bin/emacs</string>
      <string>--daemon</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
  </dict>
</plist>

I confirmed that this starts the process. So far I tried various variants to call emacsclient:

  1. /usr/local/Cellar/emacs/HEAD/bin/emacsclient file.txt this opens emacs in my terminal
  2. /usr/local/Cellar/emacs/HEAD/bin/emacsclient -c file.txt this opens up a new Emacs GUI frame (good!) but it somehow does not have the same font-size/typeface settings. Also when I close that frame and open another file with emacsclient -c the Emacs process seems to crash.
  3. /usr/local/Cellar/emacs/HEAD/bin/emacsclient -n file.txt just exits right away

I'm not sure what I'm supposed to do. I've found a lot of tutorials on this but many weren't clear if the objective is to get a daemon working just for terminal usage of emacs etc.

I'd be very interested to hear how you use Emacs on OS X, especially how you setup an Emacs daemon and how/if you are using the GUI variant.

As it happens so often I found a solution while writing this. Putting the following into my init.el allowed me to open files in the existing GUI frame.

(require 'server)
(unless (server-running-p)
  (server-start))

I assume that this works because the server process is bound to the GUI instance of Emacs but I'm still very open to someone else enlightening me on this.

As far as 2019 goes, this answer really works for me. This is my configuration for using emacsclient and emacs daemon in macos Mojave with emacs-mac v.26.2 :

1) First, I put this line in my init file:

(server-start)

As EmacsWiki and Martin explain, this line is necessary for link the GUI Emacs to the server it creates.

2) Then, I set the Emacs GUI to start at the beginning of my mac session (there is no magic here, It is set as any other application).

3) Finally, I create a shortcut for this terminal command using automator:

/usr/local/Cellar/emacs-mac/emacs-26.2-z-mac-7.6/bin/emacsclient -c -a ""

and bound it to any keybinding of my preference (I set mine to shift + super + e) using the keyboard menu of my mac preferences.

After this, you are set to go. Emacs GUI will start when the session begins and with just a fast shortcut it will quickly open an emacsclient frame.

PD: Do NOT close the emacs GUI, It is essential to keep it running as emacsclient needs it to create a new graphical frame. See the documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM