(U-00024182): Apply new conventions for glyph granularity.
[chise/xemacs-chise.git.1] / lisp / auto-save.el
index e7df3e6..a54b581 100644 (file)
@@ -2,6 +2,7 @@
 
 ;; Copyright (C) 1997 Free Software Foundation, Inc.
 ;; Copyright (C) 1992 by Sebastian Kremer <sk@thp.uni-koeln.de>
+;; Copyright (C) 2001 Ben Wing.
 
 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
 ;; Maintainer: XEmacs Development Team
 ;; disk, in case NFS is slow.  The auto-save file used for
 ;;     /usr/foo/bar/baz.txt
 ;; will be
-;;     AUTOSAVE/#\!usr\!foo\!bar\!baz.txt#
+;;     AUTOSAVE/#=2Fusr=2Ffoo=2Fbar=2Fbaz.txt#"
 ;; assuming AUTOSAVE is the non-nil value of the variable
 ;; `auto-save-directory'.
 
-;; Takes care that autosave files for non-file-buffers (e.g. *mail*)
-;; from two simultaneous Emacses don't collide.
-
 ;; Autosaves even if the current directory is not writable.
 
 ;; Can limit autosave names to 14 characters using a hash function,
@@ -60,7 +58,7 @@
 ;;       (concat "/tmp/" (user-login-name) "-autosave/"))
 
 ;; If you don't want to save in /tmp (e.g., because it is swap
-;; mounted) but rather in ~/autosave/
+;; mounted) but rather in ~/.autosave/
 ;;   (setq auto-save-directory (expand-file-name "~/.autosave/"))
 
 ;; If you want to save each file in its own directory (the default)
@@ -79,7 +77,7 @@
 ;;; Acknowledgement:
 
 ;; This code is loosely derived from autosave-in-tmp.el by Jamie
-;; Zawinski <jwz@netscape.com> (the version I had was last modified 22
+;; Zawinski <jwz@jwz.org> (the version I had was last modified 22
 ;; dec 90 jwz) and code submitted to ange-ftp-lovers on Sun, 5 Apr
 ;; 92 23:20:47 EDT by drw@BOURBAKI.MIT.EDU (Dale R. Worley).
 ;; auto-save.el tries to cover the functionality of those two
@@ -144,7 +142,7 @@ a file named
 
 will have a longish filename like
 
-    AUTO-SAVE-DIRECTORY/#\\!home\\!sk\\!lib\\!emacs\\!lisp\\!auto-save.el#
+    AUTO-SAVE-DIRECTORY/#=2Fhome=2Fsk=2Flib=2Femacs=2Flisp=2Fauto-save.el#
 
 as auto save file.
 
@@ -186,7 +184,8 @@ Special value 'always deletes those files silently."
 \f
 ;;; Preparations to be done at load time
 
-(defvar auto-save-directory-fallback (expand-file-name "~/.autosave/")
+;; Do not call expand-file-name! This is evaluated at dump time now!
+(defvar auto-save-directory-fallback "~/.autosave/"
   ;; not user-variable-p, see above
   "Directory used for local autosaving of remote files if
 both `auto-save-directory' and `efs-auto-save-remotely' are nil.
@@ -199,35 +198,29 @@ created by you, never nil.")
                                auto-save-directory-fallback))
   "If non-nil, directory used for hashed autosave filenames.")
 
