Adapt query-replace and lazy highlighting to text conversion

* lisp/isearch.el (isearch-lazy-highlight-new-loop): Rather call
redisplay to guarantee a valid window-start than sit-for, to
account for cases where this function is called from an
after-change-function bound to a special input event quickly
followed by another event that does not modify the buffer.

* lisp/progmodes/prog-mode.el (treesit-node-at): Declare
function before references.

* lisp/replace.el (perform-replace): When querying, suppress
text conversion around the input loop.
This commit is contained in:
Po Lu 2025-02-17 16:46:48 +08:00
parent 310f62a948
commit b236a2529d
3 changed files with 23 additions and 3 deletions

View file

@ -4098,7 +4098,10 @@ This is called when `isearch-update' is invoked (which can cause the
search string to change or the window to scroll). It is also used
by other Emacs features."
(when (and (null executing-kbd-macro)
(sit-for 0) ;make sure (window-start) is credible
;; This used to read `(sit-for 0)', but that has proved
;; unreliable when called from within
;; after-change-functions bound to certain special events.
(redisplay) ;make sure (window-start) is credible
(or (not (equal isearch-string
isearch-lazy-highlight-last-string))
(not (memq (selected-window)

View file

@ -144,6 +144,8 @@ instead."
(end (progn (forward-sexp 1) (point))))
(indent-region start end nil))))
(declare-function treesit-node-at "treesit.c")
(defun prog-fill-reindent-defun (&optional argument)
"Refill or reindent the paragraph or defun that contains point.

View file

@ -2815,6 +2815,8 @@ and END."
(<= end (cdr bounds))))
region-bounds)))))
(defvar overriding-text-conversion-style)
(defun perform-replace (from-string replacements
query-flag regexp-flag delimited-flag
&optional repeat-count map start end backward region-noncontiguous-p)
@ -2877,10 +2879,13 @@ characters."
(limit nil)
(region-filter nil)
;; Disable text conversion during the replacement operation.
(old-text-conversion-style overriding-text-conversion-style)
overriding-text-conversion-style
;; Data for the next match. If a cons, it has the same format as
;; (match-data); otherwise it is t if a match is possible at point.
(match-again t)
(message
(if query-flag
(apply #'propertize
@ -2935,6 +2940,10 @@ characters."
(push-mark)
(undo-boundary)
(when query-flag
(setq overriding-text-conversion-style nil)
(when (fboundp 'set-text-conversion-style)
(set-text-conversion-style text-conversion-style)))
(unwind-protect
;; Loop finding occurrences that perhaps should be replaced.
(while (and keep-going
@ -3352,7 +3361,13 @@ characters."
last-was-act-and-show nil))))))
(replace-dehighlight)
(when region-filter
(remove-function isearch-filter-predicate region-filter)))
(remove-function isearch-filter-predicate region-filter))
(when query-flag
;; Resume text conversion.
(setq overriding-text-conversion-style
old-text-conversion-style)
(when (fboundp 'set-text-conversion-style)
(set-text-conversion-style text-conversion-style))))
(or unread-command-events
(message (ngettext "Replaced %d occurrence%s"
"Replaced %d occurrences%s"