From e718d3a84920f545b6a3540a3ba9c2ccd7eefdf7 Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Tue, 19 Jan 2021 20:12:47 +0200 Subject: [PATCH] Better check for nil in search-/query-replace-highlight-submatches (bug#45973) * lisp/isearch.el (isearch-highlight): * lisp/replace.el (replace-highlight): Use integer-or-marker-p to check matches. --- lisp/isearch.el | 30 +++++++++++++++++------------- lisp/replace.el | 30 +++++++++++++++++------------- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/lisp/isearch.el b/lisp/isearch.el index c6f7fe7bd4a..a86678572c4 100644 --- a/lisp/isearch.el +++ b/lisp/isearch.el @@ -3757,23 +3757,27 @@ since they have special meaning in a regexp." (overlay-put isearch-overlay 'priority 1001) (overlay-put isearch-overlay 'face isearch-face))) - (when (and search-highlight-submatches - isearch-regexp) + (when (and search-highlight-submatches isearch-regexp) (mapc 'delete-overlay isearch-submatches-overlays) (setq isearch-submatches-overlays nil) - (let ((submatch-data (cddr (butlast match-data))) + ;; 'cddr' removes whole expression match from match-data + (let ((submatch-data (cddr match-data)) (group 0) - ov face) + b e 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 isearch-submatches-overlays))))) + (setq b (pop submatch-data) + e (pop submatch-data)) + (when (and (integer-or-marker-p b) + (integer-or-marker-p e)) + (setq ov (make-overlay b e) + group (1+ group) + 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 isearch-submatches-overlays)))))) (defun isearch-dehighlight () (when isearch-overlay diff --git a/lisp/replace.el b/lisp/replace.el index 8f8cbfac542..db5b340631a 100644 --- a/lisp/replace.el +++ b/lisp/replace.el @@ -2425,23 +2425,27 @@ It is called with three arguments, as if it were (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) + (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)))) + ;; 'cddr' removes whole expression match from match-data + (let ((submatch-data (cddr (match-data t))) (group 0) - ov face) + b e 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)))) + (setq b (pop submatch-data) + e (pop submatch-data)) + (when (and (integer-or-marker-p b) + (integer-or-marker-p e)) + (setq ov (make-overlay b e) + group (1+ group) + 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) -- 2.39.5