emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 4bf9bb5: Highlight regexp sub-expressions in query-replace


From: Juri Linkov
Subject: master 4bf9bb5: Highlight regexp sub-expressions in query-replace
Date: Wed, 14 Oct 2020 04:56:34 -0400 (EDT)

branch: master
commit 4bf9bb56b56b4acacd5d9430a19db32291bd078b
Author: Juri Linkov <juri@linkov.net>
Commit: Juri Linkov <juri@linkov.net>

    Highlight regexp sub-expressions in query-replace
    
    * lisp/replace.el (query-replace-highlight-submatches): New defcustom.
    (replace-submatches-overlays): New variable.
    (replace-highlight): Use query-replace-highlight-submatches.
    (replace-dehighlight): Use query-replace-highlight-submatches.
    
    * doc/emacs/search.texi (Query Replace):
    Add documentation for query-replace-highlight-submatches.
    
    Suggested by Drew Adams <drew.adams@oracle.com> in bug#43702.
---
 doc/emacs/search.texi |  5 +++++
 etc/NEWS              |  5 +++++
 lisp/replace.el       | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 45 insertions(+)

diff --git a/doc/emacs/search.texi b/doc/emacs/search.texi
index 2169a41..91b433f 100644
--- a/doc/emacs/search.texi
+++ b/doc/emacs/search.texi
@@ -1662,6 +1662,7 @@ reused.
 @cindex @code{query-replace} face
 @cindex @code{lazy-highlight} face, in replace
 @vindex query-replace-highlight
+@vindex query-replace-highlight-submatches
 @vindex query-replace-lazy-highlight
 @vindex query-replace-show-replacement
   These commands highlight the current match using the face
@@ -1674,6 +1675,10 @@ other matches using @code{lazy-highlight} just like 
incremental search
 string for the current match in the minibuffer.  If you want to keep
 special sequences @samp{\&} and @samp{\@var{n}} unexpanded, customize
 @code{query-replace-show-replacement} variable.
+Like @code{search-highlight-submatches} highlights subexpressions in
+incremental search (@pxref{Search Customizations}), the variable
+@code{query-replace-highlight-submatches} defines whether to highlight
+subexpressions in the regexp replacement commands.
 
 @vindex query-replace-skip-read-only
   The variable @code{query-replace-skip-read-only}, if set
diff --git a/etc/NEWS b/etc/NEWS
index 0ee69d9..cb35dc8 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1163,6 +1163,11 @@ This is controlled by the 'search-highlight-submatches' 
user option.
 This feature is available only on terminals that have enough colors to
 distinguish between sub-expression highlighting.
 
++++
+*** Interactive regular expression replace now uses faces for sub-groups.
+Like 'search-highlight-submatches', this is controlled by the new user option
+'query-replace-highlight-submatches'.
+
 ---
 *** New user option 'reveal-auto-hide'.
 If non-nil (the default), revealed text is automatically hidden when
diff --git a/lisp/replace.el b/lisp/replace.el
index e363924..d34cabf 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -126,6 +126,18 @@ This variable affects only `query-replace-regexp'."
   :type 'boolean
   :group 'matching)
 
+(defcustom query-replace-highlight-submatches t
+  "Whether to highlight regexp subexpressions during query replacement.
+The faces used to do the highlights are named `isearch-group-1',
+`isearch-group-2', etc.  (By default, only these 2 are defined.)
+When there are more matches than faces, then faces are reused from the
+beginning, in a cyclical manner, so the `isearch-group-1' face is
+isreused for the third match.  If you want to use more distinctive colors,
+you can define more of these faces using the same numbering scheme."
+  :type 'boolean
+  :group 'matching
+  :version "28.1")
+
 (defcustom query-replace-lazy-highlight t
   "Controls the lazy-highlighting during query replacements.
 When non-nil, all text in the buffer matching the current match
@@ -2403,6 +2415,7 @@ It is called with three arguments, as if it were
     (funcall search-function search-string limit t)))
 
 (defvar replace-overlay nil)
+(defvar replace-submatches-overlays nil)
 
 (defun replace-highlight (match-beg match-end range-beg range-end
                          search-string regexp-flag delimited-flag
@@ -2413,6 +2426,25 @@ It is called with three arguments, as if it were
        (setq replace-overlay (make-overlay match-beg match-end))
        (overlay-put replace-overlay 'priority 1001) ;higher than lazy overlays
        (overlay-put replace-overlay 'face 'query-replace)))
+
+  (when (and query-replace-highlight-submatches
+            regexp-flag)
+    (mapc 'delete-overlay replace-submatches-overlays)
+    (setq replace-submatches-overlays nil)
+    (let ((submatch-data (cddr (butlast (match-data t))))
+          (group 0)
+          ov face)
+      (while submatch-data
+        (setq group (1+ group))
+        (setq ov (make-overlay (pop submatch-data) (pop submatch-data))
+              face (intern-soft (format "isearch-group-%d" group)))
+        ;; Recycle faces from beginning.
+        (unless (facep face)
+          (setq group 1 face 'isearch-group-1))
+        (overlay-put ov 'face face)
+        (overlay-put ov 'priority 1002)
+        (push ov replace-submatches-overlays))))
+
   (if query-replace-lazy-highlight
       (let ((isearch-string search-string)
            (isearch-regexp regexp-flag)
@@ -2433,6 +2465,9 @@ It is called with three arguments, as if it were
 (defun replace-dehighlight ()
   (when replace-overlay
     (delete-overlay replace-overlay))
+  (when query-replace-highlight-submatches
+    (mapc 'delete-overlay replace-submatches-overlays)
+    (setq replace-submatches-overlays nil))
   (when query-replace-lazy-highlight
     (lazy-highlight-cleanup lazy-highlight-cleanup)
     (setq isearch-lazy-highlight-last-string nil))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]