1 ;;; view.el --- peruse file or buffer without editing
3 ;; Copyright (C) 1985, 1989, 1994, 1995, 1997, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: K. Shane Hartman
7 ;; Maintainer: Inge Frick <inge@nada.kth.se>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This package provides the `view' minor mode documented in the Emacs
29 ;; View mode entry and exit is done through the functions view-mode-enter
30 ;; and view-mode-exit. Use these functions to enter or exit view-mode from
31 ;; emacs lisp programs.
32 ;; We use both view- and View- as prefix for symbols. View- is used as
33 ;; prefix for commands that have a key binding. view- is used for commands
34 ;; without key binding. The purpose of this is to make it easier for a
35 ;; user to use command name completion.
37 ;;; Suggested key bindings:
39 ;; (define-key ctl-x-4-map "v" 'view-file-other-window) ; ^x4v
40 ;; (define-key ctl-x-5-map "v" 'view-file-other-frame) ; ^x5v
42 ;; You could also bind view-file, view-buffer, view-buffer-other-window and
43 ;; view-buffer-other-frame to keys.
48 "Peruse file or buffer without editing."
49 :link
'(function-link view-mode
)
50 :link
'(custom-manual "(emacs)Misc File Ops")
53 (defcustom view-highlight-face
'highlight
54 "The face used for highlighting the match found by View mode search."
58 (defcustom view-scroll-auto-exit nil
59 "Non-nil means scrolling past the end of buffer exits View mode.
60 A value of nil means attempting to scroll past the end of the buffer,
61 only rings the bell and gives a message on how to leave."
65 (defcustom view-try-extend-at-buffer-end nil
66 "Non-nil means try to load more of file when reaching end of buffer.
67 This variable is mainly intended to be temporarily set to non-nil by
68 the F command in view-mode, but you can set it to t if you want the action
69 for all scroll commands in view mode."
74 (defcustom view-remove-frame-by-deleting t
75 "Determine how View mode removes a frame no longer needed.
76 If nil, make an icon of the frame. If non-nil, delete the frame."
81 (defcustom view-exits-all-viewing-windows nil
82 "Non-nil means restore all windows used to view buffer.
83 Commands that restore windows when finished viewing a buffer,
84 apply to all windows that display the buffer and have restore
85 information. If `view-exits-all-viewing-windows' is nil, only
86 the selected window is considered for restoring."
90 (defcustom view-inhibit-help-message nil
91 "Non-nil inhibits the help message shown upon entering View mode."
98 "Non-nil if View mode is enabled.
99 Don't change this variable directly, you must change it by one of the
100 functions that enable or disable view mode.")
102 (make-variable-buffer-local 'view-mode
)
104 (defcustom view-mode-hook nil
105 "Normal hook run when starting to view a buffer or file."
109 (defvar view-old-buffer-read-only nil
)
110 (make-variable-buffer-local 'view-old-buffer-read-only
)
112 (defvar view-old-Helper-return-blurb
)
113 (make-variable-buffer-local 'view-old-Helper-return-blurb
)
115 (defvar view-page-size nil
116 "Default number of lines to scroll by View page commands.
117 If nil that means use the window size.")
118 (make-variable-buffer-local 'view-page-size
)
120 (defvar view-half-page-size nil
121 "Default number of lines to scroll by View half page commands.
122 If nil that means use half the window size.")
123 (make-variable-buffer-local 'view-half-page-size
)
125 (defvar view-last-regexp nil
)
126 (make-variable-buffer-local 'view-last-regexp
) ; Global is better???
128 (defvar view-exit-action nil
129 "If non-nil, a function with one argument (a buffer) called when finished viewing.
130 Commands like \\[view-file] and \\[view-file-other-window] may
131 set this to bury or kill the viewed buffer.
132 Observe that the buffer viewed might not appear in any window at
133 the time this function is called.")
134 (make-variable-buffer-local 'view-exit-action
)
136 (defvar view-no-disable-on-exit nil
137 "If non-nil, View mode \"exit\" commands don't actually disable View mode.
138 Instead, these commands just switch buffers or windows.
139 This is set in certain buffers by specialized features such as help commands
140 that use View mode automatically.")
142 (defvar view-overlay nil
143 "Overlay used to display where a search operation found its match.
144 This is local in each buffer, once it is used.")
145 (make-variable-buffer-local 'view-overlay
)
147 ;; Define keymap inside defvar to make it easier to load changes.
148 ;; Some redundant "less"-like key bindings below have been commented out.
149 (defvar view-mode-map
150 (let ((map (make-sparse-keymap)))
151 (define-key map
"C" 'View-kill-and-leave
)
152 (define-key map
"c" 'View-leave
)
153 (define-key map
"Q" 'View-quit-all
)
154 (define-key map
"E" 'View-exit-and-edit
)
155 ;; (define-key map "v" 'View-exit)
156 (define-key map
"e" 'View-exit
)
157 (define-key map
"q" 'View-quit
)
158 ;; (define-key map "N" 'View-search-last-regexp-backward)
159 (define-key map
"p" 'View-search-last-regexp-backward
)
160 (define-key map
"n" 'View-search-last-regexp-forward
)
161 ;; (define-key map "?" 'View-search-regexp-backward) ; Less does this.
162 (define-key map
"\\" 'View-search-regexp-backward
)
163 (define-key map
"/" 'View-search-regexp-forward
)
164 (define-key map
"r" 'isearch-backward
)
165 (define-key map
"s" 'isearch-forward
)
166 (define-key map
"m" 'point-to-register
)
167 (define-key map
"'" 'register-to-point
)
168 (define-key map
"x" 'exchange-point-and-mark
)
169 (define-key map
"@" 'View-back-to-mark
)
170 (define-key map
"." 'set-mark-command
)
171 (define-key map
"%" 'View-goto-percent
)
172 ;; (define-key map "G" 'View-goto-line-last)
173 (define-key map
"g" 'View-goto-line
)
174 (define-key map
"=" 'what-line
)
175 (define-key map
"F" 'View-revert-buffer-scroll-page-forward
)
176 ;; (define-key map "k" 'View-scroll-line-backward)
177 (define-key map
"y" 'View-scroll-line-backward
)
178 ;; (define-key map "j" 'View-scroll-line-forward)
179 (define-key map
"\n" 'View-scroll-line-forward
)
180 (define-key map
"\r" 'View-scroll-line-forward
)
181 (define-key map
"u" 'View-scroll-half-page-backward
)
182 (define-key map
"d" 'View-scroll-half-page-forward
)
183 (define-key map
"z" 'View-scroll-page-forward-set-page-size
)
184 (define-key map
"w" 'View-scroll-page-backward-set-page-size
)
185 ;; (define-key map "b" 'View-scroll-page-backward)
186 (define-key map
"\C-?" 'View-scroll-page-backward
)
187 ;; (define-key map "f" 'View-scroll-page-forward)
188 (define-key map
" " 'View-scroll-page-forward
)
189 (define-key map
"o" 'View-scroll-to-buffer-end
)
190 (define-key map
">" 'end-of-buffer
)
191 (define-key map
"<" 'beginning-of-buffer
)
192 (define-key map
"-" 'negative-argument
)
193 (define-key map
"9" 'digit-argument
)
194 (define-key map
"8" 'digit-argument
)
195 (define-key map
"7" 'digit-argument
)
196 (define-key map
"6" 'digit-argument
)
197 (define-key map
"5" 'digit-argument
)
198 (define-key map
"4" 'digit-argument
)
199 (define-key map
"3" 'digit-argument
)
200 (define-key map
"2" 'digit-argument
)
201 (define-key map
"1" 'digit-argument
)
202 (define-key map
"0" 'digit-argument
)
203 (define-key map
"H" 'describe-mode
)
204 (define-key map
"?" 'describe-mode
) ; Maybe do as less instead? See above.
205 (define-key map
"h" 'describe-mode
)
208 ;;; Commands that enter or exit view mode.
210 ;; This is used when view mode is exited, to make sure we don't try to
211 ;; kill a buffer modified by the user. A buffer in view mode can
212 ;; become modified if the user types C-x C-q, edits the buffer, then
213 ;; types C-x C-q again to return to view mode.
215 (defun kill-buffer-if-not-modified (buf)
216 "Like `kill-buffer', but does nothing if the buffer is modified."
217 (let ((buf (get-buffer buf
)))
218 (and buf
(not (buffer-modified-p buf
))
222 (defun view-file (file)
223 "View FILE in View mode, returning to previous buffer when done.
224 Emacs commands editing the buffer contents are not available; instead, a
225 special set of commands (mostly letters and punctuation) are defined for
226 moving around in the buffer.
227 Space scrolls forward, Delete scrolls backward.
228 For a list of all View commands, type H or h while viewing.
230 This command runs the normal hook `view-mode-hook'."
231 (interactive "fView file: ")
232 (unless (file-exists-p file
) (error "%s does not exist" file
))
233 (let ((had-a-buf (get-file-buffer file
))
234 (buffer (find-file-noselect file
)))
235 (view-buffer buffer
(and (not had-a-buf
) 'kill-buffer-if-not-modified
))))
238 (defun view-file-other-window (file)
239 "View FILE in View mode in another window.
240 When done, return that window to its previous buffer, and kill the
241 buffer visiting FILE if unmodified and if it wasn't visited before.
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 a list of all View commands, type H or h while viewing.
249 This command runs the normal hook `view-mode-hook'."
250 (interactive "fIn other window view file: ")
251 (unless (file-exists-p file
) (error "%s does not exist" file
))
252 (let ((had-a-buf (get-file-buffer file
))
253 (buf-to-view (find-file-noselect file
)))
254 (view-buffer-other-window buf-to-view
256 'kill-buffer-if-not-modified
))))
259 (defun view-file-other-frame (file)
260 "View FILE in View mode in another frame.
261 When done, kill the buffer visiting FILE if unmodified and if it wasn't
262 visited before; also, maybe delete other frame and/or return to previous
265 Emacs commands editing the buffer contents are not available; instead,
266 a special set of commands (mostly letters and punctuation)
267 are defined for moving around in the buffer.
268 Space scrolls forward, Delete scrolls backward.
269 For a list of all View commands, type H or h while viewing.
271 This command runs the normal hook `view-mode-hook'."
272 (interactive "fIn other frame view file: ")
273 (unless (file-exists-p file
) (error "%s does not exist" file
))
274 (let ((had-a-buf (get-file-buffer file
))
275 (buf-to-view (find-file-noselect file
)))
276 (view-buffer-other-frame buf-to-view
278 'kill-buffer-if-not-modified
))))
282 (defun view-buffer (buffer &optional exit-action
)
283 "View BUFFER in View mode, returning to previous buffer when done.
284 Emacs commands editing the buffer contents are not available; instead, a
285 special set of commands (mostly letters and punctuation) are defined for
286 moving around in the buffer.
287 Space scrolls forward, Delete scrolls backward.
288 For a list of all View commands, type H or h while viewing.
290 This command runs the normal hook `view-mode-hook'.
292 Optional argument EXIT-ACTION is either nil or a function with buffer as
293 argument. This function is called when finished viewing buffer. Use
294 this argument instead of explicitly setting `view-exit-action'.
296 Do not set EXIT-ACTION to `kill-buffer' when BUFFER visits a
297 file: Users may suspend viewing in order to modify the buffer.
298 Exiting View mode will then discard the user's edits. Setting
299 EXIT-ACTION to `kill-buffer-if-not-modified' avoids this."
300 (interactive "bView buffer: ")
301 (if (eq (with-current-buffer buffer
302 (get major-mode
'mode-class
))
305 (switch-to-buffer buffer
)
306 (message "Not using View mode because the major mode is special"))
307 (switch-to-buffer buffer
)
308 (view-mode-enter nil exit-action
)))
311 (defun view-buffer-other-window (buffer &optional exit-action
)
312 "View BUFFER in View mode in another window.
313 Emacs commands editing the buffer contents are not available;
314 instead, a special set of commands (mostly letters and
315 punctuation) are defined for moving around in the buffer.
316 Space scrolls forward, Delete scrolls backward.
317 For a list of all View commands, type H or h while viewing.
319 This command runs the normal hook `view-mode-hook'.
321 Optional argument EXIT-ACTION is either nil or a function with buffer as
322 argument. This function is called when finished viewing buffer. Use
323 this argument instead of explicitly setting `view-exit-action'."
324 (interactive "bIn other window view buffer:\nP")
325 (switch-to-buffer-other-window buffer
)
326 (view-mode-enter nil exit-action
))
329 (defun view-buffer-other-frame (buffer &optional exit-action
)
330 "View BUFFER in View mode in another frame.
331 Emacs commands editing the buffer contents are not available;
332 instead, a special set of commands (mostly letters and
333 punctuation) are defined for moving around in the buffer.
334 Space scrolls forward, Delete scrolls backward.
335 For a list of all View commands, type H or h while viewing.
337 This command runs the normal hook `view-mode-hook'.
339 Optional argument EXIT-ACTION is either nil or a function with buffer as
340 argument. This function is called when finished viewing buffer. Use
341 this argument instead of explicitly setting `view-exit-action'."
342 (interactive "bView buffer in other frame: \nP")
343 (switch-to-buffer-other-frame buffer
)
344 (view-mode-enter nil exit-action
))
347 (define-minor-mode view-mode
348 ;; In the following documentation string we have to use some explicit key
349 ;; bindings instead of using the \\[] construction. The reason for this
350 ;; is that most commands have more than one key binding.
351 "Toggle View mode, a minor mode for viewing text but not editing it.
352 With prefix argument ARG, turn View mode on if ARG is positive, otherwise
355 Emacs commands that do not change the buffer contents are available as usual.
356 Kill commands insert text in kill buffers but do not delete. Other commands
357 \(among them most letters and punctuation) beep and tell that the buffer is
360 The following additional commands are provided. Most commands take prefix
361 arguments. Page commands default to \"page size\" lines which is almost a whole
362 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
363 and set \"half page size\" lines which initially is half a window full. Search
364 commands default to a repeat count of one.
366 H, h, ? This message.
367 Digits provide prefix arguments.
368 \\[negative-argument] negative prefix argument.
369 \\[beginning-of-buffer] move to the beginning of buffer.
370 > move to the end of buffer.
371 \\[View-scroll-to-buffer-end] scroll so that buffer end is at last line of window.
372 SPC scroll forward \"page size\" lines.
373 With prefix scroll forward prefix lines.
374 DEL scroll backward \"page size\" lines.
375 With prefix scroll backward prefix lines.
376 \\[View-scroll-page-forward-set-page-size] like \\[View-scroll-page-forward] but with prefix sets \"page size\" to prefix.
377 \\[View-scroll-page-backward-set-page-size] like \\[View-scroll-page-backward] but with prefix sets \"page size\" to prefix.
378 \\[View-scroll-half-page-forward] scroll forward \"half page size\" lines. With prefix, sets
379 \"half page size\" to prefix lines and scrolls forward that much.
380 \\[View-scroll-half-page-backward] scroll backward \"half page size\" lines. With prefix, sets
381 \"half page size\" to prefix lines and scrolls backward that much.
382 RET, LFD scroll forward one line. With prefix scroll forward prefix line(s).
383 y scroll backward one line. With prefix scroll backward prefix line(s).
384 \\[View-revert-buffer-scroll-page-forward] revert-buffer if necessary and scroll forward.
385 Use this to view a changing file.
386 \\[what-line] prints the current line number.
387 \\[View-goto-percent] goes prefix argument (default 100) percent into buffer.
388 \\[View-goto-line] goes to line given by prefix argument (default first line).
390 x exchanges point and mark.
391 \\[View-back-to-mark] return to mark and pops mark ring.
392 Mark ring is pushed at start of every successful search and when
393 jump to line occurs. The mark is set on jump to buffer start or end.
394 \\[point-to-register] save current position in character register.
395 ' go to position saved in character register.
396 s do forward incremental search.
397 r do reverse incremental search.
398 \\[View-search-regexp-forward] searches forward for regular expression, starting after current page.
399 ! and @ have a special meaning at the beginning of the regexp.
400 ! means search for a line with no match for regexp. @ means start
401 search at beginning (end for backward search) of buffer.
402 \\ searches backward for regular expression, starting before current page.
403 \\[View-search-last-regexp-forward] searches forward for last regular expression.
404 p searches backward for last regular expression.
405 \\[View-quit] quit View mode, restoring this window and buffer to previous state.
406 \\[View-quit] is the normal way to leave view mode.
407 \\[View-exit] exit View mode but stay in current buffer. Use this if you started
408 viewing a buffer (file) and find out you want to edit it.
409 This command restores the previous read-only status of the buffer.
410 \\[View-exit-and-edit] exit View mode, and make the current buffer editable
411 even if it was not editable before entry to View mode.
412 \\[View-quit-all] quit View mode, restoring all windows to previous state.
413 \\[View-leave] quit View mode and maybe switch buffers, but don't kill this buffer.
414 \\[View-kill-and-leave] quit View mode, kill current buffer and go back to other buffer.
416 The effect of \\[View-leave], \\[View-quit] and \\[View-kill-and-leave] depends on how view-mode was entered. If it was
417 entered by view-file, view-file-other-window, view-file-other-frame, or
418 \\[dired-view-file] \(\\[view-file], \\[view-file-other-window],
419 \\[view-file-other-frame], or the Dired mode v command),
420 then \\[View-quit] will try to kill the current buffer.
421 If view-mode was entered from another buffer, by \\[view-buffer],
422 \\[view-buffer-other-window], \\[view-buffer-other frame], \\[view-file],
423 \\[view-file-other-window], or \\[view-file-other-frame],
424 then \\[View-leave], \\[View-quit] and \\[View-kill-and-leave] will return to that buffer.
426 Entry to view-mode runs the normal hook `view-mode-hook'."
427 :lighter
" View" :keymap view-mode-map
428 (if view-mode
(view-mode-enable) (view-mode-disable)))
430 (defun view-mode-enable ()
432 ;; Always leave view mode before changing major mode.
433 ;; This is to guarantee that the buffer-read-only variable is restored.
434 (add-hook 'change-major-mode-hook
'view-mode-disable nil t
)
437 view-half-page-size nil
438 view-old-buffer-read-only buffer-read-only
440 (if (boundp 'Helper-return-blurb
)
441 (setq view-old-Helper-return-blurb
(and (boundp 'Helper-return-blurb
)
444 (format "continue viewing %s"
445 (if (buffer-file-name)
446 (file-name-nondirectory (buffer-file-name))
448 (force-mode-line-update)
449 (run-hooks 'view-mode-hook
))
451 (defun view-mode-disable ()
452 "Turn off View mode."
453 (remove-hook 'change-major-mode-hook
'view-mode-disable t
)
454 (and view-overlay
(delete-overlay view-overlay
))
455 (force-mode-line-update)
456 ;; Calling toggle-read-only while View mode is enabled
457 ;; sets view-read-only to t as a buffer-local variable
458 ;; after exiting View mode. That arranges that the next toggle-read-only
459 ;; will reenable View mode.
460 ;; Cancelling View mode in any other way should cancel that, too,
461 ;; so that View mode stays off if toggle-read-only is called.
462 (if (local-variable-p 'view-read-only
)
463 (kill-local-variable 'view-read-only
))
465 (if (boundp 'Helper-return-blurb
)
466 (setq Helper-return-blurb view-old-Helper-return-blurb
))
468 (setq buffer-read-only view-old-buffer-read-only
)))
471 (defun view-mode-enter (&optional quit-restore exit-action
)
472 "Enter View mode and set up exit from view mode depending on optional arguments.
473 Optional argument QUIT-RESTORE if non-nil must specify a valid
474 entry for quitting and restoring any window showing the current
475 buffer. This entry replaces any parameter installed by
476 `display-buffer' and is used by `view-mode-exit'.
478 Optional argument EXIT-ACTION, if non-nil, must specify a
479 function that takes a buffer as argument. This function will be
480 called by `view-mode-exit'.
482 For a list of all View commands, type H or h while viewing.
484 This function runs the normal hook `view-mode-hook'."
486 (dolist (window (get-buffer-window-list nil nil t
))
487 (set-window-parameter window
'quit-restore quit-restore
)))
490 (setq view-exit-action exit-action
))
494 (force-mode-line-update)
495 (unless view-inhibit-help-message
497 (substitute-command-keys "\
498 View mode: type \\[help-command] for help, \\[describe-mode] for commands, \\[View-quit] to quit.")))))
500 ;; This is awful because it assumes that the selected window shows the
501 ;; current buffer when this is called.
502 (defun view-mode-exit (&optional exit-only exit-action all-windows
)
503 "Exit View mode in various ways.
504 If all arguments are nil, remove the current buffer from the
505 selected window using the `quit-restore' information associated
506 with the selected window. If optional argument ALL-WINDOWS or
507 `view-exits-all-viewing-windows' are non-nil, remove the current
508 buffer from all windows showing it.
510 Optional argument EXIT-ONLY non-nil means just exit `view-mode'
511 \(unless `view-no-disable-on-exit' is non-nil) but do not change
512 the associations of any windows with the current buffer.
514 EXIT-ACTION, if non-nil, must specify a function that is called
515 with the current buffer as argument and is called after disabling
516 `view-mode' and removing any associations of windows with the
519 (let ((buffer (window-buffer)))
520 (unless view-no-disable-on-exit
525 ((or all-windows view-exits-all-viewing-windows
)
526 (dolist (window (get-buffer-window-list))
527 (quit-restore-window window
)))
528 ((eq (window-buffer) (current-buffer))
529 (quit-restore-window)))
532 (funcall exit-action buffer
))
533 (force-mode-line-update)))))
536 "Exit View mode but stay in current buffer."
541 (defun View-exit-and-edit ()
542 "Exit View mode and make the current buffer editable."
544 (let ((view-old-buffer-read-only nil
)
545 (view-no-disable-on-exit nil
))
549 "Quit View mode and maybe switch buffers, but don't kill this buffer."
554 "Quit View mode, trying to restore window and buffer to previous state.
555 Maybe kill this buffer. Try to restore selected window to previous state
556 and go to previous buffer or window."
558 (view-mode-exit nil view-exit-action
))
560 (defun View-quit-all ()
561 "Quit View mode, trying to restore windows and buffers to previous state.
562 Maybe kill current buffer. Try to restore all windows viewing buffer to
563 previous state and go to previous buffer or window."
565 (view-mode-exit nil view-exit-action t
))
567 (defun View-kill-and-leave ()
568 "Quit View mode, kill current buffer and return to previous buffer."
570 (view-mode-exit nil
(or view-exit-action
'kill-buffer
) t
))
573 ;;; Some help routines.
575 (defun view-window-size ()
576 ;; Return the height of the current window, excluding the mode line.
577 ;; Using `window-line-height' accounts for variable-height fonts.
578 (let ((h (window-line-height -
1)))
581 ;; This should not happen, but if `window-line-height' returns
582 ;; nil, fall back on `window-height'.
583 (1- (window-height)))))
585 ;; (defun view-last-command (&optional who what)
586 ;; (setq view-last-command-entry this-command)
587 ;; (setq view-last-command who)
588 ;; (setq view-last-command-argument what))
590 ;; (defun View-repeat-last-command ()
591 ;; "Repeat last command issued in View mode."
593 ;; (if (and view-last-command
594 ;; (eq view-last-command-entry last-command))
595 ;; (funcall view-last-command view-last-command-argument))
596 ;; (setq this-command view-last-command-entry))
598 (defun view-recenter ()
599 ;; Recenter point in window and redisplay normally.
602 (defun view-page-size-default (lines)
603 ;; If LINES is nil, 0, or larger than `view-window-size', return nil.
604 ;; Otherwise, return LINES.
606 (not (zerop (setq lines
(prefix-numeric-value lines
))))
608 (abs (- (view-window-size) next-screen-context-lines
)))
611 (defun view-set-half-page-size-default (lines)
612 ;; Get and maybe set half page size.
613 (if (not lines
) (or view-half-page-size
614 (/ (view-window-size) 2))
615 (setq view-half-page-size
616 (if (zerop (setq lines
(prefix-numeric-value lines
)))
617 (/ (view-window-size) 2)
618 (view-page-size-default lines
)))))
621 ;;; Commands for moving around in the buffer.
623 (defun View-goto-percent (&optional percent
)
624 "Move to end (or prefix PERCENT) of buffer in View mode.
625 Display is centered at point.
626 Also set the mark at the position where point was."
632 (floor (* (- (point-max) (point-min)) 0.01
633 (max 0 (min 100 (prefix-numeric-value percent
))))))
637 ;; (defun View-goto-line-last (&optional line)
638 ;; "Move to last (or prefix LINE) line in View mode.
639 ;; Display is centered at LINE.
640 ;; Sets mark at starting position and pushes mark ring."
643 ;; (if line (goto-line (prefix-numeric-value line))
644 ;; (goto-char (point-max))
645 ;; (beginning-of-line))
648 (defun View-goto-line (&optional line
)
649 "Move to first (or prefix LINE) line in View mode.
650 Display is centered at LINE.
651 Also set the mark at the position where point was."
654 (goto-char (point-min))
655 (forward-line (1- line
))
658 (defun View-back-to-mark (&optional ignore
)
659 "Return to last mark set in View mode, else beginning of file.
660 Display that line at the center of the window.
661 This command pops the mark ring, so that successive
662 invocations return to earlier marks."
664 (goto-char (or (mark t
) (point-min)))
668 (defun view-scroll-lines (lines backward default maxdefault
)
669 ;; This function does the job for all the scrolling commands.
670 ;; Scroll forward LINES lines. If BACKWARD is non-nil, scroll backwards.
671 ;; If LINES is negative scroll in the other direction.
672 ;; If LINES is 0 or nil, scroll DEFAULT lines (if DEFAULT is nil, scroll
673 ;; by one page). If MAXDEFAULT is non-nil, scroll no more than a window.
674 (if (or (null lines
) (zerop (setq lines
(prefix-numeric-value lines
))))
675 (setq lines default
))
676 (when (and lines
(< lines
0))
677 (setq backward
(not backward
) lines
(- lines
)))
678 (when (and maxdefault lines
(> lines
(view-window-size)))
680 (cond (backward (scroll-down lines
))
681 ((view-really-at-end)
682 (if view-scroll-auto-exit
687 (if (view-really-at-end) (view-end-message)))))
689 (defun view-really-at-end ()
690 ;; Return true if buffer end visible. Maybe revert buffer and test.
691 (and (pos-visible-in-window-p (point-max))
692 (let ((buf (current-buffer))
693 (bufname (buffer-name))
694 (file (buffer-file-name)))
695 (or (not view-try-extend-at-buffer-end
)
697 (verify-visited-file-modtime buf
)
698 (not (file-exists-p file
))
699 (when (buffer-modified-p buf
)
700 (setq file
(file-name-nondirectory file
))
703 "File %s changed on disk. Discard your edits%s? "
705 (if (string= bufname file
) ""
706 (concat " in " bufname
))))))
708 (revert-buffer t t t
)
709 (pos-visible-in-window-p (point-max)))))))
711 (defun view-end-message ()
712 ;; Tell that we are at end of buffer.
713 (goto-char (point-max))
714 (if (window-parameter nil
'quit-restore
)
715 (message "End of buffer. Type %s to quit viewing."
716 (substitute-command-keys
717 (if view-scroll-auto-exit
"\\[View-scroll-page-forward]"
719 (message "End of buffer")))
721 (defun View-scroll-to-buffer-end ()
722 "Scroll backward or forward so that buffer end is at last line of window."
724 (let ((p (if (pos-visible-in-window-p (point-max)) (point))))
725 (goto-char (point-max))
727 (and p
(goto-char p
))))
729 (defun View-scroll-page-forward (&optional lines
)
730 "Scroll \"page size\" or prefix LINES lines forward in View mode.
731 Exit if end of text is visible and `view-scroll-auto-exit' is non-nil.
732 \"page size\" is whole window full, or number of lines set by
733 \\[View-scroll-page-forward-set-page-size] or
734 \\[View-scroll-page-backward-set-page-size].
735 If LINES is more than a window-full, only the last window-full is shown."
737 (view-scroll-lines lines nil
(view-page-size-default view-page-size
) nil
))
739 (defun View-scroll-page-backward (&optional lines
)
740 "Scroll \"page size\" or prefix LINES lines backward in View mode.
741 See also `View-scroll-page-forward'."
743 (view-scroll-lines lines t
(view-page-size-default view-page-size
) nil
))
745 (defun View-scroll-page-forward-set-page-size (&optional lines
)
746 "Scroll forward LINES lines in View mode, setting the \"page size\".
747 This is the number of lines which \\[View-scroll-page-forward] and
748 \\[View-scroll-page-backward] scroll by default.
749 If LINES is omitted or = 0, sets \"page size\" to window height and
750 scrolls forward that much, otherwise scrolls forward LINES lines and sets
751 \"page size\" to the minimum of window height and the absolute value of LINES.
752 See also `View-scroll-page-forward'."
754 (view-scroll-lines lines nil
755 (setq view-page-size
(view-page-size-default lines
))
758 (defun View-scroll-page-backward-set-page-size (&optional lines
)
759 "Scroll backward prefix LINES lines in View mode, setting the \"page size\".
760 See also `View-scroll-page-forward-set-page-size'."
762 (view-scroll-lines lines t
763 (setq view-page-size
(view-page-size-default lines
))
766 (defun View-scroll-line-forward (&optional lines
)
767 "Scroll forward one line (or prefix LINES lines) in View mode.
768 See also `View-scroll-page-forward', but note that scrolling is limited
769 to minimum of LINES and one window-full."
771 (view-scroll-lines lines nil
1 t
))
773 (defun View-scroll-line-backward (&optional lines
)
774 "Scroll backward one line (or prefix LINES lines) in View mode.
775 See also `View-scroll-line-forward'."
777 (view-scroll-lines lines t
1 t
))
779 (defun View-scroll-half-page-forward (&optional lines
)
780 "Scroll forward a \"half page\" (or prefix LINES) lines in View mode.
781 If LINES is not omitted, the \"half page size\" is set to the minimum of
782 window height and the absolute value of LINES.
783 LINES=0 resets \"half page size\" to half window height."
785 (view-scroll-lines lines nil
(view-set-half-page-size-default lines
) t
))
787 (defun View-scroll-half-page-backward (&optional lines
)
788 "Scroll backward a \"half page\" (or prefix LINES) lines in View mode.
789 See also `View-scroll-half-page-forward'."
791 (view-scroll-lines lines t
(view-set-half-page-size-default lines
) t
))
793 (defun View-revert-buffer-scroll-page-forward (&optional lines
)
794 "Scroll forward, reverting buffer if needed, in View mode.
795 If buffer has not been changed and the corresponding file is newer, first
796 revert the buffer, then scroll.
797 This command is useful if you are viewing a changing file.
799 The prefix argument LINES says how many lines to scroll.
800 If you don't specify a prefix argument, it uses the number of lines set by
801 \\[View-scroll-page-forward-set-page-size] or
802 \\[View-scroll-page-backward-set-page-size].
803 If LINES is more than a window-full, only the last window-full is shown."
805 (let ((view-scroll-auto-exit nil
)
806 (view-try-extend-at-buffer-end t
))
807 (view-scroll-lines lines nil
(view-page-size-default view-page-size
) nil
)))
809 (defun View-search-regexp-forward (n regexp
)
810 "Search forward for first (or prefix Nth) occurrence of REGEXP in View mode.
812 Displays line found at center of window. Sets mark at starting position and
815 Characters @ and ! are special at the beginning of REGEXP. They modify
816 the search rather than become part of the pattern searched for.
817 @ means search all the buffer i.e. start search at the beginning of buffer.
818 ! means search for a line that contains no match for the pattern.
819 If REGEXP is empty or only consist of these control characters, then
820 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
821 for use by later search commands.
823 The variable `view-highlight-face' controls the face that is used
824 for highlighting the match that is found."
825 (interactive "p\nsSearch forward (regexp): ")
826 (view-search n regexp
))
828 (defun View-search-regexp-backward (n regexp
)
829 "Search backward for first (or prefix Nth) occurrence of REGEXP in View mode.
831 Displays line found at center of window. Sets mark at starting position and
834 Characters @ and ! are special at the beginning of REGEXP. They modify
835 the search rather than become part of the pattern searched for.
836 @ means search all the buffer i.e. start search at the end of buffer.
837 ! means search for a line that contains no match for the pattern.
838 If REGEXP is empty or only consist of these control characters, then
839 an earlier remembered REGEXP is used, otherwise REGEXP is remembered
840 for use by later search commands.
842 The variable `view-highlight-face' controls the face that is used
843 for highlighting the match that is found."
844 (interactive "p\nsSearch backward (regexp): ")
845 (view-search (- n
) regexp
))
847 (defun View-search-last-regexp-forward (n) "\
848 Search forward for first (or prefix Nth) instance of last regexp in View mode.
849 Displays line found at center of window. Sets mark at starting position and
852 The variable `view-highlight-face' controls the face that is used
853 for highlighting the match that is found."
857 (defun View-search-last-regexp-backward (n) "\
858 Search backward for first (or prefix Nth) instance of last regexp in View mode.
859 Displays line found at center of window. Sets mark at starting position and
862 The variable `view-highlight-face' controls the face that is used
863 for highlighting the match that is found."
865 (view-search (- n
) nil
))
867 (defun view-search (times regexp
)
868 ;; This function does the job for all the View-search- commands.
869 ;; Search for the TIMESt match for REGEXP. If TIMES is negative
870 ;; search backwards. If REGEXP is nil use `view-last-regexp'.
871 ;; Characters "!" and "@" have a special meaning at the beginning of
872 ;; REGEXP and are removed from REGEXP before the search "!" means
873 ;; search for lines with no match for REGEXP. "@" means search in
874 ;; the whole buffer, don't start searching from the present point.
875 (let (where no end ln
)
877 ((and regexp
(> (length regexp
) 0)
878 (or (not (memq (string-to-char regexp
) '(?
! ?
@)))
880 (if (member (substring regexp
0 2) '("!@" "@!"))
881 (setq end t no t ln
2)
882 (setq no
(not (setq end
(eq ?
@ (string-to-char regexp
))))
884 (> (length (setq regexp
(substring regexp ln
))) 0))))
885 (setq view-last-regexp
(if no
(list regexp
) regexp
)))
886 ((consp view-last-regexp
)
887 (setq regexp
(car view-last-regexp
))
888 (unless (setq no
(not no
)) (setq view-last-regexp regexp
)))
889 (view-last-regexp (setq regexp view-last-regexp
)
890 (if no
(setq view-last-regexp
(list regexp
))))
891 (t (error "No previous View-mode search")))
893 (if end
(goto-char (if (< times
0) (point-max) (point-min)))
894 (move-to-window-line (if (< times
0) 0 -
1)))
895 (if (if no
(view-search-no-match-lines times regexp
)
896 (re-search-forward regexp nil t times
))
897 (setq where
(point))))
903 (move-overlay view-overlay
(match-beginning 0) (match-end 0))
905 (make-overlay (match-beginning 0) (match-end 0))))
906 (overlay-put view-overlay
'face view-highlight-face
)
909 (message "Can't find occurrence %d of %s%s"
910 times
(if no
"no " "") regexp
)
913 ;; This is the dumb approach, looking at each line. The original
914 ;; version of this function looked like it might have been trying to
915 ;; do something clever, but not succeeding:
916 ;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2007-09/msg00073.html
917 (defun view-search-no-match-lines (times regexp
)
918 "Search for the TIMESth occurrence of a line with no match for REGEXP.
919 If such a line is found, return non-nil and set the match-data to that line.
920 If TIMES is negative, search backwards."
921 (let ((step (if (>= times
0) 1
922 (setq times
(- times
))
924 ;; Note that we do not check the current line.
925 (while (and (> times
0)
926 (zerop (forward-line step
)))
927 ;; (forward-line 1) returns 0 on moving within the last line.
930 (or (re-search-forward regexp
(line-end-position) t
)
931 (setq times
(1- times
))))))
937 ;; arch-tag: 6d0ace36-1d12-4de3-8de3-1fa3231636d7
938 ;;; view.el ends here