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.
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:
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)
delete-windowso that if the window is alone in its frame, then the frame is deleted (using
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 for
kill-buffer-and-its-windowsis in library
The code that advises
delete-windowis in librarly
Each of those libraries requires these libraries:
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 `save-buffers-kill-terminal'." (interactive "P") (cond ((> (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"))))