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