view.el 40.2 KB
Newer Older
1
;;; view.el --- peruse file or buffer without editing
Eric S. Raymond's avatar
Eric S. Raymond committed
2

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 1985, 1989, 1994-1995, 1997, 2000-2019 Free Software
4
;; Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
5

Eric S. Raymond's avatar
Eric S. Raymond committed
6
;; Author: K. Shane Hartman
7
;; Maintainer: emacs-devel@gnu.org
Gerd Moellmann's avatar
Gerd Moellmann committed
8
;; Keywords: files
Richard M. Stallman's avatar
Richard M. Stallman committed
9 10 11

;; This file is part of GNU Emacs.

12
;; GNU Emacs is free software: you can redistribute it and/or modify
Richard M. Stallman's avatar
Richard M. Stallman committed
13
;; it under the terms of the GNU General Public License as published by
14 15
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Richard M. Stallman's avatar
Richard M. Stallman committed
16 17 18 19 20 21 22

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
23
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
Richard M. Stallman's avatar
Richard M. Stallman committed
24

25 26
;;; Commentary:

Karl Heuer's avatar
Karl Heuer committed
27
;; This package provides the `view' minor mode documented in the Emacs
28
;; user's manual.
29
;; View mode entry and exit is done through the functions view-mode-enter
30
;; and view-mode-exit.  Use these functions to enter or exit view-mode from
31
;; emacs lisp programs.
32
;; We use both view- and View- as prefix for symbols.  View- is used as
33
;; prefix for commands that have a key binding.  view- is used for commands
34
;; without key binding.  The purpose of this is to make it easier for a
35 36 37 38 39 40 41 42 43
;; user to use command name completion.

;;; Suggested key bindings:
;;
;; (define-key ctl-x-4-map "v" 'view-file-other-window)  ; ^x4v
;; (define-key ctl-x-5-map "v" 'view-file-other-frame)  ; ^x5v
;;
;; You could also bind view-file, view-buffer, view-buffer-other-window and
;; view-buffer-other-frame to keys.
44

Eric S. Raymond's avatar
Eric S. Raymond committed
45
;;; Code:
Richard M. Stallman's avatar
Richard M. Stallman committed
46

47 48 49 50
(defgroup view nil
  "Peruse file or buffer without editing."
  :link '(function-link view-mode)
  :link '(custom-manual "(emacs)Misc File Ops")
51
  :group 'text)
52 53

(defcustom view-highlight-face 'highlight
Lute Kamstra's avatar
Lute Kamstra committed
54
   "The face used for highlighting the match found by View mode search."
55 56
   :type 'face
   :group 'view)
57

58
(defcustom view-scroll-auto-exit nil
Lute Kamstra's avatar
Lute Kamstra committed
59
  "Non-nil means scrolling past the end of buffer exits View mode.
60
A value of nil means attempting to scroll past the end of the buffer,
61 62 63
only rings the bell and gives a message on how to leave."
  :type 'boolean
  :group 'view)
64

65
(defcustom view-try-extend-at-buffer-end nil
Lute Kamstra's avatar
Lute Kamstra committed
66
 "Non-nil means try to load more of file when reaching end of buffer.
67 68
This variable is mainly intended to be temporarily set to non-nil by
the F command in view-mode, but you can set it to t if you want the action
69 70 71
for all scroll commands in view mode."
  :type 'boolean
  :group 'view)
72

73 74
;;;###autoload
(defcustom view-remove-frame-by-deleting t
Lute Kamstra's avatar
Lute Kamstra committed
75
  "Determine how View mode removes a frame no longer needed.
76 77
If nil, make an icon of the frame.  If non-nil, delete the frame."
  :type 'boolean
78 79
  :group 'view
  :version "23.1")
80

81
(defcustom view-exits-all-viewing-windows nil
Lute Kamstra's avatar
Lute Kamstra committed
82
  "Non-nil means restore all windows used to view buffer.
83 84 85 86
Commands that restore windows when finished viewing a buffer,
apply to all windows that display the buffer and have restore
information.  If `view-exits-all-viewing-windows' is nil, only
the selected window is considered for restoring."
87 88
  :type 'boolean
  :group 'view)
89

90
(defcustom view-inhibit-help-message nil
Lute Kamstra's avatar
Lute Kamstra committed
91
  "Non-nil inhibits the help message shown upon entering View mode."
92 93 94 95
  :type 'boolean
  :group 'view
  :version "22.1")

