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