1 ;;; shadowfile.el --- automatic file copying
3 ;; Copyright (C) 1993-1994, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: Boris Goldowsky <boris@gnu.org>
6 ;; Keywords: comm files
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 3 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
25 ;; This package helps you to keep identical copies of files in more than one
26 ;; place - possibly on different machines. When you save a file, it checks
27 ;; whether it is on the list of files with "shadows", and if so, it tries to
28 ;; copy it when you exit Emacs (or use the shadow-copy-files command).
30 ;; Installation & Use:
32 ;; Add clusters (if necessary) and file groups with shadow-define-cluster,
33 ;; shadow-define-literal-group, and shadow-define-regexp-group (see the
34 ;; documentation for these functions for information on how and when to use
35 ;; them). After doing this once, everything should be automatic.
37 ;; The lists of clusters and shadows are saved in a ~/.emacs.d/shadows
38 ;; (`shadow-info-file') file, so that they can be remembered from one
39 ;; Emacs session to another, even (as much as possible) if the Emacs
40 ;; session terminates abnormally. The files needing to be copied are
41 ;; stored in `shadow-todo-file'; if a file cannot be copied for any
42 ;; reason, it will stay on the list to be tried again next time. The
43 ;; `shadow-info-file' file should itself have shadows on all your accounts
44 ;; so that the information in it is consistent everywhere, but
45 ;; `shadow-todo-file' is local information and should have no shadows.
47 ;; If you do not want to copy a particular file, you can answer "no" and
48 ;; be asked again next time you hit C-x 4 s or exit Emacs. If you do not
49 ;; want to be asked again, use shadow-cancel, and you will not be asked
50 ;; until you change the file and save it again. If you do not want to
51 ;; shadow that file ever again, you can edit it out of the shadows
52 ;; buffer. Anytime you edit the shadows buffer, you must type M-x
53 ;; shadow-read-files to load in the new information, or your changes will
58 ;; - It is bad to have two emacses both running shadowfile at the same
59 ;; time. It tries to detect this condition, but is not always successful.
61 ;; - You have to be careful not to edit a file in two locations
62 ;; before shadowfile has had a chance to copy it; otherwise
63 ;; "updating shadows" will overwrite one of the changed versions.
65 ;; - It ought to check modification times of both files to make sure
66 ;; it is doing the right thing. This will have to wait until
67 ;; file-newer-than-file-p works between machines.
69 ;; - It will not make directories for you, it just fails to copy files
70 ;; that belong in non-existent directories.
72 ;; Please report any bugs to me (boris@gnu.org). Also let me know
73 ;; if you have suggestions or would like to be informed of updates.
81 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
83 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
86 "Automatic file copying when saving a file."
88 :link '(emacs-commentary-link "shadowfile")
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 non-nil, do not display a message when a file needs copying."
103 (defcustom shadow-inhibit-overload nil
104 "If non-nil, shadowfile won't redefine \\[save-buffers-kill-emacs].
105 Normally it overloads the function `save-buffers-kill-emacs' to check for
106 files that have been changed and need to be copied to other systems."
110 ;; FIXME in a sense, this changed in 24.4 (addition of locate-user-emacs-file),
111 ;; but due to the weird way this variable is initialized to nil, it didn't
112 ;; literally change. Same for shadow-todo-file.
113 (defcustom shadow-info-file nil
114 "File to keep shadow information in.
115 The `shadow-info-file' should be shadowed to all your accounts to
116 ensure consistency. Default: ~/.emacs.d/shadows"
117 :type '(choice (const nil) file)
120 ;; FIXME use .emacs.d
121 (defcustom shadow-todo-file nil
122 "File to store the list of uncopied shadows in.
123 This means that if a remote system is down, or for any reason you cannot or
124 decide not to copy your shadow files at the end of one Emacs session, it will
125 remember and ask you again in your next Emacs session.
126 This file must NOT be shadowed to any other system, it is host-specific.
127 Default: ~/.emacs.d/shadow_todo"
128 :type '(choice (const nil) file)
132 ;;; The following two variables should in most cases initialize themselves
133 ;;; correctly. They are provided as variables in case the defaults are wrong
134 ;;; on your machine (and for efficiency).
136 (defvar shadow-system-name (system-name)
137 "The complete hostname of this machine.")
139 (defvar shadow-homedir nil
140 "Your home directory on this machine.")
143 ;;; Internal variables whose values are stored in the info and todo files:
146 (defvar shadow-clusters nil
147 "List of host clusters (see `shadow-define-cluster').")
149 (defvar shadow-literal-groups nil
150 "List of files that are shared between hosts.
151 This list contains shadow structures with literal filenames, created by
152 `shadow-define-literal-group'.")
154 (defvar shadow-regexp-groups nil
155 "List of file types that are shared between hosts.
156 This list contains shadow structures with regexps matching filenames,
157 created by `shadow-define-regexp-group'.")
160 ;;; Other internal variables:
163 (defvar shadow-files-to-copy nil) ; List of files that need to
164 ; be copied to remote hosts.
166 (defvar shadow-hashtable nil) ; for speed
168 (defvar shadow-info-buffer nil) ; buf visiting shadow-info-file
169 (defvar shadow-todo-buffer nil) ; buf visiting shadow-todo-file
171 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
172 ;;; Syntactic sugar; General list and string manipulation
173 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
175 (defun shadow-union (a b)
176 "Add members of list A to list B if not equal to items already in B."
179 (if (member (car a) b)
180 (shadow-union (cdr a) b)
181 (shadow-union (cdr a) (cons (car a) b)))))
183 (defun shadow-find (func list)
184 "If FUNC applied to some element of LIST is non-nil, return first such element."
185 (while (and list (not (funcall func (car list))))
186 (setq list (cdr list)))
189 (defun shadow-regexp-superquote (string)
190 "Like `regexp-quote', but includes the ^ and $.
191 This makes sure regexp matches nothing but STRING."
192 (concat "^" (regexp-quote string) "$"))
194 (defun shadow-suffix (prefix string)
195 "If PREFIX begins STRING, return the rest.
196 Return value is non-nil if PREFIX and STRING are `string=' up to the length of
198 (let ((lp (length prefix))
199 (ls (length string)))
201 (string= prefix (substring string 0 lp)))
202 (substring string lp))))
204 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
205 ;;; Clusters and sites
206 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
208 ;;; I use the term `site' to refer to a string which may be the name of a
209 ;;; cluster or a literal hostname. All user-level commands should accept
212 (defun shadow-make-cluster (name primary regexp)
213 "Create a shadow cluster.
214 It is called NAME, uses the PRIMARY hostname and REGEXP matching all
215 hosts in the cluster. The variable `shadow-clusters' associates the
216 names of clusters to these structures. This function is for program
217 use: to create clusters interactively, use `shadow-define-cluster'
219 (list name primary regexp))
221 (defmacro shadow-cluster-name (cluster)
222 "Return the name of the CLUSTER."
223 (list 'elt cluster 0))
225 (defmacro shadow-cluster-primary (cluster)
226 "Return the primary hostname of a CLUSTER."
227 (list 'elt cluster 1))
229 (defmacro shadow-cluster-regexp (cluster)
230 "Return the regexp matching hosts in a CLUSTER."
231 (list 'elt cluster 2))
233 (defun shadow-set-cluster (name primary regexp)
234 "Put cluster NAME on the list of clusters.
235 Replace old definition, if any. PRIMARY and REGEXP are the
236 information defining the cluster. For interactive use, call
237 `shadow-define-cluster' instead."
238 (let ((rest (cl-remove-if (lambda (x) (equal name (car x)))
240 (setq shadow-clusters
241 (cons (shadow-make-cluster name primary regexp)
244 (defmacro shadow-get-cluster (name)
245 "Return cluster named NAME, or nil."
246 (list 'assoc name 'shadow-clusters))
248 (defun shadow-site-primary (site)
249 "If SITE is a cluster, return primary host, otherwise return SITE."
250 (let ((c (shadow-get-cluster site)))
252 (shadow-cluster-primary c)
257 (defun shadow-site-cluster (site)
258 "Given a SITE (hostname or cluster name), return cluster it is in, or nil."
259 (or (assoc site shadow-clusters)
261 (function (lambda (x)
262 (string-match (shadow-cluster-regexp x)
266 (defun shadow-read-site ()
267 "Read a cluster name or hostname from the minibuffer."
268 (let ((ans (completing-read "Host or cluster name [RET when done]: "
274 (defun shadow-site-match (site1 site2)
275 "Non-nil if SITE1 is or includes SITE2.
276 Each may be a host or cluster name; if they are clusters, regexp of SITE1 will
277 be matched against the primary of SITE2."
278 (or (string-equal site1 site2) ; quick check
279 (let* ((cluster1 (shadow-get-cluster site1))
280 (primary2 (shadow-site-primary site2)))
282 (string-match (shadow-cluster-regexp cluster1) primary2)
283 (string-equal site1 primary2)))))
285 (defun shadow-get-user (site)
286 "Return the default username for a SITE."
287 (ange-ftp-get-user (shadow-site-primary site)))
289 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
290 ;;; Filename manipulation
291 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
293 (defun shadow-parse-fullname (fullname)
294 "Parse FULLNAME into (site user path) list.
295 Leave it alone if it already is one. Return nil if the argument is
296 not a full ange-ftp pathname."
299 (ange-ftp-ftp-name fullname)))
301 (defun shadow-parse-name (name)
302 "Parse any NAME into (site user name) list.
303 Argument can be a simple name, full ange-ftp name, or already a hup list."
304 (or (shadow-parse-fullname name)
305 (list shadow-system-name
309 (defsubst shadow-make-fullname (host user name)
310 "Make an ange-ftp style fullname out of HOST, USER (optional), and NAME.
311 This is probably not as general as it ought to be."
313 (if user (concat user "@"))
317 (defun shadow-replace-name-component (fullname newname)
318 "Return FULLNAME with the name component changed to NEWNAME."
319 (let ((hup (shadow-parse-fullname fullname)))
320 (shadow-make-fullname (nth 0 hup) (nth 1 hup) newname)))
322 (defun shadow-local-file (file)
323 "If FILE is at this site, remove /user@host part.
324 If refers to a different system or a different user on this system,
326 (let ((hup (shadow-parse-fullname file)))
327 (cond ((null hup) file)
328 ((and (shadow-site-match (nth 0 hup) shadow-system-name)
329 (string-equal (nth 1 hup) (user-login-name)))
333 (defun shadow-expand-cluster-in-file-name (file)
334 "If hostname part of FILE is a cluster, expand it to cluster's primary hostname.
335 Will return the name bare if it is a local file."
336 (let ((hup (shadow-parse-name file)))
337 (cond ((null hup) file)
338 ((shadow-local-file hup))
339 ((shadow-make-fullname (shadow-site-primary (nth 0 hup))
343 (defun shadow-expand-file-name (file &optional default)
344 "Expand file name and get FILE's true name."
345 (file-truename (expand-file-name file default)))
347 (defun shadow-contract-file-name (file)
349 Do so by replacing (when possible) home directory with ~, and hostname
350 with cluster name that includes it. Filename should be absolute and
352 (let* ((hup (shadow-parse-name file))
353 (homedir (if (shadow-local-file hup)
355 (file-name-as-directory
356 (nth 2 (shadow-parse-fullname
358 (shadow-make-fullname
359 (nth 0 hup) (nth 1 hup) "~")))))))
360 (suffix (shadow-suffix homedir (nth 2 hup)))
361 (cluster (shadow-site-cluster (nth 0 hup))))
362 (shadow-make-fullname
364 (shadow-cluster-name cluster)
371 (defun shadow-same-site (pattern file)
372 "True if the site of PATTERN and of FILE are on the same site.
373 If usernames are supplied, they must also match exactly. PATTERN and FILE may
374 be lists of host, user, name, or ange-ftp file names. FILE may also be just a
376 (let ((pattern-sup (shadow-parse-fullname pattern))
377 (file-sup (shadow-parse-name file)))
379 (shadow-site-match (nth 0 pattern-sup) (nth 0 file-sup))
380 (or (null (nth 1 pattern-sup))
381 (string-equal (nth 1 pattern-sup) (nth 1 file-sup))))))
383 (defun shadow-file-match (pattern file &optional regexp)
384 "Return t if PATTERN matches FILE.
385 If REGEXP is supplied and non-nil, the file part of the pattern is a regular
386 expression, otherwise it must match exactly. The sites and usernames must
387 match---see `shadow-same-site'. The pattern must be in full ange-ftp format,
388 but the file can be any valid filename. This function does not do any
389 filename expansion or contraction, you must do that yourself first."
390 (let* ((pattern-sup (shadow-parse-fullname pattern))
391 (file-sup (shadow-parse-name file)))
392 (and (shadow-same-site pattern-sup file-sup)
394 (string-match (nth 2 pattern-sup) (nth 2 file-sup))
395 (string-equal (nth 2 pattern-sup) (nth 2 file-sup))))))
397 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
398 ;;; User-level Commands
399 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
402 (defun shadow-define-cluster (name)
403 "Edit (or create) the definition of a cluster NAME.
404 This is a group of hosts that share directories, so that copying to or from
405 one of them is sufficient to update the file on all of them. Clusters are
406 defined by a name, the network address of a primary host (the one we copy
407 files to), and a regular expression that matches the hostnames of all the
408 sites in the cluster."
409 (interactive (list (completing-read "Cluster name: " shadow-clusters () ())))
410 (let* ((old (shadow-get-cluster name))
411 (primary (read-string "Primary host: "
412 (if old (shadow-cluster-primary old)
414 (regexp (let (try-regexp)
419 "Regexp matching all host names: "
420 (if old (shadow-cluster-regexp old)
421 (shadow-regexp-superquote primary))))
423 (message "Regexp doesn't include the primary host!")
426 ; (username (read-no-blanks-input
427 ; (format "Username (default %s): "
428 ; (shadow-get-user primary))
429 ; (if old (or (shadow-cluster-username old) "")
430 ; (user-login-name))))
432 ; (if (string-equal "" username) (setq username nil))
433 (shadow-set-cluster name primary regexp)))
436 (defun shadow-define-literal-group ()
437 "Declare a single file to be shared between sites.
438 It may have different filenames on each site. When this file is edited, the
439 new version will be copied to each of the other locations. Sites can be
440 specific hostnames, or names of clusters (see `shadow-define-cluster')."
442 (let ((name (if (buffer-file-name)
443 (nth 2 (shadow-parse-fullname
444 (shadow-contract-file-name (buffer-file-name))))))
446 (while (setq site (shadow-read-site))
447 ;; FIXME fix read-string calls
448 (setq user (read-string (format "Username (default %s): "
449 (shadow-get-user site)))
450 name (read-string "Filename: " nil nil name))
451 (if (zerop (length name))
452 (error "You must specify a filename"))
453 (setq group (cons (shadow-make-fullname site
454 (if (string-equal "" user)
455 (shadow-get-user site)
459 (setq shadow-literal-groups (cons group shadow-literal-groups)))
460 (shadow-write-info-file))
463 (defun shadow-define-regexp-group ()
464 "Make each of a group of files be shared between hosts.
465 Prompts for regular expression; files matching this are shared between a list
466 of sites, which are also prompted for. The filenames must be identical on all
467 hosts (if they aren't, use `shadow-define-literal-group' instead of this
468 function). Each site can be either a hostname or the name of a cluster (see
469 `shadow-define-cluster')."
471 (let ((regexp (read-string
473 (if (buffer-file-name)
474 (shadow-regexp-superquote
477 (shadow-contract-file-name
478 (buffer-file-name))))))))
479 site sites usernames)
480 (while (setq site (shadow-read-site))
481 (setq sites (cons site sites))
483 (cons (read-string (format "Username for %s: " site)
484 (shadow-get-user site))
486 (setq shadow-regexp-groups
487 (cons (shadow-make-group regexp sites usernames)
488 shadow-regexp-groups))
489 (shadow-write-info-file)))
491 (defun shadow-shadows ()
492 ;; Mostly for debugging.
493 "Interactive function to display shadows of a buffer."
495 (let ((msg (mapconcat #'cdr (shadow-shadows-of (buffer-file-name)) " ")))
497 (if (zerop (length msg))
501 (defun shadow-copy-files (&optional arg)
502 "Copy all pending shadow files.
503 With prefix argument, copy all pending files without query.
504 Pending copies are stored in variable `shadow-files-to-copy', and in
505 `shadow-todo-file' if necessary. This function is invoked by
506 `shadow-save-buffers-kill-emacs', so it is not usually necessary to
509 (if (not shadow-files-to-copy)
510 (if (called-interactively-p 'interactive)
511 (message "No files need to be shadowed."))
513 (map-y-or-n-p (function
515 (or arg shadow-noquery
516 (format "Copy shadow file %s? " (cdr pair)))))
517 (function shadow-copy-file)
519 '("shadow" "shadows" "copy"))
520 (shadow-write-todo-file t))))
522 (defun shadow-cancel ()
523 "Cancel the instruction to copy some files.
524 Prompts for which copy operations to cancel. You will not be asked to copy
525 them again, unless you make more changes to the files. To cancel a shadow
526 permanently, remove the group from `shadow-literal-groups' or
527 `shadow-regexp-groups'."
529 (map-y-or-n-p (function (lambda (pair)
530 (format "Cancel copying %s to %s? "
531 (car pair) (cdr pair))))
532 (function (lambda (pair)
533 (shadow-remove-from-todo pair)))
535 '("shadow" "shadows" "cancel copy"))
536 (message "There are %d shadows to be updated."
537 (length shadow-files-to-copy))
538 (shadow-write-todo-file))
540 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
541 ;;; Internal functions
542 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
544 (defun shadow-make-group (regexp sites usernames)
545 "Make a description of a file group---
546 actually a list of regexp ange-ftp file names---from REGEXP (name of file to
547 be shadowed), list of SITES, and corresponding list of USERNAMES for each
550 (cons (shadow-make-fullname (car sites) (car usernames) regexp)
551 (shadow-make-group regexp (cdr sites) (cdr usernames)))
554 (defun shadow-copy-file (s)
555 "Copy one shadow file."
557 (cond ((get-file-buffer
558 (abbreviate-file-name (shadow-expand-file-name (car s)))))
559 ((not (file-readable-p (car s)))
561 (format "Cannot find file %s--cancel copy request? "
563 (shadow-remove-from-todo s))
565 ((or (eq t shadow-noquery)
567 (format "No buffer for %s -- update shadow anyway? "
569 (find-file-noselect (car s)))))
570 (to (shadow-expand-cluster-in-file-name (cdr s))))
575 (write-region nil nil to)
576 (shadow-remove-from-todo s))
577 (error (message "Shadow %s not updated!" (cdr s)))))))
579 (defun shadow-shadows-of (file)
580 "Return copy operations needed to update FILE.
581 Filename should have clusters expanded, but otherwise can have any format.
582 Return value is a list of dotted pairs like (from . to), where from
583 and to are absolute file names."
584 (or (symbol-value (intern-soft file shadow-hashtable))
585 (let* ((absolute-file (shadow-expand-file-name
586 (or (shadow-local-file file) file)
588 (canonical-file (shadow-contract-file-name absolute-file))
590 (mapcar (function (lambda (shadow)
591 (cons absolute-file shadow)))
594 canonical-file shadow-literal-groups nil)
596 canonical-file shadow-regexp-groups t)))))
597 (set (intern file shadow-hashtable) shadows))))
599 (defun shadow-shadows-of-1 (file groups regexp)
600 "Return list of FILE's shadows in GROUPS.
601 Consider them as regular expressions if third arg REGEXP is true."
604 (cl-remove-if (lambda (x) (shadow-file-match x file regexp))
606 (append (cond ((equal nonmatching (car groups)) nil)
608 (let ((realname (nth 2 (shadow-parse-fullname file))))
612 (shadow-replace-name-component x realname)))
615 (shadow-shadows-of-1 file (cdr groups) regexp)))))
617 (defun shadow-add-to-todo ()
618 "If current buffer has shadows, add them to the list needing to be copied."
619 (let ((shadows (shadow-shadows-of
620 (shadow-expand-file-name
621 (buffer-file-name (current-buffer))))))
623 (setq shadow-files-to-copy
624 (shadow-union shadows shadow-files-to-copy))
625 (when (not shadow-inhibit-message)
626 (message "%s" (substitute-command-keys
627 "Use \\[shadow-copy-files] to update shadows."))
629 (shadow-write-todo-file)))
630 nil) ; Return nil for write-file-functions
632 (defun shadow-remove-from-todo (pair)
633 "Remove PAIR from `shadow-files-to-copy'.
634 PAIR must be `eq' to one of the elements of that list."
635 (setq shadow-files-to-copy
636 (cl-remove-if (lambda (s) (eq s pair)) shadow-files-to-copy)))
638 (defun shadow-read-files ()
639 "Visit and load `shadow-info-file' and `shadow-todo-file'.
640 Thus restores shadowfile's state from your last Emacs session.
641 Return t unless files were locked; then return nil."
643 (if (and (fboundp 'file-locked-p)
644 (or (stringp (file-locked-p shadow-info-file))
645 (stringp (file-locked-p shadow-todo-file))))
647 (message "Shadowfile is running in another Emacs; can't have two.")
652 (when shadow-info-file
653 (set-buffer (setq shadow-info-buffer
654 (find-file-noselect shadow-info-file)))
655 (when (and (not (buffer-modified-p))
656 (file-newer-than-file-p (make-auto-save-file-name)
659 (message "Data recovered from %s."
660 (car (insert-file-contents (make-auto-save-file-name))))
663 (when shadow-todo-file
664 (set-buffer (setq shadow-todo-buffer
665 (find-file-noselect shadow-todo-file)))
666 (when (and (not (buffer-modified-p))
667 (file-newer-than-file-p (make-auto-save-file-name)
670 (message "Data recovered from %s."
671 (car (insert-file-contents (make-auto-save-file-name))))
674 (shadow-invalidate-hashtable))
677 (defun shadow-write-info-file ()
678 "Write out information to `shadow-info-file'.
679 Also clear `shadow-hashtable', since when there are new shadows
680 defined, the old hashtable info is invalid."
681 (shadow-invalidate-hashtable)
684 (if (not shadow-info-buffer)
685 (setq shadow-info-buffer (find-file-noselect shadow-info-file)))
686 (set-buffer shadow-info-buffer)
687 (delete-region (point-min) (point-max))
688 (shadow-insert-var 'shadow-clusters)
689 (shadow-insert-var 'shadow-literal-groups)
690 (shadow-insert-var 'shadow-regexp-groups))))
692 (defun shadow-write-todo-file (&optional save)
693 "Write out information to `shadow-todo-file'.
694 With non-nil argument also saves the buffer."
696 (if (not shadow-todo-buffer)
697 (setq shadow-todo-buffer (find-file-noselect shadow-todo-file)))
698 (set-buffer shadow-todo-buffer)
699 (delete-region (point-min) (point-max))
700 (shadow-insert-var 'shadow-files-to-copy)
701 (if save (shadow-save-todo-file))))
703 (defun shadow-save-todo-file ()
704 (if (and shadow-todo-buffer (buffer-modified-p shadow-todo-buffer))
705 (with-current-buffer shadow-todo-buffer
706 (condition-case nil ; have to continue even in case of
707 (basic-save-buffer) ; error, otherwise kill-emacs might
709 (message "WARNING: Can't save shadow todo file; it is locked!")
712 (defun shadow-invalidate-hashtable ()
713 (setq shadow-hashtable (make-vector 37 0)))
715 (defun shadow-insert-var (variable)
716 "Build a `setq' to restore VARIABLE.
717 Prettily insert a `setq' command which, when later evaluated,
718 will restore VARIABLE to its current setting.
719 VARIABLE must be the name of a variable whose value is a list."
720 (let ((standard-output (current-buffer)))
721 (insert (format "(setq %s" variable))
722 (cond ((consp (eval variable))
724 (prin1 (car (eval variable)))
725 (let ((rest (cdr (eval variable))))
729 (setq rest (cdr rest)))
732 (prin1 (eval variable))
735 (defun shadow-save-buffers-kill-emacs (&optional arg)
736 "Offer to save each buffer and copy shadows, then kill this Emacs process.
737 With prefix arg, silently save all file-visiting buffers, then kill.
739 Extended by shadowfile to automatically save `shadow-todo-file' and
740 look for files that have been changed and need to be copied to other systems."
741 ;; This function is necessary because we need to get control and save
742 ;; the todo file /after/ saving other files, but /before/ the warning
743 ;; message about unsaved buffers (because it can get modified by the
744 ;; action of saving other buffers). `kill-emacs-hook' is no good
745 ;; because it is not called at the correct time, and also because it is
746 ;; called when the terminal is disconnected and we cannot ask whether
749 (shadow-save-todo-file)
750 (save-some-buffers arg t)
752 (shadow-save-todo-file)
753 (and (or (not (memq t (mapcar (function
754 (lambda (buf) (and (buffer-file-name buf)
755 (buffer-modified-p buf))))
757 (yes-or-no-p "Modified buffers exist; exit anyway? "))
758 (or (not (fboundp 'process-list))
759 ;; process-list is not defined on MSDOS.
760 (let ((processes (process-list))
763 (and (memq (process-status (car processes)) '(run stop open listen))
764 (process-query-on-exit-flag (car processes))
766 (setq processes (cdr processes)))
768 (yes-or-no-p "Active processes exist; kill them and exit anyway? "))))
771 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
772 ;;; Lucid Emacs compatibility
773 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
775 ;; This is on hold until someone tells me about a working version of
776 ;; map-ynp for Lucid Emacs.
778 ;(when (string-match "Lucid" emacs-version)
779 ; (require 'symlink-fix)
780 ; (require 'ange-ftp)
782 ; (if (not (fboundp 'file-truename))
783 ; (fset 'shadow-expand-file-name
784 ; (symbol-function 'symlink-expand-file-name)))
785 ; (if (not (fboundp 'ange-ftp-ftp-name))
786 ; (fset 'ange-ftp-ftp-name
787 ; (symbol-function 'ange-ftp-ftp-name))))
789 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
791 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
794 (defun shadow-initialize ()
795 "Set up file shadowing."
797 (if (null shadow-homedir)
799 (file-name-as-directory (shadow-expand-file-name "~"))))
800 (if (null shadow-info-file)
801 (setq shadow-info-file
802 ;; FIXME: Move defaults to their defcustom.
803 (shadow-expand-file-name
804 (locate-user-emacs-file "shadows" ".shadows"))))
805 (if (null shadow-todo-file)
806 (setq shadow-todo-file
807 (shadow-expand-file-name
808 (locate-user-emacs-file "shadow_todo" ".shadow_todo"))))
809 (if (not (shadow-read-files))
811 (message "Shadowfile information files not found - aborting")
814 (when (and (not shadow-inhibit-overload)
815 (not (fboundp 'shadow-orig-save-buffers-kill-emacs)))
816 (defalias 'shadow-orig-save-buffers-kill-emacs
817 (symbol-function 'save-buffers-kill-emacs))
818 (defalias 'save-buffers-kill-emacs 'shadow-save-buffers-kill-emacs))
819 (add-hook 'write-file-functions 'shadow-add-to-todo)
820 (define-key ctl-x-4-map "s" 'shadow-copy-files)))
822 (defun shadowfile-unload-function ()
823 (substitute-key-definition 'shadow-copy-files nil ctl-x-4-map)
824 (when (fboundp 'shadow-orig-save-buffers-kill-emacs)
825 (fset 'save-buffers-kill-emacs
826 (symbol-function 'shadow-orig-save-buffers-kill-emacs)))
827 ;; continue standard unloading
830 (provide 'shadowfile)
832 ;;; shadowfile.el ends here