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