home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / misc / auto-carbon.el < prev    next >
Encoding:
Text File  |  1992-11-25  |  5.1 KB  |  140 lines

  1. ; Path: hal.com!olivea!uunet!pipex!demon!edscom!kevin
  2. ; From: kevin@edscom.demon.co.uk (Kevin Broadey)
  3. ; Newsgroups: gnu.emacs.sources
  4. ; Subject: Automatic file carbon-copying
  5. ; Date: 18 Nov 92 16:37:06 GMT
  6. ; Organization: EDS-Scicon, Milton Keynes, UK
  7. ; A while ago Ray Nickson <Ray.Nickson@comp.vuw.ac.nz> posted
  8. ; carbon-file.el (that's what it says in my file header, anyway!).  This
  9. ; sits in your `write-file-hooks' and automatically writes your buffer
  10. ; contents to one or more `carbon-copy' files every time you save it.
  11. ; I found this very useful, except that I often forgot to set up the
  12. ; carbon-copy file name.
  13. ; To get around this I wrote auto-carbon.el which sits in your
  14. ; `find-file-hooks'.  It uses a regular expression to trap files which you
  15. ; may want to carbon-copy and a "replace-match" string to generate the
  16. ; copy file name.
  17. ; This post includes carbon-file.el for those of you who haven't already
  18. ; got it.
  19. ; Bug reports, suggestions, improvements and praise to:-
  20. ;     kbroadey@edscom.demon.co.uk
  21.  
  22.  
  23. ; ------------------------------------------------------------------------
  24. ;; auto-carbon.el - automatically call carbon-buffer-to-file from
  25. ;;                  find-file-hooks
  26. ;;
  27. ;; Written 04-Nov-92 by Kevin Broadey <kbroadey@edscom.demon.co.uk>
  28.  
  29. ;; LCD Archive Entry:
  30. ;; auto-carbon|Kevin Broadey|kevin@edscom.demon.co.uk|
  31. ;; Automatically write buffer contents to "carbon copy" file on saves.|
  32. ;; 92-11-04||~/misc/auto-carbon.el.Z|
  33.  
  34. ;;
  35. ;; Usage:
  36. ;;
  37. ;;    (require 'auto-carbon)
  38. ;;    (or (memq 'auto-carbon find-file-hooks)
  39. ;;        (setq find-file-hooks (cons 'auto-carbon find-file-hooks)))
  40.  
  41. (provide 'auto-carbon)
  42.  
  43. (defvar auto-carbon-alist nil
  44.   "ALIST of source regexps and target patterns for automatic file carbon
  45. copying.
  46.  
  47. Each element looks like  (SOURCE . TARGET)  where SOURCE is a regular
  48. expression and TARGET is a  replace-match  compliant replacement string.
  49. This means that \\1 in TARGET is replaced by the first \\( ... \\) expression
  50. in SOURCE and \\& is replaced by the whole of SOURCE.
  51.  
  52. Note that SOURCE is not anchored by default, so you must use ^ and $ to
  53. anchor the match to the beginning or end of the file name.")
  54.  
  55. (defun auto-carbon ()
  56.   "Function for inclusion in `find-file-hooks' which uses `auto-carbon-alist'
  57. to determine whether to carbon-copy a file.
  58.  
  59. Calls `carbon-buffer-to-file' to arrange for carbon-copying."
  60.   (let ((alist auto-carbon-alist)
  61.     (orig-buffer-file-name buffer-file-name)
  62.     carbon-file-name)
  63.  
  64.     ;; Check whether buffer is visiting a file.  Error if not.
  65.     (or buffer-file-name
  66.     (error "Buffer is not visiting a file."))
  67.  
  68.     ;; Scan the alist looking for all matches
  69.     (while alist
  70.       (if (string-match (car (car alist)) orig-buffer-file-name)
  71.       ;; We've got a match.  Switch to a temporary buffer and use it to
  72.       ;; apply the target pattern to the source regexp using
  73.       ;; `replace-match'.  This does the "\&" and "\1" stuff for us.
  74.       ;; Let me know if you know of a version of replace-match that can be
  75.       ;; applied to a string!
  76.       (let ((orig-buf (current-buffer))
  77.         (buf (get-buffer-create " *auto-carbon-scratchpad* ")))
  78.         (set-buffer buf)
  79.         (widen)
  80.         (erase-buffer)
  81.         (insert orig-buffer-file-name)
  82.         (goto-char (point-min))
  83.         (re-search-forward (car (car alist))) ; sets up match data
  84.         (replace-match (cdr (car alist)) t nil)
  85.         (setq carbon-file-name (buffer-substring (point-min) (point-max)))
  86.         (set-buffer orig-buf)
  87.         (kill-buffer buf)
  88.  
  89.         ;; Ask whether to do the carbon copy.
  90.         ;; Note that we have to be back in the original buffer before we
  91.         ;; call carbon-buffer-to-file because it sets a buffer-local
  92.         ;; variable.
  93.         (if (y-or-n-p (format "Carbon copy to %s? " carbon-file-name))
  94.         (carbon-buffer-to-file carbon-file-name)
  95.           )))
  96.  
  97.       ;; Try next element is alist.
  98.       (setq alist (cdr alist)))))
  99. ------------------------------------------------------------------------
  100. ;;;carbon-file.el
  101. ;;;
  102. ;;;Authorizing-Users: Ray Nickson <Ray.Nickson@comp.vuw.ac.nz>
  103.  
  104. ;;;To use, just M-x carbon-buffer-to-file to the remote file name when
  105. ;;;you find the local one (or vice versa).
  106. ;;;(I had to chamge it for distribution; hope it still works)
  107.  
  108. ;;;You can also put the call in the file's Local Variables section with
  109. ;;;an eval, or just set buffer-carbon-file-names there.
  110.  
  111. (defvar buffer-carbon-file-names nil
  112.   "List of files to carbon-copy this buffer into.")
  113. (make-variable-buffer-local 'buffer-carbon-file-names)
  114.  
  115. (defun carbon-buffer-to-file (file)
  116.   "Make FILE be a carbon-copy of the file visited by this buffer.
  117. Any time you save the buffer, changes will go both to the buffer's own file
  118. and to FILE.  Yes, you can carbon to many files at once; the list of files
  119. being carbonned to is in the variable buffer-carbon-file-names."
  120.   (interactive "FCarbon to file: ")
  121.   (setq buffer-carbon-file-names (cons file buffer-carbon-file-names)))
  122.  
  123. (defun write-carbon-files ()
  124.   "A write-file-hook.  See \\[carbon-buffer-to-file]."
  125.   (save-restriction
  126.     (widen)
  127.     (mapcar
  128.      (function (lambda (file)
  129.        (write-region (point-min) (point-max) file)))
  130.      buffer-carbon-file-names))
  131.   nil) ; hook must return nil
  132.  
  133. (setq write-file-hooks (cons 'write-carbon-files write-file-hooks))
  134.