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