Subject: Re: bug#14013: 24.3.50; dired-isearch-filenames-regexp is matching text outside filenames

From: Juri Linkov <[email-masked]>
Date: Wed, 15 Jun 2022 19:34:39 +0300

> BTW, do you think that such regexps as ".*" and "^.*$"
> should be supported not only in Dired buffers, but also
> when making replacements in rectangular regions?
> E.g. C-x SPC ... C-M-% .* RET RET should effectively
> clear the region, etc.

After a small refactoring, searching in rectangular regions
is implemented as well:

diff --git a/lisp/isearch.el b/lisp/isearch.el
index 7650ebcfce..2fd172e75c 100644
--- a/lisp/isearch.el
+++ b/lisp/isearch.el
@@ -4455,18 +4518,45 @@ minibuffer-lazy-highlight-setup
         (funcall after-change nil nil nil)))))
 
 
+(defun isearch-search-fun-in-noncontiguous-region (search-fun region-bounds)
+  "Return the function that searches inside noncontiguous regions."
+  (apply-partially
+   #'search-within-boundaries
+   search-fun
+   (lambda (pos)
+     (seq-some (lambda (b) (and (>= pos (car b)) (<= pos (cdr b))))
+               region-bounds))
+   (lambda (pos)
+     (let* ((bounds (flatten-list region-bounds))
+            found)
+       (unless isearch-forward
+         (setq bounds (nreverse bounds)))
+       (while (and bounds (not found))
+         (if (if isearch-forward (< pos (car bounds)) (> pos (car bounds)))
+             (setq found (car bounds))
+           (setq bounds (cdr bounds))))
+       found))))
+
 (defun isearch-search-fun-in-text-property (search-fun property)
   "Return the function to search inside text that has the specified PROPERTY.
 The function will limit the search for matches only inside text which has
 this property in the current buffer.
 The argument SEARCH-FUN provides the function to search text, and
 defaults to the value of `isearch-search-fun-default' when nil."
-  (lambda (string &optional bound noerror count)
+  (apply-partially
+   #'search-within-boundaries
+   search-fun
+   (lambda (pos) (get-text-property pos property))
+   (lambda (pos) (if isearch-forward
+                     (next-single-property-change pos property)
+                   (previous-single-property-change pos property)))))
+
+(defun search-within-boundaries ( search-fun get-fun next-fun
+                                  string &optional bound noerror count)
   (let* ((old (point))
          ;; Check if point is already on the property.
-           (beg (when (get-text-property
-                       (if isearch-forward old (max (1- old) (point-min)))
-                       property)
+         (beg (when (funcall get-fun (if isearch-forward old
+                                       (max (1- old) (point-min))))
                 old))
          end found (i 0)
          (subregexp
@@ -4480,30 +4570,23 @@ isearch-search-fun-in-text-property
                        (throw 'subregexp t))))))))
     ;; Otherwise, try to search for the next property.
     (unless beg
-        (setq beg (if isearch-forward
-                      (next-single-property-change old property)
-                    (previous-single-property-change old property)))
+      (setq beg (funcall next-fun old))
       (when beg (goto-char beg)))
     ;; Non-nil `beg' means there are more properties.
     (while (and beg (not found))
       ;; Search for the end of the current property.
-        (setq end (if isearch-forward
-                      (next-single-property-change beg property)
-                    (previous-single-property-change beg property)))
+      (setq end (funcall next-fun beg))
       ;; Handle ^/$ specially by matching in a temporary buffer.
       (if subregexp
           (let* ((prop-beg
                   (if (or (if isearch-forward (bobp) (eobp))
-                            (null (get-text-property
-                                   (+ (point) (if isearch-forward -1 0))
-                                   property)))
+                          (null (funcall get-fun (+ (point)
+                                                    (if isearch-forward -1 0)))))
                       ;; Already at the beginning of the field.
                       beg
                     ;; Get the real beginning of the field when
                     ;; the search was started in the middle.
-                      (if isearch-forward
-                          (previous-single-property-change beg property)
-                        (next-single-property-change beg property))))
+                    (funcall next-fun beg)))
                  (substring (buffer-substring prop-beg end))
                  (offset (if isearch-forward prop-beg end))
                  match-data)
@@ -4532,12 +4615,10 @@ isearch-search-fun-in-text-property
                      noerror count)))
       ;; Get the next text property.
       (unless found
-          (setq beg (if isearch-forward
-                        (next-single-property-change end property)
-                      (previous-single-property-change end property)))
+        (setq beg (funcall next-fun end))
         (when beg (goto-char beg))))
     (unless found (goto-char old))
-      found)))
+    found))
 
 
 (defun isearch-resume (string regexp word forward message case-fold)