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