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