comparison lisp/replace.el @ 38040:79be85ef3a31

(keep-lines-read-args): Return just a regexp. Provide nil for the region args. (keep-lines, flush-lines, how-many): Calculate the defaults for the region here, when args are nil.
author Richard M. Stallman <rms@gnu.org>
date Thu, 14 Jun 2001 20:54:34 +0000
parents 018637678f29
children be5236e1efc7
comparison
equal deleted inserted replaced
38039:cb751a987da5 38040:79be85ef3a31
291 291
292 292
293 (defun keep-lines-read-args (prompt) 293 (defun keep-lines-read-args (prompt)
294 "Read arguments for `keep-lines' and friends. 294 "Read arguments for `keep-lines' and friends.
295 Prompt for a regexp with PROMPT. 295 Prompt for a regexp with PROMPT.
296 296 Value is a list, (REGEXP)."
297 Value is a list (REGEXP START END). 297 (list (read-from-minibuffer prompt nil nil nil
298 298 'regexp-history nil t)))
299 If in Transient Mark mode, and the mark is active, START is the
300 start of the region, and END is a marker for the end of the region.
301 Otherwise, START is the current point, and END is the value of
302 function `point-max-marker'."
303 (let ((regexp (read-from-minibuffer prompt nil nil nil
304 'regexp-history nil t))
305 start end)
306 (if (and transient-mark-mode mark-active)
307 (setq start (region-beginning)
308 end (copy-marker (region-end)))
309 (setq start (point)
310 end (point-max-marker)))
311 (list regexp start end)))
312
313 299
314 (defun keep-lines (regexp &optional rstart rend) 300 (defun keep-lines (regexp &optional rstart rend)
315 "Delete all lines except those containing matches for REGEXP. 301 "Delete all lines except those containing matches for REGEXP.
316 A match split across lines preserves all the lines it lies in. 302 A match split across lines preserves all the lines it lies in.
317 Applies to all lines after point. 303 Applies to all lines after point.
319 If REGEXP contains upper case characters (excluding those preceded by `\\'), 305 If REGEXP contains upper case characters (excluding those preceded by `\\'),
320 the matching is case-sensitive. 306 the matching is case-sensitive.
321 307
322 Second and third arg RSTART and REND specify the region to operate on. 308 Second and third arg RSTART and REND specify the region to operate on.
323 309
324 In Transient Mark mode, if the mark is active, operate on the contents 310 Interactively, in Transient Mark mode when the mark is active, operate
325 of the region. Otherwise, operate from point to the end of the buffer." 311 on the contents of the region. Otherwise, operate from point to the
312 end of the buffer."
313
326 (interactive 314 (interactive
327 (keep-lines-read-args "Keep lines (containing match for regexp): ")) 315 (keep-lines-read-args "Keep lines (containing match for regexp): "))
328 (if rstart 316 (if rstart
329 (goto-char (min rstart rend)) 317 (goto-char (min rstart rend))
330 (setq rstart (point) rend (point-max-marker))) 318 (if (and transient-mark-mode mark-active)
319 (setq rstart (region-beginning)
320 rend (copy-marker (region-end)))
321 (setq rstart (point)
322 rend (point-max-marker)))
323 (goto-char rstart))
331 (save-excursion 324 (save-excursion
332 (or (bolp) (forward-line 1)) 325 (or (bolp) (forward-line 1))
333 (let ((start (point)) 326 (let ((start (point))
334 (case-fold-search (and case-fold-search 327 (case-fold-search (and case-fold-search
335 (isearch-no-upper-case-p regexp t)))) 328 (isearch-no-upper-case-p regexp t))))
359 If REGEXP contains upper case characters (excluding those preceded by `\\'), 352 If REGEXP contains upper case characters (excluding those preceded by `\\'),
360 the matching is case-sensitive. 353 the matching is case-sensitive.
361 354
362 Second and third arg RSTART and REND specify the region to operate on. 355 Second and third arg RSTART and REND specify the region to operate on.
363 356
364 In Transient Mark mode, if the mark is active, operate on the contents 357 Interactively, in Transient Mark mode when the mark is active, operate
365 of the region. Otherwise, operate from point to the end of the buffer." 358 on the contents of the region. Otherwise, operate from point to the
359 end of the buffer."
360
366 (interactive 361 (interactive
367 (keep-lines-read-args "Flush lines (containing match for regexp): ")) 362 (keep-lines-read-args "Flush lines (containing match for regexp): "))
368 (if rstart 363 (if rstart
369 (goto-char (min rstart rend)) 364 (goto-char (min rstart rend))
370 (setq rstart (point) rend (point-max-marker))) 365 (if (and transient-mark-mode mark-active)
366 (setq rstart (region-beginning)
367 rend (copy-marker (region-end)))
368 (setq rstart (point)
369 rend (point-max-marker)))
370 (goto-char rstart))
371 (let ((case-fold-search (and case-fold-search 371 (let ((case-fold-search (and case-fold-search
372 (isearch-no-upper-case-p regexp t)))) 372 (isearch-no-upper-case-p regexp t))))
373 (save-excursion 373 (save-excursion
374 (while (and (< (point) rend) 374 (while (and (< (point) rend)
375 (re-search-forward regexp rend t)) 375 (re-search-forward regexp rend t))
385 If REGEXP contains upper case characters (excluding those preceded by `\\'), 385 If REGEXP contains upper case characters (excluding those preceded by `\\'),
386 the matching is case-sensitive. 386 the matching is case-sensitive.
387 387
388 Second and third arg RSTART and REND specify the region to operate on. 388 Second and third arg RSTART and REND specify the region to operate on.
389 389
390 In Transient Mark mode, if the mark is active, operate on the contents 390 Interactively, in Transient Mark mode when the mark is active, operate
391 of the region. Otherwise, operate from point to the end of the buffer." 391 on the contents of the region. Otherwise, operate from point to the
392 end of the buffer."
393
392 (interactive 394 (interactive
393 (keep-lines-read-args "How many matches for (regexp): ")) 395 (keep-lines-read-args "How many matches for (regexp): "))
394 (save-excursion 396 (save-excursion
395 (if rstart 397 (if rstart
396 (goto-char (min rstart rend)) 398 (goto-char (min rstart rend))
397 (setq rstart (point) rend (point-max-marker))) 399 (if (and transient-mark-mode mark-active)
400 (setq rstart (region-beginning)
401 rend (copy-marker (region-end)))
402 (setq rstart (point)
403 rend (point-max-marker)))
404 (goto-char rstart))
398 (let ((count 0) 405 (let ((count 0)
399 opoint 406 opoint
400 (case-fold-search (and case-fold-search 407 (case-fold-search (and case-fold-search
401 (isearch-no-upper-case-p regexp t)))) 408 (isearch-no-upper-case-p regexp t))))
402 (while (and (< (point) rend) 409 (while (and (< (point) rend)