New file.
[emacs.git] / lisp / shadowfile.el
blob6b7eb8744c4d6885e7b67b370f8209d0696af4e6
1 ;;; shadowfile.el --- automatic file copying
3 ;; Copyright (C) 1993, 1994, 2001, 2002 Free Software Foundation, Inc.
5 ;; Author: Boris Goldowsky <boris@gnu.org>
6 ;; Keywords: comm files
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; This package helps you to keep identical copies of files in more than one
28 ;; place - possibly on different machines. When you save a file, it checks
29 ;; whether it is on the list of files with "shadows", and if so, it tries to
30 ;; copy it when you exit emacs (or use the shadow-copy-files command).
32 ;; Installation & Use:
34 ;; Add clusters (if necessary) and file groups with shadow-define-cluster,
35 ;; shadow-define-literal-group, and shadow-define-regexp-group (see the
36 ;; documentation for these functions for information on how and when to use
37 ;; them). After doing this once, everything should be automatic.
39 ;; The lists of clusters and shadows are saved in a file called .shadows,
40 ;; so that they can be remembered from one emacs session to another, even
41 ;; (as much as possible) if the emacs session terminates abnormally. The
42 ;; files needing to be copied are stored in .shadow_todo; if a file cannot
43 ;; be copied for any reason, it will stay on the list to be tried again
44 ;; next time. The .shadows file should itself have shadows on all your
45 ;; accounts so that the information in it is consistent everywhere, but
46 ;; .shadow_todo is local information and should have no shadows.
48 ;; If you do not want to copy a particular file, you can answer "no" and
49 ;; be asked again next time you hit C-x 4 s or exit emacs. If you do not
50 ;; want to be asked again, use shadow-cancel, and you will not be asked
51 ;; until you change the file and save it again. If you do not want to
52 ;; shadow that file ever again, you can edit it out of the .shadows
53 ;; buffer. Anytime you edit the .shadows buffer, you must type M-x
54 ;; shadow-read-files to load in the new information, or your changes will
55 ;; be overwritten!
57 ;; Bugs & Warnings:
59 ;; - It is bad to have two emacses both running shadowfile at the same
60 ;; time. It tries to detect this condition, but is not always successful.
62 ;; - You have to be careful not to edit a file in two locations
63 ;; before shadowfile has had a chance to copy it; otherwise
64 ;; "updating shadows" will overwrite one of the changed versions.
66 ;; - It ought to check modification times of both files to make sure
67 ;; it is doing the right thing. This will have to wait until
68 ;; file-newer-than-file-p works between machines.
70 ;; - It will not make directories for you, it just fails to copy files
71 ;; that belong in non-existent directories.
73 ;; Please report any bugs to me (boris@gnu.org). Also let me know
74 ;; if you have suggestions or would like to be informed of updates.
77 ;;; Code:
79 (require 'ange-ftp)
81 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
82 ;;; Variables
83 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
85 (defgroup shadow nil
86 "Automatic file copying when saving a file."
87 :prefix "shadow-"
88 :link '(emacs-commentary-link "shadowfile")
89 :group 'files)
91 (defcustom shadow-noquery nil
92 "*If t, always copy shadow files without asking.
93 If nil \(the default), always ask. If not nil and not t, ask only if there
94 is no buffer currently visiting the file."
95 :type '(choice (const t) (const nil) (other :tag "Ask if no buffer" maybe))
96 :group 'shadow)
98 (defcustom shadow-inhibit-message nil
99 "*If nonnil, do not display a message when a file needs copying."
100 :type 'boolean
101 :group 'shadow)
103 (defcustom shadow-inhibit-overload nil
104 "If nonnil, shadowfile won't redefine \\[save-buffers-kill-emacs].
105 Normally it overloads the function `save-buffers-kill-emacs' to check
106 for files have been changed and need to be copied to other systems."
107 :type 'boolean
108 :group 'shadow)
110 (defcustom shadow-info-file nil
111 "File to keep shadow information in.
112 The `shadow-info-file' should be shadowed to all your accounts to
113 ensure consistency. Default: ~/.shadows"
114 :type '(choice (const nil) file)
115 :group 'shadow)
117 (defcustom shadow-todo-file nil
118 "File to store the list of uncopied shadows in.
119 This means that if a remote system is down, or for any reason you cannot or
120 decide not to copy your shadow files at the end of one Emacs session, it will
121 remember and ask you again in your next Emacs session.
122 This file must NOT be shadowed to any other system, it is host-specific.
123 Default: ~/.shadow_todo"
124 :type '(choice (const nil) file)
125 :group 'shadow)
128 ;;; The following two variables should in most cases initialize themselves
129 ;;; correctly. They are provided as variables in case the defaults are wrong
130 ;;; on your machine \(and for efficiency).
132 (defvar shadow-system-name (system-name)
133 "The complete hostname of this machine.")
135 (defvar shadow-homedir nil
136 "Your home directory on this machine.")
139 ;;; Internal variables whose values are stored in the info and todo files:
142 (defvar shadow-clusters nil
143 "List of host clusters \(see `shadow-define-cluster').")
145 (defvar shadow-literal-groups nil
146 "List of files that are shared between hosts.
147 This list contains shadow structures with literal filenames, created by
148 shadow-define-group.")
150 (defvar shadow-regexp-groups nil
151 "List of file types that are shared between hosts.
152 This list contains shadow structures with regexps matching filenames,
153 created by `shadow-define-regexp-group'.")
156 ;;; Other internal variables:
159 (defvar shadow-files-to-copy nil) ; List of files that need to
160 ; be copied to remote hosts.
162 (defvar shadow-hashtable nil) ; for speed
164 (defvar shadow-info-buffer nil) ; buf visiting shadow-info-file
165 (defvar shadow-todo-buffer nil) ; buf visiting shadow-todo-file
167 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
168 ;;; Syntactic sugar; General list and string manipulation
169 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
171 (defun shadow-union (a b)
172 "Add members of list A to list B if not equal to items already in B."
173 (if (null a)
175 (if (member (car a) b)
176 (shadow-union (cdr a) b)
177 (shadow-union (cdr a) (cons (car a) b)))))
179 (defun shadow-find (func list)
180 "If FUNC applied to some element of LIST is nonnil, return first such element."
181 (while (and list (not (funcall func (car list))))
182 (setq list (cdr list)))
183 (car list))
185 (defun shadow-remove-if (func list)
186 "Remove elements satisfying FUNC from LIST.
187 Nondestructive; actually returns a copy of the list with the elements removed."
188 (if list
189 (if (funcall func (car list))
190 (shadow-remove-if func (cdr list))
191 (cons (car list) (shadow-remove-if func (cdr list))))
192 nil))
194 (defun shadow-join (strings sep)
195 "Concatenate elements of the list of STRINGS with SEP between each."
196 (cond ((null strings) "")
197 ((null (cdr strings)) (car strings))
198 ((concat (car strings) " " (shadow-join (cdr strings) sep)))))
200 (defun shadow-regexp-superquote (string)
201 "Like `regexp-quote', but includes the ^ and $.
202 This makes sure regexp matches nothing but STRING."
203 (concat "^" (regexp-quote string) "$"))
205 (defun shadow-suffix (prefix string)
206 "If PREFIX begins STRING, return the rest.
207 Return value is nonnil if PREFIX and STRING are string= up to the length of
208 PREFIX."
209 (let ((lp (length prefix))
210 (ls (length string)))
211 (if (and (>= ls lp)
212 (string= prefix (substring string 0 lp)))
213 (substring string lp))))
215 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
216 ;;; Clusters and sites
217 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
219 ;;; I use the term `site' to refer to a string which may be the name of a
220 ;;; cluster or a literal hostname. All user-level commands should accept
221 ;;; either.
223 (defun shadow-make-cluster (name primary regexp)
224 "Create a shadow cluster.
225 It is called NAME, uses the PRIMARY hostname and REGEXP matching all
226 hosts in the cluster. The variable `shadow-clusters' associates the
227 names of clusters to these structures. This function is for program
228 use: to create clusters interactively, use `shadow-define-cluster'
229 instead."
230 (list name primary regexp))
232 (defmacro shadow-cluster-name (cluster)
233 "Return the name of the CLUSTER."
234 (list 'elt cluster 0))
236 (defmacro shadow-cluster-primary (cluster)
237 "Return the primary hostname of a CLUSTER."
238 (list 'elt cluster 1))
240 (defmacro shadow-cluster-regexp (cluster)
241 "Return the regexp matching hosts in a CLUSTER."
242 (list 'elt cluster 2))
244 (defun shadow-set-cluster (name primary regexp)
245 "Put cluster NAME on the list of clusters.
246 Replace old definition, if any. PRIMARY and REGEXP are the
247 information defining the cluster. For interactive use, call
248 `shadow-define-cluster' instead."
249 (let ((rest (shadow-remove-if
250 (function (lambda (x) (equal name (car x))))
251 shadow-clusters)))
252 (setq shadow-clusters
253 (cons (shadow-make-cluster name primary regexp)
254 rest))))
256 (defmacro shadow-get-cluster (name)
257 "Return cluster named NAME, or nil."
258 (list 'assoc name 'shadow-clusters))
260 (defun shadow-site-primary (site)
261 "If SITE is a cluster, return primary host, otherwise return SITE."
262 (let ((c (shadow-get-cluster site)))
263 (if c
264 (shadow-cluster-primary c)
265 site)))
267 ;;; SITES
269 (defun shadow-site-cluster (site)
270 "Given a SITE \(hostname or cluster name), return cluster it is in, or nil."
271 (or (assoc site shadow-clusters)
272 (shadow-find
273 (function (lambda (x)
274 (string-match (shadow-cluster-regexp x)
275 site)))
276 shadow-clusters)))
278 (defun shadow-read-site ()
279 "Read a cluster name or hostname from the minibuffer."
280 (let ((ans (completing-read "Host or cluster name [RET when done]: "
281 shadow-clusters)))
282 (if (equal "" ans)
284 ans)))
286 (defun shadow-site-match (site1 site2)
287 "Nonnil iff SITE1 is or includes SITE2.
288 Each may be a host or cluster name; if they are clusters, regexp of site1 will
289 be matched against the primary of site2."
290 (or (string-equal site1 site2) ; quick check
291 (let* ((cluster1 (shadow-get-cluster site1))
292 (primary2 (shadow-site-primary site2)))
293 (if cluster1
294 (string-match (shadow-cluster-regexp cluster1) primary2)
295 (string-equal site1 primary2)))))
297 (defun shadow-get-user (site)
298 "Return the default username for a SITE."
299 (ange-ftp-get-user (shadow-site-primary site)))
301 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
302 ;;; Filename manipulation
303 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
305 (defun shadow-parse-fullpath (fullpath)
306 "Parse FULLPATH into \(site user path) list.
307 Leave it alone if it already is one. Returns nil if the argument is
308 not a full ange-ftp pathname."
309 (if (listp fullpath)
310 fullpath
311 (ange-ftp-ftp-name fullpath)))
313 (defun shadow-parse-path (path)
314 "Parse any PATH into \(site user path) list.
315 Argument can be a simple path, full ange-ftp path, or already a hup list."
316 (or (shadow-parse-fullpath path)
317 (list shadow-system-name
318 (user-login-name)
319 path)))
321 (defsubst shadow-make-fullpath (host user path)
322 "Make an ange-ftp style fullpath out of HOST, USER (optional), and PATH.
323 This is probably not as general as it ought to be."
324 (concat "/"
325 (if user (concat user "@"))
326 host ":"
327 path))
329 (defun shadow-replace-path-component (fullpath newpath)
330 "Return FULLPATH with the pathname component changed to NEWPATH."
331 (let ((hup (shadow-parse-fullpath fullpath)))
332 (shadow-make-fullpath (nth 0 hup) (nth 1 hup) newpath)))
334 (defun shadow-local-file (file)
335 "If FILE is at this site, remove /user@host part.
336 If refers to a different system or a different user on this system,
337 return nil."
338 (let ((hup (shadow-parse-fullpath file)))
339 (cond ((null hup) file)
340 ((and (shadow-site-match (nth 0 hup) shadow-system-name)
341 (string-equal (nth 1 hup) (user-login-name)))
342 (nth 2 hup))
343 (t nil))))
345 (defun shadow-expand-cluster-in-file-name (file)
346 "If hostname part of FILE is a cluster, expand it to cluster's primary hostname.
347 Will return the pathname bare if it is a local file."
348 (let ((hup (shadow-parse-path file))
349 cluster)
350 (cond ((null hup) file)
351 ((shadow-local-file hup))
352 ((shadow-make-fullpath (shadow-site-primary (nth 0 hup))
353 (nth 1 hup)
354 (nth 2 hup))))))
356 (defun shadow-expand-file-name (file &optional default)
357 "Expand file name and get file's true name."
358 (file-truename (expand-file-name file default)))
360 (defun shadow-contract-file-name (file)
361 "Simplify FILE.
362 Do so by replacing (when possible) home directory with ~, and hostname
363 with cluster name that includes it. Filename should be absolute and
364 true."
365 (let* ((hup (shadow-parse-path file))
366 (homedir (if (shadow-local-file hup)
367 shadow-homedir
368 (file-name-as-directory
369 (nth 2 (shadow-parse-fullpath
370 (expand-file-name
371 (shadow-make-fullpath
372 (nth 0 hup) (nth 1 hup) "~")))))))
373 (suffix (shadow-suffix homedir (nth 2 hup)))
374 (cluster (shadow-site-cluster (nth 0 hup))))
375 (shadow-make-fullpath
376 (if cluster
377 (shadow-cluster-name cluster)
378 (nth 0 hup))
379 (nth 1 hup)
380 (if suffix
381 (concat "~/" suffix)
382 (nth 2 hup)))))
384 (defun shadow-same-site (pattern file)
385 "True if the site of PATTERN and of FILE are on the same site.
386 If usernames are supplied, they must also match exactly. PATTERN and FILE may
387 be lists of host, user, path, or ange-ftp pathnames. FILE may also be just a
388 local filename."
389 (let ((pattern-sup (shadow-parse-fullpath pattern))
390 (file-sup (shadow-parse-path file)))
391 (and
392 (shadow-site-match (nth 0 pattern-sup) (nth 0 file-sup))
393 (or (null (nth 1 pattern-sup))
394 (string-equal (nth 1 pattern-sup) (nth 1 file-sup))))))
396 (defun shadow-file-match (pattern file &optional regexp)
397 "Return t if PATTERN matches FILE.
398 If REGEXP is supplied and nonnil, the pathname part of the pattern is a regular
399 expression, otherwise it must match exactly. The sites and usernames must
400 match---see shadow-same-site. The pattern must be in full ange-ftp format, but
401 the file can be any valid filename. This function does not do any filename
402 expansion or contraction, you must do that yourself first."
403 (let* ((pattern-sup (shadow-parse-fullpath pattern))
404 (file-sup (shadow-parse-path file)))
405 (and (shadow-same-site pattern-sup file-sup)
406 (if regexp
407 (string-match (nth 2 pattern-sup) (nth 2 file-sup))
408 (string-equal (nth 2 pattern-sup) (nth 2 file-sup))))))
410 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
411 ;;; User-level Commands
412 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
414 ;;;###autoload
415 (defun shadow-define-cluster (name)
416 "Edit \(or create) the definition of a cluster NAME.
417 This is a group of hosts that share directories, so that copying to or from
418 one of them is sufficient to update the file on all of them. Clusters are
419 defined by a name, the network address of a primary host \(the one we copy
420 files to), and a regular expression that matches the hostnames of all the sites
421 in the cluster."
422 (interactive (list (completing-read "Cluster name: " shadow-clusters () ())))
423 (let* ((old (shadow-get-cluster name))
424 (primary (read-string "Primary host: "
425 (if old (shadow-cluster-primary old)
426 name)))
427 (regexp (let (try-regexp)
428 (while (not
429 (string-match
430 (setq try-regexp
431 (read-string
432 "Regexp matching all host names: "
433 (if old (shadow-cluster-regexp old)
434 (shadow-regexp-superquote primary))))
435 primary))
436 (message "Regexp doesn't include the primary host!")
437 (sit-for 2))
438 try-regexp))
439 ; (username (read-no-blanks-input
440 ; (format "Username [default: %s]: "
441 ; (shadow-get-user primary))
442 ; (if old (or (shadow-cluster-username old) "")
443 ; (user-login-name))))
445 ; (if (string-equal "" username) (setq username nil))
446 (shadow-set-cluster name primary regexp)))
448 ;;;###autoload
449 (defun shadow-define-literal-group ()
450 "Declare a single file to be shared between sites.
451 It may have different filenames on each site. When this file is edited, the
452 new version will be copied to each of the other locations. Sites can be
453 specific hostnames, or names of clusters \(see `shadow-define-cluster')."
454 (interactive)
455 (let* ((hup (shadow-parse-fullpath
456 (shadow-contract-file-name (buffer-file-name))))
457 (path (nth 2 hup))
458 user site group)
459 (while (setq site (shadow-read-site))
460 (setq user (read-string (format "Username [default %s]: "
461 (shadow-get-user site)))
462 path (read-string "Filename: " path))
463 (setq group (cons (shadow-make-fullpath site
464 (if (string-equal "" user)
465 (shadow-get-user site)
466 user)
467 path)
468 group)))
469 (setq shadow-literal-groups (cons group shadow-literal-groups)))
470 (shadow-write-info-file))
472 ;;;###autoload
473 (defun shadow-define-regexp-group ()
474 "Make each of a group of files be shared between hosts.
475 Prompts for regular expression; files matching this are shared between a list
476 of sites, which are also prompted for. The filenames must be identical on all
477 hosts \(if they aren't, use shadow-define-group instead of this function).
478 Each site can be either a hostname or the name of a cluster \(see
479 `shadow-define-cluster')."
480 (interactive)
481 (let ((regexp (read-string
482 "Filename regexp: "
483 (if (buffer-file-name)
484 (shadow-regexp-superquote
485 (nth 2
486 (shadow-parse-path
487 (shadow-contract-file-name
488 (buffer-file-name))))))))
489 site sites usernames)
490 (while (setq site (shadow-read-site))
491 (setq sites (cons site sites))
492 (setq usernames
493 (cons (read-string (format "Username for %s: " site)
494 (shadow-get-user site))
495 usernames)))
496 (setq shadow-regexp-groups
497 (cons (shadow-make-group regexp sites usernames)
498 shadow-regexp-groups))
499 (shadow-write-info-file)))
501 (defun shadow-shadows ()
502 ;; Mostly for debugging.
503 "Interactive function to display shadows of a buffer."
504 (interactive)
505 (let ((msg (shadow-join (mapcar (function cdr)
506 (shadow-shadows-of (buffer-file-name)))
507 " ")))
508 (message "%s"
509 (if (zerop (length msg))
510 "No shadows."
511 msg))))
513 (defun shadow-copy-files (&optional arg)
514 "Copy all pending shadow files.
515 With prefix argument, copy all pending files without query.
516 Pending copies are stored in variable `shadow-files-to-copy', and in
517 `shadow-todo-file' if necessary. This function is invoked by
518 `shadow-save-buffers-kill-emacs', so it is not usually necessary to
519 call it manually."
520 (interactive "P")
521 (if (and (not shadow-files-to-copy) (interactive-p))
522 (message "No files need to be shadowed.")
523 (save-excursion
524 (map-y-or-n-p (function
525 (lambda (pair)
526 (or arg shadow-noquery
527 (format "Copy shadow file %s? " (cdr pair)))))
528 (function shadow-copy-file)
529 shadow-files-to-copy
530 '("shadow" "shadows" "copy"))
531 (shadow-write-todo-file t))))
533 (defun shadow-cancel ()
534 "Cancel the instruction to copy some files.
535 Prompts for which copy operations to cancel. You will not be asked to copy
536 them again, unless you make more changes to the files. To cancel a shadow
537 permanently, remove the group from `shadow-literal-groups' or
538 `shadow-regexp-groups'."
539 (interactive)
540 (map-y-or-n-p (function (lambda (pair)
541 (format "Cancel copying %s to %s? "
542 (car pair) (cdr pair))))
543 (function (lambda (pair)
544 (shadow-remove-from-todo pair)))
545 shadow-files-to-copy
546 '("shadow" "shadows" "cancel copy"))
547 (message "There are %d shadows to be updated."
548 (length shadow-files-to-copy))
549 (shadow-write-todo-file))
551 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
552 ;;; Internal functions
553 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
555 (defun shadow-make-group (regexp sites usernames)
556 "Make a description of a file group---
557 actually a list of regexp ange-ftp file names---from REGEXP \(name of file to
558 be shadowed), list of SITES, and corresponding list of USERNAMES for each
559 site."
560 (if sites
561 (cons (shadow-make-fullpath (car sites) (car usernames) regexp)
562 (shadow-make-group regexp (cdr sites) (cdr usernames)))
563 nil))
565 (defun shadow-copy-file (s)
566 "Copy one shadow file."
567 (let* ((buffer
568 (cond ((get-file-buffer
569 (abbreviate-file-name (shadow-expand-file-name (car s)))))
570 ((not (file-readable-p (car s)))
571 (if (y-or-n-p
572 (format "Cannot find file %s--cancel copy request? "
573 (car s)))
574 (shadow-remove-from-todo s))
575 nil)
576 ((or (eq t shadow-noquery)
577 (y-or-n-p
578 (format "No buffer for %s -- update shadow anyway? "
579 (car s))))
580 (find-file-noselect (car s)))))
581 (to (shadow-expand-cluster-in-file-name (cdr s))))
582 (when buffer
583 (set-buffer buffer)
584 (save-restriction
585 (widen)
586 (condition-case i
587 (progn
588 (write-region (point-min) (point-max) to)
589 (shadow-remove-from-todo s))
590 (error (message "Shadow %s not updated!" (cdr s))))))))
592 (defun shadow-shadows-of (file)
593 "Return copy operations needed to update FILE.
594 Filename should have clusters expanded, but otherwise can have any format.
595 Return value is a list of dotted pairs like \(from . to), where from
596 and to are absolute file names."
597 (or (symbol-value (intern-soft file shadow-hashtable))
598 (let* ((absolute-file (shadow-expand-file-name
599 (or (shadow-local-file file) file)
600 shadow-homedir))
601 (canonical-file (shadow-contract-file-name absolute-file))
602 (shadows
603 (mapcar (function (lambda (shadow)
604 (cons absolute-file shadow)))
605 (append
606 (shadow-shadows-of-1
607 canonical-file shadow-literal-groups nil)
608 (shadow-shadows-of-1
609 canonical-file shadow-regexp-groups t)))))
610 (set (intern file shadow-hashtable) shadows))))
612 (defun shadow-shadows-of-1 (file groups regexp)
613 "Return list of FILE's shadows in GROUPS.
614 Consider them as regular expressions if third arg REGEXP is true."
615 (if groups
616 (let ((nonmatching
617 (shadow-remove-if
618 (function (lambda (x) (shadow-file-match x file regexp)))
619 (car groups))))
620 (append (cond ((equal nonmatching (car groups)) nil)
621 (regexp
622 (let ((realpath (nth 2 (shadow-parse-fullpath file))))
623 (mapcar
624 (function
625 (lambda (x)
626 (shadow-replace-path-component x realpath)))
627 nonmatching)))
628 (t nonmatching))
629 (shadow-shadows-of-1 file (cdr groups) regexp)))))
631 (defun shadow-add-to-todo ()
632 "If current buffer has shadows, add them to the list needing to be copied."
633 (let ((shadows (shadow-shadows-of
634 (shadow-expand-file-name
635 (buffer-file-name (current-buffer))))))
636 (when shadows
637 (setq shadow-files-to-copy
638 (shadow-union shadows shadow-files-to-copy))
639 (when (not shadow-inhibit-message)
640 (message "%s" (substitute-command-keys
641 "Use \\[shadow-copy-files] to update shadows."))
642 (sit-for 1))
643 (shadow-write-todo-file)))
644 nil) ; Return nil for write-file-hooks
646 (defun shadow-remove-from-todo (pair)
647 "Remove PAIR from `shadow-files-to-copy'.
648 PAIR must be (eq to) one of the elements of that list."
649 (setq shadow-files-to-copy
650 (shadow-remove-if (function (lambda (s) (eq s pair)))
651 shadow-files-to-copy)))
653 (defun shadow-read-files ()
654 "Visit and load `shadow-info-file' and `shadow-todo-file'.
655 Thus restores shadowfile's state from your last Emacs session.
656 Returns t unless files were locked; then returns nil."
657 (interactive)
658 (if (and (fboundp 'file-locked-p)
659 (or (stringp (file-locked-p shadow-info-file))
660 (stringp (file-locked-p shadow-todo-file))))
661 (progn
662 (message "Shadowfile is running in another emacs; can't have two.")
663 (beep)
664 (sit-for 3)
665 nil)
666 (save-excursion
667 (when shadow-info-file
668 (set-buffer (setq shadow-info-buffer
669 (find-file-noselect shadow-info-file)))
670 (when (and (not (buffer-modified-p))
671 (file-newer-than-file-p (make-auto-save-file-name)
672 shadow-info-file))
673 (erase-buffer)
674 (message "Data recovered from %s."
675 (car (insert-file-contents (make-auto-save-file-name))))
676 (sit-for 1))
677 (eval-current-buffer))
678 (when shadow-todo-file
679 (set-buffer (setq shadow-todo-buffer
680 (find-file-noselect shadow-todo-file)))
681 (when (and (not (buffer-modified-p))
682 (file-newer-than-file-p (make-auto-save-file-name)
683 shadow-todo-file))
684 (erase-buffer)
685 (message "Data recovered from %s."
686 (car (insert-file-contents (make-auto-save-file-name))))
687 (sit-for 1))
688 (eval-current-buffer nil))
689 (shadow-invalidate-hashtable))
692 (defun shadow-write-info-file ()
693 "Write out information to `shadow-info-file'.
694 Also clear `shadow-hashtable', since when there are new shadows
695 defined, the old hashtable info is invalid."
696 (shadow-invalidate-hashtable)
697 (if shadow-info-file
698 (save-excursion
699 (if (not shadow-info-buffer)
700 (setq shadow-info-buffer (find-file-noselect shadow-info-file)))
701 (set-buffer shadow-info-buffer)
702 (delete-region (point-min) (point-max))
703 (shadow-insert-var 'shadow-clusters)
704 (shadow-insert-var 'shadow-literal-groups)
705 (shadow-insert-var 'shadow-regexp-groups))))
707 (defun shadow-write-todo-file (&optional save)
708 "Write out information to shadow-todo-file.
709 With nonnil argument also saves the buffer."
710 (save-excursion
711 (if (not shadow-todo-buffer)
712 (setq shadow-todo-buffer (find-file-noselect shadow-todo-file)))
713 (set-buffer shadow-todo-buffer)
714 (delete-region (point-min) (point-max))
715 (shadow-insert-var 'shadow-files-to-copy)
716 (if save (shadow-save-todo-file))))
718 (defun shadow-save-todo-file ()
719 (if (and shadow-todo-buffer (buffer-modified-p shadow-todo-buffer))
720 (save-excursion
721 (set-buffer shadow-todo-buffer)
722 (condition-case nil ; have to continue even in case of
723 (basic-save-buffer) ; error, otherwise kill-emacs might
724 (error ; not work!
725 (message "WARNING: Can't save shadow todo file; it is locked!")
726 (sit-for 1))))))
728 (defun shadow-invalidate-hashtable ()
729 (setq shadow-hashtable (make-vector 37 0)))
731 (defun shadow-insert-var (variable)
732 "Prettily insert a setq command for VARIABLE.
733 which, when later evaluated, will restore it to its current setting.
734 SYMBOL must be the name of a variable whose value is a list."
735 (let ((standard-output (current-buffer)))
736 (insert (format "(setq %s" variable))
737 (cond ((consp (eval variable))
738 (insert "\n '(")
739 (prin1 (car (eval variable)))
740 (let ((rest (cdr (eval variable))))
741 (while rest
742 (insert "\n ")
743 (prin1 (car rest))
744 (setq rest (cdr rest)))
745 (insert "))\n\n")))
746 (t (insert " ")
747 (prin1 (eval variable))
748 (insert ")\n\n")))))
750 (defun shadow-save-buffers-kill-emacs (&optional arg)
751 "Offer to save each buffer and copy shadows, then kill this Emacs process.
752 With prefix arg, silently save all file-visiting buffers, then kill.
754 Extended by shadowfile to automatically save `shadow-todo-file' and
755 look for files that have been changed and need to be copied to other systems."
756 ;; This function is necessary because we need to get control and save
757 ;; the todo file /after/ saving other files, but /before/ the warning
758 ;; message about unsaved buffers (because it can get modified by the
759 ;; action of saving other buffers). `kill-emacs-hook' is no good
760 ;; because it is not called at the correct time, and also because it is
761 ;; called when the terminal is disconnected and we cannot ask whether
762 ;; to copy files.
763 (interactive "P")
764 (shadow-save-todo-file)
765 (save-some-buffers arg t)
766 (shadow-copy-files)
767 (shadow-save-todo-file)
768 (and (or (not (memq t (mapcar (function
769 (lambda (buf) (and (buffer-file-name buf)
770 (buffer-modified-p buf))))
771 (buffer-list))))
772 (yes-or-no-p "Modified buffers exist; exit anyway? "))
773 (or (not (fboundp 'process-list))
774 ;; process-list is not defined on VMS.
775 (let ((processes (process-list))
776 active)
777 (while processes
778 (and (memq (process-status (car processes)) '(run stop open listen))
779 (process-query-on-exit-flag (car processes))
780 (setq active t))
781 (setq processes (cdr processes)))
782 (or (not active)
783 (yes-or-no-p "Active processes exist; kill them and exit anyway? "))))
784 (kill-emacs)))
786 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
787 ;;; Lucid Emacs compatibility
788 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
790 ;; This is on hold until someone tells me about a working version of
791 ;; map-ynp for Lucid Emacs.
793 ;(when (string-match "Lucid" emacs-version)
794 ; (require 'symlink-fix)
795 ; (require 'ange-ftp)
796 ; (require 'map-ynp)
797 ; (if (not (fboundp 'file-truename))
798 ; (fset 'shadow-expand-file-name
799 ; (symbol-function 'symlink-expand-file-name)))
800 ; (if (not (fboundp 'ange-ftp-ftp-name))
801 ; (fset 'ange-ftp-ftp-name
802 ; (symbol-function 'ange-ftp-ftp-path))))
804 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
805 ;;; Hook us up
806 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
808 ;;;###autoload
809 (defun shadow-initialize ()
810 "Set up file shadowing."
811 (interactive)
812 (if (null shadow-homedir)
813 (setq shadow-homedir
814 (file-name-as-directory (shadow-expand-file-name "~"))))
815 (if (null shadow-info-file)
816 (setq shadow-info-file
817 (shadow-expand-file-name "~/.shadows")))
818 (if (null shadow-todo-file)
819 (setq shadow-todo-file
820 (shadow-expand-file-name "~/.shadow_todo")))
821 (if (not (shadow-read-files))
822 (progn
823 (message "Shadowfile information files not found - aborting")
824 (beep)
825 (sit-for 3))
826 (when (and (not shadow-inhibit-overload)
827 (not (fboundp 'shadow-orig-save-buffers-kill-emacs)))
828 (defalias 'shadow-orig-save-buffers-kill-emacs
829 (symbol-function 'save-buffers-kill-emacs))
830 (defalias 'save-buffers-kill-emacs 'shadow-save-buffers-kill-emacs))
831 (add-hook 'write-file-hooks 'shadow-add-to-todo)
832 (define-key ctl-x-4-map "s" 'shadow-copy-files)))
834 (defun shadowfile-unload-hook ()
835 (if (fboundp 'shadow-orig-save-buffers-kill-emacs)
836 (fset 'save-buffers-kill-emacs
837 (symbol-function 'shadow-orig-save-buffers-kill-emacs)))
838 (remove-hook 'write-file-hooks 'shadow-add-to-todo))
840 (provide 'shadowfile)
842 ;;; shadowfile.el ends here