comparison lisp/modes/view.el @ 0:376386a54a3c r19-14

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