14

I've gotten the Emacs daemon to work through the command line using the EmacsWiki page, but I'm stuck on how to get the client to run through the Gnome 3 launcher. How can I set up a Gnome desktop file that launches a GUI window that connects to the Emacs daemon?

T. Verron
  • 4,233
  • 1
  • 22
  • 55
tbekolay
  • 243
  • 3
  • 9

2 Answers2

20

I'm using Ubuntu, so the location of the desktop file may be different but I think the contents should be similar.

$ cat ~/.local/share/applications/emacsclient.desktop 
#!/usr/bin/env xdg-open

[Desktop Entry]
Version=1.0
Name=GNU Emacs 24
GenericName=Text Editor
Comment=View and edit files
MimeType=text/english;text/plain;text/x-makefile;text/x-c++hdr;text/x-c++src;text/x-chdr;text/x-csrc;text/x-java;text/x-moc;text/x-pascal;text/x-tcl;text/x-tex;application/x-shellscript;text/x-c;text/x-c++;
Exec=/usr/bin/emacsclient -c -a "" %F
Icon=/usr/share/icons/hicolor/scalable/apps/emacs-snapshot.svg
Type=Application
Terminal=false
Categories=Utility;Development;TextEditor;
StartupWMClass=Emacs
Name[en_US]=GNU Emacs 24

The important part is:

Exec=/usr/bin/emacsclient -c -a "" %F

To clarify that should probably be:

Exec=/usr/bin/emacsclient --create-frame --alternate-editor "" %F

From the documentation on emacsclient those switches do the following:

-c, --create-frame      Create a new frame instead of trying to
                        use the current Emacs frame
-a EDITOR, --alternate-editor=EDITOR
                        Editor to fallback to if the server is not running
                        If EDITOR is the empty string, start Emacs in daemon
                        mode and try connecting again

So it makes a new frame using -c and then if emacs is not already daemonized it starts a new emacs daemon with -a "". Finally, the %F fixes it so you can drag a text file onto the icon and it will open it.

Just as an aside you might also look into including -F "((fullscreen . maximized))" to force the frame to maximize at launch. Unfortunately, I can't remember which version of emacs is required for that to work.

dgtized
  • 4,169
  • 20
  • 41
  • 1
    This works otherwise great, but it always opens a new Emacs window. For other launcher icons, an existing window is focused. Is there a way to get the same behavior? – akaihola Nov 02 '17 at 08:05
2

I'm really just tweaking dgtized's answer to address the comment from akaihola. I.e. what we want is to

  • make use of Emacs's daemon functionality
  • summon an existing frame or create one if necessary.

I find that creating an emacs25.desktop file rather than emacsclient.desktop magically worked (frankly, I don't know why).

So I did:

cp /usr/share/applications/emacs25.desktop ~/.local/share/applications

(depending on your system, it may not be emacs25)

Then in my ~/.local/share/applications/emacs25.desktop I changed the Exec line to be:

Exec=/bin/bash --login -c "emacsclient --alternate-editor= --create-frame %F"

The bash login shell makes sure the daemon has my full login environment.

And I removed the TryExec line.

Finally, I edited Name so I'd be able to recognize it in the launcher, tapped Super and right clicked to add my new item to favorites. I always have Emacs the first item for me so then Super+1 summons it.

Name=GNU Emacs client

And all seems to be well.