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