shadowfile.el 30 KB

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