]> git.eshelyaron.com Git - emacs.git/commitdiff
(window-buffer-height): New function, split from
authorRichard M. Stallman <rms@gnu.org>
Sun, 22 Nov 1998 17:26:33 +0000 (17:26 +0000)
committerRichard M. Stallman <rms@gnu.org>
Sun, 22 Nov 1998 17:26:33 +0000 (17:26 +0000)
shrink-window-if-larger-than-buffer.
(shrink-window-if-larger-than-buffer): Use window-buffer-height.

lisp/window.el

index c18dcc41accd7352c2d2664a708ada785366f9cc..cd043e0d47e8e0a0638bfd18fb15966271ff2c42 100644 (file)
@@ -237,6 +237,23 @@ to the window's right, if any.  No arg means split equally."
   (interactive "p")
   (shrink-window arg t))
 
+(defun window-buffer-height (window)
+  "Return the height (in screen lines) of the buffer that WINDOW is displaying."
+  (save-excursion
+    (set-buffer (window-buffer window))
+    (goto-char (point-min))
+    (let ((ignore-final-newline
+           ;; If buffer ends with a newline, ignore it when counting height
+           ;; unless point is after it.
+           (and (not (eobp)) (eq ?\n (char-after (1- (point-max)))))))
+      (+ 1 (nth 2 (compute-motion (point-min)
+                                  '(0 . 0)
+                                  (- (point-max) (if ignore-final-newline 1 0))
+                                  (cons 0 100000000)
+                                  (window-width window)
+                                  nil
+                                  window))))))
+
 (defun shrink-window-if-larger-than-buffer (&optional window)
   "Shrink the WINDOW to be as small as possible to display its contents.
 Do not shrink to less than `window-min-height' lines.
@@ -249,41 +266,21 @@ or if the window is the only window of its frame."
     (if window
        (select-window window)
       (setq window (selected-window)))
-    (save-excursion
-      (set-buffer (window-buffer window))
-      (goto-char (point-min))
-      (let* ((ignore-final-newline
-             ;; If buffer ends with a newline, ignore it when counting height
-             ;; unless point is after it.
-             (and (not (eobp))
-                  (eq ?\n (char-after (1- (point-max))))))
-            (params (frame-parameters))
-            (mini (cdr (assq 'minibuffer params)))
-            (edges (window-edges))
-            text-height)
-       (if (and (< 1 (count-windows))
-                (= (window-width) (frame-width))
-                (pos-visible-in-window-p (point-min) window)
-                (not (eq mini 'only))
-                (or (not mini)
-                    (< (nth 3 edges)
-                       (nth 1 (window-edges mini)))
-                    (> (nth 1 edges)
-                       (cdr (assq 'menu-bar-lines params)))))
-           (let (result height)
-             (setq result
-                   (compute-motion (point-min) '(0 . 0)
-                                   (- (point-max)
-                                      (if ignore-final-newline 1 0))
-                                   (cons 0 (window-height))
-                                   (window-width) nil
-                                   window))
-             ;; Get number of screen lines that the text needs.
-             (setq text-height (+ 1 (nth 2 result)))
-             ;; Shrink down to that, or as far as we can go.
-             (if (> (window-height) (1+ text-height))
-                 (shrink-window (- (window-height)
-                                   (max (1+ text-height) window-min-height))))))))))
+    (let* ((params (frame-parameters))
+           (mini (cdr (assq 'minibuffer params)))
+           (edges (window-edges)))
+      (if (and (< 1 (count-windows))
+               (= (window-width) (frame-width))
+               (pos-visible-in-window-p (point-min) window)
+               (not (eq mini 'only))
+               (or (not mini)
+                   (< (nth 3 edges) (nth 1 (window-edges mini)))
+                   (> (nth 1 edges) (cdr (assq 'menu-bar-lines params)))))
+          (let ((text-height (window-buffer-height window))
+                (window-height (window-height)))
+            (when (> window-height (1+ text-height))
+              (shrink-window
+               (- window-height (max (1+ text-height) window-min-height)))))))))
 
 (defun kill-buffer-and-window ()
   "Kill the current buffer and delete the selected window."