96
;;;###autoload
97 98 99 100
(defvar view-mode nil
  "Non-nil if View mode is enabled.
Don't change this variable directly, you must change it by one of the
functions that enable or disable view mode.")
101
;;;###autoload
Karl Heuer's avatar
Karl Heuer committed
102 103
(make-variable-buffer-local 'view-mode)

104 105 106 107
(defcustom view-mode-hook nil
  "Normal hook run when starting to view a buffer or file."
  :type 'hook
  :group 'view)
108

109 110
(defvar view-old-buffer-read-only nil)
(make-variable-buffer-local 'view-old-buffer-read-only)
111

112 113 114
(defvar view-old-Helper-return-blurb)
(make-variable-buffer-local 'view-old-Helper-return-blurb)

115 116
(defvar view-page-size nil
  "Default number of lines to scroll by View page commands.
117
If nil that means use the window size.")
118
(make-variable-buffer-local 'view-page-size)
119

120 121
(defvar view-half-page-size nil
  "Default number of lines to scroll by View half page commands.
122
If nil that means use half the window size.")
123
(make-variable-buffer-local 'view-half-page-size)
124

125 126 127 128
(defvar view-last-regexp nil)
(make-variable-buffer-local 'view-last-regexp) ; Global is better???

(defvar view-return-to-alist nil
129 130
  "What to do with used windows and where to go when finished viewing buffer.
This is local in each buffer being viewed.
131 132
It is added to by `view-mode-enter' when starting to view a buffer and
subtracted from by `view-mode-exit' when finished viewing the buffer.
133 134

See RETURN-TO-ALIST argument of function `view-mode-exit' for the format of
Richard M. Stallman's avatar
Richard M. Stallman committed
135
`view-return-to-alist'.")
136 137
(make-obsolete-variable
 'view-return-to-alist "this variable is no more used." "24.1")
138
(make-variable-buffer-local 'view-return-to-alist)
139
(put 'view-return-to-alist 'permanent-local t)
140 141

(defvar view-exit-action nil
142 143 144 145 146
  "If non-nil, a function with one argument (a buffer) called when finished viewing.
Commands like \\[view-file] and \\[view-file-other-window] may
set this to bury or kill the viewed buffer.
Observe that the buffer viewed might not appear in any window at
the time this function is called.")
147 148
(make-variable-buffer-local 'view-exit-action)

149 150 151 152 153 154
(defvar view-no-disable-on-exit nil
  "If non-nil, View mode \"exit\" commands don't actually disable View mode.
Instead, these commands just switch buffers or windows.
This is set in certain buffers by specialized features such as help commands
that use View mode automatically.")

155
(defvar view-overlay nil
156 157
  "Overlay used to display where a search operation found its match.
This is local in each buffer, once it is used.")
158
(make-variable-buffer-local 'view-overlay)
159

160
;; Define keymap inside defvar to make it easier to load changes.
161
;; Some redundant "less"-like key bindings below have been commented out.
162 163 164 165 166 167
(defvar view-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "C" 'View-kill-and-leave)
    (define-key map "c" 'View-leave)
    (define-key map "Q" 'View-quit-all)
    (define-key map "E" 'View-exit-and-edit)
Stefan Monnier's avatar
Stefan Monnier committed
168
    ;; (define-key map "v" 'View-exit)
169 170
    (define-key map "e" 'View-exit)
    (define-key map "q" 'View-quit)
Stefan Monnier's avatar
Stefan Monnier committed
171
    ;; (define-key map "N" 'View-search-last-regexp-backward)
172 173
    (define-key map "p" 'View-search-last-regexp-backward)
    (define-key map "n" 'View-search-last-regexp-forward)
Stefan Monnier's avatar
Stefan Monnier committed
174
    ;; (define-key map "?" 'View-search-regexp-backward) ; Less does this.
175 176 177 178 179 180 181
    (define-key map "\\" 'View-search-regexp-backward)
    (define-key map "/" 'View-search-regexp-forward)
    (define-key map "r" 'isearch-backward)
    (define-key map "s" 'isearch-forward)
    (define-key map "m" 'point-to-register)
    (define-key map "'" 'register-to-point)
    (define-key map "x" 'exchange-point-and-mark)
182
    (define-key map "@" 'View-back-to-mark)
183 184
    (define-key map "." 'set-mark-command)
    (define-key map "%" 'View-goto-percent)
Stefan Monnier's avatar
Stefan Monnier committed
185
    ;; (define-key map "G" 'View-goto-line-last)
186 187 188
    (define-key map "g" 'View-goto-line)
    (define-key map "=" 'what-line)
    (define-key map "F" 'View-revert-buffer-scroll-page-forward)
Stefan Monnier's avatar
Stefan Monnier committed
189
    ;; (define-key map "k" 'View-scroll-line-backward)
190
    (define-key map "y" 'View-scroll-line-backward)
Stefan Monnier's avatar
Stefan Monnier committed
191
    ;; (define-key map "j" 'View-scroll-line-forward)
192 193 194 195 196 197
    (define-key map "\n" 'View-scroll-line-forward)
    (define-key map "\r" 'View-scroll-line-forward)
    (define-key map "u" 'View-scroll-half-page-backward)
    (define-key map "d" 'View-scroll-half-page-forward)
    (define-key map "z" 'View-scroll-page-forward-set-page-size)
    (define-key map "w" 'View-scroll-page-backward-set-page-size)
Stefan Monnier's avatar
Stefan Monnier committed
198
    ;; (define-key map "b" 'View-scroll-page-backward)
199
    (define-key map "\C-?" 'View-scroll-page-backward)
Stefan Monnier's avatar
Stefan Monnier committed
200
    ;; (define-key map "f" 'View-scroll-page-forward)
201
    (define-key map " " 'View-scroll-page-forward)
202
    (define-key map [?\S-\ ]  'View-scroll-page-backward)
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    (define-key map "o" 'View-scroll-to-buffer-end)
    (define-key map ">" 'end-of-buffer)
    (define-key map "<" 'beginning-of-buffer)
    (define-key map "-" 'negative-argument)
    (define-key map "9" 'digit-argument)
    (define-key map "8" 'digit-argument)
    (define-key map "7" 'digit-argument)
    (define-key map "6" 'digit-argument)
    (define-key map "5" 'digit-argument)
    (define-key map "4" 'digit-argument)
    (define-key map "3" 'digit-argument)
    (define-key map "2" 'digit-argument)
    (define-key map "1" 'digit-argument)
    (define-key map "0" 'digit-argument)
    (define-key map "H" 'describe-mode)
218
    (define-key map "?" 'describe-mode)	; Maybe do as less instead? See above.
219 220
    (define-key map "h" 'describe-mode)
    map))
221

222
;;; Commands that enter or exit view mode.
Richard M. Stallman's avatar
Richard M. Stallman committed
223

224 225 226 227
;; This is used when view mode is exited, to make sure we don't try to
;; kill a buffer modified by the user.  A buffer in view mode can
;; become modified if the user types C-x C-q, edits the buffer, then
;; types C-x C-q again to return to view mode.
228
;;;###autoload
229 230
(defun kill-buffer-if-not-modified (buf)
  "Like `kill-buffer', but does nothing if the buffer is modified."
231
  (let ((buf (get-buffer buf)))
232 233 234
    (and buf (not (buffer-modified-p buf))
	 (kill-buffer buf))))

Roland McGrath's avatar
Roland McGrath committed
235
;;;###autoload
236
(defun view-file (file)
Richard M. Stallman's avatar
Richard M. Stallman committed
237
  "View FILE in View mode, returning to previous buffer when done.
238 239 240
Emacs commands editing the buffer contents are not available; instead, a
special set of commands (mostly letters and punctuation) are defined for
moving around in the buffer.
Richard M. Stallman's avatar
Richard M. Stallman committed
241
Space scrolls forward, Delete scrolls backward.
242
For a list of all View commands, type H or h while viewing.
Richard M. Stallman's avatar
Richard M. Stallman committed
243

244
This command runs the normal hook `view-mode-hook'."
Richard M. Stallman's avatar
Richard M. Stallman committed
245
  (interactive "fView file: ")
246
  (unless (file-exists-p file) (error "%s does not exist" file))
247 248
  (let ((had-a-buf (get-file-buffer file))
	(buffer (find-file-noselect file)))
249
    (view-buffer buffer (and (not had-a-buf) 'kill-buffer-if-not-modified))))
Richard M. Stallman's avatar
Richard M. Stallman committed
250

Jim Blandy's avatar
Jim Blandy committed
251
;;;###autoload
252
(defun view-file-other-window (file)
253
  "View FILE in View mode in another window.
254 255 256
When done, return that window to its previous buffer, and kill the
buffer visiting FILE if unmodified and if it wasn't visited before.

257
Emacs commands editing the buffer contents are not available; instead,
Jim Blandy's avatar
Jim Blandy committed
258 259 260
a special set of commands (mostly letters and punctuation)
are defined for moving around in the buffer.
Space scrolls forward, Delete scrolls backward.
261
For a list of all View commands, type H or h while viewing.
Jim Blandy's avatar
Jim Blandy committed
262

263
This command runs the normal hook `view-mode-hook'."
264
  (interactive "fIn other window view file: ")
265
  (unless (file-exists-p file) (error "%s does not exist" file))
266 267 268 269
  (let ((had-a-buf (get-file-buffer file))
	(buf-to-view (find-file-noselect file)))
    (view-buffer-other-window buf-to-view nil
			      (and (not had-a-buf)
270
				   'kill-buffer-if-not-modified))))
Jim Blandy's avatar
Jim Blandy committed
271

Roland McGrath's avatar
Roland McGrath committed
272
;;;###autoload
273
(defun view-file-other-frame (file)
274
  "View FILE in View mode in another frame.
275 276 277 278
When done, kill the buffer visiting FILE if unmodified and if it wasn't
visited before; also, maybe delete other frame and/or return to previous
buffer.

279
Emacs commands editing the buffer contents are not available; instead,
Richard M. Stallman's avatar
Richard M. Stallman committed
280 281 282
a special set of commands (mostly letters and punctuation)
are defined for moving around in the buffer.
Space scrolls forward, Delete scrolls backward.
283
For a list of all View commands, type H or h while viewing.
Richard M. Stallman's avatar
Richard M. Stallman committed
284

285
This command runs the normal hook `view-mode-hook'."
286
  (interactive "fIn other frame view file: ")
287
  (unless (file-exists-p file) (error "%s does not exist" file))
288 289 290 291
  (let ((had-a-buf (get-file-buffer file))
	(buf-to-view (find-file-noselect file)))
    (view-buffer-other-frame buf-to-view nil
			     (and (not had-a-buf)
292
				  'kill-buffer-if-not-modified))))
293 294 295


;;;###autoload
296
(defun view-buffer (buffer &optional exit-action)
297
  "View BUFFER in View mode, returning to previous buffer when done.
298 299 300
Emacs commands editing the buffer contents are not available; instead, a
special set of commands (mostly letters and punctuation) are defined for
moving around in the buffer.
301
Space scrolls forward, Delete scrolls backward.
302
For a list of all View commands, type H or h while viewing.
303 304 305 306

This command runs the normal hook `view-mode-hook'.

Optional argument EXIT-ACTION is either nil or a function with buffer as
307
argument.  This function is called when finished viewing buffer.  Use
308 309 310 311 312
this argument instead of explicitly setting `view-exit-action'.

Do not set EXIT-ACTION to `kill-buffer' when BUFFER visits a
file: Users may suspend viewing in order to modify the buffer.
Exiting View mode will then discard the user's edits.  Setting
313 314 315 316 317
EXIT-ACTION to `kill-buffer-if-not-modified' avoids this.

This function does not enable View mode if the buffer's major-mode
has a `special' mode-class, because such modes usually have their
own View-like bindings."
Richard M. Stallman's avatar
Richard M. Stallman committed
318
  (interactive "bView buffer: ")
319 320 321
  (switch-to-buffer buffer)
  (if (eq (get major-mode 'mode-class) 'special)
      (message "Not using View mode because the major mode is special")
322
    (view-mode-enter nil exit-action)))
Richard M. Stallman's avatar
Richard M. Stallman committed
323

Jim Blandy's avatar
Jim Blandy committed
324
;;;###autoload
325
(defun view-buffer-other-window (buffer &optional _not-return exit-action)
Karl Heuer's avatar
Karl Heuer committed
326
  "View BUFFER in View mode in another window.
327 328 329
Emacs commands editing the buffer contents are not available;
instead, a special set of commands (mostly letters and
punctuation) are defined for moving around in the buffer.
330
Space scrolls forward, Delete scrolls backward.
331
For a list of all View commands, type H or h while viewing.
332 333 334

This command runs the normal hook `view-mode-hook'.

335 336
Optional argument NOT-RETURN is ignored.

337
Optional argument EXIT-ACTION is either nil or a function with buffer as
338
argument.  This function is called when finished viewing buffer.  Use
339 340 341 342 343
this argument instead of explicitly setting `view-exit-action'.

This function does not enable View mode if the buffer's major-mode
has a `special' mode-class, because such modes usually have their
own View-like bindings."
344
  (interactive "bIn other window view buffer:\nP")
345 346
  (let ((pop-up-windows t))
    (pop-to-buffer buffer t))
347 348 349
  (if (eq (get major-mode 'mode-class) 'special)
      (message "Not using View mode because the major mode is special")
    (view-mode-enter nil exit-action)))
350

351
;;;###autoload
352
(defun view-buffer-other-frame (buffer &optional _not-return exit-action)
353
  "View BUFFER in View mode in another frame.
354 355 356
Emacs commands editing the buffer contents are not available;
instead, a special set of commands (mostly letters and
punctuation) are defined for moving around in the buffer.
Jim Blandy's avatar
Jim Blandy committed
357
Space scrolls forward, Delete scrolls backward.
358
For a list of all View commands, type H or h while viewing.
Jim Blandy's avatar
Jim Blandy committed
359

360 361
This command runs the normal hook `view-mode-hook'.

362 363
Optional argument NOT-RETURN is ignored.

364
Optional argument EXIT-ACTION is either nil or a function with buffer as
365
argument.  This function is called when finished viewing buffer.  Use
366 367 368 369 370
this argument instead of explicitly setting `view-exit-action'.

This function does not enable View mode if the buffer's major-mode
has a `special' mode-class, because such modes usually have their
own View-like bindings."
371
  (interactive "bView buffer in other frame: \nP")
372 373
  (let ((pop-up-frames t))
    (pop-to-buffer buffer t))
374 375 376
  (if (eq (get major-mode 'mode-class) 'special)
      (message "Not using View mode because the major mode is special")
    (view-mode-enter nil exit-action)))
377

Roland McGrath's avatar
Roland McGrath committed
378
;;;###autoload
379
(define-minor-mode view-mode
380
  ;; In the following documentation string we have to use some explicit key
Richard M. Stallman's avatar
Richard M. Stallman committed
381
  ;; bindings instead of using the \\[] construction.  The reason for this
382 383
  ;; is that most commands have more than one key binding.
  "Toggle View mode, a minor mode for viewing text but not editing it.
384 385 386 387 388

When View mode is enabled, commands that do not change the buffer
contents are available as usual.  Kill commands insert text in
kill buffers but do not delete.  Most other commands beep and
tell the user that the buffer is read-only.
389 390

\\<view-mode-map>
391 392 393 394 395 396 397 398

The following additional commands are provided.  Most commands
take prefix arguments.  Page commands default to \"page size\"
lines which is almost a whole window, 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 and set \"half page size\" lines
which initially is half a window full.  Search commands default
to a repeat count of one.
399 400 401 402 403 404 405

H, h, ?	 This message.
Digits	provide prefix arguments.
\\[negative-argument]	negative prefix argument.
\\[beginning-of-buffer]	move to the beginning of buffer.
>	move to the end of buffer.
\\[View-scroll-to-buffer-end]	scroll so that buffer end is at last line of window.
406 407
SPC	scroll forward \"page size\" lines.
	  With prefix scroll forward prefix lines.
408 409
DEL, S-SPC  scroll backward \"page size\" lines.
	      With prefix scroll backward prefix lines.
410 411 412 413 414 415 416 417
\\[View-scroll-page-forward-set-page-size]	like  \\[View-scroll-page-forward]  but with prefix sets \"page size\" to prefix.
\\[View-scroll-page-backward-set-page-size]	like  \\[View-scroll-page-backward]  but with prefix sets \"page size\" to prefix.
\\[View-scroll-half-page-forward]	scroll forward \"half page size\" lines.  With prefix, sets
	  \"half page size\" to prefix lines and scrolls forward that much.
\\[View-scroll-half-page-backward]	scroll backward \"half page size\" lines.  With prefix, sets
	  \"half page size\" to prefix lines and scrolls backward that much.
RET, LFD  scroll forward one line.  With prefix scroll forward prefix line(s).
y	scroll backward one line.  With prefix scroll backward prefix line(s).
418 419 420 421 422 423 424 425 426
\\[View-revert-buffer-scroll-page-forward]	revert-buffer if necessary and scroll forward.
	  Use this to view a changing file.
\\[what-line]	prints the current line number.
\\[View-goto-percent]	goes prefix argument (default 100) percent into buffer.
\\[View-goto-line]	goes to line given by prefix argument (default first line).
.	set the mark.
x	exchanges point and mark.
\\[View-back-to-mark]	return to mark and pops mark ring.
	  Mark ring is pushed at start of every successful search and when
Richard M. Stallman's avatar
Richard M. Stallman committed
427
	  jump to line occurs.  The mark is set on jump to buffer start or end.
428
\\[point-to-register]	save current position in character register.
Stephen Berman's avatar
Stephen Berman committed
429
\\='	go to position saved in character register.
430 431 432 433
s	do forward incremental search.
r	do reverse incremental search.
\\[View-search-regexp-forward]	searches forward for regular expression, starting after current page.
	  ! and @ have a special meaning at the beginning of the regexp.
Richard M. Stallman's avatar
Richard M. Stallman committed
434
	  ! means search for a line with no match for regexp.  @ means start
435 436 437 438
	  search at beginning (end for backward search) of buffer.
\\	searches backward for regular expression, starting before current page.
\\[View-search-last-regexp-forward]	searches forward for last regular expression.
p	searches backward for last regular expression.
Richard M. Stallman's avatar
Richard M. Stallman committed
439
\\[View-quit]	quit View mode, restoring this window and buffer to previous state.
440
	  \\[View-quit] is the normal way to leave view mode.
Richard M. Stallman's avatar
Richard M. Stallman committed
441
\\[View-exit]	exit View mode but stay in current buffer.  Use this if you started
442
	  viewing a buffer (file) and find out you want to edit it.
Richard M. Stallman's avatar
Richard M. Stallman committed
443 444 445 446
	  This command restores the previous read-only status of the buffer.
\\[View-exit-and-edit]	exit View mode, and make the current buffer editable
	  even if it was not editable before entry to View mode.
\\[View-quit-all]	quit View mode, restoring all windows to previous state.
447 448 449
\\[View-leave]	quit View mode and maybe switch buffers, but don't kill this buffer.
\\[View-kill-and-leave]	quit View mode, kill current buffer and go back to other buffer.

450
The effect of \\[View-leave], \\[View-quit] and \\[View-kill-and-leave] depends on how view-mode was entered.  If it was
Eli Zaretskii's avatar
Eli Zaretskii committed
451 452 453 454 455 456 457
entered by view-file, view-file-other-window, view-file-other-frame, or
\\[dired-view-file] \(\\[view-file], \\[view-file-other-window],
\\[view-file-other-frame], or the Dired mode v command),
then \\[View-quit] will try to kill the current buffer.
If view-mode was entered from another buffer, by \\[view-buffer],
\\[view-buffer-other-window], \\[view-buffer-other frame], \\[view-file],
\\[view-file-other-window], or \\[view-file-other-frame],
458
then \\[View-leave], \\[View-quit] and \\[View-kill-and-leave] will return to that buffer.
459 460

Entry to view-mode runs the normal hook `view-mode-hook'."
461
  :lighter " View" :keymap view-mode-map
462
  (if view-mode (view--enable) (view--disable)))
463

464
(defun view--enable ()
465 466
  ;; Always leave view mode before changing major mode.
  ;; This is to guarantee that the buffer-read-only variable is restored.
467 468
  (add-hook 'change-major-mode-hook 'view--disable nil t)
  (setq view-page-size nil
469
	view-half-page-size nil
Richard M. Stallman's avatar
Richard M. Stallman committed
470
	view-old-buffer-read-only buffer-read-only
471 472 473 474 475 476 477 478
	buffer-read-only t)
  (if (boundp 'Helper-return-blurb)
      (setq view-old-Helper-return-blurb (and (boundp 'Helper-return-blurb)
					      Helper-return-blurb)
	    Helper-return-blurb
	    (format "continue viewing %s"
		    (if (buffer-file-name)
			(file-name-nondirectory (buffer-file-name))
479
		      (buffer-name))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
480

481 482

(define-obsolete-function-alias 'view-mode-enable 'view-mode "24.4")
Richard M. Stallman's avatar
Richard M. Stallman committed
483 484
(defun view-mode-disable ()
  "Turn off View mode."
485 486 487 488 489
  (declare (obsolete view-mode "24.4"))
  (view-mode -1))

(defun view--disable ()
  (remove-hook 'change-major-mode-hook 'view--disable t)
Richard M. Stallman's avatar
Richard M. Stallman committed
490
  (and view-overlay (delete-overlay view-overlay))
491
  ;; Calling read-only-mode while View mode is enabled
492
  ;; sets view-read-only to t as a buffer-local variable
493
  ;; after exiting View mode.  That arranges that the next read-only-mode
494
  ;; will reenable View mode.
Paul Eggert's avatar
Paul Eggert committed
495
  ;; Canceling View mode in any other way should cancel that, too,
496
  ;; so that View mode stays off if read-only-mode is called.
497 498
  (if (local-variable-p 'view-read-only)
      (kill-local-variable 'view-read-only))
499 500
  (if (boundp 'Helper-return-blurb)
      (setq Helper-return-blurb view-old-Helper-return-blurb))
501 502
  (if buffer-read-only
      (setq buffer-read-only view-old-buffer-read-only)))
503 504

;;;###autoload
505 506 507 508
(defun view-return-to-alist-update (buffer &optional item)
  "Update `view-return-to-alist' of buffer BUFFER.
Remove from `view-return-to-alist' all entries referencing dead
windows.  Optional argument ITEM non-nil means add ITEM to
Paul Eggert's avatar
Paul Eggert committed
509
`view-return-to-alist' after purging.  For a description of items
510 511 512 513
that can be added see the RETURN-TO-ALIST argument of the
function `view-mode-exit'.  If `view-return-to-alist' contains an
entry for the selected window, purge that entry from
`view-return-to-alist' before adding ITEM."
514
  (declare (obsolete "this function has no effect." "24.1"))
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
  (with-current-buffer buffer
    (when view-return-to-alist
      (let* ((list view-return-to-alist)
	     entry entry-window last)
	(while list
	  (setq entry (car list))
	  (setq entry-window (car entry))
	  (if (and (windowp entry-window)
		   (or (and item (eq entry-window (selected-window)))
		       (not (window-live-p entry-window))))
	      ;; Remove that entry.
	      (if last
		  (setcdr last (cdr list))
		(setq view-return-to-alist
		      (cdr view-return-to-alist)))
	    ;; Leave entry alone.
	    (setq last entry))
	  (setq list (cdr list)))))
    ;; Add ITEM.
    (when item
      (setq view-return-to-alist
	    (cons item view-return-to-alist)))))

;;;###autoload
539
(defun view-mode-enter (&optional quit-restore exit-action)
540
  "Enter View mode and set up exit from view mode depending on optional arguments.
541 542 543 544 545 546
Optional argument QUIT-RESTORE if non-nil must specify a valid
entry for quitting and restoring any window showing the current
buffer.  This entry replaces any parameter installed by
`display-buffer' and is used by `view-mode-exit'.

Optional argument EXIT-ACTION, if non-nil, must specify a
547 548 549 550
function that takes a buffer as argument.  This function will be
called by `view-mode-exit'.

For a list of all View commands, type H or h while viewing.
551 552

This function runs the normal hook `view-mode-hook'."
553 554 555 556
  (when quit-restore
    (dolist (window (get-buffer-window-list nil nil t))
      (set-window-parameter window 'quit-restore quit-restore)))

557 558 559 560
  (when exit-action
    (setq view-exit-action exit-action))

  (unless view-mode
561
    (view-mode 1)
562 563 564 565
    (unless view-inhibit-help-message
      (message "%s"
	       (substitute-command-keys "\
View mode: type \\[help-command] for help, \\[describe-mode] for commands, \\[View-quit] to quit.")))))
566

567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
;; This is awful because it assumes that the selected window shows the
;; current buffer when this is called.
(defun view-mode-exit (&optional exit-only exit-action all-windows)
  "Exit View mode in various ways.
If all arguments are nil, remove the current buffer from the
selected window using the `quit-restore' information associated
with the selected window.  If optional argument ALL-WINDOWS or
`view-exits-all-viewing-windows' are non-nil, remove the current
buffer from all windows showing it.

Optional argument EXIT-ONLY non-nil means just exit `view-mode'
\(unless `view-no-disable-on-exit' is non-nil) but do not change
the associations of any windows with the current buffer.

EXIT-ACTION, if non-nil, must specify a function that is called
with the current buffer as argument and is called after disabling
`view-mode' and removing any associations of windows with the
current buffer. "
  (when view-mode
    (let ((buffer (window-buffer)))
587
      (unless view-no-disable-on-exit
588
	(view-mode -1))
589 590 591 592 593

      (unless exit-only
	(cond
	 ((or all-windows view-exits-all-viewing-windows)
	  (dolist (window (get-buffer-window-list))
594
	    (quit-window nil window)))
595
	 ((eq (window-buffer) (current-buffer))
596
	  (quit-window)))
597 598

	(when exit-action
599
	  (funcall exit-action buffer))))))
600

601 602
(defun View-exit ()
  "Exit View mode but stay in current buffer."
Richard M. Stallman's avatar
Richard M. Stallman committed
603
  (interactive)
604
  (view-mode-exit t))
Richard M. Stallman's avatar
Richard M. Stallman committed
605

606
;;;###autoload
607 608 609
(defun View-exit-and-edit ()
  "Exit View mode and make the current buffer editable."
  (interactive)
610 611
  (let ((view-old-buffer-read-only nil)
	(view-no-disable-on-exit nil))
612
    (view-mode-exit t)))
613 614 615 616

(defun View-leave ()
  "Quit View mode and maybe switch buffers, but don't kill this buffer."
  (interactive)
617
  (view-mode-exit))
618 619 620

(defun View-quit ()
  "Quit View mode, trying to restore window and buffer to previous state.
Richard M. Stallman's avatar
Richard M. Stallman committed
621
Maybe kill this buffer.  Try to restore selected window to previous state
622 623
and go to previous buffer or window."
  (interactive)
624
  (view-mode-exit nil view-exit-action))
625 626

(defun View-quit-all ()
Richard M. Stallman's avatar
Richard M. Stallman committed
627 628
  "Quit View mode, trying to restore windows and buffers to previous state.
Maybe kill current buffer.  Try to restore all windows viewing buffer to
629 630
previous state and go to previous buffer or window."
  (interactive)
631
  (view-mode-exit nil view-exit-action t))
632 633 634 635

(defun View-kill-and-leave ()
  "Quit View mode, kill current buffer and return to previous buffer."
  (interactive)
636
  (view-mode-exit nil (or view-exit-action 'kill-buffer) t))
637

638 639 640 641

;;; Some help routines.

(defun view-window-size ()
642 643 644 645 646 647 648 649
  ;; Return the height of the current window, excluding the mode line.
  ;; Using `window-line-height' accounts for variable-height fonts.
  (let ((h (window-line-height -1)))
    (if h
	(1+ (nth 1 h))
      ;; This should not happen, but if `window-line-height' returns
      ;; nil, fall back on `window-height'.
      (1- (window-height)))))
650

Stefan Monnier's avatar
Stefan Monnier committed
651 652 653 654
;; (defun view-last-command (&optional who what)
;;  (setq view-last-command-entry this-command)
;;  (setq view-last-command who)
;;  (setq view-last-command-argument what))
Richard M. Stallman's avatar
Richard M. Stallman committed
655

Stefan Monnier's avatar
Stefan Monnier committed
656 657 658 659 660 661 662
;; (defun View-repeat-last-command ()
;;  "Repeat last command issued in View mode."
;;  (interactive)
;;  (if (and view-last-command
;; 	   (eq view-last-command-entry last-command))
;;      (funcall view-last-command view-last-command-argument))
;;  (setq this-command view-last-command-entry))
Richard M. Stallman's avatar
Richard M. Stallman committed
663

664
(defun view-recenter ()
665 666
  ;; Recenter point in window and redisplay normally.
  (recenter '(1)))
667 668

(defun view-page-size-default (lines)
669 670 671 672 673 674 675
  ;; If LINES is nil, 0, or larger than `view-window-size', return nil.
  ;; Otherwise, return LINES.
  (and lines
       (not (zerop (setq lines (prefix-numeric-value lines))))
       (<= (abs lines)
	   (abs (- (view-window-size) next-screen-context-lines)))
       (abs lines)))
676 677 678

(defun view-set-half-page-size-default (lines)
  ;; Get and maybe set half page size.
679 680
  (if (not lines) (or view-half-page-size
		      (/ (view-window-size) 2))
681 682 683 684 685 686 687 688 689 690 691
    (setq view-half-page-size
	  (if (zerop (setq lines (prefix-numeric-value lines)))
	      (/ (view-window-size) 2)
	    (view-page-size-default lines)))))


;;; Commands for moving around in the buffer.

(defun View-goto-percent (&optional percent)
  "Move to end (or prefix PERCENT) of buffer in View mode.
Display is centered at point.
Richard M. Stallman's avatar
Richard M. Stallman committed
692
Also set the mark at the position where point was."
693 694 695 696 697 698 699 700 701 702
  (interactive "P")
  (push-mark)
  (goto-char
   (if percent
       (+ (point-min)
	  (floor (* (- (point-max) (point-min)) 0.01
		    (max 0 (min 100 (prefix-numeric-value percent))))))
     (point-max)))
  (view-recenter))

Stefan Monnier's avatar
Stefan Monnier committed
703 704 705 706 707 708 709 710 711 712
;; (defun View-goto-line-last (&optional line)
;; "Move to last (or prefix LINE) line in View mode.
;; Display is centered at LINE.
;; Sets mark at starting position and pushes mark ring."
;;  (interactive "P")
;;  (push-mark)
;;  (if line (goto-line (prefix-numeric-value line))
;;    (goto-char (point-max))
;;    (beginning-of-line))
;;  (view-recenter))
713

714 715 716
(defun View-goto-line (&optional line)
  "Move to first (or prefix LINE) line in View mode.
Display is centered at LINE.
Richard M. Stallman's avatar
Richard M. Stallman committed
717
Also set the mark at the position where point was."
Richard M. Stallman's avatar
Richard M. Stallman committed
718 719
  (interactive "p")
  (push-mark)
720 721
  (goto-char (point-min))
  (forward-line (1- line))
722
  (view-recenter))
Richard M. Stallman's avatar
Richard M. Stallman committed
723

724
(defun View-back-to-mark (&optional _ignore)
725 726 727 728
  "Return to last mark set in View mode, else beginning of file.
Display that line at the center of the window.
This command pops the mark ring, so that successive
invocations return to earlier marks."
729
  (interactive)
730 731 732 733
  (goto-char (or (mark t) (point-min)))
  (pop-mark)
  (view-recenter))

734 735
(defun view-scroll-lines (lines backward default maxdefault)
  ;; This function does the job for all the scrolling commands.
736 737 738
  ;; Scroll forward LINES lines.  If BACKWARD is non-nil, scroll backwards.
  ;; If LINES is negative scroll in the other direction.
  ;; If LINES is 0 or nil, scroll DEFAULT lines (if DEFAULT is nil, scroll
Juanma Barranquero's avatar
Juanma Barranquero committed
739
  ;; by one page).  If MAXDEFAULT is non-nil, scroll no more than a window.
740 741
  (if (or (null lines) (zerop (setq lines (prefix-numeric-value lines))))
      (setq lines default))
742 743 744 745
  (when (and lines (< lines 0))
    (setq backward (not backward) lines (- lines)))
  (when (and maxdefault lines (> lines (view-window-size)))
    (setq lines nil))
746
  (cond (backward (scroll-down-command lines))
747 748 749 750 751
	((view-really-at-end)
	 (if view-scroll-auto-exit
	     (View-quit)
	   (ding)
	   (view-end-message)))
752
	(t (scroll-up-command lines)
753
	   (if (view-really-at-end) (view-end-message)))))
754 755

(defun view-really-at-end ()
Richard M. Stallman's avatar
Richard M. Stallman committed
756
  ;; Return true if buffer end visible.  Maybe revert buffer and test.
757 758
  (and (or (null scroll-error-top-bottom) (eobp))
       (pos-visible-in-window-p (point-max))
759 760 761
       (let ((buf (current-buffer))
	     (bufname (buffer-name))
	     (file (buffer-file-name)))
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
	 (or (not view-try-extend-at-buffer-end)
	     (null file)
	     (verify-visited-file-modtime buf)
	     (not (file-exists-p file))
	     (when (buffer-modified-p buf)
	       (setq file (file-name-nondirectory file))
	       (not (yes-or-no-p
		     (format
		      "File %s changed on disk.  Discard your edits%s? "
		      file
		      (if (string= bufname file) ""
			(concat " in " bufname))))))
	     (progn
	       (revert-buffer t t t)
	       (pos-visible-in-window-p (point-max)))))))
777 778 779 780

(defun view-end-message ()
  ;; Tell that we are at end of buffer.
  (goto-char (point-max))
781
  (if (window-parameter nil 'quit-restore)
782 783 784 785 786
      (message "End of buffer.  Type %s to quit viewing."
	       (substitute-command-keys
		(if view-scroll-auto-exit "\\[View-scroll-page-forward]"
		  "\\[View-quit]")))
    (message "End of buffer")))
787 788 789 790 791 792 793 794

(defun View-scroll-to-buffer-end ()
  "Scroll backward or forward so that buffer end is at last line of window."
  (interactive)
  (let ((p (if (pos-visible-in-window-p (point-max)) (point))))
    (goto-char (point-max))
    (recenter -1)
    (and p (goto-char p))))
795 796 797

(defun View-scroll-page-forward (&optional lines)
  "Scroll \"page size\" or prefix LINES lines forward in View mode.
Richard M. Stallman's avatar
Richard M. Stallman committed
798
Exit if end of text is visible and `view-scroll-auto-exit' is non-nil.
799 800 801 802
\"page size\" is whole window full, or number of lines set by
\\[View-scroll-page-forward-set-page-size] or
\\[View-scroll-page-backward-set-page-size].
If LINES is more than a window-full, only the last window-full is shown."
Richard M. Stallman's avatar
Richard M. Stallman committed
803
  (interactive "P")
804
  (view-scroll-lines lines nil (view-page-size-default view-page-size) nil))
805

806
(defun View-scroll-page-backward (&optional lines)
807
  "Scroll \"page size\" or prefix LINES lines backward in View mode.
Richard M. Stallman's avatar
Richard M. Stallman committed
808
See also `View-scroll-page-forward'."
Richard M. Stallman's avatar
Richard M. Stallman committed
809
  (interactive "P")
810
  (view-scroll-lines lines t (view-page-size-default view-page-size) nil))
811

812
(defun View-scroll-page-forward-set-page-size (&optional lines)
Richard M. Stallman's avatar
Richard M. Stallman committed
813
  "Scroll forward LINES lines in View mode, setting the \"page size\".
814
This is the number of lines which \\[View-scroll-page-forward] and
815 816 817 818
\\[View-scroll-page-backward] scroll by default.
If LINES is omitted or = 0, sets \"page size\" to window height and
scrolls forward that much, otherwise scrolls forward LINES lines and sets
\"page size\" to the minimum of window height and the absolute value of LINES.
Richard M. Stallman's avatar
Richard M. Stallman committed
819
See also `View-scroll-page-forward'."
Richard M. Stallman's avatar
Richard M. Stallman committed
820
  (interactive "P")
821 822 823
  (view-scroll-lines lines nil
		     (setq view-page-size (view-page-size-default lines))
		     nil))
Richard M. Stallman's avatar
Richard M. Stallman committed
824

825 826
(defun View-scroll-page-backward-set-page-size (&optional lines)
  "Scroll backward prefix LINES lines in View mode, setting the \"page size\".
Richard M. Stallman's avatar
Richard M. Stallman committed
827
See also `View-scroll-page-forward-set-page-size'."
Richard M. Stallman's avatar
Richard M. Stallman committed
828
  (interactive "P")
829 830 831 832 833 834
  (view-scroll-lines lines t
		     (setq view-page-size (view-page-size-default lines))
		     nil))

(defun View-scroll-line-forward (&optional lines)
  "Scroll forward one line (or prefix LINES lines) in View mode.
Juanma Barranquero's avatar
Juanma Barranquero committed
835
See also `View-scroll-page-forward', but note that scrolling is limited
836 837 838 839 840 841
to minimum of LINES and one window-full."
  (interactive "P")
  (view-scroll-lines lines nil 1 t))

(defun View-scroll-line-backward (&optional lines)
  "Scroll backward one line (or prefix LINES lines) in View mode.
Richard M. Stallman's avatar
Richard M. Stallman committed
842
See also `View-scroll-line-forward'."
843 844 845 846
  (interactive "P")
  (view-scroll-lines lines t 1 t))

(defun View-scroll-half-page-forward (&optional lines)
Richard M. Stallman's avatar
Richard M. Stallman committed
847
  "Scroll forward a \"half page\" (or prefix LINES) lines in View mode.
848 849 850 851 852 853 854
If LINES is not omitted, the \"half page size\" is set to the minimum of
window height and the absolute value of LINES.
LINES=0 resets \"half page size\" to half window height."
  (interactive "P")
  (view-scroll-lines lines nil (view-set-half-page-size-default lines) t))

(defun View-scroll-half-page-backward (&optional lines)
Richard M. Stallman's avatar
Richard M. Stallman committed
855 856
  "Scroll backward a \"half page\" (or prefix LINES) lines in View mode.
See also `View-scroll-half-page-forward'."
857 858 859
  (interactive "P")
  (view-scroll-lines lines t (view-set-half-page-size-default lines) t))

Richard M. Stallman's avatar
Richard M. Stallman committed
860 861
(defun View-revert-buffer-scroll-page-forward (&optional lines)
  "Scroll forward, reverting buffer if needed, in View mode.
862 863 864
If buffer has not been changed and the corresponding file is newer, first
revert the buffer, then scroll.
This command is useful if you are viewing a changing file.
Richard M. Stallman's avatar
Richard M. Stallman committed
865 866 867

The prefix argument LINES says how many lines to scroll.
If you don't specify a prefix argument, it uses the number of lines set by
868 869 870 871
\\[View-scroll-page-forward-set-page-size] or
\\[View-scroll-page-backward-set-page-size].
If LINES is more than a window-full, only the last window-full is shown."
  (interactive "P")
872
  (let ((view-scroll-auto-exit nil)
873
	(view-try-extend-at-buffer-end t))
874
    (view-scroll-lines lines nil (view-page-size-default view-page-size) nil)))
875

876
(defun View-search-regexp-forward (n regexp)
877
  "Search forward for first (or prefix Nth) occurrence of REGEXP in View mode.
878 879 880 881 882 883 884 885 886 887 888

Displays line found at center of window.  Sets mark at starting position and
pushes mark ring.

Characters @ and ! are special at the beginning of REGEXP.  They modify
the search rather than become part of the pattern searched for.
@ means search all the buffer i.e. start search at the beginning of buffer.
! means search for a line that contains no match for the pattern.
If REGEXP is empty or only consist of these control characters, then
an earlier remembered REGEXP is used, otherwise REGEXP is remembered
for use by later search commands.
Richard M. Stallman's avatar
Richard M. Stallman committed
889 890 891

The variable `view-highlight-face' controls the face that is used
for highlighting the match that is found."
892
  (interactive "p\nsSearch forward (regexp): ")
893
  (view-search n regexp))
Richard M. Stallman's avatar
Richard M. Stallman committed
894

895
(defun View-search-regexp-backward (n regexp)
896
  "Search backward for first (or prefix Nth) occurrence of REGEXP in View mode.
897 898 899 900 901 902 903 904 905 906 907

Displays line found at center of window.  Sets mark at starting position and
pushes mark ring.

Characters @ and ! are special at the beginning of REGEXP.  They modify
the search rather than become part of the pattern searched for.
@ means search all the buffer i.e. start search at the end of buffer.
! means search for a line that contains no match for the pattern.
If REGEXP is empty or only consist of these control characters, then
an earlier remembered REGEXP is used, otherwise REGEXP is remembered
for use by later search commands.
Richard M. Stallman's avatar
Richard M. Stallman committed
908 909 910

The variable `view-highlight-face' controls the face that is used
for highlighting the match that is found."
Richard M. Stallman's avatar
Richard M. Stallman committed
911
  (interactive "p\nsSearch backward (regexp): ")
912
  (view-search (- n) regexp))
Richard M. Stallman's avatar
Richard M. Stallman committed
913

914 915 916 917
(defun View-search-last-regexp-forward (n) "\
Search forward for first (or prefix Nth) instance of last regexp in View mode.
Displays line found at center of window.  Sets mark at starting position and
pushes mark ring.
Richard M. Stallman's avatar
Richard M. Stallman committed
918 919 920

The variable `view-highlight-face' controls the face that is used
for highlighting the match that is found."
Richard M. Stallman's avatar
Richard M. Stallman committed
921
  (interactive "p")
922
  (view-search n nil))
Richard M. Stallman's avatar
Richard M. Stallman committed
923

924 925
(defun View-search-last-regexp-backward (n) "\
Search backward for first (or prefix Nth) instance of last regexp in View mode.
Richard M. Stallman's avatar
Richard M. Stallman committed
926
Displays line found at center of window.  Sets mark at starting position and
Richard M. Stallman's avatar
Richard M. Stallman committed
927 928 929 930
pushes mark ring.

The variable `view-highlight-face' controls the face that is used
for highlighting the match that is found."
Richard M. Stallman's avatar
Richard M. Stallman committed
931
  (interactive "p")
932
  (view-search (- n) nil))
Richard M. Stallman's avatar
Richard M. Stallman committed
933 934

(defun view-search (times regexp)
935
  ;; This function does the job for all the View-search- commands.
Paul Eggert's avatar
Paul Eggert committed
936
  ;; Search for the TIMESth match for REGEXP.  If TIMES is negative
Juanma Barranquero's avatar
Juanma Barranquero committed
937
  ;; search backwards.  If REGEXP is nil use `view-last-regexp'.
Juanma Barranquero's avatar
Juanma Barranquero committed
938
  ;; Characters "!" and "@" have a special meaning at the beginning of
939 940 941
  ;; REGEXP and are removed from REGEXP before the search "!" means
  ;; search for lines with no match for REGEXP.  "@" means search in
  ;; the whole buffer, don't start searching from the present point.
942 943 944 945 946 947 948 949 950 951 952 953 954
  (let (where no end ln)
    (cond
     ((and regexp (> (length regexp) 0)
	   (or (not (memq (string-to-char regexp) '(?! ?@)))
	       (progn
		 (if (member (substring regexp 0 2) '("!@" "@!"))
		     (setq end t no t ln 2)
		   (setq no (not (setq end (eq ?@ (string-to-char regexp))))
			 ln 1))
		 (> (length (setq regexp (substring regexp ln))) 0))))
      (setq view-last-regexp (if no (list regexp) regexp)))
     ((consp view-last-regexp)
      (setq regexp (car view-last-regexp))
955
      (unless (setq no (not no)) (setq view-last-regexp regexp)))
956 957 958
     (view-last-regexp (setq regexp view-last-regexp)
		       (if no (setq view-last-regexp (list regexp))))
     (t (error "No previous View-mode search")))
Richard M. Stallman's avatar
Richard M. Stallman committed
959
    (save-excursion
960
      (if end (goto-char (if (< times 0) (point-max) (point-min)))
961
	(forward-line (if (< times 0) -1 1)))
962 963
      (if (if no (view-search-no-match-lines times regexp)
	    (re-search-forward regexp nil t times))
Richard M. Stallman's avatar
Richard M. Stallman committed
964 965 966 967 968
	  (setq where (point))))
    (if where
	(progn
	  (push-mark)
	  (goto-char where)
969 970 971 972
	  (if view-overlay
	      (move-overlay view-overlay (match-beginning 0) (match-end 0))
	    (setq view-overlay
		  (make-overlay (match-beginning 0) (match-end 0))))
973
	  (overlay-put view-overlay 'face view-highlight-face)
Richard M. Stallman's avatar
Richard M. Stallman committed
974
	  (beginning-of-line)
975 976 977
	  (view-recenter))
      (message "Can't find occurrence %d of %s%s"
	       times (if no "no " "") regexp)
Richard M. Stallman's avatar
Richard M. Stallman committed
978 979
      (sit-for 4))))

980 981 982
;; This is the dumb approach, looking at each line.  The original
;; version of this function looked like it might have been trying to
;; do something clever, but not succeeding:
983
;; https://lists.gnu.org/r/bug-gnu-emacs/2007-09/msg00073.html
984
(defun view-search-no-match-lines (times regexp)
985 986 987
  "Search for the TIMESth occurrence of a line with no match for REGEXP.
If such a line is found, return non-nil and set the match-data to that line.
If TIMES is negative, search backwards."
988 989 990
  (let ((step (if (>= times 0) 1
                (setq times (- times))
                -1)))
991 992 993
    ;; Note that we do not check the current line.
    (while (and (> times 0)
                (zerop (forward-line step)))
994 995 996 997 998 999 1000
      ;; (forward-line 1) returns 0 on moving within the last line.
      (if (eobp)
          (setq times -1)
        (or (re-search-forward regexp (line-end-position) t)
            (setq times (1- times))))))
  (and (zerop times)
       (looking-at ".*")))
1001

Jim Blandy's avatar
Jim Blandy committed
1002 1003
(provide 'view)

Eric S. Raymond's avatar
Eric S. Raymond committed
1004
;;; view.el ends here