Subject: bug#25905: 25.1; Can't find regexp and replace in SVG files

From: Juri Linkov <[email-masked]>
Date: Fri, 18 Dec 2020 10:46:38 +0200

>>>> There is also the dired-do-isearch command that searches in marked files.
>>>>
>>>> OTOH, when such "making text visible to search" would be implemented
>>>> for isearch, then it will be used automatically by query-replace that
>>>> relies on the same search functions.
>>>
>>> That's true...
>>>
>>> So we basically need some new functionality for switching to text mode
>>> in image buffers when we're doing searches?
>>
>> Maybe isearch-open-necessary-overlays should detect if there is an image
>> over point, then switch the buffer to text mode.
>
> Here is one possible fix, but I'm still unsure if using image functions
> in isearch is a good style.  Perhaps a better fix would be to add this
> code to a buffer-local isearch-mode-hook in image-mode.el that runs
> when isearch starts.

Here is a more correct implementation:

diff --git a/lisp/image-mode.el b/lisp/image-mode.el
index 032ebf3873..87a8dc9ef2 100644
--- a/lisp/image-mode.el
+++ b/lisp/image-mode.el
@@ -667,6 +667,9 @@ image-mode--setup-mode
   (when image-auto-resize-on-window-resize
     (add-hook 'window-state-change-functions #'image--window-state-change nil t))
 
+  (add-function :before-while (local 'isearch-filter-predicate)
+                #'image-mode-isearch-filter)
+
   (run-mode-hooks 'image-mode-hook)
   (let ((image (image-get-display-property))
 	(msg1 (substitute-command-keys
@@ -782,6 +790,13 @@ image-toggle-display-text
     (if (called-interactively-p 'any)
 	(message "Repeat this command to go back to displaying the image"))))
 
+(defun image-mode-isearch-filter (_beg _end)
+  "Show image as text when trying to search in the image buffer."
+  (when (and (derived-mode-p 'image-mode)
+             (image-get-display-property))
+    (image-mode-as-text))
+  t)
+
 (defvar archive-superior-buffer)
 (defvar tar-superior-buffer)
 (declare-function image-flush "image.c" (spec &optional frame))