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