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