*** empty log message ***
[emacs.git] / lisp / view.el
blob2ef84d2351071838a92ebb95a555f6bcc9aae264
1 ;;; view.el --- peruse file or buffer without editing
3 ;; Copyright (C) 1985, 1989, 1994, 1995, 1997, 2000, 2001
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 2, 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., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, 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 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 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 (defcustom view-remove-frame-by-deleting nil
81 "*Determine how View mode removes a frame no longer needed.
82 If nil, make an icon of the frame. If non-nil, delete the frame."
83 :type 'boolean
84 :group 'view)
86 (defcustom view-exits-all-viewing-windows nil
87 "*Non-nil means restore all windows used to view buffer.
88 Commands that restore windows when finished viewing a buffer, apply to all
89 windows that display the buffer and have restore information in
90 `view-return-to-alist'.
91 If `view-exits-all-viewing-windows' is nil, only the selected window is
92 considered for restoring."
93 :type 'boolean
94 :group 'view)
96 ;;;###autoload
97 (defvar view-mode nil
98 "Non-nil if View mode is enabled.
99 Don't change this variable directly, you must change it by one of the
100 functions that enable or disable view mode.")
101 ;;;###autoload
102 (make-variable-buffer-local 'view-mode)
104 (defcustom view-mode-hook nil
105 "Normal hook run when starting to view a buffer or file."
106 :type 'hook
107 :group 'view)
109 (defvar view-old-buffer-read-only nil)
110 (make-variable-buffer-local 'view-old-buffer-read-only)
112 (defvar view-old-Helper-return-blurb)
113 (make-variable-buffer-local 'view-old-Helper-return-blurb)
115 ;; Just to avoid warnings.
116 (defvar Helper-return-blurb)
118 (defvar view-page-size nil
119 "Default number of lines to scroll by View page commands.
120 If nil then the local value of this is initially set to window size.")
121 (make-variable-buffer-local 'view-page-size)
123 (defvar view-half-page-size nil
124 "Default number of lines to scroll by View half page commands.
125 If nil then the local value of this is initially set to half window size.")
126 (make-variable-buffer-local 'view-half-page-size)
128 (defvar view-last-regexp nil)
129 (make-variable-buffer-local 'view-last-regexp) ; Global is better???
131 (defvar view-return-to-alist nil
132 "What to do with used windows and where to go when finished viewing buffer.
133 This is local in each buffer being viewed.
134 It is added to by `view-mode-enter' when starting to view a buffer and
135 subtracted from by `view-mode-exit' when finished viewing the buffer.
137 See RETURN-TO-ALIST argument of function `view-mode-exit' for the format of
138 `view-return-to-alist'.")
139 (make-variable-buffer-local 'view-return-to-alist)
141 (defvar view-exit-action nil
142 "nil or a function with one argument (a buffer) called when finished viewing.
143 This is local in each buffer being viewed.
144 The \\[view-file] and \\[view-file-other-window] commands may set this to
145 `kill-buffer'.")
146 (make-variable-buffer-local 'view-exit-action)
148 (defvar view-no-disable-on-exit nil
149 "If non-nil, View mode \"exit\" commands don't actually disable View mode.
150 Instead, these commands just switch buffers or windows.
151 This is set in certain buffers by specialized features such as help commands
152 that use View mode automatically.")
154 (defvar view-overlay nil
155 "Overlay used to display where a search operation found its match.
156 This is local in each buffer, once it is used.")
157 (make-variable-buffer-local 'view-overlay)
159 (unless (assq 'view-mode minor-mode-alist)
160 (setq minor-mode-alist
161 (cons (list 'view-mode
162 (propertize " View"
163 'local-map mode-line-minor-mode-keymap
164 'help-echo "mouse-3: minor mode menu"))
165 minor-mode-alist)))
167 ;; Define keymap inside defvar to make it easier to load changes.
168 ;; Some redundant "less"-like key bindings below have been commented out.
169 (defvar view-mode-map
170 (let ((map (make-sparse-keymap)))
171 (define-key map "C" 'View-kill-and-leave)
172 (define-key map "c" 'View-leave)
173 (define-key map "Q" 'View-quit-all)
174 (define-key map "E" 'View-exit-and-edit)
175 ; (define-key map "v" 'View-exit)
176 (define-key map "e" 'View-exit)
177 (define-key map "q" 'View-quit)
178 ; (define-key map "N" 'View-search-last-regexp-backward)
179 (define-key map "p" 'View-search-last-regexp-backward)
180 (define-key map "n" 'View-search-last-regexp-forward)
181 ; (define-key map "?" 'View-search-regexp-backward) ; Less does this.
182 (define-key map "\\" 'View-search-regexp-backward)
183 (define-key map "/" 'View-search-regexp-forward)
184 (define-key map "r" 'isearch-backward)
185 (define-key map "s" 'isearch-forward)
186 (define-key map "m" 'point-to-register)
187 (define-key map "'" 'register-to-point)
188 (define-key map "x" 'exchange-point-and-mark)
189 (define-key map "@" 'View-back-to-mark)
190 (define-key map "." 'set-mark-command)
191 (define-key map "%" 'View-goto-percent)
192 ; (define-key map "G" 'View-goto-line-last)
193 (define-key map "g" 'View-goto-line)
194 (define-key map "=" 'what-line)
195 (define-key map "F" 'View-revert-buffer-scroll-page-forward)
196 ; (define-key map "k" 'View-scroll-line-backward)
197 (define-key map "y" 'View-scroll-line-backward)
198 ; (define-key map "j" 'View-scroll-line-forward)
199 (define-key map "\n" 'View-scroll-line-forward)
200 (define-key map "\r" 'View-scroll-line-forward)
201 (define-key map "u" 'View-scroll-half-page-backward)
202 (define-key map "d" 'View-scroll-half-page-forward)
203 (define-key map "z" 'View-scroll-page-forward-set-page-size)
204 (define-key map "w" 'View-scroll-page-backward-set-page-size)
205 ; (define-key map "b" 'View-scroll-page-backward)
206 (define-key map "\C-?" 'View-scroll-page-backward)
207 ; (define-key map "f" 'View-scroll-page-forward)
208 (define-key map " " 'View-scroll-page-forward)
209 (define-key map "o" 'View-scroll-to-buffer-end)
210 (define-key map ">" 'end-of-buffer)
211 (define-key map "<" 'beginning-of-buffer)
212 (define-key map "-" 'negative-argument)
213 (define-key map "9" 'digit-argument)
214 (define-key map "8" 'digit-argument)
215 (define-key map "7" 'digit-argument)
216 (define-key map "6" 'digit-argument)
217 (define-key map "5" 'digit-argument)
218 (define-key map "4" 'digit-argument)
219 (define-key map "3" 'digit-argument)
220 (define-key map "2" 'digit-argument)
221 (define-key map "1" 'digit-argument)
222 (define-key map "0" 'digit-argument)
223 (define-key map "H" 'describe-mode)
224 (define-key map "?" 'describe-mode) ; Maybe do as less instead? See above.
225 (define-key map "h" 'describe-mode)
226 map))
228 (or (assq 'view-mode minor-mode-map-alist)
229 (setq minor-mode-map-alist
230 (cons (cons 'view-mode view-mode-map) minor-mode-map-alist)))
232 ;;; Commands that enter or exit view mode.
234 ;;;###autoload
235 (defun view-file (file)
236 "View FILE in View mode, returning to previous buffer when done.
237 Emacs commands editing the buffer contents are not available; instead,
238 a special set of commands (mostly letters and punctuation)
239 are defined for moving around in the buffer.
240 Space scrolls forward, Delete scrolls backward.
241 For list of all View commands, type H or h while viewing.
243 This command runs the normal hook `view-mode-hook'."
244 (interactive "fView file: ")
245 (unless (file-exists-p file) (error "%s does not exist" file))
246 (let ((had-a-buf (get-file-buffer file)))
247 (view-buffer (find-file-noselect file)
248 (and (not had-a-buf) 'kill-buffer))))
250 ;;;###autoload
251 (defun view-file-other-window (file)
252 "View FILE in View mode in another window.
253 Return that window to its previous buffer when done.
254 Emacs commands editing the buffer contents are not available; instead,
255 a special set of commands (mostly letters and punctuation)
256 are defined for moving around in the buffer.
257 Space scrolls forward, Delete scrolls backward.
258 For list of all View commands, type H or h while viewing.
260 This command runs the normal hook `view-mode-hook'."
261 (interactive "fIn other window view file: ")
262 (unless (file-exists-p file) (error "%s does not exist" file))
263 (let ((had-a-buf (get-file-buffer file)))
264 (view-buffer-other-window (find-file-noselect file) nil
265 (and (not had-a-buf) 'kill-buffer))))
267 ;;;###autoload
268 (defun view-file-other-frame (file)
269 "View FILE in View mode in another frame.
270 Maybe delete other frame and/or return to previous buffer when done.
271 Emacs commands editing the buffer contents are not available; instead,
272 a special set of commands (mostly letters and punctuation)
273 are defined for moving around in the buffer.
274 Space scrolls forward, Delete scrolls backward.
275 For list of all View commands, type H or h while viewing.
277 This command runs the normal hook `view-mode-hook'."
278 (interactive "fIn other frame view file: ")
279 (unless (file-exists-p file) (error "%s does not exist" file))
280 (let ((had-a-buf (get-file-buffer file)))
281 (view-buffer-other-frame (find-file-noselect file) nil
282 (and (not had-a-buf) 'kill-buffer))))
285 ;;;###autoload
286 (defun view-buffer (buffer &optional exit-action)
287 "View BUFFER in View mode, returning to previous buffer when done.
288 Emacs commands editing the buffer contents are not available; instead,
289 a special set of commands (mostly letters and punctuation)
290 are defined for moving around in the buffer.
291 Space scrolls forward, Delete scrolls backward.
292 For list of all View commands, type H or h while viewing.
294 This command runs the normal hook `view-mode-hook'.
296 Optional argument EXIT-ACTION is either nil or a function with buffer as
297 argument. This function is called when finished viewing buffer.
298 Use this argument instead of explicitly setting `view-exit-action'."
300 (interactive "bView buffer: ")
301 (let ((undo-window (list (window-buffer) (window-start) (window-point))))
302 (switch-to-buffer buffer)
303 (view-mode-enter (cons (selected-window) (cons nil undo-window))
304 exit-action)))
306 ;;;###autoload
307 (defun view-buffer-other-window (buffer &optional not-return exit-action)
308 "View BUFFER in View mode in another window.
309 Return to previous buffer when done, unless optional NOT-RETURN is non-nil.
310 Emacs commands editing the buffer contents are not available; instead,
311 a special set of commands (mostly letters and punctuation)
312 are defined for moving around in the buffer.
313 Space scrolls forward, Delete scrolls backward.
314 For list of all View commands, type H or h while viewing.
316 This command runs the normal hook `view-mode-hook'.
318 Optional argument EXIT-ACTION is either nil or a function with buffer as
319 argument. This function is called when finished viewing buffer.
320 Use this argument instead of explicitly setting `view-exit-action'."
321 (interactive "bIn other window view buffer:\nP")
322 (let* ((win ; This window will be selected by
323 (get-lru-window)) ; switch-to-buffer-other-window below.
324 (return-to
325 (and (not not-return)
326 (cons (selected-window)
327 (if (eq win (selected-window))
328 t ; Has to make new window.
329 (list
330 (window-buffer win) ; Other windows old buffer.
331 (window-start win)
332 (window-point win)))))))
333 (switch-to-buffer-other-window buffer)
334 (view-mode-enter (and return-to (cons (selected-window) return-to))
335 exit-action)))
337 ;;;###autoload
338 (defun view-buffer-other-frame (buffer &optional not-return exit-action)
339 "View BUFFER in View mode in another frame.
340 Return to previous buffer when done, unless optional NOT-RETURN is non-nil.
341 Emacs commands editing the buffer contents are not available; instead,
342 a special set of commands (mostly letters and punctuation)
343 are defined for moving around in the buffer.
344 Space scrolls forward, Delete scrolls backward.
345 For list of all View commands, type H or h while viewing.
347 This command runs the normal hook `view-mode-hook'.
349 Optional argument EXIT-ACTION is either nil or a function with buffer as
350 argument. This function is called when finished viewing buffer.
351 Use this argument instead of explicitly setting `view-exit-action'."
352 (interactive "bView buffer in other frame: \nP")
353 (let ((return-to
354 (and (not not-return) (cons (selected-window) t)))) ; Old window.
355 (switch-to-buffer-other-frame buffer)
356 (view-mode-enter (and return-to (cons (selected-window) return-to))
357 exit-action)))
359 ;;;###autoload
360 (defun view-mode (&optional arg)
361 ;; In the following documentation string we have to use some explicit key
362 ;; bindings instead of using the \\[] construction. The reason for this
363 ;; is that most commands have more than one key binding.
364 "Toggle View mode, a minor mode for viewing text but not editing it.
365 With ARG, turn View mode on iff ARG is positive.
367 Emacs commands that do not change the buffer contents are available as usual.
368 Kill commands insert text in kill buffers but do not delete. Other commands
369 \(among them most letters and punctuation) beep and tell that the buffer is
370 read-only.
371 \\<view-mode-map>
372 The following additional commands are provided. Most commands take prefix
373 arguments. Page commands default to \"page size\" lines which is almost a whole
374 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
375 and set \"half page size\" lines which initially is half a window full. Search
376 commands default to a repeat count of one.
378 H, h, ? This message.
379 Digits provide prefix arguments.
380 \\[negative-argument] negative prefix argument.
381 \\[beginning-of-buffer] move to the beginning of buffer.
382 > move to the end of buffer.
383 \\[View-scroll-to-buffer-end] scroll so that buffer end is at last line of window.
384 SPC scroll forward \"page size\" lines.
385 With prefix scroll forward prefix lines.
386 DEL scroll backward \"page size\" lines.
387 With prefix scroll backward prefix lines.
388 \\[View-scroll-page-forward-set-page-size] like \\[View-scroll-page-forward] but with prefix sets \"page size\" to prefix.
389 \\[View-scroll-page-backward-set-page-size] like \\[View-scroll-page-backward] but with prefix sets \"page size\" to prefix.
390 \\[View-scroll-half-page-forward] scroll forward \"half page size\" lines. With prefix, sets
391 \"half page size\" to prefix lines and scrolls forward that much.
392 \\[View-scroll-half-page-backward] scroll backward \"half page size\" lines. With prefix, sets
393 \"half page size\" to prefix lines and scrolls backward that much.
394 RET, LFD scroll forward one line. With prefix scroll forward prefix line(s).
395 y scroll backward one line. With prefix scroll backward prefix line(s).
396 \\[View-revert-buffer-scroll-page-forward] revert-buffer if necessary and scroll forward.
397 Use this to view a changing file.
398 \\[what-line] prints the current line number.
399 \\[View-goto-percent] goes prefix argument (default 100) percent into buffer.
400 \\[View-goto-line] goes to line given by prefix argument (default first line).
401 . set the mark.
402 x exchanges point and mark.
403 \\[View-back-to-mark] return to mark and pops mark ring.
404 Mark ring is pushed at start of every successful search and when
405 jump to line occurs. The mark is set on jump to buffer start or end.
406 \\[point-to-register] save current position in character register.
407 ' go to position saved in character register.
408 s do forward incremental search.
409 r do reverse incremental search.
410 \\[View-search-regexp-forward] searches forward for regular expression, starting after current page.
411 ! and @ have a special meaning at the beginning of the regexp.
412 ! means search for a line with no match for regexp. @ means start
413 search at beginning (end for backward search) of buffer.
414 \\ searches backward for regular expression, starting before current page.
415 \\[View-search-last-regexp-forward] searches forward for last regular expression.
416 p searches backward for last regular expression.
417 \\[View-quit] quit View mode, restoring this window and buffer to previous state.
418 \\[View-quit] is the normal way to leave view mode.
419 \\[View-exit] exit View mode but stay in current buffer. Use this if you started
420 viewing a buffer (file) and find out you want to edit it.
421 This command restores the previous read-only status of the buffer.
422 \\[View-exit-and-edit] exit View mode, and make the current buffer editable
423 even if it was not editable before entry to View mode.
424 \\[View-quit-all] quit View mode, restoring all windows to previous state.
425 \\[View-leave] quit View mode and maybe switch buffers, but don't kill this buffer.
426 \\[View-kill-and-leave] quit View mode, kill current buffer and go back to other buffer.
428 The effect of \\[View-leave] , \\[View-quit] and \\[View-kill-and-leave] depends on how view-mode was entered. If it was
429 entered by view-file, view-file-other-window or view-file-other-frame
430 \(\\[view-file], \\[view-file-other-window], \\[view-file-other-frame] or the dired mode v command), then \\[View-quit] will
431 try to kill the current buffer. If view-mode was entered from another buffer
432 as is done by View-buffer, View-buffer-other-window, View-buffer-other frame,
433 View-file, View-file-other-window or View-file-other-frame then \\[View-leave] , \\[View-quit] and \\[View-kill-and-leave]
434 will return to that buffer.
436 Entry to view-mode runs the normal hook `view-mode-hook'."
437 (interactive "P")
438 (unless (and arg ; Do nothing if already OK.
439 (if (> (prefix-numeric-value arg) 0) view-mode (not view-mode)))
440 (if view-mode (view-mode-disable)
441 (view-mode-enable))))
443 (defun view-mode-enable ()
444 "Turn on View mode."
445 ;; Always leave view mode before changing major mode.
446 ;; This is to guarantee that the buffer-read-only variable is restored.
447 (add-hook 'change-major-mode-hook 'view-mode-disable nil t)
448 (setq view-mode t
449 view-page-size (view-page-size-default view-page-size)
450 view-half-page-size (or view-half-page-size (/ (view-window-size) 2))
451 view-old-buffer-read-only buffer-read-only
452 buffer-read-only t
453 view-old-Helper-return-blurb (and (boundp 'Helper-return-blurb)
454 Helper-return-blurb)
455 Helper-return-blurb
456 (format "continue viewing %s"
457 (if (buffer-file-name)
458 (file-name-nondirectory (buffer-file-name))
459 (buffer-name))))
460 (force-mode-line-update)
461 (run-hooks 'view-mode-hook))
463 (defun view-mode-disable ()
464 "Turn off View mode."
465 (remove-hook 'change-major-mode-hook 'view-mode-disable t)
466 (and view-overlay (delete-overlay view-overlay))
467 (force-mode-line-update)
468 ;; Calling toggle-read-only while View mode is enabled
469 ;; sets view-read-only to t as a buffer-local variable
470 ;; after exiting View mode. That arranges that the next toggle-read-only
471 ;; will reenable View mode.
472 ;; Cancelling View mode in any other way should cancel that, too,
473 ;; so that View mode stays off if toggle-read-only is called.
474 (if (local-variable-p 'view-read-only)
475 (kill-local-variable 'view-read-only))
476 (setq view-mode nil
477 Helper-return-blurb view-old-Helper-return-blurb)
478 (if buffer-read-only
479 (setq buffer-read-only view-old-buffer-read-only)))
481 ;;;###autoload
482 (defun view-mode-enter (&optional return-to exit-action) "\
483 Enter View mode and set up exit from view mode depending on optional arguments.
484 If RETURN-TO is non-nil it is added as an element to the buffer local alist
485 `view-return-to-alist'.
486 Save EXIT-ACTION in buffer local variable `view-exit-action'.
487 It should be either nil or a function that takes a buffer as argument.
488 This function will be called by `view-mode-exit'.
490 RETURN-TO is either nil, meaning do nothing when exiting view mode, or
491 it has the format (WINDOW OLD-WINDOW . OLD-BUF-INFO).
492 WINDOW is a window used for viewing.
493 OLD-WINDOW is nil or the window to select after viewing.
494 OLD-BUF-INFO tells what to do with WINDOW when exiting. It is one of:
495 1) nil Do nothing.
496 2) t Delete WINDOW or, if it is the only window, its frame.
497 3) (OLD-BUFF START POINT) Display buffer OLD-BUFF with displayed text
498 starting at START and point at POINT in WINDOW.
499 4) quit-window Do `quit-window' in WINDOW.
501 For list of all View commands, type H or h while viewing.
503 This function runs the normal hook `view-mode-hook'."
504 (if return-to
505 (let ((entry (assq (car return-to) view-return-to-alist)))
506 (if entry (setcdr entry (cdr return-to))
507 (setq view-return-to-alist (cons return-to view-return-to-alist)))))
508 (if exit-action (setq view-exit-action exit-action))
509 (unless view-mode ; Do nothing if already in view mode.
510 (view-mode-enable)
511 (force-mode-line-update)
512 (message "%s"
513 (substitute-command-keys "\
514 View mode: type \\[help-command] for help, \\[describe-mode] for commands, \\[View-quit] to quit."))))
516 (defun view-mode-exit (&optional return-to-alist exit-action all-win)
517 "Exit View mode in various ways, depending on optional arguments.
518 RETURN-TO-ALIST, EXIT-ACTION and ALL-WIN determine what to do after exit.
519 EXIT-ACTION is nil or a function that is called with current buffer as
520 argument.
521 RETURN-TO-ALIST is an alist that for some of the windows displaying the
522 current buffer, associate information on what to do with those windows.
523 If ALL-WIN or the variable `view-exits-all-viewing-windows' is non-nil,
524 then all windows on RETURN-TO-ALIST are restored to their old state.
525 Otherwise only the selected window is affected (if it is on RETURN-TO-ALIST).
527 Elements of RETURN-TO-ALIST have the format (WINDOW OLD-WINDOW . OLD-BUF-INFO).
528 WINDOW is a window displaying the current buffer.
529 OLD-WINDOW is nil or a window to select after viewing.
530 OLD-BUF-INFO is information on what to do with WINDOW and is one of:
531 1) nil Do nothing.
532 2) t Delete WINDOW and, if it is the only window, its frame.
533 3) (OLD-BUF START POINT) Display buffer OLD-BUF with displayed text
534 starting at START and point at POINT in WINDOW.
535 4) quit-window Do `quit-window' in WINDOW.
537 If one of the WINDOW in RETURN-TO-ALIST is the selected window and the
538 corresponding OLD-WINDOW is a live window, then select OLD-WINDOW."
539 (setq all-win
540 (and return-to-alist (or all-win view-exits-all-viewing-windows)))
541 (if view-mode ; Only do something if in view mode.
542 (let* ((buffer (current-buffer))
543 window notlost
544 (sel-old (assq (selected-window) return-to-alist))
545 (alist (cond
546 (all-win ; Try to restore all windows.
547 (append return-to-alist nil)) ; Copy.
548 (sel-old ; Only selected window.
549 (list sel-old))))
550 (old-window (if sel-old (car (cdr sel-old)))))
551 (if all-win ; Follow chains of old-windows.
552 (let ((c (length alist)) a)
553 (while (and (> c 0) ; Safety if mutually refering windows.
554 (or (not (window-live-p old-window))
555 (eq buffer (window-buffer old-window)))
556 (setq a (assq old-window alist)))
557 (setq c (1- c))
558 (setq old-window (car (cdr a))))
559 (if (or (zerop c) (not (window-live-p old-window)))
560 (setq old-window (selected-window)))))
561 (or view-no-disable-on-exit
562 (view-mode-disable))
563 (while alist ; Restore windows with info.
564 (setq notlost nil)
565 (if (and (window-live-p (setq window (car (car alist))))
566 (eq buffer (window-buffer window)))
567 (let ((frame (window-frame window))
568 (old-buf-info (cdr (cdr (car alist)))))
569 (if all-win (select-window window))
570 (cond
571 ((and (consp old-buf-info) ; Case 3.
572 (buffer-live-p (car old-buf-info)))
573 (set-window-buffer window (car old-buf-info)) ; old-buf
574 (set-window-start window (car (cdr old-buf-info)))
575 (set-window-point window (car (cdr (cdr old-buf-info)))))
576 ((eq old-buf-info 'quit-window)
577 (quit-window)) ; Case 4.
578 ((not (eq old-buf-info t)) nil) ; Not case 2, do nothing.
579 ((not (one-window-p t)) (delete-window))
580 ((not (eq frame (next-frame)))
581 ;; Not the only frame, so can safely be removed.
582 (if view-remove-frame-by-deleting
583 (delete-frame frame)
584 (setq notlost t) ; Keep the window. See below.
585 (iconify-frame frame))))))
586 ;; If a frame is removed by iconifying it, then the window is not
587 ;; really lost. In this case we keep the entry in
588 ;; view-return-to-alist so that if the user deiconifies the frame
589 ;; and then press q, then the frame is iconified again.
590 (unless notlost
591 (setq view-return-to-alist
592 (delete (car alist) view-return-to-alist)))
593 (setq alist (cdr alist)))
594 (if (window-live-p old-window) ; still existing window
595 (select-window old-window))
596 (when exit-action
597 (setq view-exit-action nil)
598 (funcall exit-action buffer))
599 (force-mode-line-update))))
601 (defun View-exit ()
602 "Exit View mode but stay in current buffer."
603 (interactive)
604 (view-mode-exit))
606 ;;;###autoload
607 (defun View-exit-and-edit ()
608 "Exit View mode and make the current buffer editable."
609 (interactive)
610 (let ((view-old-buffer-read-only nil)
611 (view-no-disable-on-exit nil))
612 (view-mode-exit)))
614 (defun View-leave ()
615 "Quit View mode and maybe switch buffers, but don't kill this buffer."
616 (interactive)
617 (view-mode-exit view-return-to-alist))
619 (defun View-quit ()
620 "Quit View mode, trying to restore window and buffer to previous state.
621 Maybe kill this buffer. Try to restore selected window to previous state
622 and go to previous buffer or window."
623 (interactive)
624 (view-mode-exit view-return-to-alist view-exit-action))
626 (defun View-quit-all ()
627 "Quit View mode, trying to restore windows and buffers to previous state.
628 Maybe kill current buffer. Try to restore all windows viewing buffer to
629 previous state and go to previous buffer or window."
630 (interactive)
631 (view-mode-exit view-return-to-alist view-exit-action t))
633 (defun View-kill-and-leave ()
634 "Quit View mode, kill current buffer and return to previous buffer."
635 (interactive)
636 (view-mode-exit view-return-to-alist (or view-exit-action 'kill-buffer) t))
639 ;;; Some help routines.
641 (defun view-window-size ()
642 ;; Window height excluding mode line.
643 (1- (window-height)))
645 ;(defun view-last-command (&optional who what)
646 ; (setq view-last-command-entry this-command)
647 ; (setq view-last-command who)
648 ; (setq view-last-command-argument what))
650 ;(defun View-repeat-last-command ()
651 ; "Repeat last command issued in View mode."
652 ; (interactive)
653 ; (if (and view-last-command
654 ; (eq view-last-command-entry last-command))
655 ; (funcall view-last-command view-last-command-argument))
656 ; (setq this-command view-last-command-entry))
658 (defun view-recenter ()
659 ;; Center point in window.
660 (recenter (/ (view-window-size) 2)))
662 (defun view-page-size-default (lines)
663 ;; Get page size.
664 (let ((default (- (view-window-size) next-screen-context-lines)))
665 (if (or (null lines) (zerop (setq lines (prefix-numeric-value lines))))
666 default
667 (min (abs lines) default))))
669 (defun view-set-half-page-size-default (lines)
670 ;; Get and maybe set half page size.
671 (if (not lines) view-half-page-size
672 (setq view-half-page-size
673 (if (zerop (setq lines (prefix-numeric-value lines)))
674 (/ (view-window-size) 2)
675 (view-page-size-default lines)))))
678 ;;; Commands for moving around in the buffer.
680 (defun View-goto-percent (&optional percent)
681 "Move to end (or prefix PERCENT) of buffer in View mode.
682 Display is centered at point.
683 Also set the mark at the position where point was."
684 (interactive "P")
685 (push-mark)
686 (goto-char
687 (if percent
688 (+ (point-min)
689 (floor (* (- (point-max) (point-min)) 0.01
690 (max 0 (min 100 (prefix-numeric-value percent))))))
691 (point-max)))
692 (view-recenter))
694 ;(defun View-goto-line-last (&optional line)
695 ;"Move to last (or prefix LINE) line in View mode.
696 ;Display is centered at LINE.
697 ;Sets mark at starting position and pushes mark ring."
698 ; (interactive "P")
699 ; (push-mark)
700 ; (if line (goto-line (prefix-numeric-value line))
701 ; (goto-char (point-max))
702 ; (beginning-of-line))
703 ; (view-recenter))
705 (defun View-goto-line (&optional line)
706 "Move to first (or prefix LINE) line in View mode.
707 Display is centered at LINE.
708 Also set the mark at the position where point was."
709 (interactive "p")
710 (push-mark)
711 (goto-line line)
712 (view-recenter))
714 (defun View-back-to-mark (&optional ignore)
715 "Return to last mark set in View mode, else beginning of file.
716 Display that line at the center of the window.
717 This command pops the mark ring, so that successive
718 invocations return to earlier marks."
719 (interactive)
720 (goto-char (or (mark t) (point-min)))
721 (pop-mark)
722 (view-recenter))
724 (defun view-scroll-lines (lines backward default maxdefault)
725 ;; This function does the job for all the scrolling commands.
726 ;; Scroll forward LINES lines. If BACKWARD is true scroll backwards.
727 ;; If LINES is negative scroll in the other direction. If LINES is 0 or nil,
728 ;; scroll DEFAULT lines. If MAXDEFAULT is true then scroll no more than a
729 ;; window full.
730 (if (or (null lines) (zerop (setq lines (prefix-numeric-value lines))))
731 (setq lines default))
732 (when (< lines 0)
733 (setq backward (not backward)) (setq lines (- lines)))
734 (setq default (view-page-size-default nil)) ; Max scrolled at a time.
735 (if maxdefault (setq lines (min lines default)))
736 (cond
737 (backward (scroll-down lines))
738 ((view-really-at-end)
739 (if view-scroll-auto-exit (View-quit)
740 (ding)
741 (view-end-message)))
742 (t (while (> lines default)
743 (scroll-up default)
744 (setq lines (- lines default))
745 (if (view-really-at-end) (setq lines 0)))
746 (scroll-up lines)
747 (if (view-really-at-end) (view-end-message))
748 (move-to-window-line -1)
749 (beginning-of-line))))
751 (defun view-really-at-end ()
752 ;; Return true if buffer end visible. Maybe revert buffer and test.
753 (and (pos-visible-in-window-p (point-max))
754 (let ((buf (current-buffer))
755 (bufname (buffer-name))
756 (file (buffer-file-name)))
757 (or (not view-try-extend-at-buffer-end)
758 (null file)
759 (verify-visited-file-modtime buf)
760 (not (file-exists-p file))
761 (when (buffer-modified-p buf)
762 (setq file (file-name-nondirectory file))
763 (not (yes-or-no-p
764 (format
765 "File %s changed on disk. Discard your edits%s? "
766 file
767 (if (string= bufname file) ""
768 (concat " in " bufname))))))
769 (progn
770 (revert-buffer t t t)
771 (pos-visible-in-window-p (point-max)))))))
773 (defun view-end-message ()
774 ;; Tell that we are at end of buffer.
775 (goto-char (point-max))
776 (if view-return-to-alist
777 (message "End of buffer. Type %s to quit viewing."
778 (substitute-command-keys
779 (if view-scroll-auto-exit "\\[View-scroll-page-forward]"
780 "\\[View-quit]")))
781 (message "End of buffer")))
783 (defun View-scroll-to-buffer-end ()
784 "Scroll backward or forward so that buffer end is at last line of window."
785 (interactive)
786 (let ((p (if (pos-visible-in-window-p (point-max)) (point))))
787 (goto-char (point-max))
788 (recenter -1)
789 (and p (goto-char p))))
791 (defun View-scroll-page-forward (&optional lines)
792 "Scroll \"page size\" or prefix LINES lines forward in View mode.
793 Exit if end of text is visible and `view-scroll-auto-exit' is non-nil.
794 \"page size\" is whole window full, or number of lines set by
795 \\[View-scroll-page-forward-set-page-size] or
796 \\[View-scroll-page-backward-set-page-size].
797 If LINES is more than a window-full, only the last window-full is shown."
798 (interactive "P")
799 (view-scroll-lines lines nil view-page-size nil))
801 (defun View-scroll-page-backward (&optional lines)
802 "Scroll \"page size\" or prefix LINES lines backward in View mode.
803 See also `View-scroll-page-forward'."
804 (interactive "P")
805 (view-scroll-lines lines t view-page-size nil))
807 (defun View-scroll-page-forward-set-page-size (&optional lines)
808 "Scroll forward LINES lines in View mode, setting the \"page size\".
809 This is the number of lines which \\[View-scroll-page-forward] and
810 \\[View-scroll-page-backward] scroll by default.
811 If LINES is omitted or = 0, sets \"page size\" to window height and
812 scrolls forward that much, otherwise scrolls forward LINES lines and sets
813 \"page size\" to the minimum of window height and the absolute value of LINES.
814 See also `View-scroll-page-forward'."
815 (interactive "P")
816 (view-scroll-lines lines nil
817 (setq view-page-size (view-page-size-default lines))
818 nil))
820 (defun View-scroll-page-backward-set-page-size (&optional lines)
821 "Scroll backward prefix LINES lines in View mode, setting the \"page size\".
822 See also `View-scroll-page-forward-set-page-size'."
823 (interactive "P")
824 (view-scroll-lines lines t
825 (setq view-page-size (view-page-size-default lines))
826 nil))
828 (defun View-scroll-line-forward (&optional lines)
829 "Scroll forward one line (or prefix LINES lines) in View mode.
830 See also `View-scroll-page-forward,' but note that scrolling is limited
831 to minimum of LINES and one window-full."
832 (interactive "P")
833 (view-scroll-lines lines nil 1 t))
835 (defun View-scroll-line-backward (&optional lines)
836 "Scroll backward one line (or prefix LINES lines) in View mode.
837 See also `View-scroll-line-forward'."
838 (interactive "P")
839 (view-scroll-lines lines t 1 t))
841 (defun View-scroll-half-page-forward (&optional lines)
842 "Scroll forward a \"half page\" (or prefix LINES) lines in View mode.
843 If LINES is not omitted, the \"half page size\" is set to the minimum of
844 window height and the absolute value of LINES.
845 LINES=0 resets \"half page size\" to half window height."
846 (interactive "P")
847 (view-scroll-lines lines nil (view-set-half-page-size-default lines) t))
849 (defun View-scroll-half-page-backward (&optional lines)
850 "Scroll backward a \"half page\" (or prefix LINES) lines in View mode.
851 See also `View-scroll-half-page-forward'."
852 (interactive "P")
853 (view-scroll-lines lines t (view-set-half-page-size-default lines) t))
855 (defun View-revert-buffer-scroll-page-forward (&optional lines)
856 "Scroll forward, reverting buffer if needed, in View mode.
857 If buffer has not been changed and the corresponding file is newer, first
858 revert the buffer, then scroll.
859 This command is useful if you are viewing a changing file.
861 The prefix argument LINES says how many lines to scroll.
862 If you don't specify a prefix argument, it uses the number of lines set by
863 \\[View-scroll-page-forward-set-page-size] or
864 \\[View-scroll-page-backward-set-page-size].
865 If LINES is more than a window-full, only the last window-full is shown."
866 (interactive "P")
867 (let ((view-scroll-auto-exit nil)
868 (view-try-extend-at-buffer-end t))
869 (view-scroll-lines lines nil view-page-size nil)))
871 (defun View-search-regexp-forward (n regexp)
872 "Search forward for first (or prefix Nth) occurrence of REGEXP in View mode.
874 Displays line found at center of window. Sets mark at starting position and
875 pushes mark ring.
877 Characters @ and ! are special at the beginning of REGEXP. They modify
878 the search rather than become part of the pattern searched for.
879 @ means search all the buffer i.e. start search at the beginning of buffer.
880 ! means search for a line that contains no match for the pattern.
881 If REGEXP is empty or only consist of these control characters, then
882 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
883 for use by later search commands.
885 The variable `view-highlight-face' controls the face that is used
886 for highlighting the match that is found."
887 (interactive "p\nsSearch forward (regexp): ")
888 (view-search n regexp))
890 (defun View-search-regexp-backward (n regexp)
891 "Search backward for first (or prefix Nth) occurrence of REGEXP in View mode.
893 Displays line found at center of window. Sets mark at starting position and
894 pushes mark ring.
896 Characters @ and ! are special at the beginning of REGEXP. They modify
897 the search rather than become part of the pattern searched for.
898 @ means search all the buffer i.e. start search at the end of buffer.
899 ! means search for a line that contains no match for the pattern.
900 If REGEXP is empty or only consist of these control characters, then
901 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
902 for use by later search commands.
904 The variable `view-highlight-face' controls the face that is used
905 for highlighting the match that is found."
906 (interactive "p\nsSearch backward (regexp): ")
907 (view-search (- n) regexp))
909 (defun View-search-last-regexp-forward (n) "\
910 Search forward for first (or prefix Nth) instance of last regexp in View mode.
911 Displays line found at center of window. Sets mark at starting position and
912 pushes mark ring.
914 The variable `view-highlight-face' controls the face that is used
915 for highlighting the match that is found."
916 (interactive "p")
917 (view-search n nil))
919 (defun View-search-last-regexp-backward (n) "\
920 Search backward for first (or prefix Nth) instance of last regexp in View mode.
921 Displays line found at center of window. Sets mark at starting position and
922 pushes mark ring.
924 The variable `view-highlight-face' controls the face that is used
925 for highlighting the match that is found."
926 (interactive "p")
927 (view-search (- n) nil))
929 (defun view-search (times regexp)
930 ;; This function does the job for all the View-search- commands.
931 ;; Search for the TIMESt match for REGEXP. If TIMES is negative
932 ;; search backwards. If REGEXP is nil use `view-last-regexp'.
933 ;; Charcters "!" and "@" have a special meaning at the beginning of
934 ;; REGEXP and are removed from REGEXP before the search "!" means
935 ;; search for lines with no match for REGEXP. "@" means search in
936 ;; the whole buffer, don't start searching from the present point.
937 (let (where no end ln)
938 (cond
939 ((and regexp (> (length regexp) 0)
940 (or (not (memq (string-to-char regexp) '(?! ?@)))
941 (progn
942 (if (member (substring regexp 0 2) '("!@" "@!"))
943 (setq end t no t ln 2)
944 (setq no (not (setq end (eq ?@ (string-to-char regexp))))
945 ln 1))
946 (> (length (setq regexp (substring regexp ln))) 0))))
947 (setq view-last-regexp (if no (list regexp) regexp)))
948 ((consp view-last-regexp)
949 (setq regexp (car view-last-regexp))
950 (unless (setq no (not no)) (setq view-last-regexp regexp)))
951 (view-last-regexp (setq regexp view-last-regexp)
952 (if no (setq view-last-regexp (list regexp))))
953 (t (error "No previous View-mode search")))
954 (save-excursion
955 (if end (goto-char (if (< times 0) (point-max) (point-min)))
956 (move-to-window-line (if (< times 0) 0 -1)))
957 (if (if no (view-search-no-match-lines times regexp)
958 (re-search-forward regexp nil t times))
959 (setq where (point))))
960 (if where
961 (progn
962 (push-mark)
963 (goto-char where)
964 (if view-overlay
965 (move-overlay view-overlay (match-beginning 0) (match-end 0))
966 (setq view-overlay
967 (make-overlay (match-beginning 0) (match-end 0))))
968 (overlay-put view-overlay 'face view-highlight-face)
969 (beginning-of-line)
970 (view-recenter))
971 (message "Can't find occurrence %d of %s%s"
972 times (if no "no " "") regexp)
973 (sit-for 4))))
975 (defun view-search-no-match-lines (times regexp)
976 ;; Search for the TIMESt occurrence of line with no match for REGEXP.
977 (let ((back (and (< times 0) (setq times (- times)) -1))
979 (while (> times 0)
980 (save-excursion (beginning-of-line (if back (- times) (1+ times)))
981 (setq n (point)))
982 (setq times
983 (cond
984 ((< (count-lines (point) n) times) -1) ; Not enough lines.
985 ((or (null (re-search-forward regexp nil t back))
986 (if back (and (< (match-end 0) n)
987 (> (count-lines (match-end 0) n) 1))
988 (and (< n (match-beginning 0))
989 (> (count-lines n (match-beginning 0)) 1))))
990 0) ; No match within lines.
991 (back (count-lines (max n (match-beginning 0)) (match-end 0)))
992 (t (count-lines (match-beginning 0) (min n (match-end 0))))))
993 (goto-char n))
994 (and (zerop times) (looking-at "^.*$"))))
997 (provide 'view)
999 ;;; view.el ends here