44
|
1 ;; View: Peruse file or buffer without editing.
|
|
2 ;; Copyright (C) 1985, 1989 Free Software Foundation, Inc.
|
|
3 ;; Principal author K. Shane Hartman
|
|
4
|
|
5 ;; This file is part of GNU Emacs.
|
|
6
|
|
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
8 ;; it under the terms of the GNU General Public License as published by
|
|
9 ;; the Free Software Foundation; either version 1, or (at your option)
|
|
10 ;; any later version.
|
|
11
|
|
12 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 ;; GNU General Public License for more details.
|
|
16
|
|
17 ;; You should have received a copy of the GNU General Public License
|
|
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
20
|
|
21
|
|
22 (defvar view-mode-map nil)
|
|
23 (if view-mode-map
|
|
24 nil
|
|
25 (setq view-mode-map (make-keymap))
|
354
|
26 (fillarray (nth 1 view-mode-map) 'View-undefined)
|
44
|
27 (define-key view-mode-map "\C-c" 'view-exit)
|
|
28 (define-key view-mode-map "\C-z" 'suspend-emacs)
|
|
29 (define-key view-mode-map "q" 'view-exit)
|
|
30 (define-key view-mode-map "-" 'negative-argument)
|
|
31 (define-key view-mode-map "0" 'digit-argument)
|
|
32 (define-key view-mode-map "1" 'digit-argument)
|
|
33 (define-key view-mode-map "2" 'digit-argument)
|
|
34 (define-key view-mode-map "3" 'digit-argument)
|
|
35 (define-key view-mode-map "4" 'digit-argument)
|
|
36 (define-key view-mode-map "5" 'digit-argument)
|
|
37 (define-key view-mode-map "6" 'digit-argument)
|
|
38 (define-key view-mode-map "7" 'digit-argument)
|
|
39 (define-key view-mode-map "8" 'digit-argument)
|
|
40 (define-key view-mode-map "9" 'digit-argument)
|
|
41 (define-key view-mode-map "\C-u" 'universal-argument)
|
|
42 (define-key view-mode-map "\e" nil)
|
|
43 (define-key view-mode-map "\C-x" 'Control-X-prefix)
|
|
44 (define-key view-mode-map "\e-" 'negative-argument)
|
|
45 (define-key view-mode-map "\e0" 'digit-argument)
|
|
46 (define-key view-mode-map "\e1" 'digit-argument)
|
|
47 (define-key view-mode-map "\e2" 'digit-argument)
|
|
48 (define-key view-mode-map "\e3" 'digit-argument)
|
|
49 (define-key view-mode-map "\e4" 'digit-argument)
|
|
50 (define-key view-mode-map "\e5" 'digit-argument)
|
|
51 (define-key view-mode-map "\e6" 'digit-argument)
|
|
52 (define-key view-mode-map "\e7" 'digit-argument)
|
|
53 (define-key view-mode-map "\e8" 'digit-argument)
|
|
54 (define-key view-mode-map "\e9" 'digit-argument)
|
|
55 (define-key view-mode-map "<" 'beginning-of-buffer)
|
|
56 (define-key view-mode-map ">" 'end-of-buffer)
|
|
57 (define-key view-mode-map "\ev" 'View-scroll-lines-backward)
|
|
58 (define-key view-mode-map "\C-v" 'View-scroll-lines-forward)
|
|
59 (define-key view-mode-map " " 'View-scroll-lines-forward)
|
|
60 (define-key view-mode-map "\177" 'View-scroll-lines-backward)
|
|
61 (define-key view-mode-map "\n" 'View-scroll-one-more-line)
|
|
62 (define-key view-mode-map "\r" 'View-scroll-one-more-line)
|
|
63 (define-key view-mode-map "\C-l" 'recenter)
|
|
64 (define-key view-mode-map "z" 'View-scroll-lines-forward-set-scroll-size)
|
|
65 (define-key view-mode-map "g" 'View-goto-line)
|
|
66 (define-key view-mode-map "=" 'what-line)
|
|
67 (define-key view-mode-map "." 'set-mark-command)
|
|
68 (define-key view-mode-map "\C-@" 'set-mark-command)
|
|
69 (define-key view-mode-map "'" 'View-back-to-mark)
|
|
70 (define-key view-mode-map "@" 'View-back-to-mark)
|
|
71 (define-key view-mode-map "x" 'exchange-point-and-mark)
|
|
72 (define-key view-mode-map "h" 'Helper-describe-bindings)
|
|
73 (define-key view-mode-map "?" 'Helper-describe-bindings)
|
|
74 (define-key view-mode-map "\C-h" 'Helper-help)
|
|
75 (define-key view-mode-map "\C-n" 'next-line)
|
|
76 (define-key view-mode-map "\C-p" 'previous-line)
|
|
77 (define-key view-mode-map "\C-s" 'isearch-forward)
|
|
78 (define-key view-mode-map "\C-r" 'isearch-backward)
|
|
79 (define-key view-mode-map "s" 'isearch-forward)
|
|
80 (define-key view-mode-map "r" 'isearch-backward)
|
|
81 (define-key view-mode-map "/" 'View-search-regexp-forward)
|
|
82 (define-key view-mode-map "\\" 'View-search-regexp-backward)
|
|
83 ;; This conflicts with the standard binding of isearch-regexp-forward
|
|
84 (define-key view-mode-map "\e\C-s" 'View-search-regexp-forward)
|
|
85 (define-key view-mode-map "\e\C-r" 'View-search-regexp-backward)
|
|
86 (define-key view-mode-map "n" 'View-search-last-regexp-forward)
|
|
87 (define-key view-mode-map "p" 'View-search-last-regexp-backward)
|
|
88 )
|
|
89
|
|
90
|
256
|
91 ;;;###autoload
|
44
|
92 (defun view-file (file-name)
|
|
93 "View FILE in View mode, returning to previous buffer when done.
|
|
94 The usual Emacs commands are not available; instead,
|
|
95 a special set of commands (mostly letters and punctuation)
|
|
96 are defined for moving around in the buffer.
|
|
97 Space scrolls forward, Delete scrolls backward.
|
|
98 For list of all View commands, type ? or h while viewing.
|
|
99
|
|
100 Calls the value of view-hook if that is non-nil."
|
|
101 (interactive "fView file: ")
|
|
102 (let ((old-buf (current-buffer))
|
|
103 (had-a-buf (get-file-buffer file-name))
|
|
104 (buf-to-view (find-file-noselect file-name)))
|
|
105 (switch-to-buffer buf-to-view t)
|
|
106 (view-mode old-buf
|
|
107 (and (not had-a-buf) (not (buffer-modified-p buf-to-view))
|
|
108 'kill-buffer))))
|
|
109
|
256
|
110 ;;;###autoload
|
354
|
111 (defun view-file-other-window (file-name)
|
|
112 "View FILE in View mode in other window.
|
|
113 Return to previous buffer when done.
|
|
114 The usual Emacs commands are not available; instead,
|
|
115 a special set of commands (mostly letters and punctuation)
|
|
116 are defined for moving around in the buffer.
|
|
117 Space scrolls forward, Delete scrolls backward.
|
|
118 For list of all View commands, type ? or h while viewing.
|
|
119
|
|
120 Calls the value of view-hook if that is non-nil."
|
|
121 (interactive "fView file: ")
|
|
122 (let ((old-arrangement (current-window-configuration))
|
|
123 (had-a-buf (get-file-buffer file-name))
|
|
124 (buf-to-view (find-file-noselect file-name)))
|
|
125 (switch-to-buffer-other-window buf-to-view)
|
|
126 (view-mode old-arrangement
|
|
127 (and (not had-a-buf) (not (buffer-modified-p buf-to-view))
|
|
128 'kill-buffer))))
|
|
129
|
|
130 ;;;###autoload
|
44
|
131 (defun view-buffer (buffer-name)
|
|
132 "View BUFFER in View mode, returning to previous buffer when done.
|
|
133 The usual Emacs commands are not available; instead,
|
|
134 a special set of commands (mostly letters and punctuation)
|
|
135 are defined for moving around in the buffer.
|
|
136 Space scrolls forward, Delete scrolls backward.
|
|
137 For list of all View commands, type ? or h while viewing.
|
|
138
|
|
139 Calls the value of view-hook if that is non-nil."
|
|
140 (interactive "bView buffer: ")
|
|
141 (let ((old-buf (current-buffer)))
|
|
142 (switch-to-buffer buffer-name t)
|
|
143 (view-mode old-buf nil)))
|
|
144
|
256
|
145 ;;;###autoload
|
354
|
146 (defun view-buffer-other-window (buffer-name not-return)
|
|
147 "View BUFFER in View mode in another window,
|
|
148 returning to original buffer when done ONLY if
|
|
149 prefix argument not-return is nil (as by default).
|
|
150 The usual Emacs commands are not available; instead,
|
|
151 a special set of commands (mostly letters and punctuation)
|
|
152 are defined for moving around in the buffer.
|
|
153 Space scrolls forward, Delete scrolls backward.
|
|
154 For list of all View commands, type ? or h while viewing.
|
|
155
|
|
156 Calls the value of view-hook if that is non-nil."
|
|
157 (interactive \"bView buffer:\\nP\")
|
|
158 (let ((return-to (and not-return (current-window-configuration))))
|
|
159 (switch-to-buffer-other-window buffer-name)
|
|
160 (view-mode return-to)))
|
|
161
|
|
162 ;;;###autoload
|
44
|
163 (defun view-mode (&optional prev-buffer action)
|
|
164 "Major mode for viewing text but not editing it.
|
|
165 Letters do not insert themselves. Instead these commands are provided.
|
|
166 Most commands take prefix arguments. Commands dealing with lines
|
|
167 default to \"scroll size\" lines (initially size of window).
|
|
168 Search commands default to a repeat count of one.
|
|
169 M-< or < move to beginning of buffer.
|
|
170 M-> or > move to end of buffer.
|
|
171 C-v or Space scroll forward lines.
|
|
172 M-v or DEL scroll backward lines.
|
|
173 CR or LF scroll forward one line (backward with prefix argument).
|
|
174 z like Space except set number of lines for further
|
|
175 scrolling commands to scroll by.
|
|
176 C-u and Digits provide prefix arguments. `-' denotes negative argument.
|
|
177 = prints the current line number.
|
|
178 g goes to line given by prefix argument.
|
|
179 / or M-C-s searches forward for regular expression
|
|
180 \\ or M-C-r searches backward for regular expression.
|
|
181 n searches forward for last regular expression.
|
|
182 p searches backward for last regular expression.
|
|
183 C-@ or . set the mark.
|
|
184 x exchanges point and mark.
|
|
185 C-s or s do forward incremental search.
|
|
186 C-r or r do reverse incremental search.
|
|
187 @ or ' return to mark and pops mark ring.
|
|
188 Mark ring is pushed at start of every
|
|
189 successful search and when jump to line to occurs.
|
|
190 The mark is set on jump to buffer start or end.
|
|
191 ? or h provide help message (list of commands).
|
|
192 C-h provides help (list of commands or description of a command).
|
|
193 C-n moves down lines vertically.
|
|
194 C-p moves upward lines vertically.
|
|
195 C-l recenters the screen.
|
|
196 q or C-c exit view-mode and return to previous buffer.
|
|
197
|
|
198 Entry to this mode calls the value of view-hook if non-nil.
|
|
199 \\{view-mode-map}"
|
|
200 ; Not interactive because dangerous things happen
|
|
201 ; if you call it without passing a buffer as argument
|
|
202 ; and they are not easy to fix.
|
|
203 ; (interactive)
|
|
204 (make-local-variable 'view-old-mode-line-buffer-identification)
|
|
205 (setq view-old-mode-line-buffer-identification
|
|
206 mode-line-buffer-identification)
|
|
207 (make-local-variable 'view-old-buffer-read-only)
|
|
208 (setq view-old-buffer-read-only buffer-read-only)
|
|
209 (make-local-variable 'view-old-mode-name)
|
|
210 (setq view-old-mode-name mode-name)
|
|
211 (make-local-variable 'view-old-major-mode)
|
|
212 (setq view-old-major-mode major-mode)
|
|
213 (make-local-variable 'view-old-local-map)
|
|
214 (setq view-old-local-map (current-local-map))
|
|
215 (make-local-variable 'view-old-Helper-return-blurb)
|
|
216 (setq view-old-Helper-return-blurb
|
|
217 (and (boundp 'Helper-return-blurb) Helper-return-blurb))
|
|
218
|
|
219 (setq buffer-read-only t)
|
|
220 (setq mode-line-buffer-identification
|
|
221 (list
|
|
222 (if (buffer-file-name)
|
|
223 "Viewing %f"
|
|
224 "Viewing %b")))
|
|
225 (setq mode-name "View")
|
|
226 (setq major-mode 'view-mode)
|
|
227 (setq Helper-return-blurb
|
|
228 (format "continue viewing %s"
|
|
229 (if (buffer-file-name)
|
|
230 (file-name-nondirectory (buffer-file-name))
|
|
231 (buffer-name))))
|
|
232
|
|
233 (make-local-variable 'view-exit-action)
|
|
234 (setq view-exit-action action)
|
354
|
235 (make-local-variable 'view-return-here)
|
|
236 (setq view-return-here prev-buffer)
|
44
|
237 (make-local-variable 'view-exit-position)
|
|
238 (setq view-exit-position (point-marker))
|
|
239
|
|
240 (make-local-variable 'view-scroll-size)
|
|
241 (setq view-scroll-size nil)
|
|
242 (make-local-variable 'view-last-regexp)
|
|
243 (setq view-last-regexp nil)
|
|
244
|
|
245 (beginning-of-line)
|
|
246 (setq goal-column nil)
|
|
247
|
|
248 (use-local-map view-mode-map)
|
|
249 (run-hooks 'view-hook)
|
|
250 (view-helpful-message))
|
|
251
|
354
|
252
|
44
|
253 (defun view-exit ()
|
|
254 "Exit from view-mode.
|
|
255 If you viewed an existing buffer, that buffer returns to its previous mode.
|
|
256 If you viewed a file that was not present in Emacs, its buffer is killed."
|
|
257 (interactive)
|
|
258 (setq mode-line-buffer-identification
|
|
259 view-old-mode-line-buffer-identification)
|
|
260 (setq major-mode view-old-major-mode)
|
|
261 (setq mode-name view-old-mode-name)
|
|
262 (use-local-map (current-local-map))
|
|
263 (setq buffer-read-only view-old-buffer-read-only)
|
|
264
|
|
265 (goto-char view-exit-position)
|
|
266 (set-marker view-exit-position nil)
|
|
267
|
|
268 ;; Now do something to the buffer that we were viewing
|
|
269 ;; (such as kill it).
|
|
270 (let ((viewed-buffer (current-buffer))
|
|
271 (action view-exit-action))
|
354
|
272 (cond
|
|
273 ((bufferp view-return-here)
|
|
274 (switch-to-buffer view-return-here))
|
|
275 ((window-configuration-p view-return-here)
|
|
276 (set-window-configuration view-return-here)))
|
44
|
277 (if action (funcall action viewed-buffer))))
|
|
278
|
|
279 (defun view-helpful-message ()
|
|
280 (message
|
|
281 (if (and (eq (key-binding "\C-h") 'Helper-help)
|
|
282 (eq (key-binding "?") 'Helper-describe-bindings)
|
|
283 (eq (key-binding "\C-c") 'view-exit))
|
|
284 "Type C-h for help, ? for commands, C-c to quit"
|
|
285 (substitute-command-keys
|
|
286 "Type \\[Helper-help] for help, \\[Helper-describe-bindings] for commands, \\[exit-recursive-edit] to quit."))))
|
|
287
|
|
288 (defun View-undefined ()
|
|
289 (interactive)
|
|
290 (ding)
|
|
291 (view-helpful-message))
|
|
292
|
|
293 (defun view-window-size () (1- (window-height)))
|
|
294
|
|
295 (defun view-scroll-size ()
|
|
296 (min (view-window-size) (or view-scroll-size (view-window-size))))
|
|
297
|
|
298 (defvar view-hook nil
|
|
299 "If non-nil, its value is called when viewing buffer or file.")
|
|
300
|
|
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))
|
|
305
|
|
306 ;(defun View-repeat-last-command ()
|
|
307 ; "Repeat last command issued in View mode."
|
|
308 ; (interactive)
|
|
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))
|
|
313
|
|
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
|
|
317 mark ring."
|
|
318 (interactive "p")
|
|
319 (push-mark)
|
|
320 (goto-line (or line 1))
|
|
321 (recenter (/ (view-window-size) 2)))
|
|
322
|
|
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."
|
|
327 (interactive "P")
|
|
328 (if (pos-visible-in-window-p (point-max))
|
|
329 (exit-recursive-edit))
|
|
330 (setq lines
|
|
331 (if lines (prefix-numeric-value lines)
|
|
332 (view-scroll-size)))
|
|
333 ; (view-last-command 'View-scroll-lines-forward lines)
|
|
334 (if (>= lines (view-window-size))
|
|
335 (scroll-up nil)
|
|
336 (if (>= (- lines) (view-window-size))
|
|
337 (scroll-down nil)
|
|
338 (scroll-up lines)))
|
|
339 (cond ((pos-visible-in-window-p (point-max))
|
|
340 (goto-char (point-max))
|
|
341 (recenter -1)
|
|
342 (message (substitute-command-keys
|
|
343 "End. Type \\[exit-recursive-edit] to quit viewing."))))
|
|
344 (move-to-window-line -1)
|
|
345 (beginning-of-line))
|
|
346
|
|
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."
|
|
354 (interactive "P")
|
|
355 (if (not lines)
|
|
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))
|
|
361
|
|
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."
|
|
365 (interactive "P")
|
|
366 (View-scroll-lines-forward (if (not arg) 1 -1)))
|
|
367
|
|
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."
|
|
372 (interactive "P")
|
|
373 (View-scroll-lines-forward (if lines
|
|
374 (- (prefix-numeric-value lines))
|
|
375 (- (view-scroll-size)))))
|
|
376
|
|
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)
|
|
383 (progn
|
|
384 ;(view-last-command 'View-search-last-regexp-forward times)
|
|
385 (view-search times regexp))))
|
|
386
|
|
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))
|
|
393
|
|
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."
|
|
398 (interactive "p")
|
|
399 (View-search-regexp-forward times view-last-regexp))
|
|
400
|
|
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
|
|
404 pushes mark ring."
|
|
405 (interactive "p")
|
|
406 (View-search-regexp-backward times view-last-regexp))
|
|
407
|
|
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."
|
|
412 (interactive)
|
|
413 (goto-char (or (mark) (point-min)))
|
|
414 (pop-mark)
|
|
415 (recenter (/ (view-window-size) 2)))
|
|
416
|
|
417 (defun view-search (times regexp)
|
|
418 (setq view-last-regexp regexp)
|
|
419 (let (where)
|
|
420 (save-excursion
|
|
421 (move-to-window-line (if (< times 0) 0 -1))
|
|
422 (if (re-search-forward regexp nil t times)
|
|
423 (setq where (point))))
|
|
424 (if where
|
|
425 (progn
|
|
426 (push-mark)
|
|
427 (goto-char where)
|
|
428 (beginning-of-line)
|
|
429 (recenter (/ (view-window-size) 2)))
|
|
430 (message "Can't find occurrence %d of %s" times regexp)
|
|
431 (sit-for 4))))
|
|
432
|
354
|
433
|
|
434 ;;;###autoload
|
|
435 (define-key ctl-x-map "v" 'view-file)
|
|
436
|
|
437 ;;;###autoload
|
|
438 (define-key ctl-x-4-map "v" 'view-file-other-window)
|
584
|
439
|
|
440 (provide 'view)
|
|
441
|