comparison lisp/replace.el @ 2610:16738b49b833

[xemacs-hg @ 2005-02-23 22:09:13 by adrian] [PATCH] xemacs-21.5-clean: Avoid prohibitive string consing and GC <r7j8tbas.fsf@smtprelay.t-online.de>
author adrian
date Wed, 23 Feb 2005 22:09:15 +0000
parents d824841064f3
children 5df5ea55d3fc
comparison
equal deleted inserted replaced
2609:c2580215c222 2610:16738b49b833
245 ;; Always start on the beginning of a line. 245 ;; Always start on the beginning of a line.
246 (or (bolp) (forward-line 1)) 246 (or (bolp) (forward-line 1))
247 247
248 (let ((matched-text nil) 248 (let ((matched-text nil)
249 (curmatch-start (point)) 249 (curmatch-start (point))
250 (limit (copy-marker (point-max)))) 250 (limit (copy-marker (point-max)))
251 (matched-text-buffer (generate-new-buffer " *matched-text*"))
252 lines-matched)
251 ;; Limit search if limits were specified. 253 ;; Limit search if limits were specified.
252 (when end (setq limit (copy-marker end))) 254 (when end (setq limit (copy-marker end)))
253 255
254 ;; Search. Stop if we are at end of buffer or outside the 256 ;; Search. Stop if we are at end of buffer or outside the
255 ;; limit. 257 ;; limit.
257 (eobp) 259 (eobp)
258 (and limit (>= (point) limit)))) 260 (and limit (>= (point) limit))))
259 ;; curmatch-start is first char not preserved by previous match. 261 ;; curmatch-start is first char not preserved by previous match.
260 (if (not (re-search-forward regexp limit 'move)) 262 (if (not (re-search-forward regexp limit 'move))
261 (let ((curmatch-end limit)) 263 (let ((curmatch-end limit))
262 (setq matched-text (concat matched-text (buffer-substring curmatch-start curmatch-end))) 264 (append-to-buffer matched-text-buffer curmatch-start curmatch-end)
263 (if delete (delete-region curmatch-start curmatch-end))) 265 (if delete (delete-region curmatch-start curmatch-end)))
264 (let ((curmatch-end (save-excursion (goto-char (match-beginning 0)) 266 (let ((curmatch-end (save-excursion (goto-char (match-beginning 0))
265 (beginning-of-line) 267 (beginning-of-line)
266 (point)))) 268 (point))))
267 ;; Now curmatch-end is first char preserved by the new match. 269 ;; Now curmatch-end is first char preserved by the new match.
268 (if (< curmatch-start curmatch-end) 270 (if (< curmatch-start curmatch-end)
269 (progn 271 (progn
270 (setq matched-text (concat matched-text (buffer-substring curmatch-start curmatch-end))) 272 (append-to-buffer matched-text-buffer curmatch-start curmatch-end)
271 (if delete (delete-region curmatch-start curmatch-end)))))) 273 (if delete (delete-region curmatch-start curmatch-end))))))
272 (setq curmatch-start (save-excursion (forward-line 1) 274 (setq curmatch-start (save-excursion (forward-line 1)
273 (point))) 275 (point)))
274 ;; If the match was empty, avoid matching again at same place. 276 ;; If the match was empty, avoid matching again at same place.
275 (and (not (eobp)) (= (match-beginning 0) (match-end 0)) 277 (and (not (eobp)) (= (match-beginning 0) (match-end 0))
276 (forward-char 1))) 278 (forward-char 1)))
277 279
278 ;; If any lines were matched and KILL is non-nil, insert the 280 ;; If any lines were matched and KILL is non-nil, insert the
279 ;; matched lines into the kill ring. 281 ;; matched lines into the kill ring.
282 (setq matched-text (buffer-string matched-text-buffer))
280 (if (and matched-text kill) (kill-new matched-text)) 283 (if (and matched-text kill) (kill-new matched-text))
281 284
282 ;; Return the number of matched lines. 285 ;; Return the number of matched lines.
283 (with-temp-buffer 286 (setq lines-matched
284 ;; Use concat to make a string even if matched-text is nil. 287 (with-current-buffer matched-text-buffer
285 (insert (concat matched-text)) 288 (count-lines (point-min) (point-max))))
286 (count-lines (point-min) (point-max))) 289 (kill-buffer matched-text-buffer)
287 )))) 290 lines-matched))))
288 291
289 (define-function 'keep-lines 'delete-non-matching-lines) 292 (define-function 'keep-lines 'delete-non-matching-lines)
290 (defun delete-non-matching-lines (regexp) 293 (defun delete-non-matching-lines (regexp)
291 "Delete lines that do not match REGEXP, from point to the end of the 294 "Delete lines that do not match REGEXP, from point to the end of the
292 buffer (or within the region, if it is active)." 295 buffer (or within the region, if it is active)."
356 (with-search-caps-disable-folding regexp t 359 (with-search-caps-disable-folding regexp t
357 (save-excursion 360 (save-excursion
358 (let ((matched-text nil) 361 (let ((matched-text nil)
359 (curmatch-start nil) 362 (curmatch-start nil)
360 (curmatch-end nil) 363 (curmatch-end nil)
361 (limit nil)) 364 (limit nil)
362 365 (matched-text-buffer (generate-new-buffer " *matched-text*"))
366 lines-matched)
363 ;; Limit search if limits were specified. 367 ;; Limit search if limits were specified.
364 (when beg (goto-char beg)) 368 (when beg (goto-char beg))
365 (when end (setq limit (copy-marker end))) 369 (when end (setq limit (copy-marker end)))
366 370
367 (while (and (not (eobp)) 371 (while (and (not (eobp))
368 (re-search-forward regexp limit t)) 372 (re-search-forward regexp limit t))
369 (setq curmatch-start (save-excursion (goto-char (match-beginning 0)) 373 (setq curmatch-start (save-excursion (goto-char (match-beginning 0))
370 (beginning-of-line) 374 (beginning-of-line)
371 (point))) 375 (point)))
372 (setq curmatch-end (progn (forward-line 1) (point))) 376 (setq curmatch-end (progn (forward-line 1) (point)))
373 (setq matched-text (concat matched-text (buffer-substring curmatch-start curmatch-end))) 377 (append-to-buffer matched-text-buffer curmatch-start curmatch-end)
374 (if delete (delete-region curmatch-start curmatch-end))) 378 (if delete (delete-region curmatch-start curmatch-end)))
375 379 (setq matched-text (buffer-string matched-text-buffer))
376 (if (and matched-text kill) (kill-new matched-text)) 380 (if (and matched-text kill) (kill-new matched-text))
377 381
378 ;; Return the number of matched lines. 382 ;; Return the number of matched lines.
379 (with-temp-buffer 383 (setq lines-matched
380 ;; Use concat to make a string even if matched-text is nil. 384 (with-current-buffer matched-text-buffer
381 (insert (concat matched-text)) 385 (count-lines (point-min) (point-max))))
382 (count-lines (point-min) (point-max))) 386 (kill-buffer matched-text-buffer)
383 )))) 387 lines-matched))))
384 388
385 (define-function 'flush-lines 'delete-matching-lines) 389 (define-function 'flush-lines 'delete-matching-lines)
386 (defun delete-matching-lines (regexp) 390 (defun delete-matching-lines (regexp)
387 "Delete the lines that match REGEXP, from point to the end of the 391 "Delete the lines that match REGEXP, from point to the end of the
388 buffer (or within the region, if it is active)." 392 buffer (or within the region, if it is active)."