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