diff options
| author | Miles Bader | 2000-10-21 05:38:11 +0000 |
|---|---|---|
| committer | Miles Bader | 2000-10-21 05:38:11 +0000 |
| commit | 65e742bdea8d2f54ee7bdf49922912302d3fb5ef (patch) | |
| tree | 7f082be5bbba11f06d658c6e8e4ef44a422ec6b4 | |
| parent | 2253894d74185cb55fcef8a9669a27970b454e07 (diff) | |
| download | emacs-65e742bdea8d2f54ee7bdf49922912302d3fb5ef.tar.gz emacs-65e742bdea8d2f54ee7bdf49922912302d3fb5ef.zip | |
(fit-window-to-buffer): New function.
(shrink-window-if-larger-than-buffer): Use it.
| -rw-r--r-- | lisp/window.el | 95 |
1 files changed, 65 insertions, 30 deletions
diff --git a/lisp/window.el b/lisp/window.el index 9ce6c28f93a..ab1b52b43fa 100644 --- a/lisp/window.el +++ b/lisp/window.el | |||
| @@ -428,6 +428,58 @@ in some window." | |||
| 428 | (goto-char (point-min)) | 428 | (goto-char (point-min)) |
| 429 | (1+ (vertical-motion (buffer-size) window)))))) | 429 | (1+ (vertical-motion (buffer-size) window)))))) |
| 430 | 430 | ||
| 431 | (defun fit-window-to-buffer (&optional window max-height min-height) | ||
| 432 | "Make WINDOW the right size to display its contents exactly. | ||
| 433 | If the optional argument MAX-HEIGHT is supplied, it is the maximum height | ||
| 434 | the window is allowed to be, defaulting to the frame height. | ||
| 435 | If the optional argument MIN-HEIGHT is supplied, it is the minimum | ||
| 436 | height the window is allowed to be, defaulting to `window-min-height'. | ||
| 437 | |||
| 438 | The heights in MAX-HEIGHT and MIN-HEIGHT include the mode-line and/or | ||
| 439 | header-line." | ||
| 440 | (interactive) | ||
| 441 | |||
| 442 | (when (null window) | ||
| 443 | (setq window (selected-window))) | ||
| 444 | |||
| 445 | (let* ((window-height | ||
| 446 | ;; The current height of WINDOW | ||
| 447 | (window-height window)) | ||
| 448 | (extra | ||
| 449 | ;; The amount by which the text height differs from the window | ||
| 450 | ;; height. | ||
| 451 | (- window-height (window-text-height window))) | ||
| 452 | (text-height | ||
| 453 | ;; The height necessary to show the buffer displayed by WINDOW | ||
| 454 | ;; (`count-screen-lines' always works on the current buffer). | ||
| 455 | (with-current-buffer (window-buffer window) | ||
| 456 | (count-screen-lines))) | ||
| 457 | (delta | ||
| 458 | ;; Calculate how much the window height has to change to show | ||
| 459 | ;; text-height lines, constrained by MIN-HEIGHT and MAX-HEIGHT. | ||
| 460 | (- (max (min (+ text-height extra) | ||
| 461 | (or max-height | ||
| 462 | (frame-height | ||
| 463 | (window-frame | ||
| 464 | (or window (selected-window)))))) | ||
| 465 | (or min-height window-min-height)) | ||
| 466 | window-height)) | ||
| 467 | ;; We do our own height checking, so avoid any restrictions due to | ||
| 468 | ;; window-min-height. | ||
| 469 | (window-min-height 1)) | ||
| 470 | |||
| 471 | ;; Don't try to redisplay with the cursor at the end | ||
| 472 | ;; on its own line--that would force a scroll and spoil things. | ||
| 473 | (when (and (eobp) (bolp) (not (bobp))) | ||
| 474 | (forward-char -1)) | ||
| 475 | |||
| 476 | (unless (zerop delta) | ||
| 477 | (if (eq window (selected-window)) | ||
| 478 | (enlarge-window delta) | ||
| 479 | (save-selected-window | ||
| 480 | (select-window window) | ||
| 481 | (enlarge-window delta)))))) | ||
| 482 | |||
| 431 | (defun shrink-window-if-larger-than-buffer (&optional window) | 483 | (defun shrink-window-if-larger-than-buffer (&optional window) |
| 432 | "Shrink the WINDOW to be as small as possible to display its contents. | 484 | "Shrink the WINDOW to be as small as possible to display its contents. |
| 433 | Do not shrink to less than `window-min-height' lines. | 485 | Do not shrink to less than `window-min-height' lines. |
| @@ -436,36 +488,19 @@ or if some of the window's contents are scrolled out of view, | |||
| 436 | or if the window is not the full width of the frame, | 488 | or if the window is not the full width of the frame, |
| 437 | or if the window is the only window of its frame." | 489 | or if the window is the only window of its frame." |
| 438 | (interactive) | 490 | (interactive) |
| 439 | (save-selected-window | 491 | (when (null window) |
| 440 | (if window | 492 | (setq window (selected-window))) |
| 441 | (select-window window) | 493 | (let* ((frame (window-frame window)) |
| 442 | (setq window (selected-window))) | 494 | (mini (frame-parameter frame 'minibuffer)) |
| 443 | (let* ((mini (frame-parameter nil 'minibuffer)) | 495 | (edges (window-edges window))) |
| 444 | (edges (window-edges))) | 496 | (if (and (not (eq window (frame-root-window frame))) |
| 445 | (if (and (< 1 (count-windows)) | 497 | (= (window-width) (frame-width)) |
| 446 | (= (window-width) (frame-width)) | 498 | (pos-visible-in-window-p (point-min) window) |
| 447 | (pos-visible-in-window-p (point-min) window) | 499 | (not (eq mini 'only)) |
| 448 | (not (eq mini 'only)) | 500 | (or (not mini) |
| 449 | (or (not mini) | 501 | (< (nth 3 edges) (nth 1 (window-edges mini))) |
| 450 | (< (nth 3 edges) (nth 1 (window-edges mini))) | 502 | (> (nth 1 edges) (frame-parameter frame 'menu-bar-lines)))) |
| 451 | (> (nth 1 edges) (frame-parameter nil 'menu-bar-lines)))) | 503 | (fit-window-to-buffer window (window-height window))))) |
| 452 | |||
| 453 | ;; `count-screen-lines' always works on the current buffer, so | ||
| 454 | ;; make sure it is the buffer displayed by WINDOW. | ||
| 455 | (let ((text-height | ||
| 456 | (with-current-buffer (window-buffer window) | ||
| 457 | (count-screen-lines))) | ||
| 458 | (window-height | ||
| 459 | (window-text-height))) | ||
| 460 | |||
| 461 | ;; Don't try to redisplay with the cursor at the end | ||
| 462 | ;; on its own line--that would force a scroll and spoil things. | ||
| 463 | (when (and (eobp) (bolp) (not (bobp))) | ||
| 464 | (forward-char -1)) | ||
| 465 | |||
| 466 | (when (> window-height text-height) | ||
| 467 | (shrink-window | ||
| 468 | (- window-height (max text-height window-min-height))))))))) | ||
| 469 | 504 | ||
| 470 | (defun kill-buffer-and-window () | 505 | (defun kill-buffer-and-window () |
| 471 | "Kill the current buffer and delete the selected window." | 506 | "Kill the current buffer and delete the selected window." |