Mercurial > emacs
annotate lisp/view.el @ 911:e18a3f6689a9
entered into RCS
author | Roland McGrath <roland@gnu.org> |
---|---|
date | Mon, 03 Aug 1992 16:41:27 +0000 |
parents | 4c6cdb66c74c |
children | 1dac458b5117 |
rev | line source |
---|---|
657
fec3f9a1e3e5
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
1 ;;; view.el --- peruse file or buffer without editing. |
fec3f9a1e3e5
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
2 |
840
113281b361ec
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
3 ;; Copyright (C) 1985, 1989 Free Software Foundation, Inc. |
113281b361ec
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
4 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
657
diff
changeset
|
5 ;; Author: K. Shane Hartman |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
657
diff
changeset
|
6 ;; Maintainer: FSF |
44 | 7 |
8 ;; This file is part of GNU Emacs. | |
9 | |
10 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 ;; it under the terms of the GNU General Public License as published by | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
657
diff
changeset
|
12 ;; the Free Software Foundation; either version 2, or (at your option) |
44 | 13 ;; any later version. |
14 | |
15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
21 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
657
diff
changeset
|
24 ;;; Code: |
44 | 25 |
26 (defvar view-mode-map nil) | |
27 (if view-mode-map | |
28 nil | |
29 (setq view-mode-map (make-keymap)) | |
354 | 30 (fillarray (nth 1 view-mode-map) 'View-undefined) |
44 | 31 (define-key view-mode-map "\C-c" 'view-exit) |
32 (define-key view-mode-map "\C-z" 'suspend-emacs) | |
33 (define-key view-mode-map "q" 'view-exit) | |
34 (define-key view-mode-map "-" 'negative-argument) | |
35 (define-key view-mode-map "0" 'digit-argument) | |
36 (define-key view-mode-map "1" 'digit-argument) | |
37 (define-key view-mode-map "2" 'digit-argument) | |
38 (define-key view-mode-map "3" 'digit-argument) | |
39 (define-key view-mode-map "4" 'digit-argument) | |
40 (define-key view-mode-map "5" 'digit-argument) | |
41 (define-key view-mode-map "6" 'digit-argument) | |
42 (define-key view-mode-map "7" 'digit-argument) | |
43 (define-key view-mode-map "8" 'digit-argument) | |
44 (define-key view-mode-map "9" 'digit-argument) | |
45 (define-key view-mode-map "\C-u" 'universal-argument) | |
46 (define-key view-mode-map "\e" nil) | |
47 (define-key view-mode-map "\C-x" 'Control-X-prefix) | |
48 (define-key view-mode-map "\e-" 'negative-argument) | |
49 (define-key view-mode-map "\e0" 'digit-argument) | |
50 (define-key view-mode-map "\e1" 'digit-argument) | |
51 (define-key view-mode-map "\e2" 'digit-argument) | |
52 (define-key view-mode-map "\e3" 'digit-argument) | |
53 (define-key view-mode-map "\e4" 'digit-argument) | |
54 (define-key view-mode-map "\e5" 'digit-argument) | |
55 (define-key view-mode-map "\e6" 'digit-argument) | |
56 (define-key view-mode-map "\e7" 'digit-argument) | |
57 (define-key view-mode-map "\e8" 'digit-argument) | |
58 (define-key view-mode-map "\e9" 'digit-argument) | |
59 (define-key view-mode-map "<" 'beginning-of-buffer) | |
60 (define-key view-mode-map ">" 'end-of-buffer) | |
61 (define-key view-mode-map "\ev" 'View-scroll-lines-backward) | |
62 (define-key view-mode-map "\C-v" 'View-scroll-lines-forward) | |
63 (define-key view-mode-map " " 'View-scroll-lines-forward) | |
64 (define-key view-mode-map "\177" 'View-scroll-lines-backward) | |
65 (define-key view-mode-map "\n" 'View-scroll-one-more-line) | |
66 (define-key view-mode-map "\r" 'View-scroll-one-more-line) | |
67 (define-key view-mode-map "\C-l" 'recenter) | |
68 (define-key view-mode-map "z" 'View-scroll-lines-forward-set-scroll-size) | |
69 (define-key view-mode-map "g" 'View-goto-line) | |
70 (define-key view-mode-map "=" 'what-line) | |
71 (define-key view-mode-map "." 'set-mark-command) | |
72 (define-key view-mode-map "\C-@" 'set-mark-command) | |
73 (define-key view-mode-map "'" 'View-back-to-mark) | |
74 (define-key view-mode-map "@" 'View-back-to-mark) | |
75 (define-key view-mode-map "x" 'exchange-point-and-mark) | |
76 (define-key view-mode-map "h" 'Helper-describe-bindings) | |
77 (define-key view-mode-map "?" 'Helper-describe-bindings) | |
78 (define-key view-mode-map "\C-h" 'Helper-help) | |
79 (define-key view-mode-map "\C-n" 'next-line) | |
80 (define-key view-mode-map "\C-p" 'previous-line) | |
81 (define-key view-mode-map "\C-s" 'isearch-forward) | |
82 (define-key view-mode-map "\C-r" 'isearch-backward) | |
83 (define-key view-mode-map "s" 'isearch-forward) | |
84 (define-key view-mode-map "r" 'isearch-backward) | |
85 (define-key view-mode-map "/" 'View-search-regexp-forward) | |
86 (define-key view-mode-map "\\" 'View-search-regexp-backward) | |
87 ;; This conflicts with the standard binding of isearch-regexp-forward | |
88 (define-key view-mode-map "\e\C-s" 'View-search-regexp-forward) | |
89 (define-key view-mode-map "\e\C-r" 'View-search-regexp-backward) | |
90 (define-key view-mode-map "n" 'View-search-last-regexp-forward) | |
91 (define-key view-mode-map "p" 'View-search-last-regexp-backward) | |
92 ) | |
93 | |
94 | |
256 | 95 ;;;###autoload |
44 | 96 (defun view-file (file-name) |
97 "View FILE in View mode, returning to previous buffer when done. | |
98 The usual Emacs commands are not available; instead, | |
99 a special set of commands (mostly letters and punctuation) | |
100 are defined for moving around in the buffer. | |
101 Space scrolls forward, Delete scrolls backward. | |
102 For list of all View commands, type ? or h while viewing. | |
103 | |
104 Calls the value of view-hook if that is non-nil." | |
105 (interactive "fView file: ") | |
106 (let ((old-buf (current-buffer)) | |
107 (had-a-buf (get-file-buffer file-name)) | |
108 (buf-to-view (find-file-noselect file-name))) | |
109 (switch-to-buffer buf-to-view t) | |
110 (view-mode old-buf | |
111 (and (not had-a-buf) (not (buffer-modified-p buf-to-view)) | |
112 'kill-buffer)))) | |
113 | |
256 | 114 ;;;###autoload |
354 | 115 (defun view-file-other-window (file-name) |
116 "View FILE in View mode in other window. | |
117 Return to previous buffer when done. | |
118 The usual Emacs commands are not available; instead, | |
119 a special set of commands (mostly letters and punctuation) | |
120 are defined for moving around in the buffer. | |
121 Space scrolls forward, Delete scrolls backward. | |
122 For list of all View commands, type ? or h while viewing. | |
123 | |
124 Calls the value of view-hook if that is non-nil." | |
125 (interactive "fView file: ") | |
126 (let ((old-arrangement (current-window-configuration)) | |
127 (had-a-buf (get-file-buffer file-name)) | |
128 (buf-to-view (find-file-noselect file-name))) | |
129 (switch-to-buffer-other-window buf-to-view) | |
130 (view-mode old-arrangement | |
131 (and (not had-a-buf) (not (buffer-modified-p buf-to-view)) | |
132 'kill-buffer)))) | |
133 | |
134 ;;;###autoload | |
44 | 135 (defun view-buffer (buffer-name) |
136 "View BUFFER in View mode, returning to previous buffer when done. | |
137 The usual Emacs commands are not available; instead, | |
138 a special set of commands (mostly letters and punctuation) | |
139 are defined for moving around in the buffer. | |
140 Space scrolls forward, Delete scrolls backward. | |
141 For list of all View commands, type ? or h while viewing. | |
142 | |
143 Calls the value of view-hook if that is non-nil." | |
144 (interactive "bView buffer: ") | |
145 (let ((old-buf (current-buffer))) | |
146 (switch-to-buffer buffer-name t) | |
147 (view-mode old-buf nil))) | |
148 | |
256 | 149 ;;;###autoload |
354 | 150 (defun view-buffer-other-window (buffer-name not-return) |
151 "View BUFFER in View mode in another window, | |
152 returning to original buffer when done ONLY if | |
153 prefix argument not-return is nil (as by default). | |
154 The usual Emacs commands are not available; instead, | |
155 a special set of commands (mostly letters and punctuation) | |
156 are defined for moving around in the buffer. | |
157 Space scrolls forward, Delete scrolls backward. | |
158 For list of all View commands, type ? or h while viewing. | |
159 | |
160 Calls the value of view-hook if that is non-nil." | |
161 (interactive \"bView buffer:\\nP\") | |
162 (let ((return-to (and not-return (current-window-configuration)))) | |
163 (switch-to-buffer-other-window buffer-name) | |
164 (view-mode return-to))) | |
165 | |
166 ;;;###autoload | |
44 | 167 (defun view-mode (&optional prev-buffer action) |
168 "Major mode for viewing text but not editing it. | |
169 Letters do not insert themselves. Instead these commands are provided. | |
170 Most commands take prefix arguments. Commands dealing with lines | |
171 default to \"scroll size\" lines (initially size of window). | |
172 Search commands default to a repeat count of one. | |
173 M-< or < move to beginning of buffer. | |
174 M-> or > move to end of buffer. | |
175 C-v or Space scroll forward lines. | |
176 M-v or DEL scroll backward lines. | |
177 CR or LF scroll forward one line (backward with prefix argument). | |
178 z like Space except set number of lines for further | |
179 scrolling commands to scroll by. | |
180 C-u and Digits provide prefix arguments. `-' denotes negative argument. | |
181 = prints the current line number. | |
182 g goes to line given by prefix argument. | |
183 / or M-C-s searches forward for regular expression | |
184 \\ or M-C-r searches backward for regular expression. | |
185 n searches forward for last regular expression. | |
186 p searches backward for last regular expression. | |
187 C-@ or . set the mark. | |
188 x exchanges point and mark. | |
189 C-s or s do forward incremental search. | |
190 C-r or r do reverse incremental search. | |
191 @ or ' return to mark and pops mark ring. | |
192 Mark ring is pushed at start of every | |
193 successful search and when jump to line to occurs. | |
194 The mark is set on jump to buffer start or end. | |
195 ? or h provide help message (list of commands). | |
196 C-h provides help (list of commands or description of a command). | |
197 C-n moves down lines vertically. | |
198 C-p moves upward lines vertically. | |
199 C-l recenters the screen. | |
200 q or C-c exit view-mode and return to previous buffer. | |
201 | |
202 Entry to this mode calls the value of view-hook if non-nil. | |
203 \\{view-mode-map}" | |
204 ; Not interactive because dangerous things happen | |
205 ; if you call it without passing a buffer as argument | |
206 ; and they are not easy to fix. | |
207 ; (interactive) | |
208 (make-local-variable 'view-old-mode-line-buffer-identification) | |
209 (setq view-old-mode-line-buffer-identification | |
210 mode-line-buffer-identification) | |
211 (make-local-variable 'view-old-buffer-read-only) | |
212 (setq view-old-buffer-read-only buffer-read-only) | |
213 (make-local-variable 'view-old-mode-name) | |
214 (setq view-old-mode-name mode-name) | |
215 (make-local-variable 'view-old-major-mode) | |
216 (setq view-old-major-mode major-mode) | |
217 (make-local-variable 'view-old-local-map) | |
218 (setq view-old-local-map (current-local-map)) | |
219 (make-local-variable 'view-old-Helper-return-blurb) | |
220 (setq view-old-Helper-return-blurb | |
221 (and (boundp 'Helper-return-blurb) Helper-return-blurb)) | |
222 | |
223 (setq buffer-read-only t) | |
224 (setq mode-line-buffer-identification | |
225 (list | |
226 (if (buffer-file-name) | |
227 "Viewing %f" | |
228 "Viewing %b"))) | |
229 (setq mode-name "View") | |
230 (setq major-mode 'view-mode) | |
231 (setq Helper-return-blurb | |
232 (format "continue viewing %s" | |
233 (if (buffer-file-name) | |
234 (file-name-nondirectory (buffer-file-name)) | |
235 (buffer-name)))) | |
236 | |
237 (make-local-variable 'view-exit-action) | |
238 (setq view-exit-action action) | |
354 | 239 (make-local-variable 'view-return-here) |
240 (setq view-return-here prev-buffer) | |
44 | 241 (make-local-variable 'view-exit-position) |
242 (setq view-exit-position (point-marker)) | |
243 | |
244 (make-local-variable 'view-scroll-size) | |
245 (setq view-scroll-size nil) | |
246 (make-local-variable 'view-last-regexp) | |
247 (setq view-last-regexp nil) | |
248 | |
249 (beginning-of-line) | |
250 (setq goal-column nil) | |
251 | |
252 (use-local-map view-mode-map) | |
253 (run-hooks 'view-hook) | |
254 (view-helpful-message)) | |
255 | |
354 | 256 |
44 | 257 (defun view-exit () |
258 "Exit from view-mode. | |
259 If you viewed an existing buffer, that buffer returns to its previous mode. | |
260 If you viewed a file that was not present in Emacs, its buffer is killed." | |
261 (interactive) | |
262 (setq mode-line-buffer-identification | |
263 view-old-mode-line-buffer-identification) | |
264 (setq major-mode view-old-major-mode) | |
265 (setq mode-name view-old-mode-name) | |
266 (use-local-map (current-local-map)) | |
267 (setq buffer-read-only view-old-buffer-read-only) | |
268 | |
269 (goto-char view-exit-position) | |
270 (set-marker view-exit-position nil) | |
271 | |
272 ;; Now do something to the buffer that we were viewing | |
273 ;; (such as kill it). | |
274 (let ((viewed-buffer (current-buffer)) | |
275 (action view-exit-action)) | |
354 | 276 (cond |
277 ((bufferp view-return-here) | |
278 (switch-to-buffer view-return-here)) | |
279 ((window-configuration-p view-return-here) | |
280 (set-window-configuration view-return-here))) | |
44 | 281 (if action (funcall action viewed-buffer)))) |
282 | |
283 (defun view-helpful-message () | |
284 (message | |
285 (if (and (eq (key-binding "\C-h") 'Helper-help) | |
286 (eq (key-binding "?") 'Helper-describe-bindings) | |
287 (eq (key-binding "\C-c") 'view-exit)) | |
288 "Type C-h for help, ? for commands, C-c to quit" | |
289 (substitute-command-keys | |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
840
diff
changeset
|
290 "Type \\[Helper-help] for help, \\[Helper-describe-bindings] for commands, \\[view-exit] to quit.")))) |
44 | 291 |
292 (defun View-undefined () | |
293 (interactive) | |
294 (ding) | |
295 (view-helpful-message)) | |
296 | |
297 (defun view-window-size () (1- (window-height))) | |
298 | |
299 (defun view-scroll-size () | |
300 (min (view-window-size) (or view-scroll-size (view-window-size)))) | |
301 | |
302 (defvar view-hook nil | |
303 "If non-nil, its value is called when viewing buffer or file.") | |
304 | |
305 ;(defun view-last-command (&optional who what) | |
306 ; (setq view-last-command-entry this-command) | |
307 ; (setq view-last-command who) | |
308 ; (setq view-last-command-argument what)) | |
309 | |
310 ;(defun View-repeat-last-command () | |
311 ; "Repeat last command issued in View mode." | |
312 ; (interactive) | |
313 ; (if (and view-last-command | |
314 ; (eq view-last-command-entry last-command)) | |
315 ; (funcall view-last-command view-last-command-argument)) | |
316 ; (setq this-command view-last-command-entry)) | |
317 | |
318 (defun View-goto-line (&optional line) | |
319 "Move to LINE in View mode. | |
320 Display is centered at LINE. Sets mark at starting position and pushes | |
321 mark ring." | |
322 (interactive "p") | |
323 (push-mark) | |
324 (goto-line (or line 1)) | |
325 (recenter (/ (view-window-size) 2))) | |
326 | |
327 (defun View-scroll-lines-forward (&optional lines) | |
328 "Scroll forward in View mode, or exit if end of text is visible. | |
329 No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size]. | |
330 Arg is number of lines to scroll." | |
331 (interactive "P") | |
332 (if (pos-visible-in-window-p (point-max)) | |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
840
diff
changeset
|
333 (view-exit)) |
44 | 334 (setq lines |
335 (if lines (prefix-numeric-value lines) | |
336 (view-scroll-size))) | |
337 ; (view-last-command 'View-scroll-lines-forward lines) | |
338 (if (>= lines (view-window-size)) | |
339 (scroll-up nil) | |
340 (if (>= (- lines) (view-window-size)) | |
341 (scroll-down nil) | |
342 (scroll-up lines))) | |
343 (cond ((pos-visible-in-window-p (point-max)) | |
344 (goto-char (point-max)) | |
345 (recenter -1) | |
346 (message (substitute-command-keys | |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
840
diff
changeset
|
347 "End. Type \\[view-exit] to quit viewing.")))) |
44 | 348 (move-to-window-line -1) |
349 (beginning-of-line)) | |
350 | |
351 (defun View-scroll-lines-forward-set-scroll-size (&optional lines) | |
352 "Scroll forward LINES lines in View mode, setting the \"scroll size\". | |
353 This is the number of lines which \\[View-scroll-lines-forward] and \\[View-scroll-lines-backward] scroll by default. | |
354 The absolute value of LINES is used, so this command can be used to scroll | |
355 backwards (but \"scroll size\" is always positive). If LINES is greater than | |
356 window height or omitted, then window height is assumed. If LINES is less | |
357 than window height then scrolling context is provided from previous screen." | |
358 (interactive "P") | |
359 (if (not lines) | |
360 (setq view-scroll-size (view-window-size)) | |
361 (setq lines (prefix-numeric-value lines)) | |
362 (setq view-scroll-size | |
363 (min (if (> lines 0) lines (- lines)) (view-window-size)))) | |
364 (View-scroll-lines-forward lines)) | |
365 | |
366 (defun View-scroll-one-more-line (&optional arg) | |
367 "Scroll one more line up in View mode. | |
368 With ARG scroll one line down." | |
369 (interactive "P") | |
370 (View-scroll-lines-forward (if (not arg) 1 -1))) | |
371 | |
372 (defun View-scroll-lines-backward (&optional lines) | |
373 "Scroll backward in View mode. | |
374 No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size]. | |
375 Arg is number of lines to scroll." | |
376 (interactive "P") | |
377 (View-scroll-lines-forward (if lines | |
378 (- (prefix-numeric-value lines)) | |
379 (- (view-scroll-size))))) | |
380 | |
381 (defun View-search-regexp-forward (times regexp) | |
382 "Search forward for NTH occurrence of REGEXP in View mode. | |
383 Displays line found at center of window. REGEXP is remembered for | |
384 searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward]. Sets mark at starting position and pushes mark ring." | |
385 (interactive "p\nsSearch forward (regexp): ") | |
386 (if (> (length regexp) 0) | |
387 (progn | |
388 ;(view-last-command 'View-search-last-regexp-forward times) | |
389 (view-search times regexp)))) | |
390 | |
391 (defun View-search-regexp-backward (times regexp) | |
392 "Search backward from window start for NTH instance of REGEXP in View mode. | |
393 Displays line found at center of window. REGEXP is remembered for | |
394 searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward]. Sets mark at starting position and pushes mark ring." | |
395 (interactive "p\nsSearch backward (regexp): ") | |
396 (View-search-regexp-forward (- times) regexp)) | |
397 | |
398 (defun View-search-last-regexp-forward (times) | |
399 "Search forward from window end for NTH instance of last regexp in View mode. | |
400 Displays line found at center of window. Sets mark at starting position | |
401 and pushes mark ring." | |
402 (interactive "p") | |
403 (View-search-regexp-forward times view-last-regexp)) | |
404 | |
405 (defun View-search-last-regexp-backward (times) | |
406 "Search backward from window start for NTH instance of last regexp in View mode. | |
407 Displays line found at center of window. Sets mark at starting position and | |
408 pushes mark ring." | |
409 (interactive "p") | |
410 (View-search-regexp-backward times view-last-regexp)) | |
411 | |
412 (defun View-back-to-mark (&optional ignore) | |
413 "Return to last mark set in View mode, else beginning of file. | |
414 Displays line at center of window. Pops mark ring so successive | |
415 invocations return to earlier marks." | |
416 (interactive) | |
417 (goto-char (or (mark) (point-min))) | |
418 (pop-mark) | |
419 (recenter (/ (view-window-size) 2))) | |
420 | |
421 (defun view-search (times regexp) | |
422 (setq view-last-regexp regexp) | |
423 (let (where) | |
424 (save-excursion | |
425 (move-to-window-line (if (< times 0) 0 -1)) | |
426 (if (re-search-forward regexp nil t times) | |
427 (setq where (point)))) | |
428 (if where | |
429 (progn | |
430 (push-mark) | |
431 (goto-char where) | |
432 (beginning-of-line) | |
433 (recenter (/ (view-window-size) 2))) | |
434 (message "Can't find occurrence %d of %s" times regexp) | |
435 (sit-for 4)))) | |
436 | |
354 | 437 |
584 | 438 (provide 'view) |
439 | |
657
fec3f9a1e3e5
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
440 ;;; view.el ends here |