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