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