Update copyright and maintainer.
[emacs.git] / lisp / view.el
blobb17cd52ae35b19e276955345408364e249f3d7d0
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)
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 (view-page-size-default view-page-size)
457 view-half-page-size (or view-half-page-size (/ (view-window-size) 2))
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) view-half-page-size
679 (setq view-half-page-size
680 (if (zerop (setq lines (prefix-numeric-value lines)))
681 (/ (view-window-size) 2)
682 (view-page-size-default lines)))))
685 ;;; Commands for moving around in the buffer.
687 (defun View-goto-percent (&optional percent)
688 "Move to end (or prefix PERCENT) of buffer in View mode.
689 Display is centered at point.
690 Also set the mark at the position where point was."
691 (interactive "P")
692 (push-mark)
693 (goto-char
694 (if percent
695 (+ (point-min)
696 (floor (* (- (point-max) (point-min)) 0.01
697 (max 0 (min 100 (prefix-numeric-value percent))))))
698 (point-max)))
699 (view-recenter))
701 ;(defun View-goto-line-last (&optional line)
702 ;"Move to last (or prefix LINE) line in View mode.
703 ;Display is centered at LINE.
704 ;Sets mark at starting position and pushes mark ring."
705 ; (interactive "P")
706 ; (push-mark)
707 ; (if line (goto-line (prefix-numeric-value line))
708 ; (goto-char (point-max))
709 ; (beginning-of-line))
710 ; (view-recenter))
712 (defun View-goto-line (&optional line)
713 "Move to first (or prefix LINE) line in View mode.
714 Display is centered at LINE.
715 Also set the mark at the position where point was."
716 (interactive "p")
717 (push-mark)
718 (goto-line line)
719 (view-recenter))
721 (defun View-back-to-mark (&optional ignore)
722 "Return to last mark set in View mode, else beginning of file.
723 Display that line at the center of the window.
724 This command pops the mark ring, so that successive
725 invocations return to earlier marks."
726 (interactive)
727 (goto-char (or (mark t) (point-min)))
728 (pop-mark)
729 (view-recenter))
731 (defun view-scroll-lines (lines backward default maxdefault)
732 ;; This function does the job for all the scrolling commands.
733 ;; Scroll forward LINES lines. If BACKWARD is true scroll backwards.
734 ;; If LINES is negative scroll in the other direction. If LINES is 0 or nil,
735 ;; scroll DEFAULT lines. If MAXDEFAULT is true then scroll no more than a
736 ;; window full.
737 (if (or (null lines) (zerop (setq lines (prefix-numeric-value lines))))
738 (setq lines default))
739 (when (< lines 0)
740 (setq backward (not backward)) (setq lines (- lines)))
741 (setq default (view-page-size-default nil)) ; Max scrolled at a time.
742 (if maxdefault (setq lines (min lines default)))
743 (cond
744 (backward (scroll-down lines))
745 ((view-really-at-end)
746 (if view-scroll-auto-exit (View-quit)
747 (ding)
748 (view-end-message)))
749 (t (while (> lines default)
750 (scroll-up default)
751 (setq lines (- lines default))
752 (if (view-really-at-end) (setq lines 0)))
753 (scroll-up lines)
754 (if (view-really-at-end) (view-end-message))
755 (move-to-window-line -1)
756 (beginning-of-line))))
758 (defun view-really-at-end ()
759 ;; Return true if buffer end visible. Maybe revert buffer and test.
760 (and (pos-visible-in-window-p (point-max))
761 (let ((buf (current-buffer))
762 (bufname (buffer-name))
763 (file (buffer-file-name)))
764 (or (not view-try-extend-at-buffer-end)
765 (null file)
766 (verify-visited-file-modtime buf)
767 (not (file-exists-p file))
768 (when (buffer-modified-p buf)
769 (setq file (file-name-nondirectory file))
770 (not (yes-or-no-p
771 (format
772 "File %s changed on disk. Discard your edits%s? "
773 file
774 (if (string= bufname file) ""
775 (concat " in " bufname))))))
776 (progn
777 (revert-buffer t t t)
778 (pos-visible-in-window-p (point-max)))))))
780 (defun view-end-message ()
781 ;; Tell that we are at end of buffer.
782 (goto-char (point-max))
783 (if view-return-to-alist
784 (message "End of buffer. Type %s to quit viewing."
785 (substitute-command-keys
786 (if view-scroll-auto-exit "\\[View-scroll-page-forward]"
787 "\\[View-quit]")))
788 (message "End of buffer")))
790 (defun View-scroll-to-buffer-end ()
791 "Scroll backward or forward so that buffer end is at last line of window."
792 (interactive)
793 (let ((p (if (pos-visible-in-window-p (point-max)) (point))))
794 (goto-char (point-max))
795 (recenter -1)
796 (and p (goto-char p))))
798 (defun View-scroll-page-forward (&optional lines)
799 "Scroll \"page size\" or prefix LINES lines forward in View mode.
800 Exit if end of text is visible and `view-scroll-auto-exit' is non-nil.
801 \"page size\" is whole window full, or number of lines set by
802 \\[View-scroll-page-forward-set-page-size] or
803 \\[View-scroll-page-backward-set-page-size].
804 If LINES is more than a window-full, only the last window-full is shown."
805 (interactive "P")
806 (view-scroll-lines lines nil view-page-size nil))
808 (defun View-scroll-page-backward (&optional lines)
809 "Scroll \"page size\" or prefix LINES lines backward in View mode.
810 See also `View-scroll-page-forward'."
811 (interactive "P")
812 (view-scroll-lines lines t view-page-size nil))
814 (defun View-scroll-page-forward-set-page-size (&optional lines)
815 "Scroll forward LINES lines in View mode, setting the \"page size\".
816 This is the number of lines which \\[View-scroll-page-forward] and
817 \\[View-scroll-page-backward] scroll by default.
818 If LINES is omitted or = 0, sets \"page size\" to window height and
819 scrolls forward that much, otherwise scrolls forward LINES lines and sets
820 \"page size\" to the minimum of window height and the absolute value of LINES.
821 See also `View-scroll-page-forward'."
822 (interactive "P")
823 (view-scroll-lines lines nil
824 (setq view-page-size (view-page-size-default lines))
825 nil))
827 (defun View-scroll-page-backward-set-page-size (&optional lines)
828 "Scroll backward prefix LINES lines in View mode, setting the \"page size\".
829 See also `View-scroll-page-forward-set-page-size'."
830 (interactive "P")
831 (view-scroll-lines lines t
832 (setq view-page-size (view-page-size-default lines))
833 nil))
835 (defun View-scroll-line-forward (&optional lines)
836 "Scroll forward one line (or prefix LINES lines) in View mode.
837 See also `View-scroll-page-forward,' but note that scrolling is limited
838 to minimum of LINES and one window-full."
839 (interactive "P")
840 (view-scroll-lines lines nil 1 t))
842 (defun View-scroll-line-backward (&optional lines)
843 "Scroll backward one line (or prefix LINES lines) in View mode.
844 See also `View-scroll-line-forward'."
845 (interactive "P")
846 (view-scroll-lines lines t 1 t))
848 (defun View-scroll-half-page-forward (&optional lines)
849 "Scroll forward a \"half page\" (or prefix LINES) lines in View mode.
850 If LINES is not omitted, the \"half page size\" is set to the minimum of
851 window height and the absolute value of LINES.
852 LINES=0 resets \"half page size\" to half window height."
853 (interactive "P")
854 (view-scroll-lines lines nil (view-set-half-page-size-default lines) t))
856 (defun View-scroll-half-page-backward (&optional lines)
857 "Scroll backward a \"half page\" (or prefix LINES) lines in View mode.
858 See also `View-scroll-half-page-forward'."
859 (interactive "P")
860 (view-scroll-lines lines t (view-set-half-page-size-default lines) t))
862 (defun View-revert-buffer-scroll-page-forward (&optional lines)
863 "Scroll forward, reverting buffer if needed, in View mode.
864 If buffer has not been changed and the corresponding file is newer, first
865 revert the buffer, then scroll.
866 This command is useful if you are viewing a changing file.
868 The prefix argument LINES says how many lines to scroll.
869 If you don't specify a prefix argument, it uses the number of lines set by
870 \\[View-scroll-page-forward-set-page-size] or
871 \\[View-scroll-page-backward-set-page-size].
872 If LINES is more than a window-full, only the last window-full is shown."
873 (interactive "P")
874 (let ((view-scroll-auto-exit nil)
875 (view-try-extend-at-buffer-end t))
876 (view-scroll-lines lines nil view-page-size nil)))
878 (defun View-search-regexp-forward (n regexp)
879 "Search forward for first (or prefix Nth) occurrence of REGEXP in View mode.
881 Displays line found at center of window. Sets mark at starting position and
882 pushes mark ring.
884 Characters @ and ! are special at the beginning of REGEXP. They modify
885 the search rather than become part of the pattern searched for.
886 @ means search all the buffer i.e. start search at the beginning of buffer.
887 ! means search for a line that contains no match for the pattern.
888 If REGEXP is empty or only consist of these control characters, then
889 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
890 for use by later search commands.
892 The variable `view-highlight-face' controls the face that is used
893 for highlighting the match that is found."
894 (interactive "p\nsSearch forward (regexp): ")
895 (view-search n regexp))
897 (defun View-search-regexp-backward (n regexp)
898 "Search backward for first (or prefix Nth) occurrence of REGEXP in View mode.
900 Displays line found at center of window. Sets mark at starting position and
901 pushes mark ring.
903 Characters @ and ! are special at the beginning of REGEXP. They modify
904 the search rather than become part of the pattern searched for.
905 @ means search all the buffer i.e. start search at the end of buffer.
906 ! means search for a line that contains no match for the pattern.
907 If REGEXP is empty or only consist of these control characters, then
908 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
909 for use by later search commands.
911 The variable `view-highlight-face' controls the face that is used
912 for highlighting the match that is found."
913 (interactive "p\nsSearch backward (regexp): ")
914 (view-search (- n) regexp))
916 (defun View-search-last-regexp-forward (n) "\
917 Search forward 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-last-regexp-backward (n) "\
927 Search backward for first (or prefix Nth) instance of last regexp in View mode.
928 Displays line found at center of window. Sets mark at starting position and
929 pushes mark ring.
931 The variable `view-highlight-face' controls the face that is used
932 for highlighting the match that is found."
933 (interactive "p")
934 (view-search (- n) nil))
936 (defun view-search (times regexp)
937 ;; This function does the job for all the View-search- commands.
938 ;; Search for the TIMESt match for REGEXP. If TIMES is negative
939 ;; search backwards. If REGEXP is nil use `view-last-regexp'.
940 ;; Charcters "!" and "@" have a special meaning at the beginning of
941 ;; REGEXP and are removed from REGEXP before the search "!" means
942 ;; search for lines with no match for REGEXP. "@" means search in
943 ;; the whole buffer, don't start searching from the present point.
944 (let (where no end ln)
945 (cond
946 ((and regexp (> (length regexp) 0)
947 (or (not (memq (string-to-char regexp) '(?! ?@)))
948 (progn
949 (if (member (substring regexp 0 2) '("!@" "@!"))
950 (setq end t no t ln 2)
951 (setq no (not (setq end (eq ?@ (string-to-char regexp))))
952 ln 1))
953 (> (length (setq regexp (substring regexp ln))) 0))))
954 (setq view-last-regexp (if no (list regexp) regexp)))
955 ((consp view-last-regexp)
956 (setq regexp (car view-last-regexp))
957 (unless (setq no (not no)) (setq view-last-regexp regexp)))
958 (view-last-regexp (setq regexp view-last-regexp)
959 (if no (setq view-last-regexp (list regexp))))
960 (t (error "No previous View-mode search")))
961 (save-excursion
962 (if end (goto-char (if (< times 0) (point-max) (point-min)))
963 (move-to-window-line (if (< times 0) 0 -1)))
964 (if (if no (view-search-no-match-lines times regexp)
965 (re-search-forward regexp nil t times))
966 (setq where (point))))
967 (if where
968 (progn
969 (push-mark)
970 (goto-char where)
971 (if view-overlay
972 (move-overlay view-overlay (match-beginning 0) (match-end 0))
973 (setq view-overlay
974 (make-overlay (match-beginning 0) (match-end 0))))
975 (overlay-put view-overlay 'face view-highlight-face)
976 (beginning-of-line)
977 (view-recenter))
978 (message "Can't find occurrence %d of %s%s"
979 times (if no "no " "") regexp)
980 (sit-for 4))))
982 (defun view-search-no-match-lines (times regexp)
983 ;; Search for the TIMESt occurrence of line with no match for REGEXP.
984 (let ((back (and (< times 0) (setq times (- times)) -1))
986 (while (> times 0)
987 (save-excursion (beginning-of-line (if back (- times) (1+ times)))
988 (setq n (point)))
989 (setq times
990 (cond
991 ((< (count-lines (point) n) times) -1) ; Not enough lines.
992 ((or (null (re-search-forward regexp nil t back))
993 (if back (and (< (match-end 0) n)
994 (> (count-lines (match-end 0) n) 1))
995 (and (< n (match-beginning 0))
996 (> (count-lines n (match-beginning 0)) 1))))
997 0) ; No match within lines.
998 (back (count-lines (max n (match-beginning 0)) (match-end 0)))
999 (t (count-lines (match-beginning 0) (min n (match-end 0))))))
1000 (goto-char n))
1001 (and (zerop times) (looking-at "^.*$"))))
1004 (provide 'view)
1006 ;;; arch-tag: 6d0ace36-1d12-4de3-8de3-1fa3231636d7
1007 ;;; view.el ends here