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