]> code.delx.au - gnu-emacs/blob - lisp/view.el
(auto-raise-mode): When enabling the mode,
[gnu-emacs] / lisp / view.el
1 ;;; view.el --- peruse file or buffer without editing.
2
3 ;; Copyright (C) 1985, 1989, 1994, 1995, 1997 Free Software Foundation, Inc.
4
5 ;; Author: K. Shane Hartman
6 ;; Maintainer: Inge Frick <inge@nada.kth.se>
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
12 ;; the Free Software Foundation; either version 2, or (at your option)
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
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.
24
25 ;;; Commentary:
26
27 ;; This package provides the `view' minor mode documented in the Emacs
28 ;; user's manual.
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.
44
45 ;;; Code:
46
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)
63
64 ;; `view-mode-auto-exit' is replaced by the following option variable which
65 ;; only says if scrolling past buffer end should leave view mode or not, it
66 ;; doesn't say if leaving view mode should restore windows or not. The latter
67 ;; is now controlled by the presence of a value in `view-return-to-alist'.
68 (defcustom view-scroll-auto-exit nil
69 "*Non-nil means scrolling past the end of buffer exits View mode.
70 nil means attempting to scroll past the end of the buffer,
71 only rings the bell and gives a message on how to leave."
72 :type 'boolean
73 :group 'view)
74
75 (defcustom view-try-extend-at-buffer-end nil
76 "*Non-nil means try load more of file when reaching end of buffer.
77 This variable is mainly intended to be temporarily set to non-nil by
78 the F command in view-mode, but you can set it to t if you want the action
79 for all scroll commands in view mode."
80 :type 'boolean
81 :group 'view)
82
83 (defcustom view-remove-frame-by-deleting nil
84 "*Determine how View mode removes a frame no longer needed.
85 If nil, make an icon of the frame. If non-nil, delete the frame."
86 :type 'boolean
87 :group 'view)
88
89 (defcustom view-exits-all-viewing-windows nil
90 "*Non-nil means restore all windows used to view buffer.
91 Commands that restore windows when finished viewing a buffer, apply to all
92 windows that display the buffer and have restore information in
93 `view-return-to-alist'.
94 If `view-exits-all-viewing-windows' is nil, only the selected window is
95 considered for restoring."
96 :type 'boolean
97 :group 'view)
98
99 ;;;###autoload
100 (defvar view-mode nil
101 "Non-nil if View mode is enabled.
102 Don't change this variable directly, you must change it by one of the
103 functions that enable or disable view mode.")
104 ;;;###autoload
105 (make-variable-buffer-local 'view-mode)
106
107 (defcustom view-mode-hook nil
108 "Normal hook run when starting to view a buffer or file."
109 :type 'hook
110 :group 'view)
111
112 (defvar view-old-buffer-read-only nil)
113 (make-variable-buffer-local 'view-old-buffer-read-only)
114
115 (defvar view-old-Helper-return-blurb)
116 (make-variable-buffer-local 'view-old-Helper-return-blurb)
117
118 (defvar view-page-size nil
119 "Default number of lines to scroll by View page commands.
120 If nil then the local value of this is initially set to window size.")
121 (make-variable-buffer-local 'view-page-size)
122
123 (defvar view-half-page-size nil
124 "Default number of lines to scroll by View half page commands.
125 If nil then the local value of this is initially set to half window size.")
126 (make-variable-buffer-local 'view-half-page-size)
127
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
132 "What to do with used windows and where to go when finished viewing buffer.
133 This is local in each buffer being viewed.
134 It is added to by `view-mode-enter' when starting to view a buffer and
135 subtracted from by `view-mode-exit' when finished viewing the buffer.
136
137 See RETURN-TO-ALIST argument of function `view-mode-exit' for the format of
138 `view-return-to-alist'.")
139 (make-variable-buffer-local 'view-return-to-alist)
140
141 (defvar view-exit-action nil
142 "nil or a function with one argument (a buffer) called when finished viewing.
143 This is local in each buffer being viewed.
144 The \\[view-file] and \\[view-file-other-window] commands may set this to
145 `kill-buffer'.")
146 (make-variable-buffer-local 'view-exit-action)
147
148 (defvar view-no-disable-on-exit nil
149 "If non-nil, View mode \"exit\" commands don't actually disable View mode.
150 Instead, these commands just switch buffers or windows.
151 This is set in certain buffers by specialized features such as help commands
152 that use View mode automatically.")
153
154 (defvar view-overlay nil
155 "Overlay used to display where a search operation found its match.
156 This is local in each buffer, once it is used.")
157 (make-variable-buffer-local 'view-overlay)
158
159 (or (assq 'view-mode minor-mode-alist)
160 (setq minor-mode-alist
161 (cons '(view-mode " View") minor-mode-alist)))
162
163 ;; Define keymap inside defvar to make it easier to load changes.
164 ;; Some redundant "less"-like key bindings below have been commented out.
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))
223
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
228 ;;; Commands that enter or exit view mode.
229
230 ;;;###autoload
231 (defun view-file (file-name)
232 "View FILE in View mode, returning to previous buffer when done.
233 Emacs commands editing the buffer contents are not available; instead,
234 a special set of commands (mostly letters and punctuation)
235 are defined for moving around in the buffer.
236 Space scrolls forward, Delete scrolls backward.
237 For list of all View commands, type H or h while viewing.
238
239 This command runs the normal hook `view-mode-hook'."
240 (interactive "fView file: ")
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))))
244
245 ;;;###autoload
246 (defun view-file-other-window (file-name)
247 "View FILE in View mode in another window.
248 Return that window to its previous buffer when done.
249 Emacs commands editing the buffer contents are not available; instead,
250 a special set of commands (mostly letters and punctuation)
251 are defined for moving around in the buffer.
252 Space scrolls forward, Delete scrolls backward.
253 For list of all View commands, type H or h while viewing.
254
255 This command runs the normal hook `view-mode-hook'."
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))))
260
261 ;;;###autoload
262 (defun view-file-other-frame (file-name)
263 "View FILE in View mode in another frame.
264 Maybe delete other frame and/or return to previous buffer when done.
265 Emacs commands editing the buffer contents are not available; instead,
266 a special set of commands (mostly letters and punctuation)
267 are defined for moving around in the buffer.
268 Space scrolls forward, Delete scrolls backward.
269 For list of all View commands, type H or h while viewing.
270
271 This command runs the normal hook `view-mode-hook'."
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.
281 Emacs commands editing the buffer contents are not available; instead,
282 a special set of commands (mostly letters and punctuation)
283 are defined for moving around in the buffer.
284 Space scrolls forward, Delete scrolls backward.
285 For list of all View commands, type H or h while viewing.
286
287 This command runs the normal hook `view-mode-hook'.
288
289 Optional argument EXIT-ACTION is either nil or a function with buffer as
290 argument. This function is called when finished viewing buffer.
291 Use this argument instead of explicitly setting `view-exit-action'."
292
293 (interactive "bView buffer: ")
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)))
298
299 ;;;###autoload
300 (defun view-buffer-other-window
301 (buffer-name &optional not-return exit-action)
302 "View BUFFER in View mode in another window.
303 Return to previous buffer when done, unless optional NOT-RETURN is non-nil.
304 Emacs commands editing the buffer contents are not available; instead,
305 a special set of commands (mostly letters and punctuation)
306 are defined for moving around in the buffer.
307 Space scrolls forward, Delete scrolls backward.
308 For list of all View commands, type H or h while viewing.
309
310 This command runs the normal hook `view-mode-hook'.
311
312 Optional argument EXIT-ACTION is either nil or a function with buffer as
313 argument. This function is called when finished viewing buffer.
314 Use 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)))
330
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.
335 Return to previous buffer when done, unless optional NOT-RETURN is non-nil.
336 Emacs commands editing the buffer contents are not available; instead,
337 a special set of commands (mostly letters and punctuation)
338 are defined for moving around in the buffer.
339 Space scrolls forward, Delete scrolls backward.
340 For list of all View commands, type H or h while viewing.
341
342 This command runs the normal hook `view-mode-hook'.
343
344 Optional argument EXIT-ACTION is either nil or a function with buffer as
345 argument. This function is called when finished viewing buffer.
346 Use 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)))
353
354 ;;;###autoload
355 (defun view-mode (&optional arg)
356 ;; In the following documentation string we have to use some explicit key
357 ;; bindings instead of using the \\[] construction. The reason for this
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.
360 With arg, turn View mode on iff arg is positive.
361
362 Emacs commands that do not change the buffer contents are available as usual.
363 Kill commands insert text in kill buffers but do not delete. Other commands
364 \(among them most letters and punctuation) beep and tell that the buffer is
365 read-only.
366 \\<view-mode-map>
367 The following additional commands are provided. Most commands take prefix
368 arguments. Page commands default to \"page size\" lines which is almost a whole
369 window full, or number of lines set by \\[View-scroll-page-forward-set-page-size] or \\[View-scroll-page-backward-set-page-size]. Half page commands default to
370 and set \"half page size\" lines which initially is half a window full. Search
371 commands default to a repeat count of one.
372
373 H, h, ? This message.
374 Digits 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.
379 SPC scroll forward prefix (default \"page size\") lines.
380 DEL 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.
385 RET, LFD scroll forward prefix (default one) line(s).
386 y 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.
393 x 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
396 jump to line occurs. The mark is set on jump to buffer start or end.
397 \\[point-to-register] save current position in character register.
398 ' go to position saved in character register.
399 s do forward incremental search.
400 r 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.
403 ! means search for a line with no match for regexp. @ means start
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.
407 p 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.
410 \\[View-exit] exit View mode but stay in current buffer. Use this if you started
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
417 The effect of \\[View-leave] , \\[View-quit] and \\[View-kill-and-leave] depends on how view-mode was entered. If it was
418 entered 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
420 current buffer. If view-mode was entered from another buffer as is done by
421 View-buffer, View-buffer-other-window, View-buffer-other frame, View-file,
422 View-file-other-window or View-file-other-frame then \\[view-leave] , \\[view-quit] and \\[view-kill-and-leave] will return
423 to that buffer.
424
425 Entry to view-mode runs the normal hook `view-mode-hook'."
426 (interactive "P")
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))))
431
432 (defun view-mode-enable ()
433 "Turn on View mode."
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)
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 (force-mode-line-update)
451 (run-hooks 'view-mode-hook))
452
453 (defun view-mode-disable ()
454 "Turn off View mode."
455 (remove-hook 'change-major-mode-hook 'view-mode-disable t)
456 (and view-overlay (delete-overlay view-overlay))
457 (force-mode-line-update)
458 (setq view-mode nil
459 Helper-return-blurb view-old-Helper-return-blurb)
460 (if buffer-read-only
461 (setq buffer-read-only view-old-buffer-read-only)))
462
463 ;;;###autoload
464 (defun view-mode-enter (&optional return-to exit-action) "\
465 Enter View mode and set up exit from view mode depending on optional arguments.
466 If RETURN-TO is non-nil it is added as an element to the buffer local alist
467 `view-return-to-alist'.
468 Save EXIT-ACTION in buffer local variable `view-exit-action'.
469 It should be either nil or a function that takes a buffer as argument.
470 This function will be called by `view-mode-exit'.
471
472 RETURN-TO is either nil, meaning do nothing when exiting view mode, or
473 it has the format (WINDOW OLD-WINDOW . OLD-BUF-INFO).
474 WINDOW is a window used for viewing.
475 OLD-WINDOW is nil or the window to select after viewing.
476 OLD-BUF-INFO tells what to do with WINDOW when exiting. It is one of:
477 1) nil Do nothing.
478 2) t Delete WINDOW or, if it is the only window, its frame.
479 3) (OLD-BUFF START POINT) Display buffer OLD-BUFF with displayed text
480 starting at START and point at POINT in WINDOW.
481 4) quit-window Do quit-window in WINDOW.
482
483 For list of all View commands, type H or h while viewing.
484
485 This function runs the normal hook `view-mode-hook'."
486 (if return-to
487 (let ((entry (assq (car return-to) view-return-to-alist)))
488 (if entry (setcdr entry (cdr return-to))
489 (setq view-return-to-alist (cons return-to view-return-to-alist)))))
490 (if exit-action (setq view-exit-action exit-action))
491 (unless view-mode ; Do nothing if already in view mode.
492 (view-mode-enable)
493 (force-mode-line-update)
494 (message "%s"
495 (substitute-command-keys "\
496 Type \\[help-command] for help, \\[describe-mode] for commands, \\[View-quit] to quit."))))
497
498 (defun view-mode-exit (&optional return-to-alist exit-action all-win)
499 "Exit View mode in various ways, depending on optional arguments.
500 RETURN-TO-ALIST, EXIT-ACTION and ALL-WIN determine what to do after exit.
501 EXIT-ACTION is nil or a function that is called with current buffer as
502 argument.
503 RETURN-TO-ALIST is an alist that for some of the windows displaying the
504 current buffer, associate information on what to do with those windows.
505 If ALL-WIN or the variable `view-exits-all-viewing-windows' is non-nil,
506 then all windows on RETURN-TO-ALIST are restored to their old state.
507 Otherwise only the selected window is affected (if it is on RETURN-TO-ALIST).
508
509 Elements of RETURN-TO-ALIST have the format (WINDOW OLD-WINDOW . OLD-BUF-INFO).
510 WINDOW is a window displaying the current buffer.
511 OLD-WINDOW is nil or a window to select after viewing.
512 OLD-BUF-INFO is information on what to do with WINDOW and is one of:
513 1) nil Do nothing.
514 2) t Delete WINDOW or, if it is the only window, its frame.
515 3) (OLD-BUF START POINT) Display buffer OLD-BUF with displayed text
516 starting at START and point at POINT in WINDOW.
517 4) quit-window Do quit-window in WINDOW.
518
519 If one of the WINDOW in RETURN-TO-ALIST is the selected window and the
520 corresponding OLD-WINDOW is a live window, then select OLD-WINDOW."
521 (setq all-win
522 (and return-to-alist (or all-win view-exits-all-viewing-windows)))
523 (if view-mode ; Only do something if in view mode.
524 (let* ((buffer (current-buffer))
525 window
526 (sel-old (assq (selected-window) return-to-alist))
527 (alist (cond
528 (all-win ; Try to restore all windows.
529 (append return-to-alist nil)) ; Copy.
530 (sel-old ; Only selected window.
531 (list sel-old))))
532 (old-window (if sel-old (car (cdr sel-old)))))
533 (if all-win ; Follow chains of old-windows.
534 (let ((c (length alist)) a)
535 (while (and (> c 0) ; Safety if mutually refering windows.
536 (or (not (window-live-p old-window))
537 (eq buffer (window-buffer old-window)))
538 (setq a (assq old-window alist)))
539 (setq c (1- c))
540 (setq old-window (car (cdr a))))
541 (if (or (zerop c) (not (window-live-p old-window)))
542 (setq old-window (selected-window)))))
543 (or view-no-disable-on-exit
544 (view-mode-disable))
545 (while alist ; Restore windows with info.
546 (if (and (window-live-p (setq window (car (car alist))))
547 (eq buffer (window-buffer window)))
548 (let ((frame (window-frame window))
549 (old-buf-info (cdr (cdr (car alist)))))
550 (if all-win (select-window window))
551 (cond
552 ((and (consp old-buf-info) ; Case 3.
553 (buffer-live-p (car old-buf-info)))
554 (set-window-buffer window (car old-buf-info)) ; old-buf
555 (set-window-start window (car (cdr old-buf-info)))
556 (set-window-point window (car (cdr (cdr old-buf-info)))))
557 ((eq old-buf-info 'quit-window)
558 (quit-window)) ; Not case 2, do nothing.
559 ((not (eq old-buf-info t)) nil) ; Not case 2, do nothing.
560 ((not (one-window-p t)) (delete-window))
561 ((not (eq frame (next-frame)))
562 ;; Not the only frame, so can safely be removed.
563 (if view-remove-frame-by-deleting
564 (delete-frame frame)
565 (iconify-frame frame))))))
566 ;; Altering view-return-to-alist causes trouble when
567 ;; the user deiconifies the frame, then types q again.
568 ;; If we leave view-return-to-alist unchanged, that
569 ;; iconifies the frame again, as expected.
570 ;;; (setq view-return-to-alist (delete (car alist) view-return-to-alist))
571 (setq alist (cdr alist)))
572 (if (window-live-p old-window) ; still existing window
573 (select-window old-window))
574 (when exit-action
575 (setq view-exit-action nil)
576 (funcall exit-action buffer))
577 (force-mode-line-update))))
578
579 (defun View-exit ()
580 "Exit View mode but stay in current buffer."
581 (interactive)
582 (view-mode-exit))
583
584 ;;;###autoload
585 (defun View-exit-and-edit ()
586 "Exit View mode and make the current buffer editable."
587 (interactive)
588 (let ((view-old-buffer-read-only nil)
589 (view-no-disable-on-exit nil))
590 (view-mode-exit)))
591
592 (defun View-leave ()
593 "Quit View mode and maybe switch buffers, but don't kill this buffer."
594 (interactive)
595 (view-mode-exit view-return-to-alist))
596
597 (defun View-quit ()
598 "Quit View mode, trying to restore window and buffer to previous state.
599 Maybe kill this buffer. Try to restore selected window to previous state
600 and go to previous buffer or window."
601 (interactive)
602 (view-mode-exit view-return-to-alist view-exit-action))
603
604 (defun View-quit-all ()
605 "Quit View mode, trying to restore windows and buffers to previous state.
606 Maybe kill current buffer. Try to restore all windows viewing buffer to
607 previous state and go to previous buffer or window."
608 (interactive)
609 (view-mode-exit view-return-to-alist view-exit-action t))
610
611 (defun View-kill-and-leave ()
612 "Quit View mode, kill current buffer and return to previous buffer."
613 (interactive)
614 (view-mode-exit view-return-to-alist (or view-exit-action 'kill-buffer) t))
615
616
617 ;;; Some help routines.
618
619 (defun view-window-size ()
620 ;; Window height excluding mode line.
621 (1- (window-height)))
622
623 ;(defun view-last-command (&optional who what)
624 ; (setq view-last-command-entry this-command)
625 ; (setq view-last-command who)
626 ; (setq view-last-command-argument what))
627
628 ;(defun View-repeat-last-command ()
629 ; "Repeat last command issued in View mode."
630 ; (interactive)
631 ; (if (and view-last-command
632 ; (eq view-last-command-entry last-command))
633 ; (funcall view-last-command view-last-command-argument))
634 ; (setq this-command view-last-command-entry))
635
636 (defun view-recenter ()
637 ;; Center point in window.
638 (recenter (/ (view-window-size) 2)))
639
640 (defun view-page-size-default (lines)
641 ;; Get page size.
642 (let ((default (- (view-window-size) next-screen-context-lines)))
643 (if (or (null lines) (zerop (setq lines (prefix-numeric-value lines))))
644 default
645 (min (abs lines) default))))
646
647 (defun view-set-half-page-size-default (lines)
648 ;; Get and maybe set half page size.
649 (if (not lines) view-half-page-size
650 (setq view-half-page-size
651 (if (zerop (setq lines (prefix-numeric-value lines)))
652 (/ (view-window-size) 2)
653 (view-page-size-default lines)))))
654
655
656 ;;; Commands for moving around in the buffer.
657
658 (defun View-goto-percent (&optional percent)
659 "Move to end (or prefix PERCENT) of buffer in View mode.
660 Display is centered at point.
661 Also set the mark at the position where point was."
662 (interactive "P")
663 (push-mark)
664 (goto-char
665 (if percent
666 (+ (point-min)
667 (floor (* (- (point-max) (point-min)) 0.01
668 (max 0 (min 100 (prefix-numeric-value percent))))))
669 (point-max)))
670 (view-recenter))
671
672 ;(defun View-goto-line-last (&optional line)
673 ;"Move to last (or prefix LINE) line in View mode.
674 ;Display is centered at LINE.
675 ;Sets mark at starting position and pushes mark ring."
676 ; (interactive "P")
677 ; (push-mark)
678 ; (if line (goto-line (prefix-numeric-value line))
679 ; (goto-char (point-max))
680 ; (beginning-of-line))
681 ; (view-recenter))
682
683 (defun View-goto-line (&optional line)
684 "Move to first (or prefix LINE) line in View mode.
685 Display is centered at LINE.
686 Also set the mark at the position where point was."
687 (interactive "p")
688 (push-mark)
689 (goto-line line)
690 (view-recenter))
691
692 (defun View-scroll-to-buffer-end ()
693 "Scroll backward or forward so that buffer end is at last line of window."
694 (interactive)
695 (let ((p (if (pos-visible-in-window-p (point-max)) (point))))
696 (goto-char (point-max))
697 (recenter -1)
698 (and p (goto-char p))))
699
700 (defun view-scroll-lines (lines backward default maxdefault)
701 ;; This function does the job for all the scrolling commands.
702 ;; Scroll forward LINES lines. If BACKWARD is true scroll backwards.
703 ;; If LINES is negative scroll in the other direction. If LINES is 0 or nil,
704 ;; scroll DEFAULT lines. If MAXDEFAULT is true then scroll no more than a
705 ;; window full.
706 (if (or (null lines) (zerop (setq lines (prefix-numeric-value lines))))
707 (setq lines default))
708 (when (< lines 0)
709 (setq backward (not backward)) (setq lines (- lines)))
710 (setq default (view-page-size-default nil)) ; Max scrolled at a time.
711 (if maxdefault (setq lines (min lines default)))
712 (cond
713 (backward (scroll-down lines))
714 ((view-really-at-end)
715 (if view-scroll-auto-exit (View-quit)
716 (ding)
717 (view-end-message)))
718 (t (while (> lines default)
719 (scroll-up default)
720 (setq lines (- lines default))
721 (if (view-really-at-end) (setq lines 0)))
722 (scroll-up lines)
723 (if (view-really-at-end) (view-end-message))
724 (move-to-window-line -1)
725 (beginning-of-line))))
726
727 (defun view-really-at-end ()
728 ;; Return true if buffer end visible. Maybe revert buffer and test.
729 (and (pos-visible-in-window-p (point-max))
730 (let ((buf (current-buffer))
731 (bufname (buffer-name))
732 (file (buffer-file-name)))
733 (or (not view-try-extend-at-buffer-end)
734 (null file)
735 (verify-visited-file-modtime buf)
736 (not (file-exists-p file))
737 (when (buffer-modified-p buf)
738 (setq file (file-name-nondirectory file))
739 (not (yes-or-no-p
740 (format
741 "File %s changed on disk. Discard your edits%s? "
742 file
743 (if (string= bufname file) ""
744 (concat " in " bufname))))))
745 (progn
746 (revert-buffer t t t)
747 (pos-visible-in-window-p (point-max)))))))
748
749 (defun view-end-message ()
750 ;; Tell that we are at end of buffer.
751 (goto-char (point-max))
752 (if view-return-to-alist
753 (message "End of buffer. Type %s to quit viewing."
754 (substitute-command-keys
755 (if view-scroll-auto-exit "\\[View-scroll-page-forward]"
756 "\\[View-quit]")))
757 (message "End of buffer")))
758
759 (defun View-scroll-page-forward (&optional lines)
760 "Scroll \"page size\" or prefix LINES lines forward in View mode.
761 Exit if end of text is visible and `view-scroll-auto-exit' is non-nil.
762 \"page size\" is whole window full, or number of lines set by
763 \\[View-scroll-page-forward-set-page-size] or
764 \\[View-scroll-page-backward-set-page-size].
765 If LINES is more than a window-full, only the last window-full is shown."
766 (interactive "P")
767 (view-scroll-lines lines nil view-page-size nil))
768
769 (defun View-scroll-page-backward (&optional lines)
770 "Scroll \"page size\" or prefix LINES lines backward in View mode.
771 See also `View-scroll-page-forward'."
772 (interactive "P")
773 (view-scroll-lines lines t view-page-size nil))
774
775 (defun View-scroll-page-forward-set-page-size (&optional lines)
776 "Scroll forward LINES lines in View mode, setting the \"page size\".
777 This is the number of lines which \\[View-scroll-page-forward] and
778 \\[View-scroll-page-backward] scroll by default.
779 If LINES is omitted or = 0, sets \"page size\" to window height and
780 scrolls forward that much, otherwise scrolls forward LINES lines and sets
781 \"page size\" to the minimum of window height and the absolute value of LINES.
782 See also `View-scroll-page-forward'."
783 (interactive "P")
784 (view-scroll-lines lines nil
785 (setq view-page-size (view-page-size-default lines))
786 nil))
787
788 (defun View-scroll-page-backward-set-page-size (&optional lines)
789 "Scroll backward prefix LINES lines in View mode, setting the \"page size\".
790 See also `View-scroll-page-forward-set-page-size'."
791 (interactive "P")
792 (view-scroll-lines lines t
793 (setq view-page-size (view-page-size-default lines))
794 nil))
795
796 (defun View-scroll-line-forward (&optional lines)
797 "Scroll forward one line (or prefix LINES lines) in View mode.
798 See also `View-scroll-page-forward,' but note that scrolling is limited
799 to minimum of LINES and one window-full."
800 (interactive "P")
801 (view-scroll-lines lines nil 1 t))
802
803 (defun View-scroll-line-backward (&optional lines)
804 "Scroll backward one line (or prefix LINES lines) in View mode.
805 See also `View-scroll-line-forward'."
806 (interactive "P")
807 (view-scroll-lines lines t 1 t))
808
809 (defun View-scroll-half-page-forward (&optional lines)
810 "Scroll forward a \"half page\" (or prefix LINES) lines in View mode.
811 If LINES is not omitted, the \"half page size\" is set to the minimum of
812 window height and the absolute value of LINES.
813 LINES=0 resets \"half page size\" to half window height."
814 (interactive "P")
815 (view-scroll-lines lines nil (view-set-half-page-size-default lines) t))
816
817 (defun View-scroll-half-page-backward (&optional lines)
818 "Scroll backward a \"half page\" (or prefix LINES) lines in View mode.
819 See also `View-scroll-half-page-forward'."
820 (interactive "P")
821 (view-scroll-lines lines t (view-set-half-page-size-default lines) t))
822
823 (defun View-revert-buffer-scroll-page-forward (&optional lines)
824 "Scroll forward, reverting buffer if needed, in View mode.
825 If buffer has not been changed and the corresponding file is newer, first
826 revert the buffer, then scroll.
827 This command is useful if you are viewing a changing file.
828
829 The prefix argument LINES says how many lines to scroll.
830 If you don't specify a prefix argument, it uses the number of lines set by
831 \\[View-scroll-page-forward-set-page-size] or
832 \\[View-scroll-page-backward-set-page-size].
833 If LINES is more than a window-full, only the last window-full is shown."
834 (interactive "P")
835 (let ((view-scroll-auto-exit nil)
836 (view-try-extend-at-buffer-end t))
837 (view-scroll-lines lines nil view-page-size nil)))
838
839 (defun View-back-to-mark (&optional ignore)
840 "Return to last mark set in View mode, else beginning of file.
841 Display that line at the center of the window.
842 This command pops the mark ring, so that successive
843 invocations return to earlier marks."
844 (interactive)
845 (goto-char (or (mark t) (point-min)))
846 (pop-mark)
847 (view-recenter))
848
849 (defun View-search-regexp-forward (n regexp)
850 "Search forward for first (or prefix Nth) occurrence of REGEXP in View mode.
851
852 Displays line found at center of window. Sets mark at starting position and
853 pushes mark ring.
854
855 Characters @ and ! are special at the beginning of REGEXP. They modify
856 the search rather than become part of the pattern searched for.
857 @ means search all the buffer i.e. start search at the beginning of buffer.
858 ! means search for a line that contains no match for the pattern.
859 If REGEXP is empty or only consist of these control characters, then
860 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
861 for use by later search commands.
862
863 The variable `view-highlight-face' controls the face that is used
864 for highlighting the match that is found."
865 (interactive "p\nsSearch forward (regexp): ")
866 (view-search n regexp))
867
868 (defun View-search-regexp-backward (n regexp)
869 "Search backward for first (or prefix Nth) occurrence of REGEXP in View mode.
870
871 Displays line found at center of window. Sets mark at starting position and
872 pushes mark ring.
873
874 Characters @ and ! are special at the beginning of REGEXP. They modify
875 the search rather than become part of the pattern searched for.
876 @ means search all the buffer i.e. start search at the end of buffer.
877 ! means search for a line that contains no match for the pattern.
878 If REGEXP is empty or only consist of these control characters, then
879 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
880 for use by later search commands.
881
882 The variable `view-highlight-face' controls the face that is used
883 for highlighting the match that is found."
884 (interactive "p\nsSearch backward (regexp): ")
885 (view-search (- n) regexp))
886
887 (defun View-search-last-regexp-forward (n) "\
888 Search forward for first (or prefix Nth) instance of last regexp in View mode.
889 Displays line found at center of window. Sets mark at starting position and
890 pushes mark ring.
891
892 The variable `view-highlight-face' controls the face that is used
893 for highlighting the match that is found."
894 (interactive "p")
895 (view-search n nil))
896
897 (defun View-search-last-regexp-backward (n) "\
898 Search backward for first (or prefix Nth) instance of last regexp in View mode.
899 Displays line found at center of window. Sets mark at starting position and
900 pushes mark ring.
901
902 The variable `view-highlight-face' controls the face that is used
903 for highlighting the match that is found."
904 (interactive "p")
905 (view-search (- n) nil))
906
907 (defun view-search (times regexp)
908 ;; This function does the job for all the View-search- commands.
909 ;; Search for the TIMESt match for REGEXP. If TIMES is negative
910 ;; search backwards. If REGEXP is nil use `view-last-regexp'.
911 ;; Charcters "!" and "@" have a special meaning at the beginning of
912 ;; REGEXP and are removed from REGEXP before the search "!" means
913 ;; search for lines with no match for REGEXP. "@" means search in
914 ;; the whole buffer, don't start searching from the present point.
915 (let (where no end ln)
916 (cond
917 ((and regexp (> (length regexp) 0)
918 (or (not (memq (string-to-char regexp) '(?! ?@)))
919 (progn
920 (if (member (substring regexp 0 2) '("!@" "@!"))
921 (setq end t no t ln 2)
922 (setq no (not (setq end (eq ?@ (string-to-char regexp))))
923 ln 1))
924 (> (length (setq regexp (substring regexp ln))) 0))))
925 (setq view-last-regexp (if no (list regexp) regexp)))
926 ((consp view-last-regexp)
927 (setq regexp (car view-last-regexp))
928 (unless (setq no (not no)) (setq view-last-regexp regexp)))
929 (view-last-regexp (setq regexp view-last-regexp)
930 (if no (setq view-last-regexp (list regexp))))
931 (t (error "No previous View-mode search")))
932 (save-excursion
933 (if end (goto-char (if (< times 0) (point-max) (point-min)))
934 (move-to-window-line (if (< times 0) 0 -1)))
935 (if (if no (view-search-no-match-lines times regexp)
936 (re-search-forward regexp nil t times))
937 (setq where (point))))
938 (if where
939 (progn
940 (push-mark)
941 (goto-char where)
942 (if view-overlay
943 (move-overlay view-overlay (match-beginning 0) (match-end 0))
944 (setq view-overlay
945 (make-overlay (match-beginning 0) (match-end 0))))
946 (overlay-put view-overlay 'face view-highlight-face)
947 (beginning-of-line)
948 (view-recenter))
949 (message "Can't find occurrence %d of %s%s"
950 times (if no "no " "") regexp)
951 (sit-for 4))))
952
953 (defun view-search-no-match-lines (times regexp)
954 ;; Search for the TIMESt occurrence of line with no match for REGEXP.
955 (let ((back (and (< times 0) (setq times (- times)) -1))
956 n)
957 (while (> times 0)
958 (save-excursion (beginning-of-line (if back (- times) (1+ times)))
959 (setq n (point)))
960 (setq times
961 (cond
962 ((< (count-lines (point) n) times) -1) ; Not enough lines.
963 ((or (null (re-search-forward regexp nil t back))
964 (if back (and (< (match-end 0) n)
965 (> (count-lines (match-end 0) n) 1))
966 (and (< n (match-beginning 0))
967 (> (count-lines n (match-beginning 0)) 1))))
968 0) ; No match within lines.
969 (back (count-lines (max n (match-beginning 0)) (match-end 0)))
970 (t (count-lines (match-beginning 0) (min n (match-end 0))))))
971 (goto-char n))
972 (and (zerop times) (looking-at "^.*$"))))
973
974
975 (provide 'view)
976
977 ;;; view.el ends here