1 ;;; all.el --- Edit all lines matching a given regexp
3 ;; Copyright (C) 1985-1987, 1992, 1994, 2011 Free Software Foundation, Inc.
4 ;; Copyright (C) 1994 Per Abrahamsen
6 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
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|
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)
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.
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/>.
30 ;; Just like occur, except that changes in the *All* buffer are
31 ;; propagated to the original buffer.
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.
37 ;; Line numbers are not listed in the *All* buffer.
39 ;; Ok, it is _not_ just like occur.
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.
50 (let ((map (make-sparse-keymap)))
51 (define-key map "\C-c\C-c" 'all-mode-goto)
54 (defvar all-buffer nil)
55 (make-variable-buffer-local 'all-buffer)
57 (define-derived-mode all-mode fundamental-mode "All"
58 "Major mode for output from \\[all].
60 All changes made in this buffer will be propagated to the buffer where
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))
67 (defun all-mode-find (pos)
68 ;; Find position in original buffer corresponding to POS.
69 (let ((overlay (all-mode-find-overlay pos)))
71 (+ (marker-position (overlay-get overlay 'all-marker))
72 (- pos (overlay-start overlay))))))
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)))
81 (defun all-mode-goto ()
82 "Move point to the corresponding position in the original buffer."
84 (let ((pos (all-mode-find (point))))
86 (pop-to-buffer all-buffer)
87 (error "This text is not from the original buffer"))
90 (defvar all-initialization-p nil)
92 (defun all-before-change-function (from to)
93 ;; Check that change is legal.
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")))
101 (defun all-after-change-function (from to length)
102 ;; Propagate changes from *All* buffer.
104 (null all-initialization-p)
105 (let ((buffer (current-buffer))
106 (pos (all-mode-find from)))
108 (with-current-buffer all-buffer
111 (delete-region pos (+ pos length))
112 (insert-buffer-substring buffer from to)))))))
115 (defun all (regexp &optional nlines)
116 "Show all lines in the current buffer containing a match for REGEXP.
118 If a match spreads across multiple lines, all those lines are shown.
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.
125 The lines are shown in a buffer named `*All*'.
126 Any changes made in that buffer will be propagated to this buffer."
128 (list (let* ((default (car regexp-history)))
132 "Edit lines matching regexp (default `%s'): " default)
133 "Edit lines matching regexp: ")
134 nil 'regexp-history default))
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))
142 (prevpos (point-min)))
143 (with-output-to-temp-buffer "*All*"
144 (with-current-buffer standard-output
146 (setq all-buffer buffer)
147 (insert "Lines matching ")
149 (insert " in buffer " (buffer-name buffer) ?. ?\n)
150 (insert "--------\n"))
151 (if (eq buffer standard-output)
152 (goto-char (point-max)))
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))
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)))
167 (goto-char (match-end 0))
169 (forward-line (1+ nlines))
172 (cond ((null prevend)
173 (setq prevstart start
176 (all-insert prevstart prevend regexp nlines)
177 (setq prevstart start
180 (setq prevend end)))))
182 (all-insert prevstart prevend regexp nlines))))))
184 (defun all-insert (start end regexp nlines)
186 (let ((marker (copy-marker start))
187 (buffer (current-buffer)))
188 (with-current-buffer standard-output
191 (insert-buffer-substring buffer start end)
193 (overlay-put (make-overlay from to) 'all-marker marker)
195 (while (re-search-forward regexp to t)
196 (put-text-property (match-beginning 0) (match-end 0)
200 (insert "--------\n"))))))