home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / packages / man+.el < prev    next >
Encoding:
Text File  |  1990-07-22  |  14.2 KB  |  364 lines

  1. ;From ark1!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!usc!ucsd!ucbvax!SOMEWHERE.BERKELEY.EDU!aks Fri May 18 20:08:09 EDT 1990
  2. ;Article 1962 of comp.emacs:
  3. ;Path: ark1!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!usc!ucsd!ucbvax!SOMEWHERE.BERKELEY.EDU!aks
  4. ;>From: aks@SOMEWHERE.BERKELEY.EDU (Alan Stebbens)
  5. ;Newsgroups: comp.emacs
  6. ;Subject: Fix to man.el to use MANPATH
  7. ;Message-ID: <9005180023.AA16456@somewhere>
  8. ;Date: 18 May 90 00:23:52 GMT
  9. ;Sender: daemon@ucbvax.BERKELEY.EDU
  10. ;Lines: 350
  11. ;
  12. ;After avoiding the use of M-x manual-entry because it didn't know
  13. ;about alternate man directories, as given by MANPATH, it finally
  14. ;occurred to me that it's silly to not use Emacs to read the man
  15. ;pages, and that it shouldn't be that hard to make "manual-entry"
  16. ;Do The Right Thing.  Well it wasn't very hard, and it wasn't a
  17. ;major rewrite, although it was more than a minor one.
  18. ;
  19. ;The features of this version are:
  20. ;
  21. ;  o  Match multiple man pages using TOPIC as a simple pattern
  22. ;  o  Search unformatted pages, even when formatted matches are found
  23. ;  o  Query the user as to which pages are desired
  24. ;  o  Use of the prefix arg to toggle/bypass the above features
  25. ;  o  Buffers named by the first topic in the buffer
  26. ;  o  Automatic uncompress for compressed man pages (.Z and .z)
  27. ;  o  View the resulting buffer using M-x view mode
  28. ;
  29. ;All the features may be disabled to achieve the (limited)
  30. ;features of the original M-x manual-entry.
  31. ;
  32. ;This is a first cut.  Send improvements, fixes, and comments to me
  33. ;via email.  
  34. ;
  35. ;Alan Stebbens        <aks@hub.ucsb.edu>             (805) 961-3221
  36. ;     Center for Computational Sciences and Engineering (CCSE)
  37. ;          University of California, Santa Barbara (UCSB)
  38. ;           3111 Engineering I, Santa Barbara, CA 93106
  39. ;
  40. ;
  41. ;The following is the complete file; the diffs were so extensive as
  42. ;to be almost as large as the file itself.
  43. ;======================================================================
  44. ;; Read in and display parts of Unix manual.
  45. ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  46.  
  47. ;; This file is part of GNU Emacs.
  48.  
  49. ;; GNU Emacs is distributed in the hope that it will be useful,
  50. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  51. ;; accepts responsibility to anyone for the consequences of using it
  52. ;; or for whether it serves any particular purpose or works at all,
  53. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  54. ;; License for full details.
  55.  
  56. ;; Everyone is granted permission to copy, modify and redistribute
  57. ;; GNU Emacs, but only under the conditions described in the
  58. ;; GNU Emacs General Public License.   A copy of this license is
  59. ;; supposed to have been given to you along with GNU Emacs so you
  60. ;; can know your rights and responsibilities.  It should be in a
  61. ;; file named COPYING.  Among other things, the copyright notice
  62. ;; and this notice must be preserved on all copies.
  63. ;;
  64. ;;
  65. ;; Written by Alan K. Stebbens, CCSE, Univ. of CA, Santa Barbara
  66. ;;
  67. ;; This file defines "manual-entry", and the remaining definitions all
  68. ;; begin with "Manual-".  This makes the autocompletion on "M-x man" work.
  69. ;;
  70. ;; Variables of interest:
  71. ;;
  72. ;;    Manual-program
  73. ;;    Manual-topic-buffer
  74. ;;    Manual-buffer-view-mode
  75. ;;    Manual-directory-list
  76. ;;    Manual-formatted-directory-list
  77. ;;    Manual-match-topic-exactly
  78. ;;    Manual-query-multiple-pages
  79. ;;
  80. ;; Last edited:
  81. ;; 
  82. ;; Thu May 17 14:35:17 1990 by Alan Stebbens (aks at somewhere)
  83. ;;      Added force -- allow unformatted search even with
  84. ;;      formatted finds.
  85. ;;      Broke out heavily nested mapcars into the functions
  86. ;;      Manual-select-directories and Manual-select-man-pages.
  87. ;;      Fixed bugs.
  88. ;;      Renamed "Manual.el"
  89. ;;      Added multiple man pages query code.
  90. ;; 
  91. ;; Wed Apr 11 13:42:31 1990 by Alan Stebbens (aks at somewhere)
  92. ;;      Initial rewrite
  93.  
  94. (defconst Manual-program "man" "\
  95. *Name of the program to invoke in order to format the source man pages.")
  96.  
  97. (defconst Manual-topic-buffer t "\
  98. *Non-nil means \\[Manual-entry] should output the manual entry for TOPIC into
  99. a buffer named *TOPIC Manual Entry*, otherwise, it should name the buffer
  100. *Manual Entry*.")
  101.  
  102. (defconst Manual-buffer-view-mode t "\
  103. *Non-nil means that \\[view-buffer] is used to display the output from
  104. \\[Manual-entry]; nil means that the buffer is left in fundamental-mode
  105. in another window.")
  106.  
  107. (defconst Manual-match-topic-exactly nil "\
  108. *Non-nil means that \\[manual-entry] will match the given TOPIC exactly, rather
  109. apply it as a pattern.  When this is nil, and \"Manual-query-multiple-pages\" is
  110. non-nil, then \\[manual-entry] will query you for all matching TOPICs.
  111. This variable only has affect on the preformatted man pages (the \"cat\" files),
  112. since the \"man\" command always does exact topic matches.")
  113.  
  114. (defconst Manual-query-multiple-pages t "\
  115. *Non-nil means that \\[manual-entry] will query the user about multiple man
  116. pages which match the given topic.  The query is done using the function 
  117. \"y-or-n-p\".  If this variable is nil, all man pages with topics matching the
  118. topic given to \\[manual-entry] will be inserted into the temporary buffer.
  119. See the variable \"Manual-match-topic-exactly\" to control the matching.")
  120.  
  121. (defconst Manual-directory-list nil "\
  122. *A list of directories used with the \"man\" command, where each directory
  123. contains a set of \"man?\" and \"cat?\" subdirectories.  If this variable is nil,
  124. it is initialized by \\[Manual-directory-list-init].")
  125.  
  126. (defvar Manual-formatted-directory-list nil "\
  127. A list of directories containing formatted man pages.  Initialized by
  128. \\[Manual-directory-list-init].")
  129.  
  130. (defvar Manual-unformatted-directory-list nil "\
  131. A list of directories containing the unformatted (source) man pages.  Initialized
  132. by \\[Manual-directory-list-init].")
  133.  
  134. ;; Manual-directory-list-init
  135. ;; Initialize the directory lists.
  136.  
  137. (defun Manual-directory-list-init (&optional arg) "\
  138. Unless the variable Manual-directory-list is nil, initialize it using the
  139. MANPATH environment variable.  Once this variable is set,
  140. \\[Manual-directory-list-init] will not reinitialize it unless a prefix
  141. argument is given."
  142.   (interactive "P")
  143.   (if arg (setq Manual-directory-list nil))
  144.   (if (null Manual-directory-list)
  145.       (let ((manpath (getenv "MANPATH")) dirlist)
  146.     (while (and manpath (not (null (string-match "[^:]+" manpath))))
  147.       (setq dirlist (nconc dirlist (list (substring manpath 0 (match-end 0)))))
  148.       (setq manpath (if (< (match-end 0) (length manpath))
  149.                 (substring manpath (1+ (match-end 0))))))
  150.     (setq Manual-directory-list dirlist)
  151.     (setq Manual-formatted-directory-list nil)
  152.     (setq Manual-unformatted-directory-list nil)))
  153.   (if (null Manual-formatted-directory-list)
  154.       (setq Manual-formatted-directory-list
  155.         (Manual-select-subdirectories Manual-directory-list "cat")))
  156.   (if (null Manual-unformatted-directory-list)
  157.       (setq Manual-unformatted-directory-list
  158.         (Manual-select-subdirectories Manual-directory-list "man"))))
  159.  
  160. ;;
  161. ;; manual-entry  -- The "main" user function
  162. ;;
  163.  
  164. (defun manual-entry (topic arg)
  165.   "Display the Unix manual entry (or entries) for TOPIC.  If prefix
  166. arg is given, modify the search according to the value:
  167.   2 = toggle exact matching of the TOPIC name
  168.   3 = force a search of the unformatted man directories
  169.   4 = both 2 and 3
  170. The manual entries are searched according to the variable
  171. Manual-directory-list, which should be a list of directories.  If
  172. Manual-directory-list is nil, \\[Manual-directory-list-init] is
  173. invoked to create this list from the MANPATH environment variable.
  174. See the variable Manual-topic-buffer which controls how the buffer
  175. is named.  See also the variables Manual-match-topic-exactly,
  176. Manual-query-multiple-pages, and Manual-buffer-view-mode."
  177.   (interactive "sManual entry (topic): \np")
  178.   (Manual-directory-list-init nil)
  179.   (let ((case-fold-search nil)        ; let search be easy
  180.     (hook temp-buffer-show-hook)
  181.     (exact (if (or (= arg 2)(= arg 4))
  182.            (not Manual-match-topic-exactly)
  183.          Manual-match-topic-exactly))
  184.     (force (>= arg 3))
  185.     section fmtlist manlist)
  186.     (if (and (null section)
  187.          (string-match "\\`[ \t]*\\([^( \t]+\\)[ \t]*(\\(.+\\))[ \t]*\\'" topic))
  188.     (setq section (substring topic (match-beginning 2)
  189.                  (match-end 2))
  190.           topic (substring topic (match-beginning 1)
  191.                    (match-end 1))))
  192.     (message "Looking for formatted entry for %s%s..."
  193.          topic (if section (concat "(" section ")") ""))
  194.     (setq fmtlist (Manual-select-man-pages
  195.            (Manual-select-directories Manual-formatted-directory-list section) 
  196.            topic section exact))
  197.     (if (or force (not fmtlist))
  198.     (progn
  199.       (message "%sooking for unformatted entry for %s%s..."
  200.            (if fmtlist "L" "No formatted entry, l")
  201.            topic (if section (concat "(" section ")") ""))
  202.       (setq manlist (Manual-select-man-pages
  203.              (Manual-select-directories Manual-unformatted-directory-list section)
  204.              topic section exact))))
  205.     (if (or fmtlist manlist)
  206.     (let* ((name (car (or fmtlist manlist)))
  207.            (bufname (concat (if Manual-topic-buffer
  208.                     (concat "*" (and (string-match "/\\([^/]+\\)$" name)
  209.                              (substring name (match-beginning 1) (match-end 1)))
  210.                         " "))
  211.                 "*Manual Entry*")))
  212.       (with-output-to-temp-buffer bufname
  213.         (buffer-flush-undo standard-output)
  214.         (save-excursion
  215.           (set-buffer standard-output)
  216.           (if fmtlist        ; insert any formatted files
  217.           (mapcar '(lambda (name)
  218.                  (goto-char (point-max))
  219.                  ;; In case the file can't be read or uncompressed or something like that.
  220.                  (condition-case ()
  221.                  (Manual-insert-man-file name)
  222.                    (file-error nil)))
  223.               fmtlist))
  224.           (if manlist        ; process any unformatted files
  225.           (mapcar '(lambda (name)
  226.                  (let (topic section)
  227.                    (string-match "\\([^/]+\\)\\.\\([^./]+\\)$" name)
  228.                    (setq topic (substring name (match-beginning 1) (match-end 1)))
  229.                    (setq section (substring name (match-beginning 2) (match-end 2)))
  230.                    (message "Invoking man %s %s ..." section topic)
  231.                    (call-process Manual-program nil t nil section topic)))
  232.               manlist))
  233.           (if (< (buffer-size) 80)
  234.           (progn
  235.             (goto-char (point-min))
  236.             (end-of-line)
  237.             (error (buffer-substring 1 (point)))))
  238.           (message "Cleaning manual entr%s..." 
  239.                (if (> (length (or fmtlist manlist)) 1) "ies" (concat "y for " topic)))
  240.           (Manual-nuke-nroff-bs)
  241.           (set-buffer-modified-p nil)
  242.           (setq hook temp-buffer-show-hook)
  243.           (if Manual-buffer-view-mode
  244.           (setq temp-buffer-show-hook 'view-buffer))
  245.           (message ""))))
  246.       (message "No entries found for %s%s" topic (if section (concat "(" section ")") "")))
  247.     (setq temp-buffer-show-hook hook)))
  248.  
  249. ;; Manual-select-subdirectories
  250. ;; Given a DIRLIST and a SUBDIR name, return all subdirectories of the former which
  251. ;; match the latter.
  252.  
  253. (defun Manual-select-subdirectories (dirlist subdir)
  254.   (apply 'append (mapcar '(lambda (dir)
  255.                 (mapcar '(lambda (name) (concat dir "/" name))
  256.                     (file-name-all-completions subdir dir)))
  257.              dirlist)))
  258.  
  259. ;; Manual-select-directories
  260. ;;
  261. ;; Select from DIRLIST the appropriate directories by SECTION.
  262. ;; Return selected directories in a list.  If SECTION is nil, select
  263. ;; all SECTION directories.
  264.  
  265. (defun Manual-select-directories (dirlist section)
  266.   (mapcar '(lambda (fmtdir)
  267.          (if (or (not section)
  268.              (string-match (concat (substr section 0 1) "/$") fmtdir))
  269.          fmtdir))
  270.       dirlist))
  271.  
  272. ;; Manual-select-man-pages
  273. ;;
  274. ;; Given a DIRLIST, discover all filenames which complete given the TOPIC and SECTION.
  275.  
  276. (defun Manual-select-man-pages (dirlist topic section exact)
  277.   (let ((manlist (apply 'append        ; this removes the nulls
  278.             (mapcar '(lambda (dir)
  279.                    (mapcar '(lambda (file)
  280.                           ;; If Manual-match-topic-exactly is set, then we must make sure
  281.                           ;; the completions are exact, except for trailing weird characters
  282.                           ;; after the section.
  283.                           (if (or (not exact)
  284.                               (eq 0 (string-match (concat "^" topic "\\." (or section)) file)))
  285.                           (concat dir file)))
  286.                        (file-name-all-completions (concat topic (if section (concat "." section))) dir)))
  287.                 dirlist))))
  288.     (if (and manlist Manual-query-multiple-pages)
  289.     (apply 'append
  290.            (mapcar '(lambda (page)
  291.               (if (and page 
  292.                    (y-or-n-p (format "Read %s? " page)))
  293.                   (list page)))
  294.                manlist))
  295.       manlist)))
  296.  
  297. ;; Hint: BS stands form more things than "back space"
  298. (defun Manual-nuke-nroff-bs ()
  299.   (interactive "*")
  300.   ;; Nuke underlining and overstriking (only by the same letter)
  301.   (goto-char (point-min))
  302.   (while (search-forward "\b" nil t)
  303.     (let* ((preceding (char-after (- (point) 2)))
  304.        (following (following-char)))
  305.       (cond ((or (= preceding following)    ; x\bx
  306.          (= preceding ?\_))         ; _\b
  307.          (delete-char -2))
  308.         ((or (= following ?\_)        ; \b_
  309.          (= following ?\ ))             ; \b(SPACE)
  310.          (delete-region (1- (point)) (1+ (point))))
  311.         (t (delete-char -1)))))        ; \b by itself (remove it)
  312.         
  313.  
  314.   ;; Nuke headers: "MORE(1) UNIX Programmer's Manual MORE(1)"
  315.   (goto-char (point-min))
  316.   (while (re-search-forward "^ *\\([A-Za-z][-_A-Za-z0-9]*([0-9A-Za-z]+)\\).*\\1$" nil t)
  317.     (replace-match ""))
  318.   
  319.   ;; Nuke footers: "Printed 12/3/85    27 April 1981    1"
  320.   ;;    Sun appear to be on drugz:
  321.   ;;     "Sun Release 3.0B  Last change: 1 February 1985     1"
  322.   ;;    HP are even worse!
  323.   ;;     "     Hewlett-Packard   -1- (printed 12/31/99)"  FMHWA12ID!!
  324.   ;;    System V (well WICATs anyway):
  325.   ;;     "Page 1              (printed 7/24/85)"
  326.   ;;    Who is administering PCP to these corporate bozos?
  327.   (goto-char (point-min))
  328.   (while (re-search-forward
  329.        (cond ((eq system-type 'hpux)
  330.           "^[ \t]*Hewlett-Packard\\(\\| Company\\)[ \t]*- [0-9]* -.*$")
  331.          ((eq system-type 'usg-unix-v)
  332.           "^ *Page [0-9]*.*(printed [0-9/]*)$")
  333.          (t
  334.           "^\\(Printed\\|Sun Release\\) [0-9].*[0-9]$"))
  335.        nil t)
  336.     (replace-match ""))
  337.  
  338.   ;; Zap ESC7,  ESC8, and ESC9
  339.   ;; This is for Sun man pages like "man 1 csh"
  340.   (goto-char (point-min))
  341.   (while (re-search-forward "\e[789]" nil t)
  342.     (replace-match ""))
  343.  
  344.   ;; Crunch blank lines
  345.   (goto-char (point-min))
  346.   (while (re-search-forward "\n\n\n\n*" nil t)
  347.     (replace-match "\n\n"))
  348.  
  349.   ;; Nuke blanks lines at start.
  350.   (goto-char (point-min))
  351.   (skip-chars-forward "\n")
  352.   (delete-region (point-min) (point)))
  353.  
  354.  
  355. (defun Manual-insert-man-file (name)
  356.   ;; Insert manual file (unpacked as necessary) into buffer
  357.   (if (equal (substring name -2) ".Z")
  358.       (call-process "zcat" nil t nil name)
  359.     (if (equal (substring name -2) ".z")
  360.     (call-process "pcat" nil t nil name)
  361.       (insert-file-contents name))))
  362.  
  363.  
  364.