]> code.delx.au - gnu-emacs/blob - lisp/man.el
Doc fixes.
[gnu-emacs] / lisp / man.el
1 ;;; man.el --- browse UNIX manual pages
2
3 ;; Copyright (C) 1993, 1994, 1996, 1997 Free Software Foundation, Inc.
4
5 ;; Author: Barry A. Warsaw <bwarsaw@cen.com>
6 ;; Maintainer: FSF
7 ;; Keywords: help
8 ;; Adapted-By: ESR, pot
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; This code provides a function, `man', with which you can browse
30 ;; UNIX manual pages. Formatting is done in background so that you
31 ;; can continue to use your Emacs while processing is going on.
32 ;;
33 ;; The mode also supports hypertext-like following of manual page SEE
34 ;; ALSO references, and other features. See below or do `?' in a
35 ;; manual page buffer for details.
36
37 ;; ========== Credits and History ==========
38 ;; In mid 1991, several people posted some interesting improvements to
39 ;; man.el from the standard emacs 18.57 distribution. I liked many of
40 ;; these, but wanted everything in one single package, so I decided
41 ;; to incorporate them into a single manual browsing mode. While
42 ;; much of the code here has been rewritten, and some features added,
43 ;; these folks deserve lots of credit for providing the initial
44 ;; excellent packages on which this one is based.
45
46 ;; Nick Duffek <duffek@chaos.cs.brandeis.edu>, posted a very nice
47 ;; improvement which retrieved and cleaned the manpages in a
48 ;; background process, and which correctly deciphered such options as
49 ;; man -k.
50
51 ;; Eric Rose <erose@jessica.stanford.edu>, submitted manual.el which
52 ;; provided a very nice manual browsing mode.
53
54 ;; This package was available as `superman.el' from the LCD package
55 ;; for some time before it was accepted into Emacs 19. The entry
56 ;; point and some other names have been changed to make it a drop-in
57 ;; replacement for the old man.el package.
58
59 ;; Francesco Potorti` <pot@cnuce.cnr.it> cleaned it up thoroughly,
60 ;; making it faster, more robust and more tolerant of different
61 ;; systems' man idiosyncrasies.
62
63 ;; ========== Features ==========
64 ;; + Runs "man" in the background and pipes the results through a
65 ;; series of sed and awk scripts so that all retrieving and cleaning
66 ;; is done in the background. The cleaning commands are configurable.
67 ;; + Syntax is the same as Un*x man
68 ;; + Functionality is the same as Un*x man, including "man -k" and
69 ;; "man <section>", etc.
70 ;; + Provides a manual browsing mode with keybindings for traversing
71 ;; the sections of a manpage, following references in the SEE ALSO
72 ;; section, and more.
73 ;; + Multiple manpages created with the same man command are put into
74 ;; a narrowed buffer circular list.
75
76 ;; ============= TODO ===========
77 ;; - Add a command for printing.
78 ;; - The awk script deletes multiple blank lines. This behaviour does
79 ;; not allow to understand if there was indeed a blank line at the
80 ;; end or beginning of a page (after the header, or before the
81 ;; footer). A different algorithm should be used. It is easy to
82 ;; compute how many blank lines there are before and after the page
83 ;; headers, and after the page footer. But it is possible to compute
84 ;; the number of blank lines before the page footer by euristhics
85 ;; only. Is it worth doing?
86 ;; - Allow a user option to mean that all the manpages should go in
87 ;; the same buffer, where they can be browsed with M-n and M-p.
88 ;; - Allow completion on the manpage name when calling man. This
89 ;; requires a reliable list of places where manpages can be found. The
90 ;; drawback would be that if the list is not complete, the user might
91 ;; be led to believe that the manpages in the missing directories do
92 ;; not exist.
93
94 \f
95 ;;; Code:
96
97 (require 'assoc)
98
99 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
100 ;; empty defvars (keep the compiler quiet)
101
102 (defgroup man nil
103 "Browse UNIX manual pages."
104 :prefix "Man-"
105 :group 'help)
106
107
108 (defvar Man-notify)
109 (defvar Man-current-page)
110 (defvar Man-page-list)
111 (defcustom Man-filter-list nil
112 "*Manpage cleaning filter command phrases.
113 This variable contains a list of the following form:
114
115 '((command-string phrase-string*)*)
116
117 Each phrase-string is concatenated onto the command-string to form a
118 command filter. The (standard) output (and standard error) of the Un*x
119 man command is piped through each command filter in the order the
120 commands appear in the association list. The final output is placed in
121 the manpage buffer."
122 :type '(repeat (list (string :tag "Command String")
123 (repeat :inline t
124 (string :tag "Phrase String"))))
125 :group 'man)
126
127 (defvar Man-original-frame)
128 (defvar Man-arguments)
129 (defvar Man-sections-alist)
130 (defvar Man-refpages-alist)
131 (defvar Man-uses-untabify-flag t
132 "Non-nil means use `untabify' instead of `Man-untabify-command'.")
133 (defvar Man-page-mode-string)
134 (defvar Man-sed-script nil
135 "Script for sed to nuke backspaces and ANSI codes from manpages.")
136
137 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
138 ;; user variables
139
140 (defcustom Man-fontify-manpage-flag t
141 "*Non-nil means make up the manpage with fonts."
142 :type 'boolean
143 :group 'man)
144
145 (defcustom Man-overstrike-face 'bold
146 "*Face to use when fontifying overstrike."
147 :type 'face
148 :group 'man)
149
150 (defcustom Man-underline-face 'underline
151 "*Face to use when fontifying underlining."
152 :type 'face
153 :group 'man)
154
155 ;; Use the value of the obsolete user option Man-notify, if set.
156 (defcustom Man-notify-method (if (boundp 'Man-notify) Man-notify 'friendly)
157 "*Selects the behavior when manpage is ready.
158 This variable may have one of the following values, where (sf) means
159 that the frames are switched, so the manpage is displayed in the frame
160 where the man command was called from:
161
162 newframe -- put the manpage in its own frame (see `Man-frame-parameters')
163 pushy -- make the manpage the current buffer in the current window
164 bully -- make the manpage the current buffer and only window (sf)
165 aggressive -- make the manpage the current buffer in the other window (sf)
166 friendly -- display manpage in the other window but don't make current (sf)
167 polite -- don't display manpage, but prints message and beep when ready
168 quiet -- like `polite', but don't beep
169 meek -- make no indication that the manpage is ready
170
171 Any other value of `Man-notify-method' is equivalent to `meek'."
172 :type '(radio (const newframe) (const pushy) (const bully)
173 (const aggressive) (const friendly)
174 (const polite) (const quiet) (const meek))
175 :group 'man)
176
177 (defcustom Man-frame-parameters nil
178 "*Frame parameter list for creating a new frame for a manual page."
179 :type 'sexp
180 :group 'man)
181
182 (defcustom Man-downcase-section-letters-flag t
183 "*Non-nil means letters in sections are converted to lower case.
184 Some Un*x man commands can't handle uppercase letters in sections, for
185 example \"man 2V chmod\", but they are often displayed in the manpage
186 with the upper case letter. When this variable is t, the section
187 letter (e.g., \"2V\") is converted to lowercase (e.g., \"2v\") before
188 being sent to the man background process."
189 :type 'boolean
190 :group 'man)
191
192 (defcustom Man-circular-pages-flag t
193 "*Non-nil means the manpage list is treated as circular for traversal."
194 :type 'boolean
195 :group 'man)
196
197 (defcustom Man-section-translations-alist
198 (list
199 '("3C++" . "3")
200 ;; Some systems have a real 3x man section, so let's comment this.
201 ;; '("3X" . "3") ; Xlib man pages
202 '("3X11" . "3")
203 '("1-UCB" . ""))
204 "*Association list of bogus sections to real section numbers.
205 Some manpages (e.g. the Sun C++ 2.1 manpages) have section numbers in
206 their references which Un*x `man' does not recognize. This
207 association list is used to translate those sections, when found, to
208 the associated section number."
209 :type '(repeat (cons (string :tag "Bogus Section")
210 (string :tag "Real Section")))
211 :group 'man)
212
213 (defvar manual-program "man"
214 "The name of the program that produces man pages.")
215
216 (defvar Man-untabify-command "pr"
217 "Command used for untabifying.")
218
219 (defvar Man-untabify-command-args (list "-t" "-e")
220 "List of arguments to be passed to `Man-untabify-command' (which see).")
221
222 (defvar Man-sed-command "sed"
223 "Command used for processing sed scripts.")
224
225 (defvar Man-awk-command "awk"
226 "Command used for processing awk scripts.")
227
228 (defvar Man-mode-line-format
229 '("-"
230 mode-line-mule-info
231 mode-line-modified
232 mode-line-frame-identification
233 mode-line-buffer-identification " "
234 global-mode-string
235 " " Man-page-mode-string
236 " %[(" mode-name mode-line-process minor-mode-alist "%n)%]--"
237 (line-number-mode "L%l--")
238 (column-number-mode "C%c--")
239 (-3 . "%p") "-%-")
240 "Mode line format for manual mode buffer.")
241
242 (defvar Man-mode-map nil
243 "Keymap for Man mode.")
244
245 (defvar Man-mode-hook nil
246 "Hook run when Man mode is enabled.")
247
248 (defvar Man-cooked-hook nil
249 "Hook run after removing backspaces but before `Man-mode' processing.")
250
251 (defvar Man-name-regexp "[-a-zA-Z0-9_][-a-zA-Z0-9_.]*"
252 "Regular expression describing the name of a manpage (without section).")
253
254 (defvar Man-section-regexp "[0-9][a-zA-Z+]*\\|[LNln]"
255 "Regular expression describing a manpage section within parentheses.")
256
257 (defvar Man-page-header-regexp
258 (if (and (string-match "-solaris2\\." system-configuration)
259 (not (string-match "-solaris2\\.[123435]$" system-configuration)))
260 (concat "^[-A-Za-z0-9_].*[ \t]\\(" Man-name-regexp
261 "(\\(" Man-section-regexp "\\))\\)$")
262 (concat "^[ \t]*\\(" Man-name-regexp
263 "(\\(" Man-section-regexp "\\))\\).*\\1"))
264 "Regular expression describing the heading of a page.")
265
266 (defvar Man-heading-regexp "^\\([A-Z][A-Z ]+\\)$"
267 "Regular expression describing a manpage heading entry.")
268
269 (defvar Man-see-also-regexp "SEE ALSO"
270 "Regular expression for SEE ALSO heading (or your equivalent).
271 This regexp should not start with a `^' character.")
272
273 (defvar Man-first-heading-regexp "^[ \t]*NAME$\\|^[ \t]*No manual entry fo.*$"
274 "Regular expression describing first heading on a manpage.
275 This regular expression should start with a `^' character.")
276
277 (defvar Man-reference-regexp
278 (concat "\\(" Man-name-regexp "\\)(\\(" Man-section-regexp "\\))")
279 "Regular expression describing a reference in the SEE ALSO section.")
280
281 (defvar Man-switches ""
282 "Switches passed to the man command, as a single string.")
283
284 (defvar Man-specified-section-option
285 (if (string-match "-solaris[0-9.]*$" system-configuration)
286 "-s"
287 "")
288 "Option that indicates a specified a manual section name.")
289
290 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
291 ;; end user variables
292 \f
293 ;; other variables and keymap initializations
294 (make-variable-buffer-local 'Man-sections-alist)
295 (make-variable-buffer-local 'Man-refpages-alist)
296 (make-variable-buffer-local 'Man-page-list)
297 (make-variable-buffer-local 'Man-current-page)
298 (make-variable-buffer-local 'Man-page-mode-string)
299 (make-variable-buffer-local 'Man-original-frame)
300 (make-variable-buffer-local 'Man-arguments)
301
302 (setq-default Man-sections-alist nil)
303 (setq-default Man-refpages-alist nil)
304 (setq-default Man-page-list nil)
305 (setq-default Man-current-page 0)
306 (setq-default Man-page-mode-string "1 of 1")
307
308 (defconst Man-sysv-sed-script "\
309 /\b/ { s/_\b//g
310 s/\b_//g
311 s/o\b+/o/g
312 s/+\bo/o/g
313 :ovstrk
314 s/\\(.\\)\b\\1/\\1/g
315 t ovstrk
316 }
317 /\e\\[[0-9][0-9]*m/ s///g"
318 "Script for sysV-like sed to nuke backspaces and ANSI codes from manpages.")
319
320 (defconst Man-berkeley-sed-script "\
321 /\b/ { s/_\b//g\\
322 s/\b_//g\\
323 s/o\b+/o/g\\
324 s/+\bo/o/g\\
325 :ovstrk\\
326 s/\\(.\\)\b\\1/\\1/g\\
327 t ovstrk\\
328 }\\
329 /\e\\[[0-9][0-9]*m/ s///g"
330 "Script for berkeley-like sed to nuke backspaces and ANSI codes from manpages.")
331
332 (defvar man-mode-syntax-table
333 (let ((table (copy-syntax-table (standard-syntax-table))))
334 (modify-syntax-entry ?. "w" table)
335 (modify-syntax-entry ?_ "w" table)
336 table)
337 "Syntax table used in Man mode buffers.")
338
339 (if Man-mode-map
340 nil
341 (setq Man-mode-map (make-keymap))
342 (suppress-keymap Man-mode-map)
343 (define-key Man-mode-map " " 'scroll-up)
344 (define-key Man-mode-map "\177" 'scroll-down)
345 (define-key Man-mode-map "n" 'Man-next-section)
346 (define-key Man-mode-map "p" 'Man-previous-section)
347 (define-key Man-mode-map "\en" 'Man-next-manpage)
348 (define-key Man-mode-map "\ep" 'Man-previous-manpage)
349 (define-key Man-mode-map ">" 'end-of-buffer)
350 (define-key Man-mode-map "<" 'beginning-of-buffer)
351 (define-key Man-mode-map "." 'beginning-of-buffer)
352 (define-key Man-mode-map "r" 'Man-follow-manual-reference)
353 (define-key Man-mode-map "g" 'Man-goto-section)
354 (define-key Man-mode-map "s" 'Man-goto-see-also-section)
355 (define-key Man-mode-map "k" 'Man-kill)
356 (define-key Man-mode-map "q" 'Man-quit)
357 (define-key Man-mode-map "m" 'man)
358 (define-key Man-mode-map "\r" 'man-follow)
359 (define-key Man-mode-map "?" 'describe-mode)
360 )
361
362 \f
363 ;; ======================================================================
364 ;; utilities
365
366 (defun Man-init-defvars ()
367 "Used for initialising variables based on the value of `window-system'.
368 This is necessary if one wants to dump man.el with Emacs."
369
370 ;; The following is necessary until fonts are implemented on
371 ;; terminals.
372 (setq Man-fontify-manpage-flag (and Man-fontify-manpage-flag
373 (display-color-p)))
374
375 ;; Avoid possible error in call-process by using a directory that must exist.
376 (let ((default-directory "/"))
377 (setq Man-sed-script
378 (cond
379 (Man-fontify-manpage-flag
380 nil)
381 ((= 0 (call-process Man-sed-command nil nil nil Man-sysv-sed-script))
382 Man-sysv-sed-script)
383 ((= 0 (call-process Man-sed-command nil nil nil Man-berkeley-sed-script))
384 Man-berkeley-sed-script)
385 (t
386 nil))))
387
388 (setq Man-filter-list
389 ;; Avoid trailing nil which confuses customize.
390 (apply 'list
391 (cons
392 Man-sed-command
393 (list
394 (if Man-sed-script
395 (concat "-e '" Man-sed-script "'")
396 "")
397 "-e '/^[\001-\032][\001-\032]*$/d'"
398 "-e '/\e[789]/s///g'"
399 "-e '/Reformatting page. Wait/d'"
400 "-e '/Reformatting entry. Wait/d'"
401 "-e '/^[ \t]*Hewlett-Packard[ \t]Company[ \t]*-[ \t][0-9]*[ \t]-/d'"
402 "-e '/^[ \t]*Hewlett-Packard[ \t]*-[ \t][0-9]*[ \t]-.*$/d'"
403 "-e '/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d'"
404 "-e '/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d'"
405 "-e '/^Printed[ \t][0-9].*[0-9]$/d'"
406 "-e '/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d'"
407 "-e '/^[A-Za-z].*Last[ \t]change:/d'"
408 "-e '/^Sun[ \t]Release[ \t][0-9].*[0-9]$/d'"
409 "-e '/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d'"
410 "-e '/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d'"
411 ))
412 (cons
413 Man-awk-command
414 (list
415 "'\n"
416 "BEGIN { blankline=0; anonblank=0; }\n"
417 "/^$/ { if (anonblank==0) next; }\n"
418 "{ anonblank=1; }\n"
419 "/^$/ { blankline++; next; }\n"
420 "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }\n"
421 "'"
422 ))
423 (if (not Man-uses-untabify-flag)
424 ;; The outer list will be stripped off by apply.
425 (list (cons
426 Man-untabify-command
427 Man-untabify-command-args))
428 )))
429 )
430
431 (defsubst Man-match-substring (&optional n string)
432 "Return the substring matched by the last search.
433 Optional arg N means return the substring matched by the Nth paren
434 grouping. Optional second arg STRING means return a substring from
435 that string instead of from the current buffer."
436 (if (null n) (setq n 0))
437 (if string
438 (substring string (match-beginning n) (match-end n))
439 (buffer-substring (match-beginning n) (match-end n))))
440
441 (defsubst Man-make-page-mode-string ()
442 "Formats part of the mode line for Man mode."
443 (format "%s page %d of %d"
444 (or (nth 2 (nth (1- Man-current-page) Man-page-list))
445 "")
446 Man-current-page
447 (length Man-page-list)))
448
449 (defsubst Man-build-man-command ()
450 "Builds the entire background manpage and cleaning command."
451 (let ((command (concat manual-program " " Man-switches
452 ; Stock MS-DOS shells cannot redirect stderr;
453 ; `call-process' below sends it to /dev/null,
454 ; so we don't need `2>' even with DOS shells
455 ; which do support stderr redirection.
456 (if (not (fboundp 'start-process))
457 " %s"
458 (concat " %s 2>" null-device))))
459 (flist Man-filter-list))
460 (while (and flist (car flist))
461 (let ((pcom (car (car flist)))
462 (pargs (cdr (car flist))))
463 (setq command
464 (concat command " | " pcom " "
465 (mapconcat '(lambda (phrase)
466 (if (not (stringp phrase))
467 (error "Malformed Man-filter-list"))
468 phrase)
469 pargs " ")))
470 (setq flist (cdr flist))))
471 command))
472
473 (defun Man-translate-references (ref)
474 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style.
475 Leave it as is if already in that style. Possibly downcase and
476 translate the section (see the Man-downcase-section-letters-flag
477 and the Man-section-translations-alist variables)."
478 (let ((name "")
479 (section "")
480 (slist Man-section-translations-alist))
481 (cond
482 ;; "chmod(2V)" case ?
483 ((string-match (concat "^" Man-reference-regexp "$") ref)
484 (setq name (Man-match-substring 1 ref)
485 section (Man-match-substring 2 ref)))
486 ;; "2v chmod" case ?
487 ((string-match (concat "^\\(" Man-section-regexp
488 "\\) +\\(" Man-name-regexp "\\)$") ref)
489 (setq name (Man-match-substring 2 ref)
490 section (Man-match-substring 1 ref))))
491 (if (string= name "")
492 ref ; Return the reference as is
493 (if Man-downcase-section-letters-flag
494 (setq section (downcase section)))
495 (while slist
496 (let ((s1 (car (car slist)))
497 (s2 (cdr (car slist))))
498 (setq slist (cdr slist))
499 (if Man-downcase-section-letters-flag
500 (setq s1 (downcase s1)))
501 (if (not (string= s1 section)) nil
502 (setq section (if Man-downcase-section-letters-flag
503 (downcase s2)
504 s2)
505 slist nil))))
506 (concat Man-specified-section-option section " " name))))
507
508 \f
509 ;; ======================================================================
510 ;; default man entry: get word under point
511
512 (defsubst Man-default-man-entry ()
513 "Make a guess at a default manual entry.
514 This guess is based on the text surrounding the cursor, and the
515 default section number is selected from `Man-auto-section-alist'."
516 (let (word)
517 (save-excursion
518 ;; Default man entry title is any word the cursor is on, or if
519 ;; cursor not on a word, then nearest preceding word.
520 (setq word (current-word))
521 (if (string-match "[._]+$" word)
522 (setq word (substring word 0 (match-beginning 0))))
523 ;; If looking at something like ioctl(2) or brc(1M), include the
524 ;; section number in the returned value. Remove text properties.
525 (forward-word 1)
526 ;; Use `format' here to clear any text props from `word'.
527 (format "%s%s"
528 word
529 (if (looking-at
530 (concat "[ \t]*([ \t]*\\(" Man-section-regexp "\\)[ \t]*)"))
531 (format "(%s)" (Man-match-substring 1))
532 "")))))
533
534 \f
535 ;; ======================================================================
536 ;; Top level command and background process sentinel
537
538 ;; For compatibility with older versions.
539 ;;;###autoload
540 (defalias 'manual-entry 'man)
541
542 ;;;###autoload
543 (defun man (man-args)
544 "Get a Un*x manual page and put it in a buffer.
545 This command is the top-level command in the man package. It runs a Un*x
546 command to retrieve and clean a manpage in the background and places the
547 results in a Man mode (manpage browsing) buffer. See variable
548 `Man-notify-method' for what happens when the buffer is ready.
549 If a buffer already exists for this man page, it will display immediately."
550 (interactive
551 (list (let* ((default-entry (Man-default-man-entry))
552 (input (read-string
553 (format "Manual entry%s: "
554 (if (string= default-entry "")
555 ""
556 (format " (default %s)" default-entry))))))
557 (if (string= input "")
558 (if (string= default-entry "")
559 (error "No man args given")
560 default-entry)
561 input))))
562
563 ;; Possibly translate the "subject(section)" syntax into the
564 ;; "section subject" syntax and possibly downcase the section.
565 (setq man-args (Man-translate-references man-args))
566
567 (Man-getpage-in-background man-args))
568
569 ;;;###autoload
570 (defun man-follow (man-args)
571 "Get a Un*x manual page of the item under point and put it in a buffer."
572 (interactive (list (Man-default-man-entry)))
573 (if (or (not man-args)
574 (string= man-args ""))
575 (error "No item under point")
576 (man man-args)))
577
578 (defun Man-getpage-in-background (topic)
579 "Use TOPIC to build and fire off the manpage and cleaning command."
580 (let* ((man-args topic)
581 (bufname (concat "*Man " man-args "*"))
582 (buffer (get-buffer bufname)))
583 (if buffer
584 (Man-notify-when-ready buffer)
585 (require 'env)
586 (message "Invoking %s %s in the background" manual-program man-args)
587 (setq buffer (generate-new-buffer bufname))
588 (save-excursion
589 (set-buffer buffer)
590 (setq Man-original-frame (selected-frame))
591 (setq Man-arguments man-args))
592 (let ((process-environment (copy-sequence process-environment))
593 ;; The following is so Awk script gets \n intact
594 ;; But don't prevent decoding of the outside.
595 (coding-system-for-write 'raw-text-unix)
596 ;; Avoid possible error by using a directory that always exists.
597 (default-directory "/"))
598 ;; Prevent any attempt to use display terminal fanciness.
599 (setenv "TERM" "dumb")
600 (if (fboundp 'start-process)
601 (set-process-sentinel
602 (start-process manual-program buffer "sh" "-c"
603 (format (Man-build-man-command) man-args))
604 'Man-bgproc-sentinel)
605 (progn
606 (let ((exit-status
607 (call-process shell-file-name nil (list buffer nil) nil "-c"
608 (format (Man-build-man-command) man-args)))
609 (msg ""))
610 (or (and (numberp exit-status)
611 (= exit-status 0))
612 (and (numberp exit-status)
613 (setq msg
614 (format "exited abnormally with code %d"
615 exit-status)))
616 (setq msg exit-status))
617 (Man-bgproc-sentinel bufname msg))))))))
618
619 (defun Man-notify-when-ready (man-buffer)
620 "Notify the user when MAN-BUFFER is ready.
621 See the variable `Man-notify-method' for the different notification behaviors."
622 (let ((saved-frame (save-excursion
623 (set-buffer man-buffer)
624 Man-original-frame)))
625 (cond
626 ((eq Man-notify-method 'newframe)
627 ;; Since we run asynchronously, perhaps while Emacs is waiting
628 ;; for input, we must not leave a different buffer current. We
629 ;; can't rely on the editor command loop to reselect the
630 ;; selected window's buffer.
631 (save-excursion
632 (let ((frame (make-frame Man-frame-parameters)))
633 (set-window-buffer (frame-selected-window frame) man-buffer)
634 (set-window-dedicated-p (frame-selected-window frame) t))))
635 ((eq Man-notify-method 'pushy)
636 (switch-to-buffer man-buffer))
637 ((eq Man-notify-method 'bully)
638 (and window-system
639 (frame-live-p saved-frame)
640 (select-frame saved-frame))
641 (pop-to-buffer man-buffer)
642 (delete-other-windows))
643 ((eq Man-notify-method 'aggressive)
644 (and window-system
645 (frame-live-p saved-frame)
646 (select-frame saved-frame))
647 (pop-to-buffer man-buffer))
648 ((eq Man-notify-method 'friendly)
649 (and window-system
650 (frame-live-p saved-frame)
651 (select-frame saved-frame))
652 (display-buffer man-buffer 'not-this-window))
653 ((eq Man-notify-method 'polite)
654 (beep)
655 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
656 ((eq Man-notify-method 'quiet)
657 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
658 ((or (eq Man-notify-method 'meek)
659 t)
660 (message ""))
661 )))
662
663 (defun Man-softhyphen-to-minus ()
664 ;; \255 is some kind of dash in Latin-N. Versions of Debian man, at
665 ;; least, emit it even when not in a Latin-N locale.
666 (unless (eq t (compare-strings "latin-" 0 nil
667 current-language-environment 0 6 t))
668 (goto-char (point-min))
669 (let ((str "\255"))
670 (if enable-multibyte-characters
671 (setq str (string-as-multibyte str)))
672 (while (search-forward str nil t) (replace-match "-")))))
673
674 (defun Man-fontify-manpage ()
675 "Convert overstriking and underlining to the correct fonts.
676 Same for the ANSI bold and normal escape sequences."
677 (interactive)
678 (message "Please wait: making up the %s man page..." Man-arguments)
679 (goto-char (point-min))
680 (while (search-forward "\e[1m" nil t)
681 (delete-backward-char 4)
682 (put-text-property (point)
683 (progn (if (search-forward "\e[0m" nil 'move)
684 (delete-backward-char 4))
685 (point))
686 'face Man-overstrike-face))
687 (goto-char (point-min))
688 (while (search-forward "_\b" nil t)
689 (backward-delete-char 2)
690 (put-text-property (point) (1+ (point)) 'face Man-underline-face))
691 (goto-char (point-min))
692 (while (search-forward "\b_" nil t)
693 (backward-delete-char 2)
694 (put-text-property (1- (point)) (point) 'face Man-underline-face))
695 (goto-char (point-min))
696 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
697 (replace-match "\\1")
698 (put-text-property (1- (point)) (point) 'face Man-overstrike-face))
699 (goto-char (point-min))
700 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t)
701 (replace-match "o")
702 (put-text-property (1- (point)) (point) 'face 'bold))
703 (goto-char (point-min))
704 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t)
705 (replace-match "+")
706 (put-text-property (1- (point)) (point) 'face 'bold))
707 (Man-softhyphen-to-minus)
708 (message "%s man page made up" Man-arguments))
709
710 (defun Man-cleanup-manpage ()
711 "Remove overstriking and underlining from the current buffer."
712 (interactive)
713 (message "Please wait: cleaning up the %s man page..."
714 Man-arguments)
715 (if (or (interactive-p) (not Man-sed-script))
716 (progn
717 (goto-char (point-min))
718 (while (search-forward "_\b" nil t) (backward-delete-char 2))
719 (goto-char (point-min))
720 (while (search-forward "\b_" nil t) (backward-delete-char 2))
721 (goto-char (point-min))
722 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
723 (replace-match "\\1"))
724 (goto-char (point-min))
725 (while (re-search-forward "\e\\[[0-9]+m" nil t) (replace-match ""))
726 (goto-char (point-min))
727 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t) (replace-match "o"))
728 ))
729 (goto-char (point-min))
730 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t) (replace-match "+"))
731 (Man-softhyphen-to-minus)
732 (message "%s man page cleaned up" Man-arguments))
733
734 (defun Man-bgproc-sentinel (process msg)
735 "Manpage background process sentinel.
736 When manpage command is run asynchronously, PROCESS is the process
737 object for the manpage command; when manpage command is run
738 synchronously, PROCESS is the name of the buffer where the manpage
739 command is run. Second argument MSG is the exit message of the
740 manpage command."
741 (let ((Man-buffer (if (stringp process) (get-buffer process)
742 (process-buffer process)))
743 (delete-buff nil)
744 (err-mess nil))
745
746 (if (null (buffer-name Man-buffer)) ;; deleted buffer
747 (or (stringp process)
748 (set-process-buffer process nil))
749
750 (save-excursion
751 (set-buffer Man-buffer)
752 (let ((case-fold-search nil))
753 (goto-char (point-min))
754 (cond ((or (looking-at "No \\(manual \\)*entry for")
755 (looking-at "[^\n]*: nothing appropriate$"))
756 (setq err-mess (buffer-substring (point)
757 (progn
758 (end-of-line) (point)))
759 delete-buff t))
760 ((or (stringp process)
761 (not (and (eq (process-status process) 'exit)
762 (= (process-exit-status process) 0))))
763 (or (zerop (length msg))
764 (progn
765 (setq err-mess
766 (concat (buffer-name Man-buffer)
767 ": process "
768 (let ((eos (1- (length msg))))
769 (if (= (aref msg eos) ?\n)
770 (substring msg 0 eos) msg))))
771 (goto-char (point-max))
772 (insert (format "\nprocess %s" msg))))
773 ))
774 (if delete-buff
775 (kill-buffer Man-buffer)
776 (if Man-fontify-manpage-flag
777 (Man-fontify-manpage)
778 (Man-cleanup-manpage))
779 (run-hooks 'Man-cooked-hook)
780 (Man-mode)
781 (set-buffer-modified-p nil)
782 ))
783 ;; Restore case-fold-search before calling
784 ;; Man-notify-when-ready because it may switch buffers.
785
786 (if (not delete-buff)
787 (Man-notify-when-ready Man-buffer))
788
789 (if err-mess
790 (error err-mess))
791 ))))
792
793 \f
794 ;; ======================================================================
795 ;; set up manual mode in buffer and build alists
796
797 (defun Man-mode ()
798 "A mode for browsing Un*x manual pages.
799
800 The following man commands are available in the buffer. Try
801 \"\\[describe-key] <key> RET\" for more information:
802
803 \\[man] Prompt to retrieve a new manpage.
804 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
805 \\[Man-next-manpage] Jump to next manpage in circular list.
806 \\[Man-previous-manpage] Jump to previous manpage in circular list.
807 \\[Man-next-section] Jump to next manpage section.
808 \\[Man-previous-section] Jump to previous manpage section.
809 \\[Man-goto-section] Go to a manpage section.
810 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
811 \\[Man-quit] Deletes the manpage window, bury its buffer.
812 \\[Man-kill] Deletes the manpage window, kill its buffer.
813 \\[describe-mode] Prints this help text.
814
815 The following variables may be of some use. Try
816 \"\\[describe-variable] <variable-name> RET\" for more information:
817
818 `Man-notify-method' What happens when manpage formatting is done.
819 `Man-downcase-section-letters-flag' Force section letters to lower case.
820 `Man-circular-pages-flag' Treat multiple manpage list as circular.
821 `Man-auto-section-alist' List of major modes and their section numbers.
822 `Man-section-translations-alist' List of section numbers and their Un*x equiv.
823 `Man-filter-list' Background manpage filter command.
824 `Man-mode-line-format' Mode line format for Man mode buffers.
825 `Man-mode-map' Keymap bindings for Man mode buffers.
826 `Man-mode-hook' Normal hook run on entry to Man mode.
827 `Man-section-regexp' Regexp describing manpage section letters.
828 `Man-heading-regexp' Regexp describing section headers.
829 `Man-see-also-regexp' Regexp for SEE ALSO section (or your equiv).
830 `Man-first-heading-regexp' Regexp for first heading on a manpage.
831 `Man-reference-regexp' Regexp matching a references in SEE ALSO.
832 `Man-switches' Background `man' command switches.
833
834 The following key bindings are currently in effect in the buffer:
835 \\{Man-mode-map}"
836 (interactive)
837 (setq major-mode 'Man-mode
838 mode-name "Man"
839 buffer-auto-save-file-name nil
840 mode-line-format Man-mode-line-format
841 truncate-lines t
842 buffer-read-only t)
843 (buffer-disable-undo (current-buffer))
844 (auto-fill-mode -1)
845 (use-local-map Man-mode-map)
846 (set-syntax-table man-mode-syntax-table)
847 (Man-build-page-list)
848 (Man-strip-page-headers)
849 (Man-unindent)
850 (Man-goto-page 1)
851 (run-hooks 'Man-mode-hook))
852
853 (defsubst Man-build-section-alist ()
854 "Build the association list of manpage sections."
855 (setq Man-sections-alist nil)
856 (goto-char (point-min))
857 (let ((case-fold-search nil))
858 (while (re-search-forward Man-heading-regexp (point-max) t)
859 (aput 'Man-sections-alist (Man-match-substring 1))
860 (forward-line 1))))
861
862 (defsubst Man-build-references-alist ()
863 "Build the association list of references (in the SEE ALSO section)."
864 (setq Man-refpages-alist nil)
865 (save-excursion
866 (if (Man-find-section Man-see-also-regexp)
867 (let ((start (progn (forward-line 1) (point)))
868 (end (progn
869 (Man-next-section 1)
870 (point)))
871 hyphenated
872 (runningpoint -1))
873 (save-restriction
874 (narrow-to-region start end)
875 (goto-char (point-min))
876 (back-to-indentation)
877 (while (and (not (eobp)) (/= (point) runningpoint))
878 (setq runningpoint (point))
879 (if (re-search-forward Man-reference-regexp end t)
880 (let* ((word (Man-match-substring 0))
881 (len (1- (length word))))
882 (if hyphenated
883 (setq word (concat hyphenated word)
884 hyphenated nil))
885 (if (= (aref word len) ?-)
886 (setq hyphenated (substring word 0 len))
887 (aput 'Man-refpages-alist word))))
888 (skip-chars-forward " \t\n,")))))))
889
890 (defun Man-build-page-list ()
891 "Build the list of separate manpages in the buffer."
892 (setq Man-page-list nil)
893 (let ((page-start (point-min))
894 (page-end (point-max))
895 (header ""))
896 (goto-char page-start)
897 ;; (switch-to-buffer (current-buffer))(debug)
898 (while (not (eobp))
899 (setq header
900 (if (looking-at Man-page-header-regexp)
901 (Man-match-substring 1)
902 nil))
903 ;; Go past both the current and the next Man-first-heading-regexp
904 (if (re-search-forward Man-first-heading-regexp nil 'move 2)
905 (let ((p (progn (beginning-of-line) (point))))
906 ;; We assume that the page header is delimited by blank
907 ;; lines and that it contains at most one blank line. So
908 ;; if we back by three blank lines we will be sure to be
909 ;; before the page header but not before the possible
910 ;; previous page header.
911 (search-backward "\n\n" nil t 3)
912 (if (re-search-forward Man-page-header-regexp p 'move)
913 (beginning-of-line))))
914 (setq page-end (point))
915 (setq Man-page-list (append Man-page-list
916 (list (list (copy-marker page-start)
917 (copy-marker page-end)
918 header))))
919 (setq page-start page-end)
920 )))
921
922 (defun Man-strip-page-headers ()
923 "Strip all the page headers but the first from the manpage."
924 (let ((buffer-read-only nil)
925 (case-fold-search nil)
926 (page-list Man-page-list)
927 (page ())
928 (header ""))
929 (while page-list
930 (setq page (car page-list))
931 (and (nth 2 page)
932 (goto-char (car page))
933 (re-search-forward Man-first-heading-regexp nil t)
934 (setq header (buffer-substring (car page) (match-beginning 0)))
935 ;; Since the awk script collapses all successive blank
936 ;; lines into one, and since we don't want to get rid of
937 ;; the fast awk script, one must choose between adding
938 ;; spare blank lines between pages when there were none and
939 ;; deleting blank lines at page boundaries when there were
940 ;; some. We choose the first, so we comment the following
941 ;; line.
942 ;; (setq header (concat "\n" header)))
943 (while (search-forward header (nth 1 page) t)
944 (replace-match "")))
945 (setq page-list (cdr page-list)))))
946
947 (defun Man-unindent ()
948 "Delete the leading spaces that indent the manpage."
949 (let ((buffer-read-only nil)
950 (case-fold-search nil)
951 (page-list Man-page-list))
952 (while page-list
953 (let ((page (car page-list))
954 (indent "")
955 (nindent 0))
956 (narrow-to-region (car page) (car (cdr page)))
957 (if Man-uses-untabify-flag
958 (untabify (point-min) (point-max)))
959 (if (catch 'unindent
960 (goto-char (point-min))
961 (if (not (re-search-forward Man-first-heading-regexp nil t))
962 (throw 'unindent nil))
963 (beginning-of-line)
964 (setq indent (buffer-substring (point)
965 (progn
966 (skip-chars-forward " ")
967 (point))))
968 (setq nindent (length indent))
969 (if (zerop nindent)
970 (throw 'unindent nil))
971 (setq indent (concat indent "\\|$"))
972 (goto-char (point-min))
973 (while (not (eobp))
974 (if (looking-at indent)
975 (forward-line 1)
976 (throw 'unindent nil)))
977 (goto-char (point-min)))
978 (while (not (eobp))
979 (or (eolp)
980 (delete-char nindent))
981 (forward-line 1)))
982 (setq page-list (cdr page-list))
983 ))))
984
985 \f
986 ;; ======================================================================
987 ;; Man mode commands
988
989 (defun Man-next-section (n)
990 "Move point to Nth next section (default 1)."
991 (interactive "p")
992 (let ((case-fold-search nil))
993 (if (looking-at Man-heading-regexp)
994 (forward-line 1))
995 (if (re-search-forward Man-heading-regexp (point-max) t n)
996 (beginning-of-line)
997 (goto-char (point-max)))))
998
999 (defun Man-previous-section (n)
1000 "Move point to Nth previous section (default 1)."
1001 (interactive "p")
1002 (let ((case-fold-search nil))
1003 (if (looking-at Man-heading-regexp)
1004 (forward-line -1))
1005 (if (re-search-backward Man-heading-regexp (point-min) t n)
1006 (beginning-of-line)
1007 (goto-char (point-min)))))
1008
1009 (defun Man-find-section (section)
1010 "Move point to SECTION if it exists, otherwise don't move point.
1011 Returns t if section is found, nil otherwise."
1012 (let ((curpos (point))
1013 (case-fold-search nil))
1014 (goto-char (point-min))
1015 (if (re-search-forward (concat "^" section) (point-max) t)
1016 (progn (beginning-of-line) t)
1017 (goto-char curpos)
1018 nil)
1019 ))
1020
1021 (defun Man-goto-section ()
1022 "Query for section to move point to."
1023 (interactive)
1024 (aput 'Man-sections-alist
1025 (let* ((default (aheadsym Man-sections-alist))
1026 (completion-ignore-case t)
1027 chosen
1028 (prompt (concat "Go to section: (default " default ") ")))
1029 (setq chosen (completing-read prompt Man-sections-alist))
1030 (if (or (not chosen)
1031 (string= chosen ""))
1032 default
1033 chosen)))
1034 (Man-find-section (aheadsym Man-sections-alist)))
1035
1036 (defun Man-goto-see-also-section ()
1037 "Move point the the \"SEE ALSO\" section.
1038 Actually the section moved to is described by `Man-see-also-regexp'."
1039 (interactive)
1040 (if (not (Man-find-section Man-see-also-regexp))
1041 (error (concat "No " Man-see-also-regexp
1042 " section found in the current manpage"))))
1043
1044 (defun Man-follow-manual-reference (reference)
1045 "Get one of the manpages referred to in the \"SEE ALSO\" section.
1046 Specify which REFERENCE to use; default is based on word at point."
1047 (interactive
1048 (if (not Man-refpages-alist)
1049 (error "There are no references in the current man page")
1050 (list (let* ((default (or
1051 (car (all-completions
1052 (save-excursion
1053 (skip-syntax-backward "w()")
1054 (skip-chars-forward " \t")
1055 (let ((word (current-word)))
1056 ;; strip a trailing '-':
1057 (if (string-match "-$" word)
1058 (substring word 0
1059 (match-beginning 0))
1060 word)))
1061 Man-refpages-alist))
1062 (aheadsym Man-refpages-alist)))
1063 chosen
1064 (prompt (concat "Refer to: (default " default ") ")))
1065 (setq chosen (completing-read prompt Man-refpages-alist nil t))
1066 (if (or (not chosen)
1067 (string= chosen ""))
1068 default
1069 chosen)))))
1070 (if (not Man-refpages-alist)
1071 (error "Can't find any references in the current manpage")
1072 (aput 'Man-refpages-alist reference)
1073 (Man-getpage-in-background
1074 (Man-translate-references (aheadsym Man-refpages-alist)))))
1075
1076 (defun Man-kill ()
1077 "Kill the buffer containing the manpage."
1078 (interactive)
1079 (quit-window t))
1080
1081 (defun Man-quit ()
1082 "Bury the buffer containing the manpage."
1083 (interactive)
1084 (quit-window))
1085
1086 (defun Man-goto-page (page)
1087 "Go to the manual page on page PAGE."
1088 (interactive
1089 (if (not Man-page-list)
1090 (let ((args Man-arguments))
1091 (kill-buffer (current-buffer))
1092 (error "Can't find the %s manpage" args))
1093 (if (= (length Man-page-list) 1)
1094 (error "You're looking at the only manpage in the buffer")
1095 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1096 (length Man-page-list)))))))
1097 (if (not Man-page-list)
1098 (let ((args Man-arguments))
1099 (kill-buffer (current-buffer))
1100 (error "Can't find the %s manpage" args)))
1101 (if (or (< page 1)
1102 (> page (length Man-page-list)))
1103 (error "No manpage %d found" page))
1104 (let* ((page-range (nth (1- page) Man-page-list))
1105 (page-start (car page-range))
1106 (page-end (car (cdr page-range))))
1107 (setq Man-current-page page
1108 Man-page-mode-string (Man-make-page-mode-string))
1109 (widen)
1110 (goto-char page-start)
1111 (narrow-to-region page-start page-end)
1112 (Man-build-section-alist)
1113 (Man-build-references-alist)
1114 (goto-char (point-min))))
1115
1116
1117 (defun Man-next-manpage ()
1118 "Find the next manpage entry in the buffer."
1119 (interactive)
1120 (if (= (length Man-page-list) 1)
1121 (error "This is the only manpage in the buffer"))
1122 (if (< Man-current-page (length Man-page-list))
1123 (Man-goto-page (1+ Man-current-page))
1124 (if Man-circular-pages-flag
1125 (Man-goto-page 1)
1126 (error "You're looking at the last manpage in the buffer"))))
1127
1128 (defun Man-previous-manpage ()
1129 "Find the previous manpage entry in the buffer."
1130 (interactive)
1131 (if (= (length Man-page-list) 1)
1132 (error "This is the only manpage in the buffer"))
1133 (if (> Man-current-page 1)
1134 (Man-goto-page (1- Man-current-page))
1135 (if Man-circular-pages-flag
1136 (Man-goto-page (length Man-page-list))
1137 (error "You're looking at the first manpage in the buffer"))))
1138 \f
1139 ;; Init the man package variables, if not already done.
1140 (Man-init-defvars)
1141
1142 (add-to-list 'debug-ignored-errors "^No manpage [0-9]* found$")
1143 (add-to-list 'debug-ignored-errors "^Can't find the .* manpage$")
1144
1145 (provide 'man)
1146
1147 ;;; man.el ends here