1 ;;; man.el --- browse UNIX manual pages -*- coding: iso-8859-1 -*-
3 ;; Copyright (C) 1993-1994, 1996-1997, 2001-2011
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.
91 (eval-when-compile (require 'cl
))
95 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
96 ;; empty defvars (keep the compiler quiet)
99 "Browse UNIX manual pages."
105 (defcustom Man-filter-list nil
106 "Manpage cleaning filter command phrases.
107 This variable contains a list of the following form:
109 '((command-string phrase-string*)*)
111 Each phrase-string is concatenated onto the command-string to form a
112 command filter. The (standard) output (and standard error) of the Un*x
113 man command is piped through each command filter in the order the
114 commands appear in the association list. The final output is placed in
116 :type
'(repeat (list (string :tag
"Command String")
118 (string :tag
"Phrase String"))))
121 (defvar Man-uses-untabify-flag t
122 "Non-nil means use `untabify' instead of `Man-untabify-command'.")
123 (defvar Man-sed-script nil
124 "Script for sed to nuke backspaces and ANSI codes from manpages.")
126 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
129 (defcustom Man-fontify-manpage-flag t
130 "Non-nil means make up the manpage with fonts."
134 (defcustom Man-overstrike-face
'bold
135 "Face to use when fontifying overstrike."
139 (defcustom Man-underline-face
'underline
140 "Face to use when fontifying underlining."
144 (defcustom Man-reverse-face
'highlight
145 "Face to use when fontifying reverse video."
149 ;; Use the value of the obsolete user option Man-notify, if set.
150 (defcustom Man-notify-method
(if (boundp 'Man-notify
) Man-notify
'friendly
)
151 "Selects the behavior when manpage is ready.
152 This variable may have one of the following values, where (sf) means
153 that the frames are switched, so the manpage is displayed in the frame
154 where the man command was called from:
156 newframe -- put the manpage in its own frame (see `Man-frame-parameters')
157 pushy -- make the manpage the current buffer in the current window
158 bully -- make the manpage the current buffer and only window (sf)
159 aggressive -- make the manpage the current buffer in the other window (sf)
160 friendly -- display manpage in the other window but don't make current (sf)
161 polite -- don't display manpage, but prints message and beep when ready
162 quiet -- like `polite', but don't beep
163 meek -- make no indication that the manpage is ready
165 Any other value of `Man-notify-method' is equivalent to `meek'."
166 :type
'(radio (const newframe
) (const pushy
) (const bully
)
167 (const aggressive
) (const friendly
)
168 (const polite
) (const quiet
) (const meek
))
171 (defcustom Man-width nil
172 "Number of columns for which manual pages should be formatted.
173 If nil, the width of the window selected at the moment of man
174 invocation is used. If non-nil, the width of the frame selected
175 at the moment of man invocation is used. The value also can be a
177 :type
'(choice (const :tag
"Window width" nil
)
178 (const :tag
"Frame width" t
)
179 (integer :tag
"Specific width" :value
65))
182 (defcustom Man-frame-parameters nil
183 "Frame parameter list for creating a new frame for a manual page."
187 (defcustom Man-downcase-section-letters-flag t
188 "Non-nil means letters in sections are converted to lower case.
189 Some Un*x man commands can't handle uppercase letters in sections, for
190 example \"man 2V chmod\", but they are often displayed in the manpage
191 with the upper case letter. When this variable is t, the section
192 letter (e.g., \"2V\") is converted to lowercase (e.g., \"2v\") before
193 being sent to the man background process."
197 (defcustom Man-circular-pages-flag t
198 "Non-nil means the manpage list is treated as circular for traversal."
202 (defcustom Man-section-translations-alist
205 ;; Some systems have a real 3x man section, so let's comment this.
206 ;; '("3X" . "3") ; Xlib man pages
209 "Association list of bogus sections to real section numbers.
210 Some manpages (e.g. the Sun C++ 2.1 manpages) have section numbers in
211 their references which Un*x `man' does not recognize. This
212 association list is used to translate those sections, when found, to
213 the associated section number."
214 :type
'(repeat (cons (string :tag
"Bogus Section")
215 (string :tag
"Real Section")))
218 (defcustom Man-header-file-path
219 '("/usr/include" "/usr/local/include")
220 "C Header file search path used in Man."
221 :type
'(repeat string
)
224 (defcustom Man-name-local-regexp
(concat "^" (regexp-opt '("NOM" "NAME")) "$")
225 "Regexp that matches the text that precedes the command's name.
226 Used in `bookmark-set' to get the default bookmark name."
227 :type
'string
:group
'bookmark
)
229 (defvar manual-program
"man"
230 "The name of the program that produces man pages.")
232 (defvar Man-untabify-command
"pr"
233 "Command used for untabifying.")
235 (defvar Man-untabify-command-args
(list "-t" "-e")
236 "List of arguments to be passed to `Man-untabify-command' (which see).")
238 (defvar Man-sed-command
"sed"
239 "Command used for processing sed scripts.")
241 (defvar Man-awk-command
"awk"
242 "Command used for processing awk scripts.")
244 (defvar Man-mode-hook nil
245 "Hook run when Man mode is enabled.")
247 (defvar Man-cooked-hook nil
248 "Hook run after removing backspaces but before `Man-mode' processing.")
250 (defvar Man-name-regexp
"[-a-zA-Z0-9_+][-a-zA-Z0-9_.:+]*"
251 "Regular expression describing the name of a manpage (without section).")
253 (defvar Man-section-regexp
"[0-9][a-zA-Z0-9+]*\\|[LNln]"
254 "Regular expression describing a manpage section within parentheses.")
256 (defvar Man-page-header-regexp
257 (if (string-match "-solaris2\\." system-configuration
)
258 (concat "^[-A-Za-z0-9_].*[ \t]\\(" Man-name-regexp
259 "(\\(" Man-section-regexp
"\\))\\)$")
260 (concat "^[ \t]*\\(" Man-name-regexp
261 "(\\(" Man-section-regexp
"\\))\\).*\\1"))
262 "Regular expression describing the heading of a page.")
264 (defvar Man-heading-regexp
"^\\([A-Z][A-Z0-9 /-]+\\)$"
265 "Regular expression describing a manpage heading entry.")
267 (defvar Man-see-also-regexp
"SEE ALSO"
268 "Regular expression for SEE ALSO heading (or your equivalent).
269 This regexp should not start with a `^' character.")
271 ;; This used to have leading space [ \t]*, but was removed because it
272 ;; causes false page splits on an occasional NAME with leading space
273 ;; inside a manpage. And `Man-heading-regexp' doesn't have [ \t]* anyway.
274 (defvar Man-first-heading-regexp
"^NAME$\\|^[ \t]*No manual entry fo.*$"
275 "Regular expression describing first heading on a manpage.
276 This regular expression should start with a `^' character.")
278 (defvar Man-reference-regexp
279 (concat "\\(" Man-name-regexp
280 "\\(\n[ \t]+" Man-name-regexp
"\\)*\\)[ \t]*(\\("
281 Man-section-regexp
"\\))")
282 "Regular expression describing a reference to another manpage.")
284 (defvar Man-apropos-regexp
285 (concat "\\\[\\(" Man-name-regexp
"\\)\\\][ \t]*(\\(" Man-section-regexp
"\\))")
286 "Regular expression describing a reference to manpages in \"man -k output\".")
288 (defvar Man-synopsis-regexp
"SYNOPSIS"
289 "Regular expression for SYNOPSIS heading (or your equivalent).
290 This regexp should not start with a `^' character.")
292 (defvar Man-files-regexp
"FILES\\>"
293 ;; Add \> so as not to match mount(8)'s FILESYSTEM INDEPENDENT MOUNT OPTIONS.
294 "Regular expression for FILES heading (or your equivalent).
295 This regexp should not start with a `^' character.")
297 (defvar Man-include-regexp
"#[ \t]*include[ \t]*"
298 "Regular expression describing the #include (directive of cpp).")
300 (defvar Man-file-name-regexp
"[^<>\", \t\n]+"
301 "Regular expression describing <> in #include line (directive of cpp).")
303 (defvar Man-normal-file-prefix-regexp
"[/~$]"
304 "Regular expression describing a file path appeared in FILES section.")
306 (defvar Man-header-regexp
307 (concat "\\(" Man-include-regexp
"\\)"
309 "\\(" Man-file-name-regexp
"\\)"
311 "Regular expression describing references to header files.")
313 (defvar Man-normal-file-regexp
314 (concat Man-normal-file-prefix-regexp Man-file-name-regexp
)
315 "Regular expression describing references to normal files.")
317 ;; This includes the section as an optional part to catch hyphenated
318 ;; references to manpages.
319 (defvar Man-hyphenated-reference-regexp
320 (concat "\\(" Man-name-regexp
"\\)\\((\\(" Man-section-regexp
"\\))\\)?")
321 "Regular expression describing a reference in the SEE ALSO section.")
323 (defvar Man-switches
""
324 "Switches passed to the man command, as a single string.
326 If you want to be able to see all the manpages for a subject you type,
327 make -a one of the switches, if your `man' program supports it.")
329 (defvar Man-specified-section-option
330 (if (string-match "-solaris[0-9.]*$" system-configuration
)
333 "Option that indicates a specified a manual section name.")
335 (defvar Man-support-local-filenames
'auto-detect
336 "Internal cache for the value of the function `Man-support-local-filenames'.
337 `auto-detect' means the value is not yet determined.
338 Otherwise, the value is whatever the function
339 `Man-support-local-filenames' should return.")
341 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
342 ;; end user variables
344 ;; other variables and keymap initializations
345 (defvar Man-original-frame
)
346 (make-variable-buffer-local 'Man-original-frame
)
347 (defvar Man-arguments
)
348 (make-variable-buffer-local 'Man-arguments
)
349 (put 'Man-arguments
'permanent-local t
)
351 (defvar Man-sections-alist nil
)
352 (make-variable-buffer-local 'Man-sections-alist
)
353 (defvar Man-refpages-alist nil
)
354 (make-variable-buffer-local 'Man-refpages-alist
)
355 (defvar Man-page-list nil
)
356 (make-variable-buffer-local 'Man-page-list
)
357 (defvar Man-current-page
0)
358 (make-variable-buffer-local 'Man-current-page
)
359 (defvar Man-page-mode-string
"1 of 1")
360 (make-variable-buffer-local 'Man-page-mode-string
)
362 (defconst Man-sysv-sed-script
"\
371 /\e\\[[0-9][0-9]*m/ s///g"
372 "Script for sysV-like sed to nuke backspaces and ANSI codes from manpages.")
374 (defconst Man-berkeley-sed-script
"\
380 s/\\(.\\)\b\\1/\\1/g\\
383 /\e\\[[0-9][0-9]*m/ s///g"
384 "Script for berkeley-like sed to nuke backspaces and ANSI codes from manpages.")
386 (defvar Man-topic-history nil
"Topic read history.")
388 (defvar man-mode-syntax-table
389 (let ((table (copy-syntax-table (standard-syntax-table))))
390 (modify-syntax-entry ?.
"w" table
)
391 (modify-syntax-entry ?_
"w" table
)
392 (modify-syntax-entry ?
: "w" table
) ; for PDL::Primitive in Perl man pages
394 "Syntax table used in Man mode buffers.")
397 (let ((map (make-sparse-keymap)))
398 (suppress-keymap map
)
399 (set-keymap-parent map button-buffer-map
)
401 (define-key map
" " 'scroll-up-command
)
402 (define-key map
"\177" 'scroll-down-command
)
403 (define-key map
"n" 'Man-next-section
)
404 (define-key map
"p" 'Man-previous-section
)
405 (define-key map
"\en" 'Man-next-manpage
)
406 (define-key map
"\ep" 'Man-previous-manpage
)
407 (define-key map
">" 'end-of-buffer
)
408 (define-key map
"<" 'beginning-of-buffer
)
409 (define-key map
"." 'beginning-of-buffer
)
410 (define-key map
"r" 'Man-follow-manual-reference
)
411 (define-key map
"g" 'Man-goto-section
)
412 (define-key map
"s" 'Man-goto-see-also-section
)
413 (define-key map
"k" 'Man-kill
)
414 (define-key map
"q" 'Man-quit
)
415 (define-key map
"m" 'man
)
416 ;; Not all the man references get buttons currently. The text in the
417 ;; manual page can contain references to other man pages
418 (define-key map
"\r" 'man-follow
)
419 (define-key map
"?" 'describe-mode
)
421 "Keymap for Man mode.")
424 (define-button-type 'Man-abstract-xref-man-page
426 'help-echo
"mouse-2, RET: display this man page"
428 'action
#'Man-xref-button-action
)
430 (defun Man-xref-button-action (button)
431 (let ((target (button-get button
'Man-target-string
)))
433 (button-get button
'func
)
435 (button-label button
))
437 (funcall target
(button-start button
)))
440 (define-button-type 'Man-xref-man-page
441 :supertype
'Man-abstract-xref-man-page
445 (define-button-type 'Man-xref-header-file
446 'action
(lambda (button)
447 (let ((w (button-get button
'Man-target-string
)))
448 (unless (Man-view-header-file w
)
449 (error "Cannot find header file: %s" w
))))
451 'help-echo
"mouse-2: display this header file")
453 (define-button-type 'Man-xref-normal-file
454 'action
(lambda (button)
455 (let ((f (substitute-in-file-name
456 (button-get button
'Man-target-string
))))
457 (if (file-exists-p f
)
458 (if (file-readable-p f
)
460 (error "Cannot read a file: %s" f
))
461 (error "Cannot find a file: %s" f
))))
463 'help-echo
"mouse-2: display this file")
466 ;; ======================================================================
469 (defun Man-init-defvars ()
470 "Used for initializing variables based on display's color support.
471 This is necessary if one wants to dump man.el with Emacs."
473 ;; Avoid possible error in call-process by using a directory that must exist.
474 (let ((default-directory "/"))
477 (Man-fontify-manpage-flag
479 ((eq 0 (call-process Man-sed-command nil nil nil Man-sysv-sed-script
))
481 ((eq 0 (call-process Man-sed-command nil nil nil Man-berkeley-sed-script
))
482 Man-berkeley-sed-script
)
486 (setq Man-filter-list
487 ;; Avoid trailing nil which confuses customize.
491 (if (eq system-type
'windows-nt
)
492 ;; Windows needs ".." quoting, not '..'.
494 "-e \"/Reformatting page. Wait/d\""
495 "-e \"/Reformatting entry. Wait/d\""
496 "-e \"/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d\""
497 "-e \"/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d\""
498 "-e \"/^Printed[ \t][0-9].*[0-9]$/d\""
499 "-e \"/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d\""
500 "-e \"/^[A-Za-z].*Last[ \t]change:/d\""
501 "-e \"/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d\""
502 "-e \"/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d\"")
505 (concat "-e '" Man-sed-script
"'")
507 "-e '/^[\001-\032][\001-\032]*$/d'"
508 "-e '/\e[789]/s///g'"
509 "-e '/Reformatting page. Wait/d'"
510 "-e '/Reformatting entry. Wait/d'"
511 "-e '/^[ \t]*Hewlett-Packard[ \t]Company[ \t]*-[ \t][0-9]*[ \t]-/d'"
512 "-e '/^[ \t]*Hewlett-Packard[ \t]*-[ \t][0-9]*[ \t]-.*$/d'"
513 "-e '/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d'"
514 "-e '/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d'"
515 "-e '/^Printed[ \t][0-9].*[0-9]$/d'"
516 "-e '/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d'"
517 "-e '/^[A-Za-z].*Last[ \t]change:/d'"
518 "-e '/^Sun[ \t]Release[ \t][0-9].*[0-9]$/d'"
519 "-e '/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d'"
520 "-e '/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d'"
522 ;; Windows doesn't support multi-line commands, so don't
524 (unless (eq system-type
'windows-nt
)
529 "BEGIN { blankline=0; anonblank=0; }\n"
530 "/^$/ { if (anonblank==0) next; }\n"
532 "/^$/ { blankline++; next; }\n"
533 "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }\n"
536 (if (not Man-uses-untabify-flag
)
537 ;; The outer list will be stripped off by apply.
540 Man-untabify-command-args
))
544 (defsubst Man-make-page-mode-string
()
545 "Formats part of the mode line for Man mode."
546 (format "%s page %d of %d"
547 (or (nth 2 (nth (1- Man-current-page
) Man-page-list
))
550 (length Man-page-list
)))
552 (defsubst Man-build-man-command
()
553 "Builds the entire background manpage and cleaning command."
554 (let ((command (concat manual-program
" " Man-switches
557 ((string-match "%s" manual-program
) "")
558 ;; Stock MS-DOS shells cannot redirect stderr;
559 ;; `call-process' below sends it to /dev/null,
560 ;; so we don't need `2>' even with DOS shells
561 ;; which do support stderr redirection.
562 ((not (fboundp 'start-process
)) " %s")
563 ((concat " %s 2>" null-device
)))))
564 (flist Man-filter-list
))
565 (while (and flist
(car flist
))
566 (let ((pcom (car (car flist
)))
567 (pargs (cdr (car flist
))))
569 (concat command
" | " pcom
" "
570 (mapconcat (lambda (phrase)
571 (if (not (stringp phrase
))
572 (error "Malformed Man-filter-list"))
575 (setq flist
(cdr flist
))))
579 (defun Man-translate-cleanup (string)
580 "Strip leading, trailing and middle spaces."
581 (when (stringp string
)
582 ;; Strip leading and trailing
583 (if (string-match "^[ \t\f\r\n]*\\(.+[^ \t\f\r\n]\\)" string
)
584 (setq string
(match-string 1 string
)))
586 (setq string
(replace-regexp-in-string "[\t\r\n]" " " string
))
587 (setq string
(replace-regexp-in-string " +" " " string
))
590 (defun Man-translate-references (ref)
591 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style.
592 Leave it as is if already in that style. Possibly downcase and
593 translate the section (see the `Man-downcase-section-letters-flag'
594 and the `Man-section-translations-alist' variables)."
597 (slist Man-section-translations-alist
))
598 (setq ref
(Man-translate-cleanup ref
))
600 ;; "chmod(2V)" case ?
601 ((string-match (concat "^" Man-reference-regexp
"$") ref
)
602 (setq name
(replace-regexp-in-string "[\n\t ]" "" (match-string 1 ref
))
603 section
(match-string 3 ref
)))
605 ((string-match (concat "^\\(" Man-section-regexp
606 "\\) +\\(" Man-name-regexp
"\\)$") ref
)
607 (setq name
(match-string 2 ref
)
608 section
(match-string 1 ref
))))
609 (if (string= name
"")
610 ref
; Return the reference as is
611 (if Man-downcase-section-letters-flag
612 (setq section
(downcase section
)))
614 (let ((s1 (car (car slist
)))
615 (s2 (cdr (car slist
))))
616 (setq slist
(cdr slist
))
617 (if Man-downcase-section-letters-flag
618 (setq s1
(downcase s1
)))
619 (if (not (string= s1 section
)) nil
620 (setq section
(if Man-downcase-section-letters-flag
624 (concat Man-specified-section-option section
" " name
))))
626 (defun Man-support-local-filenames ()
627 "Return non-nil if the man command supports local filenames.
628 Different man programs support this feature in different ways.
629 The default Debian man program (\"man-db\") has a `--local-file'
630 \(or `-l') option for this purpose. The default Red Hat man
631 program has no such option, but interprets any name containing
632 a \"/\" as a local filename. The function returns either `man-db'
634 (if (eq Man-support-local-filenames
'auto-detect
)
635 (setq Man-support-local-filenames
637 (let ((default-directory
638 ;; Ensure that `default-directory' exists and is readable.
639 (if (and (file-directory-p default-directory
)
640 (file-readable-p default-directory
))
642 (expand-file-name "~/"))))
644 (call-process manual-program nil t nil
"--help")))
645 (cond ((search-backward "--local-file" nil
'move
)
647 ;; This feature seems to be present in at least ver 1.4f,
648 ;; which is about 20 years old.
649 ;; I don't know if this version has an official name?
650 ((looking-at "^man, versione? [1-9]")
652 Man-support-local-filenames
))
655 ;; ======================================================================
656 ;; default man entry: get word near point
658 (defun Man-default-man-entry (&optional pos
)
659 "Guess default manual entry based on the text near position POS.
660 POS defaults to `point'."
661 (let (word start column distance
)
663 (when pos
(goto-char pos
))
665 ;; The default title is the nearest entry-like object before or
667 (if (and (skip-chars-backward " \ta-zA-Z0-9+")
668 (not (zerop (skip-chars-backward "(")))
669 ;; Try to handle the special case where POS is on a
672 (concat "([ \t]*\\(" Man-section-regexp
"\\)[ \t]*)"))
673 ;; We skipped a valid section number backwards, look at
675 (or (and (skip-chars-backward ",; \t")
676 (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:"))))
677 ;; Not a valid entry, move POS after closing paren.
678 (not (setq pos
(match-end 0)))))
679 ;; We have a candidate, make `start' record its starting
682 ;; Otherwise look at char before POS.
684 (if (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:")))
685 ;; Our candidate is just before or around POS.
687 ;; Otherwise record the current column and look backwards.
688 (setq column
(current-column))
689 (skip-chars-backward ",; \t")
690 ;; Record the distance travelled.
691 (setq distance
(- column
(current-column)))
693 (concat "([ \t]*\\(?:" Man-section-regexp
"\\)[ \t]*)"))
694 ;; Skip section number backwards.
695 (goto-char (match-beginning 0))
696 (skip-chars-backward " \t"))
697 (if (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:")))
699 ;; We have a candidate before POS ...
702 (if (and (skip-chars-forward ",; \t")
703 (< (- (current-column) column
) distance
)
704 (looking-at "[-a-zA-Z0-9._+:]"))
705 ;; ... but the one after POS is better.
707 ;; ... and anything after POS is worse.
709 ;; No candidate before POS.
711 (skip-chars-forward ",; \t")
712 (setq start
(point)))))
713 ;; We have found a suitable starting point, try to skip at least
715 (skip-chars-forward "-a-zA-Z0-9._+:")
716 (setq word
(buffer-substring-no-properties start
(point)))
717 ;; If there is a continuation at the end of line, check the
718 ;; following line too, eg:
721 ;; Note: This code gets executed iff our entry is after POS.
722 (when (looking-at "[ \t\r\n]+\\([-a-zA-Z0-9._+:]+\\)([0-9])")
723 (setq word
(concat word
(match-string-no-properties 1)))
724 ;; Make sure the section number gets included by the code below.
725 (goto-char (match-end 1)))
726 (when (string-match "[._]+$" word
)
727 (setq word
(substring word
0 (match-beginning 0))))
728 ;; The following was commented out since the preceding code
729 ;; should not produce a leading "*" in the first place.
730 ;;; ;; If looking at something like *strcat(... , remove the '*'
731 ;;; (when (string-match "^*" word)
732 ;;; (setq word (substring word 1)))
735 (and (not (string-equal word
""))
736 ;; If looking at something like ioctl(2) or brc(1M),
737 ;; include the section number in the returned value.
739 (concat "[ \t]*([ \t]*\\(" Man-section-regexp
"\\)[ \t]*)"))
740 (format "(%s)" (match-string-no-properties 1)))))))
743 ;; ======================================================================
744 ;; Top level command and background process sentinel
746 ;; For compatibility with older versions.
748 (defalias 'manual-entry
'man
)
750 (defvar Man-completion-cache nil
751 ;; On my machine, "man -k" is so fast that a cache makes no sense,
752 ;; but apparently that's not the case in all cases, so let's add a cache.
753 "Cache of completion table of the form (PREFIX . TABLE).")
755 (defun Man-completion-table (string pred action
)
757 ;; This ends up returning t for pretty much any string, and hence leads to
758 ;; spurious "complete but not unique" messages. And since `man' doesn't
759 ;; require-match anyway, there's not point being clever.
760 ;;((eq action 'lambda) (not (string-match "([^)]*\\'" string)))
762 ;; Let SPC (minibuffer-complete-word) insert the space.
763 (complete-with-action action
'("-k ") string pred
))
765 (let ((table (cdr Man-completion-cache
))
768 (when (string-match "\\`\\([[:digit:]].*?\\) " string
)
769 (setq section
(match-string 1 string
))
770 (setq prefix
(substring string
(match-end 0))))
771 (unless (and Man-completion-cache
772 (string-prefix-p (car Man-completion-cache
) prefix
))
774 (setq default-directory
"/") ;; in case inherited doesn't exist
775 ;; Actually for my `man' the arg is a regexp.
776 ;; POSIX says it must be ERE and "man-db" seems to agree,
777 ;; whereas under MacOSX it seems to be BRE-style and doesn't
778 ;; accept backslashes at all. Let's not bother to
780 (let ((process-environment (copy-sequence process-environment
)))
781 (setenv "COLUMNS" "999") ;; don't truncate long names
782 ;; manual-program might not even exist. And since it's
783 ;; run differently in Man-getpage-in-background, an error
784 ;; here may not necessarily mean that we'll also get an
787 (call-process manual-program nil
'(t nil
) nil
788 "-k" (concat "^" prefix
))))
789 (goto-char (point-min))
790 (while (re-search-forward "^\\([^ \t\n]+\\)\\(?: ?\\((.+?)\\)\\(?:[ \t]+- \\(.*\\)\\)?\\)?" nil t
)
791 (push (propertize (concat (match-string 1) (match-string 2))
792 'help-echo
(match-string 3))
794 ;; Cache the table for later reuse.
795 (setq Man-completion-cache
(cons prefix table
)))
796 ;; The table may contain false positives since the match is made
797 ;; by "man -k" not just on the manpage's name.
799 (let ((re (concat "(" (regexp-quote section
) ")\\'")))
800 (dolist (comp (prog1 table
(setq table nil
)))
801 (if (string-match re comp
)
802 (push (substring comp
0 (match-beginning 0)) table
)))
803 (completion-table-with-context (concat section
" ") table
805 ;; If the current text looks like a possible section name,
806 ;; then add a completion entry that just adds a space so SPC
807 ;; can be used to insert a space.
808 (if (string-match "\\`[[:digit:]]" string
)
809 (push (concat string
" ") table
))
810 (let ((res (complete-with-action action table string pred
)))
811 ;; In case we're completing to a single name that exists in
812 ;; several sections, the longest prefix will look like "foo(".
813 (if (and (stringp res
)
814 (string-match "([^(]*\\'" res
)
815 ;; In case the paren was already in `prefix', don't
817 (> (match-beginning 0) (length prefix
)))
818 (substring res
0 (match-beginning 0))
822 (defun man (man-args)
823 "Get a Un*x manual page and put it in a buffer.
824 This command is the top-level command in the man package. It
825 runs a Un*x command to retrieve and clean a manpage in the
826 background and places the results in a `Man-mode' browsing
827 buffer. See variable `Man-notify-method' for what happens when
828 the buffer is ready. If a buffer already exists for this man
829 page, it will display immediately.
831 For a manpage from a particular section, use either of the
832 following. \"cat(1)\" is how cross-references appear and is
833 passed to man as \"1 cat\".
838 To see manpages from all sections related to a subject, use an
839 \"all pages\" option (which might be \"-a\" if it's not the
840 default), then step through with `Man-next-manpage' (\\<Man-mode-map>\\[Man-next-manpage]) etc.
841 Add to `Man-switches' to make this option permanent.
845 An explicit filename can be given too. Use -l if it might
846 otherwise look like a page name.
851 An \"apropos\" query with -k gives a buffer of matching page
852 names or descriptions. The pattern argument is usually an
853 \"egrep\" style regexp.
858 (list (let* ((default-entry (Man-default-man-entry))
859 ;; ignore case because that's friendly for bizarre
860 ;; caps things like the X11 function names and because
861 ;; "man" itself is case-sensitive on the command line
862 ;; so you're accustomed not to bother about the case
863 ;; ("man -k" is case-insensitive similarly, so the
864 ;; table has everything available to complete)
865 (completion-ignore-case t
)
866 (input (completing-read
867 (format "Manual entry%s"
868 (if (string= default-entry
"")
870 (format " (default %s): " default-entry
)))
871 'Man-completion-table
872 nil nil nil
'Man-topic-history default-entry
)))
873 (if (string= input
"")
874 (error "No man args given")
877 ;; Possibly translate the "subject(section)" syntax into the
878 ;; "section subject" syntax and possibly downcase the section.
879 (setq man-args
(Man-translate-references man-args
))
881 (Man-getpage-in-background man-args
))
884 (defun man-follow (man-args)
885 "Get a Un*x manual page of the item under point and put it in a buffer."
886 (interactive (list (Man-default-man-entry)))
887 (if (or (not man-args
)
888 (string= man-args
""))
889 (error "No item under point")
892 (defun Man-getpage-in-background (topic)
893 "Use TOPIC to build and fire off the manpage and cleaning command.
894 Return the buffer in which the manpage will appear."
895 (let* ((man-args topic
)
896 (bufname (concat "*Man " man-args
"*"))
897 (buffer (get-buffer bufname
)))
899 (Man-notify-when-ready buffer
)
901 (message "Invoking %s %s in the background" manual-program man-args
)
902 (setq buffer
(generate-new-buffer bufname
))
903 (with-current-buffer buffer
904 (setq buffer-undo-list t
)
905 (setq Man-original-frame
(selected-frame))
906 (setq Man-arguments man-args
))
907 (let ((process-environment (copy-sequence process-environment
))
908 ;; The following is so Awk script gets \n intact
909 ;; But don't prevent decoding of the outside.
910 (coding-system-for-write 'raw-text-unix
)
911 ;; We must decode the output by a coding system that the
912 ;; system's locale suggests in multibyte mode.
913 (coding-system-for-read
914 (if (default-value 'enable-multibyte-characters
)
915 locale-coding-system
'raw-text-unix
))
916 ;; Avoid possible error by using a directory that always exists.
918 (if (and (file-directory-p default-directory
)
919 (not (find-file-name-handler default-directory
923 ;; Prevent any attempt to use display terminal fanciness.
924 (setenv "TERM" "dumb")
925 ;; In Debian Woody, at least, we get overlong lines under X
926 ;; unless COLUMNS or MANWIDTH is set. This isn't a problem on
927 ;; a tty. man(1) says:
929 ;; If $MANWIDTH is set, its value is used as the line
930 ;; length for which manual pages should be formatted.
931 ;; If it is not set, manual pages will be formatted
932 ;; with a line length appropriate to the current ter-
933 ;; minal (using an ioctl(2) if available, the value of
934 ;; $COLUMNS, or falling back to 80 characters if nei-
935 ;; ther is available).
936 (unless (or (getenv "MANWIDTH") (getenv "COLUMNS"))
937 ;; This isn't strictly correct, since we don't know how
938 ;; the page will actually be displayed, but it seems
940 (setenv "COLUMNS" (number-to-string
942 ((and (integerp Man-width
) (> Man-width
0))
944 (Man-width (frame-width))
946 (setenv "GROFF_NO_SGR" "1")
947 ;; Since man-db 2.4.3-1, man writes plain text with no escape
948 ;; sequences when stdout is not a tty. In 2.5.0, the following
949 ;; env-var was added to allow control of this (see Debian Bug#340673).
950 (setenv "MAN_KEEP_FORMATTING" "1")
951 (if (fboundp 'start-process
)
952 (set-process-sentinel
953 (start-process manual-program buffer
954 (if (memq system-type
'(cygwin windows-nt
))
958 (format (Man-build-man-command) man-args
))
959 'Man-bgproc-sentinel
)
961 (call-process shell-file-name nil
(list buffer nil
) nil
963 (format (Man-build-man-command) man-args
)))
965 (or (and (numberp exit-status
)
967 (and (numberp exit-status
)
969 (format "exited abnormally with code %d"
971 (setq msg exit-status
))
972 (Man-bgproc-sentinel bufname msg
)))))
975 (defun Man-notify-when-ready (man-buffer)
976 "Notify the user when MAN-BUFFER is ready.
977 See the variable `Man-notify-method' for the different notification behaviors."
978 (let ((saved-frame (with-current-buffer man-buffer
979 Man-original-frame
)))
980 (case Man-notify-method
982 ;; Since we run asynchronously, perhaps while Emacs is waiting
983 ;; for input, we must not leave a different buffer current. We
984 ;; can't rely on the editor command loop to reselect the
985 ;; selected window's buffer.
987 (let ((frame (make-frame Man-frame-parameters
)))
988 (set-window-buffer (frame-selected-window frame
) man-buffer
)
989 (set-window-dedicated-p (frame-selected-window frame
) t
)
990 (or (display-multi-frame-p frame
)
991 (select-frame frame
)))))
993 (switch-to-buffer man-buffer
))
995 (and (frame-live-p saved-frame
)
996 (select-frame saved-frame
))
997 (pop-to-buffer man-buffer
)
998 (delete-other-windows))
1000 (and (frame-live-p saved-frame
)
1001 (select-frame saved-frame
))
1002 (pop-to-buffer man-buffer
))
1004 (and (frame-live-p saved-frame
)
1005 (select-frame saved-frame
))
1006 (display-buffer man-buffer
'not-this-window
))
1009 (message "Manual buffer %s is ready" (buffer-name man-buffer
)))
1011 (message "Manual buffer %s is ready" (buffer-name man-buffer
)))
1016 (defun Man-softhyphen-to-minus ()
1017 ;; \255 is SOFT HYPHEN in Latin-N. Versions of Debian man, at
1018 ;; least, emit it even when not in a Latin-N locale.
1019 (unless (eq t
(compare-strings "latin-" 0 nil
1020 current-language-environment
0 6 t
))
1021 (goto-char (point-min))
1023 (if enable-multibyte-characters
1024 (setq str
(string-as-multibyte str
)))
1025 (while (search-forward str nil t
) (replace-match "-")))))
1027 (defun Man-fontify-manpage ()
1028 "Convert overstriking and underlining to the correct fonts.
1029 Same for the ANSI bold and normal escape sequences."
1031 (message "Please wait: formatting the %s man page..." Man-arguments
)
1032 (goto-char (point-min))
1033 ;; Fontify ANSI escapes.
1035 (buffer-undo-list t
)
1037 ;; http://www.isthe.com/chongo/tech/comp/ansi_escapes.html
1038 ;; suggests many codes, but we only handle:
1039 ;; ESC [ 00 m reset to normal display
1041 ;; ESC [ 04 m underline
1042 ;; ESC [ 07 m reverse-video
1043 ;; ESC [ 22 m no-bold
1044 ;; ESC [ 24 m no-underline
1045 ;; ESC [ 27 m no-reverse-video
1046 (while (re-search-forward "\e\\[0?\\([1470]\\|2\\([247]\\)\\)m" nil t
)
1047 (if faces
(put-text-property start
(match-beginning 0) 'face
1048 (if (cdr faces
) faces
(car faces
))))
1051 ((match-beginning 2)
1052 (delq (case (char-after (match-beginning 2))
1053 (?
2 Man-overstrike-face
)
1054 (?
4 Man-underline-face
)
1055 (?
7 Man-reverse-face
))
1057 ((eq (char-after (match-beginning 1)) ?
0) nil
)
1059 (cons (case (char-after (match-beginning 1))
1060 (?
1 Man-overstrike-face
)
1061 (?
4 Man-underline-face
)
1062 (?
7 Man-reverse-face
))
1064 (delete-region (match-beginning 0) (match-end 0))
1065 (setq start
(point))))
1066 ;; Other highlighting.
1067 (let ((buffer-undo-list t
))
1068 (if (< (buffer-size) (position-bytes (point-max)))
1069 ;; Multibyte characters exist.
1071 (goto-char (point-min))
1072 (while (search-forward "__\b\b" nil t
)
1073 (backward-delete-char 4)
1074 (put-text-property (point) (1+ (point)) 'face Man-underline-face
))
1075 (goto-char (point-min))
1076 (while (search-forward "\b\b__" nil t
)
1077 (backward-delete-char 4)
1078 (put-text-property (1- (point)) (point) 'face Man-underline-face
))))
1079 (goto-char (point-min))
1080 (while (search-forward "_\b" nil t
)
1081 (backward-delete-char 2)
1082 (put-text-property (point) (1+ (point)) 'face Man-underline-face
))
1083 (goto-char (point-min))
1084 (while (search-forward "\b_" nil t
)
1085 (backward-delete-char 2)
1086 (put-text-property (1- (point)) (point) 'face Man-underline-face
))
1087 (goto-char (point-min))
1088 (while (re-search-forward "\\(.\\)\\(\b+\\1\\)+" nil t
)
1089 (replace-match "\\1")
1090 (put-text-property (1- (point)) (point) 'face Man-overstrike-face
))
1091 (goto-char (point-min))
1092 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t
)
1094 (put-text-property (1- (point)) (point) 'face
'bold
))
1095 (goto-char (point-min))
1096 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t
)
1098 (put-text-property (1- (point)) (point) 'face
'bold
))
1099 ;; When the header is longer than the manpage name, groff tries to
1100 ;; condense it to a shorter line interspersed with ^H. Remove ^H with
1101 ;; their preceding chars (but don't put Man-overstrike-face). (Bug#5566)
1102 (goto-char (point-min))
1103 (while (re-search-forward ".\b" nil t
) (backward-delete-char 2))
1104 (goto-char (point-min))
1105 ;; Try to recognize common forms of cross references.
1106 (Man-highlight-references)
1107 (Man-softhyphen-to-minus)
1108 (goto-char (point-min))
1109 (while (re-search-forward Man-heading-regexp nil t
)
1110 (put-text-property (match-beginning 0)
1112 'face Man-overstrike-face
)))
1113 (message "%s man page formatted" (Man-page-from-arguments Man-arguments
)))
1115 (defun Man-highlight-references (&optional xref-man-type
)
1116 "Highlight the references on mouse-over.
1117 References include items in the SEE ALSO section,
1118 header file (#include <foo.h>), and files in FILES.
1119 If optional argument XREF-MAN-TYPE is non-nil, it used as the
1120 button type for items in SEE ALSO section. If it is nil, the
1121 default type, `Man-xref-man-page' is used for the buttons."
1122 ;; `Man-highlight-references' is used from woman.el, too.
1123 ;; woman.el doesn't set `Man-arguments'.
1124 (unless Man-arguments
1125 (setq Man-arguments
""))
1126 (if (string-match "-k " Man-arguments
)
1128 (Man-highlight-references0 nil Man-reference-regexp
1
1129 'Man-default-man-entry
1130 (or xref-man-type
'Man-xref-man-page
))
1131 (Man-highlight-references0 nil Man-apropos-regexp
1
1132 'Man-default-man-entry
1133 (or xref-man-type
'Man-xref-man-page
)))
1134 (Man-highlight-references0 Man-see-also-regexp Man-reference-regexp
1
1135 'Man-default-man-entry
1136 (or xref-man-type
'Man-xref-man-page
))
1137 (Man-highlight-references0 Man-synopsis-regexp Man-header-regexp
0 2
1138 'Man-xref-header-file
)
1139 (Man-highlight-references0 Man-files-regexp Man-normal-file-regexp
0 0
1140 'Man-xref-normal-file
)))
1142 (defun Man-highlight-references0 (start-section regexp button-pos target type
)
1143 ;; Based on `Man-build-references-alist'
1144 (when (or (null start-section
)
1145 (Man-find-section start-section
))
1146 (let ((end (if start-section
1149 (back-to-indentation)
1151 (Man-next-section 1)
1153 (goto-char (point-min))
1155 (while (re-search-forward regexp end t
)
1156 ;; An overlay button is preferable because the underlying text
1157 ;; may have text property highlights (Bug#7881).
1159 (match-beginning button-pos
)
1160 (match-end button-pos
)
1162 'Man-target-string
(cond
1164 (match-string target
))
1169 (defun Man-cleanup-manpage (&optional interactive
)
1170 "Remove overstriking and underlining from the current buffer.
1171 Normally skip any jobs that should have been done by the sed script,
1172 but when called interactively, do those jobs even if the sed
1173 script would have done them."
1175 (message "Please wait: cleaning up the %s man page..."
1177 (if (or interactive
(not Man-sed-script
))
1179 (goto-char (point-min))
1180 (while (search-forward "_\b" nil t
) (backward-delete-char 2))
1181 (goto-char (point-min))
1182 (while (search-forward "\b_" nil t
) (backward-delete-char 2))
1183 (goto-char (point-min))
1184 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t
)
1185 (replace-match "\\1"))
1186 (goto-char (point-min))
1187 (while (re-search-forward "\e\\[[0-9]+m" nil t
) (replace-match ""))
1188 (goto-char (point-min))
1189 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t
) (replace-match "o"))
1191 (goto-char (point-min))
1192 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t
) (replace-match "+"))
1193 ;; When the header is longer than the manpage name, groff tries to
1194 ;; condense it to a shorter line interspersed with ^H. Remove ^H with
1195 ;; their preceding chars (but don't put Man-overstrike-face). (Bug#5566)
1196 (goto-char (point-min))
1197 (while (re-search-forward ".\b" nil t
) (backward-delete-char 2))
1198 (Man-softhyphen-to-minus)
1199 (message "%s man page cleaned up" Man-arguments
))
1201 (defun Man-bgproc-sentinel (process msg
)
1202 "Manpage background process sentinel.
1203 When manpage command is run asynchronously, PROCESS is the process
1204 object for the manpage command; when manpage command is run
1205 synchronously, PROCESS is the name of the buffer where the manpage
1206 command is run. Second argument MSG is the exit message of the
1208 (let ((Man-buffer (if (stringp process
) (get-buffer process
)
1209 (process-buffer process
)))
1213 (if (null (buffer-name Man-buffer
)) ;; deleted buffer
1214 (or (stringp process
)
1215 (set-process-buffer process nil
))
1217 (with-current-buffer Man-buffer
1218 (let ((case-fold-search nil
))
1219 (goto-char (point-min))
1220 (cond ((or (looking-at "No \\(manual \\)*entry for")
1221 (looking-at "[^\n]*: nothing appropriate$"))
1222 (setq err-mess
(buffer-substring (point)
1224 (end-of-line) (point)))
1227 ;; "-k foo", successful exit, but no output (from man-db)
1228 ;; ENHANCE-ME: share the check for -k with
1229 ;; `Man-highlight-references'. The \\s- bits here are
1230 ;; meant to allow for multiple options with -k among them.
1231 ((and (string-match "\\(\\`\\|\\s-\\)-k\\s-" Man-arguments
)
1232 (eq (process-status process
) 'exit
)
1233 (= (process-exit-status process
) 0)
1234 (= (point-min) (point-max)))
1235 (setq err-mess
(format "%s: no matches" Man-arguments
)
1238 ((or (stringp process
)
1239 (not (and (eq (process-status process
) 'exit
)
1240 (= (process-exit-status process
) 0))))
1241 (or (zerop (length msg
))
1244 (concat (buffer-name Man-buffer
)
1246 (let ((eos (1- (length msg
))))
1247 (if (= (aref msg eos
) ?
\n)
1248 (substring msg
0 eos
) msg
))))
1249 (goto-char (point-max))
1250 (insert (format "\nprocess %s" msg
))))
1253 (kill-buffer Man-buffer
)
1254 (if Man-fontify-manpage-flag
1255 (Man-fontify-manpage)
1256 (Man-cleanup-manpage))
1258 (run-hooks 'Man-cooked-hook
)
1261 (if (not Man-page-list
)
1262 (let ((args Man-arguments
))
1263 (kill-buffer (current-buffer))
1264 (error "Can't find the %s manpage"
1265 (Man-page-from-arguments args
)))
1266 (set-buffer-modified-p nil
))))
1267 ;; Restore case-fold-search before calling
1268 ;; Man-notify-when-ready because it may switch buffers.
1270 (if (not delete-buff
)
1271 (Man-notify-when-ready Man-buffer
))
1274 (error "%s" err-mess
))
1277 (defun Man-page-from-arguments (args)
1278 ;; Skip arguments and only print the page name.
1284 (and (not (string-match "^-" elem
))
1286 (split-string args
" ")))
1290 ;; ======================================================================
1291 ;; set up manual mode in buffer and build alists
1293 (defvar bookmark-make-record-function
)
1295 (put 'Man-mode
'mode-class
'special
)
1298 "A mode for browsing Un*x manual pages.
1300 The following man commands are available in the buffer. Try
1301 \"\\[describe-key] <key> RET\" for more information:
1303 \\[man] Prompt to retrieve a new manpage.
1304 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
1305 \\[Man-next-manpage] Jump to next manpage in circular list.
1306 \\[Man-previous-manpage] Jump to previous manpage in circular list.
1307 \\[Man-next-section] Jump to next manpage section.
1308 \\[Man-previous-section] Jump to previous manpage section.
1309 \\[Man-goto-section] Go to a manpage section.
1310 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
1311 \\[Man-quit] Deletes the manpage window, bury its buffer.
1312 \\[Man-kill] Deletes the manpage window, kill its buffer.
1313 \\[describe-mode] Prints this help text.
1315 The following variables may be of some use. Try
1316 \"\\[describe-variable] <variable-name> RET\" for more information:
1318 `Man-notify-method' What happens when manpage formatting is done.
1319 `Man-downcase-section-letters-flag' Force section letters to lower case.
1320 `Man-circular-pages-flag' Treat multiple manpage list as circular.
1321 `Man-section-translations-alist' List of section numbers and their Un*x equiv.
1322 `Man-filter-list' Background manpage filter command.
1323 `Man-mode-map' Keymap bindings for Man mode buffers.
1324 `Man-mode-hook' Normal hook run on entry to Man mode.
1325 `Man-section-regexp' Regexp describing manpage section letters.
1326 `Man-heading-regexp' Regexp describing section headers.
1327 `Man-see-also-regexp' Regexp for SEE ALSO section (or your equiv).
1328 `Man-first-heading-regexp' Regexp for first heading on a manpage.
1329 `Man-reference-regexp' Regexp matching a references in SEE ALSO.
1330 `Man-switches' Background `man' command switches.
1332 The following key bindings are currently in effect in the buffer:
1335 (kill-all-local-variables)
1336 (setq major-mode
'Man-mode
1338 buffer-auto-save-file-name nil
1339 mode-line-buffer-identification
1340 (list (default-value 'mode-line-buffer-identification
)
1341 " {" 'Man-page-mode-string
"}")
1344 (buffer-disable-undo)
1346 (use-local-map Man-mode-map
)
1347 (set-syntax-table man-mode-syntax-table
)
1348 (setq imenu-generic-expression
(list (list nil Man-heading-regexp
0)))
1349 (set (make-local-variable 'outline-regexp
) Man-heading-regexp
)
1350 (set (make-local-variable 'outline-level
) (lambda () 1))
1351 (set (make-local-variable 'bookmark-make-record-function
)
1352 'Man-bookmark-make-record
)
1353 (Man-build-page-list)
1354 (Man-strip-page-headers)
1357 (run-mode-hooks 'Man-mode-hook
))
1359 (defsubst Man-build-section-alist
()
1360 "Build the association list of manpage sections."
1361 (setq Man-sections-alist nil
)
1362 (goto-char (point-min))
1363 (let ((case-fold-search nil
))
1364 (while (re-search-forward Man-heading-regexp
(point-max) t
)
1365 (aput 'Man-sections-alist
(match-string 1))
1368 (defsubst Man-build-references-alist
()
1369 "Build the association list of references (in the SEE ALSO section)."
1370 (setq Man-refpages-alist nil
)
1372 (if (Man-find-section Man-see-also-regexp
)
1373 (let ((start (progn (forward-line 1) (point)))
1375 (Man-next-section 1)
1380 (narrow-to-region start end
)
1381 (goto-char (point-min))
1382 (back-to-indentation)
1383 (while (and (not (eobp)) (/= (point) runningpoint
))
1384 (setq runningpoint
(point))
1385 (if (re-search-forward Man-hyphenated-reference-regexp end t
)
1386 (let* ((word (match-string 0))
1387 (len (1- (length word
))))
1389 (setq word
(concat hyphenated word
)
1391 ;; Update len, in case a reference spans
1392 ;; more than two lines (paranoia).
1393 len
(1- (length word
))))
1394 (if (memq (aref word len
) '(?- ?
))
1395 (setq hyphenated
(substring word
0 len
)))
1396 (if (string-match Man-reference-regexp word
)
1397 (aput 'Man-refpages-alist word
))))
1398 (skip-chars-forward " \t\n,"))))))
1399 (setq Man-refpages-alist
(nreverse Man-refpages-alist
)))
1401 (defun Man-build-page-list ()
1402 "Build the list of separate manpages in the buffer."
1403 (setq Man-page-list nil
)
1404 (let ((page-start (point-min))
1405 (page-end (point-max))
1407 (goto-char page-start
)
1408 ;; (switch-to-buffer (current-buffer))(debug)
1411 (if (looking-at Man-page-header-regexp
)
1414 ;; Go past both the current and the next Man-first-heading-regexp
1415 (if (re-search-forward Man-first-heading-regexp nil
'move
2)
1416 (let ((p (progn (beginning-of-line) (point))))
1417 ;; We assume that the page header is delimited by blank
1418 ;; lines and that it contains at most one blank line. So
1419 ;; if we back by three blank lines we will be sure to be
1420 ;; before the page header but not before the possible
1421 ;; previous page header.
1422 (search-backward "\n\n" nil t
3)
1423 (if (re-search-forward Man-page-header-regexp p
'move
)
1424 (beginning-of-line))))
1425 (setq page-end
(point))
1426 (setq Man-page-list
(append Man-page-list
1427 (list (list (copy-marker page-start
)
1428 (copy-marker page-end
)
1430 (setq page-start page-end
)
1433 (defun Man-strip-page-headers ()
1434 "Strip all the page headers but the first from the manpage."
1435 (let ((inhibit-read-only t
)
1436 (case-fold-search nil
)
1438 (dolist (page Man-page-list
)
1440 (goto-char (car page
))
1441 (re-search-forward Man-first-heading-regexp nil t
)
1442 (setq header
(buffer-substring (car page
) (match-beginning 0)))
1443 ;; Since the awk script collapses all successive blank
1444 ;; lines into one, and since we don't want to get rid of
1445 ;; the fast awk script, one must choose between adding
1446 ;; spare blank lines between pages when there were none and
1447 ;; deleting blank lines at page boundaries when there were
1448 ;; some. We choose the first, so we comment the following
1450 ;; (setq header (concat "\n" header)))
1451 (while (search-forward header
(nth 1 page
) t
)
1452 (replace-match ""))))))
1454 (defun Man-unindent ()
1455 "Delete the leading spaces that indent the manpage."
1456 (let ((inhibit-read-only t
)
1457 (case-fold-search nil
))
1458 (dolist (page Man-page-list
)
1461 (narrow-to-region (car page
) (car (cdr page
)))
1462 (if Man-uses-untabify-flag
1463 (untabify (point-min) (point-max)))
1464 (if (catch 'unindent
1465 (goto-char (point-min))
1466 (if (not (re-search-forward Man-first-heading-regexp nil t
))
1467 (throw 'unindent nil
))
1469 (setq indent
(buffer-substring (point)
1471 (skip-chars-forward " ")
1473 (setq nindent
(length indent
))
1475 (throw 'unindent nil
))
1476 (setq indent
(concat indent
"\\|$"))
1477 (goto-char (point-min))
1479 (if (looking-at indent
)
1481 (throw 'unindent nil
)))
1482 (goto-char (point-min)))
1485 (delete-char nindent
))
1490 ;; ======================================================================
1491 ;; Man mode commands
1493 (defun Man-next-section (n)
1494 "Move point to Nth next section (default 1)."
1496 (let ((case-fold-search nil
)
1498 (if (looking-at Man-heading-regexp
)
1500 (if (re-search-forward Man-heading-regexp
(point-max) t n
)
1502 (goto-char (point-max))
1503 ;; The last line doesn't belong to any section.
1505 ;; But don't move back from the starting point (can happen if `start'
1506 ;; is somewhere on the last line).
1507 (if (< (point) start
) (goto-char start
))))
1509 (defun Man-previous-section (n)
1510 "Move point to Nth previous section (default 1)."
1512 (let ((case-fold-search nil
))
1513 (if (looking-at Man-heading-regexp
)
1515 (if (re-search-backward Man-heading-regexp
(point-min) t n
)
1517 (goto-char (point-min)))))
1519 (defun Man-find-section (section)
1520 "Move point to SECTION if it exists, otherwise don't move point.
1521 Returns t if section is found, nil otherwise."
1522 (let ((curpos (point))
1523 (case-fold-search nil
))
1524 (goto-char (point-min))
1525 (if (re-search-forward (concat "^" section
) (point-max) t
)
1526 (progn (beginning-of-line) t
)
1531 (defun Man-goto-section ()
1532 "Query for section to move point to."
1534 (aput 'Man-sections-alist
1535 (let* ((default (aheadsym Man-sections-alist
))
1536 (completion-ignore-case t
)
1538 (prompt (concat "Go to section (default " default
"): ")))
1539 (setq chosen
(completing-read prompt Man-sections-alist
))
1540 (if (or (not chosen
)
1541 (string= chosen
""))
1544 (unless (Man-find-section (aheadsym Man-sections-alist
))
1545 (error "Section not found")))
1548 (defun Man-goto-see-also-section ()
1549 "Move point to the \"SEE ALSO\" section.
1550 Actually the section moved to is described by `Man-see-also-regexp'."
1552 (if (not (Man-find-section Man-see-also-regexp
))
1553 (error "%s" (concat "No " Man-see-also-regexp
1554 " section found in the current manpage"))))
1556 (defun Man-possibly-hyphenated-word ()
1557 "Return a possibly hyphenated word at point.
1558 If the word starts at the first non-whitespace column, and the
1559 previous line ends with a hyphen, return the last word on the previous
1560 line instead. Thus, if a reference to \"tcgetpgrp(3V)\" is hyphenated
1561 as \"tcgetp-grp(3V)\", and point is at \"grp(3V)\", we return
1562 \"tcgetp-\" instead of \"grp\"."
1564 (skip-syntax-backward "w()")
1565 (skip-chars-forward " \t")
1567 (word (current-word)))
1568 (when (eq beg
(save-excursion
1569 (back-to-indentation)
1572 (if (eq (char-before) ?-
)
1573 (setq word
(current-word))))
1576 (defun Man-follow-manual-reference (reference)
1577 "Get one of the manpages referred to in the \"SEE ALSO\" section.
1578 Specify which REFERENCE to use; default is based on word at point."
1580 (if (not Man-refpages-alist
)
1581 (error "There are no references in the current man page")
1584 (car (all-completions
1586 (or (Man-possibly-hyphenated-word)
1588 ;; strip a trailing '-':
1589 (if (string-match "-$" word
)
1591 (match-beginning 0))
1593 Man-refpages-alist
))
1594 (aheadsym Man-refpages-alist
)))
1596 (mapcar 'substring-no-properties
1598 (delq nil
(cons default
1599 (mapcar 'car Man-refpages-alist
))))))
1601 (prompt (concat "Refer to (default " default
"): ")))
1602 (setq chosen
(completing-read prompt Man-refpages-alist
1603 nil nil nil nil defaults
))
1604 (if (or (not chosen
)
1605 (string= chosen
""))
1608 (if (not Man-refpages-alist
)
1609 (error "Can't find any references in the current manpage")
1610 (aput 'Man-refpages-alist reference
)
1611 (Man-getpage-in-background
1612 (Man-translate-references (aheadsym Man-refpages-alist
)))))
1615 "Kill the buffer containing the manpage."
1620 "Bury the buffer containing the manpage."
1624 (defun Man-goto-page (page &optional noerror
)
1625 "Go to the manual page on page PAGE."
1627 (if (not Man-page-list
)
1628 (error "Not a man page buffer")
1629 (if (= (length Man-page-list
) 1)
1630 (error "You're looking at the only manpage in the buffer")
1631 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1632 (length Man-page-list
)))))))
1633 (if (and (not Man-page-list
) (not noerror
))
1634 (error "Not a man page buffer"))
1637 (> page
(length Man-page-list
)))
1638 (error "No manpage %d found" page
))
1639 (let* ((page-range (nth (1- page
) Man-page-list
))
1640 (page-start (car page-range
))
1641 (page-end (car (cdr page-range
))))
1642 (setq Man-current-page page
1643 Man-page-mode-string
(Man-make-page-mode-string))
1645 (goto-char page-start
)
1646 (narrow-to-region page-start page-end
)
1647 (Man-build-section-alist)
1648 (Man-build-references-alist)
1649 (goto-char (point-min)))))
1652 (defun Man-next-manpage ()
1653 "Find the next manpage entry in the buffer."
1655 (if (= (length Man-page-list
) 1)
1656 (error "This is the only manpage in the buffer"))
1657 (if (< Man-current-page
(length Man-page-list
))
1658 (Man-goto-page (1+ Man-current-page
))
1659 (if Man-circular-pages-flag
1661 (error "You're looking at the last manpage in the buffer"))))
1663 (defun Man-previous-manpage ()
1664 "Find the previous manpage entry in the buffer."
1666 (if (= (length Man-page-list
) 1)
1667 (error "This is the only manpage in the buffer"))
1668 (if (> Man-current-page
1)
1669 (Man-goto-page (1- Man-current-page
))
1670 (if Man-circular-pages-flag
1671 (Man-goto-page (length Man-page-list
))
1672 (error "You're looking at the first manpage in the buffer"))))
1674 ;; Header file support
1675 (defun Man-view-header-file (file)
1676 "View a header file specified by FILE from `Man-header-file-path'."
1677 (let ((path Man-header-file-path
)
1680 (setq complete-path
(expand-file-name file
(car path
))
1682 (if (file-readable-p complete-path
)
1683 (progn (view-file complete-path
)
1685 (setq complete-path nil
)))
1688 ;;; Bookmark Man Support
1689 (declare-function bookmark-make-record-default
1690 "bookmark" (&optional no-file no-context posn
))
1691 (declare-function bookmark-prop-get
"bookmark" (bookmark prop
))
1692 (declare-function bookmark-default-handler
"bookmark" (bmk))
1693 (declare-function bookmark-get-bookmark-record
"bookmark" (bmk))
1695 (defun Man-default-bookmark-title ()
1696 "Default bookmark name for Man or WoMan pages.
1697 Uses `Man-name-local-regexp'."
1699 (goto-char (point-min))
1700 (when (re-search-forward Man-name-local-regexp nil t
)
1701 (skip-chars-forward "\n\t ")
1702 (buffer-substring-no-properties (point) (line-end-position)))))
1704 (defun Man-bookmark-make-record ()
1705 "Make a bookmark entry for a Man buffer."
1706 `(,(Man-default-bookmark-title)
1707 ,@(bookmark-make-record-default 'no-file
)
1708 (location .
,(concat "man " Man-arguments
))
1709 (man-args .
,Man-arguments
)
1710 (handler . Man-bookmark-jump
)))
1713 (defun Man-bookmark-jump (bookmark)
1714 "Default bookmark handler for Man buffers."
1715 (let* ((man-args (bookmark-prop-get bookmark
'man-args
))
1716 ;; Let bookmark.el do the window handling.
1717 ;; This let-binding needs to be active during the call to both
1718 ;; Man-getpage-in-background and accept-process-output.
1719 (Man-notify-method 'meek
)
1720 (buf (Man-getpage-in-background man-args
))
1721 (proc (get-buffer-process buf
)))
1722 (while (and proc
(eq (process-status proc
) 'run
))
1723 (accept-process-output proc
))
1724 (bookmark-default-handler
1725 `("" (buffer .
,buf
) .
,(bookmark-get-bookmark-record bookmark
)))))
1728 ;; Init the man package variables, if not already done.
1731 (add-to-list 'debug-ignored-errors
"^No manpage [0-9]* found$")
1732 (add-to-list 'debug-ignored-errors
"^Can't find the .* manpage$")
1736 ;;; man.el ends here