*** empty log message ***
[emacs.git] / lisp / man.el
blobc2e0d45ac3f2be1c985d4d3d771e0097b816e1ca
1 ;;; man.el --- read in and display parts of Unix manual.
3 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 1, or (at your option)
10 ;; any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 ;;;###autoload
22 (defun manual-entry (topic &optional section)
23 "Display the Unix manual entry for TOPIC.
24 TOPIC is either the title of the entry, or has the form TITLE(SECTION)
25 where SECTION is the desired section of the manual, as in \"tty(4)\"."
26 (interactive "sManual entry (topic): ")
27 (if (= (length topic) 0)
28 (error "Must specify topic"))
29 (if (and (null section)
30 (string-match "\\`[ \t]*\\([^( \t]+\\)[ \t]*(\\(.+\\))[ \t]*\\'" topic))
31 (setq section (substring topic (match-beginning 2)
32 (match-end 2))
33 topic (substring topic (match-beginning 1)
34 (match-end 1))))
35 (with-output-to-temp-buffer (concat "*" topic " Manual Entry*")
36 (buffer-disable-undo standard-output)
37 (save-excursion
38 (set-buffer standard-output)
39 (message "Looking for formatted entry for %s%s..."
40 topic (if section (concat "(" section ")") ""))
41 (let ((dirlist manual-formatted-dirlist)
42 (case-fold-search nil)
43 name)
44 (if (and section (or (file-exists-p
45 (setq name (concat manual-formatted-dir-prefix
46 (substring section 0 1)
47 "/"
48 topic "." section)))
49 (file-exists-p
50 (setq name (concat manual-formatted-dir-prefix
51 section
52 "/"
53 topic "." section)))))
54 (insert-man-file name)
55 (while dirlist
56 (let* ((dir (car dirlist))
57 (name1 (concat dir "/" topic "."
58 (or section
59 (substring
60 dir
61 (1+ (or (string-match "\\.[^./]*$" dir)
62 -2))))))
63 completions)
64 (if (file-exists-p name1)
65 (insert-man-file name1)
66 (condition-case ()
67 (progn
68 (setq completions (file-name-all-completions
69 (concat topic "." (or section ""))
70 dir))
71 (while completions
72 (insert-man-file (concat dir "/" (car completions)))
73 (setq completions (cdr completions))))
74 (file-error nil)))
75 (goto-char (point-max)))
76 (setq dirlist (cdr dirlist)))))
78 (if (= (buffer-size) 0)
79 (progn
80 (message "No formatted entry, invoking man %s%s..."
81 (if section (concat section " ") "") topic)
82 (if section
83 (call-process manual-program nil t nil section topic)
84 (call-process manual-program nil t nil topic))
85 (if (< (buffer-size) 80)
86 (progn
87 (goto-char (point-min))
88 (end-of-line)
89 (error (buffer-substring 1 (point)))))))
91 (message "Cleaning manual entry for %s..." topic)
92 (nuke-nroff-bs)
93 (set-buffer-modified-p nil)
94 (setq buffer-read-only t)
95 (view-mode nil 'bury-buffer)
96 (message ""))))
98 ;; Hint: BS stands for more things than "back space"
99 (defun nuke-nroff-bs ()
100 (interactive "*")
101 ;; Nuke headers: "MORE(1) UNIX Programmer's Manual MORE(1)"
102 ;; We expext to find a footer just before the header except at the beginning.
103 (goto-char (point-min))
104 (while (re-search-forward "^ *\\([A-Za-z][-_.A-Za-z0-9]*([0-9A-Z]+)\\).*\\1$" nil t)
105 (let (start end)
106 ;; Put START and END around footer and header and garbage blank lines.
107 ;; Fixed line counts are risky, but allow us to preserve
108 ;; significant blank lines.
109 (setq start (save-excursion (forward-line -10) (point)))
110 (setq end (save-excursion (forward-line 4) (point)))
111 (delete-region start end)))
112 ;; Catch the final footer.
113 (goto-char (point-max))
114 (delete-region (point) (save-excursion (forward-line -7) (point)))
116 ;; Nuke underlining and overstriking (only by the same letter)
117 (goto-char (point-min))
118 (while (search-forward "\b" nil t)
119 (let* ((preceding (char-after (- (point) 2)))
120 (following (following-char)))
121 (cond ((= preceding following)
122 ;; x\bx
123 (delete-char -2))
124 ((and (= preceding ?o) (= following ?\+))
125 ;; o\b+
126 (delete-char -2))
127 ((= preceding ?\_)
128 ;; _\b
129 (delete-char -2))
130 ((= following ?\_)
131 ;; \b_
132 (delete-region (1- (point)) (1+ (point)))))))
134 ;; Zap ESC7, ESC8, and ESC9.
135 ;; This is for Sun man pages like "man 1 csh"
136 (goto-char (point-min))
137 (while (re-search-forward "\e[789]" nil t)
138 (replace-match ""))
140 ;; Convert o^H+ into o.
141 (goto-char (point-min))
142 (while (re-search-forward "o\010\\+" nil t)
143 (replace-match "o"))
145 ;; Nuke the dumb reformatting message
146 (goto-char (point-min))
147 (while (re-search-forward "Reformatting page. Wait... done\n\n" nil t)
148 (replace-match ""))
150 ;; Crunch blank lines
151 (goto-char (point-min))
152 (while (re-search-forward "\n\n\n\n*" nil t)
153 (replace-match "\n\n"))
155 ;; Nuke blanks lines at start.
156 (goto-char (point-min))
157 (skip-chars-forward "\n")
158 (delete-region (point-min) (point)))
161 (defun insert-man-file (name)
162 ;; Insert manual file (unpacked as necessary) into buffer
163 (if (or (equal (substring name -2) ".Z")
164 (string-match "/cat[0-9][a-z]?\\.Z/" name))
165 (call-process "zcat" name t nil)
166 (if (equal (substring name -2) ".z")
167 (call-process "pcat" nil t nil name)
168 (insert-file-contents name))))
170 ;;; man.el ends here