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 (provide 'view)
|
|
23
|
|
24 (defvar view-mode-map nil)
|
|
25 (if view-mode-map
|
|
26 nil
|
|
27 (setq view-mode-map (make-keymap))
|
|
28 (fillarray view-mode-map 'View-undefined)
|
|
29 (define-key view-mode-map "\C-c" 'view-exit)
|
|
30 (define-key view-mode-map "\C-z" 'suspend-emacs)
|
|
31 (define-key view-mode-map "q" 'view-exit)
|
|
32 (define-key view-mode-map "-" 'negative-argument)
|
|
33 (define-key view-mode-map "0" 'digit-argument)
|
|
34 (define-key view-mode-map "1" 'digit-argument)
|
|
35 (define-key view-mode-map "2" 'digit-argument)
|
|
36 (define-key view-mode-map "3" 'digit-argument)
|
|
37 (define-key view-mode-map "4" 'digit-argument)
|
|
38 (define-key view-mode-map "5" 'digit-argument)
|
|
39 (define-key view-mode-map "6" 'digit-argument)
|
|
40 (define-key view-mode-map "7" 'digit-argument)
|
|
41 (define-key view-mode-map "8" 'digit-argument)
|
|
42 (define-key view-mode-map "9" 'digit-argument)
|
|
43 (define-key view-mode-map "\C-u" 'universal-argument)
|
|
44 (define-key view-mode-map "\e" nil)
|
|
45 (define-key view-mode-map "\C-x" 'Control-X-prefix)
|
|
46 (define-key view-mode-map "\e-" 'negative-argument)
|
|
47 (define-key view-mode-map "\e0" 'digit-argument)
|
|
48 (define-key view-mode-map "\e1" 'digit-argument)
|
|
49 (define-key view-mode-map "\e2" 'digit-argument)
|
|
50 (define-key view-mode-map "\e3" 'digit-argument)
|
|
51 (define-key view-mode-map "\e4" 'digit-argument)
|
|
52 (define-key view-mode-map "\e5" 'digit-argument)
|
|
53 (define-key view-mode-map "\e6" 'digit-argument)
|
|
54 (define-key view-mode-map "\e7" 'digit-argument)
|
|
55 (define-key view-mode-map "\e8" 'digit-argument)
|
|
56 (define-key view-mode-map "\e9" 'digit-argument)
|
|
57 (define-key view-mode-map "<" 'beginning-of-buffer)
|
|
58 (define-key view-mode-map ">" 'end-of-buffer)
|
|
59 (define-key view-mode-map "\ev" 'View-scroll-lines-backward)
|
|
60 (define-key view-mode-map "\C-v" 'View-scroll-lines-forward)
|
|
61 (define-key view-mode-map " " 'View-scroll-lines-forward)
|
|
62 (define-key view-mode-map "\177" 'View-scroll-lines-backward)
|
|
63 (define-key view-mode-map "\n" 'View-scroll-one-more-line)
|
|
64 (define-key view-mode-map "\r" 'View-scroll-one-more-line)
|
|
65 (define-key view-mode-map "\C-l" 'recenter)
|
|
66 (define-key view-mode-map "z" 'View-scroll-lines-forward-set-scroll-size)
|
|
67 (define-key view-mode-map "g" 'View-goto-line)
|
|
68 (define-key view-mode-map "=" 'what-line)
|
|
69 (define-key view-mode-map "." 'set-mark-command)
|
|
70 (define-key view-mode-map "\C-@" 'set-mark-command)
|
|
71 (define-key view-mode-map "'" 'View-back-to-mark)
|
|
72 (define-key view-mode-map "@" 'View-back-to-mark)
|
|
73 (define-key view-mode-map "x" 'exchange-point-and-mark)
|
|
74 (define-key view-mode-map "h" 'Helper-describe-bindings)
|
|
75 (define-key view-mode-map "?" 'Helper-describe-bindings)
|
|
76 (define-key view-mode-map "\C-h" 'Helper-help)
|
|
77 (define-key view-mode-map "\C-n" 'next-line)
|
|
78 (define-key view-mode-map "\C-p" 'previous-line)
|
|
79 (define-key view-mode-map "\C-s" 'isearch-forward)
|
|
80 (define-key view-mode-map "\C-r" 'isearch-backward)
|
|
81 (define-key view-mode-map "s" 'isearch-forward)
|
|
82 (define-key view-mode-map "r" 'isearch-backward)
|
|
83 (define-key view-mode-map "/" 'View-search-regexp-forward)
|
|
84 (define-key view-mode-map "\\" 'View-search-regexp-backward)
|
|
85 ;; This conflicts with the standard binding of isearch-regexp-forward
|
|
86 (define-key view-mode-map "\e\C-s" 'View-search-regexp-forward)
|
|
87 (define-key view-mode-map "\e\C-r" 'View-search-regexp-backward)
|
|
88 (define-key view-mode-map "n" 'View-search-last-regexp-forward)
|
|
89 (define-key view-mode-map "p" 'View-search-last-regexp-backward)
|
|
90 )
|
|
91
|
|
92
|
256
|
93 ;;;###autoload
|
44
|
94 (defun view-file (file-name)
|
|
95 "View FILE in View mode, returning to previous buffer when done.
|
|
96 The usual Emacs commands are not available; instead,
|
|
97 a special set of commands (mostly letters and punctuation)
|
|
98 are defined for moving around in the buffer.
|
|
99 Space scrolls forward, Delete scrolls backward.
|
|
100 For list of all View commands, type ? or h while viewing.
|
|
101
|
|
102 Calls the value of view-hook if that is non-nil."
|
|
103 (interactive "fView file: ")
|
|
104 (let ((old-buf (current-buffer))
|
|
105 (had-a-buf (get-file-buffer file-name))
|
|
106 (buf-to-view (find-file-noselect file-name)))
|
|
107 (switch-to-buffer buf-to-view t)
|
|
108 (view-mode old-buf
|
|
109 (and (not had-a-buf) (not (buffer-modified-p buf-to-view))
|
|
110 'kill-buffer))))
|
|
111
|
256
|
112 ;;;###autoload
|
44
|
113 (defun view-buffer (buffer-name)
|
|
114 "View BUFFER in View mode, returning to previous buffer when done.
|
|
115 The usual Emacs commands are not available; instead,
|
|
116 a special set of commands (mostly letters and punctuation)
|
|
117 are defined for moving around in the buffer.
|
|
118 Space scrolls forward, Delete scrolls backward.
|
|
119 For list of all View commands, type ? or h while viewing.
|
|
120
|
|
121 Calls the value of view-hook if that is non-nil."
|
|
122 (interactive "bView buffer: ")
|
|
123 (let ((old-buf (current-buffer)))
|
|
124 (switch-to-buffer buffer-name t)
|
|
125 (view-mode old-buf nil)))
|
|
126
|
256
|
127 ;;;###autoload
|
44
|
128 (defun view-mode (&optional prev-buffer action)
|
|
129 "Major mode for viewing text but not editing it.
|
|
130 Letters do not insert themselves. Instead these commands are provided.
|
|
131 Most commands take prefix arguments. Commands dealing with lines
|
|
132 default to \"scroll size\" lines (initially size of window).
|
|
133 Search commands default to a repeat count of one.
|
|
134 M-< or < move to beginning of buffer.
|
|
135 M-> or > move to end of buffer.
|
|
136 C-v or Space scroll forward lines.
|
|
137 M-v or DEL scroll backward lines.
|
|
138 CR or LF scroll forward one line (backward with prefix argument).
|
|
139 z like Space except set number of lines for further
|
|
140 scrolling commands to scroll by.
|
|
141 C-u and Digits provide prefix arguments. `-' denotes negative argument.
|
|
142 = prints the current line number.
|
|
143 g goes to line given by prefix argument.
|
|
144 / or M-C-s searches forward for regular expression
|
|
145 \\ or M-C-r searches backward for regular expression.
|
|
146 n searches forward for last regular expression.
|
|
147 p searches backward for last regular expression.
|
|
148 C-@ or . set the mark.
|
|
149 x exchanges point and mark.
|
|
150 C-s or s do forward incremental search.
|
|
151 C-r or r do reverse incremental search.
|
|
152 @ or ' return to mark and pops mark ring.
|
|
153 Mark ring is pushed at start of every
|
|
154 successful search and when jump to line to occurs.
|
|
155 The mark is set on jump to buffer start or end.
|
|
156 ? or h provide help message (list of commands).
|
|
157 C-h provides help (list of commands or description of a command).
|
|
158 C-n moves down lines vertically.
|
|
159 C-p moves upward lines vertically.
|
|
160 C-l recenters the screen.
|
|
161 q or C-c exit view-mode and return to previous buffer.
|
|
162
|
|
163 Entry to this mode calls the value of view-hook if non-nil.
|
|
164 \\{view-mode-map}"
|
|
165 ; Not interactive because dangerous things happen
|
|
166 ; if you call it without passing a buffer as argument
|
|
167 ; and they are not easy to fix.
|
|
168 ; (interactive)
|
|
169 (make-local-variable 'view-old-mode-line-buffer-identification)
|
|
170 (setq view-old-mode-line-buffer-identification
|
|
171 mode-line-buffer-identification)
|
|
172 (make-local-variable 'view-old-buffer-read-only)
|
|
173 (setq view-old-buffer-read-only buffer-read-only)
|
|
174 (make-local-variable 'view-old-mode-name)
|
|
175 (setq view-old-mode-name mode-name)
|
|
176 (make-local-variable 'view-old-major-mode)
|
|
177 (setq view-old-major-mode major-mode)
|
|
178 (make-local-variable 'view-old-local-map)
|
|
179 (setq view-old-local-map (current-local-map))
|
|
180 (make-local-variable 'view-old-Helper-return-blurb)
|
|
181 (setq view-old-Helper-return-blurb
|
|
182 (and (boundp 'Helper-return-blurb) Helper-return-blurb))
|
|
183
|
|
184 (setq buffer-read-only t)
|
|
185 (setq mode-line-buffer-identification
|
|
186 (list
|
|
187 (if (buffer-file-name)
|
|
188 "Viewing %f"
|
|
189 "Viewing %b")))
|
|
190 (setq mode-name "View")
|
|
191 (setq major-mode 'view-mode)
|
|
192 (setq Helper-return-blurb
|
|
193 (format "continue viewing %s"
|
|
194 (if (buffer-file-name)
|
|
195 (file-name-nondirectory (buffer-file-name))
|
|
196 (buffer-name))))
|
|
197
|
|
198 (make-local-variable 'view-exit-action)
|
|
199 (setq view-exit-action action)
|
|
200 (make-local-variable 'view-prev-buffer)
|
|
201 (setq view-prev-buffer prev-buffer)
|
|
202 (make-local-variable 'view-exit-position)
|
|
203 (setq view-exit-position (point-marker))
|
|
204
|
|
205 (make-local-variable 'view-scroll-size)
|
|
206 (setq view-scroll-size nil)
|
|
207 (make-local-variable 'view-last-regexp)
|
|
208 (setq view-last-regexp nil)
|
|
209
|
|
210 (beginning-of-line)
|
|
211 (setq goal-column nil)
|
|
212
|
|
213 (use-local-map view-mode-map)
|
|
214 (run-hooks 'view-hook)
|
|
215 (view-helpful-message))
|
|
216
|
|
217 (defun view-exit ()
|
|
218 "Exit from view-mode.
|
|
219 If you viewed an existing buffer, that buffer returns to its previous mode.
|
|
220 If you viewed a file that was not present in Emacs, its buffer is killed."
|
|
221 (interactive)
|
|
222 (setq mode-line-buffer-identification
|
|
223 view-old-mode-line-buffer-identification)
|
|
224 (setq major-mode view-old-major-mode)
|
|
225 (setq mode-name view-old-mode-name)
|
|
226 (use-local-map (current-local-map))
|
|
227 (setq buffer-read-only view-old-buffer-read-only)
|
|
228
|
|
229 (goto-char view-exit-position)
|
|
230 (set-marker view-exit-position nil)
|
|
231
|
|
232 ;; Now do something to the buffer that we were viewing
|
|
233 ;; (such as kill it).
|
|
234 (let ((viewed-buffer (current-buffer))
|
|
235 (action view-exit-action))
|
|
236 (switch-to-buffer view-prev-buffer)
|
|
237 (if action (funcall action viewed-buffer))))
|
|
238
|
|
239 (defun view-helpful-message ()
|
|
240 (message
|
|
241 (if (and (eq (key-binding "\C-h") 'Helper-help)
|
|
242 (eq (key-binding "?") 'Helper-describe-bindings)
|
|
243 (eq (key-binding "\C-c") 'view-exit))
|
|
244 "Type C-h for help, ? for commands, C-c to quit"
|
|
245 (substitute-command-keys
|
|
246 "Type \\[Helper-help] for help, \\[Helper-describe-bindings] for commands, \\[exit-recursive-edit] to quit."))))
|
|
247
|
|
248 (defun View-undefined ()
|
|
249 (interactive)
|
|
250 (ding)
|
|
251 (view-helpful-message))
|
|
252
|
|
253 (defun view-window-size () (1- (window-height)))
|
|
254
|
|
255 (defun view-scroll-size ()
|
|
256 (min (view-window-size) (or view-scroll-size (view-window-size))))
|
|
257
|
|
258 (defvar view-hook nil
|
|
259 "If non-nil, its value is called when viewing buffer or file.")
|
|
260
|
|
261 ;(defun view-last-command (&optional who what)
|
|
262 ; (setq view-last-command-entry this-command)
|
|
263 ; (setq view-last-command who)
|
|
264 ; (setq view-last-command-argument what))
|
|
265
|
|
266 ;(defun View-repeat-last-command ()
|
|
267 ; "Repeat last command issued in View mode."
|
|
268 ; (interactive)
|
|
269 ; (if (and view-last-command
|
|
270 ; (eq view-last-command-entry last-command))
|
|
271 ; (funcall view-last-command view-last-command-argument))
|
|
272 ; (setq this-command view-last-command-entry))
|
|
273
|
|
274 (defun View-goto-line (&optional line)
|
|
275 "Move to LINE in View mode.
|
|
276 Display is centered at LINE. Sets mark at starting position and pushes
|
|
277 mark ring."
|
|
278 (interactive "p")
|
|
279 (push-mark)
|
|
280 (goto-line (or line 1))
|
|
281 (recenter (/ (view-window-size) 2)))
|
|
282
|
|
283 (defun View-scroll-lines-forward (&optional lines)
|
|
284 "Scroll forward in View mode, or exit if end of text is visible.
|
|
285 No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size].
|
|
286 Arg is number of lines to scroll."
|
|
287 (interactive "P")
|
|
288 (if (pos-visible-in-window-p (point-max))
|
|
289 (exit-recursive-edit))
|
|
290 (setq lines
|
|
291 (if lines (prefix-numeric-value lines)
|
|
292 (view-scroll-size)))
|
|
293 ; (view-last-command 'View-scroll-lines-forward lines)
|
|
294 (if (>= lines (view-window-size))
|
|
295 (scroll-up nil)
|
|
296 (if (>= (- lines) (view-window-size))
|
|
297 (scroll-down nil)
|
|
298 (scroll-up lines)))
|
|
299 (cond ((pos-visible-in-window-p (point-max))
|
|
300 (goto-char (point-max))
|
|
301 (recenter -1)
|
|
302 (message (substitute-command-keys
|
|
303 "End. Type \\[exit-recursive-edit] to quit viewing."))))
|
|
304 (move-to-window-line -1)
|
|
305 (beginning-of-line))
|
|
306
|
|
307 (defun View-scroll-lines-forward-set-scroll-size (&optional lines)
|
|
308 "Scroll forward LINES lines in View mode, setting the \"scroll size\".
|
|
309 This is the number of lines which \\[View-scroll-lines-forward] and \\[View-scroll-lines-backward] scroll by default.
|
|
310 The absolute value of LINES is used, so this command can be used to scroll
|
|
311 backwards (but \"scroll size\" is always positive). If LINES is greater than
|
|
312 window height or omitted, then window height is assumed. If LINES is less
|
|
313 than window height then scrolling context is provided from previous screen."
|
|
314 (interactive "P")
|
|
315 (if (not lines)
|
|
316 (setq view-scroll-size (view-window-size))
|
|
317 (setq lines (prefix-numeric-value lines))
|
|
318 (setq view-scroll-size
|
|
319 (min (if (> lines 0) lines (- lines)) (view-window-size))))
|
|
320 (View-scroll-lines-forward lines))
|
|
321
|
|
322 (defun View-scroll-one-more-line (&optional arg)
|
|
323 "Scroll one more line up in View mode.
|
|
324 With ARG scroll one line down."
|
|
325 (interactive "P")
|
|
326 (View-scroll-lines-forward (if (not arg) 1 -1)))
|
|
327
|
|
328 (defun View-scroll-lines-backward (&optional lines)
|
|
329 "Scroll backward in View mode.
|
|
330 No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size].
|
|
331 Arg is number of lines to scroll."
|
|
332 (interactive "P")
|
|
333 (View-scroll-lines-forward (if lines
|
|
334 (- (prefix-numeric-value lines))
|
|
335 (- (view-scroll-size)))))
|
|
336
|
|
337 (defun View-search-regexp-forward (times regexp)
|
|
338 "Search forward for NTH occurrence of REGEXP in View mode.
|
|
339 Displays line found at center of window. REGEXP is remembered for
|
|
340 searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward]. Sets mark at starting position and pushes mark ring."
|
|
341 (interactive "p\nsSearch forward (regexp): ")
|
|
342 (if (> (length regexp) 0)
|
|
343 (progn
|
|
344 ;(view-last-command 'View-search-last-regexp-forward times)
|
|
345 (view-search times regexp))))
|
|
346
|
|
347 (defun View-search-regexp-backward (times regexp)
|
|
348 "Search backward from window start for NTH instance of REGEXP in View mode.
|
|
349 Displays line found at center of window. REGEXP is remembered for
|
|
350 searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward]. Sets mark at starting position and pushes mark ring."
|
|
351 (interactive "p\nsSearch backward (regexp): ")
|
|
352 (View-search-regexp-forward (- times) regexp))
|
|
353
|
|
354 (defun View-search-last-regexp-forward (times)
|
|
355 "Search forward from window end for NTH instance of last regexp in View mode.
|
|
356 Displays line found at center of window. Sets mark at starting position
|
|
357 and pushes mark ring."
|
|
358 (interactive "p")
|
|
359 (View-search-regexp-forward times view-last-regexp))
|
|
360
|
|
361 (defun View-search-last-regexp-backward (times)
|
|
362 "Search backward from window start for NTH instance of last regexp in View mode.
|
|
363 Displays line found at center of window. Sets mark at starting position and
|
|
364 pushes mark ring."
|
|
365 (interactive "p")
|
|
366 (View-search-regexp-backward times view-last-regexp))
|
|
367
|
|
368 (defun View-back-to-mark (&optional ignore)
|
|
369 "Return to last mark set in View mode, else beginning of file.
|
|
370 Displays line at center of window. Pops mark ring so successive
|
|
371 invocations return to earlier marks."
|
|
372 (interactive)
|
|
373 (goto-char (or (mark) (point-min)))
|
|
374 (pop-mark)
|
|
375 (recenter (/ (view-window-size) 2)))
|
|
376
|
|
377 (defun view-search (times regexp)
|
|
378 (setq view-last-regexp regexp)
|
|
379 (let (where)
|
|
380 (save-excursion
|
|
381 (move-to-window-line (if (< times 0) 0 -1))
|
|
382 (if (re-search-forward regexp nil t times)
|
|
383 (setq where (point))))
|
|
384 (if where
|
|
385 (progn
|
|
386 (push-mark)
|
|
387 (goto-char where)
|
|
388 (beginning-of-line)
|
|
389 (recenter (/ (view-window-size) 2)))
|
|
390 (message "Can't find occurrence %d of %s" times regexp)
|
|
391 (sit-for 4))))
|
|
392
|