(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / view.el
blobe184d68b117874774db6abedffd25851e8c804e5
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 that means use the 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 that means use half the 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)
140 (put 'view-return-to-alist 'permanent-local t)
142 (defvar view-exit-action nil
143 "nil or a function with one argument (a buffer) called when finished viewing.
144 This is local in each buffer being viewed.
145 The \\[view-file] and \\[view-file-other-window] commands may set this to
146 `kill-buffer'.")
147 (make-variable-buffer-local 'view-exit-action)
149 (defvar view-no-disable-on-exit nil
150 "If non-nil, View mode \"exit\" commands don't actually disable View mode.
151 Instead, these commands just switch buffers or windows.
152 This is set in certain buffers by specialized features such as help commands
153 that use View mode automatically.")
155 (defvar view-overlay nil
156 "Overlay used to display where a search operation found its match.
157 This is local in each buffer, once it is used.")
158 (make-variable-buffer-local 'view-overlay)
160 (unless (assq 'view-mode minor-mode-alist)
161 (setq minor-mode-alist
162 (cons (list 'view-mode
163 (propertize " View"
164 'local-map mode-line-minor-mode-keymap
165 'help-echo "mouse-3: minor mode menu"))
166 minor-mode-alist)))
168 ;; Define keymap inside defvar to make it easier to load changes.
169 ;; Some redundant "less"-like key bindings below have been commented out.
170 (defvar view-mode-map
171 (let ((map (make-sparse-keymap)))
172 (define-key map "C" 'View-kill-and-leave)
173 (define-key map "c" 'View-leave)
174 (define-key map "Q" 'View-quit-all)
175 (define-key map "E" 'View-exit-and-edit)
176 ; (define-key map "v" 'View-exit)
177 (define-key map "e" 'View-exit)
178 (define-key map "q" 'View-quit)
179 ; (define-key map "N" 'View-search-last-regexp-backward)
180 (define-key map "p" 'View-search-last-regexp-backward)
181 (define-key map "n" 'View-search-last-regexp-forward)
182 ; (define-key map "?" 'View-search-regexp-backward) ; Less does this.
183 (define-key map "\\" 'View-search-regexp-backward)
184 (define-key map "/" 'View-search-regexp-forward)
185 (define-key map "r" 'isearch-backward)
186 (define-key map "s" 'isearch-forward)
187 (define-key map "m" 'point-to-register)
188 (define-key map "'" 'register-to-point)
189 (define-key map "x" 'exchange-point-and-mark)
190 (define-key map "@" 'View-back-to-mark)
191 (define-key map "." 'set-mark-command)
192 (define-key map "%" 'View-goto-percent)
193 ; (define-key map "G" 'View-goto-line-last)
194 (define-key map "g" 'View-goto-line)
195 (define-key map "=" 'what-line)
196 (define-key map "F" 'View-revert-buffer-scroll-page-forward)
197 ; (define-key map "k" 'View-scroll-line-backward)
198 (define-key map "y" 'View-scroll-line-backward)
199 ; (define-key map "j" 'View-scroll-line-forward)
200 (define-key map "\n" 'View-scroll-line-forward)
201 (define-key map "\r" 'View-scroll-line-forward)
202 (define-key map "u" 'View-scroll-half-page-backward)
203 (define-key map "d" 'View-scroll-half-page-forward)
204 (define-key map "z" 'View-scroll-page-forward-set-page-size)
205 (define-key map "w" 'View-scroll-page-backward-set-page-size)
206 ; (define-key map "b" 'View-scroll-page-backward)
207 (define-key map "\C-?" 'View-scroll-page-backward)
208 ; (define-key map "f" 'View-scroll-page-forward)
209 (define-key map " " 'View-scroll-page-forward)
210 (define-key map "o" 'View-scroll-to-buffer-end)
211 (define-key map ">" 'end-of-buffer)
212 (define-key map "<" 'beginning-of-buffer)
213 (define-key map "-" 'negative-argument)
214 (define-key map "9" 'digit-argument)
215 (define-key map "8" 'digit-argument)
216 (define-key map "7" 'digit-argument)
217 (define-key map "6" 'digit-argument)
218 (define-key map "5" 'digit-argument)
219 (define-key map "4" 'digit-argument)
220 (define-key map "3" 'digit-argument)
221 (define-key map "2" 'digit-argument)
222 (define-key map "1" 'digit-argument)
223 (define-key map "0" 'digit-argument)
224 (define-key map "H" 'describe-mode)
225 (define-key map "?" 'describe-mode) ; Maybe do as less instead? See above.
226 (define-key map "h" 'describe-mode)
227 map))
229 (or (assq 'view-mode minor-mode-map-alist)
230 (setq minor-mode-map-alist
231 (cons (cons 'view-mode view-mode-map) minor-mode-map-alist)))
233 ;;; Commands that enter or exit view mode.
235 ;;;###autoload
236 (defun view-file (file)
237 "View FILE in View mode, returning to previous buffer when done.
238 Emacs commands editing the buffer contents are not available; instead,
239 a special set of commands (mostly letters and punctuation)
240 are defined for moving around in the buffer.
241 Space scrolls forward, Delete scrolls backward.
242 For list of all View commands, type H or h while viewing.
244 This command runs the normal hook `view-mode-hook'."
245 (interactive "fView file: ")
246 (unless (file-exists-p file) (error "%s does not exist" file))
247 (let ((had-a-buf (get-file-buffer file))
248 (buffer (find-file-noselect file)))
249 (if (eq (with-current-buffer buffer
250 (get major-mode 'mode-class))
251 'special)
252 (progn
253 (switch-to-buffer buffer)
254 (message "Not using View mode because the major mode is special"))
255 (view-buffer buffer (and (not had-a-buf) 'kill-buffer)))))
257 ;;;###autoload
258 (defun view-file-other-window (file)
259 "View FILE in View mode in another window.
260 Return that window to its previous buffer when done.
261 Emacs commands editing the buffer contents are not available; instead,
262 a special set of commands (mostly letters and punctuation)
263 are defined for moving around in the buffer.
264 Space scrolls forward, Delete scrolls backward.
265 For list of all View commands, type H or h while viewing.
267 This command runs the normal hook `view-mode-hook'."
268 (interactive "fIn other window view file: ")
269 (unless (file-exists-p file) (error "%s does not exist" file))
270 (let ((had-a-buf (get-file-buffer file)))
271 (view-buffer-other-window (find-file-noselect file) nil
272 (and (not had-a-buf) 'kill-buffer))))
274 ;;;###autoload
275 (defun view-file-other-frame (file)
276 "View FILE in View mode in another frame.
277 Maybe delete other frame and/or return to previous buffer when done.
278 Emacs commands editing the buffer contents are not available; instead,
279 a special set of commands (mostly letters and punctuation)
280 are defined for moving around in the buffer.
281 Space scrolls forward, Delete scrolls backward.
282 For list of all View commands, type H or h while viewing.
284 This command runs the normal hook `view-mode-hook'."
285 (interactive "fIn other frame view file: ")
286 (unless (file-exists-p file) (error "%s does not exist" file))
287 (let ((had-a-buf (get-file-buffer file)))
288 (view-buffer-other-frame (find-file-noselect file) nil
289 (and (not had-a-buf) 'kill-buffer))))
292 ;;;###autoload
293 (defun view-buffer (buffer &optional exit-action)
294 "View BUFFER in View mode, returning to previous buffer when done.
295 Emacs commands editing the buffer contents are not available; instead,
296 a special set of commands (mostly letters and punctuation)
297 are defined for moving around in the buffer.
298 Space scrolls forward, Delete scrolls backward.
299 For list of all View commands, type H or h while viewing.
301 This command runs the normal hook `view-mode-hook'.
303 Optional argument EXIT-ACTION is either nil or a function with buffer as
304 argument. This function is called when finished viewing buffer.
305 Use this argument instead of explicitly setting `view-exit-action'."
307 (interactive "bView buffer: ")
308 (let ((undo-window (list (window-buffer) (window-start) (window-point))))
309 (switch-to-buffer buffer)
310 (view-mode-enter (cons (selected-window) (cons nil undo-window))
311 exit-action)))
313 ;;;###autoload
314 (defun view-buffer-other-window (buffer &optional not-return exit-action)
315 "View BUFFER in View mode in another window.
316 Return to previous buffer when done, unless optional NOT-RETURN is non-nil.
317 Emacs commands editing the buffer contents are not available; instead,
318 a special set of commands (mostly letters and punctuation)
319 are defined for moving around in the buffer.
320 Space scrolls forward, Delete scrolls backward.
321 For list of all View commands, type H or h while viewing.
323 This command runs the normal hook `view-mode-hook'.
325 Optional argument EXIT-ACTION is either nil or a function with buffer as
326 argument. This function is called when finished viewing buffer.
327 Use this argument instead of explicitly setting `view-exit-action'."
328 (interactive "bIn other window view buffer:\nP")
329 (let* ((win ; This window will be selected by
330 (get-lru-window)) ; switch-to-buffer-other-window below.
331 (return-to
332 (and (not not-return)
333 (cons (selected-window)
334 (if (eq win (selected-window))
335 t ; Has to make new window.
336 (list
337 (window-buffer win) ; Other windows old buffer.
338 (window-start win)
339 (window-point win)))))))
340 (switch-to-buffer-other-window buffer)
341 (view-mode-enter (and return-to (cons (selected-window) return-to))
342 exit-action)))
344 ;;;###autoload
345 (defun view-buffer-other-frame (buffer &optional not-return exit-action)
346 "View BUFFER in View mode in another frame.
347 Return to previous buffer when done, unless optional NOT-RETURN is non-nil.
348 Emacs commands editing the buffer contents are not available; instead,
349 a special set of commands (mostly letters and punctuation)
350 are defined for moving around in the buffer.
351 Space scrolls forward, Delete scrolls backward.
352 For list of all View commands, type H or h while viewing.
354 This command runs the normal hook `view-mode-hook'.
356 Optional argument EXIT-ACTION is either nil or a function with buffer as
357 argument. This function is called when finished viewing buffer.
358 Use this argument instead of explicitly setting `view-exit-action'."
359 (interactive "bView buffer in other frame: \nP")
360 (let ((return-to
361 (and (not not-return) (cons (selected-window) t)))) ; Old window.
362 (switch-to-buffer-other-frame buffer)
363 (view-mode-enter (and return-to (cons (selected-window) return-to))
364 exit-action)))
366 ;;;###autoload
367 (defun view-mode (&optional arg)
368 ;; In the following documentation string we have to use some explicit key
369 ;; bindings instead of using the \\[] construction. The reason for this
370 ;; is that most commands have more than one key binding.
371 "Toggle View mode, a minor mode for viewing text but not editing it.
372 With ARG, turn View mode on iff ARG is positive.
374 Emacs commands that do not change the buffer contents are available as usual.
375 Kill commands insert text in kill buffers but do not delete. Other commands
376 \(among them most letters and punctuation) beep and tell that the buffer is
377 read-only.
378 \\<view-mode-map>
379 The following additional commands are provided. Most commands take prefix
380 arguments. Page commands default to \"page size\" lines which is almost a whole
381 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
382 and set \"half page size\" lines which initially is half a window full. Search
383 commands default to a repeat count of one.
385 H, h, ? This message.
386 Digits provide prefix arguments.
387 \\[negative-argument] negative prefix argument.
388 \\[beginning-of-buffer] move to the beginning of buffer.
389 > move to the end of buffer.
390 \\[View-scroll-to-buffer-end] scroll so that buffer end is at last line of window.
391 SPC scroll forward \"page size\" lines.
392 With prefix scroll forward prefix lines.
393 DEL scroll backward \"page size\" lines.
394 With prefix scroll backward prefix lines.
395 \\[View-scroll-page-forward-set-page-size] like \\[View-scroll-page-forward] but with prefix sets \"page size\" to prefix.
396 \\[View-scroll-page-backward-set-page-size] like \\[View-scroll-page-backward] but with prefix sets \"page size\" to prefix.
397 \\[View-scroll-half-page-forward] scroll forward \"half page size\" lines. With prefix, sets
398 \"half page size\" to prefix lines and scrolls forward that much.
399 \\[View-scroll-half-page-backward] scroll backward \"half page size\" lines. With prefix, sets
400 \"half page size\" to prefix lines and scrolls backward that much.
401 RET, LFD scroll forward one line. With prefix scroll forward prefix line(s).
402 y scroll backward one line. With prefix scroll backward prefix line(s).
403 \\[View-revert-buffer-scroll-page-forward] revert-buffer if necessary and scroll forward.
404 Use this to view a changing file.
405 \\[what-line] prints the current line number.
406 \\[View-goto-percent] goes prefix argument (default 100) percent into buffer.
407 \\[View-goto-line] goes to line given by prefix argument (default first line).
408 . set the mark.
409 x exchanges point and mark.
410 \\[View-back-to-mark] return to mark and pops mark ring.
411 Mark ring is pushed at start of every successful search and when
412 jump to line occurs. The mark is set on jump to buffer start or end.
413 \\[point-to-register] save current position in character register.
414 ' go to position saved in character register.
415 s do forward incremental search.
416 r do reverse incremental search.
417 \\[View-search-regexp-forward] searches forward for regular expression, starting after current page.
418 ! and @ have a special meaning at the beginning of the regexp.
419 ! means search for a line with no match for regexp. @ means start
420 search at beginning (end for backward search) of buffer.
421 \\ searches backward for regular expression, starting before current page.
422 \\[View-search-last-regexp-forward] searches forward for last regular expression.
423 p searches backward for last regular expression.
424 \\[View-quit] quit View mode, restoring this window and buffer to previous state.
425 \\[View-quit] is the normal way to leave view mode.
426 \\[View-exit] exit View mode but stay in current buffer. Use this if you started
427 viewing a buffer (file) and find out you want to edit it.
428 This command restores the previous read-only status of the buffer.
429 \\[View-exit-and-edit] exit View mode, and make the current buffer editable
430 even if it was not editable before entry to View mode.
431 \\[View-quit-all] quit View mode, restoring all windows to previous state.
432 \\[View-leave] quit View mode and maybe switch buffers, but don't kill this buffer.
433 \\[View-kill-and-leave] quit View mode, kill current buffer and go back to other buffer.
435 The effect of \\[View-leave] , \\[View-quit] and \\[View-kill-and-leave] depends on how view-mode was entered. If it was
436 entered by view-file, view-file-other-window or view-file-other-frame
437 \(\\[view-file], \\[view-file-other-window], \\[view-file-other-frame] or the dired mode v command), then \\[View-quit] will
438 try to kill the current buffer. If view-mode was entered from another buffer
439 as is done by View-buffer, View-buffer-other-window, View-buffer-other frame,
440 View-file, View-file-other-window or View-file-other-frame then \\[View-leave] , \\[View-quit] and \\[View-kill-and-leave]
441 will return to that buffer.
443 Entry to view-mode runs the normal hook `view-mode-hook'."
444 (interactive "P")
445 (unless (and arg ; Do nothing if already OK.
446 (if (> (prefix-numeric-value arg) 0) view-mode (not view-mode)))
447 (if view-mode (view-mode-disable)
448 (view-mode-enable))))
450 (defun view-mode-enable ()
451 "Turn on View mode."
452 ;; Always leave view mode before changing major mode.
453 ;; This is to guarantee that the buffer-read-only variable is restored.
454 (add-hook 'change-major-mode-hook 'view-mode-disable nil t)
455 (setq view-mode t
456 view-page-size nil
457 view-half-page-size nil
458 view-old-buffer-read-only buffer-read-only
459 buffer-read-only t
460 view-old-Helper-return-blurb (and (boundp 'Helper-return-blurb)
461 Helper-return-blurb)
462 Helper-return-blurb
463 (format "continue viewing %s"
464 (if (buffer-file-name)
465 (file-name-nondirectory (buffer-file-name))
466 (buffer-name))))
467 (force-mode-line-update)
468 (run-hooks 'view-mode-hook))
470 (defun view-mode-disable ()
471 "Turn off View mode."
472 (remove-hook 'change-major-mode-hook 'view-mode-disable t)
473 (and view-overlay (delete-overlay view-overlay))
474 (force-mode-line-update)
475 ;; Calling toggle-read-only while View mode is enabled
476 ;; sets view-read-only to t as a buffer-local variable
477 ;; after exiting View mode. That arranges that the next toggle-read-only
478 ;; will reenable View mode.
479 ;; Cancelling View mode in any other way should cancel that, too,
480 ;; so that View mode stays off if toggle-read-only is called.
481 (if (local-variable-p 'view-read-only)
482 (kill-local-variable 'view-read-only))
483 (setq view-mode nil
484 Helper-return-blurb view-old-Helper-return-blurb)
485 (if buffer-read-only
486 (setq buffer-read-only view-old-buffer-read-only)))
488 ;;;###autoload
489 (defun view-mode-enter (&optional return-to exit-action) "\
490 Enter View mode and set up exit from view mode depending on optional arguments.
491 If RETURN-TO is non-nil it is added as an element to the buffer local alist
492 `view-return-to-alist'.
493 Save EXIT-ACTION in buffer local variable `view-exit-action'.
494 It should be either nil or a function that takes a buffer as argument.
495 This function will be called by `view-mode-exit'.
497 RETURN-TO is either nil, meaning do nothing when exiting view mode, or
498 it has the format (WINDOW OLD-WINDOW . OLD-BUF-INFO).
499 WINDOW is a window used for viewing.
500 OLD-WINDOW is nil or the window to select after viewing.
501 OLD-BUF-INFO tells what to do with WINDOW when exiting. It is one of:
502 1) nil Do nothing.
503 2) t Delete WINDOW or, if it is the only window, its frame.
504 3) (OLD-BUFF START POINT) Display buffer OLD-BUFF with displayed text
505 starting at START and point at POINT in WINDOW.
506 4) quit-window Do `quit-window' in WINDOW.
508 For list of all View commands, type H or h while viewing.
510 This function runs the normal hook `view-mode-hook'."
511 (if return-to
512 (let ((entry (assq (car return-to) view-return-to-alist)))
513 (if entry (setcdr entry (cdr return-to))
514 (setq view-return-to-alist (cons return-to view-return-to-alist)))))
515 (if exit-action (setq view-exit-action exit-action))
516 (unless view-mode ; Do nothing if already in view mode.
517 (view-mode-enable)
518 (force-mode-line-update)
519 (message "%s"
520 (substitute-command-keys "\
521 View mode: type \\[help-command] for help, \\[describe-mode] for commands, \\[View-quit] to quit."))))
523 (defun view-mode-exit (&optional return-to-alist exit-action all-win)
524 "Exit View mode in various ways, depending on optional arguments.
525 RETURN-TO-ALIST, EXIT-ACTION and ALL-WIN determine what to do after exit.
526 EXIT-ACTION is nil or a function that is called with current buffer as
527 argument.
528 RETURN-TO-ALIST is an alist that for some of the windows displaying the
529 current buffer, associate information on what to do with those windows.
530 If ALL-WIN or the variable `view-exits-all-viewing-windows' is non-nil,
531 then all windows on RETURN-TO-ALIST are restored to their old state.
532 Otherwise only the selected window is affected (if it is on RETURN-TO-ALIST).
534 Elements of RETURN-TO-ALIST have the format (WINDOW OLD-WINDOW . OLD-BUF-INFO).
535 WINDOW is a window displaying the current buffer.
536 OLD-WINDOW is nil or a window to select after viewing.
537 OLD-BUF-INFO is information on what to do with WINDOW and is one of:
538 1) nil Do nothing.
539 2) t Delete WINDOW and, if it is the only window, its frame.
540 3) (OLD-BUF START POINT) Display buffer OLD-BUF with displayed text
541 starting at START and point at POINT in WINDOW.
542 4) quit-window Do `quit-window' in WINDOW.
544 If one of the WINDOW in RETURN-TO-ALIST is the selected window and the
545 corresponding OLD-WINDOW is a live window, then select OLD-WINDOW."
546 (setq all-win
547 (and return-to-alist (or all-win view-exits-all-viewing-windows)))
548 (if view-mode ; Only do something if in view mode.
549 (let* ((buffer (current-buffer))
550 window notlost
551 (sel-old (assq (selected-window) return-to-alist))
552 (alist (cond
553 (all-win ; Try to restore all windows.
554 (append return-to-alist nil)) ; Copy.
555 (sel-old ; Only selected window.
556 (list sel-old))))
557 (old-window (if sel-old (car (cdr sel-old)))))
558 (if all-win ; Follow chains of old-windows.
559 (let ((c (length alist)) a)
560 (while (and (> c 0) ; Safety if mutually refering windows.
561 (or (not (window-live-p old-window))
562 (eq buffer (window-buffer old-window)))
563 (setq a (assq old-window alist)))
564 (setq c (1- c))
565 (setq old-window (car (cdr a))))
566 (if (or (zerop c) (not (window-live-p old-window)))
567 (setq old-window (selected-window)))))
568 (or view-no-disable-on-exit
569 (view-mode-disable))
570 (while alist ; Restore windows with info.
571 (setq notlost nil)
572 (if (and (window-live-p (setq window (car (car alist))))
573 (eq buffer (window-buffer window)))
574 (let ((frame (window-frame window))
575 (old-buf-info (cdr (cdr (car alist)))))
576 (if all-win (select-window window))
577 (cond
578 ((and (consp old-buf-info) ; Case 3.
579 (buffer-live-p (car old-buf-info)))
580 (set-window-buffer window (car old-buf-info)) ; old-buf
581 (set-window-start window (car (cdr old-buf-info)))
582 (set-window-point window (car (cdr (cdr old-buf-info)))))
583 ((eq old-buf-info 'quit-window)
584 (quit-window)) ; Case 4.
585 ((not (eq old-buf-info t)) nil) ; Not case 2, do nothing.
586 ((not (one-window-p t)) (delete-window))
587 ((not (eq frame (next-frame)))
588 ;; Not the only frame, so can safely be removed.
589 (if view-remove-frame-by-deleting
590 (delete-frame frame)
591 (setq notlost t) ; Keep the window. See below.
592 (iconify-frame frame))))))
593 ;; If a frame is removed by iconifying it, then the window is not
594 ;; really lost. In this case we keep the entry in
595 ;; view-return-to-alist so that if the user deiconifies the frame
596 ;; and then press q, then the frame is iconified again.
597 (unless notlost
598 (setq view-return-to-alist
599 (delete (car alist) view-return-to-alist)))
600 (setq alist (cdr alist)))
601 (if (window-live-p old-window) ; still existing window
602 (select-window old-window))
603 (when exit-action
604 (setq view-exit-action nil)
605 (funcall exit-action buffer))
606 (force-mode-line-update))))
608 (defun View-exit ()
609 "Exit View mode but stay in current buffer."
610 (interactive)
611 (view-mode-exit))
613 ;;;###autoload
614 (defun View-exit-and-edit ()
615 "Exit View mode and make the current buffer editable."
616 (interactive)
617 (let ((view-old-buffer-read-only nil)
618 (view-no-disable-on-exit nil))
619 (view-mode-exit)))
621 (defun View-leave ()
622 "Quit View mode and maybe switch buffers, but don't kill this buffer."
623 (interactive)
624 (view-mode-exit view-return-to-alist))
626 (defun View-quit ()
627 "Quit View mode, trying to restore window and buffer to previous state.
628 Maybe kill this buffer. Try to restore selected window to previous state
629 and go to previous buffer or window."
630 (interactive)
631 (view-mode-exit view-return-to-alist view-exit-action))
633 (defun View-quit-all ()
634 "Quit View mode, trying to restore windows and buffers to previous state.
635 Maybe kill current buffer. Try to restore all windows viewing buffer to
636 previous state and go to previous buffer or window."
637 (interactive)
638 (view-mode-exit view-return-to-alist view-exit-action t))
640 (defun View-kill-and-leave ()
641 "Quit View mode, kill current buffer and return to previous buffer."
642 (interactive)
643 (view-mode-exit view-return-to-alist (or view-exit-action 'kill-buffer) t))
646 ;;; Some help routines.
648 (defun view-window-size ()
649 ;; Window height excluding mode line.
650 (1- (window-height)))
652 ;(defun view-last-command (&optional who what)
653 ; (setq view-last-command-entry this-command)
654 ; (setq view-last-command who)
655 ; (setq view-last-command-argument what))
657 ;(defun View-repeat-last-command ()
658 ; "Repeat last command issued in View mode."
659 ; (interactive)
660 ; (if (and view-last-command
661 ; (eq view-last-command-entry last-command))
662 ; (funcall view-last-command view-last-command-argument))
663 ; (setq this-command view-last-command-entry))
665 (defun view-recenter ()
666 ;; Center point in window.
667 (recenter (/ (view-window-size) 2)))
669 (defun view-page-size-default (lines)
670 ;; Get page size.
671 (let ((default (- (view-window-size) next-screen-context-lines)))
672 (if (or (null lines) (zerop (setq lines (prefix-numeric-value lines))))
673 default
674 (min (abs lines) default))))
676 (defun view-set-half-page-size-default (lines)
677 ;; Get and maybe set half page size.
678 (if (not lines) (or view-half-page-size
679 (/ (view-window-size) 2))
680 (setq view-half-page-size
681 (if (zerop (setq lines (prefix-numeric-value lines)))
682 (/ (view-window-size) 2)
683 (view-page-size-default lines)))))
686 ;;; Commands for moving around in the buffer.
688 (defun View-goto-percent (&optional percent)
689 "Move to end (or prefix PERCENT) of buffer in View mode.
690 Display is centered at point.
691 Also set the mark at the position where point was."
692 (interactive "P")
693 (push-mark)
694 (goto-char
695 (if percent
696 (+ (point-min)
697 (floor (* (- (point-max) (point-min)) 0.01
698 (max 0 (min 100 (prefix-numeric-value percent))))))
699 (point-max)))
700 (view-recenter))
702 ;(defun View-goto-line-last (&optional line)
703 ;"Move to last (or prefix LINE) line in View mode.
704 ;Display is centered at LINE.
705 ;Sets mark at starting position and pushes mark ring."
706 ; (interactive "P")
707 ; (push-mark)
708 ; (if line (goto-line (prefix-numeric-value line))
709 ; (goto-char (point-max))
710 ; (beginning-of-line))
711 ; (view-recenter))
713 (defun View-goto-line (&optional line)
714 "Move to first (or prefix LINE) line in View mode.
715 Display is centered at LINE.
716 Also set the mark at the position where point was."
717 (interactive "p")
718 (push-mark)
719 (goto-line line)
720 (view-recenter))
722 (defun View-back-to-mark (&optional ignore)
723 "Return to last mark set in View mode, else beginning of file.
724 Display that line at the center of the window.
725 This command pops the mark ring, so that successive
726 invocations return to earlier marks."
727 (interactive)
728 (goto-char (or (mark t) (point-min)))
729 (pop-mark)
730 (view-recenter))
732 (defun view-scroll-lines (lines backward default maxdefault)
733 ;; This function does the job for all the scrolling commands.
734 ;; Scroll forward LINES lines. If BACKWARD is true scroll backwards.
735 ;; If LINES is negative scroll in the other direction. If LINES is 0 or nil,
736 ;; scroll DEFAULT lines. If MAXDEFAULT is true then scroll no more than a
737 ;; window full.
738 (if (or (null lines) (zerop (setq lines (prefix-numeric-value lines))))
739 (setq lines default))
740 (when (< lines 0)
741 (setq backward (not backward)) (setq lines (- lines)))
742 (setq default (view-page-size-default nil)) ; Max scrolled at a time.
743 (if maxdefault (setq lines (min lines default)))
744 (cond
745 (backward (scroll-down lines))
746 ((view-really-at-end)
747 (if view-scroll-auto-exit (View-quit)
748 (ding)
749 (view-end-message)))
750 (t (while (> lines default)
751 (scroll-up default)
752 (setq lines (- lines default))
753 (if (view-really-at-end) (setq lines 0)))
754 (scroll-up lines)
755 (if (view-really-at-end) (view-end-message))
756 (move-to-window-line -1)
757 (beginning-of-line))))
759 (defun view-really-at-end ()
760 ;; Return true if buffer end visible. Maybe revert buffer and test.
761 (and (pos-visible-in-window-p (point-max))
762 (let ((buf (current-buffer))
763 (bufname (buffer-name))
764 (file (buffer-file-name)))
765 (or (not view-try-extend-at-buffer-end)
766 (null file)
767 (verify-visited-file-modtime buf)
768 (not (file-exists-p file))
769 (when (buffer-modified-p buf)
770 (setq file (file-name-nondirectory file))
771 (not (yes-or-no-p
772 (format
773 "File %s changed on disk. Discard your edits%s? "
774 file
775 (if (string= bufname file) ""
776 (concat " in " bufname))))))
777 (progn
778 (revert-buffer t t t)
779 (pos-visible-in-window-p (point-max)))))))
781 (defun view-end-message ()
782 ;; Tell that we are at end of buffer.
783 (goto-char (point-max))
784 (if view-return-to-alist
785 (message "End of buffer. Type %s to quit viewing."
786 (substitute-command-keys
787 (if view-scroll-auto-exit "\\[View-scroll-page-forward]"
788 "\\[View-quit]")))
789 (message "End of buffer")))
791 (defun View-scroll-to-buffer-end ()
792 "Scroll backward or forward so that buffer end is at last line of window."
793 (interactive)
794 (let ((p (if (pos-visible-in-window-p (point-max)) (point))))
795 (goto-char (point-max))
796 (recenter -1)
797 (and p (goto-char p))))
799 (defun View-scroll-page-forward (&optional lines)
800 "Scroll \"page size\" or prefix LINES lines forward in View mode.
801 Exit if end of text is visible and `view-scroll-auto-exit' is non-nil.
802 \"page size\" is whole window full, or number of lines set by
803 \\[View-scroll-page-forward-set-page-size] or
804 \\[View-scroll-page-backward-set-page-size].
805 If LINES is more than a window-full, only the last window-full is shown."
806 (interactive "P")
807 (view-scroll-lines lines nil (view-page-size-default view-page-size) nil))
809 (defun View-scroll-page-backward (&optional lines)
810 "Scroll \"page size\" or prefix LINES lines backward in View mode.
811 See also `View-scroll-page-forward'."
812 (interactive "P")
813 (view-scroll-lines lines t (view-page-size-default view-page-size) nil))
815 (defun View-scroll-page-forward-set-page-size (&optional lines)
816 "Scroll forward LINES lines in View mode, setting the \"page size\".
817 This is the number of lines which \\[View-scroll-page-forward] and
818 \\[View-scroll-page-backward] scroll by default.
819 If LINES is omitted or = 0, sets \"page size\" to window height and
820 scrolls forward that much, otherwise scrolls forward LINES lines and sets
821 \"page size\" to the minimum of window height and the absolute value of LINES.
822 See also `View-scroll-page-forward'."
823 (interactive "P")
824 (view-scroll-lines lines nil
825 (setq view-page-size (view-page-size-default lines))
826 nil))
828 (defun View-scroll-page-backward-set-page-size (&optional lines)
829 "Scroll backward prefix LINES lines in View mode, setting the \"page size\".
830 See also `View-scroll-page-forward-set-page-size'."
831 (interactive "P")
832 (view-scroll-lines lines t
833 (setq view-page-size (view-page-size-default lines))
834 nil))
836 (defun View-scroll-line-forward (&optional lines)
837 "Scroll forward one line (or prefix LINES lines) in View mode.
838 See also `View-scroll-page-forward,' but note that scrolling is limited
839 to minimum of LINES and one window-full."
840 (interactive "P")
841 (view-scroll-lines lines nil 1 t))
843 (defun View-scroll-line-backward (&optional lines)
844 "Scroll backward one line (or prefix LINES lines) in View mode.
845 See also `View-scroll-line-forward'."
846 (interactive "P")
847 (view-scroll-lines lines t 1 t))
849 (defun View-scroll-half-page-forward (&optional lines)
850 "Scroll forward a \"half page\" (or prefix LINES) lines in View mode.
851 If LINES is not omitted, the \"half page size\" is set to the minimum of
852 window height and the absolute value of LINES.
853 LINES=0 resets \"half page size\" to half window height."
854 (interactive "P")
855 (view-scroll-lines lines nil (view-set-half-page-size-default lines) t))
857 (defun View-scroll-half-page-backward (&optional lines)
858 "Scroll backward a \"half page\" (or prefix LINES) lines in View mode.
859 See also `View-scroll-half-page-forward'."
860 (interactive "P")
861 (view-scroll-lines lines t (view-set-half-page-size-default lines) t))
863 (defun View-revert-buffer-scroll-page-forward (&optional lines)
864 "Scroll forward, reverting buffer if needed, in View mode.
865 If buffer has not been changed and the corresponding file is newer, first
866 revert the buffer, then scroll.
867 This command is useful if you are viewing a changing file.
869 The prefix argument LINES says how many lines to scroll.
870 If you don't specify a prefix argument, it uses the number of lines set by
871 \\[View-scroll-page-forward-set-page-size] or
872 \\[View-scroll-page-backward-set-page-size].
873 If LINES is more than a window-full, only the last window-full is shown."
874 (interactive "P")
875 (let ((view-scroll-auto-exit nil)
876 (view-try-extend-at-buffer-end t))
877 (view-scroll-lines lines nil view-page-size nil)))
879 (defun View-search-regexp-forward (n regexp)
880 "Search forward for first (or prefix Nth) occurrence of REGEXP in View mode.
882 Displays line found at center of window. Sets mark at starting position and
883 pushes mark ring.
885 Characters @ and ! are special at the beginning of REGEXP. They modify
886 the search rather than become part of the pattern searched for.
887 @ means search all the buffer i.e. start search at the beginning of buffer.
888 ! means search for a line that contains no match for the pattern.
889 If REGEXP is empty or only consist of these control characters, then
890 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
891 for use by later search commands.
893 The variable `view-highlight-face' controls the face that is used
894 for highlighting the match that is found."
895 (interactive "p\nsSearch forward (regexp): ")
896 (view-search n regexp))
898 (defun View-search-regexp-backward (n regexp)
899 "Search backward for first (or prefix Nth) occurrence of REGEXP in View mode.
901 Displays line found at center of window. Sets mark at starting position and
902 pushes mark ring.
904 Characters @ and ! are special at the beginning of REGEXP. They modify
905 the search rather than become part of the pattern searched for.
906 @ means search all the buffer i.e. start search at the end of buffer.
907 ! means search for a line that contains no match for the pattern.
908 If REGEXP is empty or only consist of these control characters, then
909 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
910 for use by later search commands.
912 The variable `view-highlight-face' controls the face that is used
913 for highlighting the match that is found."
914 (interactive "p\nsSearch backward (regexp): ")
915 (view-search (- n) regexp))
917 (defun View-search-last-regexp-forward (n) "\
918 Search forward for first (or prefix Nth) instance of last regexp in View mode.
919 Displays line found at center of window. Sets mark at starting position and
920 pushes mark ring.
922 The variable `view-highlight-face' controls the face that is used
923 for highlighting the match that is found."
924 (interactive "p")
925 (view-search n nil))
927 (defun View-search-last-regexp-backward (n) "\
928 Search backward for first (or prefix Nth) instance of last regexp in View mode.
929 Displays line found at center of window. Sets mark at starting position and
930 pushes mark ring.
932 The variable `view-highlight-face' controls the face that is used
933 for highlighting the match that is found."
934 (interactive "p")
935 (view-search (- n) nil))
937 (defun view-search (times regexp)
938 ;; This function does the job for all the View-search- commands.
939 ;; Search for the TIMESt match for REGEXP. If TIMES is negative
940 ;; search backwards. If REGEXP is nil use `view-last-regexp'.
941 ;; Charcters "!" and "@" have a special meaning at the beginning of
942 ;; REGEXP and are removed from REGEXP before the search "!" means
943 ;; search for lines with no match for REGEXP. "@" means search in
944 ;; the whole buffer, don't start searching from the present point.
945 (let (where no end ln)
946 (cond
947 ((and regexp (> (length regexp) 0)
948 (or (not (memq (string-to-char regexp) '(?! ?@)))
949 (progn
950 (if (member (substring regexp 0 2) '("!@" "@!"))
951 (setq end t no t ln 2)
952 (setq no (not (setq end (eq ?@ (string-to-char regexp))))
953 ln 1))
954 (> (length (setq regexp (substring regexp ln))) 0))))
955 (setq view-last-regexp (if no (list regexp) regexp)))
956 ((consp view-last-regexp)
957 (setq regexp (car view-last-regexp))
958 (unless (setq no (not no)) (setq view-last-regexp regexp)))
959 (view-last-regexp (setq regexp view-last-regexp)
960 (if no (setq view-last-regexp (list regexp))))
961 (t (error "No previous View-mode search")))
962 (save-excursion
963 (if end (goto-char (if (< times 0) (point-max) (point-min)))
964 (move-to-window-line (if (< times 0) 0 -1)))
965 (if (if no (view-search-no-match-lines times regexp)
966 (re-search-forward regexp nil t times))
967 (setq where (point))))
968 (if where
969 (progn
970 (push-mark)
971 (goto-char where)
972 (if view-overlay
973 (move-overlay view-overlay (match-beginning 0) (match-end 0))
974 (setq view-overlay
975 (make-overlay (match-beginning 0) (match-end 0))))
976 (overlay-put view-overlay 'face view-highlight-face)
977 (beginning-of-line)
978 (view-recenter))
979 (message "Can't find occurrence %d of %s%s"
980 times (if no "no " "") regexp)
981 (sit-for 4))))
983 (defun view-search-no-match-lines (times regexp)
984 ;; Search for the TIMESt occurrence of line with no match for REGEXP.
985 (let ((back (and (< times 0) (setq times (- times)) -1))
987 (while (> times 0)
988 (save-excursion (beginning-of-line (if back (- times) (1+ times)))
989 (setq n (point)))
990 (setq times
991 (cond
992 ((< (count-lines (point) n) times) -1) ; Not enough lines.
993 ((or (null (re-search-forward regexp nil t back))
994 (if back (and (< (match-end 0) n)
995 (> (count-lines (match-end 0) n) 1))
996 (and (< n (match-beginning 0))
997 (> (count-lines n (match-beginning 0)) 1))))
998 0) ; No match within lines.
999 (back (count-lines (max n (match-beginning 0)) (match-end 0)))
1000 (t (count-lines (match-beginning 0) (min n (match-end 0))))))
1001 (goto-char n))
1002 (and (zerop times) (looking-at "^.*$"))))
1005 (provide 'view)
1007 ;;; arch-tag: 6d0ace36-1d12-4de3-8de3-1fa3231636d7
1008 ;;; view.el ends here