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