Not killing Emacs on Windows

On windows, many people use the built-in server and emacsclientw to make startup faster. The basics for this are discussed here. I have a slightly modified version that closes a buffer opened through the server mode. I often open up code through emacsclientw, edit, save then close.

The following code helps make this a more automatic process and give the feel of opening the editor, editing and then closing through normal keystrokes.

(defun bnb/exit ()
  (interactive)
;  Check for a server-buffer before closing the server-buffer
  (if server-clients
      (server-edit))
  (make-frame-invisible nil t))
(global-set-key (kbd "C-x C-c") 'bnb/exit)

While that code is great, it does not stop me from clicking the X to close emacs. To ensure that this does not kill emacs, I advise the kill-emacs function. The advice wraps around the kill-emacs function and only calls the underlying function if the bnb/really-kill-emacs variable is set. If the variable is still nil, then the frame is simply hidden (turned invisible) through the method above. I then have a utility function to kill emacs if that need ever arises.

(defvar bnb/really-kill-emacs nil)
(defadvice kill-emacs (around bnb/really-exit activate)
  "Only kill emacs if a prefix is set"
  (if bnb/really-kill-emacs
      ad-do-it)
    (bnb/exit))

(defun bnb/really-kill-emacs ()
  (interactive)
  (setq bnb/really-kill-emacs t)
  (kill-emacs))

Together these help me have a fluid editing environment that is always ready but out of my way.

Published on Monday, January 04, 2010. Posted in: elisp, emacs, programming, tools.