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