emacs-devel
[Top][All Lists]
Advanced

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

query-replace highlighting


From: Juri Linkov
Subject: query-replace highlighting
Date: Fri, 10 Dec 2004 04:06:33 +0200
User-agent: Gnus/5.110002 (No Gnus v0.2) Emacs/21.3.50 (gnu/linux)

isearch and query-replace are similar operations in the sense that
both of them search for a string and highlight the current string.
But query-replace misses a useful feature of isearch which highlights
all other matches in a window using `isearch-lazy-highlight-face'.
This can be added to query-replace as well with a simple change in
replace.el.  The value `isearch' of the `query-replace-highlight'
variable specifies that query-replace uses isearch highlighting.

While testing it, I noticed a small bug in perform-replace:
when automatic replacement is requested with ! in literal mode,
it still highlights every remaining occurrence quickly while
automatically replacing them.  This is especially well visible with
isearch highlighting while every replacement flashes in isearch face.
I think the following patch fixes it correctly:

Index: lisp/replace.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/replace.el,v
retrieving revision 1.192
diff -u -r1.192 replace.el
--- lisp/replace.el     3 Dec 2004 00:19:52 -0000       1.192
+++ lisp/replace.el     10 Dec 2004 01:12:54 -0000
@@ -1380,7 +1419,7 @@
            (if (not query-flag)
                (let ((inhibit-read-only
                       query-replace-skip-read-only))
-                 (unless noedit
+                 (unless (or literal noedit)
                    (replace-highlight (nth 0 real-match-data)
                                       (nth 1 real-match-data)))
                  (setq noedit

And here is a patch for isearch highlighting in perform-replace:

Index: lisp/replace.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/replace.el,v
retrieving revision 1.192
diff -u -r1.192 replace.el
--- lisp/replace.el     3 Dec 2004 00:19:52 -0000       1.192
+++ lisp/replace.el     10 Dec 2004 02:15:07 -0000
@@ -1281,6 +1311,9 @@
        ;; (match-data); otherwise it is t if a match is possible at point.
        (match-again t)
 
+       (isearch-string nil)
+       (isearch-regexp nil)
+
        (message
         (if query-flag
             (substitute-command-keys
@@ -1313,6 +1346,11 @@
                                    (if regexp-flag from-string
                                      (regexp-quote from-string))
                                    "\\b")))
+
+    (if (eq query-replace-highlight 'isearch)
+       (setq isearch-string search-string
+             isearch-regexp regexp-flag))
+
     (push-mark)
     (undo-boundary)
     (unwind-protect
@@ -1528,7 +1569,14 @@
                         (setq unread-command-events
                               (append (listify-key-sequence key)
                                       unread-command-events))
-                        (setq done t))))
+                        (setq done t)))
+                 (when (eq query-replace-highlight 'isearch)
+                   ;; Force isearch rehighlighting
+                   (if (not (memq def '(skip backup)))
+                       (setq isearch-lazy-highlight-last-string nil))
+                   ;; Restore isearch data in case of isearching during edit
+                   (setq isearch-string search-string
+                         isearch-regexp regexp-flag)))
                ;; Record previous position for ^ when we move on.
                ;; Change markers to numbers in the match data
                ;; since lots of markers slow down editing.
@@ -1563,27 +1611,38 @@
                 (if (= replace-count 1) "" "s")))
     (and keep-going stack)))
 
-(defcustom query-replace-highlight t
-  "*Non-nil means to highlight words during query replacement."
-  :type 'boolean
+(defcustom query-replace-highlight
+  (if (and search-highlight isearch-lazy-highlight) 'isearch t)
+  "*Non-nil means to highlight words during query replacement.
+If `isearch', use isearch highlighting for query replacement."
+  :type '(choice (const :tag "Highlight" t)
+                 (const :tag "No highlighting" nil)
+                 (const :tag "Isearch highlighting" 'isearch))
   :group 'matching)
 
 (defvar replace-overlay nil)
 
 (defun replace-dehighlight ()
-  (and replace-overlay
-       (progn
-        (delete-overlay replace-overlay)
-        (setq replace-overlay nil))))
+  (cond ((eq query-replace-highlight 'isearch)
+        (setq isearch-lazy-highlight-last-string nil)
+        (isearch-dehighlight t)
+        (isearch-lazy-highlight-cleanup isearch-lazy-highlight-cleanup))
+       (query-replace-highlight
+        (when replace-overlay
+          (delete-overlay replace-overlay)
+          (setq replace-overlay nil)))))
 
 (defun replace-highlight (start end)
-  (and query-replace-highlight
-       (if replace-overlay
-          (move-overlay replace-overlay start end (current-buffer))
-        (setq replace-overlay (make-overlay start end))
-        (overlay-put replace-overlay 'face
-                     (if (facep 'query-replace)
-                         'query-replace 'region)))))
+  (cond ((eq query-replace-highlight 'isearch)
+        (isearch-highlight start end)
+        (isearch-lazy-highlight-new-loop))
+       (query-replace-highlight
+        (if replace-overlay
+            (move-overlay replace-overlay start end (current-buffer))
+          (setq replace-overlay (make-overlay start end))
+          (overlay-put replace-overlay 'face
+                       (if (facep 'query-replace)
+                           'query-replace 'region))))))
 
 ;; arch-tag: 16b4cd61-fd40-497b-b86f-b667c4cf88e4
 ;;; replace.el ends here

-- 
Juri Linkov
http://www.jurta.org/emacs/





reply via email to

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