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