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