0
|
1 ;;; man.el --- browse UNIX manual pages
|
8
|
2 ;; Keywords: help
|
0
|
3
|
24
|
4 ;; Copyright (C) 1985, 1993, 1994, 1996 Free Software Foundation, Inc.
|
8
|
5 ;;
|
0
|
6 ;; This file is part of XEmacs.
|
|
7
|
|
8 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
9 ;; under the terms of the GNU General Public License as published by
|
|
10 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
11 ;; any later version.
|
|
12
|
|
13 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
16 ;; General Public License for more details.
|
|
17
|
|
18 ;; You should have received a copy of the GNU General Public License
|
24
|
19 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
20 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
0
|
21
|
8
|
22 ;; This file defines "manual-entry", and the remaining definitions all
|
|
23 ;; begin with "Manual-". This makes the autocompletion on "M-x man" work.
|
24
|
24 ;;
|
|
25 ;; Eviscerated 26-Jun-96 by Jamie Zawinski <jwz@netscape.com>.
|
|
26 ;; All that stuff about looking at $MANPATH and building up lists of
|
|
27 ;; directories was bullshit. Now we just invoke "man" and format the
|
|
28 ;; output, end of story.
|
8
|
29 ;;
|
24
|
30 ;; [ older changelog entries removed, since they're all about code that
|
|
31 ;; I've deleted. ]
|
0
|
32
|
8
|
33 (defvar Manual-program "man" "\
|
|
34 *Name of the program to invoke in order to format the source man pages.")
|
0
|
35
|
8
|
36 (defvar Manual-buffer-view-mode t "\
|
|
37 *Whether manual buffers should be placed in view-mode.
|
|
38 nil means leave the buffer in fundamental-mode in another window.
|
|
39 t means use `view-buffer' to display the man page in the current window.
|
|
40 Any other value means use `view-buffer-other-window'.")
|
0
|
41
|
8
|
42 (defvar Manual-mode-hook nil
|
|
43 "Function or functions run on entry to Manual-mode.")
|
0
|
44
|
8
|
45 (defvar Manual-page-history nil "\
|
|
46 A list of names of previously visited man page buffers.")
|
0
|
47
|
|
48
|
8
|
49 ;; New variables.
|
0
|
50
|
10
|
51 (defvar Manual-use-rosetta-man (not (null (locate-file "rman" exec-path))) "\
|
|
52 If non-nil, use RosettaMan (rman) to filter man pages.
|
|
53 This makes man-page cleanup virtually instantaneous, instead of
|
|
54 potentially taking a long time.
|
|
55
|
|
56 Here is information on RosettaMan, from Neal.Becker@comsat.com (Neal Becker):
|
|
57
|
|
58 RosettaMan is a filter for UNIX manual pages. It takes as input man
|
|
59 pages formatted for a variety of UNIX flavors (not [tn]roff source)
|
|
60 and produces as output a variety of file formats. Currently
|
|
61 RosettaMan accepts man pages as formatted by the following flavors of
|
|
62 UNIX: Hewlett-Packard HP-UX, AT&T System V, SunOS, Sun Solaris, OSF/1,
|
|
63 DEC Ultrix, SGI IRIX, Linux, SCO; and produces output for the following
|
|
64 formats: printable ASCII only (stripping page headers and footers),
|
|
65 section and subsection headers only, TkMan, [tn]roff, Ensemble, RTF,
|
|
66 SGML (soon--I finally found a DTD), HTML, MIME, LaTeX, LaTeX 2e, Perl 5's pod.
|
|
67
|
|
68 RosettaMan improves on other man page filters in several ways: (1) its
|
|
69 analysis recognizes the structural pieces of man pages, enabling high
|
|
70 quality output, (2) its modular structure permits easy augmentation of
|
|
71 output formats, (3) it accepts man pages formatted with the varient
|
|
72 macros of many different flavors of UNIX, and (4) it doesn't require
|
|
73 modification or cooperation with any other program.
|
|
74
|
|
75 RosettaMan is a rewrite of TkMan's man page filter, called bs2tk. (If
|
|
76 you haven't heard about TkMan, a hypertext man page browser, you
|
|
77 should grab it via anonymous ftp from ftp.cs.berkeley.edu:
|
|
78 /ucb/people/phelps/tkman.tar.Z.) Whereas bs2tk generated output only for
|
|
79 TkMan, RosettaMan generalizes the process so that the analysis can be
|
|
80 leveraged to new output formats. A single analysis engine recognizes
|
|
81 section heads, subsection heads, body text, lists, references to other
|
|
82 man pages, boldface, italics, bold italics, special characters (like
|
|
83 bullets), tables (to a degree) and strips out page headers and
|
|
84 footers. The engine sends signals to the selected output functions so
|
|
85 that an enhancement in the engine improves the quality of output of
|
|
86 all of them. Output format functions are easy to add, and thus far
|
|
87 average about about 75 lines of C code each.
|
|
88
|
|
89
|
|
90
|
|
91 *** NOTES ON CURRENT VERSION ***
|
|
92
|
|
93 Help! I'm looking for people to help with the following projects.
|
|
94 \(1) Better RTF output format. The current one works, but could be
|
|
95 made better. (2) Roff macros that produce text that is easily
|
|
96 parsable. RosettaMan handles a great variety, but some things, like
|
|
97 H-P's tables, are intractable. If you write an output format or
|
|
98 otherwise improve RosettaMan, please send in your code so that I may
|
|
99 share the wealth in future releases.
|
|
100
|
|
101 This version can try to identify tables (turn this on with the -T
|
|
102 switch) by looking for lines with a large amount of interword spacing,
|
|
103 reasoning that this is space between columns of a table. This
|
|
104 heuristic doesn't always work and sometimes misidentifies ordinary
|
|
105 text as tables. In general I think it is impossible to perfectly
|
|
106 identify tables from nroff formatted text. However, I do think the
|
|
107 heuristics can be tuned, so if you have a collection of manual pages
|
|
108 with unrecognized tables, send me the lot, in formatted form (i.e.,
|
|
109 after formatting with nroff -man), and uuencode them to preserve the
|
|
110 control characters. Better, if you can think of heuristics that
|
|
111 distinguish tables from ordinary text, I'd like to hear them.
|
|
112
|
|
113
|
|
114 Notes for HTML consumers: This filter does real (heuristic)
|
|
115 parsing--no <PRE>! Man page references are turned into hypertext links.")
|
|
116
|
8
|
117 (make-face 'man-italic)
|
|
118 (or (face-differs-from-default-p 'man-italic)
|
|
119 (copy-face 'italic 'man-italic))
|
|
120 ;; XEmacs (from Darrell Kindred): underlining is annoying due to
|
|
121 ;; large blank spaces in this face.
|
|
122 ;; (or (face-differs-from-default-p 'man-italic)
|
|
123 ;; (set-face-underline-p 'man-italic t))
|
0
|
124
|
8
|
125 (make-face 'man-bold)
|
|
126 (or (face-differs-from-default-p 'man-bold)
|
|
127 (copy-face 'bold 'man-bold))
|
|
128 (or (face-differs-from-default-p 'man-bold)
|
|
129 (copy-face 'man-italic 'man-bold))
|
0
|
130
|
8
|
131 (make-face 'man-heading)
|
|
132 (or (face-differs-from-default-p 'man-heading)
|
|
133 (copy-face 'man-bold 'man-heading))
|
0
|
134
|
8
|
135 (make-face 'man-xref)
|
|
136 (or (face-differs-from-default-p 'man-xref)
|
|
137 (set-face-underline-p 'man-xref t))
|
|
138
|
24
|
139 (defvar Manual-mode-map
|
|
140 (let ((m (make-sparse-keymap)))
|
|
141 (set-keymap-name m 'Manual-mode-map)
|
|
142 (define-key m "l" 'Manual-last-page)
|
|
143 (define-key m 'button2 'Manual-follow-xref)
|
|
144 (define-key m 'button3 'Manual-popup-menu)
|
|
145 m))
|
0
|
146
|
|
147 ;;;###autoload
|
8
|
148 (defun manual-entry (topic &optional arg silent)
|
24
|
149 "Display the Unix manual entry (or entries) for TOPIC."
|
8
|
150 (interactive
|
|
151 (list (let* ((fmh "-A-Za-z0-9_.")
|
|
152 (default (save-excursion
|
|
153 (buffer-substring
|
|
154 (progn
|
|
155 (re-search-backward "\\sw" nil t)
|
|
156 (skip-chars-backward fmh) (point))
|
|
157 (progn (skip-chars-forward fmh) (point)))))
|
|
158 (thing (read-string
|
|
159 (if (equal default "") "Manual entry: "
|
|
160 (concat "Manual entry: (default " default ") ")))))
|
|
161 (if (equal thing "") default thing))
|
|
162 (prefix-numeric-value current-prefix-arg)))
|
|
163 ;;(interactive "sManual entry (topic): \np")
|
|
164 (or arg (setq arg 1))
|
24
|
165 (let (section apropos-mode)
|
8
|
166 (let ((case-fold-search nil))
|
|
167 (if (and (null section)
|
24
|
168 (string-match "\\`[ \t]*\\([^( \t]+\\)[ \t]*(\\(.+\\))[ \t]*\\'"
|
|
169 topic))
|
8
|
170 (setq section (substring topic (match-beginning 2)
|
|
171 (match-end 2))
|
|
172 topic (substring topic (match-beginning 1)
|
|
173 (match-end 1)))
|
|
174 (if (string-match "\\`[ \t]*-k[ \t]+\\([^ \t]+\\)\\'" topic)
|
|
175 (setq section "-k"
|
|
176 topic (substring topic (match-beginning 1))))))
|
24
|
177
|
|
178 ;; jwz: turn section "3x11" and "3n" into "3".
|
|
179 (if (and section (string-match "\\`\\([0-9]+\\)[^0-9]" section))
|
|
180 (setq section (substring section 0 (match-end 1))))
|
8
|
181 (if (equal section "-k")
|
24
|
182 (setq apropos-mode t))
|
8
|
183
|
24
|
184 (let ((bufname (cond (apropos-mode
|
|
185 (concat "*man apropos " topic "*"))
|
|
186 (t
|
|
187 (concat "*man " topic
|
|
188 (if section (concat "." section) "")
|
|
189 "*"))))
|
|
190 (temp-buffer-show-function
|
|
191 (cond ((eq 't Manual-buffer-view-mode)
|
|
192 'view-buffer)
|
|
193 ((eq 'nil Manual-buffer-view-mode)
|
|
194 temp-buffer-show-function)
|
|
195 (t
|
|
196 'view-buffer-other-window))))
|
8
|
197
|
24
|
198 (cond ((get-buffer bufname)
|
|
199 ;; reselect an old man page buffer if it exists already.
|
|
200 (save-excursion
|
|
201 (set-buffer (get-buffer bufname))
|
|
202 (Manual-mode))
|
|
203 (if temp-buffer-show-function
|
|
204 (funcall temp-buffer-show-function (get-buffer bufname))
|
|
205 (display-buffer bufname)))
|
|
206 (t
|
|
207 (with-output-to-temp-buffer bufname
|
|
208 (buffer-disable-undo standard-output)
|
|
209 (save-excursion
|
|
210 (set-buffer standard-output)
|
|
211 (setq buffer-read-only nil)
|
|
212 (erase-buffer)
|
8
|
213
|
24
|
214 (let ((args (list topic))
|
|
215 args-string)
|
|
216 (if section
|
|
217 (setq args
|
|
218 (if (eq system-type 'usg-unix-v)
|
|
219 (cons "-s" (cons section args))
|
|
220 (cons section args))))
|
|
221 (setq args-string
|
|
222 (mapconcat 'identity (cons Manual-program args) " "))
|
|
223 (if (string-match "\\`\\([^ \t/]*/\\)+" args-string)
|
|
224 (setq args-string
|
|
225 (substring args-string (match-end 0))))
|
8
|
226
|
24
|
227 (message "%s (running...)" args-string)
|
|
228 (apply 'call-process Manual-program nil t nil args)
|
0
|
229
|
24
|
230 (if (< (buffer-size) 200)
|
|
231 (progn
|
|
232 (goto-char (point-min))
|
|
233 (error (buffer-substring (point)
|
|
234 (progn (end-of-line)
|
|
235 (point))))))
|
8
|
236
|
24
|
237 (message "%s (cleaning...)" args-string)
|
|
238 (Manual-nuke-nroff-bs apropos-mode)
|
|
239 (message "%s (done.)" args-string)
|
|
240 )
|
8
|
241
|
24
|
242 (set-buffer-modified-p nil)
|
|
243 (Manual-mode)
|
|
244 ))))
|
|
245 (setq Manual-page-history
|
|
246 (cons (buffer-name)
|
|
247 (delete (buffer-name) Manual-page-history)))))
|
|
248 (message nil)
|
|
249 t)
|
8
|
250
|
|
251 (defun Manual-mode ()
|
|
252 (kill-all-local-variables)
|
|
253 (setq buffer-read-only t)
|
|
254 (use-local-map Manual-mode-map)
|
|
255 (setq major-mode 'Manual-mode
|
|
256 mode-name "Manual")
|
|
257 ;; man pages with long lines are buggy!
|
|
258 ;; This looks slightly better if they only
|
|
259 ;; overran by a couple of chars.
|
|
260 (setq truncate-lines t)
|
|
261 ;; turn off horizontal scrollbars in this buffer
|
26
|
262 (when (featurep 'scrollbar)
|
|
263 (set-specifier scrollbar-height (cons (current-buffer) 0)))
|
8
|
264 (run-hooks 'Manual-mode-hook))
|
|
265
|
|
266 (defun Manual-last-page ()
|
|
267 (interactive)
|
|
268 (while (or (not (get-buffer (car (or Manual-page-history
|
|
269 (error "No more history.")))))
|
|
270 (eq (get-buffer (car Manual-page-history)) (current-buffer)))
|
|
271 (setq Manual-page-history (cdr Manual-page-history)))
|
|
272 (switch-to-buffer (car Manual-page-history)))
|
|
273
|
|
274
|
|
275 (defmacro Manual-delete-char (n)
|
|
276 ;; in v19, delete-char is compiled as a function call, but delete-region
|
|
277 ;; is byte-coded, so it's much faster. (We were spending 40% of our time
|
|
278 ;; in delete-char alone.)
|
|
279 (list 'delete-region '(point) (list '+ '(point) n)))
|
|
280
|
|
281 ;; Hint: BS stands form more things than "back space"
|
|
282 (defun Manual-nuke-nroff-bs (&optional apropos-mode)
|
|
283 (interactive "*")
|
10
|
284 (if Manual-use-rosetta-man
|
|
285 (call-process-region (point-min) (point-max) "rman" t t nil)
|
|
286 ;;
|
|
287 ;; turn underlining into italics
|
|
288 ;;
|
|
289 (goto-char (point-min))
|
|
290 (while (search-forward "_\b" nil t)
|
24
|
291 ;; searching for underscore-backspace and then comparing the
|
|
292 ;; following chars until the sequence ends turns out to be much
|
|
293 ;; faster than searching for a regexp which matches the whole
|
|
294 ;; sequence.
|
10
|
295 (let ((s (match-beginning 0)))
|
|
296 (goto-char s)
|
|
297 (while (and (= (following-char) ?_)
|
|
298 (= (char-after (1+ (point))) ?\b))
|
|
299 (Manual-delete-char 2)
|
|
300 (forward-char 1))
|
|
301 (set-extent-face (make-extent s (point)) 'man-italic)))
|
|
302 ;;
|
|
303 ;; turn overstriking into bold
|
|
304 ;;
|
|
305 (goto-char (point-min))
|
|
306 (while (re-search-forward "\\([^\n]\\)\\(\b\\1\\)" nil t)
|
|
307 ;; Surprisingly, searching for the above regexp is faster than searching
|
|
308 ;; for a backspace and then comparing the preceding and following chars,
|
|
309 ;; I presume because there are many false matches, meaning more funcalls
|
|
310 ;; to re-search-forward.
|
|
311 (let ((s (match-beginning 0)))
|
|
312 (goto-char s)
|
|
313 ;; Some systems (SGI) overstrike multiple times, eg, "M\bM\bM\bM".
|
|
314 (while (looking-at "\\([^\n]\\)\\(\b\\1\\)+")
|
|
315 (delete-region (+ (point) 1) (match-end 0))
|
|
316 (forward-char 1))
|
|
317 (set-extent-face (make-extent s (point)) 'man-bold)))
|
|
318 ;;
|
|
319 ;; hack bullets: o^H+ --> +
|
|
320 (goto-char (point-min))
|
|
321 (while (search-forward "\b" nil t)
|
|
322 (Manual-delete-char -2))
|
0
|
323
|
10
|
324 (if (> (buffer-size) 100) ; minor kludge
|
|
325 (Manual-nuke-nroff-bs-footers))
|
|
326 ) ;; not Manual-use-rosetta-man
|
8
|
327 ;;
|
|
328 ;; turn subsection header lines into bold
|
|
329 ;;
|
|
330 (goto-char (point-min))
|
|
331 (if apropos-mode
|
|
332 (while (re-search-forward "[a-zA-Z0-9] ([0-9]" nil t)
|
|
333 (forward-char -2)
|
|
334 (delete-backward-char 1))
|
0
|
335
|
8
|
336 ;; (while (re-search-forward "^[^ \t\n]" nil t)
|
|
337 ;; (set-extent-face (make-extent (match-beginning 0)
|
|
338 ;; (progn (end-of-line) (point)))
|
|
339 ;; 'man-heading))
|
4
|
340
|
8
|
341 ;; boldface the first line
|
|
342 (if (looking-at "[^ \t\n].*$")
|
|
343 (set-extent-face (make-extent (match-beginning 0) (match-end 0))
|
|
344 'man-bold))
|
0
|
345
|
8
|
346 ;; boldface subsequent title lines
|
|
347 ;; Regexp to match section headers changed to match a non-indented
|
|
348 ;; line preceded by a blank line and followed by an indented line.
|
|
349 ;; This seems to work ok for manual pages but gives better results
|
|
350 ;; with other nroff'd files
|
|
351 (while (re-search-forward "\n\n\\([^ \t\n].*\\)\n[ \t]+[^ \t\n]" nil t)
|
|
352 (goto-char (match-end 1))
|
|
353 (set-extent-face (make-extent (match-beginning 1) (match-end 1))
|
|
354 'man-heading)
|
|
355 (forward-line 1))
|
|
356 )
|
0
|
357
|
10
|
358 (if Manual-use-rosetta-man
|
|
359 nil
|
|
360 ;; Zap ESC7, ESC8, and ESC9
|
|
361 ;; This is for Sun man pages like "man 1 csh"
|
|
362 (goto-char (point-min))
|
|
363 (while (re-search-forward "\e[789]" nil t)
|
|
364 (replace-match "")))
|
24
|
365
|
8
|
366 ;; Nuke blanks lines at start.
|
|
367 ;; (goto-char (point-min))
|
|
368 ;; (skip-chars-forward "\n")
|
|
369 ;; (delete-region (point-min) (point))
|
|
370
|
|
371 (Manual-mouseify-xrefs)
|
|
372 )
|
|
373
|
|
374 (fset 'nuke-nroff-bs 'Manual-nuke-nroff-bs) ; use old name
|
|
375
|
0
|
376
|
8
|
377 (defun Manual-nuke-nroff-bs-footers ()
|
|
378 ;; Nuke headers and footers.
|
|
379 ;;
|
|
380 ;; nroff assumes pages are 66 lines high. We assume that, and that the
|
|
381 ;; first and last line on each page is expendible. There is no way to
|
|
382 ;; tell the difference between a page break in the middle of a paragraph
|
|
383 ;; and a page break between paragraphs (the amount of extra whitespace
|
|
384 ;; that nroff inserts is the same in both cases) so this might strip out
|
|
385 ;; a blank line were one should remain. I think that's better than
|
|
386 ;; leaving in a blank line where there shouldn't be one. (Need I say
|
|
387 ;; it: FMH.)
|
|
388 ;;
|
|
389 ;; Note that if nroff spits out error messages, pages will be more than
|
|
390 ;; 66 lines high, and we'll lose badly. That's ok because standard
|
|
391 ;; nroff doesn't do any diagnostics, and the "gnroff" wrapper for groff
|
|
392 ;; turns off error messages for compatibility. (At least, it's supposed
|
|
393 ;; to.)
|
|
394 ;;
|
|
395 (goto-char (point-min))
|
|
396 ;; first lose the status output
|
|
397 (let ((case-fold-search t))
|
|
398 (if (and (not (looking-at "[^\n]*warning"))
|
|
399 (looking-at "Reformatting.*\n"))
|
|
400 (delete-region (match-beginning 0) (match-end 0))))
|
4
|
401
|
8
|
402 ;; kludge around a groff bug where it won't keep quiet about some
|
|
403 ;; warnings even with -Wall or -Ww.
|
|
404 (cond ((looking-at "grotty:")
|
|
405 (while (looking-at "grotty:")
|
|
406 (delete-region (point) (progn (forward-line 1) (point))))
|
|
407 (if (looking-at " *done\n")
|
|
408 (delete-region (point) (match-end 0)))))
|
0
|
409
|
8
|
410 (let ((pages '())
|
|
411 p)
|
|
412 ;; collect the page boundary markers before we start deleting, to make
|
|
413 ;; it easier to strip things out without changing the page sizes.
|
|
414 (while (not (eobp))
|
|
415 (forward-line 66)
|
|
416 (setq pages (cons (point-marker) pages)))
|
|
417 (setq pages (nreverse pages))
|
|
418 (while pages
|
|
419 (goto-char (car pages))
|
|
420 (set-marker (car pages) nil)
|
|
421 ;;
|
|
422 ;; The lines are: 3 blank; footer; 6 blank; header; 3 blank.
|
|
423 ;; We're in between the previous footer and the following header,
|
|
424 ;;
|
|
425 ;; First lose 3 blank lines, the header, and then 3 more.
|
|
426 ;;
|
|
427 (setq p (point))
|
|
428 (skip-chars-forward "\n")
|
|
429 (delete-region p (point))
|
|
430 (and (looking-at "[^\n]+\n\n?\n?\n?")
|
|
431 (delete-region (match-beginning 0) (match-end 0)))
|
|
432 ;;
|
|
433 ;; Next lose the footer, and the 3 blank lines after, and before it.
|
|
434 ;; But don't lose the last footer of the manual entry; that contains
|
|
435 ;; the "last change" date, so it's not completely uninteresting.
|
|
436 ;; (Actually lose all blank lines before it; sh(1) needs this.)
|
|
437 ;;
|
|
438 (skip-chars-backward "\n")
|
|
439 (beginning-of-line)
|
|
440 (if (null (cdr pages))
|
|
441 nil
|
|
442 (and (looking-at "[^\n]+\n\n?\n?\n?")
|
|
443 (delete-region (match-beginning 0) (match-end 0))))
|
|
444 (setq p (point))
|
|
445 (skip-chars-backward "\n")
|
|
446 (if (> (- p (point)) 4)
|
|
447 (delete-region (+ 2 (point)) p)
|
|
448 (delete-region (1+ (point)) p))
|
|
449 ; (and (looking-at "\n\n?\n?")
|
|
450 ; (delete-region (match-beginning 0) (match-end 0)))
|
4
|
451
|
8
|
452 (setq pages (cdr pages)))
|
|
453 ;;
|
|
454 ;; Now nuke the extra blank lines at the beginning and end.
|
4
|
455 (goto-char (point-min))
|
8
|
456 (if (looking-at "\n+")
|
|
457 (delete-region (match-beginning 0) (match-end 0)))
|
|
458 (forward-line 1)
|
|
459 (if (looking-at "\n\n+")
|
|
460 (delete-region (1+ (match-beginning 0)) (match-end 0)))
|
|
461 (goto-char (point-max))
|
|
462 (skip-chars-backward "\n")
|
|
463 (delete-region (point) (point-max))
|
|
464 (beginning-of-line)
|
|
465 (forward-char -1)
|
|
466 (setq p (point))
|
|
467 (skip-chars-backward "\n")
|
|
468 (if (= ?\n (following-char)) (forward-char 1))
|
|
469 (if (> (point) (1+ p))
|
|
470 (delete-region (point) p))
|
4
|
471 ))
|
|
472
|
8
|
473 (defun Manual-mouseify-xrefs ()
|
|
474 (goto-char (point-min))
|
|
475 (forward-line 1)
|
|
476 (let ((case-fold-search nil)
|
|
477 s e name extent)
|
|
478 ;; possibly it would be faster to rewrite this expression to search for
|
|
479 ;; a less common sequence first (like "([0-9]") and then back up to see
|
|
480 ;; if it's really a match. This function is 15% of the total time, 13%
|
|
481 ;; of which is this call to re-search-forward.
|
|
482 (while (re-search-forward "[a-zA-Z_][-a-zA-Z0-9_.]*([0-9][a-zA-Z0-9]*)"
|
|
483 nil t)
|
|
484 (setq s (match-beginning 0)
|
|
485 e (match-end 0)
|
|
486 name (buffer-substring s e))
|
|
487 (goto-char s)
|
|
488 (skip-chars-backward " \t")
|
|
489 (if (and (bolp)
|
|
490 (progn (backward-char 1) (= (preceding-char) ?-)))
|
|
491 (progn
|
|
492 (setq s (point))
|
|
493 (skip-chars-backward "-a-zA-Z0-9_.")
|
|
494 (setq name (concat (buffer-substring (point) (1- s)) name))
|
|
495 (setq s (point))))
|
|
496 ;; if there are upper case letters in the section, downcase them.
|
|
497 (if (string-match "(.*[A-Z]+.*)$" name)
|
|
498 (setq name (concat (substring name 0 (match-beginning 0))
|
|
499 (downcase (substring name (match-beginning 0))))))
|
|
500 ;; (setq already-fontified (extent-at s))
|
|
501 (setq extent (make-extent s e))
|
|
502 (set-extent-property extent 'man (list 'Manual-follow-xref name))
|
|
503 (set-extent-property extent 'highlight t)
|
|
504 ;; (if (not already-fontified)...
|
|
505 (set-extent-face extent 'man-xref)
|
|
506 (goto-char e))))
|
4
|
507
|
8
|
508 (defun Manual-follow-xref (&optional name-or-event)
|
|
509 "Invoke `manual-entry' on the cross-reference under the mouse.
|
|
510 When invoked noninteractively, the arg may be an xref string to parse instead."
|
|
511 (interactive "e")
|
|
512 (if (eventp name-or-event)
|
|
513 (let* ((p (event-point name-or-event))
|
|
514 (extent (and p (extent-at p
|
|
515 (event-buffer name-or-event)
|
|
516 'highlight)))
|
|
517 (data (and extent (extent-property extent 'man))))
|
|
518 (if (eq (car-safe data) 'Manual-follow-xref)
|
|
519 (eval data)
|
|
520 (error "no manual cross-reference there.")))
|
24
|
521 (or (manual-entry name-or-event)
|
|
522 ;; If that didn't work, maybe it's in a different section than the
|
|
523 ;; man page writer expected. For example, man pages tend assume
|
|
524 ;; that all user programs are in section 1, but X tends to generate
|
|
525 ;; makefiles that put things in section "n" instead...
|
|
526 (and (string-match "[ \t]*([^)]+)\\'" name-or-event)
|
|
527 (progn
|
|
528 (message "No entries found for %s; checking other sections..."
|
|
529 name-or-event)
|
|
530 (manual-entry
|
|
531 (substring name-or-event 0 (match-beginning 0))
|
|
532 nil t))))))
|
4
|
533
|
8
|
534 (defun Manual-popup-menu (&optional event)
|
|
535 "Pops up a menu of cross-references in this manual page.
|
|
536 If there is a cross-reference under the mouse button which invoked this
|
|
537 command, it will be the first item on the menu. Otherwise, they are
|
|
538 on the menu in the order in which they appear in the buffer."
|
|
539 (interactive "e")
|
|
540 (let ((buffer (current-buffer))
|
|
541 (sep "---")
|
|
542 (prefix "Show Manual Page for ")
|
|
543 xref items)
|
|
544 (cond (event
|
|
545 (setq buffer (event-buffer event))
|
|
546 (let* ((p (event-point event))
|
|
547 (extent (and p (extent-at p buffer 'highlight)))
|
|
548 (data (and extent (extent-property extent 'man))))
|
|
549 (if (eq (car-safe data) 'Manual-follow-xref)
|
|
550 (setq xref (nth 1 data))))))
|
|
551 (if xref (setq items (list sep xref)))
|
|
552 (map-extents #'(lambda (extent ignore)
|
|
553 (let ((data (extent-property extent 'man)))
|
|
554 (if (and (eq (car-safe data) 'Manual-follow-xref)
|
|
555 (not (member (nth 1 data) items)))
|
|
556 (setq items (cons (nth 1 data) items)))
|
|
557 nil))
|
|
558 buffer)
|
|
559 (if (eq sep (car items)) (setq items (cdr items)))
|
|
560 (let ((popup-menu-titles nil))
|
|
561 (popup-menu
|
|
562 (cons "Manual Entry"
|
|
563 (mapcar #'(lambda (item)
|
|
564 (if (eq item sep)
|
|
565 item
|
|
566 (vector (concat prefix item)
|
|
567 (list 'Manual-follow-xref item) t)))
|
|
568 (nreverse items)))))))
|
0
|
569
|
8
|
570 (defun pager-cleanup-hook ()
|
|
571 "cleanup man page if called via $PAGER"
|
|
572 (let ((buf-name (or buffer-file-name (buffer-name))))
|
24
|
573 (if (or (string-match "^/tmp/man[0-9]+" buf-name)
|
|
574 (string-match ".*/man/\\(man\\|cat\\)[1-9a-z]/" buf-name))
|
8
|
575 (let (buffer manpage)
|
|
576 (require 'man)
|
|
577 (goto-char (point-min))
|
|
578 (setq buffer-read-only nil)
|
|
579 (Manual-nuke-nroff-bs)
|
|
580 (goto-char (point-min))
|
|
581 (if (re-search-forward "[^ \t]")
|
|
582 (goto-char (- (point) 1)))
|
|
583 (if (looking-at "\\([a-zA-Z0-9]+\\)[ \t]*(")
|
24
|
584 (setq manpage (buffer-substring (match-beginning 1)
|
|
585 (match-end 1)))
|
8
|
586 (setq manpage "???"))
|
|
587 (setq buffer
|
|
588 (rename-buffer
|
|
589 (generate-new-buffer-name (concat "*man " manpage "*"))))
|
|
590 (setq buffer-file-name nil)
|
|
591 (goto-char (point-min))
|
|
592 (insert (format "%s\n" buf-name))
|
|
593 (goto-char (point-min))
|
|
594 (buffer-disable-undo buffer)
|
|
595 (set-buffer-modified-p nil)
|
|
596 (Manual-mode)
|
|
597 ))))
|
0
|
598
|
8
|
599 (add-hook 'server-visit-hook 'pager-cleanup-hook)
|
4
|
600 (provide 'man)
|