-(defun auto-save-check-directory (var)
-  (let ((dir (symbol-value var)))
-    (if (null dir)
-       nil
-      ;; Expand and store back into the variable
-      (set var (setq dir (expand-file-name dir)))
+(defun auto-save-checked-directory (dir)
+  "Make sure the directory DIR exists and return it expanded if non-nil."
+    (when dir
+      (setq dir (expand-file-name dir))
       ;; Make sure directory exists
-      (if (file-directory-p dir)
-         nil
+      (unless (file-directory-p dir)
        ;; Else we create and chmod 0700 the directory
        (setq dir (directory-file-name dir)) ; some systems need this
        (make-directory dir)
-       (set-file-modes dir #o700)))))
+       (set-file-modes dir #o700))
+      dir))
 
-(mapc #'auto-save-check-directory
-     '(auto-save-directory auto-save-directory-fallback))
+;; This make no sense at dump time
+;; (mapc #'auto-save-check-directory
+;     '(auto-save-directory auto-save-directory-fallback))
 
-(and auto-save-hash-p
-     (auto-save-check-directory 'auto-save-hash-directory))
+;(and auto-save-hash-p
+;     (auto-save-check-directory 'auto-save-hash-directory))
 
 \f
 ;;; Computing an autosave name for a file and vice versa
 
-;; #### Now that this file is dumped, we should turn off the routine
-;; from files.el.  But it would make it harder to remove it!
-
-(defun make-auto-save-file-name (&optional file-name);; redefines files.el
-  ;; auto-save-file-name-p need not be redefined.
-
+(defun make-auto-save-file-name (&optional file-name)
   "Return file name to use for auto-saves of current buffer.
 Does not consider `auto-save-visited-file-name'; that is checked
 before calling this function.
@@ -285,8 +278,8 @@ See also function `auto-save-file-name-p'."
            (save-name (or file-name
                           ;; Prevent autosave errors.  Buffername
                           ;; (to become non-dir part of filename) will
-                          ;; be unslashified twice.  Don't care.
-                          (auto-save-unslashify-name (buffer-name))))
+                          ;; be escaped twice.  Don't care.
+                          (auto-save-escape-name (buffer-name))))
            (remote-p (and (stringp file-name)
                           (fboundp 'efs-ftp-path)
                           (efs-ftp-path file-name))))
@@ -316,12 +309,26 @@ See also function `auto-save-file-name-p'."
 
     (error (warn "Error caught in `make-auto-save-file-name':\n%s"
                 (error-message-string error-data))
-          (if buffer-file-name
-              (concat (file-name-directory buffer-file-name)
-                      "#"
-                      (file-name-nondirectory buffer-file-name)
-                      "#")
-            (expand-file-name (concat "#%" (buffer-name) "#"))))))
+          (let ((fname
+                 (if file-name
+                     (concat (file-name-directory file-name)
+                             "#"
+                             (file-name-nondirectory file-name)
+                             "#")
+                   (expand-file-name
+                    (concat "#%" (auto-save-escape-name (buffer-name))
+                            "#")))))
+            (if (or (file-writable-p fname)
+                    (file-exists-p fname))
+                fname
+              (expand-file-name (concat "~/"
+                                        (file-name-nondirectory fname))))))))
+
+(defun auto-save-file-name-p (filename)
+  "Return non-nil if FILENAME can be yielded by `make-auto-save-file-name'.
+FILENAME should lack slashes.
+You can redefine this for customization."
+  (string-match "\\`#.*#\\'" filename))
 
 (defun auto-save-original-name (savename)
   "Reverse of `make-auto-save-file-name'.
@@ -335,16 +342,20 @@ Hashed files are not understood, see `auto-save-hash-p'."
               (string-match "^#%" basename))
           nil)
          ;; now we know it looks like #...# thus substring is safe to use
-         ((or (equal savedir auto-save-directory) ; 2nd arg may be nil
-              (equal savedir auto-save-directory-fallback))
+         ((or (equal savedir
+                     (and auto-save-directory
+                          (expand-file-name auto-save-directory)))
+                                       ; 2nd arg may be nil
+              (equal savedir
+                     (expand-file-name auto-save-directory-fallback)))
           ;; it is of the `-fixed-directory' type
-          (auto-save-slashify-name (substring basename 1 -1)))
+          (auto-save-unescape-name (substring basename 1 -1)))
          (t
           ;; else it is of `-same-directory' type
           (concat savedir (substring basename 1 -1))))))
 
 (defun auto-save-name-in-fixed-directory (filename &optional prefix)
-  ;; Unslashify and enclose the whole FILENAME in `#' to make an auto
+  ;; Escape and enclose the whole FILENAME in `#' to make an auto
   ;; save file in the auto-save-directory, or if that is nil, in
   ;; auto-save-directory-fallback (which must be the name of an
   ;; existing directory).  If the results would be too long for 14
@@ -352,62 +363,113 @@ Hashed files are not understood, see `auto-save-hash-p'."
   ;; into a shorter name.
   ;; Optional PREFIX is string to use instead of "#" to prefix name.
   (let ((base-name (concat (or prefix "#")
-                          (auto-save-unslashify-name filename)
+                          (auto-save-escape-name filename)
                           "#")))
     (if (and auto-save-hash-p
             auto-save-hash-directory
             (> (length base-name) 14))
        (expand-file-name (auto-save-cyclic-hash-14 filename)
-                         auto-save-hash-directory)
+                         (auto-save-checked-directory auto-save-hash-directory))
       (expand-file-name base-name
-                       (or auto-save-directory
-                           auto-save-directory-fallback)))))
+                       (auto-save-checked-directory
+                          (or auto-save-directory
+                              auto-save-directory-fallback))))))
 
 (defun auto-save-name-in-same-directory (filename &optional prefix)
   ;; Enclose the non-directory part of FILENAME in `#' to make an auto
   ;; save file in the same directory as FILENAME.  But if this
   ;; directory is not writable, use auto-save-directory-fallback.
   ;; FILENAME is assumed to be in non-directory form (no trailing slash).
-  ;; It may be a name without a directory part (pesumably it really
+  ;; It may be a name without a directory part (presumably it really
   ;; comes from a buffer name then), the fallback is used then.
   ;; Optional PREFIX is string to use instead of "#" to prefix name.
   (let ((directory (file-name-directory filename)))
     (or (null directory)
        (file-writable-p directory)
-       (setq directory auto-save-directory-fallback))
+       (setq directory (auto-save-checked-directory
+                        auto-save-directory-fallback)))
     (concat directory                  ; (concat nil) is ""
            (or prefix "#")
            (file-name-nondirectory filename)
            "#")))
 
-;; #### The following two should probably use `replace-in-string'.
-
-(defun auto-save-unslashify-name (s)
-  ;;  "Quote any slashes in string S by replacing them with the two
-  ;;characters `\\!'.
-  ;;Also, replace any backslash by double backslash, to make it one-to-one."
-  (let ((limit 0))
-    (while (string-match "[/\\]" s limit)
-      (setq s (concat (substring s 0 (match-beginning 0))
-                     (if (string= (substring s
-                                             (match-beginning 0)
-                                             (match-end 0))
-                                  "/")
-                         "\\!"
-                       "\\\\")
-                     (substring s (match-end 0))))
-      (setq limit (1+ (match-end 0)))))
-  s)
-
-(defun auto-save-slashify-name (s)
-  ;;"Reverse of `auto-save-unslashify-name'."
-  (let (pos)
-    (while (setq pos (string-match "\\\\[\\!]" s pos))
-      (setq s (concat (substring s 0 pos)
-                     (if (eq ?! (aref s (1+ pos))) "/" "\\")
-                     (substring s (+ pos 2)))
-           pos (1+ pos))))
-  s)
+(defconst auto-save-reserved-chars
+  '(
+    ?\0 ?\1 ?\2 ?\3 ?\4 ?\5 ?\6 ?\7 ?\10 ?\11 ?\12 ?\13 ?\14 ?\15 ?\16
+    ?\17 ?\20 ?\21 ?\22 ?\23 ?\24 ?\25 ?\26 ?\27 ?\30 ?\31 ?\32 ?\33
+    ?\34 ?\35 ?\36 ?\37 ?\40 ?? ?* ?: ?< ?> ?| ?/ ?\\ ?& ?^ ?% ?= ?\")
+  "List of characters disallowed (or potentially disallowed) in filenames.
+Includes everything that can get us into trouble under MS Windows or Unix.")
+
+;; This code based on code in Bill Perry's url.el.
+    
+(defun auto-save-escape-name (str)
+  "Escape any evil nasty characters in a potential filename.
+Uses quoted-printable-style escaping -- e.g. the dreaded =3D.
+Does not use URL escaping (with %) because filenames beginning with #% are
+a special signal for non-file buffers."
+  (mapconcat
+   (function
+    (lambda (char)
+      (if (memq char auto-save-reserved-chars)
+         (if (< char 16)
+             (upcase (format "=0%x" char))
+           (upcase (format "=%x" char)))
+       (char-to-string char))))
+   str ""))
+
+(defun auto-save-unhex (x)
+  (if (> x ?9)
+      (if (>= x ?a)
+         (+ 10 (- x ?a))
+       (+ 10 (- x ?A)))
+    (- x ?0)))
+
+(defun auto-save-unescape-name (str)
+  "Undo any escaping of evil nasty characters in a file name.
+See `auto-save-escape-name'."
+  (setq str (or str ""))
+  (let ((tmp "")
+       (case-fold-search t))
+    (while (string-match "=[0-9a-f][0-9a-f]" str)
+      (let* ((start (match-beginning 0))
+            (ch1 (auto-save-unhex (elt str (+ start 1))))
+            (code (+ (* 16 ch1)
+                     (auto-save-unhex (elt str (+ start 2))))))
+       (setq tmp (concat tmp (substring str 0 start)
+                         (char-to-string code))
+             str (substring str (match-end 0)))))
+    (setq tmp (concat tmp str))
+    tmp))
+
+;; The old versions are below.
+
+;(defun auto-save-escape-name (s)
+;  ;;  "Quote any slashes in string S by replacing them with the two
+;  ;;characters `\\!'.
+;  ;;Also, replace any backslash by double backslash, to make it one-to-one."
+;  (let ((limit 0))
+;    (while (string-match "[/\\]" s limit)
+;      (setq s (concat (substring s 0 (match-beginning 0))
+;                    (if (string= (substring s
+;                                            (match-beginning 0)
+;                                            (match-end 0))
+;                                 "/")
+;                        "\\!"
+;                      "\\\\")
+;                    (substring s (match-end 0))))
+;      (setq limit (1+ (match-end 0)))))
+;  s)
+
+;(defun auto-save-unescape-name (s)
+;  ;;"Reverse of `auto-save-escape-name'."
+;  (let (pos)
+;    (while (setq pos (string-match "\\\\[\\!]" s pos))
+;      (setq s (concat (substring s 0 pos)
+;                    (if (eq ?! (aref s (1+ pos))) "/" "\\")
+;                    (substring s (+ pos 2)))
+;          pos (1+ pos))))
+;  s)
 
 \f
 ;;; Hashing for autosave names
@@ -448,7 +510,7 @@ Hashed files are not understood, see `auto-save-hash-p'."
 
 ;; This leaves two characters that could be used to wrap it in `#' or
 ;; make two filenames from it: one for autosaving, and another for a
-;; file containing the name of the autosaved filed, to make hashing
+;; file containing the name of the autosaved file, to make hashing
 ;; reversible.
 ;(defun auto-save-cyclic-hash-12 (s)
 ;  "Outputs the 12-characters ascii hex representation of a 6-bytes
@@ -497,7 +559,8 @@ Hashed files (see `auto-save-hash-p') are not understood, use
        file                            ; its original file
        (total 0)                       ; # of files offered to recover
        (count 0))                      ; # of files actually recovered
-    (or (equal auto-save-directory auto-save-directory-fallback)
+    (or (equal (expand-file-name auto-save-directory)
+              (expand-file-name auto-save-directory-fallback))
        (setq savefiles
              (nconc savefiles
                     (directory-files auto-save-directory-fallback
@@ -511,8 +574,14 @@ Hashed files (see `auto-save-hash-p') are not understood, use
            (t
             (incf total)
             (with-output-to-temp-buffer "*Directory*"
-              (apply 'call-process "ls" nil standard-output nil
-                     "-l" afile (if file (list file))))
+              (buffer-disable-undo standard-output)
+              (save-excursion
+                (set-buffer "*Directory*")
+                (setq default-directory (file-name-directory afile))
+                (insert-directory afile "-l")
+                (when file
+                  (setq default-directory (file-name-directory file))
+                  (insert-directory file "-l"))))
             (if (yes-or-no-p (format "Recover %s from auto save file? "
                                      (or file "non-file buffer")))
                 (let* ((obuf (current-buffer)))