(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)
初始消息由 isearch-string 提供,在键入之前是您上次使用的消息。这就是为什么在重新输入之前您会看到错误的数字。这可以很容易地纠正。
最初的错误消息可以通过重置isearch-message-suffix-add
名为 的钩子中的值来纠正isearch-exit-mode
。
更新:
这段代码是可以写的最好的。我今天看了看。编辑字符串与 不同isearch-string
,但延迟了 1 个字符。为了获得正确的显示,需要键入 Cs 或 Cr 2 次,以使 edit-string 与 isearch-string 相同。isearch 不会在外部环境中导出具有编辑字符串值的变量。
(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)
在isearch-mode-end-hook
我这样插入:
(setq isearch-message-suffix-add "")
(remove-hook 'isearch-update-post-hook 'isearch-display-count-matches t)
这对我很有用——它会显示 isearch 字符串,以了解每时每刻会发生什么。