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