1 ;;; shadowfile.el --- automatic file copying for Emacs 19
3 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
5 ;; Author: Boris Goldowsky <boris@gnu.org>
8 ;; This file is part of GNU Emacs.
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)
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.
20 ;; You should have received a copy of the GNU General Public License
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.
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).
32 ;; Installation & Use:
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.
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.
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
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.
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.
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.
71 ;; - It will not make directories for you, it just fails to copy files
72 ;; that belong in non-existent directories.
74 ;; Please report any bugs to me (boris@gnu.org). Also let me know
75 ;; if you have suggestions or would like to be informed of updates.
82 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
87 "Automatic file copying when saving a file."
91 (defcustom shadow-noquery nil
92 "*If t, always copy shadow files without asking.
93 If nil \(the default), always ask. If not nil and not t, ask only if there
94 is no buffer currently visiting the file."
95 :type
'(choice (const t
) (const nil
) (other :tag
"Ask if no buffer" maybe
))
98 (defcustom shadow-inhibit-message nil
99 "*If nonnil, do not display a message when a file needs copying."
103 (defcustom shadow-inhibit-overload nil
104 "If nonnil, shadowfile won't redefine C-x C-c.
105 Normally it overloads the function `save-buffers-kill-emacs' to check
106 for files have been changed and need to be copied to other systems."
110 (defcustom shadow-info-file nil
111 "File to keep shadow information in.
112 The shadow-info-file should be shadowed to all your accounts to
113 ensure consistency. Default: ~/.shadows"
114 :type
'(choice (const nil
) file
)
117 (defcustom shadow-todo-file nil
118 "File to store the list of uncopied shadows in.
119 This means that if a remote system is down, or for any reason you cannot or
120 decide not to copy your shadow files at the end of one emacs session, it will
121 remember and ask you again in your next emacs session.
122 This file must NOT be shadowed to any other system, it is host-specific.
123 Default: ~/.shadow_todo"
124 :type
'(choice (const nil
) file
)
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).
132 (defvar shadow-system-name
(system-name)
133 "The complete hostname of this machine.")
135 (defvar shadow-homedir nil
136 "Your home directory on this machine.")
139 ;;; Internal variables whose values are stored in the info and todo files:
142 (defvar shadow-clusters nil
143 "List of host clusters \(see shadow-define-cluster).")
145 (defvar shadow-literal-groups nil
146 "List of files that are shared between hosts.
147 This list contains shadow structures with literal filenames, created by
148 shadow-define-group.")
150 (defvar shadow-regexp-groups nil
151 "List of file types that are shared between hosts.
152 This list contains shadow structures with regexps matching filenames,
153 created by shadow-define-regexp-group.")
156 ;;; Other internal variables:
159 (defvar shadow-files-to-copy nil
) ; List of files that need to
160 ; be copied to remote hosts.
162 (defvar shadow-hashtable nil
) ; for speed
164 (defvar shadow-info-buffer nil
) ; buf visiting shadow-info-file
165 (defvar shadow-todo-buffer nil
) ; buf visiting shadow-todo-file
167 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
168 ;;; Syntactic sugar; General list and string manipulation
169 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
171 (defmacro shadow-when
(condition &rest body
)
173 "(shadow-when CONDITION . BODY) => evaluate BODY if CONDITION is true."
174 (` (if (not (, condition
)) () (,@ body
))))
176 (defun shadow-union (a b
)
177 "Add members of list A to list B
178 if they are not equal to items already in B."
181 (if (member (car a
) b
)
182 (shadow-union (cdr a
) b
)
183 (shadow-union (cdr a
) (cons (car a
) b
)))))
185 (defun shadow-find (func list
)
186 "If FUNC applied to some element of LIST is nonnil,
187 return the first such element."
188 (while (and list
(not (funcall func
(car list
))))
189 (setq list
(cdr list
)))
192 (defun shadow-remove-if (func list
)
193 "Remove elements satisfying FUNC from LIST.
194 Nondestructive; actually returns a copy of the list with the elements removed."
196 (if (funcall func
(car list
))
197 (shadow-remove-if func
(cdr list
))
198 (cons (car list
) (shadow-remove-if func
(cdr list
))))
201 (defun shadow-join (strings sep
)
202 "Concatenate elements of the list of STRINGS with SEP between each."
203 (cond ((null strings
) "")
204 ((null (cdr strings
)) (car strings
))
205 ((concat (car strings
) " " (shadow-join (cdr strings
) sep
)))))
207 (defun shadow-regexp-superquote (string)
208 "Like regexp-quote, but includes the ^ and $
209 to make sure regexp matches nothing but STRING."
210 (concat "^" (regexp-quote string
) "$"))
212 (defun shadow-suffix (prefix string
)
213 "If PREFIX begins STRING, return the rest.
214 Return value is nonnil if PREFIX and STRING are string= up to the length of
216 (let ((lp (length prefix
))
217 (ls (length string
)))
219 (string= prefix
(substring string
0 lp
)))
220 (substring string lp
))))
222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
223 ;;; Clusters and sites
224 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
226 ;;; I use the term `site' to refer to a string which may be the name of a
227 ;;; cluster or a literal hostname. All user-level commands should accept
230 (defun shadow-make-cluster (name primary regexp
)
231 "Creates a shadow cluster
232 called NAME, using the PRIMARY hostname, REGEXP matching all hosts in the
233 cluster. The variable shadow-clusters associates the names of clusters to
235 This function is for program use: to create clusters interactively, use
236 shadow-define-cluster instead."
237 (list name primary regexp
))
239 (defmacro shadow-cluster-name
(cluster)
240 "Return the name of the CLUSTER."
241 (list 'elt cluster
0))
243 (defmacro shadow-cluster-primary
(cluster)
244 "Return the primary hostname of a CLUSTER."
245 (list 'elt cluster
1))
247 (defmacro shadow-cluster-regexp
(cluster)
248 "Return the regexp matching hosts in a CLUSTER."
249 (list 'elt cluster
2))
251 (defun shadow-set-cluster (name primary regexp
)
252 "Put cluster NAME on the list of clusters,
253 replacing old definition if any. PRIMARY and REGEXP are the
254 information defining the cluster. For interactive use, call
255 shadow-define-cluster instead."
256 (let ((rest (shadow-remove-if
257 (function (lambda (x) (equal name
(car x
))))
259 (setq shadow-clusters
260 (cons (shadow-make-cluster name primary regexp
)
263 (defmacro shadow-get-cluster
(name)
264 "Return cluster named NAME, or nil."
265 (list 'assoc name
'shadow-clusters
))
267 (defun shadow-site-primary (site)
268 "If SITE is a cluster, return primary host, otherwise return SITE."
269 (let ((c (shadow-get-cluster site
)))
271 (shadow-cluster-primary c
)
276 (defun shadow-site-cluster (site)
277 "Given a SITE \(hostname or cluster name), return the cluster
278 that it is in, or nil."
279 (or (assoc site shadow-clusters
)
281 (function (lambda (x)
282 (string-match (shadow-cluster-regexp x
)
286 (defun shadow-read-site ()
287 "Read a cluster name or hostname from the minibuffer."
288 (let ((ans (completing-read "Host or cluster name [RET when done]: "
294 (defun shadow-site-match (site1 site2
)
295 "Nonnil iff SITE1 is or includes SITE2.
296 Each may be a host or cluster name; if they are clusters, regexp of site1 will
297 be matched against the primary of site2."
298 (or (string-equal site1 site2
) ; quick check
299 (let* ((cluster1 (shadow-get-cluster site1
))
300 (primary2 (shadow-site-primary site2
)))
302 (string-match (shadow-cluster-regexp cluster1
) primary2
)
303 (string-equal site1 primary2
)))))
305 (defun shadow-get-user (site)
306 "Returns the default username for a site."
307 (ange-ftp-get-user (shadow-site-primary site
)))
309 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
310 ;;; Filename manipulation
311 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
313 (defun shadow-parse-fullpath (fullpath)
314 "Parse PATH into \(site user path) list,
315 or leave it alone if it already is one. Returns nil if the argument is not a
316 full ange-ftp pathname."
319 (ange-ftp-ftp-name fullpath
)))
321 (defun shadow-parse-path (path)
322 "Parse any PATH into \(site user path) list.
323 Argument can be a simple path, full ange-ftp path, or already a hup list."
324 (or (shadow-parse-fullpath path
)
325 (list shadow-system-name
329 (defsubst shadow-make-fullpath
(host user path
)
330 "Make an ange-ftp style fullpath out of HOST, USER (optional), and PATH.
331 This is probably not as general as it ought to be."
333 (if user
(concat user
"@"))
337 (defun shadow-replace-path-component (fullpath newpath
)
338 "Return FULLPATH with the pathname component changed to NEWPATH."
339 (let ((hup (shadow-parse-fullpath fullpath
)))
340 (shadow-make-fullpath (nth 0 hup
) (nth 1 hup
) newpath
)))
342 (defun shadow-local-file (file)
343 "If FILENAME is at this site,
344 remove /user@host part. If refers to a different system or a different user on
345 this system, return nil."
346 (let ((hup (shadow-parse-fullpath file
)))
347 (cond ((null hup
) file
)
348 ((and (shadow-site-match (nth 0 hup
) shadow-system-name
)
349 (string-equal (nth 1 hup
) (user-login-name)))
353 (defun shadow-expand-cluster-in-file-name (file)
354 "If hostname part of FILE is a cluster, expand it
355 into the cluster's primary hostname. Will return the pathname bare if it is
357 (let ((hup (shadow-parse-path file
))
359 (cond ((null hup
) file
)
360 ((shadow-local-file hup
))
361 ((shadow-make-fullpath (shadow-site-primary (nth 0 hup
))
365 (defun shadow-expand-file-name (file &optional default
)
366 "Expand file name and get file's true name."
367 (file-truename (expand-file-name file default
)))
369 (defun shadow-contract-file-name (file)
371 by replacing (when possible) home directory with ~, and hostname with cluster
372 name that includes it. Filename should be absolute and true."
373 (let* ((hup (shadow-parse-path file
))
374 (homedir (if (shadow-local-file hup
)
376 (file-name-as-directory
377 (nth 2 (shadow-parse-fullpath
379 (shadow-make-fullpath
380 (nth 0 hup
) (nth 1 hup
) "~")))))))
381 (suffix (shadow-suffix homedir
(nth 2 hup
)))
382 (cluster (shadow-site-cluster (nth 0 hup
))))
383 (shadow-make-fullpath
385 (shadow-cluster-name cluster
)
392 (defun shadow-same-site (pattern file
)
393 "True if the site of PATTERN and of FILE are on the same site.
394 If usernames are supplied, they must also match exactly. PATTERN and FILE may
395 be lists of host, user, path, or ange-ftp pathnames. FILE may also be just a
397 (let ((pattern-sup (shadow-parse-fullpath pattern
))
398 (file-sup (shadow-parse-path file
)))
400 (shadow-site-match (nth 0 pattern-sup
) (nth 0 file-sup
))
401 (or (null (nth 1 pattern-sup
))
402 (string-equal (nth 1 pattern-sup
) (nth 1 file-sup
))))))
404 (defun shadow-file-match (pattern file
&optional regexp
)
405 "Returns t if PATTERN matches FILE.
406 If REGEXP is supplied and nonnil, the pathname part of the pattern is a regular
407 expression, otherwise it must match exactly. The sites and usernames must
408 match---see shadow-same-site. The pattern must be in full ange-ftp format, but
409 the file can be any valid filename. This function does not do any filename
410 expansion or contraction, you must do that yourself first."
411 (let* ((pattern-sup (shadow-parse-fullpath pattern
))
412 (file-sup (shadow-parse-path file
)))
413 (and (shadow-same-site pattern-sup file-sup
)
415 (string-match (nth 2 pattern-sup
) (nth 2 file-sup
))
416 (string-equal (nth 2 pattern-sup
) (nth 2 file-sup
))))))
418 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
419 ;;; User-level Commands
420 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
422 (defun shadow-define-cluster (name)
423 "Edit \(or create) the definition of a cluster.
424 This is a group of hosts that share directories, so that copying to or from
425 one of them is sufficient to update the file on all of them. Clusters are
426 defined by a name, the network address of a primary host \(the one we copy
427 files to), and a regular expression that matches the hostnames of all the sites
429 (interactive (list (completing-read "Cluster name: " shadow-clusters
() ())))
430 (let* ((old (shadow-get-cluster name
))
431 (primary (read-string "Primary host: "
432 (if old
(shadow-cluster-primary old
)
434 (regexp (let (try-regexp)
439 "Regexp matching all host names: "
440 (if old
(shadow-cluster-regexp old
)
441 (shadow-regexp-superquote primary
))))
443 (message "Regexp doesn't include the primary host!")
446 ; (username (read-no-blanks-input
447 ; (format "Username [default: %s]: "
448 ; (shadow-get-user primary))
449 ; (if old (or (shadow-cluster-username old) "")
450 ; (user-login-name))))
452 ; (if (string-equal "" username) (setq username nil))
453 (shadow-set-cluster name primary regexp
)))
455 (defun shadow-define-literal-group ()
456 "Declare a single file to be shared between sites.
457 It may have different filenames on each site. When this file is edited, the
458 new version will be copied to each of the other locations. Sites can be
459 specific hostnames, or names of clusters \(see shadow-define-cluster)."
461 (let* ((hup (shadow-parse-fullpath
462 (shadow-contract-file-name (buffer-file-name))))
465 (while (setq site
(shadow-read-site))
466 (setq user
(read-string (format "Username [default %s]: "
467 (shadow-get-user site
)))
468 path
(read-string "Filename: " path
))
469 (setq group
(cons (shadow-make-fullpath site
470 (if (string-equal "" user
)
471 (shadow-get-user site
)
475 (setq shadow-literal-groups
(cons group shadow-literal-groups
)))
476 (shadow-write-info-file))
478 (defun shadow-define-regexp-group ()
479 "Make each of a group of files be shared between hosts.
480 Prompts for regular expression; files matching this are shared between a list
481 of sites, which are also prompted for. The filenames must be identical on all
482 hosts \(if they aren't, use shadow-define-group instead of this function).
483 Each site can be either a hostname or the name of a cluster \(see
484 shadow-define-cluster)."
486 (let ((regexp (read-string
488 (if (buffer-file-name)
489 (shadow-regexp-superquote
492 (shadow-contract-file-name
493 (buffer-file-name))))))))
494 site sites usernames
)
495 (while (setq site
(shadow-read-site))
496 (setq sites
(cons site sites
))
498 (cons (read-string (format "Username for %s: " site
)
499 (shadow-get-user site
))
501 (setq shadow-regexp-groups
502 (cons (shadow-make-group regexp sites usernames
)
503 shadow-regexp-groups
))
504 (shadow-write-info-file)))
506 (defun shadow-shadows ()
507 ;; Mostly for debugging.
508 "Interactive function to display shadows of a buffer."
510 (let ((msg (shadow-join (mapcar (function cdr
)
511 (shadow-shadows-of (buffer-file-name)))
514 (if (zerop (length msg
))
518 (defun shadow-copy-files (&optional arg
)
519 "Copy all pending shadow files.
520 With prefix argument, copy all pending files without query.
521 Pending copies are stored in variable shadow-files-to-copy, and in
522 shadow-todo-file if necessary. This function is invoked by
523 shadow-save-buffers-kill-emacs, so it is not usually necessary to
526 (if (and (not shadow-files-to-copy
) (interactive-p))
527 (message "No files need to be shadowed.")
529 (map-y-or-n-p (function
531 (or arg shadow-noquery
532 (format "Copy shadow file %s? " (cdr pair
)))))
533 (function shadow-copy-file
)
535 '("shadow" "shadows" "copy"))
536 (shadow-write-todo-file t
))))
538 (defun shadow-cancel ()
539 "Cancel the instruction to copy some files.
540 Prompts for which copy operations to cancel. You will not be asked to copy
541 them again, unless you make more changes to the files. To cancel a shadow
542 permanently, remove the group from shadow-literal-groups or
543 shadow-regexp-groups."
545 (map-y-or-n-p (function (lambda (pair)
546 (format "Cancel copying %s to %s? "
547 (car pair
) (cdr pair
))))
548 (function (lambda (pair)
549 (shadow-remove-from-todo pair
)))
551 '("shadow" "shadows" "cancel copy"))
552 (message "There are %d shadows to be updated."
553 (length shadow-files-to-copy
))
554 (shadow-write-todo-file))
556 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
557 ;;; Internal functions
558 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
560 (defun shadow-make-group (regexp sites usernames
)
561 "Makes a description of a file group---
562 actually a list of regexp ange-ftp file names---from REGEXP \(name of file to
563 be shadowed), list of SITES, and corresponding list of USERNAMES for each
566 (cons (shadow-make-fullpath (car sites
) (car usernames
) regexp
)
567 (shadow-make-group regexp
(cdr sites
) (cdr usernames
)))
570 (defun shadow-copy-file (s)
571 "Copy one shadow file."
573 (cond ((get-file-buffer
574 (abbreviate-file-name (shadow-expand-file-name (car s
)))))
575 ((not (file-readable-p (car s
)))
577 (format "Cannot find file %s--cancel copy request?"
579 (shadow-remove-from-todo s
))
581 ((or (eq t shadow-noquery
)
583 (format "No buffer for %s -- update shadow anyway?"
585 (find-file-noselect (car s
)))))
586 (to (shadow-expand-cluster-in-file-name (cdr s
))))
593 (write-region (point-min) (point-max) to
)
594 (shadow-remove-from-todo s
))
595 (error (message "Shadow %s not updated!" (cdr s
))))))))
597 (defun shadow-shadows-of (file)
598 "Returns copy operations needed to update FILE.
599 Filename should have clusters expanded, but otherwise can have any format.
600 Return value is a list of dotted pairs like \(from . to), where from
601 and to are absolute file names."
602 (or (symbol-value (intern-soft file shadow-hashtable
))
603 (let* ((absolute-file (shadow-expand-file-name
604 (or (shadow-local-file file
) file
)
606 (canonical-file (shadow-contract-file-name absolute-file
))
608 (mapcar (function (lambda (shadow)
609 (cons absolute-file shadow
)))
612 canonical-file shadow-literal-groups nil
)
614 canonical-file shadow-regexp-groups t
)))))
615 (set (intern file shadow-hashtable
) shadows
))))
617 (defun shadow-shadows-of-1 (file groups regexp
)
618 "Return list of FILE's shadows in GROUPS,
619 which are considered as regular expressions if third arg REGEXP is true."
623 (function (lambda (x) (shadow-file-match x file regexp
)))
625 (append (cond ((equal nonmatching
(car groups
)) nil
)
627 (let ((realpath (nth 2 (shadow-parse-fullpath file
))))
631 (shadow-replace-path-component x realpath
)))
634 (shadow-shadows-of-1 file
(cdr groups
) regexp
)))))
636 (defun shadow-add-to-todo ()
637 "If current buffer has shadows, add them to the list
638 of files needing to be copied."
639 (let ((shadows (shadow-shadows-of
640 (shadow-expand-file-name
641 (buffer-file-name (current-buffer))))))
643 (setq shadow-files-to-copy
644 (shadow-union shadows shadow-files-to-copy
))
645 (shadow-when (not shadow-inhibit-message
)
646 (message "%s" (substitute-command-keys
647 "Use \\[shadow-copy-files] to update shadows."))
649 (shadow-write-todo-file)))
650 nil
) ; Return nil for write-file-hooks
652 (defun shadow-remove-from-todo (pair)
653 "Remove PAIR from shadow-files-to-copy.
654 PAIR must be (eq to) one of the elements of that list."
655 (setq shadow-files-to-copy
656 (shadow-remove-if (function (lambda (s) (eq s pair
)))
657 shadow-files-to-copy
)))
659 (defun shadow-read-files ()
660 "Visits and loads shadow-info-file and shadow-todo-file,
661 thus restoring shadowfile's state from your last emacs session.
662 Returns t unless files were locked; then returns nil."
664 (if (and (fboundp 'file-locked-p
)
665 (or (stringp (file-locked-p shadow-info-file
))
666 (stringp (file-locked-p shadow-todo-file
))))
668 (message "Shadowfile is running in another emacs; can't have two.")
673 (shadow-when shadow-info-file
674 (set-buffer (setq shadow-info-buffer
675 (find-file-noselect shadow-info-file
)))
676 (shadow-when (and (not (buffer-modified-p))
677 (file-newer-than-file-p (make-auto-save-file-name)
680 (message "Data recovered from %s."
681 (car (insert-file-contents (make-auto-save-file-name))))
683 (eval-current-buffer))
684 (shadow-when shadow-todo-file
685 (set-buffer (setq shadow-todo-buffer
686 (find-file-noselect shadow-todo-file
)))
687 (shadow-when (and (not (buffer-modified-p))
688 (file-newer-than-file-p (make-auto-save-file-name)
691 (message "Data recovered from %s."
692 (car (insert-file-contents (make-auto-save-file-name))))
694 (eval-current-buffer nil
))
695 (shadow-invalidate-hashtable))
698 (defun shadow-write-info-file ()
699 "Write out information to shadow-info-file.
700 Also clears shadow-hashtable, since when there are new shadows defined, the old
701 hashtable info is invalid."
702 (shadow-invalidate-hashtable)
705 (if (not shadow-info-buffer
)
706 (setq shadow-info-buffer
(find-file-noselect shadow-info-file
)))
707 (set-buffer shadow-info-buffer
)
708 (delete-region (point-min) (point-max))
709 (shadow-insert-var 'shadow-clusters
)
710 (shadow-insert-var 'shadow-literal-groups
)
711 (shadow-insert-var 'shadow-regexp-groups
))))
713 (defun shadow-write-todo-file (&optional save
)
714 "Write out information to shadow-todo-file.
715 With nonnil argument also saves the buffer."
717 (if (not shadow-todo-buffer
)
718 (setq shadow-todo-buffer
(find-file-noselect shadow-todo-file
)))
719 (set-buffer shadow-todo-buffer
)
720 (delete-region (point-min) (point-max))
721 (shadow-insert-var 'shadow-files-to-copy
)
722 (if save
(shadow-save-todo-file))))
724 (defun shadow-save-todo-file ()
725 (if (and shadow-todo-buffer
(buffer-modified-p shadow-todo-buffer
))
727 (set-buffer shadow-todo-buffer
)
728 (condition-case nil
; have to continue even in case of
729 (basic-save-buffer) ; error, otherwise kill-emacs might
731 (message "WARNING: Can't save shadow todo file; it is locked!")
734 (defun shadow-invalidate-hashtable ()
735 (setq shadow-hashtable
(make-vector 37 0)))
737 (defun shadow-insert-var (variable)
738 "Prettily insert a setq command for VARIABLE.
739 which, when later evaluated, will restore it to its current setting.
740 SYMBOL must be the name of a variable whose value is a list."
741 (let ((standard-output (current-buffer)))
742 (insert (format "(setq %s" variable
))
743 (cond ((consp (eval variable
))
745 (prin1 (car (eval variable
)))
746 (let ((rest (cdr (eval variable
))))
750 (setq rest
(cdr rest
)))
753 (prin1 (eval variable
))
756 (defun shadow-save-buffers-kill-emacs (&optional arg
)
757 "Offer to save each buffer and copy shadows, then kill this Emacs process.
758 With prefix arg, silently save all file-visiting buffers, then kill.
760 Extended by shadowfile to automatically save `shadow-todo-file' and
761 look for files that have been changed and need to be copied to other systems."
762 ;; This function is necessary because we need to get control and save
763 ;; the todo file /after/ saving other files, but /before/ the warning
764 ;; message about unsaved buffers (because it can get modified by the
765 ;; action of saving other buffers). `kill-emacs-hook' is no good
766 ;; because it is not called at the correct time, and also because it is
767 ;; called when the terminal is disconnected and we cannot ask whether
770 (shadow-save-todo-file)
771 (save-some-buffers arg t
)
773 (shadow-save-todo-file)
774 (and (or (not (memq t
(mapcar (function
775 (lambda (buf) (and (buffer-file-name buf
)
776 (buffer-modified-p buf
))))
778 (yes-or-no-p "Modified buffers exist; exit anyway? "))
779 (or (not (fboundp 'process-list
))
780 ;; process-list is not defined on VMS.
781 (let ((processes (process-list))
784 (and (memq (process-status (car processes
)) '(run stop open
))
785 (let ((val (process-kill-without-query (car processes
))))
786 (process-kill-without-query (car processes
) val
)
789 (setq processes
(cdr processes
)))
791 (yes-or-no-p "Active processes exist; kill them and exit anyway? "))))
794 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
795 ;;; Lucid Emacs compatibility
796 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
798 ;; This is on hold until someone tells me about a working version of
799 ;; map-ynp for Lucid Emacs.
801 ;(shadow-when (string-match "Lucid" emacs-version)
802 ; (require 'symlink-fix)
803 ; (require 'ange-ftp)
805 ; (if (not (fboundp 'file-truename))
806 ; (fset 'shadow-expand-file-name
807 ; (symbol-function 'symlink-expand-file-name)))
808 ; (if (not (fboundp 'ange-ftp-ftp-name))
809 ; (fset 'ange-ftp-ftp-name
810 ; (symbol-function 'ange-ftp-ftp-path))))
812 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
814 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
816 ;;; File shadowing is activated at load time, unless this this file is
817 ;;; being preloaded, in which case it is added to after-init-hook.
818 ;;; Thanks to Richard Caley for this scheme.
820 (defun shadow-initialize ()
821 (if (null shadow-homedir
)
823 (file-name-as-directory (shadow-expand-file-name "~"))))
824 (if (null shadow-info-file
)
825 (setq shadow-info-file
826 (shadow-expand-file-name "~/.shadows")))
827 (if (null shadow-todo-file
)
828 (setq shadow-todo-file
829 (shadow-expand-file-name "~/.shadow_todo")))
830 (if (not (shadow-read-files))
832 (message "Shadowfile information files not found - aborting")
835 (shadow-when (and (not shadow-inhibit-overload
)
836 (not (fboundp 'shadow-orig-save-buffers-kill-emacs
)))
837 (fset 'shadow-orig-save-buffers-kill-emacs
838 (symbol-function 'save-buffers-kill-emacs
))
839 (fset 'save-buffers-kill-emacs
840 (symbol-function 'shadow-save-buffers-kill-emacs
)))
841 (add-hook 'write-file-hooks
'shadow-add-to-todo
)
842 (define-key ctl-x-4-map
"s" 'shadow-copy-files
)))
845 ;;;eval:(put 'shadow-when 'lisp-indent-hook 1)
848 ;;; shadowfile.el ends here