]> code.delx.au - gnu-emacs/blobdiff - lisp/man.el
(diary-display-hook): Change default and fix doc string.
[gnu-emacs] / lisp / man.el
index 2130f902a3577b3a109481490f1eb35d66d35ee5..1e60e660b49962844a5d5f955dce432c2436cdfe 100644 (file)
@@ -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.
@@ -72,7 +72,6 @@
 ;;; Code:
 
 (require 'assoc)
-(provide 'man)
 
 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
 ;; user variables
@@ -94,27 +93,31 @@ Any other value of `Man-notify' is equivalent to `meek'.")
   "*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
+`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,16 +125,18 @@ 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
+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'"
@@ -144,12 +149,12 @@ the associated section number.")
       "-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"
       "'"
       ))
      )
@@ -159,50 +164,49 @@ 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
+       " " Man-page-mode-string
        "    %[(" mode-name minor-mode-alist mode-line-process ")%]----"
        (-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-hooks nil
-  "*Hooks for Man-mode.")
+  "*Hooks for Man mode.")
 
 (defvar Man-section-regexp "[0-9][a-zA-Z+]*"
   "*Regular expression describing a manpage section within parentheses.")
 
-(defvar Man-heading-regexp "^[A-Z]"
+(defvar Man-heading-regexp "^ ?[A-Z]"
   "*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 "^ ?NAME$\\|^ ?No manual entry for .*$"
   "*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.")
 
 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 ;; 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)
@@ -235,7 +239,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)
   )
 
@@ -254,7 +257,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 "man " Man-switches " %s 2>&1"))
        (flist Man-filter-list))
     (while flist
       (let ((pcom (car (car flist)))
@@ -263,11 +266,9 @@ This regular expression should start with a `^' character.")
        (if (or (not (stringp pcom))
                (not (listp pargs)))
            (error "malformed Man-filter-list."))
-       (setq command (concat command pcom
+       (setq command (concat command " | " pcom
                              (mapconcat '(lambda (phrase) phrase)
-                                        pargs " "))))
-      (if flist
-         (setq command (concat command " | " ))))
+                                        pargs " ")))))
     command))
 
 (defun Man-downcase (man-args)
@@ -314,7 +315,7 @@ This regular expression should start with a `^' character.")
 
 (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
@@ -322,7 +323,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
@@ -340,30 +342,20 @@ 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 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,
@@ -382,33 +374,33 @@ 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 (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.
+Universal argument ARG, is passed to `Man-getpage-in-background'."
   (interactive "P")
   (let* ((default-entry (Man-default-man-entry))
         (man-args
@@ -420,24 +412,22 @@ Universal argument ARG, is passed to Man-getpage-in-background."
         (if (string= default-entry "")
             (error "No man args given.")
           (setq man-args default-entry)))
+
     ;; Recognize the subject(section) syntax.
-    (if (string-match "^[ \t]*\\([^( \t]+\\)[ \t]*(\\([^)]+\\))[ \t]*$"
-                     man-args)
-       (setq man-args
-             (concat (substring man-args (match-beginning 2) (match-end 2))
-                     " "
-                     (substring man-args (match-beginning 1) (match-end 1)))))
+    (setq man-args (Man-translate-references man-args))
+
     (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)
@@ -445,7 +435,7 @@ Man-reuse-okay-p is non-nil."
        (Man-notify-when-ready buffer)
       (message "Invoking man %s in background..." man-args)
       (setq buffer (generate-new-buffer bufname))
-      (let ((process-environment process-environment))
+      (let ((process-environment (copy-sequence process-environment)))
        ;; Prevent any attempt to use display terminal fanciness.
        (setenv "TERM" "dumb")
        (set-process-sentinel
@@ -475,6 +465,17 @@ See the variable `Man-notify' for the different notification behaviors."
     (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."
   (let ((Man-buffer (process-buffer process))
@@ -506,6 +507,7 @@ See the variable `Man-notify' for the different notification behaviors."
        (save-window-excursion
          (save-excursion
            (set-buffer Man-buffer)
+           (Man-set-fonts)
            (Man-mode)
            (set-buffer-modified-p nil)))
        (Man-notify-when-ready Man-buffer))
@@ -519,7 +521,7 @@ See the variable `Man-notify' for the different notification behaviors."
 ;; 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:
@@ -533,7 +535,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
@@ -554,13 +555,13 @@ 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
@@ -599,9 +600,8 @@ The following key bindings are currently in effect in the buffer:
          (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)))
+           (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
@@ -661,7 +661,7 @@ The following key bindings are currently in effect in the buffer:
 Returns t if section is found, nil otherwise."
   (let ((curpos (point)))
     (goto-char (point-min))
-    (if (re-search-forward (concat "^" section) (point-max) t)
+    (if (re-search-forward (concat "^\\s-?" section) (point-max) t)
        (progn (beginning-of-line) t)
       (goto-char curpos)
       nil)
@@ -684,7 +684,7 @@ 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
@@ -692,8 +692,8 @@ Actually the section moved to is described by Man-see-also-regexp."
 
 (defun Man-follow-manual-reference (arg)
   "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."
+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")
   (if (not Man-refpages-alist)
       (error (concat "No references found in current manpage."))
@@ -766,15 +766,7 @@ background. Universal argument ARG is passed to Man-getpage-in-background."
     (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)))
+\f
+(provide 'man)
 
 ;;; man.el ends here