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