1 ;;; view.el --- peruse file or buffer without editing
3 ;; Copyright (C) 1985, 1989, 1994, 1995, 1997, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: K. Shane Hartman
7 ;; Maintainer: Inge Frick <inge@nada.kth.se>
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/>.
27 ;; This package provides the `view' minor mode documented in the Emacs
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.
48 "Peruse file or buffer without editing."
49 :link
'(function-link view-mode
)
50 :link
'(custom-manual "(emacs)Misc File Ops")
53 (defcustom view-highlight-face
'highlight
54 "The face used for highlighting the match found by View mode search."
58 ;; `view-mode-auto-exit' is replaced by the following option variable which
59 ;; only says if scrolling past buffer end should leave view mode or not, it
60 ;; doesn't say if leaving view mode should restore windows or not. The latter
61 ;; is now controlled by the presence of a value in `view-return-to-alist'.
62 (defcustom view-scroll-auto-exit nil
63 "Non-nil means scrolling past the end of buffer exits View mode.
64 A value of nil means attempting to scroll past the end of the buffer,
65 only rings the bell and gives a message on how to leave."
69 (defcustom view-try-extend-at-buffer-end nil
70 "Non-nil means try to load more of file when reaching end of buffer.
71 This variable is mainly intended to be temporarily set to non-nil by
72 the F command in view-mode, but you can set it to t if you want the action
73 for all scroll commands in view mode."
78 (defcustom view-remove-frame-by-deleting t
79 "Determine how View mode removes a frame no longer needed.
80 If nil, make an icon of the frame. If non-nil, delete the frame."
83 ;; Changed the default of this to t for Emacs 23. Users consider
84 ;; frame iconification annoying.
87 (defcustom view-exits-all-viewing-windows nil
88 "Non-nil means restore all windows used to view buffer.
89 Commands that restore windows when finished viewing a buffer, apply to all
90 windows that display the buffer and have restore information in
91 `view-return-to-alist'.
92 If `view-exits-all-viewing-windows' is nil, only the selected window is
93 considered for restoring."
97 (defcustom view-inhibit-help-message nil
98 "Non-nil inhibits the help message shown upon entering View mode."
104 (defvar view-mode nil
105 "Non-nil if View mode is enabled.
106 Don't change this variable directly, you must change it by one of the
107 functions that enable or disable view mode.")
109 (make-variable-buffer-local 'view-mode
)
111 (defcustom view-mode-hook nil
112 "Normal hook run when starting to view a buffer or file."
116 (defvar view-old-buffer-read-only nil
)
117 (make-variable-buffer-local 'view-old-buffer-read-only
)
119 (defvar view-old-Helper-return-blurb
)
120 (make-variable-buffer-local 'view-old-Helper-return-blurb
)
122 (defvar view-page-size nil
123 "Default number of lines to scroll by View page commands.
124 If nil that means use the window size.")
125 (make-variable-buffer-local 'view-page-size
)
127 (defvar view-half-page-size nil
128 "Default number of lines to scroll by View half page commands.
129 If nil that means use half the window size.")
130 (make-variable-buffer-local 'view-half-page-size
)
132 (defvar view-last-regexp nil
)
133 (make-variable-buffer-local 'view-last-regexp
) ; Global is better???
135 (defvar view-return-to-alist nil
136 "What to do with used windows and where to go when finished viewing buffer.
137 This is local in each buffer being viewed.
138 It is added to by `view-mode-enter' when starting to view a buffer and
139 subtracted from by `view-mode-exit' when finished viewing the buffer.
141 See RETURN-TO-ALIST argument of function `view-mode-exit' for the format of
142 `view-return-to-alist'.")
143 (make-variable-buffer-local 'view-return-to-alist
)
144 (put 'view-return-to-alist
'permanent-local t
)
146 (defvar view-exit-action nil
147 "If non-nil, a function with one argument (a buffer) called when finished viewing.
148 Commands like \\[view-file] and \\[view-file-other-window] may
149 set this to bury or kill the viewed buffer.
150 Observe that the buffer viewed might not appear in any window at
151 the time this function is called.")
152 (make-variable-buffer-local 'view-exit-action
)
154 (defvar view-no-disable-on-exit nil
155 "If non-nil, View mode \"exit\" commands don't actually disable View mode.
156 Instead, these commands just switch buffers or windows.
157 This is set in certain buffers by specialized features such as help commands
158 that use View mode automatically.")
160 (defvar view-overlay nil
161 "Overlay used to display where a search operation found its match.
162 This is local in each buffer, once it is used.")
163 (make-variable-buffer-local 'view-overlay
)
165 (unless (assq 'view-mode minor-mode-alist
)
166 (setq minor-mode-alist
167 (cons (list 'view-mode
169 'local-map mode-line-minor-mode-keymap
170 'help-echo
"mouse-3: minor mode menu"))
173 ;; Define keymap inside defvar to make it easier to load changes.
174 ;; Some redundant "less"-like key bindings below have been commented out.
175 (defvar view-mode-map
176 (let ((map (make-sparse-keymap)))
177 (define-key map
"C" 'View-kill-and-leave
)
178 (define-key map
"c" 'View-leave
)
179 (define-key map
"Q" 'View-quit-all
)
180 (define-key map
"E" 'View-exit-and-edit
)
181 ;; (define-key map "v" 'View-exit)
182 (define-key map
"e" 'View-exit
)
183 (define-key map
"q" 'View-quit
)
184 ;; (define-key map "N" 'View-search-last-regexp-backward)
185 (define-key map
"p" 'View-search-last-regexp-backward
)
186 (define-key map
"n" 'View-search-last-regexp-forward
)
187 ;; (define-key map "?" 'View-search-regexp-backward) ; Less does this.
188 (define-key map
"\\" 'View-search-regexp-backward
)
189 (define-key map
"/" 'View-search-regexp-forward
)
190 (define-key map
"r" 'isearch-backward
)
191 (define-key map
"s" 'isearch-forward
)
192 (define-key map
"m" 'point-to-register
)
193 (define-key map
"'" 'register-to-point
)
194 (define-key map
"x" 'exchange-point-and-mark
)
195 (define-key map
"@" 'View-back-to-mark
)
196 (define-key map
"." 'set-mark-command
)
197 (define-key map
"%" 'View-goto-percent
)
198 ;; (define-key map "G" 'View-goto-line-last)
199 (define-key map
"g" 'View-goto-line
)
200 (define-key map
"=" 'what-line
)
201 (define-key map
"F" 'View-revert-buffer-scroll-page-forward
)
202 ;; (define-key map "k" 'View-scroll-line-backward)
203 (define-key map
"y" 'View-scroll-line-backward
)
204 ;; (define-key map "j" 'View-scroll-line-forward)
205 (define-key map
"\n" 'View-scroll-line-forward
)
206 (define-key map
"\r" 'View-scroll-line-forward
)
207 (define-key map
"u" 'View-scroll-half-page-backward
)
208 (define-key map
"d" 'View-scroll-half-page-forward
)
209 (define-key map
"z" 'View-scroll-page-forward-set-page-size
)
210 (define-key map
"w" 'View-scroll-page-backward-set-page-size
)
211 ;; (define-key map "b" 'View-scroll-page-backward)
212 (define-key map
"\C-?" 'View-scroll-page-backward
)
213 ;; (define-key map "f" 'View-scroll-page-forward)
214 (define-key map
" " 'View-scroll-page-forward
)
215 (define-key map
"o" 'View-scroll-to-buffer-end
)
216 (define-key map
">" 'end-of-buffer
)
217 (define-key map
"<" 'beginning-of-buffer
)
218 (define-key map
"-" 'negative-argument
)
219 (define-key map
"9" 'digit-argument
)
220 (define-key map
"8" 'digit-argument
)
221 (define-key map
"7" 'digit-argument
)
222 (define-key map
"6" 'digit-argument
)
223 (define-key map
"5" 'digit-argument
)
224 (define-key map
"4" 'digit-argument
)
225 (define-key map
"3" 'digit-argument
)
226 (define-key map
"2" 'digit-argument
)
227 (define-key map
"1" 'digit-argument
)
228 (define-key map
"0" 'digit-argument
)
229 (define-key map
"H" 'describe-mode
)
230 (define-key map
"?" 'describe-mode
) ; Maybe do as less instead? See above.
231 (define-key map
"h" 'describe-mode
)
234 (or (assq 'view-mode minor-mode-map-alist
)
235 (setq minor-mode-map-alist
236 (cons (cons 'view-mode view-mode-map
) minor-mode-map-alist
)))
238 ;;; Commands that enter or exit view mode.
240 ;; This is used when view mode is exited, to make sure we don't try to
241 ;; kill a buffer modified by the user. A buffer in view mode can
242 ;; become modified if the user types C-x C-q, edits the buffer, then
243 ;; types C-x C-q again to return to view mode.
245 (defun kill-buffer-if-not-modified (buf)
246 "Like `kill-buffer', but does nothing if the buffer is modified."
247 (let ((buf (get-buffer buf
)))
248 (and buf
(not (buffer-modified-p buf
))
252 (defun view-file (file)
253 "View FILE in View mode, returning to previous buffer when done.
254 Emacs commands editing the buffer contents are not available; instead, a
255 special set of commands (mostly letters and punctuation) are defined for
256 moving around in the buffer.
257 Space scrolls forward, Delete scrolls backward.
258 For a list of all View commands, type H or h while viewing.
260 This command runs the normal hook `view-mode-hook'."
261 (interactive "fView file: ")
262 (unless (file-exists-p file
) (error "%s does not exist" file
))
263 (let ((had-a-buf (get-file-buffer file
))
264 (buffer (find-file-noselect file
)))
265 (view-buffer buffer
(and (not had-a-buf
) 'kill-buffer-if-not-modified
))))
268 (defun view-file-other-window (file)
269 "View FILE in View mode in another window.
270 When done, return that window to its previous buffer, and kill the
271 buffer visiting FILE if unmodified and if it wasn't visited before.
273 Emacs commands editing the buffer contents are not available; instead,
274 a special set of commands (mostly letters and punctuation)
275 are defined for moving around in the buffer.
276 Space scrolls forward, Delete scrolls backward.
277 For a list of all View commands, type H or h while viewing.
279 This command runs the normal hook `view-mode-hook'."
280 (interactive "fIn other window view file: ")
281 (unless (file-exists-p file
) (error "%s does not exist" file
))
282 (let ((had-a-buf (get-file-buffer file
))
283 (buf-to-view (find-file-noselect file
)))
284 (view-buffer-other-window buf-to-view nil
286 'kill-buffer-if-not-modified
))))
289 (defun view-file-other-frame (file)
290 "View FILE in View mode in another frame.
291 When done, kill the buffer visiting FILE if unmodified and if it wasn't
292 visited before; also, maybe delete other frame and/or return to previous
295 Emacs commands editing the buffer contents are not available; instead,
296 a special set of commands (mostly letters and punctuation)
297 are defined for moving around in the buffer.
298 Space scrolls forward, Delete scrolls backward.
299 For a list of all View commands, type H or h while viewing.
301 This command runs the normal hook `view-mode-hook'."
302 (interactive "fIn other frame view file: ")
303 (unless (file-exists-p file
) (error "%s does not exist" file
))
304 (let ((had-a-buf (get-file-buffer file
))
305 (buf-to-view (find-file-noselect file
)))
306 (view-buffer-other-frame buf-to-view nil
308 'kill-buffer-if-not-modified
))))
312 (defun view-buffer (buffer &optional exit-action
)
313 "View BUFFER in View mode, returning to previous buffer when done.
314 Emacs commands editing the buffer contents are not available; instead, a
315 special set of commands (mostly letters and punctuation) are defined for
316 moving around in the buffer.
317 Space scrolls forward, Delete scrolls backward.
318 For a list of all View commands, type H or h while viewing.
320 This command runs the normal hook `view-mode-hook'.
322 Optional argument EXIT-ACTION is either nil or a function with buffer as
323 argument. This function is called when finished viewing buffer. Use
324 this argument instead of explicitly setting `view-exit-action'.
326 Do not set EXIT-ACTION to `kill-buffer' when BUFFER visits a
327 file: Users may suspend viewing in order to modify the buffer.
328 Exiting View mode will then discard the user's edits. Setting
329 EXIT-ACTION to `kill-buffer-if-not-modified' avoids this."
330 (interactive "bView buffer: ")
331 (if (eq (with-current-buffer buffer
332 (get major-mode
'mode-class
))
335 (switch-to-buffer buffer
)
336 (message "Not using View mode because the major mode is special"))
337 (let ((undo-window (list (window-buffer) (window-start) (window-point))))
338 (switch-to-buffer buffer
)
339 (view-mode-enter (cons (selected-window) (cons nil undo-window
))
343 (defun view-buffer-other-window (buffer &optional not-return exit-action
)
344 "View BUFFER in View mode in another window.
345 Return to previous buffer when done, unless optional NOT-RETURN is
346 non-nil. Emacs commands editing the buffer contents are not available;
347 instead, a special set of commands (mostly letters and punctuation) are
348 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 EXIT-ACTION is either nil or a function with buffer as
355 argument. This function is called when finished viewing buffer. Use
356 this argument instead of explicitly setting `view-exit-action'."
357 (interactive "bIn other window view buffer:\nP")
358 (let* ((win ; This window will be selected by
359 (get-lru-window)) ; switch-to-buffer-other-window below.
361 (and (not not-return
)
362 (cons (selected-window)
363 (if (eq win
(selected-window))
364 t
; Has to make new window.
366 (window-buffer win
) ; Other windows old buffer.
368 (window-point win
)))))))
369 (switch-to-buffer-other-window buffer
)
370 (view-mode-enter (and return-to
(cons (selected-window) return-to
))
374 (defun view-buffer-other-frame (buffer &optional not-return exit-action
)
375 "View BUFFER in View mode in another frame.
376 Return to previous buffer when done, unless optional NOT-RETURN is
377 non-nil. Emacs commands editing the buffer contents are not available;
378 instead, a special set of commands (mostly letters and punctuation) are
379 defined for moving around in the buffer.
380 Space scrolls forward, Delete scrolls backward.
381 For a list of all View commands, type H or h while viewing.
383 This command runs the normal hook `view-mode-hook'.
385 Optional argument EXIT-ACTION is either nil or a function with buffer as
386 argument. This function is called when finished viewing buffer. Use
387 this argument instead of explicitly setting `view-exit-action'."
388 (interactive "bView buffer in other frame: \nP")
390 (and (not not-return
) (cons (selected-window) t
)))) ; Old window.
391 (switch-to-buffer-other-frame buffer
)
392 (view-mode-enter (and return-to
(cons (selected-window) return-to
))
396 (defun view-mode (&optional arg
)
397 ;; In the following documentation string we have to use some explicit key
398 ;; bindings instead of using the \\[] construction. The reason for this
399 ;; is that most commands have more than one key binding.
400 "Toggle View mode, a minor mode for viewing text but not editing it.
401 With prefix argument ARG, turn View mode on if ARG is positive, otherwise
404 Emacs commands that do not change the buffer contents are available as usual.
405 Kill commands insert text in kill buffers but do not delete. Other commands
406 \(among them most letters and punctuation) beep and tell that the buffer is
409 The following additional commands are provided. Most commands take prefix
410 arguments. Page commands default to \"page size\" lines which is almost a whole
411 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
412 and set \"half page size\" lines which initially is half a window full. Search
413 commands default to a repeat count of one.
415 H, h, ? This message.
416 Digits provide prefix arguments.
417 \\[negative-argument] negative prefix argument.
418 \\[beginning-of-buffer] move to the beginning of buffer.
419 > move to the end of buffer.
420 \\[View-scroll-to-buffer-end] scroll so that buffer end is at last line of window.
421 SPC scroll forward \"page size\" lines.
422 With prefix scroll forward prefix lines.
423 DEL scroll backward \"page size\" lines.
424 With prefix scroll backward prefix lines.
425 \\[View-scroll-page-forward-set-page-size] like \\[View-scroll-page-forward] but with prefix sets \"page size\" to prefix.
426 \\[View-scroll-page-backward-set-page-size] like \\[View-scroll-page-backward] but with prefix sets \"page size\" to prefix.
427 \\[View-scroll-half-page-forward] scroll forward \"half page size\" lines. With prefix, sets
428 \"half page size\" to prefix lines and scrolls forward that much.
429 \\[View-scroll-half-page-backward] scroll backward \"half page size\" lines. With prefix, sets
430 \"half page size\" to prefix lines and scrolls backward that much.
431 RET, LFD scroll forward one line. With prefix scroll forward prefix line(s).
432 y scroll backward one line. With prefix scroll backward prefix line(s).
433 \\[View-revert-buffer-scroll-page-forward] revert-buffer if necessary and scroll forward.
434 Use this to view a changing file.
435 \\[what-line] prints the current line number.
436 \\[View-goto-percent] goes prefix argument (default 100) percent into buffer.
437 \\[View-goto-line] goes to line given by prefix argument (default first line).
439 x exchanges point and mark.
440 \\[View-back-to-mark] return to mark and pops mark ring.
441 Mark ring is pushed at start of every successful search and when
442 jump to line occurs. The mark is set on jump to buffer start or end.
443 \\[point-to-register] save current position in character register.
444 ' go to position saved in character register.
445 s do forward incremental search.
446 r do reverse incremental search.
447 \\[View-search-regexp-forward] searches forward for regular expression, starting after current page.
448 ! and @ have a special meaning at the beginning of the regexp.
449 ! means search for a line with no match for regexp. @ means start
450 search at beginning (end for backward search) of buffer.
451 \\ searches backward for regular expression, starting before current page.
452 \\[View-search-last-regexp-forward] searches forward for last regular expression.
453 p searches backward for last regular expression.
454 \\[View-quit] quit View mode, restoring this window and buffer to previous state.
455 \\[View-quit] is the normal way to leave view mode.
456 \\[View-exit] exit View mode but stay in current buffer. Use this if you started
457 viewing a buffer (file) and find out you want to edit it.
458 This command restores the previous read-only status of the buffer.
459 \\[View-exit-and-edit] exit View mode, and make the current buffer editable
460 even if it was not editable before entry to View mode.
461 \\[View-quit-all] quit View mode, restoring all windows to previous state.
462 \\[View-leave] quit View mode and maybe switch buffers, but don't kill this buffer.
463 \\[View-kill-and-leave] quit View mode, kill current buffer and go back to other buffer.
465 The effect of \\[View-leave], \\[View-quit] and \\[View-kill-and-leave] depends on how view-mode was entered. If it was
466 entered by view-file, view-file-other-window, view-file-other-frame, or
467 \\[dired-view-file] \(\\[view-file], \\[view-file-other-window],
468 \\[view-file-other-frame], or the Dired mode v command),
469 then \\[View-quit] will try to kill the current buffer.
470 If view-mode was entered from another buffer, by \\[view-buffer],
471 \\[view-buffer-other-window], \\[view-buffer-other frame], \\[view-file],
472 \\[view-file-other-window], or \\[view-file-other-frame],
473 then \\[View-leave], \\[View-quit] and \\[View-kill-and-leave] will return to that buffer.
475 Entry to view-mode runs the normal hook `view-mode-hook'."
477 (unless (and arg
; Do nothing if already OK.
478 (if (> (prefix-numeric-value arg
) 0) view-mode
(not view-mode
)))
479 (if view-mode
(view-mode-disable)
480 (view-mode-enable))))
482 (defun view-mode-enable ()
484 ;; Always leave view mode before changing major mode.
485 ;; This is to guarantee that the buffer-read-only variable is restored.
486 (add-hook 'change-major-mode-hook
'view-mode-disable nil t
)
489 view-half-page-size nil
490 view-old-buffer-read-only buffer-read-only
492 (if (boundp 'Helper-return-blurb
)
493 (setq view-old-Helper-return-blurb
(and (boundp 'Helper-return-blurb
)
496 (format "continue viewing %s"
497 (if (buffer-file-name)
498 (file-name-nondirectory (buffer-file-name))
500 (force-mode-line-update)
501 (run-hooks 'view-mode-hook
))
503 (defun view-mode-disable ()
504 "Turn off View mode."
505 (remove-hook 'change-major-mode-hook
'view-mode-disable t
)
506 (and view-overlay
(delete-overlay view-overlay
))
507 (force-mode-line-update)
508 ;; Calling toggle-read-only while View mode is enabled
509 ;; sets view-read-only to t as a buffer-local variable
510 ;; after exiting View mode. That arranges that the next toggle-read-only
511 ;; will reenable View mode.
512 ;; Cancelling View mode in any other way should cancel that, too,
513 ;; so that View mode stays off if toggle-read-only is called.
514 (if (local-variable-p 'view-read-only
)
515 (kill-local-variable 'view-read-only
))
517 (if (boundp 'Helper-return-blurb
)
518 (setq Helper-return-blurb view-old-Helper-return-blurb
))
520 (setq buffer-read-only view-old-buffer-read-only
)))
523 (defun view-return-to-alist-update (buffer &optional item
)
524 "Update `view-return-to-alist' of buffer BUFFER.
525 Remove from `view-return-to-alist' all entries referencing dead
526 windows. Optional argument ITEM non-nil means add ITEM to
527 `view-return-to-alist' after purging. For a decsription of items
528 that can be added see the RETURN-TO-ALIST argument of the
529 function `view-mode-exit'. If `view-return-to-alist' contains an
530 entry for the selected window, purge that entry from
531 `view-return-to-alist' before adding ITEM."
532 (with-current-buffer buffer
533 (when view-return-to-alist
534 (let* ((list view-return-to-alist
)
535 entry entry-window last
)
537 (setq entry
(car list
))
538 (setq entry-window
(car entry
))
539 (if (and (windowp entry-window
)
540 (or (and item
(eq entry-window
(selected-window)))
541 (not (window-live-p entry-window
))))
542 ;; Remove that entry.
544 (setcdr last
(cdr list
))
545 (setq view-return-to-alist
546 (cdr view-return-to-alist
)))
547 ;; Leave entry alone.
549 (setq list
(cdr list
)))))
552 (setq view-return-to-alist
553 (cons item view-return-to-alist
)))))
556 (defun view-mode-enter (&optional return-to exit-action
)
557 "Enter View mode and set up exit from view mode depending on optional arguments.
558 RETURN-TO non-nil means add RETURN-TO as an element to the buffer
559 local alist `view-return-to-alist'. Save EXIT-ACTION in buffer
560 local variable `view-exit-action'. It should be either nil or a
561 function that takes a buffer as argument. This function will be
562 called by `view-mode-exit'.
564 RETURN-TO is either nil, meaning do nothing when exiting view
565 mode, or must have the format (WINDOW OLD-WINDOW . OLD-BUF-INFO).
566 WINDOW is the window used for viewing. OLD-WINDOW is nil or the
567 window to select after viewing. OLD-BUF-INFO tells what to do
568 with WINDOW when exiting. It is one of:
570 2) t Delete WINDOW or, if it is the only window and
571 `view-remove-frame-by-deleting' is non-nil, its
573 3) (OLD-BUFF START POINT) Display buffer OLD-BUFF with displayed text
574 starting at START and point at POINT in WINDOW.
575 4) quit-window Do `quit-window' in WINDOW.
576 5) keep-frame Like case 2) but do not delete the frame.
578 For a list of all View commands, type H or h while viewing.
580 This function runs the normal hook `view-mode-hook'."
582 (let ((entry (assq (car return-to
) view-return-to-alist
)))
584 (setcdr entry
(cdr return-to
))
585 (setq view-return-to-alist
(cons return-to view-return-to-alist
)))))
587 (setq view-exit-action exit-action
))
591 (force-mode-line-update)
592 (unless view-inhibit-help-message
594 (substitute-command-keys "\
595 View mode: type \\[help-command] for help, \\[describe-mode] for commands, \\[View-quit] to quit.")))))
597 (defun view-mode-exit (&optional return-to-alist exit-action all-win
)
598 "Exit View mode in various ways, depending on optional arguments.
599 RETURN-TO-ALIST, EXIT-ACTION and ALL-WIN determine what to do
600 after exit. EXIT-ACTION is nil or a function that is called with
601 current buffer as argument.
603 RETURN-TO-ALIST is an alist that, for some of the windows
604 displaying the current buffer, maintains information on what to
605 do when exiting those windows. If ALL-WIN is non-nil or the
606 variable `view-exits-all-viewing-windows' is non-nil,
607 view-mode-exit attempts to restore all windows showing the
608 current buffer to their old state. Otherwise, only the selected
609 window is affected (provided it is on RETURN-TO-ALIST).
611 Elements of RETURN-TO-ALIST must have the format
612 (WINDOW OLD-WINDOW . OLD-BUF-INFO) where
614 WINDOW is a window displaying the current buffer and OLD-WINDOW
615 is either nil or a window to select after viewing. OLD-BUF-INFO
616 provides information on what to do with WINDOW and may be one of:
618 2) t Delete WINDOW and, if it is the only window and
619 `view-remove-frame-by-deleting' is non-nil, its
621 3) (OLD-BUF START POINT) Display buffer OLD-BUF with displayed text
622 starting at START and point at POINT in WINDOW.
623 4) quit-window Do `quit-window' in WINDOW.
624 5) keep-frame Like case 2) but do not delete the frame.
626 If one of the WINDOW in RETURN-TO-ALIST is the selected window
627 and the corresponding OLD-WINDOW is a live window, then select
629 (when view-mode
; Only do something if in view mode.
632 (or all-win view-exits-all-viewing-windows
)))
633 (let* ((buffer (current-buffer))
635 (sel-old (assq (selected-window) return-to-alist
))
637 (all-win ; Try to restore all windows.
638 (append return-to-alist nil
)) ; Copy.
639 (sel-old ; Only selected window.
641 (old-window (if sel-old
(car (cdr sel-old
)))))
642 (if all-win
; Follow chains of old-windows.
643 (let ((c (length alist
)) a
)
644 (while (and (> c
0) ; Safety if mutually refering windows.
645 (or (not (window-live-p old-window
))
646 (eq buffer
(window-buffer old-window
)))
647 (setq a
(assq old-window alist
)))
649 (setq old-window
(car (cdr a
))))
650 (if (or (zerop c
) (not (window-live-p old-window
)))
651 (setq old-window
(selected-window)))))
652 (unless view-no-disable-on-exit
654 (while alist
; Restore windows with info.
656 (when (and (window-live-p (setq window
(car (car alist
))))
657 (eq buffer
(window-buffer window
)))
658 (let ((frame (window-frame window
))
659 (old-buf-info (cdr (cdr (car alist
)))))
660 (if all-win
(select-window window
))
662 ((consp old-buf-info
) ; Case 3.
663 (if (buffer-live-p (car old-buf-info
))
665 (set-window-buffer window
(car old-buf-info
)) ; old-buf
666 (set-window-start window
(car (cdr old-buf-info
)))
667 (set-window-point window
(car (cdr (cdr old-buf-info
)))))
669 ((eq old-buf-info
'quit-window
)
670 (quit-window)) ; Case 4.
671 (old-buf-info ; Case 2 or 5.
673 ((not (one-window-p t
)) ; Not only window.
675 ((eq old-buf-info
'keep-frame
) ; Case 5.
677 ((not (eq frame
(next-frame))) ; Case 2 and only window.
678 ;; Not the only frame, so can safely be removed.
679 (if view-remove-frame-by-deleting
681 (setq notlost t
) ; Keep the window. See below.
682 (iconify-frame frame
))))))))
683 ;; If a frame is removed by iconifying it, the window is not
684 ;; really lost. In this case we keep the entry in
685 ;; `view-return-to-alist' so that if the user deiconifies the
686 ;; frame and then hits q, the frame is iconified again.
688 (with-current-buffer buffer
689 (setq view-return-to-alist
690 (delete (car alist
) view-return-to-alist
))))
691 (setq alist
(cdr alist
)))
692 (when (window-live-p old-window
)
693 ;; old-window is still alive => select it.
694 (select-window old-window
))
696 ;; Don't do that: If the user wants to quit the *Help* buffer a
697 ;; second time it won't have any effect.
698 ;;(setq view-exit-action nil)
699 (funcall exit-action buffer
))
700 (force-mode-line-update))))
703 "Exit View mode but stay in current buffer."
708 (defun View-exit-and-edit ()
709 "Exit View mode and make the current buffer editable."
711 (let ((view-old-buffer-read-only nil
)
712 (view-no-disable-on-exit nil
))
716 "Quit View mode and maybe switch buffers, but don't kill this buffer."
718 (view-mode-exit view-return-to-alist
))
721 "Quit View mode, trying to restore window and buffer to previous state.
722 Maybe kill this buffer. Try to restore selected window to previous state
723 and go to previous buffer or window."
725 (view-mode-exit view-return-to-alist view-exit-action
))
727 (defun View-quit-all ()
728 "Quit View mode, trying to restore windows and buffers to previous state.
729 Maybe kill current buffer. Try to restore all windows viewing buffer to
730 previous state and go to previous buffer or window."
732 (view-mode-exit view-return-to-alist view-exit-action t
))
734 (defun View-kill-and-leave ()
735 "Quit View mode, kill current buffer and return to previous buffer."
737 (view-mode-exit view-return-to-alist
(or view-exit-action
'kill-buffer
) t
))
740 ;;; Some help routines.
742 (defun view-window-size ()
743 ;; Return the height of the current window, excluding the mode line.
744 ;; Using `window-line-height' accounts for variable-height fonts.
745 (let ((h (window-line-height -
1)))
748 ;; This should not happen, but if `window-line-height' returns
749 ;; nil, fall back on `window-height'.
750 (1- (window-height)))))
752 ;; (defun view-last-command (&optional who what)
753 ;; (setq view-last-command-entry this-command)
754 ;; (setq view-last-command who)
755 ;; (setq view-last-command-argument what))
757 ;; (defun View-repeat-last-command ()
758 ;; "Repeat last command issued in View mode."
760 ;; (if (and view-last-command
761 ;; (eq view-last-command-entry last-command))
762 ;; (funcall view-last-command view-last-command-argument))
763 ;; (setq this-command view-last-command-entry))
765 (defun view-recenter ()
766 ;; Recenter point in window and redisplay normally.
769 (defun view-page-size-default (lines)
770 ;; If LINES is nil, 0, or larger than `view-window-size', return nil.
771 ;; Otherwise, return LINES.
773 (not (zerop (setq lines
(prefix-numeric-value lines
))))
775 (abs (- (view-window-size) next-screen-context-lines
)))
778 (defun view-set-half-page-size-default (lines)
779 ;; Get and maybe set half page size.
780 (if (not lines
) (or view-half-page-size
781 (/ (view-window-size) 2))
782 (setq view-half-page-size
783 (if (zerop (setq lines
(prefix-numeric-value lines
)))
784 (/ (view-window-size) 2)
785 (view-page-size-default lines
)))))
788 ;;; Commands for moving around in the buffer.
790 (defun View-goto-percent (&optional percent
)
791 "Move to end (or prefix PERCENT) of buffer in View mode.
792 Display is centered at point.
793 Also set the mark at the position where point was."
799 (floor (* (- (point-max) (point-min)) 0.01
800 (max 0 (min 100 (prefix-numeric-value percent
))))))
804 ;; (defun View-goto-line-last (&optional line)
805 ;; "Move to last (or prefix LINE) line in View mode.
806 ;; Display is centered at LINE.
807 ;; Sets mark at starting position and pushes mark ring."
810 ;; (if line (goto-line (prefix-numeric-value line))
811 ;; (goto-char (point-max))
812 ;; (beginning-of-line))
815 (defun View-goto-line (&optional line
)
816 "Move to first (or prefix LINE) line in View mode.
817 Display is centered at LINE.
818 Also set the mark at the position where point was."
821 (goto-char (point-min))
822 (forward-line (1- line
))
825 (defun View-back-to-mark (&optional ignore
)
826 "Return to last mark set in View mode, else beginning of file.
827 Display that line at the center of the window.
828 This command pops the mark ring, so that successive
829 invocations return to earlier marks."
831 (goto-char (or (mark t
) (point-min)))
835 (defun view-scroll-lines (lines backward default maxdefault
)
836 ;; This function does the job for all the scrolling commands.
837 ;; Scroll forward LINES lines. If BACKWARD is non-nil, scroll backwards.
838 ;; If LINES is negative scroll in the other direction.
839 ;; If LINES is 0 or nil, scroll DEFAULT lines (if DEFAULT is nil, scroll
840 ;; by one page). If MAXDEFAULT is non-nil, scroll no more than a window.
841 (if (or (null lines
) (zerop (setq lines
(prefix-numeric-value lines
))))
842 (setq lines default
))
843 (when (and lines
(< lines
0))
844 (setq backward
(not backward
) lines
(- lines
)))
845 (when (and maxdefault lines
(> lines
(view-window-size)))
847 (cond (backward (scroll-down lines
))
848 ((view-really-at-end)
849 (if view-scroll-auto-exit
854 (if (view-really-at-end) (view-end-message)))))
856 (defun view-really-at-end ()
857 ;; Return true if buffer end visible. Maybe revert buffer and test.
858 (and (pos-visible-in-window-p (point-max))
859 (let ((buf (current-buffer))
860 (bufname (buffer-name))
861 (file (buffer-file-name)))
862 (or (not view-try-extend-at-buffer-end
)
864 (verify-visited-file-modtime buf
)
865 (not (file-exists-p file
))
866 (when (buffer-modified-p buf
)
867 (setq file
(file-name-nondirectory file
))
870 "File %s changed on disk. Discard your edits%s? "
872 (if (string= bufname file
) ""
873 (concat " in " bufname
))))))
875 (revert-buffer t t t
)
876 (pos-visible-in-window-p (point-max)))))))
878 (defun view-end-message ()
879 ;; Tell that we are at end of buffer.
880 (goto-char (point-max))
881 (if view-return-to-alist
882 (message "End of buffer. Type %s to quit viewing."
883 (substitute-command-keys
884 (if view-scroll-auto-exit
"\\[View-scroll-page-forward]"
886 (message "End of buffer")))
888 (defun View-scroll-to-buffer-end ()
889 "Scroll backward or forward so that buffer end is at last line of window."
891 (let ((p (if (pos-visible-in-window-p (point-max)) (point))))
892 (goto-char (point-max))
894 (and p
(goto-char p
))))
896 (defun View-scroll-page-forward (&optional lines
)
897 "Scroll \"page size\" or prefix LINES lines forward in View mode.
898 Exit if end of text is visible and `view-scroll-auto-exit' is non-nil.
899 \"page size\" is whole window full, or number of lines set by
900 \\[View-scroll-page-forward-set-page-size] or
901 \\[View-scroll-page-backward-set-page-size].
902 If LINES is more than a window-full, only the last window-full is shown."
904 (view-scroll-lines lines nil
(view-page-size-default view-page-size
) nil
))
906 (defun View-scroll-page-backward (&optional lines
)
907 "Scroll \"page size\" or prefix LINES lines backward in View mode.
908 See also `View-scroll-page-forward'."
910 (view-scroll-lines lines t
(view-page-size-default view-page-size
) nil
))
912 (defun View-scroll-page-forward-set-page-size (&optional lines
)
913 "Scroll forward LINES lines in View mode, setting the \"page size\".
914 This is the number of lines which \\[View-scroll-page-forward] and
915 \\[View-scroll-page-backward] scroll by default.
916 If LINES is omitted or = 0, sets \"page size\" to window height and
917 scrolls forward that much, otherwise scrolls forward LINES lines and sets
918 \"page size\" to the minimum of window height and the absolute value of LINES.
919 See also `View-scroll-page-forward'."
921 (view-scroll-lines lines nil
922 (setq view-page-size
(view-page-size-default lines
))
925 (defun View-scroll-page-backward-set-page-size (&optional lines
)
926 "Scroll backward prefix LINES lines in View mode, setting the \"page size\".
927 See also `View-scroll-page-forward-set-page-size'."
929 (view-scroll-lines lines t
930 (setq view-page-size
(view-page-size-default lines
))
933 (defun View-scroll-line-forward (&optional lines
)
934 "Scroll forward one line (or prefix LINES lines) in View mode.
935 See also `View-scroll-page-forward', but note that scrolling is limited
936 to minimum of LINES and one window-full."
938 (view-scroll-lines lines nil
1 t
))
940 (defun View-scroll-line-backward (&optional lines
)
941 "Scroll backward one line (or prefix LINES lines) in View mode.
942 See also `View-scroll-line-forward'."
944 (view-scroll-lines lines t
1 t
))
946 (defun View-scroll-half-page-forward (&optional lines
)
947 "Scroll forward a \"half page\" (or prefix LINES) lines in View mode.
948 If LINES is not omitted, the \"half page size\" is set to the minimum of
949 window height and the absolute value of LINES.
950 LINES=0 resets \"half page size\" to half window height."
952 (view-scroll-lines lines nil
(view-set-half-page-size-default lines
) t
))
954 (defun View-scroll-half-page-backward (&optional lines
)
955 "Scroll backward a \"half page\" (or prefix LINES) lines in View mode.
956 See also `View-scroll-half-page-forward'."
958 (view-scroll-lines lines t
(view-set-half-page-size-default lines
) t
))
960 (defun View-revert-buffer-scroll-page-forward (&optional lines
)
961 "Scroll forward, reverting buffer if needed, in View mode.
962 If buffer has not been changed and the corresponding file is newer, first
963 revert the buffer, then scroll.
964 This command is useful if you are viewing a changing file.
966 The prefix argument LINES says how many lines to scroll.
967 If you don't specify a prefix argument, it uses the number of lines set by
968 \\[View-scroll-page-forward-set-page-size] or
969 \\[View-scroll-page-backward-set-page-size].
970 If LINES is more than a window-full, only the last window-full is shown."
972 (let ((view-scroll-auto-exit nil
)
973 (view-try-extend-at-buffer-end t
))
974 (view-scroll-lines lines nil
(view-page-size-default view-page-size
) nil
)))
976 (defun View-search-regexp-forward (n regexp
)
977 "Search forward for first (or prefix Nth) occurrence of REGEXP in View mode.
979 Displays line found at center of window. Sets mark at starting position and
982 Characters @ and ! are special at the beginning of REGEXP. They modify
983 the search rather than become part of the pattern searched for.
984 @ means search all the buffer i.e. start search at the beginning of buffer.
985 ! means search for a line that contains no match for the pattern.
986 If REGEXP is empty or only consist of these control characters, then
987 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
988 for use by later search commands.
990 The variable `view-highlight-face' controls the face that is used
991 for highlighting the match that is found."
992 (interactive "p\nsSearch forward (regexp): ")
993 (view-search n regexp
))
995 (defun View-search-regexp-backward (n regexp
)
996 "Search backward for first (or prefix Nth) occurrence of REGEXP in View mode.
998 Displays line found at center of window. Sets mark at starting position and
1001 Characters @ and ! are special at the beginning of REGEXP. They modify
1002 the search rather than become part of the pattern searched for.
1003 @ means search all the buffer i.e. start search at the end of buffer.
1004 ! means search for a line that contains no match for the pattern.
1005 If REGEXP is empty or only consist of these control characters, then
1006 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
1007 for use by later search commands.
1009 The variable `view-highlight-face' controls the face that is used
1010 for highlighting the match that is found."
1011 (interactive "p\nsSearch backward (regexp): ")
1012 (view-search (- n
) regexp
))
1014 (defun View-search-last-regexp-forward (n) "\
1015 Search forward for first (or prefix Nth) instance of last regexp in View mode.
1016 Displays line found at center of window. Sets mark at starting position and
1019 The variable `view-highlight-face' controls the face that is used
1020 for highlighting the match that is found."
1022 (view-search n nil
))
1024 (defun View-search-last-regexp-backward (n) "\
1025 Search backward for first (or prefix Nth) instance of last regexp in View mode.
1026 Displays line found at center of window. Sets mark at starting position and
1029 The variable `view-highlight-face' controls the face that is used
1030 for highlighting the match that is found."
1032 (view-search (- n
) nil
))
1034 (defun view-search (times regexp
)
1035 ;; This function does the job for all the View-search- commands.
1036 ;; Search for the TIMESt match for REGEXP. If TIMES is negative
1037 ;; search backwards. If REGEXP is nil use `view-last-regexp'.
1038 ;; Characters "!" and "@" have a special meaning at the beginning of
1039 ;; REGEXP and are removed from REGEXP before the search "!" means
1040 ;; search for lines with no match for REGEXP. "@" means search in
1041 ;; the whole buffer, don't start searching from the present point.
1042 (let (where no end ln
)
1044 ((and regexp
(> (length regexp
) 0)
1045 (or (not (memq (string-to-char regexp
) '(?
! ?
@)))
1047 (if (member (substring regexp
0 2) '("!@" "@!"))
1048 (setq end t no t ln
2)
1049 (setq no
(not (setq end
(eq ?
@ (string-to-char regexp
))))
1051 (> (length (setq regexp
(substring regexp ln
))) 0))))
1052 (setq view-last-regexp
(if no
(list regexp
) regexp
)))
1053 ((consp view-last-regexp
)
1054 (setq regexp
(car view-last-regexp
))
1055 (unless (setq no
(not no
)) (setq view-last-regexp regexp
)))
1056 (view-last-regexp (setq regexp view-last-regexp
)
1057 (if no
(setq view-last-regexp
(list regexp
))))
1058 (t (error "No previous View-mode search")))
1060 (if end
(goto-char (if (< times
0) (point-max) (point-min)))
1061 (move-to-window-line (if (< times
0) 0 -
1)))
1062 (if (if no
(view-search-no-match-lines times regexp
)
1063 (re-search-forward regexp nil t times
))
1064 (setq where
(point))))
1070 (move-overlay view-overlay
(match-beginning 0) (match-end 0))
1072 (make-overlay (match-beginning 0) (match-end 0))))
1073 (overlay-put view-overlay
'face view-highlight-face
)
1076 (message "Can't find occurrence %d of %s%s"
1077 times
(if no
"no " "") regexp
)
1080 ;; This is the dumb approach, looking at each line. The original
1081 ;; version of this function looked like it might have been trying to
1082 ;; do something clever, but not succeeding:
1083 ;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2007-09/msg00073.html
1084 (defun view-search-no-match-lines (times regexp
)
1085 "Search for the TIMESth occurrence of a line with no match for REGEXP.
1086 If such a line is found, return non-nil and set the match-data to that line.
1087 If TIMES is negative, search backwards."
1088 (let ((step (if (>= times
0) 1
1089 (setq times
(- times
))
1091 ;; Note that we do not check the current line.
1092 (while (and (> times
0)
1093 (zerop (forward-line step
)))
1094 ;; (forward-line 1) returns 0 on moving within the last line.
1097 (or (re-search-forward regexp
(line-end-position) t
)
1098 (setq times
(1- times
))))))
1104 ;; arch-tag: 6d0ace36-1d12-4de3-8de3-1fa3231636d7
1105 ;;; view.el ends here