view lisp/reposition.el @ 51195:3cbf29813eea

(struct frame): Rename members height to text_lines, width to text_cols, window_height to total_lines, window_width to total_cols, new_height to new_text_lines, new_width to new_text_cols. All uses changed. (struct frame): New members which consolidate common members of x_output, w32_output, and mac_output structures: left_pos, top_pos, pixel_height, pixel_width, x_pixels_diff, y_pixels_diff, win_gravity, size_hint_flags, border_width, internal_border_width, line_height, fringe_cols, left_fringe_width, right_fringe_width, want_fullscreen. All uses changed. (struct frame): New member column_width contaning the canonical column width, analogue to line_height. All uses changed. (struct frame): Rename members scroll_bar_pixel_width to config_scroll_bar_width, and scroll_bar_cols to config_scroll_bar_cols. All uses changed. (struct frame): New member scroll_bar_actual_width which consolidates and renames the vertical_scroll_bar_extra member of x_output, w32_output, and mac_output structures. All uses changed. (FRAME_PIXEL_HEIGHT): Renamed from PIXEL_HEIGHT and moved from x/w32/macterm.h files. All uses changed. Also change code which referred to f->output_data...->pixel_height. (FRAME_PIXEL_WIDTH): Renamed from PIXEL_WIDTH and moved from x/w32/macterm.h files. All uses changed. Also change code which referred to f->output_data...->pixel_width. (FRAME_LINES): Renamed from FRAME_HEIGHT. All uses changed. Also change code which referred to f->height. (FRAME_COLS): Renamed from FRAME_WIDTH. All uses changed. Also change code which referred to f->width. (FRAME_NEW_HEIGHT, FRAME_NEW_WIDTH): Remove macros; change uses to update new_text_lines and new_text_cols members directly. (FRAME_CONFIG_SCROLL_BAR_WIDTH): Renamed from FRAME_SCROLL_BAR_PIXEL_WIDTH. All uses changed. (FRAME_CONFIG_SCROLL_BAR_COLS): Renamed from FRAME_SCROLL_BAR_COLS. All uses changed. (FRAME_LEFT_SCROLL_BAR_COLS, FRAME_RIGHT_SCROLL_BAR_COLS): Renamed from FRAME_LEFT_SCROLL_BAR_WIDTH and FRAME_RIGHT_SCROLL_BAR_WIDTH, resp. All uses changed. (FRAME_SCROLL_BAR_AREA_WIDTH, FRAME_LEFT_SCROLL_BAR_AREA_WIDTH) (FRAME_RIGHT_SCROLL_BAR_AREA_WIDTH): New macros. (FRAME_TOTAL_COLS): Renamed from FRAME_WINDOW_WIDTH. (SET_FRAME_COLS): Renamed from SET_FRAME_WIDTH. (FRAME_TOTAL_COLS_ARG): Renamed from FRAME_WINDOW_WIDTH_ARG. (WINDOW_VERTICAL_SCROLL_BAR_COLUMN): Remove unused macro. (WINDOW_VERTICAL_SCROLL_BAR_HEIGHT): Remove unused macro. (FRAME_LINE_HEIGHT): Renamed from CANON_Y_UNIT. Unconditionally return line_height member (it now has proper value also for non-window frames). (FRAME_COLUMN_WIDTH): Renamed from CANON_X_UNIT. Unconditionally return new column_width member (rather than the default font width). (FRAME_FRINGE_COLS, FRAME_LEFT_FRINGE_WIDTH) (FRAME_RIGHT_FRINGE_WIDTH): Renamed from FRAME_X_... and moved from x/w32/macterm.h files. Unconditionally return corresponding member of frame structure (they now have proper values also for non-window frames). (FRAME_TOTAL_FRINGE_WIDTH): Renamed from FRAME_FRINGE_WIDTH. Calculate return value from left and right widths. (FRAME_INTERNAL_BORDER_WIDTH): Unconditionally return internal_border_width member (has proper value for non-window frame). (FRAME_PIXEL_X_FROM_CANON_X): Renamed from PIXEL_X_FROM_CANON_X. (FRAME_PIXEL_Y_FROM_CANON_Y): Renamed from PIXEL_Y_FROM_CANON_Y. (FRAME_CANON_X_FROM_PIXEL_X): Renamed from CANON_X_FROM_PIXEL_X. (FRAME_CANON_Y_FROM_PIXEL_Y): Renamed from CANON_Y_FROM_PIXEL_Y. (FRAME_LINE_TO_PIXEL_Y): Renamed from CHAR_TO_PIXEL_ROW, consolidated from xterm.h, macterm.h, and w32term.h. (FRAME_COL_TO_PIXEL_X): Renamed from CHAR_TO_PIXEL_COL, consolidated from xterm.h, macterm.h, and w32term.h. (FRAME_TEXT_COLS_TO_PIXEL_WIDTH): Renamed from CHAR_TO_PIXEL_WIDTH consolidated from x/mac/w32term.h. (FRAME_TEXT_LINES_TO_PIXEL_HEIGHT): Renamed from CHAR_TO_PIXEL_HEIGHT consolidated from x/mac/w32term.h. (FRAME_PIXEL_Y_TO_LINE): Renamed from PIXEL_TO_CHAR_ROW consolidated from x/mac/w32term.h. (FRAME_PIXEL_X_TO_COL): Renamed from PIXEL_TO_CHAR_COL consolidated from x/mac/w32term.h. (FRAME_PIXEL_WIDTH_TO_TEXT_COLS): Renamed from PIXEL_TO_CHAR_WIDTH consolidated from x/mac/w32term.h. (FRAME_PIXEL_HEIGHT_TO_TEXT_LINES): Renamed from PIXEL_TO_CHAR_HEIGHT consolidated from x/mac/w32term.h.
author Kim F. Storm <storm@cua.dk>
date Sat, 24 May 2003 21:58:07 +0000
parents 64ee9a4a698e
children 695cf19ef79e d7ddb3e565de
line wrap: on
line source

