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