Mercurial > hg > xemacs-beta
annotate lisp/auto-save.el @ 5315:2a7b6ddb8063
#'float: if handed a bigfloat, give the same bigfloat back.
2010-12-29 Aidan Kehoe <kehoea@parhasard.net>
* floatfns.c (Ffloat): If we've been handed a bigfloat here, it's
appropriate to give the same bigfloat back.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Wed, 29 Dec 2010 23:51:08 +0000 |
parents | cd167465bf69 |
children | b9167d522a9a |
rev | line source |
---|---|
428 | 1 ;;; auto-save.el -- Safer autosaving for EFS and tmp. |
2 | |
3 ;; Copyright (C) 1997 Free Software Foundation, Inc. | |
4 ;; Copyright (C) 1992 by Sebastian Kremer <sk@thp.uni-koeln.de> | |
793 | 5 ;; Copyright (C) 2001, 2002 Ben Wing. |
428 | 6 |
7 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de> | |
8 ;; Maintainer: XEmacs Development Team | |
9 ;; Keywords: extensions, dumped | |
10 ;; Version: 1.26 | |
11 | |
5287
cd167465bf69
More permission consistency.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
793
diff
changeset
|
12 ;; This file is part of XEmacs. |
cd167465bf69
More permission consistency.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
793
diff
changeset
|
13 |
428 | 14 ;; XEmacs is free software; you can redistribute it and/or modify it |
15 ;; under the terms of the GNU General Public License as published by | |
793 | 16 ;; the Free Software Foundation; either version 2, or (at your option) |
428 | 17 ;; any later version. |
18 | |
19 ;; XEmacs is distributed in the hope that it will be useful, but | |
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
22 ;; General Public License for more details. | |
23 | |
24 ;; You should have received a copy of the GNU General Public License | |
25 ;; along with XEmacs; see the file COPYING. If not, write to the Free | |
26 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
27 ;; 02111-1307, USA. | |
28 | |
29 ;;; Synched up with: Not in FSF | |
30 | |
31 ;;; Commentary: | |
32 | |
33 ;; This file is dumped with XEmacs. | |
34 | |
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 | |
464 | 40 ;; AUTOSAVE/#=2Fusr=2Ffoo=2Fbar=2Fbaz.txt#" |
428 | 41 ;; assuming AUTOSAVE is the non-nil value of the variable |
42 ;; `auto-save-directory'. | |
43 | |
44 ;; Autosaves even if the current directory is not writable. | |
45 | |
46 ;; Can limit autosave names to 14 characters using a hash function, | |
47 ;; see `auto-save-hash-p'. | |
48 | |
49 ;; See `auto-save-directory' and `make-auto-save-file-name' and | |
50 ;; references therein for complete documentation. | |
51 | |
52 ;; `M-x recover-all-files' will effectively do recover-file on all | |
53 ;; files whose autosave file is newer (one of the benefits of having | |
54 ;; all autosave files in the same place). | |
55 | |
56 ;; This file is dumped with XEmacs. | |
57 | |
58 ;; If you want to autosave in the fixed directory /tmp/USER-autosave/ | |
59 ;; (setq auto-save-directory | |
60 ;; (concat "/tmp/" (user-login-name) "-autosave/")) | |
61 | |
62 ;; If you don't want to save in /tmp (e.g., because it is swap | |
464 | 63 ;; mounted) but rather in ~/.autosave/ |
428 | 64 ;; (setq auto-save-directory (expand-file-name "~/.autosave/")) |
65 | |
66 ;; If you want to save each file in its own directory (the default) | |
67 ;; (setq auto-save-directory nil) | |
68 ;; You still can take advantage of autosaving efs remote files | |
69 ;; in a fixed local directory, `auto-save-directory-fallback' will | |
70 ;; be used. | |
71 | |
72 ;; If you want to use 14 character hashed autosave filenames | |
73 ;; (setq auto-save-hash-p t) | |
74 | |
75 ;; Finally, put this line after the others in your ~/.emacs: | |
76 ;; (require 'auto-save) | |
77 | |
78 | |
79 ;;; Acknowledgement: | |
80 | |
81 ;; This code is loosely derived from autosave-in-tmp.el by Jamie | |
82 ;; Zawinski <jwz@jwz.org> (the version I had was last modified 22 | |
83 ;; dec 90 jwz) and code submitted to ange-ftp-lovers on Sun, 5 Apr | |
84 ;; 92 23:20:47 EDT by drw@BOURBAKI.MIT.EDU (Dale R. Worley). | |
85 ;; auto-save.el tries to cover the functionality of those two | |
86 ;; packages. | |
87 | |
88 ;; Valuable comments and help from Dale Worley, Andy Norman, Jamie | |
89 ;; Zawinski and Sandy Rutherford are gratefully acknowledged. | |
90 | |
91 (defconst auto-save-version "1.26" | |
92 "Version number of auto-save.") | |
93 | |
94 (provide 'auto-save) | |
95 | |
96 | |
97 ;;; Customization: | |
98 | |
99 (defgroup auto-save nil | |
100 "Autosaving with support for efs and /tmp." | |
101 :group 'data) | |
102 | |
103 (put 'auto-save-interval 'custom-type 'integer) | |
104 (put 'auto-save-interval 'factory-value '(300)) | |
105 (custom-add-to-group 'auto-save 'auto-save-interval 'custom-variable) | |
106 | |
107 (defcustom auto-save-directory nil | |
108 | |
109 ;; Don't make this user-variable-p, it should be set in .emacs and | |
110 ;; left at that. In particular, it should remain constant across | |
111 ;; several Emacs session to make recover-all-files work. | |
112 | |
113 ;; However, it's OK for it to be customizable, as most of the | |
114 ;; customizable variables are set at the time `.emacs' is read. | |
115 ;; -hniksic | |
116 | |
117 "If non-nil, fixed directory for autosaving: all autosave files go | |
118 there. If this directory does not yet exist at load time, it is | |
119 created and its mode is set to 0700 so that nobody else can read your | |
120 autosave files. | |
121 | |
122 If nil, each autosave files goes into the same directory as its | |
123 corresponding visited file. | |
124 | |
125 A non-nil `auto-save-directory' could be on a local disk such as in | |
126 /tmp, then auto-saves will always be fast, even if NFS or the | |
127 automounter is slow. In the usual case of /tmp being locally mounted, | |
128 note that if you run emacs on two different machines, they will not | |
129 see each other's auto-save files. | |
130 | |
131 The value \(expand-file-name \"~/.autosave/\"\) might be better if /tmp | |
132 is mounted from swap (possible in SunOS, type `df /tmp' to find out) | |
133 and thus vanishes after a reboot, or if your system is particularly | |
134 thorough when cleaning up /tmp, clearing even non-empty subdirectories. | |
135 | |
136 It should never be an efs remote filename because that would | |
137 defeat `efs-auto-save-remotely'. | |
138 | |
139 Unless you set `auto-save-hash-p', you shouldn't set this to a | |
140 directory in a filesystem that does not support long filenames, since | |
141 a file named | |
142 | |
143 /home/sk/lib/emacs/lisp/auto-save.el | |
144 | |
145 will have a longish filename like | |
146 | |
464 | 147 AUTO-SAVE-DIRECTORY/#=2Fhome=2Fsk=2Flib=2Femacs=2Flisp=2Fauto-save.el# |
428 | 148 |
149 as auto save file. | |
150 | |
151 See also variables `auto-save-directory-fallback', | |
152 `efs-auto-save' and `efs-auto-save-remotely'." | |
153 :type '(choice (const :tag "Same as file" nil) | |
154 directory) | |
155 :group 'auto-save) | |
156 | |
157 | |
158 (defcustom auto-save-hash-p nil | |
159 "If non-nil, hashed autosave names of length 14 are used. | |
160 This is to avoid autosave filenames longer than 14 characters. | |
161 The directory used is `auto-save-hash-directory' regardless of | |
162 `auto-save-directory'. | |
163 Hashing defeats `recover-all-files', you have to recover files | |
164 individually by doing `recover-file'." | |
165 :type 'boolean | |
166 :group 'auto-save) | |
167 | |
168 ;;; This defvar is in efs.el now, but doesn't hurt to give it here as | |
169 ;;; well so that loading first auto-save.el does not abort. | |
170 | |
171 ;; #### Now that `auto-save' is dumped, this is looks obnoxious. | |
172 (or (boundp 'efs-auto-save) (defvar efs-auto-save 0)) | |
173 (or (boundp 'efs-auto-save-remotely) (defvar efs-auto-save-remotely nil)) | |
174 | |
175 (defcustom auto-save-offer-delete nil | |
176 "*If non-nil, `recover-all-files' offers to delete autosave files | |
177 that are out of date or were dismissed for recovering. | |
178 Special value 'always deletes those files silently." | |
179 :type '(choice (const :tag "on" t) | |
180 (const :tag "off" nil) | |
181 (const :tag "Delete silently" always)) | |
182 :group 'auto-save) | |
183 | |
184 ;;;; end of customization | |
185 | |
186 | |
187 ;;; Preparations to be done at load time | |
188 | |
189 ;; Do not call expand-file-name! This is evaluated at dump time now! | |
190 (defvar auto-save-directory-fallback "~/.autosave/" | |
191 ;; not user-variable-p, see above | |
192 "Directory used for local autosaving of remote files if | |
193 both `auto-save-directory' and `efs-auto-save-remotely' are nil. | |
194 Also used if a working directory to be used for autosaving is not writable. | |
195 This *must* always be the name of directory that exists or can be | |
196 created by you, never nil.") | |
197 | |
198 (defvar auto-save-hash-directory | |
199 (expand-file-name "hash/" (or auto-save-directory | |
200 auto-save-directory-fallback)) | |
201 "If non-nil, directory used for hashed autosave filenames.") | |
202 | |
203 (defun auto-save-checked-directory (dir) | |
204 "Make sure the directory DIR exists and return it expanded if non-nil." | |
205 (when dir | |
206 (setq dir (expand-file-name dir)) | |
207 ;; Make sure directory exists | |
208 (unless (file-directory-p dir) | |
209 ;; Else we create and chmod 0700 the directory | |
210 (setq dir (directory-file-name dir)) ; some systems need this | |
211 (make-directory dir) | |
212 (set-file-modes dir #o700)) | |
213 dir)) | |
214 | |
215 ;; This make no sense at dump time | |
216 ;; (mapc #'auto-save-check-directory | |
217 ; '(auto-save-directory auto-save-directory-fallback)) | |
218 | |
219 ;(and auto-save-hash-p | |
220 ; (auto-save-check-directory 'auto-save-hash-directory)) | |
221 | |
222 | |
223 ;;; Computing an autosave name for a file and vice versa | |
224 | |
464 | 225 (defun make-auto-save-file-name (&optional file-name) |
428 | 226 "Return file name to use for auto-saves of current buffer. |
227 Does not consider `auto-save-visited-file-name'; that is checked | |
228 before calling this function. | |
229 | |
230 Offers to autosave all files in the same `auto-save-directory'. All | |
231 autosave files can then be recovered at once with function | |
232 `recover-all-files'. | |
233 | |
234 Takes care to make autosave files for files accessed through efs | |
235 be local files if variable `efs-auto-save-remotely' is nil. | |
236 | |
237 Takes care of slashes in buffer names to prevent autosave errors. | |
238 | |
239 Takes care that autosave files for buffers not visiting any file (such | |
240 as `*mail*') from two simultaneous Emacses don't collide by prepending | |
241 the Emacs pid. | |
242 | |
243 Uses 14 character autosave names if `auto-save-hash-p' is true. | |
244 | |
245 Autosaves even if the current directory is not writable, using | |
246 directory `auto-save-directory-fallback'. | |
247 | |
248 You can redefine this for customization (he he :-). | |
249 See also function `auto-save-file-name-p'." | |
250 | |
251 ;; We have to be very careful about not signalling an error in this | |
252 ;; function since files.el does not provide for this (e.g. find-file | |
253 ;; would fail for each new file). | |
254 | |
255 (setq file-name (or file-name | |
256 buffer-file-truename | |
257 (and buffer-file-name | |
258 (expand-file-name buffer-file-name)))) | |
793 | 259 (with-trapping-errors |
260 :operation 'make-auto-save-file-name | |
261 :error-form | |
262 (let ((fname | |
263 (if file-name | |
264 (concat (file-name-directory file-name) | |
265 "#" | |
266 (file-name-nondirectory file-name) | |
267 "#") | |
268 (expand-file-name | |
269 (concat "#%" (auto-save-escape-name (buffer-name)) | |
270 "#"))))) | |
271 (if (or (file-writable-p fname) | |
272 (file-exists-p fname)) | |
273 fname | |
274 (expand-file-name (concat "~/" | |
275 (file-name-nondirectory fname))))) | |
276 (let ( | |
277 ;; So autosavename looks like #%...#, roughly as with the | |
278 ;; old make-auto-save-file-name function. The | |
279 ;; make-temp-name inserts the pid of this Emacs: this | |
280 ;; avoids autosaving from two Emacses into the same file. | |
281 ;; It cannot be recovered automatically then because in | |
282 ;; the next Emacs session (the one after the crash) the | |
283 ;; pid will be different, but file-less buffers like | |
284 ;; *mail* must be recovered manually anyway. | |
428 | 285 |
793 | 286 ;; jwz: putting the emacs PID in the auto-save file name is bad |
287 ;; news, because that defeats auto-save-recovery of *mail* | |
288 ;; buffers -- the (sensible) code in sendmail.el calls | |
289 ;; (make-auto-save-file-name) to determine whether there is | |
290 ;; unsent, auto-saved mail to recover. If that mail came from a | |
291 ;; previous emacs process (far and away the most likely case) | |
292 ;; then this can never succeed as the pid differs. | |
293 ;;(name-prefix (if file-name nil (make-temp-name "#%"))) | |
294 (name-prefix (if file-name nil "#%")) | |
428 | 295 |
793 | 296 (save-name (or file-name |
297 ;; Prevent autosave errors. Buffername | |
298 ;; (to become non-dir part of filename) will | |
299 ;; be escaped twice. Don't care. | |
300 (auto-save-escape-name (buffer-name)))) | |
301 (remote-p (and-fboundp 'efs-ftp-path | |
302 (stringp file-name) | |
303 (efs-ftp-path file-name)))) | |
304 ;; Return the appropriate auto save file name: | |
305 (expand-file-name;; a buffername needs this, a filename not | |
306 (cond (remote-p | |
307 (if efs-auto-save-remotely | |
308 (auto-save-name-in-same-directory save-name) | |
309 ;; We have to use the `fixed-directory' now since the | |
310 ;; `same-directory' would be remote. | |
311 ;; It will use the fallback if needed. | |
312 (auto-save-name-in-fixed-directory save-name))) | |
313 ;; Else it is a local file (or a buffer without a file, | |
314 ;; hence the name-prefix). | |
315 ((or auto-save-directory auto-save-hash-p) | |
316 ;; Hashed files always go into the special hash dir, | |
317 ;; never in the same directory, to make recognizing | |
318 ;; reliable. | |
319 (auto-save-name-in-fixed-directory save-name name-prefix)) | |
320 (t | |
321 (auto-save-name-in-same-directory save-name name-prefix))))))) | |
464 | 322 |
323 (defun auto-save-file-name-p (filename) | |
324 "Return non-nil if FILENAME can be yielded by `make-auto-save-file-name'. | |
325 FILENAME should lack slashes. | |
326 You can redefine this for customization." | |
327 (string-match "\\`#.*#\\'" filename)) | |
428 | 328 |
329 (defun auto-save-original-name (savename) | |
330 "Reverse of `make-auto-save-file-name'. | |
331 Returns nil if SAVENAME was not associated with a file (e.g., it came | |
332 from an autosaved `*mail*' buffer) or does not appear to be an | |
333 autosave file at all. | |
334 Hashed files are not understood, see `auto-save-hash-p'." | |
335 (let ((basename (file-name-nondirectory savename)) | |
336 (savedir (file-name-directory savename))) | |
337 (cond ((or (not (auto-save-file-name-p basename)) | |
338 (string-match "^#%" basename)) | |
339 nil) | |
340 ;; now we know it looks like #...# thus substring is safe to use | |
341 ((or (equal savedir | |
342 (and auto-save-directory | |
343 (expand-file-name auto-save-directory))) | |
344 ; 2nd arg may be nil | |
345 (equal savedir | |
346 (expand-file-name auto-save-directory-fallback))) | |
347 ;; it is of the `-fixed-directory' type | |
464 | 348 (auto-save-unescape-name (substring basename 1 -1))) |
428 | 349 (t |
350 ;; else it is of `-same-directory' type | |
351 (concat savedir (substring basename 1 -1)))))) | |
352 | |
353 (defun auto-save-name-in-fixed-directory (filename &optional prefix) | |
464 | 354 ;; Escape and enclose the whole FILENAME in `#' to make an auto |
428 | 355 ;; save file in the auto-save-directory, or if that is nil, in |
356 ;; auto-save-directory-fallback (which must be the name of an | |
357 ;; existing directory). If the results would be too long for 14 | |
358 ;; character filenames, and `auto-save-hash-p' is set, hash FILENAME | |
359 ;; into a shorter name. | |
360 ;; Optional PREFIX is string to use instead of "#" to prefix name. | |
361 (let ((base-name (concat (or prefix "#") | |
464 | 362 (auto-save-escape-name filename) |
428 | 363 "#"))) |
364 (if (and auto-save-hash-p | |
365 auto-save-hash-directory | |
366 (> (length base-name) 14)) | |
367 (expand-file-name (auto-save-cyclic-hash-14 filename) | |
368 (auto-save-checked-directory auto-save-hash-directory)) | |
369 (expand-file-name base-name | |
370 (auto-save-checked-directory | |
371 (or auto-save-directory | |
372 auto-save-directory-fallback)))))) | |
373 | |
374 (defun auto-save-name-in-same-directory (filename &optional prefix) | |
375 ;; Enclose the non-directory part of FILENAME in `#' to make an auto | |
376 ;; save file in the same directory as FILENAME. But if this | |
377 ;; directory is not writable, use auto-save-directory-fallback. | |
378 ;; FILENAME is assumed to be in non-directory form (no trailing slash). | |
440 | 379 ;; It may be a name without a directory part (presumably it really |
428 | 380 ;; comes from a buffer name then), the fallback is used then. |
381 ;; Optional PREFIX is string to use instead of "#" to prefix name. | |
382 (let ((directory (file-name-directory filename))) | |
383 (or (null directory) | |
384 (file-writable-p directory) | |
385 (setq directory (auto-save-checked-directory | |
386 auto-save-directory-fallback))) | |
387 (concat directory ; (concat nil) is "" | |
388 (or prefix "#") | |
389 (file-name-nondirectory filename) | |
390 "#"))) | |
391 | |
464 | 392 (defconst auto-save-reserved-chars |
393 '( | |
394 ?\0 ?\1 ?\2 ?\3 ?\4 ?\5 ?\6 ?\7 ?\10 ?\11 ?\12 ?\13 ?\14 ?\15 ?\16 | |
395 ?\17 ?\20 ?\21 ?\22 ?\23 ?\24 ?\25 ?\26 ?\27 ?\30 ?\31 ?\32 ?\33 | |
396 ?\34 ?\35 ?\36 ?\37 ?\40 ?? ?* ?: ?< ?> ?| ?/ ?\\ ?& ?^ ?% ?= ?\") | |
397 "List of characters disallowed (or potentially disallowed) in filenames. | |
398 Includes everything that can get us into trouble under MS Windows or Unix.") | |
399 | |
400 ;; This code based on code in Bill Perry's url.el. | |
401 | |
402 (defun auto-save-escape-name (str) | |
403 "Escape any evil nasty characters in a potential filename. | |
404 Uses quoted-printable-style escaping -- e.g. the dreaded =3D. | |
405 Does not use URL escaping (with %) because filenames beginning with #% are | |
406 a special signal for non-file buffers." | |
407 (mapconcat | |
408 (function | |
409 (lambda (char) | |
410 (if (memq char auto-save-reserved-chars) | |
411 (if (< char 16) | |
412 (upcase (format "=0%x" char)) | |
413 (upcase (format "=%x" char))) | |
414 (char-to-string char)))) | |
415 str "")) | |
416 | |
417 (defun auto-save-unhex (x) | |
418 (if (> x ?9) | |
419 (if (>= x ?a) | |
420 (+ 10 (- x ?a)) | |
421 (+ 10 (- x ?A))) | |
422 (- x ?0))) | |
428 | 423 |
464 | 424 (defun auto-save-unescape-name (str) |
425 "Undo any escaping of evil nasty characters in a file name. | |
426 See `auto-save-escape-name'." | |
427 (setq str (or str "")) | |
428 (let ((tmp "") | |
429 (case-fold-search t)) | |
430 (while (string-match "=[0-9a-f][0-9a-f]" str) | |
431 (let* ((start (match-beginning 0)) | |
432 (ch1 (auto-save-unhex (elt str (+ start 1)))) | |
433 (code (+ (* 16 ch1) | |
434 (auto-save-unhex (elt str (+ start 2)))))) | |
435 (setq tmp (concat tmp (substring str 0 start) | |
436 (char-to-string code)) | |
437 str (substring str (match-end 0))))) | |
438 (setq tmp (concat tmp str)) | |
439 tmp)) | |
440 | |
441 ;; The old versions are below. | |
428 | 442 |
464 | 443 ;(defun auto-save-escape-name (s) |
444 ; ;; "Quote any slashes in string S by replacing them with the two | |
445 ; ;;characters `\\!'. | |
446 ; ;;Also, replace any backslash by double backslash, to make it one-to-one." | |
447 ; (let ((limit 0)) | |
448 ; (while (string-match "[/\\]" s limit) | |
449 ; (setq s (concat (substring s 0 (match-beginning 0)) | |
450 ; (if (string= (substring s | |
451 ; (match-beginning 0) | |
452 ; (match-end 0)) | |
453 ; "/") | |
454 ; "\\!" | |
455 ; "\\\\") | |
456 ; (substring s (match-end 0)))) | |
457 ; (setq limit (1+ (match-end 0))))) | |
458 ; s) | |
459 | |
460 ;(defun auto-save-unescape-name (s) | |
461 ; ;;"Reverse of `auto-save-escape-name'." | |
462 ; (let (pos) | |
463 ; (while (setq pos (string-match "\\\\[\\!]" s pos)) | |
464 ; (setq s (concat (substring s 0 pos) | |
465 ; (if (eq ?! (aref s (1+ pos))) "/" "\\") | |
466 ; (substring s (+ pos 2))) | |
467 ; pos (1+ pos)))) | |
468 ; s) | |
428 | 469 |
470 | |
471 ;;; Hashing for autosave names | |
472 | |
473 ;;; Hashing function contributed by Andy Norman <ange@hplb.hpl.hp.com> | |
474 ;;; based upon C code from pot@fly.cnuce.cnr.IT (Francesco Potorti`). | |
475 | |
476 (defun auto-save-cyclic-hash-14 (s) | |
477 ;; "Hash string S into a string of length 14. | |
478 ;; A 7-bytes cyclic code for burst correction is calculated on a | |
479 ;; byte-by-byte basis. The polynomial used is D^7 + D^6 + D^3 +1. | |
480 ;; The resulting string consists of hexadecimal digits [0-9a-f]. | |
481 ;; In particular, it contains no slash, so it can be used as autosave name." | |
482 (let ((crc (make-vector 7 ?\0))) | |
483 (mapc | |
484 (lambda (new) | |
485 (setq new (+ new (aref crc 6))) | |
486 (aset crc 6 (+ (aref crc 5) new)) | |
487 (aset crc 5 (aref crc 4)) | |
488 (aset crc 4 (aref crc 3)) | |
489 (aset crc 3 (+ (aref crc 2) new)) | |
490 (aset crc 2 (aref crc 1)) | |
491 (aset crc 1 (aref crc 0)) | |
492 (aset crc 0 new)) | |
493 s) | |
494 (format "%02x%02x%02x%02x%02x%02x%02x" | |
495 (logand 255 (aref crc 0)) | |
496 (logand 255 (aref crc 1)) | |
497 (logand 255 (aref crc 2)) | |
498 (logand 255 (aref crc 3)) | |
499 (logand 255 (aref crc 4)) | |
500 (logand 255 (aref crc 5)) | |
501 (logand 255 (aref crc 6))))) | |
502 | |
503 ;; #### It is unclear to me how the following function is useful. It | |
504 ;; should be used in `auto-save-name-in-same-directory', if anywhere. | |
505 ;; -hniksic | |
506 | |
507 ;; This leaves two characters that could be used to wrap it in `#' or | |
508 ;; make two filenames from it: one for autosaving, and another for a | |
464 | 509 ;; file containing the name of the autosaved file, to make hashing |
428 | 510 ;; reversible. |
511 ;(defun auto-save-cyclic-hash-12 (s) | |
512 ; "Outputs the 12-characters ascii hex representation of a 6-bytes | |
513 ;cyclic code for burst correction calculated on STRING on a | |
514 ;byte-by-byte basis. The used polynomial is D^6 + D^5 + D^4 + D^3 +1." | |
515 ; (let ((crc (make-string 6 0))) | |
516 ; (mapc | |
517 ; (lambda (new) | |
518 ; (setq new (+ new (aref crc 5))) | |
519 ; (aset crc 5 (+ (aref crc 4) new)) | |
520 ; (aset crc 4 (+ (aref crc 3) new)) | |
521 ; (aset crc 3 (+ (aref crc 2) new)) | |
522 ; (aset crc 2 (aref crc 1)) | |
523 ; (aset crc 1 (aref crc 0)) | |
524 ; (aset crc 0 new)) | |
525 ; s) | |
526 ; (format "%02x%02x%02x%02x%02x%02x" | |
527 ; (aref crc 0) | |
528 ; (aref crc 1) | |
529 ; (aref crc 2) | |
530 ; (aref crc 3) | |
531 ; (aref crc 4) | |
532 ; (aref crc 5)))) | |
533 | |
534 | |
535 | |
536 ;;; Recovering files | |
537 | |
538 (defun recover-all-files (&optional silent) | |
539 "Do recover-file for all autosave files which are current. | |
540 Only works if you have a non-nil `auto-save-directory'. | |
541 | |
542 Optional prefix argument SILENT means to be silent about non-current | |
543 autosave files. This is useful if invoked automatically at Emacs | |
544 startup. | |
545 | |
546 If `auto-save-offer-delete' is t, this function will offer to delete | |
547 old or rejected autosave files. | |
548 | |
549 Hashed files (see `auto-save-hash-p') are not understood, use | |
550 `recover-file' to recover them individually." | |
551 (interactive "P") | |
552 (let ((savefiles (directory-files auto-save-directory | |
553 t "\\`#" nil t)) | |
554 afile ; the auto save file | |
555 file ; its original file | |
556 (total 0) ; # of files offered to recover | |
557 (count 0)) ; # of files actually recovered | |
558 (or (equal (expand-file-name auto-save-directory) | |
559 (expand-file-name auto-save-directory-fallback)) | |
560 (setq savefiles | |
561 (nconc savefiles | |
562 (directory-files auto-save-directory-fallback | |
563 t "\\`#" nil t)))) | |
564 (while savefiles | |
565 (setq afile (car savefiles) | |
566 file (auto-save-original-name afile) | |
567 savefiles (cdr savefiles)) | |
568 (cond ((and file (not (file-newer-than-file-p afile file))) | |
793 | 569 (warn "Autosave file \"%s\" is not current" afile)) |
428 | 570 (t |
571 (incf total) | |
572 (with-output-to-temp-buffer "*Directory*" | |
464 | 573 (buffer-disable-undo standard-output) |
574 (save-excursion | |
575 (set-buffer "*Directory*") | |
576 (setq default-directory (file-name-directory afile)) | |
577 (insert-directory afile "-l") | |
578 (when file | |
579 (setq default-directory (file-name-directory file)) | |
580 (insert-directory file "-l")))) | |
428 | 581 (if (yes-or-no-p (format "Recover %s from auto save file? " |
582 (or file "non-file buffer"))) | |
583 (let* ((obuf (current-buffer))) | |
584 (set-buffer (if file | |
585 (find-file-noselect file t) | |
586 (generate-new-buffer "*recovered*"))) | |
587 (setq buffer-read-only nil) | |
588 (erase-buffer) | |
771 | 589 ;; see comment in `revert-buffer' in files.el about |
590 ;; why it's safe to always use `escape-quoted'. | |
591 (let ((coding-system-for-read 'escape-quoted)) | |
592 (insert-file-contents afile nil)) | |
428 | 593 (ignore-errors |
594 (after-find-file nil)) | |
595 (setq buffer-auto-save-file-name nil) | |
596 (incf count) | |
597 (message "\ | |
598 Auto-save off in buffer \"%s\" till you do M-x auto-save-mode." | |
599 (buffer-name)) | |
600 (set-buffer obuf) | |
601 (sit-for 1)) | |
602 ;; If not used for recovering, offer to delete | |
603 ;; autosave file | |
604 (and auto-save-offer-delete | |
605 (or (eq 'always auto-save-offer-delete) | |
606 (yes-or-no-p | |
607 (format "Delete autosave file for `%s'? " file))) | |
608 (delete-file afile)))))) | |
609 (if (zerop total) | |
610 (or silent (message "Nothing to recover.")) | |
611 (message "%d/%d file%s recovered." count total (if (= count 1) "" "s")))) | |
612 (and (get-buffer "*Directory*") | |
613 (kill-buffer "*Directory*"))) | |
614 | |
615 ;;; auto-save.el ends here |