|
|
1.1 root 1: ;; Find occurrences of regexp and pick one from a menu.
2: ;; Copyright (C) 1985 Richard M. Stallman.
3: ;; based loosely on mocklisp original by Jeff Shrager and Duane Williams: CMU.
4:
5: ;; This file is part of GNU Emacs.
6:
7: ;; GNU Emacs is distributed in the hope that it will be useful,
8: ;; but WITHOUT ANY WARRANTY. No author or distributor
9: ;; accepts responsibility to anyone for the consequences of using it
10: ;; or for whether it serves any particular purpose or works at all,
11: ;; unless he says so in writing. Refer to the GNU Emacs General Public
12: ;; License for full details.
13:
14: ;; Everyone is granted permission to copy, modify and redistribute
15: ;; GNU Emacs, but only under the conditions described in the
16: ;; GNU Emacs General Public License. A copy of this license is
17: ;; supposed to have been given to you along with GNU Emacs so you
18: ;; can know your rights and responsibilities. It should be in a
19: ;; file named COPYING. Among other things, the copyright notice
20: ;; and this notice must be preserved on all copies.
21:
22:
23: (defun occur-menu (string)
24: "Show menu of lines containing match for REGEXP.
25: Enters recursive edit on text showing an entry for each matching line.
26: User can move to an entry and then exit with \\[exit-recursive-edit] to
27: move to the line in the original buffer described by the selected entry.
28: Abort with \\[abort-recursive-edit] to avoid moving in the original buffer.
29:
30: If REGEXP is empty then THE EXACT SAME menu is presented again,
31: with cursor initially at the next successive entry.
32: This is useful for stepping through located lines rapidly in order."
33: (interactive "sOccur menu (regexp): ")
34: (let (ln track-eol
35: (accumbuf (get-buffer-create " *Occur menu*"))
36: (databuf (current-buffer))
37: (prev (point-min))
38: (rebuild-summary (not (string-equal string ""))))
39: (if rebuild-summary
40: (progn
41: (save-excursion ; setup temp buffer
42: (set-buffer accumbuf)
43: (erase-buffer))
44: (save-excursion
45: (goto-char (point-min))
46: (setq ln 1) ; init accumulator
47: (while (re-search-forward string nil t)
48: (beginning-of-line)
49: (setq ln (+ ln (count-lines prev (point))))
50: (setq beg (point))
51: (setq prev (point))
52: (end-of-line) ; to pick up entire line
53: (setq end (point))
54: (save-excursion
55: (set-buffer accumbuf)
56: (insert (int-to-string ln) ". ")
57: (insert-buffer-substring databuf beg end)
58: (newline))
59: (forward-line 1))
60: (set-buffer accumbuf)
61: (set-buffer-modified-p nil))))
62: (goto-line
63: (save-window-excursion
64: (switch-to-buffer accumbuf)
65: (delete-other-windows)
66: (if rebuild-summary
67: (goto-char (point-min))
68: (forward-line 1))
69: (if (= (point-max) 1)
70: (error "No occurrences found."))
71: (message (substitute-command-keys "Use \\[exit-recursive-edit] to select line, \\[abort-recursive-edit] to abort."))
72: (let ((buffer-read-only t))
73: (recursive-edit))
74: (beginning-of-line)
75: (read accumbuf)))))
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.