From: gerdh@ims.COM (Gerd Hoeren)
Subject: Window shrink ELISP function
Organization: Gnus Not Usenet
Distribution: gnu
Date: Thu, 31 Dec 1992 16:51:35 GMT

Here is some Epoch ELISP code that changes the size of a window to fit
the current buffer.  This is useful for taking up less screen space
when working with small files.

You can assign shrink-to-fit to a function key and/or connect it to
the DIRED display hook.

;;
;; Reduce the window to fit small buffers.  The following global variables
;;  can be set to control how the (shrink-to-fit) function behaves.
;;
(defvar shrink-max-lines 50
  "*Maximum number of lines that (shrink-to-fit) puts in a window.")
(defvar shrink-min-lines 3
  "*Minimum number of lines that (shrink-to-fit) puts in a window.")
(defvar shrink-max-columns 150
  "*Maximum number of columns that (shrink-to-fit) puts in a window.")
(defvar shrink-min-columns 40
  "*Minimum number of columns that (shrink-to-fit) puts in a window.")

(defun shrink-to-fit ()
  "Shrink the current window to fit around the current buffer.  Variables
SHRINK-MAX-LINES, SHRINK-MIN-LINES, SHRINK-MAX-COLUMNS and SHRINK-MIN-COLUMNS
set bounds on the largest and smallest resulting window."

  (let ((max-width shrink-min-columns) ; Longest line in the file
	(num-lines 0)                  ; Number of lines in file
	(off-screen))                  ; Number of lines to scroll down
    (save-excursion

      ;; Calculate the number of lines and the longest line
      (goto-char (point-min))
      (while (search-forward "\n" (point-max) t)
	(backward-char 1)
	(setq max-width (max (1+ (current-column)) max-width))
	(forward-char 1)
	(setq num-lines (1+ num-lines)))

      ;; Change the screen size to fit around the text.  Note that the height
      ;;  of the window is the size of the file plus one for the mode line
      ;;  plus one for the local mini-buffer (if present).
      (setq num-lines (1+ (max
			   (min num-lines shrink-max-lines)
			   shrink-min-lines)))
      (setq max-width (min max-width shrink-max-columns))
      (change-screen-size
       max-width
       (if epoch::nonlocal-minibuffer num-lines (1+ num-lines))
       (current-screen))

      ;; See if we have white space beyond the last line of the file.
      (goto-char (window-start))
      (setq off-screen (forward-line (1- num-lines))))
    (if (not (eq 0 off-screen))
	;; Put the last line of the file on the bottom of the window.
	(scroll-down off-screen))))


-----

You can assign shrink-to-fit to a function key with something like:

(define-function-key "KP_Add"       ;; Shrink window around buffer
  '(lambda () (interactive)
     (shrink-to-fit)))


-----

The following connects shrink-to-fit to the DIRED display hook so that
all DIRED windows automatically are fit to the size of their buffers.
(dired-version's value is "5.257 from
ftp.cs.buffalo.edu:pub/Emacs/diredall.tar.Z)

;; The following shrinks all DIRED windows around the size of the text.
(setq dired-after-readin-hook
      (append dired-after-readin-hook '(shrink-to-fit)))


If you start Epoch in DIRED, put the following in your startup file to
shrink the initial DIRED display:

(shrink-to-fit)		    ;; Shrink our current (Home) dired window


-----

Happy Epoching!

--------------------------------------
Gerd Hoeren
  Integrated Measurement Systems, Inc.
  Voice: (503) 626-7117
  Fax:   (503) 644-6969
  Email: gerdh@ims.com
--------------------------------------

Newsgroups: gnu.epoch.misc