1

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.

9
  • 1
    I've just tested it, it just quits Emacs.app entirely.
    – Lenar Hoyt
    Dec 27, 2015 at 20:24
  • 2
    OK, this behaviour seems to be different on OS X and Ubuntu. Try calling the command delete-frame (for me it is bound to s-w or C-x 5 0.
    – elethan
    Dec 27, 2015 at 20:34
  • 1
    That will delete the current frame, but does not kill the current buffer.
    – Lenar Hoyt
    Dec 27, 2015 at 20:36
  • 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, 2015 at 22:20
  • 1
    If you just want to "kill current buffer and frame" without plugins: C-x k RET C-x 5 0. This calls kill-buffer, accepts the default (= current buffer), then calls delete-frame.
    – hraban
    May 13, 2022 at 19:32

2 Answers 2

1

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.)

0

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"))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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