]> code.delx.au - gnu-emacs/blobdiff - lisp/man.el
(find-file-noselect): If after-find-file switches buffers,
[gnu-emacs] / lisp / man.el
index ab27a6b74ab1963532d0f24e60966e287b94a372..4fa5ae533d0ef6d74bb11059845f194ecd26db51 100644 (file)
@@ -3,8 +3,6 @@
 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
 
 ;; Author:             Barry A. Warsaw <bwarsaw@cen.com>
-;; Last-Modified:      $Date: 1994/10/24 12:37:01 $
-;; Version:            $Revision: 1.54 $
 ;; Keywords:           help
 ;; Adapted-By:         ESR, pot
 
@@ -21,8 +19,9 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to
-;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
 
 ;;; Commentary:
 
@@ -37,7 +36,7 @@
 ;; ========== Credits and History ========== 
 ;; In mid 1991, several people posted some interesting improvements to
 ;; man.el from the standard emacs 18.57 distribution.  I liked many of
-;; these, but wanted everthing in one single package, so I decided
+;; these, but wanted everything in one single package, so I decided
 ;; to incorporate them into a single manual browsing mode.  While
 ;; much of the code here has been rewritten, and some features added,
 ;; these folks deserve lots of credit for providing the initial
@@ -58,7 +57,7 @@
 
 ;; Francesco Potorti` <pot@cnuce.cnr.it> cleaned it up thoroughly,
 ;; making it faster, more robust and more tolerant of different
-;; systems' man idiosynchrasies.
+;; systems' man idiosyncrasies.
 
 ;; ========== Features ==========
 ;; + Runs "man" in the background and pipes the results through a
@@ -83,9 +82,8 @@
 ;;   headers, and after the page footer.  But it is possible to compute
 ;;   the number of blank lines before the page footer by euristhics
 ;;   only.  Is it worth doing?
-;; - Allow the Man-reuse-okay-flag to be set to 'always, meaning that all
-;;   the manpages should go in the same buffer, where they can be browsed
-;;   with M-n and M-p.
+;; - Allow a user option to mean that all the manpages should go in
+;;   the same buffer, where they can be browsed with M-n and M-p.
 ;; - Allow completion on the manpage name when calling man.  This
 ;;   requires a reliable list of places where manpages can be found.  The
 ;;   drawback would be that if the list is not complete, the user might
 (defvar Man-filter-list)
 (defvar Man-original-frame)
 (defvar Man-arguments)
-(defvar Man-fontify-manpage-flag)
 (defvar Man-sections-alist)
 (defvar Man-refpages-alist)
 (defvar Man-uses-untabify-flag)
 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
 ;; user variables
 
-(defvar manual-program "man"
-  "The name of the program that produces man pages.")
+(defvar Man-fontify-manpage-flag t
+  "*Make up the manpage with fonts.")
+
+(defvar Man-overstrike-face 'bold
+  "*Face to use when fontifying overstrike.")
+
+(defvar Man-underline-face 'underline
+  "*Face to use when fontifying underlining.")
 
 ;; Use the value of the obsolete user option Man-notify, if set.
 (defvar Man-notify-method (if (boundp 'Man-notify) Man-notify 'friendly)
@@ -140,13 +143,6 @@ Any other value of `Man-notify-method' 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-flag t
-  "*Reuse a manpage buffer if possible.
-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-method'.  When nil, it always fires off a
-background process,putting the results in a uniquely named buffer.")
-
 (defvar Man-downcase-section-letters-flag t
   "*Letters in sections are converted to lower case.
 Some Un*x man commands can't handle uppercase letters in sections, for
@@ -171,39 +167,20 @@ 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 manual-program "man"
+  "The name of the program that produces man pages.")
+
 (defvar Man-untabify-command "pr"
-  "*Command used for untabifying.")
+  "Command used for untabifying.")
 
 (defvar Man-untabify-command-args (list "-t" "-e")
-  "*List of arguments to be passed to Man-untabify-command (which see).")
+  "List of arguments to be passed to Man-untabify-command (which see).")
 
 (defvar Man-sed-command "sed"
-  "*Command used for processing sed scripts.")
+  "Command used for processing sed scripts.")
 
 (defvar Man-awk-command "awk"
-  "*Command used for processing awk scripts.")
-
-(defconst Man-sysv-sed-script "\
-/\b/ { s/_\b//g
-       s/\b_//g
-        s/o\b+/o/g
-       :ovstrk
-       s/\\(.\\)\b\\1/\\1/g
-       t ovstrk
-       }
-/\e\\[[0-9][0-9]*m/ s///g"
-  "Script for sysV-like sed to nuke backspaces and ANSI codes from manpages.")
-
-(defconst Man-berkeley-sed-script "\
-/\b/ { s/_\b//g\\
-       s/\b_//g\\
-        s/o\b+/o/g\\
-       :ovstrk\\
-       s/\\(.\\)\b\\1/\\1/g\\
-       t ovstrk\\
-       }\\
-/\e\\[[0-9][0-9]*m/ s///g"
-  "Script for berkeley-like sed to nuke backspaces and ANSI codes from manpages.")
+  "Command used for processing awk scripts.")
 
 (defvar Man-mode-line-format
   '("" mode-line-modified
@@ -212,54 +189,51 @@ the associated section number.")
        " " Man-page-mode-string
        "  %[(" mode-name mode-line-process minor-mode-alist ")%]----"
        (-3 . "%p") "-%-")
