unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@jurta.org>
Subject: query-replace highlighting
Date: Fri, 10 Dec 2004 04:06:33 +0200	[thread overview]
Message-ID: <87ekhy6h5i.fsf@jurta.org> (raw)

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	other threads:[~2004-12-10  2:06 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-10  2:06 Juri Linkov [this message]
2004-12-13 19:51 ` query-replace highlighting Richard Stallman
2004-12-14 10:34   ` Juri Linkov
2004-12-15 14:58     ` Richard Stallman
2004-12-15 12:00   ` Juri Linkov
2005-01-12  1:54 ` Juri Linkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ekhy6h5i.fsf@jurta.org \
    --to=juri@jurta.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).