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

From: Juri Linkov <[email-masked]>
Date: Fri, 22 Mar 2013 01:03:31 +0200

> Would it be an appropriate approach to use a more sophisticated value
> for `isearch-search-fun-function' for that case?  This function could
> e.g. jump to the next filename before starting searching.

Instead of duplicating the complex logic of `isearch-search-fun-function'
in a new specialized function it would be better to implement this
using hooks.

We have already a post-processing hook `isearch-update-post-hook'
invoked after isearch has found matches.  So we need a similar hook
invoked before isearch starts searching matches, with a name like
`isearch-search-fun-pre-hook'.

It fixes your test case of searching for ".*":

=== modified file 'lisp/isearch.el'
--- lisp/isearch.el	2013-02-25 21:10:59 +0000
+++ lisp/isearch.el	2013-03-21 22:53:41 +0000
@@ -163,6 +163,9 @@ (defcustom isearch-resume-in-command-his
 (defvar isearch-mode-hook nil
   "Function(s) to call after starting up an incremental search.")
 
+(defvar isearch-search-fun-pre-hook nil
+  "Function(s) to call before isearch starts searching matches in the buffer.")
+
 (defvar isearch-update-post-hook nil
   "Function(s) to call after isearch has found matches in the buffer.")
 
@@ -2651,7 +2654,9 @@ (defun isearch-search-string (string bou
 If found, move point to the end of the occurrence,
 update the match data, and return point."
   (let* ((func (isearch-search-fun))
-         (pos1 (save-excursion (funcall func string bound noerror)))
+         (pos1 (save-excursion
+		 (run-hooks 'isearch-search-fun-pre-hook)
+		 (funcall func string bound noerror)))
          pos2)
     (when (and
 	   ;; Avoid "obsolete" warnings for translation-table-for-input.

=== modified file 'lisp/dired-aux.el'
--- lisp/dired-aux.el	2013-02-28 21:51:11 +0000
+++ lisp/dired-aux.el	2013-03-21 22:54:14 +0000
@@ -2506,6 +2506,12 @@ (defun dired-isearch-filenames-toggle ()
   (setq isearch-success t isearch-adjusted t)
   (isearch-update))
 
+(defun dired-isearch-filenames-pre-hook ()
+  (unless (get-text-property (point) 'dired-filename)
+    (if isearch-forward
+	(goto-char (or (next-single-property-change (point) 'dired-filename) (point-max)))
+      (goto-char (or (previous-single-property-change (point) 'dired-filename) (point-min))))))
+
 ;;;###autoload
 (defun dired-isearch-filenames-setup ()
   "Set up isearch to search in Dired file names.
@@ -2518,14 +2524,16 @@ (defun dired-isearch-filenames-setup ()
     (setq dired-isearch-filter-predicate-orig
 	  (default-value 'isearch-filter-predicate))
     (setq-default isearch-filter-predicate 'dired-isearch-filter-filenames)
-    (add-hook 'isearch-mode-end-hook 'dired-isearch-filenames-end nil t)))
+    (add-hook 'isearch-mode-end-hook 'dired-isearch-filenames-end nil t)
+    (add-hook 'isearch-search-fun-pre-hook 'dired-isearch-filenames-pre-hook nil t)))
 
 (defun dired-isearch-filenames-end ()
   "Clean up the Dired file name search after terminating isearch."
   (setq isearch-message-prefix-add nil)
   (define-key isearch-mode-map "\M-sf" nil)
   (setq-default isearch-filter-predicate dired-isearch-filter-predicate-orig)
-  (remove-hook 'isearch-mode-end-hook 'dired-isearch-filenames-end t))
+  (remove-hook 'isearch-mode-end-hook 'dired-isearch-filenames-end t)
+  (remove-hook 'isearch-search-fun-pre-hook 'dired-isearch-filenames-pre-hook t))
 
 (defun dired-isearch-filter-filenames (beg end)
   "Test whether the current search hit is a visible file name.

=== modified file 'lisp/wdired.el'
--- lisp/wdired.el	2013-03-05 17:13:01 +0000
+++ lisp/wdired.el	2013-03-21 22:55:11 +0000
@@ -241,6 +241,7 @@ (defun wdired-change-to-wdired-mode ()
   (set (make-local-variable 'query-replace-skip-read-only) t)
   (set (make-local-variable 'isearch-filter-predicate)
        'wdired-isearch-filter-read-only)
+  (add-hook 'isearch-search-fun-pre-hook 'dired-isearch-filenames-pre-hook nil t)
   (use-local-map wdired-mode-map)
   (force-mode-line-update)
   (setq buffer-read-only nil)

Also (run-hooks 'isearch-search-fun-pre-hook) should be added to replace.el,
but first I have to install the pending patches in bug#11378 and bug#11746.