comparison lisp/reposition.el @ 196:cb231c9208d8

Initial revision
author Richard M. Stallman <rms@gnu.org>
date Sat, 23 Feb 1991 20:24:49 +0000
parents
children 590c27da44e4
comparison
equal deleted inserted replaced
195:15510c65f799 196:cb231c9208d8
1 ;;; -*- Mode: Emacs-lisp -*-
2 ;; Copyright (C) 1991 Free Software Foundation, Inc.
3
4 ;; This file is part of GNU Emacs.
5
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 1, or (at your option)
9 ;; any later version.
10
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
15
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 ;;; Written by Michael D. Ernst, mernst@theory.lcs.mit.edu, 1/26/91
21
22 ;;; Reposition-window makes an entire function definition or comment visible,
23 ;;; or, if it is already visible, places it at the top of the window;
24 ;;; additional invocations toggle the visibility of comments preceding the
25 ;;; code. For the gory details, see the documentation for reposition-window;
26 ;;; rather than reading that, you may just want to play with it.
27
28 ;;; The recommended binding for this function is M-C-r:
29 ;;; (global-set-key "\e\C-r" 'reposition-window)
30
31 ;;; This tries pretty hard to do the recentering correctly; the precise
32 ;;; action depends on what the buffer looks like. If you find a situation
33 ;;; where it doesn't behave well, let me know. This function is modeled
34 ;;; after one of the same name in ZMACS, but the code is all-new and the
35 ;;; behavior in some situations differs.
36
37 (defun reposition-window (&optional arg)
38 "Make the current definition and/or comment visible.
39 Further invocations move it to the top of the window or toggle the
40 visibility of comments that precede it.
41 Point is left unchanged unless prefix ARG is supplied.
42 If the definition is fully onscreen, it is moved to the top of the
43 window. If it is partly offscreen, the window is scrolled to get the
44 definition (or as much as will fit) onscreen, unless point is in a comment
45 which is also partly offscreen, in which case the scrolling attempts to get
46 as much of the comment onscreen as possible.
47 Initially `reposition-window' attempts to make both the definition and
48 preceding comments visible. Further invocations toggle the visibility of
49 the comment lines.
50 If ARG is non-nil, point may move in order to make the whole defun
51 visible (if only part could otherwise be made so), to make the defun line
52 visible (if point is in code and it could not be made so, or if only
53 comments, including the first comment line, are visible), or to make the
54 first comment line visible (if point is in a comment)."
55 (interactive "P")
56 (let* (;; (here (save-excursion (beginning-of-line) (point)))
57 (here (point))
58 ;; change this name once I've gotten rid of references to ht.
59 ;; this is actually the number of the last screen line
60 (ht (- (window-height (selected-window)) 2))
61 (line (repos-count-screen-lines (window-start) (point)))
62 (comment-height
63 ;; The call to max deals with the case of cursor between defuns.
64 (max 0
65 (repos-count-screen-lines-signed
66 ;; the beginning of the preceding comment
67 (save-excursion
68 (forward-char 1) (end-of-defun -1)
69 ;; Skip whitespace, newlines, and form feeds.
70 (re-search-forward "[^\\s \n\014]")
71 (backward-char 1)
72 (point))
73 here)))
74 (defun-height
75 (repos-count-screen-lines-signed
76 (save-excursion
77 (end-of-defun 1) ; so comments associate with following defuns
78 (beginning-of-defun 1)
79 (point))
80 here))
81 ;; This must be positive, so don't use the signed version.
82 (defun-depth (repos-count-screen-lines here
83 (save-excursion
84 (end-of-defun 1)
85 (point))))
86 (defun-line-onscreen-p
87 (and (<= defun-height line)
88 (<= (- line defun-height) ht))))
89 (cond ((or (= comment-height line)
90 (and (= line ht)
91 (> comment-height line)
92 ;; if defun line offscreen, we should be in case 4
93 defun-line-onscreen-p))
94 ;; Either first comment line is at top of screen or (point at
95 ;; bottom of screen, defun line onscreen, and first comment line
96 ;; off top of screen). That is, it looks like we just did
97 ;; recenter-definition, trying to fit as much of the comment
98 ;; onscreen as possible. Put defun line at top of screen; that
99 ;; is, show as much code, and as few comments, as possible.
100
101 (if (and arg (> defun-depth (1+ ht)))
102 ;; Can't fit whole defun onscreen without moving point.
103 (progn (end-of-defun) (beginning-of-defun) (recenter 0))
104 (recenter (max defun-height 0)))
105 ;;(repos-debug-macro "1")
106 )
107
108 ((or (= defun-height line)
109 (= line 0)
110 (and (< line comment-height)
111 (< defun-height 0)))
112 ;; Defun line or cursor at top of screen, OR cursor in comment
113 ;; whose first line is offscreen.
114 ;; Avoid moving definition up even if defun runs offscreen;
115 ;; we care more about getting the comment onscreen.
116
117 (cond ((= line ht)
118 ;; cursor on last screen line (and so in a comment)
119 (if arg (progn (end-of-defun) (beginning-of-defun)))
120 (recenter 0)
121 ;;(repos-debug-macro "2a")
122 )
123
124 ;; This condition, copied from case 4, may not be quite right
125
126 ((and arg (< ht comment-height))
127 ;; Can't get first comment line onscreen.
128 ;; Go there and try again.
129 (forward-line (- comment-height))
130 (beginning-of-line)
131 ;; was (reposition-window)
132 (recenter 0)
133 ;;(repos-debug-macro "2b")
134 )
135 (t
136 (recenter (min ht comment-height))
137 ;;(repos-debug-macro "2c")
138 ))
139 ;; (recenter (min ht comment-height))
140 )
141
142 ((and (> (+ line defun-depth -1) ht)
143 defun-line-onscreen-p)
144 ;; Defun runs off the bottom of the screen and the defun line
145 ;; is onscreen.
146 ;; Move the defun up.
147 (recenter (max 0 (1+ (- ht defun-depth)) defun-height))
148 ;;(repos-debug-macro "3")
149 )
150
151 (t
152 ;; If on the bottom line and comment start is offscreen
153 ;; then just move all comments offscreen, or at least as
154 ;; far as they'll go.
155
156 ;; Try to get as much of the comments onscreen as possible.
157 (if (and arg (< ht comment-height))
158 ;; Can't get defun line onscreen; go there and try again.
159 (progn (forward-line (- defun-height))
160 (beginning-of-line)
161 (reposition-window))
162 (recenter (min ht comment-height)))
163 ;;(repos-debug-macro "4")
164 ))))
165
166 ;;; Auxiliary functions
167
168 ;; Return number of screen lines between START and END.
169 (defun repos-count-screen-lines (start end)
170 (save-excursion
171 (save-restriction
172 (narrow-to-region start end)
173 (goto-char (point-min))
174 (vertical-motion (- (point-max) (point-min))))))
175
176 ;; Return number of screen lines between START and END; returns a negative
177 ;; number if END precedes START.
178 (defun repos-count-screen-lines-signed (start end)
179 (let ((lines (repos-count-screen-lines start end)))
180 (if (< start end)
181 lines
182 (- lines))))
183
184 ; (defmacro repos-debug-macro (case-no)
185 ; (` (message
186 ; (concat "Case " (, case-no) ": %s %s %s %s %s")
187 ; ht line comment-height defun-height defun-depth)))
188