annotate lisp/packages/saveconf.el @ 36:c53a95d3c46d r19-15b101

Import from CVS: tag r19-15b101
author cvs
date Mon, 13 Aug 2007 08:53:38 +0200
parents ac2d302a0011
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; Save Emacs buffer and window configuration between editing sessions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 ;;; Copyright (C) 1987, 1988, 1989 Kyle E. Jones
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 ;;; This program is free software; you can redistribute it and/or modify
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;;; it under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;;; the Free Software Foundation; either version 1, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;;; This program is distributed in the hope that it will be useful,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;;; GNU General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;;; A copy of the GNU General Public License can be obtained from the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;;; program's author (send electronic mail to kyle@cs.odu.edu) or from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;;; 02139, USA.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 ;;; Send bug reports to kyle@cs.odu.edu.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 ;;; Synched up with: Not in FSF.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 ;; This package of functions gives Emacs the ability to remember which
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 ;; files were being visited, the windows that were on them, and the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 ;; value of point in their buffers the last Emacs session in the same
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 ;; directory. This is an emulation of an old Gosling Emacs feature.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 ;; The relevant commands are save-context and recover-context.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;; Most of the time you'll want an Emacs session's context saved even if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;; you choose not to recover it later. To avoid having to manually
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ;; M-x save-context at each emacs exit, put the line:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 ;; (setq auto-save-and-recover-context t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 ;; in your .emacs or in default.el in the lisp directory of the Emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;; distribution. The context will then automatically be saved when
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;; Emacs exits.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;; By default only the contexts of visible buffers (buffers with windows
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 ;; on them) are saved. Setting the variable save-buffer-context to t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 ;; causes the contexts of all buffers to be saved.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 ;; Put this file in the "lisp" directory of the emacs distribution in a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 ;; file called saveconf.el. Byte-compile it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 ;; There are two ways to use this package.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 ;; 1) Put the line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 ;; (require 'saveconf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 ;; in the file site-init.el in the lisp directory of the Emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 ;; directory and rebuild Emacs. If you get the "Pure Lisp storage
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 ;; exhausted" error message when rebuilding Emacs, increase PURESIZE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 ;; in src/config.h by about 30000 bytes and try again. It's almost
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 ;; certain that this will happen to you so you might as well increase
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 ;; PURESIZE beforehand.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 ;; This is the preferred mode of operation because it allows the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 ;; package to become part of Emacs' startup sequence and automatically
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 ;; restore context in a directory if Emacs is invoked without any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 ;; command line arguments.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 ;; 2) Put these lines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 ;; (require 'saveconf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 ;; (if (null (cdr command-line-args))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 ;; (setq inihibit-startup-message (recover-context)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 ;; at the end of your .emacs file or the default.el file in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 ;; lisp directory of the Emacs distribution. This causes the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 ;; context saved in the current directory to be recovered whenever
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 ;; Emacs is invoked without any arguments.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 (provide 'saveconf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 (defconst save-context-version "Norma Jean"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 "A unique string which is placed at the beginning of every saved context
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 file. If the string at the beginning of the context file doesn't match the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 value of this variable the `recover-context' command will ignore the file's
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 contents.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 (defvar auto-save-and-recover-context nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 "*If non-nil the `save-context' command will always be run before Emacs is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 exited. Also upon Emacs startup, if this variable is non-nil and Emacs is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 passed no command line arguments, `recover-context' will be run.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 (defvar save-buffer-context nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 "*If non-nil the `save-context' command will save the context
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 of buffers that are visiting files, as well as the contexts of buffers
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 that have windows.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (defvar save-context-predicate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 (function (lambda (w)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 (and (buffer-file-name (window-buffer w))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 (not (string-match "^\\(/usr\\)?/tmp/"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 (buffer-file-name (window-buffer w)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 "*Value is a predicate function which determines which windows' contexts
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 are saved. When the `save-context' command is invoked, this function will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 be called once for each existing Emacs window. The function should accept
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 one argument which will be a window object, and should return non-nil if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 the window's context should be saved.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 ;; kill-emacs' function definition must be saved
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 (if (not (fboundp 'just-kill-emacs))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 (fset 'just-kill-emacs (symbol-function 'kill-emacs)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 ;; Make Emacs call recover-context at startup if appropriate.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (setq top-level
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (list 'let '((starting-up (not command-line-processed)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (list 'prog1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 top-level
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 '(and starting-up auto-save-and-recover-context
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (null (cdr command-line-args)) (recover-context)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (defun kill-emacs (&optional query)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 "End this Emacs session.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 Prefix ARG or optional first ARG non-nil means exit with no questions asked,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 even if there are unsaved buffers. If Emacs is running non-interactively
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 and ARG is an integer, then Emacs exits with ARG as its exit code.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 If the variable `auto-save-and-restore-context' is non-nil,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 the function save-context will be called first."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 (interactive "P")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 ;; check the purify flag. try to save only if this is a dumped Emacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 ;; saving context from a undumped Emacs caused a NULL pointer to be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 ;; referenced through. I'm not sure why.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 (if (and auto-save-and-recover-context (null purify-flag))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 (save-context))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 (just-kill-emacs query))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 (defun save-context ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 "Save context of all Emacs windows (files visited and position of point).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 The information goes into a file called .emacs_<username> in the directory
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 where the Emacs session was started. The context can be recovered with the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 `recover-context' command, provided you are in the same directory where
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 the context was saved.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 If the variable `save-buffer-context' is non-nil, the context of all buffers
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 visiting files will be saved as well.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 Window sizes and shapes are not saved, since these may not be recoverable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 on terminals with a different number of rows and columns."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 (condition-case error-data
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 (let (context-buffer mark save-file-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (setq save-file-name (concat (original-working-directory)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 ".emacs_" (user-login-name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 (if (not (file-writable-p save-file-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 (if (file-writable-p (original-working-directory))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 (error "context is write-protected, %s" save-file-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 (error "can't access directory, %s"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (original-working-directory))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 ;; set up a buffer for the saved context information
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 ;; Note that we can't set the visited file yet, because by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 ;; giving the buffer a file to visit we are making it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 ;; eligible to have it's context saved.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 (setq context-buffer (get-buffer-create " *Context Info*"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 (set-buffer context-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 (erase-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 (set-buffer-modified-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 ;; record the context information
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (mapcar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 (lambda (w)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 (cond ((funcall save-context-predicate w)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 (prin1 (buffer-file-name (window-buffer w)) context-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 (princ " " context-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 (prin1 (window-point w) context-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 (princ "\n" context-buffer)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 (window-list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 ;; nil is the data sentinel. We will insert it later if we
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 ;; need it but for now just remember where the last line of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 ;; window context ended.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 (setq mark (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 ;; If `save-buffer-context' is non-nil we save buffer contexts.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 (if save-buffer-context
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 (mapcar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 (lambda (b)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 (set-buffer b)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 (cond (buffer-file-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 (prin1 buffer-file-name context-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 (princ " " context-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 (prin1 (point) context-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 (princ "\n" context-buffer)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 (buffer-list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 ;; If the context-buffer contains information, we add the version
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 ;; string and sentinels, and write out the saved context.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 ;; If the context-buffer is empty, we don't create a file at all.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 ;; If there's an old saved context in this directory we attempt
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 ;; to delete it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 (cond ((buffer-modified-p context-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 (set-buffer context-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 (setq buffer-offer-save nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 ;; sentinel for EOF
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 (insert "nil\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 ;; sentinel for end of window contexts
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 (goto-char mark)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 (insert "nil\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 ;; version string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 (prin1 save-context-version context-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 (insert "\n\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 ;; so kill-buffer won't need confirmation later
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 (set-buffer-modified-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 ;; save it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 (write-region (point-min) (point-max) save-file-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 nil 'quiet))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 (t (condition-case data
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 (delete-file save-file-name) (error nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 (kill-buffer context-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 (error nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 (defun recover-context ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 "Recover an Emacs context saved by `save-context' command.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 Files that were visible in windows when the context was saved are visited and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 point is set in each window to what is was when the context was saved."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 (condition-case error-data
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 ;; Set up some local variables.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 (let (sexpr context-buffer recover-file-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 (setq recover-file-name (concat (original-working-directory)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 ".emacs_" (user-login-name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 (if (not (file-readable-p recover-file-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 (error "can't access context, %s" recover-file-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 ;; create a temp buffer and copy the saved context into it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 (setq context-buffer (get-buffer-create " *Recovered Context*"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 (set-buffer context-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 (erase-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 (insert-file-contents recover-file-name nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 ;; so kill-buffer won't need confirmation later
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 (set-buffer-modified-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 ;; If it's empty forget it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 (if (zerop (buffer-size))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 (error "context file is empty, %s" recover-file-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 ;; check the version and make sure it matches ours
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 (setq sexpr (read context-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 (if (not (equal sexpr save-context-version))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 (error "version string incorrect, %s" sexpr))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 ;; Recover the window contexts
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 (while (setq sexpr (read context-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 (select-window (get-largest-window))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 (if (buffer-file-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 (split-window))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 (other-window 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 (find-file sexpr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 (goto-char (read context-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 ;; Recover buffer contexts, if any.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 (while (setq sexpr (read context-buffer))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
272 (set-buffer (find-file-noselect sexpr t))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
273 (if (zerop (buffer-size))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
274 (kill-buffer (current-buffer))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
275 (goto-char (read context-buffer))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 (bury-buffer "*scratch*")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 (kill-buffer context-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 t )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 (error nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 (defun original-working-directory ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 (set-buffer (get-buffer-create "*scratch*"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 default-directory))