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