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