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