2009-08-19

how to prevent emacs losing unsafed un-file buffers when quit

On Aug 19, 5:22 pm, YOUNG wrote:
> Hi,
>
> If a buffer does not connect to a file but it is modified, if you type
> c-x c, emacs just quits and losts the data.
>
> Is there a way to protect of it?

I don't think there's a default way. This is one of the problem that pains me.

But if you use ergoemacs
http://code.google.com/p/ergoemacs/

it's fixed there.

--------------------------------

On Aug 19, 7:09 pm, Xah Lee wrote:
> On Aug 19, 5:22 pm, YOUNG wrote:
>
> > Hi,
>
> > If a buffer does not connect to a file but it is modified, if you type
> > c-x c, emacs just quits and losts the data.
>
> > Is there a way to protect of it?
>
> I don't think there's a default way. This is one of the problem that
> pains me.
>
> But if you use ergoemacshttp://code.google.com/p/ergoemacs/
>
> it's fixed there.

O, just tried to reproduce your situation but there's a bit warning.

When you use ergoemacs, you can create buffers by Ctrl+n, which calls new-empty-buffer. Then, in this buffer, if modified and not saved, when you try to close it or quit emacs, emacs will prompt you to save it.

However, it does not fix when if you just use switch-to-buffer (C-x b) command to create a new buffer.

Here's the relevant source code. Note the line
(setq buffer-offer-save t).
Feel free to use it.

(defun new-empty-buffer ()
"Opens a new empty buffer."
(interactive)
(let ((buf (generate-new-buffer "untitled")))
(switch-to-buffer buf)
(funcall (and initial-major-mode))
(setq buffer-offer-save t)))
;; note: emacs won't offer to save a buffer that's
;; not associated with a file,
;; even if buffer-modified-p is true.
;; One work around is to define your own my-kill-buffer function
;; that wraps around kill-buffer, and check on the buffer modification
;; status to offer save
;; This custome kill buffer is close-current-buffer.

Xah
∑ http://xahlee.org/

No comments:

Post a Comment