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