[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: automatically save a specific buffer on exit
From: |
Thierry Volpiatto |
Subject: |
Re: automatically save a specific buffer on exit |
Date: |
Wed, 04 Feb 2009 22:11:42 +0100 |
User-agent: |
Gnus/5.110011 (No Gnus v0.11) Emacs/23.0.90 (gnu/linux) |
Hi,
Andreas Politz <politza@fh-trier.de> writes:
> Hi,
>
> I have a persistent scratch buffer with a file attached
> and I would like emacs to have it saved whenever I
> leave the editor.
> 'kill-emacs-hook' runs to late and `write-file-functions'
> doesn't seem to be aproppriate either.
Kill-emacs-hook work fine here.
Here the code i use:
,----
| (defvar save-scratch-file "~/.emacs.d/save-scratch.el")
| (defun tv-save-scratch (&optional append)
| (interactive "P")
| (with-current-buffer "*scratch*"
| (widen)
| (goto-char (point-min))
| (forward-line 1)
| (let* ((beg (point))
| (end (point-max))
| (buffer-contents (buffer-substring beg end)))
| (save-excursion
| (find-file save-scratch-file)
| (if (or current-prefix-arg
| append)
| (progn
| (goto-char (point-max))
| (insert buffer-contents)
| (save-buffer)
| (kill-buffer (current-buffer)))
| (erase-buffer)
| (goto-char (point-min))
| (insert buffer-contents)
| (save-buffer)
| (kill-buffer (current-buffer)))))))
|
| (defun tv-restore-scratch-buffer ()
| (with-current-buffer "*scratch*"
| (goto-char (point-max))
| (forward-line)
| (insert-file-contents save-scratch-file)))
|
| (add-hook 'kill-emacs-hook 'tv-save-scratch)
| (add-hook 'emacs-startup-hook 'tv-restore-scratch-buffer)
`----
--
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France