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