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