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