24
|
1 ;; -*- Emacs-Lisp -*-
|
|
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
3 ;;
|
|
4 ;; File: auto-save.el
|
48
|
5 ;; Version: $Revision: 1.4 $
|
24
|
6 ;; RCS:
|
|
7 ;; Description: Safer autosaving with support for efs and /tmp.
|
|
8 ;; This version of auto-save is designed to work with efs,
|
|
9 ;; instead of ange-ftp.
|
|
10 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>,
|
|
11 ;;
|
|
12 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
0
|
13
|
48
|
14 (defconst auto-save-version (substring "$Revision: 1.4 $" 11 -2)
|
24
|
15 "Version number of auto-save.")
|
0
|
16
|
24
|
17 ;;; Copyright (C) 1992 by Sebastian Kremer <sk@thp.uni-koeln.de>
|
|
18
|
|
19 ;;; This program is free software; you can redistribute it and/or modify
|
|
20 ;;; it under the terms of the GNU General Public License as published by
|
|
21 ;;; the Free Software Foundation; either version 1, or (at your option)
|
|
22 ;;; any later version.
|
0
|
23
|
24
|
24 ;;; This program is distributed in the hope that it will be useful,
|
|
25 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
26 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
27 ;;; GNU General Public License for more details.
|
0
|
28
|
24
|
29 ;;; You should have received a copy of the GNU General Public License
|
|
30 ;;; along with this program; if not, write to the Free Software
|
|
31 ;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
0
|
32
|
|
33 ;;;; OVERVIEW ==========================================================
|
|
34
|
24
|
35 ;;; Combines autosaving for efs (to a local or remote directory)
|
|
36 ;;; with the ability to do autosaves to a fixed directory on a local
|
|
37 ;;; disk, in case NFS is slow. The auto-save file used for
|
|
38 ;;; /usr/foo/bar/baz.txt
|
|
39 ;;; will be
|
|
40 ;;; AUTOSAVE/#\!usr\!foo\!bar\!baz.txt#
|
|
41 ;;; assuming AUTOSAVE is the non-nil value of the variable
|
|
42 ;;; `auto-save-directory'.
|
0
|
43
|
24
|
44 ;;; Takes care that autosave files for non-file-buffers (e.g. *mail*)
|
|
45 ;;; from two simultaneous Emacses don't collide.
|
0
|
46
|
24
|
47 ;;; Autosaves even if the current directory is not writable.
|
0
|
48
|
24
|
49 ;;; Can limit autosave names to 14 characters using a hash function,
|
|
50 ;;; see `auto-save-hash-p'.
|
0
|
51
|
24
|
52 ;;; See `auto-save-directory' and `make-auto-save-file-name' and
|
|
53 ;;; references therein for complete documentation.
|
0
|
54
|
24
|
55 ;;; Meta-x recover-all-files will effectively do recover-file on all
|
|
56 ;;; files whose autosave file is newer (one of the benefits of having
|
|
57 ;;; all autosave files in the same place).
|
0
|
58
|
|
59 ;;;; INSTALLATION ======================================================
|
|
60
|
24
|
61 ;;; Put this file into your load-path and the following in your ~/.emacs:
|
0
|
62
|
24
|
63 ;;; If you want to autosave in the fixed directory /tmp/USER-autosave/
|
|
64 ;;; (setq auto-save-directory
|
|
65 ;;; (concat "/tmp/" (user-login-name) "-autosave/"))
|
0
|
66
|
24
|
67 ;;; If you don't want to save in /tmp (e.g., because it is swap
|
|
68 ;;; mounted) but rather in ~/autosave/
|
|
69 ;;; (setq auto-save-directory (expand-file-name "~/autosave/"))
|
0
|
70
|
24
|
71 ;;; If you want to save each file in its own directory (the default)
|
|
72 ;;; (setq auto-save-directory nil)
|
|
73 ;;; You still can take advantage of autosaving efs remote files
|
|
74 ;;; in a fixed local directory, `auto-save-directory-fallback' will
|
|
75 ;;; be used.
|
0
|
76
|
24
|
77 ;;; If you want to use 14 character hashed autosave filenames
|
|
78 ;;; (setq auto-save-hash-p t)
|
0
|
79
|
24
|
80 ;;; Finally, put this line after the others in your ~/.emacs:
|
|
81 ;;; (require 'auto-save)
|
0
|
82
|
|
83
|
|
84 ;;;; ACKNOWLEDGEMENT ===================================================
|
|
85
|
24
|
86 ;;; This code is loosely derived from autosave-in-tmp.el by Jamie
|
|
87 ;;; Zawinski <jwz@lucid.com> (the version I had was last modified 22
|
|
88 ;;; dec 90 jwz) and code submitted to ange-ftp-lovers on Sun, 5 Apr
|
|
89 ;;; 92 23:20:47 EDT by drw@BOURBAKI.MIT.EDU (Dale R. Worley).
|
|
90 ;;; auto-save.el tries to cover the functionality of those two
|
|
91 ;;; packages.
|
0
|
92
|
24
|
93 ;;; Valuable comments and help from Dale Worley, Andy Norman, Jamie
|
|
94 ;;; Zawinski and Sandy Rutherford are gratefully acknowledged.
|
|
95
|
|
96 ;;;; PROVISION ========================================================
|
|
97
|
|
98 (provide 'auto-save)
|
0
|
99
|
|
100 ;;;; CUSTOMIZATION =====================================================
|
|
101
|
|
102 (defvar auto-save-directory nil
|
|
103
|
|
104 ;;; Don't make this user-variable-p, it should be set in .emacs and
|
|
105 ;;; left at that. In particular, it should remain constant across
|
|
106 ;;; several Emacs session to make recover-all-files work.
|
|
107
|
|
108 "If non-nil, fixed directory for autosaving: all autosave files go
|
|
109 there. If this directory does not yet exist at load time, it is
|
|
110 created and its mode is set to 0700 so that nobody else can read your
|
|
111 autosave files.
|
|
112
|
|
113 If nil, each autosave files goes into the same directory as its
|
|
114 corresponding visited file.
|
|
115
|
|
116 A non-nil `auto-save-directory' could be on a local disk such as in
|
|
117 /tmp, then auto-saves will always be fast, even if NFS or the
|
|
118 automounter is slow. In the usual case of /tmp being locally mounted,
|
|
119 note that if you run emacs on two different machines, they will not
|
|
120 see each other's auto-save files.
|
|
121
|
|
122 The value \(expand-file-name \"~/autosave/\"\) might be better if /tmp
|
|
123 is mounted from swap (possible in SunOS, type `df /tmp' to find out)
|
|
124 and thus vanishes after a reboot, or if your system is particularly
|
|
125 thorough when cleaning up /tmp, clearing even non-empty subdirectories.
|
|
126
|
24
|
127 It should never be an efs remote filename because that would
|
|
128 defeat `efs-auto-save-remotely'.
|
0
|
129
|
|
130 Unless you set `auto-save-hash-p', you shouldn't set this to a
|
|
131 directory in a filesystem that does not support long filenames, since
|
|
132 a file named
|
|
133
|
|
134 /home/sk/lib/emacs/lisp/auto-save.el
|
|
135
|
|
136 will have a longish filename like
|
|
137
|
|
138 AUTO-SAVE-DIRECTORY/#\\!home\\!sk\\!lib\\!emacs\\!lisp\\!auto-save.el#
|
|
139
|
|
140 as auto save file.
|
|
141
|
24
|
142 See also variables `auto-save-directory-fallback',
|
|
143 `efs-auto-save' and `efs-auto-save-remotely'.")
|
0
|
144
|
|
145 (defvar auto-save-hash-p nil
|
|
146 "If non-nil, hashed autosave names of length 14 are used.
|
|
147 This is to avoid autosave filenames longer than 14 characters.
|
|
148 The directory used is `auto-save-hash-directory' regardless of
|
|
149 `auto-save-directory'.
|
|
150 Hashing defeats `recover-all-files', you have to recover files
|
|
151 individually by doing `recover-file'.")
|
|
152
|
24
|
153 ;;; This defvar is in efs.el now, but doesn't hurt to give it here as
|
|
154 ;;; well so that loading first auto-save.el does not abort.
|
|
155 (or (boundp 'efs-auto-save) (defvar efs-auto-save 0))
|
|
156 (or (boundp 'efs-auto-save-remotely) (defvar efs-auto-save-remotely nil))
|
0
|
157
|
24
|
158 (defvar auto-save-offer-delete nil
|
|
159 "*If non-nil, `recover-all-files' offers to delete autosave files
|
|
160 that are out of date or were dismissed for recovering.
|
|
161 Special value 'always deletes those files silently.")
|
0
|
162
|
|
163 ;;;; end of customization
|
|
164
|
|
165
|
|
166 ;;; Preparations to be done at load time
|
|
167
|
|
168 (defvar auto-save-directory-fallback (expand-file-name "~/autosave/")
|
|
169 ;; not user-variable-p, see above
|
|
170 "Directory used for local autosaving of remote files if
|
24
|
171 both `auto-save-directory' and `efs-auto-save-remotely' are nil.
|
0
|
172 Also used if a working directory to be used for autosaving is not writable.
|
|
173 This *must* always be the name of directory that exists or can be
|
|
174 created by you, never nil.")
|
|
175
|
|
176 (defvar auto-save-hash-directory
|
|
177 (expand-file-name "hash/" (or auto-save-directory
|
|
178 auto-save-directory-fallback))
|
|
179 "If non-nil, directory used for hashed autosave filenames.")
|
|
180
|
|
181 (defun auto-save-check-directory (var)
|
|
182 (let ((dir (symbol-value var)))
|
|
183 (if (null dir)
|
|
184 nil
|
|
185 ;; Expand and store back into the variable
|
|
186 (set var (setq dir (expand-file-name dir)))
|
|
187 ;; Make sure directory exists
|
|
188 (if (file-directory-p dir)
|
|
189 nil
|
|
190 ;; Else we create and chmod 0700 the directory
|
|
191 (setq dir (directory-file-name dir)) ; some systems need this
|
|
192 (if (fboundp 'make-directory) ; V19 or tree dired
|
|
193 (make-directory dir)
|
|
194 (call-process "mkdir" nil nil nil dir))
|
|
195 (set-file-modes dir (* 7 8 8))))))
|
|
196
|
|
197 (mapcar (function auto-save-check-directory)
|
|
198 '(auto-save-directory auto-save-directory-fallback))
|
|
199
|
|
200 (and auto-save-hash-p
|
|
201 (auto-save-check-directory 'auto-save-hash-directory))
|
|
202
|
|
203
|
|
204 ;;; Computing an autosave name for a file and vice versa
|
|
205
|
|
206 (defun make-auto-save-file-name ();; redefines files.el
|
|
207 ;; auto-save-file-name-p need not be redefined.
|
|
208
|
|
209 "Return file name to use for auto-saves of current buffer.
|
|
210 Does not consider `auto-save-visited-file-name'; that is checked
|
|
211 before calling this function.
|
|
212
|
|
213 Offers to autosave all files in the same `auto-save-directory'. All
|
|
214 autosave files can then be recovered at once with function
|
|
215 `recover-all-files'.
|
|
216
|
24
|
217 Takes care to make autosave files for files accessed through efs
|
|
218 be local files if variable `efs-auto-save-remotely' is nil.
|
0
|
219
|
|
220 Takes care of slashes in buffer names to prevent autosave errors.
|
|
221
|
24
|
222 Takes care that autosave files for buffers not visiting any file (such
|
|
223 as `*mail*') from two simultaneous Emacses don't collide by prepending
|
|
224 the Emacs pid.
|
|
225
|
0
|
226 Uses 14 character autosave names if `auto-save-hash-p' is true.
|
|
227
|
|
228 Autosaves even if the current directory is not writable, using
|
|
229 directory `auto-save-directory-fallback'.
|
|
230
|
|
231 You can redefine this for customization (he he :-).
|
|
232 See also function `auto-save-file-name-p'."
|
|
233
|
|
234 ;; We have to be very careful about not signalling an error in this
|
|
235 ;; function since files.el does not provide for this (e.g. find-file
|
|
236 ;; would fail for each new file).
|
|
237
|
|
238 (condition-case error-data
|
24
|
239 (let* ((file-name (or (and (boundp 'buffer-file-truename)
|
|
240 buffer-file-truename
|
|
241 ;; Make sure that the file name is expanded.
|
|
242 (expand-file-name buffer-file-name))
|
|
243 (and buffer-file-name
|
|
244 (expand-file-name buffer-file-name))))
|
0
|
245 ;; So autosavename looks like #%...#, roughly as with the
|
|
246 ;; old make-auto-save-file-name function. The
|
|
247 ;; make-temp-name inserts the pid of this Emacs: this
|
|
248 ;; avoids autosaving from two Emacses into the same file.
|
|
249 ;; It cannot be recovered automatically then because in
|
|
250 ;; the next Emacs session (the one after the crash) the
|
|
251 ;; pid will be different, but file-less buffers like
|
|
252 ;; *mail* must be recovered manually anyway.
|
48
|
253
|
|
254 ;; jwz: putting the emacs PID in the auto-save file name is bad
|
|
255 ;; news, because that defeats auto-save-recovery of *mail*
|
|
256 ;; buffers -- the (sensible) code in sendmail.el calls
|
|
257 ;; (make-auto-save-file-name) to determine whether there is
|
|
258 ;; unsent, auto-saved mail to recover. If that mail came from a
|
|
259 ;; previous emacs process (far and away the most likely case)
|
|
260 ;; then this can never succeed as the pid differs.
|
|
261 ;; (name-prefix (if file-name nil (make-temp-name "#%")))
|
|
262 (name-prefix (if file-name nil "#%"))
|
|
263
|
0
|
264 (save-name (or file-name
|
|
265 ;; Prevent autosave errors. Buffername
|
|
266 ;; (to become non-dir part of filename) will
|
|
267 ;; be unslashified twice. Don't care.
|
|
268 (auto-save-unslashify-name (buffer-name))))
|
|
269 (remote-p (and (stringp file-name)
|
24
|
270 (fboundp 'efs-ftp-path)
|
|
271 (efs-ftp-path file-name))))
|
0
|
272 ;; Return the appropriate auto save file name:
|
|
273 (expand-file-name;; a buffername needs this, a filename not
|
|
274 (if remote-p
|
24
|
275 (if efs-auto-save-remotely
|
0
|
276 (auto-save-name-in-same-directory save-name)
|
|
277 ;; We have to use the `fixed-directory' now since the
|
|
278 ;; `same-directory' would be remote.
|
|
279 ;; It will use the fallback if needed.
|
|
280 (auto-save-name-in-fixed-directory save-name))
|
|
281 ;; Else it is a local file (or a buffer without a file, hence
|
24
|
282 ;; the name-prefix).
|
0
|
283 ;; Hashed files always go into the special hash dir, never
|
|
284 ;; in the same directory, to make recognizing reliable.
|
|
285 (if (or auto-save-directory auto-save-hash-p)
|
|
286 (auto-save-name-in-fixed-directory save-name name-prefix)
|
|
287 (auto-save-name-in-same-directory save-name name-prefix)))))
|
24
|
288
|
0
|
289 ;; If any error occurs in the above code, return what the old
|
|
290 ;; version of this function would have done. It is not ok to
|
|
291 ;; return nil, e.g., when after-find-file tests
|
|
292 ;; file-newer-than-file-p, nil would bomb.
|
|
293
|
|
294 (error (progn
|
|
295 (message "make-auto-save-file-name %s" error-data)
|
|
296 (sit-for 2)
|
|
297 (if buffer-file-name
|
|
298 (concat (file-name-directory buffer-file-name)
|
|
299 "#"
|
|
300 (file-name-nondirectory buffer-file-name)
|
|
301 "#")
|
|
302 (expand-file-name (concat "#%" (buffer-name) "#")))))))
|
|
303
|
|
304 (defun auto-save-original-name (savename)
|
|
305 "Reverse of `make-auto-save-file-name'.
|
|
306 Returns nil if SAVENAME was not associated with a file (e.g., it came
|
|
307 from an autosaved `*mail*' buffer) or does not appear to be an
|
|
308 autosave file at all.
|
|
309 Hashed files are not understood, see `auto-save-hash-p'."
|
|
310 (let ((basename (file-name-nondirectory savename))
|
|
311 (savedir (file-name-directory savename)))
|
|
312 (cond ((or (not (auto-save-file-name-p basename))
|
|
313 (string-match "^#%" basename))
|
|
314 nil)
|
|
315 ;; now we know it looks like #...# thus substring is safe to use
|
|
316 ((or (equal savedir auto-save-directory) ; 2nd arg may be nil
|
|
317 (equal savedir auto-save-directory-fallback))
|
|
318 ;; it is of the `-fixed-directory' type
|
|
319 (auto-save-slashify-name (substring basename 1 -1)))
|
|
320 (t
|
|
321 ;; else it is of `-same-directory' type
|
|
322 (concat savedir (substring basename 1 -1))))))
|
|
323
|
|
324 (defun auto-save-name-in-fixed-directory (filename &optional prefix)
|
|
325 ;; Unslashify and enclose the whole FILENAME in `#' to make an auto
|
|
326 ;; save file in the auto-save-directory, or if that is nil, in
|
|
327 ;; auto-save-directory-fallback (which must be the name of an
|
|
328 ;; existing directory). If the results would be too long for 14
|
|
329 ;; character filenames, and `auto-save-hash-p' is set, hash FILENAME
|
|
330 ;; into a shorter name.
|
|
331 ;; Optional PREFIX is string to use instead of "#" to prefix name.
|
|
332 (let ((base-name (concat (or prefix "#")
|
|
333 (auto-save-unslashify-name filename)
|
|
334 "#")))
|
|
335 (if (and auto-save-hash-p
|
|
336 auto-save-hash-directory
|
|
337 (> (length base-name) 14))
|
|
338 (expand-file-name (auto-save-cyclic-hash-14 filename)
|
|
339 auto-save-hash-directory)
|
|
340 (expand-file-name base-name
|
|
341 (or auto-save-directory
|
|
342 auto-save-directory-fallback)))))
|
|
343
|
|
344 (defun auto-save-name-in-same-directory (filename &optional prefix)
|
|
345 ;; Enclose the non-directory part of FILENAME in `#' to make an auto
|
|
346 ;; save file in the same directory as FILENAME. But if this
|
|
347 ;; directory is not writable, use auto-save-directory-fallback.
|
|
348 ;; FILENAME is assumed to be in non-directory form (no trailing slash).
|
|
349 ;; It may be a name without a directory part (pesumably it really
|
|
350 ;; comes from a buffer name then), the fallback is used then.
|
|
351 ;; Optional PREFIX is string to use instead of "#" to prefix name.
|
|
352 (let ((directory (file-name-directory filename)))
|
|
353 (or (null directory)
|
|
354 (file-writable-p directory)
|
|
355 (setq directory auto-save-directory-fallback))
|
|
356 (concat directory ; (concat nil) is ""
|
|
357 (or prefix "#")
|
|
358 (file-name-nondirectory filename)
|
|
359 "#")))
|
|
360
|
|
361 (defun auto-save-unslashify-name (s)
|
|
362 ;; "Quote any slashes in string S by replacing them with the two
|
|
363 ;;characters `\\!'.
|
|
364 ;;Also, replace any backslash by double backslash, to make it one-to-one."
|
|
365 (let ((limit 0))
|
|
366 (while (string-match "[/\\]" s limit)
|
|
367 (setq s (concat (substring s 0 (match-beginning 0))
|
|
368 (if (string= (substring s
|
|
369 (match-beginning 0)
|
|
370 (match-end 0))
|
|
371 "/")
|
|
372 "\\!"
|
|
373 "\\\\")
|
|
374 (substring s (match-end 0))))
|
|
375 (setq limit (1+ (match-end 0)))))
|
|
376 s)
|
|
377
|
|
378 (defun auto-save-slashify-name (s)
|
|
379 ;;"Reverse of `auto-save-unslashify-name'."
|
|
380 (let (pos)
|
|
381 (while (setq pos (string-match "\\\\[\\!]" s pos))
|
|
382 (setq s (concat (substring s 0 pos)
|
|
383 (if (eq ?! (aref s (1+ pos))) "/" "\\")
|
|
384 (substring s (+ pos 2)))
|
|
385 pos (1+ pos))))
|
|
386 s)
|
|
387
|
|
388
|
|
389 ;;; Hashing for autosave names
|
|
390
|
|
391 ;;; Hashing function contributed by Andy Norman <ange@hplb.hpl.hp.com>
|
|
392 ;;; based upon C code from pot@fly.cnuce.cnr.IT (Francesco Potorti`).
|
|
393
|
|
394 (defun auto-save-cyclic-hash-14 (s)
|
|
395 ;; "Hash string S into a string of length 14.
|
24
|
396 ;; A 7-bytes cyclic code for burst correction is calculated on a
|
|
397 ;; byte-by-byte basis. The polynomial used is D^7 + D^6 + D^3 +1.
|
0
|
398 ;; The resulting string consists of hexadecimal digits [0-9a-f].
|
|
399 ;; In particular, it contains no slash, so it can be used as autosave name."
|
24
|
400 (let ((crc (make-string 7 0))
|
0
|
401 result)
|
|
402 (mapcar
|
|
403 (function
|
|
404 (lambda (new)
|
24
|
405 (setq new (+ new (aref crc 6)))
|
0
|
406 (aset crc 6 (+ (aref crc 5) new))
|
|
407 (aset crc 5 (aref crc 4))
|
|
408 (aset crc 4 (aref crc 3))
|
|
409 (aset crc 3 (+ (aref crc 2) new))
|
|
410 (aset crc 2 (aref crc 1))
|
|
411 (aset crc 1 (aref crc 0))
|
|
412 (aset crc 0 new)))
|
|
413 s)
|
|
414 (setq result (format "%02x%02x%02x%02x%02x%02x%02x"
|
|
415 (aref crc 0)
|
|
416 (aref crc 1)
|
|
417 (aref crc 2)
|
|
418 (aref crc 3)
|
|
419 (aref crc 4)
|
|
420 (aref crc 5)
|
24
|
421 (aref crc 6)))
|
0
|
422 result))
|
|
423
|
24
|
424 ;; This leaves two characters that could be used to wrap it in `#' or
|
|
425 ;; make two filenames from it: one for autosaving, and another for a
|
|
426 ;; file containing the name of the autosaved filed, to make hashing
|
|
427 ;; reversible.
|
|
428 (defun auto-save-cyclic-hash-12 (s)
|
|
429 "Outputs the 12-characters ascii hex representation of a 6-bytes
|
|
430 cyclic code for burst correction calculated on STRING on a
|
|
431 byte-by-byte basis. The used polynomial is D^6 + D^5 + D^4 + D^3 +1."
|
|
432 (let ((crc (make-string 6 0)))
|
|
433 (mapcar
|
|
434 (function
|
|
435 (lambda (new)
|
|
436 (setq new (+ new (aref crc 5)))
|
|
437 (aset crc 5 (+ (aref crc 4) new))
|
|
438 (aset crc 4 (+ (aref crc 3) new))
|
|
439 (aset crc 3 (+ (aref crc 2) new))
|
|
440 (aset crc 2 (aref crc 1))
|
|
441 (aset crc 1 (aref crc 0))
|
|
442 (aset crc 0 new)))
|
|
443 s)
|
|
444 (format "%02x%02x%02x%02x%02x%02x"
|
|
445 (aref crc 0)
|
|
446 (aref crc 1)
|
|
447 (aref crc 2)
|
|
448 (aref crc 3)
|
|
449 (aref crc 4)
|
|
450 (aref crc 5))))
|
|
451
|
0
|
452
|
|
453
|
|
454 ;;; Recovering files
|
|
455
|
24
|
456 (defun recover-all-files (&optional silent)
|
0
|
457 "Do recover-file for all autosave files which are current.
|
|
458 Only works if you have a non-nil `auto-save-directory'.
|
24
|
459
|
|
460 Optional prefix argument SILENT means to be silent about non-current
|
|
461 autosave files. This is useful if invoked automatically at Emacs
|
|
462 startup.
|
|
463
|
|
464 If `auto-save-offer-delete' is t, this function will offer to delete
|
|
465 old or rejected autosave files.
|
|
466
|
0
|
467 Hashed files (see `auto-save-hash-p') are not understood, use
|
|
468 `recover-file' to recover them individually."
|
24
|
469 (interactive "P")
|
0
|
470 (let ((savefiles (directory-files auto-save-directory t "^#"))
|
|
471 afile ; the auto save file
|
|
472 file ; its original file
|
|
473 (total 0) ; # of files offered to recover
|
|
474 (count 0)) ; # of files actually recovered
|
|
475 (or (equal auto-save-directory auto-save-directory-fallback)
|
|
476 (setq savefiles
|
|
477 (append savefiles
|
|
478 (directory-files auto-save-directory-fallback t "^#"))))
|
|
479 (while savefiles
|
|
480 (setq afile (car savefiles)
|
|
481 file (auto-save-original-name afile)
|
|
482 savefiles (cdr savefiles))
|
|
483 (cond ((and file (not (file-newer-than-file-p afile file)))
|
|
484 (message "autosave file \"%s\" is not current." afile)
|
|
485 (sit-for 2))
|
|
486 (t
|
|
487 (setq total (1+ total))
|
|
488 (with-output-to-temp-buffer "*Directory*"
|
48
|
489 (apply 'call-process "ls" nil standard-output nil
|
|
490 "-l" afile (if file (list file))))
|
0
|
491 (if (yes-or-no-p (format "Recover %s from auto save file? "
|
48
|
492 (or file "non-file buffer")))
|
0
|
493 (let* ((obuf (current-buffer))
|
|
494 (buf (set-buffer
|
|
495 (if file
|
|
496 (find-file-noselect file t)
|
|
497 (generate-new-buffer "*recovered*"))))
|
|
498 (buffer-read-only nil))
|
|
499 (erase-buffer)
|
|
500 (insert-file-contents afile nil)
|
|
501 (condition-case ()
|
|
502 (after-find-file nil)
|
|
503 (error nil))
|
|
504 (setq buffer-auto-save-file-name nil)
|
|
505 (setq count (1+ count))
|
|
506 (message "\
|
|
507 Auto-save off in buffer \"%s\" till you do M-x auto-save-mode."
|
|
508 (buffer-name))
|
|
509 (set-buffer obuf)
|
24
|
510 (sit-for 1))
|
|
511 ;; If not used for recovering, offer to delete
|
|
512 ;; autosave file
|
|
513 (and auto-save-offer-delete
|
|
514 (or (eq 'always auto-save-offer-delete)
|
|
515 (yes-or-no-p
|
|
516 (format "Delete autosave file for `%s'? " file)))
|
|
517 (delete-file afile))))))
|
0
|
518 (if (zerop total)
|
24
|
519 (or silent (message "Nothing to recover."))
|
0
|
520 (message "%d/%d file%s recovered." count total (if (= count 1) "" "s"))))
|
|
521 (if (get-buffer "*Directory*") (kill-buffer "*Directory*")))
|
|
522
|
24
|
523 ;;; end of auto-save.el
|