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