1 ;;; man.el --- browse UNIX manual pages -*- coding: utf-8 -*-
3 ;; Copyright (C) 1993-1994, 1996-1997, 2001-2013 Free Software
6 ;; Author: Barry A. Warsaw <bwarsaw@cen.com>
9 ;; Adapted-By: ESR, pot
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This code provides a function, `man', with which you can browse
29 ;; UNIX manual pages. Formatting is done in background so that you
30 ;; can continue to use your Emacs while processing is going on.
32 ;; The mode also supports hypertext-like following of manual page SEE
33 ;; ALSO references, and other features. See below or do `?' in a
34 ;; manual page buffer for details.
36 ;; ========== Credits and History ==========
37 ;; In mid 1991, several people posted some interesting improvements to
38 ;; man.el from the standard Emacs 18.57 distribution. I liked many of
39 ;; these, but wanted everything in one single package, so I decided
40 ;; to incorporate them into a single manual browsing mode. While
41 ;; much of the code here has been rewritten, and some features added,
42 ;; these folks deserve lots of credit for providing the initial
43 ;; excellent packages on which this one is based.
45 ;; Nick Duffek <duffek@chaos.cs.brandeis.edu>, posted a very nice
46 ;; improvement which retrieved and cleaned the manpages in a
47 ;; background process, and which correctly deciphered such options as
50 ;; Eric Rose <erose@jessica.stanford.edu>, submitted manual.el which
51 ;; provided a very nice manual browsing mode.
53 ;; This package was available as `superman.el' from the LCD package
54 ;; for some time before it was accepted into Emacs 19. The entry
55 ;; point and some other names have been changed to make it a drop-in
56 ;; replacement for the old man.el package.
58 ;; Francesco Potortì <pot@cnuce.cnr.it> cleaned it up thoroughly,
59 ;; making it faster, more robust and more tolerant of different
60 ;; systems' man idiosyncrasies.
62 ;; ========== Features ==========
63 ;; + Runs "man" in the background and pipes the results through a
64 ;; series of sed and awk scripts so that all retrieving and cleaning
65 ;; is done in the background. The cleaning commands are configurable.
66 ;; + Syntax is the same as Un*x man
67 ;; + Functionality is the same as Un*x man, including "man -k" and
68 ;; "man <section>", etc.
69 ;; + Provides a manual browsing mode with keybindings for traversing
70 ;; the sections of a manpage, following references in the SEE ALSO
72 ;; + Multiple manpages created with the same man command are put into
73 ;; a narrowed buffer circular list.
75 ;; ============= TODO ===========
76 ;; - Add a command for printing.
77 ;; - The awk script deletes multiple blank lines. This behavior does
78 ;; not allow to understand if there was indeed a blank line at the
79 ;; end or beginning of a page (after the header, or before the
80 ;; footer). A different algorithm should be used. It is easy to
81 ;; compute how many blank lines there are before and after the page
82 ;; headers, and after the page footer. But it is possible to compute
83 ;; the number of blank lines before the page footer by heuristics
84 ;; only. Is it worth doing?
85 ;; - Allow a user option to mean that all the manpages should go in
86 ;; the same buffer, where they can be browsed with M-n and M-p.
96 "Browse UNIX manual pages."
103 (defcustom Man-filter-list nil
104 "Manpage cleaning filter command phrases.
105 This variable contains a list of the following form:
107 '((command-string phrase-string*)*)
109 Each phrase-string is concatenated onto the command-string to form a
110 command filter. The (standard) output (and standard error) of the Un*x
111 man command is piped through each command filter in the order the
112 commands appear in the association list. The final output is placed in
114 :type
'(repeat (list (string :tag
"Command String")
116 (string :tag
"Phrase String"))))
119 (defvar Man-uses-untabify-flag t
120 "Non-nil means use `untabify' instead of `Man-untabify-command'.")
121 (defvar Man-sed-script nil
122 "Script for sed to nuke backspaces and ANSI codes from manpages.")
124 (defcustom Man-fontify-manpage-flag t
125 "Non-nil means make up the manpage with fonts."
129 (defface Man-overstrike
130 '((t (:inherit bold
)))
131 "Face to use when fontifying overstrike."
135 (defface Man-underline
136 '((t (:inherit underline
)))
137 "Face to use when fontifying underlining."
142 '((t (:inherit highlight
)))
143 "Face to use when fontifying reverse video."
147 (defvar Man-ansi-color-map
(let ((ansi-color-faces-vector
148 [ default Man-overstrike default Man-underline
149 Man-underline default default Man-reverse
]))
150 (ansi-color-make-color-map))
151 "The value used here for `ansi-color-map'.")
153 ;; Use the value of the obsolete user option Man-notify, if set.
154 (defcustom Man-notify-method
(if (boundp 'Man-notify
) Man-notify
'friendly
)
155 "Selects the behavior when manpage is ready.
156 This variable may have one of the following values, where (sf) means
157 that the frames are switched, so the manpage is displayed in the frame
158 where the man command was called from:
160 newframe -- put the manpage in its own frame (see `Man-frame-parameters')
161 pushy -- make the manpage the current buffer in the current window
162 bully -- make the manpage the current buffer and only window (sf)
163 aggressive -- make the manpage the current buffer in the other window (sf)
164 friendly -- display manpage in the other window but don't make current (sf)
165 polite -- don't display manpage, but prints message and beep when ready
166 quiet -- like `polite', but don't beep
167 meek -- make no indication that the manpage is ready
169 Any other value of `Man-notify-method' is equivalent to `meek'."
170 :type
'(radio (const newframe
) (const pushy
) (const bully
)
171 (const aggressive
) (const friendly
)
172 (const polite
) (const quiet
) (const meek
))
175 (defcustom Man-width nil
176 "Number of columns for which manual pages should be formatted.
177 If nil, the width of the window selected at the moment of man
178 invocation is used. If non-nil, the width of the frame selected
179 at the moment of man invocation is used. The value also can be a
181 :type
'(choice (const :tag
"Window width" nil
)
182 (const :tag
"Frame width" t
)
183 (integer :tag
"Specific width" :value
65))
186 (defcustom Man-frame-parameters nil
187 "Frame parameter list for creating a new frame for a manual page."
191 (defcustom Man-downcase-section-letters-flag t
192 "Non-nil means letters in sections are converted to lower case.
193 Some Un*x man commands can't handle uppercase letters in sections, for
194 example \"man 2V chmod\", but they are often displayed in the manpage
195 with the upper case letter. When this variable is t, the section
196 letter (e.g., \"2V\") is converted to lowercase (e.g., \"2v\") before
197 being sent to the man background process."
201 (defcustom Man-circular-pages-flag t
202 "Non-nil means the manpage list is treated as circular for traversal."
206 (defcustom Man-section-translations-alist
209 ;; Some systems have a real 3x man section, so let's comment this.
210 ;; '("3X" . "3") ; Xlib man pages
213 "Association list of bogus sections to real section numbers.
214 Some manpages (e.g. the Sun C++ 2.1 manpages) have section numbers in
215 their references which Un*x `man' does not recognize. This
216 association list is used to translate those sections, when found, to
217 the associated section number."
218 :type
'(repeat (cons (string :tag
"Bogus Section")
219 (string :tag
"Real Section")))
222 ;; FIXME see comments at ffap-c-path.
223 (defcustom Man-header-file-path
224 (let ((arch (with-temp-buffer
225 (when (eq 0 (ignore-errors
226 (call-process "gcc" nil
'(t nil
) nil
227 "-print-multiarch")))
228 (goto-char (point-min))
229 (buffer-substring (point) (line-end-position)))))
230 (base '("/usr/include" "/usr/local/include")))
231 (if (zerop (length arch
))
233 (append base
(list (expand-file-name arch
"/usr/include")))))
234 "C Header file search path used in Man."
235 :version
"24.1" ; add multiarch
236 :type
'(repeat string
)
239 (defcustom Man-name-local-regexp
(concat "^" (regexp-opt '("NOM" "NAME")) "$")
240 "Regexp that matches the text that precedes the command's name.
241 Used in `bookmark-set' to get the default bookmark name."
243 :type
'string
:group
'bookmark
)
245 (defcustom manual-program
"man"
246 "Program used by `man' to produce man pages."
250 (defcustom Man-untabify-command
"pr"
251 "Program used by `man' for untabifying."
255 (defcustom Man-untabify-command-args
(list "-t" "-e")
256 "List of arguments to be passed to `Man-untabify-command' (which see)."
257 :type
'(repeat string
)
260 (defcustom Man-sed-command
"sed"
261 "Program used by `man' to process sed scripts."
265 (defcustom Man-awk-command
"awk"
266 "Program used by `man' to process awk scripts."
270 (defcustom Man-mode-hook nil
271 "Hook run when Man mode is enabled."
275 (defcustom Man-cooked-hook nil
276 "Hook run after removing backspaces but before `Man-mode' processing."
280 (defvar Man-name-regexp
"[-a-zA-Z0-9_Â+][-a-zA-Z0-9_.:Â+]*"
281 "Regular expression describing the name of a manpage (without section).")
283 (defvar Man-section-regexp
"[0-9][a-zA-Z0-9+]*\\|[LNln]"
284 "Regular expression describing a manpage section within parentheses.")
286 (defvar Man-page-header-regexp
287 (if (string-match "-solaris2\\." system-configuration
)
288 (concat "^[-A-Za-z0-9_].*[ \t]\\(" Man-name-regexp
289 "(\\(" Man-section-regexp
"\\))\\)$")
290 (concat "^[ \t]*\\(" Man-name-regexp
291 "(\\(" Man-section-regexp
"\\))\\).*\\1"))
292 "Regular expression describing the heading of a page.")
294 (defvar Man-heading-regexp
"^\\([A-Z][A-Z0-9 /-]+\\)$"
295 "Regular expression describing a manpage heading entry.")
297 (defvar Man-see-also-regexp
"SEE ALSO"
298 "Regular expression for SEE ALSO heading (or your equivalent).
299 This regexp should not start with a `^' character.")
301 ;; This used to have leading space [ \t]*, but was removed because it
302 ;; causes false page splits on an occasional NAME with leading space
303 ;; inside a manpage. And `Man-heading-regexp' doesn't have [ \t]* anyway.
304 (defvar Man-first-heading-regexp
"^NAME$\\|^[ \t]*No manual entry fo.*$"
305 "Regular expression describing first heading on a manpage.
306 This regular expression should start with a `^' character.")
308 (defvar Man-reference-regexp
309 (concat "\\(" Man-name-regexp
310 "\\(\n[ \t]+" Man-name-regexp
"\\)*\\)[ \t]*(\\("
311 Man-section-regexp
"\\))")
312 "Regular expression describing a reference to another manpage.")
314 (defvar Man-apropos-regexp
315 (concat "\\\[\\(" Man-name-regexp
"\\)\\\][ \t]*(\\(" Man-section-regexp
"\\))")
316 "Regular expression describing a reference to manpages in \"man -k output\".")
318 (defvar Man-synopsis-regexp
"SYNOPSIS"
319 "Regular expression for SYNOPSIS heading (or your equivalent).
320 This regexp should not start with a `^' character.")
322 (defvar Man-files-regexp
"FILES\\>"
323 ;; Add \> so as not to match mount(8)'s FILESYSTEM INDEPENDENT MOUNT OPTIONS.
324 "Regular expression for FILES heading (or your equivalent).
325 This regexp should not start with a `^' character.")
327 (defvar Man-include-regexp
"#[ \t]*include[ \t]*"
328 "Regular expression describing the #include (directive of cpp).")
330 (defvar Man-file-name-regexp
"[^<>\", \t\n]+"
331 "Regular expression describing <> in #include line (directive of cpp).")
333 (defvar Man-normal-file-prefix-regexp
"[/~$]"
334 "Regular expression describing a file path appeared in FILES section.")
336 (defvar Man-header-regexp
337 (concat "\\(" Man-include-regexp
"\\)"
339 "\\(" Man-file-name-regexp
"\\)"
341 "Regular expression describing references to header files.")
343 (defvar Man-normal-file-regexp
344 (concat Man-normal-file-prefix-regexp Man-file-name-regexp
)
345 "Regular expression describing references to normal files.")
347 ;; This includes the section as an optional part to catch hyphenated
348 ;; references to manpages.
349 (defvar Man-hyphenated-reference-regexp
350 (concat "\\(" Man-name-regexp
"\\)\\((\\(" Man-section-regexp
"\\))\\)?")
351 "Regular expression describing a reference in the SEE ALSO section.")
353 (defcustom Man-switches
""
354 "Switches passed to the man command, as a single string.
355 For example, the -a switch lets you see all the manpages for a
356 specified subject, if your `man' program supports it."
360 (defvar Man-specified-section-option
361 (if (string-match "-solaris[0-9.]*$" system-configuration
)
364 "Option that indicates a specified a manual section name.")
366 (defvar Man-support-local-filenames
'auto-detect
367 "Internal cache for the value of the function `Man-support-local-filenames'.
368 `auto-detect' means the value is not yet determined.
369 Otherwise, the value is whatever the function
370 `Man-support-local-filenames' should return.")
372 (defcustom man-imenu-title
"Contents"
373 "The title to use if man adds a Contents menu to the menubar."
379 ;; other variables and keymap initializations
380 (defvar Man-original-frame
)
381 (make-variable-buffer-local 'Man-original-frame
)
382 (defvar Man-arguments
)
383 (make-variable-buffer-local 'Man-arguments
)
384 (put 'Man-arguments
'permanent-local t
)
386 (defvar Man--sections nil
)
387 (make-variable-buffer-local 'Man--sections
)
388 (defvar Man--refpages nil
)
389 (make-variable-buffer-local 'Man--refpages
)
390 (defvar Man-page-list nil
)
391 (make-variable-buffer-local 'Man-page-list
)
392 (defvar Man-current-page
0)
393 (make-variable-buffer-local 'Man-current-page
)
394 (defvar Man-page-mode-string
"1 of 1")
395 (make-variable-buffer-local 'Man-page-mode-string
)
397 (defconst Man-sysv-sed-script
"\
406 /\e\\[[0-9][0-9]*m/ s///g"
407 "Script for sysV-like sed to nuke backspaces and ANSI codes from manpages.")
409 (defconst Man-berkeley-sed-script
"\
415 s/\\(.\\)\b\\1/\\1/g\\
418 /\e\\[[0-9][0-9]*m/ s///g"
419 "Script for berkeley-like sed to nuke backspaces and ANSI codes from manpages.")
421 (defvar Man-topic-history nil
"Topic read history.")
423 (defvar Man-mode-syntax-table
424 (let ((table (copy-syntax-table (standard-syntax-table))))
425 (modify-syntax-entry ?.
"w" table
)
426 (modify-syntax-entry ?_
"w" table
)
427 (modify-syntax-entry ?
: "w" table
) ; for PDL::Primitive in Perl man pages
429 "Syntax table used in Man mode buffers.")
432 (let ((map (make-sparse-keymap)))
433 (suppress-keymap map
)
434 (set-keymap-parent map button-buffer-map
)
436 (define-key map
[?\S-\
] 'scroll-down-command
)
437 (define-key map
" " 'scroll-up-command
)
438 (define-key map
"\177" 'scroll-down-command
)
439 (define-key map
"n" 'Man-next-section
)
440 (define-key map
"p" 'Man-previous-section
)
441 (define-key map
"\en" 'Man-next-manpage
)
442 (define-key map
"\ep" 'Man-previous-manpage
)
443 (define-key map
">" 'end-of-buffer
)
444 (define-key map
"<" 'beginning-of-buffer
)
445 (define-key map
"." 'beginning-of-buffer
)
446 (define-key map
"r" 'Man-follow-manual-reference
)
447 (define-key map
"g" 'Man-goto-section
)
448 (define-key map
"s" 'Man-goto-see-also-section
)
449 (define-key map
"k" 'Man-kill
)
450 (define-key map
"q" 'Man-quit
)
451 (define-key map
"u" 'Man-update-manpage
)
452 (define-key map
"m" 'man
)
453 ;; Not all the man references get buttons currently. The text in the
454 ;; manual page can contain references to other man pages
455 (define-key map
"\r" 'man-follow
)
456 (define-key map
"?" 'describe-mode
)
458 (easy-menu-define nil map
461 ["Next Section" Man-next-section t
]
462 ["Previous Section" Man-previous-section t
]
463 ["Go To Section..." Man-goto-section t
]
464 ["Go To \"SEE ALSO\" Section" Man-goto-see-also-section
465 :active
(cl-member Man-see-also-regexp Man--sections
466 :test
#'string-match-p
)]
467 ["Follow Reference..." Man-follow-manual-reference
468 :active Man--refpages
469 :help
"Go to a manpage referred to in the \"SEE ALSO\" section"]
471 ["Next Manpage" Man-next-manpage
472 :active
(> (length Man-page-list
) 1)]
473 ["Previous Manpage" Man-previous-manpage
474 :active
(> (length Man-page-list
) 1)]
477 ["Kill Buffer" Man-kill t
]
478 ["Quit" Man-quit t
]))
480 "Keymap for Man mode.")
483 (define-button-type 'Man-abstract-xref-man-page
485 'help-echo
"mouse-2, RET: display this man page"
487 'action
#'Man-xref-button-action
)
489 (defun Man-xref-button-action (button)
490 (let ((target (button-get button
'Man-target-string
)))
492 (button-get button
'func
)
494 (button-label button
))
496 (funcall target
(button-start button
)))
499 (define-button-type 'Man-xref-man-page
500 :supertype
'Man-abstract-xref-man-page
504 (define-button-type 'Man-xref-header-file
505 'action
(lambda (button)
506 (let ((w (button-get button
'Man-target-string
)))
507 (unless (Man-view-header-file w
)
508 (error "Cannot find header file: %s" w
))))
510 'help-echo
"mouse-2: display this header file")
512 (define-button-type 'Man-xref-normal-file
513 'action
(lambda (button)
514 (let ((f (substitute-in-file-name
515 (button-get button
'Man-target-string
))))
516 (if (file-exists-p f
)
517 (if (file-readable-p f
)
519 (error "Cannot read a file: %s" f
))
520 (error "Cannot find a file: %s" f
))))
522 'help-echo
"mouse-2: display this file")
525 ;; ======================================================================
528 (defun Man-init-defvars ()
529 "Used for initializing variables based on display's color support.
530 This is necessary if one wants to dump man.el with Emacs."
532 ;; Avoid possible error in call-process by using a directory that must exist.
533 (let ((default-directory "/"))
536 (Man-fontify-manpage-flag
538 ((eq 0 (call-process Man-sed-command nil nil nil Man-sysv-sed-script
))
540 ((eq 0 (call-process Man-sed-command nil nil nil Man-berkeley-sed-script
))
541 Man-berkeley-sed-script
)
545 (setq Man-filter-list
546 ;; Avoid trailing nil which confuses customize.
550 (if (eq system-type
'windows-nt
)
551 ;; Windows needs ".." quoting, not '..'.
553 "-e \"/Reformatting page. Wait/d\""
554 "-e \"/Reformatting entry. Wait/d\""
555 "-e \"/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d\""
556 "-e \"/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d\""
557 "-e \"/^Printed[ \t][0-9].*[0-9]$/d\""
558 "-e \"/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d\""
559 "-e \"/^[A-Za-z].*Last[ \t]change:/d\""
560 "-e \"/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d\""
561 "-e \"/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d\"")
564 (concat "-e '" Man-sed-script
"'")
566 "-e '/^[\001-\032][\001-\032]*$/d'"
567 "-e '/\e[789]/s///g'"
568 "-e '/Reformatting page. Wait/d'"
569 "-e '/Reformatting entry. Wait/d'"
570 "-e '/^[ \t]*Hewlett-Packard[ \t]Company[ \t]*-[ \t][0-9]*[ \t]-/d'"
571 "-e '/^[ \t]*Hewlett-Packard[ \t]*-[ \t][0-9]*[ \t]-.*$/d'"
572 "-e '/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d'"
573 "-e '/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d'"
574 "-e '/^Printed[ \t][0-9].*[0-9]$/d'"
575 "-e '/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d'"
576 "-e '/^[A-Za-z].*Last[ \t]change:/d'"
577 "-e '/^Sun[ \t]Release[ \t][0-9].*[0-9]$/d'"
578 "-e '/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d'"
579 "-e '/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d'"
581 ;; Windows doesn't support multi-line commands, so don't
583 (unless (eq system-type
'windows-nt
)
588 "BEGIN { blankline=0; anonblank=0; }\n"
589 "/^$/ { if (anonblank==0) next; }\n"
591 "/^$/ { blankline++; next; }\n"
592 "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }\n"
595 (if (not Man-uses-untabify-flag
)
596 ;; The outer list will be stripped off by apply.
599 Man-untabify-command-args
))
603 (defsubst Man-make-page-mode-string
()
604 "Formats part of the mode line for Man mode."
605 (format "%s page %d of %d"
606 (or (nth 2 (nth (1- Man-current-page
) Man-page-list
))
609 (length Man-page-list
)))
611 (defsubst Man-build-man-command
()
612 "Builds the entire background manpage and cleaning command."
613 (let ((command (concat manual-program
" " Man-switches
616 ((string-match "%s" manual-program
) "")
617 ;; Stock MS-DOS shells cannot redirect stderr;
618 ;; `call-process' below sends it to /dev/null,
619 ;; so we don't need `2>' even with DOS shells
620 ;; which do support stderr redirection.
621 ((not (fboundp 'start-process
)) " %s")
622 ((concat " %s 2>" null-device
)))))
623 (flist Man-filter-list
))
624 (while (and flist
(car flist
))
625 (let ((pcom (car (car flist
)))
626 (pargs (cdr (car flist
))))
628 (concat command
" | " pcom
" "
629 (mapconcat (lambda (phrase)
630 (if (not (stringp phrase
))
631 (error "Malformed Man-filter-list"))
634 (setq flist
(cdr flist
))))
638 (defun Man-translate-cleanup (string)
639 "Strip leading, trailing and middle spaces."
640 (when (stringp string
)
641 ;; Strip leading and trailing
642 (if (string-match "^[ \t\f\r\n]*\\(.+[^ \t\f\r\n]\\)" string
)
643 (setq string
(match-string 1 string
)))
645 (setq string
(replace-regexp-in-string "[\t\r\n]" " " string
))
646 (setq string
(replace-regexp-in-string " +" " " string
))
649 (defun Man-translate-references (ref)
650 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style.
651 Leave it as is if already in that style. Possibly downcase and
652 translate the section (see the `Man-downcase-section-letters-flag'
653 and the `Man-section-translations-alist' variables)."
656 (slist Man-section-translations-alist
))
657 (setq ref
(Man-translate-cleanup ref
))
659 ;; "chmod(2V)" case ?
660 ((string-match (concat "^" Man-reference-regexp
"$") ref
)
661 (setq name
(replace-regexp-in-string "[\n\t ]" "" (match-string 1 ref
))
662 section
(match-string 3 ref
)))
664 ((string-match (concat "^\\(" Man-section-regexp
665 "\\) +\\(" Man-name-regexp
"\\)$") ref
)
666 (setq name
(match-string 2 ref
)
667 section
(match-string 1 ref
))))
668 (if (string= name
"")
669 ref
; Return the reference as is
670 (if Man-downcase-section-letters-flag
671 (setq section
(downcase section
)))
673 (let ((s1 (car (car slist
)))
674 (s2 (cdr (car slist
))))
675 (setq slist
(cdr slist
))
676 (if Man-downcase-section-letters-flag
677 (setq s1
(downcase s1
)))
678 (if (not (string= s1 section
)) nil
679 (setq section
(if Man-downcase-section-letters-flag
683 (concat Man-specified-section-option section
" " name
))))
685 (defun Man-support-local-filenames ()
686 "Return non-nil if the man command supports local filenames.
687 Different man programs support this feature in different ways.
688 The default Debian man program (\"man-db\") has a `--local-file'
689 \(or `-l') option for this purpose. The default Red Hat man
690 program has no such option, but interprets any name containing
691 a \"/\" as a local filename. The function returns either `man-db'
693 (if (eq Man-support-local-filenames
'auto-detect
)
694 (setq Man-support-local-filenames
696 (let ((default-directory
697 ;; Ensure that `default-directory' exists and is readable.
698 (if (and (file-directory-p default-directory
)
699 (file-readable-p default-directory
))
701 (expand-file-name "~/"))))
703 (call-process manual-program nil t nil
"--help")))
704 (cond ((search-backward "--local-file" nil
'move
)
706 ;; This feature seems to be present in at least ver 1.4f,
707 ;; which is about 20 years old.
708 ;; I don't know if this version has an official name?
709 ((looking-at "^man, versione? [1-9]")
711 Man-support-local-filenames
))
714 ;; ======================================================================
715 ;; default man entry: get word near point
717 (defun Man-default-man-entry (&optional pos
)
718 "Guess default manual entry based on the text near position POS.
719 POS defaults to `point'."
720 (let (word start column distance
)
722 (when pos
(goto-char pos
))
724 ;; The default title is the nearest entry-like object before or
726 (if (and (skip-chars-backward " \ta-zA-Z0-9+")
727 (not (zerop (skip-chars-backward "(")))
728 ;; Try to handle the special case where POS is on a
731 (concat "([ \t]*\\(" Man-section-regexp
"\\)[ \t]*)"))
732 ;; We skipped a valid section number backwards, look at
734 (or (and (skip-chars-backward ",; \t")
735 (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:"))))
736 ;; Not a valid entry, move POS after closing paren.
737 (not (setq pos
(match-end 0)))))
738 ;; We have a candidate, make `start' record its starting
741 ;; Otherwise look at char before POS.
743 (if (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:")))
744 ;; Our candidate is just before or around POS.
746 ;; Otherwise record the current column and look backwards.
747 (setq column
(current-column))
748 (skip-chars-backward ",; \t")
749 ;; Record the distance traveled.
750 (setq distance
(- column
(current-column)))
752 (concat "([ \t]*\\(?:" Man-section-regexp
"\\)[ \t]*)"))
753 ;; Skip section number backwards.
754 (goto-char (match-beginning 0))
755 (skip-chars-backward " \t"))
756 (if (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:")))
758 ;; We have a candidate before POS ...
761 (if (and (skip-chars-forward ",; \t")
762 (< (- (current-column) column
) distance
)
763 (looking-at "[-a-zA-Z0-9._+:]"))
764 ;; ... but the one after POS is better.
766 ;; ... and anything after POS is worse.
768 ;; No candidate before POS.
770 (skip-chars-forward ",; \t")
771 (setq start
(point)))))
772 ;; We have found a suitable starting point, try to skip at least
774 (skip-chars-forward "-a-zA-Z0-9._+:")
775 (setq word
(buffer-substring-no-properties start
(point)))
776 ;; If there is a continuation at the end of line, check the
777 ;; following line too, eg:
780 ;; Note: This code gets executed iff our entry is after POS.
781 (when (looking-at "[ \t\r\n]+\\([-a-zA-Z0-9._+:]+\\)([0-9])")
782 (setq word
(concat word
(match-string-no-properties 1)))
783 ;; Make sure the section number gets included by the code below.
784 (goto-char (match-end 1)))
785 (when (string-match "[-._]+$" word
)
786 (setq word
(substring word
0 (match-beginning 0))))
787 ;; The following was commented out since the preceding code
788 ;; should not produce a leading "*" in the first place.
789 ;;; ;; If looking at something like *strcat(... , remove the '*'
790 ;;; (when (string-match "^*" word)
791 ;;; (setq word (substring word 1)))
794 (and (not (string-equal word
""))
795 ;; If looking at something like ioctl(2) or brc(1M),
796 ;; include the section number in the returned value.
798 (concat "[ \t]*([ \t]*\\(" Man-section-regexp
"\\)[ \t]*)"))
799 (format "(%s)" (match-string-no-properties 1)))))))
802 ;; ======================================================================
803 ;; Top level command and background process sentinel
805 ;; For compatibility with older versions.
807 (defalias 'manual-entry
'man
)
809 (defvar Man-completion-cache nil
810 ;; On my machine, "man -k" is so fast that a cache makes no sense,
811 ;; but apparently that's not the case in all cases, so let's add a cache.
812 "Cache of completion table of the form (PREFIX . TABLE).")
814 (defvar Man-man-k-use-anchor
816 (memq system-type
'(gnu gnu
/linux gnu
/kfreebsd
))
817 "If non-nil prepend ^ to the prefix passed to \"man -k\" for completion.
818 The value should be nil if \"man -k ^PREFIX\" may omit some man
819 pages whose names start with PREFIX.
821 Currently, the default value depends on `system-type' and is
822 non-nil where the standard man programs are known to behave
823 properly. Setting the value to nil always gives correct results
824 but computing the list of completions may take a bit longer.")
826 (defun Man-parse-man-k ()
827 "Parse \"man -k\" output and return the list of page names.
829 The current buffer should contain the output of a command of the
830 form \"man -k keyword\", which is traditionally also available with
833 While POSIX man(1p) is a bit vague about what to expect here,
834 this function tries to parse some commonly used formats, which
835 can be described in the following informal way, with square brackets
836 indicating optional parts and whitespace being interpreted
839 foo[, bar [, ...]] [other stuff] (sec) - description
840 foo(sec)[, bar(sec) [, ...]] [other stuff] - description
842 For more details and some regression tests, please see
843 test/automated/man-tests.el in the emacs bzr repository."
844 (goto-char (point-min))
845 ;; See man-tests for data about which systems use which format (hopefully we
846 ;; will be able to simplify the code if/when some of those formats aren't
849 (while (search-forward-regexp "^\\([^ \t,\n]+\\)\\(.*?\\)\
850 \\(?:[ \t]\\(([^ \t,\n]+?)\\)\\)?\\(?:[ \t]+- ?\\(.*\\)\\)?$" nil t
)
851 (let ((section (match-string 3))
852 (description (match-string 4))
853 (bound (match-end 2)))
854 (goto-char (match-end 1))
857 ;; The first regexp grouping may already match the section
858 ;; tacked on to the name, which is ok since for the formats we
859 ;; claim to support the third (non-shy) grouping does not
860 ;; match in this case, i.e., section is nil.
861 (push (propertize (concat (match-string 1) section
)
862 'help-echo description
)
864 (search-forward-regexp "\\=, *\\([^ \t,]+\\)" bound t
)))))
867 (defun Man-completion-table (string pred action
)
869 ;; This ends up returning t for pretty much any string, and hence leads to
870 ;; spurious "complete but not unique" messages. And since `man' doesn't
871 ;; require-match anyway, there's not point being clever.
872 ;;((eq action 'lambda) (not (string-match "([^)]*\\'" string)))
874 ;; Let SPC (minibuffer-complete-word) insert the space.
875 (complete-with-action action
'("-k ") string pred
))
877 (let ((table (cdr Man-completion-cache
))
880 (when (string-match "\\`\\([[:digit:]].*?\\) " string
)
881 (setq section
(match-string 1 string
))
882 (setq prefix
(substring string
(match-end 0))))
883 (unless (and Man-completion-cache
884 (string-prefix-p (car Man-completion-cache
) prefix
))
886 (setq default-directory
"/") ;; in case inherited doesn't exist
887 ;; Actually for my `man' the arg is a regexp.
888 ;; POSIX says it must be ERE and "man-db" seems to agree,
889 ;; whereas under MacOSX it seems to be BRE-style and doesn't
890 ;; accept backslashes at all. Let's not bother to
892 (let ((process-environment (copy-sequence process-environment
)))
893 (setenv "COLUMNS" "999") ;; don't truncate long names
894 ;; manual-program might not even exist. And since it's
895 ;; run differently in Man-getpage-in-background, an error
896 ;; here may not necessarily mean that we'll also get an
899 (call-process manual-program nil
'(t nil
) nil
900 "-k" (concat (when (or Man-man-k-use-anchor
901 (string-equal prefix
""))
904 (setq table
(Man-parse-man-k)))
905 ;; Cache the table for later reuse.
906 (setq Man-completion-cache
(cons prefix table
)))
907 ;; The table may contain false positives since the match is made
908 ;; by "man -k" not just on the manpage's name.
910 (let ((re (concat "(" (regexp-quote section
) ")\\'")))
911 (dolist (comp (prog1 table
(setq table nil
)))
912 (if (string-match re comp
)
913 (push (substring comp
0 (match-beginning 0)) table
)))
914 (completion-table-with-context (concat section
" ") table
916 ;; If the current text looks like a possible section name,
917 ;; then add a completion entry that just adds a space so SPC
918 ;; can be used to insert a space.
919 (if (string-match "\\`[[:digit:]]" string
)
920 (push (concat string
" ") table
))
921 (let ((res (complete-with-action action table string pred
)))
922 ;; In case we're completing to a single name that exists in
923 ;; several sections, the longest prefix will look like "foo(".
924 (if (and (stringp res
)
925 (string-match "([^(]*\\'" res
)
926 ;; In case the paren was already in `prefix', don't
928 (> (match-beginning 0) (length prefix
)))
929 (substring res
0 (match-beginning 0))
933 (defun man (man-args)
934 "Get a Un*x manual page and put it in a buffer.
935 This command is the top-level command in the man package. It
936 runs a Un*x command to retrieve and clean a manpage in the
937 background and places the results in a `Man-mode' browsing
938 buffer. See variable `Man-notify-method' for what happens when
939 the buffer is ready. If a buffer already exists for this man
940 page, it will display immediately.
942 For a manpage from a particular section, use either of the
943 following. \"cat(1)\" is how cross-references appear and is
944 passed to man as \"1 cat\".
949 To see manpages from all sections related to a subject, use an
950 \"all pages\" option (which might be \"-a\" if it's not the
951 default), then step through with `Man-next-manpage' (\\<Man-mode-map>\\[Man-next-manpage]) etc.
952 Add to `Man-switches' to make this option permanent.
956 An explicit filename can be given too. Use -l if it might
957 otherwise look like a page name.
962 An \"apropos\" query with -k gives a buffer of matching page
963 names or descriptions. The pattern argument is usually an
964 \"egrep\" style regexp.
969 (list (let* ((default-entry (Man-default-man-entry))
970 ;; ignore case because that's friendly for bizarre
971 ;; caps things like the X11 function names and because
972 ;; "man" itself is case-insensitive on the command line
973 ;; so you're accustomed not to bother about the case
974 ;; ("man -k" is case-insensitive similarly, so the
975 ;; table has everything available to complete)
976 (completion-ignore-case t
)
977 Man-completion-cache
;Don't cache across calls.
978 (input (completing-read
979 (format "Manual entry%s"
980 (if (string= default-entry
"")
982 (format " (default %s): " default-entry
)))
983 'Man-completion-table
984 nil nil nil
'Man-topic-history default-entry
)))
985 (if (string= input
"")
986 (error "No man args given")
989 ;; Possibly translate the "subject(section)" syntax into the
990 ;; "section subject" syntax and possibly downcase the section.
991 (setq man-args
(Man-translate-references man-args
))
993 (Man-getpage-in-background man-args
))
996 (defun man-follow (man-args)
997 "Get a Un*x manual page of the item under point and put it in a buffer."
998 (interactive (list (Man-default-man-entry)))
999 (if (or (not man-args
)
1000 (string= man-args
""))
1001 (error "No item under point")
1004 (defmacro Man-start-calling
(&rest body
)
1005 "Start the man command in `body' after setting up the environment"
1006 `(let ((process-environment (copy-sequence process-environment
))
1007 ;; The following is so Awk script gets \n intact
1008 ;; But don't prevent decoding of the outside.
1009 (coding-system-for-write 'raw-text-unix
)
1010 ;; We must decode the output by a coding system that the
1011 ;; system's locale suggests in multibyte mode.
1012 (coding-system-for-read locale-coding-system
)
1013 ;; Avoid possible error by using a directory that always exists.
1015 (if (and (file-directory-p default-directory
)
1016 (not (find-file-name-handler default-directory
1017 'file-directory-p
)))
1020 ;; Prevent any attempt to use display terminal fanciness.
1021 (setenv "TERM" "dumb")
1022 ;; In Debian Woody, at least, we get overlong lines under X
1023 ;; unless COLUMNS or MANWIDTH is set. This isn't a problem on
1024 ;; a tty. man(1) says:
1026 ;; If $MANWIDTH is set, its value is used as the line
1027 ;; length for which manual pages should be formatted.
1028 ;; If it is not set, manual pages will be formatted
1029 ;; with a line length appropriate to the current ter-
1030 ;; minal (using an ioctl(2) if available, the value of
1031 ;; $COLUMNS, or falling back to 80 characters if nei-
1032 ;; ther is available).
1033 (when (or window-system
1034 (not (or (getenv "MANWIDTH") (getenv "COLUMNS"))))
1035 ;; This isn't strictly correct, since we don't know how
1036 ;; the page will actually be displayed, but it seems
1038 (setenv "COLUMNS" (number-to-string
1040 ((and (integerp Man-width
) (> Man-width
0))
1042 (Man-width (frame-width))
1043 ((window-width))))))
1044 ;; Since man-db 2.4.3-1, man writes plain text with no escape
1045 ;; sequences when stdout is not a tty. In 2.5.0, the following
1046 ;; env-var was added to allow control of this (see Debian Bug#340673).
1047 (setenv "MAN_KEEP_FORMATTING" "1")
1050 (defun Man-getpage-in-background (topic)
1051 "Use TOPIC to build and fire off the manpage and cleaning command.
1052 Return the buffer in which the manpage will appear."
1053 (let* ((man-args topic
)
1054 (bufname (concat "*Man " man-args
"*"))
1055 (buffer (get-buffer bufname
)))
1057 (Man-notify-when-ready buffer
)
1059 (message "Invoking %s %s in the background" manual-program man-args
)
1060 (setq buffer
(generate-new-buffer bufname
))
1061 (with-current-buffer buffer
1062 (setq buffer-undo-list t
)
1063 (setq Man-original-frame
(selected-frame))
1064 (setq Man-arguments man-args
))
1066 (if (fboundp 'start-process
)
1067 (set-process-sentinel
1068 (start-process manual-program buffer
1069 (if (memq system-type
'(cygwin windows-nt
))
1072 shell-command-switch
1073 (format (Man-build-man-command) man-args
))
1074 'Man-bgproc-sentinel
)
1076 (call-process shell-file-name nil
(list buffer nil
) nil
1077 shell-command-switch
1078 (format (Man-build-man-command) man-args
)))
1080 (or (and (numberp exit-status
)
1082 (and (numberp exit-status
)
1084 (format "exited abnormally with code %d"
1086 (setq msg exit-status
))
1087 (Man-bgproc-sentinel bufname msg
)))))
1090 (defun Man-update-manpage ()
1091 "Reformat current manpage by calling the man command again synchronously."
1093 (when (eq Man-arguments nil
)
1094 ;;this shouldn't happen unless it is not in a Man buffer."
1095 (error "Man-arguments not initialized"))
1096 (let ((old-pos (point))
1097 (text (current-word))
1098 (old-size (buffer-size))
1099 (inhibit-read-only t
)
1100 (buffer-read-only nil
))
1103 (call-process shell-file-name nil
(list (current-buffer) nil
) nil
1104 shell-command-switch
1105 (format (Man-build-man-command) Man-arguments
)))
1106 (if Man-fontify-manpage-flag
1107 (Man-fontify-manpage)
1108 (Man-cleanup-manpage))
1110 ;;restore the point, not strictly right.
1111 (unless (or (eq text nil
) (= old-size
(buffer-size)))
1112 (let ((case-fold-search nil
))
1113 (if (> old-size
(buffer-size))
1114 (search-backward text nil t
))
1115 (search-forward text nil t
)))))
1117 (defun Man-notify-when-ready (man-buffer)
1118 "Notify the user when MAN-BUFFER is ready.
1119 See the variable `Man-notify-method' for the different notification behaviors."
1120 (let ((saved-frame (with-current-buffer man-buffer
1121 Man-original-frame
)))
1122 (pcase Man-notify-method
1124 ;; Since we run asynchronously, perhaps while Emacs is waiting
1125 ;; for input, we must not leave a different buffer current. We
1126 ;; can't rely on the editor command loop to reselect the
1127 ;; selected window's buffer.
1129 (let ((frame (make-frame Man-frame-parameters
)))
1130 (set-window-buffer (frame-selected-window frame
) man-buffer
)
1131 (set-window-dedicated-p (frame-selected-window frame
) t
)
1132 (or (display-multi-frame-p frame
)
1133 (select-frame frame
)))))
1135 (switch-to-buffer man-buffer
))
1137 (and (frame-live-p saved-frame
)
1138 (select-frame saved-frame
))
1139 (pop-to-buffer man-buffer
)
1140 (delete-other-windows))
1142 (and (frame-live-p saved-frame
)
1143 (select-frame saved-frame
))
1144 (pop-to-buffer man-buffer
))
1146 (and (frame-live-p saved-frame
)
1147 (select-frame saved-frame
))
1148 (display-buffer man-buffer
'not-this-window
))
1151 (message "Manual buffer %s is ready" (buffer-name man-buffer
)))
1153 (message "Manual buffer %s is ready" (buffer-name man-buffer
)))
1158 (defun Man-softhyphen-to-minus ()
1159 ;; \255 is SOFT HYPHEN in Latin-N. Versions of Debian man, at
1160 ;; least, emit it even when not in a Latin-N locale.
1161 (unless (eq t
(compare-strings "latin-" 0 nil
1162 current-language-environment
0 6 t
))
1163 (goto-char (point-min))
1165 (if enable-multibyte-characters
1166 (setq str
(string-as-multibyte str
)))
1167 (while (search-forward str nil t
) (replace-match "-")))))
1169 (defun Man-fontify-manpage ()
1170 "Convert overstriking and underlining to the correct fonts.
1171 Same for the ANSI bold and normal escape sequences."
1173 (message "Please wait: formatting the %s man page..." Man-arguments
)
1174 (goto-char (point-min))
1175 ;; Fontify ANSI escapes.
1176 (let ((ansi-color-apply-face-function
1177 (lambda (beg end face
)
1179 (put-text-property beg end
'face face
))))
1180 (ansi-color-map Man-ansi-color-map
))
1181 (ansi-color-apply-on-region (point-min) (point-max)))
1182 ;; Other highlighting.
1183 (let ((buffer-undo-list t
))
1184 (if (< (buffer-size) (position-bytes (point-max)))
1185 ;; Multibyte characters exist.
1187 (goto-char (point-min))
1188 (while (search-forward "__\b\b" nil t
)
1189 (backward-delete-char 4)
1190 (put-text-property (point) (1+ (point)) 'face
'Man-underline
))
1191 (goto-char (point-min))
1192 (while (search-forward "\b\b__" nil t
)
1193 (backward-delete-char 4)
1194 (put-text-property (1- (point)) (point) 'face
'Man-underline
))))
1195 (goto-char (point-min))
1196 (while (search-forward "_\b" nil t
)
1197 (backward-delete-char 2)
1198 (put-text-property (point) (1+ (point)) 'face
'Man-underline
))
1199 (goto-char (point-min))
1200 (while (search-forward "\b_" nil t
)
1201 (backward-delete-char 2)
1202 (put-text-property (1- (point)) (point) 'face
'Man-underline
))
1203 (goto-char (point-min))
1204 (while (re-search-forward "\\(.\\)\\(\b+\\1\\)+" nil t
)
1205 (replace-match "\\1")
1206 (put-text-property (1- (point)) (point) 'face
'Man-overstrike
))
1207 (goto-char (point-min))
1208 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t
)
1210 (put-text-property (1- (point)) (point) 'face
'bold
))
1211 (goto-char (point-min))
1212 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t
)
1214 (put-text-property (1- (point)) (point) 'face
'bold
))
1215 ;; When the header is longer than the manpage name, groff tries to
1216 ;; condense it to a shorter line interspersed with ^H. Remove ^H with
1217 ;; their preceding chars (but don't put Man-overstrike). (Bug#5566)
1218 (goto-char (point-min))
1219 (while (re-search-forward ".\b" nil t
) (backward-delete-char 2))
1220 (goto-char (point-min))
1221 ;; Try to recognize common forms of cross references.
1222 (Man-highlight-references)
1223 (Man-softhyphen-to-minus)
1224 (goto-char (point-min))
1225 (while (re-search-forward Man-heading-regexp nil t
)
1226 (put-text-property (match-beginning 0)
1228 'face
'Man-overstrike
)))
1229 (message "%s man page formatted" (Man-page-from-arguments Man-arguments
)))
1231 (defun Man-highlight-references (&optional xref-man-type
)
1232 "Highlight the references on mouse-over.
1233 References include items in the SEE ALSO section,
1234 header file (#include <foo.h>), and files in FILES.
1235 If optional argument XREF-MAN-TYPE is non-nil, it used as the
1236 button type for items in SEE ALSO section. If it is nil, the
1237 default type, `Man-xref-man-page' is used for the buttons."
1238 ;; `Man-highlight-references' is used from woman.el, too.
1239 ;; woman.el doesn't set `Man-arguments'.
1240 (unless Man-arguments
1241 (setq Man-arguments
""))
1242 (if (string-match "-k " Man-arguments
)
1244 (Man-highlight-references0 nil Man-reference-regexp
1
1245 'Man-default-man-entry
1246 (or xref-man-type
'Man-xref-man-page
))
1247 (Man-highlight-references0 nil Man-apropos-regexp
1
1248 'Man-default-man-entry
1249 (or xref-man-type
'Man-xref-man-page
)))
1250 (Man-highlight-references0 Man-see-also-regexp Man-reference-regexp
1
1251 'Man-default-man-entry
1252 (or xref-man-type
'Man-xref-man-page
))
1253 (Man-highlight-references0 Man-synopsis-regexp Man-header-regexp
0 2
1254 'Man-xref-header-file
)
1255 (Man-highlight-references0 Man-files-regexp Man-normal-file-regexp
0 0
1256 'Man-xref-normal-file
)))
1258 (defun Man-highlight-references0 (start-section regexp button-pos target type
)
1259 ;; Based on `Man-build-references-alist'
1260 (when (or (null start-section
)
1261 (Man-find-section start-section
))
1262 (let ((end (if start-section
1265 (back-to-indentation)
1267 (Man-next-section 1)
1269 (goto-char (point-min))
1271 (while (re-search-forward regexp end t
)
1272 ;; An overlay button is preferable because the underlying text
1273 ;; may have text property highlights (Bug#7881).
1275 (match-beginning button-pos
)
1276 (match-end button-pos
)
1278 'Man-target-string
(cond
1280 (match-string target
))
1285 (defun Man-cleanup-manpage (&optional interactive
)
1286 "Remove overstriking and underlining from the current buffer.
1287 Normally skip any jobs that should have been done by the sed script,
1288 but when called interactively, do those jobs even if the sed
1289 script would have done them."
1291 (message "Please wait: cleaning up the %s man page..."
1293 (if (or interactive
(not Man-sed-script
))
1295 (goto-char (point-min))
1296 (while (search-forward "_\b" nil t
) (backward-delete-char 2))
1297 (goto-char (point-min))
1298 (while (search-forward "\b_" nil t
) (backward-delete-char 2))
1299 (goto-char (point-min))
1300 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t
)
1301 (replace-match "\\1"))
1302 (goto-char (point-min))
1303 (while (re-search-forward "\e\\[[0-9]+m" nil t
) (replace-match ""))
1304 (goto-char (point-min))
1305 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t
) (replace-match "o"))
1307 (goto-char (point-min))
1308 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t
) (replace-match "+"))
1309 ;; When the header is longer than the manpage name, groff tries to
1310 ;; condense it to a shorter line interspersed with ^H. Remove ^H with
1311 ;; their preceding chars (but don't put Man-overstrike). (Bug#5566)
1312 (goto-char (point-min))
1313 (while (re-search-forward ".\b" nil t
) (backward-delete-char 2))
1314 (Man-softhyphen-to-minus)
1315 (message "%s man page cleaned up" Man-arguments
))
1317 (defun Man-bgproc-sentinel (process msg
)
1318 "Manpage background process sentinel.
1319 When manpage command is run asynchronously, PROCESS is the process
1320 object for the manpage command; when manpage command is run
1321 synchronously, PROCESS is the name of the buffer where the manpage
1322 command is run. Second argument MSG is the exit message of the
1324 (let ((Man-buffer (if (stringp process
) (get-buffer process
)
1325 (process-buffer process
)))
1329 (if (null (buffer-name Man-buffer
)) ;; deleted buffer
1330 (or (stringp process
)
1331 (set-process-buffer process nil
))
1333 (with-current-buffer Man-buffer
1334 (let ((case-fold-search nil
))
1335 (goto-char (point-min))
1336 (cond ((or (looking-at "No \\(manual \\)*entry for")
1337 (looking-at "[^\n]*: nothing appropriate$"))
1338 (setq err-mess
(buffer-substring (point)
1340 (end-of-line) (point)))
1343 ;; "-k foo", successful exit, but no output (from man-db)
1344 ;; ENHANCE-ME: share the check for -k with
1345 ;; `Man-highlight-references'. The \\s- bits here are
1346 ;; meant to allow for multiple options with -k among them.
1347 ((and (string-match "\\(\\`\\|\\s-\\)-k\\s-" Man-arguments
)
1348 (eq (process-status process
) 'exit
)
1349 (= (process-exit-status process
) 0)
1350 (= (point-min) (point-max)))
1351 (setq err-mess
(format "%s: no matches" Man-arguments
)
1354 ((or (stringp process
)
1355 (not (and (eq (process-status process
) 'exit
)
1356 (= (process-exit-status process
) 0))))
1357 (or (zerop (length msg
))
1360 (concat (buffer-name Man-buffer
)
1362 (let ((eos (1- (length msg
))))
1363 (if (= (aref msg eos
) ?
\n)
1364 (substring msg
0 eos
) msg
))))
1365 (goto-char (point-max))
1366 (insert (format "\nprocess %s" msg
))))
1369 (kill-buffer Man-buffer
)
1370 (if Man-fontify-manpage-flag
1371 (Man-fontify-manpage)
1372 (Man-cleanup-manpage))
1374 (run-hooks 'Man-cooked-hook
)
1377 (if (not Man-page-list
)
1378 (let ((args Man-arguments
))
1379 (kill-buffer (current-buffer))
1380 (user-error "Can't find the %s manpage"
1381 (Man-page-from-arguments args
)))
1382 (set-buffer-modified-p nil
))))
1383 ;; Restore case-fold-search before calling
1384 ;; Man-notify-when-ready because it may switch buffers.
1386 (if (not delete-buff
)
1387 (Man-notify-when-ready Man-buffer
))
1390 (error "%s" err-mess
))
1393 (defun Man-page-from-arguments (args)
1394 ;; Skip arguments and only print the page name.
1400 (and (not (string-match "^-" elem
))
1402 (split-string args
" ")))
1406 ;; ======================================================================
1407 ;; set up manual mode in buffer and build alists
1409 (defvar bookmark-make-record-function
)
1411 (put 'Man-mode
'mode-class
'special
)
1413 (define-derived-mode Man-mode fundamental-mode
"Man"
1414 "A mode for browsing Un*x manual pages.
1416 The following man commands are available in the buffer. Try
1417 \"\\[describe-key] <key> RET\" for more information:
1419 \\[man] Prompt to retrieve a new manpage.
1420 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
1421 \\[Man-next-manpage] Jump to next manpage in circular list.
1422 \\[Man-previous-manpage] Jump to previous manpage in circular list.
1423 \\[Man-next-section] Jump to next manpage section.
1424 \\[Man-previous-section] Jump to previous manpage section.
1425 \\[Man-goto-section] Go to a manpage section.
1426 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
1427 \\[Man-quit] Deletes the manpage window, bury its buffer.
1428 \\[Man-kill] Deletes the manpage window, kill its buffer.
1429 \\[describe-mode] Prints this help text.
1431 The following variables may be of some use. Try
1432 \"\\[describe-variable] <variable-name> RET\" for more information:
1434 `Man-notify-method' What happens when manpage formatting is done.
1435 `Man-downcase-section-letters-flag' Force section letters to lower case.
1436 `Man-circular-pages-flag' Treat multiple manpage list as circular.
1437 `Man-section-translations-alist' List of section numbers and their Un*x equiv.
1438 `Man-filter-list' Background manpage filter command.
1439 `Man-mode-map' Keymap bindings for Man mode buffers.
1440 `Man-mode-hook' Normal hook run on entry to Man mode.
1441 `Man-section-regexp' Regexp describing manpage section letters.
1442 `Man-heading-regexp' Regexp describing section headers.
1443 `Man-see-also-regexp' Regexp for SEE ALSO section (or your equiv).
1444 `Man-first-heading-regexp' Regexp for first heading on a manpage.
1445 `Man-reference-regexp' Regexp matching a references in SEE ALSO.
1446 `Man-switches' Background `man' command switches.
1448 The following key bindings are currently in effect in the buffer:
1450 (setq buffer-auto-save-file-name nil
1451 mode-line-buffer-identification
1452 (list (default-value 'mode-line-buffer-identification
)
1453 " {" 'Man-page-mode-string
"}")
1456 (buffer-disable-undo)
1458 (setq imenu-generic-expression
(list (list nil Man-heading-regexp
0)))
1459 (imenu-add-to-menubar man-imenu-title
)
1460 (set (make-local-variable 'outline-regexp
) Man-heading-regexp
)
1461 (set (make-local-variable 'outline-level
) (lambda () 1))
1462 (set (make-local-variable 'bookmark-make-record-function
)
1463 'Man-bookmark-make-record
)
1464 (Man-build-page-list)
1465 (Man-strip-page-headers)
1467 (Man-goto-page 1 t
))
1469 (defsubst Man-build-section-alist
()
1470 "Build the list of manpage sections."
1471 (setq Man--sections nil
)
1472 (goto-char (point-min))
1473 (let ((case-fold-search nil
))
1474 (while (re-search-forward Man-heading-regexp
(point-max) t
)
1475 (let ((section (match-string 1)))
1476 (unless (member section Man--sections
)
1477 (push section Man--sections
)))
1480 (defsubst Man-build-references-alist
()
1481 "Build the list of references (in the SEE ALSO section)."
1482 (setq Man--refpages nil
)
1484 (if (Man-find-section Man-see-also-regexp
)
1485 (let ((start (progn (forward-line 1) (point)))
1487 (Man-next-section 1)
1492 (narrow-to-region start end
)
1493 (goto-char (point-min))
1494 (back-to-indentation)
1495 (while (and (not (eobp)) (/= (point) runningpoint
))
1496 (setq runningpoint
(point))
1497 (if (re-search-forward Man-hyphenated-reference-regexp end t
)
1498 (let* ((word (match-string 0))
1499 (len (1- (length word
))))
1501 (setq word
(concat hyphenated word
)
1503 ;; Update len, in case a reference spans
1504 ;; more than two lines (paranoia).
1505 len
(1- (length word
))))
1506 (if (memq (aref word len
) '(?- ?Â
))
1507 (setq hyphenated
(substring word
0 len
)))
1508 (and (string-match Man-reference-regexp word
)
1509 (not (member word Man--refpages
))
1510 (push word Man--refpages
))))
1511 (skip-chars-forward " \t\n,"))))))
1512 (setq Man--refpages
(nreverse Man--refpages
)))
1514 (defun Man-build-page-list ()
1515 "Build the list of separate manpages in the buffer."
1516 (setq Man-page-list nil
)
1517 (let ((page-start (point-min))
1518 (page-end (point-max))
1520 (goto-char page-start
)
1521 ;; (switch-to-buffer (current-buffer))(debug)
1524 (if (looking-at Man-page-header-regexp
)
1527 ;; Go past both the current and the next Man-first-heading-regexp
1528 (if (re-search-forward Man-first-heading-regexp nil
'move
2)
1529 (let ((p (progn (beginning-of-line) (point))))
1530 ;; We assume that the page header is delimited by blank
1531 ;; lines and that it contains at most one blank line. So
1532 ;; if we back by three blank lines we will be sure to be
1533 ;; before the page header but not before the possible
1534 ;; previous page header.
1535 (search-backward "\n\n" nil t
3)
1536 (if (re-search-forward Man-page-header-regexp p
'move
)
1537 (beginning-of-line))))
1538 (setq page-end
(point))
1539 (setq Man-page-list
(append Man-page-list
1540 (list (list (copy-marker page-start
)
1541 (copy-marker page-end
)
1543 (setq page-start page-end
)
1546 (defun Man-strip-page-headers ()
1547 "Strip all the page headers but the first from the manpage."
1548 (let ((inhibit-read-only t
)
1549 (case-fold-search nil
)
1551 (dolist (page Man-page-list
)
1553 (goto-char (car page
))
1554 (re-search-forward Man-first-heading-regexp nil t
)
1555 (setq header
(buffer-substring (car page
) (match-beginning 0)))
1556 ;; Since the awk script collapses all successive blank
1557 ;; lines into one, and since we don't want to get rid of
1558 ;; the fast awk script, one must choose between adding
1559 ;; spare blank lines between pages when there were none and
1560 ;; deleting blank lines at page boundaries when there were
1561 ;; some. We choose the first, so we comment the following
1563 ;; (setq header (concat "\n" header)))
1564 (while (search-forward header
(nth 1 page
) t
)
1565 (replace-match ""))))))
1567 (defun Man-unindent ()
1568 "Delete the leading spaces that indent the manpage."
1569 (let ((inhibit-read-only t
)
1570 (case-fold-search nil
))
1571 (dolist (page Man-page-list
)
1574 (narrow-to-region (car page
) (car (cdr page
)))
1575 (if Man-uses-untabify-flag
1576 ;; The space characters inserted by `untabify' inherit
1577 ;; sticky text properties, which is unnecessary and looks
1578 ;; ugly with underlining (Bug#11408).
1579 (let ((text-property-default-nonsticky
1580 (cons '(face . t
) text-property-default-nonsticky
)))
1581 (untabify (point-min) (point-max))))
1582 (if (catch 'unindent
1583 (goto-char (point-min))
1584 (if (not (re-search-forward Man-first-heading-regexp nil t
))
1585 (throw 'unindent nil
))
1587 (setq indent
(buffer-substring (point)
1589 (skip-chars-forward " ")
1591 (setq nindent
(length indent
))
1593 (throw 'unindent nil
))
1594 (setq indent
(concat indent
"\\|$"))
1595 (goto-char (point-min))
1597 (if (looking-at indent
)
1599 (throw 'unindent nil
)))
1600 (goto-char (point-min)))
1603 (delete-char nindent
))
1608 ;; ======================================================================
1609 ;; Man mode commands
1611 (defun Man-next-section (n)
1612 "Move point to Nth next section (default 1)."
1614 (let ((case-fold-search nil
)
1616 (if (looking-at Man-heading-regexp
)
1618 (if (re-search-forward Man-heading-regexp
(point-max) t n
)
1620 (goto-char (point-max))
1621 ;; The last line doesn't belong to any section.
1623 ;; But don't move back from the starting point (can happen if `start'
1624 ;; is somewhere on the last line).
1625 (if (< (point) start
) (goto-char start
))))
1627 (defun Man-previous-section (n)
1628 "Move point to Nth previous section (default 1)."
1630 (let ((case-fold-search nil
))
1631 (if (looking-at Man-heading-regexp
)
1633 (if (re-search-backward Man-heading-regexp
(point-min) t n
)
1635 (goto-char (point-min)))))
1637 (defun Man-find-section (section)
1638 "Move point to SECTION if it exists, otherwise don't move point.
1639 Returns t if section is found, nil otherwise."
1640 (let ((curpos (point))
1641 (case-fold-search nil
))
1642 (goto-char (point-min))
1643 (if (re-search-forward (concat "^" section
) (point-max) t
)
1644 (progn (beginning-of-line) t
)
1649 (defvar Man--last-section nil
)
1651 (defun Man-goto-section (section)
1652 "Move point to SECTION."
1654 (let* ((default (if (member Man--last-section Man--sections
)
1656 (car Man--sections
)))
1657 (completion-ignore-case t
)
1658 (prompt (concat "Go to section (default " default
"): "))
1659 (chosen (completing-read prompt Man--sections
1660 nil nil nil nil default
)))
1662 (setq Man--last-section section
)
1663 (unless (Man-find-section section
)
1664 (error "Section %s not found" section
)))
1667 (defun Man-goto-see-also-section ()
1668 "Move point to the \"SEE ALSO\" section.
1669 Actually the section moved to is described by `Man-see-also-regexp'."
1671 (if (not (Man-find-section Man-see-also-regexp
))
1672 (error "%s" (concat "No " Man-see-also-regexp
1673 " section found in the current manpage"))))
1675 (defun Man-possibly-hyphenated-word ()
1676 "Return a possibly hyphenated word at point.
1677 If the word starts at the first non-whitespace column, and the
1678 previous line ends with a hyphen, return the last word on the previous
1679 line instead. Thus, if a reference to \"tcgetpgrp(3V)\" is hyphenated
1680 as \"tcgetp-grp(3V)\", and point is at \"grp(3V)\", we return
1681 \"tcgetp-\" instead of \"grp\"."
1683 (skip-syntax-backward "w()")
1684 (skip-chars-forward " \t")
1686 (word (current-word)))
1687 (when (eq beg
(save-excursion
1688 (back-to-indentation)
1691 (if (eq (char-before) ?-
)
1692 (setq word
(current-word))))
1695 (defvar Man--last-refpage nil
)
1697 (defun Man-follow-manual-reference (reference)
1698 "Get one of the manpages referred to in the \"SEE ALSO\" section.
1699 Specify which REFERENCE to use; default is based on word at point."
1701 (if (not Man--refpages
)
1702 (error "There are no references in the current man page")
1705 (car (all-completions
1707 (or (Man-possibly-hyphenated-word)
1709 ;; strip a trailing '-':
1710 (if (string-match "-$" word
)
1712 (match-beginning 0))
1715 (if (member Man--last-refpage Man--refpages
)
1717 (car Man--refpages
))))
1719 (mapcar 'substring-no-properties
1720 (cons default Man--refpages
)))
1721 (prompt (concat "Refer to (default " default
"): "))
1722 (chosen (completing-read prompt Man--refpages
1723 nil nil nil nil defaults
)))
1725 (if (not Man--refpages
)
1726 (error "Can't find any references in the current manpage")
1727 (setq Man--last-refpage reference
)
1728 (Man-getpage-in-background
1729 (Man-translate-references reference
))))
1732 "Kill the buffer containing the manpage."
1737 "Bury the buffer containing the manpage."
1741 (defun Man-goto-page (page &optional noerror
)
1742 "Go to the manual page on page PAGE."
1744 (if (not Man-page-list
)
1745 (error "Not a man page buffer")
1746 (if (= (length Man-page-list
) 1)
1747 (error "You're looking at the only manpage in the buffer")
1748 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1749 (length Man-page-list
)))))))
1750 (if (and (not Man-page-list
) (not noerror
))
1751 (error "Not a man page buffer"))
1754 (> page
(length Man-page-list
)))
1755 (user-error "No manpage %d found" page
))
1756 (let* ((page-range (nth (1- page
) Man-page-list
))
1757 (page-start (car page-range
))
1758 (page-end (car (cdr page-range
))))
1759 (setq Man-current-page page
1760 Man-page-mode-string
(Man-make-page-mode-string))
1762 (goto-char page-start
)
1763 (narrow-to-region page-start page-end
)
1764 (Man-build-section-alist)
1765 (Man-build-references-alist)
1766 (goto-char (point-min)))))
1769 (defun Man-next-manpage ()
1770 "Find the next manpage entry in the buffer."
1772 (if (= (length Man-page-list
) 1)
1773 (error "This is the only manpage in the buffer"))
1774 (if (< Man-current-page
(length Man-page-list
))
1775 (Man-goto-page (1+ Man-current-page
))
1776 (if Man-circular-pages-flag
1778 (error "You're looking at the last manpage in the buffer"))))
1780 (defun Man-previous-manpage ()
1781 "Find the previous manpage entry in the buffer."
1783 (if (= (length Man-page-list
) 1)
1784 (error "This is the only manpage in the buffer"))
1785 (if (> Man-current-page
1)
1786 (Man-goto-page (1- Man-current-page
))
1787 (if Man-circular-pages-flag
1788 (Man-goto-page (length Man-page-list
))
1789 (error "You're looking at the first manpage in the buffer"))))
1791 ;; Header file support
1792 (defun Man-view-header-file (file)
1793 "View a header file specified by FILE from `Man-header-file-path'."
1794 (let ((path Man-header-file-path
)
1797 (setq complete-path
(expand-file-name file
(car path
))
1799 (if (file-readable-p complete-path
)
1800 (progn (view-file complete-path
)
1802 (setq complete-path nil
)))
1805 ;;; Bookmark Man Support
1806 (declare-function bookmark-make-record-default
1807 "bookmark" (&optional no-file no-context posn
))
1808 (declare-function bookmark-prop-get
"bookmark" (bookmark prop
))
1809 (declare-function bookmark-default-handler
"bookmark" (bmk))
1810 (declare-function bookmark-get-bookmark-record
"bookmark" (bmk))
1812 (defun Man-default-bookmark-title ()
1813 "Default bookmark name for Man or WoMan pages.
1814 Uses `Man-name-local-regexp'."
1816 (goto-char (point-min))
1817 (when (re-search-forward Man-name-local-regexp nil t
)
1818 (skip-chars-forward "\n\t ")
1819 (buffer-substring-no-properties (point) (line-end-position)))))
1821 (defun Man-bookmark-make-record ()
1822 "Make a bookmark entry for a Man buffer."
1823 `(,(Man-default-bookmark-title)
1824 ,@(bookmark-make-record-default 'no-file
)
1825 (location .
,(concat "man " Man-arguments
))
1826 (man-args .
,Man-arguments
)
1827 (handler . Man-bookmark-jump
)))
1830 (defun Man-bookmark-jump (bookmark)
1831 "Default bookmark handler for Man buffers."
1832 (let* ((man-args (bookmark-prop-get bookmark
'man-args
))
1833 ;; Let bookmark.el do the window handling.
1834 ;; This let-binding needs to be active during the call to both
1835 ;; Man-getpage-in-background and accept-process-output.
1836 (Man-notify-method 'meek
)
1837 (buf (Man-getpage-in-background man-args
))
1838 (proc (get-buffer-process buf
)))
1839 (while (and proc
(eq (process-status proc
) 'run
))
1840 (accept-process-output proc
))
1841 (bookmark-default-handler
1842 `("" (buffer .
,buf
) .
,(bookmark-get-bookmark-record bookmark
)))))
1845 ;; Init the man package variables, if not already done.
1850 ;;; man.el ends here