Subject: bug#32825: 27.0.50; Deterministic window management

From: martin rudalics <[email-masked]>
Date: Mon, 01 Oct 2018 10:36:18 +0200

> Ehm - Noam, since you seem the most knowing of us all, could you maybe
 > please care about this report?

So we got a hot potato here.  Let me try to recapitulate what should
be done:

(1) Try to never display the *Backtrace* buffer in the selected (aka
same) window.  So far, we all agreed on that and nobody asked for an
exception from this rule.

(2) Try to keep 'debugger-previous-window' around even after the
debugger session has been closed.  Doing that naively will get us back
Bug#17882.  So we probably don't want to use
'debugger-previous-window' in a new session when it is on another
frame (or display).  But we could use it even in a new session when it
is on the selected frame and not the selected window.

This should take care of the default value of 'debugger-bury-or-kill'
- the immediate problem that caused this bug report.  Does anyone see
a problem with this?

I attach a preliminary patch.  Please try it and tell me whether you
see any difficulties with it.

Thanks, martin
--- a/lisp/emacs-lisp/debug.el
+++ b/lisp/emacs-lisp/debug.el
@@ -101,7 +101,10 @@ debugger-old-buffer
   "This is the buffer that was current when the debugger was entered.")
 
 (defvar debugger-previous-window nil
-  "This is the window last showing the debugger buffer.")
+  "The last debugger window from the same session.")
+
+(defvar debugger-pre-previous-window nil
+  "The last debugger window from the previous session.")
 
 (defvar debugger-previous-window-height nil
   "The last recorded height of `debugger-previous-window'.")
@@ -240,10 +243,16 @@ debug
 	       debugger-buffer
 	       `((display-buffer-reuse-window
 		  display-buffer-in-previous-window)
-		 . (,(when (and (window-live-p debugger-previous-window)
-				(frame-visible-p
-				 (window-frame debugger-previous-window)))
-		       `(previous-window . ,debugger-previous-window)))))
+		 . (,(cond
+                      ((and (window-live-p debugger-previous-window)
+			    (frame-visible-p
+			     (window-frame debugger-previous-window)))
+		       `(previous-window . ,debugger-previous-window))
+                      ((and (window-live-p debugger-pre-previous-window)
+                            (eq (window-frame debugger-pre-previous-window)
+                                (selected-frame)))
+		       `(previous-window . ,debugger-pre-previous-window)))
+                    (inhibit-same-window . t))))
 	      (setq debugger-window (selected-window))
 	      (if (eq debugger-previous-window debugger-window)
 		  (when debugger-jumping-flag
@@ -297,7 +306,9 @@ debug
 		(quit-restore-window debugger-window debugger-bury-or-kill)
 		;; Restore current buffer (Bug#12502).
 		(set-buffer debugger-old-buffer)))
-            ;; Forget debugger window, it won't be back (Bug#17882).
+            ;; Forget debugger window, it won't be back (Bug#17882)
+            ;; unless it's on the selected frame (Bug#32825).
+            (setq debugger-pre-previous-window debugger-previous-window)
             (setq debugger-previous-window nil))
           ;; Restore previous state of debugger-buffer in case we were
           ;; in a recursive invocation of the debugger, otherwise just