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