Update minibuffer in the isearch-update-post-hook

128 Views Asked by At

In my search to find a simpler solution that the one found "1 of n" result for Emacs search I've come up with the following package

https://github.com/nordlow/elisp/blob/master/mine/indexed-isearch.el

But the information displayed is delayed one key-stroke. How do fix this? My guess is that I either should call some function that updates the minibuffer at the end of isearch-cound-message or inject the update to isearch-message-suffix sooner the isearch logic that displays information in the minibuffer.

Does anybody know?

1

There are 1 best solutions below

4
On
(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.