-  "*Mode line format for manual mode buffer.")
+  "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
-  "*Hook run when Man mode is enabled.")
+  "Hook run when Man mode is enabled.")
 
 (defvar Man-cooked-hook nil
-  "*Hook run after removing backspaces but before Man-mode processing.")
+  "Hook run after removing backspaces but before Man-mode processing.")
 
 (defvar Man-name-regexp "[-a-zA-Z0-9_][-a-zA-Z0-9_.]*"
-  "*Regular expression describing the name of a manpage (without section).")
+  "Regular expression describing the name of a manpage (without section).")
 
 (defvar Man-section-regexp "[0-9][a-zA-Z+]*\\|[LNln]"
-  "*Regular expression describing a manpage section within parentheses.")
+  "Regular expression describing a manpage section within parentheses.")
 
 (defvar Man-page-header-regexp
   (concat "^[ \t]*\\(" Man-name-regexp
          "(\\(" Man-section-regexp "\\))\\).*\\1")
-  "*Regular expression describing the heading of a page.")
+  "Regular expression describing the heading of a page.")
 
 (defvar Man-heading-regexp "^\\([A-Z][A-Z ]+\\)$"
-  "*Regular expression describing a manpage heading entry.")
+  "Regular expression describing a manpage heading entry.")
 
 (defvar Man-see-also-regexp "SEE ALSO"
-  "*Regular expression for SEE ALSO heading (or your equivalent).
+  "Regular expression for SEE ALSO heading (or your equivalent).
 This regexp should not start with a `^' character.")
 
 (defvar Man-first-heading-regexp "^[ \t]*NAME$\\|^[ \t]*No manual entry fo.*$"
-  "*Regular expression describing first heading on a manpage.
+  "Regular expression describing first heading on a manpage.
 This regular expression should start with a `^' character.")
 
 (defvar Man-reference-regexp
   (concat "\\(" Man-name-regexp "\\)(\\(" Man-section-regexp "\\))")
-  "*Regular expression describing a reference in the SEE ALSO section.")
+  "Regular expression describing a reference in the SEE ALSO section.")
 
 (defvar Man-switches ""
-  "*Switches passed to the man command, as a single string.")
+  "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.")
+  "Option that indicates a specified a manual section name.")
 
 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 ;; end user variables
