changeset 33478:05a649280be2

(fit-window-to-buffer): Handle windows without mode-lines. Handle header-lines. Don't loop forever if we can't enlarge the window anymore. Simplify a bit.
author Miles Bader <miles@gnu.org>
date Tue, 14 Nov 2000 12:54:47 +0000
parents 51a929f58d72
children 9eec4a620afc
files lisp/window.el
diffstat 1 files changed, 39 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/window.el	Tue Nov 14 11:50:40 2000 +0000
+++ b/lisp/window.el	Tue Nov 14 12:54:47 2000 +0000
@@ -445,19 +445,27 @@
   (when (null max-height)
     (setq max-height (frame-height (window-frame window))))
 
-  (let* ((window-height
+  (let* ((buf
+	  ;; Buffer that is displayed in WINDOW
+	  (window-buffer window))
+	 (window-height
 	  ;; The current height of WINDOW
 	  (window-height window))
-	 (text-height
+	 (desired-height
 	  ;; The height necessary to show the buffer displayed by WINDOW
 	  ;; (`count-screen-lines' always works on the current buffer).
-	  ;; We add 1 for mode-line.
-	  (1+ (with-current-buffer (window-buffer window)
-		(count-screen-lines))))
+	  (with-current-buffer buf
+	    (+ (count-screen-lines)
+	       ;; For non-minibuffers, count the mode-line, if any
+	       (if (and (not (window-minibuffer-p window))
+			mode-line-format)
+		   1 0)
+	       ;; Count the header-line, if any
+	       (if header-line-format 1 0))))
 	 (delta
 	  ;; Calculate how much the window height has to change to show
-	  ;; text-height lines, constrained by MIN-HEIGHT and MAX-HEIGHT.
-	  (- (max (min text-height max-height)
+	  ;; desired-height lines, constrained by MIN-HEIGHT and MAX-HEIGHT.
+	  (- (max (min desired-height max-height)
 		  (or min-height window-min-height))
 	     window-height))
 	 ;; We do our own height checking, so avoid any restrictions due to
@@ -466,31 +474,32 @@
 
     ;; Don't try to redisplay with the cursor at the end
     ;; on its own line--that would force a scroll and spoil things.
-    (if (with-current-buffer (window-buffer window)
-	  (and (eobp) (bolp) (not (bobp))))
-	(set-window-point window (1- (window-point window))))
+    (when (with-current-buffer buf
+	    (and (eobp) (bolp) (not (bobp))))
+      (set-window-point window (1- (window-point window))))
 
-    (unless (zerop delta)
-      (if (eq window (selected-window))
-	  (enlarge-window delta)
-	(save-selected-window
-	  (select-window window)
-	  (enlarge-window delta))))
+    (save-selected-window
+      (select-window window)
+
+      ;; Adjust WINDOW to the nominally correct size (which may actually
+      ;; be slightly off because of variable height text, etc).
+      (unless (zerop delta)
+	(enlarge-window delta))
 
-    ;; Check if the last line is surely fully visible.  If not,
-    ;; enlarge the window.
-    (let ((pos (with-current-buffer (window-buffer window)
-		 (save-excursion
-		   (goto-char (point-max))
-		   (if (and (bolp) (not (bobp)))
-		       (1- (point))
-		     (point))))))
-      (set-window-vscroll window 0)
-      (save-selected-window
-	(select-window window)
-	(while (and (< (window-height window) max-height) 
-		    (not (pos-visible-in-window-p pos window t)))
-	  (enlarge-window 1))))))
+      ;; Check if the last line is surely fully visible.  If not,
+      ;; enlarge the window.
+      (let ((end (with-current-buffer buf
+		   (save-excursion
+		     (goto-char (point-max))
+		     (if (and (bolp) (not (bobp)))
+			 (1- (point))
+		       (point))))))
+	(set-window-vscroll window 0)
+	(while (and (< desired-height max-height)
+		    (= desired-height (window-height window))
+		    (not (pos-visible-in-window-p end window t)))
+	  (enlarge-window 1)
+	  (setq desired-height (1+ desired-height)))))))
 
 (defun shrink-window-if-larger-than-buffer (&optional window)
   "Shrink the WINDOW to be as small as possible to display its contents.