Commit | Line | Data |
---|---|---|
e8af40ee | 1 | ;;; view.el --- peruse file or buffer without editing |
d501f516 | 2 | |
73b0cd50 | 3 | ;; Copyright (C) 1985, 1989, 1994-1995, 1997, 2000-2011 Free Software Foundation, Inc. |
eea8d4ef | 4 | |
e5167999 | 5 | ;; Author: K. Shane Hartman |
1036798c | 6 | ;; Maintainer: Inge Frick <inge@nada.kth.se> |
284b3043 | 7 | ;; Keywords: files |
e6211d55 RS |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
eb3fa2cf | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
e6211d55 | 12 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
e6211d55 RS |
15 | |
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
e6211d55 | 23 | |
c91c4e6d ER |
24 | ;;; Commentary: |
25 | ||
bab0c3c1 | 26 | ;; This package provides the `view' minor mode documented in the Emacs |
c91c4e6d | 27 | ;; user's manual. |
ab7782fc | 28 | ;; View mode entry and exit is done through the functions view-mode-enter |
66d3a2b5 | 29 | ;; and view-mode-exit. Use these functions to enter or exit view-mode from |
ab7782fc | 30 | ;; emacs lisp programs. |
66d3a2b5 | 31 | ;; We use both view- and View- as prefix for symbols. View- is used as |
cae55197 | 32 | ;; prefix for commands that have a key binding. view- is used for commands |
66d3a2b5 | 33 | ;; without key binding. The purpose of this is to make it easier for a |
ab7782fc RS |
34 | ;; user to use command name completion. |
35 | ||
36 | ;;; Suggested key bindings: | |
37 | ;; | |
38 | ;; (define-key ctl-x-4-map "v" 'view-file-other-window) ; ^x4v | |
39 | ;; (define-key ctl-x-5-map "v" 'view-file-other-frame) ; ^x5v | |
40 | ;; | |
41 | ;; You could also bind view-file, view-buffer, view-buffer-other-window and | |
42 | ;; view-buffer-other-frame to keys. | |
0d63ba8c | 43 | \f |
e5167999 | 44 | ;;; Code: |
e6211d55 | 45 | |
cae55197 KH |
46 | (defgroup view nil |
47 | "Peruse file or buffer without editing." | |
48 | :link '(function-link view-mode) | |
49 | :link '(custom-manual "(emacs)Misc File Ops") | |
eba5b4dd | 50 | :group 'wp) |
cae55197 | 51 | |
cae55197 | 52 | (defcustom view-highlight-face 'highlight |
9201cc28 | 53 | "The face used for highlighting the match found by View mode search." |
cae55197 KH |
54 | :type 'face |
55 | :group 'view) | |
82380f84 | 56 | |
66d3a2b5 | 57 | ;; `view-mode-auto-exit' is replaced by the following option variable which |
ab7782fc | 58 | ;; only says if scrolling past buffer end should leave view mode or not, it |
e51f616f | 59 | ;; doesn't say if leaving view mode should restore windows or not. The latter |
ab7782fc | 60 | ;; is now controlled by the presence of a value in `view-return-to-alist'. |
cae55197 | 61 | (defcustom view-scroll-auto-exit nil |
9201cc28 | 62 | "Non-nil means scrolling past the end of buffer exits View mode. |
39d9e44f | 63 | A value of nil means attempting to scroll past the end of the buffer, |
cae55197 KH |
64 | only rings the bell and gives a message on how to leave." |
65 | :type 'boolean | |
66 | :group 'view) | |
ab7782fc | 67 | |
cae55197 | 68 | (defcustom view-try-extend-at-buffer-end nil |
9201cc28 | 69 | "Non-nil means try to load more of file when reaching end of buffer. |
66d3a2b5 RS |
70 | This variable is mainly intended to be temporarily set to non-nil by |
71 | the F command in view-mode, but you can set it to t if you want the action | |
cae55197 KH |
72 | for all scroll commands in view mode." |
73 | :type 'boolean | |
74 | :group 'view) | |
ab7782fc | 75 | |
78ddc123 MR |
76 | ;;;###autoload |
77 | (defcustom view-remove-frame-by-deleting t | |
9201cc28 | 78 | "Determine how View mode removes a frame no longer needed. |
cae55197 KH |
79 | If nil, make an icon of the frame. If non-nil, delete the frame." |
80 | :type 'boolean | |
78ddc123 MR |
81 | :group 'view |
82 | ;; Changed the default of this to t for Emacs 23. Users consider | |
83 | ;; frame iconification annoying. | |
84 | :version "23.1") | |
ab7782fc | 85 | |
cae55197 | 86 | (defcustom view-exits-all-viewing-windows nil |
9201cc28 | 87 | "Non-nil means restore all windows used to view buffer. |
66d3a2b5 RS |
88 | Commands that restore windows when finished viewing a buffer, apply to all |
89 | windows that display the buffer and have restore information in | |
cae55197 | 90 | `view-return-to-alist'. |
d9855810 | 91 | If `view-exits-all-viewing-windows' is nil, only the selected window is |
cae55197 KH |
92 | considered for restoring." |
93 | :type 'boolean | |
94 | :group 'view) | |
ab7782fc | 95 | |
1c0484e2 | 96 | (defcustom view-inhibit-help-message nil |
9201cc28 | 97 | "Non-nil inhibits the help message shown upon entering View mode." |
1c0484e2 RF |
98 | :type 'boolean |
99 | :group 'view | |
100 | :version "22.1") | |
101 | ||
0093dc5a | 102 | ;;;###autoload |
cae55197 KH |
103 | (defvar view-mode nil |
104 | "Non-nil if View mode is enabled. | |
105 | Don't change this variable directly, you must change it by one of the | |
106 | functions that enable or disable view mode.") | |
0093dc5a | 107 | ;;;###autoload |
bab0c3c1 KH |
108 | (make-variable-buffer-local 'view-mode) |
109 | ||
cae55197 KH |
110 | (defcustom view-mode-hook nil |
111 | "Normal hook run when starting to view a buffer or file." | |
112 | :type 'hook | |
113 | :group 'view) | |
0d63ba8c | 114 | \f |
c88daaef RS |
115 | (defvar view-old-buffer-read-only nil) |
116 | (make-variable-buffer-local 'view-old-buffer-read-only) | |
ab7782fc | 117 | |
c88daaef RS |
118 | (defvar view-old-Helper-return-blurb) |
119 | (make-variable-buffer-local 'view-old-Helper-return-blurb) | |
120 | ||
ab7782fc RS |
121 | (defvar view-page-size nil |
122 | "Default number of lines to scroll by View page commands. | |
5bf70f47 | 123 | If nil that means use the window size.") |
ab7782fc | 124 | (make-variable-buffer-local 'view-page-size) |
c88daaef | 125 | |
ab7782fc RS |
126 | (defvar view-half-page-size nil |
127 | "Default number of lines to scroll by View half page commands. | |
5bf70f47 | 128 | If nil that means use half the window size.") |
ab7782fc | 129 | (make-variable-buffer-local 'view-half-page-size) |
c88daaef | 130 | |
ab7782fc RS |
131 | (defvar view-last-regexp nil) |
132 | (make-variable-buffer-local 'view-last-regexp) ; Global is better??? | |
133 | ||
134 | (defvar view-return-to-alist nil | |
66d3a2b5 RS |
135 | "What to do with used windows and where to go when finished viewing buffer. |
136 | This is local in each buffer being viewed. | |
d9855810 RS |
137 | It is added to by `view-mode-enter' when starting to view a buffer and |
138 | subtracted from by `view-mode-exit' when finished viewing the buffer. | |
66d3a2b5 RS |
139 | |
140 | See RETURN-TO-ALIST argument of function `view-mode-exit' for the format of | |
e51f616f | 141 | `view-return-to-alist'.") |
ab7782fc | 142 | (make-variable-buffer-local 'view-return-to-alist) |
da932079 | 143 | (put 'view-return-to-alist 'permanent-local t) |
ab7782fc RS |
144 | |
145 | (defvar view-exit-action nil | |
78ddc123 MR |
146 | "If non-nil, a function with one argument (a buffer) called when finished viewing. |
147 | Commands like \\[view-file] and \\[view-file-other-window] may | |
148 | set this to bury or kill the viewed buffer. | |
149 | Observe that the buffer viewed might not appear in any window at | |
150 | the time this function is called.") | |
c88daaef | 151 | (make-variable-buffer-local 'view-exit-action) |
c88daaef | 152 | |
1a552841 RS |
153 | (defvar view-no-disable-on-exit nil |
154 | "If non-nil, View mode \"exit\" commands don't actually disable View mode. | |
155 | Instead, these commands just switch buffers or windows. | |
156 | This is set in certain buffers by specialized features such as help commands | |
157 | that use View mode automatically.") | |
158 | ||
4fe11426 | 159 | (defvar view-overlay nil |
82380f84 RS |
160 | "Overlay used to display where a search operation found its match. |
161 | This is local in each buffer, once it is used.") | |
4fe11426 | 162 | (make-variable-buffer-local 'view-overlay) |
0d63ba8c | 163 | \f |
ab7782fc | 164 | ;; Define keymap inside defvar to make it easier to load changes. |
cae55197 | 165 | ;; Some redundant "less"-like key bindings below have been commented out. |
ab7782fc RS |
166 | (defvar view-mode-map |
167 | (let ((map (make-sparse-keymap))) | |
168 | (define-key map "C" 'View-kill-and-leave) | |
169 | (define-key map "c" 'View-leave) | |
170 | (define-key map "Q" 'View-quit-all) | |
171 | (define-key map "E" 'View-exit-and-edit) | |
4e099504 | 172 | ;; (define-key map "v" 'View-exit) |
ab7782fc RS |
173 | (define-key map "e" 'View-exit) |
174 | (define-key map "q" 'View-quit) | |
4e099504 | 175 | ;; (define-key map "N" 'View-search-last-regexp-backward) |
ab7782fc RS |
176 | (define-key map "p" 'View-search-last-regexp-backward) |
177 | (define-key map "n" 'View-search-last-regexp-forward) | |
4e099504 | 178 | ;; (define-key map "?" 'View-search-regexp-backward) ; Less does this. |
ab7782fc RS |
179 | (define-key map "\\" 'View-search-regexp-backward) |
180 | (define-key map "/" 'View-search-regexp-forward) | |
181 | (define-key map "r" 'isearch-backward) | |
182 | (define-key map "s" 'isearch-forward) | |
183 | (define-key map "m" 'point-to-register) | |
184 | (define-key map "'" 'register-to-point) | |
185 | (define-key map "x" 'exchange-point-and-mark) | |
d1ed8492 | 186 | (define-key map "@" 'View-back-to-mark) |
ab7782fc RS |
187 | (define-key map "." 'set-mark-command) |
188 | (define-key map "%" 'View-goto-percent) | |
4e099504 | 189 | ;; (define-key map "G" 'View-goto-line-last) |
ab7782fc RS |
190 | (define-key map "g" 'View-goto-line) |
191 | (define-key map "=" 'what-line) | |
192 | (define-key map "F" 'View-revert-buffer-scroll-page-forward) | |
4e099504 | 193 | ;; (define-key map "k" 'View-scroll-line-backward) |
ab7782fc | 194 | (define-key map "y" 'View-scroll-line-backward) |
4e099504 | 195 | ;; (define-key map "j" 'View-scroll-line-forward) |
ab7782fc RS |
196 | (define-key map "\n" 'View-scroll-line-forward) |
197 | (define-key map "\r" 'View-scroll-line-forward) | |
198 | (define-key map "u" 'View-scroll-half-page-backward) | |
199 | (define-key map "d" 'View-scroll-half-page-forward) | |
200 | (define-key map "z" 'View-scroll-page-forward-set-page-size) | |
201 | (define-key map "w" 'View-scroll-page-backward-set-page-size) | |
4e099504 | 202 | ;; (define-key map "b" 'View-scroll-page-backward) |
ab7782fc | 203 | (define-key map "\C-?" 'View-scroll-page-backward) |
4e099504 | 204 | ;; (define-key map "f" 'View-scroll-page-forward) |
ab7782fc RS |
205 | (define-key map " " 'View-scroll-page-forward) |
206 | (define-key map "o" 'View-scroll-to-buffer-end) | |
207 | (define-key map ">" 'end-of-buffer) | |
208 | (define-key map "<" 'beginning-of-buffer) | |
209 | (define-key map "-" 'negative-argument) | |
210 | (define-key map "9" 'digit-argument) | |
211 | (define-key map "8" 'digit-argument) | |
212 | (define-key map "7" 'digit-argument) | |
213 | (define-key map "6" 'digit-argument) | |
214 | (define-key map "5" 'digit-argument) | |
215 | (define-key map "4" 'digit-argument) | |
216 | (define-key map "3" 'digit-argument) | |
217 | (define-key map "2" 'digit-argument) | |
218 | (define-key map "1" 'digit-argument) | |
219 | (define-key map "0" 'digit-argument) | |
220 | (define-key map "H" 'describe-mode) | |
d1ed8492 | 221 | (define-key map "?" 'describe-mode) ; Maybe do as less instead? See above. |
ab7782fc RS |
222 | (define-key map "h" 'describe-mode) |
223 | map)) | |
0d63ba8c | 224 | \f |
ab7782fc | 225 | ;;; Commands that enter or exit view mode. |
e6211d55 | 226 | |
404967ca EZ |
227 | ;; This is used when view mode is exited, to make sure we don't try to |
228 | ;; kill a buffer modified by the user. A buffer in view mode can | |
229 | ;; become modified if the user types C-x C-q, edits the buffer, then | |
230 | ;; types C-x C-q again to return to view mode. | |
5e4f40ee | 231 | ;;;###autoload |
dd18f44f EZ |
232 | (defun kill-buffer-if-not-modified (buf) |
233 | "Like `kill-buffer', but does nothing if the buffer is modified." | |
ce173b30 | 234 | (let ((buf (get-buffer buf))) |
dd18f44f EZ |
235 | (and buf (not (buffer-modified-p buf)) |
236 | (kill-buffer buf)))) | |
237 | ||
7229064d | 238 | ;;;###autoload |
d1ed8492 | 239 | (defun view-file (file) |
e6211d55 | 240 | "View FILE in View mode, returning to previous buffer when done. |
78ddc123 MR |
241 | Emacs commands editing the buffer contents are not available; instead, a |
242 | special set of commands (mostly letters and punctuation) are defined for | |
243 | moving around in the buffer. | |
e6211d55 | 244 | Space scrolls forward, Delete scrolls backward. |
78ddc123 | 245 | For a list of all View commands, type H or h while viewing. |
e6211d55 | 246 | |
bd0d2c58 | 247 | This command runs the normal hook `view-mode-hook'." |
e6211d55 | 248 | (interactive "fView file: ") |
981b7b0c | 249 | (unless (file-exists-p file) (error "%s does not exist" file)) |
90bb2ff4 RS |
250 | (let ((had-a-buf (get-file-buffer file)) |
251 | (buffer (find-file-noselect file))) | |
fd5c9dfa | 252 | (view-buffer buffer (and (not had-a-buf) 'kill-buffer-if-not-modified)))) |
e6211d55 | 253 | |
b82fef5c | 254 | ;;;###autoload |
d1ed8492 | 255 | (defun view-file-other-window (file) |
ab7782fc | 256 | "View FILE in View mode in another window. |
48d93beb EZ |
257 | When done, return that window to its previous buffer, and kill the |
258 | buffer visiting FILE if unmodified and if it wasn't visited before. | |
259 | ||
ab7782fc | 260 | Emacs commands editing the buffer contents are not available; instead, |
b82fef5c JB |
261 | a special set of commands (mostly letters and punctuation) |
262 | are defined for moving around in the buffer. | |
263 | Space scrolls forward, Delete scrolls backward. | |
78ddc123 | 264 | For a list of all View commands, type H or h while viewing. |
b82fef5c | 265 | |
bd0d2c58 | 266 | This command runs the normal hook `view-mode-hook'." |
ab7782fc | 267 | (interactive "fIn other window view file: ") |
981b7b0c | 268 | (unless (file-exists-p file) (error "%s does not exist" file)) |
48d93beb EZ |
269 | (let ((had-a-buf (get-file-buffer file)) |
270 | (buf-to-view (find-file-noselect file))) | |
271 | (view-buffer-other-window buf-to-view nil | |
272 | (and (not had-a-buf) | |
dd18f44f | 273 | 'kill-buffer-if-not-modified)))) |
b82fef5c | 274 | |
7229064d | 275 | ;;;###autoload |
d1ed8492 | 276 | (defun view-file-other-frame (file) |
ab7782fc | 277 | "View FILE in View mode in another frame. |
48d93beb EZ |
278 | When done, kill the buffer visiting FILE if unmodified and if it wasn't |
279 | visited before; also, maybe delete other frame and/or return to previous | |
280 | buffer. | |
281 | ||
ab7782fc | 282 | Emacs commands editing the buffer contents are not available; instead, |
e6211d55 RS |
283 | a special set of commands (mostly letters and punctuation) |
284 | are defined for moving around in the buffer. | |
285 | Space scrolls forward, Delete scrolls backward. | |
78ddc123 | 286 | For a list of all View commands, type H or h while viewing. |
e6211d55 | 287 | |
bd0d2c58 | 288 | This command runs the normal hook `view-mode-hook'." |
ab7782fc | 289 | (interactive "fIn other frame view file: ") |
981b7b0c | 290 | (unless (file-exists-p file) (error "%s does not exist" file)) |
48d93beb EZ |
291 | (let ((had-a-buf (get-file-buffer file)) |
292 | (buf-to-view (find-file-noselect file))) | |
293 | (view-buffer-other-frame buf-to-view nil | |
294 | (and (not had-a-buf) | |
dd18f44f | 295 | 'kill-buffer-if-not-modified)))) |
ab7782fc RS |
296 | |
297 | ||
298 | ;;;###autoload | |
d1ed8492 | 299 | (defun view-buffer (buffer &optional exit-action) |
ab7782fc | 300 | "View BUFFER in View mode, returning to previous buffer when done. |
78ddc123 MR |
301 | Emacs commands editing the buffer contents are not available; instead, a |
302 | special set of commands (mostly letters and punctuation) are defined for | |
303 | moving around in the buffer. | |
ab7782fc | 304 | Space scrolls forward, Delete scrolls backward. |
78ddc123 | 305 | For a list of all View commands, type H or h while viewing. |
ab7782fc RS |
306 | |
307 | This command runs the normal hook `view-mode-hook'. | |
308 | ||
309 | Optional argument EXIT-ACTION is either nil or a function with buffer as | |
78ddc123 | 310 | argument. This function is called when finished viewing buffer. Use |
5e613c02 MR |
311 | this argument instead of explicitly setting `view-exit-action'. |
312 | ||
313 | Do not set EXIT-ACTION to `kill-buffer' when BUFFER visits a | |
314 | file: Users may suspend viewing in order to modify the buffer. | |
315 | Exiting View mode will then discard the user's edits. Setting | |
316 | EXIT-ACTION to `kill-buffer-if-not-modified' avoids this." | |
e6211d55 | 317 | (interactive "bView buffer: ") |
fd5c9dfa JL |
318 | (if (eq (with-current-buffer buffer |
319 | (get major-mode 'mode-class)) | |
320 | 'special) | |
321 | (progn | |
322 | (switch-to-buffer buffer) | |
323 | (message "Not using View mode because the major mode is special")) | |
324 | (let ((undo-window (list (window-buffer) (window-start) (window-point)))) | |
325 | (switch-to-buffer buffer) | |
326 | (view-mode-enter (cons (selected-window) (cons nil undo-window)) | |
327 | exit-action)))) | |
e6211d55 | 328 | |
b82fef5c | 329 | ;;;###autoload |
d1ed8492 | 330 | (defun view-buffer-other-window (buffer &optional not-return exit-action) |
bab0c3c1 | 331 | "View BUFFER in View mode in another window. |
78ddc123 MR |
332 | Return to previous buffer when done, unless optional NOT-RETURN is |
333 | non-nil. Emacs commands editing the buffer contents are not available; | |
334 | instead, a special set of commands (mostly letters and punctuation) are | |
335 | defined for moving around in the buffer. | |
ab7782fc | 336 | Space scrolls forward, Delete scrolls backward. |
78ddc123 | 337 | For a list of all View commands, type H or h while viewing. |
ab7782fc RS |
338 | |
339 | This command runs the normal hook `view-mode-hook'. | |
340 | ||
341 | Optional argument EXIT-ACTION is either nil or a function with buffer as | |
78ddc123 MR |
342 | argument. This function is called when finished viewing buffer. Use |
343 | this argument instead of explicitly setting `view-exit-action'." | |
ab7782fc RS |
344 | (interactive "bIn other window view buffer:\nP") |
345 | (let* ((win ; This window will be selected by | |
346 | (get-lru-window)) ; switch-to-buffer-other-window below. | |
347 | (return-to | |
348 | (and (not not-return) | |
349 | (cons (selected-window) | |
350 | (if (eq win (selected-window)) | |
351 | t ; Has to make new window. | |
d1ed8492 | 352 | (list |
ab7782fc RS |
353 | (window-buffer win) ; Other windows old buffer. |
354 | (window-start win) | |
355 | (window-point win))))))) | |
d1ed8492 | 356 | (switch-to-buffer-other-window buffer) |
ab7782fc RS |
357 | (view-mode-enter (and return-to (cons (selected-window) return-to)) |
358 | exit-action))) | |
5c05f569 | 359 | |
ab7782fc | 360 | ;;;###autoload |
d1ed8492 | 361 | (defun view-buffer-other-frame (buffer &optional not-return exit-action) |
ab7782fc | 362 | "View BUFFER in View mode in another frame. |
78ddc123 MR |
363 | Return to previous buffer when done, unless optional NOT-RETURN is |
364 | non-nil. Emacs commands editing the buffer contents are not available; | |
365 | instead, a special set of commands (mostly letters and punctuation) are | |
366 | defined for moving around in the buffer. | |
b82fef5c | 367 | Space scrolls forward, Delete scrolls backward. |
78ddc123 | 368 | For a list of all View commands, type H or h while viewing. |
b82fef5c | 369 | |
ab7782fc RS |
370 | This command runs the normal hook `view-mode-hook'. |
371 | ||
372 | Optional argument EXIT-ACTION is either nil or a function with buffer as | |
78ddc123 MR |
373 | argument. This function is called when finished viewing buffer. Use |
374 | this argument instead of explicitly setting `view-exit-action'." | |
ab7782fc RS |
375 | (interactive "bView buffer in other frame: \nP") |
376 | (let ((return-to | |
377 | (and (not not-return) (cons (selected-window) t)))) ; Old window. | |
d1ed8492 | 378 | (switch-to-buffer-other-frame buffer) |
ab7782fc RS |
379 | (view-mode-enter (and return-to (cons (selected-window) return-to)) |
380 | exit-action))) | |
0d63ba8c | 381 | \f |
7229064d | 382 | ;;;###autoload |
56eb0904 | 383 | (define-minor-mode view-mode |
ab7782fc | 384 | ;; In the following documentation string we have to use some explicit key |
e51f616f | 385 | ;; bindings instead of using the \\[] construction. The reason for this |
ab7782fc RS |
386 | ;; is that most commands have more than one key binding. |
387 | "Toggle View mode, a minor mode for viewing text but not editing it. | |
4837b516 GM |
388 | With prefix argument ARG, turn View mode on if ARG is positive, otherwise |
389 | turn it off. | |
ab7782fc RS |
390 | |
391 | Emacs commands that do not change the buffer contents are available as usual. | |
e51f616f | 392 | Kill commands insert text in kill buffers but do not delete. Other commands |
ab7782fc RS |
393 | \(among them most letters and punctuation) beep and tell that the buffer is |
394 | read-only. | |
395 | \\<view-mode-map> | |
396 | The following additional commands are provided. Most commands take prefix | |
397 | arguments. Page commands default to \"page size\" lines which is almost a whole | |
398 | 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 | |
399 | and set \"half page size\" lines which initially is half a window full. Search | |
400 | commands default to a repeat count of one. | |
401 | ||
402 | H, h, ? This message. | |
403 | Digits 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. | |
d1ed8492 GM |
408 | SPC scroll forward \"page size\" lines. |
409 | With prefix scroll forward prefix lines. | |
410 | DEL 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. | |
418 | RET, LFD scroll forward one line. With prefix scroll forward prefix line(s). | |
419 | y scroll backward one line. With prefix scroll backward prefix line(s). | |
ab7782fc RS |
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. | |
426 | x 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 | |
e51f616f | 429 | jump to line occurs. The mark is set on jump to buffer start or end. |
ab7782fc RS |
430 | \\[point-to-register] save current position in character register. |
431 | ' go to position saved in character register. | |
432 | s do forward incremental search. | |
433 | r 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. | |
e51f616f | 436 | ! means search for a line with no match for regexp. @ means start |
ab7782fc RS |
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. | |
440 | p searches backward for last regular expression. | |
4790accb | 441 | \\[View-quit] quit View mode, restoring this window and buffer to previous state. |
ab7782fc | 442 | \\[View-quit] is the normal way to leave view mode. |
e51f616f | 443 | \\[View-exit] exit View mode but stay in current buffer. Use this if you started |
ab7782fc | 444 | viewing a buffer (file) and find out you want to edit it. |
4790accb RS |
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. | |
ab7782fc RS |
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 | ||
38acf670 | 452 | The effect of \\[View-leave], \\[View-quit] and \\[View-kill-and-leave] depends on how view-mode was entered. If it was |
2f33468c EZ |
453 | entered 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), | |
456 | then \\[View-quit] will try to kill the current buffer. | |
457 | If 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], | |
38acf670 | 460 | then \\[View-leave], \\[View-quit] and \\[View-kill-and-leave] will return to that buffer. |
ab7782fc RS |
461 | |
462 | Entry to view-mode runs the normal hook `view-mode-hook'." | |
56eb0904 SM |
463 | :lighter " View" :keymap view-mode-map |
464 | (if view-mode (view-mode-enable) (view-mode-disable))) | |
0d63ba8c | 465 | \f |
e51f616f RS |
466 | (defun view-mode-enable () |
467 | "Turn on View mode." | |
8b7037d9 RS |
468 | ;; Always leave view mode before changing major mode. |
469 | ;; This is to guarantee that the buffer-read-only variable is restored. | |
8b7037d9 | 470 | (add-hook 'change-major-mode-hook 'view-mode-disable nil t) |
e51f616f | 471 | (setq view-mode t |
5bf70f47 RS |
472 | view-page-size nil |
473 | view-half-page-size nil | |
e51f616f | 474 | view-old-buffer-read-only buffer-read-only |
5704140c CY |
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))))) | |
19b7f6c9 | 484 | (force-mode-line-update) |
e51f616f RS |
485 | (run-hooks 'view-mode-hook)) |
486 | ||
487 | (defun view-mode-disable () | |
488 | "Turn off View mode." | |
8b7037d9 | 489 | (remove-hook 'change-major-mode-hook 'view-mode-disable t) |
e51f616f | 490 | (and view-overlay (delete-overlay view-overlay)) |
19b7f6c9 | 491 | (force-mode-line-update) |
b75c9917 RS |
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 | ;; Cancelling 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)) | |
5704140c CY |
500 | (setq view-mode nil) |
501 | (if (boundp 'Helper-return-blurb) | |
502 | (setq Helper-return-blurb view-old-Helper-return-blurb)) | |
bf551cf7 KH |
503 | (if buffer-read-only |
504 | (setq buffer-read-only view-old-buffer-read-only))) | |
ab7782fc RS |
505 | |
506 | ;;;###autoload | |
78ddc123 MR |
507 | (defun view-return-to-alist-update (buffer &optional item) |
508 | "Update `view-return-to-alist' of buffer BUFFER. | |
509 | Remove from `view-return-to-alist' all entries referencing dead | |
510 | windows. Optional argument ITEM non-nil means add ITEM to | |
511 | `view-return-to-alist' after purging. For a decsription of items | |
512 | that can be added see the RETURN-TO-ALIST argument of the | |
513 | function `view-mode-exit'. If `view-return-to-alist' contains an | |
514 | entry 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 | ||
539 | ;;;###autoload | |
540 | (defun view-mode-enter (&optional return-to exit-action) | |
541 | "Enter View mode and set up exit from view mode depending on optional arguments. | |
542 | RETURN-TO non-nil means add RETURN-TO as an element to the buffer | |
543 | local alist `view-return-to-alist'. Save EXIT-ACTION in buffer | |
544 | local variable `view-exit-action'. It should be either nil or a | |
545 | function that takes a buffer as argument. This function will be | |
546 | called by `view-mode-exit'. | |
547 | ||
548 | RETURN-TO is either nil, meaning do nothing when exiting view | |
549 | mode, or must have the format (WINDOW OLD-WINDOW . OLD-BUF-INFO). | |
550 | WINDOW is the window used for viewing. OLD-WINDOW is nil or the | |
551 | window to select after viewing. OLD-BUF-INFO tells what to do | |
552 | with WINDOW when exiting. It is one of: | |
553 | 1) nil Do nothing. | |
554 | 2) t Delete WINDOW or, if it is the only window and | |
555 | `view-remove-frame-by-deleting' is non-nil, its | |
556 | frame. | |
e51f616f | 557 | 3) (OLD-BUFF START POINT) Display buffer OLD-BUFF with displayed text |
78ddc123 MR |
558 | starting at START and point at POINT in WINDOW. |
559 | 4) quit-window Do `quit-window' in WINDOW. | |
560 | 5) keep-frame Like case 2) but do not delete the frame. | |
ab7782fc | 561 | |
78ddc123 | 562 | For a list of all View commands, type H or h while viewing. |
ab7782fc RS |
563 | |
564 | This function runs the normal hook `view-mode-hook'." | |
78ddc123 MR |
565 | (when return-to |
566 | (let ((entry (assq (car return-to) view-return-to-alist))) | |
567 | (if entry | |
568 | (setcdr entry (cdr return-to)) | |
569 | (setq view-return-to-alist (cons return-to view-return-to-alist))))) | |
570 | (when exit-action | |
571 | (setq view-exit-action exit-action)) | |
572 | ||
573 | (unless view-mode | |
e51f616f | 574 | (view-mode-enable) |
ab7782fc | 575 | (force-mode-line-update) |
1c0484e2 RF |
576 | (unless view-inhibit-help-message |
577 | (message "%s" | |
578 | (substitute-command-keys "\ | |
579 | View mode: type \\[help-command] for help, \\[describe-mode] for commands, \\[View-quit] to quit."))))) | |
0d63ba8c | 580 | \f |
ab7782fc | 581 | (defun view-mode-exit (&optional return-to-alist exit-action all-win) |
7f9fcc0f | 582 | "Exit View mode in various ways, depending on optional arguments. |
78ddc123 MR |
583 | RETURN-TO-ALIST, EXIT-ACTION and ALL-WIN determine what to do |
584 | after exit. EXIT-ACTION is nil or a function that is called with | |
585 | current buffer as argument. | |
586 | ||
587 | RETURN-TO-ALIST is an alist that, for some of the windows | |
588 | displaying the current buffer, maintains information on what to | |
589 | do when exiting those windows. If ALL-WIN is non-nil or the | |
590 | variable `view-exits-all-viewing-windows' is non-nil, | |
591 | view-mode-exit attempts to restore all windows showing the | |
592 | current buffer to their old state. Otherwise, only the selected | |
593 | window is affected (provided it is on RETURN-TO-ALIST). | |
594 | ||
595 | Elements of RETURN-TO-ALIST must have the format | |
596 | (WINDOW OLD-WINDOW . OLD-BUF-INFO) where | |
597 | ||
598 | WINDOW is a window displaying the current buffer and OLD-WINDOW | |
599 | is either nil or a window to select after viewing. OLD-BUF-INFO | |
600 | provides information on what to do with WINDOW and may be one of: | |
601 | 1) nil Do nothing. | |
602 | 2) t Delete WINDOW and, if it is the only window and | |
603 | `view-remove-frame-by-deleting' is non-nil, its | |
604 | frame. | |
e51f616f | 605 | 3) (OLD-BUF START POINT) Display buffer OLD-BUF with displayed text |
78ddc123 MR |
606 | starting at START and point at POINT in WINDOW. |
607 | 4) quit-window Do `quit-window' in WINDOW. | |
608 | 5) keep-frame Like case 2) but do not delete the frame. | |
609 | ||
610 | If one of the WINDOW in RETURN-TO-ALIST is the selected window | |
611 | and the corresponding OLD-WINDOW is a live window, then select | |
612 | OLD-WINDOW." | |
613 | (when view-mode ; Only do something if in view mode. | |
614 | (setq all-win | |
615 | (and return-to-alist | |
616 | (or all-win view-exits-all-viewing-windows))) | |
617 | (let* ((buffer (current-buffer)) | |
618 | window notlost | |
619 | (sel-old (assq (selected-window) return-to-alist)) | |
620 | (alist (cond | |
621 | (all-win ; Try to restore all windows. | |
622 | (append return-to-alist nil)) ; Copy. | |
623 | (sel-old ; Only selected window. | |
624 | (list sel-old)))) | |
625 | (old-window (if sel-old (car (cdr sel-old))))) | |
626 | (if all-win ; Follow chains of old-windows. | |
627 | (let ((c (length alist)) a) | |
628 | (while (and (> c 0) ; Safety if mutually refering windows. | |
629 | (or (not (window-live-p old-window)) | |
630 | (eq buffer (window-buffer old-window))) | |
631 | (setq a (assq old-window alist))) | |
632 | (setq c (1- c)) | |
633 | (setq old-window (car (cdr a)))) | |
634 | (if (or (zerop c) (not (window-live-p old-window))) | |
635 | (setq old-window (selected-window))))) | |
636 | (unless view-no-disable-on-exit | |
637 | (view-mode-disable)) | |
638 | (while alist ; Restore windows with info. | |
639 | (setq notlost nil) | |
640 | (when (and (window-live-p (setq window (car (car alist)))) | |
ab7782fc | 641 | (eq buffer (window-buffer window))) |
78ddc123 MR |
642 | (let ((frame (window-frame window)) |
643 | (old-buf-info (cdr (cdr (car alist))))) | |
644 | (if all-win (select-window window)) | |
645 | (cond | |
97ab3f47 AS |
646 | ((consp old-buf-info) ; Case 3. |
647 | (if (buffer-live-p (car old-buf-info)) | |
648 | (progn | |
649 | (set-window-buffer window (car old-buf-info)) ; old-buf | |
650 | (set-window-start window (car (cdr old-buf-info))) | |
651 | (set-window-point window (car (cdr (cdr old-buf-info))))) | |
652 | (bury-buffer))) | |
78ddc123 MR |
653 | ((eq old-buf-info 'quit-window) |
654 | (quit-window)) ; Case 4. | |
655 | (old-buf-info ; Case 2 or 5. | |
656 | (cond | |
657 | ((not (one-window-p t)) ; Not only window. | |
658 | (delete-window)) | |
659 | ((eq old-buf-info 'keep-frame) ; Case 5. | |
660 | (bury-buffer)) | |
661 | ((not (eq frame (next-frame))) ; Case 2 and only window. | |
662 | ;; Not the only frame, so can safely be removed. | |
663 | (if view-remove-frame-by-deleting | |
664 | (delete-frame frame) | |
5a0c3f56 | 665 | (setq notlost t) ; Keep the window. See below. |
78ddc123 MR |
666 | (iconify-frame frame)))))))) |
667 | ;; If a frame is removed by iconifying it, the window is not | |
668 | ;; really lost. In this case we keep the entry in | |
669 | ;; `view-return-to-alist' so that if the user deiconifies the | |
670 | ;; frame and then hits q, the frame is iconified again. | |
671 | (unless notlost | |
672 | (with-current-buffer buffer | |
d1ed8492 | 673 | (setq view-return-to-alist |
78ddc123 MR |
674 | (delete (car alist) view-return-to-alist)))) |
675 | (setq alist (cdr alist))) | |
676 | (when (window-live-p old-window) | |
677 | ;; old-window is still alive => select it. | |
678 | (select-window old-window)) | |
679 | (when exit-action | |
680 | ;; Don't do that: If the user wants to quit the *Help* buffer a | |
681 | ;; second time it won't have any effect. | |
4e099504 | 682 | ;;(setq view-exit-action nil) |
78ddc123 MR |
683 | (funcall exit-action buffer)) |
684 | (force-mode-line-update)))) | |
0d63ba8c | 685 | \f |
ab7782fc RS |
686 | (defun View-exit () |
687 | "Exit View mode but stay in current buffer." | |
e6211d55 | 688 | (interactive) |
ab7782fc | 689 | (view-mode-exit)) |
e6211d55 | 690 | |
0093dc5a | 691 | ;;;###autoload |
ab7782fc RS |
692 | (defun View-exit-and-edit () |
693 | "Exit View mode and make the current buffer editable." | |
694 | (interactive) | |
72b5cb89 KH |
695 | (let ((view-old-buffer-read-only nil) |
696 | (view-no-disable-on-exit nil)) | |
66d3a2b5 | 697 | (view-mode-exit))) |
ab7782fc RS |
698 | |
699 | (defun View-leave () | |
700 | "Quit View mode and maybe switch buffers, but don't kill this buffer." | |
701 | (interactive) | |
702 | (view-mode-exit view-return-to-alist)) | |
703 | ||
704 | (defun View-quit () | |
705 | "Quit View mode, trying to restore window and buffer to previous state. | |
e51f616f | 706 | Maybe kill this buffer. Try to restore selected window to previous state |
ab7782fc RS |
707 | and go to previous buffer or window." |
708 | (interactive) | |
709 | (view-mode-exit view-return-to-alist view-exit-action)) | |
710 | ||
711 | (defun View-quit-all () | |
e51f616f RS |
712 | "Quit View mode, trying to restore windows and buffers to previous state. |
713 | Maybe kill current buffer. Try to restore all windows viewing buffer to | |
ab7782fc RS |
714 | previous state and go to previous buffer or window." |
715 | (interactive) | |
716 | (view-mode-exit view-return-to-alist view-exit-action t)) | |
717 | ||
718 | (defun View-kill-and-leave () | |
719 | "Quit View mode, kill current buffer and return to previous buffer." | |
720 | (interactive) | |
721 | (view-mode-exit view-return-to-alist (or view-exit-action 'kill-buffer) t)) | |
0d63ba8c | 722 | \f |
ab7782fc RS |
723 | |
724 | ;;; Some help routines. | |
725 | ||
726 | (defun view-window-size () | |
9d14bd37 CY |
727 | ;; Return the height of the current window, excluding the mode line. |
728 | ;; Using `window-line-height' accounts for variable-height fonts. | |
729 | (let ((h (window-line-height -1))) | |
730 | (if h | |
731 | (1+ (nth 1 h)) | |
732 | ;; This should not happen, but if `window-line-height' returns | |
733 | ;; nil, fall back on `window-height'. | |
734 | (1- (window-height))))) | |
bd0d2c58 | 735 | |
4e099504 SM |
736 | ;; (defun view-last-command (&optional who what) |
737 | ;; (setq view-last-command-entry this-command) | |
738 | ;; (setq view-last-command who) | |
739 | ;; (setq view-last-command-argument what)) | |
e6211d55 | 740 | |
4e099504 SM |
741 | ;; (defun View-repeat-last-command () |
742 | ;; "Repeat last command issued in View mode." | |
743 | ;; (interactive) | |
744 | ;; (if (and view-last-command | |
745 | ;; (eq view-last-command-entry last-command)) | |
746 | ;; (funcall view-last-command view-last-command-argument)) | |
747 | ;; (setq this-command view-last-command-entry)) | |
e6211d55 | 748 | |
ab7782fc | 749 | (defun view-recenter () |
f2872f04 CY |
750 | ;; Recenter point in window and redisplay normally. |
751 | (recenter '(1))) | |
ab7782fc RS |
752 | |
753 | (defun view-page-size-default (lines) | |
9d14bd37 CY |
754 | ;; If LINES is nil, 0, or larger than `view-window-size', return nil. |
755 | ;; Otherwise, return LINES. | |
756 | (and lines | |
757 | (not (zerop (setq lines (prefix-numeric-value lines)))) | |
758 | (<= (abs lines) | |
759 | (abs (- (view-window-size) next-screen-context-lines))) | |
760 | (abs lines))) | |
ab7782fc RS |
761 | |
762 | (defun view-set-half-page-size-default (lines) | |
763 | ;; Get and maybe set half page size. | |
5bf70f47 RS |
764 | (if (not lines) (or view-half-page-size |
765 | (/ (view-window-size) 2)) | |
ab7782fc RS |
766 | (setq view-half-page-size |
767 | (if (zerop (setq lines (prefix-numeric-value lines))) | |
768 | (/ (view-window-size) 2) | |
769 | (view-page-size-default lines))))) | |
770 | ||
771 | ||
772 | ;;; Commands for moving around in the buffer. | |
773 | ||
774 | (defun View-goto-percent (&optional percent) | |
775 | "Move to end (or prefix PERCENT) of buffer in View mode. | |
776 | Display is centered at point. | |
e51f616f | 777 | Also set the mark at the position where point was." |
ab7782fc RS |
778 | (interactive "P") |
779 | (push-mark) | |
780 | (goto-char | |
781 | (if percent | |
782 | (+ (point-min) | |
783 | (floor (* (- (point-max) (point-min)) 0.01 | |
784 | (max 0 (min 100 (prefix-numeric-value percent)))))) | |
785 | (point-max))) | |
786 | (view-recenter)) | |
787 | ||
4e099504 SM |
788 | ;; (defun View-goto-line-last (&optional line) |
789 | ;; "Move to last (or prefix LINE) line in View mode. | |
790 | ;; Display is centered at LINE. | |
791 | ;; Sets mark at starting position and pushes mark ring." | |
792 | ;; (interactive "P") | |
793 | ;; (push-mark) | |
794 | ;; (if line (goto-line (prefix-numeric-value line)) | |
795 | ;; (goto-char (point-max)) | |
796 | ;; (beginning-of-line)) | |
797 | ;; (view-recenter)) | |
f1180544 | 798 | |
ab7782fc RS |
799 | (defun View-goto-line (&optional line) |
800 | "Move to first (or prefix LINE) line in View mode. | |
801 | Display is centered at LINE. | |
e51f616f | 802 | Also set the mark at the position where point was." |
e6211d55 RS |
803 | (interactive "p") |
804 | (push-mark) | |
e6ce8c42 GM |
805 | (goto-char (point-min)) |
806 | (forward-line (1- line)) | |
ab7782fc | 807 | (view-recenter)) |
e6211d55 | 808 | |
0d63ba8c RS |
809 | (defun View-back-to-mark (&optional ignore) |
810 | "Return to last mark set in View mode, else beginning of file. | |
811 | Display that line at the center of the window. | |
812 | This command pops the mark ring, so that successive | |
813 | invocations return to earlier marks." | |
ab7782fc | 814 | (interactive) |
0d63ba8c RS |
815 | (goto-char (or (mark t) (point-min))) |
816 | (pop-mark) | |
817 | (view-recenter)) | |
818 | \f | |
ab7782fc RS |
819 | (defun view-scroll-lines (lines backward default maxdefault) |
820 | ;; This function does the job for all the scrolling commands. | |
9d14bd37 CY |
821 | ;; Scroll forward LINES lines. If BACKWARD is non-nil, scroll backwards. |
822 | ;; If LINES is negative scroll in the other direction. | |
823 | ;; If LINES is 0 or nil, scroll DEFAULT lines (if DEFAULT is nil, scroll | |
5a0c3f56 | 824 | ;; by one page). If MAXDEFAULT is non-nil, scroll no more than a window. |
ab7782fc RS |
825 | (if (or (null lines) (zerop (setq lines (prefix-numeric-value lines)))) |
826 | (setq lines default)) | |
9d14bd37 CY |
827 | (when (and lines (< lines 0)) |
828 | (setq backward (not backward) lines (- lines))) | |
829 | (when (and maxdefault lines (> lines (view-window-size))) | |
830 | (setq lines nil)) | |
831 | (cond (backward (scroll-down lines)) | |
832 | ((view-really-at-end) | |
833 | (if view-scroll-auto-exit | |
834 | (View-quit) | |
835 | (ding) | |
836 | (view-end-message))) | |
837 | (t (scroll-up lines) | |
838 | (if (view-really-at-end) (view-end-message))))) | |
ab7782fc RS |
839 | |
840 | (defun view-really-at-end () | |
e51f616f | 841 | ;; Return true if buffer end visible. Maybe revert buffer and test. |
ab7782fc RS |
842 | (and (pos-visible-in-window-p (point-max)) |
843 | (let ((buf (current-buffer)) | |
844 | (bufname (buffer-name)) | |
845 | (file (buffer-file-name))) | |
69576401 AS |
846 | (or (not view-try-extend-at-buffer-end) |
847 | (null file) | |
848 | (verify-visited-file-modtime buf) | |
849 | (not (file-exists-p file)) | |
850 | (when (buffer-modified-p buf) | |
851 | (setq file (file-name-nondirectory file)) | |
852 | (not (yes-or-no-p | |
853 | (format | |
854 | "File %s changed on disk. Discard your edits%s? " | |
855 | file | |
856 | (if (string= bufname file) "" | |
857 | (concat " in " bufname)))))) | |
858 | (progn | |
859 | (revert-buffer t t t) | |
860 | (pos-visible-in-window-p (point-max))))))) | |
ab7782fc RS |
861 | |
862 | (defun view-end-message () | |
863 | ;; Tell that we are at end of buffer. | |
864 | (goto-char (point-max)) | |
19d73313 RS |
865 | (if view-return-to-alist |
866 | (message "End of buffer. Type %s to quit viewing." | |
867 | (substitute-command-keys | |
868 | (if view-scroll-auto-exit "\\[View-scroll-page-forward]" | |
869 | "\\[View-quit]"))) | |
870 | (message "End of buffer"))) | |
0d63ba8c RS |
871 | \f |
872 | (defun View-scroll-to-buffer-end () | |
873 | "Scroll backward or forward so that buffer end is at last line of window." | |
874 | (interactive) | |
875 | (let ((p (if (pos-visible-in-window-p (point-max)) (point)))) | |
876 | (goto-char (point-max)) | |
877 | (recenter -1) | |
878 | (and p (goto-char p)))) | |
ab7782fc RS |
879 | |
880 | (defun View-scroll-page-forward (&optional lines) | |
881 | "Scroll \"page size\" or prefix LINES lines forward in View mode. | |
e51f616f | 882 | Exit if end of text is visible and `view-scroll-auto-exit' is non-nil. |
ab7782fc RS |
883 | \"page size\" is whole window full, or number of lines set by |
884 | \\[View-scroll-page-forward-set-page-size] or | |
885 | \\[View-scroll-page-backward-set-page-size]. | |
886 | If LINES is more than a window-full, only the last window-full is shown." | |
e6211d55 | 887 | (interactive "P") |
5bf70f47 | 888 | (view-scroll-lines lines nil (view-page-size-default view-page-size) nil)) |
ab7782fc | 889 | |
d1ed8492 | 890 | (defun View-scroll-page-backward (&optional lines) |
ab7782fc | 891 | "Scroll \"page size\" or prefix LINES lines backward in View mode. |
e51f616f | 892 | See also `View-scroll-page-forward'." |
e6211d55 | 893 | (interactive "P") |
5bf70f47 | 894 | (view-scroll-lines lines t (view-page-size-default view-page-size) nil)) |
f1180544 | 895 | |
ab7782fc | 896 | (defun View-scroll-page-forward-set-page-size (&optional lines) |
e51f616f | 897 | "Scroll forward LINES lines in View mode, setting the \"page size\". |
ab7782fc | 898 | This is the number of lines which \\[View-scroll-page-forward] and |
66d3a2b5 RS |
899 | \\[View-scroll-page-backward] scroll by default. |
900 | If LINES is omitted or = 0, sets \"page size\" to window height and | |
901 | scrolls forward that much, otherwise scrolls forward LINES lines and sets | |
902 | \"page size\" to the minimum of window height and the absolute value of LINES. | |
e51f616f | 903 | See also `View-scroll-page-forward'." |
e6211d55 | 904 | (interactive "P") |
ab7782fc RS |
905 | (view-scroll-lines lines nil |
906 | (setq view-page-size (view-page-size-default lines)) | |
907 | nil)) | |
e6211d55 | 908 | |
ab7782fc RS |
909 | (defun View-scroll-page-backward-set-page-size (&optional lines) |
910 | "Scroll backward prefix LINES lines in View mode, setting the \"page size\". | |
e51f616f | 911 | See also `View-scroll-page-forward-set-page-size'." |
e6211d55 | 912 | (interactive "P") |
ab7782fc RS |
913 | (view-scroll-lines lines t |
914 | (setq view-page-size (view-page-size-default lines)) | |
915 | nil)) | |
916 | ||
917 | (defun View-scroll-line-forward (&optional lines) | |
918 | "Scroll forward one line (or prefix LINES lines) in View mode. | |
5a0c3f56 | 919 | See also `View-scroll-page-forward', but note that scrolling is limited |
ab7782fc RS |
920 | to minimum of LINES and one window-full." |
921 | (interactive "P") | |
922 | (view-scroll-lines lines nil 1 t)) | |
923 | ||
924 | (defun View-scroll-line-backward (&optional lines) | |
925 | "Scroll backward one line (or prefix LINES lines) in View mode. | |
e51f616f | 926 | See also `View-scroll-line-forward'." |
ab7782fc RS |
927 | (interactive "P") |
928 | (view-scroll-lines lines t 1 t)) | |
929 | ||
930 | (defun View-scroll-half-page-forward (&optional lines) | |
e51f616f | 931 | "Scroll forward a \"half page\" (or prefix LINES) lines in View mode. |
ab7782fc RS |
932 | If LINES is not omitted, the \"half page size\" is set to the minimum of |
933 | window height and the absolute value of LINES. | |
934 | LINES=0 resets \"half page size\" to half window height." | |
935 | (interactive "P") | |
936 | (view-scroll-lines lines nil (view-set-half-page-size-default lines) t)) | |
937 | ||
938 | (defun View-scroll-half-page-backward (&optional lines) | |
e51f616f RS |
939 | "Scroll backward a \"half page\" (or prefix LINES) lines in View mode. |
940 | See also `View-scroll-half-page-forward'." | |
ab7782fc RS |
941 | (interactive "P") |
942 | (view-scroll-lines lines t (view-set-half-page-size-default lines) t)) | |
943 | ||
e51f616f RS |
944 | (defun View-revert-buffer-scroll-page-forward (&optional lines) |
945 | "Scroll forward, reverting buffer if needed, in View mode. | |
ab7782fc RS |
946 | If buffer has not been changed and the corresponding file is newer, first |
947 | revert the buffer, then scroll. | |
948 | This command is useful if you are viewing a changing file. | |
e51f616f RS |
949 | |
950 | The prefix argument LINES says how many lines to scroll. | |
951 | If you don't specify a prefix argument, it uses the number of lines set by | |
ab7782fc RS |
952 | \\[View-scroll-page-forward-set-page-size] or |
953 | \\[View-scroll-page-backward-set-page-size]. | |
954 | If LINES is more than a window-full, only the last window-full is shown." | |
955 | (interactive "P") | |
5d9b78b7 | 956 | (let ((view-scroll-auto-exit nil) |
ab7782fc | 957 | (view-try-extend-at-buffer-end t)) |
a312f214 | 958 | (view-scroll-lines lines nil (view-page-size-default view-page-size) nil))) |
0d63ba8c | 959 | \f |
5c05f569 | 960 | (defun View-search-regexp-forward (n regexp) |
ab7782fc | 961 | "Search forward for first (or prefix Nth) occurrence of REGEXP in View mode. |
66d3a2b5 RS |
962 | |
963 | Displays line found at center of window. Sets mark at starting position and | |
964 | pushes mark ring. | |
965 | ||
966 | Characters @ and ! are special at the beginning of REGEXP. They modify | |
967 | the search rather than become part of the pattern searched for. | |
968 | @ means search all the buffer i.e. start search at the beginning of buffer. | |
969 | ! means search for a line that contains no match for the pattern. | |
970 | If REGEXP is empty or only consist of these control characters, then | |
971 | an earlier remembered REGEXP is used, otherwise REGEXP is remembered | |
972 | for use by later search commands. | |
5534818b RS |
973 | |
974 | The variable `view-highlight-face' controls the face that is used | |
975 | for highlighting the match that is found." | |
d1ed8492 | 976 | (interactive "p\nsSearch forward (regexp): ") |
ab7782fc | 977 | (view-search n regexp)) |
e6211d55 | 978 | |
5c05f569 | 979 | (defun View-search-regexp-backward (n regexp) |
ab7782fc | 980 | "Search backward for first (or prefix Nth) occurrence of REGEXP in View mode. |
66d3a2b5 RS |
981 | |
982 | Displays line found at center of window. Sets mark at starting position and | |
983 | pushes mark ring. | |
984 | ||
985 | Characters @ and ! are special at the beginning of REGEXP. They modify | |
986 | the search rather than become part of the pattern searched for. | |
987 | @ means search all the buffer i.e. start search at the end of buffer. | |
988 | ! means search for a line that contains no match for the pattern. | |
989 | If REGEXP is empty or only consist of these control characters, then | |
990 | an earlier remembered REGEXP is used, otherwise REGEXP is remembered | |
991 | for use by later search commands. | |
5534818b RS |
992 | |
993 | The variable `view-highlight-face' controls the face that is used | |
994 | for highlighting the match that is found." | |
e6211d55 | 995 | (interactive "p\nsSearch backward (regexp): ") |
ab7782fc | 996 | (view-search (- n) regexp)) |
e6211d55 | 997 | |
ab7782fc RS |
998 | (defun View-search-last-regexp-forward (n) "\ |
999 | Search forward for first (or prefix Nth) instance of last regexp in View mode. | |
1000 | Displays line found at center of window. Sets mark at starting position and | |
1001 | pushes mark ring. | |
5534818b RS |
1002 | |
1003 | The variable `view-highlight-face' controls the face that is used | |
1004 | for highlighting the match that is found." | |
e6211d55 | 1005 | (interactive "p") |
ab7782fc | 1006 | (view-search n nil)) |
e6211d55 | 1007 | |
ab7782fc RS |
1008 | (defun View-search-last-regexp-backward (n) "\ |
1009 | Search backward for first (or prefix Nth) instance of last regexp in View mode. | |
e6211d55 | 1010 | Displays line found at center of window. Sets mark at starting position and |
5534818b RS |
1011 | pushes mark ring. |
1012 | ||
1013 | The variable `view-highlight-face' controls the face that is used | |
1014 | for highlighting the match that is found." | |
e6211d55 | 1015 | (interactive "p") |
ab7782fc | 1016 | (view-search (- n) nil)) |
e6211d55 | 1017 | |
e6211d55 | 1018 | (defun view-search (times regexp) |
66d3a2b5 | 1019 | ;; This function does the job for all the View-search- commands. |
5a0c3f56 JB |
1020 | ;; Search for the TIMESt match for REGEXP. If TIMES is negative |
1021 | ;; search backwards. If REGEXP is nil use `view-last-regexp'. | |
53785457 | 1022 | ;; Characters "!" and "@" have a special meaning at the beginning of |
cae55197 KH |
1023 | ;; REGEXP and are removed from REGEXP before the search "!" means |
1024 | ;; search for lines with no match for REGEXP. "@" means search in | |
1025 | ;; the whole buffer, don't start searching from the present point. | |
ab7782fc RS |
1026 | (let (where no end ln) |
1027 | (cond | |
1028 | ((and regexp (> (length regexp) 0) | |
1029 | (or (not (memq (string-to-char regexp) '(?! ?@))) | |
1030 | (progn | |
1031 | (if (member (substring regexp 0 2) '("!@" "@!")) | |
1032 | (setq end t no t ln 2) | |
1033 | (setq no (not (setq end (eq ?@ (string-to-char regexp)))) | |
1034 | ln 1)) | |
1035 | (> (length (setq regexp (substring regexp ln))) 0)))) | |
1036 | (setq view-last-regexp (if no (list regexp) regexp))) | |
1037 | ((consp view-last-regexp) | |
1038 | (setq regexp (car view-last-regexp)) | |
cae55197 | 1039 | (unless (setq no (not no)) (setq view-last-regexp regexp))) |
ab7782fc RS |
1040 | (view-last-regexp (setq regexp view-last-regexp) |
1041 | (if no (setq view-last-regexp (list regexp)))) | |
1042 | (t (error "No previous View-mode search"))) | |
e6211d55 | 1043 | (save-excursion |
ab7782fc RS |
1044 | (if end (goto-char (if (< times 0) (point-max) (point-min))) |
1045 | (move-to-window-line (if (< times 0) 0 -1))) | |
1046 | (if (if no (view-search-no-match-lines times regexp) | |
1047 | (re-search-forward regexp nil t times)) | |
e6211d55 RS |
1048 | (setq where (point)))) |
1049 | (if where | |
1050 | (progn | |
1051 | (push-mark) | |
1052 | (goto-char where) | |
4fe11426 RS |
1053 | (if view-overlay |
1054 | (move-overlay view-overlay (match-beginning 0) (match-end 0)) | |
1055 | (setq view-overlay | |
1056 | (make-overlay (match-beginning 0) (match-end 0)))) | |
82380f84 | 1057 | (overlay-put view-overlay 'face view-highlight-face) |
e6211d55 | 1058 | (beginning-of-line) |
ab7782fc RS |
1059 | (view-recenter)) |
1060 | (message "Can't find occurrence %d of %s%s" | |
1061 | times (if no "no " "") regexp) | |
e6211d55 RS |
1062 | (sit-for 4)))) |
1063 | ||
5aca5ac3 GM |
1064 | ;; This is the dumb approach, looking at each line. The original |
1065 | ;; version of this function looked like it might have been trying to | |
1066 | ;; do something clever, but not succeeding: | |
1067 | ;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2007-09/msg00073.html | |
ab7782fc | 1068 | (defun view-search-no-match-lines (times regexp) |
5aca5ac3 GM |
1069 | "Search for the TIMESth occurrence of a line with no match for REGEXP. |
1070 | If such a line is found, return non-nil and set the match-data to that line. | |
1071 | If TIMES is negative, search backwards." | |
138c66b0 GM |
1072 | (let ((step (if (>= times 0) 1 |
1073 | (setq times (- times)) | |
1074 | -1))) | |
5aca5ac3 GM |
1075 | ;; Note that we do not check the current line. |
1076 | (while (and (> times 0) | |
1077 | (zerop (forward-line step))) | |
138c66b0 GM |
1078 | ;; (forward-line 1) returns 0 on moving within the last line. |
1079 | (if (eobp) | |
1080 | (setq times -1) | |
1081 | (or (re-search-forward regexp (line-end-position) t) | |
1082 | (setq times (1- times)))))) | |
1083 | (and (zerop times) | |
1084 | (looking-at ".*"))) | |
ab7782fc | 1085 | |
49116ac0 JB |
1086 | (provide 'view) |
1087 | ||
d501f516 | 1088 | ;;; view.el ends here |