From 5de0a843180e198d5c0fbf209777021cd9105df5 Mon Sep 17 00:00:00 2001 From: Po Lu Date: Tue, 25 Jun 2024 11:37:09 +0800 Subject: [PATCH] Fix latent side-effects of respecting field boundaries in text conversion * src/textconv.c (textconv_query, get_extracted_text) (get_surrounding_text): Restore selected window in addition to excursion. (locate_and_save_position_in_field): Restore excursion lest the current buffer differ from the selected window's contents, to prevent redisplay from clobbering the current buffer when called while the current buffer is at variance with the selected window's contents, as seen with dired-other-window. (cherry picked from commit 1b4c56272110944c4ea1483c6f9662de9e42a07b) --- src/textconv.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/textconv.c b/src/textconv.c index 0e43bd9d458..e3f928cd789 100644 --- a/src/textconv.c +++ b/src/textconv.c @@ -153,6 +153,15 @@ select_window (Lisp_Object window, Lisp_Object norecord) Fselect_window (window, norecord); } +/* Restore the selected window WINDOW. */ + +static void +restore_selected_window (Lisp_Object window) +{ + /* FIXME: not sure what to do if WINDOW has been deleted. */ + select_window (window, Qt); +} + /* Perform the text conversion operation specified in QUERY and return the results. @@ -188,6 +197,7 @@ textconv_query (struct frame *f, struct textconv_callback_struct *query, selected window. */ count = SPECPDL_INDEX (); record_unwind_protect_excursion (); + record_unwind_protect (restore_selected_window, selected_window); /* Inhibit quitting. */ specbind (Qinhibit_quit, Qt); @@ -586,15 +596,6 @@ detect_conversion_events (void) return false; } -/* Restore the selected window WINDOW. */ - -static void -restore_selected_window (Lisp_Object window) -{ - /* FIXME: not sure what to do if WINDOW has been deleted. */ - select_window (window, Qt); -} - /* Commit the given text in the composing region. If there is no composing region, then insert the text after frame F's selected window's last point instead, unless the mark is active. Finally, @@ -1138,6 +1139,10 @@ locate_and_save_position_in_field (struct frame *f, struct window *w, /* Set the current buffer to W's. */ count = SPECPDL_INDEX (); + + /* The current buffer must be saved, not merely the selected + window. */ + record_unwind_protect_excursion (); record_unwind_protect (restore_selected_window, selected_window); XSETWINDOW (window, w); select_window (window, Qt); @@ -2097,6 +2102,7 @@ get_extracted_text (struct frame *f, ptrdiff_t n, selected window. */ count = SPECPDL_INDEX (); record_unwind_protect_excursion (); + record_unwind_protect (restore_selected_window, selected_window); /* Inhibit quitting. */ specbind (Qinhibit_quit, Qt); @@ -2219,6 +2225,7 @@ get_surrounding_text (struct frame *f, ptrdiff_t left, selected window. */ count = SPECPDL_INDEX (); record_unwind_protect_excursion (); + record_unwind_protect (restore_selected_window, selected_window); /* Inhibit quitting. */ specbind (Qinhibit_quit, Qt); -- 2.39.2