]> code.delx.au - gnu-emacs/blob - lisp/view.el
(view-read-only): Move from view.el to files.el.
[gnu-emacs] / lisp / view.el
1 ;;; view.el --- peruse file or buffer without editing
2
3 ;; Copyright (C) 1985, 1989, 1994, 1995, 1997, 2000, 2001
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: K. Shane Hartman
7 ;; Maintainer: Inge Frick <inge@nada.kth.se>
8 ;; Keywords: files
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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.
26
27 ;;; Commentary:
28
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.
38
39 ;;; Suggested key bindings:
40 ;;
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
43 ;;
44 ;; You could also bind view-file, view-buffer, view-buffer-other-window and
45 ;; view-buffer-other-frame to keys.
46
47 ;;; Code:
48
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)
55
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)
60
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)
71
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)
79
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)
85
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)
95
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)
103
104 (defcustom view-mode-hook nil
105 "Normal hook run when starting to view a buffer or file."
106 :type 'hook
107 :group 'view)
108
109 (defvar view-old-buffer-read-only nil)
110 (make-variable-buffer-local 'view-old-buffer-read-only)
111
112 (defvar view-old-Helper-return-blurb)
113 (make-variable-buffer-local 'view-old-Helper-return-blurb)
114
115 (defvar view-page-size nil
116 "Default number of lines to scroll by View page commands.
117 If nil then the local value of this is initially set to window size.")
118 (make-variable-buffer-local 'view-page-size)
119
120 (defvar view-half-page-size nil
121 "Default number of lines to scroll by View half page commands.
122 If nil then the local value of this is initially set to half window size.")
123 (make-variable-buffer-local 'view-half-page-size)
124
125 (defvar view-last-regexp nil)
126 (make-variable-buffer-local 'view-last-regexp) ; Global is better???
127
128 (defvar view-return-to-alist nil
129 "What to do with used windows and where to go when finished viewing buffer.
130 This is local in each buffer being viewed.
131 It is added to by `view-mode-enter' when starting to view a buffer and
132 subtracted from by `view-mode-exit' when finished viewing the buffer.
133
134 See RETURN-TO-ALIST argument of function `view-mode-exit' for the format of
135 `view-return-to-alist'.")
136 (make-variable-buffer-local 'view-return-to-alist)
137
138 (defvar view-exit-action nil
139 "nil or a function with one argument (a buffer) called when finished viewing.
140 This is local in each buffer being viewed.
141 The \\[view-file] and \\[view-file-other-window] commands may set this to
142 `kill-buffer'.")
143 (make-variable-buffer-local 'view-exit-action)
144
145 (defvar view-no-disable-on-exit nil
146 "If non-nil, View mode \"exit\" commands don't actually disable View mode.
147 Instead, these commands just switch buffers or windows.
148 This is set in certain buffers by specialized features such as help commands
149 that use View mode automatically.")
150
151 (defvar view-overlay nil
152 "Overlay used to display where a search operation found its match.
153 This is local in each buffer, once it is used.")
154 (make-variable-buffer-local 'view-overlay)
155
156 (unless (assq 'view-mode minor-mode-alist)
157 (setq minor-mode-alist
158 (cons (list 'view-mode
159 (propertize " View"
160 'local-map mode-line-minor-mode-keymap
161 'help-echo "mouse-3: minor mode menu"))
162 minor-mode-alist)))
163
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))
224
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)))
228
229 ;;; Commands that enter or exit view mode.
230
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.
239
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))))
245
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.
255
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))))
261
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.
271
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))))
277
278
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.
287
288 This command runs the normal hook `view-mode-hook'.
289
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'."
293
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)))
299
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.
309
310 This command runs the normal hook `view-mode-hook'.
311
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)))
330
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.
340
341 This command runs the normal hook `view-mode-hook'.
342
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)))
352
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.
360
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.
371
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, restoring this 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 This command restores the previous read-only status of the buffer.
416 \\[View-exit-and-edit] exit View mode, and make the current buffer editable
417 even if it was not editable before entry to View mode.
418 \\[View-quit-all] quit View mode, restoring all windows to previous state.
419 \\[View-leave] quit View mode and maybe switch buffers, but don't kill this buffer.
420 \\[View-kill-and-leave] quit View mode, kill current buffer and go back to other buffer.
421
422 The effect of \\[View-leave] , \\[View-quit] and \\[View-kill-and-leave] depends on how view-mode was entered. If it was
423 entered by view-file, view-file-other-window or view-file-other-frame
424 \(\\[view-file], \\[view-file-other-window], \\[view-file-other-frame] or the dired mode v command), then \\[View-quit] will
425 try to kill the current buffer. If view-mode was entered from another buffer
426 as is done by View-buffer, View-buffer-other-window, View-buffer-other frame,
427 View-file, View-file-other-window or View-file-other-frame then \\[View-leave] , \\[View-quit] and \\[View-kill-and-leave]
428 will return to that buffer.
429
430 Entry to view-mode runs the normal hook `view-mode-hook'."
431 (interactive "P")
432 (unless (and arg ; Do nothing if already OK.
433 (if (> (prefix-numeric-value arg) 0) view-mode (not view-mode)))
434 (if view-mode (view-mode-disable)
435 (view-mode-enable))))
436
437 (defun view-mode-enable ()
438 "Turn on View mode."
439 ;; Always leave view mode before changing major mode.
440 ;; This is to guarantee that the buffer-read-only variable is restored.
441 (add-hook 'change-major-mode-hook 'view-mode-disable nil t)
442 (setq view-mode t
443 view-page-size (view-page-size-default view-page-size)
444 view-half-page-size (or view-half-page-size (/ (view-window-size) 2))
445 view-old-buffer-read-only buffer-read-only
446 buffer-read-only t
447 view-old-Helper-return-blurb (and (boundp 'Helper-return-blurb)
448 Helper-return-blurb)
449 Helper-return-blurb
450 (format "continue viewing %s"
451 (if (buffer-file-name)
452 (file-name-nondirectory (buffer-file-name))
453 (buffer-name))))
454 (force-mode-line-update)
455 (run-hooks 'view-mode-hook))
456
457 (defun view-mode-disable ()
458 "Turn off View mode."
459 (remove-hook 'change-major-mode-hook 'view-mode-disable t)
460 (and view-overlay (delete-overlay view-overlay))
461 (force-mode-line-update)
462 ;; Calling toggle-read-only while View mode is enabled
463 ;; sets view-read-only to t as a buffer-local variable
464 ;; after exiting View mode. That arranges that the next toggle-read-only
465 ;; will reenable View mode.
466 ;; Cancelling View mode in any other way should cancel that, too,
467 ;; so that View mode stays off if toggle-read-only is called.
468 (if (local-variable-p 'view-read-only)
469 (kill-local-variable 'view-read-only))
470 (setq view-mode nil
471 Helper-return-blurb view-old-Helper-return-blurb)
472 (if buffer-read-only
473 (setq buffer-read-only view-old-buffer-read-only)))
474
475 ;;;###autoload
476 (defun view-mode-enter (&optional return-to exit-action) "\
477 Enter View mode and set up exit from view mode depending on optional arguments.
478 If RETURN-TO is non-nil it is added as an element to the buffer local alist
479 `view-return-to-alist'.
480 Save EXIT-ACTION in buffer local variable `view-exit-action'.
481 It should be either nil or a function that takes a buffer as argument.
482 This function will be called by `view-mode-exit'.
483
484 RETURN-TO is either nil, meaning do nothing when exiting view mode, or
485 it has the format (WINDOW OLD-WINDOW . OLD-BUF-INFO).
486 WINDOW is a window used for viewing.
487 OLD-WINDOW is nil or the window to select after viewing.
488 OLD-BUF-INFO tells what to do with WINDOW when exiting. It is one of:
489 1) nil Do nothing.
490 2) t Delete WINDOW or, if it is the only window, its frame.
491 3) (OLD-BUFF START POINT) Display buffer OLD-BUFF with displayed text
492 starting at START and point at POINT in WINDOW.
493 4) quit-window Do `quit-window' in WINDOW.
494
495 For list of all View commands, type H or h while viewing.
496
497 This function runs the normal hook `view-mode-hook'."
498 (if return-to
499 (let ((entry (assq (car return-to) view-return-to-alist)))
500 (if entry (setcdr entry (cdr return-to))
501 (setq view-return-to-alist (cons return-to view-return-to-alist)))))
502 (if exit-action (setq view-exit-action exit-action))
503 (unless view-mode ; Do nothing if already in view mode.
504 (view-mode-enable)
505 (force-mode-line-update)
506 (message "%s"
507 (substitute-command-keys "\
508 View mode: type \\[help-command] for help, \\[describe-mode] for commands, \\[View-quit] to quit."))))
509
510 (defun view-mode-exit (&optional return-to-alist exit-action all-win)
511 "Exit View mode in various ways, depending on optional arguments.
512 RETURN-TO-ALIST, EXIT-ACTION and ALL-WIN determine what to do after exit.
513 EXIT-ACTION is nil or a function that is called with current buffer as
514 argument.
515 RETURN-TO-ALIST is an alist that for some of the windows displaying the
516 current buffer, associate information on what to do with those windows.
517 If ALL-WIN or the variable `view-exits-all-viewing-windows' is non-nil,
518 then all windows on RETURN-TO-ALIST are restored to their old state.
519 Otherwise only the selected window is affected (if it is on RETURN-TO-ALIST).
520
521 Elements of RETURN-TO-ALIST have the format (WINDOW OLD-WINDOW . OLD-BUF-INFO).
522 WINDOW is a window displaying the current buffer.
523 OLD-WINDOW is nil or a window to select after viewing.
524 OLD-BUF-INFO is information on what to do with WINDOW and is one of:
525 1) nil Do nothing.
526 2) t Delete WINDOW and, if it is the only window, its frame.
527 3) (OLD-BUF START POINT) Display buffer OLD-BUF with displayed text
528 starting at START and point at POINT in WINDOW.
529 4) quit-window Do `quit-window' in WINDOW.
530
531 If one of the WINDOW in RETURN-TO-ALIST is the selected window and the
532 corresponding OLD-WINDOW is a live window, then select OLD-WINDOW."
533 (setq all-win
534 (and return-to-alist (or all-win view-exits-all-viewing-windows)))
535 (if view-mode ; Only do something if in view mode.
536 (let* ((buffer (current-buffer))
537 window notlost
538 (sel-old (assq (selected-window) return-to-alist))
539 (alist (cond
540 (all-win ; Try to restore all windows.
541 (append return-to-alist nil)) ; Copy.
542 (sel-old ; Only selected window.
543 (list sel-old))))
544 (old-window (if sel-old (car (cdr sel-old)))))
545 (if all-win ; Follow chains of old-windows.
546 (let ((c (length alist)) a)
547 (while (and (> c 0) ; Safety if mutually refering windows.
548 (or (not (window-live-p old-window))
549 (eq buffer (window-buffer old-window)))
550 (setq a (assq old-window alist)))
551 (setq c (1- c))
552 (setq old-window (car (cdr a))))
553 (if (or (zerop c) (not (window-live-p old-window)))
554 (setq old-window (selected-window)))))
555 (or view-no-disable-on-exit
556 (view-mode-disable))
557 (while alist ; Restore windows with info.
558 (setq notlost nil)
559 (if (and (window-live-p (setq window (car (car alist))))
560 (eq buffer (window-buffer window)))
561 (let ((frame (window-frame window))
562 (old-buf-info (cdr (cdr (car alist)))))
563 (if all-win (select-window window))
564 (cond
565 ((and (consp old-buf-info) ; Case 3.
566 (buffer-live-p (car old-buf-info)))
567 (set-window-buffer window (car old-buf-info)) ; old-buf
568 (set-window-start window (car (cdr old-buf-info)))
569 (set-window-point window (car (cdr (cdr old-buf-info)))))
570 ((eq old-buf-info 'quit-window)
571 (quit-window)) ; Case 4.
572 ((not (eq old-buf-info t)) nil) ; Not case 2, do nothing.
573 ((not (one-window-p t)) (delete-window))
574 ((not (eq frame (next-frame)))
575 ;; Not the only frame, so can safely be removed.
576 (if view-remove-frame-by-deleting
577 (delete-frame frame)
578 (setq notlost t) ; Keep the window. See below.
579 (iconify-frame frame))))))
580 ;; If a frame is removed by iconifying it, then the window is not
581 ;; really lost. In this case we keep the entry in
582 ;; view-return-to-alist so that if the user deiconifies the frame
583 ;; and then press q, then the frame is iconified again.
584 (unless notlost
585 (setq view-return-to-alist
586 (delete (car alist) view-return-to-alist)))
587 (setq alist (cdr alist)))
588 (if (window-live-p old-window) ; still existing window
589 (select-window old-window))
590 (when exit-action
591 (setq view-exit-action nil)
592 (funcall exit-action buffer))
593 (force-mode-line-update))))
594
595 (defun View-exit ()
596 "Exit View mode but stay in current buffer."
597 (interactive)
598 (view-mode-exit))
599
600 ;;;###autoload
601 (defun View-exit-and-edit ()
602 "Exit View mode and make the current buffer editable."
603 (interactive)
604 (let ((view-old-buffer-read-only nil)
605 (view-no-disable-on-exit nil))
606 (view-mode-exit)))
607
608 (defun View-leave ()
609 "Quit View mode and maybe switch buffers, but don't kill this buffer."
610 (interactive)
611 (view-mode-exit view-return-to-alist))
612
613 (defun View-quit ()
614 "Quit View mode, trying to restore window and buffer to previous state.
615 Maybe kill this buffer. Try to restore selected window to previous state
616 and go to previous buffer or window."
617 (interactive)
618 (view-mode-exit view-return-to-alist view-exit-action))
619
620 (defun View-quit-all ()
621 "Quit View mode, trying to restore windows and buffers to previous state.
622 Maybe kill current buffer. Try to restore all windows viewing buffer to
623 previous state and go to previous buffer or window."
624 (interactive)
625 (view-mode-exit view-return-to-alist view-exit-action t))
626
627 (defun View-kill-and-leave ()
628 "Quit View mode, kill current buffer and return to previous buffer."
629 (interactive)
630 (view-mode-exit view-return-to-alist (or view-exit-action 'kill-buffer) t))
631
632
633 ;;; Some help routines.
634
635 (defun view-window-size ()
636 ;; Window height excluding mode line.
637 (1- (window-height)))
638
639 ;(defun view-last-command (&optional who what)
640 ; (setq view-last-command-entry this-command)
641 ; (setq view-last-command who)
642 ; (setq view-last-command-argument what))
643
644 ;(defun View-repeat-last-command ()
645 ; "Repeat last command issued in View mode."
646 ; (interactive)
647 ; (if (and view-last-command
648 ; (eq view-last-command-entry last-command))
649 ; (funcall view-last-command view-last-command-argument))
650 ; (setq this-command view-last-command-entry))
651
652 (defun view-recenter ()
653 ;; Center point in window.
654 (recenter (/ (view-window-size) 2)))
655
656 (defun view-page-size-default (lines)
657 ;; Get page size.
658 (let ((default (- (view-window-size) next-screen-context-lines)))
659 (if (or (null lines) (zerop (setq lines (prefix-numeric-value lines))))
660 default
661 (min (abs lines) default))))
662
663 (defun view-set-half-page-size-default (lines)
664 ;; Get and maybe set half page size.
665 (if (not lines) view-half-page-size
666 (setq view-half-page-size
667 (if (zerop (setq lines (prefix-numeric-value lines)))
668 (/ (view-window-size) 2)
669 (view-page-size-default lines)))))
670
671
672 ;;; Commands for moving around in the buffer.
673
674 (defun View-goto-percent (&optional percent)
675 "Move to end (or prefix PERCENT) of buffer in View mode.
676 Display is centered at point.
677 Also set the mark at the position where point was."
678 (interactive "P")
679 (push-mark)
680 (goto-char
681 (if percent
682 (+ (point-min)
683 (floor (* (- (point-max) (point-min)) 0.01
684 (max 0 (min 100 (prefix-numeric-value percent))))))
685 (point-max)))
686 (view-recenter))
687
688 ;(defun View-goto-line-last (&optional line)
689 ;"Move to last (or prefix LINE) line in View mode.
690 ;Display is centered at LINE.
691 ;Sets mark at starting position and pushes mark ring."
692 ; (interactive "P")
693 ; (push-mark)
694 ; (if line (goto-line (prefix-numeric-value line))
695 ; (goto-char (point-max))
696 ; (beginning-of-line))
697 ; (view-recenter))
698
699 (defun View-goto-line (&optional line)
700 "Move to first (or prefix LINE) line in View mode.
701 Display is centered at LINE.
702 Also set the mark at the position where point was."
703 (interactive "p")
704 (push-mark)
705 (goto-line line)
706 (view-recenter))
707
708 (defun View-scroll-to-buffer-end ()
709 "Scroll backward or forward so that buffer end is at last line of window."
710 (interactive)
711 (let ((p (if (pos-visible-in-window-p (point-max)) (point))))
712 (goto-char (point-max))
713 (recenter -1)
714 (and p (goto-char p))))
715
716 (defun view-scroll-lines (lines backward default maxdefault)
717 ;; This function does the job for all the scrolling commands.
718 ;; Scroll forward LINES lines. If BACKWARD is true scroll backwards.
719 ;; If LINES is negative scroll in the other direction. If LINES is 0 or nil,
720 ;; scroll DEFAULT lines. If MAXDEFAULT is true then scroll no more than a
721 ;; window full.
722 (if (or (null lines) (zerop (setq lines (prefix-numeric-value lines))))
723 (setq lines default))
724 (when (< lines 0)
725 (setq backward (not backward)) (setq lines (- lines)))
726 (setq default (view-page-size-default nil)) ; Max scrolled at a time.
727 (if maxdefault (setq lines (min lines default)))
728 (cond
729 (backward (scroll-down lines))
730 ((view-really-at-end)
731 (if view-scroll-auto-exit (View-quit)
732 (ding)
733 (view-end-message)))
734 (t (while (> lines default)
735 (scroll-up default)
736 (setq lines (- lines default))
737 (if (view-really-at-end) (setq lines 0)))
738 (scroll-up lines)
739 (if (view-really-at-end) (view-end-message))
740 (move-to-window-line -1)
741 (beginning-of-line))))
742
743 (defun view-really-at-end ()
744 ;; Return true if buffer end visible. Maybe revert buffer and test.
745 (and (pos-visible-in-window-p (point-max))
746 (let ((buf (current-buffer))
747 (bufname (buffer-name))
748 (file (buffer-file-name)))
749 (or (not view-try-extend-at-buffer-end)
750 (null file)
751 (verify-visited-file-modtime buf)
752 (not (file-exists-p file))
753 (when (buffer-modified-p buf)
754 (setq file (file-name-nondirectory file))
755 (not (yes-or-no-p
756 (format
757 "File %s changed on disk. Discard your edits%s? "
758 file
759 (if (string= bufname file) ""
760 (concat " in " bufname))))))
761 (progn
762 (revert-buffer t t t)
763 (pos-visible-in-window-p (point-max)))))))
764
765 (defun view-end-message ()
766 ;; Tell that we are at end of buffer.
767 (goto-char (point-max))
768 (if view-return-to-alist
769 (message "End of buffer. Type %s to quit viewing."
770 (substitute-command-keys
771 (if view-scroll-auto-exit "\\[View-scroll-page-forward]"
772 "\\[View-quit]")))
773 (message "End of buffer")))
774
775 (defun View-scroll-page-forward (&optional lines)
776 "Scroll \"page size\" or prefix LINES lines forward in View mode.
777 Exit if end of text is visible and `view-scroll-auto-exit' is non-nil.
778 \"page size\" is whole window full, or number of lines set by
779 \\[View-scroll-page-forward-set-page-size] or
780 \\[View-scroll-page-backward-set-page-size].
781 If LINES is more than a window-full, only the last window-full is shown."
782 (interactive "P")
783 (view-scroll-lines lines nil view-page-size nil))
784
785 (defun View-scroll-page-backward (&optional lines)
786 "Scroll \"page size\" or prefix LINES lines backward in View mode.
787 See also `View-scroll-page-forward'."
788 (interactive "P")
789 (view-scroll-lines lines t view-page-size nil))
790
791 (defun View-scroll-page-forward-set-page-size (&optional lines)
792 "Scroll forward LINES lines in View mode, setting the \"page size\".
793 This is the number of lines which \\[View-scroll-page-forward] and
794 \\[View-scroll-page-backward] scroll by default.
795 If LINES is omitted or = 0, sets \"page size\" to window height and
796 scrolls forward that much, otherwise scrolls forward LINES lines and sets
797 \"page size\" to the minimum of window height and the absolute value of LINES.
798 See also `View-scroll-page-forward'."
799 (interactive "P")
800 (view-scroll-lines lines nil
801 (setq view-page-size (view-page-size-default lines))
802 nil))
803
804 (defun View-scroll-page-backward-set-page-size (&optional lines)
805 "Scroll backward prefix LINES lines in View mode, setting the \"page size\".
806 See also `View-scroll-page-forward-set-page-size'."
807 (interactive "P")
808 (view-scroll-lines lines t
809 (setq view-page-size (view-page-size-default lines))
810 nil))
811
812 (defun View-scroll-line-forward (&optional lines)
813 "Scroll forward one line (or prefix LINES lines) in View mode.
814 See also `View-scroll-page-forward,' but note that scrolling is limited
815 to minimum of LINES and one window-full."
816 (interactive "P")
817 (view-scroll-lines lines nil 1 t))
818
819 (defun View-scroll-line-backward (&optional lines)
820 "Scroll backward one line (or prefix LINES lines) in View mode.
821 See also `View-scroll-line-forward'."
822 (interactive "P")
823 (view-scroll-lines lines t 1 t))
824
825 (defun View-scroll-half-page-forward (&optional lines)
826 "Scroll forward a \"half page\" (or prefix LINES) lines in View mode.
827 If LINES is not omitted, the \"half page size\" is set to the minimum of
828 window height and the absolute value of LINES.
829 LINES=0 resets \"half page size\" to half window height."
830 (interactive "P")
831 (view-scroll-lines lines nil (view-set-half-page-size-default lines) t))
832
833 (defun View-scroll-half-page-backward (&optional lines)
834 "Scroll backward a \"half page\" (or prefix LINES) lines in View mode.
835 See also `View-scroll-half-page-forward'."
836 (interactive "P")
837 (view-scroll-lines lines t (view-set-half-page-size-default lines) t))
838
839 (defun View-revert-buffer-scroll-page-forward (&optional lines)
840 "Scroll forward, reverting buffer if needed, in View mode.
841 If buffer has not been changed and the corresponding file is newer, first
842 revert the buffer, then scroll.
843 This command is useful if you are viewing a changing file.
844
845 The prefix argument LINES says how many lines to scroll.
846 If you don't specify a prefix argument, it uses the number of lines set by
847 \\[View-scroll-page-forward-set-page-size] or
848 \\[View-scroll-page-backward-set-page-size].
849 If LINES is more than a window-full, only the last window-full is shown."
850 (interactive "P")
851 (let ((view-scroll-auto-exit nil)
852 (view-try-extend-at-buffer-end t))
853 (view-scroll-lines lines nil view-page-size nil)))
854
855 (defun View-back-to-mark (&optional ignore)
856 "Return to last mark set in View mode, else beginning of file.
857 Display that line at the center of the window.
858 This command pops the mark ring, so that successive
859 invocations return to earlier marks."
860 (interactive)
861 (goto-char (or (mark t) (point-min)))
862 (pop-mark)
863 (view-recenter))
864
865 (defun View-search-regexp-forward (n regexp)
866 "Search forward for first (or prefix Nth) occurrence of REGEXP in View mode.
867
868 Displays line found at center of window. Sets mark at starting position and
869 pushes mark ring.
870
871 Characters @ and ! are special at the beginning of REGEXP. They modify
872 the search rather than become part of the pattern searched for.
873 @ means search all the buffer i.e. start search at the beginning of buffer.
874 ! means search for a line that contains no match for the pattern.
875 If REGEXP is empty or only consist of these control characters, then
876 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
877 for use by later search commands.
878
879 The variable `view-highlight-face' controls the face that is used
880 for highlighting the match that is found."
881 (interactive "p\nsSearch forward (regexp): ")
882 (view-search n regexp))
883
884 (defun View-search-regexp-backward (n regexp)
885 "Search backward for first (or prefix Nth) occurrence of REGEXP in View mode.
886
887 Displays line found at center of window. Sets mark at starting position and
888 pushes mark ring.
889
890 Characters @ and ! are special at the beginning of REGEXP. They modify
891 the search rather than become part of the pattern searched for.
892 @ means search all the buffer i.e. start search at the end of buffer.
893 ! means search for a line that contains no match for the pattern.
894 If REGEXP is empty or only consist of these control characters, then
895 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
896 for use by later search commands.
897
898 The variable `view-highlight-face' controls the face that is used
899 for highlighting the match that is found."
900 (interactive "p\nsSearch backward (regexp): ")
901 (view-search (- n) regexp))
902
903 (defun View-search-last-regexp-forward (n) "\
904 Search forward for first (or prefix Nth) instance of last regexp in View mode.
905 Displays line found at center of window. Sets mark at starting position and
906 pushes mark ring.
907
908 The variable `view-highlight-face' controls the face that is used
909 for highlighting the match that is found."
910 (interactive "p")
911 (view-search n nil))
912
913 (defun View-search-last-regexp-backward (n) "\
914 Search backward for first (or prefix Nth) instance of last regexp in View mode.
915 Displays line found at center of window. Sets mark at starting position and
916 pushes mark ring.
917
918 The variable `view-highlight-face' controls the face that is used
919 for highlighting the match that is found."
920 (interactive "p")
921 (view-search (- n) nil))
922
923 (defun view-search (times regexp)
924 ;; This function does the job for all the View-search- commands.
925 ;; Search for the TIMESt match for REGEXP. If TIMES is negative
926 ;; search backwards. If REGEXP is nil use `view-last-regexp'.
927 ;; Charcters "!" and "@" have a special meaning at the beginning of
928 ;; REGEXP and are removed from REGEXP before the search "!" means
929 ;; search for lines with no match for REGEXP. "@" means search in
930 ;; the whole buffer, don't start searching from the present point.
931 (let (where no end ln)
932 (cond
933 ((and regexp (> (length regexp) 0)
934 (or (not (memq (string-to-char regexp) '(?! ?@)))
935 (progn
936 (if (member (substring regexp 0 2) '("!@" "@!"))
937 (setq end t no t ln 2)
938 (setq no (not (setq end (eq ?@ (string-to-char regexp))))
939 ln 1))
940 (> (length (setq regexp (substring regexp ln))) 0))))
941 (setq view-last-regexp (if no (list regexp) regexp)))
942 ((consp view-last-regexp)
943 (setq regexp (car view-last-regexp))
944 (unless (setq no (not no)) (setq view-last-regexp regexp)))
945 (view-last-regexp (setq regexp view-last-regexp)
946 (if no (setq view-last-regexp (list regexp))))
947 (t (error "No previous View-mode search")))
948 (save-excursion
949 (if end (goto-char (if (< times 0) (point-max) (point-min)))
950 (move-to-window-line (if (< times 0) 0 -1)))
951 (if (if no (view-search-no-match-lines times regexp)
952 (re-search-forward regexp nil t times))
953 (setq where (point))))
954 (if where
955 (progn
956 (push-mark)
957 (goto-char where)
958 (if view-overlay
959 (move-overlay view-overlay (match-beginning 0) (match-end 0))
960 (setq view-overlay
961 (make-overlay (match-beginning 0) (match-end 0))))
962 (overlay-put view-overlay 'face view-highlight-face)
963 (beginning-of-line)
964 (view-recenter))
965 (message "Can't find occurrence %d of %s%s"
966 times (if no "no " "") regexp)
967 (sit-for 4))))
968
969 (defun view-search-no-match-lines (times regexp)
970 ;; Search for the TIMESt occurrence of line with no match for REGEXP.
971 (let ((back (and (< times 0) (setq times (- times)) -1))
972 n)
973 (while (> times 0)
974 (save-excursion (beginning-of-line (if back (- times) (1+ times)))
975 (setq n (point)))
976 (setq times
977 (cond
978 ((< (count-lines (point) n) times) -1) ; Not enough lines.
979 ((or (null (re-search-forward regexp nil t back))
980 (if back (and (< (match-end 0) n)
981 (> (count-lines (match-end 0) n) 1))
982 (and (< n (match-beginning 0))
983 (> (count-lines n (match-beginning 0)) 1))))
984 0) ; No match within lines.
985 (back (count-lines (max n (match-beginning 0)) (match-end 0)))
986 (t (count-lines (match-beginning 0) (min n (match-end 0))))))
987 (goto-char n))
988 (and (zerop times) (looking-at "^.*$"))))
989
990
991 (provide 'view)
992
993 ;;; view.el ends here