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