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