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