(elint-check-defcustom-form): Don't use `evenp' so we don't implicitly
[emacs.git] / lisp / view.el
blob1ee6014c73a12e7e7afa508f28b850b9097b145d
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 (unless (file-exists-p file) (error "%s does not exist" file))
246 (let ((had-a-buf (get-file-buffer file))
247 (buffer (find-file-noselect file)))
248 (if (eq (with-current-buffer buffer
249 (get major-mode 'mode-class))
250 'special)
251 (progn
252 (switch-to-buffer buffer)
253 (message "Not using View mode because the major mode is special"))
254 (view-buffer buffer (and (not had-a-buf) 'kill-buffer)))))
256 ;;;###autoload
257 (defun view-file-other-window (file)
258 "View FILE in View mode in another window.
259 Return that window to its previous buffer when done.
260 Emacs commands editing the buffer contents are not available; instead,
261 a special set of commands (mostly letters and punctuation)
262 are defined for moving around in the buffer.
263 Space scrolls forward, Delete scrolls backward.
264 For list of all View commands, type H or h while viewing.
266 This command runs the normal hook `view-mode-hook'."
267 (interactive "fIn other window view file: ")
268 (unless (file-exists-p file) (error "%s does not exist" file))
269 (let ((had-a-buf (get-file-buffer file)))
270 (view-buffer-other-window (find-file-noselect file) nil
271 (and (not had-a-buf) 'kill-buffer))))
273 ;;;###autoload
274 (defun view-file-other-frame (file)
275 "View FILE in View mode in another frame.
276 Maybe delete other frame and/or return to previous buffer when done.
277 Emacs commands editing the buffer contents are not available; instead,
278 a special set of commands (mostly letters and punctuation)
279 are defined for moving around in the buffer.
280 Space scrolls forward, Delete scrolls backward.
281 For list of all View commands, type H or h while viewing.
283 This command runs the normal hook `view-mode-hook'."
284 (interactive "fIn other frame view file: ")
285 (unless (file-exists-p file) (error "%s does not exist" file))
286 (let ((had-a-buf (get-file-buffer file)))
287 (view-buffer-other-frame (find-file-noselect file) nil
288 (and (not had-a-buf) 'kill-buffer))))
291 ;;;###autoload
292 (defun view-buffer (buffer &optional exit-action)
293 "View BUFFER in View mode, returning to previous buffer when done.
294 Emacs commands editing the buffer contents are not available; instead,
295 a special set of commands (mostly letters and punctuation)
296 are defined for moving around in the buffer.
297 Space scrolls forward, Delete scrolls backward.
298 For list of all View commands, type H or h while viewing.
300 This command runs the normal hook `view-mode-hook'.
302 Optional argument EXIT-ACTION is either nil or a function with buffer as
303 argument. This function is called when finished viewing buffer.
304 Use this argument instead of explicitly setting `view-exit-action'."
306 (interactive "bView buffer: ")
307 (let ((undo-window (list (window-buffer) (window-start) (window-point))))
308 (switch-to-buffer buffer)
309 (view-mode-enter (cons (selected-window) (cons nil undo-window))
310 exit-action)))
312 ;;;###autoload
313 (defun view-buffer-other-window (buffer &optional not-return exit-action)
314 "View BUFFER in View mode in another window.
315 Return to previous buffer when done, unless optional NOT-RETURN is non-nil.
316 Emacs commands editing the buffer contents are not available; instead,
317 a special set of commands (mostly letters and punctuation)
318 are defined for moving around in the buffer.
319 Space scrolls forward, Delete scrolls backward.
320 For list of all View commands, type H or h while viewing.
322 This command runs the normal hook `view-mode-hook'.
324 Optional argument EXIT-ACTION is either nil or a function with buffer as
325 argument. This function is called when finished viewing buffer.
326 Use this argument instead of explicitly setting `view-exit-action'."
327 (interactive "bIn other window view buffer:\nP")
328 (let* ((win ; This window will be selected by
329 (get-lru-window)) ; switch-to-buffer-other-window below.
330 (return-to
331 (and (not not-return)
332 (cons (selected-window)
333 (if (eq win (selected-window))
334 t ; Has to make new window.
335 (list
336 (window-buffer win) ; Other windows old buffer.
337 (window-start win)
338 (window-point win)))))))
339 (switch-to-buffer-other-window buffer)
340 (view-mode-enter (and return-to (cons (selected-window) return-to))
341 exit-action)))
343 ;;;###autoload
344 (defun view-buffer-other-frame (buffer &optional not-return exit-action)
345 "View BUFFER in View mode in another frame.
346 Return to previous buffer when done, unless optional NOT-RETURN is non-nil.
347 Emacs commands editing the buffer contents are not available; instead,
348 a special set of commands (mostly letters and punctuation)
349 are defined for moving around in the buffer.
350 Space scrolls forward, Delete scrolls backward.
351 For list of all View commands, type H or h while viewing.
353 This command runs the normal hook `view-mode-hook'.
355 Optional argument EXIT-ACTION is either nil or a function with buffer as
356 argument. This function is called when finished viewing buffer.
357 Use this argument instead of explicitly setting `view-exit-action'."
358 (interactive "bView buffer in other frame: \nP")
359 (let ((return-to
360 (and (not not-return) (cons (selected-window) t)))) ; Old window.
361 (switch-to-buffer-other-frame buffer)
362 (view-mode-enter (and return-to (cons (selected-window) return-to))
363 exit-action)))
365 ;;;###autoload
366 (defun view-mode (&optional arg)
367 ;; In the following documentation string we have to use some explicit key
368 ;; bindings instead of using the \\[] construction. The reason for this
369 ;; is that most commands have more than one key binding.
370 "Toggle View mode, a minor mode for viewing text but not editing it.
371 With ARG, turn View mode on iff ARG is positive.
373 Emacs commands that do not change the buffer contents are available as usual.
374 Kill commands insert text in kill buffers but do not delete. Other commands
375 \(among them most letters and punctuation) beep and tell that the buffer is
376 read-only.
377 \\<view-mode-map>
378 The following additional commands are provided. Most commands take prefix
379 arguments. Page commands default to \"page size\" lines which is almost a whole
380 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
381 and set \"half page size\" lines which initially is half a window full. Search
382 commands default to a repeat count of one.
384 H, h, ? This message.
385 Digits provide prefix arguments.
386 \\[negative-argument] negative prefix argument.
387 \\[beginning-of-buffer] move to the beginning of buffer.
388 > move to the end of buffer.
389 \\[View-scroll-to-buffer-end] scroll so that buffer end is at last line of window.
390 SPC scroll forward \"page size\" lines.
391 With prefix scroll forward prefix lines.
392 DEL scroll backward \"page size\" lines.
393 With prefix scroll backward prefix lines.
394 \\[View-scroll-page-forward-set-page-size] like \\[View-scroll-page-forward] but with prefix sets \"page size\" to prefix.
395 \\[View-scroll-page-backward-set-page-size] like \\[View-scroll-page-backward] but with prefix sets \"page size\" to prefix.
396 \\[View-scroll-half-page-forward] scroll forward \"half page size\" lines. With prefix, sets
397 \"half page size\" to prefix lines and scrolls forward that much.
398 \\[View-scroll-half-page-backward] scroll backward \"half page size\" lines. With prefix, sets
399 \"half page size\" to prefix lines and scrolls backward that much.
400 RET, LFD scroll forward one line. With prefix scroll forward prefix line(s).
401 y scroll backward one line. With prefix scroll backward prefix line(s).
402 \\[View-revert-buffer-scroll-page-forward] revert-buffer if necessary and scroll forward.
403 Use this to view a changing file.
404 \\[what-line] prints the current line number.
405 \\[View-goto-percent] goes prefix argument (default 100) percent into buffer.
406 \\[View-goto-line] goes to line given by prefix argument (default first line).
407 . set the mark.
408 x exchanges point and mark.
409 \\[View-back-to-mark] return to mark and pops mark ring.
410 Mark ring is pushed at start of every successful search and when
411 jump to line occurs. The mark is set on jump to buffer start or end.
412 \\[point-to-register] save current position in character register.
413 ' go to position saved in character register.
414 s do forward incremental search.
415 r do reverse incremental search.
416 \\[View-search-regexp-forward] searches forward for regular expression, starting after current page.
417 ! and @ have a special meaning at the beginning of the regexp.
418 ! means search for a line with no match for regexp. @ means start
419 search at beginning (end for backward search) of buffer.
420 \\ searches backward for regular expression, starting before current page.
421 \\[View-search-last-regexp-forward] searches forward for last regular expression.
422 p searches backward for last regular expression.
423 \\[View-quit] quit View mode, restoring this window and buffer to previous state.
424 \\[View-quit] is the normal way to leave view mode.
425 \\[View-exit] exit View mode but stay in current buffer. Use this if you started
426 viewing a buffer (file) and find out you want to edit it.
427 This command restores the previous read-only status of the buffer.
428 \\[View-exit-and-edit] exit View mode, and make the current buffer editable
429 even if it was not editable before entry to View mode.
430 \\[View-quit-all] quit View mode, restoring all windows to previous state.
431 \\[View-leave] quit View mode and maybe switch buffers, but don't kill this buffer.
432 \\[View-kill-and-leave] quit View mode, kill current buffer and go back to other buffer.
434 The effect of \\[View-leave] , \\[View-quit] and \\[View-kill-and-leave] depends on how view-mode was entered. If it was
435 entered by view-file, view-file-other-window or view-file-other-frame
436 \(\\[view-file], \\[view-file-other-window], \\[view-file-other-frame] or the dired mode v command), then \\[View-quit] will
437 try to kill the current buffer. If view-mode was entered from another buffer
438 as is done by View-buffer, View-buffer-other-window, View-buffer-other frame,
439 View-file, View-file-other-window or View-file-other-frame then \\[View-leave] , \\[View-quit] and \\[View-kill-and-leave]
440 will return to that buffer.
442 Entry to view-mode runs the normal hook `view-mode-hook'."
443 (interactive "P")
444 (unless (and arg ; Do nothing if already OK.
445 (if (> (prefix-numeric-value arg) 0) view-mode (not view-mode)))
446 (if view-mode (view-mode-disable)
447 (view-mode-enable))))
449 (defun view-mode-enable ()
450 "Turn on View mode."
451 ;; Always leave view mode before changing major mode.
452 ;; This is to guarantee that the buffer-read-only variable is restored.
453 (add-hook 'change-major-mode-hook 'view-mode-disable nil t)
454 (setq view-mode t
455 view-page-size (view-page-size-default view-page-size)
456 view-half-page-size (or view-half-page-size (/ (view-window-size) 2))
457 view-old-buffer-read-only buffer-read-only
458 buffer-read-only t
459 view-old-Helper-return-blurb (and (boundp 'Helper-return-blurb)
460 Helper-return-blurb)
461 Helper-return-blurb
462 (format "continue viewing %s"
463 (if (buffer-file-name)
464 (file-name-nondirectory (buffer-file-name))
465 (buffer-name))))
466 (force-mode-line-update)
467 (run-hooks 'view-mode-hook))
469 (defun view-mode-disable ()
470 "Turn off View mode."
471 (remove-hook 'change-major-mode-hook 'view-mode-disable t)
472 (and view-overlay (delete-overlay view-overlay))
473 (force-mode-line-update)
474 ;; Calling toggle-read-only while View mode is enabled
475 ;; sets view-read-only to t as a buffer-local variable
476 ;; after exiting View mode. That arranges that the next toggle-read-only
477 ;; will reenable View mode.
478 ;; Cancelling View mode in any other way should cancel that, too,
479 ;; so that View mode stays off if toggle-read-only is called.
480 (if (local-variable-p 'view-read-only)
481 (kill-local-variable 'view-read-only))
482 (setq view-mode nil
483 Helper-return-blurb view-old-Helper-return-blurb)
484 (if buffer-read-only
485 (setq buffer-read-only view-old-buffer-read-only)))
487 ;;;###autoload
488 (defun view-mode-enter (&optional return-to exit-action) "\
489 Enter View mode and set up exit from view mode depending on optional arguments.
490 If RETURN-TO is non-nil it is added as an element to the buffer local alist
491 `view-return-to-alist'.
492 Save EXIT-ACTION in buffer local variable `view-exit-action'.
493 It should be either nil or a function that takes a buffer as argument.
494 This function will be called by `view-mode-exit'.
496 RETURN-TO is either nil, meaning do nothing when exiting view mode, or
497 it has the format (WINDOW OLD-WINDOW . OLD-BUF-INFO).
498 WINDOW is a window used for viewing.
499 OLD-WINDOW is nil or the window to select after viewing.
500 OLD-BUF-INFO tells what to do with WINDOW when exiting. It is one of:
501 1) nil Do nothing.
502 2) t Delete WINDOW or, if it is the only window, its frame.
503 3) (OLD-BUFF START POINT) Display buffer OLD-BUFF with displayed text
504 starting at START and point at POINT in WINDOW.
505 4) quit-window Do `quit-window' in WINDOW.
507 For list of all View commands, type H or h while viewing.
509 This function runs the normal hook `view-mode-hook'."
510 (if return-to
511 (let ((entry (assq (car return-to) view-return-to-alist)))
512 (if entry (setcdr entry (cdr return-to))
513 (setq view-return-to-alist (cons return-to view-return-to-alist)))))
514 (if exit-action (setq view-exit-action exit-action))
515 (unless view-mode ; Do nothing if already in view mode.
516 (view-mode-enable)
517 (force-mode-line-update)
518 (message "%s"
519 (substitute-command-keys "\
520 View mode: type \\[help-command] for help, \\[describe-mode] for commands, \\[View-quit] to quit."))))
522 (defun view-mode-exit (&optional return-to-alist exit-action all-win)
523 "Exit View mode in various ways, depending on optional arguments.
524 RETURN-TO-ALIST, EXIT-ACTION and ALL-WIN determine what to do after exit.
525 EXIT-ACTION is nil or a function that is called with current buffer as
526 argument.
527 RETURN-TO-ALIST is an alist that for some of the windows displaying the
528 current buffer, associate information on what to do with those windows.
529 If ALL-WIN or the variable `view-exits-all-viewing-windows' is non-nil,
530 then all windows on RETURN-TO-ALIST are restored to their old state.
531 Otherwise only the selected window is affected (if it is on RETURN-TO-ALIST).
533 Elements of RETURN-TO-ALIST have the format (WINDOW OLD-WINDOW . OLD-BUF-INFO).
534 WINDOW is a window displaying the current buffer.
535 OLD-WINDOW is nil or a window to select after viewing.
536 OLD-BUF-INFO is information on what to do with WINDOW and is one of:
537 1) nil Do nothing.
538 2) t Delete WINDOW and, if it is the only window, its frame.
539 3) (OLD-BUF START POINT) Display buffer OLD-BUF with displayed text
540 starting at START and point at POINT in WINDOW.
541 4) quit-window Do `quit-window' in WINDOW.
543 If one of the WINDOW in RETURN-TO-ALIST is the selected window and the
544 corresponding OLD-WINDOW is a live window, then select OLD-WINDOW."
545 (setq all-win
546 (and return-to-alist (or all-win view-exits-all-viewing-windows)))
547 (if view-mode ; Only do something if in view mode.
548 (let* ((buffer (current-buffer))
549 window notlost
550 (sel-old (assq (selected-window) return-to-alist))
551 (alist (cond
552 (all-win ; Try to restore all windows.
553 (append return-to-alist nil)) ; Copy.
554 (sel-old ; Only selected window.
555 (list sel-old))))
556 (old-window (if sel-old (car (cdr sel-old)))))
557 (if all-win ; Follow chains of old-windows.
558 (let ((c (length alist)) a)
559 (while (and (> c 0) ; Safety if mutually refering windows.
560 (or (not (window-live-p old-window))
561 (eq buffer (window-buffer old-window)))
562 (setq a (assq old-window alist)))
563 (setq c (1- c))
564 (setq old-window (car (cdr a))))
565 (if (or (zerop c) (not (window-live-p old-window)))
566 (setq old-window (selected-window)))))
567 (or view-no-disable-on-exit
568 (view-mode-disable))
569 (while alist ; Restore windows with info.
570 (setq notlost nil)
571 (if (and (window-live-p (setq window (car (car alist))))
572 (eq buffer (window-buffer window)))
573 (let ((frame (window-frame window))
574 (old-buf-info (cdr (cdr (car alist)))))
575 (if all-win (select-window window))
576 (cond
577 ((and (consp old-buf-info) ; Case 3.
578 (buffer-live-p (car old-buf-info)))
579 (set-window-buffer window (car old-buf-info)) ; old-buf
580 (set-window-start window (car (cdr old-buf-info)))
581 (set-window-point window (car (cdr (cdr old-buf-info)))))
582 ((eq old-buf-info 'quit-window)
583 (quit-window)) ; Case 4.
584 ((not (eq old-buf-info t)) nil) ; Not case 2, do nothing.
585 ((not (one-window-p t)) (delete-window))
586 ((not (eq frame (next-frame)))
587 ;; Not the only frame, so can safely be removed.
588 (if view-remove-frame-by-deleting
589 (delete-frame frame)
590 (setq notlost t) ; Keep the window. See below.
591 (iconify-frame frame))))))
592 ;; If a frame is removed by iconifying it, then the window is not
593 ;; really lost. In this case we keep the entry in
594 ;; view-return-to-alist so that if the user deiconifies the frame
595 ;; and then press q, then the frame is iconified again.
596 (unless notlost
597 (setq view-return-to-alist
598 (delete (car alist) view-return-to-alist)))
599 (setq alist (cdr alist)))
600 (if (window-live-p old-window) ; still existing window
601 (select-window old-window))
602 (when exit-action
603 (setq view-exit-action nil)
604 (funcall exit-action buffer))
605 (force-mode-line-update))))
607 (defun View-exit ()
608 "Exit View mode but stay in current buffer."
609 (interactive)
610 (view-mode-exit))
612 ;;;###autoload
613 (defun View-exit-and-edit ()
614 "Exit View mode and make the current buffer editable."
615 (interactive)
616 (let ((view-old-buffer-read-only nil)
617 (view-no-disable-on-exit nil))
618 (view-mode-exit)))
620 (defun View-leave ()
621 "Quit View mode and maybe switch buffers, but don't kill this buffer."
622 (interactive)
623 (view-mode-exit view-return-to-alist))
625 (defun View-quit ()
626 "Quit View mode, trying to restore window and buffer to previous state.
627 Maybe kill this buffer. Try to restore selected window to previous state
628 and go to previous buffer or window."
629 (interactive)
630 (view-mode-exit view-return-to-alist view-exit-action))
632 (defun View-quit-all ()
633 "Quit View mode, trying to restore windows and buffers to previous state.
634 Maybe kill current buffer. Try to restore all windows viewing buffer to
635 previous state and go to previous buffer or window."
636 (interactive)
637 (view-mode-exit view-return-to-alist view-exit-action t))
639 (defun View-kill-and-leave ()
640 "Quit View mode, kill current buffer and return to previous buffer."
641 (interactive)
642 (view-mode-exit view-return-to-alist (or view-exit-action 'kill-buffer) t))
645 ;;; Some help routines.
647 (defun view-window-size ()
648 ;; Window height excluding mode line.
649 (1- (window-height)))
651 ;(defun view-last-command (&optional who what)
652 ; (setq view-last-command-entry this-command)
653 ; (setq view-last-command who)
654 ; (setq view-last-command-argument what))
656 ;(defun View-repeat-last-command ()
657 ; "Repeat last command issued in View mode."
658 ; (interactive)
659 ; (if (and view-last-command
660 ; (eq view-last-command-entry last-command))
661 ; (funcall view-last-command view-last-command-argument))
662 ; (setq this-command view-last-command-entry))
664 (defun view-recenter ()
665 ;; Center point in window.
666 (recenter (/ (view-window-size) 2)))
668 (defun view-page-size-default (lines)
669 ;; Get page size.
670 (let ((default (- (view-window-size) next-screen-context-lines)))
671 (if (or (null lines) (zerop (setq lines (prefix-numeric-value lines))))
672 default
673 (min (abs lines) default))))
675 (defun view-set-half-page-size-default (lines)
676 ;; Get and maybe set half page size.
677 (if (not lines) view-half-page-size
678 (setq view-half-page-size
679 (if (zerop (setq lines (prefix-numeric-value lines)))
680 (/ (view-window-size) 2)
681 (view-page-size-default lines)))))
684 ;;; Commands for moving around in the buffer.
686 (defun View-goto-percent (&optional percent)
687 "Move to end (or prefix PERCENT) of buffer in View mode.
688 Display is centered at point.
689 Also set the mark at the position where point was."
690 (interactive "P")
691 (push-mark)
692 (goto-char
693 (if percent
694 (+ (point-min)
695 (floor (* (- (point-max) (point-min)) 0.01
696 (max 0 (min 100 (prefix-numeric-value percent))))))
697 (point-max)))
698 (view-recenter))
700 ;(defun View-goto-line-last (&optional line)
701 ;"Move to last (or prefix LINE) line in View mode.
702 ;Display is centered at LINE.
703 ;Sets mark at starting position and pushes mark ring."
704 ; (interactive "P")
705 ; (push-mark)
706 ; (if line (goto-line (prefix-numeric-value line))
707 ; (goto-char (point-max))
708 ; (beginning-of-line))
709 ; (view-recenter))
711 (defun View-goto-line (&optional line)
712 "Move to first (or prefix LINE) line in View mode.
713 Display is centered at LINE.
714 Also set the mark at the position where point was."
715 (interactive "p")
716 (push-mark)
717 (goto-line line)
718 (view-recenter))
720 (defun View-back-to-mark (&optional ignore)
721 "Return to last mark set in View mode, else beginning of file.
722 Display that line at the center of the window.
723 This command pops the mark ring, so that successive
724 invocations return to earlier marks."
725 (interactive)
726 (goto-char (or (mark t) (point-min)))
727 (pop-mark)
728 (view-recenter))
730 (defun view-scroll-lines (lines backward default maxdefault)
731 ;; This function does the job for all the scrolling commands.
732 ;; Scroll forward LINES lines. If BACKWARD is true scroll backwards.
733 ;; If LINES is negative scroll in the other direction. If LINES is 0 or nil,
734 ;; scroll DEFAULT lines. If MAXDEFAULT is true then scroll no more than a
735 ;; window full.
736 (if (or (null lines) (zerop (setq lines (prefix-numeric-value lines))))
737 (setq lines default))
738 (when (< lines 0)
739 (setq backward (not backward)) (setq lines (- lines)))
740 (setq default (view-page-size-default nil)) ; Max scrolled at a time.
741 (if maxdefault (setq lines (min lines default)))
742 (cond
743 (backward (scroll-down lines))
744 ((view-really-at-end)
745 (if view-scroll-auto-exit (View-quit)
746 (ding)
747 (view-end-message)))
748 (t (while (> lines default)
749 (scroll-up default)
750 (setq lines (- lines default))
751 (if (view-really-at-end) (setq lines 0)))
752 (scroll-up lines)
753 (if (view-really-at-end) (view-end-message))
754 (move-to-window-line -1)
755 (beginning-of-line))))
757 (defun view-really-at-end ()
758 ;; Return true if buffer end visible. Maybe revert buffer and test.
759 (and (pos-visible-in-window-p (point-max))
760 (let ((buf (current-buffer))
761 (bufname (buffer-name))
762 (file (buffer-file-name)))
763 (or (not view-try-extend-at-buffer-end)
764 (null file)
765 (verify-visited-file-modtime buf)
766 (not (file-exists-p file))
767 (when (buffer-modified-p buf)
768 (setq file (file-name-nondirectory file))
769 (not (yes-or-no-p
770 (format
771 "File %s changed on disk. Discard your edits%s? "
772 file
773 (if (string= bufname file) ""
774 (concat " in " bufname))))))
775 (progn
776 (revert-buffer t t t)
777 (pos-visible-in-window-p (point-max)))))))
779 (defun view-end-message ()
780 ;; Tell that we are at end of buffer.
781 (goto-char (point-max))
782 (if view-return-to-alist
783 (message "End of buffer. Type %s to quit viewing."
784 (substitute-command-keys
785 (if view-scroll-auto-exit "\\[View-scroll-page-forward]"
786 "\\[View-quit]")))
787 (message "End of buffer")))
789 (defun View-scroll-to-buffer-end ()
790 "Scroll backward or forward so that buffer end is at last line of window."
791 (interactive)
792 (let ((p (if (pos-visible-in-window-p (point-max)) (point))))
793 (goto-char (point-max))
794 (recenter -1)
795 (and p (goto-char p))))
797 (defun View-scroll-page-forward (&optional lines)
798 "Scroll \"page size\" or prefix LINES lines forward in View mode.
799 Exit if end of text is visible and `view-scroll-auto-exit' is non-nil.
800 \"page size\" is whole window full, or number of lines set by
801 \\[View-scroll-page-forward-set-page-size] or
802 \\[View-scroll-page-backward-set-page-size].
803 If LINES is more than a window-full, only the last window-full is shown."
804 (interactive "P")
805 (view-scroll-lines lines nil view-page-size nil))
807 (defun View-scroll-page-backward (&optional lines)
808 "Scroll \"page size\" or prefix LINES lines backward in View mode.
809 See also `View-scroll-page-forward'."
810 (interactive "P")
811 (view-scroll-lines lines t view-page-size nil))
813 (defun View-scroll-page-forward-set-page-size (&optional lines)
814 "Scroll forward LINES lines in View mode, setting the \"page size\".
815 This is the number of lines which \\[View-scroll-page-forward] and
816 \\[View-scroll-page-backward] scroll by default.
817 If LINES is omitted or = 0, sets \"page size\" to window height and
818 scrolls forward that much, otherwise scrolls forward LINES lines and sets
819 \"page size\" to the minimum of window height and the absolute value of LINES.
820 See also `View-scroll-page-forward'."
821 (interactive "P")
822 (view-scroll-lines lines nil
823 (setq view-page-size (view-page-size-default lines))
824 nil))
826 (defun View-scroll-page-backward-set-page-size (&optional lines)
827 "Scroll backward prefix LINES lines in View mode, setting the \"page size\".
828 See also `View-scroll-page-forward-set-page-size'."
829 (interactive "P")
830 (view-scroll-lines lines t
831 (setq view-page-size (view-page-size-default lines))
832 nil))
834 (defun View-scroll-line-forward (&optional lines)
835 "Scroll forward one line (or prefix LINES lines) in View mode.
836 See also `View-scroll-page-forward,' but note that scrolling is limited
837 to minimum of LINES and one window-full."
838 (interactive "P")
839 (view-scroll-lines lines nil 1 t))
841 (defun View-scroll-line-backward (&optional lines)
842 "Scroll backward one line (or prefix LINES lines) in View mode.
843 See also `View-scroll-line-forward'."
844 (interactive "P")
845 (view-scroll-lines lines t 1 t))
847 (defun View-scroll-half-page-forward (&optional lines)
848 "Scroll forward a \"half page\" (or prefix LINES) lines in View mode.
849 If LINES is not omitted, the \"half page size\" is set to the minimum of
850 window height and the absolute value of LINES.
851 LINES=0 resets \"half page size\" to half window height."
852 (interactive "P")
853 (view-scroll-lines lines nil (view-set-half-page-size-default lines) t))
855 (defun View-scroll-half-page-backward (&optional lines)
856 "Scroll backward a \"half page\" (or prefix LINES) lines in View mode.
857 See also `View-scroll-half-page-forward'."
858 (interactive "P")
859 (view-scroll-lines lines t (view-set-half-page-size-default lines) t))
861 (defun View-revert-buffer-scroll-page-forward (&optional lines)
862 "Scroll forward, reverting buffer if needed, in View mode.
863 If buffer has not been changed and the corresponding file is newer, first
864 revert the buffer, then scroll.
865 This command is useful if you are viewing a changing file.
867 The prefix argument LINES says how many lines to scroll.
868 If you don't specify a prefix argument, it uses the number of lines set by
869 \\[View-scroll-page-forward-set-page-size] or
870 \\[View-scroll-page-backward-set-page-size].
871 If LINES is more than a window-full, only the last window-full is shown."
872 (interactive "P")
873 (let ((view-scroll-auto-exit nil)
874 (view-try-extend-at-buffer-end t))
875 (view-scroll-lines lines nil view-page-size nil)))
877 (defun View-search-regexp-forward (n regexp)
878 "Search forward for first (or prefix Nth) occurrence of REGEXP in View mode.
880 Displays line found at center of window. Sets mark at starting position and
881 pushes mark ring.
883 Characters @ and ! are special at the beginning of REGEXP. They modify
884 the search rather than become part of the pattern searched for.
885 @ means search all the buffer i.e. start search at the beginning of buffer.
886 ! means search for a line that contains no match for the pattern.
887 If REGEXP is empty or only consist of these control characters, then
888 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
889 for use by later search commands.
891 The variable `view-highlight-face' controls the face that is used
892 for highlighting the match that is found."
893 (interactive "p\nsSearch forward (regexp): ")
894 (view-search n regexp))
896 (defun View-search-regexp-backward (n regexp)
897 "Search backward for first (or prefix Nth) occurrence of REGEXP in View mode.
899 Displays line found at center of window. Sets mark at starting position and
900 pushes mark ring.
902 Characters @ and ! are special at the beginning of REGEXP. They modify
903 the search rather than become part of the pattern searched for.
904 @ means search all the buffer i.e. start search at the end of buffer.
905 ! means search for a line that contains no match for the pattern.
906 If REGEXP is empty or only consist of these control characters, then
907 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
908 for use by later search commands.
910 The variable `view-highlight-face' controls the face that is used
911 for highlighting the match that is found."
912 (interactive "p\nsSearch backward (regexp): ")
913 (view-search (- n) regexp))
915 (defun View-search-last-regexp-forward (n) "\
916 Search forward for first (or prefix Nth) instance of last regexp in View mode.
917 Displays line found at center of window. Sets mark at starting position and
918 pushes mark ring.
920 The variable `view-highlight-face' controls the face that is used
921 for highlighting the match that is found."
922 (interactive "p")
923 (view-search n nil))
925 (defun View-search-last-regexp-backward (n) "\
926 Search backward for first (or prefix Nth) instance of last regexp in View mode.
927 Displays line found at center of window. Sets mark at starting position and
928 pushes mark ring.
930 The variable `view-highlight-face' controls the face that is used
931 for highlighting the match that is found."
932 (interactive "p")
933 (view-search (- n) nil))
935 (defun view-search (times regexp)
936 ;; This function does the job for all the View-search- commands.
937 ;; Search for the TIMESt match for REGEXP. If TIMES is negative
938 ;; search backwards. If REGEXP is nil use `view-last-regexp'.
939 ;; Charcters "!" and "@" have a special meaning at the beginning of
940 ;; REGEXP and are removed from REGEXP before the search "!" means
941 ;; search for lines with no match for REGEXP. "@" means search in
942 ;; the whole buffer, don't start searching from the present point.
943 (let (where no end ln)
944 (cond
945 ((and regexp (> (length regexp) 0)
946 (or (not (memq (string-to-char regexp) '(?! ?@)))
947 (progn
948 (if (member (substring regexp 0 2) '("!@" "@!"))
949 (setq end t no t ln 2)
950 (setq no (not (setq end (eq ?@ (string-to-char regexp))))
951 ln 1))
952 (> (length (setq regexp (substring regexp ln))) 0))))
953 (setq view-last-regexp (if no (list regexp) regexp)))
954 ((consp view-last-regexp)
955 (setq regexp (car view-last-regexp))
956 (unless (setq no (not no)) (setq view-last-regexp regexp)))
957 (view-last-regexp (setq regexp view-last-regexp)
958 (if no (setq view-last-regexp (list regexp))))
959 (t (error "No previous View-mode search")))
960 (save-excursion
961 (if end (goto-char (if (< times 0) (point-max) (point-min)))
962 (move-to-window-line (if (< times 0) 0 -1)))
963 (if (if no (view-search-no-match-lines times regexp)
964 (re-search-forward regexp nil t times))
965 (setq where (point))))
966 (if where
967 (progn
968 (push-mark)
969 (goto-char where)
970 (if view-overlay
971 (move-overlay view-overlay (match-beginning 0) (match-end 0))
972 (setq view-overlay
973 (make-overlay (match-beginning 0) (match-end 0))))
974 (overlay-put view-overlay 'face view-highlight-face)
975 (beginning-of-line)
976 (view-recenter))
977 (message "Can't find occurrence %d of %s%s"
978 times (if no "no " "") regexp)
979 (sit-for 4))))
981 (defun view-search-no-match-lines (times regexp)
982 ;; Search for the TIMESt occurrence of line with no match for REGEXP.
983 (let ((back (and (< times 0) (setq times (- times)) -1))
985 (while (> times 0)
986 (save-excursion (beginning-of-line (if back (- times) (1+ times)))
987 (setq n (point)))
988 (setq times
989 (cond
990 ((< (count-lines (point) n) times) -1) ; Not enough lines.
991 ((or (null (re-search-forward regexp nil t back))
992 (if back (and (< (match-end 0) n)
993 (> (count-lines (match-end 0) n) 1))
994 (and (< n (match-beginning 0))
995 (> (count-lines n (match-beginning 0)) 1))))
996 0) ; No match within lines.
997 (back (count-lines (max n (match-beginning 0)) (match-end 0)))
998 (t (count-lines (match-beginning 0) (min n (match-end 0))))))
999 (goto-char n))
1000 (and (zerop times) (looking-at "^.*$"))))
1003 (provide 'view)
1005 ;;; arch-tag: 6d0ace36-1d12-4de3-8de3-1fa3231636d7
1006 ;;; view.el ends here