2009-03-08

windows and daemons

Update: Scott has updated instructions -- please see this Reddit page for all the details. Thanks Scott!
[Today, a nice trick from guest blogger Scott Turner (thanks Scott!), on how to improve emacsclient behavior on Windows (see emacs --daemon for a discussion of a daemonized emacs on Unix/Linux). As Scott mentions, there is a nice Emacs-for-Windows package called EmacsW32; the people behind that did a very nice job. It makes the Windows editing experience so much nicer, even when sometimes Emacs' non-Windows origins shine through.

If you'd also like to contribute to Emacs-Fu, please contact me by e-mail (rot13 "qwpo@qwpofbsgjner.ay") or IM (rot13 "qwpo@wnoore.bet").]


First, I recommend using the W32 package. W32 has hacked the emacsclient in several useful ways. It will start Emacs if it isn't already running, it will call "raise-frame" on the Emacs window when started, and it permits starting emacsclient without a filename.

Second, put the following in your .emacs file:

(defun my-done ()
  (interactive)                                                                                     
  (server-edit)
  (make-frame-invisible nil t))                                                                     
(global-set-key (kbd "C-x C-c") 'my-done)
Now when you "exit" Emacs with the familiar key sequence, what actually happens is that the server buffers (from emacsclientw) are saved and deleted and then the frame (Emacs window) is made invisible. You can check the task manager to see that Emacs is indeed still running.

When you next invoke Emacs through emacsclient, the Emacs window is "raised", making it visible,and switched to the emacsclient buffer.

The end result is a single reusable Emacs which is hidden off-screen when not in use.

(If you're used to using C-x # to exit from an emacsclient session, you might want to bind the my-done function to that key combination. That's harder than you might expect, though. If you try to do that in your .emacs file, the key binding will be overwritten when Emacs starts server-mode. One option is to force the loading of server.el before you change the keybinding. Another is to hang the keybinding on server-visit-hook.)

You can probably achieve much the same effect in a vanilla Emacs 23 package, but you'll have to use the alternate editor argument to emacsclient to start up an Emacs if one isn't running and enter server-mode, and the eval argument to execute raise-frame and make the Emacs window visible again.

5 comments:

kbm said...

server-edit does not work for me: function definition void. Will get latest emacs w32 and see if that does it.

OtherMichael said...

Exit emacs? I had to do that recently when I restarted the machine.... but normally, I leave it running with a bazillion buffers open.

Anonymous said...

good idea. Working well with Linux too. Better than --daemon mode.

Unknown said...

Thanks for the tip. How do I exit the daemon though?

Marty N. said...

@Keith I had to add (require 'server) to my .emacs for (server-edit) to be resolved.