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