Subject: Re: bug#32825: 27.0.50; Deterministic window management

From: martin rudalics <[email-masked]>
Date: Fri, 21 Dec 2018 10:15:11 +0100

> Please note that if there is no suitable window with at least 10 lines high,
 > it still displays the *Backtrace* buffer in random windows, every time
 > in another window because it doesn't remember a previous window.

For starters, users can customize the number of lines by supplying a
'window-min-height' entry.  They can also provide an additional action
function like 'display-buffer-below-selected' as fallback.  And
finally we could try what I suggested earlier: Fill the *Backtrace*
buffer first and pop to it afterwards using 'fit-window-to-buffer'.
The attached untested patch tries to do that.

martin
diff --git a/lisp/emacs-lisp/debug.el b/lisp/emacs-lisp/debug.el
index b6b1f20..5c186f7 100644
--- a/lisp/emacs-lisp/debug.el
+++ b/lisp/emacs-lisp/debug.el
@@ -236,12 +236,33 @@ debug
 		;; Place an extra debug-on-exit for macro's.
 		(when (eq 'lambda (car-safe (cadr (backtrace-frame 4))))
 		  (backtrace-debug 5 t)))
+              (with-current-buffer debugger-buffer
+                (unless (derived-mode-p 'debugger-mode)
+	          (debugger-mode))
+	        (debugger-setup-buffer debugger-args))
+	      (when noninteractive
+		;; If the backtrace is long, save the beginning
+		;; and the end, but discard the middle.
+		(when (> (count-lines (point-min) (point-max))
+			 debugger-batch-max-lines)
+		  (goto-char (point-min))
+		  (forward-line (/ 2 debugger-batch-max-lines))
+		  (let ((middlestart (point)))
+		    (goto-char (point-max))
+		    (forward-line (- (/ 2 debugger-batch-max-lines)
+				     debugger-batch-max-lines))
+		    (delete-region middlestart (point)))
+		  (insert "...\n"))
+		(goto-char (point-min))
+		(message "%s" (buffer-string))
+		(kill-emacs -1))
 	      (pop-to-buffer
 	       debugger-buffer
 	       `((display-buffer-reuse-window
 		  display-buffer-in-previous-window
 		  display-buffer-below-selected)
 		 . ((window-min-height . 10)
+                    (window-height . fit-window-to-buffer)
 		    ,@(when (and (window-live-p debugger-previous-window)
 				(frame-visible-p
 				 (window-frame debugger-previous-window)))
@@ -258,25 +279,6 @@ debug
 			    (window-total-height debugger-window)))
 		      (error nil)))
 		(setq debugger-previous-window debugger-window))
-              (unless (derived-mode-p 'debugger-mode)
-	        (debugger-mode))
-	      (debugger-setup-buffer debugger-args)
-	      (when noninteractive
-		;; If the backtrace is long, save the beginning
-		;; and the end, but discard the middle.
-		(when (> (count-lines (point-min) (point-max))
-			 debugger-batch-max-lines)
-		  (goto-char (point-min))
-		  (forward-line (/ 2 debugger-batch-max-lines))
-		  (let ((middlestart (point)))
-		    (goto-char (point-max))
-		    (forward-line (- (/ 2 debugger-batch-max-lines)
-				     debugger-batch-max-lines))
-		    (delete-region middlestart (point)))
-		  (insert "...\n"))
-		(goto-char (point-min))
-		(message "%s" (buffer-string))
-		(kill-emacs -1))
 	      (message "")
 	      (let ((standard-output nil)
 		    (buffer-read-only t))