Subject: bug#8789: 23.3; debug backtrace buffer changes window on step-through

From: martin rudalics <[email-masked]>
Date: Sun, 11 Mar 2012 19:14:43 +0100

>  > The general approach sounds good, but we should probably try to refine
 >  > it so as to minimize changes in behavior, and so it works right in the
 >  > multi-frame and even multi-terminal case.
 >  >
 >  > We could try to store the last-used-window in a variable
 >  > `debugger-last-used-window' and use that window after checking that it's
 >  > still live and is visible in the selected terminal.

OK.  Attached find my last ;-) stab at this.

martin
=== modified file 'lisp/emacs-lisp/debug.el'
--- lisp/emacs-lisp/debug.el	2012-01-19 07:21:25 +0000
+++ lisp/emacs-lisp/debug.el	2012-03-11 18:00:23 +0000
@@ -108,6 +108,25 @@
 - exit: called because of exit of a flagged function.
 - error: called because of `debug-on-error'.")
 
+(defvar debugger-buffer-last-window nil
+  "If non-nil, the last window displaying `debugger-buffer'")
+
+;; An appropriate substitute for this should be implemented in
+;; window.el.
+(defun debugger-buffer-use-last-window (buffer alist)
+  "Try displaying debugger buffer in last window where it was seen."
+  ;; Reuse only a window on the selected frame, excluding the selected
+  ;; and dedicated windows.
+  (when (and (window-live-p debugger-buffer-last-window)
+	     (not (eq debugger-buffer-last-window (selected-window)))
+	     (not (window-dedicated-p debugger-buffer-last-window))
+	     (eq (window-frame debugger-buffer-last-window)
+		 (selected-frame)))
+    (display-buffer-record-window
+     'reuse debugger-buffer-last-window buffer)
+    (window--display-buffer-2
+     buffer debugger-buffer-last-window display-buffer-mark-dedicated)))
+
 ;;;###autoload
 (setq debugger 'debug)
 ;;;###autoload
@@ -194,7 +213,15 @@
 		  ;; Place an extra debug-on-exit for macro's.
 		  (when (eq 'lambda (car-safe (cadr (backtrace-frame 4))))
 		    (backtrace-debug 5 t)))
-                (pop-to-buffer debugger-buffer)
+
+                (pop-to-buffer
+		 debugger-buffer
+		 '((display-buffer-reuse-window
+		    ;; Try reusing the last window where debugger-buffer
+		    ;; was seen (Bug#8789).
+		    debugger-buffer-use-last-window) . nil))
+		(setq debugger-buffer-last-window (selected-window))
+
 		(debugger-mode)
 		(debugger-setup-buffer debugger-args)
 		(when noninteractive