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