Comment change.
[emacs.git] / lisp / man.el
blob8b4d9072cb7092f535e23040f0351dd39d61cccf
1 ;;; man.el --- browse UNIX manual pages
3 ;; Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc.
5 ;; Author: Barry A. Warsaw <bwarsaw@cen.com>
6 ;; Keywords: help
7 ;; Adapted-By: ESR, pot
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
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.
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.
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.
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.
50 ;; Eric Rose <erose@jessica.stanford.edu>, submitted manual.el which
51 ;; provided a very nice manual browsing mode.
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.
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.
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.
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.
94 ;;; Code:
96 (require 'assoc)
98 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
99 ;; empty defvars (keep the compiler quiet)
101 (defvar Man-notify)
102 (defvar Man-current-page)
103 (defvar Man-page-list)
104 (defvar Man-filter-list nil
105 "*Manpage cleaning filter command phrases.
106 This variable contains a list of the following form:
108 '((command-string phrase-string*)*)
110 Each phrase-string is concatenated onto the command-string to form a
111 command filter. The (standard) output (and standard error) of the Un*x
112 man command is piped through each command filter in the order the
113 commands appear in the association list. The final output is placed in
114 the manpage buffer.")
116 (defvar Man-original-frame)
117 (defvar Man-arguments)
118 (defvar Man-sections-alist)
119 (defvar Man-refpages-alist)
120 (defvar Man-uses-untabify-flag t
121 "When non-nil use `untabify' instead of Man-untabify-command.")
122 (defvar Man-page-mode-string)
123 (defvar Man-sed-script nil
124 "Script for sed to nuke backspaces and ANSI codes from manpages.")
126 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
127 ;; user variables
129 (defvar Man-fontify-manpage-flag t
130 "*Make up the manpage with fonts.")
132 (defvar Man-overstrike-face 'bold
133 "*Face to use when fontifying overstrike.")
135 (defvar Man-underline-face 'underline
136 "*Face to use when fontifying underlining.")
138 ;; Use the value of the obsolete user option Man-notify, if set.
139 (defvar Man-notify-method (if (boundp 'Man-notify) Man-notify 'friendly)
140 "*Selects the behavior when manpage is ready.
141 This variable may have one of the following values, where (sf) means
142 that the frames are switched, so the manpage is displayed in the frame
143 where the man command was called from:
145 newframe -- put the manpage in its own frame (see `Man-frame-parameters')
146 pushy -- make the manpage the current buffer in the current window
147 bully -- make the manpage the current buffer and only window (sf)
148 aggressive -- make the manpage the current buffer in the other window (sf)
149 friendly -- display manpage in the other window but don't make current (sf)
150 polite -- don't display manpage, but prints message and beep when ready
151 quiet -- like `polite', but don't beep
152 meek -- make no indication that the manpage is ready
154 Any other value of `Man-notify-method' is equivalent to `meek'.")
156 (defvar Man-frame-parameters nil
157 "*Frame parameter list for creating a new frame for a manual page.")
159 (defvar Man-downcase-section-letters-flag t
160 "*Letters in sections are converted to lower case.
161 Some Un*x man commands can't handle uppercase letters in sections, for
162 example \"man 2V chmod\", but they are often displayed in the manpage
163 with the upper case letter. When this variable is t, the section
164 letter (e.g., \"2V\") is converted to lowercase (e.g., \"2v\") before
165 being sent to the man background process.")
167 (defvar Man-circular-pages-flag t
168 "*If t, the manpage list is treated as circular for traversal.")
170 (defvar Man-section-translations-alist
171 (list
172 '("3C++" . "3")
173 ;; Some systems have a real 3x man section, so let's comment this.
174 ;; '("3X" . "3") ; Xlib man pages
175 '("3X11" . "3")
176 '("1-UCB" . ""))
177 "*Association list of bogus sections to real section numbers.
178 Some manpages (e.g. the Sun C++ 2.1 manpages) have section numbers in
179 their references which Un*x `man' does not recognize. This
180 association list is used to translate those sections, when found, to
181 the associated section number.")
183 (defvar manual-program "man"
184 "The name of the program that produces man pages.")
186 (defvar Man-untabify-command "pr"
187 "Command used for untabifying.")
189 (defvar Man-untabify-command-args (list "-t" "-e")
190 "List of arguments to be passed to Man-untabify-command (which see).")
192 (defvar Man-sed-command "sed"
193 "Command used for processing sed scripts.")
195 (defvar Man-awk-command "awk"
196 "Command used for processing awk scripts.")
198 (defvar Man-mode-line-format
199 '("" mode-line-modified
200 mode-line-buffer-identification " "
201 global-mode-string
202 " " Man-page-mode-string
203 " %[(" mode-name mode-line-process minor-mode-alist ")%]----"
204 (-3 . "%p") "-%-")
205 "Mode line format for manual mode buffer.")
207 (defvar Man-mode-map nil
208 "Keymap for Man mode.")
210 (defvar Man-mode-hook nil
211 "Hook run when Man mode is enabled.")
213 (defvar Man-cooked-hook nil
214 "Hook run after removing backspaces but before Man-mode processing.")
216 (defvar Man-name-regexp "[-a-zA-Z0-9_][-a-zA-Z0-9_.]*"
217 "Regular expression describing the name of a manpage (without section).")
219 (defvar Man-section-regexp "[0-9][a-zA-Z+]*\\|[LNln]"
220 "Regular expression describing a manpage section within parentheses.")
222 (defvar Man-page-header-regexp
223 (concat "^[ \t]*\\(" Man-name-regexp
224 "(\\(" Man-section-regexp "\\))\\).*\\1")
225 "Regular expression describing the heading of a page.")
227 (defvar Man-heading-regexp "^\\([A-Z][A-Z ]+\\)$"
228 "Regular expression describing a manpage heading entry.")
230 (defvar Man-see-also-regexp "SEE ALSO"
231 "Regular expression for SEE ALSO heading (or your equivalent).
232 This regexp should not start with a `^' character.")
234 (defvar Man-first-heading-regexp "^[ \t]*NAME$\\|^[ \t]*No manual entry fo.*$"
235 "Regular expression describing first heading on a manpage.
236 This regular expression should start with a `^' character.")
238 (defvar Man-reference-regexp
239 (concat "\\(" Man-name-regexp "\\)(\\(" Man-section-regexp "\\))")
240 "Regular expression describing a reference in the SEE ALSO section.")
242 (defvar Man-switches ""
243 "Switches passed to the man command, as a single string.")
245 (defvar Man-specified-section-option
246 (if (string-match "-solaris[0-9.]*$" system-configuration)
247 "-s"
249 "Option that indicates a specified a manual section name.")
251 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
252 ;; end user variables
254 ;; other variables and keymap initializations
255 (make-variable-buffer-local 'Man-sections-alist)
256 (make-variable-buffer-local 'Man-refpages-alist)
257 (make-variable-buffer-local 'Man-page-list)
258 (make-variable-buffer-local 'Man-current-page)
259 (make-variable-buffer-local 'Man-page-mode-string)
260 (make-variable-buffer-local 'Man-original-frame)
261 (make-variable-buffer-local 'Man-arguments)
263 (setq-default Man-sections-alist nil)
264 (setq-default Man-refpages-alist nil)
265 (setq-default Man-page-list nil)
266 (setq-default Man-current-page 0)
267 (setq-default Man-page-mode-string "1 of 1")
269 (defconst Man-sysv-sed-script "\
270 /\b/ { s/_\b//g
271 s/\b_//g
272 s/o\b+/o/g
273 s/+\bo/o/g
274 :ovstrk
275 s/\\(.\\)\b\\1/\\1/g
276 t ovstrk
278 /\e\\[[0-9][0-9]*m/ s///g"
279 "Script for sysV-like sed to nuke backspaces and ANSI codes from manpages.")
281 (defconst Man-berkeley-sed-script "\
282 /\b/ { s/_\b//g\\
283 s/\b_//g\\
284 s/o\b+/o/g\\
285 s/+\bo/o/g\\
286 :ovstrk\\
287 s/\\(.\\)\b\\1/\\1/g\\
288 t ovstrk\\
290 /\e\\[[0-9][0-9]*m/ s///g"
291 "Script for berkeley-like sed to nuke backspaces and ANSI codes from manpages.")
293 (defvar man-mode-syntax-table
294 (let ((table (copy-syntax-table (standard-syntax-table))))
295 (modify-syntax-entry ?. "w" table)
296 (modify-syntax-entry ?_ "w" table)
297 table)
298 "Syntax table used in Man mode buffers.")
300 (if Man-mode-map
302 (setq Man-mode-map (make-keymap))
303 (suppress-keymap Man-mode-map)
304 (define-key Man-mode-map " " 'scroll-up)
305 (define-key Man-mode-map "\177" 'scroll-down)
306 (define-key Man-mode-map "n" 'Man-next-section)
307 (define-key Man-mode-map "p" 'Man-previous-section)
308 (define-key Man-mode-map "\en" 'Man-next-manpage)
309 (define-key Man-mode-map "\ep" 'Man-previous-manpage)
310 (define-key Man-mode-map ">" 'end-of-buffer)
311 (define-key Man-mode-map "<" 'beginning-of-buffer)
312 (define-key Man-mode-map "." 'beginning-of-buffer)
313 (define-key Man-mode-map "r" 'Man-follow-manual-reference)
314 (define-key Man-mode-map "g" 'Man-goto-section)
315 (define-key Man-mode-map "s" 'Man-goto-see-also-section)
316 (define-key Man-mode-map "k" 'Man-kill)
317 (define-key Man-mode-map "q" 'Man-quit)
318 (define-key Man-mode-map "m" 'man)
319 (define-key Man-mode-map "\r" 'man-follow)
320 (define-key Man-mode-map "?" 'describe-mode)
324 ;; ======================================================================
325 ;; utilities
327 (defun Man-init-defvars ()
328 "Used for initialising variables based on the value of window-system.
329 This is necessary if one wants to dump man.el with emacs."
331 ;; The following is necessary until fonts are implemented on
332 ;; terminals.
333 (setq Man-fontify-manpage-flag (and Man-fontify-manpage-flag
334 window-system))
336 (setq Man-sed-script
337 (cond
338 (Man-fontify-manpage-flag
339 nil)
340 ((= 0 (call-process Man-sed-command nil nil nil Man-sysv-sed-script))
341 Man-sysv-sed-script)
342 ((= 0 (call-process Man-sed-command nil nil nil Man-berkeley-sed-script))
343 Man-berkeley-sed-script)
345 nil)))
347 (setq Man-filter-list
348 (list
349 (cons
350 Man-sed-command
351 (list
352 (if Man-sed-script
353 (concat "-e '" Man-sed-script "'")
355 "-e '/^[\001-\032][\001-\032]*$/d'"
356 "-e '/\e[789]/s///g'"
357 "-e '/Reformatting page. Wait/d'"
358 "-e '/Reformatting entry. Wait/d'"
359 "-e '/^[ \t]*Hewlett-Packard[ \t]Company[ \t]*-[ \t][0-9]*[ \t]-/d'"
360 "-e '/^[ \t]*Hewlett-Packard[ \t]*-[ \t][0-9]*[ \t]-.*$/d'"
361 "-e '/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d'"
362 "-e '/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d'"
363 "-e '/^Printed[ \t][0-9].*[0-9]$/d'"
364 "-e '/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d'"
365 "-e '/^[A-Za-z].*Last[ \t]change:/d'"
366 "-e '/^Sun[ \t]Release[ \t][0-9].*[0-9]$/d'"
367 "-e '/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d'"
368 "-e '/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d'"
370 (cons
371 Man-awk-command
372 (list
373 "'\n"
374 "BEGIN { blankline=0; anonblank=0; }\n"
375 "/^$/ { if (anonblank==0) next; }\n"
376 "{ anonblank=1; }\n"
377 "/^$/ { blankline++; next; }\n"
378 "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }\n"
381 (if (not Man-uses-untabify-flag)
382 (cons
383 Man-untabify-command
384 Man-untabify-command-args)
388 (defsubst Man-match-substring (&optional n string)
389 "Return the substring matched by the last search.
390 Optional arg N means return the substring matched by the Nth paren
391 grouping. Optional second arg STRING means return a substring from
392 that string instead of from the current buffer."
393 (if (null n) (setq n 0))
394 (if string
395 (substring string (match-beginning n) (match-end n))
396 (buffer-substring (match-beginning n) (match-end n))))
398 (defsubst Man-make-page-mode-string ()
399 "Formats part of the mode line for Man mode."
400 (format "%s page %d of %d"
401 (or (nth 2 (nth (1- Man-current-page) Man-page-list))
403 Man-current-page
404 (length Man-page-list)))
406 (defsubst Man-build-man-command ()
407 "Builds the entire background manpage and cleaning command."
408 (let ((command (concat manual-program " " Man-switches
409 ; Stock MS-DOS shells cannot redirect stderr;
410 ; `call-process' below sends it to /dev/null,
411 ; so we don't need `2>' even with DOS shells
412 ; which do support stderr redirection.
413 (if (not (fboundp 'start-process))
414 " %s"
415 " %s 2>/dev/null")))
416 (flist Man-filter-list))
417 (while (and flist (car flist))
418 (let ((pcom (car (car flist)))
419 (pargs (cdr (car flist))))
420 (setq command
421 (concat command " | " pcom " "
422 (mapconcat '(lambda (phrase)
423 (if (not (stringp phrase))
424 (error "Malformed Man-filter-list"))
425 phrase)
426 pargs " ")))
427 (setq flist (cdr flist))))
428 command))
430 (defun Man-translate-references (ref)
431 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style.
432 Leave it as is if already in that style. Possibly downcase and
433 translate the section (see the Man-downcase-section-letters-flag
434 and the Man-section-translations-alist variables)."
435 (let ((name "")
436 (section "")
437 (slist Man-section-translations-alist))
438 (cond
439 ;; "chmod(2V)" case ?
440 ((string-match (concat "^" Man-reference-regexp "$") ref)
441 (setq name (Man-match-substring 1 ref)
442 section (Man-match-substring 2 ref)))
443 ;; "2v chmod" case ?
444 ((string-match (concat "^\\(" Man-section-regexp
445 "\\) +\\(" Man-name-regexp "\\)$") ref)
446 (setq name (Man-match-substring 2 ref)
447 section (Man-match-substring 1 ref))))
448 (if (string= name "")
449 ref ; Return the reference as is
450 (if Man-downcase-section-letters-flag
451 (setq section (downcase section)))
452 (while slist
453 (let ((s1 (car (car slist)))
454 (s2 (cdr (car slist))))
455 (setq slist (cdr slist))
456 (if Man-downcase-section-letters-flag
457 (setq s1 (downcase s1)))
458 (if (not (string= s1 section)) nil
459 (setq section (if Man-downcase-section-letters-flag
460 (downcase s2)
462 slist nil))))
463 (concat Man-specified-section-option section " " name))))
466 ;; ======================================================================
467 ;; default man entry: get word under point
469 (defsubst Man-default-man-entry ()
470 "Make a guess at a default manual entry.
471 This guess is based on the text surrounding the cursor, and the
472 default section number is selected from `Man-auto-section-alist'."
473 (let (word)
474 (save-excursion
475 ;; Default man entry title is any word the cursor is on, or if
476 ;; cursor not on a word, then nearest preceding word.
477 (setq word (current-word))
478 (if (string-match "[._]+$" word)
479 (setq word (substring word 0 (match-beginning 0))))
480 ;; If looking at something like ioctl(2) or brc(1M), include the
481 ;; section number in the returned value. Remove text properties.
482 (forward-word 1)
483 ;; Use `format' here to clear any text props from `word'.
484 (format "%s%s"
485 word
486 (if (looking-at
487 (concat "[ \t]*([ \t]*\\(" Man-section-regexp "\\)[ \t]*)"))
488 (format "(%s)" (Man-match-substring 1))
489 "")))))
492 ;; ======================================================================
493 ;; Top level command and background process sentinel
495 ;; For compatibility with older versions.
496 ;;;###autoload
497 (defalias 'manual-entry 'man)
499 ;;;###autoload
500 (defun man (man-args)
501 "Get a Un*x manual page and put it in a buffer.
502 This command is the top-level command in the man package. It runs a Un*x
503 command to retrieve and clean a manpage in the background and places the
504 results in a Man mode (manpage browsing) buffer. See variable
505 `Man-notify-method' for what happens when the buffer is ready.
506 If a buffer already exists for this man page, it will display immediately."
507 (interactive
508 (list (let* ((default-entry (Man-default-man-entry))
509 (input (read-string
510 (format "Manual entry%s: "
511 (if (string= default-entry "")
513 (format " (default %s)" default-entry))))))
514 (if (string= input "")
515 (if (string= default-entry "")
516 (error "No man args given")
517 default-entry)
518 input))))
520 ;; Possibly translate the "subject(section)" syntax into the
521 ;; "section subject" syntax and possibly downcase the section.
522 (setq man-args (Man-translate-references man-args))
524 (Man-getpage-in-background man-args))
526 ;;;###autoload
527 (defun man-follow (man-args)
528 "Get a Un*x manual page of the item under point and put it in a buffer."
529 (interactive (list (Man-default-man-entry)))
530 (if (or (not man-args)
531 (string= man-args ""))
532 (error "No item under point")
533 (man man-args)))
535 (defun Man-getpage-in-background (topic)
536 "Uses TOPIC to build and fire off the manpage and cleaning command."
537 (let* ((man-args topic)
538 (bufname (concat "*Man " man-args "*"))
539 (buffer (get-buffer bufname)))
540 (if buffer
541 (Man-notify-when-ready buffer)
542 (require 'env)
543 (message "Invoking %s %s in the background" manual-program man-args)
544 (setq buffer (generate-new-buffer bufname))
545 (save-excursion
546 (set-buffer buffer)
547 (setq Man-original-frame (selected-frame))
548 (setq Man-arguments man-args))
549 (let ((process-environment (copy-sequence process-environment)))
550 ;; Prevent any attempt to use display terminal fanciness.
551 (setenv "TERM" "dumb")
552 (if (fboundp 'start-process)
553 (set-process-sentinel
554 (start-process manual-program buffer "sh" "-c"
555 (format (Man-build-man-command) man-args))
556 'Man-bgproc-sentinel)
557 (progn
558 (let ((exit-status
559 (call-process shell-file-name nil (list buffer nil) nil "-c"
560 (format (Man-build-man-command) man-args)))
561 (msg ""))
562 (or (and (numberp exit-status)
563 (= exit-status 0))
564 (and (numberp exit-status)
565 (setq msg
566 (format "exited abnormally with code %d"
567 exit-status)))
568 (setq msg exit-status))
569 (Man-bgproc-sentinel bufname msg))))))))
571 (defun Man-notify-when-ready (man-buffer)
572 "Notify the user when MAN-BUFFER is ready.
573 See the variable `Man-notify-method' for the different notification behaviors."
574 (let ((saved-frame (save-excursion
575 (set-buffer man-buffer)
576 Man-original-frame)))
577 (cond
578 ((eq Man-notify-method 'newframe)
579 ;; Since we run asynchronously, perhaps while Emacs is waiting
580 ;; for input, we must not leave a different buffer current. We
581 ;; can't rely on the editor command loop to reselect the
582 ;; selected window's buffer.
583 (save-excursion
584 (set-buffer man-buffer)
585 (make-frame Man-frame-parameters)))
586 ((eq Man-notify-method 'pushy)
587 (switch-to-buffer man-buffer))
588 ((eq Man-notify-method 'bully)
589 (and window-system
590 (frame-live-p saved-frame)
591 (select-frame saved-frame))
592 (pop-to-buffer man-buffer)
593 (delete-other-windows))
594 ((eq Man-notify-method 'aggressive)
595 (and window-system
596 (frame-live-p saved-frame)
597 (select-frame saved-frame))
598 (pop-to-buffer man-buffer))
599 ((eq Man-notify-method 'friendly)
600 (and window-system
601 (frame-live-p saved-frame)
602 (select-frame saved-frame))
603 (display-buffer man-buffer 'not-this-window))
604 ((eq Man-notify-method 'polite)
605 (beep)
606 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
607 ((eq Man-notify-method 'quiet)
608 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
609 ((or (eq Man-notify-method 'meek)
611 (message ""))
614 (defun Man-fontify-manpage ()
615 "Convert overstriking and underlining to the correct fonts.
616 Same for the ANSI bold and normal escape sequences."
617 (interactive)
618 (message "Please wait: making up the %s man page..." Man-arguments)
619 (goto-char (point-min))
620 (while (search-forward "\e[1m" nil t)
621 (delete-backward-char 4)
622 (put-text-property (point)
623 (progn (if (search-forward "\e[0m" nil 'move)
624 (delete-backward-char 4))
625 (point))
626 'face Man-overstrike-face))
627 (goto-char (point-min))
628 (while (search-forward "_\b" nil t)
629 (backward-delete-char 2)
630 (put-text-property (point) (1+ (point)) 'face Man-underline-face))
631 (goto-char (point-min))
632 (while (search-forward "\b_" nil t)
633 (backward-delete-char 2)
634 (put-text-property (1- (point)) (point) 'face Man-underline-face))
635 (goto-char (point-min))
636 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
637 (replace-match "\\1")
638 (put-text-property (1- (point)) (point) 'face Man-overstrike-face))
639 (goto-char (point-min))
640 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t)
641 (replace-match "o")
642 (put-text-property (1- (point)) (point) 'face 'bold))
643 (goto-char (point-min))
644 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t)
645 (replace-match "+")
646 (put-text-property (1- (point)) (point) 'face 'bold))
647 ;; \255 is some kind of dash in Latin-1.
648 (goto-char (point-min))
649 (while (search-forward "\255" nil t) (replace-match "-"))
650 (message "%s man page made up" Man-arguments))
652 (defun Man-cleanup-manpage ()
653 "Remove overstriking and underlining from the current buffer."
654 (interactive)
655 (message "Please wait: cleaning up the %s man page..."
656 Man-arguments)
657 (if (or (interactive-p) (not Man-sed-script))
658 (progn
659 (goto-char (point-min))
660 (while (search-forward "_\b" nil t) (backward-delete-char 2))
661 (goto-char (point-min))
662 (while (search-forward "\b_" nil t) (backward-delete-char 2))
663 (goto-char (point-min))
664 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
665 (replace-match "\\1"))
666 (goto-char (point-min))
667 (while (re-search-forward "\e\\[[0-9]+m" nil t) (replace-match ""))
668 (goto-char (point-min))
669 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t) (replace-match "o"))
671 (goto-char (point-min))
672 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t) (replace-match "+"))
673 ;; \255 is some kind of dash in Latin-1.
674 (goto-char (point-min))
675 (while (search-forward "\255" nil t) (replace-match "-"))
676 (message "%s man page cleaned up" Man-arguments))
678 (defun Man-bgproc-sentinel (process msg)
679 "Manpage background process sentinel.
680 When manpage command is run asynchronously, PROCESS is the process
681 object for the manpage command; when manpage command is run
682 synchronously, PROCESS is the name of the buffer where the manpage
683 command is run. Second argument MSG is the exit message of the
684 manpage command."
685 (let ((Man-buffer (if (stringp process) (get-buffer process)
686 (process-buffer process)))
687 (delete-buff nil)
688 (err-mess nil))
690 (if (null (buffer-name Man-buffer)) ;; deleted buffer
691 (or (stringp process)
692 (set-process-buffer process nil))
694 (save-excursion
695 (set-buffer Man-buffer)
696 (let ((case-fold-search nil))
697 (goto-char (point-min))
698 (cond ((or (looking-at "No \\(manual \\)*entry for")
699 (looking-at "[^\n]*: nothing appropriate$"))
700 (setq err-mess (buffer-substring (point)
701 (progn
702 (end-of-line) (point)))
703 delete-buff t))
704 ((or (stringp process)
705 (not (and (eq (process-status process) 'exit)
706 (= (process-exit-status process) 0))))
707 (or (zerop (length msg))
708 (progn
709 (setq err-mess
710 (concat (buffer-name Man-buffer)
711 ": process "
712 (let ((eos (1- (length msg))))
713 (if (= (aref msg eos) ?\n)
714 (substring msg 0 eos) msg))))
715 (goto-char (point-max))
716 (insert (format "\nprocess %s" msg))))
718 (if delete-buff
719 (kill-buffer Man-buffer)
720 (if Man-fontify-manpage-flag
721 (Man-fontify-manpage)
722 (Man-cleanup-manpage))
723 (run-hooks 'Man-cooked-hook)
724 (Man-mode)
725 (set-buffer-modified-p nil)
727 ;; Restore case-fold-search before calling
728 ;; Man-notify-when-ready because it may switch buffers.
730 (if (not delete-buff)
731 (Man-notify-when-ready Man-buffer))
733 (if err-mess
734 (error err-mess))
735 ))))
738 ;; ======================================================================
739 ;; set up manual mode in buffer and build alists
741 (defun Man-mode ()
742 "A mode for browsing Un*x manual pages.
744 The following man commands are available in the buffer. Try
745 \"\\[describe-key] <key> RET\" for more information:
747 \\[man] Prompt to retrieve a new manpage.
748 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
749 \\[Man-next-manpage] Jump to next manpage in circular list.
750 \\[Man-previous-manpage] Jump to previous manpage in circular list.
751 \\[Man-next-section] Jump to next manpage section.
752 \\[Man-previous-section] Jump to previous manpage section.
753 \\[Man-goto-section] Go to a manpage section.
754 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
755 \\[Man-quit] Deletes the manpage window, bury its buffer.
756 \\[Man-kill] Deletes the manpage window, kill its buffer.
757 \\[describe-mode] Prints this help text.
759 The following variables may be of some use. Try
760 \"\\[describe-variable] <variable-name> RET\" for more information:
762 Man-notify-method What happens when manpage formatting is done.
763 Man-downcase-section-letters-flag Force section letters to lower case.
764 Man-circular-pages-flag Treat multiple manpage list as circular.
765 Man-auto-section-alist List of major modes and their section numbers.
766 Man-section-translations-alist List of section numbers and their Un*x equiv.
767 Man-filter-list Background manpage filter command.
768 Man-mode-line-format Mode line format for Man mode buffers.
769 Man-mode-map Keymap bindings for Man mode buffers.
770 Man-mode-hook Normal hook run on entry to Man mode.
771 Man-section-regexp Regexp describing manpage section letters.
772 Man-heading-regexp Regexp describing section headers.
773 Man-see-also-regexp Regexp for SEE ALSO section (or your equiv).
774 Man-first-heading-regexp Regexp for first heading on a manpage.
775 Man-reference-regexp Regexp matching a references in SEE ALSO.
776 Man-switches Background `man' command switches.
778 The following key bindings are currently in effect in the buffer:
779 \\{Man-mode-map}"
780 (interactive)
781 (setq major-mode 'Man-mode
782 mode-name "Man"
783 buffer-auto-save-file-name nil
784 mode-line-format Man-mode-line-format
785 truncate-lines t
786 buffer-read-only t)
787 (buffer-disable-undo (current-buffer))
788 (auto-fill-mode -1)
789 (use-local-map Man-mode-map)
790 (set-syntax-table man-mode-syntax-table)
791 (Man-build-page-list)
792 (Man-strip-page-headers)
793 (Man-unindent)
794 (Man-goto-page 1)
795 (run-hooks 'Man-mode-hook))
797 (defsubst Man-build-section-alist ()
798 "Build the association list of manpage sections."
799 (setq Man-sections-alist nil)
800 (goto-char (point-min))
801 (let ((case-fold-search nil))
802 (while (re-search-forward Man-heading-regexp (point-max) t)
803 (aput 'Man-sections-alist (Man-match-substring 1))
804 (forward-line 1))))
806 (defsubst Man-build-references-alist ()
807 "Build the association list of references (in the SEE ALSO section)."
808 (setq Man-refpages-alist nil)
809 (save-excursion
810 (if (Man-find-section Man-see-also-regexp)
811 (let ((start (progn (forward-line 1) (point)))
812 (end (progn
813 (Man-next-section 1)
814 (point)))
815 hyphenated
816 (runningpoint -1))
817 (save-restriction
818 (narrow-to-region start end)
819 (goto-char (point-min))
820 (back-to-indentation)
821 (while (and (not (eobp)) (/= (point) runningpoint))
822 (setq runningpoint (point))
823 (if (re-search-forward Man-reference-regexp end t)
824 (let* ((word (Man-match-substring 0))
825 (len (1- (length word))))
826 (if hyphenated
827 (setq word (concat hyphenated word)
828 hyphenated nil))
829 (if (= (aref word len) ?-)
830 (setq hyphenated (substring word 0 len))
831 (aput 'Man-refpages-alist word))))
832 (skip-chars-forward " \t\n,")))))))
834 (defun Man-build-page-list ()
835 "Build the list of separate manpages in the buffer."
836 (setq Man-page-list nil)
837 (let ((page-start (point-min))
838 (page-end (point-max))
839 (header ""))
840 (goto-char page-start)
841 ;; (switch-to-buffer (current-buffer))(debug)
842 (while (not (eobp))
843 (setq header
844 (if (looking-at Man-page-header-regexp)
845 (Man-match-substring 1)
846 nil))
847 ;; Go past both the current and the next Man-first-heading-regexp
848 (if (re-search-forward Man-first-heading-regexp nil 'move 2)
849 (let ((p (progn (beginning-of-line) (point))))
850 ;; We assume that the page header is delimited by blank
851 ;; lines and that it contains at most one blank line. So
852 ;; if we back by three blank lines we will be sure to be
853 ;; before the page header but not before the possible
854 ;; previous page header.
855 (search-backward "\n\n" nil t 3)
856 (if (re-search-forward Man-page-header-regexp p 'move)
857 (beginning-of-line))))
858 (setq page-end (point))
859 (setq Man-page-list (append Man-page-list
860 (list (list (copy-marker page-start)
861 (copy-marker page-end)
862 header))))
863 (setq page-start page-end)
866 (defun Man-strip-page-headers ()
867 "Strip all the page headers but the first from the manpage."
868 (let ((buffer-read-only nil)
869 (case-fold-search nil)
870 (page-list Man-page-list)
871 (page ())
872 (header ""))
873 (while page-list
874 (setq page (car page-list))
875 (and (nth 2 page)
876 (goto-char (car page))
877 (re-search-forward Man-first-heading-regexp nil t)
878 (setq header (buffer-substring (car page) (match-beginning 0)))
879 ;; Since the awk script collapses all successive blank
880 ;; lines into one, and since we don't want to get rid of
881 ;; the fast awk script, one must choose between adding
882 ;; spare blank lines between pages when there were none and
883 ;; deleting blank lines at page boundaries when there were
884 ;; some. We choose the first, so we comment the following
885 ;; line.
886 ;; (setq header (concat "\n" header)))
887 (while (search-forward header (nth 1 page) t)
888 (replace-match "")))
889 (setq page-list (cdr page-list)))))
891 (defun Man-unindent ()
892 "Delete the leading spaces that indent the manpage."
893 (let ((buffer-read-only nil)
894 (case-fold-search nil)
895 (page-list Man-page-list))
896 (while page-list
897 (let ((page (car page-list))
898 (indent "")
899 (nindent 0))
900 (narrow-to-region (car page) (car (cdr page)))
901 (if Man-uses-untabify-flag
902 (untabify (point-min) (point-max)))
903 (if (catch 'unindent
904 (goto-char (point-min))
905 (if (not (re-search-forward Man-first-heading-regexp nil t))
906 (throw 'unindent nil))
907 (beginning-of-line)
908 (setq indent (buffer-substring (point)
909 (progn
910 (skip-chars-forward " ")
911 (point))))
912 (setq nindent (length indent))
913 (if (zerop nindent)
914 (throw 'unindent nil))
915 (setq indent (concat indent "\\|$"))
916 (goto-char (point-min))
917 (while (not (eobp))
918 (if (looking-at indent)
919 (forward-line 1)
920 (throw 'unindent nil)))
921 (goto-char (point-min)))
922 (while (not (eobp))
923 (or (eolp)
924 (delete-char nindent))
925 (forward-line 1)))
926 (setq page-list (cdr page-list))
927 ))))
930 ;; ======================================================================
931 ;; Man mode commands
933 (defun Man-next-section (n)
934 "Move point to Nth next section (default 1)."
935 (interactive "p")
936 (let ((case-fold-search nil))
937 (if (looking-at Man-heading-regexp)
938 (forward-line 1))
939 (if (re-search-forward Man-heading-regexp (point-max) t n)
940 (beginning-of-line)
941 (goto-char (point-max)))))
943 (defun Man-previous-section (n)
944 "Move point to Nth previous section (default 1)."
945 (interactive "p")
946 (let ((case-fold-search nil))
947 (if (looking-at Man-heading-regexp)
948 (forward-line -1))
949 (if (re-search-backward Man-heading-regexp (point-min) t n)
950 (beginning-of-line)
951 (goto-char (point-min)))))
953 (defun Man-find-section (section)
954 "Move point to SECTION if it exists, otherwise don't move point.
955 Returns t if section is found, nil otherwise."
956 (let ((curpos (point))
957 (case-fold-search nil))
958 (goto-char (point-min))
959 (if (re-search-forward (concat "^" section) (point-max) t)
960 (progn (beginning-of-line) t)
961 (goto-char curpos)
962 nil)
965 (defun Man-goto-section ()
966 "Query for section to move point to."
967 (interactive)
968 (aput 'Man-sections-alist
969 (let* ((default (aheadsym Man-sections-alist))
970 (completion-ignore-case t)
971 chosen
972 (prompt (concat "Go to section: (default " default ") ")))
973 (setq chosen (completing-read prompt Man-sections-alist))
974 (if (or (not chosen)
975 (string= chosen ""))
976 default
977 chosen)))
978 (Man-find-section (aheadsym Man-sections-alist)))
980 (defun Man-goto-see-also-section ()
981 "Move point the the \"SEE ALSO\" section.
982 Actually the section moved to is described by `Man-see-also-regexp'."
983 (interactive)
984 (if (not (Man-find-section Man-see-also-regexp))
985 (error (concat "No " Man-see-also-regexp
986 " section found in the current manpage"))))
988 (defun Man-follow-manual-reference (reference)
989 "Get one of the manpages referred to in the \"SEE ALSO\" section.
990 Specify which reference to use; default is based on word at point."
991 (interactive
992 (if (not Man-refpages-alist)
993 (error "There are no references in the current man page")
994 (list (let* ((default (or
995 (car (all-completions
996 (save-excursion
997 (skip-syntax-backward "w()")
998 (skip-chars-forward " \t")
999 (let ((word (current-word)))
1000 ;; strip a trailing '-':
1001 (if (string-match "-$" word)
1002 (substring word 0
1003 (match-beginning 0))
1004 word)))
1005 Man-refpages-alist))
1006 (aheadsym Man-refpages-alist)))
1007 chosen
1008 (prompt (concat "Refer to: (default " default ") ")))
1009 (setq chosen (completing-read prompt Man-refpages-alist nil t))
1010 (if (or (not chosen)
1011 (string= chosen ""))
1012 default
1013 chosen)))))
1014 (if (not Man-refpages-alist)
1015 (error "Can't find any references in the current manpage")
1016 (aput 'Man-refpages-alist reference)
1017 (Man-getpage-in-background
1018 (Man-translate-references (aheadsym Man-refpages-alist)))))
1020 (defun Man-kill ()
1021 "Kill the buffer containing the manpage."
1022 (interactive)
1023 (let ((buff (current-buffer)))
1024 (delete-windows-on buff)
1025 (kill-buffer buff))
1026 (if (and window-system
1027 (or (eq Man-notify-method 'newframe)
1028 (and pop-up-frames
1029 (eq Man-notify-method 'bully))))
1030 (delete-frame)))
1032 (defun Man-quit ()
1033 "Bury the buffer containing the manpage."
1034 (interactive)
1035 (let ((buff (current-buffer)))
1036 (delete-windows-on buff)
1037 (bury-buffer buff))
1038 (if (and window-system
1039 (or (eq Man-notify-method 'newframe)
1040 (and pop-up-frames
1041 (eq Man-notify-method 'bully))))
1042 (delete-frame)))
1044 (defun Man-goto-page (page)
1045 "Go to the manual page on page PAGE."
1046 (interactive
1047 (if (not Man-page-list)
1048 (let ((args Man-arguments))
1049 (kill-buffer (current-buffer))
1050 (error "Can't find the %s manpage" args))
1051 (if (= (length Man-page-list) 1)
1052 (error "You're looking at the only manpage in the buffer")
1053 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1054 (length Man-page-list)))))))
1055 (if (not Man-page-list)
1056 (let ((args Man-arguments))
1057 (kill-buffer (current-buffer))
1058 (error "Can't find the %s manpage" args)))
1059 (if (or (< page 1)
1060 (> page (length Man-page-list)))
1061 (error "No manpage %d found" page))
1062 (let* ((page-range (nth (1- page) Man-page-list))
1063 (page-start (car page-range))
1064 (page-end (car (cdr page-range))))
1065 (setq Man-current-page page
1066 Man-page-mode-string (Man-make-page-mode-string))
1067 (widen)
1068 (goto-char page-start)
1069 (narrow-to-region page-start page-end)
1070 (Man-build-section-alist)
1071 (Man-build-references-alist)
1072 (goto-char (point-min))))
1075 (defun Man-next-manpage ()
1076 "Find the next manpage entry in the buffer."
1077 (interactive)
1078 (if (= (length Man-page-list) 1)
1079 (error "This is the only manpage in the buffer"))
1080 (if (< Man-current-page (length Man-page-list))
1081 (Man-goto-page (1+ Man-current-page))
1082 (if Man-circular-pages-flag
1083 (Man-goto-page 1)
1084 (error "You're looking at the last manpage in the buffer"))))
1086 (defun Man-previous-manpage ()
1087 "Find the previous manpage entry in the buffer."
1088 (interactive)
1089 (if (= (length Man-page-list) 1)
1090 (error "This is the only manpage in the buffer"))
1091 (if (> Man-current-page 1)
1092 (Man-goto-page (1- Man-current-page))
1093 (if Man-circular-pages-flag
1094 (Man-goto-page (length Man-page-list))
1095 (error "You're looking at the first manpage in the buffer"))))
1097 ;; Init the man package variables, if not already done.
1098 (Man-init-defvars)
1100 (provide 'man)
1102 ;;; man.el ends here