1 ;;; man.el --- browse UNIX manual pages -*- coding: iso-8859-1 -*-
3 ;; Copyright (C) 1993-1994, 1996-1997, 2001-2012
4 ;; Free Software Foundation, Inc.
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 Potorti` <pot@cnuce.cnr.it> cleaned it up thoroughly,
59 ;; making it faster, more robust and more tolerant of different
60 ;; systems' man idiosyncrasies.
62 ;; ========== Features ==========
63 ;; + Runs "man" in the background and pipes the results through a
64 ;; series of sed and awk scripts so that all retrieving and cleaning
65 ;; is done in the background. The cleaning commands are configurable.
66 ;; + Syntax is the same as Un*x man
67 ;; + Functionality is the same as Un*x man, including "man -k" and
68 ;; "man <section>", etc.
69 ;; + Provides a manual browsing mode with keybindings for traversing
70 ;; the sections of a manpage, following references in the SEE ALSO
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.
94 "Browse UNIX manual pages."
101 (defcustom Man-filter-list nil
102 "Manpage cleaning filter command phrases.
103 This variable contains a list of the following form:
105 '((command-string phrase-string*)*)
107 Each phrase-string is concatenated onto the command-string to form a
108 command filter. The (standard) output (and standard error) of the Un*x
109 man command is piped through each command filter in the order the
110 commands appear in the association list. The final output is placed in
112 :type
'(repeat (list (string :tag
"Command String")
114 (string :tag
"Phrase String"))))
117 (defvar Man-uses-untabify-flag t
118 "Non-nil means use `untabify' instead of `Man-untabify-command'.")
119 (defvar Man-sed-script nil
120 "Script for sed to nuke backspaces and ANSI codes from manpages.")
122 (defcustom Man-fontify-manpage-flag t
123 "Non-nil means make up the manpage with fonts."
127 (defcustom Man-overstrike-face
'bold
128 "Face to use when fontifying overstrike."
132 (defcustom Man-underline-face
'underline
133 "Face to use when fontifying underlining."
137 (defcustom Man-reverse-face
'highlight
138 "Face to use when fontifying reverse video."
142 ;; Use the value of the obsolete user option Man-notify, if set.
143 (defcustom Man-notify-method
(if (boundp 'Man-notify
) Man-notify
'friendly
)
144 "Selects the behavior when manpage is ready.
145 This variable may have one of the following values, where (sf) means
146 that the frames are switched, so the manpage is displayed in the frame
147 where the man command was called from:
149 newframe -- put the manpage in its own frame (see `Man-frame-parameters')
150 pushy -- make the manpage the current buffer in the current window
151 bully -- make the manpage the current buffer and only window (sf)
152 aggressive -- make the manpage the current buffer in the other window (sf)
153 friendly -- display manpage in the other window but don't make current (sf)
154 polite -- don't display manpage, but prints message and beep when ready
155 quiet -- like `polite', but don't beep
156 meek -- make no indication that the manpage is ready
158 Any other value of `Man-notify-method' is equivalent to `meek'."
159 :type
'(radio (const newframe
) (const pushy
) (const bully
)
160 (const aggressive
) (const friendly
)
161 (const polite
) (const quiet
) (const meek
))
164 (defcustom Man-width nil
165 "Number of columns for which manual pages should be formatted.
166 If nil, the width of the window selected at the moment of man
167 invocation is used. If non-nil, the width of the frame selected
168 at the moment of man invocation is used. The value also can be a
170 :type
'(choice (const :tag
"Window width" nil
)
171 (const :tag
"Frame width" t
)
172 (integer :tag
"Specific width" :value
65))
175 (defcustom Man-frame-parameters nil
176 "Frame parameter list for creating a new frame for a manual page."
180 (defcustom Man-downcase-section-letters-flag t
181 "Non-nil means letters in sections are converted to lower case.
182 Some Un*x man commands can't handle uppercase letters in sections, for
183 example \"man 2V chmod\", but they are often displayed in the manpage
184 with the upper case letter. When this variable is t, the section
185 letter (e.g., \"2V\") is converted to lowercase (e.g., \"2v\") before
186 being sent to the man background process."
190 (defcustom Man-circular-pages-flag t
191 "Non-nil means the manpage list is treated as circular for traversal."
195 (defcustom Man-section-translations-alist
198 ;; Some systems have a real 3x man section, so let's comment this.
199 ;; '("3X" . "3") ; Xlib man pages
202 "Association list of bogus sections to real section numbers.
203 Some manpages (e.g. the Sun C++ 2.1 manpages) have section numbers in
204 their references which Un*x `man' does not recognize. This
205 association list is used to translate those sections, when found, to
206 the associated section number."
207 :type
'(repeat (cons (string :tag
"Bogus Section")
208 (string :tag
"Real Section")))
211 ;; FIXME see comments at ffap-c-path.
212 (defcustom Man-header-file-path
213 (let ((arch (with-temp-buffer
214 (when (eq 0 (ignore-errors
215 (call-process "gcc" nil
'(t nil
) nil
216 "-print-multiarch")))
217 (goto-char (point-min))
218 (buffer-substring (point) (line-end-position)))))
219 (base '("/usr/include" "/usr/local/include")))
220 (if (zerop (length arch
))
222 (append base
(list (expand-file-name arch
"/usr/include")))))
223 "C Header file search path used in Man."
224 :version
"24.1" ; add multiarch
225 :type
'(repeat string
)
228 (defcustom Man-name-local-regexp
(concat "^" (regexp-opt '("NOM" "NAME")) "$")
229 "Regexp that matches the text that precedes the command's name.
230 Used in `bookmark-set' to get the default bookmark name."
232 :type
'string
:group
'bookmark
)
234 (defcustom manual-program
"man"
235 "Program used by `man' to produce man pages."
239 (defcustom Man-untabify-command
"pr"
240 "Program used by `man' for untabifying."
244 (defcustom Man-untabify-command-args
(list "-t" "-e")
245 "List of arguments to be passed to `Man-untabify-command' (which see)."
246 :type
'(repeat string
)
249 (defcustom Man-sed-command
"sed"
250 "Program used by `man' to process sed scripts."
254 (defcustom Man-awk-command
"awk"
255 "Program used by `man' to process awk scripts."
259 (defcustom Man-mode-hook nil
260 "Hook run when Man mode is enabled."
264 (defcustom Man-cooked-hook nil
265 "Hook run after removing backspaces but before `Man-mode' processing."
269 (defvar Man-name-regexp
"[-a-zA-Z0-9_Â+][-a-zA-Z0-9_.:Â+]*"
270 "Regular expression describing the name of a manpage (without section).")
272 (defvar Man-section-regexp
"[0-9][a-zA-Z0-9+]*\\|[LNln]"
273 "Regular expression describing a manpage section within parentheses.")
275 (defvar Man-page-header-regexp
276 (if (string-match "-solaris2\\." system-configuration
)
277 (concat "^[-A-Za-z0-9_].*[ \t]\\(" Man-name-regexp
278 "(\\(" Man-section-regexp
"\\))\\)$")
279 (concat "^[ \t]*\\(" Man-name-regexp
280 "(\\(" Man-section-regexp
"\\))\\).*\\1"))
281 "Regular expression describing the heading of a page.")
283 (defvar Man-heading-regexp
"^\\([A-Z][A-Z0-9 /-]+\\)$"
284 "Regular expression describing a manpage heading entry.")
286 (defvar Man-see-also-regexp
"SEE ALSO"
287 "Regular expression for SEE ALSO heading (or your equivalent).
288 This regexp should not start with a `^' character.")
290 ;; This used to have leading space [ \t]*, but was removed because it
291 ;; causes false page splits on an occasional NAME with leading space
292 ;; inside a manpage. And `Man-heading-regexp' doesn't have [ \t]* anyway.
293 (defvar Man-first-heading-regexp
"^NAME$\\|^[ \t]*No manual entry fo.*$"
294 "Regular expression describing first heading on a manpage.
295 This regular expression should start with a `^' character.")
297 (defvar Man-reference-regexp
298 (concat "\\(" Man-name-regexp
299 "\\(\n[ \t]+" Man-name-regexp
"\\)*\\)[ \t]*(\\("
300 Man-section-regexp
"\\))")
301 "Regular expression describing a reference to another manpage.")
303 (defvar Man-apropos-regexp
304 (concat "\\\[\\(" Man-name-regexp
"\\)\\\][ \t]*(\\(" Man-section-regexp
"\\))")
305 "Regular expression describing a reference to manpages in \"man -k output\".")
307 (defvar Man-synopsis-regexp
"SYNOPSIS"
308 "Regular expression for SYNOPSIS heading (or your equivalent).
309 This regexp should not start with a `^' character.")
311 (defvar Man-files-regexp
"FILES\\>"
312 ;; Add \> so as not to match mount(8)'s FILESYSTEM INDEPENDENT MOUNT OPTIONS.
313 "Regular expression for FILES heading (or your equivalent).
314 This regexp should not start with a `^' character.")
316 (defvar Man-include-regexp
"#[ \t]*include[ \t]*"
317 "Regular expression describing the #include (directive of cpp).")
319 (defvar Man-file-name-regexp
"[^<>\", \t\n]+"
320 "Regular expression describing <> in #include line (directive of cpp).")
322 (defvar Man-normal-file-prefix-regexp
"[/~$]"
323 "Regular expression describing a file path appeared in FILES section.")
325 (defvar Man-header-regexp
326 (concat "\\(" Man-include-regexp
"\\)"
328 "\\(" Man-file-name-regexp
"\\)"
330 "Regular expression describing references to header files.")
332 (defvar Man-normal-file-regexp
333 (concat Man-normal-file-prefix-regexp Man-file-name-regexp
)
334 "Regular expression describing references to normal files.")
336 ;; This includes the section as an optional part to catch hyphenated
337 ;; references to manpages.
338 (defvar Man-hyphenated-reference-regexp
339 (concat "\\(" Man-name-regexp
"\\)\\((\\(" Man-section-regexp
"\\))\\)?")
340 "Regular expression describing a reference in the SEE ALSO section.")
342 (defcustom Man-switches
""
343 "Switches passed to the man command, as a single string.
344 For example, the -a switch lets you see all the manpages for a
345 specified subject, if your `man' program supports it."
349 (defvar Man-specified-section-option
350 (if (string-match "-solaris[0-9.]*$" system-configuration
)
353 "Option that indicates a specified a manual section name.")
355 (defvar Man-support-local-filenames
'auto-detect
356 "Internal cache for the value of the function `Man-support-local-filenames'.
357 `auto-detect' means the value is not yet determined.
358 Otherwise, the value is whatever the function
359 `Man-support-local-filenames' should return.")
362 ;; other variables and keymap initializations
363 (defvar Man-original-frame
)
364 (make-variable-buffer-local 'Man-original-frame
)
365 (defvar Man-arguments
)
366 (make-variable-buffer-local 'Man-arguments
)
367 (put 'Man-arguments
'permanent-local t
)
369 (defvar Man--sections nil
)
370 (make-variable-buffer-local 'Man--sections
)
371 (defvar Man--refpages nil
)
372 (make-variable-buffer-local 'Man--refpages
)
373 (defvar Man-page-list nil
)
374 (make-variable-buffer-local 'Man-page-list
)
375 (defvar Man-current-page
0)
376 (make-variable-buffer-local 'Man-current-page
)
377 (defvar Man-page-mode-string
"1 of 1")
378 (make-variable-buffer-local 'Man-page-mode-string
)
380 (defconst Man-sysv-sed-script
"\
389 /\e\\[[0-9][0-9]*m/ s///g"
390 "Script for sysV-like sed to nuke backspaces and ANSI codes from manpages.")
392 (defconst Man-berkeley-sed-script
"\
398 s/\\(.\\)\b\\1/\\1/g\\
401 /\e\\[[0-9][0-9]*m/ s///g"
402 "Script for berkeley-like sed to nuke backspaces and ANSI codes from manpages.")
404 (defvar Man-topic-history nil
"Topic read history.")
406 (defvar man-mode-syntax-table
407 (let ((table (copy-syntax-table (standard-syntax-table))))
408 (modify-syntax-entry ?.
"w" table
)
409 (modify-syntax-entry ?_
"w" table
)
410 (modify-syntax-entry ?
: "w" table
) ; for PDL::Primitive in Perl man pages
412 "Syntax table used in Man mode buffers.")
415 (let ((map (make-sparse-keymap)))
416 (suppress-keymap map
)
417 (set-keymap-parent map button-buffer-map
)
419 (define-key map
" " 'scroll-up-command
)
420 (define-key map
"\177" 'scroll-down-command
)
421 (define-key map
"n" 'Man-next-section
)
422 (define-key map
"p" 'Man-previous-section
)
423 (define-key map
"\en" 'Man-next-manpage
)
424 (define-key map
"\ep" 'Man-previous-manpage
)
425 (define-key map
">" 'end-of-buffer
)
426 (define-key map
"<" 'beginning-of-buffer
)
427 (define-key map
"." 'beginning-of-buffer
)
428 (define-key map
"r" 'Man-follow-manual-reference
)
429 (define-key map
"g" 'Man-goto-section
)
430 (define-key map
"s" 'Man-goto-see-also-section
)
431 (define-key map
"k" 'Man-kill
)
432 (define-key map
"q" 'Man-quit
)
433 (define-key map
"m" 'man
)
434 ;; Not all the man references get buttons currently. The text in the
435 ;; manual page can contain references to other man pages
436 (define-key map
"\r" 'man-follow
)
437 (define-key map
"?" 'describe-mode
)
439 "Keymap for Man mode.")
442 (define-button-type 'Man-abstract-xref-man-page
444 'help-echo
"mouse-2, RET: display this man page"
446 'action
#'Man-xref-button-action
)
448 (defun Man-xref-button-action (button)
449 (let ((target (button-get button
'Man-target-string
)))
451 (button-get button
'func
)
453 (button-label button
))
455 (funcall target
(button-start button
)))
458 (define-button-type 'Man-xref-man-page
459 :supertype
'Man-abstract-xref-man-page
463 (define-button-type 'Man-xref-header-file
464 'action
(lambda (button)
465 (let ((w (button-get button
'Man-target-string
)))
466 (unless (Man-view-header-file w
)
467 (error "Cannot find header file: %s" w
))))
469 'help-echo
"mouse-2: display this header file")
471 (define-button-type 'Man-xref-normal-file
472 'action
(lambda (button)
473 (let ((f (substitute-in-file-name
474 (button-get button
'Man-target-string
))))
475 (if (file-exists-p f
)
476 (if (file-readable-p f
)
478 (error "Cannot read a file: %s" f
))
479 (error "Cannot find a file: %s" f
))))
481 'help-echo
"mouse-2: display this file")
484 ;; ======================================================================
487 (defun Man-init-defvars ()
488 "Used for initializing variables based on display's color support.
489 This is necessary if one wants to dump man.el with Emacs."
491 ;; Avoid possible error in call-process by using a directory that must exist.
492 (let ((default-directory "/"))
495 (Man-fontify-manpage-flag
497 ((eq 0 (call-process Man-sed-command nil nil nil Man-sysv-sed-script
))
499 ((eq 0 (call-process Man-sed-command nil nil nil Man-berkeley-sed-script
))
500 Man-berkeley-sed-script
)
504 (setq Man-filter-list
505 ;; Avoid trailing nil which confuses customize.
509 (if (eq system-type
'windows-nt
)
510 ;; Windows needs ".." quoting, not '..'.
512 "-e \"/Reformatting page. Wait/d\""
513 "-e \"/Reformatting entry. Wait/d\""
514 "-e \"/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d\""
515 "-e \"/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d\""
516 "-e \"/^Printed[ \t][0-9].*[0-9]$/d\""
517 "-e \"/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d\""
518 "-e \"/^[A-Za-z].*Last[ \t]change:/d\""
519 "-e \"/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d\""
520 "-e \"/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d\"")
523 (concat "-e '" Man-sed-script
"'")
525 "-e '/^[\001-\032][\001-\032]*$/d'"
526 "-e '/\e[789]/s///g'"
527 "-e '/Reformatting page. Wait/d'"
528 "-e '/Reformatting entry. Wait/d'"
529 "-e '/^[ \t]*Hewlett-Packard[ \t]Company[ \t]*-[ \t][0-9]*[ \t]-/d'"
530 "-e '/^[ \t]*Hewlett-Packard[ \t]*-[ \t][0-9]*[ \t]-.*$/d'"
531 "-e '/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d'"
532 "-e '/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d'"
533 "-e '/^Printed[ \t][0-9].*[0-9]$/d'"
534 "-e '/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d'"
535 "-e '/^[A-Za-z].*Last[ \t]change:/d'"
536 "-e '/^Sun[ \t]Release[ \t][0-9].*[0-9]$/d'"
537 "-e '/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d'"
538 "-e '/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d'"
540 ;; Windows doesn't support multi-line commands, so don't
542 (unless (eq system-type
'windows-nt
)
547 "BEGIN { blankline=0; anonblank=0; }\n"
548 "/^$/ { if (anonblank==0) next; }\n"
550 "/^$/ { blankline++; next; }\n"
551 "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }\n"
554 (if (not Man-uses-untabify-flag
)
555 ;; The outer list will be stripped off by apply.
558 Man-untabify-command-args
))
562 (defsubst Man-make-page-mode-string
()
563 "Formats part of the mode line for Man mode."
564 (format "%s page %d of %d"
565 (or (nth 2 (nth (1- Man-current-page
) Man-page-list
))
568 (length Man-page-list
)))
570 (defsubst Man-build-man-command
()
571 "Builds the entire background manpage and cleaning command."
572 (let ((command (concat manual-program
" " Man-switches
575 ((string-match "%s" manual-program
) "")
576 ;; Stock MS-DOS shells cannot redirect stderr;
577 ;; `call-process' below sends it to /dev/null,
578 ;; so we don't need `2>' even with DOS shells
579 ;; which do support stderr redirection.
580 ((not (fboundp 'start-process
)) " %s")
581 ((concat " %s 2>" null-device
)))))
582 (flist Man-filter-list
))
583 (while (and flist
(car flist
))
584 (let ((pcom (car (car flist
)))
585 (pargs (cdr (car flist
))))
587 (concat command
" | " pcom
" "
588 (mapconcat (lambda (phrase)
589 (if (not (stringp phrase
))
590 (error "Malformed Man-filter-list"))
593 (setq flist
(cdr flist
))))
597 (defun Man-translate-cleanup (string)
598 "Strip leading, trailing and middle spaces."
599 (when (stringp string
)
600 ;; Strip leading and trailing
601 (if (string-match "^[ \t\f\r\n]*\\(.+[^ \t\f\r\n]\\)" string
)
602 (setq string
(match-string 1 string
)))
604 (setq string
(replace-regexp-in-string "[\t\r\n]" " " string
))
605 (setq string
(replace-regexp-in-string " +" " " string
))
608 (defun Man-translate-references (ref)
609 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style.
610 Leave it as is if already in that style. Possibly downcase and
611 translate the section (see the `Man-downcase-section-letters-flag'
612 and the `Man-section-translations-alist' variables)."
615 (slist Man-section-translations-alist
))
616 (setq ref
(Man-translate-cleanup ref
))
618 ;; "chmod(2V)" case ?
619 ((string-match (concat "^" Man-reference-regexp
"$") ref
)
620 (setq name
(replace-regexp-in-string "[\n\t ]" "" (match-string 1 ref
))
621 section
(match-string 3 ref
)))
623 ((string-match (concat "^\\(" Man-section-regexp
624 "\\) +\\(" Man-name-regexp
"\\)$") ref
)
625 (setq name
(match-string 2 ref
)
626 section
(match-string 1 ref
))))
627 (if (string= name
"")
628 ref
; Return the reference as is
629 (if Man-downcase-section-letters-flag
630 (setq section
(downcase section
)))
632 (let ((s1 (car (car slist
)))
633 (s2 (cdr (car slist
))))
634 (setq slist
(cdr slist
))
635 (if Man-downcase-section-letters-flag
636 (setq s1
(downcase s1
)))
637 (if (not (string= s1 section
)) nil
638 (setq section
(if Man-downcase-section-letters-flag
642 (concat Man-specified-section-option section
" " name
))))
644 (defun Man-support-local-filenames ()
645 "Return non-nil if the man command supports local filenames.
646 Different man programs support this feature in different ways.
647 The default Debian man program (\"man-db\") has a `--local-file'
648 \(or `-l') option for this purpose. The default Red Hat man
649 program has no such option, but interprets any name containing
650 a \"/\" as a local filename. The function returns either `man-db'
652 (if (eq Man-support-local-filenames
'auto-detect
)
653 (setq Man-support-local-filenames
655 (let ((default-directory
656 ;; Ensure that `default-directory' exists and is readable.
657 (if (and (file-directory-p default-directory
)
658 (file-readable-p default-directory
))
660 (expand-file-name "~/"))))
662 (call-process manual-program nil t nil
"--help")))
663 (cond ((search-backward "--local-file" nil
'move
)
665 ;; This feature seems to be present in at least ver 1.4f,
666 ;; which is about 20 years old.
667 ;; I don't know if this version has an official name?
668 ((looking-at "^man, versione? [1-9]")
670 Man-support-local-filenames
))
673 ;; ======================================================================
674 ;; default man entry: get word near point
676 (defun Man-default-man-entry (&optional pos
)
677 "Guess default manual entry based on the text near position POS.
678 POS defaults to `point'."
679 (let (word start column distance
)
681 (when pos
(goto-char pos
))
683 ;; The default title is the nearest entry-like object before or
685 (if (and (skip-chars-backward " \ta-zA-Z0-9+")
686 (not (zerop (skip-chars-backward "(")))
687 ;; Try to handle the special case where POS is on a
690 (concat "([ \t]*\\(" Man-section-regexp
"\\)[ \t]*)"))
691 ;; We skipped a valid section number backwards, look at
693 (or (and (skip-chars-backward ",; \t")
694 (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:"))))
695 ;; Not a valid entry, move POS after closing paren.
696 (not (setq pos
(match-end 0)))))
697 ;; We have a candidate, make `start' record its starting
700 ;; Otherwise look at char before POS.
702 (if (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:")))
703 ;; Our candidate is just before or around POS.
705 ;; Otherwise record the current column and look backwards.
706 (setq column
(current-column))
707 (skip-chars-backward ",; \t")
708 ;; Record the distance traveled.
709 (setq distance
(- column
(current-column)))
711 (concat "([ \t]*\\(?:" Man-section-regexp
"\\)[ \t]*)"))
712 ;; Skip section number backwards.
713 (goto-char (match-beginning 0))
714 (skip-chars-backward " \t"))
715 (if (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:")))
717 ;; We have a candidate before POS ...
720 (if (and (skip-chars-forward ",; \t")
721 (< (- (current-column) column
) distance
)
722 (looking-at "[-a-zA-Z0-9._+:]"))
723 ;; ... but the one after POS is better.
725 ;; ... and anything after POS is worse.
727 ;; No candidate before POS.
729 (skip-chars-forward ",; \t")
730 (setq start
(point)))))
731 ;; We have found a suitable starting point, try to skip at least
733 (skip-chars-forward "-a-zA-Z0-9._+:")
734 (setq word
(buffer-substring-no-properties start
(point)))
735 ;; If there is a continuation at the end of line, check the
736 ;; following line too, eg:
739 ;; Note: This code gets executed iff our entry is after POS.
740 (when (looking-at "[ \t\r\n]+\\([-a-zA-Z0-9._+:]+\\)([0-9])")
741 (setq word
(concat word
(match-string-no-properties 1)))
742 ;; Make sure the section number gets included by the code below.
743 (goto-char (match-end 1)))
744 (when (string-match "[._]+$" word
)
745 (setq word
(substring word
0 (match-beginning 0))))
746 ;; The following was commented out since the preceding code
747 ;; should not produce a leading "*" in the first place.
748 ;;; ;; If looking at something like *strcat(... , remove the '*'
749 ;;; (when (string-match "^*" word)
750 ;;; (setq word (substring word 1)))
753 (and (not (string-equal word
""))
754 ;; If looking at something like ioctl(2) or brc(1M),
755 ;; include the section number in the returned value.
757 (concat "[ \t]*([ \t]*\\(" Man-section-regexp
"\\)[ \t]*)"))
758 (format "(%s)" (match-string-no-properties 1)))))))
761 ;; ======================================================================
762 ;; Top level command and background process sentinel
764 ;; For compatibility with older versions.
766 (defalias 'manual-entry
'man
)
768 (defvar Man-completion-cache nil
769 ;; On my machine, "man -k" is so fast that a cache makes no sense,
770 ;; but apparently that's not the case in all cases, so let's add a cache.
771 "Cache of completion table of the form (PREFIX . TABLE).")
773 (defun Man-completion-table (string pred action
)
775 ;; This ends up returning t for pretty much any string, and hence leads to
776 ;; spurious "complete but not unique" messages. And since `man' doesn't
777 ;; require-match anyway, there's not point being clever.
778 ;;((eq action 'lambda) (not (string-match "([^)]*\\'" string)))
780 ;; Let SPC (minibuffer-complete-word) insert the space.
781 (complete-with-action action
'("-k ") string pred
))
783 (let ((table (cdr Man-completion-cache
))
786 (when (string-match "\\`\\([[:digit:]].*?\\) " string
)
787 (setq section
(match-string 1 string
))
788 (setq prefix
(substring string
(match-end 0))))
789 (unless (and Man-completion-cache
790 (string-prefix-p (car Man-completion-cache
) prefix
))
792 (setq default-directory
"/") ;; in case inherited doesn't exist
793 ;; Actually for my `man' the arg is a regexp.
794 ;; POSIX says it must be ERE and "man-db" seems to agree,
795 ;; whereas under MacOSX it seems to be BRE-style and doesn't
796 ;; accept backslashes at all. Let's not bother to
798 (let ((process-environment (copy-sequence process-environment
)))
799 (setenv "COLUMNS" "999") ;; don't truncate long names
800 ;; manual-program might not even exist. And since it's
801 ;; run differently in Man-getpage-in-background, an error
802 ;; here may not necessarily mean that we'll also get an
805 (call-process manual-program nil
'(t nil
) nil
806 "-k" (concat "^" prefix
))))
807 (goto-char (point-min))
808 (while (re-search-forward "^\\([^ \t\n]+\\)\\(?: ?\\((.+?)\\)\\(?:[ \t]+- \\(.*\\)\\)?\\)?" nil t
)
809 (push (propertize (concat (match-string 1) (match-string 2))
810 'help-echo
(match-string 3))
812 ;; Cache the table for later reuse.
813 (setq Man-completion-cache
(cons prefix table
)))
814 ;; The table may contain false positives since the match is made
815 ;; by "man -k" not just on the manpage's name.
817 (let ((re (concat "(" (regexp-quote section
) ")\\'")))
818 (dolist (comp (prog1 table
(setq table nil
)))
819 (if (string-match re comp
)
820 (push (substring comp
0 (match-beginning 0)) table
)))
821 (completion-table-with-context (concat section
" ") table
823 ;; If the current text looks like a possible section name,
824 ;; then add a completion entry that just adds a space so SPC
825 ;; can be used to insert a space.
826 (if (string-match "\\`[[:digit:]]" string
)
827 (push (concat string
" ") table
))
828 (let ((res (complete-with-action action table string pred
)))
829 ;; In case we're completing to a single name that exists in
830 ;; several sections, the longest prefix will look like "foo(".
831 (if (and (stringp res
)
832 (string-match "([^(]*\\'" res
)
833 ;; In case the paren was already in `prefix', don't
835 (> (match-beginning 0) (length prefix
)))
836 (substring res
0 (match-beginning 0))
840 (defun man (man-args)
841 "Get a Un*x manual page and put it in a buffer.
842 This command is the top-level command in the man package. It
843 runs a Un*x command to retrieve and clean a manpage in the
844 background and places the results in a `Man-mode' browsing
845 buffer. See variable `Man-notify-method' for what happens when
846 the buffer is ready. If a buffer already exists for this man
847 page, it will display immediately.
849 For a manpage from a particular section, use either of the
850 following. \"cat(1)\" is how cross-references appear and is
851 passed to man as \"1 cat\".
856 To see manpages from all sections related to a subject, use an
857 \"all pages\" option (which might be \"-a\" if it's not the
858 default), then step through with `Man-next-manpage' (\\<Man-mode-map>\\[Man-next-manpage]) etc.
859 Add to `Man-switches' to make this option permanent.
863 An explicit filename can be given too. Use -l if it might
864 otherwise look like a page name.
869 An \"apropos\" query with -k gives a buffer of matching page
870 names or descriptions. The pattern argument is usually an
871 \"egrep\" style regexp.
876 (list (let* ((default-entry (Man-default-man-entry))
877 ;; ignore case because that's friendly for bizarre
878 ;; caps things like the X11 function names and because
879 ;; "man" itself is case-insensitive on the command line
880 ;; so you're accustomed not to bother about the case
881 ;; ("man -k" is case-insensitive similarly, so the
882 ;; table has everything available to complete)
883 (completion-ignore-case t
)
884 (input (completing-read
885 (format "Manual entry%s"
886 (if (string= default-entry
"")
888 (format " (default %s): " default-entry
)))
889 'Man-completion-table
890 nil nil nil
'Man-topic-history default-entry
)))
891 (if (string= input
"")
892 (error "No man args given")
895 ;; Possibly translate the "subject(section)" syntax into the
896 ;; "section subject" syntax and possibly downcase the section.
897 (setq man-args
(Man-translate-references man-args
))
899 (Man-getpage-in-background man-args
))
902 (defun man-follow (man-args)
903 "Get a Un*x manual page of the item under point and put it in a buffer."
904 (interactive (list (Man-default-man-entry)))
905 (if (or (not man-args
)
906 (string= man-args
""))
907 (error "No item under point")
910 (defun Man-getpage-in-background (topic)
911 "Use TOPIC to build and fire off the manpage and cleaning command.
912 Return the buffer in which the manpage will appear."
913 (let* ((man-args topic
)
914 (bufname (concat "*Man " man-args
"*"))
915 (buffer (get-buffer bufname
)))
917 (Man-notify-when-ready buffer
)
919 (message "Invoking %s %s in the background" manual-program man-args
)
920 (setq buffer
(generate-new-buffer bufname
))
921 (with-current-buffer buffer
922 (setq buffer-undo-list t
)
923 (setq Man-original-frame
(selected-frame))
924 (setq Man-arguments man-args
))
925 (let ((process-environment (copy-sequence process-environment
))
926 ;; The following is so Awk script gets \n intact
927 ;; But don't prevent decoding of the outside.
928 (coding-system-for-write 'raw-text-unix
)
929 ;; We must decode the output by a coding system that the
930 ;; system's locale suggests in multibyte mode.
931 (coding-system-for-read
932 (if (default-value 'enable-multibyte-characters
)
933 locale-coding-system
'raw-text-unix
))
934 ;; Avoid possible error by using a directory that always exists.
936 (if (and (file-directory-p default-directory
)
937 (not (find-file-name-handler default-directory
941 ;; Prevent any attempt to use display terminal fanciness.
942 (setenv "TERM" "dumb")
943 ;; In Debian Woody, at least, we get overlong lines under X
944 ;; unless COLUMNS or MANWIDTH is set. This isn't a problem on
945 ;; a tty. man(1) says:
947 ;; If $MANWIDTH is set, its value is used as the line
948 ;; length for which manual pages should be formatted.
949 ;; If it is not set, manual pages will be formatted
950 ;; with a line length appropriate to the current ter-
951 ;; minal (using an ioctl(2) if available, the value of
952 ;; $COLUMNS, or falling back to 80 characters if nei-
953 ;; ther is available).
954 (when (or window-system
955 (not (or (getenv "MANWIDTH") (getenv "COLUMNS"))))
956 ;; This isn't strictly correct, since we don't know how
957 ;; the page will actually be displayed, but it seems
959 (setenv "COLUMNS" (number-to-string
961 ((and (integerp Man-width
) (> Man-width
0))
963 (Man-width (frame-width))
965 (setenv "GROFF_NO_SGR" "1")
966 ;; Since man-db 2.4.3-1, man writes plain text with no escape
967 ;; sequences when stdout is not a tty. In 2.5.0, the following
968 ;; env-var was added to allow control of this (see Debian Bug#340673).
969 (setenv "MAN_KEEP_FORMATTING" "1")
970 (if (fboundp 'start-process
)
971 (set-process-sentinel
972 (start-process manual-program buffer
973 (if (memq system-type
'(cygwin windows-nt
))
977 (format (Man-build-man-command) man-args
))
978 'Man-bgproc-sentinel
)
980 (call-process shell-file-name nil
(list buffer nil
) nil
982 (format (Man-build-man-command) man-args
)))
984 (or (and (numberp exit-status
)
986 (and (numberp exit-status
)
988 (format "exited abnormally with code %d"
990 (setq msg exit-status
))
991 (Man-bgproc-sentinel bufname msg
)))))
994 (defun Man-notify-when-ready (man-buffer)
995 "Notify the user when MAN-BUFFER is ready.
996 See the variable `Man-notify-method' for the different notification behaviors."
997 (let ((saved-frame (with-current-buffer man-buffer
998 Man-original-frame
)))
999 (pcase Man-notify-method
1001 ;; Since we run asynchronously, perhaps while Emacs is waiting
1002 ;; for input, we must not leave a different buffer current. We
1003 ;; can't rely on the editor command loop to reselect the
1004 ;; selected window's buffer.
1006 (let ((frame (make-frame Man-frame-parameters
)))
1007 (set-window-buffer (frame-selected-window frame
) man-buffer
)
1008 (set-window-dedicated-p (frame-selected-window frame
) t
)
1009 (or (display-multi-frame-p frame
)
1010 (select-frame frame
)))))
1012 (switch-to-buffer man-buffer
))
1014 (and (frame-live-p saved-frame
)
1015 (select-frame saved-frame
))
1016 (pop-to-buffer man-buffer
)
1017 (delete-other-windows))
1019 (and (frame-live-p saved-frame
)
1020 (select-frame saved-frame
))
1021 (pop-to-buffer man-buffer
))
1023 (and (frame-live-p saved-frame
)
1024 (select-frame saved-frame
))
1025 (display-buffer man-buffer
'not-this-window
))
1028 (message "Manual buffer %s is ready" (buffer-name man-buffer
)))
1030 (message "Manual buffer %s is ready" (buffer-name man-buffer
)))
1035 (defun Man-softhyphen-to-minus ()
1036 ;; \255 is SOFT HYPHEN in Latin-N. Versions of Debian man, at
1037 ;; least, emit it even when not in a Latin-N locale.
1038 (unless (eq t
(compare-strings "latin-" 0 nil
1039 current-language-environment
0 6 t
))
1040 (goto-char (point-min))
1042 (if enable-multibyte-characters
1043 (setq str
(string-as-multibyte str
)))
1044 (while (search-forward str nil t
) (replace-match "-")))))
1046 (defun Man-fontify-manpage ()
1047 "Convert overstriking and underlining to the correct fonts.
1048 Same for the ANSI bold and normal escape sequences."
1050 (message "Please wait: formatting the %s man page..." Man-arguments
)
1051 (goto-char (point-min))
1052 ;; Fontify ANSI escapes.
1054 (buffer-undo-list t
)
1056 ;; http://www.isthe.com/chongo/tech/comp/ansi_escapes.html
1057 ;; suggests many codes, but we only handle:
1058 ;; ESC [ 00 m reset to normal display
1060 ;; ESC [ 04 m underline
1061 ;; ESC [ 07 m reverse-video
1062 ;; ESC [ 22 m no-bold
1063 ;; ESC [ 24 m no-underline
1064 ;; ESC [ 27 m no-reverse-video
1065 (while (re-search-forward "\e\\[0?\\([1470]\\|2\\([247]\\)\\)m" nil t
)
1066 (if faces
(put-text-property start
(match-beginning 0) 'face
1067 (if (cdr faces
) faces
(car faces
))))
1070 ((match-beginning 2)
1071 (delq (pcase (char-after (match-beginning 2))
1072 (?
2 Man-overstrike-face
)
1073 (?
4 Man-underline-face
)
1074 (?
7 Man-reverse-face
))
1076 ((eq (char-after (match-beginning 1)) ?
0) nil
)
1078 (cons (pcase (char-after (match-beginning 1))
1079 (?
1 Man-overstrike-face
)
1080 (?
4 Man-underline-face
)
1081 (?
7 Man-reverse-face
))
1083 (delete-region (match-beginning 0) (match-end 0))
1084 (setq start
(point))))
1085 ;; Other highlighting.
1086 (let ((buffer-undo-list t
))
1087 (if (< (buffer-size) (position-bytes (point-max)))
1088 ;; Multibyte characters exist.
1090 (goto-char (point-min))
1091 (while (search-forward "__\b\b" nil t
)
1092 (backward-delete-char 4)
1093 (put-text-property (point) (1+ (point)) 'face Man-underline-face
))
1094 (goto-char (point-min))
1095 (while (search-forward "\b\b__" nil t
)
1096 (backward-delete-char 4)
1097 (put-text-property (1- (point)) (point) 'face Man-underline-face
))))
1098 (goto-char (point-min))
1099 (while (search-forward "_\b" nil t
)
1100 (backward-delete-char 2)
1101 (put-text-property (point) (1+ (point)) 'face Man-underline-face
))
1102 (goto-char (point-min))
1103 (while (search-forward "\b_" nil t
)
1104 (backward-delete-char 2)
1105 (put-text-property (1- (point)) (point) 'face Man-underline-face
))
1106 (goto-char (point-min))
1107 (while (re-search-forward "\\(.\\)\\(\b+\\1\\)+" nil t
)
1108 (replace-match "\\1")
1109 (put-text-property (1- (point)) (point) 'face Man-overstrike-face
))
1110 (goto-char (point-min))
1111 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t
)
1113 (put-text-property (1- (point)) (point) 'face
'bold
))
1114 (goto-char (point-min))
1115 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t
)
1117 (put-text-property (1- (point)) (point) 'face
'bold
))
1118 ;; When the header is longer than the manpage name, groff tries to
1119 ;; condense it to a shorter line interspersed with ^H. Remove ^H with
1120 ;; their preceding chars (but don't put Man-overstrike-face). (Bug#5566)
1121 (goto-char (point-min))
1122 (while (re-search-forward ".\b" nil t
) (backward-delete-char 2))
1123 (goto-char (point-min))
1124 ;; Try to recognize common forms of cross references.
1125 (Man-highlight-references)
1126 (Man-softhyphen-to-minus)
1127 (goto-char (point-min))
1128 (while (re-search-forward Man-heading-regexp nil t
)
1129 (put-text-property (match-beginning 0)
1131 'face Man-overstrike-face
)))
1132 (message "%s man page formatted" (Man-page-from-arguments Man-arguments
)))
1134 (defun Man-highlight-references (&optional xref-man-type
)
1135 "Highlight the references on mouse-over.
1136 References include items in the SEE ALSO section,
1137 header file (#include <foo.h>), and files in FILES.
1138 If optional argument XREF-MAN-TYPE is non-nil, it used as the
1139 button type for items in SEE ALSO section. If it is nil, the
1140 default type, `Man-xref-man-page' is used for the buttons."
1141 ;; `Man-highlight-references' is used from woman.el, too.
1142 ;; woman.el doesn't set `Man-arguments'.
1143 (unless Man-arguments
1144 (setq Man-arguments
""))
1145 (if (string-match "-k " Man-arguments
)
1147 (Man-highlight-references0 nil Man-reference-regexp
1
1148 'Man-default-man-entry
1149 (or xref-man-type
'Man-xref-man-page
))
1150 (Man-highlight-references0 nil Man-apropos-regexp
1
1151 'Man-default-man-entry
1152 (or xref-man-type
'Man-xref-man-page
)))
1153 (Man-highlight-references0 Man-see-also-regexp Man-reference-regexp
1
1154 'Man-default-man-entry
1155 (or xref-man-type
'Man-xref-man-page
))
1156 (Man-highlight-references0 Man-synopsis-regexp Man-header-regexp
0 2
1157 'Man-xref-header-file
)
1158 (Man-highlight-references0 Man-files-regexp Man-normal-file-regexp
0 0
1159 'Man-xref-normal-file
)))
1161 (defun Man-highlight-references0 (start-section regexp button-pos target type
)
1162 ;; Based on `Man-build-references-alist'
1163 (when (or (null start-section
)
1164 (Man-find-section start-section
))
1165 (let ((end (if start-section
1168 (back-to-indentation)
1170 (Man-next-section 1)
1172 (goto-char (point-min))
1174 (while (re-search-forward regexp end t
)
1175 ;; An overlay button is preferable because the underlying text
1176 ;; may have text property highlights (Bug#7881).
1178 (match-beginning button-pos
)
1179 (match-end button-pos
)
1181 'Man-target-string
(cond
1183 (match-string target
))
1188 (defun Man-cleanup-manpage (&optional interactive
)
1189 "Remove overstriking and underlining from the current buffer.
1190 Normally skip any jobs that should have been done by the sed script,
1191 but when called interactively, do those jobs even if the sed
1192 script would have done them."
1194 (message "Please wait: cleaning up the %s man page..."
1196 (if (or interactive
(not Man-sed-script
))
1198 (goto-char (point-min))
1199 (while (search-forward "_\b" nil t
) (backward-delete-char 2))
1200 (goto-char (point-min))
1201 (while (search-forward "\b_" nil t
) (backward-delete-char 2))
1202 (goto-char (point-min))
1203 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t
)
1204 (replace-match "\\1"))
1205 (goto-char (point-min))
1206 (while (re-search-forward "\e\\[[0-9]+m" nil t
) (replace-match ""))
1207 (goto-char (point-min))
1208 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t
) (replace-match "o"))
1210 (goto-char (point-min))
1211 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t
) (replace-match "+"))
1212 ;; When the header is longer than the manpage name, groff tries to
1213 ;; condense it to a shorter line interspersed with ^H. Remove ^H with
1214 ;; their preceding chars (but don't put Man-overstrike-face). (Bug#5566)
1215 (goto-char (point-min))
1216 (while (re-search-forward ".\b" nil t
) (backward-delete-char 2))
1217 (Man-softhyphen-to-minus)
1218 (message "%s man page cleaned up" Man-arguments
))
1220 (defun Man-bgproc-sentinel (process msg
)
1221 "Manpage background process sentinel.
1222 When manpage command is run asynchronously, PROCESS is the process
1223 object for the manpage command; when manpage command is run
1224 synchronously, PROCESS is the name of the buffer where the manpage
1225 command is run. Second argument MSG is the exit message of the
1227 (let ((Man-buffer (if (stringp process
) (get-buffer process
)
1228 (process-buffer process
)))
1232 (if (null (buffer-name Man-buffer
)) ;; deleted buffer
1233 (or (stringp process
)
1234 (set-process-buffer process nil
))
1236 (with-current-buffer Man-buffer
1237 (let ((case-fold-search nil
))
1238 (goto-char (point-min))
1239 (cond ((or (looking-at "No \\(manual \\)*entry for")
1240 (looking-at "[^\n]*: nothing appropriate$"))
1241 (setq err-mess
(buffer-substring (point)
1243 (end-of-line) (point)))
1246 ;; "-k foo", successful exit, but no output (from man-db)
1247 ;; ENHANCE-ME: share the check for -k with
1248 ;; `Man-highlight-references'. The \\s- bits here are
1249 ;; meant to allow for multiple options with -k among them.
1250 ((and (string-match "\\(\\`\\|\\s-\\)-k\\s-" Man-arguments
)
1251 (eq (process-status process
) 'exit
)
1252 (= (process-exit-status process
) 0)
1253 (= (point-min) (point-max)))
1254 (setq err-mess
(format "%s: no matches" Man-arguments
)
1257 ((or (stringp process
)
1258 (not (and (eq (process-status process
) 'exit
)
1259 (= (process-exit-status process
) 0))))
1260 (or (zerop (length msg
))
1263 (concat (buffer-name Man-buffer
)
1265 (let ((eos (1- (length msg
))))
1266 (if (= (aref msg eos
) ?
\n)
1267 (substring msg
0 eos
) msg
))))
1268 (goto-char (point-max))
1269 (insert (format "\nprocess %s" msg
))))
1272 (kill-buffer Man-buffer
)
1273 (if Man-fontify-manpage-flag
1274 (Man-fontify-manpage)
1275 (Man-cleanup-manpage))
1277 (run-hooks 'Man-cooked-hook
)
1280 (if (not Man-page-list
)
1281 (let ((args Man-arguments
))
1282 (kill-buffer (current-buffer))
1283 (user-error "Can't find the %s manpage"
1284 (Man-page-from-arguments args
)))
1285 (set-buffer-modified-p nil
))))
1286 ;; Restore case-fold-search before calling
1287 ;; Man-notify-when-ready because it may switch buffers.
1289 (if (not delete-buff
)
1290 (Man-notify-when-ready Man-buffer
))
1293 (error "%s" err-mess
))
1296 (defun Man-page-from-arguments (args)
1297 ;; Skip arguments and only print the page name.
1303 (and (not (string-match "^-" elem
))
1305 (split-string args
" ")))
1309 ;; ======================================================================
1310 ;; set up manual mode in buffer and build alists
1312 (defvar bookmark-make-record-function
)
1314 (put 'Man-mode
'mode-class
'special
)
1317 "A mode for browsing Un*x manual pages.
1319 The following man commands are available in the buffer. Try
1320 \"\\[describe-key] <key> RET\" for more information:
1322 \\[man] Prompt to retrieve a new manpage.
1323 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
1324 \\[Man-next-manpage] Jump to next manpage in circular list.
1325 \\[Man-previous-manpage] Jump to previous manpage in circular list.
1326 \\[Man-next-section] Jump to next manpage section.
1327 \\[Man-previous-section] Jump to previous manpage section.
1328 \\[Man-goto-section] Go to a manpage section.
1329 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
1330 \\[Man-quit] Deletes the manpage window, bury its buffer.
1331 \\[Man-kill] Deletes the manpage window, kill its buffer.
1332 \\[describe-mode] Prints this help text.
1334 The following variables may be of some use. Try
1335 \"\\[describe-variable] <variable-name> RET\" for more information:
1337 `Man-notify-method' What happens when manpage formatting is done.
1338 `Man-downcase-section-letters-flag' Force section letters to lower case.
1339 `Man-circular-pages-flag' Treat multiple manpage list as circular.
1340 `Man-section-translations-alist' List of section numbers and their Un*x equiv.
1341 `Man-filter-list' Background manpage filter command.
1342 `Man-mode-map' Keymap bindings for Man mode buffers.
1343 `Man-mode-hook' Normal hook run on entry to Man mode.
1344 `Man-section-regexp' Regexp describing manpage section letters.
1345 `Man-heading-regexp' Regexp describing section headers.
1346 `Man-see-also-regexp' Regexp for SEE ALSO section (or your equiv).
1347 `Man-first-heading-regexp' Regexp for first heading on a manpage.
1348 `Man-reference-regexp' Regexp matching a references in SEE ALSO.
1349 `Man-switches' Background `man' command switches.
1351 The following key bindings are currently in effect in the buffer:
1354 (kill-all-local-variables)
1355 (setq major-mode
'Man-mode
1357 buffer-auto-save-file-name nil
1358 mode-line-buffer-identification
1359 (list (default-value 'mode-line-buffer-identification
)
1360 " {" 'Man-page-mode-string
"}")
1363 (buffer-disable-undo)
1365 (use-local-map Man-mode-map
)
1366 (set-syntax-table man-mode-syntax-table
)
1367 (setq imenu-generic-expression
(list (list nil Man-heading-regexp
0)))
1368 (set (make-local-variable 'outline-regexp
) Man-heading-regexp
)
1369 (set (make-local-variable 'outline-level
) (lambda () 1))
1370 (set (make-local-variable 'bookmark-make-record-function
)
1371 'Man-bookmark-make-record
)
1372 (Man-build-page-list)
1373 (Man-strip-page-headers)
1376 (run-mode-hooks 'Man-mode-hook
))
1378 (defsubst Man-build-section-alist
()
1379 "Build the list of manpage sections."
1380 (setq Man--sections nil
)
1381 (goto-char (point-min))
1382 (let ((case-fold-search nil
))
1383 (while (re-search-forward Man-heading-regexp
(point-max) t
)
1384 (let ((section (match-string 1)))
1385 (unless (member section Man--sections
)
1386 (push section Man--sections
)))
1389 (defsubst Man-build-references-alist
()
1390 "Build the list of references (in the SEE ALSO section)."
1391 (setq Man--refpages nil
)
1393 (if (Man-find-section Man-see-also-regexp
)
1394 (let ((start (progn (forward-line 1) (point)))
1396 (Man-next-section 1)
1401 (narrow-to-region start end
)
1402 (goto-char (point-min))
1403 (back-to-indentation)
1404 (while (and (not (eobp)) (/= (point) runningpoint
))
1405 (setq runningpoint
(point))
1406 (if (re-search-forward Man-hyphenated-reference-regexp end t
)
1407 (let* ((word (match-string 0))
1408 (len (1- (length word
))))
1410 (setq word
(concat hyphenated word
)
1412 ;; Update len, in case a reference spans
1413 ;; more than two lines (paranoia).
1414 len
(1- (length word
))))
1415 (if (memq (aref word len
) '(?- ?Â
))
1416 (setq hyphenated
(substring word
0 len
)))
1417 (and (string-match Man-reference-regexp word
)
1418 (not (member word Man--refpages
))
1419 (push word Man--refpages
))))
1420 (skip-chars-forward " \t\n,"))))))
1421 (setq Man--refpages
(nreverse Man--refpages
)))
1423 (defun Man-build-page-list ()
1424 "Build the list of separate manpages in the buffer."
1425 (setq Man-page-list nil
)
1426 (let ((page-start (point-min))
1427 (page-end (point-max))
1429 (goto-char page-start
)
1430 ;; (switch-to-buffer (current-buffer))(debug)
1433 (if (looking-at Man-page-header-regexp
)
1436 ;; Go past both the current and the next Man-first-heading-regexp
1437 (if (re-search-forward Man-first-heading-regexp nil
'move
2)
1438 (let ((p (progn (beginning-of-line) (point))))
1439 ;; We assume that the page header is delimited by blank
1440 ;; lines and that it contains at most one blank line. So
1441 ;; if we back by three blank lines we will be sure to be
1442 ;; before the page header but not before the possible
1443 ;; previous page header.
1444 (search-backward "\n\n" nil t
3)
1445 (if (re-search-forward Man-page-header-regexp p
'move
)
1446 (beginning-of-line))))
1447 (setq page-end
(point))
1448 (setq Man-page-list
(append Man-page-list
1449 (list (list (copy-marker page-start
)
1450 (copy-marker page-end
)
1452 (setq page-start page-end
)
1455 (defun Man-strip-page-headers ()
1456 "Strip all the page headers but the first from the manpage."
1457 (let ((inhibit-read-only t
)
1458 (case-fold-search nil
)
1460 (dolist (page Man-page-list
)
1462 (goto-char (car page
))
1463 (re-search-forward Man-first-heading-regexp nil t
)
1464 (setq header
(buffer-substring (car page
) (match-beginning 0)))
1465 ;; Since the awk script collapses all successive blank
1466 ;; lines into one, and since we don't want to get rid of
1467 ;; the fast awk script, one must choose between adding
1468 ;; spare blank lines between pages when there were none and
1469 ;; deleting blank lines at page boundaries when there were
1470 ;; some. We choose the first, so we comment the following
1472 ;; (setq header (concat "\n" header)))
1473 (while (search-forward header
(nth 1 page
) t
)
1474 (replace-match ""))))))
1476 (defun Man-unindent ()
1477 "Delete the leading spaces that indent the manpage."
1478 (let ((inhibit-read-only t
)
1479 (case-fold-search nil
))
1480 (dolist (page Man-page-list
)
1483 (narrow-to-region (car page
) (car (cdr page
)))
1484 (if Man-uses-untabify-flag
1485 ;; The space characters inserted by `untabify' inherit
1486 ;; sticky text properties, which is unnecessary and looks
1487 ;; ugly with underlining (Bug#11408).
1488 (let ((text-property-default-nonsticky
1489 (cons '(face . t
) text-property-default-nonsticky
)))
1490 (untabify (point-min) (point-max))))
1491 (if (catch 'unindent
1492 (goto-char (point-min))
1493 (if (not (re-search-forward Man-first-heading-regexp nil t
))
1494 (throw 'unindent nil
))
1496 (setq indent
(buffer-substring (point)
1498 (skip-chars-forward " ")
1500 (setq nindent
(length indent
))
1502 (throw 'unindent nil
))
1503 (setq indent
(concat indent
"\\|$"))
1504 (goto-char (point-min))
1506 (if (looking-at indent
)
1508 (throw 'unindent nil
)))
1509 (goto-char (point-min)))
1512 (delete-char nindent
))
1517 ;; ======================================================================
1518 ;; Man mode commands
1520 (defun Man-next-section (n)
1521 "Move point to Nth next section (default 1)."
1523 (let ((case-fold-search nil
)
1525 (if (looking-at Man-heading-regexp
)
1527 (if (re-search-forward Man-heading-regexp
(point-max) t n
)
1529 (goto-char (point-max))
1530 ;; The last line doesn't belong to any section.
1532 ;; But don't move back from the starting point (can happen if `start'
1533 ;; is somewhere on the last line).
1534 (if (< (point) start
) (goto-char start
))))
1536 (defun Man-previous-section (n)
1537 "Move point to Nth previous section (default 1)."
1539 (let ((case-fold-search nil
))
1540 (if (looking-at Man-heading-regexp
)
1542 (if (re-search-backward Man-heading-regexp
(point-min) t n
)
1544 (goto-char (point-min)))))
1546 (defun Man-find-section (section)
1547 "Move point to SECTION if it exists, otherwise don't move point.
1548 Returns t if section is found, nil otherwise."
1549 (let ((curpos (point))
1550 (case-fold-search nil
))
1551 (goto-char (point-min))
1552 (if (re-search-forward (concat "^" section
) (point-max) t
)
1553 (progn (beginning-of-line) t
)
1558 (defvar Man--last-section nil
)
1560 (defun Man-goto-section (section)
1561 "Move point to SECTION."
1563 (let* ((default (if (member Man--last-section Man--sections
)
1565 (car Man--sections
)))
1566 (completion-ignore-case t
)
1567 (prompt (concat "Go to section (default " default
"): "))
1568 (chosen (completing-read prompt Man--sections
1569 nil nil nil nil default
)))
1571 (setq Man--last-section section
)
1572 (unless (Man-find-section section
)
1573 (error "Section %s not found" section
)))
1576 (defun Man-goto-see-also-section ()
1577 "Move point to the \"SEE ALSO\" section.
1578 Actually the section moved to is described by `Man-see-also-regexp'."
1580 (if (not (Man-find-section Man-see-also-regexp
))
1581 (error "%s" (concat "No " Man-see-also-regexp
1582 " section found in the current manpage"))))
1584 (defun Man-possibly-hyphenated-word ()
1585 "Return a possibly hyphenated word at point.
1586 If the word starts at the first non-whitespace column, and the
1587 previous line ends with a hyphen, return the last word on the previous
1588 line instead. Thus, if a reference to \"tcgetpgrp(3V)\" is hyphenated
1589 as \"tcgetp-grp(3V)\", and point is at \"grp(3V)\", we return
1590 \"tcgetp-\" instead of \"grp\"."
1592 (skip-syntax-backward "w()")
1593 (skip-chars-forward " \t")
1595 (word (current-word)))
1596 (when (eq beg
(save-excursion
1597 (back-to-indentation)
1600 (if (eq (char-before) ?-
)
1601 (setq word
(current-word))))
1604 (defvar Man--last-refpage nil
)
1606 (defun Man-follow-manual-reference (reference)
1607 "Get one of the manpages referred to in the \"SEE ALSO\" section.
1608 Specify which REFERENCE to use; default is based on word at point."
1610 (if (not Man--refpages
)
1611 (error "There are no references in the current man page")
1614 (car (all-completions
1616 (or (Man-possibly-hyphenated-word)
1618 ;; strip a trailing '-':
1619 (if (string-match "-$" word
)
1621 (match-beginning 0))
1624 (if (member Man--last-refpage Man--refpages
)
1626 (car Man--refpages
))))
1628 (mapcar 'substring-no-properties
1629 (cons default Man--refpages
)))
1630 (prompt (concat "Refer to (default " default
"): "))
1631 (chosen (completing-read prompt Man--refpages
1632 nil nil nil nil defaults
)))
1634 (if (not Man--refpages
)
1635 (error "Can't find any references in the current manpage")
1636 (setq Man--last-refpage reference
)
1637 (Man-getpage-in-background
1638 (Man-translate-references reference
))))
1641 "Kill the buffer containing the manpage."
1646 "Bury the buffer containing the manpage."
1650 (defun Man-goto-page (page &optional noerror
)
1651 "Go to the manual page on page PAGE."
1653 (if (not Man-page-list
)
1654 (error "Not a man page buffer")
1655 (if (= (length Man-page-list
) 1)
1656 (error "You're looking at the only manpage in the buffer")
1657 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1658 (length Man-page-list
)))))))
1659 (if (and (not Man-page-list
) (not noerror
))
1660 (error "Not a man page buffer"))
1663 (> page
(length Man-page-list
)))
1664 (user-error "No manpage %d found" page
))
1665 (let* ((page-range (nth (1- page
) Man-page-list
))
1666 (page-start (car page-range
))
1667 (page-end (car (cdr page-range
))))
1668 (setq Man-current-page page
1669 Man-page-mode-string
(Man-make-page-mode-string))
1671 (goto-char page-start
)
1672 (narrow-to-region page-start page-end
)
1673 (Man-build-section-alist)
1674 (Man-build-references-alist)
1675 (goto-char (point-min)))))
1678 (defun Man-next-manpage ()
1679 "Find the next manpage entry in the buffer."
1681 (if (= (length Man-page-list
) 1)
1682 (error "This is the only manpage in the buffer"))
1683 (if (< Man-current-page
(length Man-page-list
))
1684 (Man-goto-page (1+ Man-current-page
))
1685 (if Man-circular-pages-flag
1687 (error "You're looking at the last manpage in the buffer"))))
1689 (defun Man-previous-manpage ()
1690 "Find the previous manpage entry in the buffer."
1692 (if (= (length Man-page-list
) 1)
1693 (error "This is the only manpage in the buffer"))
1694 (if (> Man-current-page
1)
1695 (Man-goto-page (1- Man-current-page
))
1696 (if Man-circular-pages-flag
1697 (Man-goto-page (length Man-page-list
))
1698 (error "You're looking at the first manpage in the buffer"))))
1700 ;; Header file support
1701 (defun Man-view-header-file (file)
1702 "View a header file specified by FILE from `Man-header-file-path'."
1703 (let ((path Man-header-file-path
)
1706 (setq complete-path
(expand-file-name file
(car path
))
1708 (if (file-readable-p complete-path
)
1709 (progn (view-file complete-path
)
1711 (setq complete-path nil
)))
1714 ;;; Bookmark Man Support
1715 (declare-function bookmark-make-record-default
1716 "bookmark" (&optional no-file no-context posn
))
1717 (declare-function bookmark-prop-get
"bookmark" (bookmark prop
))
1718 (declare-function bookmark-default-handler
"bookmark" (bmk))
1719 (declare-function bookmark-get-bookmark-record
"bookmark" (bmk))
1721 (defun Man-default-bookmark-title ()
1722 "Default bookmark name for Man or WoMan pages.
1723 Uses `Man-name-local-regexp'."
1725 (goto-char (point-min))
1726 (when (re-search-forward Man-name-local-regexp nil t
)
1727 (skip-chars-forward "\n\t ")
1728 (buffer-substring-no-properties (point) (line-end-position)))))
1730 (defun Man-bookmark-make-record ()
1731 "Make a bookmark entry for a Man buffer."
1732 `(,(Man-default-bookmark-title)
1733 ,@(bookmark-make-record-default 'no-file
)
1734 (location .
,(concat "man " Man-arguments
))
1735 (man-args .
,Man-arguments
)
1736 (handler . Man-bookmark-jump
)))
1739 (defun Man-bookmark-jump (bookmark)
1740 "Default bookmark handler for Man buffers."
1741 (let* ((man-args (bookmark-prop-get bookmark
'man-args
))
1742 ;; Let bookmark.el do the window handling.
1743 ;; This let-binding needs to be active during the call to both
1744 ;; Man-getpage-in-background and accept-process-output.
1745 (Man-notify-method 'meek
)
1746 (buf (Man-getpage-in-background man-args
))
1747 (proc (get-buffer-process buf
)))
1748 (while (and proc
(eq (process-status proc
) 'run
))
1749 (accept-process-output proc
))
1750 (bookmark-default-handler
1751 `("" (buffer .
,buf
) .
,(bookmark-get-bookmark-record bookmark
)))))
1754 ;; Init the man package variables, if not already done.
1759 ;;; man.el ends here