comparison lisp/replace.el @ 90199:bb71c6cf2009

Revision: miles@gnu.org--gnu-2005/emacs--unicode--0--patch-67 Merge from emacs--cvs-trunk--0 Patches applied: * emacs--cvs-trunk--0 (patch 447-458) - Update from CVS - Update from CVS: lisp/subr.el (add-to-ordered-list): Doc fix. - Merge from gnus--rel--5.10 * gnus--rel--5.10 (patch 83-85) - Merge from emacs--cvs-trunk--0 - Update from CVS
author Miles Bader <miles@gnu.org>
date Thu, 30 Jun 2005 00:31:46 +0000
parents 173dee4e2611 98208dd356b4
children f9a65d7ebd29
comparison
equal deleted inserted replaced
90198:97f6c3a96df1 90199:bb71c6cf2009
514 (defun keep-lines-read-args (prompt) 514 (defun keep-lines-read-args (prompt)
515 "Read arguments for `keep-lines' and friends. 515 "Read arguments for `keep-lines' and friends.
516 Prompt for a regexp with PROMPT. 516 Prompt for a regexp with PROMPT.
517 Value is a list, (REGEXP)." 517 Value 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 520 nil nil t))
521 (defun keep-lines (regexp &optional rstart rend) 521
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.
523 A match split across lines preserves all the lines it lies in. 524 A match split across lines preserves all the lines it lies in.
524 Applies to all lines after point. 525 When called from Lisp (and usually interactively as well, see below)
526 applies to all lines starting after point.
525 527
526 If REGEXP contains upper case characters (excluding those preceded by `\\'), 528 If REGEXP contains upper case characters (excluding those preceded by `\\'),
527 the matching is case-sensitive. 529 the matching is case-sensitive.
528 530
529 Second and third arg RSTART and REND specify the region to operate on. 531 Second and third arg RSTART and REND specify the region to operate on.
532 This command operates on (the accessible part of) all lines whose
533 accessible part is entirely contained in the region determined by RSTART
534 and REND. (A newline ending a line counts as part of that line.)
530 535
531 Interactively, in Transient Mark mode when the mark is active, operate 536 Interactively, in Transient Mark mode when the mark is active, operate
532 on the contents of the region. Otherwise, operate from point to the 537 on all lines whose accessible part is entirely contained in the region.
533 end of the buffer." 538 Otherwise, the command applies to all lines starting after point.
539 When calling this function from Lisp, you can pretend that it was
540 called interactively by passing a non-nil INTERACTIVE argument.
541
542 This function starts looking for the next match from the end of
543 the previous match. Hence, it ignores matches that overlap
544 a previously found match."
534 545
535 (interactive 546 (interactive
536 (progn 547 (progn
537 (barf-if-buffer-read-only) 548 (barf-if-buffer-read-only)
538 (keep-lines-read-args "Keep lines (containing match for regexp): "))) 549 (keep-lines-read-args "Keep lines (containing match for regexp): ")))
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))
549 (save-excursion 570 (save-excursion
550 (or (bolp) (forward-line 1)) 571 (or (bolp) (forward-line 1))
554 (while (< (point) rend) 575 (while (< (point) rend)
555 ;; Start is first char not preserved by previous match. 576 ;; Start is first char not preserved by previous match.
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)
563 (delete-region start end)))) 584 (delete-region start end))))
564 585
565 (setq start (save-excursion (forward-line 1) (point))) 586 (setq start (save-excursion (forward-line 1) (point)))
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)))))
570 591 (set-marker rend nil)
571 592 nil)
572 (defun flush-lines (regexp &optional rstart rend) 593
573 "Delete lines containing matches for REGEXP. 594
574 If a match is split across lines, all the lines it lies in are deleted. 595 (defun flush-lines (regexp &optional rstart rend interactive)
575 Applies to lines after point. 596 "Delete lines containing matches for REGEXP.
597 When called from Lisp (and usually when called interactively as
598 well, see below), applies to the part of the buffer after point.
599 The line point is in is deleted if and only if it contains a
600 match for regexp starting after point.
576 601
577 If REGEXP contains upper case characters (excluding those preceded by `\\'), 602 If REGEXP contains upper case characters (excluding those preceded by `\\'),
578 the matching is case-sensitive. 603 the matching is case-sensitive.
579 604
580 Second and third arg RSTART and REND specify the region to operate on. 605 Second and third arg RSTART and REND specify the region to operate on.
606 Lines partially contained in this region are deleted if and only if
607 they contain a match entirely contained in it.
581 608
582 Interactively, in Transient Mark mode when the mark is active, operate 609 Interactively, in Transient Mark mode when the mark is active, operate
583 on the contents of the region. Otherwise, operate from point to the 610 on the contents of the region. Otherwise, operate from point to the
584 end of the buffer." 611 end of (the accessible portion of) the buffer. When calling this function
612 from Lisp, you can pretend that it was called interactively by passing
613 a non-nil INTERACTIVE argument.
614
615 If a match is split across lines, all the lines it lies in are deleted.
616 They are deleted _before_ looking for the next match. Hence, a match
617 starting on the same line at which another match ended is ignored."
585 618
586 (interactive 619 (interactive
587 (progn 620 (progn
588 (barf-if-buffer-read-only) 621 (barf-if-buffer-read-only)
589 (keep-lines-read-args "Flush lines (containing match for regexp): "))) 622 (keep-lines-read-args "Flush lines (containing match for regexp): ")))
590 (if rstart 623 (if rstart
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)
598 rend (point-max-marker))) 631 rend (point-max-marker)))
599 (goto-char rstart)) 632 (goto-char rstart))
601 (isearch-no-upper-case-p regexp t)))) 634 (isearch-no-upper-case-p regexp t))))
602 (save-excursion 635 (save-excursion
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))))))
609 642 (set-marker rend nil)
610 643 nil)
611 (defun how-many (regexp &optional rstart rend) 644
612 "Print number of matches for REGEXP following point. 645
646 (defun how-many (regexp &optional rstart rend interactive)
647 "Print and return number of matches for REGEXP following point.
648 When called from Lisp and INTERACTIVE is omitted or nil, just return
649 the number, do not print it; if INTERACTIVE is t, the function behaves
650 in all respects has if it had been called interactively.
613 651
614 If REGEXP contains upper case characters (excluding those preceded by `\\'), 652 If REGEXP contains upper case characters (excluding those preceded by `\\'),
615 the matching is case-sensitive. 653 the matching is case-sensitive.
616 654
617 Second and third arg RSTART and REND specify the region to operate on. 655 Second and third arg RSTART and REND specify the region to operate on.
618 656
619 Interactively, in Transient Mark mode when the mark is active, operate 657 Interactively, in Transient Mark mode when the mark is active, operate
620 on the contents of the region. Otherwise, operate from point to the 658 on the contents of the region. Otherwise, operate from point to the
621 end of the buffer." 659 end of (the accessible portion of) the buffer.
660
661 This function starts looking for the next match from the end of
662 the previous match. Hence, it ignores matches that overlap
663 a 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
636 (case-fold-search (and case-fold-search 680 (case-fold-search (and case-fold-search
637 (isearch-no-upper-case-p regexp t)))) 681 (isearch-no-upper-case-p regexp t))))
639 (progn (setq opoint (point)) 683 (progn (setq opoint (point))
640 (re-search-forward regexp rend t))) 684 (re-search-forward regexp rend t)))
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
648 (let ((map (make-sparse-keymap))) 695 (let ((map (make-sparse-keymap)))
649 (define-key map [mouse-2] 'occur-mode-mouse-goto) 696 (define-key map [mouse-2] 'occur-mode-mouse-goto)
890 "*") 937 "*")
891 unique-p))) 938 unique-p)))
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 942 This function can not handle matches that span more than one line.
896 If a match spreads across multiple lines, all those lines are shown.
897 943
898 Each line is displayed with NLINES lines before and after, or -NLINES 944 Each line is displayed with NLINES lines before and after, or -NLINES
899 before if NLINES is negative. 945 before if NLINES is negative.
900 NLINES defaults to `list-matching-lines-default-context-lines'. 946 NLINES defaults to `list-matching-lines-default-context-lines'.
901 Interactively it is the prefix arg. 947 Interactively it is the prefix arg.
999 (if (> count 0) 1045 (if (> count 0)
1000 (progn 1046 (progn
1001 (display-buffer occur-buf) 1047 (display-buffer occur-buf)
1002 (setq next-error-last-buffer occur-buf)) 1048 (setq next-error-last-buffer occur-buf))
1003 (kill-buffer occur-buf))) 1049 (kill-buffer occur-buf)))
1004 (run-hooks 'occur-hook)) 1050 (setq buffer-read-only t)
1005 (setq buffer-read-only t) 1051 (set-buffer-modified-p nil)
1006 (set-buffer-modified-p nil)))) 1052 (run-hooks 'occur-hook)))))
1007 1053
1008 (defun occur-engine-add-prefix (lines) 1054 (defun occur-engine-add-prefix (lines)
1009 (mapcar 1055 (mapcar
1010 #'(lambda (line) 1056 #'(lambda (line)
1011 (concat " :" line "\n")) 1057 (concat " :" line "\n"))
1601 (setq isearch-lazy-highlight-last-string nil)))) 1647 (setq isearch-lazy-highlight-last-string nil))))
1602 ;; Record previous position for ^ when we move on. 1648 ;; Record previous position for ^ when we move on.
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)
1618 (match-end 0) 1664 (match-end 0)
1619 (current-buffer)) 1665 (current-buffer))