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