tags:

views:

198

answers:

2

Every now and then, I accidentally hit C-x C-c in Emacs when I'm intending to just hit C-x or C-c. This, of course, closes all open frames and buffers with no confirmation. I know that I can make Emacs prompt "Are you sure you want to exit?", but I don't want to do that all the time, which would get annoying. I just want it to do it when there are more than N files (or buffers) open.

So I'd like to bind C-x C-c to a function along the lines of:

(if (< number of open buffers n)
    (save-buffers-kill-emacs)
    (are-you-sure))

But I can't figure out how to get the number of open buffers (or the number of open frames, or the number of open files, etc).

+6  A: 

This is what I use:

(defun count-buffers (&optional display-anyway)
  "Display or return the number of buffers."
  (interactive)
  (let ((buf-count (length (buffer-list))))
    (if (or (interactive-p) display-anyway)
    (message "%d buffers in this Emacs" buf-count)) buf-count))

I stole it , but can't remember from where. from John Sturdy, who sounds like a fascinating fellow; it's available from his website.

JasonFruit
+4  A: 

Also, you can consider using desktop-mode, which will automatically restore your buffers when you start Emacs again. Just add

(desktop-save-mode 1)

to your .emacs. See GNU Emacs manual or Emacswiki.

ShreevatsaR