(tramp-handle-verify-visited-file-modtime): `visited-file-modtime' now
[emacs.git] / lisp / man.el
blobcbfae21e44b117bbba5e00206d1b376603b20a53
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
736 (if (memq system-type '(cygwin windows-nt))
737 shell-file-name
738 "sh")
739 shell-command-switch
740 (format (Man-build-man-command) man-args))
741 'Man-bgproc-sentinel)
742 (let ((exit-status
743 (call-process shell-file-name nil (list buffer nil) nil
744 shell-command-switch
745 (format (Man-build-man-command) man-args)))
746 (msg ""))
747 (or (and (numberp exit-status)
748 (= exit-status 0))
749 (and (numberp exit-status)
750 (setq msg
751 (format "exited abnormally with code %d"
752 exit-status)))
753 (setq msg exit-status))
754 (Man-bgproc-sentinel bufname msg)))))))
756 (defun Man-notify-when-ready (man-buffer)
757 "Notify the user when MAN-BUFFER is ready.
758 See the variable `Man-notify-method' for the different notification behaviors."
759 (let ((saved-frame (save-excursion
760 (set-buffer man-buffer)
761 Man-original-frame)))
762 (cond
763 ((eq Man-notify-method 'newframe)
764 ;; Since we run asynchronously, perhaps while Emacs is waiting
765 ;; for input, we must not leave a different buffer current. We
766 ;; can't rely on the editor command loop to reselect the
767 ;; selected window's buffer.
768 (save-excursion
769 (let ((frame (make-frame Man-frame-parameters)))
770 (set-window-buffer (frame-selected-window frame) man-buffer)
771 (set-window-dedicated-p (frame-selected-window frame) t)
772 (or (display-multi-frame-p frame)
773 (select-frame frame)))))
774 ((eq Man-notify-method 'pushy)
775 (switch-to-buffer man-buffer))
776 ((eq Man-notify-method 'bully)
777 (and (frame-live-p saved-frame)
778 (select-frame saved-frame))
779 (pop-to-buffer man-buffer)
780 (delete-other-windows))
781 ((eq Man-notify-method 'aggressive)
782 (and (frame-live-p saved-frame)
783 (select-frame saved-frame))
784 (pop-to-buffer man-buffer))
785 ((eq Man-notify-method 'friendly)
786 (and (frame-live-p saved-frame)
787 (select-frame saved-frame))
788 (display-buffer man-buffer 'not-this-window))
789 ((eq Man-notify-method 'polite)
790 (beep)
791 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
792 ((eq Man-notify-method 'quiet)
793 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
794 ((or (eq Man-notify-method 'meek)
796 (message ""))
799 (defun Man-softhyphen-to-minus ()
800 ;; \255 is SOFT HYPHEN in Latin-N. Versions of Debian man, at
801 ;; least, emit it even when not in a Latin-N locale.
802 (unless (eq t (compare-strings "latin-" 0 nil
803 current-language-environment 0 6 t))
804 (goto-char (point-min))
805 (let ((str "\255"))
806 (if enable-multibyte-characters
807 (setq str (string-as-multibyte str)))
808 (while (search-forward str nil t) (replace-match "-")))))
810 (defun Man-fontify-manpage ()
811 "Convert overstriking and underlining to the correct fonts.
812 Same for the ANSI bold and normal escape sequences."
813 (interactive)
814 (message "Please wait: formatting the %s man page..." Man-arguments)
815 (goto-char (point-min))
816 (while (search-forward "\e[1m" nil t)
817 (delete-backward-char 4)
818 (put-text-property (point)
819 (progn (if (search-forward "\e[0m" nil 'move)
820 (delete-backward-char 4))
821 (point))
822 'face Man-overstrike-face))
823 (if (< (buffer-size) (position-bytes (point-max)))
824 ;; Multibyte characters exist.
825 (progn
826 (goto-char (point-min))
827 (while (search-forward "__\b\b" nil t)
828 (backward-delete-char 4)
829 (put-text-property (point) (1+ (point)) 'face Man-underline-face))
830 (goto-char (point-min))
831 (while (search-forward "\b\b__" nil t)
832 (backward-delete-char 4)
833 (put-text-property (1- (point)) (point) 'face Man-underline-face))))
834 (goto-char (point-min))
835 (while (search-forward "_\b" nil t)
836 (backward-delete-char 2)
837 (put-text-property (point) (1+ (point)) 'face Man-underline-face))
838 (goto-char (point-min))
839 (while (search-forward "\b_" nil t)
840 (backward-delete-char 2)
841 (put-text-property (1- (point)) (point) 'face Man-underline-face))
842 (goto-char (point-min))
843 (while (re-search-forward "\\(.\\)\\(\b+\\1\\)+" nil t)
844 (replace-match "\\1")
845 (put-text-property (1- (point)) (point) 'face Man-overstrike-face))
846 (goto-char (point-min))
847 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t)
848 (replace-match "o")
849 (put-text-property (1- (point)) (point) 'face 'bold))
850 (goto-char (point-min))
851 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t)
852 (replace-match "+")
853 (put-text-property (1- (point)) (point) 'face 'bold))
854 (goto-char (point-min))
855 ;; Try to recognize common forms of cross references.
856 (Man-highlight-references)
857 (Man-softhyphen-to-minus)
858 (goto-char (point-min))
859 (while (re-search-forward Man-heading-regexp nil t)
860 (put-text-property (match-beginning 0)
861 (match-end 0)
862 'face Man-overstrike-face))
863 (message "%s man page formatted" Man-arguments))
865 (defun Man-highlight-references ()
866 "Highlight the references on mouse-over.
867 references include items in the SEE ALSO section,
868 header file(#include <foo.h>) and files in FILES"
869 (let ((dummy 0))
870 (Man-highlight-references0
871 Man-see-also-regexp Man-reference-regexp 1 dummy
872 'Man-xref-man-page)
873 (Man-highlight-references0
874 Man-synopsis-regexp Man-header-regexp 0 2
875 'Man-xref-header-file)
876 (Man-highlight-references0
877 Man-files-regexp Man-normal-file-regexp 0 0
878 'Man-xref-normal-file)))
880 (defun Man-highlight-references0 (start-section regexp button-pos target-pos type)
881 ;; Based on `Man-build-references-alist'
882 (when (Man-find-section start-section)
883 (forward-line 1)
884 (let ((end (save-excursion
885 (Man-next-section 1)
886 (point))))
887 (back-to-indentation)
888 (while (re-search-forward regexp end t)
889 (make-text-button
890 (match-beginning button-pos)
891 (match-end button-pos)
892 'type type
893 'Man-target-string (match-string target-pos)
894 )))))
896 (defun Man-cleanup-manpage ()
897 "Remove overstriking and underlining from the current buffer."
898 (interactive)
899 (message "Please wait: cleaning up the %s man page..."
900 Man-arguments)
901 (if (or (interactive-p) (not Man-sed-script))
902 (progn
903 (goto-char (point-min))
904 (while (search-forward "_\b" nil t) (backward-delete-char 2))
905 (goto-char (point-min))
906 (while (search-forward "\b_" nil t) (backward-delete-char 2))
907 (goto-char (point-min))
908 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
909 (replace-match "\\1"))
910 (goto-char (point-min))
911 (while (re-search-forward "\e\\[[0-9]+m" nil t) (replace-match ""))
912 (goto-char (point-min))
913 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t) (replace-match "o"))
915 (goto-char (point-min))
916 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t) (replace-match "+"))
917 (Man-softhyphen-to-minus)
918 (message "%s man page cleaned up" Man-arguments))
920 (defun Man-bgproc-sentinel (process msg)
921 "Manpage background process sentinel.
922 When manpage command is run asynchronously, PROCESS is the process
923 object for the manpage command; when manpage command is run
924 synchronously, PROCESS is the name of the buffer where the manpage
925 command is run. Second argument MSG is the exit message of the
926 manpage command."
927 (let ((Man-buffer (if (stringp process) (get-buffer process)
928 (process-buffer process)))
929 (delete-buff nil)
930 (err-mess nil))
932 (if (null (buffer-name Man-buffer)) ;; deleted buffer
933 (or (stringp process)
934 (set-process-buffer process nil))
936 (save-excursion
937 (set-buffer Man-buffer)
938 (let ((case-fold-search nil))
939 (goto-char (point-min))
940 (cond ((or (looking-at "No \\(manual \\)*entry for")
941 (looking-at "[^\n]*: nothing appropriate$"))
942 (setq err-mess (buffer-substring (point)
943 (progn
944 (end-of-line) (point)))
945 delete-buff t))
946 ((or (stringp process)
947 (not (and (eq (process-status process) 'exit)
948 (= (process-exit-status process) 0))))
949 (or (zerop (length msg))
950 (progn
951 (setq err-mess
952 (concat (buffer-name Man-buffer)
953 ": process "
954 (let ((eos (1- (length msg))))
955 (if (= (aref msg eos) ?\n)
956 (substring msg 0 eos) msg))))
957 (goto-char (point-max))
958 (insert (format "\nprocess %s" msg))))
960 (if delete-buff
961 (kill-buffer Man-buffer)
962 (if Man-fontify-manpage-flag
963 (Man-fontify-manpage)
964 (Man-cleanup-manpage))
965 (run-hooks 'Man-cooked-hook)
966 (Man-mode)
967 (set-buffer-modified-p nil)
969 ;; Restore case-fold-search before calling
970 ;; Man-notify-when-ready because it may switch buffers.
972 (if (not delete-buff)
973 (Man-notify-when-ready Man-buffer))
975 (if err-mess
976 (error err-mess))
977 ))))
980 ;; ======================================================================
981 ;; set up manual mode in buffer and build alists
983 (defun Man-mode ()
984 "A mode for browsing Un*x manual pages.
986 The following man commands are available in the buffer. Try
987 \"\\[describe-key] <key> RET\" for more information:
989 \\[man] Prompt to retrieve a new manpage.
990 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
991 \\[Man-next-manpage] Jump to next manpage in circular list.
992 \\[Man-previous-manpage] Jump to previous manpage in circular list.
993 \\[Man-next-section] Jump to next manpage section.
994 \\[Man-previous-section] Jump to previous manpage section.
995 \\[Man-goto-section] Go to a manpage section.
996 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
997 \\[Man-quit] Deletes the manpage window, bury its buffer.
998 \\[Man-kill] Deletes the manpage window, kill its buffer.
999 \\[describe-mode] Prints this help text.
1001 The following variables may be of some use. Try
1002 \"\\[describe-variable] <variable-name> RET\" for more information:
1004 `Man-notify-method' What happens when manpage formatting is done.
1005 `Man-downcase-section-letters-flag' Force section letters to lower case.
1006 `Man-circular-pages-flag' Treat multiple manpage list as circular.
1007 `Man-section-translations-alist' List of section numbers and their Un*x equiv.
1008 `Man-filter-list' Background manpage filter command.
1009 `Man-mode-map' Keymap bindings for Man mode buffers.
1010 `Man-mode-hook' Normal hook run on entry to Man mode.
1011 `Man-section-regexp' Regexp describing manpage section letters.
1012 `Man-heading-regexp' Regexp describing section headers.
1013 `Man-see-also-regexp' Regexp for SEE ALSO section (or your equiv).
1014 `Man-first-heading-regexp' Regexp for first heading on a manpage.
1015 `Man-reference-regexp' Regexp matching a references in SEE ALSO.
1016 `Man-switches' Background `man' command switches.
1018 The following key bindings are currently in effect in the buffer:
1019 \\{Man-mode-map}"
1020 (interactive)
1021 (setq major-mode 'Man-mode
1022 mode-name "Man"
1023 buffer-auto-save-file-name nil
1024 mode-line-buffer-identification
1025 (list (default-value 'mode-line-buffer-identification)
1026 " {" 'Man-page-mode-string "}")
1027 truncate-lines t
1028 buffer-read-only t)
1029 (buffer-disable-undo (current-buffer))
1030 (auto-fill-mode -1)
1031 (use-local-map Man-mode-map)
1032 (set-syntax-table man-mode-syntax-table)
1033 (setq imenu-generic-expression (list (list nil Man-heading-regexp 0)))
1034 (set (make-local-variable 'outline-regexp) Man-heading-regexp)
1035 (set (make-local-variable 'outline-level) (lambda () 1))
1036 (Man-build-page-list)
1037 (Man-strip-page-headers)
1038 (Man-unindent)
1039 (Man-goto-page 1)
1040 (run-hooks 'Man-mode-hook))
1042 (defsubst Man-build-section-alist ()
1043 "Build the association list of manpage sections."
1044 (setq Man-sections-alist nil)
1045 (goto-char (point-min))
1046 (let ((case-fold-search nil))
1047 (while (re-search-forward Man-heading-regexp (point-max) t)
1048 (aput 'Man-sections-alist (match-string 1))
1049 (forward-line 1))))
1051 (defsubst Man-build-references-alist ()
1052 "Build the association list of references (in the SEE ALSO section)."
1053 (setq Man-refpages-alist nil)
1054 (save-excursion
1055 (if (Man-find-section Man-see-also-regexp)
1056 (let ((start (progn (forward-line 1) (point)))
1057 (end (progn
1058 (Man-next-section 1)
1059 (point)))
1060 hyphenated
1061 (runningpoint -1))
1062 (save-restriction
1063 (narrow-to-region start end)
1064 (goto-char (point-min))
1065 (back-to-indentation)
1066 (while (and (not (eobp)) (/= (point) runningpoint))
1067 (setq runningpoint (point))
1068 (if (re-search-forward Man-hyphenated-reference-regexp end t)
1069 (let* ((word (match-string 0))
1070 (len (1- (length word))))
1071 (if hyphenated
1072 (setq word (concat hyphenated word)
1073 hyphenated nil
1074 ;; Update len, in case a reference spans
1075 ;; more than two lines (paranoia).
1076 len (1- (length word))))
1077 (if (memq (aref word len) '(?- ?­))
1078 (setq hyphenated (substring word 0 len)))
1079 (if (string-match Man-reference-regexp word)
1080 (aput 'Man-refpages-alist word))))
1081 (skip-chars-forward " \t\n,"))))))
1082 (setq Man-refpages-alist (nreverse Man-refpages-alist)))
1084 (defun Man-build-page-list ()
1085 "Build the list of separate manpages in the buffer."
1086 (setq Man-page-list nil)
1087 (let ((page-start (point-min))
1088 (page-end (point-max))
1089 (header ""))
1090 (goto-char page-start)
1091 ;; (switch-to-buffer (current-buffer))(debug)
1092 (while (not (eobp))
1093 (setq header
1094 (if (looking-at Man-page-header-regexp)
1095 (match-string 1)
1096 nil))
1097 ;; Go past both the current and the next Man-first-heading-regexp
1098 (if (re-search-forward Man-first-heading-regexp nil 'move 2)
1099 (let ((p (progn (beginning-of-line) (point))))
1100 ;; We assume that the page header is delimited by blank
1101 ;; lines and that it contains at most one blank line. So
1102 ;; if we back by three blank lines we will be sure to be
1103 ;; before the page header but not before the possible
1104 ;; previous page header.
1105 (search-backward "\n\n" nil t 3)
1106 (if (re-search-forward Man-page-header-regexp p 'move)
1107 (beginning-of-line))))
1108 (setq page-end (point))
1109 (setq Man-page-list (append Man-page-list
1110 (list (list (copy-marker page-start)
1111 (copy-marker page-end)
1112 header))))
1113 (setq page-start page-end)
1116 (defun Man-strip-page-headers ()
1117 "Strip all the page headers but the first from the manpage."
1118 (let ((buffer-read-only nil)
1119 (case-fold-search nil)
1120 (page-list Man-page-list)
1121 (page ())
1122 (header ""))
1123 (while page-list
1124 (setq page (car page-list))
1125 (and (nth 2 page)
1126 (goto-char (car page))
1127 (re-search-forward Man-first-heading-regexp nil t)
1128 (setq header (buffer-substring (car page) (match-beginning 0)))
1129 ;; Since the awk script collapses all successive blank
1130 ;; lines into one, and since we don't want to get rid of
1131 ;; the fast awk script, one must choose between adding
1132 ;; spare blank lines between pages when there were none and
1133 ;; deleting blank lines at page boundaries when there were
1134 ;; some. We choose the first, so we comment the following
1135 ;; line.
1136 ;; (setq header (concat "\n" header)))
1137 (while (search-forward header (nth 1 page) t)
1138 (replace-match "")))
1139 (setq page-list (cdr page-list)))))
1141 (defun Man-unindent ()
1142 "Delete the leading spaces that indent the manpage."
1143 (let ((buffer-read-only nil)
1144 (case-fold-search nil)
1145 (page-list Man-page-list))
1146 (while page-list
1147 (let ((page (car page-list))
1148 (indent "")
1149 (nindent 0))
1150 (narrow-to-region (car page) (car (cdr page)))
1151 (if Man-uses-untabify-flag
1152 (untabify (point-min) (point-max)))
1153 (if (catch 'unindent
1154 (goto-char (point-min))
1155 (if (not (re-search-forward Man-first-heading-regexp nil t))
1156 (throw 'unindent nil))
1157 (beginning-of-line)
1158 (setq indent (buffer-substring (point)
1159 (progn
1160 (skip-chars-forward " ")
1161 (point))))
1162 (setq nindent (length indent))
1163 (if (zerop nindent)
1164 (throw 'unindent nil))
1165 (setq indent (concat indent "\\|$"))
1166 (goto-char (point-min))
1167 (while (not (eobp))
1168 (if (looking-at indent)
1169 (forward-line 1)
1170 (throw 'unindent nil)))
1171 (goto-char (point-min)))
1172 (while (not (eobp))
1173 (or (eolp)
1174 (delete-char nindent))
1175 (forward-line 1)))
1176 (setq page-list (cdr page-list))
1177 ))))
1180 ;; ======================================================================
1181 ;; Man mode commands
1183 (defun Man-next-section (n)
1184 "Move point to Nth next section (default 1)."
1185 (interactive "p")
1186 (let ((case-fold-search nil))
1187 (if (looking-at Man-heading-regexp)
1188 (forward-line 1))
1189 (if (re-search-forward Man-heading-regexp (point-max) t n)
1190 (beginning-of-line)
1191 (goto-char (point-max)))))
1193 (defun Man-previous-section (n)
1194 "Move point to Nth previous section (default 1)."
1195 (interactive "p")
1196 (let ((case-fold-search nil))
1197 (if (looking-at Man-heading-regexp)
1198 (forward-line -1))
1199 (if (re-search-backward Man-heading-regexp (point-min) t n)
1200 (beginning-of-line)
1201 (goto-char (point-min)))))
1203 (defun Man-find-section (section)
1204 "Move point to SECTION if it exists, otherwise don't move point.
1205 Returns t if section is found, nil otherwise."
1206 (let ((curpos (point))
1207 (case-fold-search nil))
1208 (goto-char (point-min))
1209 (if (re-search-forward (concat "^" section) (point-max) t)
1210 (progn (beginning-of-line) t)
1211 (goto-char curpos)
1212 nil)
1215 (defun Man-goto-section ()
1216 "Query for section to move point to."
1217 (interactive)
1218 (aput 'Man-sections-alist
1219 (let* ((default (aheadsym Man-sections-alist))
1220 (completion-ignore-case t)
1221 chosen
1222 (prompt (concat "Go to section: (default " default ") ")))
1223 (setq chosen (completing-read prompt Man-sections-alist))
1224 (if (or (not chosen)
1225 (string= chosen ""))
1226 default
1227 chosen)))
1228 (Man-find-section (aheadsym Man-sections-alist)))
1230 (defun Man-goto-see-also-section ()
1231 "Move point to the \"SEE ALSO\" section.
1232 Actually the section moved to is described by `Man-see-also-regexp'."
1233 (interactive)
1234 (if (not (Man-find-section Man-see-also-regexp))
1235 (error (concat "No " Man-see-also-regexp
1236 " section found in the current manpage"))))
1238 (defun Man-possibly-hyphenated-word ()
1239 "Return a possibly hyphenated word at point.
1240 If the word starts at the first non-whitespace column, and the
1241 previous line ends with a hyphen, return the last word on the previous
1242 line instead. Thus, if a reference to \"tcgetpgrp(3V)\" is hyphenated
1243 as \"tcgetp-grp(3V)\", and point is at \"grp(3V)\", we return
1244 \"tcgetp-\" instead of \"grp\"."
1245 (save-excursion
1246 (skip-syntax-backward "w()")
1247 (skip-chars-forward " \t")
1248 (let ((beg (point))
1249 (word (current-word)))
1250 (when (eq beg (save-excursion
1251 (back-to-indentation)
1252 (point)))
1253 (end-of-line 0)
1254 (if (eq (char-before) ?-)
1255 (setq word (current-word))))
1256 word)))
1258 (defun Man-follow-manual-reference (reference)
1259 "Get one of the manpages referred to in the \"SEE ALSO\" section.
1260 Specify which REFERENCE to use; default is based on word at point."
1261 (interactive
1262 (if (not Man-refpages-alist)
1263 (error "There are no references in the current man page")
1264 (list (let* ((default (or
1265 (car (all-completions
1266 (let ((word (Man-possibly-hyphenated-word)))
1267 ;; strip a trailing '-':
1268 (if (string-match "-$" word)
1269 (substring word 0
1270 (match-beginning 0))
1271 word))
1272 Man-refpages-alist))
1273 (aheadsym Man-refpages-alist)))
1274 chosen
1275 (prompt (concat "Refer to: (default " default ") ")))
1276 (setq chosen (completing-read prompt Man-refpages-alist))
1277 (if (or (not chosen)
1278 (string= chosen ""))
1279 default
1280 chosen)))))
1281 (if (not Man-refpages-alist)
1282 (error "Can't find any references in the current manpage")
1283 (aput 'Man-refpages-alist reference)
1284 (Man-getpage-in-background
1285 (Man-translate-references (aheadsym Man-refpages-alist)))))
1287 (defun Man-kill ()
1288 "Kill the buffer containing the manpage."
1289 (interactive)
1290 (quit-window t))
1292 (defun Man-quit ()
1293 "Bury the buffer containing the manpage."
1294 (interactive)
1295 (quit-window))
1297 (defun Man-goto-page (page)
1298 "Go to the manual page on page PAGE."
1299 (interactive
1300 (if (not Man-page-list)
1301 (let ((args Man-arguments))
1302 (kill-buffer (current-buffer))
1303 (error "Can't find the %s manpage" args))
1304 (if (= (length Man-page-list) 1)
1305 (error "You're looking at the only manpage in the buffer")
1306 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1307 (length Man-page-list)))))))
1308 (if (not Man-page-list)
1309 (let ((args Man-arguments))
1310 (kill-buffer (current-buffer))
1311 (error "Can't find the %s manpage" args)))
1312 (if (or (< page 1)
1313 (> page (length Man-page-list)))
1314 (error "No manpage %d found" page))
1315 (let* ((page-range (nth (1- page) Man-page-list))
1316 (page-start (car page-range))
1317 (page-end (car (cdr page-range))))
1318 (setq Man-current-page page
1319 Man-page-mode-string (Man-make-page-mode-string))
1320 (widen)
1321 (goto-char page-start)
1322 (narrow-to-region page-start page-end)
1323 (Man-build-section-alist)
1324 (Man-build-references-alist)
1325 (goto-char (point-min))))
1328 (defun Man-next-manpage ()
1329 "Find the next manpage entry in the buffer."
1330 (interactive)
1331 (if (= (length Man-page-list) 1)
1332 (error "This is the only manpage in the buffer"))
1333 (if (< Man-current-page (length Man-page-list))
1334 (Man-goto-page (1+ Man-current-page))
1335 (if Man-circular-pages-flag
1336 (Man-goto-page 1)
1337 (error "You're looking at the last manpage in the buffer"))))
1339 (defun Man-previous-manpage ()
1340 "Find the previous manpage entry in the buffer."
1341 (interactive)
1342 (if (= (length Man-page-list) 1)
1343 (error "This is the only manpage in the buffer"))
1344 (if (> Man-current-page 1)
1345 (Man-goto-page (1- Man-current-page))
1346 (if Man-circular-pages-flag
1347 (Man-goto-page (length Man-page-list))
1348 (error "You're looking at the first manpage in the buffer"))))
1350 ;; Header file support
1351 (defun Man-view-header-file (file)
1352 "View a header file specified by FILE from `Man-header-file-path'."
1353 (let ((path Man-header-file-path)
1354 complete-path)
1355 (while path
1356 (setq complete-path (concat (car path) "/" file)
1357 path (cdr path))
1358 (if (file-readable-p complete-path)
1359 (progn (view-file complete-path)
1360 (setq path nil))
1361 (setq complete-path nil)))
1362 complete-path))
1364 ;; Init the man package variables, if not already done.
1365 (Man-init-defvars)
1367 (add-to-list 'debug-ignored-errors "^No manpage [0-9]* found$")
1368 (add-to-list 'debug-ignored-errors "^Can't find the .* manpage$")
1370 (provide 'man)
1372 ;;; arch-tag: 587cda76-8e23-4594-b1f3-89b6b09a0d47
1373 ;;; man.el ends here