]> code.delx.au - gnu-emacs/blobdiff - lisp/shadowfile.el
Merge changes from emacs-23 branch
[gnu-emacs] / lisp / shadowfile.el
index b3427ac59e5e3535d9381d3a9d2597aa538a9d97..1a929ebb58a166a542ddf3a0e493cf60fb7813c1 100644 (file)
@@ -1,16 +1,16 @@
 ;;; shadowfile.el --- automatic file copying
 
-;; Copyright (C) 1993, 1994, 2001, 2002 Free Software Foundation, Inc.
+;; Copyright (C) 1993-1994, 2001-2011 Free Software Foundation, Inc.
 
 ;; Author: Boris Goldowsky <boris@gnu.org>
 ;; Keywords: comm files
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
 ;;  This package helps you to keep identical copies of files in more than one
 ;;  place - possibly on different machines.  When you save a file, it checks
 ;;  whether it is on the list of files with "shadows", and if so, it tries to
-;;  copy it when you exit emacs (or use the shadow-copy-files command).
+;;  copy it when you exit Emacs (or use the shadow-copy-files command).
 
 ;; Installation & Use:
 
@@ -37,8 +35,8 @@
 ;;  them).  After doing this once, everything should be automatic.
 
 ;;  The lists of clusters and shadows are saved in a file called .shadows,
-;;  so that they can be remembered from one emacs session to another, even
-;;  (as much as possible) if the emacs session terminates abnormally.  The
+;;  so that they can be remembered from one Emacs session to another, even
+;;  (as much as possible) if the Emacs session terminates abnormally.  The
 ;;  files needing to be copied are stored in .shadow_todo; if a file cannot
 ;;  be copied for any reason, it will stay on the list to be tried again
 ;;  next time.  The .shadows file should itself have shadows on all your
@@ -46,7 +44,7 @@
 ;;  .shadow_todo is local information and should have no shadows.
 
 ;;  If you do not want to copy a particular file, you can answer "no" and
-;;  be asked again next time you hit C-x 4 s or exit emacs.  If you do not
+;;  be asked again next time you hit C-x 4 s or exit Emacs.  If you do not
 ;;  want to be asked again, use shadow-cancel, and you will not be asked
 ;;  until you change the file and save it again.  If you do not want to
 ;;  shadow that file ever again, you can edit it out of the .shadows
   :group 'files)
 
 (defcustom shadow-noquery nil
-  "*If t, always copy shadow files without asking.
-If nil \(the default), always ask.  If not nil and not t, ask only if there
+  "If t, always copy shadow files without asking.
+If nil (the default), always ask.  If not nil and not t, ask only if there
 is no buffer currently visiting the file."
   :type '(choice (const t) (const nil) (other :tag "Ask if no buffer" maybe))
   :group 'shadow)
 
 (defcustom shadow-inhibit-message nil
-  "*If nonnil, do not display a message when a file needs copying."
+  "If non-nil, do not display a message when a file needs copying."
   :type 'boolean
   :group 'shadow)
 
 (defcustom shadow-inhibit-overload nil
-  "If nonnil, shadowfile won't redefine \\[save-buffers-kill-emacs].
-Normally it overloads the function `save-buffers-kill-emacs' to check
-for files have been changed and need to be copied to other systems."
+  "If non-nil, shadowfile won't redefine \\[save-buffers-kill-emacs].
+Normally it overloads the function `save-buffers-kill-emacs' to check for
+files that have been changed and need to be copied to other systems."
   :type 'boolean
   :group 'shadow)
 
@@ -127,7 +125,7 @@ Default: ~/.shadow_todo"
 
 ;;; The following two variables should in most cases initialize themselves
 ;;; correctly.  They are provided as variables in case the defaults are wrong
-;;; on your machine \(and for efficiency).
+;;; on your machine (and for efficiency).
 
 (defvar shadow-system-name (system-name)
   "The complete hostname of this machine.")
@@ -140,12 +138,12 @@ Default: ~/.shadow_todo"
 ;;;
 
 (defvar shadow-clusters nil
-  "List of host clusters \(see `shadow-define-cluster').")
+  "List of host clusters (see `shadow-define-cluster').")
 
 (defvar shadow-literal-groups nil
   "List of files that are shared between hosts.
 This list contains shadow structures with literal filenames, created by
-shadow-define-group.")
+`shadow-define-literal-group'.")
 
 (defvar shadow-regexp-groups nil
   "List of file types that are shared between hosts.
@@ -177,7 +175,7 @@ created by `shadow-define-regexp-group'.")
       (shadow-union (cdr a) (cons (car a) b)))))
 
 (defun shadow-find (func list)
-  "If FUNC applied to some element of LIST is nonnil, return first such element."
+  "If FUNC applied to some element of LIST is non-nil, return first such element."
   (while (and list (not (funcall func (car list))))
     (setq list (cdr list)))
   (car list))
