shadowfile.el 30 KB

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