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