diff options
| author | Juri Linkov | 2020-10-14 11:56:23 +0300 |
|---|---|---|
| committer | Juri Linkov | 2020-10-14 11:56:23 +0300 |
| commit | 4bf9bb56b56b4acacd5d9430a19db32291bd078b (patch) | |
| tree | 79dcab4d02f8e52fc5c0f618c80e62e416c4fc92 /lisp/replace.el | |
| parent | b13e0c1501a21e942692718194c634e01a13928a (diff) | |
| download | emacs-4bf9bb56b56b4acacd5d9430a19db32291bd078b.tar.gz emacs-4bf9bb56b56b4acacd5d9430a19db32291bd078b.zip | |
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.
Diffstat (limited to 'lisp/replace.el')
| -rw-r--r-- | lisp/replace.el | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lisp/replace.el b/lisp/replace.el index e363924501f..d34cabfe89e 100644 --- a/lisp/replace.el +++ b/lisp/replace.el | |||
| @@ -126,6 +126,18 @@ This variable affects only `query-replace-regexp'." | |||
| 126 | :type 'boolean | 126 | :type 'boolean |
| 127 | :group 'matching) | 127 | :group 'matching) |
| 128 | 128 | ||
| 129 | (defcustom query-replace-highlight-submatches t | ||
| 130 | "Whether to highlight regexp subexpressions during query replacement. | ||
| 131 | The faces used to do the highlights are named `isearch-group-1', | ||
| 132 | `isearch-group-2', etc. (By default, only these 2 are defined.) | ||
| 133 | When there are more matches than faces, then faces are reused from the | ||
| 134 | beginning, in a cyclical manner, so the `isearch-group-1' face is | ||
| 135 | isreused for the third match. If you want to use more distinctive colors, | ||
| 136 | you can define more of these faces using the same numbering scheme." | ||
| 137 | :type 'boolean | ||
| 138 | :group 'matching | ||
| 139 | :version "28.1") | ||
| 140 | |||
| 129 | (defcustom query-replace-lazy-highlight t | 141 | (defcustom query-replace-lazy-highlight t |
| 130 | "Controls the lazy-highlighting during query replacements. | 142 | "Controls the lazy-highlighting during query replacements. |
| 131 | When non-nil, all text in the buffer matching the current match | 143 | 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 | |||
| 2403 | (funcall search-function search-string limit t))) | 2415 | (funcall search-function search-string limit t))) |
| 2404 | 2416 | ||
| 2405 | (defvar replace-overlay nil) | 2417 | (defvar replace-overlay nil) |
| 2418 | (defvar replace-submatches-overlays nil) | ||
| 2406 | 2419 | ||
| 2407 | (defun replace-highlight (match-beg match-end range-beg range-end | 2420 | (defun replace-highlight (match-beg match-end range-beg range-end |
| 2408 | search-string regexp-flag delimited-flag | 2421 | search-string regexp-flag delimited-flag |
| @@ -2413,6 +2426,25 @@ It is called with three arguments, as if it were | |||
| 2413 | (setq replace-overlay (make-overlay match-beg match-end)) | 2426 | (setq replace-overlay (make-overlay match-beg match-end)) |
| 2414 | (overlay-put replace-overlay 'priority 1001) ;higher than lazy overlays | 2427 | (overlay-put replace-overlay 'priority 1001) ;higher than lazy overlays |
| 2415 | (overlay-put replace-overlay 'face 'query-replace))) | 2428 | (overlay-put replace-overlay 'face 'query-replace))) |
| 2429 | |||
| 2430 | (when (and query-replace-highlight-submatches | ||
| 2431 | regexp-flag) | ||
| 2432 | (mapc 'delete-overlay replace-submatches-overlays) | ||
| 2433 | (setq replace-submatches-overlays nil) | ||
| 2434 | (let ((submatch-data (cddr (butlast (match-data t)))) | ||
| 2435 | (group 0) | ||
| 2436 | ov face) | ||
| 2437 | (while submatch-data | ||
| 2438 | (setq group (1+ group)) | ||
| 2439 | (setq ov (make-overlay (pop submatch-data) (pop submatch-data)) | ||
| 2440 | face (intern-soft (format "isearch-group-%d" group))) | ||
| 2441 | ;; Recycle faces from beginning. | ||
| 2442 | (unless (facep face) | ||
| 2443 | (setq group 1 face 'isearch-group-1)) | ||
| 2444 | (overlay-put ov 'face face) | ||
| 2445 | (overlay-put ov 'priority 1002) | ||
| 2446 | (push ov replace-submatches-overlays)))) | ||
| 2447 | |||
| 2416 | (if query-replace-lazy-highlight | 2448 | (if query-replace-lazy-highlight |
| 2417 | (let ((isearch-string search-string) | 2449 | (let ((isearch-string search-string) |
| 2418 | (isearch-regexp regexp-flag) | 2450 | (isearch-regexp regexp-flag) |
| @@ -2433,6 +2465,9 @@ It is called with three arguments, as if it were | |||
| 2433 | (defun replace-dehighlight () | 2465 | (defun replace-dehighlight () |
| 2434 | (when replace-overlay | 2466 | (when replace-overlay |
| 2435 | (delete-overlay replace-overlay)) | 2467 | (delete-overlay replace-overlay)) |
| 2468 | (when query-replace-highlight-submatches | ||
| 2469 | (mapc 'delete-overlay replace-submatches-overlays) | ||
| 2470 | (setq replace-submatches-overlays nil)) | ||
| 2436 | (when query-replace-lazy-highlight | 2471 | (when query-replace-lazy-highlight |
| 2437 | (lazy-highlight-cleanup lazy-highlight-cleanup) | 2472 | (lazy-highlight-cleanup lazy-highlight-cleanup) |
| 2438 | (setq isearch-lazy-highlight-last-string nil)) | 2473 | (setq isearch-lazy-highlight-last-string nil)) |