1 ;;; man.el --- browse UNIX manual pages -*- coding: utf-8 -*-
3 ;; Copyright (C) 1993-1994, 1996-1997, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: Barry A. Warsaw <bwarsaw@cen.com>
6 ;; Maintainer: emacs-devel@gnu.org
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 3 of the License, or
15 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
27 ;; This code provides a function, `man', with which you can browse
28 ;; UNIX manual pages. Formatting is done in background so that you
29 ;; can continue to use your Emacs while processing is going on.
31 ;; The mode also supports hypertext-like following of manual page SEE
32 ;; ALSO references, and other features. See below or do `?' in a
33 ;; manual page buffer for details.
35 ;; ========== Credits and History ==========
36 ;; In mid 1991, several people posted some interesting improvements to
37 ;; man.el from the standard Emacs 18.57 distribution. I liked many of
38 ;; these, but wanted everything in one single package, so I decided
39 ;; to incorporate them into a single manual browsing mode. While
40 ;; much of the code here has been rewritten, and some features added,
41 ;; these folks deserve lots of credit for providing the initial
42 ;; excellent packages on which this one is based.
44 ;; Nick Duffek <duffek@chaos.cs.brandeis.edu>, posted a very nice
45 ;; improvement which retrieved and cleaned the manpages in a
46 ;; background process, and which correctly deciphered such options as
49 ;; Eric Rose <erose@jessica.stanford.edu>, submitted manual.el which
50 ;; provided a very nice manual browsing mode.
52 ;; This package was available as `superman.el' from the LCD package
53 ;; for some time before it was accepted into Emacs 19. The entry
54 ;; point and some other names have been changed to make it a drop-in
55 ;; replacement for the old man.el package.
57 ;; Francesco Potortì <pot@cnuce.cnr.it> cleaned it up thoroughly,
58 ;; making it faster, more robust and more tolerant of different
59 ;; systems' man idiosyncrasies.
61 ;; ========== Features ==========
62 ;; + Runs "man" in the background and pipes the results through a
63 ;; series of sed and awk scripts so that all retrieving and cleaning
64 ;; is done in the background. The cleaning commands are configurable.
65 ;; + Syntax is the same as Un*x man
66 ;; + Functionality is the same as Un*x man, including "man -k" and
67 ;; "man <section>", etc.
68 ;; + Provides a manual browsing mode with keybindings for traversing
69 ;; the sections of a manpage, following references in the SEE ALSO
71 ;; + Multiple manpages created with the same man command are put into
72 ;; a narrowed buffer circular list.
74 ;; ============= TODO ===========
75 ;; - Add a command for printing.
76 ;; - The awk script deletes multiple blank lines. This behavior does
77 ;; not allow to understand if there was indeed a blank line at the
78 ;; end or beginning of a page (after the header, or before the
79 ;; footer). A different algorithm should be used. It is easy to
80 ;; compute how many blank lines there are before and after the page
81 ;; headers, and after the page footer. But it is possible to compute
82 ;; the number of blank lines before the page footer by heuristics
83 ;; only. Is it worth doing?
84 ;; - Allow a user option to mean that all the manpages should go in
85 ;; the same buffer, where they can be browsed with M-n and M-p.
95 "Browse UNIX manual pages."
102 (defcustom Man-filter-list nil
103 "Manpage cleaning filter command phrases.
104 This variable contains a list of the following form:
106 '((command-string phrase-string*)*)
108 Each phrase-string is concatenated onto the command-string to form a
109 command filter. The (standard) output (and standard error) of the Un*x
110 man command is piped through each command filter in the order the
111 commands appear in the association list. The final output is placed in
113 :type
'(repeat (list (string :tag
"Command String")
115 (string :tag
"Phrase String"))))
118 (defvar Man-uses-untabify-flag t
119 "Non-nil means use `untabify' instead of `Man-untabify-command'.")
120 (defvar Man-sed-script nil
121 "Script for sed to nuke backspaces and ANSI codes from manpages.")
123 (defcustom Man-fontify-manpage-flag t
124 "Non-nil means make up the manpage with fonts."
128 (defface Man-overstrike
129 '((t (:inherit bold
)))
130 "Face to use when fontifying overstrike."
134 (defface Man-underline
135 '((t (:inherit underline
)))
136 "Face to use when fontifying underlining."
141 '((t (:inherit highlight
)))
142 "Face to use when fontifying reverse video."
146 (defvar Man-ansi-color-map
(let ((ansi-color-faces-vector
147 [ default Man-overstrike default Man-underline
148 Man-underline default default Man-reverse
]))
149 (ansi-color-make-color-map))
150 "The value used here for `ansi-color-map'.")
152 ;; Use the value of the obsolete user option Man-notify, if set.
153 (defcustom Man-notify-method
(if (boundp 'Man-notify
) Man-notify
'friendly
)
154 "Selects the behavior when manpage is ready.
155 This variable may have one of the following values, where (sf) means
156 that the frames are switched, so the manpage is displayed in the frame
157 where the man command was called from:
159 newframe -- put the manpage in its own frame (see `Man-frame-parameters')
160 pushy -- make the manpage the current buffer in the current window
161 bully -- make the manpage the current buffer and only window (sf)
162 aggressive -- make the manpage the current buffer in the other window (sf)
163 friendly -- display manpage in the other window but don't make current (sf)
164 polite -- don't display manpage, but prints message and beep when ready
165 quiet -- like `polite', but don't beep
166 meek -- make no indication that the manpage is ready
168 Any other value of `Man-notify-method' is equivalent to `meek'."
169 :type
'(radio (const newframe
) (const pushy
) (const bully
)
170 (const aggressive
) (const friendly
)
171 (const polite
) (const quiet
) (const meek
))
174 (defcustom Man-width nil
175 "Number of columns for which manual pages should be formatted.
176 If nil, use the width of the window where the manpage is displayed.
177 If non-nil, use the width of the frame where the manpage is displayed.
178 The value also can be a positive integer for a fixed width."
179 :type
'(choice (const :tag
"Window width" nil
)
180 (const :tag
"Frame width" t
)
181 (integer :tag
"Fixed width" :value
65))
184 (defcustom Man-frame-parameters nil
185 "Frame parameter list for creating a new frame for a manual page."
189 (defcustom Man-downcase-section-letters-flag t
190 "Non-nil means letters in sections are converted to lower case.
191 Some Un*x man commands can't handle uppercase letters in sections, for
192 example \"man 2V chmod\", but they are often displayed in the manpage
193 with the upper case letter. When this variable is t, the section
194 letter (e.g., \"2V\") is converted to lowercase (e.g., \"2v\") before
195 being sent to the man background process."
199 (defcustom Man-circular-pages-flag t
200 "Non-nil means the manpage list is treated as circular for traversal."
204 (defcustom Man-section-translations-alist
207 ;; Some systems have a real 3x man section, so let's comment this.
208 ;; '("3X" . "3") ; Xlib man pages
211 "Association list of bogus sections to real section numbers.
212 Some manpages (e.g. the Sun C++ 2.1 manpages) have section numbers in
213 their references which Un*x `man' does not recognize. This
214 association list is used to translate those sections, when found, to
215 the associated section number."
216 :type
'(repeat (cons (string :tag
"Bogus Section")
217 (string :tag
"Real Section")))
220 ;; FIXME see comments at ffap-c-path.
221 (defcustom Man-header-file-path
222 (let ((arch (with-temp-buffer
223 (when (eq 0 (ignore-errors
224 (call-process "gcc" nil
'(t nil
) nil
225 "-print-multiarch")))
226 (goto-char (point-min))
227 (buffer-substring (point) (line-end-position)))))
228 (base '("/usr/include" "/usr/local/include")))
229 (if (zerop (length arch
))
231 (append base
(list (expand-file-name arch
"/usr/include")))))
232 "C Header file search path used in Man."
233 :version
"24.1" ; add multiarch
234 :type
'(repeat string
)
237 (defcustom Man-name-local-regexp
(concat "^" (regexp-opt '("NOM" "NAME")) "$")
238 "Regexp that matches the text that precedes the command's name.
239 Used in `bookmark-set' to get the default bookmark name."
241 :type
'string
:group
'bookmark
)
243 (defcustom manual-program
"man"
244 "Program used by `man' to produce man pages."
248 (defcustom Man-untabify-command
"pr"
249 "Program used by `man' for untabifying."
253 (defcustom Man-untabify-command-args
(list "-t" "-e")
254 "List of arguments to be passed to `Man-untabify-command' (which see)."
255 :type
'(repeat string
)
258 (defcustom Man-sed-command
"sed"
259 "Program used by `man' to process sed scripts."
263 (defcustom Man-awk-command
"awk"
264 "Program used by `man' to process awk scripts."
268 (defcustom Man-mode-hook nil
269 "Hook run when Man mode is enabled."
273 (defcustom Man-cooked-hook nil
274 "Hook run after removing backspaces but before `Man-mode' processing."
278 (defvar Man-name-regexp
"[-a-zA-Z0-9_Â+][-a-zA-Z0-9_.:Â+]*"
279 "Regular expression describing the name of a manpage (without section).")
281 (defvar Man-section-regexp
"[0-9][a-zA-Z0-9+]*\\|[LNln]"
282 "Regular expression describing a manpage section within parentheses.")
284 (defvar Man-page-header-regexp
285 (if (string-match "-solaris2\\." system-configuration
)
286 (concat "^[-A-Za-z0-9_].*[ \t]\\(" Man-name-regexp
287 "(\\(" Man-section-regexp
"\\))\\)$")
288 (concat "^[ \t]*\\(" Man-name-regexp
289 "(\\(" Man-section-regexp
"\\))\\).*\\1"))
290 "Regular expression describing the heading of a page.")
292 (defvar Man-heading-regexp
"^\\([A-Z][A-Z0-9 /-]+\\)$"
293 "Regular expression describing a manpage heading entry.")
295 (defvar Man-see-also-regexp
"SEE ALSO"
296 "Regular expression for SEE ALSO heading (or your equivalent).
297 This regexp should not start with a `^' character.")
299 ;; This used to have leading space [ \t]*, but was removed because it
300 ;; causes false page splits on an occasional NAME with leading space
301 ;; inside a manpage. And `Man-heading-regexp' doesn't have [ \t]* anyway.
302 (defvar Man-first-heading-regexp
"^NAME$\\|^[ \t]*No manual entry fo.*$"
303 "Regular expression describing first heading on a manpage.
304 This regular expression should start with a `^' character.")
306 (defvar Man-reference-regexp
307 (concat "\\(" Man-name-regexp
308 "\\(\n[ \t]+" Man-name-regexp
"\\)*\\)[ \t]*(\\("
309 Man-section-regexp
"\\))")
310 "Regular expression describing a reference to another manpage.")
312 (defvar Man-apropos-regexp
313 (concat "\\\[\\(" Man-name-regexp
"\\)\\\][ \t]*(\\(" Man-section-regexp
"\\))")
314 "Regular expression describing a reference to manpages in \"man -k output\".")
316 (defvar Man-synopsis-regexp
"SYNOPSIS"
317 "Regular expression for SYNOPSIS heading (or your equivalent).
318 This regexp should not start with a `^' character.")
320 (defvar Man-files-regexp
"FILES\\>"
321 ;; Add \> so as not to match mount(8)'s FILESYSTEM INDEPENDENT MOUNT OPTIONS.
322 "Regular expression for FILES heading (or your equivalent).
323 This regexp should not start with a `^' character.")
325 (defvar Man-include-regexp
"#[ \t]*include[ \t]*"
326 "Regular expression describing the #include (directive of cpp).")
328 (defvar Man-file-name-regexp
"[^<>\", \t\n]+"
329 "Regular expression describing <> in #include line (directive of cpp).")
331 (defvar Man-normal-file-prefix-regexp
"[/~$]"
332 "Regular expression describing a file path appeared in FILES section.")
334 (defvar Man-header-regexp
335 (concat "\\(" Man-include-regexp
"\\)"
337 "\\(" Man-file-name-regexp
"\\)"
339 "Regular expression describing references to header files.")
341 (defvar Man-normal-file-regexp
342 (concat Man-normal-file-prefix-regexp Man-file-name-regexp
)
343 "Regular expression describing references to normal files.")
345 ;; This includes the section as an optional part to catch hyphenated
346 ;; references to manpages.
347 (defvar Man-hyphenated-reference-regexp
348 (concat "\\(" Man-name-regexp
"\\)\\((\\(" Man-section-regexp
"\\))\\)?")
349 "Regular expression describing a reference in the SEE ALSO section.")
351 (defcustom Man-switches
""
352 "Switches passed to the man command, as a single string.
353 For example, the -a switch lets you see all the manpages for a
354 specified subject, if your `man' program supports it."
358 (defvar Man-specified-section-option
359 (if (string-match "-solaris[0-9.]*$" system-configuration
)
362 "Option that indicates a specified a manual section name.")
364 (defvar Man-support-local-filenames
'auto-detect
365 "Internal cache for the value of the function `Man-support-local-filenames'.
366 `auto-detect' means the value is not yet determined.
367 Otherwise, the value is whatever the function
368 `Man-support-local-filenames' should return.")
370 (defcustom man-imenu-title
"Contents"
371 "The title to use if man adds a Contents menu to the menubar."
377 ;; other variables and keymap initializations
378 (defvar Man-original-frame
)
379 (make-variable-buffer-local 'Man-original-frame
)
380 (defvar Man-arguments
)
381 (make-variable-buffer-local 'Man-arguments
)
382 (put 'Man-arguments
'permanent-local t
)
384 (defvar Man--sections nil
)
385 (make-variable-buffer-local 'Man--sections
)
386 (defvar Man--refpages nil
)
387 (make-variable-buffer-local 'Man--refpages
)
388 (defvar Man-page-list nil
)
389 (make-variable-buffer-local 'Man-page-list
)
390 (defvar Man-current-page
0)
391 (make-variable-buffer-local 'Man-current-page
)
392 (defvar Man-page-mode-string
"1 of 1")
393 (make-variable-buffer-local 'Man-page-mode-string
)
395 (defconst Man-sysv-sed-script
"\
404 /\e\\[[0-9][0-9]*m/ s///g"
405 "Script for sysV-like sed to nuke backspaces and ANSI codes from manpages.")
407 (defconst Man-berkeley-sed-script
"\
413 s/\\(.\\)\b\\1/\\1/g\\
416 /\e\\[[0-9][0-9]*m/ s///g"
417 "Script for berkeley-like sed to nuke backspaces and ANSI codes from manpages.")
419 (defvar Man-topic-history nil
"Topic read history.")
421 (defvar Man-mode-syntax-table
422 (let ((table (copy-syntax-table (standard-syntax-table))))
423 (modify-syntax-entry ?.
"w" table
)
424 (modify-syntax-entry ?_
"w" table
)
425 (modify-syntax-entry ?
: "w" table
) ; for PDL::Primitive in Perl man pages
427 "Syntax table used in Man mode buffers.")
430 (let ((map (make-sparse-keymap)))
431 (suppress-keymap map
)
432 (set-keymap-parent map button-buffer-map
)
434 (define-key map
[?\S-\
] 'scroll-down-command
)
435 (define-key map
" " 'scroll-up-command
)
436 (define-key map
"\177" 'scroll-down-command
)
437 (define-key map
"n" 'Man-next-section
)
438 (define-key map
"p" 'Man-previous-section
)
439 (define-key map
"\en" 'Man-next-manpage
)
440 (define-key map
"\ep" 'Man-previous-manpage
)
441 (define-key map
">" 'end-of-buffer
)
442 (define-key map
"<" 'beginning-of-buffer
)
443 (define-key map
"." 'beginning-of-buffer
)
444 (define-key map
"r" 'Man-follow-manual-reference
)
445 (define-key map
"g" 'Man-goto-section
)
446 (define-key map
"s" 'Man-goto-see-also-section
)
447 (define-key map
"k" 'Man-kill
)
448 (define-key map
"q" 'Man-quit
)
449 (define-key map
"u" 'Man-update-manpage
)
450 (define-key map
"m" 'man
)
451 ;; Not all the man references get buttons currently. The text in the
452 ;; manual page can contain references to other man pages
453 (define-key map
"\r" 'man-follow
)
454 (define-key map
"?" 'describe-mode
)
456 (easy-menu-define nil map
459 ["Next Section" Man-next-section t
]
460 ["Previous Section" Man-previous-section t
]
461 ["Go To Section..." Man-goto-section t
]
462 ["Go To \"SEE ALSO\" Section" Man-goto-see-also-section
463 :active
(cl-member Man-see-also-regexp Man--sections
464 :test
#'string-match-p
)]
465 ["Follow Reference..." Man-follow-manual-reference
466 :active Man--refpages
467 :help
"Go to a manpage referred to in the \"SEE ALSO\" section"]
469 ["Next Manpage" Man-next-manpage
470 :active
(> (length Man-page-list
) 1)]
471 ["Previous Manpage" Man-previous-manpage
472 :active
(> (length Man-page-list
) 1)]
475 ["Kill Buffer" Man-kill t
]
476 ["Quit" Man-quit t
]))
478 "Keymap for Man mode.")
481 (define-button-type 'Man-abstract-xref-man-page
483 'help-echo
"mouse-2, RET: display this man page"
485 'action
#'Man-xref-button-action
)
487 (defun Man-xref-button-action (button)
488 (let ((target (button-get button
'Man-target-string
)))
490 (button-get button
'func
)
492 (button-label button
))
494 (funcall target
(button-start button
)))
497 (define-button-type 'Man-xref-man-page
498 :supertype
'Man-abstract-xref-man-page
502 (define-button-type 'Man-xref-header-file
503 'action
(lambda (button)
504 (let ((w (button-get button
'Man-target-string
)))
505 (unless (Man-view-header-file w
)
506 (error "Cannot find header file: %s" w
))))
508 'help-echo
"mouse-2: display this header file")
510 (define-button-type 'Man-xref-normal-file
511 'action
(lambda (button)
512 (let ((f (substitute-in-file-name
513 (button-get button
'Man-target-string
))))
514 (if (file-exists-p f
)
515 (if (file-readable-p f
)
517 (error "Cannot read a file: %s" f
))
518 (error "Cannot find a file: %s" f
))))
520 'help-echo
"mouse-2: display this file")
523 ;; ======================================================================
526 (defun Man-init-defvars ()
527 "Used for initializing variables based on display's color support.
528 This is necessary if one wants to dump man.el with Emacs."
530 ;; Avoid possible error in call-process by using a directory that must exist.
531 (let ((default-directory "/"))
534 (Man-fontify-manpage-flag
536 ((eq 0 (call-process Man-sed-command nil nil nil Man-sysv-sed-script
))
538 ((eq 0 (call-process Man-sed-command nil nil nil Man-berkeley-sed-script
))
539 Man-berkeley-sed-script
)
543 (setq Man-filter-list
544 ;; Avoid trailing nil which confuses customize.
548 (if (eq system-type
'windows-nt
)
549 ;; Windows needs ".." quoting, not '..'.
551 "-e \"/Reformatting page. Wait/d\""
552 "-e \"/Reformatting entry. Wait/d\""
553 "-e \"/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d\""
554 "-e \"/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d\""
555 "-e \"/^Printed[ \t][0-9].*[0-9]$/d\""
556 "-e \"/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d\""
557 "-e \"/^[A-Za-z].*Last[ \t]change:/d\""
558 "-e \"/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d\""
559 "-e \"/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d\"")
562 (concat "-e '" Man-sed-script
"'")
564 "-e '/^[\001-\032][\001-\032]*$/d'"
565 "-e '/\e[789]/s///g'"
566 "-e '/Reformatting page. Wait/d'"
567 "-e '/Reformatting entry. Wait/d'"
568 "-e '/^[ \t]*Hewlett-Packard[ \t]Company[ \t]*-[ \t][0-9]*[ \t]-/d'"
569 "-e '/^[ \t]*Hewlett-Packard[ \t]*-[ \t][0-9]*[ \t]-.*$/d'"
570 "-e '/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d'"
571 "-e '/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d'"
572 "-e '/^Printed[ \t][0-9].*[0-9]$/d'"
573 "-e '/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d'"
574 "-e '/^[A-Za-z].*Last[ \t]change:/d'"
575 "-e '/^Sun[ \t]Release[ \t][0-9].*[0-9]$/d'"
576 "-e '/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d'"
577 "-e '/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d'"
579 ;; Windows doesn't support multi-line commands, so don't
581 (unless (eq system-type
'windows-nt
)
586 "BEGIN { blankline=0; anonblank=0; }\n"
587 "/^$/ { if (anonblank==0) next; }\n"
589 "/^$/ { blankline++; next; }\n"
590 "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }\n"
593 (if (not Man-uses-untabify-flag
)
594 ;; The outer list will be stripped off by apply.
597 Man-untabify-command-args
))
601 (defsubst Man-make-page-mode-string
()
602 "Formats part of the mode line for Man mode."
603 (format "%s page %d of %d"
604 (or (nth 2 (nth (1- Man-current-page
) Man-page-list
))
607 (length Man-page-list
)))
609 (defsubst Man-build-man-command
()
610 "Builds the entire background manpage and cleaning command."
611 (let ((command (concat manual-program
" " Man-switches
614 ((string-match "%s" manual-program
) "")
615 ;; Stock MS-DOS shells cannot redirect stderr;
616 ;; `call-process' below sends it to /dev/null,
617 ;; so we don't need `2>' even with DOS shells
618 ;; which do support stderr redirection.
619 ((not (fboundp 'start-process
)) " %s")
620 ((concat " %s 2>" null-device
)))))
621 (flist Man-filter-list
))
622 (while (and flist
(car flist
))
623 (let ((pcom (car (car flist
)))
624 (pargs (cdr (car flist
))))
626 (concat command
" | " pcom
" "
627 (mapconcat (lambda (phrase)
628 (if (not (stringp phrase
))
629 (error "Malformed Man-filter-list"))
632 (setq flist
(cdr flist
))))
636 (defun Man-translate-cleanup (string)
637 "Strip leading, trailing and middle spaces."
638 (when (stringp string
)
639 ;; Strip leading and trailing
640 (if (string-match "^[ \t\f\r\n]*\\(.+[^ \t\f\r\n]\\)" string
)
641 (setq string
(match-string 1 string
)))
643 (setq string
(replace-regexp-in-string "[\t\r\n]" " " string
))
644 (setq string
(replace-regexp-in-string " +" " " string
))
647 (defun Man-translate-references (ref)
648 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style.
649 Leave it as is if already in that style. Possibly downcase and
650 translate the section (see the `Man-downcase-section-letters-flag'
651 and the `Man-section-translations-alist' variables)."
654 (slist Man-section-translations-alist
))
655 (setq ref
(Man-translate-cleanup ref
))
657 ;; "chmod(2V)" case ?
658 ((string-match (concat "^" Man-reference-regexp
"$") ref
)
659 (setq name
(replace-regexp-in-string "[\n\t ]" "" (match-string 1 ref
))
660 section
(match-string 3 ref
)))
662 ((string-match (concat "^\\(" Man-section-regexp
663 "\\) +\\(" Man-name-regexp
"\\)$") ref
)
664 (setq name
(match-string 2 ref
)
665 section
(match-string 1 ref
))))
666 (if (string= name
"")
667 ref
; Return the reference as is
668 (if Man-downcase-section-letters-flag
669 (setq section
(downcase section
)))
671 (let ((s1 (car (car slist
)))
672 (s2 (cdr (car slist
))))
673 (setq slist
(cdr slist
))
674 (if Man-downcase-section-letters-flag
675 (setq s1
(downcase s1
)))
676 (if (not (string= s1 section
)) nil
677 (setq section
(if Man-downcase-section-letters-flag
681 (concat Man-specified-section-option section
" " name
))))
683 (defun Man-support-local-filenames ()
684 "Return non-nil if the man command supports local filenames.
685 Different man programs support this feature in different ways.
686 The default Debian man program (\"man-db\") has a `--local-file'
687 \(or `-l') option for this purpose. The default Red Hat man
688 program has no such option, but interprets any name containing
689 a \"/\" as a local filename. The function returns either `man-db'
691 (if (eq Man-support-local-filenames
'auto-detect
)
692 (setq Man-support-local-filenames
694 (let ((default-directory
695 ;; Ensure that `default-directory' exists and is readable.
696 (if (file-accessible-directory-p default-directory
)
698 (expand-file-name "~/"))))
700 (call-process manual-program nil t nil
"--help")))
701 (cond ((search-backward "--local-file" nil
'move
)
703 ;; This feature seems to be present in at least ver 1.4f,
704 ;; which is about 20 years old.
705 ;; I don't know if this version has an official name?
706 ((looking-at "^man, versione? [1-9]")
708 Man-support-local-filenames
))
711 ;; ======================================================================
712 ;; default man entry: get word near point
714 (defun Man-default-man-entry (&optional pos
)
715 "Guess default manual entry based on the text near position POS.
716 POS defaults to `point'."
717 (let (word start column distance
)
719 (when pos
(goto-char pos
))
721 ;; The default title is the nearest entry-like object before or
723 (if (and (skip-chars-backward " \ta-zA-Z0-9+")
724 (not (zerop (skip-chars-backward "(")))
725 ;; Try to handle the special case where POS is on a
728 (concat "([ \t]*\\(" Man-section-regexp
"\\)[ \t]*)"))
729 ;; We skipped a valid section number backwards, look at
731 (or (and (skip-chars-backward ",; \t")
732 (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:"))))
733 ;; Not a valid entry, move POS after closing paren.
734 (not (setq pos
(match-end 0)))))
735 ;; We have a candidate, make `start' record its starting
738 ;; Otherwise look at char before POS.
740 (if (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:")))
741 ;; Our candidate is just before or around POS.
743 ;; Otherwise record the current column and look backwards.
744 (setq column
(current-column))
745 (skip-chars-backward ",; \t")
746 ;; Record the distance traveled.
747 (setq distance
(- column
(current-column)))
749 (concat "([ \t]*\\(?:" Man-section-regexp
"\\)[ \t]*)"))
750 ;; Skip section number backwards.
751 (goto-char (match-beginning 0))
752 (skip-chars-backward " \t"))
753 (if (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:")))
755 ;; We have a candidate before POS ...
758 (if (and (skip-chars-forward ",; \t")
759 (< (- (current-column) column
) distance
)
760 (looking-at "[-a-zA-Z0-9._+:]"))
761 ;; ... but the one after POS is better.
763 ;; ... and anything after POS is worse.
765 ;; No candidate before POS.
767 (skip-chars-forward ",; \t")
768 (setq start
(point)))))
769 ;; We have found a suitable starting point, try to skip at least
771 (skip-chars-forward "-a-zA-Z0-9._+:")
772 (setq word
(buffer-substring-no-properties start
(point)))
773 ;; If there is a continuation at the end of line, check the
774 ;; following line too, eg:
777 ;; Note: This code gets executed iff our entry is after POS.
778 (when (looking-at "[ \t\r\n]+\\([-a-zA-Z0-9._+:]+\\)([0-9])")
779 (setq word
(concat word
(match-string-no-properties 1)))
780 ;; Make sure the section number gets included by the code below.
781 (goto-char (match-end 1)))
782 (when (string-match "[-._]+$" word
)
783 (setq word
(substring word
0 (match-beginning 0))))
784 ;; The following was commented out since the preceding code
785 ;; should not produce a leading "*" in the first place.
786 ;;; ;; If looking at something like *strcat(... , remove the '*'
787 ;;; (when (string-match "^*" word)
788 ;;; (setq word (substring word 1)))
791 (and (not (string-equal word
""))
792 ;; If looking at something like ioctl(2) or brc(1M),
793 ;; include the section number in the returned value.
795 (concat "[ \t]*([ \t]*\\(" Man-section-regexp
"\\)[ \t]*)"))
796 (format "(%s)" (match-string-no-properties 1)))))))
799 ;; ======================================================================
800 ;; Top level command and background process sentinel
802 ;; For compatibility with older versions.
804 (defalias 'manual-entry
'man
)
806 (defvar Man-completion-cache nil
807 ;; On my machine, "man -k" is so fast that a cache makes no sense,
808 ;; but apparently that's not the case in all cases, so let's add a cache.
809 "Cache of completion table of the form (PREFIX . TABLE).")
811 (defvar Man-man-k-use-anchor
813 (memq system-type
'(gnu gnu
/linux gnu
/kfreebsd
))
814 "If non-nil prepend ^ to the prefix passed to \"man -k\" for completion.
815 The value should be nil if \"man -k ^PREFIX\" may omit some man
816 pages whose names start with PREFIX.
818 Currently, the default value depends on `system-type' and is
819 non-nil where the standard man programs are known to behave
820 properly. Setting the value to nil always gives correct results
821 but computing the list of completions may take a bit longer.")
823 (defun Man-parse-man-k ()
824 "Parse \"man -k\" output and return the list of page names.
826 The current buffer should contain the output of a command of the
827 form \"man -k keyword\", which is traditionally also available with
830 While POSIX man(1p) is a bit vague about what to expect here,
831 this function tries to parse some commonly used formats, which
832 can be described in the following informal way, with square brackets
833 indicating optional parts and whitespace being interpreted
836 foo[, bar [, ...]] [other stuff] (sec) - description
837 foo(sec)[, bar(sec) [, ...]] [other stuff] - description
839 For more details and some regression tests, please see
840 test/automated/man-tests.el in the emacs repository."
841 (goto-char (point-min))
842 ;; See man-tests for data about which systems use which format (hopefully we
843 ;; will be able to simplify the code if/when some of those formats aren't
846 (while (search-forward-regexp "^\\([^ \t,\n]+\\)\\(.*?\\)\
847 \\(?:[ \t]\\(([^ \t,\n]+?)\\)\\)?\\(?:[ \t]+- ?\\(.*\\)\\)?$" nil t
)
848 (let ((section (match-string 3))
849 (description (match-string 4))
850 (bound (match-end 2)))
851 (goto-char (match-end 1))
854 ;; The first regexp grouping may already match the section
855 ;; tacked on to the name, which is ok since for the formats we
856 ;; claim to support the third (non-shy) grouping does not
857 ;; match in this case, i.e., section is nil.
858 (push (propertize (concat (match-string 1) section
)
859 'help-echo description
)
861 (search-forward-regexp "\\=, *\\([^ \t,]+\\)" bound t
)))))
864 (defun Man-completion-table (string pred action
)
866 ;; This ends up returning t for pretty much any string, and hence leads to
867 ;; spurious "complete but not unique" messages. And since `man' doesn't
868 ;; require-match anyway, there's not point being clever.
869 ;;((eq action 'lambda) (not (string-match "([^)]*\\'" string)))
871 ;; Let SPC (minibuffer-complete-word) insert the space.
872 (complete-with-action action
'("-k ") string pred
))
874 (let ((table (cdr Man-completion-cache
))
877 (when (string-match "\\`\\([[:digit:]].*?\\) " string
)
878 (setq section
(match-string 1 string
))
879 (setq prefix
(substring string
(match-end 0))))
880 (unless (and Man-completion-cache
881 (string-prefix-p (car Man-completion-cache
) prefix
))
883 (setq default-directory
"/") ;; in case inherited doesn't exist
884 ;; Actually for my `man' the arg is a regexp.
885 ;; POSIX says it must be ERE and "man-db" seems to agree,
886 ;; whereas under MacOSX it seems to be BRE-style and doesn't
887 ;; accept backslashes at all. Let's not bother to
889 (let ((process-environment (copy-sequence process-environment
)))
890 (setenv "COLUMNS" "999") ;; don't truncate long names
891 ;; manual-program might not even exist. And since it's
892 ;; run differently in Man-getpage-in-background, an error
893 ;; here may not necessarily mean that we'll also get an
896 (call-process manual-program nil
'(t nil
) nil
897 "-k" (concat (when (or Man-man-k-use-anchor
898 (string-equal prefix
""))
901 (setq table
(Man-parse-man-k)))
902 ;; Cache the table for later reuse.
903 (setq Man-completion-cache
(cons prefix table
)))
904 ;; The table may contain false positives since the match is made
905 ;; by "man -k" not just on the manpage's name.
907 (let ((re (concat "(" (regexp-quote section
) ")\\'")))
908 (dolist (comp (prog1 table
(setq table nil
)))
909 (if (string-match re comp
)
910 (push (substring comp
0 (match-beginning 0)) table
)))
911 (completion-table-with-context (concat section
" ") table
913 ;; If the current text looks like a possible section name,
914 ;; then add a completion entry that just adds a space so SPC
915 ;; can be used to insert a space.
916 (if (string-match "\\`[[:digit:]]" string
)
917 (push (concat string
" ") table
))
918 (let ((res (complete-with-action action table string pred
)))
919 ;; In case we're completing to a single name that exists in
920 ;; several sections, the longest prefix will look like "foo(".
921 (if (and (stringp res
)
922 (string-match "([^(]*\\'" res
)
923 ;; In case the paren was already in `prefix', don't
925 (> (match-beginning 0) (length prefix
)))
926 (substring res
0 (match-beginning 0))
930 (defun man (man-args)
931 "Get a Un*x manual page and put it in a buffer.
932 This command is the top-level command in the man package.
933 It runs a Un*x command to retrieve and clean a manpage in the
934 background and places the results in a `Man-mode' browsing
935 buffer. The variable `Man-width' defines the number of columns in
936 formatted manual pages. The buffer is displayed immediately.
937 The variable `Man-notify-method' defines how the buffer is displayed.
938 If a buffer already exists for this man page, it will be displayed
939 without running the man command.
941 For a manpage from a particular section, use either of the
942 following. \"cat(1)\" is how cross-references appear and is
943 passed to man as \"1 cat\".
948 To see manpages from all sections related to a subject, use an
949 \"all pages\" option (which might be \"-a\" if it's not the
950 default), then step through with `Man-next-manpage' (\\<Man-mode-map>\\[Man-next-manpage]) etc.
951 Add to `Man-switches' to make this option permanent.
955 An explicit filename can be given too. Use -l if it might
956 otherwise look like a page name.
961 An \"apropos\" query with -k gives a buffer of matching page
962 names or descriptions. The pattern argument is usually an
963 \"egrep\" style regexp.
968 (list (let* ((default-entry (Man-default-man-entry))
969 ;; ignore case because that's friendly for bizarre
970 ;; caps things like the X11 function names and because
971 ;; "man" itself is case-insensitive on the command line
972 ;; so you're accustomed not to bother about the case
973 ;; ("man -k" is case-insensitive similarly, so the
974 ;; table has everything available to complete)
975 (completion-ignore-case t
)
976 Man-completion-cache
;Don't cache across calls.
977 (input (completing-read
978 (format "Manual entry%s"
979 (if (string= default-entry
"")
981 (format " (default %s): " default-entry
)))
982 'Man-completion-table
983 nil nil nil
'Man-topic-history default-entry
)))
984 (if (string= input
"")
985 (error "No man args given")
988 ;; Possibly translate the "subject(section)" syntax into the
989 ;; "section subject" syntax and possibly downcase the section.
990 (setq man-args
(Man-translate-references man-args
))
992 (Man-getpage-in-background man-args
))
995 (defun man-follow (man-args)
996 "Get a Un*x manual page of the item under point and put it in a buffer."
997 (interactive (list (Man-default-man-entry)))
998 (if (or (not man-args
)
999 (string= man-args
""))
1000 (error "No item under point")
1003 (defmacro Man-start-calling
(&rest body
)
1004 "Start the man command in `body' after setting up the environment"
1005 `(let ((process-environment (copy-sequence process-environment
))
1006 ;; The following is so Awk script gets \n intact
1007 ;; But don't prevent decoding of the outside.
1008 (coding-system-for-write 'raw-text-unix
)
1009 ;; We must decode the output by a coding system that the
1010 ;; system's locale suggests in multibyte mode.
1011 (coding-system-for-read locale-coding-system
)
1012 ;; Avoid possible error by using a directory that always exists.
1014 (if (and (file-directory-p default-directory
)
1015 (not (find-file-name-handler default-directory
1016 'file-directory-p
)))
1019 ;; Prevent any attempt to use display terminal fanciness.
1020 (setenv "TERM" "dumb")
1021 ;; In Debian Woody, at least, we get overlong lines under X
1022 ;; unless COLUMNS or MANWIDTH is set. This isn't a problem on
1023 ;; a tty. man(1) says:
1025 ;; If $MANWIDTH is set, its value is used as the line
1026 ;; length for which manual pages should be formatted.
1027 ;; If it is not set, manual pages will be formatted
1028 ;; with a line length appropriate to the current ter-
1029 ;; minal (using an ioctl(2) if available, the value of
1030 ;; $COLUMNS, or falling back to 80 characters if nei-
1031 ;; ther is available).
1032 (when (or window-system
1033 (not (or (getenv "MANWIDTH") (getenv "COLUMNS"))))
1034 ;; Since the page buffer is displayed beforehand,
1035 ;; we can select its window and get the window/frame width.
1036 (setenv "COLUMNS" (number-to-string
1038 ((and (integerp Man-width
) (> Man-width
0))
1041 (if (window-live-p (get-buffer-window (current-buffer) t
))
1042 (with-selected-window (get-buffer-window (current-buffer) t
)
1046 (if (window-live-p (get-buffer-window (current-buffer) t
))
1047 (with-selected-window (get-buffer-window (current-buffer) t
)
1049 (window-width)))))))
1050 ;; Since man-db 2.4.3-1, man writes plain text with no escape
1051 ;; sequences when stdout is not a tty. In 2.5.0, the following
1052 ;; env-var was added to allow control of this (see Debian Bug#340673).
1053 (setenv "MAN_KEEP_FORMATTING" "1")
1056 (defun Man-getpage-in-background (topic)
1057 "Use TOPIC to build and fire off the manpage and cleaning command.
1058 Return the buffer in which the manpage will appear."
1059 (let* ((man-args topic
)
1060 (bufname (concat "*Man " man-args
"*"))
1061 (buffer (get-buffer bufname
)))
1063 (Man-notify-when-ready buffer
)
1065 (message "Invoking %s %s in the background" manual-program man-args
)
1066 (setq buffer
(generate-new-buffer bufname
))
1067 (with-current-buffer buffer
1068 (Man-notify-when-ready buffer
)
1069 (setq buffer-undo-list t
)
1070 (setq Man-original-frame
(selected-frame))
1071 (setq Man-arguments man-args
)
1073 (setq mode-line-process
1074 (concat " " (propertize (if Man-fontify-manpage-flag
1077 'face
'mode-line-emphasis
)))
1079 (if (fboundp 'start-process
)
1080 (let ((proc (start-process
1081 manual-program buffer
1082 (if (memq system-type
'(cygwin windows-nt
))
1085 shell-command-switch
1086 (format (Man-build-man-command) man-args
))))
1087 (set-process-sentinel proc
'Man-bgproc-sentinel
)
1088 (set-process-filter proc
'Man-bgproc-filter
))
1089 (let* ((inhibit-read-only t
)
1091 (call-process shell-file-name nil
(list buffer nil
) nil
1092 shell-command-switch
1093 (format (Man-build-man-command) man-args
)))
1095 (or (and (numberp exit-status
)
1097 (and (numberp exit-status
)
1099 (format "exited abnormally with code %d"
1101 (setq msg exit-status
))
1102 (if Man-fontify-manpage-flag
1103 (Man-fontify-manpage)
1104 (Man-cleanup-manpage))
1105 (Man-bgproc-sentinel bufname msg
))))))
1108 (defun Man-update-manpage ()
1109 "Reformat current manpage by calling the man command again synchronously."
1111 (when (eq Man-arguments nil
)
1112 ;;this shouldn't happen unless it is not in a Man buffer."
1113 (error "Man-arguments not initialized"))
1114 (let ((old-pos (point))
1115 (text (current-word))
1116 (old-size (buffer-size))
1117 (inhibit-read-only t
)
1118 (buffer-read-only nil
))
1121 (call-process shell-file-name nil
(list (current-buffer) nil
) nil
1122 shell-command-switch
1123 (format (Man-build-man-command) Man-arguments
)))
1124 (if Man-fontify-manpage-flag
1125 (Man-fontify-manpage)
1126 (Man-cleanup-manpage))
1128 ;;restore the point, not strictly right.
1129 (unless (or (eq text nil
) (= old-size
(buffer-size)))
1130 (let ((case-fold-search nil
))
1131 (if (> old-size
(buffer-size))
1132 (search-backward text nil t
))
1133 (search-forward text nil t
)))))
1135 (defun Man-notify-when-ready (man-buffer)
1136 "Notify the user when MAN-BUFFER is ready.
1137 See the variable `Man-notify-method' for the different notification behaviors."
1138 (let ((saved-frame (with-current-buffer man-buffer
1139 Man-original-frame
)))
1140 (pcase Man-notify-method
1142 ;; Since we run asynchronously, perhaps while Emacs is waiting
1143 ;; for input, we must not leave a different buffer current. We
1144 ;; can't rely on the editor command loop to reselect the
1145 ;; selected window's buffer.
1147 (let ((frame (make-frame Man-frame-parameters
)))
1148 (set-window-buffer (frame-selected-window frame
) man-buffer
)
1149 (set-window-dedicated-p (frame-selected-window frame
) t
)
1150 (or (display-multi-frame-p frame
)
1151 (select-frame frame
)))))
1153 (switch-to-buffer man-buffer
))
1155 (and (frame-live-p saved-frame
)
1156 (select-frame saved-frame
))
1157 (pop-to-buffer man-buffer
)
1158 (delete-other-windows))
1160 (and (frame-live-p saved-frame
)
1161 (select-frame saved-frame
))
1162 (pop-to-buffer man-buffer
))
1164 (and (frame-live-p saved-frame
)
1165 (select-frame saved-frame
))
1166 (display-buffer man-buffer
'not-this-window
))
1169 (message "Manual buffer %s is ready" (buffer-name man-buffer
)))
1171 (message "Manual buffer %s is ready" (buffer-name man-buffer
)))
1176 (defun Man-softhyphen-to-minus ()
1177 ;; \255 is SOFT HYPHEN in Latin-N. Versions of Debian man, at
1178 ;; least, emit it even when not in a Latin-N locale.
1179 (unless (eq t
(compare-strings "latin-" 0 nil
1180 current-language-environment
0 6 t
))
1181 (goto-char (point-min))
1183 (if enable-multibyte-characters
1184 (setq str
(string-as-multibyte str
)))
1185 (while (search-forward str nil t
) (replace-match "-")))))
1187 (defun Man-fontify-manpage ()
1188 "Convert overstriking and underlining to the correct fonts.
1189 Same for the ANSI bold and normal escape sequences."
1191 (goto-char (point-min))
1192 ;; Fontify ANSI escapes.
1193 (let ((ansi-color-apply-face-function
1194 (lambda (beg end face
)
1196 (put-text-property beg end
'face face
))))
1197 (ansi-color-map Man-ansi-color-map
))
1198 (ansi-color-apply-on-region (point-min) (point-max)))
1199 ;; Other highlighting.
1200 (let ((buffer-undo-list t
))
1201 (if (< (buffer-size) (position-bytes (point-max)))
1202 ;; Multibyte characters exist.
1204 (goto-char (point-min))
1205 (while (and (search-forward "__\b\b" nil t
) (not (eobp)))
1206 (backward-delete-char 4)
1207 (put-text-property (point) (1+ (point)) 'face
'Man-underline
))
1208 (goto-char (point-min))
1209 (while (search-forward "\b\b__" nil t
)
1210 (backward-delete-char 4)
1211 (put-text-property (1- (point)) (point) 'face
'Man-underline
))))
1212 (goto-char (point-min))
1213 (while (and (search-forward "_\b" nil t
) (not (eobp)))
1214 (backward-delete-char 2)
1215 (put-text-property (point) (1+ (point)) 'face
'Man-underline
))
1216 (goto-char (point-min))
1217 (while (search-forward "\b_" nil t
)
1218 (backward-delete-char 2)
1219 (put-text-property (1- (point)) (point) 'face
'Man-underline
))
1220 (goto-char (point-min))
1221 (while (re-search-forward "\\(.\\)\\(\b+\\1\\)+" nil t
)
1222 (replace-match "\\1")
1223 (put-text-property (1- (point)) (point) 'face
'Man-overstrike
))
1224 (goto-char (point-min))
1225 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t
)
1227 (put-text-property (1- (point)) (point) 'face
'bold
))
1228 (goto-char (point-min))
1229 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t
)
1231 (put-text-property (1- (point)) (point) 'face
'bold
))
1232 ;; When the header is longer than the manpage name, groff tries to
1233 ;; condense it to a shorter line interspersed with ^H. Remove ^H with
1234 ;; their preceding chars (but don't put Man-overstrike). (Bug#5566)
1235 (goto-char (point-min))
1236 (while (re-search-forward ".\b" nil t
) (backward-delete-char 2))
1237 (goto-char (point-min))
1238 ;; Try to recognize common forms of cross references.
1239 (Man-highlight-references)
1240 (Man-softhyphen-to-minus)
1241 (goto-char (point-min))
1242 (while (re-search-forward Man-heading-regexp nil t
)
1243 (put-text-property (match-beginning 0)
1245 'face
'Man-overstrike
))))
1247 (defun Man-highlight-references (&optional xref-man-type
)
1248 "Highlight the references on mouse-over.
1249 References include items in the SEE ALSO section,
1250 header file (#include <foo.h>), and files in FILES.
1251 If optional argument XREF-MAN-TYPE is non-nil, it used as the
1252 button type for items in SEE ALSO section. If it is nil, the
1253 default type, `Man-xref-man-page' is used for the buttons."
1254 ;; `Man-highlight-references' is used from woman.el, too.
1255 ;; woman.el doesn't set `Man-arguments'.
1256 (unless Man-arguments
1257 (setq Man-arguments
""))
1258 (if (string-match "-k " Man-arguments
)
1260 (Man-highlight-references0 nil Man-reference-regexp
1
1261 'Man-default-man-entry
1262 (or xref-man-type
'Man-xref-man-page
))
1263 (Man-highlight-references0 nil Man-apropos-regexp
1
1264 'Man-default-man-entry
1265 (or xref-man-type
'Man-xref-man-page
)))
1266 (Man-highlight-references0 Man-see-also-regexp Man-reference-regexp
1
1267 'Man-default-man-entry
1268 (or xref-man-type
'Man-xref-man-page
))
1269 (Man-highlight-references0 Man-synopsis-regexp Man-header-regexp
0 2
1270 'Man-xref-header-file
)
1271 (Man-highlight-references0 Man-files-regexp Man-normal-file-regexp
0 0
1272 'Man-xref-normal-file
)))
1274 (defun Man-highlight-references0 (start-section regexp button-pos target type
)
1275 ;; Based on `Man-build-references-alist'
1276 (when (or (null start-section
)
1277 (Man-find-section start-section
))
1278 (let ((end (if start-section
1281 (back-to-indentation)
1283 (Man-next-section 1)
1285 (goto-char (point-min))
1287 (while (re-search-forward regexp end t
)
1288 ;; An overlay button is preferable because the underlying text
1289 ;; may have text property highlights (Bug#7881).
1291 (match-beginning button-pos
)
1292 (match-end button-pos
)
1294 'Man-target-string
(cond
1296 (match-string target
))
1301 (defun Man-cleanup-manpage (&optional interactive
)
1302 "Remove overstriking and underlining from the current buffer.
1303 Normally skip any jobs that should have been done by the sed script,
1304 but when called interactively, do those jobs even if the sed
1305 script would have done them."
1307 (if (or interactive
(not Man-sed-script
))
1309 (goto-char (point-min))
1310 (while (search-forward "_\b" nil t
) (backward-delete-char 2))
1311 (goto-char (point-min))
1312 (while (search-forward "\b_" nil t
) (backward-delete-char 2))
1313 (goto-char (point-min))
1314 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t
)
1315 (replace-match "\\1"))
1316 (goto-char (point-min))
1317 (while (re-search-forward "\e\\[[0-9]+m" nil t
) (replace-match ""))
1318 (goto-char (point-min))
1319 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t
) (replace-match "o"))
1321 (goto-char (point-min))
1322 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t
) (replace-match "+"))
1323 ;; When the header is longer than the manpage name, groff tries to
1324 ;; condense it to a shorter line interspersed with ^H. Remove ^H with
1325 ;; their preceding chars (but don't put Man-overstrike). (Bug#5566)
1326 (goto-char (point-min))
1327 (while (re-search-forward ".\b" nil t
) (backward-delete-char 2))
1328 (Man-softhyphen-to-minus))
1330 (defun Man-bgproc-filter (process string
)
1331 "Manpage background process filter.
1332 When manpage command is run asynchronously, PROCESS is the process
1333 object for the manpage command; when manpage command is run
1334 synchronously, PROCESS is the name of the buffer where the manpage
1335 command is run. Second argument STRING is the entire string of output."
1337 (let ((Man-buffer (process-buffer process
)))
1338 (if (null (buffer-name Man-buffer
)) ;; deleted buffer
1339 (set-process-buffer process nil
)
1341 (with-current-buffer Man-buffer
1342 (let ((inhibit-read-only t
)
1343 (beg (marker-position (process-mark process
))))
1351 (line-beginning-position))
1353 (if Man-fontify-manpage-flag
1354 (Man-fontify-manpage)
1355 (Man-cleanup-manpage)))
1356 (set-marker (process-mark process
) (point-max)))))))))
1358 (defun Man-bgproc-sentinel (process msg
)
1359 "Manpage background process sentinel.
1360 When manpage command is run asynchronously, PROCESS is the process
1361 object for the manpage command; when manpage command is run
1362 synchronously, PROCESS is the name of the buffer where the manpage
1363 command is run. Second argument MSG is the exit message of the
1365 (let ((Man-buffer (if (stringp process
) (get-buffer process
)
1366 (process-buffer process
)))
1370 (if (null (buffer-name Man-buffer
)) ;; deleted buffer
1371 (or (stringp process
)
1372 (set-process-buffer process nil
))
1374 (with-current-buffer Man-buffer
1376 (let ((case-fold-search nil
))
1377 (goto-char (point-min))
1378 (cond ((or (looking-at "No \\(manual \\)*entry for")
1379 (looking-at "[^\n]*: nothing appropriate$"))
1380 (setq err-mess
(buffer-substring (point)
1382 (end-of-line) (point)))
1385 ;; "-k foo", successful exit, but no output (from man-db)
1386 ;; ENHANCE-ME: share the check for -k with
1387 ;; `Man-highlight-references'. The \\s- bits here are
1388 ;; meant to allow for multiple options with -k among them.
1389 ((and (string-match "\\(\\`\\|\\s-\\)-k\\s-" Man-arguments
)
1390 (eq (process-status process
) 'exit
)
1391 (= (process-exit-status process
) 0)
1392 (= (point-min) (point-max)))
1393 (setq err-mess
(format "%s: no matches" Man-arguments
)
1396 ((or (stringp process
)
1397 (not (and (eq (process-status process
) 'exit
)
1398 (= (process-exit-status process
) 0))))
1399 (or (zerop (length msg
))
1402 (concat (buffer-name Man-buffer
)
1404 (let ((eos (1- (length msg
))))
1405 (if (= (aref msg eos
) ?
\n)
1406 (substring msg
0 eos
) msg
))))
1407 (goto-char (point-max))
1408 (insert (format "\nprocess %s" msg
))))
1411 (if (window-live-p (get-buffer-window Man-buffer t
))
1412 (quit-restore-window
1413 (get-buffer-window Man-buffer t
) 'kill
)
1414 (kill-buffer Man-buffer
))
1416 (run-hooks 'Man-cooked-hook
)
1418 (Man-build-page-list)
1419 (Man-strip-page-headers)
1423 (if (not Man-page-list
)
1424 (let ((args Man-arguments
))
1425 (if (window-live-p (get-buffer-window (current-buffer) t
))
1426 (quit-restore-window
1427 (get-buffer-window (current-buffer) t
) 'kill
)
1428 (kill-buffer (current-buffer)))
1429 (message "Can't find the %s manpage"
1430 (Man-page-from-arguments args
)))
1432 (if Man-fontify-manpage-flag
1433 (message "%s man page formatted"
1434 (Man-page-from-arguments Man-arguments
))
1435 (message "%s man page cleaned up"
1436 (Man-page-from-arguments Man-arguments
)))
1437 (unless (and (processp process
)
1438 (not (eq (process-status process
) 'exit
)))
1439 (setq mode-line-process nil
))
1440 (set-buffer-modified-p nil
)))))
1443 (message "%s" err-mess
))
1446 (defun Man-page-from-arguments (args)
1447 ;; Skip arguments and only print the page name.
1453 (and (not (string-match "^-" elem
))
1455 (split-string args
" ")))
1459 ;; ======================================================================
1460 ;; set up manual mode in buffer and build alists
1462 (defvar bookmark-make-record-function
)
1464 (put 'Man-mode
'mode-class
'special
)
1466 (define-derived-mode Man-mode fundamental-mode
"Man"
1467 "A mode for browsing Un*x manual pages.
1469 The following man commands are available in the buffer. Try
1470 \"\\[describe-key] <key> RET\" for more information:
1472 \\[man] Prompt to retrieve a new manpage.
1473 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
1474 \\[Man-next-manpage] Jump to next manpage in circular list.
1475 \\[Man-previous-manpage] Jump to previous manpage in circular list.
1476 \\[Man-next-section] Jump to next manpage section.
1477 \\[Man-previous-section] Jump to previous manpage section.
1478 \\[Man-goto-section] Go to a manpage section.
1479 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
1480 \\[Man-quit] Deletes the manpage window, bury its buffer.
1481 \\[Man-kill] Deletes the manpage window, kill its buffer.
1482 \\[describe-mode] Prints this help text.
1484 The following variables may be of some use. Try
1485 \"\\[describe-variable] <variable-name> RET\" for more information:
1487 `Man-notify-method' What happens when manpage is ready to display.
1488 `Man-downcase-section-letters-flag' Force section letters to lower case.
1489 `Man-circular-pages-flag' Treat multiple manpage list as circular.
1490 `Man-section-translations-alist' List of section numbers and their Un*x equiv.
1491 `Man-filter-list' Background manpage filter command.
1492 `Man-mode-map' Keymap bindings for Man mode buffers.
1493 `Man-mode-hook' Normal hook run on entry to Man mode.
1494 `Man-section-regexp' Regexp describing manpage section letters.
1495 `Man-heading-regexp' Regexp describing section headers.
1496 `Man-see-also-regexp' Regexp for SEE ALSO section (or your equiv).
1497 `Man-first-heading-regexp' Regexp for first heading on a manpage.
1498 `Man-reference-regexp' Regexp matching a references in SEE ALSO.
1499 `Man-switches' Background `man' command switches.
1501 The following key bindings are currently in effect in the buffer:
1503 (setq buffer-auto-save-file-name nil
1504 mode-line-buffer-identification
1505 (list (default-value 'mode-line-buffer-identification
)
1506 " {" 'Man-page-mode-string
"}")
1509 (buffer-disable-undo)
1511 (setq imenu-generic-expression
(list (list nil Man-heading-regexp
0)))
1512 (imenu-add-to-menubar man-imenu-title
)
1513 (set (make-local-variable 'outline-regexp
) Man-heading-regexp
)
1514 (set (make-local-variable 'outline-level
) (lambda () 1))
1515 (set (make-local-variable 'bookmark-make-record-function
)
1516 'Man-bookmark-make-record
))
1518 (defsubst Man-build-section-alist
()
1519 "Build the list of manpage sections."
1520 (setq Man--sections nil
)
1521 (goto-char (point-min))
1522 (let ((case-fold-search nil
))
1523 (while (re-search-forward Man-heading-regexp
(point-max) t
)
1524 (let ((section (match-string 1)))
1525 (unless (member section Man--sections
)
1526 (push section Man--sections
)))
1529 (defsubst Man-build-references-alist
()
1530 "Build the list of references (in the SEE ALSO section)."
1531 (setq Man--refpages nil
)
1533 (if (Man-find-section Man-see-also-regexp
)
1534 (let ((start (progn (forward-line 1) (point)))
1536 (Man-next-section 1)
1541 (narrow-to-region start end
)
1542 (goto-char (point-min))
1543 (back-to-indentation)
1544 (while (and (not (eobp)) (/= (point) runningpoint
))
1545 (setq runningpoint
(point))
1546 (if (re-search-forward Man-hyphenated-reference-regexp end t
)
1547 (let* ((word (match-string 0))
1548 (len (1- (length word
))))
1550 (setq word
(concat hyphenated word
)
1552 ;; Update len, in case a reference spans
1553 ;; more than two lines (paranoia).
1554 len
(1- (length word
))))
1555 (if (memq (aref word len
) '(?- ?Â
))
1556 (setq hyphenated
(substring word
0 len
)))
1557 (and (string-match Man-reference-regexp word
)
1558 (not (member word Man--refpages
))
1559 (push word Man--refpages
))))
1560 (skip-chars-forward " \t\n,"))))))
1561 (setq Man--refpages
(nreverse Man--refpages
)))
1563 (defun Man-build-page-list ()
1564 "Build the list of separate manpages in the buffer."
1565 (setq Man-page-list nil
)
1566 (let ((page-start (point-min))
1567 (page-end (point-max))
1569 (goto-char page-start
)
1572 (if (looking-at Man-page-header-regexp
)
1575 ;; Go past both the current and the next Man-first-heading-regexp
1576 (if (re-search-forward Man-first-heading-regexp nil
'move
2)
1577 (let ((p (progn (beginning-of-line) (point))))
1578 ;; We assume that the page header is delimited by blank
1579 ;; lines and that it contains at most one blank line. So
1580 ;; if we back by three blank lines we will be sure to be
1581 ;; before the page header but not before the possible
1582 ;; previous page header.
1583 (search-backward "\n\n" nil t
3)
1584 (if (re-search-forward Man-page-header-regexp p
'move
)
1585 (beginning-of-line))))
1586 (setq page-end
(point))
1587 (setq Man-page-list
(append Man-page-list
1588 (list (list (copy-marker page-start
)
1589 (copy-marker page-end
)
1591 (setq page-start page-end
)
1594 (defun Man-strip-page-headers ()
1595 "Strip all the page headers but the first from the manpage."
1596 (let ((inhibit-read-only t
)
1597 (case-fold-search nil
)
1599 (dolist (page Man-page-list
)
1601 (goto-char (car page
))
1602 (re-search-forward Man-first-heading-regexp nil t
)
1603 (setq header
(buffer-substring (car page
) (match-beginning 0)))
1604 ;; Since the awk script collapses all successive blank
1605 ;; lines into one, and since we don't want to get rid of
1606 ;; the fast awk script, one must choose between adding
1607 ;; spare blank lines between pages when there were none and
1608 ;; deleting blank lines at page boundaries when there were
1609 ;; some. We choose the first, so we comment the following
1611 ;; (setq header (concat "\n" header)))
1612 (while (search-forward header
(nth 1 page
) t
)
1613 (replace-match ""))))))
1615 (defun Man-unindent ()
1616 "Delete the leading spaces that indent the manpage."
1617 (let ((inhibit-read-only t
)
1618 (case-fold-search nil
))
1619 (dolist (page Man-page-list
)
1622 (narrow-to-region (car page
) (car (cdr page
)))
1623 (if Man-uses-untabify-flag
1624 ;; The space characters inserted by `untabify' inherit
1625 ;; sticky text properties, which is unnecessary and looks
1626 ;; ugly with underlining (Bug#11408).
1627 (let ((text-property-default-nonsticky
1628 (cons '(face . t
) text-property-default-nonsticky
)))
1629 (untabify (point-min) (point-max))))
1630 (if (catch 'unindent
1631 (goto-char (point-min))
1632 (if (not (re-search-forward Man-first-heading-regexp nil t
))
1633 (throw 'unindent nil
))
1635 (setq indent
(buffer-substring (point)
1637 (skip-chars-forward " ")
1639 (setq nindent
(length indent
))
1641 (throw 'unindent nil
))
1642 (setq indent
(concat indent
"\\|$"))
1643 (goto-char (point-min))
1645 (if (looking-at indent
)
1647 (throw 'unindent nil
)))
1648 (goto-char (point-min)))
1651 (delete-char nindent
))
1656 ;; ======================================================================
1657 ;; Man mode commands
1659 (defun Man-next-section (n)
1660 "Move point to Nth next section (default 1)."
1662 (let ((case-fold-search nil
)
1664 (if (looking-at Man-heading-regexp
)
1666 (if (re-search-forward Man-heading-regexp
(point-max) t n
)
1668 (goto-char (point-max))
1669 ;; The last line doesn't belong to any section.
1671 ;; But don't move back from the starting point (can happen if `start'
1672 ;; is somewhere on the last line).
1673 (if (< (point) start
) (goto-char start
))))
1675 (defun Man-previous-section (n)
1676 "Move point to Nth previous section (default 1)."
1678 (let ((case-fold-search nil
))
1679 (if (looking-at Man-heading-regexp
)
1681 (if (re-search-backward Man-heading-regexp
(point-min) t n
)
1683 (goto-char (point-min)))))
1685 (defun Man-find-section (section)
1686 "Move point to SECTION if it exists, otherwise don't move point.
1687 Returns t if section is found, nil otherwise."
1688 (let ((curpos (point))
1689 (case-fold-search nil
))
1690 (goto-char (point-min))
1691 (if (re-search-forward (concat "^" section
) (point-max) t
)
1692 (progn (beginning-of-line) t
)
1697 (defvar Man--last-section nil
)
1699 (defun Man-goto-section (section)
1700 "Move point to SECTION."
1702 (let* ((default (if (member Man--last-section Man--sections
)
1704 (car Man--sections
)))
1705 (completion-ignore-case t
)
1706 (prompt (concat "Go to section (default " default
"): "))
1707 (chosen (completing-read prompt Man--sections
1708 nil nil nil nil default
)))
1710 (setq Man--last-section section
)
1711 (unless (Man-find-section section
)
1712 (error "Section %s not found" section
)))
1715 (defun Man-goto-see-also-section ()
1716 "Move point to the \"SEE ALSO\" section.
1717 Actually the section moved to is described by `Man-see-also-regexp'."
1719 (if (not (Man-find-section Man-see-also-regexp
))
1720 (error "%s" (concat "No " Man-see-also-regexp
1721 " section found in the current manpage"))))
1723 (defun Man-possibly-hyphenated-word ()
1724 "Return a possibly hyphenated word at point.
1725 If the word starts at the first non-whitespace column, and the
1726 previous line ends with a hyphen, return the last word on the previous
1727 line instead. Thus, if a reference to \"tcgetpgrp(3V)\" is hyphenated
1728 as \"tcgetp-grp(3V)\", and point is at \"grp(3V)\", we return
1729 \"tcgetp-\" instead of \"grp\"."
1731 (skip-syntax-backward "w()")
1732 (skip-chars-forward " \t")
1734 (word (current-word)))
1735 (when (eq beg
(save-excursion
1736 (back-to-indentation)
1739 (if (eq (char-before) ?-
)
1740 (setq word
(current-word))))
1743 (defvar Man--last-refpage nil
)
1745 (defun Man-follow-manual-reference (reference)
1746 "Get one of the manpages referred to in the \"SEE ALSO\" section.
1747 Specify which REFERENCE to use; default is based on word at point."
1749 (if (not Man--refpages
)
1750 (error "There are no references in the current man page")
1753 (car (all-completions
1755 (or (Man-possibly-hyphenated-word)
1757 ;; strip a trailing '-':
1758 (if (string-match "-$" word
)
1760 (match-beginning 0))
1763 (if (member Man--last-refpage Man--refpages
)
1765 (car Man--refpages
))))
1767 (mapcar 'substring-no-properties
1768 (cons default Man--refpages
)))
1769 (prompt (concat "Refer to (default " default
"): "))
1770 (chosen (completing-read prompt Man--refpages
1771 nil nil nil nil defaults
)))
1773 (if (not Man--refpages
)
1774 (error "Can't find any references in the current manpage")
1775 (setq Man--last-refpage reference
)
1776 (Man-getpage-in-background
1777 (Man-translate-references reference
))))
1780 "Kill the buffer containing the manpage."
1785 "Bury the buffer containing the manpage."
1789 (defun Man-goto-page (page &optional noerror
)
1790 "Go to the manual page on page PAGE."
1792 (if (not Man-page-list
)
1793 (error "Not a man page buffer")
1794 (if (= (length Man-page-list
) 1)
1795 (error "You're looking at the only manpage in the buffer")
1796 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1797 (length Man-page-list
)))))))
1798 (if (and (not Man-page-list
) (not noerror
))
1799 (error "Not a man page buffer"))
1802 (> page
(length Man-page-list
)))
1803 (user-error "No manpage %d found" page
))
1804 (let* ((page-range (nth (1- page
) Man-page-list
))
1805 (page-start (car page-range
))
1806 (page-end (car (cdr page-range
))))
1807 (setq Man-current-page page
1808 Man-page-mode-string
(Man-make-page-mode-string))
1810 (goto-char page-start
)
1811 (narrow-to-region page-start page-end
)
1812 (Man-build-section-alist)
1813 (Man-build-references-alist)
1814 (goto-char (point-min)))))
1817 (defun Man-next-manpage ()
1818 "Find the next manpage entry in the buffer."
1820 (if (= (length Man-page-list
) 1)
1821 (error "This is the only manpage in the buffer"))
1822 (if (< Man-current-page
(length Man-page-list
))
1823 (Man-goto-page (1+ Man-current-page
))
1824 (if Man-circular-pages-flag
1826 (error "You're looking at the last manpage in the buffer"))))
1828 (defun Man-previous-manpage ()
1829 "Find the previous manpage entry in the buffer."
1831 (if (= (length Man-page-list
) 1)
1832 (error "This is the only manpage in the buffer"))
1833 (if (> Man-current-page
1)
1834 (Man-goto-page (1- Man-current-page
))
1835 (if Man-circular-pages-flag
1836 (Man-goto-page (length Man-page-list
))
1837 (error "You're looking at the first manpage in the buffer"))))
1839 ;; Header file support
1840 (defun Man-view-header-file (file)
1841 "View a header file specified by FILE from `Man-header-file-path'."
1842 (let ((path Man-header-file-path
)
1845 (setq complete-path
(expand-file-name file
(car path
))
1847 (if (file-readable-p complete-path
)
1848 (progn (view-file complete-path
)
1850 (setq complete-path nil
)))
1853 ;;; Bookmark Man Support
1854 (declare-function bookmark-make-record-default
1855 "bookmark" (&optional no-file no-context posn
))
1856 (declare-function bookmark-prop-get
"bookmark" (bookmark prop
))
1857 (declare-function bookmark-default-handler
"bookmark" (bmk))
1858 (declare-function bookmark-get-bookmark-record
"bookmark" (bmk))
1860 (defun Man-default-bookmark-title ()
1861 "Default bookmark name for Man or WoMan pages.
1862 Uses `Man-name-local-regexp'."
1864 (goto-char (point-min))
1865 (when (re-search-forward Man-name-local-regexp nil t
)
1866 (skip-chars-forward "\n\t ")
1867 (buffer-substring-no-properties (point) (line-end-position)))))
1869 (defun Man-bookmark-make-record ()
1870 "Make a bookmark entry for a Man buffer."
1871 `(,(Man-default-bookmark-title)
1872 ,@(bookmark-make-record-default 'no-file
)
1873 (location .
,(concat "man " Man-arguments
))
1874 (man-args .
,Man-arguments
)
1875 (handler . Man-bookmark-jump
)))
1878 (defun Man-bookmark-jump (bookmark)
1879 "Default bookmark handler for Man buffers."
1880 (let* ((man-args (bookmark-prop-get bookmark
'man-args
))
1881 ;; Let bookmark.el do the window handling.
1882 ;; This let-binding needs to be active during the call to both
1883 ;; Man-getpage-in-background and accept-process-output.
1884 (Man-notify-method 'meek
)
1885 (buf (Man-getpage-in-background man-args
))
1886 (proc (get-buffer-process buf
)))
1887 (while (and proc
(eq (process-status proc
) 'run
))
1888 (accept-process-output proc
))
1889 (bookmark-default-handler
1890 `("" (buffer .
,buf
) .
,(bookmark-get-bookmark-record bookmark
)))))
1893 ;; Init the man package variables, if not already done.
1898 ;;; man.el ends here