(undigestify-rmail-message): Better error messages.
[bpt/emacs.git] / lisp / view.el
1 ;;; view.el --- peruse file or buffer without editing.
2
3 ;; Copyright (C) 1985, 1989, 1994, 1995 Free Software Foundation, Inc.
4
5 ;; Author: K. Shane Hartman
6 ;; Maintainer: FSF
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
12 ;; the Free Software Foundation; either version 2, or (at your option)
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
24 ;;; Commentary:
25
26 ;; This package provides the `view' minor mode documented in the Emacs
27 ;; user's manual.
28
29 ;;; Code:
30
31 (defvar view-mode nil "Non-nil if View mode is enabled.")
32 (make-variable-buffer-local 'view-mode)
33
34 (defvar view-mode-auto-exit nil
35 "Non-nil means scrolling past the end of buffer exits View mode.")
36 (make-variable-buffer-local 'view-mode-auto-exit)
37
38 (defvar view-old-buffer-read-only nil)
39 (make-variable-buffer-local 'view-old-buffer-read-only)
40 (defvar view-old-Helper-return-blurb)
41 (make-variable-buffer-local 'view-old-Helper-return-blurb)
42
43 (defvar view-scroll-size nil)
44 (make-variable-buffer-local 'view-scroll-size)
45
46 (defvar view-last-regexp nil)
47 (make-variable-buffer-local 'view-last-regexp)
48
49 (defvar view-exit-action nil)
50 (make-variable-buffer-local 'view-exit-action)
51 (defvar view-return-here nil)
52 (make-variable-buffer-local 'view-return-here)
53 (defvar view-exit-position nil)
54 (make-variable-buffer-local 'view-exit-position)
55
56 (or (assq 'view-mode minor-mode-alist)
57 (setq minor-mode-alist
58 (cons '(view-mode " View") minor-mode-alist)))
59
60 (defvar view-mode-map nil)
61 (if view-mode-map
62 nil
63 (setq view-mode-map (make-keymap))
64 (suppress-keymap view-mode-map)
65 (define-key view-mode-map "q" 'view-exit)
66 (define-key view-mode-map "<" 'beginning-of-buffer)
67 (define-key view-mode-map ">" 'end-of-buffer)
68 (define-key view-mode-map "\ev" 'View-scroll-lines-backward)
69 (define-key view-mode-map "\C-v" 'View-scroll-lines-forward)
70 (define-key view-mode-map " " 'View-scroll-lines-forward)
71 (define-key view-mode-map "\C-?" 'View-scroll-lines-backward)
72 (define-key view-mode-map "\n" 'View-scroll-one-more-line)
73 (define-key view-mode-map "\r" 'View-scroll-one-more-line)
74 (define-key view-mode-map "z" 'View-scroll-lines-forward-set-scroll-size)
75 (define-key view-mode-map "g" 'View-goto-line)
76 (define-key view-mode-map "=" 'what-line)
77 (define-key view-mode-map "." 'set-mark-command)
78 (define-key view-mode-map "'" 'View-back-to-mark)
79 (define-key view-mode-map "@" 'View-back-to-mark)
80 (define-key view-mode-map "x" 'exchange-point-and-mark)
81 (define-key view-mode-map "h" 'Helper-describe-bindings)
82 (define-key view-mode-map "?" 'Helper-describe-bindings)
83 (define-key view-mode-map (char-to-string help-char) 'Helper-help)
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 (or (assq 'view-mode minor-mode-map-alist)
96 (setq minor-mode-map-alist
97 (cons (cons 'view-mode view-mode-map) minor-mode-map-alist)))
98
99
100 ;;;###autoload
101 (defun view-file (file-name)
102 "View FILE in View mode, returning to previous buffer when done.
103 The usual Emacs commands are not available; instead,
104 a special set of commands (mostly letters and punctuation)
105 are defined for moving around in the buffer.
106 Space scrolls forward, Delete scrolls backward.
107 For list of all View commands, type ? or h while viewing.
108
109 This command runs the normal hook `view-mode-hook'."
110 (interactive "fView file: ")
111 (let ((old-buf (current-buffer))
112 (had-a-buf (get-file-buffer file-name))
113 (buf-to-view (find-file-noselect file-name)))
114 ;; This used to pass t as second argument,
115 ;; but then the buffer did not show up in the Buffers menu.
116 (switch-to-buffer buf-to-view had-a-buf)
117 (view-mode-enter old-buf
118 (and (not had-a-buf) (not (buffer-modified-p buf-to-view))
119 'kill-buffer))))
120
121 ;;;###autoload
122 (defun view-file-other-window (file-name)
123 "View FILE in View mode in other window.
124 Return to previous buffer when done.
125 The usual Emacs commands are not available; instead,
126 a special set of commands (mostly letters and punctuation)
127 are defined for moving around in the buffer.
128 Space scrolls forward, Delete scrolls backward.
129 For list of all View commands, type ? or h while viewing.
130
131 This command runs the normal hook `view-mode-hook'."
132 (interactive "fView file: ")
133 (let ((old-arrangement (current-window-configuration))
134 (had-a-buf (get-file-buffer file-name))
135 (buf-to-view (find-file-noselect file-name)))
136 (switch-to-buffer-other-window buf-to-view)
137 (view-mode-enter old-arrangement
138 (and (not had-a-buf) (not (buffer-modified-p buf-to-view))
139 'kill-buffer))))
140
141 ;;;###autoload
142 (defun view-buffer (buffer-name)
143 "View BUFFER in View mode, returning to previous buffer when done.
144 The usual Emacs commands are not available; instead,
145 a special set of commands (mostly letters and punctuation)
146 are defined for moving around in the buffer.
147 Space scrolls forward, Delete scrolls backward.
148 For list of all View commands, type ? or h while viewing.
149
150 This command runs the normal hook `view-mode-hook'."
151 (interactive "bView buffer: ")
152 (let ((old-buf (current-buffer)))
153 (switch-to-buffer buffer-name t)
154 (view-mode-enter old-buf nil)))
155
156 ;;;###autoload
157 (defun view-buffer-other-window (buffer-name not-return)
158 "View BUFFER in View mode in another window.
159 Return to previous buffer when done, unless NOT-RETURN is non-nil.
160
161 The usual Emacs commands are not available in View mode; instead,
162 a special set of commands (mostly letters and punctuation)
163 are defined for moving around in the buffer.
164 Space scrolls forward, Delete scrolls backward.
165 For list of all View commands, type ? or h while viewing.
166
167 This command runs the normal hook `view-mode-hook'."
168 (interactive "bView buffer:\nP")
169 (let ((return-to (and not-return (current-window-configuration))))
170 (switch-to-buffer-other-window buffer-name)
171 (view-mode-enter return-to)))
172
173 ;;;###autoload
174 (defun view-mode (&optional arg)
175 "Toggle View mode.
176 If you use this function to turn on View mode,
177 \"exiting\" View mode does nothing except turn View mode off.
178 The other way to turn View mode on is by calling
179 `view-mode-enter'."
180 (interactive "P")
181 (setq view-mode
182 (if (null arg)
183 (not view-mode)
184 (> (prefix-numeric-value arg) 0)))
185 (force-mode-line-update))
186
187 (defun view-mode-enter (&optional prev-buffer action)
188 "Minor mode for viewing text but not editing it.
189 Letters do not insert themselves. Instead these commands are provided.
190 Most commands take prefix arguments. Commands dealing with lines
191 default to \"scroll size\" lines (initially size of window).
192 Search commands default to a repeat count of one.
193 M-< or < move to beginning of buffer.
194 M-> or > move to end of buffer.
195 C-v or Space scroll forward lines.
196 M-v or DEL scroll backward lines.
197 CR or LF scroll forward one line (backward with prefix argument).
198 z like Space except set number of lines for further
199 scrolling commands to scroll by.
200 C-u and Digits provide prefix arguments. `-' denotes negative argument.
201 = prints the current line number.
202 g goes to line given by prefix argument.
203 / or M-C-s searches forward for regular expression
204 \\ or M-C-r searches backward for regular expression.
205 n searches forward for last regular expression.
206 p searches backward for last regular expression.
207 C-@ or . set the mark.
208 x exchanges point and mark.
209 C-s or s do forward incremental search.
210 C-r or r do reverse incremental search.
211 @ or ' return to mark and pops mark ring.
212 Mark ring is pushed at start of every
213 successful search and when jump to line to occurs.
214 The mark is set on jump to buffer start or end.
215 ? or h provide help message (list of commands).
216 \\[Helper-help] provides help (list of commands or description of a command).
217 C-n moves down lines vertically.
218 C-p moves upward lines vertically.
219 C-l recenters the screen.
220 q exit view-mode and return to previous buffer.
221
222 This function runs the normal hook `view-mode-hook'.
223
224 \\{view-mode-map}"
225 ; Not interactive because dangerous things happen
226 ; if you call it without passing a buffer as argument
227 ; and they are not easy to fix.
228 ; (interactive)
229 (setq view-old-buffer-read-only buffer-read-only)
230 (setq view-old-Helper-return-blurb
231 (and (boundp 'Helper-return-blurb) Helper-return-blurb))
232
233 ;; Enable view-exit to make use of the data we just saved
234 ;; and to perform the exit action.
235 (setq view-mode-auto-exit t)
236
237 (setq buffer-read-only t)
238 (setq view-mode t)
239 (setq Helper-return-blurb
240 (format "continue viewing %s"
241 (if (buffer-file-name)
242 (file-name-nondirectory (buffer-file-name))
243 (buffer-name))))
244
245 (setq view-exit-action action)
246 (setq view-return-here prev-buffer)
247 (setq view-exit-position (point-marker))
248
249 (beginning-of-line)
250 (setq goal-column nil)
251
252 (run-hooks 'view-mode-hook)
253 (message
254 (substitute-command-keys
255 "Type \\[Helper-help] for help, \\[Helper-describe-bindings] for commands, \\[view-exit] to quit.")))
256 \f
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 view-mode nil)
263 (force-mode-line-update)
264 (cond (view-mode-auto-exit
265 (setq buffer-read-only view-old-buffer-read-only)
266 (setq view-mode-auto-exit nil)
267
268 (goto-char view-exit-position)
269 (set-marker view-exit-position nil)
270
271 ;; Now do something to the buffer that we were viewing
272 ;; (such as kill it).
273 (let ((viewed-buffer (current-buffer))
274 (action view-exit-action))
275 (cond
276 ((bufferp view-return-here)
277 (switch-to-buffer view-return-here))
278 ((window-configuration-p view-return-here)
279 (set-window-configuration view-return-here)))
280 (if action (funcall action viewed-buffer))))))
281
282 (defun view-window-size () (1- (window-height)))
283
284 (defun view-scroll-size ()
285 (min (view-window-size) (or view-scroll-size (view-window-size))))
286
287 (defvar view-mode-hook nil
288 "Normal hook run when starting to view a buffer or file.")
289
290 ;(defun view-last-command (&optional who what)
291 ; (setq view-last-command-entry this-command)
292 ; (setq view-last-command who)
293 ; (setq view-last-command-argument what))
294
295 ;(defun View-repeat-last-command ()
296 ; "Repeat last command issued in View mode."
297 ; (interactive)
298 ; (if (and view-last-command
299 ; (eq view-last-command-entry last-command))
300 ; (funcall view-last-command view-last-command-argument))
301 ; (setq this-command view-last-command-entry))
302
303 (defun View-goto-line (line)
304 "Move to line LINE in View mode.
305 Display is centered at LINE. Sets mark at starting position and pushes
306 mark ring."
307 (interactive "p")
308 (push-mark)
309 (goto-line line)
310 (recenter (/ (view-window-size) 2)))
311
312 (defun View-scroll-lines-forward (&optional lines)
313 "Scroll forward in View mode, or exit if end of text is visible.
314 No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size].
315 Arg is number of lines to scroll."
316 (interactive "P")
317 (setq lines
318 (if lines (prefix-numeric-value lines)
319 (view-scroll-size)))
320 (if (and (pos-visible-in-window-p (point-max))
321 ;; Allow scrolling backward at the end of the buffer.
322 (> lines 0)
323 view-mode-auto-exit)
324 (view-exit)
325 ;; (view-last-command 'View-scroll-lines-forward lines)
326 (if (>= lines (view-window-size))
327 (scroll-up nil)
328 (if (>= (- lines) (view-window-size))
329 (scroll-down nil)
330 (scroll-up lines)))
331 (cond ((pos-visible-in-window-p (point-max))
332 (goto-char (point-max))
333 (recenter -1)
334 (message (substitute-command-keys
335 "End. Type \\[view-exit] to quit viewing."))))
336 (move-to-window-line -1)
337 (beginning-of-line)))
338
339 (defun View-scroll-lines-forward-set-scroll-size (&optional lines)
340 "Scroll forward LINES lines in View mode, setting the \"scroll size\".
341 This is the number of lines which \\[View-scroll-lines-forward] and \\[View-scroll-lines-backward] scroll by default.
342 The absolute value of LINES is used, so this command can be used to scroll
343 backwards (but \"scroll size\" is always positive). If LINES is greater than
344 window height or omitted, then window height is assumed. If LINES is less
345 than window height then scrolling context is provided from previous screen."
346 (interactive "P")
347 (if (not lines)
348 (setq view-scroll-size (view-window-size))
349 (setq lines (prefix-numeric-value lines))
350 (setq view-scroll-size
351 (min (if (> lines 0) lines (- lines)) (view-window-size))))
352 (View-scroll-lines-forward lines))
353
354 (defun View-scroll-one-more-line (&optional arg)
355 "Scroll one more line up in View mode.
356 With ARG scroll one line down."
357 (interactive "P")
358 (View-scroll-lines-forward (if (not arg) 1 -1)))
359
360 (defun View-scroll-lines-backward (&optional lines)
361 "Scroll backward in View mode.
362 No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size].
363 Arg is number of lines to scroll."
364 (interactive "P")
365 (View-scroll-lines-forward (if lines
366 (- (prefix-numeric-value lines))
367 (- (view-scroll-size)))))
368
369 (defun View-search-regexp-forward (n regexp)
370 "Search forward for Nth occurrence of REGEXP.
371 Displays line found at center of window. REGEXP is remembered for
372 searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward]. Sets mark at starting position and pushes mark ring."
373 (interactive "p\nsSearch forward (regexp): ")
374 (if (> (length regexp) 0)
375 (progn
376 ;(view-last-command 'View-search-last-regexp-forward n)
377 (view-search n regexp))))
378
379 (defun View-search-regexp-backward (n regexp)
380 "Search backward from window start for Nth instance of REGEXP.
381 Displays line found at center of window. REGEXP is remembered for
382 searching 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 backward (regexp): ")
384 (View-search-regexp-forward (- n) regexp))
385
386 (defun View-search-last-regexp-forward (n)
387 "Search forward from window end for Nth instance of last regexp.
388 Displays line found at center of window. Sets mark at starting position
389 and pushes mark ring."
390 (interactive "p")
391 (View-search-regexp-forward n view-last-regexp))
392
393 (defun View-search-last-regexp-backward (n)
394 "Search backward from window start for Nth instance of last regexp.
395 Displays line found at center of window. Sets mark at starting position and
396 pushes mark ring."
397 (interactive "p")
398 (View-search-regexp-backward n view-last-regexp))
399
400 (defun View-back-to-mark (&optional ignore)
401 "Return to last mark set in View mode, else beginning of file.
402 Displays line at center of window. Pops mark ring so successive
403 invocations return to earlier marks."
404 (interactive)
405 (goto-char (or (mark t) (point-min)))
406 (pop-mark)
407 (recenter (/ (view-window-size) 2)))
408
409 (defun view-search (times regexp)
410 (setq view-last-regexp regexp)
411 (let (where)
412 (save-excursion
413 (move-to-window-line (if (< times 0) 0 -1))
414 (if (re-search-forward regexp nil t times)
415 (setq where (point))))
416 (if where
417 (progn
418 (push-mark)
419 (goto-char where)
420 (beginning-of-line)
421 (recenter (/ (view-window-size) 2)))
422 (message "Can't find occurrence %d of %s" times regexp)
423 (sit-for 4))))
424
425 \f
426 (provide 'view)
427
428 ;;; view.el ends here