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