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