Subject: Re: bug#19576: write-file writes the wrong buffer

From: martin rudalics <[email-masked]>
Date: Tue, 17 Nov 2015 09:34:26 +0100

>> I'll try to get the fix of this bug applied there too.

I attach a fix for restoring current buffer and selected window (the
latter hopefully taking care of selecting the right frame).  Please tell
me whether it's sufficient for your purposes.

martin
diff --git a/src/window.c b/src/window.c
index 7c95ff9..06066fd 100644
--- a/src/window.c
+++ b/src/window.c
@@ -3142,14 +3142,14 @@ run_funs (Lisp_Object funs)
       call0 (XCAR (funs));
 }

-static void
+void
 select_window_norecord (Lisp_Object window)
 {
   if (WINDOW_LIVE_P (window))
     Fselect_window (window, Qt);
 }

-static void
+void
 select_frame_norecord (Lisp_Object frame)
 {
   if (FRAME_LIVE_P (XFRAME (frame)))
diff --git a/src/window.h b/src/window.h
index 135f5de..9ee3f81 100644
--- a/src/window.h
+++ b/src/window.h
@@ -1013,6 +1013,8 @@ extern void grow_mini_window (struct window *, int, bool);
 extern void shrink_mini_window (struct window *, bool);
 extern int window_relative_x_coord (struct window *, enum window_part, int);

+void select_window_norecord (Lisp_Object);
+void select_frame_norecord (Lisp_Object);
 void run_window_configuration_change_hook (struct frame *f);

 /* Make WINDOW display BUFFER.  RUN_HOOKS_P means it's allowed
diff --git a/src/xdisp.c b/src/xdisp.c
index dbc2d84..5e0d4ca 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -11739,8 +11739,16 @@ prepare_menu_bars (void)

 	      while (CONSP (functions))
 		{
+		  ptrdiff_t count = SPECPDL_INDEX ();
+
+		  record_unwind_current_buffer ();
+		  record_unwind_protect (select_window_norecord, selected_window);
+
 		  if (!EQ (XCAR (functions), Qt))
 		    call1 (XCAR (functions), frame);
+
+		  unbind_to (count, Qnil);
+
 		  functions = XCDR (functions);
 		}
 	    }