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