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