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