15

If you create an emacsclient launcher by copying a .desktop file (e.g. the Emacs one), it does not behave in the standard Unity launcher manner. It always launches a new window; it doesn't recognize that existing Emacsclient windows exist to jump to them. How can I make it behave?

As background, this is a very useful usage mode for Emacs, where you run it as a daemon: emacs --daemon, and then you summon an emacs window with emacsclient. A good way to do this for habitual emacs users is to put emacs --daemon into your Startup Applications.

3 Answers 3

19

The solution is to create an emacsclient.desktop file including a directive to it the class of the window using the StartupWMClass settings. Without it, Unity can't tell that that window came from Emacsclient. This will create one based on the contents of the Emacs one (/usr/share/applications/emacs23.desktop):

mkdir -p ~/.local/share/applications
cat > !$/emacsclient.desktop <<EOF
[Desktop Entry]
Version=1.0
Name=Emacsclient
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 %F
Icon=/usr/share/icons/hicolor/scalable/apps/emacs23.svg
Type=Application
Terminal=false
Categories=Utility;Development;TextEditor;
StartupWMClass=Emacs
EOF

Place the emacsclient in the launcher (e.g. tap Super, type "emacsclient", drag it on), and log out/in.

3
  • In my .bashrc, I define TMPDIR=${HOME}/tmp so temporary files are under my encrypted $HOME. emacs, invoked from the launcher, doesn't see this definition, but emacsclient, invoked from a shell does. emacs (after server-start) opens a socket in /tmp, emacsclient tries to communicate with emacs over a socket in ${HOME}/tmp, and does very poorly. Can I use emacsclient.desktop to pass the right value for TMPDIR?
    – waltinator
    Sep 28, 2011 at 19:40
  • I moved my question to a thread of its own, and got answered. See askubuntu.com/questions/64005/launcher-doesnt-respect-tmpdir/…
    – waltinator
    Nov 6, 2011 at 23:20
  • Unity totally ignored my emacsclient.desktop for some reason. I eventually got around this by creating the launcher in alacarte (the "Main Menu" GUI) instead.
    – Brian Z
    Mar 7, 2015 at 9:50
3

Solution by james.ferguson works well. What I wanted was to link emacsclient to so some file types as the "preferred application". For this, I found james's script with a new "Exec" line

Exec=/usr/bin/emacsclient --alternate-editor emacs24 --no-wait %F

works better for me: it runs a new emacs if none is running, does not open a new window and does not make emacs complain about clients still waiting for stuff to happen.

Now I can click on files and they are opened in a running emacs, if one exists, otherwise emacs starts.

1

Another way is to use a script:

if [ "$(pidof emacs)" ] ; then
    emacsclient "$@" &
else
    emacs -mm "$@" &
fi

So you always invoke the same command.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .