GNU bug report logs - #14013
24.3.50; dired-isearch-filenames-regexp is matching text outside filenames

Previous Next

Package: emacs;

Reported by: michael_heerdegen <at> web.de

Date: Wed, 20 Mar 2013 23:42:01 UTC

Severity: normal

Tags: patch

Merged with 29215

Found in versions 24.3.50, 26.0

Fixed in version 29.0.50

Done: Juri Linkov <juri <at> jurta.org>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Juri Linkov <juri <at> jurta.org>
To: Michael Heerdegen <michael_heerdegen <at> web.de>
Cc: Stefan Monnier <monnier <at> iro.umontreal.ca>, 14013 <at> debbugs.gnu.org
Subject: bug#14013: 24.3.50; dired-isearch-filenames-regexp is matching text outside filenames
Date: Wed, 08 Jun 2022 19:28:42 +0300
[Message part 1 (text/plain, inline)]
>> 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).
[dired-isearch-search-filenames.patch (text/x-diff, inline)]
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)

This bug report was last modified 1 year and 352 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.