Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / lisp / view.el
1 ;;; view.el --- peruse file or buffer without editing
2
3 ;; Copyright (C) 1985, 1989, 1994-1995, 1997, 2000-2011 Free Software Foundation, Inc.
4
5 ;; Author: K. Shane Hartman
6 ;; Maintainer: Inge Frick <inge@nada.kth.se>
7 ;; Keywords: files
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; This package provides the `view' minor mode documented in the Emacs
27 ;; user's manual.
28 ;; View mode entry and exit is done through the functions view-mode-enter
29 ;; and view-mode-exit. Use these functions to enter or exit view-mode from
30 ;; emacs lisp programs.
31 ;; We use both view- and View- as prefix for symbols. View- is used as
32 ;; prefix for commands that have a key binding. view- is used for commands
33 ;; without key binding. The purpose of this is to make it easier for a
34 ;; user to use command name completion.
35
36 ;;; Suggested key bindings:
37 ;;
38 ;; (define-key ctl-x-4-map "v" 'view-file-other-window) ; ^x4v
39 ;; (define-key ctl-x-5-map "v" 'view-file-other-frame) ; ^x5v
40 ;;
41 ;; You could also bind view-file, view-buffer, view-buffer-other-window and
42 ;; view-buffer-other-frame to keys.
43 \f
44 ;;; Code:
45
46 (defgroup view nil
47 "Peruse file or buffer without editing."
48 :link '(function-link view-mode)
49 :link '(custom-manual "(emacs)Misc File Ops")
50 :group 'wp)
51
52 (defcustom view-highlight-face 'highlight
53 "The face used for highlighting the match found by View mode search."
54 :type 'face
55 :group 'view)
56
57 ;; `view-mode-auto-exit' is replaced by the following option variable which
58 ;; only says if scrolling past buffer end should leave view mode or not, it
59 ;; doesn't say if leaving view mode should restore windows or not. The latter
60 ;; is now controlled by the presence of a value in `view-return-to-alist'.
61 (defcustom view-scroll-auto-exit nil
62 "Non-nil means scrolling past the end of buffer exits View mode.
63 A value of nil means attempting to scroll past the end of the buffer,
64 only rings the bell and gives a message on how to leave."
65 :type 'boolean
66 :group 'view)
67
68 (defcustom view-try-extend-at-buffer-end nil
69 "Non-nil means try to load more of file when reaching end of buffer.
70 This variable is mainly intended to be temporarily set to non-nil by
71 the F command in view-mode, but you can set it to t if you want the action
72 for all scroll commands in view mode."
73 :type 'boolean
74 :group 'view)
75
76 ;;;###autoload
77 (defcustom view-remove-frame-by-deleting t
78 "Determine how View mode removes a frame no longer needed.
79 If nil, make an icon of the frame. If non-nil, delete the frame."
80 :type 'boolean
81 :group 'view
82 ;; Changed the default of this to t for Emacs 23. Users consider
83 ;; frame iconification annoying.
84 :version "23.1")
85
86 (defcustom view-exits-all-viewing-windows nil
87 "Non-nil means restore all windows used to view buffer.
88 Commands that restore windows when finished viewing a buffer, apply to all
89 windows that display the buffer and have restore information in
90 `view-return-to-alist'.
91 If `view-exits-all-viewing-windows' is nil, only the selected window is
92 considered for restoring."
93 :type 'boolean
94 :group 'view)
95
96 (defcustom view-inhibit-help-message nil
97 "Non-nil inhibits the help message shown upon entering View mode."
98 :type 'boolean
99 :group 'view
100 :version "22.1")
101
102 ;;;###autoload
103 (defvar view-mode nil
104 "Non-nil if View mode is enabled.
105 Don't change this variable directly, you must change it by one of the
106 functions that enable or disable view mode.")
107 ;;;###autoload
108 (make-variable-buffer-local 'view-mode)
109
110 (defcustom view-mode-hook nil
111 "Normal hook run when starting to view a buffer or file."
112 :type 'hook
113 :group 'view)
114 \f
115 (defvar view-old-buffer-read-only nil)
116 (make-variable-buffer-local 'view-old-buffer-read-only)
117
118 (defvar view-old-Helper-return-blurb)
119 (make-variable-buffer-local 'view-old-Helper-return-blurb)
120
121 (defvar view-page-size nil
122 "Default number of lines to scroll by View page commands.
123 If nil that means use the window size.")
124 (make-variable-buffer-local 'view-page-size)
125
126 (defvar view-half-page-size nil
127 "Default number of lines to scroll by View half page commands.
128 If nil that means use half the window size.")
129 (make-variable-buffer-local 'view-half-page-size)
130
131 (defvar view-last-regexp nil)
132 (make-variable-buffer-local 'view-last-regexp) ; Global is better???
133
134 (defvar view-return-to-alist nil
135 "What to do with used windows and where to go when finished viewing buffer.
136 This is local in each buffer being viewed.
137 It is added to by `view-mode-enter' when starting to view a buffer and
138 subtracted from by `view-mode-exit' when finished viewing the buffer.
139
140 See RETURN-TO-ALIST argument of function `view-mode-exit' for the format of
141 `view-return-to-alist'.")
142 (make-variable-buffer-local 'view-return-to-alist)
143 (put 'view-return-to-alist 'permanent-local t)
144
145 (defvar view-exit-action nil
146 "If non-nil, a function with one argument (a buffer) called when finished viewing.
147 Commands like \\[view-file] and \\[view-file-other-window] may
148 set this to bury or kill the viewed buffer.
149 Observe that the buffer viewed might not appear in any window at
150 the time this function is called.")
151 (make-variable-buffer-local 'view-exit-action)
152
153 (defvar view-no-disable-on-exit nil
154 "If non-nil, View mode \"exit\" commands don't actually disable View mode.
155 Instead, these commands just switch buffers or windows.
156 This is set in certain buffers by specialized features such as help commands
157 that use View mode automatically.")
158
159 (defvar view-overlay nil
160 "Overlay used to display where a search operation found its match.
161 This is local in each buffer, once it is used.")
162 (make-variable-buffer-local 'view-overlay)
163 \f
164 ;; Define keymap inside defvar to make it easier to load changes.
165 ;; Some redundant "less"-like key bindings below have been commented out.
166 (defvar view-mode-map
167 (let ((map (make-sparse-keymap)))
168 (define-key map "C" 'View-kill-and-leave)
169 (define-key map "c" 'View-leave)
170 (define-key map "Q" 'View-quit-all)
171 (define-key map "E" 'View-exit-and-edit)
172 ;; (define-key map "v" 'View-exit)
173 (define-key map "e" 'View-exit)
174 (define-key map "q" 'View-quit)
175 ;; (define-key map "N" 'View-search-last-regexp-backward)
176 (define-key map "p" 'View-search-last-regexp-backward)
177 (define-key map "n" 'View-search-last-regexp-forward)
178 ;; (define-key map "?" 'View-search-regexp-backward) ; Less does this.
179 (define-key map "\\" 'View-search-regexp-backward)
180 (define-key map "/" 'View-search-regexp-forward)
181 (define-key map "r" 'isearch-backward)
182 (define-key map "s" 'isearch-forward)
183 (define-key map "m" 'point-to-register)
184 (define-key map "'" 'register-to-point)
185 (define-key map "x" 'exchange-point-and-mark)
186 (define-key map "@" 'View-back-to-mark)
187 (define-key map "." 'set-mark-command)
188 (define-key map "%" 'View-goto-percent)
189 ;; (define-key map "G" 'View-goto-line-last)
190 (define-key map "g" 'View-goto-line)
191 (define-key map "=" 'what-line)
192 (define-key map "F" 'View-revert-buffer-scroll-page-forward)
193 ;; (define-key map "k" 'View-scroll-line-backward)
194 (define-key map "y" 'View-scroll-line-backward)
195 ;; (define-key map "j" 'View-scroll-line-forward)
196 (define-key map "\n" 'View-scroll-line-forward)
197 (define-key map "\r" 'View-scroll-line-forward)
198 (define-key map "u" 'View-scroll-half-page-backward)
199 (define-key map "d" 'View-scroll-half-page-forward)
200 (define-key map "z" 'View-scroll-page-forward-set-page-size)
201 (define-key map "w" 'View-scroll-page-backward-set-page-size)
202 ;; (define-key map "b" 'View-scroll-page-backward)
203 (define-key map "\C-?" 'View-scroll-page-backward)
204 ;; (define-key map "f" 'View-scroll-page-forward)
205 (define-key map " " 'View-scroll-page-forward)
206 (define-key map "o" 'View-scroll-to-buffer-end)
207 (define-key map ">" 'end-of-buffer)
208 (define-key map "<" 'beginning-of-buffer)
209 (define-key map "-" 'negative-argument)
210 (define-key map "9" 'digit-argument)
211 (define-key map "8" 'digit-argument)
212 (define-key map "7" 'digit-argument)
213 (define-key map "6" 'digit-argument)
214 (define-key map "5" 'digit-argument)
215 (define-key map "4" 'digit-argument)
216 (define-key map "3" 'digit-argument)
217 (define-key map "2" 'digit-argument)
218 (define-key map "1" 'digit-argument)
219 (define-key map "0" 'digit-argument)
220 (define-key map "H" 'describe-mode)
221 (define-key map "?" 'describe-mode) ; Maybe do as less instead? See above.
222 (define-key map "h" 'describe-mode)
223 map))
224 \f
225 ;;; Commands that enter or exit view mode.
226
227 ;; This is used when view mode is exited, to make sure we don't try to
228 ;; kill a buffer modified by the user. A buffer in view mode can
229 ;; become modified if the user types C-x C-q, edits the buffer, then
230 ;; types C-x C-q again to return to view mode.
231 ;;;###autoload
232 (defun kill-buffer-if-not-modified (buf)
233 "Like `kill-buffer', but does nothing if the buffer is modified."
234 (let ((buf (get-buffer buf)))
235 (and buf (not (buffer-modified-p buf))
236 (kill-buffer buf))))
237
238 ;;;###autoload
239 (defun view-file (file)
240 "View FILE in View mode, returning to previous buffer when done.
241 Emacs commands editing the buffer contents are not available; instead, a
242 special set of commands (mostly letters and punctuation) are defined for
243 moving around in the buffer.
244 Space scrolls forward, Delete scrolls backward.
245 For a list of all View commands, type H or h while viewing.
246
247 This command runs the normal hook `view-mode-hook'."
248 (interactive "fView file: ")
249 (unless (file-exists-p file) (error "%s does not exist" file))
250 (let ((had-a-buf (get-file-buffer file))
251 (buffer (find-file-noselect file)))
252 (view-buffer buffer (and (not had-a-buf) 'kill-buffer-if-not-modified))))
253
254 ;;;###autoload
255 (defun view-file-other-window (file)
256 "View FILE in View mode in another window.
257 When done, return that window to its previous buffer, and kill the
258 buffer visiting FILE if unmodified and if it wasn't visited before.
259
260 Emacs commands editing the buffer contents are not available; instead,
261 a special set of commands (mostly letters and punctuation)
262 are defined for moving around in the buffer.
263 Space scrolls forward, Delete scrolls backward.
264 For a list of all View commands, type H or h while viewing.
265
266 This command runs the normal hook `view-mode-hook'."
267 (interactive "fIn other window view file: ")
268 (unless (file-exists-p file) (error "%s does not exist" file))
269 (let ((had-a-buf (get-file-buffer file))
270 (buf-to-view (find-file-noselect file)))
271 (view-buffer-other-window buf-to-view nil
272 (and (not had-a-buf)
273 'kill-buffer-if-not-modified))))
274
275 ;;;###autoload
276 (defun view-file-other-frame (file)
277 "View FILE in View mode in another frame.
278 When done, kill the buffer visiting FILE if unmodified and if it wasn't
279 visited before; also, maybe delete other frame and/or return to previous
280 buffer.
281
282 Emacs commands editing the buffer contents are not available; instead,
283 a special set of commands (mostly letters and punctuation)
284 are defined for moving around in the buffer.
285 Space scrolls forward, Delete scrolls backward.
286 For a list of all View commands, type H or h while viewing.
287
288 This command runs the normal hook `view-mode-hook'."
289 (interactive "fIn other frame view file: ")
290 (unless (file-exists-p file) (error "%s does not exist" file))
291 (let ((had-a-buf (get-file-buffer file))
292 (buf-to-view (find-file-noselect file)))
293 (view-buffer-other-frame buf-to-view nil
294 (and (not had-a-buf)
295 'kill-buffer-if-not-modified))))
296
297
298 ;;;###autoload
299 (defun view-buffer (buffer &optional exit-action)
300 "View BUFFER in View mode, returning to previous buffer when done.
301 Emacs commands editing the buffer contents are not available; instead, a
302 special set of commands (mostly letters and punctuation) are defined for
303 moving around in the buffer.
304 Space scrolls forward, Delete scrolls backward.
305 For a list of all View commands, type H or h while viewing.
306
307 This command runs the normal hook `view-mode-hook'.
308
309 Optional argument EXIT-ACTION is either nil or a function with buffer as
310 argument. This function is called when finished viewing buffer. Use
311 this argument instead of explicitly setting `view-exit-action'.
312
313 Do not set EXIT-ACTION to `kill-buffer' when BUFFER visits a
314 file: Users may suspend viewing in order to modify the buffer.
315 Exiting View mode will then discard the user's edits. Setting
316 EXIT-ACTION to `kill-buffer-if-not-modified' avoids this."
317 (interactive "bView buffer: ")
318 (if (eq (with-current-buffer buffer
319 (get major-mode 'mode-class))
320 'special)
321 (progn
322 (switch-to-buffer buffer)
323 (message "Not using View mode because the major mode is special"))
324 (let ((undo-window (list (window-buffer) (window-start) (window-point))))
325 (switch-to-buffer buffer)
326 (view-mode-enter (cons (selected-window) (cons nil undo-window))
327 exit-action))))
328
329 ;;;###autoload
330 (defun view-buffer-other-window (buffer &optional not-return exit-action)
331 "View BUFFER in View mode in another window.
332 Return to previous buffer when done, unless optional NOT-RETURN is
333 non-nil. Emacs commands editing the buffer contents are not available;
334 instead, a special set of commands (mostly letters and punctuation) are
335 defined for moving around in the buffer.
336 Space scrolls forward, Delete scrolls backward.
337 For a list of all View commands, type H or h while viewing.
338
339 This command runs the normal hook `view-mode-hook'.
340
341 Optional argument EXIT-ACTION is either nil or a function with buffer as
342 argument. This function is called when finished viewing buffer. Use
343 this argument instead of explicitly setting `view-exit-action'."
344 (interactive "bIn other window view buffer:\nP")
345 (let* ((win ; This window will be selected by
346 (get-lru-window)) ; switch-to-buffer-other-window below.
347 (return-to
348 (and (not not-return)
349 (cons (selected-window)
350 (if (eq win (selected-window))
351 t ; Has to make new window.
352 (list
353 (window-buffer win) ; Other windows old buffer.
354 (window-start win)
355 (window-point win)))))))
356 (switch-to-buffer-other-window buffer)
357 (view-mode-enter (and return-to (cons (selected-window) return-to))
358 exit-action)))
359
360 ;;;###autoload
361 (defun view-buffer-other-frame (buffer &optional not-return exit-action)
362 "View BUFFER in View mode in another frame.
363 Return to previous buffer when done, unless optional NOT-RETURN is
364 non-nil. Emacs commands editing the buffer contents are not available;
365 instead, a special set of commands (mostly letters and punctuation) are
366 defined for moving around in the buffer.
367 Space scrolls forward, Delete scrolls backward.
368 For a list of all View commands, type H or h while viewing.
369
370 This command runs the normal hook `view-mode-hook'.
371
372 Optional argument EXIT-ACTION is either nil or a function with buffer as
373 argument. This function is called when finished viewing buffer. Use
374 this argument instead of explicitly setting `view-exit-action'."
375 (interactive "bView buffer in other frame: \nP")
376 (let ((return-to
377 (and (not not-return) (cons (selected-window) t)))) ; Old window.
378 (switch-to-buffer-other-frame buffer)
379 (view-mode-enter (and return-to (cons (selected-window) return-to))
380 exit-action)))
381 \f
382 ;;;###autoload
383 (define-minor-mode view-mode
384 ;; In the following documentation string we have to use some explicit key
385 ;; bindings instead of using the \\[] construction. The reason for this
386 ;; is that most commands have more than one key binding.
387 "Toggle View mode, a minor mode for viewing text but not editing it.
388 With prefix argument ARG, turn View mode on if ARG is positive, otherwise
389 turn it off.
390
391 Emacs commands that do not change the buffer contents are available as usual.
392 Kill commands insert text in kill buffers but do not delete. Other commands
393 \(among them most letters and punctuation) beep and tell that the buffer is
394 read-only.
395 \\<view-mode-map>
396 The following additional commands are provided. Most commands take prefix
397 arguments. Page commands default to \"page size\" lines which is almost a whole
398 window full, or number of lines set by \\[View-scroll-page-forward-set-page-size] or \\[View-scroll-page-backward-set-page-size]. Half page commands default to
399 and set \"half page size\" lines which initially is half a window full. Search
400 commands default to a repeat count of one.
401
402 H, h, ? This message.
403 Digits provide prefix arguments.
404 \\[negative-argument] negative prefix argument.
405 \\[beginning-of-buffer] move to the beginning of buffer.
406 > move to the end of buffer.
407 \\[View-scroll-to-buffer-end] scroll so that buffer end is at last line of window.
408 SPC scroll forward \"page size\" lines.
409 With prefix scroll forward prefix lines.
410 DEL scroll backward \"page size\" lines.
411 With prefix scroll backward prefix lines.
412 \\[View-scroll-page-forward-set-page-size] like \\[View-scroll-page-forward] but with prefix sets \"page size\" to prefix.
413 \\[View-scroll-page-backward-set-page-size] like \\[View-scroll-page-backward] but with prefix sets \"page size\" to prefix.
414 \\[View-scroll-half-page-forward] scroll forward \"half page size\" lines. With prefix, sets
415 \"half page size\" to prefix lines and scrolls forward that much.
416 \\[View-scroll-half-page-backward] scroll backward \"half page size\" lines. With prefix, sets
417 \"half page size\" to prefix lines and scrolls backward that much.
418 RET, LFD scroll forward one line. With prefix scroll forward prefix line(s).
419 y scroll backward one line. With prefix scroll backward prefix line(s).
420 \\[View-revert-buffer-scroll-page-forward] revert-buffer if necessary and scroll forward.
421 Use this to view a changing file.
422 \\[what-line] prints the current line number.
423 \\[View-goto-percent] goes prefix argument (default 100) percent into buffer.
424 \\[View-goto-line] goes to line given by prefix argument (default first line).
425 . set the mark.
426 x exchanges point and mark.
427 \\[View-back-to-mark] return to mark and pops mark ring.
428 Mark ring is pushed at start of every successful search and when
429 jump to line occurs. The mark is set on jump to buffer start or end.
430 \\[point-to-register] save current position in character register.
431 ' go to position saved in character register.
432 s do forward incremental search.
433 r do reverse incremental search.
434 \\[View-search-regexp-forward] searches forward for regular expression, starting after current page.
435 ! and @ have a special meaning at the beginning of the regexp.
436 ! means search for a line with no match for regexp. @ means start
437 search at beginning (end for backward search) of buffer.
438 \\ searches backward for regular expression, starting before current page.
439 \\[View-search-last-regexp-forward] searches forward for last regular expression.
440 p searches backward for last regular expression.
441 \\[View-quit] quit View mode, restoring this window and buffer to previous state.
442 \\[View-quit] is the normal way to leave view mode.
443 \\[View-exit] exit View mode but stay in current buffer. Use this if you started
444 viewing a buffer (file) and find out you want to edit it.
445 This command restores the previous read-only status of the buffer.
446 \\[View-exit-and-edit] exit View mode, and make the current buffer editable
447 even if it was not editable before entry to View mode.
448 \\[View-quit-all] quit View mode, restoring all windows to previous state.
449 \\[View-leave] quit View mode and maybe switch buffers, but don't kill this buffer.
450 \\[View-kill-and-leave] quit View mode, kill current buffer and go back to other buffer.
451
452 The effect of \\[View-leave], \\[View-quit] and \\[View-kill-and-leave] depends on how view-mode was entered. If it was
453 entered by view-file, view-file-other-window, view-file-other-frame, or
454 \\[dired-view-file] \(\\[view-file], \\[view-file-other-window],
455 \\[view-file-other-frame], or the Dired mode v command),
456 then \\[View-quit] will try to kill the current buffer.
457 If view-mode was entered from another buffer, by \\[view-buffer],
458 \\[view-buffer-other-window], \\[view-buffer-other frame], \\[view-file],
459 \\[view-file-other-window], or \\[view-file-other-frame],
460 then \\[View-leave], \\[View-quit] and \\[View-kill-and-leave] will return to that buffer.
461
462 Entry to view-mode runs the normal hook `view-mode-hook'."
463 :lighter " View" :keymap view-mode-map
464 (if view-mode (view-mode-enable) (view-mode-disable)))
465 \f
466 (defun view-mode-enable ()
467 "Turn on View mode."
468 ;; Always leave view mode before changing major mode.
469 ;; This is to guarantee that the buffer-read-only variable is restored.
470 (add-hook 'change-major-mode-hook 'view-mode-disable nil t)
471 (setq view-mode t
472 view-page-size nil
473 view-half-page-size nil
474 view-old-buffer-read-only buffer-read-only
475 buffer-read-only t)
476 (if (boundp 'Helper-return-blurb)
477 (setq view-old-Helper-return-blurb (and (boundp 'Helper-return-blurb)
478 Helper-return-blurb)
479 Helper-return-blurb
480 (format "continue viewing %s"
481 (if (buffer-file-name)
482 (file-name-nondirectory (buffer-file-name))
483 (buffer-name)))))
484 (force-mode-line-update)
485 (run-hooks 'view-mode-hook))
486
487 (defun view-mode-disable ()
488 "Turn off View mode."
489 (remove-hook 'change-major-mode-hook 'view-mode-disable t)
490 (and view-overlay (delete-overlay view-overlay))
491 (force-mode-line-update)
492 ;; Calling toggle-read-only while View mode is enabled
493 ;; sets view-read-only to t as a buffer-local variable
494 ;; after exiting View mode. That arranges that the next toggle-read-only
495 ;; will reenable View mode.
496 ;; Cancelling View mode in any other way should cancel that, too,
497 ;; so that View mode stays off if toggle-read-only is called.
498 (if (local-variable-p 'view-read-only)
499 (kill-local-variable 'view-read-only))
500 (setq view-mode nil)
501 (if (boundp 'Helper-return-blurb)
502 (setq Helper-return-blurb view-old-Helper-return-blurb))
503 (if buffer-read-only
504 (setq buffer-read-only view-old-buffer-read-only)))
505
506 ;;;###autoload
507 (defun view-return-to-alist-update (buffer &optional item)
508 "Update `view-return-to-alist' of buffer BUFFER.
509 Remove from `view-return-to-alist' all entries referencing dead
510 windows. Optional argument ITEM non-nil means add ITEM to
511 `view-return-to-alist' after purging. For a decsription of items
512 that can be added see the RETURN-TO-ALIST argument of the
513 function `view-mode-exit'. If `view-return-to-alist' contains an
514 entry for the selected window, purge that entry from
515 `view-return-to-alist' before adding ITEM."
516 (with-current-buffer buffer
517 (when view-return-to-alist
518 (let* ((list view-return-to-alist)
519 entry entry-window last)
520 (while list
521 (setq entry (car list))
522 (setq entry-window (car entry))
523 (if (and (windowp entry-window)
524 (or (and item (eq entry-window (selected-window)))
525 (not (window-live-p entry-window))))
526 ;; Remove that entry.
527 (if last
528 (setcdr last (cdr list))
529 (setq view-return-to-alist
530 (cdr view-return-to-alist)))
531 ;; Leave entry alone.
532 (setq last entry))
533 (setq list (cdr list)))))
534 ;; Add ITEM.
535 (when item
536 (setq view-return-to-alist
537 (cons item view-return-to-alist)))))
538
539 ;;;###autoload
540 (defun view-mode-enter (&optional return-to exit-action)
541 "Enter View mode and set up exit from view mode depending on optional arguments.
542 RETURN-TO non-nil means add RETURN-TO as an element to the buffer
543 local alist `view-return-to-alist'. Save EXIT-ACTION in buffer
544 local variable `view-exit-action'. It should be either nil or a
545 function that takes a buffer as argument. This function will be
546 called by `view-mode-exit'.
547
548 RETURN-TO is either nil, meaning do nothing when exiting view
549 mode, or must have the format (WINDOW OLD-WINDOW . OLD-BUF-INFO).
550 WINDOW is the window used for viewing. OLD-WINDOW is nil or the
551 window to select after viewing. OLD-BUF-INFO tells what to do
552 with WINDOW when exiting. It is one of:
553 1) nil Do nothing.
554 2) t Delete WINDOW or, if it is the only window and
555 `view-remove-frame-by-deleting' is non-nil, its
556 frame.
557 3) (OLD-BUFF START POINT) Display buffer OLD-BUFF with displayed text
558 starting at START and point at POINT in WINDOW.
559 4) quit-window Do `quit-window' in WINDOW.
560 5) keep-frame Like case 2) but do not delete the frame.
561
562 For a list of all View commands, type H or h while viewing.
563
564 This function runs the normal hook `view-mode-hook'."
565 (when return-to
566 (let ((entry (assq (car return-to) view-return-to-alist)))
567 (if entry
568 (setcdr entry (cdr return-to))
569 (setq view-return-to-alist (cons return-to view-return-to-alist)))))
570 (when exit-action
571 (setq view-exit-action exit-action))
572
573 (unless view-mode
574 (view-mode-enable)
575 (force-mode-line-update)
576 (unless view-inhibit-help-message
577 (message "%s"
578 (substitute-command-keys "\
579 View mode: type \\[help-command] for help, \\[describe-mode] for commands, \\[View-quit] to quit.")))))
580 \f
581 (defun view-mode-exit (&optional return-to-alist exit-action all-win)
582 "Exit View mode in various ways, depending on optional arguments.
583 RETURN-TO-ALIST, EXIT-ACTION and ALL-WIN determine what to do
584 after exit. EXIT-ACTION is nil or a function that is called with
585 current buffer as argument.
586
587 RETURN-TO-ALIST is an alist that, for some of the windows
588 displaying the current buffer, maintains information on what to
589 do when exiting those windows. If ALL-WIN is non-nil or the
590 variable `view-exits-all-viewing-windows' is non-nil,
591 view-mode-exit attempts to restore all windows showing the
592 current buffer to their old state. Otherwise, only the selected
593 window is affected (provided it is on RETURN-TO-ALIST).
594
595 Elements of RETURN-TO-ALIST must have the format
596 (WINDOW OLD-WINDOW . OLD-BUF-INFO) where
597
598 WINDOW is a window displaying the current buffer and OLD-WINDOW
599 is either nil or a window to select after viewing. OLD-BUF-INFO
600 provides information on what to do with WINDOW and may be one of:
601 1) nil Do nothing.
602 2) t Delete WINDOW and, if it is the only window and
603 `view-remove-frame-by-deleting' is non-nil, its
604 frame.
605 3) (OLD-BUF START POINT) Display buffer OLD-BUF with displayed text
606 starting at START and point at POINT in WINDOW.
607 4) quit-window Do `quit-window' in WINDOW.
608 5) keep-frame Like case 2) but do not delete the frame.
609
610 If one of the WINDOW in RETURN-TO-ALIST is the selected window
611 and the corresponding OLD-WINDOW is a live window, then select
612 OLD-WINDOW."
613 (when view-mode ; Only do something if in view mode.
614 (setq all-win
615 (and return-to-alist
616 (or all-win view-exits-all-viewing-windows)))
617 (let* ((buffer (current-buffer))
618 window notlost
619 (sel-old (assq (selected-window) return-to-alist))
620 (alist (cond
621 (all-win ; Try to restore all windows.
622 (append return-to-alist nil)) ; Copy.
623 (sel-old ; Only selected window.
624 (list sel-old))))
625 (old-window (if sel-old (car (cdr sel-old)))))
626 (if all-win ; Follow chains of old-windows.
627 (let ((c (length alist)) a)
628 (while (and (> c 0) ; Safety if mutually refering windows.
629 (or (not (window-live-p old-window))
630 (eq buffer (window-buffer old-window)))
631 (setq a (assq old-window alist)))
632 (setq c (1- c))
633 (setq old-window (car (cdr a))))
634 (if (or (zerop c) (not (window-live-p old-window)))
635 (setq old-window (selected-window)))))
636 (unless view-no-disable-on-exit
637 (view-mode-disable))
638 (while alist ; Restore windows with info.
639 (setq notlost nil)
640 (when (and (window-live-p (setq window (car (car alist))))
641 (eq buffer (window-buffer window)))
642 (let ((frame (window-frame window))
643 (old-buf-info (cdr (cdr (car alist)))))
644 (if all-win (select-window window))
645 (cond
646 ((consp old-buf-info) ; Case 3.
647 (if (buffer-live-p (car old-buf-info))
648 (progn
649 (set-window-buffer window (car old-buf-info)) ; old-buf
650 (set-window-start window (car (cdr old-buf-info)))
651 (set-window-point window (car (cdr (cdr old-buf-info)))))
652 (bury-buffer)))
653 ((eq old-buf-info 'quit-window)
654 (quit-window)) ; Case 4.
655 (old-buf-info ; Case 2 or 5.
656 (cond
657 ((not (one-window-p t)) ; Not only window.
658 (delete-window))
659 ((eq old-buf-info 'keep-frame) ; Case 5.
660 (bury-buffer))
661 ((not (eq frame (next-frame))) ; Case 2 and only window.
662 ;; Not the only frame, so can safely be removed.
663 (if view-remove-frame-by-deleting
664 (delete-frame frame)
665 (setq notlost t) ; Keep the window. See below.
666 (iconify-frame frame))))))))
667 ;; If a frame is removed by iconifying it, the window is not
668 ;; really lost. In this case we keep the entry in
669 ;; `view-return-to-alist' so that if the user deiconifies the
670 ;; frame and then hits q, the frame is iconified again.
671 (unless notlost
672 (with-current-buffer buffer
673 (setq view-return-to-alist
674 (delete (car alist) view-return-to-alist))))
675 (setq alist (cdr alist)))
676 (when (window-live-p old-window)
677 ;; old-window is still alive => select it.
678 (select-window old-window))
679 (when exit-action
680 ;; Don't do that: If the user wants to quit the *Help* buffer a
681 ;; second time it won't have any effect.
682 ;;(setq view-exit-action nil)
683 (funcall exit-action buffer))
684 (force-mode-line-update))))
685 \f
686 (defun View-exit ()
687 "Exit View mode but stay in current buffer."
688 (interactive)
689 (view-mode-exit))
690
691 ;;;###autoload
692 (defun View-exit-and-edit ()
693 "Exit View mode and make the current buffer editable."
694 (interactive)
695 (let ((view-old-buffer-read-only nil)
696 (view-no-disable-on-exit nil))
697 (view-mode-exit)))
698
699 (defun View-leave ()
700 "Quit View mode and maybe switch buffers, but don't kill this buffer."
701 (interactive)
702 (view-mode-exit view-return-to-alist))
703
704 (defun View-quit ()
705 "Quit View mode, trying to restore window and buffer to previous state.
706 Maybe kill this buffer. Try to restore selected window to previous state
707 and go to previous buffer or window."
708 (interactive)
709 (view-mode-exit view-return-to-alist view-exit-action))
710
711 (defun View-quit-all ()
712 "Quit View mode, trying to restore windows and buffers to previous state.
713 Maybe kill current buffer. Try to restore all windows viewing buffer to
714 previous state and go to previous buffer or window."
715 (interactive)
716 (view-mode-exit view-return-to-alist view-exit-action t))
717
718 (defun View-kill-and-leave ()
719 "Quit View mode, kill current buffer and return to previous buffer."
720 (interactive)
721 (view-mode-exit view-return-to-alist (or view-exit-action 'kill-buffer) t))
722 \f
723
724 ;;; Some help routines.
725
726 (defun view-window-size ()
727 ;; Return the height of the current window, excluding the mode line.
728 ;; Using `window-line-height' accounts for variable-height fonts.
729 (let ((h (window-line-height -1)))
730 (if h
731 (1+ (nth 1 h))
732 ;; This should not happen, but if `window-line-height' returns
733 ;; nil, fall back on `window-height'.
734 (1- (window-height)))))
735
736 ;; (defun view-last-command (&optional who what)
737 ;; (setq view-last-command-entry this-command)
738 ;; (setq view-last-command who)
739 ;; (setq view-last-command-argument what))
740
741 ;; (defun View-repeat-last-command ()
742 ;; "Repeat last command issued in View mode."
743 ;; (interactive)
744 ;; (if (and view-last-command
745 ;; (eq view-last-command-entry last-command))
746 ;; (funcall view-last-command view-last-command-argument))
747 ;; (setq this-command view-last-command-entry))
748
749 (defun view-recenter ()
750 ;; Recenter point in window and redisplay normally.
751 (recenter '(1)))
752
753 (defun view-page-size-default (lines)
754 ;; If LINES is nil, 0, or larger than `view-window-size', return nil.
755 ;; Otherwise, return LINES.
756 (and lines
757 (not (zerop (setq lines (prefix-numeric-value lines))))
758 (<= (abs lines)
759 (abs (- (view-window-size) next-screen-context-lines)))
760 (abs lines)))
761
762 (defun view-set-half-page-size-default (lines)
763 ;; Get and maybe set half page size.
764 (if (not lines) (or view-half-page-size
765 (/ (view-window-size) 2))
766 (setq view-half-page-size
767 (if (zerop (setq lines (prefix-numeric-value lines)))
768 (/ (view-window-size) 2)
769 (view-page-size-default lines)))))
770
771
772 ;;; Commands for moving around in the buffer.
773
774 (defun View-goto-percent (&optional percent)
775 "Move to end (or prefix PERCENT) of buffer in View mode.
776 Display is centered at point.
777 Also set the mark at the position where point was."
778 (interactive "P")
779 (push-mark)
780 (goto-char
781 (if percent
782 (+ (point-min)
783 (floor (* (- (point-max) (point-min)) 0.01
784 (max 0 (min 100 (prefix-numeric-value percent))))))
785 (point-max)))
786 (view-recenter))
787
788 ;; (defun View-goto-line-last (&optional line)
789 ;; "Move to last (or prefix LINE) line in View mode.
790 ;; Display is centered at LINE.
791 ;; Sets mark at starting position and pushes mark ring."
792 ;; (interactive "P")
793 ;; (push-mark)
794 ;; (if line (goto-line (prefix-numeric-value line))
795 ;; (goto-char (point-max))
796 ;; (beginning-of-line))
797 ;; (view-recenter))
798
799 (defun View-goto-line (&optional line)
800 "Move to first (or prefix LINE) line in View mode.
801 Display is centered at LINE.
802 Also set the mark at the position where point was."
803 (interactive "p")
804 (push-mark)
805 (goto-char (point-min))
806 (forward-line (1- line))
807 (view-recenter))
808
809 (defun View-back-to-mark (&optional ignore)
810 "Return to last mark set in View mode, else beginning of file.
811 Display that line at the center of the window.
812 This command pops the mark ring, so that successive
813 invocations return to earlier marks."
814 (interactive)
815 (goto-char (or (mark t) (point-min)))
816 (pop-mark)
817 (view-recenter))
818 \f
819 (defun view-scroll-lines (lines backward default maxdefault)
820 ;; This function does the job for all the scrolling commands.
821 ;; Scroll forward LINES lines. If BACKWARD is non-nil, scroll backwards.
822 ;; If LINES is negative scroll in the other direction.
823 ;; If LINES is 0 or nil, scroll DEFAULT lines (if DEFAULT is nil, scroll
824 ;; by one page). If MAXDEFAULT is non-nil, scroll no more than a window.
825 (if (or (null lines) (zerop (setq lines (prefix-numeric-value lines))))
826 (setq lines default))
827 (when (and lines (< lines 0))
828 (setq backward (not backward) lines (- lines)))
829 (when (and maxdefault lines (> lines (view-window-size)))
830 (setq lines nil))
831 (cond (backward (scroll-down lines))
832 ((view-really-at-end)
833 (if view-scroll-auto-exit
834 (View-quit)
835 (ding)
836 (view-end-message)))
837 (t (scroll-up lines)
838 (if (view-really-at-end) (view-end-message)))))
839
840 (defun view-really-at-end ()
841 ;; Return true if buffer end visible. Maybe revert buffer and test.
842 (and (pos-visible-in-window-p (point-max))
843 (let ((buf (current-buffer))
844 (bufname (buffer-name))
845 (file (buffer-file-name)))
846 (or (not view-try-extend-at-buffer-end)
847 (null file)
848 (verify-visited-file-modtime buf)
849 (not (file-exists-p file))
850 (when (buffer-modified-p buf)
851 (setq file (file-name-nondirectory file))
852 (not (yes-or-no-p
853 (format
854 "File %s changed on disk. Discard your edits%s? "
855 file
856 (if (string= bufname file) ""
857 (concat " in " bufname))))))
858 (progn
859 (revert-buffer t t t)
860 (pos-visible-in-window-p (point-max)))))))
861
862 (defun view-end-message ()
863 ;; Tell that we are at end of buffer.
864 (goto-char (point-max))
865 (if view-return-to-alist
866 (message "End of buffer. Type %s to quit viewing."
867 (substitute-command-keys
868 (if view-scroll-auto-exit "\\[View-scroll-page-forward]"
869 "\\[View-quit]")))
870 (message "End of buffer")))
871 \f
872 (defun View-scroll-to-buffer-end ()
873 "Scroll backward or forward so that buffer end is at last line of window."
874 (interactive)
875 (let ((p (if (pos-visible-in-window-p (point-max)) (point))))
876 (goto-char (point-max))
877 (recenter -1)
878 (and p (goto-char p))))
879
880 (defun View-scroll-page-forward (&optional lines)
881 "Scroll \"page size\" or prefix LINES lines forward in View mode.
882 Exit if end of text is visible and `view-scroll-auto-exit' is non-nil.
883 \"page size\" is whole window full, or number of lines set by
884 \\[View-scroll-page-forward-set-page-size] or
885 \\[View-scroll-page-backward-set-page-size].
886 If LINES is more than a window-full, only the last window-full is shown."
887 (interactive "P")
888 (view-scroll-lines lines nil (view-page-size-default view-page-size) nil))
889
890 (defun View-scroll-page-backward (&optional lines)
891 "Scroll \"page size\" or prefix LINES lines backward in View mode.
892 See also `View-scroll-page-forward'."
893 (interactive "P")
894 (view-scroll-lines lines t (view-page-size-default view-page-size) nil))
895
896 (defun View-scroll-page-forward-set-page-size (&optional lines)
897 "Scroll forward LINES lines in View mode, setting the \"page size\".
898 This is the number of lines which \\[View-scroll-page-forward] and
899 \\[View-scroll-page-backward] scroll by default.
900 If LINES is omitted or = 0, sets \"page size\" to window height and
901 scrolls forward that much, otherwise scrolls forward LINES lines and sets
902 \"page size\" to the minimum of window height and the absolute value of LINES.
903 See also `View-scroll-page-forward'."
904 (interactive "P")
905 (view-scroll-lines lines nil
906 (setq view-page-size (view-page-size-default lines))
907 nil))
908
909 (defun View-scroll-page-backward-set-page-size (&optional lines)
910 "Scroll backward prefix LINES lines in View mode, setting the \"page size\".
911 See also `View-scroll-page-forward-set-page-size'."
912 (interactive "P")
913 (view-scroll-lines lines t
914 (setq view-page-size (view-page-size-default lines))
915 nil))
916
917 (defun View-scroll-line-forward (&optional lines)
918 "Scroll forward one line (or prefix LINES lines) in View mode.
919 See also `View-scroll-page-forward', but note that scrolling is limited
920 to minimum of LINES and one window-full."
921 (interactive "P")
922 (view-scroll-lines lines nil 1 t))
923
924 (defun View-scroll-line-backward (&optional lines)
925 "Scroll backward one line (or prefix LINES lines) in View mode.
926 See also `View-scroll-line-forward'."
927 (interactive "P")
928 (view-scroll-lines lines t 1 t))
929
930 (defun View-scroll-half-page-forward (&optional lines)
931 "Scroll forward a \"half page\" (or prefix LINES) lines in View mode.
932 If LINES is not omitted, the \"half page size\" is set to the minimum of
933 window height and the absolute value of LINES.
934 LINES=0 resets \"half page size\" to half window height."
935 (interactive "P")
936 (view-scroll-lines lines nil (view-set-half-page-size-default lines) t))
937
938 (defun View-scroll-half-page-backward (&optional lines)
939 "Scroll backward a \"half page\" (or prefix LINES) lines in View mode.
940 See also `View-scroll-half-page-forward'."
941 (interactive "P")
942 (view-scroll-lines lines t (view-set-half-page-size-default lines) t))
943
944 (defun View-revert-buffer-scroll-page-forward (&optional lines)
945 "Scroll forward, reverting buffer if needed, in View mode.
946 If buffer has not been changed and the corresponding file is newer, first
947 revert the buffer, then scroll.
948 This command is useful if you are viewing a changing file.
949
950 The prefix argument LINES says how many lines to scroll.
951 If you don't specify a prefix argument, it uses the number of lines set by
952 \\[View-scroll-page-forward-set-page-size] or
953 \\[View-scroll-page-backward-set-page-size].
954 If LINES is more than a window-full, only the last window-full is shown."
955 (interactive "P")
956 (let ((view-scroll-auto-exit nil)
957 (view-try-extend-at-buffer-end t))
958 (view-scroll-lines lines nil (view-page-size-default view-page-size) nil)))
959 \f
960 (defun View-search-regexp-forward (n regexp)
961 "Search forward for first (or prefix Nth) occurrence of REGEXP in View mode.
962
963 Displays line found at center of window. Sets mark at starting position and
964 pushes mark ring.
965
966 Characters @ and ! are special at the beginning of REGEXP. They modify
967 the search rather than become part of the pattern searched for.
968 @ means search all the buffer i.e. start search at the beginning of buffer.
969 ! means search for a line that contains no match for the pattern.
970 If REGEXP is empty or only consist of these control characters, then
971 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
972 for use by later search commands.
973
974 The variable `view-highlight-face' controls the face that is used
975 for highlighting the match that is found."
976 (interactive "p\nsSearch forward (regexp): ")
977 (view-search n regexp))
978
979 (defun View-search-regexp-backward (n regexp)
980 "Search backward for first (or prefix Nth) occurrence of REGEXP in View mode.
981
982 Displays line found at center of window. Sets mark at starting position and
983 pushes mark ring.
984
985 Characters @ and ! are special at the beginning of REGEXP. They modify
986 the search rather than become part of the pattern searched for.
987 @ means search all the buffer i.e. start search at the end of buffer.
988 ! means search for a line that contains no match for the pattern.
989 If REGEXP is empty or only consist of these control characters, then
990 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
991 for use by later search commands.
992
993 The variable `view-highlight-face' controls the face that is used
994 for highlighting the match that is found."
995 (interactive "p\nsSearch backward (regexp): ")
996 (view-search (- n) regexp))
997
998 (defun View-search-last-regexp-forward (n) "\
999 Search forward for first (or prefix Nth) instance of last regexp in View mode.
1000 Displays line found at center of window. Sets mark at starting position and
1001 pushes mark ring.
1002
1003 The variable `view-highlight-face' controls the face that is used
1004 for highlighting the match that is found."
1005 (interactive "p")
1006 (view-search n nil))
1007
1008 (defun View-search-last-regexp-backward (n) "\
1009 Search backward for first (or prefix Nth) instance of last regexp in View mode.
1010 Displays line found at center of window. Sets mark at starting position and
1011 pushes mark ring.
1012
1013 The variable `view-highlight-face' controls the face that is used
1014 for highlighting the match that is found."
1015 (interactive "p")
1016 (view-search (- n) nil))
1017
1018 (defun view-search (times regexp)
1019 ;; This function does the job for all the View-search- commands.
1020 ;; Search for the TIMESt match for REGEXP. If TIMES is negative
1021 ;; search backwards. If REGEXP is nil use `view-last-regexp'.
1022 ;; Characters "!" and "@" have a special meaning at the beginning of
1023 ;; REGEXP and are removed from REGEXP before the search "!" means
1024 ;; search for lines with no match for REGEXP. "@" means search in
1025 ;; the whole buffer, don't start searching from the present point.
1026 (let (where no end ln)
1027 (cond
1028 ((and regexp (> (length regexp) 0)
1029 (or (not (memq (string-to-char regexp) '(?! ?@)))
1030 (progn
1031 (if (member (substring regexp 0 2) '("!@" "@!"))
1032 (setq end t no t ln 2)
1033 (setq no (not (setq end (eq ?@ (string-to-char regexp))))
1034 ln 1))
1035 (> (length (setq regexp (substring regexp ln))) 0))))
1036 (setq view-last-regexp (if no (list regexp) regexp)))
1037 ((consp view-last-regexp)
1038 (setq regexp (car view-last-regexp))
1039 (unless (setq no (not no)) (setq view-last-regexp regexp)))
1040 (view-last-regexp (setq regexp view-last-regexp)
1041 (if no (setq view-last-regexp (list regexp))))
1042 (t (error "No previous View-mode search")))
1043 (save-excursion
1044 (if end (goto-char (if (< times 0) (point-max) (point-min)))
1045 (move-to-window-line (if (< times 0) 0 -1)))
1046 (if (if no (view-search-no-match-lines times regexp)
1047 (re-search-forward regexp nil t times))
1048 (setq where (point))))
1049 (if where
1050 (progn
1051 (push-mark)
1052 (goto-char where)
1053 (if view-overlay
1054 (move-overlay view-overlay (match-beginning 0) (match-end 0))
1055 (setq view-overlay
1056 (make-overlay (match-beginning 0) (match-end 0))))
1057 (overlay-put view-overlay 'face view-highlight-face)
1058 (beginning-of-line)
1059 (view-recenter))
1060 (message "Can't find occurrence %d of %s%s"
1061 times (if no "no " "") regexp)
1062 (sit-for 4))))
1063
1064 ;; This is the dumb approach, looking at each line. The original
1065 ;; version of this function looked like it might have been trying to
1066 ;; do something clever, but not succeeding:
1067 ;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2007-09/msg00073.html
1068 (defun view-search-no-match-lines (times regexp)
1069 "Search for the TIMESth occurrence of a line with no match for REGEXP.
1070 If such a line is found, return non-nil and set the match-data to that line.
1071 If TIMES is negative, search backwards."
1072 (let ((step (if (>= times 0) 1
1073 (setq times (- times))
1074 -1)))
1075 ;; Note that we do not check the current line.
1076 (while (and (> times 0)
1077 (zerop (forward-line step)))
1078 ;; (forward-line 1) returns 0 on moving within the last line.
1079 (if (eobp)
1080 (setq times -1)
1081 (or (re-search-forward regexp (line-end-position) t)
1082 (setq times (1- times))))))
1083 (and (zerop times)
1084 (looking-at ".*")))
1085
1086 (provide 'view)
1087
1088 ;;; view.el ends here