@@ -191,12 +189,6 @@ Nondestructive; actually returns a copy of the list with the elements removed."
        (cons (car list) (shadow-remove-if func (cdr list))))
     nil))
 
-(defun shadow-join (strings sep)
-  "Concatenate elements of the list of STRINGS with SEP between each."
-  (cond ((null strings) "")
-       ((null (cdr strings)) (car strings))
-       ((concat (car strings) " " (shadow-join (cdr strings) sep)))))
-
 (defun shadow-regexp-superquote (string)
   "Like `regexp-quote', but includes the ^ and $.
 This makes sure regexp matches nothing but STRING."
@@ -204,7 +196,7 @@ This makes sure regexp matches nothing but STRING."
 
 (defun shadow-suffix (prefix string)
   "If PREFIX begins STRING, return the rest.
-Return value is nonnil if PREFIX and STRING are string= up to the length of
+Return value is non-nil if PREFIX and STRING are `string=' up to the length of
 PREFIX."
   (let ((lp (length prefix))
        (ls (length string)))
@@ -267,7 +259,7 @@ information defining the cluster.  For interactive use, call
 ;;; SITES
 
 (defun shadow-site-cluster (site)
-  "Given a SITE \(hostname or cluster name), return cluster it is in, or nil."
+  "Given a SITE (hostname or cluster name), return cluster it is in, or nil."
   (or (assoc site shadow-clusters)
       (shadow-find
        (function (lambda (x)
@@ -284,9 +276,9 @@ information defining the cluster.  For interactive use, call
       ans)))
 
 (defun shadow-site-match (site1 site2)
-  "Nonnil iff SITE1 is or includes SITE2.
-Each may be a host or cluster name; if they are clusters, regexp of site1 will
-be matched against the primary of site2."
+  "Non-nil if SITE1 is or includes SITE2.
+Each may be a host or cluster name; if they are clusters, regexp of SITE1 will
+be matched against the primary of SITE2."
   (or (string-equal site1 site2) ; quick check
       (let* ((cluster1 (shadow-get-cluster site1))
             (primary2 (shadow-site-primary site2)))
@@ -303,15 +295,15 @@ be matched against the primary of site2."
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 (defun shadow-parse-fullname (fullname)
-  "Parse FULLNAME into \(site user path) list.
-Leave it alone if it already is one.  Returns nil if the argument is
+  "Parse FULLNAME into (site user path) list.
+Leave it alone if it already is one.  Return nil if the argument is
 not a full ange-ftp pathname."
   (if (listp fullname)
       fullname
     (ange-ftp-ftp-name fullname)))
 
 (defun shadow-parse-name (name)
-  "Parse any NAME into \(site user name) list.
+  "Parse any NAME into (site user name) list.
 Argument can be a simple name, full ange-ftp name, or already a hup list."
   (or (shadow-parse-fullname name)
       (list shadow-system-name
@@ -345,8 +337,7 @@ return nil."
 (defun shadow-expand-cluster-in-file-name (file)
   "If hostname part of FILE is a cluster, expand it to cluster's primary hostname.
 Will return the name bare if it is a local file."
-  (let ((hup (shadow-parse-name file))
-       cluster)
+  (let ((hup (shadow-parse-name file)))
     (cond ((null hup) file)
          ((shadow-local-file hup))
          ((shadow-make-fullname (shadow-site-primary (nth 0 hup))
@@ -354,7 +345,7 @@ Will return the name bare if it is a local file."
                                 (nth 2 hup))))))
 
 (defun shadow-expand-file-name (file &optional default)
-  "Expand file name and get file's true name."
+  "Expand file name and get FILE's true name."
   (file-truename (expand-file-name file default)))
 
 (defun shadow-contract-file-name (file)
@@ -397,9 +388,9 @@ local filename."
  "Return t if PATTERN matches FILE.
 If REGEXP is supplied and non-nil, the file part of the pattern is a regular
 expression, otherwise it must match exactly.  The sites and usernames must
-match---see shadow-same-site.  The pattern must be in full ange-ftp format, but
-the file can be any valid filename.  This function does not do any filename
-expansion or contraction, you must do that yourself first."
+match---see `shadow-same-site'.  The pattern must be in full ange-ftp format,
+but the file can be any valid filename.  This function does not do any
+filename expansion or contraction, you must do that yourself first."
  (let* ((pattern-sup (shadow-parse-fullname pattern))
        (file-sup (shadow-parse-name file)))
    (and (shadow-same-site pattern-sup file-sup)
@@ -413,12 +404,12 @@ expansion or contraction, you must do that yourself first."
 
 ;;;###autoload
 (defun shadow-define-cluster (name)
-  "Edit \(or create) the definition of a cluster NAME.
+  "Edit (or create) the definition of a cluster NAME.
 This is a group of hosts that share directories, so that copying to or from
 one of them is sufficient to update the file on all of them.  Clusters are
-defined by a name, the network address of a primary host \(the one we copy
-files to), and a regular expression that matches the hostnames of all the sites
-in the cluster."
+defined by a name, the network address of a primary host (the one we copy
+files to), and a regular expression that matches the hostnames of all the
+sites in the cluster."
   (interactive (list (completing-read "Cluster name: " shadow-clusters () ())))
   (let* ((old (shadow-get-cluster name))
         (primary (read-string "Primary host: "
@@ -437,7 +428,7 @@ in the cluster."
                       (sit-for 2))
                     try-regexp))
 ;       (username (read-no-blanks-input
-;                  (format "Username [default: %s]: "
+;                  (format "Username (default %s): "
 ;                          (shadow-get-user primary))
 ;                  (if old (or (shadow-cluster-username old) "")
 ;                    (user-login-name))))
@@ -450,14 +441,14 @@ in the cluster."
   "Declare a single file to be shared between sites.
 It may have different filenames on each site.  When this file is edited, the
 new version will be copied to each of the other locations.  Sites can be
-specific hostnames, or names of clusters \(see `shadow-define-cluster')."
+specific hostnames, or names of clusters (see `shadow-define-cluster')."
   (interactive)
   (let* ((hup (shadow-parse-fullname
               (shadow-contract-file-name (buffer-file-name))))
         (name (nth 2 hup))
         user site group)
     (while (setq site (shadow-read-site))
-      (setq user (read-string (format "Username [default %s]: "
+      (setq user (read-string (format "Username (default %s): "
                                      (shadow-get-user site)))
            name (read-string "Filename: " name))
       (setq group (cons (shadow-make-fullname site
@@ -474,8 +465,8 @@ specific hostnames, or names of clusters \(see `shadow-define-cluster')."
   "Make each of a group of files be shared between hosts.
 Prompts for regular expression; files matching this are shared between a list
 of sites, which are also prompted for.  The filenames must be identical on all
-hosts \(if they aren't, use shadow-define-group instead of this function).
-Each site can be either a hostname or the name of a cluster \(see
+hosts (if they aren't, use `shadow-define-literal-group' instead of this
+function).  Each site can be either a hostname or the name of a cluster (see
 `shadow-define-cluster')."
   (interactive)
   (let ((regexp (read-string
@@ -502,9 +493,7 @@ Each site can be either a hostname or the name of a cluster \(see
   ;; Mostly for debugging.
   "Interactive function to display shadows of a buffer."
   (interactive)
-  (let ((msg (shadow-join (mapcar (function cdr)
-                                 (shadow-shadows-of (buffer-file-name)))
-                         " ")))
+  (let ((msg (mapconcat #'cdr (shadow-shadows-of (buffer-file-name)) " ")))
     (message "%s"
             (if (zerop (length msg))
                 "No shadows."
@@ -518,8 +507,9 @@ Pending copies are stored in variable `shadow-files-to-copy', and in
 `shadow-save-buffers-kill-emacs', so it is not usually necessary to
 call it manually."
   (interactive "P")
-  (if (and (not shadow-files-to-copy) (interactive-p))
-      (message "No files need to be shadowed.")
+  (if (not shadow-files-to-copy)
+      (if (called-interactively-p 'interactive)
+         (message "No files need to be shadowed."))
     (save-excursion
       (map-y-or-n-p (function
                     (lambda (pair)
@@ -554,7 +544,7 @@ permanently, remove the group from `shadow-literal-groups' or
 
 (defun shadow-make-group (regexp sites usernames)
   "Make a description of a file group---
-actually a list of regexp ange-ftp file names---from REGEXP \(name of file to
+actually a list of regexp ange-ftp file names---from REGEXP (name of file to
 be shadowed), list of SITES, and corresponding list of USERNAMES for each
 site."
   (if sites
@@ -581,18 +571,16 @@ site."
         (to (shadow-expand-cluster-in-file-name (cdr s))))
     (when buffer
       (set-buffer buffer)
-      (save-restriction
-       (widen)
-       (condition-case i
-           (progn
-             (write-region (point-min) (point-max) to)
-             (shadow-remove-from-todo s))
-         (error (message "Shadow %s not updated!" (cdr s))))))))
+      (condition-case nil
+         (progn
+            (write-region nil nil to)
+            (shadow-remove-from-todo s))
+        (error (message "Shadow %s not updated!" (cdr s)))))))
 
 (defun shadow-shadows-of (file)
   "Return copy operations needed to update FILE.
 Filename should have clusters expanded, but otherwise can have any format.
-Return value is a list of dotted pairs like \(from . to), where from
+Return value is a list of dotted pairs like (from . to), where from
 and to are absolute file names."
   (or (symbol-value (intern-soft file shadow-hashtable))
       (let* ((absolute-file (shadow-expand-file-name
@@ -641,11 +629,11 @@ Consider them as regular expressions if third arg REGEXP is true."
                       "Use \\[shadow-copy-files] to update shadows."))
        (sit-for 1))
       (shadow-write-todo-file)))
-  nil)     ; Return nil for write-file-hooks
+  nil)     ; Return nil for write-file-functions
 
 (defun shadow-remove-from-todo (pair)
   "Remove PAIR from `shadow-files-to-copy'.
-PAIR must be (eq to) one of the elements of that list."
+PAIR must be `eq' to one of the elements of that list."
   (setq shadow-files-to-copy
        (shadow-remove-if (function (lambda (s) (eq s pair)))
                          shadow-files-to-copy)))
@@ -653,13 +641,13 @@ PAIR must be (eq to) one of the elements of that list."
 (defun shadow-read-files ()
   "Visit and load `shadow-info-file' and `shadow-todo-file'.
 Thus restores shadowfile's state from your last Emacs session.
-Returns t unless files were locked; then returns nil."
+Return t unless files were locked; then return nil."
   (interactive)
   (if (and (fboundp 'file-locked-p)
           (or (stringp (file-locked-p shadow-info-file))
               (stringp (file-locked-p shadow-todo-file))))
       (progn
-       (message "Shadowfile is running in another emacs; can't have two.")
+       (message "Shadowfile is running in another Emacs; can't have two.")
        (beep)
        (sit-for 3)
        nil)
@@ -674,7 +662,7 @@ Returns t unless files were locked; then returns nil."
          (message "Data recovered from %s."
                   (car (insert-file-contents (make-auto-save-file-name))))
          (sit-for 1))
-       (eval-current-buffer))
+       (eval-buffer))
       (when shadow-todo-file
        (set-buffer (setq shadow-todo-buffer
                          (find-file-noselect shadow-todo-file)))
@@ -685,7 +673,7 @@ Returns t unless files were locked; then returns nil."
          (message "Data recovered from %s."
                   (car (insert-file-contents (make-auto-save-file-name))))
          (sit-for 1))
-       (eval-current-buffer nil))
+       (eval-buffer nil))
       (shadow-invalidate-hashtable))
     t))
 
@@ -705,8 +693,8 @@ defined, the old hashtable info is invalid."
        (shadow-insert-var 'shadow-regexp-groups))))
 
 (defun shadow-write-todo-file (&optional save)
-  "Write out information to shadow-todo-file.
-With nonnil argument also saves the buffer."
+  "Write out information to `shadow-todo-file'.
+With non-nil argument also saves the buffer."
   (save-excursion
     (if (not shadow-todo-buffer)
        (setq shadow-todo-buffer (find-file-noselect shadow-todo-file)))
@@ -717,8 +705,7 @@ With nonnil argument also saves the buffer."
 
 (defun shadow-save-todo-file ()
   (if (and shadow-todo-buffer (buffer-modified-p shadow-todo-buffer))
-      (save-excursion
-       (set-buffer shadow-todo-buffer)
+      (with-current-buffer shadow-todo-buffer
        (condition-case nil             ; have to continue even in case of
            (basic-save-buffer)         ; error, otherwise kill-emacs might
          (error                        ; not work!
@@ -729,9 +716,10 @@ With nonnil argument also saves the buffer."
   (setq shadow-hashtable (make-vector 37 0)))
 
 (defun shadow-insert-var (variable)
-  "Prettily insert a setq command for VARIABLE.
-which, when later evaluated, will restore it to its current setting.
-SYMBOL must be the name of a variable whose value is a list."
+  "Build a `setq' to restore VARIABLE.
+Prettily insert a `setq' command which, when later evaluated,
+will restore VARIABLE to its current setting.
+VARIABLE must be the name of a variable whose value is a list."
   (let ((standard-output (current-buffer)))
     (insert (format "(setq %s" variable))
     (cond ((consp (eval variable))
@@ -771,7 +759,7 @@ look for files that have been changed and need to be copied to other systems."
                                (buffer-list))))
           (yes-or-no-p "Modified buffers exist; exit anyway? "))
        (or (not (fboundp 'process-list))
-          ;; process-list is not defined on VMS.
+          ;; process-list is not defined on MSDOS.
           (let ((processes (process-list))
                 active)
             (while processes
@@ -814,10 +802,11 @@ look for files that have been changed and need to be copied to other systems."
            (file-name-as-directory (shadow-expand-file-name "~"))))
   (if (null shadow-info-file)
       (setq shadow-info-file
-           (shadow-expand-file-name "~/.shadows")))
+           (shadow-expand-file-name (convert-standard-filename "~/.shadows"))))
   (if (null shadow-todo-file)
       (setq shadow-todo-file
-           (shadow-expand-file-name "~/.shadow_todo")))
+           (shadow-expand-file-name
+            (convert-standard-filename "~/.shadow_todo"))))
   (if (not (shadow-read-files))
       (progn
        (message "Shadowfile information files not found - aborting")
@@ -828,16 +817,17 @@ look for files that have been changed and need to be copied to other systems."
       (defalias 'shadow-orig-save-buffers-kill-emacs
        (symbol-function 'save-buffers-kill-emacs))
       (defalias 'save-buffers-kill-emacs 'shadow-save-buffers-kill-emacs))
-    (add-hook 'write-file-hooks 'shadow-add-to-todo)
+    (add-hook 'write-file-functions 'shadow-add-to-todo)
     (define-key ctl-x-4-map "s" 'shadow-copy-files)))
 
-(defun shadowfile-unload-hook ()
-  (if (fboundp 'shadow-orig-save-buffers-kill-emacs)
-      (fset 'save-buffers-kill-emacs
-           (symbol-function 'shadow-orig-save-buffers-kill-emacs)))
-  (remove-hook 'write-file-hooks 'shadow-add-to-todo))
+(defun shadowfile-unload-function ()
+  (substitute-key-definition 'shadow-copy-files nil ctl-x-4-map)
+  (when (fboundp 'shadow-orig-save-buffers-kill-emacs)
+    (fset 'save-buffers-kill-emacs
+         (symbol-function 'shadow-orig-save-buffers-kill-emacs)))
+  ;; continue standard unloading
+  nil)
 
 (provide 'shadowfile)
 
-;;; arch-tag: e2f4cdd7-2bab-4def-9130-9e69b412b79e
 ;;; shadowfile.el ends here