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