0
|
1 ;;; view.el --- peruse file or buffer without editing.
|
|
2
|
|
3 ;; Copyright (C) 1985, 1989, 1994, 1995 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: K. Shane Hartman
|
|
6 ;; Keywords: wp unix
|
|
7 ;; Maintainer: FSF
|
|
8
|
2
|
9 ;; This file is part of XEmacs.
|
0
|
10
|
2
|
11 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
12 ;; under the terms of the GNU General Public License as published by
|
0
|
13 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
14 ;; any later version.
|
|
15
|
2
|
16 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
19 ;; General Public License for more details.
|
0
|
20
|
|
21 ;; You should have received a copy of the GNU General Public License
|
2
|
22 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
23 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
24 ;; 02111-1307, USA.
|
0
|
25
|
2
|
26 ;;; Synched up with: FSF 19.34.
|
0
|
27
|
|
28 ;;; Commentary:
|
|
29
|
|
30 ;; This package provides the `view' minor mode documented in the Emacs
|
|
31 ;; user's manual.
|
|
32
|
|
33 ;; XEmacs: We don't autoload this because we use `view-less' instead.
|
|
34 ;; #### I junked the old version and replaced it with FSF's latest version.
|
|
35 ;; Perhaps something needs integrating into view-less.
|
|
36
|
|
37 ;;; Code:
|
|
38
|
|
39 (defvar view-highlight-face 'highlight
|
2
|
40 ;; XEmacs change
|
0
|
41 "*The extent face used for highlighting the match found by View mode search.")
|
|
42
|
|
43 (defvar view-mode nil "Non-nil if View mode is enabled.")
|
|
44 (make-variable-buffer-local 'view-mode)
|
|
45
|
|
46 (defvar view-mode-auto-exit nil
|
2
|
47 "Non-nil means scrolling past the end of buffer exits View mode.
|
|
48 Some commands, such as \\[view-file], set this to t locally;
|
|
49 +the only way to override that is to set it to nil using `view-mode-hook'.")
|
|
50
|
0
|
51 (make-variable-buffer-local 'view-mode-auto-exit)
|
|
52
|
|
53 (defvar view-old-buffer-read-only nil)
|
|
54 (make-variable-buffer-local 'view-old-buffer-read-only)
|
|
55 (defvar view-old-Helper-return-blurb)
|
|
56 (make-variable-buffer-local 'view-old-Helper-return-blurb)
|
|
57
|
|
58 (defvar view-scroll-size nil)
|
|
59 (make-variable-buffer-local 'view-scroll-size)
|
|
60
|
|
61 (defvar view-last-regexp nil)
|
|
62 (make-variable-buffer-local 'view-last-regexp)
|
|
63
|
|
64 (defvar view-exit-action nil)
|
|
65 (make-variable-buffer-local 'view-exit-action)
|
|
66 (defvar view-return-here nil)
|
|
67 (make-variable-buffer-local 'view-return-here)
|
|
68 (defvar view-exit-position nil)
|
|
69 (make-variable-buffer-local 'view-exit-position)
|
|
70
|
2
|
71 ;; XEmacs change
|
0
|
72 (defvar view-extent nil
|
|
73 "Extent used to display where a search operation found its match.
|
|
74 This is local in each buffer, once it is used.")
|
|
75 (make-variable-buffer-local 'view-extent)
|
|
76
|
|
77 (or (assq 'view-mode minor-mode-alist)
|
|
78 (setq minor-mode-alist
|
|
79 (cons '(view-mode " View") minor-mode-alist)))
|
|
80
|
|
81 (defvar view-mode-map nil)
|
|
82 (if view-mode-map
|
|
83 nil
|
|
84 (setq view-mode-map (make-keymap 'view-mode-map))
|
2
|
85 ;; We used to call suppress-keymap here, but that isn't good in a minor mode.
|
|
86 ;; Self-inserting characters will beep anyway, since the buffer is read-only,
|
|
87 ;; and we should not interfere with letters that serve as useful commands.
|
0
|
88 (define-key view-mode-map "q" 'view-exit)
|
|
89 (define-key view-mode-map "<" 'beginning-of-buffer)
|
|
90 (define-key view-mode-map ">" 'end-of-buffer)
|
|
91 (define-key view-mode-map "\ev" 'View-scroll-lines-backward)
|
|
92 (define-key view-mode-map "\C-v" 'View-scroll-lines-forward)
|
|
93 (define-key view-mode-map " " 'View-scroll-lines-forward)
|
|
94 (define-key view-mode-map "\C-?" 'View-scroll-lines-backward)
|
|
95 (define-key view-mode-map "\n" 'View-scroll-one-more-line)
|
|
96 (define-key view-mode-map "\r" 'View-scroll-one-more-line)
|
|
97 (define-key view-mode-map "z" 'View-scroll-lines-forward-set-scroll-size)
|
|
98 (define-key view-mode-map "g" 'View-goto-line)
|
|
99 (define-key view-mode-map "=" 'what-line)
|
|
100 (define-key view-mode-map "." 'set-mark-command)
|
|
101 (define-key view-mode-map "'" 'View-back-to-mark)
|
|
102 (define-key view-mode-map "@" 'View-back-to-mark)
|
|
103 (define-key view-mode-map "x" 'exchange-point-and-mark)
|
|
104 (define-key view-mode-map "h" 'describe-mode)
|
|
105 (define-key view-mode-map "?" 'describe-mode)
|
|
106 (define-key view-mode-map "s" 'isearch-forward)
|
|
107 (define-key view-mode-map "r" 'isearch-backward)
|
|
108 (define-key view-mode-map "/" 'View-search-regexp-forward)
|
|
109 (define-key view-mode-map "\\" 'View-search-regexp-backward)
|
|
110 ;; This conflicts with the standard binding of isearch-regexp-forward
|
|
111 (define-key view-mode-map "\e\C-s" 'View-search-regexp-forward)
|
|
112 (define-key view-mode-map "\e\C-r" 'View-search-regexp-backward)
|
|
113 (define-key view-mode-map "n" 'View-search-last-regexp-forward)
|
|
114 (define-key view-mode-map "p" 'View-search-last-regexp-backward)
|
|
115 )
|
|
116
|
|
117 (or (assq 'view-mode minor-mode-map-alist)
|
|
118 (setq minor-mode-map-alist
|
|
119 (cons (cons 'view-mode view-mode-map) minor-mode-map-alist)))
|
|
120
|
|
121
|
|
122 (defun view-file (file-name)
|
|
123 "View FILE in View mode, returning to previous buffer when done.
|
|
124 The usual Emacs commands are not available; instead,
|
|
125 a special set of commands (mostly letters and punctuation)
|
|
126 are defined for moving around in the buffer.
|
|
127 Space scrolls forward, Delete scrolls backward.
|
|
128 For list of all View commands, type ? or h while viewing.
|
|
129
|
|
130 This command runs the normal hook `view-mode-hook'."
|
|
131 (interactive "fView file: ")
|
|
132 (let ((old-buf (current-buffer))
|
|
133 (had-a-buf (get-file-buffer file-name))
|
|
134 (buf-to-view (find-file-noselect file-name)))
|
|
135 ;; This used to pass t as second argument,
|
|
136 ;; but then the buffer did not show up in the Buffers menu.
|
|
137 (switch-to-buffer buf-to-view had-a-buf)
|
|
138 (view-mode-enter old-buf
|
|
139 (and (not had-a-buf) (not (buffer-modified-p buf-to-view))
|
|
140 'kill-buffer))))
|
|
141
|
|
142 (defun view-file-other-window (file-name)
|
|
143 "View FILE in View mode in other window.
|
|
144 Return to previous buffer when done.
|
|
145 The usual Emacs commands are not available; instead,
|
|
146 a special set of commands (mostly letters and punctuation)
|
|
147 are defined for moving around in the buffer.
|
|
148 Space scrolls forward, Delete scrolls backward.
|
|
149 For list of all View commands, type ? or h while viewing.
|
|
150
|
|
151 This command runs the normal hook `view-mode-hook'."
|
|
152 (interactive "fView file: ")
|
|
153 (let ((old-arrangement (current-window-configuration))
|
|
154 (had-a-buf (get-file-buffer file-name))
|
|
155 (buf-to-view (find-file-noselect file-name)))
|
|
156 (switch-to-buffer-other-window buf-to-view)
|
|
157 (view-mode-enter old-arrangement
|
|
158 (and (not had-a-buf) (not (buffer-modified-p buf-to-view))
|
|
159 'kill-buffer))))
|
|
160
|
|
161 (defun view-buffer (buffer-name)
|
|
162 "View BUFFER in View mode, returning to previous buffer when done.
|
|
163 The usual Emacs commands are not available; instead,
|
|
164 a special set of commands (mostly letters and punctuation)
|
|
165 are defined for moving around in the buffer.
|
|
166 Space scrolls forward, Delete scrolls backward.
|
|
167 For list of all View commands, type ? or h while viewing.
|
|
168
|
|
169 This command runs the normal hook `view-mode-hook'."
|
|
170 (interactive "bView buffer: ")
|
|
171 (let ((old-buf (current-buffer)))
|
|
172 (switch-to-buffer buffer-name t)
|
|
173 (view-mode-enter old-buf nil)))
|
|
174
|
|
175 (defun view-buffer-other-window (buffer-name not-return)
|
|
176 "View BUFFER in View mode in another window.
|
|
177 Return to previous buffer when done, unless NOT-RETURN is non-nil.
|
|
178
|
|
179 The usual Emacs commands are not available in View mode; instead,
|
|
180 a special set of commands (mostly letters and punctuation)
|
|
181 are defined for moving around in the buffer.
|
|
182 Space scrolls forward, Delete scrolls backward.
|
|
183 For list of all View commands, type ? or h while viewing.
|
|
184
|
|
185 This command runs the normal hook `view-mode-hook'."
|
|
186 (interactive "bView buffer:\nP")
|
|
187 (let ((return-to (and not-return (current-window-configuration))))
|
|
188 (switch-to-buffer-other-window buffer-name)
|
|
189 (view-mode-enter return-to)))
|
|
190
|
|
191 (defun view-mode (&optional arg)
|
|
192 "Toggle View mode.
|
2
|
193 With a prefix argument, turn View mode on if the argument is >= zero
|
|
194 and off if it is not.
|
|
195
|
|
196 If you use this function to turn on View mode, then subsequently
|
|
197 \"exiting\" View mode does nothing except turn View mode off. The
|
|
198 other way to turn View mode on is by calling `view-mode-enter';
|
|
199 that is what Lisp programs usually use.
|
0
|
200
|
|
201 Letters do not insert themselves. Instead these commands are provided.
|
|
202 Most commands take prefix arguments. Commands dealing with lines
|
|
203 default to \"scroll size\" lines (initially size of window).
|
|
204 Search commands default to a repeat count of one.
|
|
205
|
|
206 M-< or < move to beginning of buffer.
|
|
207 M-> or > move to end of buffer.
|
|
208 C-v or Space scroll forward lines.
|
|
209 M-v or DEL scroll backward lines.
|
|
210 CR or LF scroll forward one line (backward with prefix argument).
|
|
211 z like Space except set number of lines for further
|
|
212 scrolling commands to scroll by.
|
|
213 C-u and Digits provide prefix arguments. `-' denotes negative argument.
|
|
214 = prints the current line number.
|
|
215 g goes to line given by prefix argument.
|
|
216 / or M-C-s searches forward for regular expression
|
|
217 \\ or M-C-r searches backward for regular expression.
|
|
218 n searches forward for last regular expression.
|
|
219 p searches backward for last regular expression.
|
|
220 C-@ or . set the mark.
|
|
221 x exchanges point and mark.
|
|
222 C-s or s do forward incremental search.
|
|
223 C-r or r do reverse incremental search.
|
|
224 @ or ' return to mark and pops mark ring.
|
|
225 Mark ring is pushed at start of every
|
|
226 successful search and when jump to line to occurs.
|
|
227 The mark is set on jump to buffer start or end.
|
|
228 ? or h provide help message (list of commands).
|
|
229 \\[help-command] provides help (list of commands or description of a command).
|
|
230 C-n moves down lines vertically.
|
|
231 C-p moves upward lines vertically.
|
|
232 C-l recenters the screen.
|
|
233 q exit view-mode and return to previous buffer."
|
|
234 (interactive "P")
|
|
235 (setq view-mode
|
|
236 (if (null arg)
|
|
237 (not view-mode)
|
|
238 (> (prefix-numeric-value arg) 0)))
|
|
239 (force-mode-line-update))
|
|
240
|
|
241 (defun view-mode-enter (&optional prev-buffer action)
|
|
242 "Enter View mode, a Minor mode for viewing text but not editing it.
|
|
243 See the function `view-mode' for more details.
|
|
244
|
|
245 This function runs the normal hook `view-mode-hook'.
|
|
246
|
|
247 \\{view-mode-map}"
|
|
248 ; Not interactive because dangerous things happen
|
|
249 ; if you call it without passing a buffer as argument
|
|
250 ; and they are not easy to fix.
|
|
251 ; (interactive)
|
|
252 (setq view-old-buffer-read-only buffer-read-only)
|
|
253 (setq view-old-Helper-return-blurb
|
|
254 (and (boundp 'Helper-return-blurb) Helper-return-blurb))
|
|
255
|
|
256 ;; Enable view-exit to make use of the data we just saved
|
|
257 ;; and to perform the exit action.
|
|
258 (setq view-mode-auto-exit t)
|
|
259
|
|
260 (setq buffer-read-only t)
|
|
261 (setq view-mode t)
|
|
262 (setq Helper-return-blurb
|
|
263 (format "continue viewing %s"
|
|
264 (if (buffer-file-name)
|
|
265 (file-name-nondirectory (buffer-file-name))
|
|
266 (buffer-name))))
|
|
267
|
|
268 (setq view-exit-action action)
|
|
269 (setq view-return-here prev-buffer)
|
|
270 (setq view-exit-position (point-marker))
|
|
271
|
|
272 (beginning-of-line)
|
|
273 (setq goal-column nil)
|
|
274
|
|
275 (run-hooks 'view-mode-hook)
|
|
276 (message
|
|
277 (substitute-command-keys
|
|
278 "Type \\[help-command] for help, \\[describe-mode] for commands, \\[view-exit] to quit.")))
|
|
279
|
|
280 (defun view-exit ()
|
|
281 "Exit from view-mode.
|
|
282 If you viewed an existing buffer, that buffer returns to its previous mode.
|
|
283 If you viewed a file that was not present in Emacs, its buffer is killed."
|
|
284 (interactive)
|
|
285 (setq view-mode nil)
|
2
|
286 ;; XEmacs change
|
0
|
287 (and view-extent (delete-extent view-extent))
|
|
288 (force-mode-line-update)
|
|
289 (cond (view-mode-auto-exit
|
|
290 (setq buffer-read-only view-old-buffer-read-only)
|
|
291 (setq view-mode-auto-exit nil)
|
|
292
|
|
293 (goto-char view-exit-position)
|
|
294 (set-marker view-exit-position nil)
|
|
295
|
|
296 ;; Now do something to the buffer that we were viewing
|
|
297 ;; (such as kill it).
|
|
298 (let ((viewed-buffer (current-buffer))
|
|
299 (action view-exit-action))
|
|
300 (cond
|
|
301 ((bufferp view-return-here)
|
|
302 (switch-to-buffer view-return-here))
|
|
303 ((window-configuration-p view-return-here)
|
|
304 (set-window-configuration view-return-here)))
|
|
305 (if action (funcall action viewed-buffer))))))
|
|
306
|
|
307 (defun view-window-size () (1- (window-height)))
|
|
308
|
|
309 (defun view-scroll-size ()
|
|
310 (min (view-window-size) (or view-scroll-size (view-window-size))))
|
|
311
|
|
312 (defvar view-mode-hook nil
|
|
313 "Normal hook run when starting to view a buffer or file.")
|
|
314
|
|
315 ;(defun view-last-command (&optional who what)
|
|
316 ; (setq view-last-command-entry this-command)
|
|
317 ; (setq view-last-command who)
|
|
318 ; (setq view-last-command-argument what))
|
|
319
|
|
320 ;(defun View-repeat-last-command ()
|
|
321 ; "Repeat last command issued in View mode."
|
|
322 ; (interactive)
|
|
323 ; (if (and view-last-command
|
|
324 ; (eq view-last-command-entry last-command))
|
|
325 ; (funcall view-last-command view-last-command-argument))
|
|
326 ; (setq this-command view-last-command-entry))
|
|
327
|
|
328 (defun View-goto-line (line)
|
|
329 "Move to line LINE in View mode.
|
|
330 Display is centered at LINE. Sets mark at starting position and pushes
|
|
331 mark ring."
|
|
332 (interactive "p")
|
|
333 (push-mark)
|
|
334 (goto-line line)
|
|
335 (recenter (/ (view-window-size) 2)))
|
|
336
|
|
337 (defun View-scroll-lines-forward (&optional lines)
|
|
338 "Scroll forward in View mode, or exit if end of text is visible.
|
|
339 No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size].
|
|
340 Arg is number of lines to scroll."
|
|
341 (interactive "P")
|
|
342 (setq lines
|
|
343 (if lines (prefix-numeric-value lines)
|
|
344 (view-scroll-size)))
|
|
345 (if (and (pos-visible-in-window-p (point-max))
|
|
346 ;; Allow scrolling backward at the end of the buffer.
|
|
347 (> lines 0)
|
|
348 view-mode-auto-exit)
|
|
349 (view-exit)
|
|
350 ;; (view-last-command 'View-scroll-lines-forward lines)
|
|
351 (if (>= lines (view-window-size))
|
|
352 (scroll-up nil)
|
|
353 (if (>= (- lines) (view-window-size))
|
|
354 (scroll-down nil)
|
|
355 (scroll-up lines)))
|
|
356 (cond ((pos-visible-in-window-p (point-max))
|
|
357 (goto-char (point-max))
|
82
|
358 (message "%s"
|
2
|
359 (substitute-command-keys
|
0
|
360 "End. Type \\[view-exit] to quit viewing."))))
|
|
361 (move-to-window-line -1)
|
|
362 (beginning-of-line)))
|
|
363
|
|
364 (defun View-scroll-lines-forward-set-scroll-size (&optional lines)
|
|
365 "Scroll forward LINES lines in View mode, setting the \"scroll size\".
|
|
366 This is the number of lines which \\[View-scroll-lines-forward] and \\[View-scroll-lines-backward] scroll by default.
|
|
367 The absolute value of LINES is used, so this command can be used to scroll
|
|
368 backwards (but \"scroll size\" is always positive). If LINES is greater than
|
|
369 window height or omitted, then window height is assumed. If LINES is less
|
|
370 than window height then scrolling context is provided from previous screen."
|
|
371 (interactive "P")
|
|
372 (if (not lines)
|
|
373 (setq view-scroll-size (view-window-size))
|
|
374 (setq lines (prefix-numeric-value lines))
|
|
375 (setq view-scroll-size
|
|
376 (min (if (> lines 0) lines (- lines)) (view-window-size))))
|
|
377 (View-scroll-lines-forward lines))
|
|
378
|
|
379 (defun View-scroll-one-more-line (&optional arg)
|
|
380 "Scroll one more line up in View mode.
|
|
381 With ARG scroll one line down."
|
|
382 (interactive "P")
|
|
383 (View-scroll-lines-forward (if (not arg) 1 -1)))
|
|
384
|
|
385 (defun View-scroll-lines-backward (&optional lines)
|
|
386 "Scroll backward in View mode.
|
|
387 No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size].
|
|
388 Arg is number of lines to scroll."
|
|
389 (interactive "P")
|
|
390 (View-scroll-lines-forward (if lines
|
|
391 (- (prefix-numeric-value lines))
|
|
392 (- (view-scroll-size)))))
|
|
393
|
|
394 (defun View-search-regexp-forward (n regexp)
|
|
395 "Search forward for Nth occurrence of REGEXP.
|
|
396 Displays line found at center of window. REGEXP is remembered for
|
|
397 searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward]. Sets mark at starting position and pushes mark ring.
|
|
398
|
|
399 The variable `view-highlight-face' controls the face that is used
|
|
400 for highlighting the match that is found."
|
|
401 (interactive "p\nsSearch forward (regexp): ")
|
|
402 ;;;(view-last-command 'View-search-last-regexp-forward n)
|
|
403 (view-search n (if (equal regexp "") view-last-regexp regexp)))
|
|
404
|
|
405 (defun View-search-regexp-backward (n regexp)
|
|
406 "Search backward from window start for Nth instance of REGEXP.
|
|
407 Displays line found at center of window. REGEXP is remembered for
|
|
408 searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward]. Sets mark at starting position and pushes mark ring.
|
|
409
|
|
410 The variable `view-highlight-face' controls the face that is used
|
|
411 for highlighting the match that is found."
|
|
412 (interactive "p\nsSearch backward (regexp): ")
|
|
413 (View-search-regexp-forward (- n)
|
|
414 (if (equal regexp "") view-last-regexp regexp)))
|
|
415
|
|
416 (defun View-search-last-regexp-forward (n)
|
|
417 "Search forward from window end for Nth instance of last regexp.
|
|
418 Displays line found at center of window. Sets mark at starting position
|
|
419 and pushes mark ring.
|
|
420
|
|
421 The variable `view-highlight-face' controls the face that is used
|
|
422 for highlighting the match that is found."
|
|
423 (interactive "p")
|
2
|
424 (if view-last-regexp
|
|
425 (View-search-regexp-forward n view-last-regexp)
|
|
426 (error "No previous View-mode search")))
|
0
|
427
|
|
428 (defun View-search-last-regexp-backward (n)
|
|
429 "Search backward from window start for Nth instance of last regexp.
|
|
430 Displays line found at center of window. Sets mark at starting position and
|
|
431 pushes mark ring.
|
|
432
|
|
433 The variable `view-highlight-face' controls the face that is used
|
|
434 for highlighting the match that is found."
|
|
435 (interactive "p")
|
2
|
436 (if view-last-regexp
|
|
437 (View-search-regexp-backward n view-last-regexp)
|
|
438 (error "No previous View-mode search")))
|
0
|
439
|
|
440 (defun View-back-to-mark (&optional ignore)
|
|
441 "Return to last mark set in View mode, else beginning of file.
|
|
442 Displays line at center of window. Pops mark ring so successive
|
|
443 invocations return to earlier marks."
|
|
444 (interactive)
|
|
445 (goto-char (or (mark t) (point-min)))
|
|
446 (pop-mark)
|
|
447 (recenter (/ (view-window-size) 2)))
|
|
448
|
|
449 (defun view-search (times regexp)
|
|
450 (setq view-last-regexp regexp)
|
|
451 (let (where)
|
|
452 (save-excursion
|
|
453 (move-to-window-line (if (< times 0) 0 -1))
|
|
454 (if (re-search-forward regexp nil t times)
|
|
455 (setq where (point))))
|
|
456 (if where
|
|
457 (progn
|
|
458 (push-mark)
|
|
459 (goto-char where)
|
2
|
460 ;; XEmacs change.
|
0
|
461 (if view-extent
|
|
462 (set-extent-endpoints view-extent (match-beginning 0)
|
|
463 (match-end 0))
|
|
464 (setq view-extent
|
|
465 (make-extent (match-beginning 0) (match-end 0))))
|
|
466 (set-extent-face view-extent view-highlight-face)
|
|
467 (beginning-of-line)
|
|
468 (recenter (/ (view-window-size) 2)))
|
|
469 (message "Can't find occurrence %d of %s" times regexp)
|
|
470 (sit-for 4))))
|
|
471
|
|
472
|
|
473 (provide 'view)
|
|
474
|
|
475 ;;; view.el ends here
|