@@ -279,6 +253,30 @@ This regular expression should start with a `^' character.")
 (setq-default Man-current-page 0)
 (setq-default Man-page-mode-string "1 of 1")
 
+(defconst Man-sysv-sed-script "\
+/\b/ { s/_\b//g
+       s/\b_//g
+        s/o\b+/o/g
+        s/+\bo/o/g
+       :ovstrk
+       s/\\(.\\)\b\\1/\\1/g
+       t ovstrk
+       }
+/\e\\[[0-9][0-9]*m/ s///g"
+  "Script for sysV-like sed to nuke backspaces and ANSI codes from manpages.")
+
+(defconst Man-berkeley-sed-script "\
+/\b/ { s/_\b//g\\
+       s/\b_//g\\
+        s/o\b+/o/g\\
+        s/+\bo/o/g\\
+       :ovstrk\\
+       s/\\(.\\)\b\\1/\\1/g\\
+       t ovstrk\\
+       }\\
+/\e\\[[0-9][0-9]*m/ s///g"
+  "Script for berkeley-like sed to nuke backspaces and ANSI codes from manpages.")
+
 (if Man-mode-map
     nil
   (setq Man-mode-map (make-keymap))
@@ -293,7 +291,6 @@ This regular expression should start with a `^' character.")
   (define-key Man-mode-map "<"    'beginning-of-buffer)
   (define-key Man-mode-map "."    'beginning-of-buffer)
   (define-key Man-mode-map "r"    'Man-follow-manual-reference)
-  (define-key Man-mode-map "t"    'toggle-truncate-lines)
   (define-key Man-mode-map "g"    'Man-goto-section)
   (define-key Man-mode-map "s"    'Man-goto-see-also-section)
   (define-key Man-mode-map "k"    'Man-kill)
@@ -310,8 +307,6 @@ This regular expression should start with a `^' character.")
   "Used for initialising variables based on the value of window-system.
 This is necessary if one wants to dump man.el with emacs."
 
-  (defvar Man-fontify-manpage-flag t
-    "*Make up the manpage with fonts.")
   ;; The following is necessary until fonts are implemented on
   ;; terminals.
   (setq Man-fontify-manpage-flag (and Man-fontify-manpage-flag
@@ -348,7 +343,6 @@ This is necessary if one wants to dump man.el with emacs."
         "")
        "-e '/^[\001-\032][\001-\032]*$/d'"
        "-e '/\e[789]/s///g'"
-       "-e '/[Nn]o such file or directory/d'"
        "-e '/Reformatting page.  Wait/d'"
        "-e '/Reformatting entry.  Wait/d'"
        "-e '/^[ \t]*Hewlett-Packard[ \t]Company[ \t]*-[ \t][0-9]*[ \t]-/d'"
@@ -360,6 +354,7 @@ This is necessary if one wants to dump man.el with emacs."
        "-e '/^[A-za-z].*Last[ \t]change:/d'"
        "-e '/^Sun[ \t]Release[ \t][0-9].*[0-9]$/d'"
        "-e '/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d'"
+       "-e '/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d'"
        ))
      (cons
       Man-awk-command
@@ -428,17 +423,17 @@ that string instead of from the current buffer."
   "Translates REF from \"chmod(2V)\" to \"2v chmod\" style.
 Leave it as is if already in that style.  Possibly downcase and
 translate the section (see the Man-downcase-section-letters-flag
-and the Man-section-translations-alist variables)." 
+and the Man-section-translations-alist variables)."
   (let ((name "")
        (section "")
        (slist Man-section-translations-alist))
     (cond
      ;; "chmod(2V)" case ?
-     ((string-match (concat Man-reference-regexp "$") ref)
+     ((string-match (concat "^" Man-reference-regexp "$") ref)
       (setq name (Man-match-substring 1 ref)
            section (Man-match-substring 2 ref)))
      ;; "2v chmod" case ?
-     ((string-match (concat "\\(" Man-section-regexp
+     ((string-match (concat "^\\(" Man-section-regexp
                            "\\) +\\(" Man-name-regexp "\\)$") ref)
       (setq name (Man-match-substring 2 ref)
            section (Man-match-substring 1 ref))))
@@ -483,13 +478,15 @@ default section number is selected from `Man-auto-section-alist'."
             (progn (skip-chars-forward "-a-zA-Z0-9_.") (point))))
       
       ;; If looking at something like ioctl(2) or brc(1M), include the
-      ;; section number in the returned value.
-      (concat
-       default-title
-       (if (looking-at
-           (concat "[ \t]*([ \t]*\\(" Man-section-regexp "\\)[ \t]*)"))
-          (format "(%s)" (Man-match-substring 1)))
-       ))))
+      ;; section number in the returned value.  Remove text properties.
+      (let ((result (concat
+                    default-title
+                    (if (looking-at
+                         (concat "[ \t]*([ \t]*\\("
+                                 Man-section-regexp "\\)[ \t]*)"))
+                        (format "(%s)" (Man-match-substring 1))))))
+       (set-text-properties 0 (length result) nil result)
+       result))))
 
 \f
 ;; ======================================================================
@@ -500,31 +497,25 @@ default section number is selected from `Man-auto-section-alist'."
 (defalias 'manual-entry 'man)
 
 ;;;###autoload
-(defun man (man-args prefix-arg)
+(defun man (man-args)
   "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
 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-method' 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-flag'
-overrides this and forces the man page to be regenerated."
+If a buffer already exists for this man page, it will display immediately."
   (interactive
-   (list
-    ;; first argument
-    (let* ((default-entry (Man-default-man-entry))
-          (input (read-string
-                  (format "Manual entry%s: "
-                          (if (string= default-entry "")
-                              ""
-                            (format " (default %s)" default-entry))))))
-      (if (string= input "")
-         (if (string= default-entry "")
-             (error "No man args given")
-           default-entry)
-       input))
-    ;; second argument
-    current-prefix-arg))
+   (list (let* ((default-entry (Man-default-man-entry))
+               (input (read-string
+                       (format "Manual entry%s: "
+                               (if (string= default-entry "")
+                                   ""
+                                 (format " (default %s)" default-entry))))))
+          (if (string= input "")
+              (if (string= default-entry "")
+                  (error "No man args given")
+                default-entry)
+            input))))
 
   ;; Init the man package variables, if not already done.
   (Man-init-defvars)
@@ -533,20 +524,15 @@ overrides this and forces the man page to be regenerated."
   ;; "section subject" syntax and possibly downcase the section.
   (setq man-args (Man-translate-references man-args))
 
-  (Man-getpage-in-background man-args (consp prefix-arg)))
+  (Man-getpage-in-background man-args))
 
 
-(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-flag' is non-nil."
+(defun Man-getpage-in-background (topic)
+  "Uses TOPIC to build and fire off the manpage and cleaning command."
   (let* ((man-args topic)
         (bufname (concat "*Man " man-args "*"))
         (buffer  (get-buffer bufname)))
-    (if (and Man-reuse-okay-flag
-            (not override-reuse-p)
-            buffer)
+    (if buffer
        (Man-notify-when-ready buffer)
       (require 'env)
       (message "Invoking %s %s in the background" manual-program man-args)
@@ -561,8 +547,7 @@ start a background process even if a buffer already exists and
        (set-process-sentinel
         (start-process manual-program buffer "sh" "-c"
                        (format (Man-build-man-command) man-args))
-        'Man-bgproc-sentinel))
-      )))
+        'Man-bgproc-sentinel)))))
 
 (defun Man-notify-when-ready (man-buffer)
   "Notify the user when MAN-BUFFER is ready.
@@ -619,27 +604,30 @@ Same for the ANSI bold and normal escape sequences."
                       (progn (if (search-forward "\e[0m" nil 'move)
                                  (delete-backward-char 4))
                              (point))
-                      'face 'bold))
+                      'face Man-overstrike-face))
   (goto-char (point-min))
   (while (search-forward "_\b" nil t)
     (backward-delete-char 2)
-    (put-text-property (point) (1+ (point)) 'face 'underline))
+    (put-text-property (point) (1+ (point)) 'face Man-underline-face))
   (goto-char (point-min))
   (while (search-forward "\b_" nil t)
     (backward-delete-char 2)
-    (put-text-property (1- (point)) (point) 'face 'underline))
+    (put-text-property (1- (point)) (point) 'face Man-underline-face))
   (goto-char (point-min))
   (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
     (replace-match "\\1")
-    (put-text-property (1- (point)) (point) 'face 'bold))
+    (put-text-property (1- (point)) (point) 'face Man-overstrike-face))
   (goto-char (point-min))
-  (while (search-forward "o\b+" nil t)
-    (backward-delete-char 2)
+  (while (re-search-forward "o\b\\+\\|\\+\bo" nil t)
+    (replace-match "o")
     (put-text-property (1- (point)) (point) 'face 'bold))
   (goto-char (point-min))
   (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t)
     (replace-match "+")
     (put-text-property (1- (point)) (point) 'face 'bold))
+  ;; \255 is some kind of dash in Latin-1.
+  (goto-char (point-min))
+  (while (search-forward "\255" nil t) (replace-match "-"))
   (message "%s man page made up" Man-arguments))
 
 (defun Man-cleanup-manpage ()
@@ -659,10 +647,13 @@ Same for the ANSI bold and normal escape sequences."
        (goto-char (point-min))
        (while (re-search-forward "\e\\[[0-9]+m" nil t) (replace-match ""))
        (goto-char (point-min))
-       (while (search-forward "o\b+" nil t) (backward-delete-char 2))
+       (while (re-search-forward "o\b\\+\\|\\+\bo" nil t) (replace-match "o"))
        ))
   (goto-char (point-min))
   (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t) (replace-match "+"))
+  ;; \255 is some kind of dash in Latin-1.
+  (goto-char (point-min))
+  (while (search-forward "\255" nil t) (replace-match "-"))
   (message "%s man page cleaned up" Man-arguments))
 
 (defun Man-bgproc-sentinel (process msg)
@@ -676,44 +667,43 @@ Same for the ANSI bold and normal escape sequences."
 
       (save-excursion
        (set-buffer Man-buffer)
-       (save-match-data
-         (let ((case-fold-search nil))
-           (goto-char (point-min))
-           (cond ((or (looking-at "No \\(manual \\)*entry for")
-                      (looking-at "[^\n]*: nothing appropriate$"))
-                  (setq err-mess (buffer-substring (point)
-                                                   (progn
-                                                     (end-of-line) (point)))
-                        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)
-             (if Man-fontify-manpage-flag
-                 (Man-fontify-manpage)
-               (Man-cleanup-manpage))
-             (run-hooks 'Man-cooked-hook)
-             (Man-mode)
-             (set-buffer-modified-p nil)
-             ))
-         ;; Restore case-fold-search before calling
-         ;; Man-notify-when-ready because it may switch buffers.
-
-         (if (not delete-buff)
-             (Man-notify-when-ready Man-buffer))
-
-         (if err-mess
-             (error err-mess))
-         )))))
+       (let ((case-fold-search nil))
+         (goto-char (point-min))
+         (cond ((or (looking-at "No \\(manual \\)*entry for")
+                    (looking-at "[^\n]*: nothing appropriate$"))
+                (setq err-mess (buffer-substring (point)
+                                                 (progn
+                                                   (end-of-line) (point)))
+                      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)
+           (if Man-fontify-manpage-flag
+               (Man-fontify-manpage)
+             (Man-cleanup-manpage))
+           (run-hooks 'Man-cooked-hook)
+           (Man-mode)
+           (set-buffer-modified-p nil)
+           ))
+       ;; Restore case-fold-search before calling
+       ;; Man-notify-when-ready because it may switch buffers.
+
+       (if (not delete-buff)
+           (Man-notify-when-ready Man-buffer))
+
+       (if err-mess
+           (error err-mess))
+       ))))
 
 \f
 ;; ======================================================================
@@ -741,7 +731,6 @@ The following variables may be of some use. Try
 \"\\[describe-variable] <variable-name> RET\" for more information:
 
 Man-notify-method               What happens when manpage formatting is done.
-Man-reuse-okay-flag             Reuse already formatted buffer.
 Man-downcase-section-letters-flag  Force section letters to lower case.
 Man-circular-pages-flag         Treat multiple manpage list as circular.
 Man-auto-section-alist          List of major modes and their section numbers.
@@ -966,15 +955,13 @@ Actually the section moved to is described by `Man-see-also-regexp'."
       (error (concat "No " Man-see-also-regexp
                     " section found in the current manpage"))))
 
-(defun Man-follow-manual-reference (arg reference)
+(defun Man-follow-manual-reference (reference)
   "Get one of the manpages referred to in the \"SEE ALSO\" section.
-Specify which reference to use; default is based on word at point.
-Prefix argument ARG is passed to `Man-getpage-in-background'."
+Specify which reference to use; default is based on word at point."
   (interactive
    (if (not Man-refpages-alist)
        (error "There are no references in the current man page")
-     (list current-prefix-arg
-          (let* ((default (or
+     (list (let* ((default (or
                             (car (all-completions
                                   (save-excursion
                                     (skip-syntax-backward "w()")
@@ -998,8 +985,7 @@ Prefix argument ARG is passed to `Man-getpage-in-background'."
       (error "Can't find any references in the current manpage")
     (aput 'Man-refpages-alist reference)
     (Man-getpage-in-background
-     (Man-translate-references (aheadsym Man-refpages-alist))
-     arg)))
+     (Man-translate-references (aheadsym Man-refpages-alist)))))
 
 (defun Man-kill ()
   "Kill the buffer containing the manpage."