Subject: bug#49842: re-builder restriction to region (lisp/emacs-lisp/re-builder)

From: Karthik Chikmagalur <[email-masked]>
Date: Mon, 02 Aug 2021 21:03:31 -0700

Hello,

The function "reb-update-overlays" (in lisp/emacs-lisp/re-builder.el)
displays matches in the whole of reb-target-buffer starting at
(point-min) even when the region is active. In keeping with the behavior
of commands like query-replace, replace-regexp and query-replace-regexp,
I modified it so that when the region is active re-builder only displays
matches in the active region. It's behavior is unchanged when the region is
inactive.

I think this change makes sense without disrupting the expectations of
re-builder users.

More generally, I think re-builder needs some work:

1. When the user quits re-builder, the point in reb-target-buffer is not
restored correctly.

2. Without an active region, there should be an option to match forward
or backward from reb-target-buffer's point instead of always matching
from (point-min), with the ability to customize the default behavior.

3. re-builder's overlay system (or some modification of it) can be used
by query-replace-regexp to show matches as the user types.

I am waiting to sign the copyright papers from FSF (which I have
applied for) before sending in these larger patches.

Commit log entry:

* lisp/emacs-lisp/re-builder.el (reb-update-overlays): Restrict
  re-builder matches to region when the region is active.
  
Karthik Chikmagalur
diff -u emacs-src/lisp/emacs-lisp/re-builder.el emacs-src/lisp/emacs-lisp/re-builder-new.el
--- /lisp/emacs-lisp/re-builder.el	2021-08-02 20:47:39.226669281 -0700
+++ /lisp/emacs-lisp/re-builder-new.el	2021-08-02 20:37:27.442020958 -0700
@@ -642,16 +642,19 @@
 	 (submatches 0)
 	 firstmatch
          here
+         start end
          firstmatch-after-here)
     (with-current-buffer reb-target-buffer
         (setq here
               (if reb-target-window
                   (with-selected-window reb-target-window (window-point))
-                (point)))
+                (point))
+              start (if (region-active-p) (region-beginning) (point-min))
+              end   (if (region-active-p) (region-end) (point-max)))
       (reb-delete-overlays)
-      (goto-char (point-min))
+      (goto-char start)
       (while (and (not (eobp))
-		  (re-search-forward re (point-max) t)
+		  (re-search-forward re end t)
 		  (or (not reb-auto-match-limit)
 		      (< matches reb-auto-match-limit)))
 	(when (and (= 0 (length (match-string 0)))

Diff finished.  Mon Aug  2 20:48:01 2021