1 ;;; view.el --- peruse file or buffer without editing.
3 ;; Copyright (C) 1985, 1989 Free Software Foundation, Inc.
5 ;; Author: K. Shane Hartman
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 (defvar view-mode-map nil
)
29 (setq view-mode-map
(make-keymap))
30 (fillarray (nth 1 view-mode-map
) 'View-undefined
)
31 (define-key view-mode-map
"\C-c" 'view-exit
)
32 (define-key view-mode-map
"\C-z" 'suspend-emacs
)
33 (define-key view-mode-map
"q" 'view-exit
)
34 (define-key view-mode-map
"-" 'negative-argument
)
35 (define-key view-mode-map
"0" 'digit-argument
)
36 (define-key view-mode-map
"1" 'digit-argument
)
37 (define-key view-mode-map
"2" 'digit-argument
)
38 (define-key view-mode-map
"3" 'digit-argument
)
39 (define-key view-mode-map
"4" 'digit-argument
)
40 (define-key view-mode-map
"5" 'digit-argument
)
41 (define-key view-mode-map
"6" 'digit-argument
)
42 (define-key view-mode-map
"7" 'digit-argument
)
43 (define-key view-mode-map
"8" 'digit-argument
)
44 (define-key view-mode-map
"9" 'digit-argument
)
45 (define-key view-mode-map
"\C-u" 'universal-argument
)
46 (define-key view-mode-map
"\e" nil
)
47 (define-key view-mode-map
"\C-x" 'Control-X-prefix
)
48 (define-key view-mode-map
"\e-" 'negative-argument
)
49 (define-key view-mode-map
"\e0" 'digit-argument
)
50 (define-key view-mode-map
"\e1" 'digit-argument
)
51 (define-key view-mode-map
"\e2" 'digit-argument
)
52 (define-key view-mode-map
"\e3" 'digit-argument
)
53 (define-key view-mode-map
"\e4" 'digit-argument
)
54 (define-key view-mode-map
"\e5" 'digit-argument
)
55 (define-key view-mode-map
"\e6" 'digit-argument
)
56 (define-key view-mode-map
"\e7" 'digit-argument
)
57 (define-key view-mode-map
"\e8" 'digit-argument
)
58 (define-key view-mode-map
"\e9" 'digit-argument
)
59 (define-key view-mode-map
"<" 'beginning-of-buffer
)
60 (define-key view-mode-map
">" 'end-of-buffer
)
61 (define-key view-mode-map
"\ev" 'View-scroll-lines-backward
)
62 (define-key view-mode-map
"\C-v" 'View-scroll-lines-forward
)
63 (define-key view-mode-map
" " 'View-scroll-lines-forward
)
64 (define-key view-mode-map
"\177" 'View-scroll-lines-backward
)
65 (define-key view-mode-map
"\n" 'View-scroll-one-more-line
)
66 (define-key view-mode-map
"\r" 'View-scroll-one-more-line
)
67 (define-key view-mode-map
"\C-l" 'recenter
)
68 (define-key view-mode-map
"z" 'View-scroll-lines-forward-set-scroll-size
)
69 (define-key view-mode-map
"g" 'View-goto-line
)
70 (define-key view-mode-map
"=" 'what-line
)
71 (define-key view-mode-map
"." 'set-mark-command
)
72 (define-key view-mode-map
"\C-@" 'set-mark-command
)
73 (define-key view-mode-map
"'" 'View-back-to-mark
)
74 (define-key view-mode-map
"@" 'View-back-to-mark
)
75 (define-key view-mode-map
"x" 'exchange-point-and-mark
)
76 (define-key view-mode-map
"h" 'Helper-describe-bindings
)
77 (define-key view-mode-map
"?" 'Helper-describe-bindings
)
78 (define-key view-mode-map
(char-to-string help-char
) 'Helper-help
)
79 (define-key view-mode-map
"\C-n" 'next-line
)
80 (define-key view-mode-map
"\C-p" 'previous-line
)
81 (define-key view-mode-map
"\C-s" 'isearch-forward
)
82 (define-key view-mode-map
"\C-r" 'isearch-backward
)
83 (define-key view-mode-map
"s" 'isearch-forward
)
84 (define-key view-mode-map
"r" 'isearch-backward
)
85 (define-key view-mode-map
"/" 'View-search-regexp-forward
)
86 (define-key view-mode-map
"\\" 'View-search-regexp-backward
)
87 ;; This conflicts with the standard binding of isearch-regexp-forward
88 (define-key view-mode-map
"\e\C-s" 'View-search-regexp-forward
)
89 (define-key view-mode-map
"\e\C-r" 'View-search-regexp-backward
)
90 (define-key view-mode-map
"n" 'View-search-last-regexp-forward
)
91 (define-key view-mode-map
"p" 'View-search-last-regexp-backward
)
96 (defun view-file (file-name)
97 "View FILE in View mode, returning to previous buffer when done.
98 The usual Emacs commands are not available; instead,
99 a special set of commands (mostly letters and punctuation)
100 are defined for moving around in the buffer.
101 Space scrolls forward, Delete scrolls backward.
102 For list of all View commands, type ? or h while viewing.
104 Calls the value of view-hook if that is non-nil."
105 (interactive "fView file: ")
106 (let ((old-buf (current-buffer))
107 (had-a-buf (get-file-buffer file-name
))
108 (buf-to-view (find-file-noselect file-name
)))
109 (switch-to-buffer buf-to-view t
)
111 (and (not had-a-buf
) (not (buffer-modified-p buf-to-view
))
115 (defun view-file-other-window (file-name)
116 "View FILE in View mode in other window.
117 Return to previous buffer when done.
118 The usual Emacs commands are not available; instead,
119 a special set of commands (mostly letters and punctuation)
120 are defined for moving around in the buffer.
121 Space scrolls forward, Delete scrolls backward.
122 For list of all View commands, type ? or h while viewing.
124 Calls the value of view-hook if that is non-nil."
125 (interactive "fView file: ")
126 (let ((old-arrangement (current-window-configuration))
127 (had-a-buf (get-file-buffer file-name
))
128 (buf-to-view (find-file-noselect file-name
)))
129 (switch-to-buffer-other-window buf-to-view
)
130 (view-mode old-arrangement
131 (and (not had-a-buf
) (not (buffer-modified-p buf-to-view
))
135 (defun view-buffer (buffer-name)
136 "View BUFFER in View mode, returning to previous buffer when done.
137 The usual Emacs commands are not available; instead,
138 a special set of commands (mostly letters and punctuation)
139 are defined for moving around in the buffer.
140 Space scrolls forward, Delete scrolls backward.
141 For list of all View commands, type ? or h while viewing.
143 Calls the value of view-hook if that is non-nil."
144 (interactive "bView buffer: ")
145 (let ((old-buf (current-buffer)))
146 (switch-to-buffer buffer-name t
)
147 (view-mode old-buf nil
)))
150 (defun view-buffer-other-window (buffer-name not-return
)
151 "View BUFFER in View mode in another window,
152 returning to original buffer when done ONLY if
153 prefix argument not-return is nil (as by default).
154 The usual Emacs commands are not available; instead,
155 a special set of commands (mostly letters and punctuation)
156 are defined for moving around in the buffer.
157 Space scrolls forward, Delete scrolls backward.
158 For list of all View commands, type ? or h while viewing.
160 Calls the value of view-hook if that is non-nil."
161 (interactive \"bView buffer
:\\nP
\")
162 (let ((return-to (and not-return
(current-window-configuration))))
163 (switch-to-buffer-other-window buffer-name
)
164 (view-mode return-to
)))
167 (defun view-mode (&optional prev-buffer action
)
168 "Major mode for viewing text but not editing it.
169 Letters do not insert themselves. Instead these commands are provided.
170 Most commands take prefix arguments. Commands dealing with lines
171 default to \"scroll size\" lines (initially size of window).
172 Search commands default to a repeat count of one.
173 M-< or < move to beginning of buffer.
174 M-> or > move to end of buffer.
175 C-v or Space scroll forward lines.
176 M-v or DEL scroll backward lines.
177 CR or LF scroll forward one line (backward with prefix argument).
178 z like Space except set number of lines for further
179 scrolling commands to scroll by.
180 C-u and Digits provide prefix arguments. `-' denotes negative argument.
181 = prints the current line number.
182 g goes to line given by prefix argument.
183 / or M-C-s searches forward for regular expression
184 \\ or M-C-r searches backward for regular expression.
185 n searches forward for last regular expression.
186 p searches backward for last regular expression.
187 C-@ or . set the mark.
188 x exchanges point and mark.
189 C-s or s do forward incremental search.
190 C-r or r do reverse incremental search.
191 @ or ' return to mark and pops mark ring.
192 Mark ring is pushed at start of every
193 successful search and when jump to line to occurs.
194 The mark is set on jump to buffer start or end.
195 ? or h provide help message (list of commands).
196 \\[Helper-help] provides help (list of commands or description of a command).
197 C-n moves down lines vertically.
198 C-p moves upward lines vertically.
199 C-l recenters the screen.
200 q or C-c exit view-mode and return to previous buffer.
202 Entry to this mode calls the value of view-hook if non-nil.
204 ; Not interactive because dangerous things happen
205 ; if you call it without passing a buffer as argument
206 ; and they are not easy to fix.
208 (make-local-variable 'view-old-mode-line-buffer-identification
)
209 (setq view-old-mode-line-buffer-identification
210 mode-line-buffer-identification
)
211 (make-local-variable 'view-old-buffer-read-only
)
212 (setq view-old-buffer-read-only buffer-read-only
)
213 (make-local-variable 'view-old-mode-name
)
214 (setq view-old-mode-name mode-name
)
215 (make-local-variable 'view-old-major-mode
)
216 (setq view-old-major-mode major-mode
)
217 (make-local-variable 'view-old-local-map
)
218 (setq view-old-local-map
(current-local-map))
219 (make-local-variable 'view-old-Helper-return-blurb
)
220 (setq view-old-Helper-return-blurb
221 (and (boundp 'Helper-return-blurb
) Helper-return-blurb
))
223 (setq buffer-read-only t
)
224 (setq mode-line-buffer-identification
226 (if (buffer-file-name)
229 (setq mode-name
"View")
230 (setq major-mode
'view-mode
)
231 (setq Helper-return-blurb
232 (format "continue viewing %s"
233 (if (buffer-file-name)
234 (file-name-nondirectory (buffer-file-name))
237 (make-local-variable 'view-exit-action
)
238 (setq view-exit-action action
)
239 (make-local-variable 'view-return-here
)
240 (setq view-return-here prev-buffer
)
241 (make-local-variable 'view-exit-position
)
242 (setq view-exit-position
(point-marker))
244 (make-local-variable 'view-scroll-size
)
245 (setq view-scroll-size nil
)
246 (make-local-variable 'view-last-regexp
)
247 (setq view-last-regexp nil
)
250 (setq goal-column nil
)
252 (use-local-map view-mode-map
)
253 (run-hooks 'view-hook
)
254 (view-helpful-message))
258 "Exit from view-mode.
259 If you viewed an existing buffer, that buffer returns to its previous mode.
260 If you viewed a file that was not present in Emacs, its buffer is killed."
262 (setq mode-line-buffer-identification
263 view-old-mode-line-buffer-identification
)
264 (setq major-mode view-old-major-mode
)
265 (setq mode-name view-old-mode-name
)
266 (use-local-map (current-local-map))
267 (setq buffer-read-only view-old-buffer-read-only
)
269 (goto-char view-exit-position
)
270 (set-marker view-exit-position nil
)
272 ;; Now do something to the buffer that we were viewing
273 ;; (such as kill it).
274 (let ((viewed-buffer (current-buffer))
275 (action view-exit-action
))
277 ((bufferp view-return-here
)
278 (switch-to-buffer view-return-here
))
279 ((window-configuration-p view-return-here
)
280 (set-window-configuration view-return-here
)))
281 (if action
(funcall action viewed-buffer
))))
283 (defun view-helpful-message ()
285 (substitute-command-keys
286 "Type \\[Helper-help] for help, \\[Helper-describe-bindings] for commands, \\[view-exit] to quit.")))
288 (defun View-undefined ()
291 (view-helpful-message))
293 (defun view-window-size () (1- (window-height)))
295 (defun view-scroll-size ()
296 (min (view-window-size) (or view-scroll-size
(view-window-size))))
298 (defvar view-hook nil
299 "If non-nil, its value is called when viewing buffer or file.")
301 ;(defun view-last-command (&optional who what)
302 ; (setq view-last-command-entry this-command)
303 ; (setq view-last-command who)
304 ; (setq view-last-command-argument what))
306 ;(defun View-repeat-last-command ()
307 ; "Repeat last command issued in View mode."
309 ; (if (and view-last-command
310 ; (eq view-last-command-entry last-command))
311 ; (funcall view-last-command view-last-command-argument))
312 ; (setq this-command view-last-command-entry))
314 (defun View-goto-line (&optional line
)
315 "Move to LINE in View mode.
316 Display is centered at LINE. Sets mark at starting position and pushes
320 (goto-line (or line
1))
321 (recenter (/ (view-window-size) 2)))
323 (defun View-scroll-lines-forward (&optional lines
)
324 "Scroll forward in View mode, or exit if end of text is visible.
325 No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size].
326 Arg is number of lines to scroll."
328 (if (pos-visible-in-window-p (point-max))
331 (if lines
(prefix-numeric-value lines
)
333 ; (view-last-command 'View-scroll-lines-forward lines)
334 (if (>= lines
(view-window-size))
336 (if (>= (- lines
) (view-window-size))
339 (cond ((pos-visible-in-window-p (point-max))
340 (goto-char (point-max))
342 (message (substitute-command-keys
343 "End. Type \\[view-exit] to quit viewing."))))
344 (move-to-window-line -
1)
347 (defun View-scroll-lines-forward-set-scroll-size (&optional lines
)
348 "Scroll forward LINES lines in View mode, setting the \"scroll size\".
349 This is the number of lines which \\[View-scroll-lines-forward] and \\[View-scroll-lines-backward] scroll by default.
350 The absolute value of LINES is used, so this command can be used to scroll
351 backwards (but \"scroll size\" is always positive). If LINES is greater than
352 window height or omitted, then window height is assumed. If LINES is less
353 than window height then scrolling context is provided from previous screen."
356 (setq view-scroll-size
(view-window-size))
357 (setq lines
(prefix-numeric-value lines
))
358 (setq view-scroll-size
359 (min (if (> lines
0) lines
(- lines
)) (view-window-size))))
360 (View-scroll-lines-forward lines
))
362 (defun View-scroll-one-more-line (&optional arg
)
363 "Scroll one more line up in View mode.
364 With ARG scroll one line down."
366 (View-scroll-lines-forward (if (not arg
) 1 -
1)))
368 (defun View-scroll-lines-backward (&optional lines
)
369 "Scroll backward in View mode.
370 No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size].
371 Arg is number of lines to scroll."
373 (View-scroll-lines-forward (if lines
374 (- (prefix-numeric-value lines
))
375 (- (view-scroll-size)))))
377 (defun View-search-regexp-forward (times regexp
)
378 "Search forward for NTH occurrence of REGEXP in View mode.
379 Displays line found at center of window. REGEXP is remembered for
380 searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward]. Sets mark at starting position and pushes mark ring."
381 (interactive "p\nsSearch forward (regexp): ")
382 (if (> (length regexp
) 0)
384 ;(view-last-command 'View-search-last-regexp-forward times)
385 (view-search times regexp
))))
387 (defun View-search-regexp-backward (times regexp
)
388 "Search backward from window start for NTH instance of REGEXP in View mode.
389 Displays line found at center of window. REGEXP is remembered for
390 searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward]. Sets mark at starting position and pushes mark ring."
391 (interactive "p\nsSearch backward (regexp): ")
392 (View-search-regexp-forward (- times
) regexp
))
394 (defun View-search-last-regexp-forward (times)
395 "Search forward from window end for NTH instance of last regexp in View mode.
396 Displays line found at center of window. Sets mark at starting position
397 and pushes mark ring."
399 (View-search-regexp-forward times view-last-regexp
))
401 (defun View-search-last-regexp-backward (times)
402 "Search backward from window start for NTH instance of last regexp in View mode.
403 Displays line found at center of window. Sets mark at starting position and
406 (View-search-regexp-backward times view-last-regexp
))
408 (defun View-back-to-mark (&optional ignore
)
409 "Return to last mark set in View mode, else beginning of file.
410 Displays line at center of window. Pops mark ring so successive
411 invocations return to earlier marks."
413 (goto-char (or (mark) (point-min)))
415 (recenter (/ (view-window-size) 2)))
417 (defun view-search (times regexp
)
418 (setq view-last-regexp regexp
)
421 (move-to-window-line (if (< times
0) 0 -
1))
422 (if (re-search-forward regexp nil t times
)
423 (setq where
(point))))
429 (recenter (/ (view-window-size) 2)))
430 (message "Can't find occurrence %d of %s" times regexp
)
436 ;;; view.el ends here