]> code.delx.au - gnu-emacs-elpa/blob - packages/all/all.el
Add f90-interface-browser.
[gnu-emacs-elpa] / packages / all / all.el
1 ;;; all.el --- Edit all lines matching a given regexp
2
3 ;; Copyright (C) 1985-1987, 1992, 1994, 2011 Free Software Foundation, Inc.
4 ;; Copyright (C) 1994 Per Abrahamsen
5
6 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7 ;; Version: 1.0
8 ;; Keywords: matching
9
10 ;; LCD Archive Entry:
11 ;; all|Per Abrahamsen|abraham@dina.kvl.dk|
12 ;; Edit all lines matching a given regexp|
13 ;; $Date: 1997/03/04 10:29:42 $|$Revision: 5.2 $|~/misc/all.Z|
14
15 ;; This program is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 3, or (at your option)
18 ;; any later version.
19 ;;
20 ;; This program is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
24 ;;
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28 ;;; Commentary:
29
30 ;; Just like occur, except that changes in the *All* buffer are
31 ;; propagated to the original buffer.
32
33 ;; You can no longer use mouse-2 to find a match in the original file,
34 ;; since the default definition of mouse is too useful.
35 ;; However, `C-c C-c' still works.
36
37 ;; Line numbers are not listed in the *All* buffer.
38
39 ;; Ok, it is _not_ just like occur.
40
41 ;; Some limitations:
42
43 ;; - Undo in the *All* buffer is an ordinary change in the original.
44 ;; - Changes to the original buffer are not reflected in the *All* buffer.
45 ;; - A single change in the *All* buffer must be limited to a single match.
46
47 ;;; Code:
48
49 (defvar all-mode-map
50 (let ((map (make-sparse-keymap)))
51 (define-key map "\C-c\C-c" 'all-mode-goto)
52 map))
53
54 (defvar all-buffer nil)
55 (make-variable-buffer-local 'all-buffer)
56
57 (define-derived-mode all-mode fundamental-mode "All"
58 "Major mode for output from \\[all].
59
60 All changes made in this buffer will be propagated to the buffer where
61 you ran \\[all].
62
63 Press \\[all-mode-goto] to go to the same spot in the original buffer."
64 (add-hook 'before-change-functions 'all-before-change-function nil 'local)
65 (add-hook 'after-change-functions 'all-after-change-function nil 'local))
66
67 (defun all-mode-find (pos)
68 ;; Find position in original buffer corresponding to POS.
69 (let ((overlay (all-mode-find-overlay pos)))
70 (if overlay
71 (+ (marker-position (overlay-get overlay 'all-marker))
72 (- pos (overlay-start overlay))))))
73
74 (defun all-mode-find-overlay (pos)
75 ;; Find the overlay containing POS.
76 (let ((overlays (overlays-at pos)))
77 (while (and overlays (null (overlay-get (car overlays) 'all-marker)))
78 (setq overlays (cdr overlays)))
79 (car-safe overlays)))
80
81 (defun all-mode-goto ()
82 "Move point to the corresponding position in the original buffer."
83 (interactive)
84 (let ((pos (all-mode-find (point))))
85 (if pos
86 (pop-to-buffer all-buffer)
87 (error "This text is not from the original buffer"))
88 (goto-char pos)))
89
90 (defvar all-initialization-p nil)
91
92 (defun all-before-change-function (from to)
93 ;; Check that change is legal.
94 (and all-buffer
95 (not all-initialization-p)
96 (let ((start (all-mode-find-overlay from))
97 (end (all-mode-find-overlay to)))
98 (not (and start (eq start end))))
99 (error "Changes should be limited to a single text piece")))
100
101 (defun all-after-change-function (from to length)
102 ;; Propagate changes from *All* buffer.
103 (and all-buffer
104 (null all-initialization-p)
105 (let ((buffer (current-buffer))
106 (pos (all-mode-find from)))
107 (if pos
108 (with-current-buffer all-buffer
109 (save-excursion
110 (goto-char pos)
111 (delete-region pos (+ pos length))
112 (insert-buffer-substring buffer from to)))))))
113
114 ;;;###autoload
115 (defun all (regexp &optional nlines)
116 "Show all lines in the current buffer containing a match for REGEXP.
117
118 If a match spreads across multiple lines, all those lines are shown.
119
120 Each line is displayed with NLINES lines before and after, or -NLINES
121 before if NLINES is negative.
122 NLINES defaults to `list-matching-lines-default-context-lines'.
123 Interactively it is the prefix arg.
124
125 The lines are shown in a buffer named `*All*'.
126 Any changes made in that buffer will be propagated to this buffer."
127 (interactive
128 (list (let* ((default (car regexp-history)))
129 (read-string
130 (if default
131 (format
132 "Edit lines matching regexp (default `%s'): " default)
133 "Edit lines matching regexp: ")
134 nil 'regexp-history default))
135 current-prefix-arg))
136 (setq nlines (if nlines (prefix-numeric-value nlines)
137 list-matching-lines-default-context-lines))
138 (let ((all-initialization-p t)
139 (buffer (current-buffer))
140 (prevend nil)
141 (prevstart nil)
142 (prevpos (point-min)))
143 (with-output-to-temp-buffer "*All*"
144 (with-current-buffer standard-output
145 (all-mode)
146 (setq all-buffer buffer)
147 (insert "Lines matching ")
148 (prin1 regexp)
149 (insert " in buffer " (buffer-name buffer) ?. ?\n)
150 (insert "--------\n"))
151 (if (eq buffer standard-output)
152 (goto-char (point-max)))
153 (save-excursion
154 (goto-char (point-min))
155 ;; Find next match, but give up if prev match was at end of buffer.
156 (while (and (not (= prevpos (point-max)))
157 (re-search-forward regexp nil t))
158 (goto-char (match-beginning 0))
159 (beginning-of-line)
160 (setq prevpos (point))
161 (goto-char (match-end 0))
162 (let* ((start (save-excursion
163 (goto-char (match-beginning 0))
164 (forward-line (if (< nlines 0) nlines (- nlines)))
165 (point)))
166 (end (save-excursion
167 (goto-char (match-end 0))
168 (if (> nlines 0)
169 (forward-line (1+ nlines))
170 (forward-line 1))
171 (point))))
172 (cond ((null prevend)
173 (setq prevstart start
174 prevend end))
175 ((> start prevend)
176 (all-insert prevstart prevend regexp nlines)
177 (setq prevstart start
178 prevend end))
179 (t
180 (setq prevend end)))))
181 (if prevend
182 (all-insert prevstart prevend regexp nlines))))))
183
184 (defun all-insert (start end regexp nlines)
185 ;; Insert match.
186 (let ((marker (copy-marker start))
187 (buffer (current-buffer)))
188 (with-current-buffer standard-output
189 (let ((from (point))
190 to)
191 (insert-buffer-substring buffer start end)
192 (setq to (point))
193 (overlay-put (make-overlay from to) 'all-marker marker)
194 (goto-char from)
195 (while (re-search-forward regexp to t)
196 (put-text-property (match-beginning 0) (match-end 0)
197 'face 'match))
198 (goto-char to)
199 (if (> nlines 0)
200 (insert "--------\n"))))))
201
202 (provide 'all)
203
204 ;;; all.el ends here