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 global 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.")
66 (defvar view-remove-frame-by-deleting nil
67 "*Determine how to remove a not needed frame.
68 If nil, make an icon of the frame. If non-nil, delete the frame.")
71 (defvar view-exit-all-windows-at-exit nil
72 "*Non-nil means restore all windows displaying buffer.
73 Commands that restore windows apply to all windows displaying buffer.
74 Buffer is removed from all windows displaying it, by using information in
75 `view-return-to-alist' if that is available, otherwise by using
76 `replace-buffer-in-windows'.")
78 (defvar view-mode nil
"Non-nil if View mode is enabled.")
79 (make-variable-buffer-local 'view-mode
)
81 (defvar view-mode-hook nil
82 "Normal hook run when starting to view a buffer or file.")
84 (defvar view-old-buffer-read-only nil
)
85 (make-variable-buffer-local 'view-old-buffer-read-only
)
87 (defvar view-old-Helper-return-blurb
)
88 (make-variable-buffer-local 'view-old-Helper-return-blurb
)
90 (defvar view-page-size nil
91 "Default number of lines to scroll by View page commands.
92 If nil then the local value of this is initially set to window size.")
93 (make-variable-buffer-local 'view-page-size
)
95 (defvar view-half-page-size nil
96 "Default number of lines to scroll by View half page commands.
97 If nil then the local value of this is initially set to half window size.")
98 (make-variable-buffer-local 'view-half-page-size
)
100 (defvar view-last-regexp nil
)
101 (make-variable-buffer-local 'view-last-regexp
) ; Global is better???
103 (defvar view-return-to-alist nil
104 "What to do with selected window and where to go when leaving View mode.
105 Added to by view-mode-enter when entering View mode.
106 See RETURN-TO-ALIST argument of function `view-mode-exit' for format of
107 `view-return-to-alist'.")
108 (make-variable-buffer-local 'view-return-to-alist
)
110 (defvar view-exit-action nil
112 nil or a function with one argument (a buffer) called at exit of view mode.
113 The \\[view-file] and \\[view-file-other-window] commands may set this to
115 (make-variable-buffer-local 'view-exit-action
)
117 (defvar view-overlay nil
118 "Overlay used to display where a search operation found its match.
119 This is local in each buffer, once it is used.")
120 (make-variable-buffer-local 'view-overlay
)
122 (or (assq 'view-mode minor-mode-alist
)
123 (setq minor-mode-alist
124 (cons '(view-mode " View") minor-mode-alist
)))
126 ;; Define keymap inside defvar to make it easier to load changes.
127 (defvar view-mode-map
128 (let ((map (make-sparse-keymap)))
129 (define-key map
"C" 'View-kill-and-leave
)
130 (define-key map
"c" 'View-leave
)
131 (define-key map
"Q" 'View-quit-all
)
132 (define-key map
"E" 'View-exit-and-edit
)
133 ; (define-key map "v" 'View-exit)
134 (define-key map
"e" 'View-exit
)
135 (define-key map
"q" 'View-quit
)
136 ; (define-key map "N" 'View-search-last-regexp-backward)
137 (define-key map
"p" 'View-search-last-regexp-backward
)
138 (define-key map
"n" 'View-search-last-regexp-forward
)
139 ; (define-key map "?" 'View-search-regexp-backward) ; Less does this.
140 (define-key map
"\\" 'View-search-regexp-backward
)
141 (define-key map
"/" 'View-search-regexp-forward
)
142 (define-key map
"r" 'isearch-backward
)
143 (define-key map
"s" 'isearch-forward
)
144 (define-key map
"m" 'point-to-register
)
145 (define-key map
"'" 'register-to-point
)
146 (define-key map
"x" 'exchange-point-and-mark
)
147 (define-key map
"@" 'View-back-to-mark
)
148 (define-key map
"." 'set-mark-command
)
149 (define-key map
"%" 'View-goto-percent
)
150 ; (define-key map "G" 'View-goto-line-last)
151 (define-key map
"g" 'View-goto-line
)
152 (define-key map
"=" 'what-line
)
153 (define-key map
"F" 'View-revert-buffer-scroll-page-forward
)
154 ; (define-key map "k" 'View-scroll-line-backward)
155 (define-key map
"y" 'View-scroll-line-backward
)
156 ; (define-key map "j" 'View-scroll-line-forward)
157 (define-key map
"\n" 'View-scroll-line-forward
)
158 (define-key map
"\r" 'View-scroll-line-forward
)
159 (define-key map
"u" 'View-scroll-half-page-backward
)
160 (define-key map
"d" 'View-scroll-half-page-forward
)
161 (define-key map
"z" 'View-scroll-page-forward-set-page-size
)
162 (define-key map
"w" 'View-scroll-page-backward-set-page-size
)
163 ; (define-key map "b" 'View-scroll-page-backward)
164 (define-key map
"\C-?" 'View-scroll-page-backward
)
165 ; (define-key map "f" 'View-scroll-page-forward)
166 (define-key map
" " 'View-scroll-page-forward
)
167 (define-key map
"o" 'View-scroll-to-buffer-end
)
168 (define-key map
">" 'end-of-buffer
)
169 (define-key map
"<" 'beginning-of-buffer
)
170 (define-key map
"-" 'negative-argument
)
171 (define-key map
"9" 'digit-argument
)
172 (define-key map
"8" 'digit-argument
)
173 (define-key map
"7" 'digit-argument
)
174 (define-key map
"6" 'digit-argument
)
175 (define-key map
"5" 'digit-argument
)
176 (define-key map
"4" 'digit-argument
)
177 (define-key map
"3" 'digit-argument
)
178 (define-key map
"2" 'digit-argument
)
179 (define-key map
"1" 'digit-argument
)
180 (define-key map
"0" 'digit-argument
)
181 (define-key map
"H" 'describe-mode
)
182 (define-key map
"?" 'describe-mode
) ; Maybe do as less instead?
183 (define-key map
"h" 'describe-mode
)
186 (or (assq 'view-mode minor-mode-map-alist
)
187 (setq minor-mode-map-alist
188 (cons (cons 'view-mode view-mode-map
) minor-mode-map-alist
)))
190 ;;; Commands that enter or exit view mode.
193 (defun view-file (file-name)
194 "View FILE in View mode, returning to previous buffer when done.
195 Emacs commands editing the buffer contents are not available; instead,
196 a special set of commands (mostly letters and punctuation)
197 are defined for moving around in the buffer.
198 Space scrolls forward, Delete scrolls backward.
199 For list of all View commands, type H or h while viewing.
201 This command runs the normal hook `view-mode-hook'."
202 (interactive "fView file: ")
203 (let ((had-a-buf (get-file-buffer file-name
)))
204 (view-buffer (find-file-noselect file-name
)
205 (and (not had-a-buf
) 'kill-buffer
))))
208 (defun view-file-other-window (file-name)
209 "View FILE in View mode in another window.
210 Return that window to its previous buffer when done.
211 Emacs commands editing the buffer contents are not available; instead,
212 a special set of commands (mostly letters and punctuation)
213 are defined for moving around in the buffer.
214 Space scrolls forward, Delete scrolls backward.
215 For list of all View commands, type H or h while viewing.
217 This command runs the normal hook `view-mode-hook'."
218 (interactive "fIn other window view file: ")
219 (let ((had-a-buf (get-file-buffer file-name
)))
220 (view-buffer-other-window (find-file-noselect file-name
) nil
221 (and (not had-a-buf
) 'kill-buffer
))))
224 (defun view-file-other-frame (file-name)
225 "View FILE in View mode in another frame.
226 Maybe delete other frame and/or return to previous buffer when done.
227 Emacs commands editing the buffer contents are not available; instead,
228 a special set of commands (mostly letters and punctuation)
229 are defined for moving around in the buffer.
230 Space scrolls forward, Delete scrolls backward.
231 For list of all View commands, type H or h while viewing.
233 This command runs the normal hook `view-mode-hook'."
234 (interactive "fIn other frame view file: ")
235 (let ((had-a-buf (get-file-buffer file-name
)))
236 (view-buffer-other-frame (find-file-noselect file-name
) nil
237 (and (not had-a-buf
) 'kill-buffer
))))
241 (defun view-buffer (buffer-name &optional exit-action
)
242 "View BUFFER in View mode, returning to previous buffer when done.
243 Emacs commands editing the buffer contents are not available; instead,
244 a special set of commands (mostly letters and punctuation)
245 are defined for moving around in the buffer.
246 Space scrolls forward, Delete scrolls backward.
247 For list of all View commands, type H or h while viewing.
249 This command runs the normal hook `view-mode-hook'.
251 Optional argument EXIT-ACTION is either nil or a function with buffer as
252 argument. This function is called when finished viewing buffer.
253 Use this argument instead of explicitly setting `view-exit-action'."
255 (interactive "bView buffer: ")
256 (let ((undo-window (list (window-buffer) (window-start) (window-point))))
257 (switch-to-buffer buffer-name
)
258 (view-mode-enter (cons (selected-window) (cons nil undo-window
))
262 (defun view-buffer-other-window
263 (buffer-name &optional not-return exit-action
)
264 "View BUFFER in View mode in another window.
265 Return to previous buffer when done, unless optional NOT-RETURN is non-nil.
266 Emacs commands editing the buffer contents are not available; instead,
267 a special set of commands (mostly letters and punctuation)
268 are defined for moving around in the buffer.
269 Space scrolls forward, Delete scrolls backward.
270 For list of all View commands, type H or h while viewing.
272 This command runs the normal hook `view-mode-hook'.
274 Optional argument EXIT-ACTION is either nil or a function with buffer as
275 argument. This function is called when finished viewing buffer.
276 Use this argument instead of explicitly setting `view-exit-action'."
277 (interactive "bIn other window view buffer:\nP")
278 (let* ((win ; This window will be selected by
279 (get-lru-window)) ; switch-to-buffer-other-window below.
281 (and (not not-return
)
282 (cons (selected-window)
283 (if (eq win
(selected-window))
284 t
; Has to make new window.
286 (window-buffer win
) ; Other windows old buffer.
288 (window-point win
)))))))
289 (switch-to-buffer-other-window buffer-name
)
290 (view-mode-enter (and return-to
(cons (selected-window) return-to
))
294 (defun view-buffer-other-frame
295 (buffer-name &optional not-return exit-action
)
296 "View BUFFER in View mode in another frame.
297 Return to previous buffer when done, unless optional NOT-RETURN is non-nil.
298 Emacs commands editing the buffer contents are not available; instead,
299 a special set of commands (mostly letters and punctuation)
300 are defined for moving around in the buffer.
301 Space scrolls forward, Delete scrolls backward.
302 For list of all View commands, type H or h while viewing.
304 This command runs the normal hook `view-mode-hook'.
306 Optional argument EXIT-ACTION is either nil or a function with buffer as
307 argument. This function is called when finished viewing buffer.
308 Use this argument instead of explicitly setting `view-exit-action'."
309 (interactive "bView buffer in other frame: \nP")
311 (and (not not-return
) (cons (selected-window) t
)))) ; Old window.
312 (switch-to-buffer-other-frame buffer-name
)
313 (view-mode-enter (and return-to
(cons (selected-window) return-to
))
317 (defun view-mode (&optional arg
)
318 ;; In the following documentation string we have to use some explicit key
319 ;; bindings instead of using the \\[] construction. The reason for this
320 ;; is that most commands have more than one key binding.
321 "Toggle View mode, a minor mode for viewing text but not editing it.
322 With arg, turn View mode on iff arg is positive.
324 Emacs commands that do not change the buffer contents are available as usual.
325 Kill commands insert text in kill buffers but do not delete. Other commands
326 \(among them most letters and punctuation) beep and tell that the buffer is
329 The following additional commands are provided. Most commands take prefix
330 arguments. Page commands default to \"page size\" lines which is almost a whole
331 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
332 and set \"half page size\" lines which initially is half a window full. Search
333 commands default to a repeat count of one.
335 H, h, ? This message.
336 Digits provide prefix arguments.
337 \\[negative-argument] negative prefix argument.
338 \\[beginning-of-buffer] move to the beginning of buffer.
339 > move to the end of buffer.
340 \\[View-scroll-to-buffer-end] scroll so that buffer end is at last line of window.
341 SPC scroll forward prefix (default \"page size\") lines.
342 DEL scroll backward prefix (default \"page size\") lines.
343 \\[View-scroll-page-forward-set-page-size] like \\[View-scroll-page-forward] except prefix sets \"page size\".
344 \\[View-scroll-page-backward-set-page-size] like \\[View-scroll-page-backward] except prefix sets \"page size\".
345 \\[View-scroll-half-page-forward] scroll forward (and if prefix set) \"half page size\" lines.
346 \\[View-scroll-half-page-backward] scroll backward (and if prefix set) \"half page size\" lines.
347 RET, LFD scroll forward prefix (default one) line(s).
348 y scroll backward prefix (default one) line(s).
349 \\[View-revert-buffer-scroll-page-forward] revert-buffer if necessary and scroll forward.
350 Use this to view a changing file.
351 \\[what-line] prints the current line number.
352 \\[View-goto-percent] goes prefix argument (default 100) percent into buffer.
353 \\[View-goto-line] goes to line given by prefix argument (default first line).
355 x exchanges point and mark.
356 \\[View-back-to-mark] return to mark and pops mark ring.
357 Mark ring is pushed at start of every successful search and when
358 jump to line occurs. The mark is set on jump to buffer start or end.
359 \\[point-to-register] save current position in character register.
360 ' go to position saved in character register.
361 s do forward incremental search.
362 r do reverse incremental search.
363 \\[View-search-regexp-forward] searches forward for regular expression, starting after current page.
364 ! and @ have a special meaning at the beginning of the regexp.
365 ! means search for a line with no match for regexp. @ means start
366 search at beginning (end for backward search) of buffer.
367 \\ searches backward for regular expression, starting before current page.
368 \\[View-search-last-regexp-forward] searches forward for last regular expression.
369 p searches backward for last regular expression.
370 \\[View-quit] quit View mode, trying to restore window and buffer to previous state.
371 \\[View-quit] is the normal way to leave view mode.
372 \\[View-exit] exit View mode but stay in current buffer. Use this if you started
373 viewing a buffer (file) and find out you want to edit it.
374 \\[View-exit-and-edit] exit View mode and make the current buffer editable.
375 \\[View-quit-all] quit View mode, trying to restore windows and buffer to previous state.
376 \\[View-leave] quit View mode and maybe switch buffers, but don't kill this buffer.
377 \\[View-kill-and-leave] quit View mode, kill current buffer and go back to other buffer.
379 The effect of \\[View-leave] , \\[View-quit] and \\[View-kill-and-leave] depends on how view-mode was entered. If it was
380 entered by view-file, view-file-other-window or view-file-other-frame (\\[view-file],
381 \\[view-file-other-window], \\[view-file-other-frame] or the dired mode v command), then \\[View-quit] will try to kill the
382 current buffer. If view-mode was entered from another buffer as is done by
383 View-buffer, View-buffer-other-window, View-buffer-other frame, View-file,
384 View-file-other-window or View-file-other-frame then \\[view-leave] , \\[view-quit] and \\[view-kill-and-leave] will return
387 Entry to view-mode runs the normal hook `view-mode-hook'."
391 (if (> (prefix-numeric-value arg
) 0) view-mode
(not view-mode
)))
392 ()) ; Do nothing if already OK.
393 (view-mode (view-mode-disable))
394 (t (view-mode-enable))))
396 (defun view-mode-enable ()
398 ;; Always leave view mode before changing major mode.
399 ;; This is to guarantee that the buffer-read-only variable is restored.
400 (make-local-hook 'change-major-mode-hook
)
401 (add-hook 'change-major-mode-hook
'view-mode-disable nil t
)
403 view-page-size
(view-page-size-default view-page-size
)
404 view-half-page-size
(or view-half-page-size
(/ (view-window-size) 2))
405 view-old-buffer-read-only buffer-read-only
407 view-old-Helper-return-blurb
(and (boundp 'Helper-return-blurb
)
410 (format "continue viewing %s"
411 (if (buffer-file-name)
412 (file-name-nondirectory (buffer-file-name))
414 (run-hooks 'view-mode-hook
))
416 (defun view-mode-disable ()
417 "Turn off View mode."
418 (remove-hook 'change-major-mode-hook
'view-mode-disable t
)
419 (and view-overlay
(delete-overlay view-overlay
))
421 Helper-return-blurb view-old-Helper-return-blurb
422 buffer-read-only view-old-buffer-read-only
))
425 (defun view-mode-enter (&optional return-to exit-action
) "\
426 Enter View mode and set up exit from view mode depending on optional arguments.
427 If RETURN-TO is non-nil it is added as an element to the buffer local alist
428 view-return-to-alist.
429 Save EXIT-ACTION in buffer local variable `view-exit-action'.
430 It should be either nil or a function that takes a buffer as argument.
431 This function will called by `view-mode-exit'.
433 RETURN-TO is either nil, meaning do nothing when exiting view mode, or
434 \(WINDOW OLD-WINDOW . OLD-BUF-INFO).
435 WINDOW is a window used for viewing.
436 OLD-WINDOW is nil or the window to select after viewing.
437 OLD-BUF-INFO tells what to do with WINDOW when exiting. It is one of:
439 2) t Delete WINDOW or, if it is the only window, its frame.
440 3) (OLD-BUFF START POINT) Display buffer OLD-BUFF with displayed text
441 starting at START and point at POINT in WINDOW.
443 See the function `view-mode' for the commands of View mode.
445 This function runs the normal hook `view-mode-hook'."
447 (let ((entry (assq (car return-to
) view-return-to-alist
)))
448 (if entry
(setcdr entry
(cdr return-to
))
449 (setq view-return-to-alist
(cons return-to view-return-to-alist
)))))
450 (if view-mode
; Do nothing if already in view mode.
452 (setq view-exit-action exit-action
)
454 (force-mode-line-update)
456 (substitute-command-keys "\
457 Type \\[help-command] for help, \\[describe-mode] for commands, \\[View-quit] to quit."))))
459 (defun view-mode-exit (&optional return-to-alist exit-action all-win
)
460 "Exit view-mode in various ways, depending on optional arguments.
461 RETURN-TO-ALIST, EXIT-ACTION and ALL-WIN determine what to do after
463 EXIT-ACTION is nil or a function that is called with current buffer as
465 RETURN-TO-ALIST is an alist that for some of the windows displaying the current
466 buffer, associate information on what to do with those windows. If ALL-WIN is
467 non-nil, then all windows on RETURN-TO-ALIST are restored to their old state.
468 If ALL-WIN is nil, then only the selected window is affected (if it is on
469 ALL-WIN). Each element has the format (WINDOW OLD-WINDOW . OLD-BUF-INFO)
470 where WINDOW is a window displaying the current buffer and OLD-BUF-INFO is
471 information on what to do with WINDOW. OLD-BUF-INFO is one of:
473 2) t Delete WINDOW or, if it is the only window, its frame.
474 3) (OLD-BUF START POINT) Display buffer OLD-BUF with displayed text
475 starting at START and point at POINT in WINDOW.
476 If one of the WINDOW in RETURN-TO-ALIST is the selected window and the
477 corresponding OLD-WINDOW is a live window, then select OLD-WINDOW."
478 (if view-mode
; Only do something if in view mode.
479 (let* ((buffer (current-buffer))
481 (sel-old (assq (selected-window) return-to-alist
))
482 (old-window (or (and sel-old
(car (cdr sel-old
)))
483 (and all-win
(selected-window))))
484 (alist (if (setq all-win
485 (or all-win view-exit-all-windows-at-exit
))
486 return-to-alist
; Try to restore all windows.
487 (and sel-old
(list sel-old
))))) ; Only selected window.
489 (setq view-exit-action nil
490 view-return-to-alist nil
)
491 (while alist
; Restore windows with info.
492 (if (and (window-live-p (setq window
(car (car alist
))))
493 (eq buffer
(window-buffer window
)))
494 (let ((frame (window-frame window
))
495 (old-buf-info (cdr (cdr (car alist
)))))
496 (if all-win
(select-window window
))
498 ((and (consp old-buf-info
) ; Case 3.
499 (buffer-live-p (car old-buf-info
)))
500 (set-window-buffer window
(car old-buf-info
)) ; old-buf
501 (set-window-start window
(car (cdr old-buf-info
)))
502 (set-window-point window
(car (cdr (cdr old-buf-info
)))))
503 ((not (eq old-buf-info t
)) nil
) ; Not case 2, do nothing.
504 ((not (one-window-p t
)) (delete-window))
505 ((not (eq frame
(next-frame))) ; Not the only frame, so
506 (if view-remove-frame-by-deleting
(delete-frame frame
)
507 (iconify-frame frame
)))))) ; can safely be removed.
508 (setq alist
(cdr alist
)))
509 (if (and return-to-alist view-exit-all-windows-at-exit
)
510 (replace-buffer-in-windows buffer
))
511 (if (window-live-p old-window
) ; still existing window
512 (select-window old-window
))
513 (if (and exit-action
(not (get-buffer-window buffer
)))
514 (funcall exit-action buffer
))
515 (force-mode-line-update))))
518 "Exit View mode but stay in current buffer."
522 (defun View-exit-and-edit ()
523 "Exit View mode and make the current buffer editable."
526 (setq buffer-read-only nil
))
529 "Quit View mode and maybe switch buffers, but don't kill this buffer."
531 (view-mode-exit view-return-to-alist
))
534 "Quit View mode, trying to restore window and buffer to previous state.
535 Maybe kill this buffer. Try to restore selected window to previous state
536 and go to previous buffer or window."
538 (view-mode-exit view-return-to-alist view-exit-action
))
540 (defun View-quit-all ()
541 "Quit View mode, trying to restore windows and buffers to previous state.
542 Maybe kill current buffer. Try to restore all windows viewing buffer to
543 previous state and go to previous buffer or window."
545 (view-mode-exit view-return-to-alist view-exit-action t
))
547 (defun View-kill-and-leave ()
548 "Quit View mode, kill current buffer and return to previous buffer."
550 (view-mode-exit view-return-to-alist
(or view-exit-action
'kill-buffer
) t
))
553 ;;; Some help routines.
555 (defun view-window-size ()
556 ;; Window height excluding mode line.
557 (1- (window-height)))
559 ;(defun view-last-command (&optional who what)
560 ; (setq view-last-command-entry this-command)
561 ; (setq view-last-command who)
562 ; (setq view-last-command-argument what))
564 ;(defun View-repeat-last-command ()
565 ; "Repeat last command issued in View mode."
567 ; (if (and view-last-command
568 ; (eq view-last-command-entry last-command))
569 ; (funcall view-last-command view-last-command-argument))
570 ; (setq this-command view-last-command-entry))
572 (defun view-recenter ()
573 ;; Center point in window.
574 (recenter (/ (view-window-size) 2)))
576 (defun view-page-size-default (lines)
578 (let ((default (- (view-window-size) next-screen-context-lines
)))
579 (if (or (null lines
) (zerop (setq lines
(prefix-numeric-value lines
))))
581 (min (abs lines
) default
))))
583 (defun view-set-half-page-size-default (lines)
584 ;; Get and maybe set half page size.
585 (if (not lines
) view-half-page-size
586 (setq view-half-page-size
587 (if (zerop (setq lines
(prefix-numeric-value lines
)))
588 (/ (view-window-size) 2)
589 (view-page-size-default lines
)))))
592 ;;; Commands for moving around in the buffer.
594 (defun View-goto-percent (&optional percent
)
595 "Move to end (or prefix PERCENT) of buffer in View mode.
596 Display is centered at point.
597 Also set the mark at the position where point was."
603 (floor (* (- (point-max) (point-min)) 0.01
604 (max 0 (min 100 (prefix-numeric-value percent
))))))
608 ;(defun View-goto-line-last (&optional line)
609 ;"Move to last (or prefix LINE) line in View mode.
610 ;Display is centered at LINE.
611 ;Sets mark at starting position and pushes mark ring."
614 ; (if line (goto-line (prefix-numeric-value line))
615 ; (goto-char (point-max))
616 ; (beginning-of-line))
619 (defun View-goto-line (&optional line
)
620 "Move to first (or prefix LINE) line in View mode.
621 Display is centered at LINE.
622 Also set the mark at the position where point was."
628 (defun View-scroll-to-buffer-end ()
629 "Scroll backward or forward so that buffer end is at last line of window."
631 (let ((p (if (pos-visible-in-window-p (point-max)) (point))))
632 (goto-char (point-max))
634 (and p
(goto-char p
))))
636 (defun view-scroll-lines (lines backward default maxdefault
)
637 ;; This function does the job for all the scrolling commands.
638 ;; Scroll forward LINES lines. If BACKWARD is true scroll backwards.
639 ;; If LINES is negative scroll in the other direction. If LINES is 0 or nil,
640 ;; scroll DEFAULT lines. If MAXDEFAULT is true then scroll no more than a
642 (if (or (null lines
) (zerop (setq lines
(prefix-numeric-value lines
))))
643 (setq lines default
))
645 (progn (setq backward
(not backward
)) (setq lines
(- lines
))))
646 (setq default
(view-page-size-default nil
)) ; Max scrolled at a time.
647 (if maxdefault
(setq lines
(min lines default
)))
649 (backward (scroll-down lines
))
650 ((view-really-at-end)
651 (if view-scroll-auto-exit
(View-quit)
654 (t (while (> lines default
)
656 (setq lines
(- lines default
))
657 (if (view-really-at-end) (setq lines
0)))
659 (if (view-really-at-end) (view-end-message))
660 (move-to-window-line -
1)
661 (beginning-of-line))))
663 (defun view-really-at-end ()
664 ;; Return true if buffer end visible. Maybe revert buffer and test.
665 (and (pos-visible-in-window-p (point-max))
666 (let ((buf (current-buffer))
667 (bufname (buffer-name))
668 (file (buffer-file-name)))
669 (or (not view-try-extend-at-buffer-end
)
671 (verify-visited-file-modtime buf
)
672 (not (file-exists-p file
))
673 (and (buffer-modified-p buf
)
674 (setq file
(file-name-nondirectory file
))
677 "File %s changed on disk. Discard your edits%s? "
679 (if (string= bufname file
) ""
680 (concat " in " bufname
))))))
681 (progn (revert-buffer t t t
)
682 (pos-visible-in-window-p (point-max)))))))
684 (defun view-end-message ()
685 ;; Tell that we are at end of buffer.
686 (goto-char (point-max))
687 (message "End of buffer. Type %s to quit viewing."
688 (substitute-command-keys
689 (if view-scroll-auto-exit
"\\[View-scroll-page-forward]"
692 (defun View-scroll-page-forward (&optional lines
)
693 "Scroll \"page size\" or prefix LINES lines forward in View mode.
694 Exit if end of text is visible and `view-scroll-auto-exit' is non-nil.
695 \"page size\" is whole window full, or number of lines set by
696 \\[View-scroll-page-forward-set-page-size] or
697 \\[View-scroll-page-backward-set-page-size].
698 If LINES is more than a window-full, only the last window-full is shown."
700 (view-scroll-lines lines nil view-page-size nil
))
702 (defun View-scroll-page-backward (&optional lines
)
703 "Scroll \"page size\" or prefix LINES lines backward in View mode.
704 See also `View-scroll-page-forward'."
706 (view-scroll-lines lines t view-page-size nil
))
708 (defun View-scroll-page-forward-set-page-size (&optional lines
)
709 "Scroll forward LINES lines in View mode, setting the \"page size\".
710 This is the number of lines which \\[View-scroll-page-forward] and
711 \\[View-scroll-page-backward] scroll by default. If LINES is omitted or = 0,
712 sets \"page size\" to window height and scrolls forward that much, otherwise
713 scrolls forward LINES lines and sets \"page size\" to the minimum of window
714 height and the absolute value of LINES.
715 See also `View-scroll-page-forward'."
717 (view-scroll-lines lines nil
718 (setq view-page-size
(view-page-size-default lines
))
721 (defun View-scroll-page-backward-set-page-size (&optional lines
)
722 "Scroll backward prefix LINES lines in View mode, setting the \"page size\".
723 See also `View-scroll-page-forward-set-page-size'."
725 (view-scroll-lines lines t
726 (setq view-page-size
(view-page-size-default lines
))
729 (defun View-scroll-line-forward (&optional lines
)
730 "Scroll forward one line (or prefix LINES lines) in View mode.
731 See also `View-scroll-page-forward,' but note that scrolling is limited
732 to minimum of LINES and one window-full."
734 (view-scroll-lines lines nil
1 t
))
736 (defun View-scroll-line-backward (&optional lines
)
737 "Scroll backward one line (or prefix LINES lines) in View mode.
738 See also `View-scroll-line-forward'."
740 (view-scroll-lines lines t
1 t
))
742 (defun View-scroll-half-page-forward (&optional lines
)
743 "Scroll forward a \"half page\" (or prefix LINES) lines in View mode.
744 If LINES is not omitted, the \"half page size\" is set to the minimum of
745 window height and the absolute value of LINES.
746 LINES=0 resets \"half page size\" to half window height."
748 (view-scroll-lines lines nil
(view-set-half-page-size-default lines
) t
))
750 (defun View-scroll-half-page-backward (&optional lines
)
751 "Scroll backward a \"half page\" (or prefix LINES) lines in View mode.
752 See also `View-scroll-half-page-forward'."
754 (view-scroll-lines lines t
(view-set-half-page-size-default lines
) t
))
756 (defun View-revert-buffer-scroll-page-forward (&optional lines
)
757 "Scroll forward, reverting buffer if needed, in View mode.
758 If buffer has not been changed and the corresponding file is newer, first
759 revert the buffer, then scroll.
760 This command is useful if you are viewing a changing file.
762 The prefix argument LINES says how many lines to scroll.
763 If you don't specify a prefix argument, it uses the number of lines set by
764 \\[View-scroll-page-forward-set-page-size] or
765 \\[View-scroll-page-backward-set-page-size].
766 If LINES is more than a window-full, only the last window-full is shown."
768 (let ((view-mode-auto-exit nil
)
769 (view-try-extend-at-buffer-end t
))
770 (view-scroll-lines lines nil view-page-size nil
)))
772 (defun View-back-to-mark (&optional ignore
)
773 "Return to last mark set in View mode, else beginning of file.
774 Display that line at the center of the window.
775 This command pops the mark ring, so that successive
776 invocations return to earlier marks."
778 (goto-char (or (mark t
) (point-min)))
782 (defun View-search-regexp-forward (n regexp
)
783 "Search forward for first (or prefix Nth) occurrence of REGEXP in View mode.
784 Displays line found at center of window. REGEXP is remembered for searching
785 with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward]. Sets mark at starting position and pushes mark ring.
786 Characters @ or ! or combined as @! or !@ are special if entered at the
787 beginning of REGEXP. They modify the search rather than become part of pattern
788 searched for. @ means start search at the beginning of buffer. ! means search
789 for line that not contains match for pattern. If REGEXP only consist of these
790 control characters, then an earlier remembered REGEXP is used.
792 The variable `view-highlight-face' controls the face that is used
793 for highlighting the match that is found."
794 (interactive "p\nsSearch forward (regexp): ")
795 (view-search n regexp
))
797 (defun View-search-regexp-backward (n regexp
)
798 "Search backward for first (or prefix Nth) occurrence of REGEXP in View mode.
799 Displays line found at center of window. REGEXP is remembered for searching
800 with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward]. Sets mark at starting position and pushes mark ring.
801 Characters @ or ! or combined as @! or !@ are special if entered at the
802 beginning of REGEXP. They modify the search rather than become part of pattern
803 searched for. @ means start search at the end of buffer. ! means search
804 for line that not contains match for pattern. If REGEXP only consist of these
805 control characters, then an earlier remembered REGEXP is used.
807 The variable `view-highlight-face' controls the face that is used
808 for highlighting the match that is found."
809 (interactive "p\nsSearch backward (regexp): ")
810 (view-search (- n
) regexp
))
812 (defun View-search-last-regexp-forward (n) "\
813 Search forward for first (or prefix Nth) instance of last regexp in View mode.
814 Displays line found at center of window. Sets mark at starting position and
817 The variable `view-highlight-face' controls the face that is used
818 for highlighting the match that is found."
822 (defun View-search-last-regexp-backward (n) "\
823 Search backward for first (or prefix Nth) instance of last regexp in View mode.
824 Displays line found at center of window. Sets mark at starting position and
827 The variable `view-highlight-face' controls the face that is used
828 for highlighting the match that is found."
830 (view-search (- n
) nil
))
832 (defun view-search (times regexp
)
833 ;; This function does the job for all the view-search commands.
834 (let (where no end ln
)
836 ((and regexp
(> (length regexp
) 0)
837 (or (not (memq (string-to-char regexp
) '(?
! ?
@)))
839 (if (member (substring regexp
0 2) '("!@" "@!"))
840 (setq end t no t ln
2)
841 (setq no
(not (setq end
(eq ?
@ (string-to-char regexp
))))
843 (> (length (setq regexp
(substring regexp ln
))) 0))))
844 (setq view-last-regexp
(if no
(list regexp
) regexp
)))
845 ((consp view-last-regexp
)
846 (setq regexp
(car view-last-regexp
))
847 (if (not (setq no
(not no
))) (setq view-last-regexp regexp
)))
848 (view-last-regexp (setq regexp view-last-regexp
)
849 (if no
(setq view-last-regexp
(list regexp
))))
850 (t (error "No previous View-mode search")))
852 (if end
(goto-char (if (< times
0) (point-max) (point-min)))
853 (move-to-window-line (if (< times
0) 0 -
1)))
854 (if (if no
(view-search-no-match-lines times regexp
)
855 (re-search-forward regexp nil t times
))
856 (setq where
(point))))
862 (move-overlay view-overlay
(match-beginning 0) (match-end 0))
864 (make-overlay (match-beginning 0) (match-end 0))))
865 (overlay-put view-overlay
'face view-highlight-face
)
868 (message "Can't find occurrence %d of %s%s"
869 times
(if no
"no " "") regexp
)
872 (defun view-search-no-match-lines (times regexp
)
873 ;; Search for the TIMESt occurrence of line with no match for REGEXP.
874 (let ((back (and (< times
0) (setq times
(- times
)) -
1))
877 (save-excursion (beginning-of-line (if back
(- times
) (1+ times
)))
881 ((< (count-lines (point) n
) times
) -
1) ; Not enough lines.
882 ((or (null (re-search-forward regexp nil t back
))
883 (if back
(and (< (match-end 0) n
)
884 (> (count-lines (match-end 0) n
) 1))
885 (and (< n
(match-beginning 0))
886 (> (count-lines n
(match-beginning 0)) 1))))
887 0) ; No match within lines.
888 (back (count-lines (max n
(match-beginning 0)) (match-end 0)))
889 (t (count-lines (match-beginning 0) (min n
(match-end 0))))))
891 (and (zerop times
) (looking-at "^.*$"))))
896 ;;; view.el ends here