Remove CVS merge cookie left in.
[emacs.git] / lisp / man.el
blobdd53df7bd6311522eb0975451f951bf55d073a5f
1 ;;; man.el --- browse UNIX manual pages
3 ;; Copyright (C) 1993, 1994, 1996, 1997 Free Software Foundation, Inc.
5 ;; Author: Barry A. Warsaw <bwarsaw@cen.com>
6 ;; Maintainer: FSF
7 ;; Keywords: help
8 ;; Adapted-By: ESR, pot
10 ;; This file is part of GNU Emacs.
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.
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.
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.
27 ;;; Commentary:
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.
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.
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.
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.
51 ;; Eric Rose <erose@jessica.stanford.edu>, submitted manual.el which
52 ;; provided a very nice manual browsing mode.
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.
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.
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.
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.
95 ;;; Code:
97 (require 'assoc)
99 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
100 ;; empty defvars (keep the compiler quiet)
102 (defgroup man nil
103 "Browse UNIX manual pages."
104 :prefix "Man-"
105 :group 'help)
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:
115 '((command-string phrase-string*)*)
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)
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.")
137 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
138 ;; user variables
140 (defcustom Man-fontify-manpage-flag t
141 "*Non-nil means make up the manpage with fonts."
142 :type 'boolean
143 :group 'man)
145 (defcustom Man-overstrike-face 'bold
146 "*Face to use when fontifying overstrike."
147 :type 'face
148 :group 'man)
150 (defcustom Man-underline-face 'underline
151 "*Face to use when fontifying underlining."
152 :type 'face
153 :group 'man)
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:
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
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)
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)
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)
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)
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)
213 (defvar manual-program "man"
214 "The name of the program that produces man pages.")
216 (defvar Man-untabify-command "pr"
217 "Command used for untabifying.")
219 (defvar Man-untabify-command-args (list "-t" "-e")
220 "List of arguments to be passed to `Man-untabify-command' (which see).")
222 (defvar Man-sed-command "sed"
223 "Command used for processing sed scripts.")
225 (defvar Man-awk-command "awk"
226 "Command used for processing awk scripts.")
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.")
242 (defvar Man-mode-map nil
243 "Keymap for Man mode.")
245 (defvar Man-mode-hook nil
246 "Hook run when Man mode is enabled.")
248 (defvar Man-cooked-hook nil
249 "Hook run after removing backspaces but before `Man-mode' processing.")
251 (defvar Man-name-regexp "[-a-zA-Z0-9_][-a-zA-Z0-9_.]*"
252 "Regular expression describing the name of a manpage (without section).")
254 (defvar Man-section-regexp "[0-9][a-zA-Z+]*\\|[LNln]"
255 "Regular expression describing a manpage section within parentheses.")
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.")
266 (defvar Man-heading-regexp "^\\([A-Z][A-Z ]+\\)$"
267 "Regular expression describing a manpage heading entry.")
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.")
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.")
277 (defvar Man-reference-regexp
278 (concat "\\(" Man-name-regexp "\\)(\\(" Man-section-regexp "\\))")
279 "Regular expression describing a reference in the SEE ALSO section.")
281 (defvar Man-switches ""
282 "Switches passed to the man command, as a single string.")
284 (defvar Man-specified-section-option
285 (if (string-match "-solaris[0-9.]*$" system-configuration)
286 "-s"
288 "Option that indicates a specified a manual section name.")
290 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
291 ;; end user variables
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)
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")
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
317 /\e\\[[0-9][0-9]*m/ s///g"
318 "Script for sysV-like sed to nuke backspaces and ANSI codes from manpages.")
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\\
329 /\e\\[[0-9][0-9]*m/ s///g"
330 "Script for berkeley-like sed to nuke backspaces and ANSI codes from manpages.")
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.")
339 (if Man-mode-map
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)
363 ;; ======================================================================
364 ;; utilities
366 (defun Man-init-defvars ()
367 "Used for initialising variables based on display's color support.
368 This is necessary if one wants to dump man.el with Emacs."
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)
381 nil))))
383 (setq Man-filter-list
384 ;; Avoid trailing nil which confuses customize.
385 (apply 'list
386 (cons
387 Man-sed-command
388 (list
389 (if Man-sed-script
390 (concat "-e '" Man-sed-script "'")
392 "-e '/^[\001-\032][\001-\032]*$/d'"
393 "-e '/\e[789]/s///g'"
394 "-e '/Reformatting page. Wait/d'"
395 "-e '/Reformatting entry. Wait/d'"
396 "-e '/^[ \t]*Hewlett-Packard[ \t]Company[ \t]*-[ \t][0-9]*[ \t]-/d'"
397 "-e '/^[ \t]*Hewlett-Packard[ \t]*-[ \t][0-9]*[ \t]-.*$/d'"
398 "-e '/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d'"
399 "-e '/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d'"
400 "-e '/^Printed[ \t][0-9].*[0-9]$/d'"
401 "-e '/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d'"
402 "-e '/^[A-Za-z].*Last[ \t]change:/d'"
403 "-e '/^Sun[ \t]Release[ \t][0-9].*[0-9]$/d'"
404 "-e '/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d'"
405 "-e '/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d'"
407 (cons
408 Man-awk-command
409 (list
410 "'\n"
411 "BEGIN { blankline=0; anonblank=0; }\n"
412 "/^$/ { if (anonblank==0) next; }\n"
413 "{ anonblank=1; }\n"
414 "/^$/ { blankline++; next; }\n"
415 "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }\n"
418 (if (not Man-uses-untabify-flag)
419 ;; The outer list will be stripped off by apply.
420 (list (cons
421 Man-untabify-command
422 Man-untabify-command-args))
426 (defsubst Man-match-substring (&optional n string)
427 "Return the substring matched by the last search.
428 Optional arg N means return the substring matched by the Nth paren
429 grouping. Optional second arg STRING means return a substring from
430 that string instead of from the current buffer."
431 (if (null n) (setq n 0))
432 (if string
433 (substring string (match-beginning n) (match-end n))
434 (buffer-substring (match-beginning n) (match-end n))))
436 (defsubst Man-make-page-mode-string ()
437 "Formats part of the mode line for Man mode."
438 (format "%s page %d of %d"
439 (or (nth 2 (nth (1- Man-current-page) Man-page-list))
441 Man-current-page
442 (length Man-page-list)))
444 (defsubst Man-build-man-command ()
445 "Builds the entire background manpage and cleaning command."
446 (let ((command (concat manual-program " " Man-switches
447 ; Stock MS-DOS shells cannot redirect stderr;
448 ; `call-process' below sends it to /dev/null,
449 ; so we don't need `2>' even with DOS shells
450 ; which do support stderr redirection.
451 (if (not (fboundp 'start-process))
452 " %s"
453 (concat " %s 2>" null-device))))
454 (flist Man-filter-list))
455 (while (and flist (car flist))
456 (let ((pcom (car (car flist)))
457 (pargs (cdr (car flist))))
458 (setq command
459 (concat command " | " pcom " "
460 (mapconcat (lambda (phrase)
461 (if (not (stringp phrase))
462 (error "Malformed Man-filter-list"))
463 phrase)
464 pargs " ")))
465 (setq flist (cdr flist))))
466 command))
468 (defun Man-translate-references (ref)
469 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style.
470 Leave it as is if already in that style. Possibly downcase and
471 translate the section (see the Man-downcase-section-letters-flag
472 and the Man-section-translations-alist variables)."
473 (let ((name "")
474 (section "")
475 (slist Man-section-translations-alist))
476 (cond
477 ;; "chmod(2V)" case ?
478 ((string-match (concat "^" Man-reference-regexp "$") ref)
479 (setq name (Man-match-substring 1 ref)
480 section (Man-match-substring 2 ref)))
481 ;; "2v chmod" case ?
482 ((string-match (concat "^\\(" Man-section-regexp
483 "\\) +\\(" Man-name-regexp "\\)$") ref)
484 (setq name (Man-match-substring 2 ref)
485 section (Man-match-substring 1 ref))))
486 (if (string= name "")
487 ref ; Return the reference as is
488 (if Man-downcase-section-letters-flag
489 (setq section (downcase section)))
490 (while slist
491 (let ((s1 (car (car slist)))
492 (s2 (cdr (car slist))))
493 (setq slist (cdr slist))
494 (if Man-downcase-section-letters-flag
495 (setq s1 (downcase s1)))
496 (if (not (string= s1 section)) nil
497 (setq section (if Man-downcase-section-letters-flag
498 (downcase s2)
500 slist nil))))
501 (concat Man-specified-section-option section " " name))))
504 ;; ======================================================================
505 ;; default man entry: get word under point
507 (defsubst Man-default-man-entry ()
508 "Make a guess at a default manual entry.
509 This guess is based on the text surrounding the cursor, and the
510 default section number is selected from `Man-auto-section-alist'."
511 (let (word)
512 (save-excursion
513 ;; Default man entry title is any word the cursor is on, or if
514 ;; cursor not on a word, then nearest preceding word.
515 (setq word (current-word))
516 (if (string-match "[._]+$" word)
517 (setq word (substring word 0 (match-beginning 0))))
518 ;; If looking at something like ioctl(2) or brc(1M), include the
519 ;; section number in the returned value. Remove text properties.
520 (forward-word 1)
521 ;; Use `format' here to clear any text props from `word'.
522 (format "%s%s"
523 word
524 (if (looking-at
525 (concat "[ \t]*([ \t]*\\(" Man-section-regexp "\\)[ \t]*)"))
526 (format "(%s)" (Man-match-substring 1))
527 "")))))
530 ;; ======================================================================
531 ;; Top level command and background process sentinel
533 ;; For compatibility with older versions.
534 ;;;###autoload
535 (defalias 'manual-entry 'man)
537 ;;;###autoload
538 (defun man (man-args)
539 "Get a Un*x manual page and put it in a buffer.
540 This command is the top-level command in the man package. It runs a Un*x
541 command to retrieve and clean a manpage in the background and places the
542 results in a Man mode (manpage browsing) buffer. See variable
543 `Man-notify-method' for what happens when the buffer is ready.
544 If a buffer already exists for this man page, it will display immediately.
546 To specify a man page from a certain section, type SUBJECT(SECTION) or
547 SECTION SUBJECT when prompted for a manual entry."
548 (interactive
549 (list (let* ((default-entry (Man-default-man-entry))
550 (input (read-string
551 (format "Manual entry%s: "
552 (if (string= default-entry "")
554 (format " (default %s)" default-entry))))))
555 (if (string= input "")
556 (if (string= default-entry "")
557 (error "No man args given")
558 default-entry)
559 input))))
561 ;; Possibly translate the "subject(section)" syntax into the
562 ;; "section subject" syntax and possibly downcase the section.
563 (setq man-args (Man-translate-references man-args))
565 (Man-getpage-in-background man-args))
567 ;;;###autoload
568 (defun man-follow (man-args)
569 "Get a Un*x manual page of the item under point and put it in a buffer."
570 (interactive (list (Man-default-man-entry)))
571 (if (or (not man-args)
572 (string= man-args ""))
573 (error "No item under point")
574 (man man-args)))
576 (defun Man-getpage-in-background (topic)
577 "Use TOPIC to build and fire off the manpage and cleaning command."
578 (let* ((man-args topic)
579 (bufname (concat "*Man " man-args "*"))
580 (buffer (get-buffer bufname)))
581 (if buffer
582 (Man-notify-when-ready buffer)
583 (require 'env)
584 (message "Invoking %s %s in the background" manual-program man-args)
585 (setq buffer (generate-new-buffer bufname))
586 (save-excursion
587 (set-buffer buffer)
588 (setq Man-original-frame (selected-frame))
589 (setq Man-arguments man-args))
590 (let ((process-environment (copy-sequence process-environment))
591 ;; The following is so Awk script gets \n intact
592 ;; But don't prevent decoding of the outside.
593 (coding-system-for-write 'raw-text-unix)
594 ;; We must decode the output by a coding system that the
595 ;; systen locale suggests.
596 (coding-system-for-read locale-coding-system)
597 ;; Avoid possible error by using a directory that always exists.
598 (default-directory "/"))
599 ;; Prevent any attempt to use display terminal fanciness.
600 (setenv "TERM" "dumb")
601 (if (fboundp 'start-process)
602 (set-process-sentinel
603 (start-process manual-program buffer "sh" "-c"
604 (format (Man-build-man-command) man-args))
605 'Man-bgproc-sentinel)
606 (progn
607 (let ((exit-status
608 (call-process shell-file-name nil (list buffer nil) nil "-c"
609 (format (Man-build-man-command) man-args)))
610 (msg ""))
611 (or (and (numberp exit-status)
612 (= exit-status 0))
613 (and (numberp exit-status)
614 (setq msg
615 (format "exited abnormally with code %d"
616 exit-status)))
617 (setq msg exit-status))
618 (Man-bgproc-sentinel bufname msg))))))))
620 (defun Man-notify-when-ready (man-buffer)
621 "Notify the user when MAN-BUFFER is ready.
622 See the variable `Man-notify-method' for the different notification behaviors."
623 (let ((saved-frame (save-excursion
624 (set-buffer man-buffer)
625 Man-original-frame)))
626 (cond
627 ((eq Man-notify-method 'newframe)
628 ;; Since we run asynchronously, perhaps while Emacs is waiting
629 ;; for input, we must not leave a different buffer current. We
630 ;; can't rely on the editor command loop to reselect the
631 ;; selected window's buffer.
632 (save-excursion
633 (let ((frame (make-frame Man-frame-parameters)))
634 (set-window-buffer (frame-selected-window frame) man-buffer)
635 (set-window-dedicated-p (frame-selected-window frame) t)
636 (or (display-multi-frame-p frame)
637 (select-frame frame)))))
638 ((eq Man-notify-method 'pushy)
639 (switch-to-buffer man-buffer))
640 ((eq Man-notify-method 'bully)
641 (and (frame-live-p saved-frame)
642 (select-frame saved-frame))
643 (pop-to-buffer man-buffer)
644 (delete-other-windows))
645 ((eq Man-notify-method 'aggressive)
646 (and (frame-live-p saved-frame)
647 (select-frame saved-frame))
648 (pop-to-buffer man-buffer))
649 ((eq Man-notify-method 'friendly)
650 (and (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)
660 (message ""))
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 "-")))))
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 (if (< (buffer-size) (position-bytes (point-max)))
688 ;; Multibyte characters exist.
689 (progn
690 (goto-char (point-min))
691 (while (search-forward "__\b\b" nil t)
692 (backward-delete-char 4)
693 (put-text-property (point) (1+ (point)) 'face Man-underline-face))
694 (goto-char (point-min))
695 (while (search-forward "\b\b__" nil t)
696 (backward-delete-char 4)
697 (put-text-property (1- (point)) (point) 'face Man-underline-face))))
698 (goto-char (point-min))
699 (while (search-forward "_\b" nil t)
700 (backward-delete-char 2)
701 (put-text-property (point) (1+ (point)) 'face Man-underline-face))
702 (goto-char (point-min))
703 (while (search-forward "\b_" nil t)
704 (backward-delete-char 2)
705 (put-text-property (1- (point)) (point) 'face Man-underline-face))
706 (goto-char (point-min))
707 (while (re-search-forward "\\(.\\)\\(\b+\\1\\)+" nil t)
708 (replace-match "\\1")
709 (put-text-property (1- (point)) (point) 'face Man-overstrike-face))
710 (goto-char (point-min))
711 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t)
712 (replace-match "o")
713 (put-text-property (1- (point)) (point) 'face 'bold))
714 (goto-char (point-min))
715 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t)
716 (replace-match "+")
717 (put-text-property (1- (point)) (point) 'face 'bold))
718 (Man-softhyphen-to-minus)
719 (message "%s man page made up" Man-arguments))
721 (defun Man-cleanup-manpage ()
722 "Remove overstriking and underlining from the current buffer."
723 (interactive)
724 (message "Please wait: cleaning up the %s man page..."
725 Man-arguments)
726 (if (or (interactive-p) (not Man-sed-script))
727 (progn
728 (goto-char (point-min))
729 (while (search-forward "_\b" nil t) (backward-delete-char 2))
730 (goto-char (point-min))
731 (while (search-forward "\b_" nil t) (backward-delete-char 2))
732 (goto-char (point-min))
733 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
734 (replace-match "\\1"))
735 (goto-char (point-min))
736 (while (re-search-forward "\e\\[[0-9]+m" nil t) (replace-match ""))
737 (goto-char (point-min))
738 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t) (replace-match "o"))
740 (goto-char (point-min))
741 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t) (replace-match "+"))
742 (Man-softhyphen-to-minus)
743 (message "%s man page cleaned up" Man-arguments))
745 (defun Man-bgproc-sentinel (process msg)
746 "Manpage background process sentinel.
747 When manpage command is run asynchronously, PROCESS is the process
748 object for the manpage command; when manpage command is run
749 synchronously, PROCESS is the name of the buffer where the manpage
750 command is run. Second argument MSG is the exit message of the
751 manpage command."
752 (let ((Man-buffer (if (stringp process) (get-buffer process)
753 (process-buffer process)))
754 (delete-buff nil)
755 (err-mess nil))
757 (if (null (buffer-name Man-buffer)) ;; deleted buffer
758 (or (stringp process)
759 (set-process-buffer process nil))
761 (save-excursion
762 (set-buffer Man-buffer)
763 (let ((case-fold-search nil))
764 (goto-char (point-min))
765 (cond ((or (looking-at "No \\(manual \\)*entry for")
766 (looking-at "[^\n]*: nothing appropriate$"))
767 (setq err-mess (buffer-substring (point)
768 (progn
769 (end-of-line) (point)))
770 delete-buff t))
771 ((or (stringp process)
772 (not (and (eq (process-status process) 'exit)
773 (= (process-exit-status process) 0))))
774 (or (zerop (length msg))
775 (progn
776 (setq err-mess
777 (concat (buffer-name Man-buffer)
778 ": process "
779 (let ((eos (1- (length msg))))
780 (if (= (aref msg eos) ?\n)
781 (substring msg 0 eos) msg))))
782 (goto-char (point-max))
783 (insert (format "\nprocess %s" msg))))
785 (if delete-buff
786 (kill-buffer Man-buffer)
787 (if Man-fontify-manpage-flag
788 (Man-fontify-manpage)
789 (Man-cleanup-manpage))
790 (run-hooks 'Man-cooked-hook)
791 (Man-mode)
792 (set-buffer-modified-p nil)
794 ;; Restore case-fold-search before calling
795 ;; Man-notify-when-ready because it may switch buffers.
797 (if (not delete-buff)
798 (Man-notify-when-ready Man-buffer))
800 (if err-mess
801 (error err-mess))
802 ))))
805 ;; ======================================================================
806 ;; set up manual mode in buffer and build alists
808 (defun Man-mode ()
809 "A mode for browsing Un*x manual pages.
811 The following man commands are available in the buffer. Try
812 \"\\[describe-key] <key> RET\" for more information:
814 \\[man] Prompt to retrieve a new manpage.
815 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
816 \\[Man-next-manpage] Jump to next manpage in circular list.
817 \\[Man-previous-manpage] Jump to previous manpage in circular list.
818 \\[Man-next-section] Jump to next manpage section.
819 \\[Man-previous-section] Jump to previous manpage section.
820 \\[Man-goto-section] Go to a manpage section.
821 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
822 \\[Man-quit] Deletes the manpage window, bury its buffer.
823 \\[Man-kill] Deletes the manpage window, kill its buffer.
824 \\[describe-mode] Prints this help text.
826 The following variables may be of some use. Try
827 \"\\[describe-variable] <variable-name> RET\" for more information:
829 `Man-notify-method' What happens when manpage formatting is done.
830 `Man-downcase-section-letters-flag' Force section letters to lower case.
831 `Man-circular-pages-flag' Treat multiple manpage list as circular.
832 `Man-auto-section-alist' List of major modes and their section numbers.
833 `Man-section-translations-alist' List of section numbers and their Un*x equiv.
834 `Man-filter-list' Background manpage filter command.
835 `Man-mode-line-format' Mode line format for Man mode buffers.
836 `Man-mode-map' Keymap bindings for Man mode buffers.
837 `Man-mode-hook' Normal hook run on entry to Man mode.
838 `Man-section-regexp' Regexp describing manpage section letters.
839 `Man-heading-regexp' Regexp describing section headers.
840 `Man-see-also-regexp' Regexp for SEE ALSO section (or your equiv).
841 `Man-first-heading-regexp' Regexp for first heading on a manpage.
842 `Man-reference-regexp' Regexp matching a references in SEE ALSO.
843 `Man-switches' Background `man' command switches.
845 The following key bindings are currently in effect in the buffer:
846 \\{Man-mode-map}"
847 (interactive)
848 (setq major-mode 'Man-mode
849 mode-name "Man"
850 buffer-auto-save-file-name nil
851 mode-line-format Man-mode-line-format
852 truncate-lines t
853 buffer-read-only t)
854 (buffer-disable-undo (current-buffer))
855 (auto-fill-mode -1)
856 (use-local-map Man-mode-map)
857 (set-syntax-table man-mode-syntax-table)
858 (Man-build-page-list)
859 (Man-strip-page-headers)
860 (Man-unindent)
861 (Man-goto-page 1)
862 (run-hooks 'Man-mode-hook))
864 (defsubst Man-build-section-alist ()
865 "Build the association list of manpage sections."
866 (setq Man-sections-alist nil)
867 (goto-char (point-min))
868 (let ((case-fold-search nil))
869 (while (re-search-forward Man-heading-regexp (point-max) t)
870 (aput 'Man-sections-alist (Man-match-substring 1))
871 (forward-line 1))))
873 (defsubst Man-build-references-alist ()
874 "Build the association list of references (in the SEE ALSO section)."
875 (setq Man-refpages-alist nil)
876 (save-excursion
877 (if (Man-find-section Man-see-also-regexp)
878 (let ((start (progn (forward-line 1) (point)))
879 (end (progn
880 (Man-next-section 1)
881 (point)))
882 hyphenated
883 (runningpoint -1))
884 (save-restriction
885 (narrow-to-region start end)
886 (goto-char (point-min))
887 (back-to-indentation)
888 (while (and (not (eobp)) (/= (point) runningpoint))
889 (setq runningpoint (point))
890 (if (re-search-forward Man-reference-regexp end t)
891 (let* ((word (Man-match-substring 0))
892 (len (1- (length word))))
893 (if hyphenated
894 (setq word (concat hyphenated word)
895 hyphenated nil))
896 (if (= (aref word len) ?-)
897 (setq hyphenated (substring word 0 len))
898 (aput 'Man-refpages-alist word))))
899 (skip-chars-forward " \t\n,")))))))
901 (defun Man-build-page-list ()
902 "Build the list of separate manpages in the buffer."
903 (setq Man-page-list nil)
904 (let ((page-start (point-min))
905 (page-end (point-max))
906 (header ""))
907 (goto-char page-start)
908 ;; (switch-to-buffer (current-buffer))(debug)
909 (while (not (eobp))
910 (setq header
911 (if (looking-at Man-page-header-regexp)
912 (Man-match-substring 1)
913 nil))
914 ;; Go past both the current and the next Man-first-heading-regexp
915 (if (re-search-forward Man-first-heading-regexp nil 'move 2)
916 (let ((p (progn (beginning-of-line) (point))))
917 ;; We assume that the page header is delimited by blank
918 ;; lines and that it contains at most one blank line. So
919 ;; if we back by three blank lines we will be sure to be
920 ;; before the page header but not before the possible
921 ;; previous page header.
922 (search-backward "\n\n" nil t 3)
923 (if (re-search-forward Man-page-header-regexp p 'move)
924 (beginning-of-line))))
925 (setq page-end (point))
926 (setq Man-page-list (append Man-page-list
927 (list (list (copy-marker page-start)
928 (copy-marker page-end)
929 header))))
930 (setq page-start page-end)
933 (defun Man-strip-page-headers ()
934 "Strip all the page headers but the first from the manpage."
935 (let ((buffer-read-only nil)
936 (case-fold-search nil)
937 (page-list Man-page-list)
938 (page ())
939 (header ""))
940 (while page-list
941 (setq page (car page-list))
942 (and (nth 2 page)
943 (goto-char (car page))
944 (re-search-forward Man-first-heading-regexp nil t)
945 (setq header (buffer-substring (car page) (match-beginning 0)))
946 ;; Since the awk script collapses all successive blank
947 ;; lines into one, and since we don't want to get rid of
948 ;; the fast awk script, one must choose between adding
949 ;; spare blank lines between pages when there were none and
950 ;; deleting blank lines at page boundaries when there were
951 ;; some. We choose the first, so we comment the following
952 ;; line.
953 ;; (setq header (concat "\n" header)))
954 (while (search-forward header (nth 1 page) t)
955 (replace-match "")))
956 (setq page-list (cdr page-list)))))
958 (defun Man-unindent ()
959 "Delete the leading spaces that indent the manpage."
960 (let ((buffer-read-only nil)
961 (case-fold-search nil)
962 (page-list Man-page-list))
963 (while page-list
964 (let ((page (car page-list))
965 (indent "")
966 (nindent 0))
967 (narrow-to-region (car page) (car (cdr page)))
968 (if Man-uses-untabify-flag
969 (untabify (point-min) (point-max)))
970 (if (catch 'unindent
971 (goto-char (point-min))
972 (if (not (re-search-forward Man-first-heading-regexp nil t))
973 (throw 'unindent nil))
974 (beginning-of-line)
975 (setq indent (buffer-substring (point)
976 (progn
977 (skip-chars-forward " ")
978 (point))))
979 (setq nindent (length indent))
980 (if (zerop nindent)
981 (throw 'unindent nil))
982 (setq indent (concat indent "\\|$"))
983 (goto-char (point-min))
984 (while (not (eobp))
985 (if (looking-at indent)
986 (forward-line 1)
987 (throw 'unindent nil)))
988 (goto-char (point-min)))
989 (while (not (eobp))
990 (or (eolp)
991 (delete-char nindent))
992 (forward-line 1)))
993 (setq page-list (cdr page-list))
994 ))))
997 ;; ======================================================================
998 ;; Man mode commands
1000 (defun Man-next-section (n)
1001 "Move point to Nth next section (default 1)."
1002 (interactive "p")
1003 (let ((case-fold-search nil))
1004 (if (looking-at Man-heading-regexp)
1005 (forward-line 1))
1006 (if (re-search-forward Man-heading-regexp (point-max) t n)
1007 (beginning-of-line)
1008 (goto-char (point-max)))))
1010 (defun Man-previous-section (n)
1011 "Move point to Nth previous section (default 1)."
1012 (interactive "p")
1013 (let ((case-fold-search nil))
1014 (if (looking-at Man-heading-regexp)
1015 (forward-line -1))
1016 (if (re-search-backward Man-heading-regexp (point-min) t n)
1017 (beginning-of-line)
1018 (goto-char (point-min)))))
1020 (defun Man-find-section (section)
1021 "Move point to SECTION if it exists, otherwise don't move point.
1022 Returns t if section is found, nil otherwise."
1023 (let ((curpos (point))
1024 (case-fold-search nil))
1025 (goto-char (point-min))
1026 (if (re-search-forward (concat "^" section) (point-max) t)
1027 (progn (beginning-of-line) t)
1028 (goto-char curpos)
1029 nil)
1032 (defun Man-goto-section ()
1033 "Query for section to move point to."
1034 (interactive)
1035 (aput 'Man-sections-alist
1036 (let* ((default (aheadsym Man-sections-alist))
1037 (completion-ignore-case t)
1038 chosen
1039 (prompt (concat "Go to section: (default " default ") ")))
1040 (setq chosen (completing-read prompt Man-sections-alist))
1041 (if (or (not chosen)
1042 (string= chosen ""))
1043 default
1044 chosen)))
1045 (Man-find-section (aheadsym Man-sections-alist)))
1047 (defun Man-goto-see-also-section ()
1048 "Move point the the \"SEE ALSO\" section.
1049 Actually the section moved to is described by `Man-see-also-regexp'."
1050 (interactive)
1051 (if (not (Man-find-section Man-see-also-regexp))
1052 (error (concat "No " Man-see-also-regexp
1053 " section found in the current manpage"))))
1055 (defun Man-follow-manual-reference (reference)
1056 "Get one of the manpages referred to in the \"SEE ALSO\" section.
1057 Specify which REFERENCE to use; default is based on word at point."
1058 (interactive
1059 (if (not Man-refpages-alist)
1060 (error "There are no references in the current man page")
1061 (list (let* ((default (or
1062 (car (all-completions
1063 (save-excursion
1064 (skip-syntax-backward "w()")
1065 (skip-chars-forward " \t")
1066 (let ((word (current-word)))
1067 ;; strip a trailing '-':
1068 (if (string-match "-$" word)
1069 (substring word 0
1070 (match-beginning 0))
1071 word)))
1072 Man-refpages-alist))
1073 (aheadsym Man-refpages-alist)))
1074 chosen
1075 (prompt (concat "Refer to: (default " default ") ")))
1076 (setq chosen (completing-read prompt Man-refpages-alist nil t))
1077 (if (or (not chosen)
1078 (string= chosen ""))
1079 default
1080 chosen)))))
1081 (if (not Man-refpages-alist)
1082 (error "Can't find any references in the current manpage")
1083 (aput 'Man-refpages-alist reference)
1084 (Man-getpage-in-background
1085 (Man-translate-references (aheadsym Man-refpages-alist)))))
1087 (defun Man-kill ()
1088 "Kill the buffer containing the manpage."
1089 (interactive)
1090 (quit-window t))
1092 (defun Man-quit ()
1093 "Bury the buffer containing the manpage."
1094 (interactive)
1095 (quit-window))
1097 (defun Man-goto-page (page)
1098 "Go to the manual page on page PAGE."
1099 (interactive
1100 (if (not Man-page-list)
1101 (let ((args Man-arguments))
1102 (kill-buffer (current-buffer))
1103 (error "Can't find the %s manpage" args))
1104 (if (= (length Man-page-list) 1)
1105 (error "You're looking at the only manpage in the buffer")
1106 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1107 (length Man-page-list)))))))
1108 (if (not Man-page-list)
1109 (let ((args Man-arguments))
1110 (kill-buffer (current-buffer))
1111 (error "Can't find the %s manpage" args)))
1112 (if (or (< page 1)
1113 (> page (length Man-page-list)))
1114 (error "No manpage %d found" page))
1115 (let* ((page-range (nth (1- page) Man-page-list))
1116 (page-start (car page-range))
1117 (page-end (car (cdr page-range))))
1118 (setq Man-current-page page
1119 Man-page-mode-string (Man-make-page-mode-string))
1120 (widen)
1121 (goto-char page-start)
1122 (narrow-to-region page-start page-end)
1123 (Man-build-section-alist)
1124 (Man-build-references-alist)
1125 (goto-char (point-min))))
1128 (defun Man-next-manpage ()
1129 "Find the next 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 (length Man-page-list))
1134 (Man-goto-page (1+ Man-current-page))
1135 (if Man-circular-pages-flag
1136 (Man-goto-page 1)
1137 (error "You're looking at the last manpage in the buffer"))))
1139 (defun Man-previous-manpage ()
1140 "Find the previous manpage entry in the buffer."
1141 (interactive)
1142 (if (= (length Man-page-list) 1)
1143 (error "This is the only manpage in the buffer"))
1144 (if (> Man-current-page 1)
1145 (Man-goto-page (1- Man-current-page))
1146 (if Man-circular-pages-flag
1147 (Man-goto-page (length Man-page-list))
1148 (error "You're looking at the first manpage in the buffer"))))
1150 ;; Init the man package variables, if not already done.
1151 (Man-init-defvars)
1153 (add-to-list 'debug-ignored-errors "^No manpage [0-9]* found$")
1154 (add-to-list 'debug-ignored-errors "^Can't find the .* manpage$")
1156 (provide 'man)
1158 ;;; man.el ends here