comparison lisp/window.el @ 28655:2ea744c8844a

(count-screen-lines): New function. (shrink-window-if-larger-than-buffer): Use count-screen-lines instead of window-buffer-height.
author Gerd Moellmann <gerd@gnu.org>
date Wed, 19 Apr 2000 19:07:56 +0000
parents 6e6ad5d67263
children 766caf49d6a9
comparison
equal deleted inserted replaced
28654:1d7862b7e02d 28655:2ea744c8844a
1 ;;; window.el --- GNU Emacs window commands aside from those written in C. 1 ;;; window.el --- GNU Emacs window commands aside from those written in C.
2 2
3 ;; Copyright (C) 1985, 1989, 1992, 1993, 1994 Free Software Foundation, Inc. 3 ;; Copyright (C) 1985, 1989, 1992, 1993, 1994, 2000
4 ;; Free Software Foundation, Inc.
4 5
5 ;; Maintainer: FSF 6 ;; Maintainer: FSF
6 7
7 ;; This file is part of GNU Emacs. 8 ;; This file is part of GNU Emacs.
8 9
256 (cons 0 100000000) 257 (cons 0 100000000)
257 (window-width window) 258 (window-width window)
258 nil 259 nil
259 window)))))) 260 window))))))
260 261
262 (defun count-screen-lines (&optional beg end count-final-newline window)
263 "Return the number of screen lines in the region.
264 The number of screen lines may be different from the number of actual lines,
265 due to line breaking, display table, etc.
266
267 Optional arguments BEG and END default to `point-min' and `point-max'
268 respectively.
269
270 If region ends with a newline, ignore it unless optinal third argument
271 COUNT-FINAL-NEWLINE is non-nil.
272
273 The optional fourth argument WINDOW specifies the window used for obtaining
274 parameters such as width, horizontal scrolling, and so on. The default is
275 to use the selected window's parameters.
276
277 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
278 regardless of which buffer is displayed in WINDOW. This makes possible to use
279 `count-screen-lines' in any buffer, whether or not it is currently displayed
280 in some window."
281 (unless beg
282 (setq beg (point-min)))
283 (unless end
284 (setq end (point-max)))
285 (if (= beg end)
286 0
287 (save-excursion
288 (save-restriction
289 (widen)
290 (narrow-to-region (min beg end)
291 (if (and (not count-final-newline)
292 (= ?\n (char-before (max beg end))))
293 (1- (max beg end))
294 (max beg end)))
295 (goto-char (point-min))
296 (1+ (vertical-motion (buffer-size) window))))))
297
261 (defun shrink-window-if-larger-than-buffer (&optional window) 298 (defun shrink-window-if-larger-than-buffer (&optional window)
262 "Shrink the WINDOW to be as small as possible to display its contents. 299 "Shrink the WINDOW to be as small as possible to display its contents.
263 Do not shrink to less than `window-min-height' lines. 300 Do not shrink to less than `window-min-height' lines.
264 Do nothing if the buffer contains more lines than the present window height, 301 Do nothing if the buffer contains more lines than the present window height,
265 or if some of the window's contents are scrolled out of view, 302 or if some of the window's contents are scrolled out of view,
278 (pos-visible-in-window-p (point-min) window) 315 (pos-visible-in-window-p (point-min) window)
279 (not (eq mini 'only)) 316 (not (eq mini 'only))
280 (or (not mini) 317 (or (not mini)
281 (< (nth 3 edges) (nth 1 (window-edges mini))) 318 (< (nth 3 edges) (nth 1 (window-edges mini)))
282 (> (nth 1 edges) (cdr (assq 'menu-bar-lines params))))) 319 (> (nth 1 edges) (cdr (assq 'menu-bar-lines params)))))
283 (let ((text-height (window-buffer-height window)) 320 ;; `count-screen-lines' always works on the current buffer, so
321 ;; make sure it is the buffer displayed by WINDOW.
322 (let ((text-height (with-current-buffer (window-buffer window)
323 (count-screen-lines)))
284 (window-height (window-height))) 324 (window-height (window-height)))
285 ;; Don't try to redisplay with the cursor at the end 325 ;; Don't try to redisplay with the cursor at the end
286 ;; on its own line--that would force a scroll and spoil things. 326 ;; on its own line--that would force a scroll and spoil things.
287 (when (and (eobp) (bolp) (not (bobp))) 327 (when (and (eobp) (bolp) (not (bobp)))
288 (forward-char -1)) 328 (forward-char -1))