1 ;;; shadowfile.el --- automatic file copying for Emacs 19
3 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
5 ;; Author: Boris Goldowsky <boris@gnu.ai.mit.edu>
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.
26 ;; shadowfile|Boris Goldowsky|boris@gnu.ai.mit.edu|
27 ;; Helps you keep identical copies of files in multiple places.|
28 ;; $Date: 1996/01/14 07:34:30 $ |$Revision: 1.7 $|~/misc/shadowfile.el.Z|
32 ;; This package helps you to keep identical copies of files in more than one
33 ;; place - possibly on different machines. When you save a file, it checks
34 ;; whether it is on the list of files with "shadows", and if so, it tries to
35 ;; copy it when you exit emacs (or use the shadow-copy-files command).
37 ;; Installation & Use:
39 ;; Put (require 'shadowfile) in your .emacs; add clusters (if necessary)
40 ;; and file groups with shadow-define-cluster,
41 ;; shadow-define-literal-group, and shadow-define-regexp-group (see the
42 ;; documentation for these functions for information on how and when to
43 ;; use them). After doing this once, everything should be automatic.
45 ;; The lists of clusters and shadows are saved in a file called .shadows,
46 ;; so that they can be remembered from one emacs session to another, even
47 ;; (as much as possible) if the emacs session terminates abnormally. The
48 ;; files needing to be copied are stored in .shadow_todo; if a file cannot
49 ;; be copied for any reason, it will stay on the list to be tried again
50 ;; next time. The .shadows file should itself have shadows on all your
51 ;; accounts so that the information in it is consistent everywhere, but
52 ;; .shadow_todo is local information and should have no shadows.
54 ;; If you do not want to copy a particular file, you can answer "no" and
55 ;; be asked again next time you hit C-x 4 s or exit emacs. If you do not
56 ;; want to be asked again, use shadow-cancel, and you will not be asked
57 ;; until you change the file and save it again. If you do not want to
58 ;; shadow that file ever again, you can edit it out of the .shadows
59 ;; buffer. Anytime you edit the .shadows buffer, you must type M-x
60 ;; shadow-read-files to load in the new information, or your changes will
65 ;; - It is bad to have two emacses both running shadowfile at the same
66 ;; time. It tries to detect this condition, but is not always successful.
68 ;; - You have to be careful not to edit a file in two locations
69 ;; before shadowfile has had a chance to copy it; otherwise
70 ;; "updating shadows" will overwrite one of the changed versions.
72 ;; - It ought to check modification times of both files to make sure
73 ;; it is doing the right thing. This will have to wait until
74 ;; file-newer-than-file-p works between machines.
76 ;; - It will not make directories for you, it just fails to copy files
77 ;; that belong in non-existent directories.
79 ;; Please report any bugs to me (boris@gnu.ai.mit.edu). Also let me know
80 ;; if you have suggestions or would like to be informed of updates.
87 (setq find-file-visit-truename t
) ; makes life easier with symbolic links
89 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
91 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
93 (defvar shadow-noquery nil
94 "*If t, always copy shadow files without asking.
95 If nil \(the default), always ask. If not nil and not t, ask only if there
96 is no buffer currently visiting the file.")
98 (defvar shadow-inhibit-message nil
99 "*If nonnil, do not display a message when a file needs copying.")
101 (defvar shadow-inhibit-overload nil
102 "If nonnil, shadowfile won't redefine C-x C-c.
103 Normally it overloads the function `save-buffers-kill-emacs' to check
104 for files have been changed and need to be copied to other systems.")
106 (defvar shadow-info-file nil
107 "File to keep shadow information in.
108 The shadow-info-file should be shadowed to all your accounts to
109 ensure consistency. Default: ~/.shadows")
111 (defvar shadow-todo-file nil
112 "File to store the list of uncopied shadows in.
113 This means that if a remote system is down, or for any reason you cannot or
114 decide not to copy your shadow files at the end of one emacs session, it will
115 remember and ask you again in your next emacs session.
116 This file must NOT be shadowed to any other system, it is host-specific.
117 Default: ~/.shadow_todo")
119 ;;; The following two variables should in most cases initialize themselves
120 ;;; correctly. They are provided as variables in case the defaults are wrong
121 ;;; on your machine \(and for efficiency).
123 (defvar shadow-system-name
(system-name)
124 "The complete hostname of this machine.")
126 (defvar shadow-homedir nil
127 "Your home directory on this machine.")
130 ;;; Internal variables whose values are stored in the info and todo files:
133 (defvar shadow-clusters nil
134 "List of host clusters \(see shadow-define-cluster).")
136 (defvar shadow-literal-groups nil
137 "List of files that are shared between hosts.
138 This list contains shadow structures with literal filenames, created by
139 shadow-define-group.")
141 (defvar shadow-regexp-groups nil
142 "List of file types that are shared between hosts.
143 This list contains shadow structures with regexps matching filenames,
144 created by shadow-define-regexp-group.")
147 ;;; Other internal variables:
150 (defvar shadow-files-to-copy nil
) ; List of files that need to
151 ; be copied to remote hosts.
153 (defvar shadow-hashtable nil
) ; for speed
155 (defvar shadow-info-buffer nil
) ; buf visiting shadow-info-file
156 (defvar shadow-todo-buffer nil
) ; buf visiting shadow-todo-file
158 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
159 ;;; Syntactic sugar; General list and string manipulation
160 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
162 (defmacro shadow-when
(condition &rest body
)
164 "(shadow-when CONDITION . BODY) => evaluate BODY if CONDITION is true."
165 (` (if (not (, condition
)) () (,@ body
))))
167 (defun shadow-union (a b
)
168 "Add members of list A to list B
169 if they are not equal to items already in B."
172 (if (member (car a
) b
)
173 (shadow-union (cdr a
) b
)
174 (shadow-union (cdr a
) (cons (car a
) b
)))))
176 (defun shadow-find (func list
)
177 "If FUNC applied to some element of LIST is nonnil,
178 return the first such element."
179 (while (and list
(not (funcall func
(car list
))))
180 (setq list
(cdr list
)))
183 (defun shadow-remove-if (func list
)
184 "Remove elements satisfying FUNC from LIST.
185 Nondestructive; actually returns a copy of the list with the elements removed."
187 (if (funcall func
(car list
))
188 (shadow-remove-if func
(cdr list
))
189 (cons (car list
) (shadow-remove-if func
(cdr list
))))
192 (defun shadow-join (strings sep
)
193 "Concatenate elements of the list of STRINGS with SEP between each."
194 (cond ((null strings
) "")
195 ((null (cdr strings
)) (car strings
))
196 ((concat (car strings
) " " (shadow-join (cdr strings
) sep
)))))
198 (defun shadow-regexp-superquote (string)
199 "Like regexp-quote, but includes the ^ and $
200 to make sure regexp matches nothing but STRING."
201 (concat "^" (regexp-quote string
) "$"))
203 (defun shadow-suffix (prefix string
)
204 "If PREFIX begins STRING, return the rest.
205 Return value is nonnil if PREFIX and STRING are string= up to the length of
207 (let ((lp (length prefix
))
208 (ls (length string
)))
210 (string= prefix
(substring string
0 lp
)))
211 (substring string lp
))))
213 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
214 ;;; Clusters and sites
215 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
217 ;;; I use the term `site' to refer to a string which may be the name of a
218 ;;; cluster or a literal hostname. All user-level commands should accept
221 (defun shadow-make-cluster (name primary regexp
)
222 "Creates a shadow cluster
223 called NAME, using the PRIMARY hostname, REGEXP matching all hosts in the
224 cluster. The variable shadow-clusters associates the names of clusters to
226 This function is for program use: to create clusters interactively, use
227 shadow-define-cluster instead."
228 (list name primary regexp
))
230 (defmacro shadow-cluster-name
(cluster)
231 "Return the name of the CLUSTER."
232 (list 'elt cluster
0))
234 (defmacro shadow-cluster-primary
(cluster)
235 "Return the primary hostname of a CLUSTER."
236 (list 'elt cluster
1))
238 (defmacro shadow-cluster-regexp
(cluster)
239 "Return the regexp matching hosts in a CLUSTER."
240 (list 'elt cluster
2))
242 (defun shadow-set-cluster (name primary regexp
)
243 "Put cluster NAME on the list of clusters,
244 replacing old definition if any. PRIMARY and REGEXP are the
245 information defining the cluster. For interactive use, call
246 shadow-define-cluster instead."
247 (let ((rest (shadow-remove-if
248 (function (lambda (x) (equal name
(car x
))))
250 (setq shadow-clusters
251 (cons (shadow-make-cluster name primary regexp
)
254 (defmacro shadow-get-cluster
(name)
255 "Return cluster named NAME, or nil."
256 (list 'assoc name
'shadow-clusters
))
258 (defun shadow-site-primary (site)
259 "If SITE is a cluster, return primary host, otherwise return SITE."
260 (let ((c (shadow-get-cluster site
)))
262 (shadow-cluster-primary c
)
267 (defun shadow-site-cluster (site)
268 "Given a SITE \(hostname or cluster name), return the cluster
269 that it is in, or nil."
270 (or (assoc site shadow-clusters
)
272 (function (lambda (x)
273 (string-match (shadow-cluster-regexp x
)
277 (defun shadow-read-site ()
278 "Read a cluster name or hostname from the minibuffer."
279 (let ((ans (completing-read "Host or cluster name [RET when done]: "
285 (defun shadow-site-match (site1 site2
)
286 "Nonnil iff SITE1 is or includes SITE2.
287 Each may be a host or cluster name; if they are clusters, regexp of site1 will
288 be matched against the primary of site2."
289 (or (string-equal site1 site2
) ; quick check
290 (let* ((cluster1 (shadow-get-cluster site1
))
291 (primary2 (shadow-site-primary site2
)))
293 (string-match (shadow-cluster-regexp cluster1
) primary2
)
294 (string-equal site1 primary2
)))))
296 (defun shadow-get-user (site)
297 "Returns the default username for a site."
298 (ange-ftp-get-user (shadow-site-primary site
)))
300 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
301 ;;; Filename manipulation
302 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
304 (defun shadow-parse-fullpath (fullpath)
305 "Parse PATH into \(site user path) list,
306 or leave it alone if it already is one. Returns nil if the argument is not a
307 full ange-ftp pathname."
310 (ange-ftp-ftp-name fullpath
)))
312 (defun shadow-parse-path (path)
313 "Parse any PATH into \(site user path) list.
314 Argument can be a simple path, full ange-ftp path, or already a hup list."
315 (or (shadow-parse-fullpath path
)
316 (list shadow-system-name
320 (defsubst shadow-make-fullpath
(host user path
)
321 "Make an ange-ftp style fullpath out of HOST, USER (optional), and PATH.
322 This is probably not as general as it ought to be."
324 (if user
(concat user
"@"))
328 (defun shadow-replace-path-component (fullpath newpath
)
329 "Return FULLPATH with the pathname component changed to NEWPATH."
330 (let ((hup (shadow-parse-fullpath fullpath
)))
331 (shadow-make-fullpath (nth 0 hup
) (nth 1 hup
) newpath
)))
333 (defun shadow-local-file (file)
334 "If FILENAME is at this site,
335 remove /user@host part. If refers to a different system or a different user on
336 this system, return nil."
337 (let ((hup (shadow-parse-fullpath file
)))
338 (cond ((null hup
) file
)
339 ((and (shadow-site-match (nth 0 hup
) shadow-system-name
)
340 (string-equal (nth 1 hup
) (user-login-name)))
344 (defun shadow-expand-cluster-in-file-name (file)
345 "If hostname part of FILE is a cluster, expand it
346 into the cluster's primary hostname. Will return the pathname bare if it is
348 (let ((hup (shadow-parse-path file
))
350 (cond ((null hup
) file
)
351 ((shadow-local-file hup
))
352 ((shadow-make-fullpath (shadow-site-primary (nth 0 hup
))
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
)))
360 (defun shadow-contract-file-name (file)
362 by replacing (when possible) home directory with ~, and hostname with cluster
363 name that includes it. Filename should be absolute and true."
364 (let* ((hup (shadow-parse-path file
))
365 (homedir (if (shadow-local-file hup
)
367 (file-name-as-directory
368 (nth 2 (shadow-parse-fullpath
370 (shadow-make-fullpath
371 (nth 0 hup
) (nth 1 hup
) "~")))))))
372 (suffix (shadow-suffix homedir
(nth 2 hup
)))
373 (cluster (shadow-site-cluster (nth 0 hup
))))
374 (shadow-make-fullpath
376 (shadow-cluster-name cluster
)
383 (defun shadow-same-site (pattern file
)
384 "True if the site of PATTERN and of FILE are on the same site.
385 If usernames are supplied, they must also match exactly. PATTERN and FILE may
386 be lists of host, user, path, or ange-ftp pathnames. FILE may also be just a
388 (let ((pattern-sup (shadow-parse-fullpath pattern
))
389 (file-sup (shadow-parse-path file
)))
391 (shadow-site-match (nth 0 pattern-sup
) (nth 0 file-sup
))
392 (or (null (nth 1 pattern-sup
))
393 (string-equal (nth 1 pattern-sup
) (nth 1 file-sup
))))))
395 (defun shadow-file-match (pattern file
&optional regexp
)
396 "Returns t if PATTERN matches FILE.
397 If REGEXP is supplied and nonnil, the pathname part of the pattern is a regular
398 expression, otherwise it must match exactly. The sites and usernames must
399 match---see shadow-same-site. The pattern must be in full ange-ftp format, but
400 the file can be any valid filename. This function does not do any filename
401 expansion or contraction, you must do that yourself first."
402 (let* ((pattern-sup (shadow-parse-fullpath pattern
))
403 (file-sup (shadow-parse-path file
)))
404 (and (shadow-same-site pattern-sup file-sup
)
406 (string-match (nth 2 pattern-sup
) (nth 2 file-sup
))
407 (string-equal (nth 2 pattern-sup
) (nth 2 file-sup
))))))
409 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
410 ;;; User-level Commands
411 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
413 (defun shadow-define-cluster (name)
414 "Edit \(or create) the definition of a cluster.
415 This is a group of hosts that share directories, so that copying to or from
416 one of them is sufficient to update the file on all of them. Clusters are
417 defined by a name, the network address of a primary host \(the one we copy
418 files to), and a regular expression that matches the hostnames of all the sites
420 (interactive (list (completing-read "Cluster name: " shadow-clusters
() ())))
421 (let* ((old (shadow-get-cluster name
))
422 (primary (read-string "Primary host: "
423 (if old
(shadow-cluster-primary old
)
425 (regexp (let (try-regexp)
430 "Regexp matching all host names: "
431 (if old
(shadow-cluster-regexp old
)
432 (shadow-regexp-superquote primary
))))
434 (message "Regexp doesn't include the primary host!")
437 ; (username (read-no-blanks-input
438 ; (format "Username [default: %s]: "
439 ; (shadow-get-user primary))
440 ; (if old (or (shadow-cluster-username old) "")
441 ; (user-login-name))))
443 ; (if (string-equal "" username) (setq username nil))
444 (shadow-set-cluster name primary regexp
)))
446 (defun shadow-define-literal-group ()
447 "Declare a single file to be shared between sites.
448 It may have different filenames on each site. When this file is edited, the
449 new version will be copied to each of the other locations. Sites can be
450 specific hostnames, or names of clusters \(see shadow-define-cluster)."
452 (let* ((hup (shadow-parse-fullpath
453 (shadow-contract-file-name (buffer-file-name))))
456 (while (setq site
(shadow-read-site))
457 (setq user
(read-string (format "Username [default %s]: "
458 (shadow-get-user site
)))
459 path
(read-string "Filename: " path
))
460 (setq group
(cons (shadow-make-fullpath site
461 (if (string-equal "" user
)
462 (shadow-get-user site
)
466 (setq shadow-literal-groups
(cons group shadow-literal-groups
)))
467 (shadow-write-info-file))
469 (defun shadow-define-regexp-group ()
470 "Make each of a group of files be shared between hosts.
471 Prompts for regular expression; files matching this are shared between a list
472 of sites, which are also prompted for. The filenames must be identical on all
473 hosts \(if they aren't, use shadow-define-group instead of this function).
474 Each site can be either a hostname or the name of a cluster \(see
475 shadow-define-cluster)."
477 (let ((regexp (read-string
479 (if (buffer-file-name)
480 (shadow-regexp-superquote
483 (shadow-contract-file-name
484 (buffer-file-name))))))))
485 site sites usernames
)
486 (while (setq site
(shadow-read-site))
487 (setq sites
(cons site sites
))
489 (cons (read-string (format "Username for %s: " site
)
490 (shadow-get-user site
))
492 (setq shadow-regexp-groups
493 (cons (shadow-make-group regexp sites usernames
)
494 shadow-regexp-groups
))
495 (shadow-write-info-file)))
497 (defun shadow-shadows ()
498 ;; Mostly for debugging.
499 "Interactive function to display shadows of a buffer."
501 (let ((msg (shadow-join (mapcar (function cdr
)
502 (shadow-shadows-of (buffer-file-name)))
505 (if (zerop (length msg
))
509 (defun shadow-copy-files (&optional arg
)
510 "Copy all pending shadow files.
511 With prefix argument, copy all pending files without query.
512 Pending copies are stored in variable shadow-files-to-copy, and in
513 shadow-todo-file if necessary. This function is invoked by
514 shadow-save-buffers-kill-emacs, so it is not usually necessary to
517 (if (and (not shadow-files-to-copy
) (interactive-p))
518 (message "No files need to be shadowed.")
520 (map-y-or-n-p (function
522 (or arg shadow-noquery
523 (format "Copy shadow file %s? " (cdr pair
)))))
524 (function shadow-copy-file
)
526 '("shadow" "shadows" "copy"))
527 (shadow-write-todo-file t
))))
529 (defun shadow-cancel ()
530 "Cancel the instruction to copy some files.
531 Prompts for which copy operations to cancel. You will not be asked to copy
532 them again, unless you make more changes to the files. To cancel a shadow
533 permanently, remove the group from shadow-literal-groups or
534 shadow-regexp-groups."
536 (map-y-or-n-p (function (lambda (pair)
537 (format "Cancel copying %s to %s? "
538 (car pair
) (cdr pair
))))
539 (function (lambda (pair)
540 (shadow-remove-from-todo pair
)))
542 '("shadow" "shadows" "cancel copy"))
543 (message "There are %d shadows to be updated."
544 (length shadow-files-to-copy
))
545 (shadow-write-todo-file))
547 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
548 ;;; Internal functions
549 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
551 (defun shadow-make-group (regexp sites usernames
)
552 "Makes a description of a file group---
553 actually a list of regexp ange-ftp file names---from REGEXP \(name of file to
554 be shadowed), list of SITES, and corresponding list of USERNAMES for each
557 (cons (shadow-make-fullpath (car sites
) (car usernames
) regexp
)
558 (shadow-make-group regexp
(cdr sites
) (cdr usernames
)))
561 (defun shadow-copy-file (s)
562 "Copy one shadow file."
564 (cond ((get-file-buffer
565 (abbreviate-file-name (shadow-expand-file-name (car s
)))))
566 ((not (file-readable-p (car s
)))
568 (format "Cannot find file %s--cancel copy request?"
570 (shadow-remove-from-todo s
))
572 ((or (eq t shadow-noquery
)
574 (format "No buffer for %s -- update shadow anyway?"
576 (find-file-noselect (car s
)))))
577 (to (shadow-expand-cluster-in-file-name (cdr s
))))
584 (write-region (point-min) (point-max) to
)
585 (shadow-remove-from-todo s
))
586 (error (message "Shadow %s not updated!" (cdr s
))))))))
588 (defun shadow-shadows-of (file)
589 "Returns copy operations needed to update FILE.
590 Filename should have clusters expanded, but otherwise can have any format.
591 Return value is a list of dotted pairs like \(from . to), where from
592 and to are absolute file names."
593 (or (symbol-value (intern-soft file shadow-hashtable
))
594 (let* ((absolute-file (shadow-expand-file-name
595 (or (shadow-local-file file
) file
)
597 (canonical-file (shadow-contract-file-name absolute-file
))
599 (mapcar (function (lambda (shadow)
600 (cons absolute-file shadow
)))
603 canonical-file shadow-literal-groups nil
)
605 canonical-file shadow-regexp-groups t
)))))
606 (set (intern file shadow-hashtable
) shadows
))))
608 (defun shadow-shadows-of-1 (file groups regexp
)
609 "Return list of FILE's shadows in GROUPS,
610 which are considered as regular expressions if third arg REGEXP is true."
614 (function (lambda (x) (shadow-file-match x file regexp
)))
616 (append (cond ((equal nonmatching
(car groups
)) nil
)
618 (let ((realpath (nth 2 (shadow-parse-fullpath file
))))
622 (shadow-replace-path-component x realpath
)))
625 (shadow-shadows-of-1 file
(cdr groups
) regexp
)))))
627 (defun shadow-add-to-todo ()
628 "If current buffer has shadows, add them to the list
629 of files needing to be copied."
630 (let ((shadows (shadow-shadows-of
631 (shadow-expand-file-name
632 (buffer-file-name (current-buffer))))))
634 (setq shadow-files-to-copy
635 (shadow-union shadows shadow-files-to-copy
))
636 (shadow-when (not shadow-inhibit-message
)
637 (message "%s" (substitute-command-keys
638 "Use \\[shadow-copy-files] to update shadows."))
640 (shadow-write-todo-file)))
641 nil
) ; Return nil for write-file-hooks
643 (defun shadow-remove-from-todo (pair)
644 "Remove PAIR from shadow-files-to-copy.
645 PAIR must be (eq to) one of the elements of that list."
646 (setq shadow-files-to-copy
647 (shadow-remove-if (function (lambda (s) (eq s pair
)))
648 shadow-files-to-copy
)))
650 (defun shadow-read-files ()
651 "Visits and loads shadow-info-file and shadow-todo-file,
652 thus restoring shadowfile's state from your last emacs session.
653 Returns t unless files were locked; then returns nil."
655 (if (and (fboundp 'file-locked-p
)
656 (or (stringp (file-locked-p shadow-info-file
))
657 (stringp (file-locked-p shadow-todo-file
))))
659 (message "Shadowfile is running in another emacs; can't have two.")
664 (shadow-when shadow-info-file
665 (set-buffer (setq shadow-info-buffer
666 (find-file-noselect shadow-info-file
)))
667 (shadow-when (and (not (buffer-modified-p))
668 (file-newer-than-file-p (make-auto-save-file-name)
671 (message "Data recovered from %s."
672 (car (insert-file-contents (make-auto-save-file-name))))
674 (eval-current-buffer))
675 (shadow-when shadow-todo-file
676 (set-buffer (setq shadow-todo-buffer
677 (find-file-noselect shadow-todo-file
)))
678 (shadow-when (and (not (buffer-modified-p))
679 (file-newer-than-file-p (make-auto-save-file-name)
682 (message "Data recovered from %s."
683 (car (insert-file-contents (make-auto-save-file-name))))
685 (eval-current-buffer nil
))
686 (shadow-invalidate-hashtable))
689 (defun shadow-write-info-file ()
690 "Write out information to shadow-info-file.
691 Also clears shadow-hashtable, since when there are new shadows defined, the old
692 hashtable info is invalid."
693 (shadow-invalidate-hashtable)
696 (if (not shadow-info-buffer
)
697 (setq shadow-info-buffer
(find-file-noselect shadow-info-file
)))
698 (set-buffer shadow-info-buffer
)
699 (delete-region (point-min) (point-max))
700 (shadow-insert-var 'shadow-clusters
)
701 (shadow-insert-var 'shadow-literal-groups
)
702 (shadow-insert-var 'shadow-regexp-groups
))))
704 (defun shadow-write-todo-file (&optional save
)
705 "Write out information to shadow-todo-file.
706 With nonnil argument also saves the buffer."
708 (if (not shadow-todo-buffer
)
709 (setq shadow-todo-buffer
(find-file-noselect shadow-todo-file
)))
710 (set-buffer shadow-todo-buffer
)
711 (delete-region (point-min) (point-max))
712 (shadow-insert-var 'shadow-files-to-copy
)
713 (if save
(shadow-save-todo-file))))
715 (defun shadow-save-todo-file ()
716 (if (and shadow-todo-buffer
(buffer-modified-p shadow-todo-buffer
))
718 (set-buffer shadow-todo-buffer
)
719 (condition-case nil
; have to continue even in case of
720 (basic-save-buffer) ; error, otherwise kill-emacs might
722 (message "WARNING: Can't save shadow todo file; it is locked!")
725 (defun shadow-invalidate-hashtable ()
726 (setq shadow-hashtable
(make-vector 37 0)))
728 (defun shadow-insert-var (variable)
729 "Prettily insert a setq command for VARIABLE.
730 which, when later evaluated, will restore it to its current setting.
731 SYMBOL must be the name of a variable whose value is a list."
732 (let ((standard-output (current-buffer)))
733 (insert (format "(setq %s" variable
))
734 (cond ((consp (eval variable
))
736 (prin1 (car (eval variable
)))
737 (let ((rest (cdr (eval variable
))))
741 (setq rest
(cdr rest
)))
744 (prin1 (eval variable
))
747 (defun shadow-save-buffers-kill-emacs (&optional arg
)
748 "Offer to save each buffer and copy shadows, then kill this Emacs process.
749 With prefix arg, silently save all file-visiting buffers, then kill.
751 Extended by shadowfile to automatically save `shadow-todo-file' and
752 look for files that have been changed and need to be copied to other systems."
753 ;; This function is necessary because we need to get control and save
754 ;; the todo file /after/ saving other files, but /before/ the warning
755 ;; message about unsaved buffers (because it can get modified by the
756 ;; action of saving other buffers). `kill-emacs-hook' is no good
757 ;; because it is not called at the correct time, and also because it is
758 ;; called when the terminal is disconnected and we cannot ask whether
761 (shadow-save-todo-file)
762 (save-some-buffers arg t
)
764 (shadow-save-todo-file)
765 (and (or (not (memq t
(mapcar (function
766 (lambda (buf) (and (buffer-file-name buf
)
767 (buffer-modified-p buf
))))
769 (yes-or-no-p "Modified buffers exist; exit anyway? "))
770 (or (not (fboundp 'process-list
))
771 ;; process-list is not defined on VMS.
772 (let ((processes (process-list))
775 (and (memq (process-status (car processes
)) '(run stop open
))
776 (let ((val (process-kill-without-query (car processes
))))
777 (process-kill-without-query (car processes
) val
)
780 (setq processes
(cdr processes
)))
782 (yes-or-no-p "Active processes exist; kill them and exit anyway? "))))
785 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
786 ;;; Lucid Emacs compatibility
787 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
789 ;; This is on hold until someone tells me about a working version of
790 ;; map-ynp for Lucid Emacs.
792 ;(shadow-when (string-match "Lucid" emacs-version)
793 ; (require 'symlink-fix)
794 ; (require 'ange-ftp)
796 ; (if (not (fboundp 'file-truename))
797 ; (fset 'shadow-expand-file-name
798 ; (symbol-function 'symlink-expand-file-name)))
799 ; (if (not (fboundp 'ange-ftp-ftp-name))
800 ; (fset 'ange-ftp-ftp-name
801 ; (symbol-function 'ange-ftp-ftp-path))))
803 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
805 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
807 ;;; File shadowing is activated at load time, unless this this file is
808 ;;; being preloaded, in which case it is added to after-init-hook.
809 ;;; Thanks to Richard Caley for this scheme.
811 (defun shadow-initialize ()
812 (if (null shadow-homedir
)
814 (file-name-as-directory (shadow-expand-file-name "~"))))
815 (if (null shadow-info-file
)
816 (setq shadow-info-file
817 (shadow-expand-file-name "~/.shadows")))
818 (if (null shadow-todo-file
)
819 (setq shadow-todo-file
820 (shadow-expand-file-name "~/.shadow_todo")))
821 (if (not (shadow-read-files))
823 (message "Shadowfile information files not found - aborting")
826 (shadow-when (and (not shadow-inhibit-overload
)
827 (not (fboundp 'shadow-orig-save-buffers-kill-emacs
)))
828 (fset 'shadow-orig-save-buffers-kill-emacs
829 (symbol-function 'save-buffers-kill-emacs
))
830 (fset 'save-buffers-kill-emacs
831 (symbol-function 'shadow-save-buffers-kill-emacs
)))
832 (add-hook 'write-file-hooks
'shadow-add-to-todo
)
833 (define-key ctl-x-4-map
"s" 'shadow-copy-files
)))
836 (add-hook 'after-init-hook
'shadow-initialize
)
840 ;;;eval:(put 'shadow-when 'lisp-indent-hook 1)
843 ;;; shadowfile.el ends here