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