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