Commit | Line | Data |
---|---|---|
57d378a9 RS |
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 | ||
cfede761 | 20 | ;;; Written by Michael D. Ernst, mernst@theory.lcs.mit.edu, Jan 1991. |
57d378a9 RS |
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 | ||
57d378a9 RS |
28 | ;;; This tries pretty hard to do the recentering correctly; the precise |
29 | ;;; action depends on what the buffer looks like. If you find a situation | |
30 | ;;; where it doesn't behave well, let me know. This function is modeled | |
31 | ;;; after one of the same name in ZMACS, but the code is all-new and the | |
32 | ;;; behavior in some situations differs. | |
33 | ||
34 | (defun reposition-window (&optional arg) | |
35 | "Make the current definition and/or comment visible. | |
36 | Further invocations move it to the top of the window or toggle the | |
37 | visibility of comments that precede it. | |
38 | Point is left unchanged unless prefix ARG is supplied. | |
39 | If the definition is fully onscreen, it is moved to the top of the | |
40 | window. If it is partly offscreen, the window is scrolled to get the | |
41 | definition (or as much as will fit) onscreen, unless point is in a comment | |
42 | which is also partly offscreen, in which case the scrolling attempts to get | |
43 | as much of the comment onscreen as possible. | |
44 | Initially `reposition-window' attempts to make both the definition and | |
45 | preceding comments visible. Further invocations toggle the visibility of | |
46 | the comment lines. | |
47 | If ARG is non-nil, point may move in order to make the whole defun | |
48 | visible (if only part could otherwise be made so), to make the defun line | |
49 | visible (if point is in code and it could not be made so, or if only | |
50 | comments, including the first comment line, are visible), or to make the | |
51 | first comment line visible (if point is in a comment)." | |
52 | (interactive "P") | |
53 | (let* (;; (here (save-excursion (beginning-of-line) (point))) | |
54 | (here (point)) | |
55 | ;; change this name once I've gotten rid of references to ht. | |
56 | ;; this is actually the number of the last screen line | |
57 | (ht (- (window-height (selected-window)) 2)) | |
58 | (line (repos-count-screen-lines (window-start) (point))) | |
59 | (comment-height | |
60 | ;; The call to max deals with the case of cursor between defuns. | |
61 | (max 0 | |
62 | (repos-count-screen-lines-signed | |
63 | ;; the beginning of the preceding comment | |
64 | (save-excursion | |
65 | (forward-char 1) (end-of-defun -1) | |
66 | ;; Skip whitespace, newlines, and form feeds. | |
67 | (re-search-forward "[^\\s \n\014]") | |
68 | (backward-char 1) | |
69 | (point)) | |
70 | here))) | |
71 | (defun-height | |
72 | (repos-count-screen-lines-signed | |
73 | (save-excursion | |
74 | (end-of-defun 1) ; so comments associate with following defuns | |
75 | (beginning-of-defun 1) | |
76 | (point)) | |
77 | here)) | |
78 | ;; This must be positive, so don't use the signed version. | |
79 | (defun-depth (repos-count-screen-lines here | |
80 | (save-excursion | |
81 | (end-of-defun 1) | |
82 | (point)))) | |
83 | (defun-line-onscreen-p | |
84 | (and (<= defun-height line) | |
85 | (<= (- line defun-height) ht)))) | |
86 | (cond ((or (= comment-height line) | |
87 | (and (= line ht) | |
88 | (> comment-height line) | |
89 | ;; if defun line offscreen, we should be in case 4 | |
90 | defun-line-onscreen-p)) | |
91 | ;; Either first comment line is at top of screen or (point at | |
92 | ;; bottom of screen, defun line onscreen, and first comment line | |
93 | ;; off top of screen). That is, it looks like we just did | |
94 | ;; recenter-definition, trying to fit as much of the comment | |
95 | ;; onscreen as possible. Put defun line at top of screen; that | |
96 | ;; is, show as much code, and as few comments, as possible. | |
97 | ||
98 | (if (and arg (> defun-depth (1+ ht))) | |
99 | ;; Can't fit whole defun onscreen without moving point. | |
100 | (progn (end-of-defun) (beginning-of-defun) (recenter 0)) | |
101 | (recenter (max defun-height 0))) | |
102 | ;;(repos-debug-macro "1") | |
103 | ) | |
104 | ||
105 | ((or (= defun-height line) | |
106 | (= line 0) | |
107 | (and (< line comment-height) | |
108 | (< defun-height 0))) | |
109 | ;; Defun line or cursor at top of screen, OR cursor in comment | |
110 | ;; whose first line is offscreen. | |
111 | ;; Avoid moving definition up even if defun runs offscreen; | |
112 | ;; we care more about getting the comment onscreen. | |
113 | ||
114 | (cond ((= line ht) | |
115 | ;; cursor on last screen line (and so in a comment) | |
116 | (if arg (progn (end-of-defun) (beginning-of-defun))) | |
117 | (recenter 0) | |
118 | ;;(repos-debug-macro "2a") | |
119 | ) | |
120 | ||
121 | ;; This condition, copied from case 4, may not be quite right | |
122 | ||
123 | ((and arg (< ht comment-height)) | |
124 | ;; Can't get first comment line onscreen. | |
125 | ;; Go there and try again. | |
126 | (forward-line (- comment-height)) | |
127 | (beginning-of-line) | |
128 | ;; was (reposition-window) | |
129 | (recenter 0) | |
130 | ;;(repos-debug-macro "2b") | |
131 | ) | |
132 | (t | |
133 | (recenter (min ht comment-height)) | |
134 | ;;(repos-debug-macro "2c") | |
135 | )) | |
136 | ;; (recenter (min ht comment-height)) | |
137 | ) | |
138 | ||
139 | ((and (> (+ line defun-depth -1) ht) | |
140 | defun-line-onscreen-p) | |
141 | ;; Defun runs off the bottom of the screen and the defun line | |
142 | ;; is onscreen. | |
143 | ;; Move the defun up. | |
144 | (recenter (max 0 (1+ (- ht defun-depth)) defun-height)) | |
145 | ;;(repos-debug-macro "3") | |
146 | ) | |
147 | ||
148 | (t | |
149 | ;; If on the bottom line and comment start is offscreen | |
150 | ;; then just move all comments offscreen, or at least as | |
151 | ;; far as they'll go. | |
152 | ||
153 | ;; Try to get as much of the comments onscreen as possible. | |
154 | (if (and arg (< ht comment-height)) | |
155 | ;; Can't get defun line onscreen; go there and try again. | |
156 | (progn (forward-line (- defun-height)) | |
157 | (beginning-of-line) | |
158 | (reposition-window)) | |
159 | (recenter (min ht comment-height))) | |
160 | ;;(repos-debug-macro "4") | |
161 | )))) | |
162 | ||
163 | ;;; Auxiliary functions | |
164 | ||
165 | ;; Return number of screen lines between START and END. | |
166 | (defun repos-count-screen-lines (start end) | |
167 | (save-excursion | |
168 | (save-restriction | |
169 | (narrow-to-region start end) | |
170 | (goto-char (point-min)) | |
171 | (vertical-motion (- (point-max) (point-min)))))) | |
172 | ||
173 | ;; Return number of screen lines between START and END; returns a negative | |
174 | ;; number if END precedes START. | |
175 | (defun repos-count-screen-lines-signed (start end) | |
176 | (let ((lines (repos-count-screen-lines start end))) | |
177 | (if (< start end) | |
178 | lines | |
179 | (- lines)))) | |
180 | ||
181 | ; (defmacro repos-debug-macro (case-no) | |
182 | ; (` (message | |
183 | ; (concat "Case " (, case-no) ": %s %s %s %s %s") | |
184 | ; ht line comment-height defun-height defun-depth))) | |
185 |