(match): Use yellow background on light-bg terminals.
[emacs.git] / lisp / man.el
bloba6aced25e3d4f4962990f566500e0c0cd914327b
1 ;;; man.el --- browse UNIX manual pages -*- coding: iso-8859-1 -*-
3 ;; Copyright (C) 1993, 1994, 1996, 1997, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Barry A. Warsaw <bwarsaw@cen.com>
7 ;; Maintainer: FSF
8 ;; Keywords: help
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 2, or (at your option)
16 ;; 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; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
28 ;;; Commentary:
30 ;; This code provides a function, `man', with which you can browse
31 ;; UNIX manual pages. Formatting is done in background so that you
32 ;; can continue to use your Emacs while processing is going on.
34 ;; The mode also supports hypertext-like following of manual page SEE
35 ;; ALSO references, and other features. See below or do `?' in a
36 ;; manual page buffer for details.
38 ;; ========== Credits and History ==========
39 ;; In mid 1991, several people posted some interesting improvements to
40 ;; man.el from the standard emacs 18.57 distribution. I liked many of
41 ;; these, but wanted everything in one single package, so I decided
42 ;; to incorporate them into a single manual browsing mode. While
43 ;; much of the code here has been rewritten, and some features added,
44 ;; these folks deserve lots of credit for providing the initial
45 ;; excellent packages on which this one is based.
47 ;; Nick Duffek <duffek@chaos.cs.brandeis.edu>, posted a very nice
48 ;; improvement which retrieved and cleaned the manpages in a
49 ;; background process, and which correctly deciphered such options as
50 ;; man -k.
52 ;; Eric Rose <erose@jessica.stanford.edu>, submitted manual.el which
53 ;; provided a very nice manual browsing mode.
55 ;; This package was available as `superman.el' from the LCD package
56 ;; for some time before it was accepted into Emacs 19. The entry
57 ;; point and some other names have been changed to make it a drop-in
58 ;; replacement for the old man.el package.
60 ;; Francesco Potorti` <pot@cnuce.cnr.it> cleaned it up thoroughly,
61 ;; making it faster, more robust and more tolerant of different
62 ;; systems' man idiosyncrasies.
64 ;; ========== Features ==========
65 ;; + Runs "man" in the background and pipes the results through a
66 ;; series of sed and awk scripts so that all retrieving and cleaning
67 ;; is done in the background. The cleaning commands are configurable.
68 ;; + Syntax is the same as Un*x man
69 ;; + Functionality is the same as Un*x man, including "man -k" and
70 ;; "man <section>", etc.
71 ;; + Provides a manual browsing mode with keybindings for traversing
72 ;; the sections of a manpage, following references in the SEE ALSO
73 ;; section, and more.
74 ;; + Multiple manpages created with the same man command are put into
75 ;; a narrowed buffer circular list.
77 ;; ============= TODO ===========
78 ;; - Add a command for printing.
79 ;; - The awk script deletes multiple blank lines. This behaviour does
80 ;; not allow to understand if there was indeed a blank line at the
81 ;; end or beginning of a page (after the header, or before the
82 ;; footer). A different algorithm should be used. It is easy to
83 ;; compute how many blank lines there are before and after the page
84 ;; headers, and after the page footer. But it is possible to compute
85 ;; the number of blank lines before the page footer by heuristics
86 ;; only. Is it worth doing?
87 ;; - Allow a user option to mean that all the manpages should go in
88 ;; the same buffer, where they can be browsed with M-n and M-p.
89 ;; - Allow completion on the manpage name when calling man. This
90 ;; requires a reliable list of places where manpages can be found. The
91 ;; drawback would be that if the list is not complete, the user might
92 ;; be led to believe that the manpages in the missing directories do
93 ;; not exist.
96 ;;; Code:
98 (eval-when-compile (require 'cl))
99 (require 'assoc)
100 (require 'button)
102 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
103 ;; empty defvars (keep the compiler quiet)
105 (defgroup man nil
106 "Browse UNIX manual pages."
107 :prefix "Man-"
108 :group 'help)
111 (defvar Man-notify)
112 (defvar Man-current-page)
113 (defvar Man-page-list)
114 (defcustom Man-filter-list nil
115 "*Manpage cleaning filter command phrases.
116 This variable contains a list of the following form:
118 '((command-string phrase-string*)*)
120 Each phrase-string is concatenated onto the command-string to form a
121 command filter. The (standard) output (and standard error) of the Un*x
122 man command is piped through each command filter in the order the
123 commands appear in the association list. The final output is placed in
124 the manpage buffer."
125 :type '(repeat (list (string :tag "Command String")
126 (repeat :inline t
127 (string :tag "Phrase String"))))
128 :group 'man)
130 (defvar Man-original-frame)
131 (defvar Man-arguments)
132 (defvar Man-sections-alist)
133 (defvar Man-refpages-alist)
134 (defvar Man-uses-untabify-flag t
135 "Non-nil means use `untabify' instead of `Man-untabify-command'.")
136 (defvar Man-page-mode-string)
137 (defvar Man-sed-script nil
138 "Script for sed to nuke backspaces and ANSI codes from manpages.")
140 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
141 ;; user variables
143 (defcustom Man-fontify-manpage-flag t
144 "*Non-nil means make up the manpage with fonts."
145 :type 'boolean
146 :group 'man)
148 (defcustom Man-overstrike-face 'bold
149 "*Face to use when fontifying overstrike."
150 :type 'face
151 :group 'man)
153 (defcustom Man-underline-face 'underline
154 "*Face to use when fontifying underlining."
155 :type 'face
156 :group 'man)
158 (defcustom Man-reverse-face 'highlight
159 "*Face to use when fontifying reverse video."
160 :type 'face
161 :group 'man)
163 ;; Use the value of the obsolete user option Man-notify, if set.
164 (defcustom Man-notify-method (if (boundp 'Man-notify) Man-notify 'friendly)
165 "*Selects the behavior when manpage is ready.
166 This variable may have one of the following values, where (sf) means
167 that the frames are switched, so the manpage is displayed in the frame
168 where the man command was called from:
170 newframe -- put the manpage in its own frame (see `Man-frame-parameters')
171 pushy -- make the manpage the current buffer in the current window
172 bully -- make the manpage the current buffer and only window (sf)
173 aggressive -- make the manpage the current buffer in the other window (sf)
174 friendly -- display manpage in the other window but don't make current (sf)
175 polite -- don't display manpage, but prints message and beep when ready
176 quiet -- like `polite', but don't beep
177 meek -- make no indication that the manpage is ready
179 Any other value of `Man-notify-method' is equivalent to `meek'."
180 :type '(radio (const newframe) (const pushy) (const bully)
181 (const aggressive) (const friendly)
182 (const polite) (const quiet) (const meek))
183 :group 'man)
185 (defcustom Man-width nil
186 "*Number of columns for which manual pages should be formatted.
187 If nil, the width of the window selected at the moment of man
188 invocation is used. If non-nil, the width of the frame selected
189 at the moment of man invocation is used. The value also can be a
190 positive integer."
191 :type '(choice (const :tag "Window width" nil)
192 (const :tag "Frame width" t)
193 (integer :tag "Specific width" :value 65))
194 :group 'man)
196 (defcustom Man-frame-parameters nil
197 "*Frame parameter list for creating a new frame for a manual page."
198 :type 'sexp
199 :group 'man)
201 (defcustom Man-downcase-section-letters-flag t
202 "*Non-nil means letters in sections are converted to lower case.
203 Some Un*x man commands can't handle uppercase letters in sections, for
204 example \"man 2V chmod\", but they are often displayed in the manpage
205 with the upper case letter. When this variable is t, the section
206 letter (e.g., \"2V\") is converted to lowercase (e.g., \"2v\") before
207 being sent to the man background process."
208 :type 'boolean
209 :group 'man)
211 (defcustom Man-circular-pages-flag t
212 "*Non-nil means the manpage list is treated as circular for traversal."
213 :type 'boolean
214 :group 'man)
216 (defcustom Man-section-translations-alist
217 (list
218 '("3C++" . "3")
219 ;; Some systems have a real 3x man section, so let's comment this.
220 ;; '("3X" . "3") ; Xlib man pages
221 '("3X11" . "3")
222 '("1-UCB" . ""))
223 "*Association list of bogus sections to real section numbers.
224 Some manpages (e.g. the Sun C++ 2.1 manpages) have section numbers in
225 their references which Un*x `man' does not recognize. This
226 association list is used to translate those sections, when found, to
227 the associated section number."
228 :type '(repeat (cons (string :tag "Bogus Section")
229 (string :tag "Real Section")))
230 :group 'man)
232 (defcustom Man-header-file-path
233 '("/usr/include" "/usr/local/include")
234 "C Header file search path used in Man."
235 :type '(repeat string)
236 :group 'man)
238 (defvar manual-program "man"
239 "The name of the program that produces man pages.")
241 (defvar Man-untabify-command "pr"
242 "Command used for untabifying.")
244 (defvar Man-untabify-command-args (list "-t" "-e")
245 "List of arguments to be passed to `Man-untabify-command' (which see).")
247 (defvar Man-sed-command "sed"
248 "Command used for processing sed scripts.")
250 (defvar Man-awk-command "awk"
251 "Command used for processing awk scripts.")
253 (defvar Man-mode-map nil
254 "Keymap for Man mode.")
256 (defvar Man-mode-hook nil
257 "Hook run when Man mode is enabled.")
259 (defvar Man-cooked-hook nil
260 "Hook run after removing backspaces but before `Man-mode' processing.")
262 (defvar Man-name-regexp "[-a-zA-Z0-9_­+][-a-zA-Z0-9_.:­+]*"
263 "Regular expression describing the name of a manpage (without section).")
265 (defvar Man-section-regexp "[0-9][a-zA-Z0-9+]*\\|[LNln]"
266 "Regular expression describing a manpage section within parentheses.")
268 (defvar Man-page-header-regexp
269 (if (and (string-match "-solaris2\\." system-configuration)
270 (not (string-match "-solaris2\\.[123435]$" system-configuration)))
271 (concat "^[-A-Za-z0-9_].*[ \t]\\(" Man-name-regexp
272 "(\\(" Man-section-regexp "\\))\\)$")
273 (concat "^[ \t]*\\(" Man-name-regexp
274 "(\\(" Man-section-regexp "\\))\\).*\\1"))
275 "Regular expression describing the heading of a page.")
277 (defvar Man-heading-regexp "^\\([A-Z][A-Z /-]+\\)$"
278 "Regular expression describing a manpage heading entry.")
280 (defvar Man-see-also-regexp "SEE ALSO"
281 "Regular expression for SEE ALSO heading (or your equivalent).
282 This regexp should not start with a `^' character.")
284 (defvar Man-first-heading-regexp "^[ \t]*NAME$\\|^[ \t]*No manual entry fo.*$"
285 "Regular expression describing first heading on a manpage.
286 This regular expression should start with a `^' character.")
288 (defvar Man-reference-regexp
289 (concat "\\(" Man-name-regexp "\\)[ \t]*(\\(" Man-section-regexp "\\))")
290 "Regular expression describing a reference to another manpage.")
292 (defvar Man-apropos-regexp
293 (concat "\\\[\\(" Man-name-regexp "\\)\\\][ \t]*(\\(" Man-section-regexp "\\))")
294 "Regular expression describing a reference to manpages in \"man -k output\".")
296 (defvar Man-synopsis-regexp "SYNOPSIS"
297 "Regular expression for SYNOPSIS heading (or your equivalent).
298 This regexp should not start with a `^' character.")
300 (defvar Man-files-regexp "FILES"
301 "Regular expression for FILES heading (or your equivalent).
302 This regexp should not start with a `^' character.")
304 (defvar Man-include-regexp "#[ \t]*include[ \t]*"
305 "Regular expression describing the #include (directive of cpp).")
307 (defvar Man-file-name-regexp "[^<>\", \t\n]+"
308 "Regular expression describing <> in #include line (directive of cpp).")
310 (defvar Man-normal-file-prefix-regexp "[/~$]"
311 "Regular expression describing a file path appeared in FILES section.")
313 (defvar Man-header-regexp
314 (concat "\\(" Man-include-regexp "\\)"
315 "[<\"]"
316 "\\(" Man-file-name-regexp "\\)"
317 "[>\"]")
318 "Regular expression describing references to header files.")
320 (defvar Man-normal-file-regexp
321 (concat Man-normal-file-prefix-regexp Man-file-name-regexp)
322 "Regular expression describing references to normal files.")
324 ;; This includes the section as an optional part to catch hyphenated
325 ;; refernces to manpages.
326 (defvar Man-hyphenated-reference-regexp
327 (concat "\\(" Man-name-regexp "\\)\\((\\(" Man-section-regexp "\\))\\)?")
328 "Regular expression describing a reference in the SEE ALSO section.")
330 (defvar Man-switches ""
331 "Switches passed to the man command, as a single string.
333 If you want to be able to see all the manpages for a subject you type,
334 make -a one of the switches, if your `man' program supports it.")
336 (defvar Man-specified-section-option
337 (if (string-match "-solaris[0-9.]*$" system-configuration)
338 "-s"
340 "Option that indicates a specified a manual section name.")
342 (defvar Man-support-local-filenames 'auto-detect
343 "Internal cache for the value of the function `Man-support-local-filenames'.
344 `auto-detect' means the value is not yet determined.
345 Otherwise, the value is whatever the function
346 `Man-support-local-filenames' should return.")
348 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
349 ;; end user variables
351 ;; other variables and keymap initializations
352 (make-variable-buffer-local 'Man-sections-alist)
353 (make-variable-buffer-local 'Man-refpages-alist)
354 (make-variable-buffer-local 'Man-page-list)
355 (make-variable-buffer-local 'Man-current-page)
356 (make-variable-buffer-local 'Man-page-mode-string)
357 (make-variable-buffer-local 'Man-original-frame)
358 (make-variable-buffer-local 'Man-arguments)
359 (put 'Man-arguments 'permanent-local t)
361 (setq-default Man-sections-alist nil)
362 (setq-default Man-refpages-alist nil)
363 (setq-default Man-page-list nil)
364 (setq-default Man-current-page 0)
365 (setq-default Man-page-mode-string "1 of 1")
367 (defconst Man-sysv-sed-script "\
368 /\b/ { s/_\b//g
369 s/\b_//g
370 s/o\b+/o/g
371 s/+\bo/o/g
372 :ovstrk
373 s/\\(.\\)\b\\1/\\1/g
374 t ovstrk
376 /\e\\[[0-9][0-9]*m/ s///g"
377 "Script for sysV-like sed to nuke backspaces and ANSI codes from manpages.")
379 (defconst Man-berkeley-sed-script "\
380 /\b/ { s/_\b//g\\
381 s/\b_//g\\
382 s/o\b+/o/g\\
383 s/+\bo/o/g\\
384 :ovstrk\\
385 s/\\(.\\)\b\\1/\\1/g\\
386 t ovstrk\\
388 /\e\\[[0-9][0-9]*m/ s///g"
389 "Script for berkeley-like sed to nuke backspaces and ANSI codes from manpages.")
391 (defvar Man-topic-history nil "Topic read history.")
393 (defvar man-mode-syntax-table
394 (let ((table (copy-syntax-table (standard-syntax-table))))
395 (modify-syntax-entry ?. "w" table)
396 (modify-syntax-entry ?_ "w" table)
397 (modify-syntax-entry ?: "w" table) ; for PDL::Primitive in Perl man pages
398 table)
399 "Syntax table used in Man mode buffers.")
401 (unless Man-mode-map
402 (setq Man-mode-map (make-sparse-keymap))
403 (suppress-keymap Man-mode-map)
404 (set-keymap-parent Man-mode-map button-buffer-map)
406 (define-key Man-mode-map " " 'scroll-up)
407 (define-key Man-mode-map "\177" 'scroll-down)
408 (define-key Man-mode-map "n" 'Man-next-section)
409 (define-key Man-mode-map "p" 'Man-previous-section)
410 (define-key Man-mode-map "\en" 'Man-next-manpage)
411 (define-key Man-mode-map "\ep" 'Man-previous-manpage)
412 (define-key Man-mode-map ">" 'end-of-buffer)
413 (define-key Man-mode-map "<" 'beginning-of-buffer)
414 (define-key Man-mode-map "." 'beginning-of-buffer)
415 (define-key Man-mode-map "r" 'Man-follow-manual-reference)
416 (define-key Man-mode-map "g" 'Man-goto-section)
417 (define-key Man-mode-map "s" 'Man-goto-see-also-section)
418 (define-key Man-mode-map "k" 'Man-kill)
419 (define-key Man-mode-map "q" 'Man-quit)
420 (define-key Man-mode-map "m" 'man)
421 ;; Not all the man references get buttons currently. The text in the
422 ;; manual page can contain references to other man pages
423 (define-key Man-mode-map "\r" 'man-follow)
424 (define-key Man-mode-map "?" 'describe-mode))
426 ;; buttons
427 (define-button-type 'Man-abstract-xref-man-page
428 'follow-link t
429 'help-echo "mouse-2, RET: display this man page"
430 'func nil
431 'action #'Man-xref-button-action)
433 (defun Man-xref-button-action (button)
434 (let ((target (button-get button 'Man-target-string)))
435 (funcall
436 (button-get button 'func)
437 (cond ((null target)
438 (button-label button))
439 ((functionp target)
440 (funcall target (button-start button)))
441 (t target)))))
443 (define-button-type 'Man-xref-man-page
444 :supertype 'Man-abstract-xref-man-page
445 'func 'man-follow)
448 (define-button-type 'Man-xref-header-file
449 'action (lambda (button)
450 (let ((w (button-get button 'Man-target-string)))
451 (unless (Man-view-header-file w)
452 (error "Cannot find header file: %s" w))))
453 'follow-link t
454 'help-echo "mouse-2: display this header file")
456 (define-button-type 'Man-xref-normal-file
457 'action (lambda (button)
458 (let ((f (substitute-in-file-name
459 (button-get button 'Man-target-string))))
460 (if (file-exists-p f)
461 (if (file-readable-p f)
462 (view-file f)
463 (error "Cannot read a file: %s" f))
464 (error "Cannot find a file: %s" f))))
465 'follow-link t
466 'help-echo "mouse-2: display this file")
469 ;; ======================================================================
470 ;; utilities
472 (defun Man-init-defvars ()
473 "Used for initializing variables based on display's color support.
474 This is necessary if one wants to dump man.el with Emacs."
476 ;; Avoid possible error in call-process by using a directory that must exist.
477 (let ((default-directory "/"))
478 (setq Man-sed-script
479 (cond
480 (Man-fontify-manpage-flag
481 nil)
482 ((eq 0 (call-process Man-sed-command nil nil nil Man-sysv-sed-script))
483 Man-sysv-sed-script)
484 ((eq 0 (call-process Man-sed-command nil nil nil Man-berkeley-sed-script))
485 Man-berkeley-sed-script)
487 nil))))
489 (setq Man-filter-list
490 ;; Avoid trailing nil which confuses customize.
491 (apply 'list
492 (cons
493 Man-sed-command
494 (list
495 (if Man-sed-script
496 (concat "-e '" Man-sed-script "'")
498 "-e '/^[\001-\032][\001-\032]*$/d'"
499 "-e '/\e[789]/s///g'"
500 "-e '/Reformatting page. Wait/d'"
501 "-e '/Reformatting entry. Wait/d'"
502 "-e '/^[ \t]*Hewlett-Packard[ \t]Company[ \t]*-[ \t][0-9]*[ \t]-/d'"
503 "-e '/^[ \t]*Hewlett-Packard[ \t]*-[ \t][0-9]*[ \t]-.*$/d'"
504 "-e '/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d'"
505 "-e '/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d'"
506 "-e '/^Printed[ \t][0-9].*[0-9]$/d'"
507 "-e '/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d'"
508 "-e '/^[A-Za-z].*Last[ \t]change:/d'"
509 "-e '/^Sun[ \t]Release[ \t][0-9].*[0-9]$/d'"
510 "-e '/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d'"
511 "-e '/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d'"
513 (cons
514 Man-awk-command
515 (list
516 "'\n"
517 "BEGIN { blankline=0; anonblank=0; }\n"
518 "/^$/ { if (anonblank==0) next; }\n"
519 "{ anonblank=1; }\n"
520 "/^$/ { blankline++; next; }\n"
521 "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }\n"
524 (if (not Man-uses-untabify-flag)
525 ;; The outer list will be stripped off by apply.
526 (list (cons
527 Man-untabify-command
528 Man-untabify-command-args))
532 (defsubst Man-make-page-mode-string ()
533 "Formats part of the mode line for Man mode."
534 (format "%s page %d of %d"
535 (or (nth 2 (nth (1- Man-current-page) Man-page-list))
537 Man-current-page
538 (length Man-page-list)))
540 (defsubst Man-build-man-command ()
541 "Builds the entire background manpage and cleaning command."
542 (let ((command (concat manual-program " " Man-switches
543 (cond
544 ;; Already has %s
545 ((string-match "%s" manual-program) "")
546 ;; Stock MS-DOS shells cannot redirect stderr;
547 ;; `call-process' below sends it to /dev/null,
548 ;; so we don't need `2>' even with DOS shells
549 ;; which do support stderr redirection.
550 ((not (fboundp 'start-process)) " %s")
551 ((concat " %s 2>" null-device)))))
552 (flist Man-filter-list))
553 (while (and flist (car flist))
554 (let ((pcom (car (car flist)))
555 (pargs (cdr (car flist))))
556 (setq command
557 (concat command " | " pcom " "
558 (mapconcat (lambda (phrase)
559 (if (not (stringp phrase))
560 (error "Malformed Man-filter-list"))
561 phrase)
562 pargs " ")))
563 (setq flist (cdr flist))))
564 command))
567 (defun Man-translate-cleanup (string)
568 "Strip leading, trailing and middle spaces."
569 (when (stringp string)
570 ;; Strip leading and trailing
571 (if (string-match "^[ \t\f\r\n]*\\(.+[^ \t\f\r\n]\\)" string)
572 (setq string (match-string 1 string)))
573 ;; middle spaces
574 (setq string (replace-regexp-in-string "[\t\r\n]" " " string))
575 (setq string (replace-regexp-in-string " +" " " string))
576 string))
578 (defun Man-translate-references (ref)
579 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style.
580 Leave it as is if already in that style. Possibly downcase and
581 translate the section (see the `Man-downcase-section-letters-flag'
582 and the `Man-section-translations-alist' variables)."
583 (let ((name "")
584 (section "")
585 (slist Man-section-translations-alist))
586 (setq ref (Man-translate-cleanup ref))
587 (cond
588 ;; "chmod(2V)" case ?
589 ((string-match (concat "^" Man-reference-regexp "$") ref)
590 (setq name (match-string 1 ref)
591 section (match-string 2 ref)))
592 ;; "2v chmod" case ?
593 ((string-match (concat "^\\(" Man-section-regexp
594 "\\) +\\(" Man-name-regexp "\\)$") ref)
595 (setq name (match-string 2 ref)
596 section (match-string 1 ref))))
597 (if (string= name "")
598 ref ; Return the reference as is
599 (if Man-downcase-section-letters-flag
600 (setq section (downcase section)))
601 (while slist
602 (let ((s1 (car (car slist)))
603 (s2 (cdr (car slist))))
604 (setq slist (cdr slist))
605 (if Man-downcase-section-letters-flag
606 (setq s1 (downcase s1)))
607 (if (not (string= s1 section)) nil
608 (setq section (if Man-downcase-section-letters-flag
609 (downcase s2)
611 slist nil))))
612 (concat Man-specified-section-option section " " name))))
614 (defun Man-support-local-filenames ()
615 "Check the availability of `-l' option of the man command.
616 This option allows `man' to interpret command line arguments
617 as local filenames.
618 Return the value of the variable `Man-support-local-filenames'
619 if it was set to nil or t before the call of this function.
620 If t, the man command supports `-l' option. If nil, it doesn't.
621 Otherwise, if the value of `Man-support-local-filenames'
622 is neither t nor nil, then determine a new value, set it
623 to the variable `Man-support-local-filenames' and return
624 a new value."
625 (if (or (not Man-support-local-filenames)
626 (eq Man-support-local-filenames t))
627 Man-support-local-filenames
628 (setq Man-support-local-filenames
629 (with-temp-buffer
630 (and (equal (condition-case nil
631 (call-process manual-program nil t nil "--help")
632 (error nil))
634 (progn
635 (goto-char (point-min))
636 (search-forward "--local-file" nil t))
637 t)))))
640 ;; ======================================================================
641 ;; default man entry: get word under point
643 (defsubst Man-default-man-entry (&optional pos)
644 "Make a guess at a default manual entry based on the text at POS.
645 If POS is nil, the current point is used."
646 (let (word)
647 (save-excursion
648 (if pos (goto-char pos))
649 ;; Default man entry title is any word the cursor is on, or if
650 ;; cursor not on a word, then nearest preceding word.
651 (skip-chars-backward "-a-zA-Z0-9._+:")
652 (let ((start (point)))
653 (skip-chars-forward "-a-zA-Z0-9._+:")
654 (setq word (buffer-substring-no-properties start (point))))
655 (if (string-match "[._]+$" word)
656 (setq word (substring word 0 (match-beginning 0))))
657 ;; If looking at something like *strcat(... , remove the '*'
658 (if (string-match "^*" word)
659 (setq word (substring word 1)))
660 ;; If looking at something like ioctl(2) or brc(1M), include the
661 ;; section number in the returned value. Remove text properties.
662 (concat word
663 (if (looking-at
664 (concat "[ \t]*([ \t]*\\(" Man-section-regexp "\\)[ \t]*)"))
665 (format "(%s)" (match-string-no-properties 1)))))))
668 ;; ======================================================================
669 ;; Top level command and background process sentinel
671 ;; For compatibility with older versions.
672 ;;;###autoload
673 (defalias 'manual-entry 'man)
676 ;;;###autoload
677 (defun man (man-args)
678 "Get a Un*x manual page and put it in a buffer.
679 This command is the top-level command in the man package. It runs a Un*x
680 command to retrieve and clean a manpage in the background and places the
681 results in a Man mode (manpage browsing) buffer. See variable
682 `Man-notify-method' for what happens when the buffer is ready.
683 If a buffer already exists for this man page, it will display immediately.
685 To specify a man page from a certain section, type SUBJECT(SECTION) or
686 SECTION SUBJECT when prompted for a manual entry. To see manpages from
687 all sections related to a subject, put something appropriate into the
688 `Man-switches' variable, which see."
689 (interactive
690 (list (let* ((default-entry (Man-default-man-entry))
691 (input (read-string
692 (format "Manual entry%s"
693 (if (string= default-entry "")
694 ": "
695 (format " (default %s): " default-entry)))
696 nil 'Man-topic-history default-entry)))
697 (if (string= input "")
698 (error "No man args given")
699 input))))
701 ;; Possibly translate the "subject(section)" syntax into the
702 ;; "section subject" syntax and possibly downcase the section.
703 (setq man-args (Man-translate-references man-args))
705 (Man-getpage-in-background man-args))
707 ;;;###autoload
708 (defun man-follow (man-args)
709 "Get a Un*x manual page of the item under point and put it in a buffer."
710 (interactive (list (Man-default-man-entry)))
711 (if (or (not man-args)
712 (string= man-args ""))
713 (error "No item under point")
714 (man man-args)))
716 (defun Man-getpage-in-background (topic)
717 "Use TOPIC to build and fire off the manpage and cleaning command."
718 (let* ((man-args topic)
719 (bufname (concat "*Man " man-args "*"))
720 (buffer (get-buffer bufname)))
721 (if buffer
722 (Man-notify-when-ready buffer)
723 (require 'env)
724 (message "Invoking %s %s in the background" manual-program man-args)
725 (setq buffer (generate-new-buffer bufname))
726 (save-excursion
727 (set-buffer buffer)
728 (setq buffer-undo-list t)
729 (setq Man-original-frame (selected-frame))
730 (setq Man-arguments man-args))
731 (let ((process-environment (copy-sequence process-environment))
732 ;; The following is so Awk script gets \n intact
733 ;; But don't prevent decoding of the outside.
734 (coding-system-for-write 'raw-text-unix)
735 ;; We must decode the output by a coding system that the
736 ;; system's locale suggests in multibyte mode.
737 (coding-system-for-read
738 (if default-enable-multibyte-characters
739 locale-coding-system 'raw-text-unix))
740 ;; Avoid possible error by using a directory that always exists.
741 (default-directory
742 (if (and (file-directory-p default-directory)
743 (not (find-file-name-handler default-directory
744 'file-directory-p)))
745 default-directory
746 "/")))
747 ;; Prevent any attempt to use display terminal fanciness.
748 (setenv "TERM" "dumb")
749 ;; In Debian Woody, at least, we get overlong lines under X
750 ;; unless COLUMNS or MANWIDTH is set. This isn't a problem on
751 ;; a tty. man(1) says:
752 ;; MANWIDTH
753 ;; If $MANWIDTH is set, its value is used as the line
754 ;; length for which manual pages should be formatted.
755 ;; If it is not set, manual pages will be formatted
756 ;; with a line length appropriate to the current ter-
757 ;; minal (using an ioctl(2) if available, the value of
758 ;; $COLUMNS, or falling back to 80 characters if nei-
759 ;; ther is available).
760 (if window-system
761 (unless (or (getenv "MANWIDTH") (getenv "COLUMNS"))
762 ;; This isn't strictly correct, since we don't know how
763 ;; the page will actually be displayed, but it seems
764 ;; reasonable.
765 (setenv "COLUMNS" (number-to-string
766 (cond
767 ((and (integerp Man-width) (> Man-width 0))
768 Man-width)
769 (Man-width (frame-width))
770 ((window-width)))))))
771 (setenv "GROFF_NO_SGR" "1")
772 (if (fboundp 'start-process)
773 (set-process-sentinel
774 (start-process manual-program buffer
775 (if (memq system-type '(cygwin windows-nt))
776 shell-file-name
777 "sh")
778 shell-command-switch
779 (format (Man-build-man-command) man-args))
780 'Man-bgproc-sentinel)
781 (let ((exit-status
782 (call-process shell-file-name nil (list buffer nil) nil
783 shell-command-switch
784 (format (Man-build-man-command) man-args)))
785 (msg ""))
786 (or (and (numberp exit-status)
787 (= exit-status 0))
788 (and (numberp exit-status)
789 (setq msg
790 (format "exited abnormally with code %d"
791 exit-status)))
792 (setq msg exit-status))
793 (Man-bgproc-sentinel bufname msg)))))))
795 (defun Man-notify-when-ready (man-buffer)
796 "Notify the user when MAN-BUFFER is ready.
797 See the variable `Man-notify-method' for the different notification behaviors."
798 (let ((saved-frame (save-excursion
799 (set-buffer man-buffer)
800 Man-original-frame)))
801 (cond
802 ((eq Man-notify-method 'newframe)
803 ;; Since we run asynchronously, perhaps while Emacs is waiting
804 ;; for input, we must not leave a different buffer current. We
805 ;; can't rely on the editor command loop to reselect the
806 ;; selected window's buffer.
807 (save-excursion
808 (let ((frame (make-frame Man-frame-parameters)))
809 (set-window-buffer (frame-selected-window frame) man-buffer)
810 (set-window-dedicated-p (frame-selected-window frame) t)
811 (or (display-multi-frame-p frame)
812 (select-frame frame)))))
813 ((eq Man-notify-method 'pushy)
814 (switch-to-buffer man-buffer))
815 ((eq Man-notify-method 'bully)
816 (and (frame-live-p saved-frame)
817 (select-frame saved-frame))
818 (pop-to-buffer man-buffer)
819 (delete-other-windows))
820 ((eq Man-notify-method 'aggressive)
821 (and (frame-live-p saved-frame)
822 (select-frame saved-frame))
823 (pop-to-buffer man-buffer))
824 ((eq Man-notify-method 'friendly)
825 (and (frame-live-p saved-frame)
826 (select-frame saved-frame))
827 (display-buffer man-buffer 'not-this-window))
828 ((eq Man-notify-method 'polite)
829 (beep)
830 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
831 ((eq Man-notify-method 'quiet)
832 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
833 ((or (eq Man-notify-method 'meek)
835 (message ""))
838 (defun Man-softhyphen-to-minus ()
839 ;; \255 is SOFT HYPHEN in Latin-N. Versions of Debian man, at
840 ;; least, emit it even when not in a Latin-N locale.
841 (unless (eq t (compare-strings "latin-" 0 nil
842 current-language-environment 0 6 t))
843 (goto-char (point-min))
844 (let ((str "\255"))
845 (if enable-multibyte-characters
846 (setq str (string-as-multibyte str)))
847 (while (search-forward str nil t) (replace-match "-")))))
849 (defun Man-fontify-manpage ()
850 "Convert overstriking and underlining to the correct fonts.
851 Same for the ANSI bold and normal escape sequences."
852 (interactive)
853 (message "Please wait: formatting the %s man page..." Man-arguments)
854 (goto-char (point-min))
855 ;; Fontify ANSI escapes.
856 (let ((faces nil)
857 (buffer-undo-list t)
858 (start (point)))
859 ;; http://www.isthe.com/chongo/tech/comp/ansi_escapes.html
860 ;; suggests many codes, but we only handle:
861 ;; ESC [ 00 m reset to normal display
862 ;; ESC [ 01 m bold
863 ;; ESC [ 04 m underline
864 ;; ESC [ 07 m reverse-video
865 ;; ESC [ 22 m no-bold
866 ;; ESC [ 24 m no-underline
867 ;; ESC [ 27 m no-reverse-video
868 (while (re-search-forward "\e\\[0?\\([1470]\\|2\\([247]\\)\\)m" nil t)
869 (if faces (put-text-property start (match-beginning 0) 'face
870 (if (cdr faces) faces (car faces))))
871 (setq faces
872 (cond
873 ((match-beginning 2)
874 (delq (case (char-after (match-beginning 2))
875 (?2 Man-overstrike-face)
876 (?4 Man-underline-face)
877 (?7 Man-reverse-face))
878 faces))
879 ((eq (char-after (match-beginning 1)) ?0) nil)
881 (cons (case (char-after (match-beginning 1))
882 (?1 Man-overstrike-face)
883 (?4 Man-underline-face)
884 (?7 Man-reverse-face))
885 faces))))
886 (delete-region (match-beginning 0) (match-end 0))
887 (setq start (point))))
888 ;; Other highlighting.
889 (let ((buffer-undo-list t))
890 (if (< (buffer-size) (position-bytes (point-max)))
891 ;; Multibyte characters exist.
892 (progn
893 (goto-char (point-min))
894 (while (search-forward "__\b\b" nil t)
895 (backward-delete-char 4)
896 (put-text-property (point) (1+ (point)) 'face Man-underline-face))
897 (goto-char (point-min))
898 (while (search-forward "\b\b__" nil t)
899 (backward-delete-char 4)
900 (put-text-property (1- (point)) (point) 'face Man-underline-face))))
901 (goto-char (point-min))
902 (while (search-forward "_\b" nil t)
903 (backward-delete-char 2)
904 (put-text-property (point) (1+ (point)) 'face Man-underline-face))
905 (goto-char (point-min))
906 (while (search-forward "\b_" nil t)
907 (backward-delete-char 2)
908 (put-text-property (1- (point)) (point) 'face Man-underline-face))
909 (goto-char (point-min))
910 (while (re-search-forward "\\(.\\)\\(\b+\\1\\)+" nil t)
911 (replace-match "\\1")
912 (put-text-property (1- (point)) (point) 'face Man-overstrike-face))
913 (goto-char (point-min))
914 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t)
915 (replace-match "o")
916 (put-text-property (1- (point)) (point) 'face 'bold))
917 (goto-char (point-min))
918 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t)
919 (replace-match "+")
920 (put-text-property (1- (point)) (point) 'face 'bold))
921 (goto-char (point-min))
922 ;; Try to recognize common forms of cross references.
923 (Man-highlight-references)
924 (Man-softhyphen-to-minus)
925 (goto-char (point-min))
926 (while (re-search-forward Man-heading-regexp nil t)
927 (put-text-property (match-beginning 0)
928 (match-end 0)
929 'face Man-overstrike-face)))
930 (message "%s man page formatted" Man-arguments))
932 (defun Man-highlight-references (&optional xref-man-type)
933 "Highlight the references on mouse-over.
934 References include items in the SEE ALSO section,
935 header file (#include <foo.h>), and files in FILES.
936 If optional argument XREF-MAN-TYPE is non-nil, it used as the
937 button type for items in SEE ALSO section. If it is nil, the
938 default type, `Man-xref-man-page' is used for the buttons."
939 ;; `Man-highlight-references' is used from woman.el, too.
940 ;; woman.el doesn't set `Man-arguments'.
941 (unless Man-arguments
942 (setq Man-arguments ""))
943 (if (string-match "-k " Man-arguments)
944 (progn
945 (Man-highlight-references0 nil Man-reference-regexp 1
946 'Man-default-man-entry
947 (or xref-man-type 'Man-xref-man-page))
948 (Man-highlight-references0 nil Man-apropos-regexp 1
949 'Man-default-man-entry
950 (or xref-man-type 'Man-xref-man-page)))
951 (Man-highlight-references0 Man-see-also-regexp Man-reference-regexp 1
952 'Man-default-man-entry
953 (or xref-man-type 'Man-xref-man-page))
954 (Man-highlight-references0 Man-synopsis-regexp Man-header-regexp 0 2
955 'Man-xref-header-file)
956 (Man-highlight-references0 Man-files-regexp Man-normal-file-regexp 0 0
957 'Man-xref-normal-file)))
959 (defun Man-highlight-references0 (start-section regexp button-pos target type)
960 ;; Based on `Man-build-references-alist'
961 (when (or (null start-section)
962 (Man-find-section start-section))
963 (let ((end (if start-section
964 (progn
965 (forward-line 1)
966 (back-to-indentation)
967 (save-excursion
968 (Man-next-section 1)
969 (point)))
970 (goto-char (point-min))
971 (point-max))))
972 (while (re-search-forward regexp end t)
973 (make-text-button
974 (match-beginning button-pos)
975 (match-end button-pos)
976 'type type
977 'Man-target-string (cond
978 ((numberp target)
979 (match-string target))
980 ((functionp target)
981 target)
982 (t nil)))))))
984 (defun Man-cleanup-manpage (&optional interactive)
985 "Remove overstriking and underlining from the current buffer.
986 Normally skip any jobs that should have been done by the sed script,
987 but when called interactively, do those jobs even if the sed
988 script would have done them."
989 (interactive "p")
990 (message "Please wait: cleaning up the %s man page..."
991 Man-arguments)
992 (if (or interactive (not Man-sed-script))
993 (progn
994 (goto-char (point-min))
995 (while (search-forward "_\b" nil t) (backward-delete-char 2))
996 (goto-char (point-min))
997 (while (search-forward "\b_" nil t) (backward-delete-char 2))
998 (goto-char (point-min))
999 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
1000 (replace-match "\\1"))
1001 (goto-char (point-min))
1002 (while (re-search-forward "\e\\[[0-9]+m" nil t) (replace-match ""))
1003 (goto-char (point-min))
1004 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t) (replace-match "o"))
1006 (goto-char (point-min))
1007 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t) (replace-match "+"))
1008 (Man-softhyphen-to-minus)
1009 (message "%s man page cleaned up" Man-arguments))
1011 (defun Man-bgproc-sentinel (process msg)
1012 "Manpage background process sentinel.
1013 When manpage command is run asynchronously, PROCESS is the process
1014 object for the manpage command; when manpage command is run
1015 synchronously, PROCESS is the name of the buffer where the manpage
1016 command is run. Second argument MSG is the exit message of the
1017 manpage command."
1018 (let ((Man-buffer (if (stringp process) (get-buffer process)
1019 (process-buffer process)))
1020 (delete-buff nil)
1021 (err-mess nil))
1023 (if (null (buffer-name Man-buffer)) ;; deleted buffer
1024 (or (stringp process)
1025 (set-process-buffer process nil))
1027 (save-excursion
1028 (set-buffer Man-buffer)
1029 (let ((case-fold-search nil))
1030 (goto-char (point-min))
1031 (cond ((or (looking-at "No \\(manual \\)*entry for")
1032 (looking-at "[^\n]*: nothing appropriate$"))
1033 (setq err-mess (buffer-substring (point)
1034 (progn
1035 (end-of-line) (point)))
1036 delete-buff t))
1037 ((or (stringp process)
1038 (not (and (eq (process-status process) 'exit)
1039 (= (process-exit-status process) 0))))
1040 (or (zerop (length msg))
1041 (progn
1042 (setq err-mess
1043 (concat (buffer-name Man-buffer)
1044 ": process "
1045 (let ((eos (1- (length msg))))
1046 (if (= (aref msg eos) ?\n)
1047 (substring msg 0 eos) msg))))
1048 (goto-char (point-max))
1049 (insert (format "\nprocess %s" msg))))
1051 (if delete-buff
1052 (kill-buffer Man-buffer)
1053 (if Man-fontify-manpage-flag
1054 (Man-fontify-manpage)
1055 (Man-cleanup-manpage))
1057 (run-hooks 'Man-cooked-hook)
1058 (Man-mode)
1060 (if (not Man-page-list)
1061 (let ((args Man-arguments))
1062 (kill-buffer (current-buffer))
1063 (error "Can't find the %s manpage" args)))
1065 (set-buffer-modified-p nil)
1067 ;; Restore case-fold-search before calling
1068 ;; Man-notify-when-ready because it may switch buffers.
1070 (if (not delete-buff)
1071 (Man-notify-when-ready Man-buffer))
1073 (if err-mess
1074 (error err-mess))
1075 ))))
1078 ;; ======================================================================
1079 ;; set up manual mode in buffer and build alists
1081 (put 'Man-mode 'mode-class 'special)
1083 (defun Man-mode ()
1084 "A mode for browsing Un*x manual pages.
1086 The following man commands are available in the buffer. Try
1087 \"\\[describe-key] <key> RET\" for more information:
1089 \\[man] Prompt to retrieve a new manpage.
1090 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
1091 \\[Man-next-manpage] Jump to next manpage in circular list.
1092 \\[Man-previous-manpage] Jump to previous manpage in circular list.
1093 \\[Man-next-section] Jump to next manpage section.
1094 \\[Man-previous-section] Jump to previous manpage section.
1095 \\[Man-goto-section] Go to a manpage section.
1096 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
1097 \\[Man-quit] Deletes the manpage window, bury its buffer.
1098 \\[Man-kill] Deletes the manpage window, kill its buffer.
1099 \\[describe-mode] Prints this help text.
1101 The following variables may be of some use. Try
1102 \"\\[describe-variable] <variable-name> RET\" for more information:
1104 `Man-notify-method' What happens when manpage formatting is done.
1105 `Man-downcase-section-letters-flag' Force section letters to lower case.
1106 `Man-circular-pages-flag' Treat multiple manpage list as circular.
1107 `Man-section-translations-alist' List of section numbers and their Un*x equiv.
1108 `Man-filter-list' Background manpage filter command.
1109 `Man-mode-map' Keymap bindings for Man mode buffers.
1110 `Man-mode-hook' Normal hook run on entry to Man mode.
1111 `Man-section-regexp' Regexp describing manpage section letters.
1112 `Man-heading-regexp' Regexp describing section headers.
1113 `Man-see-also-regexp' Regexp for SEE ALSO section (or your equiv).
1114 `Man-first-heading-regexp' Regexp for first heading on a manpage.
1115 `Man-reference-regexp' Regexp matching a references in SEE ALSO.
1116 `Man-switches' Background `man' command switches.
1118 The following key bindings are currently in effect in the buffer:
1119 \\{Man-mode-map}"
1120 (interactive)
1121 (kill-all-local-variables)
1122 (setq major-mode 'Man-mode
1123 mode-name "Man"
1124 buffer-auto-save-file-name nil
1125 mode-line-buffer-identification
1126 (list (default-value 'mode-line-buffer-identification)
1127 " {" 'Man-page-mode-string "}")
1128 truncate-lines t
1129 buffer-read-only t)
1130 (buffer-disable-undo)
1131 (auto-fill-mode -1)
1132 (use-local-map Man-mode-map)
1133 (set-syntax-table man-mode-syntax-table)
1134 (setq imenu-generic-expression (list (list nil Man-heading-regexp 0)))
1135 (set (make-local-variable 'outline-regexp) Man-heading-regexp)
1136 (set (make-local-variable 'outline-level) (lambda () 1))
1137 (Man-build-page-list)
1138 (Man-strip-page-headers)
1139 (Man-unindent)
1140 (Man-goto-page 1 t)
1141 (run-mode-hooks 'Man-mode-hook))
1143 (defsubst Man-build-section-alist ()
1144 "Build the association list of manpage sections."
1145 (setq Man-sections-alist nil)
1146 (goto-char (point-min))
1147 (let ((case-fold-search nil))
1148 (while (re-search-forward Man-heading-regexp (point-max) t)
1149 (aput 'Man-sections-alist (match-string 1))
1150 (forward-line 1))))
1152 (defsubst Man-build-references-alist ()
1153 "Build the association list of references (in the SEE ALSO section)."
1154 (setq Man-refpages-alist nil)
1155 (save-excursion
1156 (if (Man-find-section Man-see-also-regexp)
1157 (let ((start (progn (forward-line 1) (point)))
1158 (end (progn
1159 (Man-next-section 1)
1160 (point)))
1161 hyphenated
1162 (runningpoint -1))
1163 (save-restriction
1164 (narrow-to-region start end)
1165 (goto-char (point-min))
1166 (back-to-indentation)
1167 (while (and (not (eobp)) (/= (point) runningpoint))
1168 (setq runningpoint (point))
1169 (if (re-search-forward Man-hyphenated-reference-regexp end t)
1170 (let* ((word (match-string 0))
1171 (len (1- (length word))))
1172 (if hyphenated
1173 (setq word (concat hyphenated word)
1174 hyphenated nil
1175 ;; Update len, in case a reference spans
1176 ;; more than two lines (paranoia).
1177 len (1- (length word))))
1178 (if (memq (aref word len) '(?- ?­))
1179 (setq hyphenated (substring word 0 len)))
1180 (if (string-match Man-reference-regexp word)
1181 (aput 'Man-refpages-alist word))))
1182 (skip-chars-forward " \t\n,"))))))
1183 (setq Man-refpages-alist (nreverse Man-refpages-alist)))
1185 (defun Man-build-page-list ()
1186 "Build the list of separate manpages in the buffer."
1187 (setq Man-page-list nil)
1188 (let ((page-start (point-min))
1189 (page-end (point-max))
1190 (header ""))
1191 (goto-char page-start)
1192 ;; (switch-to-buffer (current-buffer))(debug)
1193 (while (not (eobp))
1194 (setq header
1195 (if (looking-at Man-page-header-regexp)
1196 (match-string 1)
1197 nil))
1198 ;; Go past both the current and the next Man-first-heading-regexp
1199 (if (re-search-forward Man-first-heading-regexp nil 'move 2)
1200 (let ((p (progn (beginning-of-line) (point))))
1201 ;; We assume that the page header is delimited by blank
1202 ;; lines and that it contains at most one blank line. So
1203 ;; if we back by three blank lines we will be sure to be
1204 ;; before the page header but not before the possible
1205 ;; previous page header.
1206 (search-backward "\n\n" nil t 3)
1207 (if (re-search-forward Man-page-header-regexp p 'move)
1208 (beginning-of-line))))
1209 (setq page-end (point))
1210 (setq Man-page-list (append Man-page-list
1211 (list (list (copy-marker page-start)
1212 (copy-marker page-end)
1213 header))))
1214 (setq page-start page-end)
1217 (defun Man-strip-page-headers ()
1218 "Strip all the page headers but the first from the manpage."
1219 (let ((buffer-read-only nil)
1220 (case-fold-search nil)
1221 (page-list Man-page-list)
1222 (page ())
1223 (header ""))
1224 (while page-list
1225 (setq page (car page-list))
1226 (and (nth 2 page)
1227 (goto-char (car page))
1228 (re-search-forward Man-first-heading-regexp nil t)
1229 (setq header (buffer-substring (car page) (match-beginning 0)))
1230 ;; Since the awk script collapses all successive blank
1231 ;; lines into one, and since we don't want to get rid of
1232 ;; the fast awk script, one must choose between adding
1233 ;; spare blank lines between pages when there were none and
1234 ;; deleting blank lines at page boundaries when there were
1235 ;; some. We choose the first, so we comment the following
1236 ;; line.
1237 ;; (setq header (concat "\n" header)))
1238 (while (search-forward header (nth 1 page) t)
1239 (replace-match "")))
1240 (setq page-list (cdr page-list)))))
1242 (defun Man-unindent ()
1243 "Delete the leading spaces that indent the manpage."
1244 (let ((buffer-read-only nil)
1245 (case-fold-search nil)
1246 (page-list Man-page-list))
1247 (while page-list
1248 (let ((page (car page-list))
1249 (indent "")
1250 (nindent 0))
1251 (narrow-to-region (car page) (car (cdr page)))
1252 (if Man-uses-untabify-flag
1253 (untabify (point-min) (point-max)))
1254 (if (catch 'unindent
1255 (goto-char (point-min))
1256 (if (not (re-search-forward Man-first-heading-regexp nil t))
1257 (throw 'unindent nil))
1258 (beginning-of-line)
1259 (setq indent (buffer-substring (point)
1260 (progn
1261 (skip-chars-forward " ")
1262 (point))))
1263 (setq nindent (length indent))
1264 (if (zerop nindent)
1265 (throw 'unindent nil))
1266 (setq indent (concat indent "\\|$"))
1267 (goto-char (point-min))
1268 (while (not (eobp))
1269 (if (looking-at indent)
1270 (forward-line 1)
1271 (throw 'unindent nil)))
1272 (goto-char (point-min)))
1273 (while (not (eobp))
1274 (or (eolp)
1275 (delete-char nindent))
1276 (forward-line 1)))
1277 (setq page-list (cdr page-list))
1278 ))))
1281 ;; ======================================================================
1282 ;; Man mode commands
1284 (defun Man-next-section (n)
1285 "Move point to Nth next section (default 1)."
1286 (interactive "p")
1287 (let ((case-fold-search nil))
1288 (if (looking-at Man-heading-regexp)
1289 (forward-line 1))
1290 (if (re-search-forward Man-heading-regexp (point-max) t n)
1291 (beginning-of-line)
1292 (goto-char (point-max)))))
1294 (defun Man-previous-section (n)
1295 "Move point to Nth previous section (default 1)."
1296 (interactive "p")
1297 (let ((case-fold-search nil))
1298 (if (looking-at Man-heading-regexp)
1299 (forward-line -1))
1300 (if (re-search-backward Man-heading-regexp (point-min) t n)
1301 (beginning-of-line)
1302 (goto-char (point-min)))))
1304 (defun Man-find-section (section)
1305 "Move point to SECTION if it exists, otherwise don't move point.
1306 Returns t if section is found, nil otherwise."
1307 (let ((curpos (point))
1308 (case-fold-search nil))
1309 (goto-char (point-min))
1310 (if (re-search-forward (concat "^" section) (point-max) t)
1311 (progn (beginning-of-line) t)
1312 (goto-char curpos)
1313 nil)
1316 (defun Man-goto-section ()
1317 "Query for section to move point to."
1318 (interactive)
1319 (aput 'Man-sections-alist
1320 (let* ((default (aheadsym Man-sections-alist))
1321 (completion-ignore-case t)
1322 chosen
1323 (prompt (concat "Go to section (default " default "): ")))
1324 (setq chosen (completing-read prompt Man-sections-alist))
1325 (if (or (not chosen)
1326 (string= chosen ""))
1327 default
1328 chosen)))
1329 (Man-find-section (aheadsym Man-sections-alist)))
1331 (defun Man-goto-see-also-section ()
1332 "Move point to the \"SEE ALSO\" section.
1333 Actually the section moved to is described by `Man-see-also-regexp'."
1334 (interactive)
1335 (if (not (Man-find-section Man-see-also-regexp))
1336 (error (concat "No " Man-see-also-regexp
1337 " section found in the current manpage"))))
1339 (defun Man-possibly-hyphenated-word ()
1340 "Return a possibly hyphenated word at point.
1341 If the word starts at the first non-whitespace column, and the
1342 previous line ends with a hyphen, return the last word on the previous
1343 line instead. Thus, if a reference to \"tcgetpgrp(3V)\" is hyphenated
1344 as \"tcgetp-grp(3V)\", and point is at \"grp(3V)\", we return
1345 \"tcgetp-\" instead of \"grp\"."
1346 (save-excursion
1347 (skip-syntax-backward "w()")
1348 (skip-chars-forward " \t")
1349 (let ((beg (point))
1350 (word (current-word)))
1351 (when (eq beg (save-excursion
1352 (back-to-indentation)
1353 (point)))
1354 (end-of-line 0)
1355 (if (eq (char-before) ?-)
1356 (setq word (current-word))))
1357 word)))
1359 (defun Man-follow-manual-reference (reference)
1360 "Get one of the manpages referred to in the \"SEE ALSO\" section.
1361 Specify which REFERENCE to use; default is based on word at point."
1362 (interactive
1363 (if (not Man-refpages-alist)
1364 (error "There are no references in the current man page")
1365 (list (let* ((default (or
1366 (car (all-completions
1367 (let ((word
1368 (or (Man-possibly-hyphenated-word)
1369 "")))
1370 ;; strip a trailing '-':
1371 (if (string-match "-$" word)
1372 (substring word 0
1373 (match-beginning 0))
1374 word))
1375 Man-refpages-alist))
1376 (aheadsym Man-refpages-alist)))
1377 chosen
1378 (prompt (concat "Refer to (default " default "): ")))
1379 (setq chosen (completing-read prompt Man-refpages-alist))
1380 (if (or (not chosen)
1381 (string= chosen ""))
1382 default
1383 chosen)))))
1384 (if (not Man-refpages-alist)
1385 (error "Can't find any references in the current manpage")
1386 (aput 'Man-refpages-alist reference)
1387 (Man-getpage-in-background
1388 (Man-translate-references (aheadsym Man-refpages-alist)))))
1390 (defun Man-kill ()
1391 "Kill the buffer containing the manpage."
1392 (interactive)
1393 (quit-window t))
1395 (defun Man-quit ()
1396 "Bury the buffer containing the manpage."
1397 (interactive)
1398 (quit-window))
1400 (defun Man-goto-page (page &optional noerror)
1401 "Go to the manual page on page PAGE."
1402 (interactive
1403 (if (not Man-page-list)
1404 (error "Not a man page buffer")
1405 (if (= (length Man-page-list) 1)
1406 (error "You're looking at the only manpage in the buffer")
1407 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1408 (length Man-page-list)))))))
1409 (if (and (not Man-page-list) (not noerror))
1410 (error "Not a man page buffer"))
1411 (when Man-page-list
1412 (if (or (< page 1)
1413 (> page (length Man-page-list)))
1414 (error "No manpage %d found" page))
1415 (let* ((page-range (nth (1- page) Man-page-list))
1416 (page-start (car page-range))
1417 (page-end (car (cdr page-range))))
1418 (setq Man-current-page page
1419 Man-page-mode-string (Man-make-page-mode-string))
1420 (widen)
1421 (goto-char page-start)
1422 (narrow-to-region page-start page-end)
1423 (Man-build-section-alist)
1424 (Man-build-references-alist)
1425 (goto-char (point-min)))))
1428 (defun Man-next-manpage ()
1429 "Find the next manpage entry in the buffer."
1430 (interactive)
1431 (if (= (length Man-page-list) 1)
1432 (error "This is the only manpage in the buffer"))
1433 (if (< Man-current-page (length Man-page-list))
1434 (Man-goto-page (1+ Man-current-page))
1435 (if Man-circular-pages-flag
1436 (Man-goto-page 1)
1437 (error "You're looking at the last manpage in the buffer"))))
1439 (defun Man-previous-manpage ()
1440 "Find the previous manpage entry in the buffer."
1441 (interactive)
1442 (if (= (length Man-page-list) 1)
1443 (error "This is the only manpage in the buffer"))
1444 (if (> Man-current-page 1)
1445 (Man-goto-page (1- Man-current-page))
1446 (if Man-circular-pages-flag
1447 (Man-goto-page (length Man-page-list))
1448 (error "You're looking at the first manpage in the buffer"))))
1450 ;; Header file support
1451 (defun Man-view-header-file (file)
1452 "View a header file specified by FILE from `Man-header-file-path'."
1453 (let ((path Man-header-file-path)
1454 complete-path)
1455 (while path
1456 (setq complete-path (concat (car path) "/" file)
1457 path (cdr path))
1458 (if (file-readable-p complete-path)
1459 (progn (view-file complete-path)
1460 (setq path nil))
1461 (setq complete-path nil)))
1462 complete-path))
1464 ;; Init the man package variables, if not already done.
1465 (Man-init-defvars)
1467 (add-to-list 'debug-ignored-errors "^No manpage [0-9]* found$")
1468 (add-to-list 'debug-ignored-errors "^Can't find the .* manpage$")
1470 (provide 'man)
1472 ;; arch-tag: 587cda76-8e23-4594-b1f3-89b6b09a0d47
1473 ;;; man.el ends here