aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/replace.el
diff options
context:
space:
mode:
authorLuc Teirlinck2005-06-26 02:38:08 +0000
committerLuc Teirlinck2005-06-26 02:38:08 +0000
commitbace72098d62721fe00c8f0ff40b1e25f1f418a9 (patch)
treefb46592eb34d9405cca20abcb38bcca836336c51 /lisp/replace.el
parentdd716cec1d25185142da0ddb3c16905b4cb98599 (diff)
downloademacs-bace72098d62721fe00c8f0ff40b1e25f1f418a9.tar.gz
emacs-bace72098d62721fe00c8f0ff40b1e25f1f418a9.zip
(keep-lines-read-args): Add INTERACTIVE arg.
(keep-lines): Add INTERACTIVE arg. Never delete lines only partially contained in the active region. Do not take active region into account when called from Lisp, unless INTERACTIVE arg is non-nil. Use `forward-line' instead of `beginning-of-line' to avoid trouble with fields. Make marker point nowhere when no longer used. Always return nil. Doc fix. (flush-lines): Add INTERACTIVE arg. Do not take active region into account when called from Lisp, unless INTERACTIVE arg is non-nil. Use `forward-line' instead of `beginning-of-line' to avoid trouble with fields. Make marker point nowhere when no longer used. Always return nil. Doc fix. (how-many): Add INTERACTIVE arg. Make RSTART and REND args interchangeable. Do not take active region into account when called from Lisp, unless INTERACTIVE arg is non-nil. Do not print message in echo area when called from Lisp, unless INTERACTIVE arg is non-nil. Avoid saying "1 occurrences". Do not use markers. Return the number of matches. Doc fix. (occur): Doc fix. (perform-replace): Make comment follow double space convention for the sake of `outline-minor-mode'.
Diffstat (limited to 'lisp/replace.el')
-rw-r--r--lisp/replace.el120
1 files changed, 83 insertions, 37 deletions
diff --git a/lisp/replace.el b/lisp/replace.el
index 2896ce133de..0b19d72178f 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -516,21 +516,32 @@ which will run faster and will not set the mark or print anything."
516Prompt for a regexp with PROMPT. 516Prompt for a regexp with PROMPT.
517Value is a list, (REGEXP)." 517Value is a list, (REGEXP)."
518 (list (read-from-minibuffer prompt nil nil nil 518 (list (read-from-minibuffer prompt nil nil nil
519 'regexp-history nil t))) 519 'regexp-history nil t)
520 nil nil t))
520 521
521(defun keep-lines (regexp &optional rstart rend) 522(defun keep-lines (regexp &optional rstart rend interactive)
522 "Delete all lines except those containing matches for REGEXP. 523 "Delete all lines except those containing matches for REGEXP.
523A match split across lines preserves all the lines it lies in. 524A match split across lines preserves all the lines it lies in.
524Applies to all lines after point. 525When called from Lisp (and usually interactively as well, see below)
526applies to all lines starting after point.
525 527
526If REGEXP contains upper case characters (excluding those preceded by `\\'), 528If REGEXP contains upper case characters (excluding those preceded by `\\'),
527the matching is case-sensitive. 529the matching is case-sensitive.
528 530
529Second and third arg RSTART and REND specify the region to operate on. 531Second and third arg RSTART and REND specify the region to operate on.
532This command operates on (the accessible part of) all lines whose
533accessible part is entirely contained in the region determined by RSTART
534and REND. (A newline ending a line counts as part of that line.)
530 535
531Interactively, in Transient Mark mode when the mark is active, operate 536Interactively, in Transient Mark mode when the mark is active, operate
532on the contents of the region. Otherwise, operate from point to the 537on all lines whose accessible part is entirely contained in the region.
533end of the buffer." 538Otherwise, the command applies to all lines starting after point.
539When calling this function from Lisp, you can pretend that it was
540called interactively by passing a non-nil INTERACTIVE argument.
541
542This function starts looking for the next match from the end of
543the previous match. Hence, it ignores matches that overlap
544a previously found match."
534 545
535 (interactive 546 (interactive
536 (progn 547 (progn
@@ -539,10 +550,20 @@ end of the buffer."
539 (if rstart 550 (if rstart
540 (progn 551 (progn
541 (goto-char (min rstart rend)) 552 (goto-char (min rstart rend))
542 (setq rend (copy-marker (max rstart rend)))) 553 (setq rend
543 (if (and transient-mark-mode mark-active) 554 (progn
555 (save-excursion
556 (goto-char (max rstart rend))
557 (unless (or (bolp) (eobp))
558 (forward-line 0))
559 (point-marker)))))
560 (if (and interactive transient-mark-mode mark-active)
544 (setq rstart (region-beginning) 561 (setq rstart (region-beginning)
545 rend (copy-marker (region-end))) 562 rend (progn
563 (goto-char (region-end))
564 (unless (or (bolp) (eobp))
565 (forward-line 0))
566 (point-marker)))
546 (setq rstart (point) 567 (setq rstart (point)
547 rend (point-max-marker))) 568 rend (point-max-marker)))
548 (goto-char rstart)) 569 (goto-char rstart))
@@ -556,7 +577,7 @@ end of the buffer."
556 (if (not (re-search-forward regexp rend 'move)) 577 (if (not (re-search-forward regexp rend 'move))
557 (delete-region start rend) 578 (delete-region start rend)
558 (let ((end (save-excursion (goto-char (match-beginning 0)) 579 (let ((end (save-excursion (goto-char (match-beginning 0))
559 (beginning-of-line) 580 (forward-line 0)
560 (point)))) 581 (point))))
561 ;; Now end is first char preserved by the new match. 582 ;; Now end is first char preserved by the new match.
562 (if (< start end) 583 (if (< start end)
@@ -566,22 +587,34 @@ end of the buffer."
566 ;; If the match was empty, avoid matching again at same place. 587 ;; If the match was empty, avoid matching again at same place.
567 (and (< (point) rend) 588 (and (< (point) rend)
568 (= (match-beginning 0) (match-end 0)) 589 (= (match-beginning 0) (match-end 0))
569 (forward-char 1)))))) 590 (forward-char 1)))))
591 (set-marker rend nil)
592 nil)
570 593
571 594
572(defun flush-lines (regexp &optional rstart rend) 595(defun flush-lines (regexp &optional rstart rend interactive)
573 "Delete lines containing matches for REGEXP. 596 "Delete lines containing matches for REGEXP.
574If a match is split across lines, all the lines it lies in are deleted. 597When called from Lisp (and usually when called interactively as
575Applies to lines after point. 598well, see below), applies to the part of the buffer after point.
599The line point is in is deleted if and only if it contains a
600match for regexp starting after point.
576 601
577If REGEXP contains upper case characters (excluding those preceded by `\\'), 602If REGEXP contains upper case characters (excluding those preceded by `\\'),
578the matching is case-sensitive. 603the matching is case-sensitive.
579 604
580Second and third arg RSTART and REND specify the region to operate on. 605Second and third arg RSTART and REND specify the region to operate on.
606Lines partially contained in this region are deleted if and only if
607they contain a match entirely contained in it.
581 608
582Interactively, in Transient Mark mode when the mark is active, operate 609Interactively, in Transient Mark mode when the mark is active, operate
583on the contents of the region. Otherwise, operate from point to the 610on the contents of the region. Otherwise, operate from point to the
584end of the buffer." 611end of (the accessible portion of) the buffer. When calling this function
612from Lisp, you can pretend that it was called interactively by passing
613a non-nil INTERACTIVE argument.
614
615If a match is split across lines, all the lines it lies in are deleted.
616They are deleted _before_ looking for the next match. Hence, a match
617starting on the same line at which another match ended is ignored."
585 618
586 (interactive 619 (interactive
587 (progn 620 (progn
@@ -591,7 +624,7 @@ end of the buffer."
591 (progn 624 (progn
592 (goto-char (min rstart rend)) 625 (goto-char (min rstart rend))
593 (setq rend (copy-marker (max rstart rend)))) 626 (setq rend (copy-marker (max rstart rend))))
594 (if (and transient-mark-mode mark-active) 627 (if (and interactive transient-mark-mode mark-active)
595 (setq rstart (region-beginning) 628 (setq rstart (region-beginning)
596 rend (copy-marker (region-end))) 629 rend (copy-marker (region-end)))
597 (setq rstart (point) 630 (setq rstart (point)
@@ -603,13 +636,18 @@ end of the buffer."
603 (while (and (< (point) rend) 636 (while (and (< (point) rend)
604 (re-search-forward regexp rend t)) 637 (re-search-forward regexp rend t))
605 (delete-region (save-excursion (goto-char (match-beginning 0)) 638 (delete-region (save-excursion (goto-char (match-beginning 0))
606 (beginning-of-line) 639 (forward-line 0)
607 (point)) 640 (point))
608 (progn (forward-line 1) (point))))))) 641 (progn (forward-line 1) (point))))))
642 (set-marker rend nil)
643 nil)
609 644
610 645
611(defun how-many (regexp &optional rstart rend) 646(defun how-many (regexp &optional rstart rend interactive)
612 "Print number of matches for REGEXP following point. 647 "Print and return number of matches for REGEXP following point.
648When called from Lisp and INTERACTIVE is omitted or nil, just return
649the number, do not print it; if INTERACTIVE is t, the function behaves
650in all respects has if it had been called interactively.
613 651
614If REGEXP contains upper case characters (excluding those preceded by `\\'), 652If REGEXP contains upper case characters (excluding those preceded by `\\'),
615the matching is case-sensitive. 653the matching is case-sensitive.
@@ -618,18 +656,24 @@ Second and third arg RSTART and REND specify the region to operate on.
618 656
619Interactively, in Transient Mark mode when the mark is active, operate 657Interactively, in Transient Mark mode when the mark is active, operate
620on the contents of the region. Otherwise, operate from point to the 658on the contents of the region. Otherwise, operate from point to the
621end of the buffer." 659end of (the accessible portion of) the buffer.
660
661This function starts looking for the next match from the end of
662the previous match. Hence, it ignores matches that overlap
663a previously found match."
622 664
623 (interactive 665 (interactive
624 (keep-lines-read-args "How many matches for (regexp): ")) 666 (keep-lines-read-args "How many matches for (regexp): "))
625 (save-excursion 667 (save-excursion
626 (if rstart 668 (if rstart
627 (goto-char (min rstart rend)) 669 (progn
628 (if (and transient-mark-mode mark-active) 670 (goto-char (min rstart rend))
671 (setq rend (max rstart rend)))
672 (if (and interactive transient-mark-mode mark-active)
629 (setq rstart (region-beginning) 673 (setq rstart (region-beginning)
630 rend (copy-marker (region-end))) 674 rend (region-end))
631 (setq rstart (point) 675 (setq rstart (point)
632 rend (point-max-marker))) 676 rend (point-max)))
633 (goto-char rstart)) 677 (goto-char rstart))
634 (let ((count 0) 678 (let ((count 0)
635 opoint 679 opoint
@@ -641,7 +685,10 @@ end of the buffer."
641 (if (= opoint (point)) 685 (if (= opoint (point))
642 (forward-char 1) 686 (forward-char 1)
643 (setq count (1+ count)))) 687 (setq count (1+ count))))
644 (message "%d occurrences" count)))) 688 (when interactive (message "%d occurrence%s"
689 count
690 (if (= count 1) "" "s")))
691 count)))
645 692
646 693
647(defvar occur-mode-map 694(defvar occur-mode-map
@@ -892,8 +939,7 @@ buffer for each buffer where you invoke `occur'."
892 939
893(defun occur (regexp &optional nlines) 940(defun occur (regexp &optional nlines)
894 "Show all lines in the current buffer containing a match for REGEXP. 941 "Show all lines in the current buffer containing a match for REGEXP.
895 942This function can not handle matches that span more than one line.
896If a match spreads across multiple lines, all those lines are shown.
897 943
898Each line is displayed with NLINES lines before and after, or -NLINES 944Each line is displayed with NLINES lines before and after, or -NLINES
899before if NLINES is negative. 945before if NLINES is negative.
@@ -1603,15 +1649,15 @@ make, or the user didn't cancel the call."
1603 ;; Change markers to numbers in the match data 1649 ;; Change markers to numbers in the match data
1604 ;; since lots of markers slow down editing. 1650 ;; since lots of markers slow down editing.
1605 (push (list (point) replaced 1651 (push (list (point) replaced
1606;;; If the replacement has already happened, all we need is the 1652;;; If the replacement has already happened, all we need is the
1607;;; current match start and end. We could get this with a trivial 1653;;; current match start and end. We could get this with a trivial
1608;;; match like 1654;;; match like
1609;;; (save-excursion (goto-char (match-beginning 0)) 1655;;; (save-excursion (goto-char (match-beginning 0))
1610;;; (search-forward (match-string 0)) 1656;;; (search-forward (match-string 0))
1611;;; (match-data t)) 1657;;; (match-data t))
1612;;; if we really wanted to avoid manually constructing match data. 1658;;; if we really wanted to avoid manually constructing match data.
1613;;; Adding current-buffer is necessary so that match-data calls can 1659;;; Adding current-buffer is necessary so that match-data calls can
1614;;; return markers which are appropriate for editing. 1660;;; return markers which are appropriate for editing.
1615 (if replaced 1661 (if replaced
1616 (list 1662 (list
1617 (match-beginning 0) 1663 (match-beginning 0)