Commit 3b134005 authored by Richard M. Stallman's avatar Richard M. Stallman
Browse files

(window-buffer-height): New function, split from

shrink-window-if-larger-than-buffer.
(shrink-window-if-larger-than-buffer): Use window-buffer-height.
parent 48ce3c22
......@@ -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."
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment