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