aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/replace.el
diff options
context:
space:
mode:
authorRichard M. Stallman1993-03-28 20:17:39 +0000
committerRichard M. Stallman1993-03-28 20:17:39 +0000
commit4c53bd2bb08b166f271b3acfb8381064dc3e8089 (patch)
tree8a255b305af58877c60ad045edea9eb8bee5686c /lisp/replace.el
parent4ad827c5ef13e13c37f26bad608cb7f4971a9d70 (diff)
downloademacs-4c53bd2bb08b166f271b3acfb8381064dc3e8089.tar.gz
emacs-4c53bd2bb08b166f271b3acfb8381064dc3e8089.zip
(regexp-history): New history list.
(occur, flush-lines, keep-lines, how-many): Use it. (occur): Don't insert previous string in minibuffer gratuitously. Just use it if input is empty. Use save-match-data around count-lines.
Diffstat (limited to 'lisp/replace.el')
-rw-r--r--lisp/replace.el38
1 files changed, 25 insertions, 13 deletions
diff --git a/lisp/replace.el b/lisp/replace.el
index 62d80278589..634c42691e8 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -142,13 +142,18 @@ which will run faster and will not set the mark or print anything."
142 (interactive (query-replace-read-args "Replace regexp")) 142 (interactive (query-replace-read-args "Replace regexp"))
143 (perform-replace regexp to-string nil t delimited) 143 (perform-replace regexp to-string nil t delimited)
144 (or unread-command-events (message "Done"))) 144 (or unread-command-events (message "Done")))
145
146(defvar regexp-history nil
147 "History list for some commands that read regular expressions.")
145 148
146(fset 'delete-non-matching-lines 'keep-lines) 149(fset 'delete-non-matching-lines 'keep-lines)
147(defun keep-lines (regexp) 150(defun keep-lines (regexp)
148 "Delete all lines except those containing matches for REGEXP. 151 "Delete all lines except those containing matches for REGEXP.
149A match split across lines preserves all the lines it lies in. 152A match split across lines preserves all the lines it lies in.
150Applies to all lines after point." 153Applies to all lines after point."
151 (interactive "sKeep lines (containing match for regexp): ") 154 (interactive (list (read-from-minibuffer
155 "sKeep lines (containing match for regexp): "
156 nil nil nil 'regexp-history)))
152 (save-excursion 157 (save-excursion
153 (or (bolp) (forward-line 1)) 158 (or (bolp) (forward-line 1))
154 (let ((start (point))) 159 (let ((start (point)))
@@ -173,7 +178,9 @@ Applies to all lines after point."
173 "Delete lines containing matches for REGEXP. 178 "Delete lines containing matches for REGEXP.
174If a match is split across lines, all the lines it lies in are deleted. 179If a match is split across lines, all the lines it lies in are deleted.
175Applies to lines after point." 180Applies to lines after point."
176 (interactive "sFlush lines (containing match for regexp): ") 181 (interactive (list (read-from-minibuffer
182 "sFlush lines (containing match for regexp): "
183 nil nil nil 'regexp-history)))
177 (save-excursion 184 (save-excursion
178 (while (and (not (eobp)) 185 (while (and (not (eobp))
179 (re-search-forward regexp nil t)) 186 (re-search-forward regexp nil t))
@@ -185,7 +192,9 @@ Applies to lines after point."
185(fset 'count-matches 'how-many) 192(fset 'count-matches 'how-many)
186(defun how-many (regexp) 193(defun how-many (regexp)
187 "Print number of matches for REGEXP following point." 194 "Print number of matches for REGEXP following point."
188 (interactive "sHow many matches for (regexp): ") 195 (interactive (list (read-from-minibuffer
196 "sHow many matches for (regexp): "
197 nil nil nil 'regexp-history)))
189 (let ((count 0) opoint) 198 (let ((count 0) opoint)
190 (save-excursion 199 (save-excursion
191 (while (and (not (eobp)) 200 (while (and (not (eobp))
@@ -195,7 +204,7 @@ Applies to lines after point."
195 (forward-char 1) 204 (forward-char 1)
196 (setq count (1+ count)))) 205 (setq count (1+ count))))
197 (message "%d occurrences" count)))) 206 (message "%d occurrences" count))))
198 207
199(defvar occur-mode-map ()) 208(defvar occur-mode-map ())
200(if occur-mode-map 209(if occur-mode-map
201 () 210 ()
@@ -205,7 +214,6 @@ Applies to lines after point."
205(defvar occur-buffer nil) 214(defvar occur-buffer nil)
206(defvar occur-nlines nil) 215(defvar occur-nlines nil)
207(defvar occur-pos-list nil) 216(defvar occur-pos-list nil)
208(defvar occur-last-string "")
209 217
210(defun occur-mode () 218(defun occur-mode ()
211 "Major mode for output from \\[occur]. 219 "Major mode for output from \\[occur].
@@ -244,7 +252,7 @@ in the buffer that the occurrences were found in.
244 (pos (nth occur-number occur-pos-list))) 252 (pos (nth occur-number occur-pos-list)))
245 (pop-to-buffer occur-buffer) 253 (pop-to-buffer occur-buffer)
246 (goto-char (marker-position pos)))) 254 (goto-char (marker-position pos))))
247 255
248(defvar list-matching-lines-default-context-lines 0 256(defvar list-matching-lines-default-context-lines 0
249 "*Default number of context lines to include around a `list-matching-lines' 257 "*Default number of context lines to include around a `list-matching-lines'
250match. A negative number means to include that many lines before the match. 258match. A negative number means to include that many lines before the match.
@@ -254,8 +262,6 @@ A positive number means to include that many lines both before and after.")
254 262
255(defun occur (regexp &optional nlines) 263(defun occur (regexp &optional nlines)
256 "Show all lines in the current buffer containing a match for REGEXP. 264 "Show all lines in the current buffer containing a match for REGEXP.
257Interactively, REGEXP defaults to the last REGEXP
258used interactively with \\[occur].
259 265
260If a match spreads across multiple lines, all those lines are shown. 266If a match spreads across multiple lines, all those lines are shown.
261 267
@@ -264,12 +270,17 @@ before if NLINES is negative.
264NLINES defaults to `list-matching-lines-default-context-lines'. 270NLINES defaults to `list-matching-lines-default-context-lines'.
265Interactively it is the prefix arg. 271Interactively it is the prefix arg.
266 272
267The lines are shown in a buffer named *Occur*. 273The lines are shown in a buffer named `*Occur*'.
268It serves as a menu to find any of the occurrences in this buffer. 274It serves as a menu to find any of the occurrences in this buffer.
269\\[describe-mode] in that buffer will explain how." 275\\[describe-mode] in that buffer will explain how."
270 (interactive (list (setq occur-last-string 276 (interactive (list (let* ((default (car regexp-history))
271 (read-string "List lines matching regexp: " 277 (input
272 occur-last-string)) 278 (read-from-minibuffer
279 (format "List lines matching regexp (default `%s'): " default)
280 nil nil nil
281 'regexp-history)))
282 (if (> (length input) 0) input
283 (setcar regexp-history default)))
273 current-prefix-arg)) 284 current-prefix-arg))
274 (setq nlines (if nlines (prefix-numeric-value nlines) 285 (setq nlines (if nlines (prefix-numeric-value nlines)
275 list-matching-lines-default-context-lines)) 286 list-matching-lines-default-context-lines))
@@ -301,7 +312,8 @@ It serves as a menu to find any of the occurrences in this buffer.
301 (re-search-forward regexp nil t)) 312 (re-search-forward regexp nil t))
302 (goto-char (match-beginning 0)) 313 (goto-char (match-beginning 0))
303 (beginning-of-line) 314 (beginning-of-line)
304 (setq linenum (+ linenum (count-lines prevpos (point)))) 315 (save-match-data
316 (setq linenum (+ linenum (count-lines prevpos (point)))))
305 (setq prevpos (point)) 317 (setq prevpos (point))
306 (goto-char (match-end 0)) 318 (goto-char (match-end 0))
307 (let* ((start (save-excursion 319 (let* ((start (save-excursion