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