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