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