1 ;;; view.el --- peruse file or buffer without editing.
3 ;; Copyright (C) 1985, 1989, 1994, 1995, 1997 Free Software Foundation, Inc.
5 ;; Author: K. Shane Hartman
6 ;; Maintainer: Inge Frick <inge@nada.kth.se>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
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 (defvar view-highlight-face
'highlight
49 "*The face used for highlighting the match found by View mode search.")
51 ;; `view-mode-auto-exit' is replaced by the following option variable which
52 ;; only says if scrolling past buffer end should leave view mode or not, it
53 ;; doesn't say if leaving view mode should restore windows or not. The latter
54 ;; is now controlled by the presence of a value in `view-return-to-alist'.
56 (defvar view-scroll-auto-exit nil
57 "*Non-nil means scrolling past the end of buffer exits View mode.
58 nil means attempting to scroll past the end of the buffer,
59 only rings the bell and gives a message on how to leave.")
62 (defvar view-try-extend-at-buffer-end nil
63 "*Non-nil means try load more of file when reaching end of buffer.
64 This variable is mainly intended to be temporarily set to non-nil by
65 the F command in view-mode, but you can set it to t if you want the action
66 for all scroll commands in view mode.")
68 (defvar view-remove-frame-by-deleting nil
69 "*Determine how to View mode removes a frame no longer needed.
70 If nil, make an icon of the frame. If non-nil, delete the frame.")
73 (defvar view-exits-all-viewing-windows nil
74 "*Non-nil means restore all windows used to view buffer.
75 Commands that restore windows when finished viewing a buffer, apply to all
76 windows that display the buffer and have restore information in
77 `view-return-to-alist'.")
80 (defvar view-mode nil
"Non-nil if View mode is enabled.")
82 (make-variable-buffer-local 'view-mode
)
84 (defvar view-mode-hook nil
85 "Normal hook run when starting to view a buffer or file.")
87 (defvar view-old-buffer-read-only nil
)
88 (make-variable-buffer-local 'view-old-buffer-read-only
)
90 (defvar view-old-Helper-return-blurb
)
91 (make-variable-buffer-local 'view-old-Helper-return-blurb
)
93 (defvar view-page-size nil
94 "Default number of lines to scroll by View page commands.
95 If nil then the local value of this is initially set to window size.")
96 (make-variable-buffer-local 'view-page-size
)
98 (defvar view-half-page-size nil
99 "Default number of lines to scroll by View half page commands.
100 If nil then the local value of this is initially set to half window size.")
101 (make-variable-buffer-local 'view-half-page-size
)
103 (defvar view-last-regexp nil
)
104 (make-variable-buffer-local 'view-last-regexp
) ; Global is better???
106 (defvar view-return-to-alist nil
107 "What to do with used windows and where to go when finished viewing buffer.
108 This is local in each buffer being viewed.
109 It is added to by view-mode-enter when starting to view a buffer and
110 subtracted from by view-mode-exit when finished viewing the buffer.
112 See RETURN-TO-ALIST argument of function `view-mode-exit' for the format of
113 `view-return-to-alist'.")
114 (make-variable-buffer-local 'view-return-to-alist
)
116 (defvar view-exit-action nil
117 "nil or a function with one argument (a buffer) called when finished viewing.
118 This is local in each buffer being viewed.
119 The \\[view-file] and \\[view-file-other-window] commands may set this to
121 (make-variable-buffer-local 'view-exit-action
)
123 (defvar view-no-disable-on-exit nil
124 "If non-nil, View mode \"exit\" commands don't actually disable View mode.
125 Instead, these commands just switch buffers or windows.
126 This is set in certain buffers by specialized features such as help commands
127 that use View mode automatically.")
129 (defvar view-overlay nil
130 "Overlay used to display where a search operation found its match.
131 This is local in each buffer, once it is used.")
132 (make-variable-buffer-local 'view-overlay
)
134 (or (assq 'view-mode minor-mode-alist
)
135 (setq minor-mode-alist
136 (cons '(view-mode " View") minor-mode-alist
)))
138 ;; Define keymap inside defvar to make it easier to load changes.
139 (defvar view-mode-map
140 (let ((map (make-sparse-keymap)))
141 (define-key map
"C" 'View-kill-and-leave
)
142 (define-key map
"c" 'View-leave
)
143 (define-key map
"Q" 'View-quit-all
)
144 (define-key map
"E" 'View-exit-and-edit
)
145 ; (define-key map "v" 'View-exit)
146 (define-key map
"e" 'View-exit
)
147 (define-key map
"q" 'View-quit
)
148 ; (define-key map "N" 'View-search-last-regexp-backward)
149 (define-key map
"p" 'View-search-last-regexp-backward
)
150 (define-key map
"n" 'View-search-last-regexp-forward
)
151 ; (define-key map "?" 'View-search-regexp-backward) ; Less does this.
152 (define-key map
"\\" 'View-search-regexp-backward
)
153 (define-key map
"/" 'View-search-regexp-forward
)
154 (define-key map
"r" 'isearch-backward
)
155 (define-key map
"s" 'isearch-forward
)
156 (define-key map
"m" 'point-to-register
)
157 (define-key map
"'" 'register-to-point
)
158 (define-key map
"x" 'exchange-point-and-mark
)
159 (define-key map
"@" 'View-back-to-mark
)
160 (define-key map
"." 'set-mark-command
)
161 (define-key map
"%" 'View-goto-percent
)
162 ; (define-key map "G" 'View-goto-line-last)
163 (define-key map
"g" 'View-goto-line
)
164 (define-key map
"=" 'what-line
)
165 (define-key map
"F" 'View-revert-buffer-scroll-page-forward
)
166 ; (define-key map "k" 'View-scroll-line-backward)
167 (define-key map
"y" 'View-scroll-line-backward
)
168 ; (define-key map "j" 'View-scroll-line-forward)
169 (define-key map
"\n" 'View-scroll-line-forward
)
170 (define-key map
"\r" 'View-scroll-line-forward
)
171 (define-key map
"u" 'View-scroll-half-page-backward
)
172 (define-key map
"d" 'View-scroll-half-page-forward
)
173 (define-key map
"z" 'View-scroll-page-forward-set-page-size
)
174 (define-key map
"w" 'View-scroll-page-backward-set-page-size
)
175 ; (define-key map "b" 'View-scroll-page-backward)
176 (define-key map
"\C-?" 'View-scroll-page-backward
)
177 ; (define-key map "f" 'View-scroll-page-forward)
178 (define-key map
" " 'View-scroll-page-forward
)
179 (define-key map
"o" 'View-scroll-to-buffer-end
)
180 (define-key map
">" 'end-of-buffer
)
181 (define-key map
"<" 'beginning-of-buffer
)
182 (define-key map
"-" 'negative-argument
)
183 (define-key map
"9" 'digit-argument
)
184 (define-key map
"8" 'digit-argument
)
185 (define-key map
"7" 'digit-argument
)
186 (define-key map
"6" 'digit-argument
)
187 (define-key map
"5" 'digit-argument
)
188 (define-key map
"4" 'digit-argument
)
189 (define-key map
"3" 'digit-argument
)
190 (define-key map
"2" 'digit-argument
)
191 (define-key map
"1" 'digit-argument
)
192 (define-key map
"0" 'digit-argument
)
193 (define-key map
"H" 'describe-mode
)
194 (define-key map
"?" 'describe-mode
) ; Maybe do as less instead?
195 (define-key map
"h" 'describe-mode
)
198 (or (assq 'view-mode minor-mode-map-alist
)
199 (setq minor-mode-map-alist
200 (cons (cons 'view-mode view-mode-map
) minor-mode-map-alist
)))
202 ;;; Commands that enter or exit view mode.
205 (defun view-file (file-name)
206 "View FILE in View mode, returning to previous buffer when done.
207 Emacs commands editing the buffer contents are not available; instead,
208 a special set of commands (mostly letters and punctuation)
209 are defined for moving around in the buffer.
210 Space scrolls forward, Delete scrolls backward.
211 For list of all View commands, type H or h while viewing.
213 This command runs the normal hook `view-mode-hook'."
214 (interactive "fView file: ")
215 (let ((had-a-buf (get-file-buffer file-name
)))
216 (view-buffer (find-file-noselect file-name
)
217 (and (not had-a-buf
) 'kill-buffer
))))
220 (defun view-file-other-window (file-name)
221 "View FILE in View mode in another window.
222 Return that window to its previous buffer when done.
223 Emacs commands editing the buffer contents are not available; instead,
224 a special set of commands (mostly letters and punctuation)
225 are defined for moving around in the buffer.
226 Space scrolls forward, Delete scrolls backward.
227 For list of all View commands, type H or h while viewing.
229 This command runs the normal hook `view-mode-hook'."
230 (interactive "fIn other window view file: ")
231 (let ((had-a-buf (get-file-buffer file-name
)))
232 (view-buffer-other-window (find-file-noselect file-name
) nil
233 (and (not had-a-buf
) 'kill-buffer
))))
236 (defun view-file-other-frame (file-name)
237 "View FILE in View mode in another frame.
238 Maybe delete other frame and/or return to previous buffer when done.
239 Emacs commands editing the buffer contents are not available; instead,
240 a special set of commands (mostly letters and punctuation)
241 are defined for moving around in the buffer.
242 Space scrolls forward, Delete scrolls backward.
243 For list of all View commands, type H or h while viewing.
245 This command runs the normal hook `view-mode-hook'."
246 (interactive "fIn other frame view file: ")
247 (let ((had-a-buf (get-file-buffer file-name
)))
248 (view-buffer-other-frame (find-file-noselect file-name
) nil
249 (and (not had-a-buf
) 'kill-buffer
))))
253 (defun view-buffer (buffer-name &optional exit-action
)
254 "View BUFFER in View mode, returning to previous buffer when done.
255 Emacs commands editing the buffer contents are not available; instead,
256 a special set of commands (mostly letters and punctuation)
257 are defined for moving around in the buffer.
258 Space scrolls forward, Delete scrolls backward.
259 For list of all View commands, type H or h while viewing.
261 This command runs the normal hook `view-mode-hook'.
263 Optional argument EXIT-ACTION is either nil or a function with buffer as
264 argument. This function is called when finished viewing buffer.
265 Use this argument instead of explicitly setting `view-exit-action'."
267 (interactive "bView buffer: ")
268 (let ((undo-window (list (window-buffer) (window-start) (window-point))))
269 (switch-to-buffer buffer-name
)
270 (view-mode-enter (cons (selected-window) (cons nil undo-window
))
274 (defun view-buffer-other-window
275 (buffer-name &optional not-return exit-action
)
276 "View BUFFER in View mode in another window.
277 Return to previous buffer when done, unless optional NOT-RETURN is non-nil.
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 list of all View commands, type H or h while viewing.
284 This command runs the normal hook `view-mode-hook'.
286 Optional argument EXIT-ACTION is either nil or a function with buffer as
287 argument. This function is called when finished viewing buffer.
288 Use this argument instead of explicitly setting `view-exit-action'."
289 (interactive "bIn other window view buffer:\nP")
290 (let* ((win ; This window will be selected by
291 (get-lru-window)) ; switch-to-buffer-other-window below.
293 (and (not not-return
)
294 (cons (selected-window)
295 (if (eq win
(selected-window))
296 t
; Has to make new window.
298 (window-buffer win
) ; Other windows old buffer.
300 (window-point win
)))))))
301 (switch-to-buffer-other-window buffer-name
)
302 (view-mode-enter (and return-to
(cons (selected-window) return-to
))
306 (defun view-buffer-other-frame
307 (buffer-name &optional not-return exit-action
)
308 "View BUFFER in View mode in another frame.
309 Return to previous buffer when done, unless optional NOT-RETURN is non-nil.
310 Emacs commands editing the buffer contents are not available; instead,
311 a special set of commands (mostly letters and punctuation)
312 are defined for moving around in the buffer.
313 Space scrolls forward, Delete scrolls backward.
314 For list of all View commands, type H or h while viewing.
316 This command runs the normal hook `view-mode-hook'.
318 Optional argument EXIT-ACTION is either nil or a function with buffer as
319 argument. This function is called when finished viewing buffer.
320 Use this argument instead of explicitly setting `view-exit-action'."
321 (interactive "bView buffer in other frame: \nP")
323 (and (not not-return
) (cons (selected-window) t
)))) ; Old window.
324 (switch-to-buffer-other-frame buffer-name
)
325 (view-mode-enter (and return-to
(cons (selected-window) return-to
))
329 (defun view-mode (&optional arg
)
330 ;; In the following documentation string we have to use some explicit key
331 ;; bindings instead of using the \\[] construction. The reason for this
332 ;; is that most commands have more than one key binding.
333 "Toggle View mode, a minor mode for viewing text but not editing it.
334 With arg, turn View mode on iff arg is positive.
336 Emacs commands that do not change the buffer contents are available as usual.
337 Kill commands insert text in kill buffers but do not delete. Other commands
338 \(among them most letters and punctuation) beep and tell that the buffer is
341 The following additional commands are provided. Most commands take prefix
342 arguments. Page commands default to \"page size\" lines which is almost a whole
343 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
344 and set \"half page size\" lines which initially is half a window full. Search
345 commands default to a repeat count of one.
347 H, h, ? This message.
348 Digits provide prefix arguments.
349 \\[negative-argument] negative prefix argument.
350 \\[beginning-of-buffer] move to the beginning of buffer.
351 > move to the end of buffer.
352 \\[View-scroll-to-buffer-end] scroll so that buffer end is at last line of window.
353 SPC scroll forward prefix (default \"page size\") lines.
354 DEL scroll backward prefix (default \"page size\") lines.
355 \\[View-scroll-page-forward-set-page-size] like \\[View-scroll-page-forward] except prefix sets \"page size\".
356 \\[View-scroll-page-backward-set-page-size] like \\[View-scroll-page-backward] except prefix sets \"page size\".
357 \\[View-scroll-half-page-forward] scroll forward (and if prefix set) \"half page size\" lines.
358 \\[View-scroll-half-page-backward] scroll backward (and if prefix set) \"half page size\" lines.
359 RET, LFD scroll forward prefix (default one) line(s).
360 y scroll backward prefix (default one) line(s).
361 \\[View-revert-buffer-scroll-page-forward] revert-buffer if necessary and scroll forward.
362 Use this to view a changing file.
363 \\[what-line] prints the current line number.
364 \\[View-goto-percent] goes prefix argument (default 100) percent into buffer.
365 \\[View-goto-line] goes to line given by prefix argument (default first line).
367 x exchanges point and mark.
368 \\[View-back-to-mark] return to mark and pops mark ring.
369 Mark ring is pushed at start of every successful search and when
370 jump to line occurs. The mark is set on jump to buffer start or end.
371 \\[point-to-register] save current position in character register.
372 ' go to position saved in character register.
373 s do forward incremental search.
374 r do reverse incremental search.
375 \\[View-search-regexp-forward] searches forward for regular expression, starting after current page.
376 ! and @ have a special meaning at the beginning of the regexp.
377 ! means search for a line with no match for regexp. @ means start
378 search at beginning (end for backward search) of buffer.
379 \\ searches backward for regular expression, starting before current page.
380 \\[View-search-last-regexp-forward] searches forward for last regular expression.
381 p searches backward for last regular expression.
382 \\[View-quit] quit View mode, trying to restore window and buffer to previous state.
383 \\[View-quit] is the normal way to leave view mode.
384 \\[View-exit] exit View mode but stay in current buffer. Use this if you started
385 viewing a buffer (file) and find out you want to edit it.
386 \\[View-exit-and-edit] exit View mode and make the current buffer editable.
387 \\[View-quit-all] quit View mode, trying to restore windows and buffer to previous state.
388 \\[View-leave] quit View mode and maybe switch buffers, but don't kill this buffer.
389 \\[View-kill-and-leave] quit View mode, kill current buffer and go back to other buffer.
391 The effect of \\[View-leave] , \\[View-quit] and \\[View-kill-and-leave] depends on how view-mode was entered. If it was
392 entered by view-file, view-file-other-window or view-file-other-frame (\\[view-file],
393 \\[view-file-other-window], \\[view-file-other-frame] or the dired mode v command), then \\[View-quit] will try to kill the
394 current buffer. If view-mode was entered from another buffer as is done by
395 View-buffer, View-buffer-other-window, View-buffer-other frame, View-file,
396 View-file-other-window or View-file-other-frame then \\[view-leave] , \\[view-quit] and \\[view-kill-and-leave] will return
399 Entry to view-mode runs the normal hook `view-mode-hook'."
403 (if (> (prefix-numeric-value arg
) 0) view-mode
(not view-mode
)))
404 ()) ; Do nothing if already OK.
405 (view-mode (view-mode-disable))
406 (t (view-mode-enable))))
408 (defun view-mode-enable ()
410 ;; Always leave view mode before changing major mode.
411 ;; This is to guarantee that the buffer-read-only variable is restored.
412 (make-local-hook 'change-major-mode-hook
)
413 (add-hook 'change-major-mode-hook
'view-mode-disable nil t
)
415 view-page-size
(view-page-size-default view-page-size
)
416 view-half-page-size
(or view-half-page-size
(/ (view-window-size) 2))
417 view-old-buffer-read-only buffer-read-only
419 view-old-Helper-return-blurb
(and (boundp 'Helper-return-blurb
)
422 (format "continue viewing %s"
423 (if (buffer-file-name)
424 (file-name-nondirectory (buffer-file-name))
426 (run-hooks 'view-mode-hook
))
428 (defun view-mode-disable ()
429 "Turn off View mode."
430 (remove-hook 'change-major-mode-hook
'view-mode-disable t
)
431 (and view-overlay
(delete-overlay view-overlay
))
433 Helper-return-blurb view-old-Helper-return-blurb
434 buffer-read-only view-old-buffer-read-only
))
437 (defun view-mode-enter (&optional return-to exit-action
) "\
438 Enter View mode and set up exit from view mode depending on optional arguments.
439 If RETURN-TO is non-nil it is added as an element to the buffer local alist
440 view-return-to-alist.
441 Save EXIT-ACTION in buffer local variable `view-exit-action'.
442 It should be either nil or a function that takes a buffer as argument.
443 This function will be called by `view-mode-exit'.
445 RETURN-TO is either nil, meaning do nothing when exiting view mode, or
446 it has the format (WINDOW OLD-WINDOW . OLD-BUF-INFO).
447 WINDOW is a window used for viewing.
448 OLD-WINDOW is nil or the window to select after viewing.
449 OLD-BUF-INFO tells what to do with WINDOW when exiting. It is one of:
451 2) t Delete WINDOW or, if it is the only window, its frame.
452 3) (OLD-BUFF START POINT) Display buffer OLD-BUFF with displayed text
453 starting at START and point at POINT in WINDOW.
455 For list of all View commands, type H or h while viewing.
457 This function runs the normal hook `view-mode-hook'."
459 (let ((entry (assq (car return-to
) view-return-to-alist
)))
460 (if entry
(setcdr entry
(cdr return-to
))
461 (setq view-return-to-alist
(cons return-to view-return-to-alist
)))))
462 (if view-mode
; Do nothing if already in view mode.
465 (if exit-action
(setq view-exit-action exit-action
))
466 (force-mode-line-update)
468 (substitute-command-keys "\
469 Type \\[help-command] for help, \\[describe-mode] for commands, \\[View-quit] to quit."))))
471 (defun view-mode-exit (&optional return-to-alist exit-action all-win
)
472 "Exit view-mode in various ways, depending on optional arguments.
473 RETURN-TO-ALIST, EXIT-ACTION and ALL-WIN determine what to do after
475 EXIT-ACTION is nil or a function that is called with current buffer as
477 RETURN-TO-ALIST is an alist that for some of the windows displaying the
478 current buffer, associate information on what to do with those windows.
479 If ALL-WIN or the variable `view-exits-all-viewing-windows' is non-nil,
480 then all windows on RETURN-TO-ALIST are restored to their old state.
481 Otherwise only the selected window is affected (if it is on RETURN-TO-ALIST).
483 Elements of RETURN-TO-ALIST have the format (WINDOW OLD-WINDOW . OLD-BUF-INFO).
484 WINDOW is a window displaying the current buffer.
485 OLD-WINDOW is nil or a window to select after viewing.
486 OLD-BUF-INFO is information on what to do with WINDOW and is one of:
488 2) t Delete WINDOW or, if it is the only window, its frame.
489 3) (OLD-BUF START POINT) Display buffer OLD-BUF with displayed text
490 starting at START and point at POINT in WINDOW.
492 If one of the WINDOW in RETURN-TO-ALIST is the selected window and the
493 corresponding OLD-WINDOW is a live window, then select OLD-WINDOW."
495 (and return-to-alist
(or all-win view-exits-all-viewing-windows
)))
496 (if view-mode
; Only do something if in view mode.
497 (let* ((buffer (current-buffer))
499 (sel-old (assq (selected-window) return-to-alist
))
501 (all-win ; Try to restore all windows.
502 (append return-to-alist nil
)) ; Copy.
503 (sel-old ; Only selected window.
505 (old-window (if sel-old
(car (cdr sel-old
)))))
506 (if all-win
; Follow chains of old-windows.
507 (let ((c (length alist
)) a
)
508 (while (and (> c
0) ; Safety if mutually refering windows.
509 (or (not (window-live-p old-window
))
510 (eq buffer
(window-buffer old-window
)))
511 (setq a
(assq old-window alist
)))
513 (setq old-window
(car (cdr a
))))
514 (if (or (zerop c
) (not (window-live-p old-window
)))
515 (setq old-window
(selected-window)))))
516 (or view-no-disable-on-exit
518 (while alist
; Restore windows with info.
519 (if (and (window-live-p (setq window
(car (car alist
))))
520 (eq buffer
(window-buffer window
)))
521 (let ((frame (window-frame window
))
522 (old-buf-info (cdr (cdr (car alist
)))))
523 (if all-win
(select-window window
))
525 ((and (consp old-buf-info
) ; Case 3.
526 (buffer-live-p (car old-buf-info
)))
527 (set-window-buffer window
(car old-buf-info
)) ; old-buf
528 (set-window-start window
(car (cdr old-buf-info
)))
529 (set-window-point window
(car (cdr (cdr old-buf-info
)))))
530 ((not (eq old-buf-info t
)) nil
) ; Not case 2, do nothing.
531 ((not (one-window-p t
)) (delete-window))
532 ((not (eq frame
(next-frame)))
533 ;; Not the only frame, so can safely be removed.
534 (if view-remove-frame-by-deleting
536 (iconify-frame frame
))))))
537 (setq view-return-to-alist
(delete (car alist
) view-return-to-alist
))
538 (setq alist
(cdr alist
)))
539 (if (window-live-p old-window
) ; still existing window
540 (select-window old-window
))
541 ; (if (and exit-action (not (get-buffer-window buffer)))
543 (progn (setq view-exit-action nil
)
544 (funcall exit-action buffer
)))
545 (force-mode-line-update))))
548 "Exit View mode but stay in current buffer."
553 (defun View-exit-and-edit ()
554 "Exit View mode and make the current buffer editable."
556 (let ((view-old-buffer-read-only nil
))
560 "Quit View mode and maybe switch buffers, but don't kill this buffer."
562 (view-mode-exit view-return-to-alist
))
565 "Quit View mode, trying to restore window and buffer to previous state.
566 Maybe kill this buffer. Try to restore selected window to previous state
567 and go to previous buffer or window."
569 (view-mode-exit view-return-to-alist view-exit-action
))
571 (defun View-quit-all ()
572 "Quit View mode, trying to restore windows and buffers to previous state.
573 Maybe kill current buffer. Try to restore all windows viewing buffer to
574 previous state and go to previous buffer or window."
576 (view-mode-exit view-return-to-alist view-exit-action t
))
578 (defun View-kill-and-leave ()
579 "Quit View mode, kill current buffer and return to previous buffer."
581 (view-mode-exit view-return-to-alist
(or view-exit-action
'kill-buffer
) t
))
584 ;;; Some help routines.
586 (defun view-window-size ()
587 ;; Window height excluding mode line.
588 (1- (window-height)))
590 ;(defun view-last-command (&optional who what)
591 ; (setq view-last-command-entry this-command)
592 ; (setq view-last-command who)
593 ; (setq view-last-command-argument what))
595 ;(defun View-repeat-last-command ()
596 ; "Repeat last command issued in View mode."
598 ; (if (and view-last-command
599 ; (eq view-last-command-entry last-command))
600 ; (funcall view-last-command view-last-command-argument))
601 ; (setq this-command view-last-command-entry))
603 (defun view-recenter ()
604 ;; Center point in window.
605 (recenter (/ (view-window-size) 2)))
607 (defun view-page-size-default (lines)
609 (let ((default (- (view-window-size) next-screen-context-lines
)))
610 (if (or (null lines
) (zerop (setq lines
(prefix-numeric-value lines
))))
612 (min (abs lines
) default
))))
614 (defun view-set-half-page-size-default (lines)
615 ;; Get and maybe set half page size.
616 (if (not lines
) view-half-page-size
617 (setq view-half-page-size
618 (if (zerop (setq lines
(prefix-numeric-value lines
)))
619 (/ (view-window-size) 2)
620 (view-page-size-default lines
)))))
623 ;;; Commands for moving around in the buffer.
625 (defun View-goto-percent (&optional percent
)
626 "Move to end (or prefix PERCENT) of buffer in View mode.
627 Display is centered at point.
628 Also set the mark at the position where point was."
634 (floor (* (- (point-max) (point-min)) 0.01
635 (max 0 (min 100 (prefix-numeric-value percent
))))))
639 ;(defun View-goto-line-last (&optional line)
640 ;"Move to last (or prefix LINE) line in View mode.
641 ;Display is centered at LINE.
642 ;Sets mark at starting position and pushes mark ring."
645 ; (if line (goto-line (prefix-numeric-value line))
646 ; (goto-char (point-max))
647 ; (beginning-of-line))
650 (defun View-goto-line (&optional line
)
651 "Move to first (or prefix LINE) line in View mode.
652 Display is centered at LINE.
653 Also set the mark at the position where point was."
659 (defun View-scroll-to-buffer-end ()
660 "Scroll backward or forward so that buffer end is at last line of window."
662 (let ((p (if (pos-visible-in-window-p (point-max)) (point))))
663 (goto-char (point-max))
665 (and p
(goto-char p
))))
667 (defun view-scroll-lines (lines backward default maxdefault
)
668 ;; This function does the job for all the scrolling commands.
669 ;; Scroll forward LINES lines. If BACKWARD is true scroll backwards.
670 ;; If LINES is negative scroll in the other direction. If LINES is 0 or nil,
671 ;; scroll DEFAULT lines. If MAXDEFAULT is true then scroll no more than a
673 (if (or (null lines
) (zerop (setq lines
(prefix-numeric-value lines
))))
674 (setq lines default
))
676 (progn (setq backward
(not backward
)) (setq lines
(- lines
))))
677 (setq default
(view-page-size-default nil
)) ; Max scrolled at a time.
678 (if maxdefault
(setq lines
(min lines default
)))
680 (backward (scroll-down lines
))
681 ((view-really-at-end)
682 (if view-scroll-auto-exit
(View-quit)
685 (t (while (> lines default
)
687 (setq lines
(- lines default
))
688 (if (view-really-at-end) (setq lines
0)))
690 (if (view-really-at-end) (view-end-message))
691 (move-to-window-line -
1)
692 (beginning-of-line))))
694 (defun view-really-at-end ()
695 ;; Return true if buffer end visible. Maybe revert buffer and test.
696 (and (pos-visible-in-window-p (point-max))
697 (let ((buf (current-buffer))
698 (bufname (buffer-name))
699 (file (buffer-file-name)))
700 (or (not view-try-extend-at-buffer-end
)
702 (verify-visited-file-modtime buf
)
703 (not (file-exists-p file
))
704 (and (buffer-modified-p buf
)
705 (setq file
(file-name-nondirectory file
))
708 "File %s changed on disk. Discard your edits%s? "
710 (if (string= bufname file
) ""
711 (concat " in " bufname
))))))
712 (progn (revert-buffer t t t
)
713 (pos-visible-in-window-p (point-max)))))))
715 (defun view-end-message ()
716 ;; Tell that we are at end of buffer.
717 (goto-char (point-max))
718 (message "End of buffer. Type %s to quit viewing."
719 (substitute-command-keys
720 (if view-scroll-auto-exit
"\\[View-scroll-page-forward]"
723 (defun View-scroll-page-forward (&optional lines
)
724 "Scroll \"page size\" or prefix LINES lines forward in View mode.
725 Exit if end of text is visible and `view-scroll-auto-exit' is non-nil.
726 \"page size\" is whole window full, or number of lines set by
727 \\[View-scroll-page-forward-set-page-size] or
728 \\[View-scroll-page-backward-set-page-size].
729 If LINES is more than a window-full, only the last window-full is shown."
731 (view-scroll-lines lines nil view-page-size nil
))
733 (defun View-scroll-page-backward (&optional lines
)
734 "Scroll \"page size\" or prefix LINES lines backward in View mode.
735 See also `View-scroll-page-forward'."
737 (view-scroll-lines lines t view-page-size nil
))
739 (defun View-scroll-page-forward-set-page-size (&optional lines
)
740 "Scroll forward LINES lines in View mode, setting the \"page size\".
741 This is the number of lines which \\[View-scroll-page-forward] and
742 \\[View-scroll-page-backward] scroll by default.
743 If LINES is omitted or = 0, sets \"page size\" to window height and
744 scrolls forward that much, otherwise scrolls forward LINES lines and sets
745 \"page size\" to the minimum of window height and the absolute value of LINES.
746 See also `View-scroll-page-forward'."
748 (view-scroll-lines lines nil
749 (setq view-page-size
(view-page-size-default lines
))
752 (defun View-scroll-page-backward-set-page-size (&optional lines
)
753 "Scroll backward prefix LINES lines in View mode, setting the \"page size\".
754 See also `View-scroll-page-forward-set-page-size'."
756 (view-scroll-lines lines t
757 (setq view-page-size
(view-page-size-default lines
))
760 (defun View-scroll-line-forward (&optional lines
)
761 "Scroll forward one line (or prefix LINES lines) in View mode.
762 See also `View-scroll-page-forward,' but note that scrolling is limited
763 to minimum of LINES and one window-full."
765 (view-scroll-lines lines nil
1 t
))
767 (defun View-scroll-line-backward (&optional lines
)
768 "Scroll backward one line (or prefix LINES lines) in View mode.
769 See also `View-scroll-line-forward'."
771 (view-scroll-lines lines t
1 t
))
773 (defun View-scroll-half-page-forward (&optional lines
)
774 "Scroll forward a \"half page\" (or prefix LINES) lines in View mode.
775 If LINES is not omitted, the \"half page size\" is set to the minimum of
776 window height and the absolute value of LINES.
777 LINES=0 resets \"half page size\" to half window height."
779 (view-scroll-lines lines nil
(view-set-half-page-size-default lines
) t
))
781 (defun View-scroll-half-page-backward (&optional lines
)
782 "Scroll backward a \"half page\" (or prefix LINES) lines in View mode.
783 See also `View-scroll-half-page-forward'."
785 (view-scroll-lines lines t
(view-set-half-page-size-default lines
) t
))
787 (defun View-revert-buffer-scroll-page-forward (&optional lines
)
788 "Scroll forward, reverting buffer if needed, in View mode.
789 If buffer has not been changed and the corresponding file is newer, first
790 revert the buffer, then scroll.
791 This command is useful if you are viewing a changing file.
793 The prefix argument LINES says how many lines to scroll.
794 If you don't specify a prefix argument, it uses the number of lines set by
795 \\[View-scroll-page-forward-set-page-size] or
796 \\[View-scroll-page-backward-set-page-size].
797 If LINES is more than a window-full, only the last window-full is shown."
799 (let ((view-mode-auto-exit nil
)
800 (view-try-extend-at-buffer-end t
))
801 (view-scroll-lines lines nil view-page-size nil
)))
803 (defun View-back-to-mark (&optional ignore
)
804 "Return to last mark set in View mode, else beginning of file.
805 Display that line at the center of the window.
806 This command pops the mark ring, so that successive
807 invocations return to earlier marks."
809 (goto-char (or (mark t
) (point-min)))
813 (defun View-search-regexp-forward (n regexp
)
814 "Search forward for first (or prefix Nth) occurrence of REGEXP in View mode.
816 Displays line found at center of window. Sets mark at starting position and
819 Characters @ and ! are special at the beginning of REGEXP. They modify
820 the search rather than become part of the pattern searched for.
821 @ means search all the buffer i.e. start search at the beginning of buffer.
822 ! means search for a line that contains no match for the pattern.
823 If REGEXP is empty or only consist of these control characters, then
824 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
825 for use by later search commands.
827 The variable `view-highlight-face' controls the face that is used
828 for highlighting the match that is found."
829 (interactive "p\nsSearch forward (regexp): ")
830 (view-search n regexp
))
832 (defun View-search-regexp-backward (n regexp
)
833 "Search backward for first (or prefix Nth) occurrence of REGEXP in View mode.
835 Displays line found at center of window. Sets mark at starting position and
838 Characters @ and ! are special at the beginning of REGEXP. They modify
839 the search rather than become part of the pattern searched for.
840 @ means search all the buffer i.e. start search at the end of buffer.
841 ! means search for a line that contains no match for the pattern.
842 If REGEXP is empty or only consist of these control characters, then
843 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
844 for use by later search commands.
846 The variable `view-highlight-face' controls the face that is used
847 for highlighting the match that is found."
848 (interactive "p\nsSearch backward (regexp): ")
849 (view-search (- n
) regexp
))
851 (defun View-search-last-regexp-forward (n) "\
852 Search forward for first (or prefix Nth) instance of last regexp in View mode.
853 Displays line found at center of window. Sets mark at starting position and
856 The variable `view-highlight-face' controls the face that is used
857 for highlighting the match that is found."
861 (defun View-search-last-regexp-backward (n) "\
862 Search backward for first (or prefix Nth) instance of last regexp in View mode.
863 Displays line found at center of window. Sets mark at starting position and
866 The variable `view-highlight-face' controls the face that is used
867 for highlighting the match that is found."
869 (view-search (- n
) nil
))
871 (defun view-search (times regexp
)
872 ;; This function does the job for all the View-search- commands.
873 (let (where no end ln
)
875 ((and regexp
(> (length regexp
) 0)
876 (or (not (memq (string-to-char regexp
) '(?
! ?
@)))
878 (if (member (substring regexp
0 2) '("!@" "@!"))
879 (setq end t no t ln
2)
880 (setq no
(not (setq end
(eq ?
@ (string-to-char regexp
))))
882 (> (length (setq regexp
(substring regexp ln
))) 0))))
883 (setq view-last-regexp
(if no
(list regexp
) regexp
)))
884 ((consp view-last-regexp
)
885 (setq regexp
(car view-last-regexp
))
886 (if (not (setq no
(not no
))) (setq view-last-regexp regexp
)))
887 (view-last-regexp (setq regexp view-last-regexp
)
888 (if no
(setq view-last-regexp
(list regexp
))))
889 (t (error "No previous View-mode search")))
891 (if end
(goto-char (if (< times
0) (point-max) (point-min)))
892 (move-to-window-line (if (< times
0) 0 -
1)))
893 (if (if no
(view-search-no-match-lines times regexp
)
894 (re-search-forward regexp nil t times
))
895 (setq where
(point))))
901 (move-overlay view-overlay
(match-beginning 0) (match-end 0))
903 (make-overlay (match-beginning 0) (match-end 0))))
904 (overlay-put view-overlay
'face view-highlight-face
)
907 (message "Can't find occurrence %d of %s%s"
908 times
(if no
"no " "") regexp
)
911 (defun view-search-no-match-lines (times regexp
)
912 ;; Search for the TIMESt occurrence of line with no match for REGEXP.
913 (let ((back (and (< times
0) (setq times
(- times
)) -
1))
916 (save-excursion (beginning-of-line (if back
(- times
) (1+ times
)))
920 ((< (count-lines (point) n
) times
) -
1) ; Not enough lines.
921 ((or (null (re-search-forward regexp nil t back
))
922 (if back
(and (< (match-end 0) n
)
923 (> (count-lines (match-end 0) n
) 1))
924 (and (< n
(match-beginning 0))
925 (> (count-lines n
(match-beginning 0)) 1))))
926 0) ; No match within lines.
927 (back (count-lines (max n
(match-beginning 0)) (match-end 0)))
928 (t (count-lines (match-beginning 0) (min n
(match-end 0))))))
930 (and (zerop times
) (looking-at "^.*$"))))
935 ;;; view.el ends here