merge trunk
[bpt/emacs.git] / lisp / view.el
... / ...
CommitLineData
1;;; view.el --- peruse file or buffer without editing
2
3;; Copyright (C) 1985, 1989, 1994-1995, 1997, 2000-2012
4;; Free Software Foundation, Inc.
5
6;; Author: K. Shane Hartman
7;; Maintainer: Inge Frick <inge@nada.kth.se>
8;; Keywords: files
9
10;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
25;;; Commentary:
26
27;; This package provides the `view' minor mode documented in the Emacs
28;; user's manual.
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.
36
37;;; Suggested key bindings:
38;;
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
41;;
42;; You could also bind view-file, view-buffer, view-buffer-other-window and
43;; view-buffer-other-frame to keys.
44\f
45;;; Code:
46
47(defgroup view nil
48 "Peruse file or buffer without editing."
49 :link '(function-link view-mode)
50 :link '(custom-manual "(emacs)Misc File Ops")
51 :group 'wp)
52
53(defcustom view-highlight-face 'highlight
54 "The face used for highlighting the match found by View mode search."
55 :type 'face
56 :group 'view)
57
58(defcustom view-scroll-auto-exit nil
59 "Non-nil means scrolling past the end of buffer exits View mode.
60A value of nil means attempting to scroll past the end of the buffer,
61only rings the bell and gives a message on how to leave."
62 :type 'boolean
63 :group 'view)
64
65(defcustom view-try-extend-at-buffer-end nil
66 "Non-nil means try to load more of file when reaching end of buffer.
67This variable is mainly intended to be temporarily set to non-nil by
68the F command in view-mode, but you can set it to t if you want the action
69for all scroll commands in view mode."
70 :type 'boolean
71 :group 'view)
72
73;;;###autoload
74(defcustom view-remove-frame-by-deleting t
75 "Determine how View mode removes a frame no longer needed.
76If nil, make an icon of the frame. If non-nil, delete the frame."
77 :type 'boolean
78 :group 'view
79 :version "23.1")
80
81(defcustom view-exits-all-viewing-windows nil
82 "Non-nil means restore all windows used to view buffer.
83Commands that restore windows when finished viewing a buffer,
84apply to all windows that display the buffer and have restore
85information. If `view-exits-all-viewing-windows' is nil, only
86the selected window is considered for restoring."
87 :type 'boolean
88 :group 'view)
89
90(defcustom view-inhibit-help-message nil
91 "Non-nil inhibits the help message shown upon entering View mode."
92 :type 'boolean
93 :group 'view
94 :version "22.1")
95
96;;;###autoload
97(defvar view-mode nil
98 "Non-nil if View mode is enabled.
99Don't change this variable directly, you must change it by one of the
100functions that enable or disable view mode.")
101;;;###autoload
102(make-variable-buffer-local 'view-mode)
103
104(defcustom view-mode-hook nil
105 "Normal hook run when starting to view a buffer or file."
106 :type 'hook
107 :group 'view)
108\f
109(defvar view-old-buffer-read-only nil)
110(make-variable-buffer-local 'view-old-buffer-read-only)
111
112(defvar view-old-Helper-return-blurb)
113(make-variable-buffer-local 'view-old-Helper-return-blurb)
114
115(defvar view-page-size nil
116 "Default number of lines to scroll by View page commands.
117If nil that means use the window size.")
118(make-variable-buffer-local 'view-page-size)
119
120(defvar view-half-page-size nil
121 "Default number of lines to scroll by View half page commands.
122If nil that means use half the window size.")
123(make-variable-buffer-local 'view-half-page-size)
124
125(defvar view-last-regexp nil)
126(make-variable-buffer-local 'view-last-regexp) ; Global is better???
127
128(defvar view-return-to-alist nil
129 "What to do with used windows and where to go when finished viewing buffer.
130This is local in each buffer being viewed.
131It is added to by `view-mode-enter' when starting to view a buffer and
132subtracted from by `view-mode-exit' when finished viewing the buffer.
133
134See RETURN-TO-ALIST argument of function `view-mode-exit' for the format of
135`view-return-to-alist'.")
136(make-obsolete-variable
137 'view-return-to-alist "this variable is no more used." "24.1")
138(make-variable-buffer-local 'view-return-to-alist)
139(put 'view-return-to-alist 'permanent-local t)
140
141(defvar view-exit-action nil
142 "If non-nil, a function with one argument (a buffer) called when finished viewing.
143Commands like \\[view-file] and \\[view-file-other-window] may
144set this to bury or kill the viewed buffer.
145Observe that the buffer viewed might not appear in any window at
146the time this function is called.")
147(make-variable-buffer-local 'view-exit-action)
148
149(defvar view-no-disable-on-exit nil
150 "If non-nil, View mode \"exit\" commands don't actually disable View mode.
151Instead, these commands just switch buffers or windows.
152This is set in certain buffers by specialized features such as help commands
153that use View mode automatically.")
154
155(defvar view-overlay nil
156 "Overlay used to display where a search operation found its match.
157This is local in each buffer, once it is used.")
158(make-variable-buffer-local 'view-overlay)
159\f
160;; Define keymap inside defvar to make it easier to load changes.
161;; Some redundant "less"-like key bindings below have been commented out.
162(defvar view-mode-map
163 (let ((map (make-sparse-keymap)))
164 (define-key map "C" 'View-kill-and-leave)
165 (define-key map "c" 'View-leave)
166 (define-key map "Q" 'View-quit-all)
167 (define-key map "E" 'View-exit-and-edit)
168 ;; (define-key map "v" 'View-exit)
169 (define-key map "e" 'View-exit)
170 (define-key map "q" 'View-quit)
171 ;; (define-key map "N" 'View-search-last-regexp-backward)
172 (define-key map "p" 'View-search-last-regexp-backward)
173 (define-key map "n" 'View-search-last-regexp-forward)
174 ;; (define-key map "?" 'View-search-regexp-backward) ; Less does this.
175 (define-key map "\\" 'View-search-regexp-backward)
176 (define-key map "/" 'View-search-regexp-forward)
177 (define-key map "r" 'isearch-backward)
178 (define-key map "s" 'isearch-forward)
179 (define-key map "m" 'point-to-register)
180 (define-key map "'" 'register-to-point)
181 (define-key map "x" 'exchange-point-and-mark)
182 (define-key map "@" 'View-back-to-mark)
183 (define-key map "." 'set-mark-command)
184 (define-key map "%" 'View-goto-percent)
185 ;; (define-key map "G" 'View-goto-line-last)
186 (define-key map "g" 'View-goto-line)
187 (define-key map "=" 'what-line)
188 (define-key map "F" 'View-revert-buffer-scroll-page-forward)
189 ;; (define-key map "k" 'View-scroll-line-backward)
190 (define-key map "y" 'View-scroll-line-backward)
191 ;; (define-key map "j" 'View-scroll-line-forward)
192 (define-key map "\n" 'View-scroll-line-forward)
193 (define-key map "\r" 'View-scroll-line-forward)
194 (define-key map "u" 'View-scroll-half-page-backward)
195 (define-key map "d" 'View-scroll-half-page-forward)
196 (define-key map "z" 'View-scroll-page-forward-set-page-size)
197 (define-key map "w" 'View-scroll-page-backward-set-page-size)
198 ;; (define-key map "b" 'View-scroll-page-backward)
199 (define-key map "\C-?" 'View-scroll-page-backward)
200 ;; (define-key map "f" 'View-scroll-page-forward)
201 (define-key map " " 'View-scroll-page-forward)
202 (define-key map "o" 'View-scroll-to-buffer-end)
203 (define-key map ">" 'end-of-buffer)
204 (define-key map "<" 'beginning-of-buffer)
205 (define-key map "-" 'negative-argument)
206 (define-key map "9" 'digit-argument)
207 (define-key map "8" 'digit-argument)
208 (define-key map "7" 'digit-argument)
209 (define-key map "6" 'digit-argument)
210 (define-key map "5" 'digit-argument)
211 (define-key map "4" 'digit-argument)
212 (define-key map "3" 'digit-argument)
213 (define-key map "2" 'digit-argument)
214 (define-key map "1" 'digit-argument)
215 (define-key map "0" 'digit-argument)
216 (define-key map "H" 'describe-mode)
217 (define-key map "?" 'describe-mode) ; Maybe do as less instead? See above.
218 (define-key map "h" 'describe-mode)
219 map))
220\f
221;;; Commands that enter or exit view mode.
222
223;; This is used when view mode is exited, to make sure we don't try to
224;; kill a buffer modified by the user. A buffer in view mode can
225;; become modified if the user types C-x C-q, edits the buffer, then
226;; types C-x C-q again to return to view mode.
227;;;###autoload
228(defun kill-buffer-if-not-modified (buf)
229 "Like `kill-buffer', but does nothing if the buffer is modified."
230 (let ((buf (get-buffer buf)))
231 (and buf (not (buffer-modified-p buf))
232 (kill-buffer buf))))
233
234;;;###autoload
235(defun view-file (file)
236 "View FILE in View mode, returning to previous buffer when done.
237Emacs commands editing the buffer contents are not available; instead, a
238special set of commands (mostly letters and punctuation) are defined for
239moving around in the buffer.
240Space scrolls forward, Delete scrolls backward.
241For a list of all View commands, type H or h while viewing.
242
243This command runs the normal hook `view-mode-hook'."
244 (interactive "fView file: ")
245 (unless (file-exists-p file) (error "%s does not exist" file))
246 (let ((had-a-buf (get-file-buffer file))
247 (buffer (find-file-noselect file)))
248 (view-buffer buffer (and (not had-a-buf) 'kill-buffer-if-not-modified))))
249
250;;;###autoload
251(defun view-file-other-window (file)
252 "View FILE in View mode in another window.
253When done, return that window to its previous buffer, and kill the
254buffer visiting FILE if unmodified and if it wasn't visited before.
255
256Emacs commands editing the buffer contents are not available; instead,
257a special set of commands (mostly letters and punctuation)
258are defined for moving around in the buffer.
259Space scrolls forward, Delete scrolls backward.
260For a list of all View commands, type H or h while viewing.
261
262This command runs the normal hook `view-mode-hook'."
263 (interactive "fIn other window view file: ")
264 (unless (file-exists-p file) (error "%s does not exist" file))
265 (let ((had-a-buf (get-file-buffer file))
266 (buf-to-view (find-file-noselect file)))
267 (view-buffer-other-window buf-to-view nil
268 (and (not had-a-buf)
269 'kill-buffer-if-not-modified))))
270
271;;;###autoload
272(defun view-file-other-frame (file)
273 "View FILE in View mode in another frame.
274When done, kill the buffer visiting FILE if unmodified and if it wasn't
275visited before; also, maybe delete other frame and/or return to previous
276buffer.
277
278Emacs commands editing the buffer contents are not available; instead,
279a special set of commands (mostly letters and punctuation)
280are defined for moving around in the buffer.
281Space scrolls forward, Delete scrolls backward.
282For a list of all View commands, type H or h while viewing.
283
284This command runs the normal hook `view-mode-hook'."
285 (interactive "fIn other frame view file: ")
286 (unless (file-exists-p file) (error "%s does not exist" file))
287 (let ((had-a-buf (get-file-buffer file))
288 (buf-to-view (find-file-noselect file)))
289 (view-buffer-other-frame buf-to-view nil
290 (and (not had-a-buf)
291 'kill-buffer-if-not-modified))))
292
293
294;;;###autoload
295(defun view-buffer (buffer &optional exit-action)
296 "View BUFFER in View mode, returning to previous buffer when done.
297Emacs commands editing the buffer contents are not available; instead, a
298special set of commands (mostly letters and punctuation) are defined for
299moving around in the buffer.
300Space scrolls forward, Delete scrolls backward.
301For a list of all View commands, type H or h while viewing.
302
303This command runs the normal hook `view-mode-hook'.
304
305Optional argument EXIT-ACTION is either nil or a function with buffer as
306argument. This function is called when finished viewing buffer. Use
307this argument instead of explicitly setting `view-exit-action'.
308
309Do not set EXIT-ACTION to `kill-buffer' when BUFFER visits a
310file: Users may suspend viewing in order to modify the buffer.
311Exiting View mode will then discard the user's edits. Setting
312EXIT-ACTION to `kill-buffer-if-not-modified' avoids this.
313
314This function does not enable View mode if the buffer's major-mode
315has a `special' mode-class, because such modes usually have their
316own View-like bindings."
317 (interactive "bView buffer: ")
318 (switch-to-buffer buffer)
319 (if (eq (get major-mode 'mode-class) 'special)
320 (message "Not using View mode because the major mode is special")
321 (view-mode-enter nil exit-action)))
322
323;;;###autoload
324(defun view-buffer-other-window (buffer &optional not-return exit-action)
325 "View BUFFER in View mode in another window.
326Emacs commands editing the buffer contents are not available;
327instead, a special set of commands (mostly letters and
328punctuation) are defined for moving around in the buffer.
329Space scrolls forward, Delete scrolls backward.
330For a list of all View commands, type H or h while viewing.
331
332This command runs the normal hook `view-mode-hook'.
333
334Optional argument NOT-RETURN is ignored.
335
336Optional argument EXIT-ACTION is either nil or a function with buffer as
337argument. This function is called when finished viewing buffer. Use
338this argument instead of explicitly setting `view-exit-action'.
339
340This function does not enable View mode if the buffer's major-mode
341has a `special' mode-class, because such modes usually have their
342own View-like bindings."
343 (interactive "bIn other window view buffer:\nP")
344 (let ((pop-up-windows t))
345 (pop-to-buffer buffer t))
346 (if (eq (get major-mode 'mode-class) 'special)
347 (message "Not using View mode because the major mode is special")
348 (view-mode-enter nil exit-action)))
349
350;;;###autoload
351(defun view-buffer-other-frame (buffer &optional not-return exit-action)
352 "View BUFFER in View mode in another frame.
353Emacs commands editing the buffer contents are not available;
354instead, a special set of commands (mostly letters and
355punctuation) are defined for moving around in the buffer.
356Space scrolls forward, Delete scrolls backward.
357For a list of all View commands, type H or h while viewing.
358
359This command runs the normal hook `view-mode-hook'.
360
361Optional argument NOT-RETURN is ignored.
362
363Optional argument EXIT-ACTION is either nil or a function with buffer as
364argument. This function is called when finished viewing buffer. Use
365this argument instead of explicitly setting `view-exit-action'.
366
367This function does not enable View mode if the buffer's major-mode
368has a `special' mode-class, because such modes usually have their
369own View-like bindings."
370 (interactive "bView buffer in other frame: \nP")
371 (let ((pop-up-frames t))
372 (pop-to-buffer buffer t))
373 (if (eq (get major-mode 'mode-class) 'special)
374 (message "Not using View mode because the major mode is special")
375 (view-mode-enter nil exit-action)))
376\f
377;;;###autoload
378(define-minor-mode view-mode
379 ;; In the following documentation string we have to use some explicit key
380 ;; bindings instead of using the \\[] construction. The reason for this
381 ;; is that most commands have more than one key binding.
382 "Toggle View mode, a minor mode for viewing text but not editing it.
383With a prefix argument ARG, enable View mode if ARG is positive,
384and disable it otherwise. If called from Lisp, enable View mode
385if ARG is omitted or nil.
386
387When View mode is enabled, commands that do not change the buffer
388contents are available as usual. Kill commands insert text in
389kill buffers but do not delete. Most other commands beep and
390tell the user that the buffer is read-only.
391
392\\<view-mode-map>
393
394The following additional commands are provided. Most commands
395take prefix arguments. Page commands default to \"page size\"
396lines which is almost a whole window, or number of lines set by
397\\[View-scroll-page-forward-set-page-size] or \\[View-scroll-page-backward-set-page-size].
398Half page commands default to and set \"half page size\" lines
399which initially is half a window full. Search commands default
400to a repeat count of one.
401
402H, h, ? This message.
403Digits provide prefix arguments.
404\\[negative-argument] negative prefix argument.
405\\[beginning-of-buffer] move to the beginning of buffer.
406> move to the end of buffer.
407\\[View-scroll-to-buffer-end] scroll so that buffer end is at last line of window.
408SPC scroll forward \"page size\" lines.
409 With prefix scroll forward prefix lines.
410DEL scroll backward \"page size\" lines.
411 With prefix scroll backward prefix lines.
412\\[View-scroll-page-forward-set-page-size] like \\[View-scroll-page-forward] but with prefix sets \"page size\" to prefix.
413\\[View-scroll-page-backward-set-page-size] like \\[View-scroll-page-backward] but with prefix sets \"page size\" to prefix.
414\\[View-scroll-half-page-forward] scroll forward \"half page size\" lines. With prefix, sets
415 \"half page size\" to prefix lines and scrolls forward that much.
416\\[View-scroll-half-page-backward] scroll backward \"half page size\" lines. With prefix, sets
417 \"half page size\" to prefix lines and scrolls backward that much.
418RET, LFD scroll forward one line. With prefix scroll forward prefix line(s).
419y scroll backward one line. With prefix scroll backward prefix line(s).
420\\[View-revert-buffer-scroll-page-forward] revert-buffer if necessary and scroll forward.
421 Use this to view a changing file.
422\\[what-line] prints the current line number.
423\\[View-goto-percent] goes prefix argument (default 100) percent into buffer.
424\\[View-goto-line] goes to line given by prefix argument (default first line).
425. set the mark.
426x exchanges point and mark.
427\\[View-back-to-mark] return to mark and pops mark ring.
428 Mark ring is pushed at start of every successful search and when
429 jump to line occurs. The mark is set on jump to buffer start or end.
430\\[point-to-register] save current position in character register.
431' go to position saved in character register.
432s do forward incremental search.
433r do reverse incremental search.
434\\[View-search-regexp-forward] searches forward for regular expression, starting after current page.
435 ! and @ have a special meaning at the beginning of the regexp.
436 ! means search for a line with no match for regexp. @ means start
437 search at beginning (end for backward search) of buffer.
438\\ searches backward for regular expression, starting before current page.
439\\[View-search-last-regexp-forward] searches forward for last regular expression.
440p searches backward for last regular expression.
441\\[View-quit] quit View mode, restoring this window and buffer to previous state.
442 \\[View-quit] is the normal way to leave view mode.
443\\[View-exit] exit View mode but stay in current buffer. Use this if you started
444 viewing a buffer (file) and find out you want to edit it.
445 This command restores the previous read-only status of the buffer.
446\\[View-exit-and-edit] exit View mode, and make the current buffer editable
447 even if it was not editable before entry to View mode.
448\\[View-quit-all] quit View mode, restoring all windows to previous state.
449\\[View-leave] quit View mode and maybe switch buffers, but don't kill this buffer.
450\\[View-kill-and-leave] quit View mode, kill current buffer and go back to other buffer.
451
452The effect of \\[View-leave], \\[View-quit] and \\[View-kill-and-leave] depends on how view-mode was entered. If it was
453entered by view-file, view-file-other-window, view-file-other-frame, or
454\\[dired-view-file] \(\\[view-file], \\[view-file-other-window],
455\\[view-file-other-frame], or the Dired mode v command),
456then \\[View-quit] will try to kill the current buffer.
457If view-mode was entered from another buffer, by \\[view-buffer],
458\\[view-buffer-other-window], \\[view-buffer-other frame], \\[view-file],
459\\[view-file-other-window], or \\[view-file-other-frame],
460then \\[View-leave], \\[View-quit] and \\[View-kill-and-leave] will return to that buffer.
461
462Entry to view-mode runs the normal hook `view-mode-hook'."
463 :lighter " View" :keymap view-mode-map
464 (if view-mode (view-mode-enable) (view-mode-disable)))
465\f
466(defun view-mode-enable ()
467 "Turn on View mode."
468 ;; Always leave view mode before changing major mode.
469 ;; This is to guarantee that the buffer-read-only variable is restored.
470 (add-hook 'change-major-mode-hook 'view-mode-disable nil t)
471 (setq view-mode t
472 view-page-size nil
473 view-half-page-size nil
474 view-old-buffer-read-only buffer-read-only
475 buffer-read-only t)
476 (if (boundp 'Helper-return-blurb)
477 (setq view-old-Helper-return-blurb (and (boundp 'Helper-return-blurb)
478 Helper-return-blurb)
479 Helper-return-blurb
480 (format "continue viewing %s"
481 (if (buffer-file-name)
482 (file-name-nondirectory (buffer-file-name))
483 (buffer-name)))))
484 (force-mode-line-update)
485 (run-hooks 'view-mode-hook))
486
487(defun view-mode-disable ()
488 "Turn off View mode."
489 (remove-hook 'change-major-mode-hook 'view-mode-disable t)
490 (and view-overlay (delete-overlay view-overlay))
491 (force-mode-line-update)
492 ;; Calling toggle-read-only while View mode is enabled
493 ;; sets view-read-only to t as a buffer-local variable
494 ;; after exiting View mode. That arranges that the next toggle-read-only
495 ;; will reenable View mode.
496 ;; Canceling View mode in any other way should cancel that, too,
497 ;; so that View mode stays off if toggle-read-only is called.
498 (if (local-variable-p 'view-read-only)
499 (kill-local-variable 'view-read-only))
500 (setq view-mode nil)
501 (if (boundp 'Helper-return-blurb)
502 (setq Helper-return-blurb view-old-Helper-return-blurb))
503 (if buffer-read-only
504 (setq buffer-read-only view-old-buffer-read-only)))
505
506;;;###autoload
507(defun view-return-to-alist-update (buffer &optional item)
508 "Update `view-return-to-alist' of buffer BUFFER.
509Remove from `view-return-to-alist' all entries referencing dead
510windows. Optional argument ITEM non-nil means add ITEM to
511`view-return-to-alist' after purging. For a description of items
512that can be added see the RETURN-TO-ALIST argument of the
513function `view-mode-exit'. If `view-return-to-alist' contains an
514entry for the selected window, purge that entry from
515`view-return-to-alist' before adding ITEM."
516 (with-current-buffer buffer
517 (when view-return-to-alist
518 (let* ((list view-return-to-alist)
519 entry entry-window last)
520 (while list
521 (setq entry (car list))
522 (setq entry-window (car entry))
523 (if (and (windowp entry-window)
524 (or (and item (eq entry-window (selected-window)))
525 (not (window-live-p entry-window))))
526 ;; Remove that entry.
527 (if last
528 (setcdr last (cdr list))
529 (setq view-return-to-alist
530 (cdr view-return-to-alist)))
531 ;; Leave entry alone.
532 (setq last entry))
533 (setq list (cdr list)))))
534 ;; Add ITEM.
535 (when item
536 (setq view-return-to-alist
537 (cons item view-return-to-alist)))))
538(make-obsolete 'view-return-to-alist-update "this function has no effect." "24.1")
539
540;;;###autoload
541(defun view-mode-enter (&optional quit-restore exit-action)
542 "Enter View mode and set up exit from view mode depending on optional arguments.
543Optional argument QUIT-RESTORE if non-nil must specify a valid
544entry for quitting and restoring any window showing the current
545buffer. This entry replaces any parameter installed by
546`display-buffer' and is used by `view-mode-exit'.
547
548Optional argument EXIT-ACTION, if non-nil, must specify a
549function that takes a buffer as argument. This function will be
550called by `view-mode-exit'.
551
552For a list of all View commands, type H or h while viewing.
553
554This function runs the normal hook `view-mode-hook'."
555 (when quit-restore
556 (dolist (window (get-buffer-window-list nil nil t))
557 (set-window-parameter window 'quit-restore quit-restore)))
558
559 (when exit-action
560 (setq view-exit-action exit-action))
561
562 (unless view-mode
563 (view-mode-enable)
564 (force-mode-line-update)
565 (unless view-inhibit-help-message
566 (message "%s"
567 (substitute-command-keys "\
568View mode: type \\[help-command] for help, \\[describe-mode] for commands, \\[View-quit] to quit.")))))
569\f
570;; This is awful because it assumes that the selected window shows the
571;; current buffer when this is called.
572(defun view-mode-exit (&optional exit-only exit-action all-windows)
573 "Exit View mode in various ways.
574If all arguments are nil, remove the current buffer from the
575selected window using the `quit-restore' information associated
576with the selected window. If optional argument ALL-WINDOWS or
577`view-exits-all-viewing-windows' are non-nil, remove the current
578buffer from all windows showing it.
579
580Optional argument EXIT-ONLY non-nil means just exit `view-mode'
581\(unless `view-no-disable-on-exit' is non-nil) but do not change
582the associations of any windows with the current buffer.
583
584EXIT-ACTION, if non-nil, must specify a function that is called
585with the current buffer as argument and is called after disabling
586`view-mode' and removing any associations of windows with the
587current buffer. "
588 (when view-mode
589 (let ((buffer (window-buffer)))
590 (unless view-no-disable-on-exit
591 (view-mode-disable))
592
593 (unless exit-only
594 (cond
595 ((or all-windows view-exits-all-viewing-windows)
596 (dolist (window (get-buffer-window-list))
597 (quit-window nil window)))
598 ((eq (window-buffer) (current-buffer))
599 (quit-window)))
600
601 (when exit-action
602 (funcall exit-action buffer))
603 (force-mode-line-update)))))
604\f
605(defun View-exit ()
606 "Exit View mode but stay in current buffer."
607 (interactive)
608 (view-mode-exit t))
609
610;;;###autoload
611(defun View-exit-and-edit ()
612 "Exit View mode and make the current buffer editable."
613 (interactive)
614 (let ((view-old-buffer-read-only nil)
615 (view-no-disable-on-exit nil))
616 (view-mode-exit t)))
617
618(defun View-leave ()
619 "Quit View mode and maybe switch buffers, but don't kill this buffer."
620 (interactive)
621 (view-mode-exit))
622
623(defun View-quit ()
624 "Quit View mode, trying to restore window and buffer to previous state.
625Maybe kill this buffer. Try to restore selected window to previous state
626and go to previous buffer or window."
627 (interactive)
628 (view-mode-exit nil view-exit-action))
629
630(defun View-quit-all ()
631 "Quit View mode, trying to restore windows and buffers to previous state.
632Maybe kill current buffer. Try to restore all windows viewing buffer to
633previous state and go to previous buffer or window."
634 (interactive)
635 (view-mode-exit nil view-exit-action t))
636
637(defun View-kill-and-leave ()
638 "Quit View mode, kill current buffer and return to previous buffer."
639 (interactive)
640 (view-mode-exit nil (or view-exit-action 'kill-buffer) t))
641\f
642
643;;; Some help routines.
644
645(defun view-window-size ()
646 ;; Return the height of the current window, excluding the mode line.
647 ;; Using `window-line-height' accounts for variable-height fonts.
648 (let ((h (window-line-height -1)))
649 (if h
650 (1+ (nth 1 h))
651 ;; This should not happen, but if `window-line-height' returns
652 ;; nil, fall back on `window-height'.
653 (1- (window-height)))))
654
655;; (defun view-last-command (&optional who what)
656;; (setq view-last-command-entry this-command)
657;; (setq view-last-command who)
658;; (setq view-last-command-argument what))
659
660;; (defun View-repeat-last-command ()
661;; "Repeat last command issued in View mode."
662;; (interactive)
663;; (if (and view-last-command
664;; (eq view-last-command-entry last-command))
665;; (funcall view-last-command view-last-command-argument))
666;; (setq this-command view-last-command-entry))
667
668(defun view-recenter ()
669 ;; Recenter point in window and redisplay normally.
670 (recenter '(1)))
671
672(defun view-page-size-default (lines)
673 ;; If LINES is nil, 0, or larger than `view-window-size', return nil.
674 ;; Otherwise, return LINES.
675 (and lines
676 (not (zerop (setq lines (prefix-numeric-value lines))))
677 (<= (abs lines)
678 (abs (- (view-window-size) next-screen-context-lines)))
679 (abs lines)))
680
681(defun view-set-half-page-size-default (lines)
682 ;; Get and maybe set half page size.
683 (if (not lines) (or view-half-page-size
684 (/ (view-window-size) 2))
685 (setq view-half-page-size
686 (if (zerop (setq lines (prefix-numeric-value lines)))
687 (/ (view-window-size) 2)
688 (view-page-size-default lines)))))
689
690
691;;; Commands for moving around in the buffer.
692
693(defun View-goto-percent (&optional percent)
694 "Move to end (or prefix PERCENT) of buffer in View mode.
695Display is centered at point.
696Also set the mark at the position where point was."
697 (interactive "P")
698 (push-mark)
699 (goto-char
700 (if percent
701 (+ (point-min)
702 (floor (* (- (point-max) (point-min)) 0.01
703 (max 0 (min 100 (prefix-numeric-value percent))))))
704 (point-max)))
705 (view-recenter))
706
707;; (defun View-goto-line-last (&optional line)
708;; "Move to last (or prefix LINE) line in View mode.
709;; Display is centered at LINE.
710;; Sets mark at starting position and pushes mark ring."
711;; (interactive "P")
712;; (push-mark)
713;; (if line (goto-line (prefix-numeric-value line))
714;; (goto-char (point-max))
715;; (beginning-of-line))
716;; (view-recenter))
717
718(defun View-goto-line (&optional line)
719 "Move to first (or prefix LINE) line in View mode.
720Display is centered at LINE.
721Also set the mark at the position where point was."
722 (interactive "p")
723 (push-mark)
724 (goto-char (point-min))
725 (forward-line (1- line))
726 (view-recenter))
727
728(defun View-back-to-mark (&optional _ignore)
729 "Return to last mark set in View mode, else beginning of file.
730Display that line at the center of the window.
731This command pops the mark ring, so that successive
732invocations return to earlier marks."
733 (interactive)
734 (goto-char (or (mark t) (point-min)))
735 (pop-mark)
736 (view-recenter))
737\f
738(defun view-scroll-lines (lines backward default maxdefault)
739 ;; This function does the job for all the scrolling commands.
740 ;; Scroll forward LINES lines. If BACKWARD is non-nil, scroll backwards.
741 ;; If LINES is negative scroll in the other direction.
742 ;; If LINES is 0 or nil, scroll DEFAULT lines (if DEFAULT is nil, scroll
743 ;; by one page). If MAXDEFAULT is non-nil, scroll no more than a window.
744 (if (or (null lines) (zerop (setq lines (prefix-numeric-value lines))))
745 (setq lines default))
746 (when (and lines (< lines 0))
747 (setq backward (not backward) lines (- lines)))
748 (when (and maxdefault lines (> lines (view-window-size)))
749 (setq lines nil))
750 (cond (backward (scroll-down lines))
751 ((view-really-at-end)
752 (if view-scroll-auto-exit
753 (View-quit)
754 (ding)
755 (view-end-message)))
756 (t (scroll-up lines)
757 (if (view-really-at-end) (view-end-message)))))
758
759(defun view-really-at-end ()
760 ;; Return true if buffer end visible. Maybe revert buffer and test.
761 (and (pos-visible-in-window-p (point-max))
762 (let ((buf (current-buffer))
763 (bufname (buffer-name))
764 (file (buffer-file-name)))
765 (or (not view-try-extend-at-buffer-end)
766 (null file)
767 (verify-visited-file-modtime buf)
768 (not (file-exists-p file))
769 (when (buffer-modified-p buf)
770 (setq file (file-name-nondirectory file))
771 (not (yes-or-no-p
772 (format
773 "File %s changed on disk. Discard your edits%s? "
774 file
775 (if (string= bufname file) ""
776 (concat " in " bufname))))))
777 (progn
778 (revert-buffer t t t)
779 (pos-visible-in-window-p (point-max)))))))
780
781(defun view-end-message ()
782 ;; Tell that we are at end of buffer.
783 (goto-char (point-max))
784 (if (window-parameter nil 'quit-restore)
785 (message "End of buffer. Type %s to quit viewing."
786 (substitute-command-keys
787 (if view-scroll-auto-exit "\\[View-scroll-page-forward]"
788 "\\[View-quit]")))
789 (message "End of buffer")))
790\f
791(defun View-scroll-to-buffer-end ()
792 "Scroll backward or forward so that buffer end is at last line of window."
793 (interactive)
794 (let ((p (if (pos-visible-in-window-p (point-max)) (point))))
795 (goto-char (point-max))
796 (recenter -1)
797 (and p (goto-char p))))
798
799(defun View-scroll-page-forward (&optional lines)
800 "Scroll \"page size\" or prefix LINES lines forward in View mode.
801Exit if end of text is visible and `view-scroll-auto-exit' is non-nil.
802\"page size\" is whole window full, or number of lines set by
803\\[View-scroll-page-forward-set-page-size] or
804\\[View-scroll-page-backward-set-page-size].
805If LINES is more than a window-full, only the last window-full is shown."
806 (interactive "P")
807 (view-scroll-lines lines nil (view-page-size-default view-page-size) nil))
808
809(defun View-scroll-page-backward (&optional lines)
810 "Scroll \"page size\" or prefix LINES lines backward in View mode.
811See also `View-scroll-page-forward'."
812 (interactive "P")
813 (view-scroll-lines lines t (view-page-size-default view-page-size) nil))
814
815(defun View-scroll-page-forward-set-page-size (&optional lines)
816 "Scroll forward LINES lines in View mode, setting the \"page size\".
817This is the number of lines which \\[View-scroll-page-forward] and
818\\[View-scroll-page-backward] scroll by default.
819If LINES is omitted or = 0, sets \"page size\" to window height and
820scrolls forward that much, otherwise scrolls forward LINES lines and sets
821\"page size\" to the minimum of window height and the absolute value of LINES.
822See also `View-scroll-page-forward'."
823 (interactive "P")
824 (view-scroll-lines lines nil
825 (setq view-page-size (view-page-size-default lines))
826 nil))
827
828(defun View-scroll-page-backward-set-page-size (&optional lines)
829 "Scroll backward prefix LINES lines in View mode, setting the \"page size\".
830See also `View-scroll-page-forward-set-page-size'."
831 (interactive "P")
832 (view-scroll-lines lines t
833 (setq view-page-size (view-page-size-default lines))
834 nil))
835
836(defun View-scroll-line-forward (&optional lines)
837 "Scroll forward one line (or prefix LINES lines) in View mode.
838See also `View-scroll-page-forward', but note that scrolling is limited
839to minimum of LINES and one window-full."
840 (interactive "P")
841 (view-scroll-lines lines nil 1 t))
842
843(defun View-scroll-line-backward (&optional lines)
844 "Scroll backward one line (or prefix LINES lines) in View mode.
845See also `View-scroll-line-forward'."
846 (interactive "P")
847 (view-scroll-lines lines t 1 t))
848
849(defun View-scroll-half-page-forward (&optional lines)
850 "Scroll forward a \"half page\" (or prefix LINES) lines in View mode.
851If LINES is not omitted, the \"half page size\" is set to the minimum of
852window height and the absolute value of LINES.
853LINES=0 resets \"half page size\" to half window height."
854 (interactive "P")
855 (view-scroll-lines lines nil (view-set-half-page-size-default lines) t))
856
857(defun View-scroll-half-page-backward (&optional lines)
858 "Scroll backward a \"half page\" (or prefix LINES) lines in View mode.
859See also `View-scroll-half-page-forward'."
860 (interactive "P")
861 (view-scroll-lines lines t (view-set-half-page-size-default lines) t))
862
863(defun View-revert-buffer-scroll-page-forward (&optional lines)
864 "Scroll forward, reverting buffer if needed, in View mode.
865If buffer has not been changed and the corresponding file is newer, first
866revert the buffer, then scroll.
867This command is useful if you are viewing a changing file.
868
869The prefix argument LINES says how many lines to scroll.
870If you don't specify a prefix argument, it uses the number of lines set by
871\\[View-scroll-page-forward-set-page-size] or
872\\[View-scroll-page-backward-set-page-size].
873If LINES is more than a window-full, only the last window-full is shown."
874 (interactive "P")
875 (let ((view-scroll-auto-exit nil)
876 (view-try-extend-at-buffer-end t))
877 (view-scroll-lines lines nil (view-page-size-default view-page-size) nil)))
878\f
879(defun View-search-regexp-forward (n regexp)
880 "Search forward for first (or prefix Nth) occurrence of REGEXP in View mode.
881
882Displays line found at center of window. Sets mark at starting position and
883pushes mark ring.
884
885Characters @ and ! are special at the beginning of REGEXP. They modify
886the search rather than become part of the pattern searched for.
887@ means search all the buffer i.e. start search at the beginning of buffer.
888! means search for a line that contains no match for the pattern.
889If REGEXP is empty or only consist of these control characters, then
890an earlier remembered REGEXP is used, otherwise REGEXP is remembered
891for use by later search commands.
892
893The variable `view-highlight-face' controls the face that is used
894for highlighting the match that is found."
895 (interactive "p\nsSearch forward (regexp): ")
896 (view-search n regexp))
897
898(defun View-search-regexp-backward (n regexp)
899 "Search backward for first (or prefix Nth) occurrence of REGEXP in View mode.
900
901Displays line found at center of window. Sets mark at starting position and
902pushes mark ring.
903
904Characters @ and ! are special at the beginning of REGEXP. They modify
905the search rather than become part of the pattern searched for.
906@ means search all the buffer i.e. start search at the end of buffer.
907! means search for a line that contains no match for the pattern.
908If REGEXP is empty or only consist of these control characters, then
909an earlier remembered REGEXP is used, otherwise REGEXP is remembered
910for use by later search commands.
911
912The variable `view-highlight-face' controls the face that is used
913for highlighting the match that is found."
914 (interactive "p\nsSearch backward (regexp): ")
915 (view-search (- n) regexp))
916
917(defun View-search-last-regexp-forward (n) "\
918Search forward for first (or prefix Nth) instance of last regexp in View mode.
919Displays line found at center of window. Sets mark at starting position and
920pushes mark ring.
921
922The variable `view-highlight-face' controls the face that is used
923for highlighting the match that is found."
924 (interactive "p")
925 (view-search n nil))
926
927(defun View-search-last-regexp-backward (n) "\
928Search backward for first (or prefix Nth) instance of last regexp in View mode.
929Displays line found at center of window. Sets mark at starting position and
930pushes mark ring.
931
932The variable `view-highlight-face' controls the face that is used
933for highlighting the match that is found."
934 (interactive "p")
935 (view-search (- n) nil))
936
937(defun view-search (times regexp)
938 ;; This function does the job for all the View-search- commands.
939 ;; Search for the TIMESth match for REGEXP. If TIMES is negative
940 ;; search backwards. If REGEXP is nil use `view-last-regexp'.
941 ;; Characters "!" and "@" have a special meaning at the beginning of
942 ;; REGEXP and are removed from REGEXP before the search "!" means
943 ;; search for lines with no match for REGEXP. "@" means search in
944 ;; the whole buffer, don't start searching from the present point.
945 (let (where no end ln)
946 (cond
947 ((and regexp (> (length regexp) 0)
948 (or (not (memq (string-to-char regexp) '(?! ?@)))
949 (progn
950 (if (member (substring regexp 0 2) '("!@" "@!"))
951 (setq end t no t ln 2)
952 (setq no (not (setq end (eq ?@ (string-to-char regexp))))
953 ln 1))
954 (> (length (setq regexp (substring regexp ln))) 0))))
955 (setq view-last-regexp (if no (list regexp) regexp)))
956 ((consp view-last-regexp)
957 (setq regexp (car view-last-regexp))
958 (unless (setq no (not no)) (setq view-last-regexp regexp)))
959 (view-last-regexp (setq regexp view-last-regexp)
960 (if no (setq view-last-regexp (list regexp))))
961 (t (error "No previous View-mode search")))
962 (save-excursion
963 (if end (goto-char (if (< times 0) (point-max) (point-min)))
964 (move-to-window-line (if (< times 0) 0 -1)))
965 (if (if no (view-search-no-match-lines times regexp)
966 (re-search-forward regexp nil t times))
967 (setq where (point))))
968 (if where
969 (progn
970 (push-mark)
971 (goto-char where)
972 (if view-overlay
973 (move-overlay view-overlay (match-beginning 0) (match-end 0))
974 (setq view-overlay
975 (make-overlay (match-beginning 0) (match-end 0))))
976 (overlay-put view-overlay 'face view-highlight-face)
977 (beginning-of-line)
978 (view-recenter))
979 (message "Can't find occurrence %d of %s%s"
980 times (if no "no " "") regexp)
981 (sit-for 4))))
982
983;; This is the dumb approach, looking at each line. The original
984;; version of this function looked like it might have been trying to
985;; do something clever, but not succeeding:
986;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2007-09/msg00073.html
987(defun view-search-no-match-lines (times regexp)
988 "Search for the TIMESth occurrence of a line with no match for REGEXP.
989If such a line is found, return non-nil and set the match-data to that line.
990If TIMES is negative, search backwards."
991 (let ((step (if (>= times 0) 1
992 (setq times (- times))
993 -1)))
994 ;; Note that we do not check the current line.
995 (while (and (> times 0)
996 (zerop (forward-line step)))
997 ;; (forward-line 1) returns 0 on moving within the last line.
998 (if (eobp)
999 (setq times -1)
1000 (or (re-search-forward regexp (line-end-position) t)
1001 (setq times (1- times))))))
1002 (and (zerop times)
1003 (looking-at ".*")))
1004
1005(provide 'view)
1006
1007;;; view.el ends here