Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to autosave temporary buffers that are not visiting a file?

Tags:

emacs

elisp

Suppose I'm entering a bunch of text in a buffer that's not visiting a file (it could be a new org2blog post, or just some scratch buffer). Is it possible to autosave it somewhere in case disaster strikes and Emacs dies?

like image 585
Leo Alekseyev Avatar asked Jan 13 '12 11:01

Leo Alekseyev


2 Answers

auto-save-mode actually works with non-file buffers. It just doesn't get enabled by default -- that usually happens in (after-find-file).

So: M-x auto-save-mode RET

By default the auto-save file will be written to the default-directory of the buffer (or /var/tmp or ~/, depending on write permissions; see C-hv buffer-auto-save-file-name RET) which may be a little awkward to figure out after a crash, so setting that to something standard is probably a good idea.

The following would ensure that these auto-save files are written to your home directory (or M-x customize-variable RET my-non-file-buffer-auto-save-dir RET), if auto-save-mode is invoked interactively. That will hopefully avoid this conflicting with any other uses of auto-save-mode with non-file buffers (the code mentions Mail mode, for instance).

(defcustom my-non-file-buffer-auto-save-dir (expand-file-name "~/")
  "Directory in which to store auto-save files for non-file buffers,
when `auto-save-mode' is invoked manually.")

(defadvice auto-save-mode (around use-my-non-file-buffer-auto-save-dir)
  "Use a standard location for auto-save files for non-file buffers"
  (if (and (not buffer-file-name)
           (called-interactively-p 'any))
      (let ((default-directory my-non-file-buffer-auto-save-dir))
        ad-do-it)
    ad-do-it))
(ad-activate 'auto-save-mode)
like image 64
phils Avatar answered Nov 09 '22 22:11

phils


phils' answer cleared things up for me, but I ended up using a somewhat different approach. I am posting it as a separate answer for documentation's sake. Here is my autosave stanza:

;; Put autosave files (ie #foo#) in one place
(defvar autosave-dir (concat "~/.emacs.d/autosave.1"))
(defvar autosave-dir-nonfile (concat "~/.emacs.d/autosave.nonfile"))
(make-directory autosave-dir t)
(make-directory autosave-dir-nonfile t)
(defun auto-save-file-name-p (filename) (string-match "^#.*#$" (file-name-nondirectory filename)))
(defun make-auto-save-file-name () 
  (if buffer-file-name (concat autosave-dir "/" "#" (file-name-nondirectory buffer-file-name) "#")
    (expand-file-name (concat autosave-dir-nonfile "/" "#%" 
                              (replace-regexp-in-string "[*]\\|/" "" (buffer-name)) "#"))))

Creating a separate directory for the non-visited file buffers is optional in this context; they could as well go in the centralized location (in this case, autosave-dir). Note also that I have to do some basic file name cleanup in case the temporary buffer name is something like "*foo/bar*" (with stars and/or slashes).

Finally, one can automatically turn on autosave in certain modes' temp buffers using something like

(add-hook 'org2blog/wp-mode-hook '(lambda () (auto-save-mode t)))
like image 39
Leo Alekseyev Avatar answered Nov 09 '22 21:11

Leo Alekseyev