Add :link (custom-group-link font-lock-faces) to defgroup.
[emacs.git] / lisp / man.el
blob5b5096f157da2d53c3d84a9a0f01e2ca9714d422
1 ;;; man.el --- browse UNIX manual pages -*- coding: iso-8859-1 -*-
3 ;; Copyright (C) 1993, 1994, 1996, 1997, 2001, 2002, 2003,
4 ;; 2004, 2005 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-Z+]*\\|[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-mode-syntax-table
392 (let ((table (copy-syntax-table (standard-syntax-table))))
393 (modify-syntax-entry ?. "w" table)
394 (modify-syntax-entry ?_ "w" table)
395 (modify-syntax-entry ?: "w" table) ; for PDL::Primitive in Perl man pages
396 table)
397 "Syntax table used in Man mode buffers.")
399 (unless Man-mode-map
400 (setq Man-mode-map (make-sparse-keymap))
401 (suppress-keymap Man-mode-map)
402 (set-keymap-parent Man-mode-map button-buffer-map)
404 (define-key Man-mode-map " " 'scroll-up)
405 (define-key Man-mode-map "\177" 'scroll-down)
406 (define-key Man-mode-map "n" 'Man-next-section)
407 (define-key Man-mode-map "p" 'Man-previous-section)
408 (define-key Man-mode-map "\en" 'Man-next-manpage)
409 (define-key Man-mode-map "\ep" 'Man-previous-manpage)
410 (define-key Man-mode-map ">" 'end-of-buffer)
411 (define-key Man-mode-map "<" 'beginning-of-buffer)
412 (define-key Man-mode-map "." 'beginning-of-buffer)
413 (define-key Man-mode-map "r" 'Man-follow-manual-reference)
414 (define-key Man-mode-map "g" 'Man-goto-section)
415 (define-key Man-mode-map "s" 'Man-goto-see-also-section)
416 (define-key Man-mode-map "k" 'Man-kill)
417 (define-key Man-mode-map "q" 'Man-quit)
418 (define-key Man-mode-map "m" 'man)
419 (define-key Man-mode-map "?" 'describe-mode))
421 ;; buttons
422 (define-button-type 'Man-abstract-xref-man-page
423 'follow-link t
424 'help-echo "mouse-2, RET: display this man page"
425 'func nil
426 'action (lambda (button) (funcall
427 (button-get button 'func)
428 (or (button-get button 'Man-target-string)
429 (button-label button)))))
431 (define-button-type 'Man-xref-man-page
432 :supertype 'Man-abstract-xref-man-page
433 'func 'man-follow)
436 (define-button-type 'Man-xref-header-file
437 'action (lambda (button)
438 (let ((w (button-get button 'Man-target-string)))
439 (unless (Man-view-header-file w)
440 (error "Cannot find header file: %s" w))))
441 'follow-link t
442 'help-echo "mouse-2: display this header file")
444 (define-button-type 'Man-xref-normal-file
445 'action (lambda (button)
446 (let ((f (substitute-in-file-name
447 (button-get button 'Man-target-string))))
448 (if (file-exists-p f)
449 (if (file-readable-p f)
450 (view-file f)
451 (error "Cannot read a file: %s" f))
452 (error "Cannot find a file: %s" f))))
453 'follow-link t
454 'help-echo "mouse-2: display this file")
457 ;; ======================================================================
458 ;; utilities
460 (defun Man-init-defvars ()
461 "Used for initializing variables based on display's color support.
462 This is necessary if one wants to dump man.el with Emacs."
464 ;; Avoid possible error in call-process by using a directory that must exist.
465 (let ((default-directory "/"))
466 (setq Man-sed-script
467 (cond
468 (Man-fontify-manpage-flag
469 nil)
470 ((eq 0 (call-process Man-sed-command nil nil nil Man-sysv-sed-script))
471 Man-sysv-sed-script)
472 ((eq 0 (call-process Man-sed-command nil nil nil Man-berkeley-sed-script))
473 Man-berkeley-sed-script)
475 nil))))
477 (setq Man-filter-list
478 ;; Avoid trailing nil which confuses customize.
479 (apply 'list
480 (cons
481 Man-sed-command
482 (list
483 (if Man-sed-script
484 (concat "-e '" Man-sed-script "'")
486 "-e '/^[\001-\032][\001-\032]*$/d'"
487 "-e '/\e[789]/s///g'"
488 "-e '/Reformatting page. Wait/d'"
489 "-e '/Reformatting entry. Wait/d'"
490 "-e '/^[ \t]*Hewlett-Packard[ \t]Company[ \t]*-[ \t][0-9]*[ \t]-/d'"
491 "-e '/^[ \t]*Hewlett-Packard[ \t]*-[ \t][0-9]*[ \t]-.*$/d'"
492 "-e '/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d'"
493 "-e '/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d'"
494 "-e '/^Printed[ \t][0-9].*[0-9]$/d'"
495 "-e '/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d'"
496 "-e '/^[A-Za-z].*Last[ \t]change:/d'"
497 "-e '/^Sun[ \t]Release[ \t][0-9].*[0-9]$/d'"
498 "-e '/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d'"
499 "-e '/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d'"
501 (cons
502 Man-awk-command
503 (list
504 "'\n"
505 "BEGIN { blankline=0; anonblank=0; }\n"
506 "/^$/ { if (anonblank==0) next; }\n"
507 "{ anonblank=1; }\n"
508 "/^$/ { blankline++; next; }\n"
509 "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }\n"
512 (if (not Man-uses-untabify-flag)
513 ;; The outer list will be stripped off by apply.
514 (list (cons
515 Man-untabify-command
516 Man-untabify-command-args))
520 (defsubst Man-make-page-mode-string ()
521 "Formats part of the mode line for Man mode."
522 (format "%s page %d of %d"
523 (or (nth 2 (nth (1- Man-current-page) Man-page-list))
525 Man-current-page
526 (length Man-page-list)))
528 (defsubst Man-build-man-command ()
529 "Builds the entire background manpage and cleaning command."
530 (let ((command (concat manual-program " " Man-switches
531 (cond
532 ;; Already has %s
533 ((string-match "%s" manual-program) "")
534 ;; Stock MS-DOS shells cannot redirect stderr;
535 ;; `call-process' below sends it to /dev/null,
536 ;; so we don't need `2>' even with DOS shells
537 ;; which do support stderr redirection.
538 ((not (fboundp 'start-process)) " %s")
539 ((concat " %s 2>" null-device)))))
540 (flist Man-filter-list))
541 (while (and flist (car flist))
542 (let ((pcom (car (car flist)))
543 (pargs (cdr (car flist))))
544 (setq command
545 (concat command " | " pcom " "
546 (mapconcat (lambda (phrase)
547 (if (not (stringp phrase))
548 (error "Malformed Man-filter-list"))
549 phrase)
550 pargs " ")))
551 (setq flist (cdr flist))))
552 command))
555 (defun Man-translate-cleanup (string)
556 "Strip leading, trailing and middle spaces."
557 (when (stringp string)
558 ;; Strip leading and trailing
559 (if (string-match "^[ \t\f\r\n]*\\(.+[^ \t\f\r\n]\\)" string)
560 (setq string (match-string 1 string)))
561 ;; middle spaces
562 (setq string (replace-regexp-in-string "[\t\r\n]" " " string))
563 (setq string (replace-regexp-in-string " +" " " string))
564 string))
566 (defun Man-translate-references (ref)
567 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style.
568 Leave it as is if already in that style. Possibly downcase and
569 translate the section (see the `Man-downcase-section-letters-flag'
570 and the `Man-section-translations-alist' variables)."
571 (let ((name "")
572 (section "")
573 (slist Man-section-translations-alist))
574 (setq ref (Man-translate-cleanup ref))
575 (cond
576 ;; "chmod(2V)" case ?
577 ((string-match (concat "^" Man-reference-regexp "$") ref)
578 (setq name (match-string 1 ref)
579 section (match-string 2 ref)))
580 ;; "2v chmod" case ?
581 ((string-match (concat "^\\(" Man-section-regexp
582 "\\) +\\(" Man-name-regexp "\\)$") ref)
583 (setq name (match-string 2 ref)
584 section (match-string 1 ref))))
585 (if (string= name "")
586 ref ; Return the reference as is
587 (if Man-downcase-section-letters-flag
588 (setq section (downcase section)))
589 (while slist
590 (let ((s1 (car (car slist)))
591 (s2 (cdr (car slist))))
592 (setq slist (cdr slist))
593 (if Man-downcase-section-letters-flag
594 (setq s1 (downcase s1)))
595 (if (not (string= s1 section)) nil
596 (setq section (if Man-downcase-section-letters-flag
597 (downcase s2)
599 slist nil))))
600 (concat Man-specified-section-option section " " name))))
602 (defun Man-support-local-filenames ()
603 "Check the availability of `-l' option of the man command.
604 This option allows `man' to interpret command line arguments
605 as local filenames.
606 Return the value of the variable `Man-support-local-filenames'
607 if it was set to nil or t before the call of this function.
608 If t, the man command supports `-l' option. If nil, it doesn't.
609 Otherwise, if the value of `Man-support-local-filenames'
610 is neither t nor nil, then determine a new value, set it
611 to the variable `Man-support-local-filenames' and return
612 a new value."
613 (if (or (not Man-support-local-filenames)
614 (eq Man-support-local-filenames t))
615 Man-support-local-filenames
616 (setq Man-support-local-filenames
617 (with-temp-buffer
618 (and (equal (condition-case nil
619 (call-process manual-program nil t nil "--help")
620 (error nil))
622 (progn
623 (goto-char (point-min))
624 (search-forward "--local-file" nil t))
625 t)))))
628 ;; ======================================================================
629 ;; default man entry: get word under point
631 (defsubst Man-default-man-entry ()
632 "Make a guess at a default manual entry.
633 This guess is based on the text surrounding the cursor."
634 (let (word)
635 (save-excursion
636 ;; Default man entry title is any word the cursor is on, or if
637 ;; cursor not on a word, then nearest preceding word.
638 (skip-chars-backward "-a-zA-Z0-9._+:")
639 (let ((start (point)))
640 (skip-chars-forward "-a-zA-Z0-9._+:")
641 (setq word (buffer-substring-no-properties start (point))))
642 (if (string-match "[._]+$" word)
643 (setq word (substring word 0 (match-beginning 0))))
644 ;; If looking at something like *strcat(... , remove the '*'
645 (if (string-match "^*" word)
646 (setq word (substring word 1)))
647 ;; If looking at something like ioctl(2) or brc(1M), include the
648 ;; section number in the returned value. Remove text properties.
649 (concat word
650 (if (looking-at
651 (concat "[ \t]*([ \t]*\\(" Man-section-regexp "\\)[ \t]*)"))
652 (format "(%s)" (match-string-no-properties 1)))))))
655 ;; ======================================================================
656 ;; Top level command and background process sentinel
658 ;; For compatibility with older versions.
659 ;;;###autoload
660 (defalias 'manual-entry 'man)
663 ;;;###autoload
664 (defun man (man-args)
665 "Get a Un*x manual page and put it in a buffer.
666 This command is the top-level command in the man package. It runs a Un*x
667 command to retrieve and clean a manpage in the background and places the
668 results in a Man mode (manpage browsing) buffer. See variable
669 `Man-notify-method' for what happens when the buffer is ready.
670 If a buffer already exists for this man page, it will display immediately.
672 To specify a man page from a certain section, type SUBJECT(SECTION) or
673 SECTION SUBJECT when prompted for a manual entry. To see manpages from
674 all sections related to a subject, put something appropriate into the
675 `Man-switches' variable, which see."
676 (interactive
677 (list (let* ((default-entry (Man-default-man-entry))
678 (input (read-string
679 (format "Manual entry%s"
680 (if (string= default-entry "")
681 ": "
682 (format " (default %s): " default-entry)))
683 nil nil default-entry)))
684 (if (string= input "")
685 (error "No man args given")
686 input))))
688 ;; Possibly translate the "subject(section)" syntax into the
689 ;; "section subject" syntax and possibly downcase the section.
690 (setq man-args (Man-translate-references man-args))
692 (Man-getpage-in-background man-args))
694 ;;;###autoload
695 (defun man-follow (man-args)
696 "Get a Un*x manual page of the item under point and put it in a buffer."
697 (interactive (list (Man-default-man-entry)))
698 (if (or (not man-args)
699 (string= man-args ""))
700 (error "No item under point")
701 (man man-args)))
703 (defun Man-getpage-in-background (topic)
704 "Use TOPIC to build and fire off the manpage and cleaning command."
705 (let* ((man-args topic)
706 (bufname (concat "*Man " man-args "*"))
707 (buffer (get-buffer bufname)))
708 (if buffer
709 (Man-notify-when-ready buffer)
710 (require 'env)
711 (message "Invoking %s %s in the background" manual-program man-args)
712 (setq buffer (generate-new-buffer bufname))
713 (save-excursion
714 (set-buffer buffer)
715 (setq buffer-undo-list t)
716 (setq Man-original-frame (selected-frame))
717 (setq Man-arguments man-args))
718 (let ((process-environment (copy-sequence process-environment))
719 ;; The following is so Awk script gets \n intact
720 ;; But don't prevent decoding of the outside.
721 (coding-system-for-write 'raw-text-unix)
722 ;; We must decode the output by a coding system that the
723 ;; system's locale suggests in multibyte mode.
724 (coding-system-for-read
725 (if default-enable-multibyte-characters
726 locale-coding-system 'raw-text-unix))
727 ;; Avoid possible error by using a directory that always exists.
728 (default-directory
729 (if (and (file-directory-p default-directory)
730 (not (find-file-name-handler default-directory
731 'file-directory-p)))
732 default-directory
733 "/")))
734 ;; Prevent any attempt to use display terminal fanciness.
735 (setenv "TERM" "dumb")
736 ;; In Debian Woody, at least, we get overlong lines under X
737 ;; unless COLUMNS or MANWIDTH is set. This isn't a problem on
738 ;; a tty. man(1) says:
739 ;; MANWIDTH
740 ;; If $MANWIDTH is set, its value is used as the line
741 ;; length for which manual pages should be formatted.
742 ;; If it is not set, manual pages will be formatted
743 ;; with a line length appropriate to the current ter-
744 ;; minal (using an ioctl(2) if available, the value of
745 ;; $COLUMNS, or falling back to 80 characters if nei-
746 ;; ther is available).
747 (if window-system
748 (unless (or (getenv "MANWIDTH") (getenv "COLUMNS"))
749 ;; This isn't strictly correct, since we don't know how
750 ;; the page will actually be displayed, but it seems
751 ;; reasonable.
752 (setenv "COLUMNS" (number-to-string
753 (cond
754 ((and (integerp Man-width) (> Man-width 0))
755 Man-width)
756 (Man-width (frame-width))
757 ((window-width)))))))
758 (setenv "GROFF_NO_SGR" "1")
759 (if (fboundp 'start-process)
760 (set-process-sentinel
761 (start-process manual-program buffer
762 (if (memq system-type '(cygwin windows-nt))
763 shell-file-name
764 "sh")
765 shell-command-switch
766 (format (Man-build-man-command) man-args))
767 'Man-bgproc-sentinel)
768 (let ((exit-status
769 (call-process shell-file-name nil (list buffer nil) nil
770 shell-command-switch
771 (format (Man-build-man-command) man-args)))
772 (msg ""))
773 (or (and (numberp exit-status)
774 (= exit-status 0))
775 (and (numberp exit-status)
776 (setq msg
777 (format "exited abnormally with code %d"
778 exit-status)))
779 (setq msg exit-status))
780 (Man-bgproc-sentinel bufname msg)))))))
782 (defun Man-notify-when-ready (man-buffer)
783 "Notify the user when MAN-BUFFER is ready.
784 See the variable `Man-notify-method' for the different notification behaviors."
785 (let ((saved-frame (save-excursion
786 (set-buffer man-buffer)
787 Man-original-frame)))
788 (cond
789 ((eq Man-notify-method 'newframe)
790 ;; Since we run asynchronously, perhaps while Emacs is waiting
791 ;; for input, we must not leave a different buffer current. We
792 ;; can't rely on the editor command loop to reselect the
793 ;; selected window's buffer.
794 (save-excursion
795 (let ((frame (make-frame Man-frame-parameters)))
796 (set-window-buffer (frame-selected-window frame) man-buffer)
797 (set-window-dedicated-p (frame-selected-window frame) t)
798 (or (display-multi-frame-p frame)
799 (select-frame frame)))))
800 ((eq Man-notify-method 'pushy)
801 (switch-to-buffer man-buffer))
802 ((eq Man-notify-method 'bully)
803 (and (frame-live-p saved-frame)
804 (select-frame saved-frame))
805 (pop-to-buffer man-buffer)
806 (delete-other-windows))
807 ((eq Man-notify-method 'aggressive)
808 (and (frame-live-p saved-frame)
809 (select-frame saved-frame))
810 (pop-to-buffer man-buffer))
811 ((eq Man-notify-method 'friendly)
812 (and (frame-live-p saved-frame)
813 (select-frame saved-frame))
814 (display-buffer man-buffer 'not-this-window))
815 ((eq Man-notify-method 'polite)
816 (beep)
817 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
818 ((eq Man-notify-method 'quiet)
819 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
820 ((or (eq Man-notify-method 'meek)
822 (message ""))
825 (defun Man-softhyphen-to-minus ()
826 ;; \255 is SOFT HYPHEN in Latin-N. Versions of Debian man, at
827 ;; least, emit it even when not in a Latin-N locale.
828 (unless (eq t (compare-strings "latin-" 0 nil
829 current-language-environment 0 6 t))
830 (goto-char (point-min))
831 (let ((str "\255"))
832 (if enable-multibyte-characters
833 (setq str (string-as-multibyte str)))
834 (while (search-forward str nil t) (replace-match "-")))))
836 (defun Man-fontify-manpage ()
837 "Convert overstriking and underlining to the correct fonts.
838 Same for the ANSI bold and normal escape sequences."
839 (interactive)
840 (message "Please wait: formatting the %s man page..." Man-arguments)
841 (goto-char (point-min))
842 ;; Fontify ANSI escapes.
843 (let ((faces nil)
844 (buffer-undo-list t)
845 (start (point)))
846 ;; http://www.isthe.com/chongo/tech/comp/ansi_escapes.html
847 ;; suggests many codes, but we only handle:
848 ;; ESC [ 00 m reset to normal display
849 ;; ESC [ 01 m bold
850 ;; ESC [ 04 m underline
851 ;; ESC [ 07 m reverse-video
852 ;; ESC [ 22 m no-bold
853 ;; ESC [ 24 m no-underline
854 ;; ESC [ 27 m no-reverse-video
855 (while (re-search-forward "\e\\[0?\\([1470]\\|2\\([247]\\)\\)m" nil t)
856 (if faces (put-text-property start (match-beginning 0) 'face
857 (if (cdr faces) faces (car faces))))
858 (setq faces
859 (cond
860 ((match-beginning 2)
861 (delq (case (char-after (match-beginning 2))
862 (?2 Man-overstrike-face)
863 (?4 Man-underline-face)
864 (?7 Man-reverse-face))
865 faces))
866 ((eq (char-after (match-beginning 1)) ?0) nil)
868 (cons (case (char-after (match-beginning 1))
869 (?1 Man-overstrike-face)
870 (?4 Man-underline-face)
871 (?7 Man-reverse-face))
872 faces))))
873 (delete-region (match-beginning 0) (match-end 0))
874 (setq start (point))))
875 ;; Other highlighting.
876 (let ((buffer-undo-list t))
877 (if (< (buffer-size) (position-bytes (point-max)))
878 ;; Multibyte characters exist.
879 (progn
880 (goto-char (point-min))
881 (while (search-forward "__\b\b" nil t)
882 (backward-delete-char 4)
883 (put-text-property (point) (1+ (point)) 'face Man-underline-face))
884 (goto-char (point-min))
885 (while (search-forward "\b\b__" nil t)
886 (backward-delete-char 4)
887 (put-text-property (1- (point)) (point) 'face Man-underline-face))))
888 (goto-char (point-min))
889 (while (search-forward "_\b" nil t)
890 (backward-delete-char 2)
891 (put-text-property (point) (1+ (point)) 'face Man-underline-face))
892 (goto-char (point-min))
893 (while (search-forward "\b_" nil t)
894 (backward-delete-char 2)
895 (put-text-property (1- (point)) (point) 'face Man-underline-face))
896 (goto-char (point-min))
897 (while (re-search-forward "\\(.\\)\\(\b+\\1\\)+" nil t)
898 (replace-match "\\1")
899 (put-text-property (1- (point)) (point) 'face Man-overstrike-face))
900 (goto-char (point-min))
901 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t)
902 (replace-match "o")
903 (put-text-property (1- (point)) (point) 'face 'bold))
904 (goto-char (point-min))
905 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t)
906 (replace-match "+")
907 (put-text-property (1- (point)) (point) 'face 'bold))
908 (goto-char (point-min))
909 ;; Try to recognize common forms of cross references.
910 (Man-highlight-references)
911 (Man-softhyphen-to-minus)
912 (goto-char (point-min))
913 (while (re-search-forward Man-heading-regexp nil t)
914 (put-text-property (match-beginning 0)
915 (match-end 0)
916 'face Man-overstrike-face)))
917 (message "%s man page formatted" Man-arguments))
919 (defun Man-highlight-references (&optional xref-man-type)
920 "Highlight the references on mouse-over.
921 References include items in the SEE ALSO section,
922 header file (#include <foo.h>) and files in FILES.
923 If XREF-MAN-TYPE is used as the button type for items
924 in SEE ALSO section. If it is nil, default type,
925 `Man-xref-man-page' is used."
926 ;; `Man-highlight-references' is used from woman.el, too.
927 ;; woman.el doesn't set `Man-arguments'.
928 (unless Man-arguments
929 (setq Man-arguments ""))
930 (if (string-match "-k " Man-arguments)
931 (progn
932 (Man-highlight-references0
933 nil Man-reference-regexp 1 nil
934 (or xref-man-type 'Man-xref-man-page))
935 (Man-highlight-references0
936 nil Man-apropos-regexp 1 (lambda ()
937 (format "%s(%s)"
938 (match-string 1)
939 (match-string 2)))
940 (or xref-man-type 'Man-xref-man-page))
942 (Man-highlight-references0
943 Man-see-also-regexp Man-reference-regexp 1 nil
944 (or xref-man-type 'Man-xref-man-page))
945 (Man-highlight-references0
946 Man-synopsis-regexp Man-header-regexp 0 2
947 'Man-xref-header-file)
948 (Man-highlight-references0
949 Man-files-regexp Man-normal-file-regexp 0 0
950 'Man-xref-normal-file)))
952 (defun Man-highlight-references0 (start-section regexp button-pos target type)
953 ;; Based on `Man-build-references-alist'
954 (when (or (null start-section)
955 (Man-find-section start-section))
956 (let ((end (if start-section
957 (progn
958 (forward-line 1)
959 (back-to-indentation)
960 (save-excursion
961 (Man-next-section 1)
962 (point)))
963 (goto-char (point-min))
964 (point-max))))
965 (while (re-search-forward regexp end t)
966 (make-text-button
967 (match-beginning button-pos)
968 (match-end button-pos)
969 'type type
970 'Man-target-string (cond
971 ((numberp target)
972 (match-string target))
973 ((functionp target)
974 (funcall target))
975 (t nil)))))))
977 (defun Man-cleanup-manpage (&optional interactive)
978 "Remove overstriking and underlining from the current buffer.
979 Normally skip any jobs that should have been done by the sed script,
980 but when called interactively, do those jobs even if the sed
981 script would have done them."
982 (interactive "p")
983 (message "Please wait: cleaning up the %s man page..."
984 Man-arguments)
985 (if (or interactive (not Man-sed-script))
986 (progn
987 (goto-char (point-min))
988 (while (search-forward "_\b" nil t) (backward-delete-char 2))
989 (goto-char (point-min))
990 (while (search-forward "\b_" nil t) (backward-delete-char 2))
991 (goto-char (point-min))
992 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
993 (replace-match "\\1"))
994 (goto-char (point-min))
995 (while (re-search-forward "\e\\[[0-9]+m" nil t) (replace-match ""))
996 (goto-char (point-min))
997 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t) (replace-match "o"))
999 (goto-char (point-min))
1000 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t) (replace-match "+"))
1001 (Man-softhyphen-to-minus)
1002 (message "%s man page cleaned up" Man-arguments))
1004 (defun Man-bgproc-sentinel (process msg)
1005 "Manpage background process sentinel.
1006 When manpage command is run asynchronously, PROCESS is the process
1007 object for the manpage command; when manpage command is run
1008 synchronously, PROCESS is the name of the buffer where the manpage
1009 command is run. Second argument MSG is the exit message of the
1010 manpage command."
1011 (let ((Man-buffer (if (stringp process) (get-buffer process)
1012 (process-buffer process)))
1013 (delete-buff nil)
1014 (err-mess nil))
1016 (if (null (buffer-name Man-buffer)) ;; deleted buffer
1017 (or (stringp process)
1018 (set-process-buffer process nil))
1020 (save-excursion
1021 (set-buffer Man-buffer)
1022 (let ((case-fold-search nil))
1023 (goto-char (point-min))
1024 (cond ((or (looking-at "No \\(manual \\)*entry for")
1025 (looking-at "[^\n]*: nothing appropriate$"))
1026 (setq err-mess (buffer-substring (point)
1027 (progn
1028 (end-of-line) (point)))
1029 delete-buff t))
1030 ((or (stringp process)
1031 (not (and (eq (process-status process) 'exit)
1032 (= (process-exit-status process) 0))))
1033 (or (zerop (length msg))
1034 (progn
1035 (setq err-mess
1036 (concat (buffer-name Man-buffer)
1037 ": process "
1038 (let ((eos (1- (length msg))))
1039 (if (= (aref msg eos) ?\n)
1040 (substring msg 0 eos) msg))))
1041 (goto-char (point-max))
1042 (insert (format "\nprocess %s" msg))))
1044 (if delete-buff
1045 (kill-buffer Man-buffer)
1046 (if Man-fontify-manpage-flag
1047 (Man-fontify-manpage)
1048 (Man-cleanup-manpage))
1050 (run-hooks 'Man-cooked-hook)
1051 (Man-mode)
1053 (if (not Man-page-list)
1054 (let ((args Man-arguments))
1055 (kill-buffer (current-buffer))
1056 (error "Can't find the %s manpage" args)))
1058 (set-buffer-modified-p nil)
1060 ;; Restore case-fold-search before calling
1061 ;; Man-notify-when-ready because it may switch buffers.
1063 (if (not delete-buff)
1064 (Man-notify-when-ready Man-buffer))
1066 (if err-mess
1067 (error err-mess))
1068 ))))
1071 ;; ======================================================================
1072 ;; set up manual mode in buffer and build alists
1074 (put 'Man-mode 'mode-class 'special)
1076 (defun Man-mode ()
1077 "A mode for browsing Un*x manual pages.
1079 The following man commands are available in the buffer. Try
1080 \"\\[describe-key] <key> RET\" for more information:
1082 \\[man] Prompt to retrieve a new manpage.
1083 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
1084 \\[Man-next-manpage] Jump to next manpage in circular list.
1085 \\[Man-previous-manpage] Jump to previous manpage in circular list.
1086 \\[Man-next-section] Jump to next manpage section.
1087 \\[Man-previous-section] Jump to previous manpage section.
1088 \\[Man-goto-section] Go to a manpage section.
1089 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
1090 \\[Man-quit] Deletes the manpage window, bury its buffer.
1091 \\[Man-kill] Deletes the manpage window, kill its buffer.
1092 \\[describe-mode] Prints this help text.
1094 The following variables may be of some use. Try
1095 \"\\[describe-variable] <variable-name> RET\" for more information:
1097 `Man-notify-method' What happens when manpage formatting is done.
1098 `Man-downcase-section-letters-flag' Force section letters to lower case.
1099 `Man-circular-pages-flag' Treat multiple manpage list as circular.
1100 `Man-section-translations-alist' List of section numbers and their Un*x equiv.
1101 `Man-filter-list' Background manpage filter command.
1102 `Man-mode-map' Keymap bindings for Man mode buffers.
1103 `Man-mode-hook' Normal hook run on entry to Man mode.
1104 `Man-section-regexp' Regexp describing manpage section letters.
1105 `Man-heading-regexp' Regexp describing section headers.
1106 `Man-see-also-regexp' Regexp for SEE ALSO section (or your equiv).
1107 `Man-first-heading-regexp' Regexp for first heading on a manpage.
1108 `Man-reference-regexp' Regexp matching a references in SEE ALSO.
1109 `Man-switches' Background `man' command switches.
1111 The following key bindings are currently in effect in the buffer:
1112 \\{Man-mode-map}"
1113 (interactive)
1114 (kill-all-local-variables)
1115 (setq major-mode 'Man-mode
1116 mode-name "Man"
1117 buffer-auto-save-file-name nil
1118 mode-line-buffer-identification
1119 (list (default-value 'mode-line-buffer-identification)
1120 " {" 'Man-page-mode-string "}")
1121 truncate-lines t
1122 buffer-read-only t)
1123 (buffer-disable-undo)
1124 (auto-fill-mode -1)
1125 (use-local-map Man-mode-map)
1126 (set-syntax-table man-mode-syntax-table)
1127 (setq imenu-generic-expression (list (list nil Man-heading-regexp 0)))
1128 (set (make-local-variable 'outline-regexp) Man-heading-regexp)
1129 (set (make-local-variable 'outline-level) (lambda () 1))
1130 (Man-build-page-list)
1131 (Man-strip-page-headers)
1132 (Man-unindent)
1133 (Man-goto-page 1 t)
1134 (run-mode-hooks 'Man-mode-hook))
1136 (defsubst Man-build-section-alist ()
1137 "Build the association list of manpage sections."
1138 (setq Man-sections-alist nil)
1139 (goto-char (point-min))
1140 (let ((case-fold-search nil))
1141 (while (re-search-forward Man-heading-regexp (point-max) t)
1142 (aput 'Man-sections-alist (match-string 1))
1143 (forward-line 1))))
1145 (defsubst Man-build-references-alist ()
1146 "Build the association list of references (in the SEE ALSO section)."
1147 (setq Man-refpages-alist nil)
1148 (save-excursion
1149 (if (Man-find-section Man-see-also-regexp)
1150 (let ((start (progn (forward-line 1) (point)))
1151 (end (progn
1152 (Man-next-section 1)
1153 (point)))
1154 hyphenated
1155 (runningpoint -1))
1156 (save-restriction
1157 (narrow-to-region start end)
1158 (goto-char (point-min))
1159 (back-to-indentation)
1160 (while (and (not (eobp)) (/= (point) runningpoint))
1161 (setq runningpoint (point))
1162 (if (re-search-forward Man-hyphenated-reference-regexp end t)
1163 (let* ((word (match-string 0))
1164 (len (1- (length word))))
1165 (if hyphenated
1166 (setq word (concat hyphenated word)
1167 hyphenated nil
1168 ;; Update len, in case a reference spans
1169 ;; more than two lines (paranoia).
1170 len (1- (length word))))
1171 (if (memq (aref word len) '(?- ?­))
1172 (setq hyphenated (substring word 0 len)))
1173 (if (string-match Man-reference-regexp word)
1174 (aput 'Man-refpages-alist word))))
1175 (skip-chars-forward " \t\n,"))))))
1176 (setq Man-refpages-alist (nreverse Man-refpages-alist)))
1178 (defun Man-build-page-list ()
1179 "Build the list of separate manpages in the buffer."
1180 (setq Man-page-list nil)
1181 (let ((page-start (point-min))
1182 (page-end (point-max))
1183 (header ""))
1184 (goto-char page-start)
1185 ;; (switch-to-buffer (current-buffer))(debug)
1186 (while (not (eobp))
1187 (setq header
1188 (if (looking-at Man-page-header-regexp)
1189 (match-string 1)
1190 nil))
1191 ;; Go past both the current and the next Man-first-heading-regexp
1192 (if (re-search-forward Man-first-heading-regexp nil 'move 2)
1193 (let ((p (progn (beginning-of-line) (point))))
1194 ;; We assume that the page header is delimited by blank
1195 ;; lines and that it contains at most one blank line. So
1196 ;; if we back by three blank lines we will be sure to be
1197 ;; before the page header but not before the possible
1198 ;; previous page header.
1199 (search-backward "\n\n" nil t 3)
1200 (if (re-search-forward Man-page-header-regexp p 'move)
1201 (beginning-of-line))))
1202 (setq page-end (point))
1203 (setq Man-page-list (append Man-page-list
1204 (list (list (copy-marker page-start)
1205 (copy-marker page-end)
1206 header))))
1207 (setq page-start page-end)
1210 (defun Man-strip-page-headers ()
1211 "Strip all the page headers but the first from the manpage."
1212 (let ((buffer-read-only nil)
1213 (case-fold-search nil)
1214 (page-list Man-page-list)
1215 (page ())
1216 (header ""))
1217 (while page-list
1218 (setq page (car page-list))
1219 (and (nth 2 page)
1220 (goto-char (car page))
1221 (re-search-forward Man-first-heading-regexp nil t)
1222 (setq header (buffer-substring (car page) (match-beginning 0)))
1223 ;; Since the awk script collapses all successive blank
1224 ;; lines into one, and since we don't want to get rid of
1225 ;; the fast awk script, one must choose between adding
1226 ;; spare blank lines between pages when there were none and
1227 ;; deleting blank lines at page boundaries when there were
1228 ;; some. We choose the first, so we comment the following
1229 ;; line.
1230 ;; (setq header (concat "\n" header)))
1231 (while (search-forward header (nth 1 page) t)
1232 (replace-match "")))
1233 (setq page-list (cdr page-list)))))
1235 (defun Man-unindent ()
1236 "Delete the leading spaces that indent the manpage."
1237 (let ((buffer-read-only nil)
1238 (case-fold-search nil)
1239 (page-list Man-page-list))
1240 (while page-list
1241 (let ((page (car page-list))
1242 (indent "")
1243 (nindent 0))
1244 (narrow-to-region (car page) (car (cdr page)))
1245 (if Man-uses-untabify-flag
1246 (untabify (point-min) (point-max)))
1247 (if (catch 'unindent
1248 (goto-char (point-min))
1249 (if (not (re-search-forward Man-first-heading-regexp nil t))
1250 (throw 'unindent nil))
1251 (beginning-of-line)
1252 (setq indent (buffer-substring (point)
1253 (progn
1254 (skip-chars-forward " ")
1255 (point))))
1256 (setq nindent (length indent))
1257 (if (zerop nindent)
1258 (throw 'unindent nil))
1259 (setq indent (concat indent "\\|$"))
1260 (goto-char (point-min))
1261 (while (not (eobp))
1262 (if (looking-at indent)
1263 (forward-line 1)
1264 (throw 'unindent nil)))
1265 (goto-char (point-min)))
1266 (while (not (eobp))
1267 (or (eolp)
1268 (delete-char nindent))
1269 (forward-line 1)))
1270 (setq page-list (cdr page-list))
1271 ))))
1274 ;; ======================================================================
1275 ;; Man mode commands
1277 (defun Man-next-section (n)
1278 "Move point to Nth next section (default 1)."
1279 (interactive "p")
1280 (let ((case-fold-search nil))
1281 (if (looking-at Man-heading-regexp)
1282 (forward-line 1))
1283 (if (re-search-forward Man-heading-regexp (point-max) t n)
1284 (beginning-of-line)
1285 (goto-char (point-max)))))
1287 (defun Man-previous-section (n)
1288 "Move point to Nth previous section (default 1)."
1289 (interactive "p")
1290 (let ((case-fold-search nil))
1291 (if (looking-at Man-heading-regexp)
1292 (forward-line -1))
1293 (if (re-search-backward Man-heading-regexp (point-min) t n)
1294 (beginning-of-line)
1295 (goto-char (point-min)))))
1297 (defun Man-find-section (section)
1298 "Move point to SECTION if it exists, otherwise don't move point.
1299 Returns t if section is found, nil otherwise."
1300 (let ((curpos (point))
1301 (case-fold-search nil))
1302 (goto-char (point-min))
1303 (if (re-search-forward (concat "^" section) (point-max) t)
1304 (progn (beginning-of-line) t)
1305 (goto-char curpos)
1306 nil)
1309 (defun Man-goto-section ()
1310 "Query for section to move point to."
1311 (interactive)
1312 (aput 'Man-sections-alist
1313 (let* ((default (aheadsym Man-sections-alist))
1314 (completion-ignore-case t)
1315 chosen
1316 (prompt (concat "Go to section (default " default "): ")))
1317 (setq chosen (completing-read prompt Man-sections-alist))
1318 (if (or (not chosen)
1319 (string= chosen ""))
1320 default
1321 chosen)))
1322 (Man-find-section (aheadsym Man-sections-alist)))
1324 (defun Man-goto-see-also-section ()
1325 "Move point to the \"SEE ALSO\" section.
1326 Actually the section moved to is described by `Man-see-also-regexp'."
1327 (interactive)
1328 (if (not (Man-find-section Man-see-also-regexp))
1329 (error (concat "No " Man-see-also-regexp
1330 " section found in the current manpage"))))
1332 (defun Man-possibly-hyphenated-word ()
1333 "Return a possibly hyphenated word at point.
1334 If the word starts at the first non-whitespace column, and the
1335 previous line ends with a hyphen, return the last word on the previous
1336 line instead. Thus, if a reference to \"tcgetpgrp(3V)\" is hyphenated
1337 as \"tcgetp-grp(3V)\", and point is at \"grp(3V)\", we return
1338 \"tcgetp-\" instead of \"grp\"."
1339 (save-excursion
1340 (skip-syntax-backward "w()")
1341 (skip-chars-forward " \t")
1342 (let ((beg (point))
1343 (word (current-word)))
1344 (when (eq beg (save-excursion
1345 (back-to-indentation)
1346 (point)))
1347 (end-of-line 0)
1348 (if (eq (char-before) ?-)
1349 (setq word (current-word))))
1350 word)))
1352 (defun Man-follow-manual-reference (reference)
1353 "Get one of the manpages referred to in the \"SEE ALSO\" section.
1354 Specify which REFERENCE to use; default is based on word at point."
1355 (interactive
1356 (if (not Man-refpages-alist)
1357 (error "There are no references in the current man page")
1358 (list (let* ((default (or
1359 (car (all-completions
1360 (let ((word
1361 (or (Man-possibly-hyphenated-word)
1362 "")))
1363 ;; strip a trailing '-':
1364 (if (string-match "-$" word)
1365 (substring word 0
1366 (match-beginning 0))
1367 word))
1368 Man-refpages-alist))
1369 (aheadsym Man-refpages-alist)))
1370 chosen
1371 (prompt (concat "Refer to (default " default "): ")))
1372 (setq chosen (completing-read prompt Man-refpages-alist))
1373 (if (or (not chosen)
1374 (string= chosen ""))
1375 default
1376 chosen)))))
1377 (if (not Man-refpages-alist)
1378 (error "Can't find any references in the current manpage")
1379 (aput 'Man-refpages-alist reference)
1380 (Man-getpage-in-background
1381 (Man-translate-references (aheadsym Man-refpages-alist)))))
1383 (defun Man-kill ()
1384 "Kill the buffer containing the manpage."
1385 (interactive)
1386 (quit-window t))
1388 (defun Man-quit ()
1389 "Bury the buffer containing the manpage."
1390 (interactive)
1391 (quit-window))
1393 (defun Man-goto-page (page &optional noerror)
1394 "Go to the manual page on page PAGE."
1395 (interactive
1396 (if (not Man-page-list)
1397 (error "Not a man page buffer")
1398 (if (= (length Man-page-list) 1)
1399 (error "You're looking at the only manpage in the buffer")
1400 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1401 (length Man-page-list)))))))
1402 (if (and (not Man-page-list) (not noerror))
1403 (error "Not a man page buffer"))
1404 (when Man-page-list
1405 (if (or (< page 1)
1406 (> page (length Man-page-list)))
1407 (error "No manpage %d found" page))
1408 (let* ((page-range (nth (1- page) Man-page-list))
1409 (page-start (car page-range))
1410 (page-end (car (cdr page-range))))
1411 (setq Man-current-page page
1412 Man-page-mode-string (Man-make-page-mode-string))
1413 (widen)
1414 (goto-char page-start)
1415 (narrow-to-region page-start page-end)
1416 (Man-build-section-alist)
1417 (Man-build-references-alist)
1418 (goto-char (point-min)))))
1421 (defun Man-next-manpage ()
1422 "Find the next manpage entry in the buffer."
1423 (interactive)
1424 (if (= (length Man-page-list) 1)
1425 (error "This is the only manpage in the buffer"))
1426 (if (< Man-current-page (length Man-page-list))
1427 (Man-goto-page (1+ Man-current-page))
1428 (if Man-circular-pages-flag
1429 (Man-goto-page 1)
1430 (error "You're looking at the last manpage in the buffer"))))
1432 (defun Man-previous-manpage ()
1433 "Find the previous manpage entry in the buffer."
1434 (interactive)
1435 (if (= (length Man-page-list) 1)
1436 (error "This is the only manpage in the buffer"))
1437 (if (> Man-current-page 1)
1438 (Man-goto-page (1- Man-current-page))
1439 (if Man-circular-pages-flag
1440 (Man-goto-page (length Man-page-list))
1441 (error "You're looking at the first manpage in the buffer"))))
1443 ;; Header file support
1444 (defun Man-view-header-file (file)
1445 "View a header file specified by FILE from `Man-header-file-path'."
1446 (let ((path Man-header-file-path)
1447 complete-path)
1448 (while path
1449 (setq complete-path (concat (car path) "/" file)
1450 path (cdr path))
1451 (if (file-readable-p complete-path)
1452 (progn (view-file complete-path)
1453 (setq path nil))
1454 (setq complete-path nil)))
1455 complete-path))
1457 ;; Init the man package variables, if not already done.
1458 (Man-init-defvars)
1460 (add-to-list 'debug-ignored-errors "^No manpage [0-9]* found$")
1461 (add-to-list 'debug-ignored-errors "^Can't find the .* manpage$")
1463 (provide 'man)
1465 ;; arch-tag: 587cda76-8e23-4594-b1f3-89b6b09a0d47
1466 ;;; man.el ends here