home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / gnu / emacs / sources / 906 < prev    next >
Encoding:
Text File  |  1992-12-23  |  8.2 KB  |  239 lines

  1. Newsgroups: gnu.emacs.sources
  2. Path: sparky!uunet!mcsun!sunic!dkuug!aud.dk!vaxc.aud.auc.dk!lbn
  3. From: lbn@hugin.dk (Lars Bo Nielsen)
  4. Subject: Re: REQUEST: find-multiple-buffers.el
  5. In-Reply-To: mjc4y@uvacs.cs.Virginia.EDU's message of 20 Dec 92 05: 49:56 GMT
  6. Message-ID: <LBN.92Dec23094516@hugin.hugin.dk>
  7. Sender: news@vaxc.aud.auc.dk (USENET News System)
  8. Reply-To: lbn@hugin.dk
  9. Organization: Hugin Expert, Ltd.
  10. References: <1992Dec20.054956.24937@murdoch.acc.Virginia.EDU>
  11. Date: Wed, 23 Dec 1992 08:45:16 GMT
  12. Lines: 225
  13.  
  14. In article <1992Dec20.054956.24937@murdoch.acc.Virginia.EDU> mjc4y@uvacs.cs.Virginia.EDU (Matt Conway) writes:
  15.    I'm looking for a function that I think is called
  16.    find-multiple-buffers.el. It allows a user to do a find-file on a
  17.    regular expression (to load all *.C files, for example.).
  18.  
  19.    Is there anyone here who knows where to find this function, or one
  20.    that does what I'm describing? 
  21.  
  22. I have some old code laying around that probably does what you are
  23. looking for.  Using this you do not have to quit your current emacs
  24. just to say:
  25.  
  26.     emacs *.c *.h
  27.  
  28. when you want to change something in all your source files.  Just
  29. call `re-find-file'. It will prompt for a directory (default current).
  30. and then let you specify a regular expression that the files you are
  31. going to load would match - you will even see which files match the
  32. regular expression. In the previous example you would just enter:
  33.  
  34.     \.[ch]$
  35.  
  36. to get the *.c and *.h files.
  37.  
  38. When you have finished editing, the function `re-remove-buffers' will
  39. remove all the buffers that was loaded in the previous call to
  40. `re-find-file'.
  41.  
  42. Hope this will do the job,
  43.  
  44. Merry Christmas and a Happy New Year,
  45.  
  46. Lars Bo Nielsen (lbn@hugin.dk)
  47.  
  48. ----cut here----cut here----cut here----cut here----cut here----
  49. ;; 
  50. ;; find-files.el - Find files matching reg-exp
  51. ;; 
  52. ;; Copyright (C) 1990 Lars Bo Nielsen (lbn@iesd.auc.dk).
  53. ;; 
  54. ;; This program is free software; you can redistribute it and/or
  55. ;; modify it under the terms of the GNU General Public License as
  56. ;; published by the Free Software Foundation.
  57. ;; 
  58. ;; This program is distributed in the hope that it will be useful,
  59. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  60. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  61. ;; See the GNU General Public License for more details.
  62. ;; 
  63. ;; Author          : Lars Bo Nielsen
  64. ;; Created On      : Wed Jan 17 14:31:31 1990
  65. ;; Last Modified By: Lars Bo Nielsen
  66. ;; Last Modified On: Tue Mar 27 18:59:25 1990
  67. ;; Update Count    : 66
  68. ;; 
  69. ;; HISTORY
  70. ;; 27-Mar-1990  (Last Mod: Wed Jan 24 15:17:22 1990 #65)  Lars Bo Nielsen
  71. ;;    're-find-file-display' has been changed to 'insert-list'
  72. ;; 19-Jan-1990  (Last Mod: Fri Jan 19 14:59:41 1990 #58)  Lars Bo Nielsen
  73. ;;    Added 're-remove-buffers' to remove the buffers. They are listed
  74. ;;    in 're-find-file-alist'.
  75. ;;    Also the function 're-find-file-display' has been added.
  76. ;; 19-Jan-1990  (Last Mod: Fri Jan 19 11:21:56 1990 #52)  Lars Bo Nielsen
  77. ;;    Reading '%' caused a bug, now two is inserted, and deleted.
  78. ;; 19-Jan-1990  (Last Mod: Fri Jan 19 09:00:29 1990 #40)  Lars Bo Nielsen
  79. ;;    Inhibit ^G inside the function. We catch it and kill the local
  80. ;;    buffers.
  81. ;;    I don't know how the normal keyboard commands (like ^X^B) is
  82. ;;    supposed to be catched.
  83. ;; 18-Jan-1990  (Last Mod: Thu Jan 18 20:35:05 1990 #35)  Lars Bo Nielsen
  84. ;;    Now it should be ok.
  85. ;; 
  86.  
  87. (defconst re-find-file-alist nil
  88.   "List of files found by resent success-full call to re-find-file.")
  89.  
  90. (defun re-find-file (directory)
  91.   (interactive "DFrom directory : ")
  92.   (let ((tmp-buf (generate-new-buffer " * re-find-file tmp-buffer * "))
  93.     (savedconfig (current-window-configuration))
  94.     (list nil)
  95.     (match-list nil)
  96.     (regexp-not-complete t)
  97.     (reg-exp-ok t)
  98.     (regexp-complain "")
  99.     (inhibit-quit t)
  100.     (match "")
  101.     (cur-buffer (current-buffer))
  102.     char
  103.     complete-buf)
  104.     (or (string-match "/$" directory)
  105.     (setq directory (concat directory "/")))
  106.     (setq complete-buf directory)
  107.     (set-buffer tmp-buf)        ; Output goes to tmp-buf
  108.     (erase-buffer)            ; erase it
  109.     (call-process "ls" nil t nil "-1A" (expand-file-name directory))
  110.     (goto-char (point-min))        ; Goto beginning of tmp-buf
  111.     (while (not (eobp))
  112.       (setq list
  113.         (cons (buffer-substring (point) (progn (end-of-line) (point)))
  114.           list))
  115.       (or (eobp) (forward-char 1)))
  116.     (switch-to-buffer complete-buf)
  117.     (delete-other-windows)
  118.     (while regexp-not-complete
  119.       (if reg-exp-ok
  120.       (let ((tmp-list list))
  121.         (setq match-list nil)
  122.         (erase-buffer)
  123.         (while tmp-list
  124.           (if (or (string-match match (car tmp-list))
  125.               (string= ""  match))
  126.           (setq match-list (cons (car tmp-list) match-list)))
  127.           (setq tmp-list (cdr tmp-list)))
  128.         (erase-buffer)
  129.         (insert-list match-list)))
  130.       (message (concat (format "%s file%s matching : "
  131.                    (if (eq (length match-list) 0) "No"
  132.                  (length match-list))
  133.                    (if (eq (length match-list) 1) "" "s"))
  134.                match regexp-complain))
  135.       (setq char (read-char))
  136.       (cond
  137.        ((eq char ?\C-g)            ; Just exit
  138.     (message "")
  139.     (kill-buffer complete-buf)
  140.     (kill-buffer tmp-buf)
  141.     (set-window-configuration savedconfig)
  142.     (signal 'quit nil))
  143.        ((eq char ?\C-?)            ; Delete last char
  144.     (if (eq (length match) 0)    ; Test if there is anything
  145.         (beep)            ; to delete
  146.       (if (string-match "%%$" match) ; Extra care with '%'
  147.           (setq match (substring match 0 (- (length match) 2)))
  148.         (setq match (substring match 0 (1- (length match)))))))
  149.        ((eq char ?\C-M)            ; Return was pressed
  150.     (setq regexp-not-complete nil))
  151.        ((eq char ?%)            ; Extra care with '%'
  152.     (setq match (concat match "%%")))
  153.        (t (setq match (concat match (char-to-string char)))))
  154.       (condition-case errorval
  155.       (progn            ; Test regexp
  156.         (looking-at match)        
  157.         (setq reg-exp-ok t regexp-complain ""))
  158.     (error (progn (setq regexp-complain " [Incomplete input]")
  159.               (setq regexp-not-complete t)
  160.               (setq reg-exp-ok nil)))))
  161.     (setq re-find-file-alist match-list)
  162.     (setq inhibit-quit nil)
  163.     (condition-case ()
  164.     (if (and match-list (yes-or-no-p "Find files? "))
  165.         (progn
  166.           (while match-list
  167.         (let* ((inhibit-quit nil) ; Allow quitting in here
  168.                (filename (car match-list))
  169.                (filefullname (concat directory filename)))
  170.           (if (file-directory-p filefullname)
  171.               (progn
  172.             (beep)
  173.             (if (y-or-n-p
  174.                  (concat "File \"" filename
  175.                      "\" is a directory. Read : "))
  176.                 (progn
  177.                   (message (concat "Getting " filename " ..."))
  178.                   (find-file filefullname))))
  179.             (message (concat "Getting " filename " ..."))
  180.             (find-file filefullname)))
  181.         (setq match-list (cdr match-list)))
  182.           (kill-buffer complete-buf)
  183.           (kill-buffer tmp-buf))
  184.       (message "")
  185.       (kill-buffer complete-buf)
  186.       (kill-buffer tmp-buf)
  187.       (set-window-configuration savedconfig))
  188.       (quit (progn
  189.           (beep)
  190.           (kill-buffer complete-buf)
  191.           (kill-buffer tmp-buf)
  192.           (set-window-configuration savedconfig))))))
  193.  
  194. (defun re-remove-buffers ()
  195.   (interactive)
  196.   (let ((buffer (get-buffer-create " *temp*"))
  197.     (list re-find-file-alist)
  198.     (cur-win (current-window-configuration)))
  199.     (if re-find-file-alist
  200.     (progn
  201.       (switch-to-buffer buffer)
  202.       (delete-other-windows)
  203.       (unwind-protect
  204.           (progn
  205.         (insert-list re-find-file-alist)
  206.         (if (yes-or-no-p "Delete buffers? ")
  207.             (progn
  208.               (message "Deleting buffers ...")
  209.               (while list
  210.             (condition-case ()
  211.                 (kill-buffer (car list))
  212.               (error ()))
  213.             (setq list (cdr list)))
  214.               (setq re-find-file-alist nil)
  215.               (message "Deleting buffers ... done"))))
  216.         (progn
  217.           (kill-buffer buffer)
  218.           (set-window-configuration cur-win))))
  219.       (kill-buffer buffer)
  220.       (error "No buffers to delete"))))
  221.  
  222. (defun insert-list (list &optional cols)
  223.   "Inserts, in current buffer, LIST in cols.
  224. Optional second arg COLS, default coloums.
  225. If second arg not supplied, as many colums as possible is made."
  226.   (let ((maxsize (if cols
  227.              (/ 80 cols)
  228.            (+ 2 (apply 'max (cons 10 (mapcar 'length list)))))))
  229.     (while list
  230.       (insert (car list))
  231.       (insert-char ?\  (- maxsize (length (car list))))
  232.       (if (> (current-column) (- 80 maxsize))
  233.       (progn
  234.         (just-one-space)
  235.         (insert "\n")))
  236.       (setq list (cdr list)))
  237.     (goto-char (point-min))))
  238.  
  239.