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