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