* mh-mime.el (mh-decode-message-subject): Fix case of Subject.
[emacs.git] / lisp / view.el
blobee85b4e7823f2663696eb2b38b7f9ce1951b0ee8
1 ;;; view.el --- peruse file or buffer without editing
3 ;; Copyright (C) 1985, 1989, 1994-1995, 1997, 2000-2011
4 ;; Free Software Foundation, Inc.
6 ;; Author: K. Shane Hartman
7 ;; Maintainer: Inge Frick <inge@nada.kth.se>
8 ;; Keywords: files
10 ;; This file is part of GNU Emacs.
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 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
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.
37 ;;; Suggested key bindings:
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
42 ;; You could also bind view-file, view-buffer, view-buffer-other-window and
43 ;; view-buffer-other-frame to keys.
45 ;;; Code:
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)
53 (defcustom view-highlight-face 'highlight
54 "The face used for highlighting the match found by View mode search."
55 :type 'face
56 :group 'view)
58 (defcustom view-scroll-auto-exit nil
59 "Non-nil means scrolling past the end of buffer exits View mode.
60 A value of nil means attempting to scroll past the end of the buffer,
61 only rings the bell and gives a message on how to leave."
62 :type 'boolean
63 :group 'view)
65 (defcustom view-try-extend-at-buffer-end nil
66 "Non-nil means try to load more of file when reaching end of buffer.
67 This variable is mainly intended to be temporarily set to non-nil by
68 the F command in view-mode, but you can set it to t if you want the action
69 for all scroll commands in view mode."
70 :type 'boolean
71 :group 'view)
73 ;;;###autoload
74 (defcustom view-remove-frame-by-deleting t
75 "Determine how View mode removes a frame no longer needed.
76 If nil, make an icon of the frame. If non-nil, delete the frame."
77 :type 'boolean
78 :group 'view
79 :version "23.1")
81 (defcustom view-exits-all-viewing-windows nil
82 "Non-nil means restore all windows used to view buffer.
83 Commands that restore windows when finished viewing a buffer,
84 apply to all windows that display the buffer and have restore
85 information. If `view-exits-all-viewing-windows' is nil, only
86 the selected window is considered for restoring."
87 :type 'boolean
88 :group 'view)
90 (defcustom view-inhibit-help-message nil
91 "Non-nil inhibits the help message shown upon entering View mode."
92 :type 'boolean
93 :group 'view
94 :version "22.1")
96 ;;;###autoload
97 (defvar view-mode nil
98 "Non-nil if View mode is enabled.
99 Don't change this variable directly, you must change it by one of the
100 functions that enable or disable view mode.")
101 ;;;###autoload
102 (make-variable-buffer-local 'view-mode)
104 (defcustom view-mode-hook nil
105 "Normal hook run when starting to view a buffer or file."
106 :type 'hook
107 :group 'view)
109 (defvar view-old-buffer-read-only nil)
110 (make-variable-buffer-local 'view-old-buffer-read-only)
112 (defvar view-old-Helper-return-blurb)
113 (make-variable-buffer-local 'view-old-Helper-return-blurb)
115 (defvar view-page-size nil
116 "Default number of lines to scroll by View page commands.
117 If nil that means use the window size.")
118 (make-variable-buffer-local 'view-page-size)
120 (defvar view-half-page-size nil
121 "Default number of lines to scroll by View half page commands.
122 If nil that means use half the window size.")
123 (make-variable-buffer-local 'view-half-page-size)
125 (defvar view-last-regexp nil)
126 (make-variable-buffer-local 'view-last-regexp) ; Global is better???
128 (defvar view-return-to-alist nil
129 "What to do with used windows and where to go when finished viewing buffer.
130 This is local in each buffer being viewed.
131 It is added to by `view-mode-enter' when starting to view a buffer and
132 subtracted from by `view-mode-exit' when finished viewing the buffer.
134 See RETURN-TO-ALIST argument of function `view-mode-exit' for the format of
135 `view-return-to-alist'.")
136 (make-obsolete-variable
137 'view-return-to-alist "this variable is no more used." "24.1")
138 (make-variable-buffer-local 'view-return-to-alist)
139 (put 'view-return-to-alist 'permanent-local t)
141 (defvar view-exit-action nil
142 "If non-nil, a function with one argument (a buffer) called when finished viewing.
143 Commands like \\[view-file] and \\[view-file-other-window] may
144 set this to bury or kill the viewed buffer.
145 Observe that the buffer viewed might not appear in any window at
146 the time this function is called.")
147 (make-variable-buffer-local 'view-exit-action)
149 (defvar view-no-disable-on-exit nil
150 "If non-nil, View mode \"exit\" commands don't actually disable View mode.
151 Instead, these commands just switch buffers or windows.
152 This is set in certain buffers by specialized features such as help commands
153 that use View mode automatically.")
155 (defvar view-overlay nil
156 "Overlay used to display where a search operation found its match.
157 This is local in each buffer, once it is used.")
158 (make-variable-buffer-local 'view-overlay)
160 ;; Define keymap inside defvar to make it easier to load changes.
161 ;; Some redundant "less"-like key bindings below have been commented out.
162 (defvar view-mode-map
163 (let ((map (make-sparse-keymap)))
164 (define-key map "C" 'View-kill-and-leave)
165 (define-key map "c" 'View-leave)
166 (define-key map "Q" 'View-quit-all)
167 (define-key map "E" 'View-exit-and-edit)
168 ;; (define-key map "v" 'View-exit)
169 (define-key map "e" 'View-exit)
170 (define-key map "q" 'View-quit)
171 ;; (define-key map "N" 'View-search-last-regexp-backward)
172 (define-key map "p" 'View-search-last-regexp-backward)
173 (define-key map "n" 'View-search-last-regexp-forward)
174 ;; (define-key map "?" 'View-search-regexp-backward) ; Less does this.
175 (define-key map "\\" 'View-search-regexp-backward)
176 (define-key map "/" 'View-search-regexp-forward)
177 (define-key map "r" 'isearch-backward)
178 (define-key map "s" 'isearch-forward)
179 (define-key map "m" 'point-to-register)
180 (define-key map "'" 'register-to-point)
181 (define-key map "x" 'exchange-point-and-mark)
182 (define-key map "@" 'View-back-to-mark)
183 (define-key map "." 'set-mark-command)
184 (define-key map "%" 'View-goto-percent)
185 ;; (define-key map "G" 'View-goto-line-last)
186 (define-key map "g" 'View-goto-line)
187 (define-key map "=" 'what-line)
188 (define-key map "F" 'View-revert-buffer-scroll-page-forward)
189 ;; (define-key map "k" 'View-scroll-line-backward)
190 (define-key map "y" 'View-scroll-line-backward)
191 ;; (define-key map "j" 'View-scroll-line-forward)
192 (define-key map "\n" 'View-scroll-line-forward)
193 (define-key map "\r" 'View-scroll-line-forward)
194 (define-key map "u" 'View-scroll-half-page-backward)
195 (define-key map "d" 'View-scroll-half-page-forward)
196 (define-key map "z" 'View-scroll-page-forward-set-page-size)
197 (define-key map "w" 'View-scroll-page-backward-set-page-size)
198 ;; (define-key map "b" 'View-scroll-page-backward)
199 (define-key map "\C-?" 'View-scroll-page-backward)
200 ;; (define-key map "f" 'View-scroll-page-forward)
201 (define-key map " " 'View-scroll-page-forward)
202 (define-key map "o" 'View-scroll-to-buffer-end)
203 (define-key map ">" 'end-of-buffer)
204 (define-key map "<" 'beginning-of-buffer)
205 (define-key map "-" 'negative-argument)
206 (define-key map "9" 'digit-argument)
207 (define-key map "8" 'digit-argument)
208 (define-key map "7" 'digit-argument)
209 (define-key map "6" 'digit-argument)
210 (define-key map "5" 'digit-argument)
211 (define-key map "4" 'digit-argument)
212 (define-key map "3" 'digit-argument)
213 (define-key map "2" 'digit-argument)
214 (define-key map "1" 'digit-argument)
215 (define-key map "0" 'digit-argument)
216 (define-key map "H" 'describe-mode)
217 (define-key map "?" 'describe-mode) ; Maybe do as less instead? See above.
218 (define-key map "h" 'describe-mode)
219 map))
221 ;;; Commands that enter or exit view mode.
223 ;; This is used when view mode is exited, to make sure we don't try to
224 ;; kill a buffer modified by the user. A buffer in view mode can
225 ;; become modified if the user types C-x C-q, edits the buffer, then
226 ;; types C-x C-q again to return to view mode.
227 ;;;###autoload
228 (defun kill-buffer-if-not-modified (buf)
229 "Like `kill-buffer', but does nothing if the buffer is modified."
230 (let ((buf (get-buffer buf)))
231 (and buf (not (buffer-modified-p buf))
232 (kill-buffer buf))))
234 ;;;###autoload
235 (defun view-file (file)
236 "View FILE in View mode, returning to previous buffer when done.
237 Emacs commands editing the buffer contents are not available; instead, a
238 special set of commands (mostly letters and punctuation) are defined for
239 moving around in the buffer.
240 Space scrolls forward, Delete scrolls backward.
241 For a list of all View commands, type H or h while viewing.
243 This command runs the normal hook `view-mode-hook'."
244 (interactive "fView file: ")
245 (unless (file-exists-p file) (error "%s does not exist" file))
246 (let ((had-a-buf (get-file-buffer file))
247 (buffer (find-file-noselect file)))
248 (view-buffer buffer (and (not had-a-buf) 'kill-buffer-if-not-modified))))
250 ;;;###autoload
251 (defun view-file-other-window (file)
252 "View FILE in View mode in another window.
253 When done, return that window to its previous buffer, and kill the
254 buffer visiting FILE if unmodified and if it wasn't visited before.
256 Emacs commands editing the buffer contents are not available; instead,
257 a special set of commands (mostly letters and punctuation)
258 are defined for moving around in the buffer.
259 Space scrolls forward, Delete scrolls backward.
260 For a list of all View commands, type H or h while viewing.
262 This command runs the normal hook `view-mode-hook'."
263 (interactive "fIn other window view file: ")
264 (unless (file-exists-p file) (error "%s does not exist" file))
265 (let ((had-a-buf (get-file-buffer file))
266 (buf-to-view (find-file-noselect file)))
267 (view-buffer-other-window buf-to-view nil
268 (and (not had-a-buf)
269 'kill-buffer-if-not-modified))))
271 ;;;###autoload
272 (defun view-file-other-frame (file)
273 "View FILE in View mode in another frame.
274 When done, kill the buffer visiting FILE if unmodified and if it wasn't
275 visited before; also, maybe delete other frame and/or return to previous
276 buffer.
278 Emacs commands editing the buffer contents are not available; instead,
279 a special set of commands (mostly letters and punctuation)
280 are defined for moving around in the buffer.
281 Space scrolls forward, Delete scrolls backward.
282 For a list of all View commands, type H or h while viewing.
284 This command runs the normal hook `view-mode-hook'."
285 (interactive "fIn other frame view file: ")
286 (unless (file-exists-p file) (error "%s does not exist" file))
287 (let ((had-a-buf (get-file-buffer file))
288 (buf-to-view (find-file-noselect file)))
289 (view-buffer-other-frame buf-to-view nil
290 (and (not had-a-buf)
291 'kill-buffer-if-not-modified))))
294 ;;;###autoload
295 (defun view-buffer (buffer &optional exit-action)
296 "View BUFFER in View mode, returning to previous buffer when done.
297 Emacs commands editing the buffer contents are not available; instead, a
298 special set of commands (mostly letters and punctuation) are defined for
299 moving around in the buffer.
300 Space scrolls forward, Delete scrolls backward.
301 For a list of all View commands, type H or h while viewing.
303 This command runs the normal hook `view-mode-hook'.
305 Optional argument EXIT-ACTION is either nil or a function with buffer as
306 argument. This function is called when finished viewing buffer. Use
307 this argument instead of explicitly setting `view-exit-action'.
309 Do not set EXIT-ACTION to `kill-buffer' when BUFFER visits a
310 file: Users may suspend viewing in order to modify the buffer.
311 Exiting View mode will then discard the user's edits. Setting
312 EXIT-ACTION to `kill-buffer-if-not-modified' avoids this."
313 (interactive "bView buffer: ")
314 (if (eq (with-current-buffer buffer
315 (get major-mode 'mode-class))
316 'special)
317 (progn
318 (switch-to-buffer buffer)
319 (message "Not using View mode because the major mode is special"))
320 (pop-to-buffer-same-window buffer)
321 (view-mode-enter nil exit-action)))
323 ;;;###autoload
324 (defun view-buffer-other-window (buffer &optional not-return exit-action)
325 "View BUFFER in View mode in another window.
326 Emacs commands editing the buffer contents are not available;
327 instead, a special set of commands (mostly letters and
328 punctuation) are defined for moving around in the buffer.
329 Space scrolls forward, Delete scrolls backward.
330 For a list of all View commands, type H or h while viewing.
332 This command runs the normal hook `view-mode-hook'.
334 Optional argument NOT-RETURN is ignored.
336 Optional argument EXIT-ACTION is either nil or a function with buffer as
337 argument. This function is called when finished viewing buffer. Use
338 this argument instead of explicitly setting `view-exit-action'."
339 (interactive "bIn other window view buffer:\nP")
340 (pop-to-buffer-other-window buffer)
341 (view-mode-enter nil exit-action))
343 ;;;###autoload
344 (defun view-buffer-other-frame (buffer &optional not-return exit-action)
345 "View BUFFER in View mode in another frame.
346 Emacs commands editing the buffer contents are not available;
347 instead, a special set of commands (mostly letters and
348 punctuation) are defined for moving around in the buffer.
349 Space scrolls forward, Delete scrolls backward.
350 For a list of all View commands, type H or h while viewing.
352 This command runs the normal hook `view-mode-hook'.
354 Optional argument NOT-RETURN is ignored.
356 Optional argument EXIT-ACTION is either nil or a function with buffer as
357 argument. This function is called when finished viewing buffer. Use
358 this argument instead of explicitly setting `view-exit-action'."
359 (interactive "bView buffer in other frame: \nP")
360 (pop-to-buffer-other-frame buffer)
361 (view-mode-enter nil exit-action))
363 ;;;###autoload
364 (define-minor-mode view-mode
365 ;; In the following documentation string we have to use some explicit key
366 ;; bindings instead of using the \\[] construction. The reason for this
367 ;; is that most commands have more than one key binding.
368 "Toggle View mode, a minor mode for viewing text but not editing it.
369 With prefix argument ARG, turn View mode on if ARG is positive, otherwise
370 turn it off.
372 Emacs commands that do not change the buffer contents are available as usual.
373 Kill commands insert text in kill buffers but do not delete. Other commands
374 \(among them most letters and punctuation) beep and tell that the buffer is
375 read-only.
376 \\<view-mode-map>
377 The following additional commands are provided. Most commands take prefix
378 arguments. Page commands default to \"page size\" lines which is almost a whole
379 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
380 and set \"half page size\" lines which initially is half a window full. Search
381 commands default to a repeat count of one.
383 H, h, ? This message.
384 Digits provide prefix arguments.
385 \\[negative-argument] negative prefix argument.
386 \\[beginning-of-buffer] move to the beginning of buffer.
387 > move to the end of buffer.
388 \\[View-scroll-to-buffer-end] scroll so that buffer end is at last line of window.
389 SPC scroll forward \"page size\" lines.
390 With prefix scroll forward prefix lines.
391 DEL scroll backward \"page size\" lines.
392 With prefix scroll backward prefix lines.
393 \\[View-scroll-page-forward-set-page-size] like \\[View-scroll-page-forward] but with prefix sets \"page size\" to prefix.
394 \\[View-scroll-page-backward-set-page-size] like \\[View-scroll-page-backward] but with prefix sets \"page size\" to prefix.
395 \\[View-scroll-half-page-forward] scroll forward \"half page size\" lines. With prefix, sets
396 \"half page size\" to prefix lines and scrolls forward that much.
397 \\[View-scroll-half-page-backward] scroll backward \"half page size\" lines. With prefix, sets
398 \"half page size\" to prefix lines and scrolls backward that much.
399 RET, LFD scroll forward one line. With prefix scroll forward prefix line(s).
400 y scroll backward one line. With prefix scroll backward prefix line(s).
401 \\[View-revert-buffer-scroll-page-forward] revert-buffer if necessary and scroll forward.
402 Use this to view a changing file.
403 \\[what-line] prints the current line number.
404 \\[View-goto-percent] goes prefix argument (default 100) percent into buffer.
405 \\[View-goto-line] goes to line given by prefix argument (default first line).
406 . set the mark.
407 x exchanges point and mark.
408 \\[View-back-to-mark] return to mark and pops mark ring.
409 Mark ring is pushed at start of every successful search and when
410 jump to line occurs. The mark is set on jump to buffer start or end.
411 \\[point-to-register] save current position in character register.
412 ' go to position saved in character register.
413 s do forward incremental search.
414 r do reverse incremental search.
415 \\[View-search-regexp-forward] searches forward for regular expression, starting after current page.
416 ! and @ have a special meaning at the beginning of the regexp.
417 ! means search for a line with no match for regexp. @ means start
418 search at beginning (end for backward search) of buffer.
419 \\ searches backward for regular expression, starting before current page.
420 \\[View-search-last-regexp-forward] searches forward for last regular expression.
421 p searches backward for last regular expression.
422 \\[View-quit] quit View mode, restoring this window and buffer to previous state.
423 \\[View-quit] is the normal way to leave view mode.
424 \\[View-exit] exit View mode but stay in current buffer. Use this if you started
425 viewing a buffer (file) and find out you want to edit it.
426 This command restores the previous read-only status of the buffer.
427 \\[View-exit-and-edit] exit View mode, and make the current buffer editable
428 even if it was not editable before entry to View mode.
429 \\[View-quit-all] quit View mode, restoring all windows to previous state.
430 \\[View-leave] quit View mode and maybe switch buffers, but don't kill this buffer.
431 \\[View-kill-and-leave] quit View mode, kill current buffer and go back to other buffer.
433 The effect of \\[View-leave], \\[View-quit] and \\[View-kill-and-leave] depends on how view-mode was entered. If it was
434 entered by view-file, view-file-other-window, view-file-other-frame, or
435 \\[dired-view-file] \(\\[view-file], \\[view-file-other-window],
436 \\[view-file-other-frame], or the Dired mode v command),
437 then \\[View-quit] will try to kill the current buffer.
438 If view-mode was entered from another buffer, by \\[view-buffer],
439 \\[view-buffer-other-window], \\[view-buffer-other frame], \\[view-file],
440 \\[view-file-other-window], or \\[view-file-other-frame],
441 then \\[View-leave], \\[View-quit] and \\[View-kill-and-leave] will return to that buffer.
443 Entry to view-mode runs the normal hook `view-mode-hook'."
444 :lighter " View" :keymap view-mode-map
445 (if view-mode (view-mode-enable) (view-mode-disable)))
447 (defun view-mode-enable ()
448 "Turn on View mode."
449 ;; Always leave view mode before changing major mode.
450 ;; This is to guarantee that the buffer-read-only variable is restored.
451 (add-hook 'change-major-mode-hook 'view-mode-disable nil t)
452 (setq view-mode t
453 view-page-size nil
454 view-half-page-size nil
455 view-old-buffer-read-only buffer-read-only
456 buffer-read-only t)
457 (if (boundp 'Helper-return-blurb)
458 (setq view-old-Helper-return-blurb (and (boundp 'Helper-return-blurb)
459 Helper-return-blurb)
460 Helper-return-blurb
461 (format "continue viewing %s"
462 (if (buffer-file-name)
463 (file-name-nondirectory (buffer-file-name))
464 (buffer-name)))))
465 (force-mode-line-update)
466 (run-hooks 'view-mode-hook))
468 (defun view-mode-disable ()
469 "Turn off View mode."
470 (remove-hook 'change-major-mode-hook 'view-mode-disable t)
471 (and view-overlay (delete-overlay view-overlay))
472 (force-mode-line-update)
473 ;; Calling toggle-read-only while View mode is enabled
474 ;; sets view-read-only to t as a buffer-local variable
475 ;; after exiting View mode. That arranges that the next toggle-read-only
476 ;; will reenable View mode.
477 ;; Cancelling View mode in any other way should cancel that, too,
478 ;; so that View mode stays off if toggle-read-only is called.
479 (if (local-variable-p 'view-read-only)
480 (kill-local-variable 'view-read-only))
481 (setq view-mode nil)
482 (if (boundp 'Helper-return-blurb)
483 (setq Helper-return-blurb view-old-Helper-return-blurb))
484 (if buffer-read-only
485 (setq buffer-read-only view-old-buffer-read-only)))
487 ;;;###autoload
488 (defun view-return-to-alist-update (buffer &optional item)
489 "Update `view-return-to-alist' of buffer BUFFER.
490 Remove from `view-return-to-alist' all entries referencing dead
491 windows. Optional argument ITEM non-nil means add ITEM to
492 `view-return-to-alist' after purging. For a decsription of items
493 that can be added see the RETURN-TO-ALIST argument of the
494 function `view-mode-exit'. If `view-return-to-alist' contains an
495 entry for the selected window, purge that entry from
496 `view-return-to-alist' before adding ITEM."
497 (with-current-buffer buffer
498 (when view-return-to-alist
499 (let* ((list view-return-to-alist)
500 entry entry-window last)
501 (while list
502 (setq entry (car list))
503 (setq entry-window (car entry))
504 (if (and (windowp entry-window)
505 (or (and item (eq entry-window (selected-window)))
506 (not (window-live-p entry-window))))
507 ;; Remove that entry.
508 (if last
509 (setcdr last (cdr list))
510 (setq view-return-to-alist
511 (cdr view-return-to-alist)))
512 ;; Leave entry alone.
513 (setq last entry))
514 (setq list (cdr list)))))
515 ;; Add ITEM.
516 (when item
517 (setq view-return-to-alist
518 (cons item view-return-to-alist)))))
519 (make-obsolete 'view-return-to-alist-update "this function has no effect." "24.1")
521 ;;;###autoload
522 (defun view-mode-enter (&optional quit-restore exit-action)
523 "Enter View mode and set up exit from view mode depending on optional arguments.
524 Optional argument QUIT-RESTORE if non-nil must specify a valid
525 entry for quitting and restoring any window showing the current
526 buffer. This entry replaces any parameter installed by
527 `display-buffer' and is used by `view-mode-exit'.
529 Optional argument EXIT-ACTION, if non-nil, must specify a
530 function that takes a buffer as argument. This function will be
531 called by `view-mode-exit'.
533 For a list of all View commands, type H or h while viewing.
535 This function runs the normal hook `view-mode-hook'."
536 (when quit-restore
537 (dolist (window (get-buffer-window-list nil nil t))
538 (set-window-parameter window 'quit-restore quit-restore)))
540 (when exit-action
541 (setq view-exit-action exit-action))
543 (unless view-mode
544 (view-mode-enable)
545 (force-mode-line-update)
546 (unless view-inhibit-help-message
547 (message "%s"
548 (substitute-command-keys "\
549 View mode: type \\[help-command] for help, \\[describe-mode] for commands, \\[View-quit] to quit.")))))
551 ;; This is awful because it assumes that the selected window shows the
552 ;; current buffer when this is called.
553 (defun view-mode-exit (&optional exit-only exit-action all-windows)
554 "Exit View mode in various ways.
555 If all arguments are nil, remove the current buffer from the
556 selected window using the `quit-restore' information associated
557 with the selected window. If optional argument ALL-WINDOWS or
558 `view-exits-all-viewing-windows' are non-nil, remove the current
559 buffer from all windows showing it.
561 Optional argument EXIT-ONLY non-nil means just exit `view-mode'
562 \(unless `view-no-disable-on-exit' is non-nil) but do not change
563 the associations of any windows with the current buffer.
565 EXIT-ACTION, if non-nil, must specify a function that is called
566 with the current buffer as argument and is called after disabling
567 `view-mode' and removing any associations of windows with the
568 current buffer. "
569 (when view-mode
570 (let ((buffer (window-buffer)))
571 (unless view-no-disable-on-exit
572 (view-mode-disable))
574 (unless exit-only
575 (cond
576 ((or all-windows view-exits-all-viewing-windows)
577 (dolist (window (get-buffer-window-list))
578 (quit-restore-window window)))
579 ((eq (window-buffer) (current-buffer))
580 (quit-restore-window)))
582 (when exit-action
583 (funcall exit-action buffer))
584 (force-mode-line-update)))))
586 (defun View-exit ()
587 "Exit View mode but stay in current buffer."
588 (interactive)
589 (view-mode-exit t))
591 ;;;###autoload
592 (defun View-exit-and-edit ()
593 "Exit View mode and make the current buffer editable."
594 (interactive)
595 (let ((view-old-buffer-read-only nil)
596 (view-no-disable-on-exit nil))
597 (view-mode-exit t)))
599 (defun View-leave ()
600 "Quit View mode and maybe switch buffers, but don't kill this buffer."
601 (interactive)
602 (view-mode-exit))
604 (defun View-quit ()
605 "Quit View mode, trying to restore window and buffer to previous state.
606 Maybe kill this buffer. Try to restore selected window to previous state
607 and go to previous buffer or window."
608 (interactive)
609 (view-mode-exit nil view-exit-action))
611 (defun View-quit-all ()
612 "Quit View mode, trying to restore windows and buffers to previous state.
613 Maybe kill current buffer. Try to restore all windows viewing buffer to
614 previous state and go to previous buffer or window."
615 (interactive)
616 (view-mode-exit nil view-exit-action t))
618 (defun View-kill-and-leave ()
619 "Quit View mode, kill current buffer and return to previous buffer."
620 (interactive)
621 (view-mode-exit nil (or view-exit-action 'kill-buffer) t))
624 ;;; Some help routines.
626 (defun view-window-size ()
627 ;; Return the height of the current window, excluding the mode line.
628 ;; Using `window-line-height' accounts for variable-height fonts.
629 (let ((h (window-line-height -1)))
630 (if h
631 (1+ (nth 1 h))
632 ;; This should not happen, but if `window-line-height' returns
633 ;; nil, fall back on `window-height'.
634 (1- (window-height)))))
636 ;; (defun view-last-command (&optional who what)
637 ;; (setq view-last-command-entry this-command)
638 ;; (setq view-last-command who)
639 ;; (setq view-last-command-argument what))
641 ;; (defun View-repeat-last-command ()
642 ;; "Repeat last command issued in View mode."
643 ;; (interactive)
644 ;; (if (and view-last-command
645 ;; (eq view-last-command-entry last-command))
646 ;; (funcall view-last-command view-last-command-argument))
647 ;; (setq this-command view-last-command-entry))
649 (defun view-recenter ()
650 ;; Recenter point in window and redisplay normally.
651 (recenter '(1)))
653 (defun view-page-size-default (lines)
654 ;; If LINES is nil, 0, or larger than `view-window-size', return nil.
655 ;; Otherwise, return LINES.
656 (and lines
657 (not (zerop (setq lines (prefix-numeric-value lines))))
658 (<= (abs lines)
659 (abs (- (view-window-size) next-screen-context-lines)))
660 (abs lines)))
662 (defun view-set-half-page-size-default (lines)
663 ;; Get and maybe set half page size.
664 (if (not lines) (or view-half-page-size
665 (/ (view-window-size) 2))
666 (setq view-half-page-size
667 (if (zerop (setq lines (prefix-numeric-value lines)))
668 (/ (view-window-size) 2)
669 (view-page-size-default lines)))))
672 ;;; Commands for moving around in the buffer.
674 (defun View-goto-percent (&optional percent)
675 "Move to end (or prefix PERCENT) of buffer in View mode.
676 Display is centered at point.
677 Also set the mark at the position where point was."
678 (interactive "P")
679 (push-mark)
680 (goto-char
681 (if percent
682 (+ (point-min)
683 (floor (* (- (point-max) (point-min)) 0.01
684 (max 0 (min 100 (prefix-numeric-value percent))))))
685 (point-max)))
686 (view-recenter))
688 ;; (defun View-goto-line-last (&optional line)
689 ;; "Move to last (or prefix LINE) line in View mode.
690 ;; Display is centered at LINE.
691 ;; Sets mark at starting position and pushes mark ring."
692 ;; (interactive "P")
693 ;; (push-mark)
694 ;; (if line (goto-line (prefix-numeric-value line))
695 ;; (goto-char (point-max))
696 ;; (beginning-of-line))
697 ;; (view-recenter))
699 (defun View-goto-line (&optional line)
700 "Move to first (or prefix LINE) line in View mode.
701 Display is centered at LINE.
702 Also set the mark at the position where point was."
703 (interactive "p")
704 (push-mark)
705 (goto-char (point-min))
706 (forward-line (1- line))
707 (view-recenter))
709 (defun View-back-to-mark (&optional _ignore)
710 "Return to last mark set in View mode, else beginning of file.
711 Display that line at the center of the window.
712 This command pops the mark ring, so that successive
713 invocations return to earlier marks."
714 (interactive)
715 (goto-char (or (mark t) (point-min)))
716 (pop-mark)
717 (view-recenter))
719 (defun view-scroll-lines (lines backward default maxdefault)
720 ;; This function does the job for all the scrolling commands.
721 ;; Scroll forward LINES lines. If BACKWARD is non-nil, scroll backwards.
722 ;; If LINES is negative scroll in the other direction.
723 ;; If LINES is 0 or nil, scroll DEFAULT lines (if DEFAULT is nil, scroll
724 ;; by one page). If MAXDEFAULT is non-nil, scroll no more than a window.
725 (if (or (null lines) (zerop (setq lines (prefix-numeric-value lines))))
726 (setq lines default))
727 (when (and lines (< lines 0))
728 (setq backward (not backward) lines (- lines)))
729 (when (and maxdefault lines (> lines (view-window-size)))
730 (setq lines nil))
731 (cond (backward (scroll-down lines))
732 ((view-really-at-end)
733 (if view-scroll-auto-exit
734 (View-quit)
735 (ding)
736 (view-end-message)))
737 (t (scroll-up lines)
738 (if (view-really-at-end) (view-end-message)))))
740 (defun view-really-at-end ()
741 ;; Return true if buffer end visible. Maybe revert buffer and test.
742 (and (pos-visible-in-window-p (point-max))
743 (let ((buf (current-buffer))
744 (bufname (buffer-name))
745 (file (buffer-file-name)))
746 (or (not view-try-extend-at-buffer-end)
747 (null file)
748 (verify-visited-file-modtime buf)
749 (not (file-exists-p file))
750 (when (buffer-modified-p buf)
751 (setq file (file-name-nondirectory file))
752 (not (yes-or-no-p
753 (format
754 "File %s changed on disk. Discard your edits%s? "
755 file
756 (if (string= bufname file) ""
757 (concat " in " bufname))))))
758 (progn
759 (revert-buffer t t t)
760 (pos-visible-in-window-p (point-max)))))))
762 (defun view-end-message ()
763 ;; Tell that we are at end of buffer.
764 (goto-char (point-max))
765 (if (window-parameter nil 'quit-restore)
766 (message "End of buffer. Type %s to quit viewing."
767 (substitute-command-keys
768 (if view-scroll-auto-exit "\\[View-scroll-page-forward]"
769 "\\[View-quit]")))
770 (message "End of buffer")))
772 (defun View-scroll-to-buffer-end ()
773 "Scroll backward or forward so that buffer end is at last line of window."
774 (interactive)
775 (let ((p (if (pos-visible-in-window-p (point-max)) (point))))
776 (goto-char (point-max))
777 (recenter -1)
778 (and p (goto-char p))))
780 (defun View-scroll-page-forward (&optional lines)
781 "Scroll \"page size\" or prefix LINES lines forward in View mode.
782 Exit if end of text is visible and `view-scroll-auto-exit' is non-nil.
783 \"page size\" is whole window full, or number of lines set by
784 \\[View-scroll-page-forward-set-page-size] or
785 \\[View-scroll-page-backward-set-page-size].
786 If LINES is more than a window-full, only the last window-full is shown."
787 (interactive "P")
788 (view-scroll-lines lines nil (view-page-size-default view-page-size) nil))
790 (defun View-scroll-page-backward (&optional lines)
791 "Scroll \"page size\" or prefix LINES lines backward in View mode.
792 See also `View-scroll-page-forward'."
793 (interactive "P")
794 (view-scroll-lines lines t (view-page-size-default view-page-size) nil))
796 (defun View-scroll-page-forward-set-page-size (&optional lines)
797 "Scroll forward LINES lines in View mode, setting the \"page size\".
798 This is the number of lines which \\[View-scroll-page-forward] and
799 \\[View-scroll-page-backward] scroll by default.
800 If LINES is omitted or = 0, sets \"page size\" to window height and
801 scrolls forward that much, otherwise scrolls forward LINES lines and sets
802 \"page size\" to the minimum of window height and the absolute value of LINES.
803 See also `View-scroll-page-forward'."
804 (interactive "P")
805 (view-scroll-lines lines nil
806 (setq view-page-size (view-page-size-default lines))
807 nil))
809 (defun View-scroll-page-backward-set-page-size (&optional lines)
810 "Scroll backward prefix LINES lines in View mode, setting the \"page size\".
811 See also `View-scroll-page-forward-set-page-size'."
812 (interactive "P")
813 (view-scroll-lines lines t
814 (setq view-page-size (view-page-size-default lines))
815 nil))
817 (defun View-scroll-line-forward (&optional lines)
818 "Scroll forward one line (or prefix LINES lines) in View mode.
819 See also `View-scroll-page-forward', but note that scrolling is limited
820 to minimum of LINES and one window-full."
821 (interactive "P")
822 (view-scroll-lines lines nil 1 t))
824 (defun View-scroll-line-backward (&optional lines)
825 "Scroll backward one line (or prefix LINES lines) in View mode.
826 See also `View-scroll-line-forward'."
827 (interactive "P")
828 (view-scroll-lines lines t 1 t))
830 (defun View-scroll-half-page-forward (&optional lines)
831 "Scroll forward a \"half page\" (or prefix LINES) lines in View mode.
832 If LINES is not omitted, the \"half page size\" is set to the minimum of
833 window height and the absolute value of LINES.
834 LINES=0 resets \"half page size\" to half window height."
835 (interactive "P")
836 (view-scroll-lines lines nil (view-set-half-page-size-default lines) t))
838 (defun View-scroll-half-page-backward (&optional lines)
839 "Scroll backward a \"half page\" (or prefix LINES) lines in View mode.
840 See also `View-scroll-half-page-forward'."
841 (interactive "P")
842 (view-scroll-lines lines t (view-set-half-page-size-default lines) t))
844 (defun View-revert-buffer-scroll-page-forward (&optional lines)
845 "Scroll forward, reverting buffer if needed, in View mode.
846 If buffer has not been changed and the corresponding file is newer, first
847 revert the buffer, then scroll.
848 This command is useful if you are viewing a changing file.
850 The prefix argument LINES says how many lines to scroll.
851 If you don't specify a prefix argument, it uses the number of lines set by
852 \\[View-scroll-page-forward-set-page-size] or
853 \\[View-scroll-page-backward-set-page-size].
854 If LINES is more than a window-full, only the last window-full is shown."
855 (interactive "P")
856 (let ((view-scroll-auto-exit nil)
857 (view-try-extend-at-buffer-end t))
858 (view-scroll-lines lines nil (view-page-size-default view-page-size) nil)))
860 (defun View-search-regexp-forward (n regexp)
861 "Search forward for first (or prefix Nth) occurrence of REGEXP in View mode.
863 Displays line found at center of window. Sets mark at starting position and
864 pushes mark ring.
866 Characters @ and ! are special at the beginning of REGEXP. They modify
867 the search rather than become part of the pattern searched for.
868 @ means search all the buffer i.e. start search at the beginning of buffer.
869 ! means search for a line that contains no match for the pattern.
870 If REGEXP is empty or only consist of these control characters, then
871 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
872 for use by later search commands.
874 The variable `view-highlight-face' controls the face that is used
875 for highlighting the match that is found."
876 (interactive "p\nsSearch forward (regexp): ")
877 (view-search n regexp))
879 (defun View-search-regexp-backward (n regexp)
880 "Search backward for first (or prefix Nth) occurrence of REGEXP in View mode.
882 Displays line found at center of window. Sets mark at starting position and
883 pushes mark ring.
885 Characters @ and ! are special at the beginning of REGEXP. They modify
886 the search rather than become part of the pattern searched for.
887 @ means search all the buffer i.e. start search at the end of buffer.
888 ! means search for a line that contains no match for the pattern.
889 If REGEXP is empty or only consist of these control characters, then
890 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
891 for use by later search commands.
893 The variable `view-highlight-face' controls the face that is used
894 for highlighting the match that is found."
895 (interactive "p\nsSearch backward (regexp): ")
896 (view-search (- n) regexp))
898 (defun View-search-last-regexp-forward (n) "\
899 Search forward for first (or prefix Nth) instance of last regexp in View mode.
900 Displays line found at center of window. Sets mark at starting position and
901 pushes mark ring.
903 The variable `view-highlight-face' controls the face that is used
904 for highlighting the match that is found."
905 (interactive "p")
906 (view-search n nil))
908 (defun View-search-last-regexp-backward (n) "\
909 Search backward for first (or prefix Nth) instance of last regexp in View mode.
910 Displays line found at center of window. Sets mark at starting position and
911 pushes mark ring.
913 The variable `view-highlight-face' controls the face that is used
914 for highlighting the match that is found."
915 (interactive "p")
916 (view-search (- n) nil))
918 (defun view-search (times regexp)
919 ;; This function does the job for all the View-search- commands.
920 ;; Search for the TIMESt match for REGEXP. If TIMES is negative
921 ;; search backwards. If REGEXP is nil use `view-last-regexp'.
922 ;; Characters "!" and "@" have a special meaning at the beginning of
923 ;; REGEXP and are removed from REGEXP before the search "!" means
924 ;; search for lines with no match for REGEXP. "@" means search in
925 ;; the whole buffer, don't start searching from the present point.
926 (let (where no end ln)
927 (cond
928 ((and regexp (> (length regexp) 0)
929 (or (not (memq (string-to-char regexp) '(?! ?@)))
930 (progn
931 (if (member (substring regexp 0 2) '("!@" "@!"))
932 (setq end t no t ln 2)
933 (setq no (not (setq end (eq ?@ (string-to-char regexp))))
934 ln 1))
935 (> (length (setq regexp (substring regexp ln))) 0))))
936 (setq view-last-regexp (if no (list regexp) regexp)))
937 ((consp view-last-regexp)
938 (setq regexp (car view-last-regexp))
939 (unless (setq no (not no)) (setq view-last-regexp regexp)))
940 (view-last-regexp (setq regexp view-last-regexp)
941 (if no (setq view-last-regexp (list regexp))))
942 (t (error "No previous View-mode search")))
943 (save-excursion
944 (if end (goto-char (if (< times 0) (point-max) (point-min)))
945 (move-to-window-line (if (< times 0) 0 -1)))
946 (if (if no (view-search-no-match-lines times regexp)
947 (re-search-forward regexp nil t times))
948 (setq where (point))))
949 (if where
950 (progn
951 (push-mark)
952 (goto-char where)
953 (if view-overlay
954 (move-overlay view-overlay (match-beginning 0) (match-end 0))
955 (setq view-overlay
956 (make-overlay (match-beginning 0) (match-end 0))))
957 (overlay-put view-overlay 'face view-highlight-face)
958 (beginning-of-line)
959 (view-recenter))
960 (message "Can't find occurrence %d of %s%s"
961 times (if no "no " "") regexp)
962 (sit-for 4))))
964 ;; This is the dumb approach, looking at each line. The original
965 ;; version of this function looked like it might have been trying to
966 ;; do something clever, but not succeeding:
967 ;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2007-09/msg00073.html
968 (defun view-search-no-match-lines (times regexp)
969 "Search for the TIMESth occurrence of a line with no match for REGEXP.
970 If such a line is found, return non-nil and set the match-data to that line.
971 If TIMES is negative, search backwards."
972 (let ((step (if (>= times 0) 1
973 (setq times (- times))
974 -1)))
975 ;; Note that we do not check the current line.
976 (while (and (> times 0)
977 (zerop (forward-line step)))
978 ;; (forward-line 1) returns 0 on moving within the last line.
979 (if (eobp)
980 (setq times -1)
981 (or (re-search-forward regexp (line-end-position) t)
982 (setq times (1- times))))))
983 (and (zerop times)
984 (looking-at ".*")))
986 (provide 'view)
988 ;;; view.el ends here