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