(defun isearch-count-message ()
(when isearch-success
(let* ((string isearch-string))
(when (>= (length string) 1)
(let ((before (count-matches string (point-min) (point)))
(after (count-matches string (point) (point-max))))
(setq isearch-message-suffix-add
(propertize (format " (%d of %d)"
before
(+ before
after))
'face 'shadow)))))))
(add-hook 'isearch-update-post-hook 'isearch-count-message)
The initial message is provided by isearch-string, which before typing is that one you used last time. This is why before re-typing you see a wrong number. This can be corrected quite easy.
The initial bad message can be corrected by resetting the value of isearch-message-suffix-add
in the hook named isearch-exit-mode
.
UPDATE:
This code is the best one can write. I looked at it today. The edit string is not the same as isearch-string
, but it's delayed by 1 character. In order to have a correct display, one needs to type C-s or C-r 2 times, for the edit-string to be the same as isearch-string. Isearch does not export in the external environment a variable to have the value of edit-string.
(defun isearch-display-count-matches ()
(if isearch-just-started
(setq isearch-message-suffix-add "")
(let ((before (count-matches isearch-string (point-min) (point)))
(after (count-matches isearch-string (point) (point-max))))
(setq isearch-message-suffix-add
(propertize (format " -%s- (%d of %d)" isearch-string
before (+ before after))
'face 'isearch-face)))))
(add-hook 'isearch-update-post-hook 'isearch-display-count-matches 'end t)
And in isearch-mode-end-hook
I inserted so:
(setq isearch-message-suffix-add "")
(remove-hook 'isearch-update-post-hook 'isearch-display-count-matches t)
This works nice for me -- and it displays the isearch-string, to know what to expect at, at each moment.