home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / packages / auto-save.el < prev    next >
Encoding:
Text File  |  1992-08-18  |  17.5 KB  |  465 lines

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