;;; reposition.el --- center a Lisp function or comment on the screen

;; Copyright (C) 1991, 1994 Free Software Foundation, Inc.

;; Author: Michael D. Ernst <mernst@theory.lcs.mit.edu>
;; Created: Jan 1991
;; Maintainer: FSF

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;; Reposition-window makes an entire function definition or comment visible,
;; or, if it is already visible, places it at the top of the window;
;; additional invocations toggle the visibility of comments preceding the
;; code.  For the gory details, see the documentation for reposition-window;
;; rather than reading that, you may just want to play with it.

;; This tries pretty hard to do the recentering correctly; the precise
;; action depends on what the buffer looks like.  If you find a situation
;; where it doesn't behave well, let me know.  This function is modeled
;; after one of the same name in ZMACS, but the code is all-new and the
;; behavior in some situations differs.

;;; Code:

;;;###autoload
(defun reposition-window (&optional arg)
  "Make the current definition and/or comment visible.
Further invocations move it to the top of the window or toggle the
visibility of comments that precede it.
  Point is left unchanged unless prefix ARG is supplied.
  If the definition is fully onscreen, it is moved to the top of the
window.  If it is partly offscreen, the window is scrolled to get the
definition (or as much as will fit) onscreen, unless point is in a comment
which is also partly offscreen, in which case the scrolling attempts to get
as much of the comment onscreen as possible.
  Initially `reposition-window' attempts to make both the definition and
preceding comments visible.  Further invocations toggle the visibility of
the comment lines.
  If ARG is non-nil, point may move in order to make the whole defun
visible (if only part could otherwise be made so), to make the defun line
visible (if point is in code and it could not be made so, or if only
comments, including the first comment line, are visible), or to make the
first comment line visible (if point is in a comment)."
  (interactive "P")
  (let* (;; (here (save-excursion (beginning-of-line) (point)))
	 (here (point))
	 ;; change this name once I've gotten rid of references to ht.
	 ;; this is actually the number of the last screen line
	 (ht (- (window-height (selected-window)) 2))
	 (line (repos-count-screen-lines (window-start) (point)))
	 (comment-height
	  ;; The call to max deals with the case of cursor between defuns.
	  (max 0
	       (repos-count-screen-lines-signed
		;; the beginning of the preceding comment
		(save-excursion
		  (if (not (eobp)) (forward-char 1))
		  (end-of-defun -1)
		  ;; Skip whitespace, newlines, and form feeds.
		  (if (re-search-forward "[^ \t\n\f]" nil t)
		      (backward-char 1))
		  (point))
		here)))
	 (defun-height
	   (repos-count-screen-lines-signed
	    (save-excursion
	      (end-of-defun 1) ; so comments associate with following defuns
	      (beginning-of-defun 1)
	      (point))
	    here))
	 ;; This must be positive, so don't use the signed version.
	 (defun-depth (repos-count-screen-lines here
						(save-excursion
						  (end-of-defun 1)
						  (point))))
	 (defun-line-onscreen-p
	   (and (<= defun-height line)
		(<= (- line defun-height) ht))))
    (cond ((or (= comment-height line)
	       (and (= line ht)
		    (> comment-height line)
		    ;; if defun line offscreen, we should be in case 4
		    defun-line-onscreen-p))
	   ;; Either first comment line is at top of screen or (point at
	   ;; bottom of screen, defun line onscreen, and first comment line
	   ;; off top of screen).  That is, it looks like we just did
	   ;; recenter-definition, trying to fit as much of the comment
	   ;; onscreen as possible.  Put defun line at top of screen; that
	   ;; is, show as much code, and as few comments, as possible.

	   (if (and arg (> defun-depth (1+ ht)))
	       ;; Can't fit whole defun onscreen without moving point.
	       (progn (end-of-defun) (beginning-of-defun) (recenter 0))
	     (recenter (max defun-height 0)))
	   ;;(repos-debug-macro "1")
	   )

	  ((or (= defun-height line)
	       (= line 0)
	       (and (< line comment-height)
		    (< defun-height 0)))
	   ;; Defun line or cursor at top of screen, OR cursor in comment
	   ;; whose first line is offscreen.
	   ;; Avoid moving definition up even if defun runs offscreen;
	   ;; we care more about getting the comment onscreen.

	   (cond ((= line ht)
		  ;; cursor on last screen line (and so in a comment)
		  (if arg (progn (end-of-defun) (beginning-of-defun)))
		  (recenter 0)
		  ;;(repos-debug-macro "2a")
		  )

		 ;; This condition, copied from case 4, may not be quite right

		 ((and arg (< ht comment-height))
		  ;; Can't get first comment line onscreen.
		  ;; Go there and try again.
		  (forward-line (- comment-height))
		  (beginning-of-line)
		  ;; was (reposition-window)
		  (recenter 0)
		  ;;(repos-debug-macro "2b")
		  )
		 (t
		  (recenter (min ht comment-height))
		  ;;(repos-debug-macro "2c")
		  ))
	   ;; (recenter (min ht comment-height))
	   )

	  ((and (> (+ line defun-depth -1) ht)
		defun-line-onscreen-p)
	   ;; Defun runs off the bottom of the screen and the defun line
	   ;; is onscreen.
	   ;; Move the defun up.
	   (recenter (max 0 (1+ (- ht defun-depth)) defun-height))
	   ;;(repos-debug-macro "3")
	   )

	  (t
	   ;; If on the bottom line and comment start is offscreen
	   ;; then just move all comments offscreen, or at least as
	   ;; far as they'll go.

	   ;; Try to get as much of the comments onscreen as possible.
	   (if (and arg (< ht comment-height))
	       ;; Can't get defun line onscreen; go there and try again.
	       (progn (forward-line (- defun-height))
		      (beginning-of-line)
		      (reposition-window))
	     (recenter (min ht comment-height)))
	   ;;(repos-debug-macro "4")
	   ))))

;;;###autoload (define-key esc-map "\C-l" 'reposition-window)

;;; Auxiliary functions

;; Return number of screen lines between START and END.
(defun repos-count-screen-lines (start end)
  (save-excursion
    (save-restriction
      (narrow-to-region start end)
      (goto-char (point-min))
      (vertical-motion (- (point-max) (point-min))))))

;; Return number of screen lines between START and END; returns a negative
;; number if END precedes START.
(defun repos-count-screen-lines-signed (start end)
  (let ((lines (repos-count-screen-lines start end)))
    (if (< start end)
	lines
      (- lines))))

;; (defmacro repos-debug-macro (case-no)
;;   `(message (concat "Case " ,case-no ": %s %s %s %s %s")
;;             ht line comment-height defun-height defun-depth))

(provide 'reposition)

;;; reposition.el ends here