(Info-insert-dir): Insert newlines before rather than
[bpt/emacs.git] / lisp / view.el
index 29d5558..d7a1c29 100644 (file)
@@ -1,12 +1,15 @@
-;; View: Peruse file or buffer without editing.
-;; Copyright (C) 1985, 1989 Free Software Foundation, Inc.
-;; Principal author K. Shane Hartman
+;;; view.el --- peruse file or buffer without editing.
+
+;; Copyright (C) 1985, 1989, 1994 Free Software Foundation, Inc.
+
+;; Author: K. Shane Hartman
+;; Maintainer: FSF
 
 ;; This file is part of GNU Emacs.
 
 ;; GNU Emacs is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 1, or (at your option)
+;; the Free Software Foundation; either version 2, or (at your option)
 ;; any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; along with GNU Emacs; see the file COPYING.  If not, write to
 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 
+;;; Commentary:
 
-(provide 'view)
+;; This package provides the `view' major mode documented in the Emacs
+;; user's manual.
+
+;;; Code:
 
 (defvar view-mode-map nil)
 (if view-mode-map
     nil
   (setq view-mode-map (make-keymap))
-  (fillarray (car (cdr view-mode-map)) 'View-undefined)
+  (fillarray (nth 1 view-mode-map) 'View-undefined)
   (define-key view-mode-map "\C-c" 'view-exit)
-  (define-key view-mode-map "\C-z" 'suspend-emacs)
   (define-key view-mode-map "q" 'view-exit)
   (define-key view-mode-map "-" 'negative-argument)
   (define-key view-mode-map "0" 'digit-argument)
@@ -73,7 +79,7 @@
   (define-key view-mode-map "x" 'exchange-point-and-mark)
   (define-key view-mode-map "h" 'Helper-describe-bindings)
   (define-key view-mode-map "?" 'Helper-describe-bindings)
-  (define-key view-mode-map "\C-h" 'Helper-help)
+  (define-key view-mode-map (char-to-string help-char) 'Helper-help)
   (define-key view-mode-map "\C-n" 'next-line)
   (define-key view-mode-map "\C-p" 'previous-line)
   (define-key view-mode-map "\C-s" 'isearch-forward)
@@ -99,7 +105,7 @@ are defined for moving around in the buffer.
 Space scrolls forward, Delete scrolls backward.
 For list of all View commands, type ? or h while viewing.
 
-Calls the value of  view-hook  if that is non-nil."
+This command runs the normal hook `view-mode-hook'."
   (interactive "fView file: ")
   (let ((old-buf (current-buffer))
        (had-a-buf (get-file-buffer file-name))
@@ -109,6 +115,26 @@ Calls the value of  view-hook  if that is non-nil."
               (and (not had-a-buf) (not (buffer-modified-p buf-to-view))
                    'kill-buffer))))
 
+;;;###autoload
+(defun view-file-other-window (file-name)
+  "View FILE in View mode in other window.
+Return to previous buffer when done.
+The usual Emacs commands are not available; instead,
+a special set of commands (mostly letters and punctuation)
+are defined for moving around in the buffer.
+Space scrolls forward, Delete scrolls backward.
+For list of all View commands, type ? or h while viewing.
+
+This command runs the normal hook `view-mode-hook'."
+  (interactive "fView file: ")
+  (let ((old-arrangement (current-window-configuration))
+       (had-a-buf (get-file-buffer file-name))
+       (buf-to-view (find-file-noselect file-name)))
+    (switch-to-buffer-other-window buf-to-view)
+    (view-mode old-arrangement
+              (and (not had-a-buf) (not (buffer-modified-p buf-to-view))
+                   'kill-buffer))))
+
 ;;;###autoload
 (defun view-buffer (buffer-name)
   "View BUFFER in View mode, returning to previous buffer when done.
@@ -118,12 +144,30 @@ are defined for moving around in the buffer.
 Space scrolls forward, Delete scrolls backward.
 For list of all View commands, type ? or h while viewing.
 
-Calls the value of  view-hook  if that is non-nil."
+This command runs the normal hook `view-mode-hook'."
   (interactive "bView buffer: ")
   (let ((old-buf (current-buffer)))
     (switch-to-buffer buffer-name t)
     (view-mode old-buf nil)))
 
+;;;###autoload
+(defun view-buffer-other-window (buffer-name not-return)
+  "View BUFFER in View mode in another window,
+returning to original buffer when done *only* if 
+prefix argument NOT-RETURN is nil (which is the default).
+
+The usual Emacs commands are not available in View mode; instead,
+a special set of commands (mostly letters and punctuation)
+are defined for moving around in the buffer.
+Space scrolls forward, Delete scrolls backward.
+For list of all View commands, type ? or h while viewing.
+
+This command runs the normal hook `view-mode-hook'."
+  (interactive "bView buffer:\nP")
+  (let ((return-to (and not-return (current-window-configuration))))
+    (switch-to-buffer-other-window buffer-name)
+    (view-mode return-to)))
+
 ;;;###autoload
 (defun view-mode (&optional prev-buffer action)
   "Major mode for viewing text but not editing it.
@@ -154,13 +198,14 @@ C-r or r  do reverse incremental search.
                  successful search and when jump to line to occurs.
                  The mark is set on jump to buffer start or end.
 ? or h         provide help message (list of commands).
-C-h            provides help (list of commands or description of a command).
+\\[Helper-help]                provides help (list of commands or description of a command).
 C-n            moves down lines vertically.
 C-p            moves upward lines vertically.
 C-l            recenters the screen.
 q or C-c       exit view-mode and return to previous buffer.
 
-Entry to this mode calls the value of  view-hook  if non-nil.
+Entry to this mode runs the normal hook `view-mode-hook'.
+
 \\{view-mode-map}"
 ;  Not interactive because dangerous things happen
 ;  if you call it without passing a buffer as argument
@@ -197,8 +242,8 @@ Entry to this mode calls the value of  view-hook  if non-nil.
 
   (make-local-variable 'view-exit-action)
   (setq view-exit-action action)
-  (make-local-variable 'view-prev-buffer)
-  (setq view-prev-buffer prev-buffer)
+  (make-local-variable 'view-return-here)
+  (setq view-return-here prev-buffer)
   (make-local-variable 'view-exit-position)
   (setq view-exit-position (point-marker))
 
@@ -211,9 +256,10 @@ Entry to this mode calls the value of  view-hook  if non-nil.
   (setq goal-column nil)
 
   (use-local-map view-mode-map)
-  (run-hooks 'view-hook)
+  (run-hooks 'view-hook 'view-mode-hook)
   (view-helpful-message))
 
+\f
 (defun view-exit ()
   "Exit from view-mode.
 If you viewed an existing buffer, that buffer returns to its previous mode.
@@ -223,7 +269,7 @@ If you viewed a file that was not present in Emacs, its buffer is killed."
        view-old-mode-line-buffer-identification)
   (setq major-mode view-old-major-mode)
   (setq mode-name view-old-mode-name)
-  (use-local-map (current-local-map))
+  (use-local-map view-old-local-map)
   (setq buffer-read-only view-old-buffer-read-only)
 
   (goto-char view-exit-position)
@@ -233,17 +279,17 @@ If you viewed a file that was not present in Emacs, its buffer is killed."
   ;; (such as kill it).
   (let ((viewed-buffer (current-buffer))
        (action view-exit-action))
-    (switch-to-buffer view-prev-buffer)
+    (cond
+     ((bufferp view-return-here)
+      (switch-to-buffer view-return-here))
+     ((window-configuration-p view-return-here)
+      (set-window-configuration view-return-here)))
     (if action (funcall action viewed-buffer))))
 
 (defun view-helpful-message ()
   (message
-   (if (and (eq (key-binding "\C-h") 'Helper-help)
-           (eq (key-binding "?") 'Helper-describe-bindings)
-           (eq (key-binding "\C-c") 'view-exit))
-       "Type C-h for help, ? for commands, C-c to quit"
      (substitute-command-keys
-      "Type \\[Helper-help] for help, \\[Helper-describe-bindings] for commands, \\[exit-recursive-edit] to quit."))))
+      "Type \\[Helper-help] for help, \\[Helper-describe-bindings] for commands, \\[view-exit] to quit.")))
 
 (defun View-undefined ()
   (interactive)
@@ -256,7 +302,10 @@ If you viewed a file that was not present in Emacs, its buffer is killed."
   (min (view-window-size) (or view-scroll-size (view-window-size))))
 
 (defvar view-hook nil
-  "If non-nil, its value is called when viewing buffer or file.")
+  "Normal hook run when starting to view a buffer or file.")
+
+(defvar view-mode-hook nil
+  "Normal hook run when starting to view a buffer or file.")
 
 ;(defun view-last-command (&optional who what)
 ;  (setq view-last-command-entry this-command)
@@ -272,7 +321,7 @@ If you viewed a file that was not present in Emacs, its buffer is killed."
 ;  (setq this-command view-last-command-entry))
 
 (defun View-goto-line (&optional line)
-  "Move to LINE in View mode.
+  "Move to line LINE in View mode.
 Display is centered at LINE.  Sets mark at starting position and pushes
 mark ring."
   (interactive "p")
@@ -285,24 +334,27 @@ mark ring."
 No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size].
 Arg is number of lines to scroll."
   (interactive "P")
-  (if (pos-visible-in-window-p (point-max))
-      (exit-recursive-edit))
-  (setq lines
-       (if lines (prefix-numeric-value lines)
-         (view-scroll-size)))
-; (view-last-command 'View-scroll-lines-forward lines)
-  (if (>= lines (view-window-size))
-      (scroll-up nil)
-    (if (>= (- lines) (view-window-size))
-       (scroll-down nil)
-      (scroll-up lines)))
-  (cond ((pos-visible-in-window-p (point-max))
-        (goto-char (point-max))
-        (recenter -1)
-        (message (substitute-command-keys
-               "End.  Type \\[exit-recursive-edit] to quit viewing."))))
-  (move-to-window-line -1)
-  (beginning-of-line))
+  (if (and (pos-visible-in-window-p (point-max))
+          ;; Allow scrolling backward at the end of the buffer.
+          (or (null lines)
+              (> lines 0)))
+      (view-exit)
+    (setq lines
+         (if lines (prefix-numeric-value lines)
+           (view-scroll-size)))
+    ;; (view-last-command 'View-scroll-lines-forward lines)
+    (if (>= lines (view-window-size))
+       (scroll-up nil)
+      (if (>= (- lines) (view-window-size))
+         (scroll-down nil)
+       (scroll-up lines)))
+    (cond ((pos-visible-in-window-p (point-max))
+          (goto-char (point-max))
+          (recenter -1)
+          (message (substitute-command-keys
+                    "End.  Type \\[view-exit] to quit viewing."))))
+    (move-to-window-line -1)
+    (beginning-of-line)))
 
 (defun View-scroll-lines-forward-set-scroll-size (&optional lines)
   "Scroll forward LINES lines in View mode, setting the \"scroll size\".
@@ -334,36 +386,36 @@ Arg is number of lines to scroll."
                                 (- (prefix-numeric-value lines))
                               (- (view-scroll-size)))))
   
-(defun View-search-regexp-forward (times regexp)
-  "Search forward for NTH occurrence of REGEXP in View mode.
+(defun View-search-regexp-forward (n regexp)
+  "Search forward for Nth occurrence of REGEXP.
 Displays line found at center of window.  REGEXP is remembered for
 searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward].  Sets mark at starting position and pushes mark ring."
   (interactive "p\nsSearch forward (regexp): ")
   (if (> (length regexp) 0)
       (progn
-       ;(view-last-command 'View-search-last-regexp-forward times)
-       (view-search times regexp))))
+       ;(view-last-command 'View-search-last-regexp-forward n)
+       (view-search n regexp))))
 
-(defun View-search-regexp-backward (times regexp)
-  "Search backward from window start for NTH instance of REGEXP in View mode.
+(defun View-search-regexp-backward (n regexp)
+  "Search backward from window start for Nth instance of REGEXP.
 Displays line found at center of window.  REGEXP is remembered for
 searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward].  Sets mark at starting position and pushes mark ring."
   (interactive "p\nsSearch backward (regexp): ")
-  (View-search-regexp-forward (- times) regexp))
+  (View-search-regexp-forward (- n) regexp))
 
-(defun View-search-last-regexp-forward (times)
-  "Search forward from window end for NTH instance of last regexp in View mode.
+(defun View-search-last-regexp-forward (n)
+  "Search forward from window end for Nth instance of last regexp.
 Displays line found at center of window.  Sets mark at starting position
 and pushes mark ring."
   (interactive "p")
-  (View-search-regexp-forward times view-last-regexp))
+  (View-search-regexp-forward n view-last-regexp))
 
-(defun View-search-last-regexp-backward (times)
-  "Search backward from window start for NTH instance of last regexp in View mode.
+(defun View-search-last-regexp-backward (n)
+  "Search backward from window start for Nth instance of last regexp.
 Displays line found at center of window.  Sets mark at starting position and
 pushes mark ring."
   (interactive "p")
-  (View-search-regexp-backward times view-last-regexp))
+  (View-search-regexp-backward n view-last-regexp))
 
 (defun View-back-to-mark (&optional ignore)
   "Return to last mark set in View mode, else beginning of file.
@@ -390,3 +442,7 @@ invocations return to earlier marks."
       (message "Can't find occurrence %d of %s" times regexp)
       (sit-for 4))))
 
+\f
+(provide 'view)
+
+;;; view.el ends here