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