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

From: Juri Linkov <[email-masked]>
Date: Wed, 08 Jun 2022 19:28:42 +0300

>> This works surprisingly well.  Maybe there are more corner cases,
>> but something already works with quick tests:
>
> Yes, not bad indeed.  Did you find any corner cases?

Below is the patch that works quite well for the most use cases.
But unfortunately, there are still some unsolvable corner cases:
when a file name is e.g. "aaxbbx" and the search regexp is "x$",
then after removing "$", searching for "x" will find the first
occurrence of "x", and will set match-data to it.  Later code
that uses string-match can adjust the found position to the
last occurrence of "x".  But it can't change match-data
used by isearch.  And using a temporary buffer won't help either
to set the real buffer positions in match-data to the last "x".
IOW, no post-processing can help the main search function
to set match-data to the correct place that matches "x$"
in the dired buffer (and file name doesn't always end at eol).
diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el
index 4faf9431aa..bb17760598 100644
--- a/lisp/dired-aux.el
+++ b/lisp/dired-aux.el
@@ -3217,7 +3217,17 @@ dired-isearch-search-filenames
                          (if isearch-forward old (max (1- old) (point-min)))
                          property)
                     old))
-             end found)
+             end found regexp regexp-^ regexp-$ (i 0))
+        (when isearch-regexp
+          (setq regexp string)
+          (while (string-match "\\(\\^\\|\\\\`\\)\\|\\$\\|\\\\'" string i)
+            (setq i (match-beginning 0))
+            (if (save-match-data (not (subregexp-context-p
+                                       string (match-beginning 0))))
+                ;; The ^/$ is inside a char-range or escaped or something.
+                (setq i (1+ i))
+              (setq string (replace-match "" t t string))
+              (if (match-beginning 1) (setq regexp-^ t) (setq regexp-$ t)))))
         ;; Otherwise, try to search for the next property.
         (unless beg
           (setq beg (if isearch-forward
@@ -3236,6 +3246,29 @@ dired-isearch-search-filenames
                                                      (max bound end))
                                            end)
                        noerror count))
+          ;; Handle ^/$ specially
+          (when (and regexp found)
+            ;; Apply ^/$ regexp on the whole filename field
+            (save-match-data
+              (if (string-match regexp (buffer-substring beg end))
+                  ;; FIXME: better to modify previous match-data
+                  (setq found (if isearch-forward
+                                  (+ beg (match-end 0))
+                                (- beg (match-end 0))))
+                (setq found nil)))
+            ;; Check ^/$ matches at filename field boundaries
+            (when found
+              (goto-char found)
+              (unless (and (or (not regexp-^)
+                               (eq (if isearch-forward beg end) (point-min))
+                               (null (get-text-property
+                                      (1- (if isearch-forward beg end)) property)))
+                           (or (not regexp-$)
+                               (eq (point) (point-max))
+                               (null (get-text-property
+                                      (point) property))))
+                (setq found nil))))
+          ;; Get the next filename field
           (unless found
             (setq beg (if isearch-forward
                           (next-single-property-change end property)