13
votes

I have a question regarding how to "gracefully exit SLIME", when I quit Emacs. Here is the relevant portion of my config file:

;; SLIME configuration

(setq inferior-lisp-program "/usr/local/bin/sbcl")
(add-to-list 'load-path "~/Scripts/slime/")
(require 'slime)
(slime-setup)

;; configure SLIME to gracefully quit when emacs
;; terminates

(defun slime-smart-quit ()
  (interactive)
  (when (slime-connected-p)
    (if (equal (slime-machine-instance) "Gregory-Gelfonds-MacBook-Pro.local")
 (slime-quit-lisp)
      (slime-disconnect)))
  (slime-kill-all-buffers))

(add-hook 'kill-emacs-hook 'slime-smart-quit)

To my knowledge this should automatically kill SLIME and it's associated processes whenever I exit Emacs. However, every time I exit, I still get the prompt:

Proc       Status   Buffer  Command
----       ------   ------  -------
SLIME Lisp    open      *cl-connection* (network stream connection to 127.0.0.1)
inferior-lisp run      *inferior-lisp* /usr/local/bin/sbcl


Active processes exist; kill them and exit anyway? (yes or no) 

Can someone shed some insight as to what I'm missing from my config?

Thanks in advance.

9
Who cares about shortcut keys? We want something to paste into .emacs that quits SLIME when emacs ends.mcandre

9 Answers

25
votes

I know it's not exactly what you asked for, but maybe this will be helpful to other noobs like me.

You can execute a SLIME command to exit, so you'll have a nice, clean emacs left over.

In a SLIME buffer, type in , (comma). You're placed in the minibuffer and SLIME asks which command to execute. Type in sayoonara and hit Enter. You should see SLIME exiting, the minibuffer mentions that "Connection closed." and you're placed in the *scratch* buffer.

I wonder if there's some way to simply invoke this "sayoonara" command from your .emacs, as opposed to manually unwiring everything.

5
votes

According to the manual page on this, "once save-buffers-kill-emacs is finished with all file saving and confirmation, it calls kill-emacs which runs the functions in this hook." So the check for active processes is done before the hook is called.

A working solution would be to make your own kill-emacs command, for example

(defun kill-emacs-slime ()
  (interactive)
  (when (slime-connected-p)
    (if (equal (slime-machine-instance) "Gregory-Gelfonds-MacBook-Pro.local")
        (slime-quit-lisp)
      (slime-disconnect)))
  (slime-kill-all-buffers)
  (save-buffers-kill-emacs))

And then bind this to your quit key

(global-set-key (kbd "C-x C-c") 'kill-emacs-slime)

(I am assuming your function correctly quit SLIME and closes its buffers, I haven't tested it).

3
votes

The problem is the check for active processes (and the query for confirmation to kill) would be activated before kill-emacs-hooks had a chance to do its job.

A very kludgy solution:

(defadvice save-buffers-kill-terminal (before slime-quit activate)
  (slime-smart-quit)
  (sleep-for 1))

The function slime-quit-lisp is asynchronous; it needs time to finish after returning, hence the sleep-for.

2
votes

One way to debug the problem is to debug the function.

Place your cursor inside the 'slime-smart-quit routine and type M-x edebug-defun. Then exit Emacs as you normally would. You should then be prompted by the Emacs lisp debugger edebug. It's a pretty easy debugger to use (type ? for help).

Step through the code and see where it doesn't do what you expect it to do.

Use q to quit out of the debugger, then make changes, and M-x edebug-defun again to debug the new version.

Repeat until you find success, or have a little more information for the question.

2
votes

@Alex - I found your method to be the cleanest way to quit SLIME. However the config file needs to be edited in this fashion for it to be used.

(require 'slime-autoloads)
(slime-setup '(slime-fancy)) ; load contrib packages

Once it's setup this way then:

  1. press comma (,) to bring up the minibuffer
  2. type quit or sayoonara to cleanly exit.

PS: Check this for SLIME config at startup - http://common-lisp.net/project/slime/doc/html/Loading-Contribs.html

1
votes

I find it annoying to have to agree to kill all processes every time I close the Emacs so I come up with this function

(defun emacs-forget-buffer-process ()
  "Emacs will not query about this process when killing."
  (let ((p (get-buffer-process (current-buffer))))
    (when p
      (set-process-query-on-exit-flag p nil))))

which makes process die silently when Emacs is closed. Use it like this

(add-hook 'slime-inferior-process-start-hook #'emacs-forget-buffer-process)
(add-hook 'slime-repl-mode-hook #'emacs-forget-buffer-process)

I use it for all repl-like buffers that I have, which includes octave, python, scheme, shell and haskell's ghci. So far nothing bad happened when these repls get killed silently so I assume this solution isn't bad though may not be graceful.

0
votes

This is a more general solution that I use. It works not just for SLIME, but for other stuff, e.g. python, terminal, lisp etc.

(defadvice save-buffers-kill-emacs (around no-query-kill-emacs activate)
  "Prevent annoying \"Active processes exist\" query when you quit Emacs."
  (flet ((process-list ())) ad-do-it))
0
votes

This took me forever. If you have two windows opened up, switch to the slime window and hit c-x 0 to kill that window. Then you can kill the emacs window normally through c-x c-c.

-1
votes

No, no, no. https://stackoverflow.com/a/10780124/539797 ; any other way of killing a process buffer is at best rude, if not plain dangerous to children and puppies.