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