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