1 ;;; hmoccur.el --- Multi-buffer or multi-file regexp occurrence location.
3 ;; Copyright (C) 1991 Markus Freericks
4 ;; Copyright (C) 1991-1995, 2008 Free Software Foundation, Inc.
5 ;; Developed with support from Motorola Inc.
7 ;; Author: Bob Weiner, Brown U.
8 ;; Maintainer: Mats Lidell <matsl@contactor.se>
9 ;; Keywords: hypermedia, matching
11 ;; This file is part of GNU Hyperbole.
13 ;; GNU Hyperbole is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 3, or (at
16 ;; your option) any later version.
18 ;; GNU Hyperbole is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; Modified by Bob Weiner to allow selection of a set of files within a
31 ;; single directory to search. By default, {M-x moccur RTN} searches
32 ;; current buffers with files attached.
34 ;; Date: 1 Aug 91 15:47:27 GMT
35 ;; From: mfx@cs.tu-berlin.de (Markus Freericks)
36 ;; Subject: moccur - multibuffer occurences
38 ;; While editing some dozen or so files, i had the dire need for
39 ;; something like 'occur' that can cope with multiple buffers. This has
40 ;; probably been done before; but still, here is my try at it. It seems
43 ;; How to use it: simple say
44 ;; M-x moccur <regexp>
45 ;; moccur then searches through *all buffers* currently existing that are
46 ;; bound to files and displays the occurences in a buffer that runs in
47 ;; Moccur-mode. Change to that buffer, scroll around, and say C-c C-c
48 ;; to jump to the occurrence. Quite simple.
50 ;; Incompatibilites to Occur mode:
51 ;; a) it browses through *all* buffers that have a file name
52 ;; associated with them; those may or may not include the current
53 ;; buffer. Especially, while standard occur works
54 ;; on 'all lines following point', Moccur does not.
55 ;; b) there is no support for the 'NLINE' argument.
58 ;; moccur <regexp> shows all occurences of <regexp> in all buffers
59 ;; currently existing that refer to files.
60 ;; the occurences are displayed in a buffer running in Moccur mode;
61 ;; C-c C-c gets you to the occurence
70 (defconst moccur-source-prefix "@loc> "
71 "Prefix for lines indicating source of matches.")
77 (defun moccur (regexp &optional file-regexp no-fold-search)
78 "Show all lines of all buffers containing a match for REGEXP.
79 With optional FILE-REGEXP, a pattern matching to files in a single
80 directory, search matching files rather than current buffers.
81 The lines are shown in a buffer named *Moccur* which serves as a menu to
82 find any of the occurrences in this buffer.
83 \\[describe-mode] in that buffer explains how."
84 (interactive "sRegexp to find occurrences of: \nsFiles to search (default current file buffers): ")
85 (if (equal file-regexp "") (setq file-regexp nil))
86 (let* ((buffers (if file-regexp (directory-files
88 (or (file-name-directory
90 'full (file-name-nondirectory file-regexp))
92 (occbuf (get-buffer-create "*Moccur*"))
96 (setq buffer-read-only nil)
99 (insert "Lines matching '" regexp "':\n\n")
100 (let ((currbuf) (currfile) (kill-buf))
102 (setq currbuf (car buffers)
103 currfile (if (stringp currbuf) currbuf)
104 kill-buf (and currfile (not (get-file-buffer currfile)))
105 buffers (cdr buffers))
107 (setq currbuf (find-file-noselect currfile))
108 (setq currfile (buffer-file-name currbuf)))
109 (if (or (not currfile) (not currbuf))
112 (let ((case-fold-search (not no-fold-search)))
114 (goto-char (point-min))
116 (while (re-search-forward regexp nil t)
117 (setq matches (+ matches 1))
118 (let* ((linenum (count-lines (point-min)(point)))
119 (tag (format "\n%4d:" linenum)))
123 (insert moccur-source-prefix currfile "\n")
124 (setq firstmatch nil)))
127 (forward-word -1) ;; needed if match goes to eoline
131 (append-to-buffer occbuf beg (point)))
135 (if (not firstmatch) (insert "\n\n"))
136 (if kill-buf (kill-buffer currbuf))))))
141 (if (fboundp 'outline-minor-mode)
142 (and (progn (goto-char 1)
143 (search-forward "\C-m" nil t))
144 (outline-minor-mode 1)))
145 (goto-char (point-min))
146 (pop-to-buffer occbuf)
147 (message "%d matches." matches)
149 (message "No matches.")
153 "Go to the line where this occurrence was found."
155 (if (not (eq major-mode 'moccur-mode))
156 (error "'moccur-to' must be called within a moccur buffer.")
165 (setq line (buffer-substring beg (point)))
166 (if (string-match "^[ ]*[0-9]+:" line)
168 (setq lineno (string-to-number (substring
169 line 0 (match-end 0))))
170 (if (re-search-backward
171 (concat "^" moccur-source-prefix
172 "\"?\\([^\" \n]+\\)\"?") nil t)
174 (setq line (buffer-substring
175 (match-beginning 1) (match-end 1))
176 dstbuf (find-file-noselect line))
179 "moccur-to: file '%s' is not readable" line)))
180 (error "No moccur header line for file.")))
181 (error "Not an moccur occurrence line.")))
182 (if (and lineno dstbuf)
184 (message "Selection <%s> line %d." line lineno)
185 (pop-to-buffer dstbuf)
186 (goto-line lineno))))))
188 (fset 'moccur-mode-goto-occurrence 'moccur-to)
192 ;;; Private functions
195 (defun moccur-mode ()
196 "Major mode for output from \\[moccur].
197 Move point to one of the occurrences in this buffer,
198 then use \\[moccur-to] to go to the same occurrence
199 in the buffer that the occurrenc was found in.
201 (kill-all-local-variables)
202 (use-local-map moccur-mode-map)
203 (setq major-mode 'moccur-mode)
204 (setq mode-name "Moccur"))
207 ;;; Private variables
210 (defvar moccur-mode-map ())
213 (setq moccur-mode-map (make-sparse-keymap))
214 (define-key moccur-mode-map "\C-c\C-c" 'moccur-to)
215 (define-key moccur-mode-map " " 'moccur-to)
216 (define-key moccur-mode-map "\C-m" 'moccur-to)
221 ;;; hmoccur.el ends here