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