When I open a file in Emacs.app, via Finder, via the edit-server or during merging on an SVN update, it shows up in a new frame. When I kill that buffer, I pretty much always also want close the frame that came with it. Is there a way to easily recognize such situations and add an appropriate advice to kill-buffer? Is there perhaps a buffer history for each frame? If that history only contains one buffer, and the frame is not the last remaining one, it would be safe to close.

  • Is there a reason you don't want to just close the frame (effectively killing the buffer as well) with C-x C-c? – elethan Dec 27 '15 at 19:45
  • Doesn't that shut down emacs altogether? – Lenar Hoyt Dec 27 '15 at 19:50
  • If you are talking about separate frames, it should only close the frame you are in when you execute the command. It may be different in your setup, but you could try it. If you are using emacsclient, I am not sure what the result will be. – elethan Dec 27 '15 at 19:52
  • I've just tested it, it just quits Emacs.app entirely. – Lenar Hoyt Dec 27 '15 at 20:24
  • 1
    I'll be using this solution for now: emacs.stackexchange.com/questions/2888/… (thanks to elethan for bringing it to my attention), but perhaps there is a simple solution to my question? – Lenar Hoyt Dec 27 '15 at 22:20

The solution you found here should be fine.

Here is another approach, which I use. To use it you will need to either pull out the relevant code from these libraries or require each of them.

This approach does two things:

  1. Defines an alternative command to kill-buffer, for interactive use: kill-buffer-and-its-windows. I remap all keys bound to the former to the latter, like this:

    (define-key global-map [remap kill-buffer] 'kill-buffer-and-its-windows)
  2. Advises delete-window so that if the window is alone in its frame, then the frame is deleted (using delete-frame).

In other words, when you use C-x k you will get this behavior: the buffer will be killed and its window (or its frame, if it has only one window) will be deleted.

The code:

(As I said, you do not need all of that code to do only what you want. You could just copy the code I mentioned from misc-cmds.el and frame-cmds.el.)

| improve this answer | |

This isn't precisely what you're asking for, but from the discussion in comments, it sounds like it might still work for you. This is a slight modification of the command I use on C-x C-c (I added the buffer-killing feature).

It will kill the current buffer and close the current frame, unless there's only one frame open. In that case, it will ask you for the go-ahead to kill Emacs. If you're using emacsclient, note that the daemon appears to have its own frame in (visible-frame-list), so in this situation you can close the last open frame without killing Emacs entirely.

(defun my/kill-buffer-and-frame-or-exit (&optional arg)
  "Kill this buffer and frame.  With only one frame, exit Emacs.

When there is more than one visible frame, run `delete-frame'.
Otherwise, exit Emacs with `save-buffers-kill-terminal' after
confirming this with user.

If a prefix ARG is supplied, ignore it in the multiple-frame
case.  Otherwise, bypass confirmation and pass the argument to
  (interactive "P")
   ((> (length (visible-frame-list)) 1)  (kill-this-buffer) (delete-frame))
   ((or arg (y-or-n-p "Quit Emacs?"))    (save-buffers-kill-terminal arg))
   (t                                    (message "Abort"))))
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.