home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / help-lucid-emacs / text0064.txt < prev    next >
Encoding:
Text File  |  1993-07-14  |  26.4 KB  |  732 lines

  1. Here's a better version of man.el; it's smarter about removing header and
  2. footer lines, uses both bold and italic, makes crossreferences be mousable,
  3. and puts a menu of crossreferences on the right button.
  4.  
  5. If you're on a system where nroff doesn't use char-backspace-char to make
  6. words bold (like Suns), you might want to get groff and put a link to gnroff
  7. named nroff on your path before the real nroff.  Then you'll see man pages in
  8. the buffer more like they'll look on paper.
  9.  
  10.     -- Jamie
  11.  
  12. ---------- slice 'n' dice --------------------------------------- file: man.el
  13. ;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
  14. ;Article 1962 of comp.emacs:
  15. ;Path: ark1!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!usc!ucsd!ucbvax!SOMEWHERE.BERKELEY.EDU!aks
  16. ;>From: aks@SOMEWHERE.BERKELEY.EDU (Alan Stebbens)
  17. ;Newsgroups: comp.emacs
  18. ;Subject: Fix to man.el to use MANPATH
  19. ;Message-ID: <9005180023.AA16456@somewhere>
  20. ;Date: 18 May 90 00:23:52 GMT
  21. ;Sender: daemon@ucbvax.BERKELEY.EDU
  22. ;Lines: 350
  23. ;
  24. ;After avoiding the use of M-x manual-entry because it didn't know
  25. ;about alternate man directories, as given by MANPATH, it finally
  26. ;occurred to me that it's silly to not use Emacs to read the man
  27. ;pages, and that it shouldn't be that hard to make "manual-entry"
  28. ;Do The Right Thing.  Well it wasn't very hard, and it wasn't a
  29. ;major rewrite, although it was more than a minor one.
  30. ;
  31. ;The features of this version are:
  32. ;
  33. ;  o  Match multiple man pages using TOPIC as a simple pattern
  34. ;  o  Search unformatted pages, even when formatted matches are found
  35. ;  o  Query the user as to which pages are desired
  36. ;  o  Use of the prefix arg to toggle/bypass the above features
  37. ;  o  Buffers named by the first topic in the buffer
  38. ;  o  Automatic uncompress for compressed man pages (.Z and .z)
  39. ;  o  View the resulting buffer using M-x view mode
  40. ;
  41. ;All the features may be disabled to achieve the (limited)
  42. ;features of the original M-x manual-entry.
  43. ;
  44. ;This is a first cut.  Send improvements, fixes, and comments to me
  45. ;via email.  
  46. ;
  47. ;Alan Stebbens        <aks@hub.ucsb.edu>             (805) 961-3221
  48. ;     Center for Computational Sciences and Engineering (CCSE)
  49. ;          University of California, Santa Barbara (UCSB)
  50. ;           3111 Engineering I, Santa Barbara, CA 93106
  51. ;
  52. ; Modified 16-mar-91 by Jamie Zawinski <jwz@lucid.com> to default the 
  53. ; manual topic to the symbol at point, just like find-tag does.
  54. ;
  55. ; Modified 22-mar-93 by jwz to use multiple fonts and follow xrefs with mouse.
  56. ;
  57. ;The following is the complete file; the diffs were so extensive as
  58. ;to be almost as large as the file itself.
  59. ;======================================================================
  60. ;; Read in and display parts of Unix manual.
  61. ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  62.  
  63. ;; This file is part of GNU Emacs.
  64.  
  65. ;; GNU Emacs is distributed in the hope that it will be useful,
  66. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  67. ;; accepts responsibility to anyone for the consequences of using it
  68. ;; or for whether it serves any particular purpose or works at all,
  69. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  70. ;; License for full details.
  71.  
  72. ;; Everyone is granted permission to copy, modify and redistribute
  73. ;; GNU Emacs, but only under the conditions described in the
  74. ;; GNU Emacs General Public License.   A copy of this license is
  75. ;; supposed to have been given to you along with GNU Emacs so you
  76. ;; can know your rights and responsibilities.  It should be in a
  77. ;; file named COPYING.  Among other things, the copyright notice
  78. ;; and this notice must be preserved on all copies.
  79. ;;
  80. ;;
  81. ;; Written by Alan K. Stebbens, CCSE, Univ. of CA, Santa Barbara
  82. ;;
  83. ;; This file defines "manual-entry", and the remaining definitions all
  84. ;; begin with "Manual-".  This makes the autocompletion on "M-x man" work.
  85. ;;
  86. ;; Variables of interest:
  87. ;;
  88. ;;    Manual-program
  89. ;;    Manual-topic-buffer
  90. ;;    Manual-buffer-view-mode
  91. ;;    Manual-directory-list
  92. ;;    Manual-formatted-directory-list
  93. ;;    Manual-match-topic-exactly
  94. ;;    Manual-query-multiple-pages
  95. ;;
  96. ;; Last edited:
  97. ;; 
  98. ;; Thu May 17 14:35:17 1990 by Alan Stebbens (aks at somewhere)
  99. ;;      Added force -- allow unformatted search even with
  100. ;;      formatted finds.
  101. ;;      Broke out heavily nested mapcars into the functions
  102. ;;      Manual-select-directories and Manual-select-man-pages.
  103. ;;      Fixed bugs.
  104. ;;      Renamed "Manual.el"
  105. ;;      Added multiple man pages query code.
  106. ;; 
  107. ;; Wed Apr 11 13:42:31 1990 by Alan Stebbens (aks at somewhere)
  108. ;;      Initial rewrite
  109.  
  110. (defvar Manual-program "man" "\
  111. *Name of the program to invoke in order to format the source man pages.")
  112.  
  113. (defvar Manual-topic-buffer t "\
  114. *Non-nil means \\[Manual-entry] should output the manual entry for TOPIC into
  115. a buffer named *TOPIC Manual Entry*, otherwise, it should name the buffer
  116. *Manual Entry*.")
  117.  
  118. (defvar Manual-buffer-view-mode t "\
  119. *Non-nil means that \\[view-buffer] is used to display the output from
  120. \\[Manual-entry]; nil means that the buffer is left in fundamental-mode
  121. in another window.")
  122.  
  123. (defvar Manual-match-topic-exactly t "\
  124. *Non-nil means that \\[manual-entry] will match the given TOPIC exactly, rather
  125. apply it as a pattern.  When this is nil, and \"Manual-query-multiple-pages\" is
  126. non-nil, then \\[manual-entry] will query you for all matching TOPICs.
  127. This variable only has affect on the preformatted man pages (the \"cat\" files),
  128. since the \"man\" command always does exact topic matches.")
  129.  
  130. (defvar Manual-query-multiple-pages nil "\
  131. *Non-nil means that \\[manual-entry] will query the user about multiple man
  132. pages which match the given topic.  The query is done using the function 
  133. \"y-or-n-p\".  If this variable is nil, all man pages with topics matching the
  134. topic given to \\[manual-entry] will be inserted into the temporary buffer.
  135. See the variable \"Manual-match-topic-exactly\" to control the matching.")
  136.  
  137. (defvar Manual-directory-list nil "\
  138. *A list of directories used with the \"man\" command, where each directory
  139. contains a set of \"man?\" and \"cat?\" subdirectories.  If this variable is nil,
  140. it is initialized by \\[Manual-directory-list-init].")
  141.  
  142. (defvar Manual-formatted-directory-list nil "\
  143. A list of directories containing formatted man pages.  Initialized by
  144. \\[Manual-directory-list-init].")
  145.  
  146. (defvar Manual-unformatted-directory-list nil "\
  147. A list of directories containing the unformatted (source) man pages.  
  148. Initialized by \\[Manual-directory-list-init].")
  149.  
  150. ;; Manual-directory-list-init
  151. ;; Initialize the directory lists.
  152.  
  153. (defun Manual-directory-list-init (&optional arg) "\
  154. Unless the variable Manual-directory-list is nil, initialize it using the
  155. MANPATH environment variable.  Once this variable is set,
  156. \\[Manual-directory-list-init] will not reinitialize it unless a prefix
  157. argument is given."
  158.   (interactive "P")
  159.   (if arg (setq Manual-directory-list nil))
  160.   (if (null Manual-directory-list)
  161.       (let ((manpath (or (getenv "MANPATH") ""))
  162.         (dirlist nil))
  163.     (while (string-match "\\`[^:]+\\(:*\\)" manpath)
  164.       (setq dirlist (cons (substring manpath 0 (match-beginning 1))
  165.                   dirlist))
  166.       (setq manpath (substring manpath (match-end 0))))
  167.     (setq dirlist (nreverse dirlist))
  168.     (setq Manual-directory-list dirlist)
  169.     (setq Manual-formatted-directory-list nil)
  170.     (setq Manual-unformatted-directory-list nil)))
  171.   (if (null Manual-formatted-directory-list)
  172.       (setq Manual-formatted-directory-list
  173.         (Manual-select-subdirectories Manual-directory-list "cat")))
  174.   (if (null Manual-unformatted-directory-list)
  175.       (setq Manual-unformatted-directory-list
  176.         (Manual-select-subdirectories Manual-directory-list "man"))))
  177.  
  178. ;;
  179. ;; manual-entry  -- The "main" user function
  180. ;;
  181.  
  182. (defun manual-entry (topic &optional arg silent)
  183.   "Display the Unix manual entry (or entries) for TOPIC.  If prefix
  184. arg is given, modify the search according to the value:
  185.   2 = toggle exact matching of the TOPIC name
  186.   3 = force a search of the unformatted man directories
  187.   4 = both 2 and 3
  188. The manual entries are searched according to the variable
  189. Manual-directory-list, which should be a list of directories.  If
  190. Manual-directory-list is nil, \\[Manual-directory-list-init] is
  191. invoked to create this list from the MANPATH environment variable.
  192. See the variable Manual-topic-buffer which controls how the buffer
  193. is named.  See also the variables Manual-match-topic-exactly,
  194. Manual-query-multiple-pages, and Manual-buffer-view-mode."
  195.   (interactive
  196.    (list (let* ((fmh "-A-Za-z0-9_.")
  197.         (default (save-excursion
  198.                (buffer-substring
  199.                 (progn
  200.                   (re-search-backward "\\sw" nil t)
  201.                   (skip-chars-backward fmh) (point))
  202.                 (progn (skip-chars-forward fmh) (point)))))
  203.         (thing (read-string
  204.             (if (equal default "") "Manual entry: "
  205.               (concat "Manual entry: (default " default ") ")))))
  206.        (if (equal thing "") default thing))
  207.      (prefix-numeric-value current-prefix-arg)))
  208.   ;;(interactive "sManual entry (topic): \np")
  209.   (or arg (setq arg 1))
  210.   (Manual-directory-list-init nil)
  211.   (let ((case-fold-search nil)        ; let search be easy
  212.     (temp-buffer-show-function
  213.      (if Manual-buffer-view-mode 'view-buffer temp-buffer-show-function))
  214.     (exact (if (or (= arg 2)(= arg 4))
  215.            (not Manual-match-topic-exactly)
  216.          Manual-match-topic-exactly))
  217.     (force (>= arg 3))
  218.     (sep (make-string 65 ?-))
  219.     section fmtlist manlist apropos-mode)
  220.     (if (and (null section)
  221.          (string-match
  222.           "\\`[ \t]*\\([^( \t]+\\)[ \t]*(\\(.+\\))[ \t]*\\'" topic))
  223.     (setq section (substring topic (match-beginning 2)
  224.                  (match-end 2))
  225.           topic (substring topic (match-beginning 1)
  226.                    (match-end 1))))
  227.     (if (equal section "-k")
  228.     (setq apropos-mode t)
  229.       (or silent
  230.       (message "Looking for formatted entry for %s%s..."
  231.            topic (if section (concat "(" section ")") "")))
  232.       (setq fmtlist (Manual-select-man-pages
  233.              (Manual-select-directories
  234.               Manual-formatted-directory-list section) 
  235.              topic section exact))
  236.       (if (or force (not fmtlist))
  237.       (progn
  238.         (or silent
  239.         (message "%sooking for unformatted entry for %s%s..."
  240.              (if fmtlist "L" "No formatted entry, l")
  241.              topic (if section (concat "(" section ")") "")))
  242.         (setq manlist (Manual-select-man-pages
  243.                (Manual-select-directories
  244.                 Manual-unformatted-directory-list section)
  245.                topic section exact)))))
  246.     (if (or fmtlist manlist apropos-mode)
  247.     (let* ((name (car (or fmtlist manlist)))
  248.            (bufname (concat
  249.              (if Manual-topic-buffer
  250.                  (if apropos-mode
  251.                  (concat "*" topic " ")
  252.                    (concat "*"
  253.                        (and (string-match "/\\([^/]+\\)$" name)
  254.                         (substring name (match-beginning 1)
  255.                                (match-end 1)))
  256.                        " ")))
  257.              (if apropos-mode
  258.                  "*Manual Apropos*" "*Manual Entry*"))))
  259.       ;; Delete duplicate man pages (a file of the same name in multiple
  260.       ;; directories.)
  261.       (let ((rest (append fmtlist manlist)))
  262.         (while rest
  263.           (let ((rest2 (cdr rest)))
  264.         (while rest2
  265.           (if (equal (file-name-nondirectory (car rest))
  266.                  (file-name-nondirectory (car rest2)))
  267.               (setq fmtlist (delq (car rest2) fmtlist)
  268.                 manlist (delq (car rest2) manlist)))
  269.           (setq rest2 (cdr rest2))))
  270.           (setq rest (cdr rest))))
  271.  
  272.       (if apropos-mode
  273.           (setq manlist (list (format "%s.%s" topic section))))
  274.  
  275.       (with-output-to-temp-buffer bufname
  276.         (buffer-disable-undo standard-output)
  277.         (save-excursion
  278.           (set-buffer standard-output)
  279.           (setq buffer-read-only nil)
  280.           (erase-buffer)
  281.           (let (name start end topic section)
  282.         (while fmtlist        ; insert any formatted files
  283.           (setq name (car fmtlist))
  284.           (goto-char (point-max))
  285.           (setq start (point))
  286.           ;; In case the file can't be read or uncompressed or
  287.           ;; something like that.
  288.           (condition-case ()
  289.               (Manual-insert-man-file name)
  290.             (file-error nil))
  291.           (goto-char (point-max))
  292.           (setq end (point))
  293.           (save-excursion
  294.             (save-restriction
  295.               (message "Cleaning manual entry for %s..."
  296.                    (file-name-nondirectory name))
  297.               (narrow-to-region start end)
  298.               (Manual-nuke-nroff-bs)))
  299.           (if (or (cdr fmtlist) manlist)
  300.               (insert "\n\n" sep "\n"))
  301.           (setq fmtlist (cdr fmtlist)))
  302.         (while manlist        ; process any unformatted files
  303.           (setq name (car manlist))
  304.           (string-match "\\([^/]+\\)\\.\\([^./]+\\)$" name)
  305.           (setq topic (substring name (match-beginning 1)
  306.                      (match-end 1)))
  307.           (setq section (substring name (match-beginning 2)
  308.                        (match-end 2)))
  309.           (message "Invoking man %s %s ..." section topic)
  310.           (setq start (point))
  311.           (if (string-match "roff\\'" Manual-program) ; kludge kludge
  312.               (call-process Manual-program nil t nil
  313.                     "-Tman" "-man" name)
  314.             (call-process Manual-program nil t nil section topic))
  315.           (setq end (point))
  316.           (save-excursion
  317.             (save-restriction
  318.               (message "Cleaning manual entry for %s(%s)..."
  319.                    topic section)
  320.               (narrow-to-region start end)
  321.               (Manual-nuke-nroff-bs)))
  322.           (if (cdr manlist)
  323.               (insert "\n\n" sep "\n"))
  324.           (setq manlist (cdr manlist))))
  325.           (if (< (buffer-size) 80)
  326.           (progn
  327.             (goto-char (point-min))
  328.             (end-of-line)
  329.             (error (buffer-substring 1 (point)))))
  330.           (set-buffer-modified-p nil)
  331.           (Manual-mode)
  332.           ))
  333.       (message nil)
  334.       t)
  335.       ;; else
  336.       (message "No entries found for %s%s" topic
  337.            (if section (concat "(" section ")") ""))
  338.       nil)))
  339.  
  340. (defvar Manual-mode-map
  341.   (let ((m (make-sparse-keymap)))
  342.     (set-keymap-name m 'Manual-mode-map)
  343.     (define-key m 'button2 'Manual-follow-xref)
  344.     (define-key m 'button3 'Manual-popup-menu)
  345.     m))
  346.  
  347. (defun Manual-mode ()
  348.   (kill-all-local-variables)
  349.   (setq buffer-read-only t)
  350.   (use-local-map Manual-mode-map)
  351.   (setq major-mode 'Manual-mode
  352.     mode-name "Manual"))
  353.  
  354.  
  355. ;; Manual-select-subdirectories
  356. ;; Given a DIRLIST and a SUBDIR name, return all subdirectories of the former which
  357. ;; match the latter.
  358.  
  359. (defun Manual-select-subdirectories (dirlist subdir)
  360.   (apply 'append (mapcar '(lambda (dir)
  361.                (and (file-exists-p dir)
  362.                               (mapcar
  363.                    '(lambda (name) (expand-file-name name dir))
  364.                    (sort (file-name-all-completions subdir dir)
  365.                      'string<))))
  366.              dirlist)))
  367.  
  368. ;; Manual-select-directories
  369. ;;
  370. ;; Select from DIRLIST the appropriate directories by SECTION.
  371. ;; Return selected directories in a list.  If SECTION is nil, select
  372. ;; all SECTION directories.
  373.  
  374. (defun Manual-select-directories (dirlist section)
  375.   (delq nil
  376.     (mapcar
  377.      (function (lambda (fmtdir)
  378.              (if (or (not section)
  379.                  (string-match (concat (substring section 0 1)
  380.                            "/$") fmtdir))
  381.              fmtdir)))
  382.      dirlist)))
  383.  
  384. ;; Manual-select-man-pages
  385. ;;
  386. ;; Given a DIRLIST, discover all filenames which complete given the TOPIC and SECTION.
  387.  
  388. (defun Manual-select-man-pages-iterator (file)
  389.   ;; If Manual-match-topic-exactly is set, then we must make sure
  390.   ;; the completions are exact, except for trailing weird characters
  391.   ;; after the section.
  392.   (if (or (not exact)
  393.       (eq 0 (string-match (concat "^" topic "\\." (or section)) file)))
  394.       (concat dir file)))
  395.  
  396. ;; ## Note: BSD man looks for .../man1/foo.1 and .../man1/$MACHINE/foo.1
  397.  
  398. (defun Manual-select-man-pages (dirlist topic section exact)
  399.   (let ((manlist
  400.      (apply 'append        ; this removes the nulls
  401.        (mapcar (function
  402.             (lambda (dir)
  403.               (if (file-directory-p dir)
  404.               (delq nil
  405.                 (mapcar 'Manual-select-man-pages-iterator
  406.                     (file-name-all-completions
  407.                      (concat topic
  408.                          (if section
  409.                              (concat "." section)))
  410.                      dir)))
  411.             (message "warning: %s is not a directory" dir)
  412.             ;;(sit-for 1)
  413.             nil)))
  414.            dirlist))))
  415.     (if (and manlist Manual-query-multiple-pages)
  416.     (apply 'append
  417.            (mapcar '(lambda (page)
  418.               (if (and page 
  419.                    (y-or-n-p (format "Read %s? " page)))
  420.                   (list page)))
  421.                manlist))
  422.       manlist)))
  423.  
  424. (defun Manual-insert-man-file (name)
  425.   ;; Insert manual file (unpacked as necessary) into buffer
  426.   (if (equal (substring name -2) ".Z")
  427.       (call-process "zcat" nil t nil name)
  428.     (if (equal (substring name -2) ".z")
  429.     (call-process "pcat" nil t nil name)
  430.       (insert-file-contents name))))
  431.  
  432. (defmacro Manual-delete-char (n)
  433.   ;; in v19, delete-char is compiled as a function call, but delete-region
  434.   ;; is byte-coded, so it's much faster.  (We were spending 40% of our time
  435.   ;; in delete-char alone.)
  436.   (list 'delete-region '(point) (list '+ '(point) n)))
  437.  
  438. ;; Hint: BS stands form more things than "back space"
  439. (defun Manual-nuke-nroff-bs ()
  440.   (interactive "*")
  441.   ;;
  442.   ;; turn underlining into italics
  443.   (goto-char (point-min))
  444.   (while (re-search-forward "\\(_\b[^\n]\\)+" nil t)
  445.     (let ((s (match-beginning 0))
  446.       (e (match-end 0)))
  447.       (goto-char s)
  448.       (while (< (point) e)
  449.     (setq e (- e 2))
  450.     (Manual-delete-char 2)
  451.     (forward-char 1))
  452.       (set-extent-face (make-extent s (point)) 'italic)))
  453.   ;;
  454.   ;; turn overstriking into bold
  455.   (goto-char (point-min))
  456.   (while (search-forward "\b" nil t)
  457.     (if (save-excursion
  458.       (forward-char -2)
  459.       (looking-at "\\(\\([^\n]\\)\b\\2\\)+"))
  460.     (let* ((s (match-beginning 0))
  461.            (e (match-end 0)))
  462.       (goto-char s)
  463.       (while (< (point) e)
  464.         (setq e (- e 2))
  465.         (Manual-delete-char 2)
  466.         (forward-char 1))
  467.       (set-extent-face (make-extent s e) 'bold))))
  468.   ;;
  469.   ;; hack bullets: o^H+ --> +
  470.   (goto-char (point-min))
  471.   (while (search-forward "\b" nil t)
  472.     (Manual-delete-char -2))
  473.  
  474.   (Manual-nuke-nroff-bs-footers)
  475.   ;;
  476.   ;; turn subsection header lines into bold
  477.   (goto-char (point-min))
  478.   (while (re-search-forward "^[^ \t\n]" nil t)
  479.     (set-extent-face (make-extent (match-beginning 0)
  480.                   (progn (end-of-line) (point)))
  481.              'bold))
  482.  
  483.   ;; Zap ESC7,  ESC8, and ESC9
  484.   ;; This is for Sun man pages like "man 1 csh"
  485. ;  (goto-char (point-min))
  486. ;  (while (re-search-forward "\e[789]" nil t)
  487. ;    (replace-match ""))
  488.  
  489.   ;; Nuke blanks lines at start.
  490. ;  (goto-char (point-min))
  491. ;  (skip-chars-forward "\n")
  492. ;  (delete-region (point-min) (point))
  493.  
  494.   (Manual-mouseify-xrefs)
  495.   )
  496.  
  497. (fset 'nuke-nroff-bs 'Manual-nuke-nroff-bs) ; use old name
  498.  
  499.  
  500. (defun Manual-nuke-nroff-bs-footers ()
  501.   ;; Nuke headers and footers.
  502.   ;;
  503.   ;; nroff assumes pages are 66 lines high.  We assume that, and that the
  504.   ;; first and last line on each page is expendible.  There is no way to
  505.   ;; tell the difference between a page break in the middle of a paragraph
  506.   ;; and a page break between paragraphs (the amount of extra whitespace
  507.   ;; that nroff inserts is the same in both cases) so this might strip out
  508.   ;; a blank line were one should remain.  I think that's better than
  509.   ;; leaving in a blank line where there shouldn't be one.  (Need I say
  510.   ;; it: FMH.)
  511.   ;;
  512.   ;; Note that if nroff spits out error messages, pages will be more than
  513.   ;; 66 lines high, and we'll lose badly.  That's ok because standard
  514.   ;; nroff doesn't do any diagnostics, and the "gnroff" wrapper for groff
  515.   ;; turns off error messages for compatibility.
  516.   ;; 
  517.   (goto-char (point-min))
  518.   ;; first lose the status output
  519.   (let ((case-fold-search t))
  520.     (if (and (not (looking-at "[^\n]*warning"))
  521.          (looking-at "Reformatting.*\n"))
  522.     (delete-region (match-beginning 0) (match-end 0))))
  523.  
  524.   (let ((pages '())
  525.     p)
  526.     ;; collect the page boundary markers before we start deleting, to make
  527.     ;; it easier to strip things out without changing the page sizes.
  528.     (while (not (eobp))
  529.       (forward-line 66)
  530.       (setq pages (cons (point-marker) pages)))
  531.     (setq pages (nreverse pages))
  532.     (while pages
  533.       (goto-char (car pages))
  534.       (set-marker (car pages) nil)
  535.       ;;
  536.       ;; The lines are: 3 blank; footer; 6 blank; header; 3 blank.
  537.       ;; We're in between the previous footer and the following header,
  538.       ;;
  539.       ;; First lose 3 blank lines, the header, and then 3 more.
  540.       ;;
  541.       (setq p (point))
  542.       (skip-chars-forward "\n")
  543.       (delete-region p (point))
  544.       (and (looking-at "[^\n]+\n\n?\n?\n?")
  545.        (delete-region (match-beginning 0) (match-end 0)))
  546.       ;;
  547.       ;; Next lose the footer, and the 3 blank lines after, and before it.
  548.       ;; But don't lose the last footer of the manual entry; that contains
  549.       ;; the "last change" date, so it's not completely uninteresting.
  550.       ;; (Actually lose all blank lines before it; sh(1) needs this.)
  551.       ;;
  552.       (skip-chars-backward "\n")
  553.       (beginning-of-line)
  554.       (if (null (cdr pages))
  555.       nil
  556.     (and (looking-at "[^\n]+\n\n?\n?\n?")
  557.          (delete-region (match-beginning 0) (match-end 0))))
  558.       (setq p (point))
  559.       (skip-chars-backward "\n")
  560.       (if (> (- p (point)) 4)
  561.       (delete-region (+ 2 (point)) p)
  562.     (delete-region (1+ (point)) p))
  563. ;      (and (looking-at "\n\n?\n?")
  564. ;       (delete-region (match-beginning 0) (match-end 0)))
  565.  
  566.       (setq pages (cdr pages)))
  567.     ;;
  568.     ;; Now nuke the extra blank lines at the beginning and end.
  569.     (goto-char (point-min))
  570.     (if (looking-at "\n+")
  571.     (delete-region (match-beginning 0) (match-end 0)))
  572.     (forward-line 1)
  573.     (if (looking-at "\n\n+")
  574.     (delete-region (1+ (match-beginning 0)) (match-end 0)))
  575.     (goto-char (point-max))
  576.     (skip-chars-backward "\n")
  577.     (delete-region (point) (point-max))
  578.     (beginning-of-line)
  579.     (forward-char -1)
  580.     (setq p (point))
  581.     (skip-chars-backward "\n")
  582.     (if (= ?\n (following-char)) (forward-char 1))
  583.     (if (> (point) (1+ p))
  584.     (delete-region (point) p))
  585.     ))
  586.  
  587. ;(defun Manual-nuke-nroff-bs-footers ()
  588. ;  ;; Nuke headers: "MORE(1) UNIX Programmer's Manual MORE(1)"
  589. ;  (goto-char (point-min))
  590. ;  (while (re-search-forward "^ *\\([A-Za-z][-_A-Za-z0-9]*([0-9A-Za-z]+)\\).*\\1$" nil t)
  591. ;    (replace-match ""))
  592. ;  
  593. ;  ;;
  594. ;  ;; it would appear that we have a choice between sometimes introducing
  595. ;  ;; an extra blank line when a paragraph was broken by a footer, and
  596. ;  ;; sometimes not putting in a blank line between two paragraphs when
  597. ;  ;; a footer appeared right between them.  FMH; I choose the latter.
  598. ;  ;;
  599. ;
  600. ;  ;; Nuke footers: "Printed 12/3/85    27 April 1981    1"
  601. ;  ;;    Sun appear to be on drugz:
  602. ;  ;;     "Sun Release 3.0B  Last change: 1 February 1985     1"
  603. ;  ;;    HP are even worse!
  604. ;  ;;     "     Hewlett-Packard   -1- (printed 12/31/99)"  FMHWA12ID!!
  605. ;  ;;    System V (well WICATs anyway):
  606. ;  ;;     "Page 1              (printed 7/24/85)"
  607. ;  ;;    Who is administering PCP to these corporate bozos?
  608. ;  (goto-char (point-min))
  609. ;  (while (re-search-forward
  610. ;       (cond
  611. ;        ((eq system-type 'hpux)
  612. ;         "\n\n?[ \t]*Hewlett-Packard\\(\\| Company\\)[ \t]*- [0-9]* -.*\n")
  613. ;        ((eq system-type 'dgux-unix)
  614. ;         "\n\n?[ \t]*Licensed material--.*Page [0-9]*\n")
  615. ;        ((eq system-type 'usg-unix-v)
  616. ;         "\n\n? *Page [0-9]*.*(printed [0-9/]*)\n")
  617. ;        (t
  618. ;         "\n\n?\\(Printed\\|Sun Release\\) [0-9].*[0-9]\n"))
  619. ;       nil t)
  620. ;    (replace-match ""))
  621. ;
  622. ;  ;;    Also, hack X footers:
  623. ;  ;;     "X Version 11         Last change: Release 5         1"
  624. ;  (goto-char (point-min))
  625. ;  (while (re-search-forward "\n\n?X Version [^\n]+\n" nil t)
  626. ;    (replace-match ""))
  627. ;
  628. ;  ;; Crunch blank lines
  629. ;  (goto-char (point-min))
  630. ;  (while (re-search-forward "\n\n\n\n*" nil t)
  631. ;    (replace-match "\n\n"))
  632. ;  )
  633.  
  634. (defun Manual-mouseify-xrefs ()
  635.   (goto-char (point-min))
  636.   (forward-line 1)
  637.   (let ((case-fold-search nil)
  638.     s e name extent already-fontified)
  639.     ;; possibly it would be faster to rewrite this expression to search for
  640.     ;; a less common sequence first (like "([0-9]") and then back up to see
  641.     ;; if it's really a match.  This function is 15% of the total time, 13%
  642.     ;; of which is this call to re-search-forward.
  643.     (while (re-search-forward "[a-zA-Z_][-a-zA-Z0-9_.]*([0-9][a-zA-Z]*)" nil t)
  644.       (setq s (match-beginning 0)
  645.         e (match-end 0)
  646.         name (buffer-substring s e))
  647.       (goto-char s)
  648.       (skip-chars-backward " \t")
  649.       (if (and (bolp)
  650.            (progn (backward-char 1) (= (preceding-char) ?-)))
  651.       (progn
  652.         (setq s (point))
  653.         (skip-chars-backward "-a-zA-Z0-9_.")
  654.         (setq name (concat (buffer-substring (point) (1- s)) name))
  655.         (setq s (point))))
  656.       ;; if there are upper case letters in the section, downcase them.
  657.       (if (string-match "(.*[A-Z]+.*)$" name)
  658.       (setq name (concat (substring name 0 (match-beginning 0))
  659.                  (downcase (substring name (match-beginning 0))))))
  660.       (setq already-fontified (extent-at s))
  661.       (setq extent (make-extent s e))
  662.       (set-extent-data extent (list 'Manual-follow-xref name))
  663.       (set-extent-attribute extent 'highlight)
  664.       (if (not already-fontified)
  665.       (set-extent-face extent 'italic))
  666.       (goto-char e))))
  667.  
  668. (defun Manual-follow-xref (&optional name-or-event)
  669.   "Invoke `manual-entry' on the cross-reference under the mouse.
  670. When invoked noninteractively, the arg may be an xref string to parse instead."
  671.   (interactive "e")
  672.   (if (eventp name-or-event)
  673.       (let* ((p (event-point name-or-event))
  674.          (extent (and p (extent-at p
  675.                  (window-buffer (event-window name-or-event))
  676.                  'highlight)))
  677.          (data (and extent (extent-data extent))))
  678.     (if (eq (car-safe data) 'Manual-follow-xref)
  679.         (eval data)
  680.       (error "no manual cross-reference there.")))
  681.     (let ((Manual-match-topic-exactly t)
  682.       (Manual-query-multiple-pages nil))
  683.       (or (manual-entry name-or-event)
  684.       ;; If that didn't work, maybe it's in a different section than the
  685.       ;; man page writer expected.  For example, man pages tend assume
  686.       ;; that all user programs are in section 1, but X tends to generate
  687.       ;; makefiles that put things in section "n" instead...
  688.       (and (string-match "[ \t]*([^)]+)\\'" name-or-event)
  689.            (progn
  690.          (message "No entries found for %s; checking other sections..."
  691.               name-or-event)
  692.          (manual-entry
  693.           (substring name-or-event 0 (match-beginning 0))
  694.           nil t)))))))
  695.  
  696. (defun Manual-popup-menu (&optional event)
  697.   "Pops up a menu of cross-references in this manual page.
  698. If there is a cross-reference under the mouse button which invoked this
  699. command, it will be the first item on the menu.  Otherwise, they are
  700. on the menu in the order in which they appear in the buffer."
  701.   (interactive "e")
  702.   (let ((buffer (current-buffer))
  703.     (sep "---")
  704.     (prefix "Show Manual Page for ")
  705.     xref items)
  706.     (cond (event
  707.        (setq buffer (window-buffer (event-window event)))
  708.        (let* ((p (event-point event))
  709.           (extent (and p (extent-at p buffer 'highlight)))
  710.           (data (and extent (extent-data extent))))
  711.          (if (eq (car-safe data) 'Manual-follow-xref)
  712.          (setq xref (nth 1 data))))))
  713.     (if xref (setq items (list sep xref)))
  714.     (map-extents (function
  715.           (lambda (extent ignore)
  716.             (let ((data (extent-data extent)))
  717.               (if (and (eq (car-safe data) 'Manual-follow-xref)
  718.                    (not (member (nth 1 data) items)))
  719.               (setq items (cons (nth 1 data) items))))
  720.             nil))
  721.          buffer)
  722.     (if (eq sep (car items)) (setq items (cdr items)))
  723.     (popup-menu
  724.      (cons "Manual Entry"
  725.        (mapcar '(lambda (item)
  726.               (if (eq item sep)
  727.               item
  728.             (vector (concat prefix item)
  729.                 (list 'Manual-follow-xref item) t)))
  730.            (nreverse items))))))
  731.  
  732.