*** empty log message ***
[emacs.git] / lisp / shadowfile.el
blob1cc343672c8262deeb7de87c17c000235e900e03
1 ;;; shadowfile.el --- automatic file copying for Emacs 19
3 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
5 ;; Author: Boris Goldowsky <boris@gnu.org>
6 ;; Keywords: comm
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 ;; Put (require 'shadowfile) in your .emacs; add clusters (if necessary)
35 ;; and file groups with shadow-define-cluster,
36 ;; shadow-define-literal-group, and shadow-define-regexp-group (see the
37 ;; documentation for these functions for information on how and when to
38 ;; use them). After doing this once, everything should be automatic.
40 ;; The lists of clusters and shadows are saved in a file called .shadows,
41 ;; so that they can be remembered from one emacs session to another, even
42 ;; (as much as possible) if the emacs session terminates abnormally. The
43 ;; files needing to be copied are stored in .shadow_todo; if a file cannot
44 ;; be copied for any reason, it will stay on the list to be tried again
45 ;; next time. The .shadows file should itself have shadows on all your
46 ;; accounts so that the information in it is consistent everywhere, but
47 ;; .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" and
50 ;; 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
52 ;; until you change the file and save it again. If you do not want to
53 ;; shadow that file ever again, you can edit it out of the .shadows
54 ;; buffer. Anytime you edit the .shadows buffer, you must type M-x
55 ;; shadow-read-files to load in the new information, or your changes will
56 ;; be overwritten!
58 ;; Bugs & Warnings:
60 ;; - It is bad to have two emacses both running shadowfile at the same
61 ;; time. It tries to detect this condition, but is not always successful.
63 ;; - You have to be careful not to edit a file in two locations
64 ;; before shadowfile has had a chance to copy it; otherwise
65 ;; "updating shadows" will overwrite one of the changed versions.
67 ;; - It ought to check modification times of both files to make sure
68 ;; it is doing the right thing. This will have to wait until
69 ;; file-newer-than-file-p works between machines.
71 ;; - It will not make directories for you, it just fails to copy files
72 ;; that belong in non-existent directories.
74 ;; Please report any bugs to me (boris@gnu.org). Also let me know
75 ;; if you have suggestions or would like to be informed of updates.
77 ;;; Code:
79 (provide 'shadowfile)
80 (require 'ange-ftp)
82 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
83 ;;; Variables
84 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
86 (defgroup shadow nil
87 "Automatic file copying when saving a file."
88 :prefix "shadow-"
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 C-x C-c.
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
173 if they are not equal to items already in B."
174 (if (null a)
176 (if (member (car a) b)
177 (shadow-union (cdr a) b)
178 (shadow-union (cdr a) (cons (car a) b)))))
180 (defun shadow-find (func list)
181 "If FUNC applied to some element of LIST is nonnil,
182 return the first such element."
183 (while (and list (not (funcall func (car list))))
184 (setq list (cdr list)))
185 (car list))
187 (defun shadow-remove-if (func list)
188 "Remove elements satisfying FUNC from LIST.
189 Nondestructive; actually returns a copy of the list with the elements removed."
190 (if list
191 (if (funcall func (car list))
192 (shadow-remove-if func (cdr list))
193 (cons (car list) (shadow-remove-if func (cdr list))))
194 nil))
196 (defun shadow-join (strings sep)
197 "Concatenate elements of the list of STRINGS with SEP between each."
198 (cond ((null strings) "")
199 ((null (cdr strings)) (car strings))
200 ((concat (car strings) " " (shadow-join (cdr strings) sep)))))
202 (defun shadow-regexp-superquote (string)
203 "Like regexp-quote, but includes the ^ and $
204 to make sure regexp matches nothing but STRING."
205 (concat "^" (regexp-quote string) "$"))
207 (defun shadow-suffix (prefix string)
208 "If PREFIX begins STRING, return the rest.
209 Return value is nonnil if PREFIX and STRING are string= up to the length of
210 PREFIX."
211 (let ((lp (length prefix))
212 (ls (length string)))
213 (if (and (>= ls lp)
214 (string= prefix (substring string 0 lp)))
215 (substring string lp))))
217 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
218 ;;; Clusters and sites
219 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
221 ;;; I use the term `site' to refer to a string which may be the name of a
222 ;;; cluster or a literal hostname. All user-level commands should accept
223 ;;; either.
225 (defun shadow-make-cluster (name primary regexp)
226 "Creates a shadow cluster
227 called NAME, using the PRIMARY hostname, REGEXP matching all hosts in the
228 cluster. The variable shadow-clusters associates the names of clusters to
229 these structures.
230 This function is for program use: to create clusters interactively, use
231 shadow-define-cluster instead."
232 (list name primary regexp))
234 (defmacro shadow-cluster-name (cluster)
235 "Return the name of the CLUSTER."
236 (list 'elt cluster 0))
238 (defmacro shadow-cluster-primary (cluster)
239 "Return the primary hostname of a CLUSTER."
240 (list 'elt cluster 1))
242 (defmacro shadow-cluster-regexp (cluster)
243 "Return the regexp matching hosts in a CLUSTER."
244 (list 'elt cluster 2))
246 (defun shadow-set-cluster (name primary regexp)
247 "Put cluster NAME on the list of clusters,
248 replacing old definition if any. PRIMARY and REGEXP are the
249 information defining the cluster. For interactive use, call
250 shadow-define-cluster instead."
251 (let ((rest (shadow-remove-if
252 (function (lambda (x) (equal name (car x))))
253 shadow-clusters)))
254 (setq shadow-clusters
255 (cons (shadow-make-cluster name primary regexp)
256 rest))))
258 (defmacro shadow-get-cluster (name)
259 "Return cluster named NAME, or nil."
260 (list 'assoc name 'shadow-clusters))
262 (defun shadow-site-primary (site)
263 "If SITE is a cluster, return primary host, otherwise return SITE."
264 (let ((c (shadow-get-cluster site)))
265 (if c
266 (shadow-cluster-primary c)
267 site)))
269 ;;; SITES
271 (defun shadow-site-cluster (site)
272 "Given a SITE \(hostname or cluster name), return the cluster
273 that it is in, or nil."
274 (or (assoc site shadow-clusters)
275 (shadow-find
276 (function (lambda (x)
277 (string-match (shadow-cluster-regexp x)
278 site)))
279 shadow-clusters)))
281 (defun shadow-read-site ()
282 "Read a cluster name or hostname from the minibuffer."
283 (let ((ans (completing-read "Host or cluster name [RET when done]: "
284 shadow-clusters)))
285 (if (equal "" ans)
287 ans)))
289 (defun shadow-site-match (site1 site2)
290 "Nonnil iff SITE1 is or includes SITE2.
291 Each may be a host or cluster name; if they are clusters, regexp of site1 will
292 be matched against the primary of site2."
293 (or (string-equal site1 site2) ; quick check
294 (let* ((cluster1 (shadow-get-cluster site1))
295 (primary2 (shadow-site-primary site2)))
296 (if cluster1
297 (string-match (shadow-cluster-regexp cluster1) primary2)
298 (string-equal site1 primary2)))))
300 (defun shadow-get-user (site)
301 "Returns the default username for a site."
302 (ange-ftp-get-user (shadow-site-primary site)))
304 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
305 ;;; Filename manipulation
306 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
308 (defun shadow-parse-fullpath (fullpath)
309 "Parse PATH into \(site user path) list,
310 or leave it alone if it already is one. Returns nil if the argument is not a
311 full ange-ftp pathname."
312 (if (listp fullpath)
313 fullpath
314 (ange-ftp-ftp-name fullpath)))
316 (defun shadow-parse-path (path)
317 "Parse any PATH into \(site user path) list.
318 Argument can be a simple path, full ange-ftp path, or already a hup list."
319 (or (shadow-parse-fullpath path)
320 (list shadow-system-name
321 (user-login-name)
322 path)))
324 (defsubst shadow-make-fullpath (host user path)
325 "Make an ange-ftp style fullpath out of HOST, USER (optional), and PATH.
326 This is probably not as general as it ought to be."
327 (concat "/"
328 (if user (concat user "@"))
329 host ":"
330 path))
332 (defun shadow-replace-path-component (fullpath newpath)
333 "Return FULLPATH with the pathname component changed to NEWPATH."
334 (let ((hup (shadow-parse-fullpath fullpath)))
335 (shadow-make-fullpath (nth 0 hup) (nth 1 hup) newpath)))
337 (defun shadow-local-file (file)
338 "If FILENAME is at this site,
339 remove /user@host part. If refers to a different system or a different user on
340 this system, return nil."
341 (let ((hup (shadow-parse-fullpath file)))
342 (cond ((null hup) file)
343 ((and (shadow-site-match (nth 0 hup) shadow-system-name)
344 (string-equal (nth 1 hup) (user-login-name)))
345 (nth 2 hup))
346 (t nil))))
348 (defun shadow-expand-cluster-in-file-name (file)
349 "If hostname part of FILE is a cluster, expand it
350 into the cluster's primary hostname. Will return the pathname bare if it is
351 a local file."
352 (let ((hup (shadow-parse-path file))
353 cluster)
354 (cond ((null hup) file)
355 ((shadow-local-file hup))
356 ((shadow-make-fullpath (shadow-site-primary (nth 0 hup))
357 (nth 1 hup)
358 (nth 2 hup))))))
360 (defun shadow-expand-file-name (file &optional default)
361 "Expand file name and get file's true name."
362 (file-truename (expand-file-name file default)))
364 (defun shadow-contract-file-name (file)
365 "Simplify FILENAME
366 by replacing (when possible) home directory with ~, and hostname with cluster
367 name that includes it. Filename should be absolute and true."
368 (let* ((hup (shadow-parse-path file))
369 (homedir (if (shadow-local-file hup)
370 shadow-homedir
371 (file-name-as-directory
372 (nth 2 (shadow-parse-fullpath
373 (expand-file-name
374 (shadow-make-fullpath
375 (nth 0 hup) (nth 1 hup) "~")))))))
376 (suffix (shadow-suffix homedir (nth 2 hup)))
377 (cluster (shadow-site-cluster (nth 0 hup))))
378 (shadow-make-fullpath
379 (if cluster
380 (shadow-cluster-name cluster)
381 (nth 0 hup))
382 (nth 1 hup)
383 (if suffix
384 (concat "~/" suffix)
385 (nth 2 hup)))))
387 (defun shadow-same-site (pattern file)
388 "True if the site of PATTERN and of FILE are on the same site.
389 If usernames are supplied, they must also match exactly. PATTERN and FILE may
390 be lists of host, user, path, or ange-ftp pathnames. FILE may also be just a
391 local filename."
392 (let ((pattern-sup (shadow-parse-fullpath pattern))
393 (file-sup (shadow-parse-path file)))
394 (and
395 (shadow-site-match (nth 0 pattern-sup) (nth 0 file-sup))
396 (or (null (nth 1 pattern-sup))
397 (string-equal (nth 1 pattern-sup) (nth 1 file-sup))))))
399 (defun shadow-file-match (pattern file &optional regexp)
400 "Returns t if PATTERN matches FILE.
401 If REGEXP is supplied and nonnil, the pathname part of the pattern is a regular
402 expression, otherwise it must match exactly. The sites and usernames must
403 match---see shadow-same-site. The pattern must be in full ange-ftp format, but
404 the file can be any valid filename. This function does not do any filename
405 expansion or contraction, you must do that yourself first."
406 (let* ((pattern-sup (shadow-parse-fullpath pattern))
407 (file-sup (shadow-parse-path file)))
408 (and (shadow-same-site pattern-sup file-sup)
409 (if regexp
410 (string-match (nth 2 pattern-sup) (nth 2 file-sup))
411 (string-equal (nth 2 pattern-sup) (nth 2 file-sup))))))
413 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
414 ;;; User-level Commands
415 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
417 (defun shadow-define-cluster (name)
418 "Edit \(or create) the definition of a cluster.
419 This is a group of hosts that share directories, so that copying to or from
420 one of them is sufficient to update the file on all of them. Clusters are
421 defined by a name, the network address of a primary host \(the one we copy
422 files to), and a regular expression that matches the hostnames of all the sites
423 in the cluster."
424 (interactive (list (completing-read "Cluster name: " shadow-clusters () ())))
425 (let* ((old (shadow-get-cluster name))
426 (primary (read-string "Primary host: "
427 (if old (shadow-cluster-primary old)
428 name)))
429 (regexp (let (try-regexp)
430 (while (not
431 (string-match
432 (setq try-regexp
433 (read-string
434 "Regexp matching all host names: "
435 (if old (shadow-cluster-regexp old)
436 (shadow-regexp-superquote primary))))
437 primary))
438 (message "Regexp doesn't include the primary host!")
439 (sit-for 2))
440 try-regexp))
441 ; (username (read-no-blanks-input
442 ; (format "Username [default: %s]: "
443 ; (shadow-get-user primary))
444 ; (if old (or (shadow-cluster-username old) "")
445 ; (user-login-name))))
447 ; (if (string-equal "" username) (setq username nil))
448 (shadow-set-cluster name primary regexp)))
450 (defun shadow-define-literal-group ()
451 "Declare a single file to be shared between sites.
452 It may have different filenames on each site. When this file is edited, the
453 new version will be copied to each of the other locations. Sites can be
454 specific hostnames, or names of clusters \(see shadow-define-cluster)."
455 (interactive)
456 (let* ((hup (shadow-parse-fullpath
457 (shadow-contract-file-name (buffer-file-name))))
458 (path (nth 2 hup))
459 user site group)
460 (while (setq site (shadow-read-site))
461 (setq user (read-string (format "Username [default %s]: "
462 (shadow-get-user site)))
463 path (read-string "Filename: " path))
464 (setq group (cons (shadow-make-fullpath site
465 (if (string-equal "" user)
466 (shadow-get-user site)
467 user)
468 path)
469 group)))
470 (setq shadow-literal-groups (cons group shadow-literal-groups)))
471 (shadow-write-info-file))
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 "Makes 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 "Returns 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 which are considered 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
633 of files needing to be copied."
634 (let ((shadows (shadow-shadows-of
635 (shadow-expand-file-name
636 (buffer-file-name (current-buffer))))))
637 (when shadows
638 (setq shadow-files-to-copy
639 (shadow-union shadows shadow-files-to-copy))
640 (when (not shadow-inhibit-message)
641 (message "%s" (substitute-command-keys
642 "Use \\[shadow-copy-files] to update shadows."))
643 (sit-for 1))
644 (shadow-write-todo-file)))
645 nil) ; Return nil for write-file-hooks
647 (defun shadow-remove-from-todo (pair)
648 "Remove PAIR from shadow-files-to-copy.
649 PAIR must be (eq to) one of the elements of that list."
650 (setq shadow-files-to-copy
651 (shadow-remove-if (function (lambda (s) (eq s pair)))
652 shadow-files-to-copy)))
654 (defun shadow-read-files ()
655 "Visits and loads shadow-info-file and shadow-todo-file,
656 thus restoring shadowfile's state from your last emacs session.
657 Returns t unless files were locked; then returns nil."
658 (interactive)
659 (if (and (fboundp 'file-locked-p)
660 (or (stringp (file-locked-p shadow-info-file))
661 (stringp (file-locked-p shadow-todo-file))))
662 (progn
663 (message "Shadowfile is running in another emacs; can't have two.")
664 (beep)
665 (sit-for 3)
666 nil)
667 (save-excursion
668 (when shadow-info-file
669 (set-buffer (setq shadow-info-buffer
670 (find-file-noselect shadow-info-file)))
671 (when (and (not (buffer-modified-p))
672 (file-newer-than-file-p (make-auto-save-file-name)
673 shadow-info-file))
674 (erase-buffer)
675 (message "Data recovered from %s."
676 (car (insert-file-contents (make-auto-save-file-name))))
677 (sit-for 1))
678 (eval-current-buffer))
679 (when shadow-todo-file
680 (set-buffer (setq shadow-todo-buffer
681 (find-file-noselect shadow-todo-file)))
682 (when (and (not (buffer-modified-p))
683 (file-newer-than-file-p (make-auto-save-file-name)
684 shadow-todo-file))
685 (erase-buffer)
686 (message "Data recovered from %s."
687 (car (insert-file-contents (make-auto-save-file-name))))
688 (sit-for 1))
689 (eval-current-buffer nil))
690 (shadow-invalidate-hashtable))
693 (defun shadow-write-info-file ()
694 "Write out information to shadow-info-file.
695 Also clears shadow-hashtable, since when there are new shadows defined, the old
696 hashtable info is invalid."
697 (shadow-invalidate-hashtable)
698 (if shadow-info-file
699 (save-excursion
700 (if (not shadow-info-buffer)
701 (setq shadow-info-buffer (find-file-noselect shadow-info-file)))
702 (set-buffer shadow-info-buffer)
703 (delete-region (point-min) (point-max))
704 (shadow-insert-var 'shadow-clusters)
705 (shadow-insert-var 'shadow-literal-groups)
706 (shadow-insert-var 'shadow-regexp-groups))))
708 (defun shadow-write-todo-file (&optional save)
709 "Write out information to shadow-todo-file.
710 With nonnil argument also saves the buffer."
711 (save-excursion
712 (if (not shadow-todo-buffer)
713 (setq shadow-todo-buffer (find-file-noselect shadow-todo-file)))
714 (set-buffer shadow-todo-buffer)
715 (delete-region (point-min) (point-max))
716 (shadow-insert-var 'shadow-files-to-copy)
717 (if save (shadow-save-todo-file))))
719 (defun shadow-save-todo-file ()
720 (if (and shadow-todo-buffer (buffer-modified-p shadow-todo-buffer))
721 (save-excursion
722 (set-buffer shadow-todo-buffer)
723 (condition-case nil ; have to continue even in case of
724 (basic-save-buffer) ; error, otherwise kill-emacs might
725 (error ; not work!
726 (message "WARNING: Can't save shadow todo file; it is locked!")
727 (sit-for 1))))))
729 (defun shadow-invalidate-hashtable ()
730 (setq shadow-hashtable (make-vector 37 0)))
732 (defun shadow-insert-var (variable)
733 "Prettily insert a setq command for VARIABLE.
734 which, when later evaluated, will restore it to its current setting.
735 SYMBOL must be the name of a variable whose value is a list."
736 (let ((standard-output (current-buffer)))
737 (insert (format "(setq %s" variable))
738 (cond ((consp (eval variable))
739 (insert "\n '(")
740 (prin1 (car (eval variable)))
741 (let ((rest (cdr (eval variable))))
742 (while rest
743 (insert "\n ")
744 (prin1 (car rest))
745 (setq rest (cdr rest)))
746 (insert "))\n\n")))
747 (t (insert " ")
748 (prin1 (eval variable))
749 (insert ")\n\n")))))
751 (defun shadow-save-buffers-kill-emacs (&optional arg)
752 "Offer to save each buffer and copy shadows, then kill this Emacs process.
753 With prefix arg, silently save all file-visiting buffers, then kill.
755 Extended by shadowfile to automatically save `shadow-todo-file' and
756 look for files that have been changed and need to be copied to other systems."
757 ;; This function is necessary because we need to get control and save
758 ;; the todo file /after/ saving other files, but /before/ the warning
759 ;; message about unsaved buffers (because it can get modified by the
760 ;; action of saving other buffers). `kill-emacs-hook' is no good
761 ;; because it is not called at the correct time, and also because it is
762 ;; called when the terminal is disconnected and we cannot ask whether
763 ;; to copy files.
764 (interactive "P")
765 (shadow-save-todo-file)
766 (save-some-buffers arg t)
767 (shadow-copy-files)
768 (shadow-save-todo-file)
769 (and (or (not (memq t (mapcar (function
770 (lambda (buf) (and (buffer-file-name buf)
771 (buffer-modified-p buf))))
772 (buffer-list))))
773 (yes-or-no-p "Modified buffers exist; exit anyway? "))
774 (or (not (fboundp 'process-list))
775 ;; process-list is not defined on VMS.
776 (let ((processes (process-list))
777 active)
778 (while processes
779 (and (memq (process-status (car processes)) '(run stop open))
780 (let ((val (process-kill-without-query (car processes))))
781 (process-kill-without-query (car processes) val)
782 val)
783 (setq active t))
784 (setq processes (cdr processes)))
785 (or (not active)
786 (yes-or-no-p "Active processes exist; kill them and exit anyway? "))))
787 (kill-emacs)))
789 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
790 ;;; Lucid Emacs compatibility
791 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
793 ;; This is on hold until someone tells me about a working version of
794 ;; map-ynp for Lucid Emacs.
796 ;(when (string-match "Lucid" emacs-version)
797 ; (require 'symlink-fix)
798 ; (require 'ange-ftp)
799 ; (require 'map-ynp)
800 ; (if (not (fboundp 'file-truename))
801 ; (fset 'shadow-expand-file-name
802 ; (symbol-function 'symlink-expand-file-name)))
803 ; (if (not (fboundp 'ange-ftp-ftp-name))
804 ; (fset 'ange-ftp-ftp-name
805 ; (symbol-function 'ange-ftp-ftp-path))))
807 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
808 ;;; Hook us up
809 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
811 ;;; File shadowing is activated at load time, unless this this file is
812 ;;; being preloaded, in which case it is added to after-init-hook.
813 ;;; Thanks to Richard Caley for this scheme.
815 (defun shadow-initialize ()
816 (if (null shadow-homedir)
817 (setq shadow-homedir
818 (file-name-as-directory (shadow-expand-file-name "~"))))
819 (if (null shadow-info-file)
820 (setq shadow-info-file
821 (shadow-expand-file-name "~/.shadows")))
822 (if (null shadow-todo-file)
823 (setq shadow-todo-file
824 (shadow-expand-file-name "~/.shadow_todo")))
825 (if (not (shadow-read-files))
826 (progn
827 (message "Shadowfile information files not found - aborting")
828 (beep)
829 (sit-for 3))
830 (when (and (not shadow-inhibit-overload)
831 (not (fboundp 'shadow-orig-save-buffers-kill-emacs)))
832 (fset 'shadow-orig-save-buffers-kill-emacs
833 (symbol-function 'save-buffers-kill-emacs))
834 (fset 'save-buffers-kill-emacs
835 (symbol-function 'shadow-save-buffers-kill-emacs)))
836 (add-hook 'write-file-hooks 'shadow-add-to-todo)
837 (define-key ctl-x-4-map "s" 'shadow-copy-files)))
839 ;;; shadowfile.el ends here