From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Andreas Politz Newsgroups: gmane.emacs.help Subject: Re: automatically save a specific buffer on exit Date: Wed, 04 Feb 2009 22:59:29 +0100 Organization: FH-Trier Message-ID: <1233784831.640257@arno.fh-trier.de> References: <1233779280.689554@arno.fh-trier.de> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1233787805 2524 80.91.229.12 (4 Feb 2009 22:50:05 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 4 Feb 2009 22:50:05 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Feb 04 23:51:19 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1LUqaU-0002yh-P5 for geh-help-gnu-emacs@m.gmane.org; Wed, 04 Feb 2009 23:51:19 +0100 Original-Received: from localhost ([127.0.0.1]:40386 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LUqZB-0004Z5-Qv for geh-help-gnu-emacs@m.gmane.org; Wed, 04 Feb 2009 17:49:57 -0500 Original-Path: news.stanford.edu!headwall.stanford.edu!newsfeed.news2me.com!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.freenet.de!news.n-ix.net!news.belwue.de!news.uni-kl.de!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 64 Original-NNTP-Posting-Host: 143-93-54-11.arno.fh-trier.de Original-X-Trace: news.uni-kl.de 1233784903 14614 143.93.54.11 (4 Feb 2009 22:01:43 GMT) Original-X-Complaints-To: usenet@news.uni-kl.de Original-NNTP-Posting-Date: Wed, 4 Feb 2009 22:01:43 +0000 (UTC) User-Agent: Mozilla-Thunderbird 2.0.0.17 (X11/20081018) In-Reply-To: Cache-Post-Path: arno.fh-trier.de!unknown@dslb-084-059-115-131.pools.arcor-ip.net X-Cache: nntpcache 3.0.1 (see http://www.nntpcache.org/) Original-Xref: news.stanford.edu gnu.emacs.help:166595 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:61907 Archived-At: Thierry Volpiatto wrote: > Hi, > Andreas Politz 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) > `---- > > Yes, I did it like this before (Btw append- and write-file would make things easier). The problem here is, there is no auto-saving involved in this setup and I am currently working with a system that crashes frequently. I just wrote a tiny function, which writes the scratch buffer and calls save-buffers-kill-emacs and bound it to C-xC-c. That seems to be the easiest solution (Which I haven't thought of before, since I was narrowed down to finding the right hook). -ap