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

From: martin rudalics <[email-masked]>
Date: Fri, 03 Jun 2011 15:19:31 +0200

> A couple of my frame window layouts cause the emacs debugger's backtrace
 > buffer to cycle between several windows when stepping through code,
 > making it very difficult to focus.
 >
 > Please follow the minimal steps below to re-produce this undesirable
 > behaviour, also confirmed as present by another user (#emacs 'off-by-1')
 > in a 'relatively recent git version'..

I never use debug and don't have Emacs 23 installed, so I'm probably not
of very much help here.

 > emacs -q
 > C-x 3
 > C-x 2
 > M-x debug-on-entry RET
 > apropos RET
 > M-x apropos RET
 > x RET

IIUC this shows *backtrace* in the window on the right?

 > d RET
 > d RET

These steps happen without the RETs I presume?

 > ..you should see the buffer alternate windows on each step through the
 > code.

On my trunk the *backtrace* buffer is alternately shown in the left
lower and the right window, is that what you see?  That is, the left
upper window is never used?

I suppose it happens because `debug' contains this pretty fragile code

	      (save-window-excursion
	        ...
                 (pop-to-buffer debugger-buffer)

which I don't understand so I can only speculate.  Your three-window
setup apparently prevents the creation of a new window so Emacs is
forced to reuse an existing one.  Now repeating "d" does apparently

(1) remove *backtrace* from the window configuration, restoring the
     previous configuration, and

(2) pop to *backtrace* in any but the selected window (which is the left
     upper one).  Now, when it reuses a window, `display-buffer' first
     tries to use the least-recently-used one, which, in your scenario,
     is alternatingly one of the two lower windows.

You can verify (2) for yourself by replacing the line

		  (or (get-lru-window frame-to-use)

in `display-buffer' with the form

		  (or (let ((window (get-lru-window frame-to-use)))
			(when window
			  (message "%s" window)
			  (sit-for 3)
			  window))

and go through your scenario.

I don't have the slightest idea how to fix this though because I don't
understand why apparently the *backtrace* buffer is removed from display
in (1), and what the subsequent fragment

	  ;; Kill or at least neuter the backtrace buffer, so that users
	  ;; don't try to execute debugger commands in an invalid context.
	  (if (get-buffer-window debugger-buffer 0)
	      ;; Still visible despite the save-window-excursion?  Maybe it
	      ;; it's in a pop-up frame.  It would be annoying to delete and
	      ;; recreate it every time the debugger stops, so instead we'll
	      ;; erase it (and maybe hide it) but keep it alive.
	      (with-current-buffer debugger-buffer
		(erase-buffer)
		(fundamental-mode)
		(with-selected-window (get-buffer-window debugger-buffer 0)
                   (when (and (window-dedicated-p (selected-window))
                              (not debugger-will-be-back))
                     ;; If the window is not dedicated, burying the buffer
                     ;; will mean that the frame created for it is left
                     ;; around showing some random buffer, and next time we
                     ;; pop to the debugger buffer we'll create yet
                     ;; another frame.
                     ;; If debugger-will-be-back is non-nil, the frame
                     ;; would need to be de-iconified anyway immediately
                     ;; after when we re-enter the debugger, so iconifying it
                     ;; here would cause flashing.
                     ;; Drew Adams is not happy with this: he wants to frame
                     ;; to be left at the top-level, still working on how
                     ;; best to do that.
                     (bury-buffer))))
	    (kill-buffer debugger-buffer))

is needed for (despite its detailed comment).  So we need help from
someone familiar with the debug code :-(

martin