(shell-mode-map): Inherit comint-mode-map, but copy the completion menu.
[bpt/emacs.git] / lisp / man.el
index db6a973..7143563 100644 (file)
@@ -1,6 +1,6 @@
 ;;; man.el --- browse UNIX manual pages
 
-;; Copyright (C) 1993 Free Software Foundation, Inc.
+;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
 
 ;; Author:             Barry A. Warsaw <bwarsaw@cen.com>
 ;; Last-Modified:      31-Jul-1991
@@ -51,7 +51,7 @@
 ;; Eric Rose <erose@jessica.stanford.edu>, submitted manual.el which
 ;; provided a very nice manual browsing mode.
 
-;; This package was available as `superman.el' from the LCD paackage
+;; This package was available as `superman.el' from the LCD package
 ;; for some time before it was accepted into Emacs 19.  The entry
 ;; point and some other names have been changed to make it a drop-in
 ;; replacement for the old man.el package.
@@ -62,7 +62,7 @@
 ;;   is done in the background. The cleaning commands are configurable.
 ;; + Syntax is the same as Un*x man
 ;; + Functionality is the same as Un*x man, including "man -k" and
-;;   "man <section>, etc.
+;;   "man <section>", etc.
 ;; + Provides a manual browsing mode with keybindings for traversing
 ;;   the sections of a manpage, following references in the SEE ALSO
 ;;   section, and more.
 ;;; Code:
 
 (require 'assoc)
-(provide 'man)
 
 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
 ;; user variables
 
+(defvar manual-program "man"
+  "The name of the program that produces man pages.")
+
 (defvar Man-notify 'friendly
   "*Selects the behavior when manpage is ready.
 This variable may have one of the following values:
 
+newframe   -- put the manpage in its own frame (see `Man-frame-parameters')
 bully      -- make the manpage the current buffer and only window
 aggressive -- make the manpage the current buffer in the other window
 friendly   -- display manpage in other window but don't make current
@@ -90,31 +93,38 @@ meek       -- make no indication that manpage is ready
 
 Any other value of `Man-notify' is equivalent to `meek'.")
 
+(defvar Man-frame-parameters nil
+  "*Frame parameter list for creating a new frame for a manual page.")
+
 (defvar Man-reuse-okay-p t
   "*Reuse a manpage buffer if possible.
-When t, and a manpage buffer already exists with the same invocation,
-man just indicates the manpage is ready according to the value of
-Man-notify. When nil, it always fires off a background process, putting
-the results in a uniquely named buffer.")
+If non-nil, and a manpage buffer already exists with the same
+invocation, man just indicates the manpage is ready according to the
+value of `Man-notify'.  When nil, it always fires off a background
+process, putting the results in a uniquely named buffer.")
 
 (defvar Man-downcase-section-letters-p t
   "*Letters in sections are converted to lower case.
 Some Un*x man commands can't handle uppercase letters in sections, for
 example \"man 2V chmod\", but they are often displayed in the manpage
-with the upper case letter. When this variable is t, the section
+with the upper case letter.  When this variable is t, the section
 letter (e.g., \"2V\") is converted to lowercase (e.g., \"2v\") before
 being sent to the man background process.")
 
 (defvar Man-circular-pages-p t
   "*If t, the manpage list is treated as circular for traversal.")
 
+;; I changed this to nil because it is a bad idea
+;; to fail to recognize things in other sections.
 (defvar Man-auto-section-alist
-  '((c-mode . ("2" "3"))
-    (c++-mode . ("2" "3"))
-    (shell-mode . ("1" "8"))
-    (cmushell-mode . ("1" "8"))
-    (text-mode . "1")
-    )
+  nil
+;;  '((c-mode . ("2" "3"))
+;;    (c++-mode . ("2" "3"))
+;;    (c++-c-mode . ("2" "3"))
+;;    (shell-mode . ("1" "8"))
+;;    (cmushell-mode . ("1" "8"))
+;;    (text-mode . "1")
+;;    )
   "*Association list of major modes and their default section numbers.
 List is of the form: (MAJOR-MODE . [SECTION | (SECTION*)]). If current
 major mode is not in list, then the default is to check for manpages
@@ -122,33 +132,38 @@ in all sections.")
 
 (defvar Man-section-translations-alist
   '(("3C++" . "3")
+    ("3X" . "3")                        ; Xlib man pages
+    ("3X11" . "3")
     ("1-UCB" . ""))
   "*Association list of bogus sections to real section numbers.
 Some manpages (e.g. the Sun C++ 2.1 manpages) have section numbers in
-their references which Un*x man(1) does not recognize.  This
-assocation list is used to translate those sections, when found, to
+their references which Un*x `man' does not recognize.  This
+association list is used to translate those sections, when found, to
 the associated section number.")
 
 (defvar Man-filter-list
   '(("sed "
-     ("-e 's/.\010//g'"
+     (;;"-e 's/.\010//g'"
       "-e '/[Nn]o such file or directory/d'"
-      "-e '/Reformatting page.  Wait... done/d'"
-      "-e '/^\\([A-Z][A-Z.]*([0-9A-Za-z][-0-9A-Za-z+]*)\\).*\\1$/d'"
-      "-e '/^[ \\t]*Hewlett-Packard Company[ \\t]*- [0-9]* -.*$/d'"
-      "-e '/^[ \\t]*Hewlett-Packard[ \\t]*- [0-9]* -.*$/d'"
-      "-e '/^ *Page [0-9]*.*(printed [0-9\\/]*)$/d'"
+      "-e '/Reformatting page.  Wait/d'"
+      "-e '/Reformatting entry.  Wait/d'"
+      "-e '/^ *\\([A-Za-z][A-Za-z.]*([0-9A-Za-z][-0-9A-Za-z+]*)\\).*\\1$/d'"
+      "-e '/^[ \t]*Hewlett-Packard Company[ \t]*- [0-9]* -.*$/d'"
+      "-e '/^[ \t]*Hewlett-Packard[ \t]*- [0-9]* -.*$/d'"
+      "-e '/^  *- [0-9]* - *Formatted:.*[0-9]$/d'"
+      "-e '/^[ \t]*Page [0-9]*.*(printed [0-9\\/]*)$/d'"
       "-e '/^Printed [0-9].*[0-9]$/d'"
-      "-e '/^Sun Microsystems.*Last change:/d'"
+      "-e '/^[ \t]*X Version 1[01].*Release [0-9]/d'"
+      "-e '/^[A-za-z].*Last change:/d'"
       "-e '/^Sun Release [0-9].*[0-9]$/d'"
       "-e '/^\\n$/D'"
       ))
-    ("awk '"
-     ("BEGIN { blankline=0; anonblank=0; }"
-      "/^$/ { if (anonblank==0) next; }"
-      "{ anonblank=1; }"
-      "/^$/ { blankline++; next; }"
-      "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }"
+    ("awk '\n"
+     ("BEGIN { blankline=0; anonblank=0; }\n"
+      "/^$/ { if (anonblank==0) next; }\n"
+      "{ anonblank=1; }\n"
+      "/^$/ { blankline++; next; }\n"
+      "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }\n"
       "'"
       ))
      )
@@ -158,50 +173,63 @@ This variable contains an association list of the following form:
 '((command-string (phrase-string*))*)
 
 Each phrase-string is concatenated onto the command-string to form a
-command filter. The (standard) output (and standard error) of the Un*x
+command filter.  The (standard) output (and standard error) of the Un*x
 man command is piped through each command filter in the order the
-commands appear in the association list. The final output is placed in
+commands appear in the association list.  The final output is placed in
 the manpage buffer.")
 
 (defvar Man-mode-line-format
   '("" mode-line-modified
        mode-line-buffer-identification "   "
        global-mode-string
-       Man-page-mode-string
-       "    %[(" mode-name minor-mode-alist mode-line-process ")%]----"
+       " " Man-page-mode-string
+       "    %[(" mode-name mode-line-process minor-mode-alist ")%]----"
        (-3 . "%p") "-%-")
   "*Mode line format for manual mode buffer.")
 
 (defvar Man-mode-map nil
-  "*Keymap for Man-mode.")
+  "*Keymap for Man mode.")
+
+(defvar Man-mode-hook nil
+  "*Normal hook run when Man mode is enabled.")
 
-(defvar Man-mode-hooks nil
-  "*Hooks for Man-mode.")
+(defvar Man-cooked-hook nil
+  "*Normal hook run after removing backspaces but before Man-mode processing.")
 
-(defvar Man-section-regexp "[0-9][a-zA-Z+]*"
+(defvar Man-section-regexp "[0-9][a-zA-Z+]*\\|[LNln]"
   "*Regular expression describing a manpage section within parentheses.")
 
-(defvar Man-heading-regexp "^[A-Z]"
+;; Unless some system actually adds leading whitespace other than one space,
+;; it is more reliable not to accept any other leading whitespace.
+(defvar Man-heading-regexp "^[ \t]*\\([A-Z][A-Z \t]+\\)$"
   "*Regular expression describing a manpage heading entry.")
 
 (defvar Man-see-also-regexp "SEE ALSO"
   "*Regular expression for SEE ALSO heading (or your equivalent).
 This regexp should not start with a `^' character.")
 
-(defvar Man-first-heading-regexp "^NAME$\\|^No manual entry for .*$"
+(defvar Man-first-heading-regexp "^[ \t]*NAME$\\|^[ \t]*No manual entry fo.*$"
   "*Regular expression describing first heading on a manpage.
 This regular expression should start with a `^' character.")
 
-(defvar Man-reference-regexp "[-a-zA-Z0-9_.]+\\(([0-9][a-zA-Z+]*)\\)?"
+(defvar Man-reference-regexp
+  "[-a-zA-Z0-9_][-a-zA-Z0-9_.]*\\(([0-9][a-zA-Z+]*)\\)?"
   "*Regular expression describing a reference in the SEE ALSO section.")
 
+(defvar Man-switches ""
+  "*Switches passed to the man command, as a single string.")
+
+;; Would someone like to provide a good test for being on Solaris?
+;; We could give it its own value of system-type, but that has drawbacks;
+;; it would require changes in lots of places that test system-type.
+(defvar Man-specified-section-option
+  (if (string-match "-solaris[0-9.]*$" system-configuration)
+      "-s"
+    "")
+  "*Option that indicates a specified a manual section name.")
 
 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 ;; end user variables
-
-(defconst Man-version-number "1.1"
-  "man's version number.")
-
 \f
 ;; other variables and keymap initializations
 (make-variable-buffer-local 'Man-sections-alist)
@@ -209,6 +237,7 @@ This regular expression should start with a `^' character.")
 (make-variable-buffer-local 'Man-page-list)
 (make-variable-buffer-local 'Man-current-page)
 (make-variable-buffer-local 'Man-page-mode-string)
+(make-variable-buffer-local 'Man-original-frame)
 
 (setq-default Man-sections-alist nil)
 (setq-default Man-refpages-alist nil)
@@ -234,7 +263,6 @@ This regular expression should start with a `^' character.")
   (define-key Man-mode-map "s"    'Man-goto-see-also-section)
   (define-key Man-mode-map "q"    'Man-quit)
   (define-key Man-mode-map "m"    'manual-entry)
-  (define-key Man-mode-map "v"    'Man-version)
   (define-key Man-mode-map "?"    'describe-mode)
   )
 
@@ -243,7 +271,7 @@ This regular expression should start with a `^' character.")
 ;; utilities
 
 (defun Man-page-mode-string ()
-  "Formats part of the mode line for manual mode."
+  "Formats part of the mode line for Man mode."
   (format "%d (of %d)" Man-current-page (length Man-page-list)))
 
 (defun Man-delete-trailing-newline (str)
@@ -253,7 +281,7 @@ This regular expression should start with a `^' character.")
 
 (defun Man-build-man-command ()
   "Builds the entire background manpage and cleaning command."
-  (let ((command "man %s 2>&1 | ")
+  (let ((command (concat manual-program " " Man-switches " %s 2>/dev/null"))
        (flist Man-filter-list))
     (while flist
       (let ((pcom (car (car flist)))
@@ -261,12 +289,10 @@ This regular expression should start with a `^' character.")
        (setq flist (cdr flist))
        (if (or (not (stringp pcom))
                (not (listp pargs)))
-           (error "malformed Man-filter-list."))
-       (setq command (concat command pcom
+           (error "Malformed Man-filter-list"))
+       (setq command (concat command " | " pcom
                              (mapconcat '(lambda (phrase) phrase)
-                                        pargs " "))))
-      (if flist
-         (setq command (concat command " | " ))))
+                                        pargs " ")))))
     command))
 
 (defun Man-downcase (man-args)
@@ -308,12 +334,12 @@ This regular expression should start with a `^' character.")
                                (Man-downcase s2)
                              s2)
                    slist nil))))
-       (concat section " " word))
+       (concat Man-specified-section-option section " " word))
     ref))
 
 (defun Man-linepos (&optional position col-p)
   "Return the character position at various line/buffer positions.
-Preserves the state of point, mark, etc. Optional POSITION can be one
+Preserves the state of point, mark, etc.  Optional arg POSITION can be one
 of the following symbols:
      bol == beginning of line
      boi == beginning of indentation
@@ -321,7 +347,8 @@ of the following symbols:
      bob == beginning of buffer
      eob == end of buffer
 
-Optional COL-P non-nil returns current-column instead of character position."
+Optional arg COL-P, if non-nil, means to return
+the current column instead of character position."
   (let ((tpnt (point))
        rval)
     (cond
@@ -339,30 +366,22 @@ Optional COL-P non-nil returns current-column instead of character position."
 ;; default man entry and get word under point
 
 (defun Man-default-man-args (manword)
-  "Build the default man args from MANWORD and major-mode."
-  (let ((mode major-mode)
-       (slist Man-auto-section-alist))
-    (while (and slist
-               (not (eq (car (car slist)) mode)))
-      (setq slist (cdr slist)))
-    (if (not slist)
-       manword
-      (let ((sections (cdr (car slist))))
-       (if (not (listp sections))
-           (concat sections " " manword)
-         (let ((manarg ""))
-           (while sections
-             (setq manarg (concat manarg " " (car sections) " " manword))
-             (setq sections (cdr sections)))
-           manarg)
-         )))))
+  "Build the default man args from MANWORD and buffer's major mode."
+  (let ((sections (cdr (assq major-mode Man-auto-section-alist))))
+    (cond
+     ((null sections) manword)
+     ((consp sections) 
+      (mapconcat (lambda (n) (concat Man-specified-section-option
+                                    n " " manword))
+                sections " "))
+     (t
+      (concat sections " " manword)))))
 
 (defun Man-default-man-entry ()
   "Make a guess at a default manual entry.
 This guess is based on the text surrounding the cursor, and the
-default section number is selected from Man-auto-section-alist."
-  (let ((default-section nil)
-       default-title)
+default section number is selected from `Man-auto-section-alist'."
+  (let (default-title)
     (save-excursion
       
       ;; Default man entry title is any word the cursor is on,
@@ -381,88 +400,135 @@ default section number is selected from Man-auto-section-alist."
       ;; section number in default-entry
       (if (looking-at "[ \t]*([ \t]*[0-9][a-zA-Z]?[ \t]*)")
          (progn (skip-chars-forward "^0-9")
-                (setq default-section
-                      (buffer-substring
-                       (point)
-                       (progn
-                         (skip-chars-forward "0-9a-zA-Z")
-                         (point)))))
-       
-       ;; Otherwise, assume section number to be 2 if we're
-       ;; in C code
-       (and (eq major-mode 'c-mode)
-            (setq default-section "2")))
-      (if default-section
-         (format "%s %s" default-section default-title)
-       default-title))))
+                (setq default-title
+                      (concat Man-specified-section-option
+                              (buffer-substring
+                               (point)
+                               (progn
+                                 (skip-chars-forward "0-9a-zA-Z")
+                                 (point)))
+                              " "
+                              default-title)))
+       (setq default-title (Man-default-man-args default-title)))
+      default-title)))
         
 \f
 ;; ======================================================================
 ;; top level command and background process sentinel
 
+;;; This alias makes completion more predictable if ignoring case.
+;;;###autoload
+(defalias 'man 'manual-entry)
+
 ;;;###autoload
 (defun manual-entry (arg)
   "Get a Un*x manual page and put it in a buffer.
-This command is the top-level command in the man package. It runs a Un*x
+This command is the top-level command in the man package.  It runs a Un*x
 command to retrieve and clean a manpage in the background and places the
-results in a Man-mode (manpage browsing) buffer. See variable
-Man-notify for what happens when the buffer is ready.
-Universal argument ARG, is passed to Man-getpage-in-background."
+results in a Man mode (manpage browsing) buffer.  See variable
+`Man-notify' for what happens when the buffer is ready.
+Normally, if a buffer already exists for this man page, it will display
+immediately; either a prefix argument or a nil value to `Man-reuse-okay-p'
+overrides this and forces the man page to be regenerated."
   (interactive "P")
   (let* ((default-entry (Man-default-man-entry))
         (man-args
          (read-string (format "Manual-entry: %s"
-                       (if (string= default-entry "") ""
-                         (format "(default: %s) "
-                                 default-entry))))))
+                              (if (string= default-entry "") ""
+                                (format "(default: %s) "
+                                        default-entry))))))
     (and (string= man-args "")
         (if (string= default-entry "")
-            (error "No man args given.")
+            (error "No man args given")
           (setq man-args default-entry)))
-    (if Man-downcase-section-letters-p
-       (setq man-args (Man-downcase man-args)))
+
+    ;; Recognize the subject(section) syntax.
+    (setq man-args (Man-translate-references man-args))
+
+    ;; This is apparently already done correctly via Man-translate-references.
+    ;; (if Man-downcase-section-letters-p
+    ;;    (setq man-args (Man-downcase man-args)))
     (Man-getpage-in-background man-args (consp arg))
     ))
 
-(defun Man-getpage-in-background (man-args &optional override-reuse-p)
-  "Uses MAN-ARGS to build and fire off the manpage and cleaning command.
-Optional OVERRIDE-REUSE-P, when supplied non-nil forces man to
+(defun Man-getpage-in-background (topic &optional override-reuse-p)
+  "Uses TOPIC to build and fire off the manpage and cleaning command.
+Optional OVERRIDE-REUSE-P, when non-nil, means to
 start a background process even if a buffer already exists and
-Man-reuse-okay-p is non-nil."
-  (let* ((bufname (concat "*man " man-args "*"))
+`Man-reuse-okay-p' is non-nil."
+  (let* ((man-args topic)
+        (bufname (concat "*man " man-args "*"))
         (buffer  (get-buffer bufname)))
     (if (and Man-reuse-okay-p
             (not override-reuse-p)
             buffer)
        (Man-notify-when-ready buffer)
-      (message "Invoking man %s in background..." man-args)
+      (require 'env)
+      (message "Invoking %s %s in background" manual-program man-args)
       (setq buffer (generate-new-buffer bufname))
-      (set-process-sentinel
-       (start-process "man" buffer "sh" "-c"
-                     (format (Man-build-man-command) man-args))
-       'Man-bgproc-sentinel))
-    ))
+      (save-excursion
+       (set-buffer buffer)
+       (setq Man-original-frame (selected-frame)))
+      (let ((process-environment (copy-sequence process-environment)))
+       ;; Prevent any attempt to use display terminal fanciness.
+       (setenv "TERM" "dumb")
+       (set-process-sentinel
+        (start-process manual-program buffer "sh" "-c"
+                       (format (Man-build-man-command) man-args))
+        'Man-bgproc-sentinel))
+      )))
 
 (defun Man-notify-when-ready (man-buffer)
   "Notify the user when MAN-BUFFER is ready.
 See the variable `Man-notify' for the different notification behaviors."
-  (cond
-   ((eq Man-notify 'bully)
-    (pop-to-buffer man-buffer)
-    (delete-other-windows))
-   ((eq Man-notify 'aggressive)
-    (pop-to-buffer man-buffer))
-   ((eq Man-notify 'friendly)
-    (display-buffer man-buffer 'not-this-window))
-   ((eq Man-notify 'polite)
-    (beep)
-    (message "Manual buffer %s is ready." (buffer-name man-buffer)))
-   ((eq Man-notify 'quiet)
-    (message "Manual buffer %s is ready." (buffer-name man-buffer)))
-   ((or (eq Man-notify 'meek)
-       t)
-    (message ""))
-   ))
+  (let ((saved-frame (save-excursion
+                      (set-buffer man-buffer)
+                      Man-original-frame)))
+    (cond
+     ((eq Man-notify 'newframe)
+      ;; Since we run asynchronously, perhaps while Emacs is waiting for input,
+      ;; we must not leave a different buffer current.
+      ;; We can't rely on the editor command loop to reselect
+      ;; the selected window's buffer.
+      (save-excursion
+       (set-buffer man-buffer)
+       (make-frame Man-frame-parameters)))
+     ((eq Man-notify 'bully)
+      (and window-system
+          (frame-live-p saved-frame)
+          (select-frame saved-frame))
+      (pop-to-buffer man-buffer)
+      (delete-other-windows))
+     ((eq Man-notify 'aggressive)
+      (and window-system
+          (frame-live-p saved-frame)
+          (select-frame saved-frame))
+      (pop-to-buffer man-buffer))
+     ((eq Man-notify 'friendly)
+      (and window-system
+          (frame-live-p saved-frame)
+          (select-frame saved-frame))
+      (display-buffer man-buffer 'not-this-window))
+     ((eq Man-notify 'polite)
+      (beep)
+      (message "Manual buffer %s is ready." (buffer-name man-buffer)))
+     ((eq Man-notify 'quiet)
+      (message "Manual buffer %s is ready." (buffer-name man-buffer)))
+     ((or (eq Man-notify 'meek)
+         t)
+      (message ""))
+     )))
+
+(defun Man-set-fonts ()
+  (goto-char (point-min))
+  (while (re-search-forward "\\(.\b\\)+" nil t)
+    (let ((st (match-beginning 0)) (en (match-end 0)))
+      (goto-char st)
+      (if window-system
+         (put-text-property st (if (= en (point-max)) en (1+ en)) 'face 
+                            (if (looking-at "_") 'underline 'bold)))
+      (while (and (< (point) en) (looking-at ".\b"))
+       (replace-match "")))))
 
 (defun Man-bgproc-sentinel (process msg)
   "Manpage background process sentinel."
@@ -471,44 +537,47 @@ See the variable `Man-notify' for the different notification behaviors."
        (err-mess nil))
     (if (null (buffer-name Man-buffer)) ;; deleted buffer
        (set-process-buffer process nil)
-      (save-excursion
-       (set-buffer Man-buffer)
-       (goto-char (point-min))
-       (cond ((or (looking-at "No \\(manual \\)*entry for")
-                  (looking-at "[^\n]*: nothing appropriate$"))
-              (setq err-mess (buffer-substring (point) (Man-linepos 'eol))
-                    delete-buff t)
-              )
-             ((not (and (eq (process-status process) 'exit)
-                        (= (process-exit-status process) 0)))
-              (setq err-mess
-                    (concat (buffer-name Man-buffer)
-                            ": process "
-                            (let ((eos (1- (length msg))))
-                              (if (= (aref msg eos) ?\n)
-                                  (substring msg 0 eos) msg))))
-              (goto-char (point-max))
-              (insert (format "\nprocess %s" msg))
-              )))
-      (if delete-buff
-         (kill-buffer Man-buffer)
-       (save-window-excursion
-         (save-excursion
-           (set-buffer Man-buffer)
-           (Man-mode)
-           (set-buffer-modified-p nil)))
-       (Man-notify-when-ready Man-buffer))
-
-      (if err-mess
-         (error err-mess))
-      )))
+      (save-match-data
+       (save-excursion
+         (set-buffer Man-buffer)
+         (goto-char (point-min))
+         (let ((case-fold-search nil))
+           (cond ((or (looking-at "No \\(manual \\)*entry for")
+                      (looking-at "[^\n]*: nothing appropriate$"))
+                  (setq err-mess (buffer-substring (point) (Man-linepos 'eol))
+                        delete-buff t))
+                 ((not (and (eq (process-status process) 'exit)
+                            (= (process-exit-status process) 0)))
+                  (setq err-mess
+                        (concat (buffer-name Man-buffer)
+                                ": process "
+                                (let ((eos (1- (length msg))))
+                                  (if (= (aref msg eos) ?\n)
+                                      (substring msg 0 eos) msg))))
+                  (goto-char (point-max))
+                  (insert (format "\nprocess %s" msg))
+                  ))))
+       (if delete-buff
+           (kill-buffer Man-buffer)
+         (save-window-excursion
+           (save-excursion
+             (set-buffer Man-buffer)
+             (let ((case-fold-search nil))
+               (Man-set-fonts)
+               (run-hooks 'Man-cooked-hook)
+               (Man-mode))
+             (set-buffer-modified-p nil)))
+         (Man-notify-when-ready Man-buffer))
+
+       (if err-mess
+           (error err-mess))))))
 
 \f
 ;; ======================================================================
 ;; set up manual mode in buffer and build alists
 
 (defun Man-mode ()
-  "SUPERMAN 1.1: A mode for browsing Un*x manual pages.
+  "A mode for browsing Un*x manual pages.
 
 The following man commands are available in the buffer. Try
 \"\\[describe-key] <key> RET\" for more information:
@@ -522,7 +591,6 @@ The following man commands are available in the buffer. Try
 \\[Man-goto-section]       Go to a manpage section.
 \\[Man-goto-see-also-section]       Jumps to the SEE ALSO manpage section.
 \\[Man-quit]       Deletes the manpage, its buffer, and window.
-\\[Man-version]       Prints man's version number.
 \\[describe-mode]       Prints this help text.
 
 The following variables may be of some use. Try
@@ -535,21 +603,21 @@ Man-circular-pages-p            Multiple manpage list treated as circular?
 Man-auto-section-alist          List of major modes and their section numbers.
 Man-section-translations-alist  List of section numbers and their Un*x equiv.
 Man-filter-list                 Background manpage filter command.
-Man-mode-line-format            Mode line format for Man-mode buffers.
-Man-mode-map                    Keymap bindings for Man-mode buffers.
-Man-mode-hooks                  Hooks for Man-mode.
+Man-mode-line-format            Mode line format for Man mode buffers.
+Man-mode-map                    Keymap bindings for Man mode buffers.
+Man-mode-hook                   Normal hook run on entry to Man mode.
 Man-section-regexp              Regexp describing manpage section letters.
 Man-heading-regexp              Regexp describing section headers.
 Man-see-also-regexp             Regexp for SEE ALSO section (or your equiv).
 Man-first-heading-regexp        Regexp for first heading on a manpage.
 Man-reference-regexp            Regexp matching a references in SEE ALSO.
-Man-version-number              Superman version number.
+Man-switches                   Background `man' command switches.
 
 The following key bindings are currently in effect in the buffer:
 \\{Man-mode-map}"
   (interactive)
   (setq major-mode 'Man-mode
-       mode-name "Manual"
+       mode-name "Man"
        buffer-auto-save-file-name nil
        mode-line-format Man-mode-line-format
        truncate-lines t
@@ -559,17 +627,18 @@ The following key bindings are currently in effect in the buffer:
   (use-local-map Man-mode-map)
   (goto-char (point-min))
   (Man-build-page-list)
-  (Man-goto-page 1))
+  (Man-goto-page 1)
+  (run-hooks 'Man-mode-hook))
 
 (defun Man-build-section-alist ()
   "Build the association list of manpage sections."
   (setq Man-sections-alist nil)
   (goto-char (point-min))
-  (while (re-search-forward Man-heading-regexp (point-max) t)
-    (aput 'Man-sections-alist
-         (buffer-substring (Man-linepos 'bol) (Man-linepos)))
-    (forward-line 1)
-    ))
+  (let ((case-fold-search nil))
+    (while (re-search-forward Man-heading-regexp (point-max) t)
+      (aput 'Man-sections-alist
+           (buffer-substring (match-beginning 1) (match-end 1)))
+      (forward-line 1))))
 
 (defun Man-build-references-alist ()
   "Build the association list of references (in the SEE ALSO section)."
@@ -582,24 +651,24 @@ The following key bindings are currently in effect in the buffer:
                     (point)))
              hyphenated
              (runningpoint -1))
-         (narrow-to-region start end)
-         (goto-char (point-min))
-         (back-to-indentation)
-         (while (and (not (eobp)) (/= (point) runningpoint))
-           (setq runningpoint (point))
-           (let* ((bow (point))
-                  (eow (re-search-forward Man-reference-regexp end t))
-                  (word (buffer-substring bow (match-end 0)))
-                  (len (1- (length word))))
-             (if (not eow) nil
-               (if hyphenated
-                   (setq word (concat hyphenated word)
-                         hyphenated nil))
-               (if (= (aref word len) ?-)
-                   (setq hyphenated (substring word 0 len))
-                 (aput 'Man-refpages-alist word))))
-           (skip-chars-forward " \t\n,"))
-         ))))
+         (save-restriction
+           (narrow-to-region start end)
+           (goto-char (point-min))
+           (back-to-indentation)
+           (while (and (not (eobp)) (/= (point) runningpoint))
+             (setq runningpoint (point))
+             (let* ((eow (re-search-forward Man-reference-regexp end t))
+                    (word (buffer-substring
+                           (match-beginning 0) (match-end 0)))
+                    (len (1- (length word))))
+               (if (not eow) nil
+                 (if hyphenated
+                     (setq word (concat hyphenated word)
+                           hyphenated nil))
+                 (if (= (aref word len) ?-)
+                     (setq hyphenated (substring word 0 len))
+                   (aput 'Man-refpages-alist word))))
+             (skip-chars-forward " \t\n,")))))))
 
 (defun Man-build-page-list ()
   "Build the list of separate manpages in the buffer."
@@ -618,38 +687,41 @@ The following key bindings are currently in effect in the buffer:
          (goto-char (point-max))
          (setq page-end (point)))
        (setq Man-page-list (append Man-page-list
-                                  (list (cons page-start page-end)))
+                                   (list (cons page-start page-end)))
              page-start page-end)
-       ))))  
+       ))))
 
 \f
 ;; ======================================================================
-;; Man-mode commands
+;; Man mode commands
 
 (defun Man-next-section (n)
   "Move point to Nth next section (default 1)."
   (interactive "p")
-  (if (looking-at Man-heading-regexp)
-      (forward-line 1))
-  (if (re-search-forward Man-heading-regexp (point-max) t n)
-      (beginning-of-line)
-    (goto-char (point-max))))
+  (let ((case-fold-search nil))
+    (if (looking-at Man-heading-regexp)
+       (forward-line 1))
+    (if (re-search-forward Man-heading-regexp (point-max) t n)
+       (beginning-of-line)
+      (goto-char (point-max)))))
 
 (defun Man-previous-section (n)
   "Move point to Nth previous section (default 1)."
   (interactive "p")
-  (if (looking-at Man-heading-regexp)
-      (forward-line -1))
-  (if (re-search-backward Man-heading-regexp (point-min) t n)
-      (beginning-of-line)
-    (goto-char (point-min))))
+  (let ((case-fold-search nil))
+    (if (looking-at Man-heading-regexp)
+       (forward-line -1))
+    (if (re-search-backward Man-heading-regexp (point-min) t n)
+       (beginning-of-line)
+      (goto-char (point-min)))))
 
 (defun Man-find-section (section)
   "Move point to SECTION if it exists, otherwise don't move point.
 Returns t if section is found, nil otherwise."
-  (let ((curpos (point)))
+  (let ((curpos (point))
+       (case-fold-search nil))
     (goto-char (point-min))
-    (if (re-search-forward (concat "^" section) (point-max) t)
+    (if (re-search-forward (concat "^[ \t]*" section) (point-max) t)
        (progn (beginning-of-line) t)
       (goto-char curpos)
       nil)
@@ -672,31 +744,46 @@ Returns t if section is found, nil otherwise."
 
 (defun Man-goto-see-also-section ()
   "Move point the the \"SEE ALSO\" section.
-Actually the section moved to is described by Man-see-also-regexp."
+Actually the section moved to is described by `Man-see-also-regexp'."
   (interactive)
   (if (not (Man-find-section Man-see-also-regexp))
       (error (concat "No " Man-see-also-regexp
-                    " section found in current manpage."))))
+                    " section found in current manpage"))))
 
-(defun Man-follow-manual-reference (arg)
+(defun Man-follow-manual-reference (arg reference)
   "Get one of the manpages referred to in the \"SEE ALSO\" section.
-Queries you for the page to retrieve. Of course it does this in the
-background. Universal argument ARG is passed to Man-getpage-in-background."
-  (interactive "P")
+Specify which reference to use; default is based on word at point.
+Prefix argument ARG is passed to `Man-getpage-in-background'."
+  (interactive
+   (if (not Man-refpages-alist)
+       (error "No references in current man page")
+     (list current-prefix-arg
+          (let* ((default (or
+                            (car (all-completions
+                                  (save-excursion
+                                    (skip-syntax-backward "w()")
+                                    (skip-chars-forward " \t")
+                                    (let ((word (current-word)))
+                                      ;; strip a trailing '-':
+                                      (if (string-match "-$" word)
+                                          (substring word 0
+                                                     (match-beginning 0))
+                                        word)))
+                                  Man-refpages-alist))
+                            (aheadsym Man-refpages-alist)))
+                  chosen
+                  (prompt (concat "Refer to: (default " default ") ")))
+             (setq chosen (completing-read prompt Man-refpages-alist nil t))
+             (if (or (not chosen)
+                     (string= chosen ""))
+                 default
+               chosen)))))
   (if (not Man-refpages-alist)
-      (error (concat "No references found in current manpage."))
-    (aput 'Man-refpages-alist
-         (let* ((default (aheadsym Man-refpages-alist))
-                chosen
-                (prompt (concat "Refer to: (default " default ") ")))
-           (setq chosen (completing-read prompt Man-refpages-alist nil t))
-           (if (or (not chosen)
-                   (string= chosen ""))
-               default
-             chosen)))
+      (error "No references found in current manpage")
+    (aput 'Man-refpages-alist reference)
     (Man-getpage-in-background
      (Man-translate-references (aheadsym Man-refpages-alist))
-     (consp arg))))
+     arg)))
 
 (defun Man-quit ()
   "Kill the buffer containing the manpage."
@@ -708,9 +795,10 @@ background. Universal argument ARG is passed to Man-getpage-in-background."
 (defun Man-goto-page (page)
   "Go to the manual page on page PAGE."
   (interactive
-   (if (not Man-page-list)
+   (if (= (length Man-page-list) 1)
        (error "You're looking at the only manpage in the buffer.")
-     (format "nGo to manpage [1-%d]: " (length Man-page-list))))
+     (list (read-minibuffer (format "Go to manpage [1-%d]: "
+                                   (length Man-page-list))))))
   (if (or (< page 1)
          (> page (length Man-page-list)))
       (error "No manpage %d found" page))
@@ -723,7 +811,11 @@ background. Universal argument ARG is passed to Man-getpage-in-background."
     (goto-char page-start)
     (narrow-to-region page-start page-end)
     (Man-build-section-alist)
-    (Man-build-references-alist)
+    ;; Don't let bugs in Man-build-references-alist
+    ;; interfere with ordinary use of this package.
+    (condition-case nil
+       (Man-build-references-alist)
+      (error))
     (widen)
     (narrow-to-region page-start page-end)
     (goto-char (point-min))))
@@ -733,32 +825,24 @@ background. Universal argument ARG is passed to Man-getpage-in-background."
   "Find the next manpage entry in the buffer."
   (interactive)
   (if (= (length Man-page-list) 1)
-      (error "This is the only manpage in the buffer."))
+      (error "This is the only manpage in the buffer"))
   (if (< Man-current-page (length Man-page-list))
       (Man-goto-page (1+ Man-current-page))
     (if Man-circular-pages-p
        (Man-goto-page 1)
-      (error "You're looking at the last manpage in the buffer."))))
+      (error "You're looking at the last manpage in the buffer"))))
 
 (defun Man-previous-manpage ()
   "Find the previous manpage entry in the buffer."
   (interactive)
   (if (= (length Man-page-list) 1)
-      (error "This is the only manpage in the buffer."))
+      (error "This is the only manpage in the buffer"))
   (if (> Man-current-page 1)
       (Man-goto-page (1- Man-current-page))
     (if Man-circular-pages-p
        (Man-goto-page (length Man-page-list))
-      (error "You're looking at the first manpage in the buffer."))))
-
-(defun Man-version (arg)
-  "Show man's version.
-Universal argument (\\[universal-argument]) ARG inserts version
-information in the current buffer instead of printing the message in
-the echo area."
-  (interactive "P")
-  (if (consp arg)
-      (insert "Using Superman version " Man-version-number ".")
-    (message "Using Superman version %s." Man-version-number)))
+      (error "You're looking at the first manpage in the buffer"))))
+\f
+(provide 'man)
 
 ;;; man.el ends here