1 ;;; man.el --- browse UNIX manual pages -*- coding: iso-8859-1 -*-
3 ;; Copyright (C) 1993, 1994, 1996, 1997, 2001, 2003 Free Software Foundation, Inc.
5 ;; Author: Barry A. Warsaw <bwarsaw@cen.com>
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)
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.
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
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
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 heuristics
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
100 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
101 ;; empty defvars (keep the compiler quiet)
104 "Browse UNIX manual pages."
110 (defvar Man-current-page
)
111 (defvar Man-page-list
)
112 (defcustom Man-filter-list nil
113 "*Manpage cleaning filter command phrases.
114 This variable contains a list of the following form:
116 '((command-string phrase-string*)*)
118 Each phrase-string is concatenated onto the command-string to form a
119 command filter. The (standard) output (and standard error) of the Un*x
120 man command is piped through each command filter in the order the
121 commands appear in the association list. The final output is placed in
123 :type
'(repeat (list (string :tag
"Command String")
125 (string :tag
"Phrase String"))))
128 (defvar Man-original-frame
)
129 (defvar Man-arguments
)
130 (defvar Man-sections-alist
)
131 (defvar Man-refpages-alist
)
132 (defvar Man-uses-untabify-flag t
133 "Non-nil means use `untabify' instead of `Man-untabify-command'.")
134 (defvar Man-page-mode-string
)
135 (defvar Man-sed-script nil
136 "Script for sed to nuke backspaces and ANSI codes from manpages.")
138 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
141 (defcustom Man-fontify-manpage-flag t
142 "*Non-nil means make up the manpage with fonts."
146 (defcustom Man-overstrike-face
'bold
147 "*Face to use when fontifying overstrike."
151 (defcustom Man-underline-face
'underline
152 "*Face to use when fontifying underlining."
156 ;; Use the value of the obsolete user option Man-notify, if set.
157 (defcustom Man-notify-method
(if (boundp 'Man-notify
) Man-notify
'friendly
)
158 "*Selects the behavior when manpage is ready.
159 This variable may have one of the following values, where (sf) means
160 that the frames are switched, so the manpage is displayed in the frame
161 where the man command was called from:
163 newframe -- put the manpage in its own frame (see `Man-frame-parameters')
164 pushy -- make the manpage the current buffer in the current window
165 bully -- make the manpage the current buffer and only window (sf)
166 aggressive -- make the manpage the current buffer in the other window (sf)
167 friendly -- display manpage in the other window but don't make current (sf)
168 polite -- don't display manpage, but prints message and beep when ready
169 quiet -- like `polite', but don't beep
170 meek -- make no indication that the manpage is ready
172 Any other value of `Man-notify-method' is equivalent to `meek'."
173 :type
'(radio (const newframe
) (const pushy
) (const bully
)
174 (const aggressive
) (const friendly
)
175 (const polite
) (const quiet
) (const meek
))
178 (defcustom Man-frame-parameters nil
179 "*Frame parameter list for creating a new frame for a manual page."
183 (defcustom Man-downcase-section-letters-flag t
184 "*Non-nil means letters in sections are converted to lower case.
185 Some Un*x man commands can't handle uppercase letters in sections, for
186 example \"man 2V chmod\", but they are often displayed in the manpage
187 with the upper case letter. When this variable is t, the section
188 letter (e.g., \"2V\") is converted to lowercase (e.g., \"2v\") before
189 being sent to the man background process."
193 (defcustom Man-circular-pages-flag t
194 "*Non-nil means the manpage list is treated as circular for traversal."
198 (defcustom Man-section-translations-alist
201 ;; Some systems have a real 3x man section, so let's comment this.
202 ;; '("3X" . "3") ; Xlib man pages
205 "*Association list of bogus sections to real section numbers.
206 Some manpages (e.g. the Sun C++ 2.1 manpages) have section numbers in
207 their references which Un*x `man' does not recognize. This
208 association list is used to translate those sections, when found, to
209 the associated section number."
210 :type
'(repeat (cons (string :tag
"Bogus Section")
211 (string :tag
"Real Section")))
214 (defcustom Man-header-file-path
215 '("/usr/include" "/usr/local/include")
216 "C Header file search path used in Man."
217 :type
'(repeat string
)
220 (defvar manual-program
"man"
221 "The name of the program that produces man pages.")
223 (defvar Man-untabify-command
"pr"
224 "Command used for untabifying.")
226 (defvar Man-untabify-command-args
(list "-t" "-e")
227 "List of arguments to be passed to `Man-untabify-command' (which see).")
229 (defvar Man-sed-command
"sed"
230 "Command used for processing sed scripts.")
232 (defvar Man-awk-command
"awk"
233 "Command used for processing awk scripts.")
235 (defvar Man-mode-map nil
236 "Keymap for Man mode.")
238 (defvar Man-mode-hook nil
239 "Hook run when Man mode is enabled.")
241 (defvar Man-cooked-hook nil
242 "Hook run after removing backspaces but before `Man-mode' processing.")
244 (defvar Man-name-regexp
"[-a-zA-Z0-9_][-a-zA-Z0-9_.]*"
245 "Regular expression describing the name of a manpage (without section).")
247 (defvar Man-section-regexp
"[0-9][a-zA-Z+]*\\|[LNln]"
248 "Regular expression describing a manpage section within parentheses.")
250 (defvar Man-page-header-regexp
251 (if (and (string-match "-solaris2\\." system-configuration
)
252 (not (string-match "-solaris2\\.[123435]$" system-configuration
)))
253 (concat "^[-A-Za-z0-9_].*[ \t]\\(" Man-name-regexp
254 "(\\(" Man-section-regexp
"\\))\\)$")
255 (concat "^[ \t]*\\(" Man-name-regexp
256 "(\\(" Man-section-regexp
"\\))\\).*\\1"))
257 "Regular expression describing the heading of a page.")
259 (defvar Man-heading-regexp
"^\\([A-Z][A-Z -]+\\)$"
260 "Regular expression describing a manpage heading entry.")
262 (defvar Man-see-also-regexp
"SEE ALSO"
263 "Regular expression for SEE ALSO heading (or your equivalent).
264 This regexp should not start with a `^' character.")
266 (defvar Man-first-heading-regexp
"^[ \t]*NAME$\\|^[ \t]*No manual entry fo.*$"
267 "Regular expression describing first heading on a manpage.
268 This regular expression should start with a `^' character.")
270 (defvar Man-reference-regexp
271 (concat "\\(" Man-name-regexp
"\\)(\\(" Man-section-regexp
"\\))")
272 "Regular expression describing a reference to another manpage.")
274 (defvar Man-synopsis-regexp
"SYNOPSIS"
275 "Regular expression for SYNOPSIS heading (or your equivalent).
276 This regexp should not start with a `^' character.")
278 (defvar Man-files-regexp
"FILES"
279 "Regular expression for FILES heading (or your equivalent).
280 This regexp should not start with a `^' character.")
282 (defvar Man-include-regexp
"#[ \t]*include[ \t]*"
283 "Regular expression describing the #include (directive of cpp).")
285 (defvar Man-file-name-regexp
"[^<>\" \t\n]+"
286 "Regular expression describing <> in #include line (directive of cpp).")
288 (defvar Man-normal-file-prefix-regexp
"[/~$]"
289 "Regular expression describing a file path appeared in FILES section.")
291 (defvar Man-header-regexp
292 (concat "\\(" Man-include-regexp
"\\)"
294 "\\(" Man-file-name-regexp
"\\)"
296 "Regular expression describing references to header files.")
298 (defvar Man-normal-file-regexp
299 (concat Man-normal-file-prefix-regexp Man-file-name-regexp
)
300 "Regular expression describing references to normal files.")
302 ;; This includes the section as an optional part to catch hyphenated
303 ;; refernces to manpages.
304 (defvar Man-hyphenated-reference-regexp
305 (concat "\\(" Man-name-regexp
"\\)\\((\\(" Man-section-regexp
"\\))\\)?")
306 "Regular expression describing a reference in the SEE ALSO section.")
308 (defvar Man-switches
""
309 "Switches passed to the man command, as a single string.
311 If you want to be able to see all the manpages for a subject you type,
312 make -a one of the switches, if your `man' program supports it.")
314 (defvar Man-specified-section-option
315 (if (string-match "-solaris[0-9.]*$" system-configuration
)
318 "Option that indicates a specified a manual section name.")
320 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
321 ;; end user variables
323 ;; other variables and keymap initializations
324 (make-variable-buffer-local 'Man-sections-alist
)
325 (make-variable-buffer-local 'Man-refpages-alist
)
326 (make-variable-buffer-local 'Man-page-list
)
327 (make-variable-buffer-local 'Man-current-page
)
328 (make-variable-buffer-local 'Man-page-mode-string
)
329 (make-variable-buffer-local 'Man-original-frame
)
330 (make-variable-buffer-local 'Man-arguments
)
332 (setq-default Man-sections-alist nil
)
333 (setq-default Man-refpages-alist nil
)
334 (setq-default Man-page-list nil
)
335 (setq-default Man-current-page
0)
336 (setq-default Man-page-mode-string
"1 of 1")
338 (defconst Man-sysv-sed-script
"\
347 /\e\\[[0-9][0-9]*m/ s///g"
348 "Script for sysV-like sed to nuke backspaces and ANSI codes from manpages.")
350 (defconst Man-berkeley-sed-script
"\
356 s/\\(.\\)\b\\1/\\1/g\\
359 /\e\\[[0-9][0-9]*m/ s///g"
360 "Script for berkeley-like sed to nuke backspaces and ANSI codes from manpages.")
362 (defvar man-mode-syntax-table
363 (let ((table (copy-syntax-table (standard-syntax-table))))
364 (modify-syntax-entry ?.
"w" table
)
365 (modify-syntax-entry ?_
"w" table
)
367 "Syntax table used in Man mode buffers.")
371 (setq Man-mode-map
(copy-keymap button-buffer-map
))
372 (suppress-keymap Man-mode-map
)
373 (define-key Man-mode-map
" " 'scroll-up
)
374 (define-key Man-mode-map
"\177" 'scroll-down
)
375 (define-key Man-mode-map
"n" 'Man-next-section
)
376 (define-key Man-mode-map
"p" 'Man-previous-section
)
377 (define-key Man-mode-map
"\en" 'Man-next-manpage
)
378 (define-key Man-mode-map
"\ep" 'Man-previous-manpage
)
379 (define-key Man-mode-map
">" 'end-of-buffer
)
380 (define-key Man-mode-map
"<" 'beginning-of-buffer
)
381 (define-key Man-mode-map
"." 'beginning-of-buffer
)
382 (define-key Man-mode-map
"r" 'Man-follow-manual-reference
)
383 (define-key Man-mode-map
"g" 'Man-goto-section
)
384 (define-key Man-mode-map
"s" 'Man-goto-see-also-section
)
385 (define-key Man-mode-map
"k" 'Man-kill
)
386 (define-key Man-mode-map
"q" 'Man-quit
)
387 (define-key Man-mode-map
"m" 'man
)
388 (define-key Man-mode-map
"?" 'describe-mode
)
392 (define-button-type 'Man-xref-man-page
393 'action
(lambda (button) (man-follow (button-label button
)))
394 'help-echo
"RET, mouse-2: display this man page")
396 (define-button-type 'Man-xref-header-file
397 'action
(lambda (button)
398 (let ((w (button-get button
'Man-target-string
)))
399 (unless (Man-view-header-file w
)
400 (error "Cannot find header file: %s" w
))))
401 'help-echo
"mouse-2: display this header file")
403 (define-button-type 'Man-xref-normal-file
404 'action
(lambda (button)
405 (let ((f (substitute-in-file-name
406 (button-get button
'Man-target-string
))))
407 (if (file-exists-p f
)
408 (if (file-readable-p f
)
410 (error "Cannot read a file: %s" f
))
411 (error "Cannot find a file: %s" f
))))
412 'help-echo
"mouse-2: mouse-2: display this file")
415 ;; ======================================================================
418 (defun Man-init-defvars ()
419 "Used for initialising variables based on display's color support.
420 This is necessary if one wants to dump man.el with Emacs."
422 ;; Avoid possible error in call-process by using a directory that must exist.
423 (let ((default-directory "/"))
426 (Man-fontify-manpage-flag
428 ((= 0 (call-process Man-sed-command nil nil nil Man-sysv-sed-script
))
430 ((= 0 (call-process Man-sed-command nil nil nil Man-berkeley-sed-script
))
431 Man-berkeley-sed-script
)
435 (setq Man-filter-list
436 ;; Avoid trailing nil which confuses customize.
442 (concat "-e '" Man-sed-script
"'")
444 "-e '/^[\001-\032][\001-\032]*$/d'"
445 "-e '/\e[789]/s///g'"
446 "-e '/Reformatting page. Wait/d'"
447 "-e '/Reformatting entry. Wait/d'"
448 "-e '/^[ \t]*Hewlett-Packard[ \t]Company[ \t]*-[ \t][0-9]*[ \t]-/d'"
449 "-e '/^[ \t]*Hewlett-Packard[ \t]*-[ \t][0-9]*[ \t]-.*$/d'"
450 "-e '/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d'"
451 "-e '/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d'"
452 "-e '/^Printed[ \t][0-9].*[0-9]$/d'"
453 "-e '/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d'"
454 "-e '/^[A-Za-z].*Last[ \t]change:/d'"
455 "-e '/^Sun[ \t]Release[ \t][0-9].*[0-9]$/d'"
456 "-e '/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d'"
457 "-e '/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d'"
463 "BEGIN { blankline=0; anonblank=0; }\n"
464 "/^$/ { if (anonblank==0) next; }\n"
466 "/^$/ { blankline++; next; }\n"
467 "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }\n"
470 (if (not Man-uses-untabify-flag
)
471 ;; The outer list will be stripped off by apply.
474 Man-untabify-command-args
))
478 (defsubst Man-make-page-mode-string
()
479 "Formats part of the mode line for Man mode."
480 (format "%s page %d of %d"
481 (or (nth 2 (nth (1- Man-current-page
) Man-page-list
))
484 (length Man-page-list
)))
486 (defsubst Man-build-man-command
()
487 "Builds the entire background manpage and cleaning command."
488 (let ((command (concat manual-program
" " Man-switches
489 ; Stock MS-DOS shells cannot redirect stderr;
490 ; `call-process' below sends it to /dev/null,
491 ; so we don't need `2>' even with DOS shells
492 ; which do support stderr redirection.
493 (if (not (fboundp 'start-process
))
495 (concat " %s 2>" null-device
))))
496 (flist Man-filter-list
))
497 (while (and flist
(car flist
))
498 (let ((pcom (car (car flist
)))
499 (pargs (cdr (car flist
))))
501 (concat command
" | " pcom
" "
502 (mapconcat (lambda (phrase)
503 (if (not (stringp phrase
))
504 (error "Malformed Man-filter-list"))
507 (setq flist
(cdr flist
))))
510 (defun Man-translate-references (ref)
511 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style.
512 Leave it as is if already in that style. Possibly downcase and
513 translate the section (see the Man-downcase-section-letters-flag
514 and the Man-section-translations-alist variables)."
517 (slist Man-section-translations-alist
))
519 ;; "chmod(2V)" case ?
520 ((string-match (concat "^" Man-reference-regexp
"$") ref
)
521 (setq name
(match-string 1 ref
)
522 section
(match-string 2 ref
)))
524 ((string-match (concat "^\\(" Man-section-regexp
525 "\\) +\\(" Man-name-regexp
"\\)$") ref
)
526 (setq name
(match-string 2 ref
)
527 section
(match-string 1 ref
))))
528 (if (string= name
"")
529 ref
; Return the reference as is
530 (if Man-downcase-section-letters-flag
531 (setq section
(downcase section
)))
533 (let ((s1 (car (car slist
)))
534 (s2 (cdr (car slist
))))
535 (setq slist
(cdr slist
))
536 (if Man-downcase-section-letters-flag
537 (setq s1
(downcase s1
)))
538 (if (not (string= s1 section
)) nil
539 (setq section
(if Man-downcase-section-letters-flag
543 (concat Man-specified-section-option section
" " name
))))
546 ;; ======================================================================
547 ;; default man entry: get word under point
549 (defsubst Man-default-man-entry
()
550 "Make a guess at a default manual entry.
551 This guess is based on the text surrounding the cursor."
554 ;; Default man entry title is any word the cursor is on, or if
555 ;; cursor not on a word, then nearest preceding word.
556 (skip-chars-backward "-a-zA-Z0-9._+:")
557 (let ((start (point)))
558 (skip-chars-forward "-a-zA-Z0-9._+:")
559 (setq word
(buffer-substring start
(point))))
560 (if (string-match "[._]+$" word
)
561 (setq word
(substring word
0 (match-beginning 0))))
562 ;; If looking at something like ioctl(2) or brc(1M), include the
563 ;; section number in the returned value. Remove text properties.
565 ;; Use `format' here to clear any text props from `word'.
569 (concat "[ \t]*([ \t]*\\(" Man-section-regexp
"\\)[ \t]*)"))
570 (format "(%s)" (match-string 1))
574 ;; ======================================================================
575 ;; Top level command and background process sentinel
577 ;; For compatibility with older versions.
579 (defalias 'manual-entry
'man
)
582 (defun man (man-args)
583 "Get a Un*x manual page and put it in a buffer.
584 This command is the top-level command in the man package. It runs a Un*x
585 command to retrieve and clean a manpage in the background and places the
586 results in a Man mode (manpage browsing) buffer. See variable
587 `Man-notify-method' for what happens when the buffer is ready.
588 If a buffer already exists for this man page, it will display immediately.
590 To specify a man page from a certain section, type SUBJECT(SECTION) or
591 SECTION SUBJECT when prompted for a manual entry. To see manpages from
592 all sections related to a subject, put something appropriate into the
593 `Man-switches' variable, which see."
595 (list (let* ((default-entry (Man-default-man-entry))
597 (format "Manual entry%s: "
598 (if (string= default-entry
"")
600 (format " (default %s)" default-entry
)))
601 nil nil default-entry
)))
602 (if (string= input
"")
603 (error "No man args given")
606 ;; Possibly translate the "subject(section)" syntax into the
607 ;; "section subject" syntax and possibly downcase the section.
608 (setq man-args
(Man-translate-references man-args
))
610 (Man-getpage-in-background man-args
))
613 (defun man-follow (man-args)
614 "Get a Un*x manual page of the item under point and put it in a buffer."
615 (interactive (list (Man-default-man-entry)))
616 (if (or (not man-args
)
617 (string= man-args
""))
618 (error "No item under point")
621 (defun Man-getpage-in-background (topic)
622 "Use TOPIC to build and fire off the manpage and cleaning command."
623 (let* ((man-args topic
)
624 (bufname (concat "*Man " man-args
"*"))
625 (buffer (get-buffer bufname
)))
627 (Man-notify-when-ready buffer
)
629 (message "Invoking %s %s in the background" manual-program man-args
)
630 (setq buffer
(generate-new-buffer bufname
))
633 (setq Man-original-frame
(selected-frame))
634 (setq Man-arguments man-args
))
635 (let ((process-environment (copy-sequence process-environment
))
636 ;; The following is so Awk script gets \n intact
637 ;; But don't prevent decoding of the outside.
638 (coding-system-for-write 'raw-text-unix
)
639 ;; We must decode the output by a coding system that the
640 ;; system's locale suggests in multibyte mode.
641 (coding-system-for-read
642 (if default-enable-multibyte-characters
643 locale-coding-system
'raw-text-unix
))
644 ;; Avoid possible error by using a directory that always exists.
646 (if (and (file-directory-p default-directory
)
647 (not (find-file-name-handler default-directory
651 ;; Prevent any attempt to use display terminal fanciness.
652 (setenv "TERM" "dumb")
653 ;; In Debian Woody, at least, we get overlong lines under X
654 ;; unless COLUMNS or MANWIDTH is set. This isn't a problem on
655 ;; a tty. man(1) says:
657 ;; If $MANWIDTH is set, its value is used as the line
658 ;; length for which manual pages should be formatted.
659 ;; If it is not set, manual pages will be formatted
660 ;; with a line length appropriate to the current ter-
661 ;; minal (using an ioctl(2) if available, the value of
662 ;; $COLUMNS, or falling back to 80 characters if nei-
663 ;; ther is available).
665 (unless (or (getenv "MANWIDTH") (getenv "COLUMNS"))
666 ;; This isn't strictly correct, since we don't know how
667 ;; the page will actually be displayed, but it seems
669 (setenv "COLUMNS" (number-to-string (frame-width)))))
670 (setenv "GROFF_NO_SGR" "1")
671 (if (fboundp 'start-process
)
672 (set-process-sentinel
673 (start-process manual-program buffer
"sh" "-c"
674 (format (Man-build-man-command) man-args
))
675 'Man-bgproc-sentinel
)
677 (call-process shell-file-name nil
(list buffer nil
) nil
"-c"
678 (format (Man-build-man-command) man-args
)))
680 (or (and (numberp exit-status
)
682 (and (numberp exit-status
)
684 (format "exited abnormally with code %d"
686 (setq msg exit-status
))
687 (Man-bgproc-sentinel bufname msg
)))))))
689 (defun Man-notify-when-ready (man-buffer)
690 "Notify the user when MAN-BUFFER is ready.
691 See the variable `Man-notify-method' for the different notification behaviors."
692 (let ((saved-frame (save-excursion
693 (set-buffer man-buffer
)
694 Man-original-frame
)))
696 ((eq Man-notify-method
'newframe
)
697 ;; Since we run asynchronously, perhaps while Emacs is waiting
698 ;; for input, we must not leave a different buffer current. We
699 ;; can't rely on the editor command loop to reselect the
700 ;; selected window's buffer.
702 (let ((frame (make-frame Man-frame-parameters
)))
703 (set-window-buffer (frame-selected-window frame
) man-buffer
)
704 (set-window-dedicated-p (frame-selected-window frame
) t
)
705 (or (display-multi-frame-p frame
)
706 (select-frame frame
)))))
707 ((eq Man-notify-method
'pushy
)
708 (switch-to-buffer man-buffer
))
709 ((eq Man-notify-method
'bully
)
710 (and (frame-live-p saved-frame
)
711 (select-frame saved-frame
))
712 (pop-to-buffer man-buffer
)
713 (delete-other-windows))
714 ((eq Man-notify-method
'aggressive
)
715 (and (frame-live-p saved-frame
)
716 (select-frame saved-frame
))
717 (pop-to-buffer man-buffer
))
718 ((eq Man-notify-method
'friendly
)
719 (and (frame-live-p saved-frame
)
720 (select-frame saved-frame
))
721 (display-buffer man-buffer
'not-this-window
))
722 ((eq Man-notify-method
'polite
)
724 (message "Manual buffer %s is ready" (buffer-name man-buffer
)))
725 ((eq Man-notify-method
'quiet
)
726 (message "Manual buffer %s is ready" (buffer-name man-buffer
)))
727 ((or (eq Man-notify-method
'meek
)
732 (defun Man-softhyphen-to-minus ()
733 ;; \255 is SOFT HYPHEN in Latin-N. Versions of Debian man, at
734 ;; least, emit it even when not in a Latin-N locale.
735 (unless (eq t
(compare-strings "latin-" 0 nil
736 current-language-environment
0 6 t
))
737 (goto-char (point-min))
739 (if enable-multibyte-characters
740 (setq str
(string-as-multibyte str
)))
741 (while (search-forward str nil t
) (replace-match "-")))))
743 (defun Man-fontify-manpage ()
744 "Convert overstriking and underlining to the correct fonts.
745 Same for the ANSI bold and normal escape sequences."
747 (message "Please wait: making up the %s man page..." Man-arguments
)
748 (goto-char (point-min))
749 (while (search-forward "\e[1m" nil t
)
750 (delete-backward-char 4)
751 (put-text-property (point)
752 (progn (if (search-forward "\e[0m" nil
'move
)
753 (delete-backward-char 4))
755 'face Man-overstrike-face
))
756 (if (< (buffer-size) (position-bytes (point-max)))
757 ;; Multibyte characters exist.
759 (goto-char (point-min))
760 (while (search-forward "__\b\b" nil t
)
761 (backward-delete-char 4)
762 (put-text-property (point) (1+ (point)) 'face Man-underline-face
))
763 (goto-char (point-min))
764 (while (search-forward "\b\b__" nil t
)
765 (backward-delete-char 4)
766 (put-text-property (1- (point)) (point) 'face Man-underline-face
))))
767 (goto-char (point-min))
768 (while (search-forward "_\b" nil t
)
769 (backward-delete-char 2)
770 (put-text-property (point) (1+ (point)) 'face Man-underline-face
))
771 (goto-char (point-min))
772 (while (search-forward "\b_" nil t
)
773 (backward-delete-char 2)
774 (put-text-property (1- (point)) (point) 'face Man-underline-face
))
775 (goto-char (point-min))
776 (while (re-search-forward "\\(.\\)\\(\b+\\1\\)+" nil t
)
777 (replace-match "\\1")
778 (put-text-property (1- (point)) (point) 'face Man-overstrike-face
))
779 (goto-char (point-min))
780 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t
)
782 (put-text-property (1- (point)) (point) 'face
'bold
))
783 (goto-char (point-min))
784 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t
)
786 (put-text-property (1- (point)) (point) 'face
'bold
))
787 (goto-char (point-min))
788 ;; Try to recognize common forms of cross references.
789 (Man-highlight-references)
790 (Man-softhyphen-to-minus)
791 (message "%s man page made up" Man-arguments
))
793 (defun Man-highlight-references ()
794 "Highlight the references on mouse-over.
795 references include items in the SEE ALSO section,
796 header file(#include <foo.h>) and files in FILES"
798 (Man-highlight-references0
799 Man-see-also-regexp Man-reference-regexp
1 dummy
801 (Man-highlight-references0
802 Man-synopsis-regexp Man-header-regexp
0 2
803 'Man-xref-header-file
)
804 (Man-highlight-references0
805 Man-files-regexp Man-normal-file-regexp
0 0
806 'Man-xref-normal-file
)))
808 (defun Man-highlight-references0 (start-section regexp button-pos target-pos type
)
809 ;; Based on `Man-build-references-alist'
810 (when (Man-find-section start-section
)
812 (let ((end (save-excursion
815 (back-to-indentation)
816 (while (re-search-forward regexp end t
)
818 (match-beginning button-pos
)
819 (match-end button-pos
)
821 'Man-target-string
(match-string target-pos
)
824 (defun Man-cleanup-manpage ()
825 "Remove overstriking and underlining from the current buffer."
827 (message "Please wait: cleaning up the %s man page..."
829 (if (or (interactive-p) (not Man-sed-script
))
831 (goto-char (point-min))
832 (while (search-forward "_\b" nil t
) (backward-delete-char 2))
833 (goto-char (point-min))
834 (while (search-forward "\b_" nil t
) (backward-delete-char 2))
835 (goto-char (point-min))
836 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t
)
837 (replace-match "\\1"))
838 (goto-char (point-min))
839 (while (re-search-forward "\e\\[[0-9]+m" nil t
) (replace-match ""))
840 (goto-char (point-min))
841 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t
) (replace-match "o"))
843 (goto-char (point-min))
844 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t
) (replace-match "+"))
845 (Man-softhyphen-to-minus)
846 (message "%s man page cleaned up" Man-arguments
))
848 (defun Man-bgproc-sentinel (process msg
)
849 "Manpage background process sentinel.
850 When manpage command is run asynchronously, PROCESS is the process
851 object for the manpage command; when manpage command is run
852 synchronously, PROCESS is the name of the buffer where the manpage
853 command is run. Second argument MSG is the exit message of the
855 (let ((Man-buffer (if (stringp process
) (get-buffer process
)
856 (process-buffer process
)))
860 (if (null (buffer-name Man-buffer
)) ;; deleted buffer
861 (or (stringp process
)
862 (set-process-buffer process nil
))
865 (set-buffer Man-buffer
)
866 (let ((case-fold-search nil
))
867 (goto-char (point-min))
868 (cond ((or (looking-at "No \\(manual \\)*entry for")
869 (looking-at "[^\n]*: nothing appropriate$"))
870 (setq err-mess
(buffer-substring (point)
872 (end-of-line) (point)))
874 ((or (stringp process
)
875 (not (and (eq (process-status process
) 'exit
)
876 (= (process-exit-status process
) 0))))
877 (or (zerop (length msg
))
880 (concat (buffer-name Man-buffer
)
882 (let ((eos (1- (length msg
))))
883 (if (= (aref msg eos
) ?
\n)
884 (substring msg
0 eos
) msg
))))
885 (goto-char (point-max))
886 (insert (format "\nprocess %s" msg
))))
889 (kill-buffer Man-buffer
)
890 (if Man-fontify-manpage-flag
891 (Man-fontify-manpage)
892 (Man-cleanup-manpage))
893 (run-hooks 'Man-cooked-hook
)
895 (set-buffer-modified-p nil
)
897 ;; Restore case-fold-search before calling
898 ;; Man-notify-when-ready because it may switch buffers.
900 (if (not delete-buff
)
901 (Man-notify-when-ready Man-buffer
))
908 ;; ======================================================================
909 ;; set up manual mode in buffer and build alists
912 "A mode for browsing Un*x manual pages.
914 The following man commands are available in the buffer. Try
915 \"\\[describe-key] <key> RET\" for more information:
917 \\[man] Prompt to retrieve a new manpage.
918 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
919 \\[Man-next-manpage] Jump to next manpage in circular list.
920 \\[Man-previous-manpage] Jump to previous manpage in circular list.
921 \\[Man-next-section] Jump to next manpage section.
922 \\[Man-previous-section] Jump to previous manpage section.
923 \\[Man-goto-section] Go to a manpage section.
924 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
925 \\[Man-quit] Deletes the manpage window, bury its buffer.
926 \\[Man-kill] Deletes the manpage window, kill its buffer.
927 \\[describe-mode] Prints this help text.
929 The following variables may be of some use. Try
930 \"\\[describe-variable] <variable-name> RET\" for more information:
932 `Man-notify-method' What happens when manpage formatting is done.
933 `Man-downcase-section-letters-flag' Force section letters to lower case.
934 `Man-circular-pages-flag' Treat multiple manpage list as circular.
935 `Man-section-translations-alist' List of section numbers and their Un*x equiv.
936 `Man-filter-list' Background manpage filter command.
937 `Man-mode-map' Keymap bindings for Man mode buffers.
938 `Man-mode-hook' Normal hook run on entry to Man mode.
939 `Man-section-regexp' Regexp describing manpage section letters.
940 `Man-heading-regexp' Regexp describing section headers.
941 `Man-see-also-regexp' Regexp for SEE ALSO section (or your equiv).
942 `Man-first-heading-regexp' Regexp for first heading on a manpage.
943 `Man-reference-regexp' Regexp matching a references in SEE ALSO.
944 `Man-switches' Background `man' command switches.
946 The following key bindings are currently in effect in the buffer:
949 (setq major-mode
'Man-mode
951 buffer-auto-save-file-name nil
952 mode-line-buffer-identification
953 (list (default-value 'mode-line-buffer-identification
)
954 " {" 'Man-page-mode-string
"}")
957 (buffer-disable-undo (current-buffer))
959 (use-local-map Man-mode-map
)
960 (set-syntax-table man-mode-syntax-table
)
961 (Man-build-page-list)
962 (Man-strip-page-headers)
965 (run-hooks 'Man-mode-hook
))
967 (defsubst Man-build-section-alist
()
968 "Build the association list of manpage sections."
969 (setq Man-sections-alist nil
)
970 (goto-char (point-min))
971 (let ((case-fold-search nil
))
972 (while (re-search-forward Man-heading-regexp
(point-max) t
)
973 (aput 'Man-sections-alist
(match-string 1))
976 (defsubst Man-build-references-alist
()
977 "Build the association list of references (in the SEE ALSO section)."
978 (setq Man-refpages-alist nil
)
980 (if (Man-find-section Man-see-also-regexp
)
981 (let ((start (progn (forward-line 1) (point)))
988 (narrow-to-region start end
)
989 (goto-char (point-min))
990 (back-to-indentation)
991 (while (and (not (eobp)) (/= (point) runningpoint
))
992 (setq runningpoint
(point))
993 (if (re-search-forward Man-hyphenated-reference-regexp end t
)
994 (let* ((word (match-string 0))
995 (len (1- (length word
))))
997 (setq word
(concat hyphenated word
)
999 ;; Update len, in case a reference spans
1000 ;; more than two lines (paranoia).
1001 len
(1- (length word
))))
1002 (if (memq (aref word len
) '(?- ?
))
1003 (setq hyphenated
(substring word
0 len
)))
1004 (if (string-match Man-reference-regexp word
)
1005 (aput 'Man-refpages-alist word
))))
1006 (skip-chars-forward " \t\n,"))))))
1007 (setq Man-refpages-alist
(nreverse Man-refpages-alist
)))
1009 (defun Man-build-page-list ()
1010 "Build the list of separate manpages in the buffer."
1011 (setq Man-page-list nil
)
1012 (let ((page-start (point-min))
1013 (page-end (point-max))
1015 (goto-char page-start
)
1016 ;; (switch-to-buffer (current-buffer))(debug)
1019 (if (looking-at Man-page-header-regexp
)
1022 ;; Go past both the current and the next Man-first-heading-regexp
1023 (if (re-search-forward Man-first-heading-regexp nil
'move
2)
1024 (let ((p (progn (beginning-of-line) (point))))
1025 ;; We assume that the page header is delimited by blank
1026 ;; lines and that it contains at most one blank line. So
1027 ;; if we back by three blank lines we will be sure to be
1028 ;; before the page header but not before the possible
1029 ;; previous page header.
1030 (search-backward "\n\n" nil t
3)
1031 (if (re-search-forward Man-page-header-regexp p
'move
)
1032 (beginning-of-line))))
1033 (setq page-end
(point))
1034 (setq Man-page-list
(append Man-page-list
1035 (list (list (copy-marker page-start
)
1036 (copy-marker page-end
)
1038 (setq page-start page-end
)
1041 (defun Man-strip-page-headers ()
1042 "Strip all the page headers but the first from the manpage."
1043 (let ((buffer-read-only nil
)
1044 (case-fold-search nil
)
1045 (page-list Man-page-list
)
1049 (setq page
(car page-list
))
1051 (goto-char (car page
))
1052 (re-search-forward Man-first-heading-regexp nil t
)
1053 (setq header
(buffer-substring (car page
) (match-beginning 0)))
1054 ;; Since the awk script collapses all successive blank
1055 ;; lines into one, and since we don't want to get rid of
1056 ;; the fast awk script, one must choose between adding
1057 ;; spare blank lines between pages when there were none and
1058 ;; deleting blank lines at page boundaries when there were
1059 ;; some. We choose the first, so we comment the following
1061 ;; (setq header (concat "\n" header)))
1062 (while (search-forward header
(nth 1 page
) t
)
1063 (replace-match "")))
1064 (setq page-list
(cdr page-list
)))))
1066 (defun Man-unindent ()
1067 "Delete the leading spaces that indent the manpage."
1068 (let ((buffer-read-only nil
)
1069 (case-fold-search nil
)
1070 (page-list Man-page-list
))
1072 (let ((page (car page-list
))
1075 (narrow-to-region (car page
) (car (cdr page
)))
1076 (if Man-uses-untabify-flag
1077 (untabify (point-min) (point-max)))
1078 (if (catch 'unindent
1079 (goto-char (point-min))
1080 (if (not (re-search-forward Man-first-heading-regexp nil t
))
1081 (throw 'unindent nil
))
1083 (setq indent
(buffer-substring (point)
1085 (skip-chars-forward " ")
1087 (setq nindent
(length indent
))
1089 (throw 'unindent nil
))
1090 (setq indent
(concat indent
"\\|$"))
1091 (goto-char (point-min))
1093 (if (looking-at indent
)
1095 (throw 'unindent nil
)))
1096 (goto-char (point-min)))
1099 (delete-char nindent
))
1101 (setq page-list
(cdr page-list
))
1105 ;; ======================================================================
1106 ;; Man mode commands
1108 (defun Man-next-section (n)
1109 "Move point to Nth next section (default 1)."
1111 (let ((case-fold-search nil
))
1112 (if (looking-at Man-heading-regexp
)
1114 (if (re-search-forward Man-heading-regexp
(point-max) t n
)
1116 (goto-char (point-max)))))
1118 (defun Man-previous-section (n)
1119 "Move point to Nth previous section (default 1)."
1121 (let ((case-fold-search nil
))
1122 (if (looking-at Man-heading-regexp
)
1124 (if (re-search-backward Man-heading-regexp
(point-min) t n
)
1126 (goto-char (point-min)))))
1128 (defun Man-find-section (section)
1129 "Move point to SECTION if it exists, otherwise don't move point.
1130 Returns t if section is found, nil otherwise."
1131 (let ((curpos (point))
1132 (case-fold-search nil
))
1133 (goto-char (point-min))
1134 (if (re-search-forward (concat "^" section
) (point-max) t
)
1135 (progn (beginning-of-line) t
)
1140 (defun Man-goto-section ()
1141 "Query for section to move point to."
1143 (aput 'Man-sections-alist
1144 (let* ((default (aheadsym Man-sections-alist
))
1145 (completion-ignore-case t
)
1147 (prompt (concat "Go to section: (default " default
") ")))
1148 (setq chosen
(completing-read prompt Man-sections-alist
))
1149 (if (or (not chosen
)
1150 (string= chosen
""))
1153 (Man-find-section (aheadsym Man-sections-alist
)))
1155 (defun Man-goto-see-also-section ()
1156 "Move point to the \"SEE ALSO\" section.
1157 Actually the section moved to is described by `Man-see-also-regexp'."
1159 (if (not (Man-find-section Man-see-also-regexp
))
1160 (error (concat "No " Man-see-also-regexp
1161 " section found in the current manpage"))))
1163 (defun Man-possibly-hyphenated-word ()
1164 "Return a possibly hyphenated word at point.
1165 If the word starts at the first non-whitespace column, and the
1166 previous line ends with a hyphen, return the last word on the previous
1167 line instead. Thus, if a reference to \"tcgetpgrp(3V)\" is hyphenated
1168 as \"tcgetp-grp(3V)\", and point is at \"grp(3V)\", we return
1169 \"tcgetp-\" instead of \"grp\"."
1171 (skip-syntax-backward "w()")
1172 (skip-chars-forward " \t")
1174 (word (current-word)))
1175 (when (eq beg
(save-excursion
1176 (back-to-indentation)
1179 (if (eq (char-before) ?-
)
1180 (setq word
(current-word))))
1183 (defun Man-follow-manual-reference (reference)
1184 "Get one of the manpages referred to in the \"SEE ALSO\" section.
1185 Specify which REFERENCE to use; default is based on word at point."
1187 (if (not Man-refpages-alist
)
1188 (error "There are no references in the current man page")
1189 (list (let* ((default (or
1190 (car (all-completions
1191 (let ((word (Man-possibly-hyphenated-word)))
1192 ;; strip a trailing '-':
1193 (if (string-match "-$" word
)
1195 (match-beginning 0))
1197 Man-refpages-alist
))
1198 (aheadsym Man-refpages-alist
)))
1200 (prompt (concat "Refer to: (default " default
") ")))
1201 (setq chosen
(completing-read prompt Man-refpages-alist
))
1202 (if (or (not chosen
)
1203 (string= chosen
""))
1206 (if (not Man-refpages-alist
)
1207 (error "Can't find any references in the current manpage")
1208 (aput 'Man-refpages-alist reference
)
1209 (Man-getpage-in-background
1210 (Man-translate-references (aheadsym Man-refpages-alist
)))))
1213 "Kill the buffer containing the manpage."
1218 "Bury the buffer containing the manpage."
1222 (defun Man-goto-page (page)
1223 "Go to the manual page on page PAGE."
1225 (if (not Man-page-list
)
1226 (let ((args Man-arguments
))
1227 (kill-buffer (current-buffer))
1228 (error "Can't find the %s manpage" args
))
1229 (if (= (length Man-page-list
) 1)
1230 (error "You're looking at the only manpage in the buffer")
1231 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1232 (length Man-page-list
)))))))
1233 (if (not Man-page-list
)
1234 (let ((args Man-arguments
))
1235 (kill-buffer (current-buffer))
1236 (error "Can't find the %s manpage" args
)))
1238 (> page
(length Man-page-list
)))
1239 (error "No manpage %d found" page
))
1240 (let* ((page-range (nth (1- page
) Man-page-list
))
1241 (page-start (car page-range
))
1242 (page-end (car (cdr page-range
))))
1243 (setq Man-current-page page
1244 Man-page-mode-string
(Man-make-page-mode-string))
1246 (goto-char page-start
)
1247 (narrow-to-region page-start page-end
)
1248 (Man-build-section-alist)
1249 (Man-build-references-alist)
1250 (goto-char (point-min))))
1253 (defun Man-next-manpage ()
1254 "Find the next manpage entry in the buffer."
1256 (if (= (length Man-page-list
) 1)
1257 (error "This is the only manpage in the buffer"))
1258 (if (< Man-current-page
(length Man-page-list
))
1259 (Man-goto-page (1+ Man-current-page
))
1260 (if Man-circular-pages-flag
1262 (error "You're looking at the last manpage in the buffer"))))
1264 (defun Man-previous-manpage ()
1265 "Find the previous manpage entry in the buffer."
1267 (if (= (length Man-page-list
) 1)
1268 (error "This is the only manpage in the buffer"))
1269 (if (> Man-current-page
1)
1270 (Man-goto-page (1- Man-current-page
))
1271 (if Man-circular-pages-flag
1272 (Man-goto-page (length Man-page-list
))
1273 (error "You're looking at the first manpage in the buffer"))))
1275 ;; Header file support
1276 (defun Man-view-header-file (file)
1277 "View a header file specified by FILE from `Man-header-file-path'."
1278 (let ((path Man-header-file-path
)
1281 (setq complete-path
(concat (car path
) "/" file
)
1283 (if (file-readable-p complete-path
)
1284 (progn (view-file complete-path
)
1286 (setq complete-path nil
)))
1289 ;; Init the man package variables, if not already done.
1292 (add-to-list 'debug-ignored-errors
"^No manpage [0-9]* found$")
1293 (add-to-list 'debug-ignored-errors
"^Can't find the .* manpage$")
1297 ;;; man.el ends here