aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/replace.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/replace.el')
-rw-r--r--lisp/replace.el166
1 files changed, 99 insertions, 67 deletions
diff --git a/lisp/replace.el b/lisp/replace.el
index 646f693cd7f..775ad0ffb05 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -735,16 +735,17 @@ Alternatively, click \\[occur-mode-mouse-goto] on an item to go to it.
735Compatibility function for \\[next-error] invocations." 735Compatibility function for \\[next-error] invocations."
736 (interactive "p") 736 (interactive "p")
737 ;; we need to run occur-find-match from within the Occur buffer 737 ;; we need to run occur-find-match from within the Occur buffer
738 (with-current-buffer 738 (with-current-buffer
739 (if (next-error-buffer-p (current-buffer)) 739 (if (next-error-buffer-p (current-buffer))
740 (current-buffer) 740 (current-buffer)
741 (next-error-find-buffer nil nil (lambda() (eq major-mode 'occur-mode)))) 741 (next-error-find-buffer nil nil (lambda() (eq major-mode 'occur-mode))))
742 742
743 (when reset 743 (goto-char (cond (reset (point-min))
744 (goto-char (point-min))) 744 ((< argp 0) (line-beginning-position))
745 ((line-end-position))))
745 (occur-find-match 746 (occur-find-match
746 (abs (prefix-numeric-value argp)) 747 (abs argp)
747 (if (> 0 (prefix-numeric-value argp)) 748 (if (> 0 argp)
748 #'previous-single-property-change 749 #'previous-single-property-change
749 #'next-single-property-change) 750 #'next-single-property-change)
750 "No more matches") 751 "No more matches")
@@ -752,6 +753,20 @@ Compatibility function for \\[next-error] invocations."
752 (set-window-point (get-buffer-window (current-buffer)) (point)) 753 (set-window-point (get-buffer-window (current-buffer)) (point))
753 (occur-mode-goto-occurrence))) 754 (occur-mode-goto-occurrence)))
754 755
756(defface match
757 '((((class color) (min-colors 88) (background light))
758 :background "Tan")
759 (((class color) (min-colors 88) (background dark))
760 :background "RoyalBlue4")
761 (((class color) (min-colors 8))
762 :background "blue" :foreground "white")
763 (((type tty) (class mono))
764 :inverse-video t)
765 (t :background "gray"))
766 "Face used to highlight matches permanently."
767 :group 'matching
768 :version "21.4")
769
755(defcustom list-matching-lines-default-context-lines 0 770(defcustom list-matching-lines-default-context-lines 0
756 "*Default number of context lines included around `list-matching-lines' matches. 771 "*Default number of context lines included around `list-matching-lines' matches.
757A negative number means to include that many lines before the match. 772A negative number means to include that many lines before the match.
@@ -761,7 +776,7 @@ A positive number means to include that many lines both before and after."
761 776
762(defalias 'list-matching-lines 'occur) 777(defalias 'list-matching-lines 'occur)
763 778
764(defcustom list-matching-lines-face 'bold 779(defcustom list-matching-lines-face 'match
765 "*Face used by \\[list-matching-lines] to show the text that matches. 780 "*Face used by \\[list-matching-lines] to show the text that matches.
766If the value is nil, don't highlight the matching portions specially." 781If the value is nil, don't highlight the matching portions specially."
767 :type 'face 782 :type 'face
@@ -776,18 +791,22 @@ If the value is nil, don't highlight the buffer names specially."
776(defun occur-accumulate-lines (count &optional keep-props) 791(defun occur-accumulate-lines (count &optional keep-props)
777 (save-excursion 792 (save-excursion
778 (let ((forwardp (> count 0)) 793 (let ((forwardp (> count 0))
779 (result nil)) 794 result beg end)
780 (while (not (or (zerop count) 795 (while (not (or (zerop count)
781 (if forwardp 796 (if forwardp
782 (eobp) 797 (eobp)
783 (bobp)))) 798 (bobp))))
784 (setq count (+ count (if forwardp -1 1))) 799 (setq count (+ count (if forwardp -1 1)))
800 (setq beg (line-beginning-position)
801 end (line-end-position))
802 (if (and keep-props (boundp 'jit-lock-mode) jit-lock-mode
803 (text-property-not-all beg end 'fontified t))
804 (jit-lock-fontify-now beg end))
785 (push 805 (push
786 (funcall (if keep-props 806 (funcall (if keep-props
787 #'buffer-substring 807 #'buffer-substring
788 #'buffer-substring-no-properties) 808 #'buffer-substring-no-properties)
789 (line-beginning-position) 809 beg end)
790 (line-end-position))
791 result) 810 result)
792 (forward-line (if forwardp 1 -1))) 811 (forward-line (if forwardp 1 -1)))
793 (nreverse result)))) 812 (nreverse result))))
@@ -982,14 +1001,17 @@ See also `multi-occur'."
982 (when (setq endpt (re-search-forward regexp nil t)) 1001 (when (setq endpt (re-search-forward regexp nil t))
983 (setq matches (1+ matches)) ;; increment match count 1002 (setq matches (1+ matches)) ;; increment match count
984 (setq matchbeg (match-beginning 0)) 1003 (setq matchbeg (match-beginning 0))
985 (setq begpt (save-excursion
986 (goto-char matchbeg)
987 (line-beginning-position)))
988 (setq lines (+ lines (1- (count-lines origpt endpt)))) 1004 (setq lines (+ lines (1- (count-lines origpt endpt))))
1005 (save-excursion
1006 (goto-char matchbeg)
1007 (setq begpt (line-beginning-position)
1008 endpt (line-end-position)))
989 (setq marker (make-marker)) 1009 (setq marker (make-marker))
990 (set-marker marker matchbeg) 1010 (set-marker marker matchbeg)
991 (setq curstring (buffer-substring begpt 1011 (if (and keep-props (boundp 'jit-lock-mode) jit-lock-mode
992 (line-end-position))) 1012 (text-property-not-all begpt endpt 'fontified t))
1013 (jit-lock-fontify-now begpt endpt))
1014 (setq curstring (buffer-substring begpt endpt))
993 ;; Depropertize the string, and maybe 1015 ;; Depropertize the string, and maybe
994 ;; highlight the matches 1016 ;; highlight the matches
995 (let ((len (length curstring)) 1017 (let ((len (length curstring))
@@ -998,17 +1020,15 @@ See also `multi-occur'."
998 (set-text-properties 0 len nil curstring)) 1020 (set-text-properties 0 len nil curstring))
999 (while (and (< start len) 1021 (while (and (< start len)
1000 (string-match regexp curstring start)) 1022 (string-match regexp curstring start))
1001 (add-text-properties (match-beginning 0) 1023 (add-text-properties
1002 (match-end 0) 1024 (match-beginning 0) (match-end 0)
1003 (append 1025 (append
1004 `(occur-match t) 1026 `(occur-match t)
1005 (when match-face 1027 (when match-face
1006 ;; Use `face' rather than 1028 ;; Use `face' rather than `font-lock-face' here
1007 ;; `font-lock-face' here 1029 ;; so as to override faces copied from the buffer.
1008 ;; so as to override faces 1030 `(face ,match-face)))
1009 ;; copied from the buffer. 1031 curstring)
1010 `(face ,match-face)))
1011 curstring)
1012 (setq start (match-end 0)))) 1032 (setq start (match-end 0))))
1013 ;; Generate the string to insert for this match 1033 ;; Generate the string to insert for this match
1014 (let* ((out-line 1034 (let* ((out-line
@@ -1019,7 +1039,10 @@ See also `multi-occur'."
1019 (when prefix-face 1039 (when prefix-face
1020 `(font-lock-face prefix-face)) 1040 `(font-lock-face prefix-face))
1021 '(occur-prefix t))) 1041 '(occur-prefix t)))
1022 curstring 1042 ;; We don't put `mouse-face' on the newline,
1043 ;; because that loses. And don't put it
1044 ;; on context lines to reduce flicker.
1045 (propertize curstring 'mouse-face 'highlight)
1023 "\n")) 1046 "\n"))
1024 (data 1047 (data
1025 (if (= nlines 0) 1048 (if (= nlines 0)
@@ -1043,10 +1066,7 @@ See also `multi-occur'."
1043 (insert "-------\n")) 1066 (insert "-------\n"))
1044 (add-text-properties 1067 (add-text-properties
1045 beg end 1068 beg end
1046 `(occur-target ,marker help-echo "mouse-2: go to this occurrence")) 1069 `(occur-target ,marker help-echo "mouse-2: go to this occurrence")))))
1047 ;; We don't put `mouse-face' on the newline,
1048 ;; because that loses.
1049 (add-text-properties beg (1- end) '(mouse-face highlight)))))
1050 (goto-char endpt)) 1070 (goto-char endpt))
1051 (if endpt 1071 (if endpt
1052 (progn 1072 (progn
@@ -1283,6 +1303,7 @@ make, or the user didn't cancel the call."
1283 1303
1284 (isearch-string isearch-string) 1304 (isearch-string isearch-string)
1285 (isearch-regexp isearch-regexp) 1305 (isearch-regexp isearch-regexp)
1306 (isearch-case-fold-search isearch-case-fold-search)
1286 (message 1307 (message
1287 (if query-flag 1308 (if query-flag
1288 (substitute-command-keys 1309 (substitute-command-keys
@@ -1315,9 +1336,11 @@ make, or the user didn't cancel the call."
1315 (if regexp-flag from-string 1336 (if regexp-flag from-string
1316 (regexp-quote from-string)) 1337 (regexp-quote from-string))
1317 "\\b"))) 1338 "\\b")))
1318 (if (eq query-replace-highlight 'isearch) 1339 (when query-replace-lazy-highlight
1319 (setq isearch-string search-string 1340 (setq isearch-string search-string
1320 isearch-regexp regexp-flag)) 1341 isearch-regexp (or delimited-flag regexp-flag)
1342 isearch-case-fold-search case-fold-search
1343 isearch-lazy-highlight-last-string nil))
1321 1344
1322 (push-mark) 1345 (push-mark)
1323 (undo-boundary) 1346 (undo-boundary)
@@ -1535,13 +1558,15 @@ make, or the user didn't cancel the call."
1535 (append (listify-key-sequence key) 1558 (append (listify-key-sequence key)
1536 unread-command-events)) 1559 unread-command-events))
1537 (setq done t))) 1560 (setq done t)))
1538 (when (eq query-replace-highlight 'isearch) 1561 (when query-replace-lazy-highlight
1539 ;; Force isearch rehighlighting 1562 ;; Restore isearch data for lazy highlighting
1540 (if (not (memq def '(skip backup))) 1563 ;; in case of isearching during recursive edit
1541 (setq isearch-lazy-highlight-last-string nil))
1542 ;; Restore isearch data in case of isearching during edit
1543 (setq isearch-string search-string 1564 (setq isearch-string search-string
1544 isearch-regexp regexp-flag))) 1565 isearch-regexp (or delimited-flag regexp-flag)
1566 isearch-case-fold-search case-fold-search)
1567 ;; Force lazy rehighlighting only after replacements
1568 (if (not (memq def '(skip backup)))
1569 (setq isearch-lazy-highlight-last-string nil))))
1545 ;; Record previous position for ^ when we move on. 1570 ;; Record previous position for ^ when we move on.
1546 ;; Change markers to numbers in the match data 1571 ;; Change markers to numbers in the match data
1547 ;; since lots of markers slow down editing. 1572 ;; since lots of markers slow down editing.
@@ -1576,38 +1601,45 @@ make, or the user didn't cancel the call."
1576 (if (= replace-count 1) "" "s"))) 1601 (if (= replace-count 1) "" "s")))
1577 (and keep-going stack))) 1602 (and keep-going stack)))
1578 1603
1579(defcustom query-replace-highlight 1604(defcustom query-replace-highlight t
1580 (if (and search-highlight isearch-lazy-highlight) 'isearch t) 1605 "*Non-nil means to highlight matches during query replacement."
1581 "*Non-nil means to highlight words during query replacement. 1606 :type 'boolean
1582If `isearch', use isearch highlighting for query replacement."
1583 :type '(choice (const :tag "Highlight" t)
1584 (const :tag "No highlighting" nil)
1585 (const :tag "Isearch highlighting" 'isearch))
1586 :group 'matching) 1607 :group 'matching)
1587 1608
1609(defcustom query-replace-lazy-highlight t
1610 "*Controls the lazy-highlighting during query replacements.
1611When non-nil, all text in the buffer matching the current match
1612is highlighted lazily using isearch lazy highlighting (see
1613`isearch-lazy-highlight-initial-delay' and
1614`isearch-lazy-highlight-interval')."
1615 :type 'boolean
1616 :group 'matching
1617 :version "21.4")
1618
1619(defface query-replace
1620 '((t (:inherit isearch)))
1621 "Face for highlighting query replacement matches."
1622 :group 'matching
1623 :version "21.4")
1624
1588(defvar replace-overlay nil) 1625(defvar replace-overlay nil)
1589 1626
1627(defun replace-highlight (beg end)
1628 (if query-replace-highlight
1629 (if replace-overlay
1630 (move-overlay replace-overlay beg end (current-buffer))
1631 (setq replace-overlay (make-overlay beg end))
1632 (overlay-put replace-overlay 'priority 1) ;higher than lazy overlays
1633 (overlay-put replace-overlay 'face 'query-replace)))
1634 (if query-replace-lazy-highlight
1635 (isearch-lazy-highlight-new-loop)))
1636
1590(defun replace-dehighlight () 1637(defun replace-dehighlight ()
1591 (cond ((eq query-replace-highlight 'isearch) 1638 (when replace-overlay
1592 (isearch-dehighlight t) 1639 (delete-overlay replace-overlay))
1593 (isearch-lazy-highlight-cleanup isearch-lazy-highlight-cleanup) 1640 (when query-replace-lazy-highlight
1594 (setq isearch-lazy-highlight-last-string nil)) 1641 (isearch-lazy-highlight-cleanup isearch-lazy-highlight-cleanup)
1595 (query-replace-highlight 1642 (setq isearch-lazy-highlight-last-string nil)))
1596 (when replace-overlay
1597 (delete-overlay replace-overlay)
1598 (setq replace-overlay nil)))))
1599
1600(defun replace-highlight (start end)
1601 (cond ((eq query-replace-highlight 'isearch)
1602 (isearch-highlight start end)
1603 (isearch-lazy-highlight-new-loop))
1604 (query-replace-highlight
1605 (if replace-overlay
1606 (move-overlay replace-overlay start end (current-buffer))
1607 (setq replace-overlay (make-overlay start end))
1608 (overlay-put replace-overlay 'face
1609 (if (facep 'query-replace)
1610 'query-replace 'region))))))
1611 1643
1612;; arch-tag: 16b4cd61-fd40-497b-b86f-b667c4cf88e4 1644;; arch-tag: 16b4cd61-fd40-497b-b86f-b667c4cf88e4
1613;;; replace.el ends here 1645;;; replace.el ends here