* man.el (Man-fontify-manpage-flag): defvar put at outer level.
[emacs.git] / lisp / man.el
blob52645b61515bf4fac689b2488770b73f9d00f41b
1 ;;; man.el --- browse UNIX manual pages
3 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
5 ;; Author: Barry A. Warsaw <bwarsaw@cen.com>
6 ;; Last-Modified: $Date: 1994/10/24 15:34:50 $
7 ;; Version: $Revision: 1.55 $
8 ;; Keywords: help
9 ;; Adapted-By: ESR, pot
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, 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 everthing 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 idiosynchrasies.
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 euristhics
85 ;; only. Is it worth doing?
86 ;; - Allow the Man-reuse-okay-flag to be set to 'always, meaning that all
87 ;; the manpages should go in the same buffer, where they can be browsed
88 ;; with M-n and M-p.
89 ;; - Allow completion on the manpage name when calling man. This
90 ;; requires a reliable list of places where manpages can be found. The
91 ;; drawback would be that if the list is not complete, the user might
92 ;; be led to believe that the manpages in the missing directories do
93 ;; not exist.
96 ;;; Code:
98 (require 'assoc)
100 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
101 ;; empty defvars (keep the compiler quiet)
103 (defvar Man-notify)
104 (defvar Man-current-page)
105 (defvar Man-page-list)
106 (defvar Man-filter-list)
107 (defvar Man-original-frame)
108 (defvar Man-arguments)
109 (defvar Man-sections-alist)
110 (defvar Man-refpages-alist)
111 (defvar Man-uses-untabify-flag)
112 (defvar Man-page-mode-string)
113 (defvar Man-sed-script)
115 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
116 ;; user variables
118 (defvar Man-fontify-manpage-flag t
119 "*Make up the manpage with fonts.")
121 (defvar Man-overstrike-face 'bold
122 "*Face to use when fontifying overstrike.")
124 (defvar Man-underline-face 'underline
125 "*Face to use when fontifying underlinining.")
127 ;; Use the value of the obsolete user option Man-notify, if set.
128 (defvar Man-notify-method (if (boundp 'Man-notify) Man-notify 'friendly)
129 "*Selects the behavior when manpage is ready.
130 This variable may have one of the following values, where (sf) means
131 that the frames are switched, so the manpage is displayed in the frame
132 where the man command was called from:
134 newframe -- put the manpage in its own frame (see `Man-frame-parameters')
135 pushy -- make the manpage the current buffer in the current window
136 bully -- make the manpage the current buffer and only window (sf)
137 aggressive -- make the manpage the current buffer in the other window (sf)
138 friendly -- display manpage in the other window but don't make current (sf)
139 polite -- don't display manpage, but prints message and beep when ready
140 quiet -- like `polite', but don't beep
141 meek -- make no indication that the manpage is ready
143 Any other value of `Man-notify-method' is equivalent to `meek'.")
145 (defvar Man-frame-parameters nil
146 "*Frame parameter list for creating a new frame for a manual page.")
148 (defvar Man-reuse-okay-flag t
149 "*Reuse a manpage buffer if possible.
150 If non-nil, and a manpage buffer already exists with the same
151 invocation, man just indicates the manpage is ready according to the
152 value of `Man-notify-method'. When nil, it always fires off a
153 background process,putting the results in a uniquely named buffer.")
155 (defvar Man-downcase-section-letters-flag t
156 "*Letters in sections are converted to lower case.
157 Some Un*x man commands can't handle uppercase letters in sections, for
158 example \"man 2V chmod\", but they are often displayed in the manpage
159 with the upper case letter. When this variable is t, the section
160 letter (e.g., \"2V\") is converted to lowercase (e.g., \"2v\") before
161 being sent to the man background process.")
163 (defvar Man-circular-pages-flag t
164 "*If t, the manpage list is treated as circular for traversal.")
166 (defvar Man-section-translations-alist
167 (list
168 '("3C++" . "3")
169 ;; Some systems have a real 3x man section, so let's comment this.
170 ;; '("3X" . "3") ; Xlib man pages
171 '("3X11" . "3")
172 '("1-UCB" . ""))
173 "*Association list of bogus sections to real section numbers.
174 Some manpages (e.g. the Sun C++ 2.1 manpages) have section numbers in
175 their references which Un*x `man' does not recognize. This
176 association list is used to translate those sections, when found, to
177 the associated section number.")
179 (defvar manual-program "man"
180 "The name of the program that produces man pages.")
182 (defvar Man-untabify-command "pr"
183 "Command used for untabifying.")
185 (defvar Man-untabify-command-args (list "-t" "-e")
186 "List of arguments to be passed to Man-untabify-command (which see).")
188 (defvar Man-sed-command "sed"
189 "Command used for processing sed scripts.")
191 (defvar Man-awk-command "awk"
192 "Command used for processing awk scripts.")
194 (defvar Man-mode-line-format
195 '("" mode-line-modified
196 mode-line-buffer-identification " "
197 global-mode-string
198 " " Man-page-mode-string
199 " %[(" mode-name mode-line-process minor-mode-alist ")%]----"
200 (-3 . "%p") "-%-")
201 "Mode line format for manual mode buffer.")
203 (defvar Man-mode-map nil
204 "Keymap for Man mode.")
206 (defvar Man-mode-hook nil
207 "Hook run when Man mode is enabled.")
209 (defvar Man-cooked-hook nil
210 "Hook run after removing backspaces but before Man-mode processing.")
212 (defvar Man-name-regexp "[-a-zA-Z0-9_][-a-zA-Z0-9_.]*"
213 "Regular expression describing the name of a manpage (without section).")
215 (defvar Man-section-regexp "[0-9][a-zA-Z+]*\\|[LNln]"
216 "Regular expression describing a manpage section within parentheses.")
218 (defvar Man-page-header-regexp
219 (concat "^[ \t]*\\(" Man-name-regexp
220 "(\\(" Man-section-regexp "\\))\\).*\\1")
221 "Regular expression describing the heading of a page.")
223 (defvar Man-heading-regexp "^\\([A-Z][A-Z ]+\\)$"
224 "Regular expression describing a manpage heading entry.")
226 (defvar Man-see-also-regexp "SEE ALSO"
227 "Regular expression for SEE ALSO heading (or your equivalent).
228 This regexp should not start with a `^' character.")
230 (defvar Man-first-heading-regexp "^[ \t]*NAME$\\|^[ \t]*No manual entry fo.*$"
231 "Regular expression describing first heading on a manpage.
232 This regular expression should start with a `^' character.")
234 (defvar Man-reference-regexp
235 (concat "\\(" Man-name-regexp "\\)(\\(" Man-section-regexp "\\))")
236 "Regular expression describing a reference in the SEE ALSO section.")
238 (defvar Man-switches ""
239 "Switches passed to the man command, as a single string.")
241 (defvar Man-specified-section-option
242 (if (string-match "-solaris[0-9.]*$" system-configuration)
243 "-s"
245 "Option that indicates a specified a manual section name.")
247 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
248 ;; end user variables
250 ;; other variables and keymap initializations
251 (make-variable-buffer-local 'Man-sections-alist)
252 (make-variable-buffer-local 'Man-refpages-alist)
253 (make-variable-buffer-local 'Man-page-list)
254 (make-variable-buffer-local 'Man-current-page)
255 (make-variable-buffer-local 'Man-page-mode-string)
256 (make-variable-buffer-local 'Man-original-frame)
257 (make-variable-buffer-local 'Man-arguments)
259 (setq-default Man-sections-alist nil)
260 (setq-default Man-refpages-alist nil)
261 (setq-default Man-page-list nil)
262 (setq-default Man-current-page 0)
263 (setq-default Man-page-mode-string "1 of 1")
265 (defconst Man-sysv-sed-script "\
266 /\b/ { s/_\b//g
267 s/\b_//g
268 s/o\b+/o/g
269 :ovstrk
270 s/\\(.\\)\b\\1/\\1/g
271 t ovstrk
273 /\e\\[[0-9][0-9]*m/ s///g"
274 "Script for sysV-like sed to nuke backspaces and ANSI codes from manpages.")
276 (defconst Man-berkeley-sed-script "\
277 /\b/ { s/_\b//g\\
278 s/\b_//g\\
279 s/o\b+/o/g\\
280 :ovstrk\\
281 s/\\(.\\)\b\\1/\\1/g\\
282 t ovstrk\\
284 /\e\\[[0-9][0-9]*m/ s///g"
285 "Script for berkeley-like sed to nuke backspaces and ANSI codes from manpages.")
287 (if Man-mode-map
289 (setq Man-mode-map (make-keymap))
290 (suppress-keymap Man-mode-map)
291 (define-key Man-mode-map " " 'scroll-up)
292 (define-key Man-mode-map "\177" 'scroll-down)
293 (define-key Man-mode-map "n" 'Man-next-section)
294 (define-key Man-mode-map "p" 'Man-previous-section)
295 (define-key Man-mode-map "\en" 'Man-next-manpage)
296 (define-key Man-mode-map "\ep" 'Man-previous-manpage)
297 (define-key Man-mode-map ">" 'end-of-buffer)
298 (define-key Man-mode-map "<" 'beginning-of-buffer)
299 (define-key Man-mode-map "." 'beginning-of-buffer)
300 (define-key Man-mode-map "r" 'Man-follow-manual-reference)
301 (define-key Man-mode-map "t" 'toggle-truncate-lines)
302 (define-key Man-mode-map "g" 'Man-goto-section)
303 (define-key Man-mode-map "s" 'Man-goto-see-also-section)
304 (define-key Man-mode-map "k" 'Man-kill)
305 (define-key Man-mode-map "q" 'Man-quit)
306 (define-key Man-mode-map "m" 'man)
307 (define-key Man-mode-map "?" 'describe-mode)
311 ;; ======================================================================
312 ;; utilities
314 (defsubst Man-init-defvars ()
315 "Used for initialising variables based on the value of window-system.
316 This is necessary if one wants to dump man.el with emacs."
318 ;; The following is necessary until fonts are implemented on
319 ;; terminals.
320 (setq Man-fontify-manpage-flag (and Man-fontify-manpage-flag
321 window-system))
323 (defconst Man-uses-untabify-flag t
324 ;; don't use pr: it is buggy
325 ;; (or (not (file-readable-p "/etc/passwd"))
326 ;; (/= 0 (apply 'call-process
327 ;; Man-untabify-command nil nil nil
328 ;; (append Man-untabify-command-args
329 ;; (list "/etc/passwd")))))
330 "When non-nil use `untabify' instead of Man-untabify-command.")
332 (defconst Man-sed-script
333 (cond
334 (Man-fontify-manpage-flag
335 nil)
336 ((= 0 (call-process Man-sed-command nil nil nil Man-sysv-sed-script))
337 Man-sysv-sed-script)
338 ((= 0 (call-process Man-sed-command nil nil nil Man-berkeley-sed-script))
339 Man-berkeley-sed-script)
341 nil))
342 "Script for sed to nuke backspaces and ANSI codes from manpages.")
344 (defvar Man-filter-list
345 (list
346 (cons
347 Man-sed-command
348 (list
349 (if Man-sed-script
350 (concat "-e '" Man-sed-script "'")
352 "-e '/^[\001-\032][\001-\032]*$/d'"
353 "-e '/\e[789]/s///g'"
354 "-e '/[Nn]o such file or directory/d'"
355 "-e '/Reformatting page. Wait/d'"
356 "-e '/Reformatting entry. Wait/d'"
357 "-e '/^[ \t]*Hewlett-Packard[ \t]Company[ \t]*-[ \t][0-9]*[ \t]-/d'"
358 "-e '/^[ \t]*Hewlett-Packard[ \t]*-[ \t][0-9]*[ \t]-.*$/d'"
359 "-e '/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d'"
360 "-e '/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d'"
361 "-e '/^Printed[ \t][0-9].*[0-9]$/d'"
362 "-e '/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d'"
363 "-e '/^[A-za-z].*Last[ \t]change:/d'"
364 "-e '/^Sun[ \t]Release[ \t][0-9].*[0-9]$/d'"
365 "-e '/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d'"
367 (cons
368 Man-awk-command
369 (list
370 "'\n"
371 "BEGIN { blankline=0; anonblank=0; }\n"
372 "/^$/ { if (anonblank==0) next; }\n"
373 "{ anonblank=1; }\n"
374 "/^$/ { blankline++; next; }\n"
375 "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }\n"
378 (if (not Man-uses-untabify-flag)
379 (cons
380 Man-untabify-command
381 Man-untabify-command-args)
383 "*Manpage cleaning filter command phrases.
384 This variable contains a list of the following form:
386 '((command-string phrase-string*)*)
388 Each phrase-string is concatenated onto the command-string to form a
389 command filter. The (standard) output (and standard error) of the Un*x
390 man command is piped through each command filter in the order the
391 commands appear in the association list. The final output is placed in
392 the manpage buffer.")
395 (defsubst Man-match-substring (&optional n string)
396 "Return the substring matched by the last search.
397 Optional arg N means return the substring matched by the Nth paren
398 grouping. Optional second arg STRING means return a substring from
399 that string instead of from the current buffer."
400 (if (null n) (setq n 0))
401 (if string
402 (substring string (match-beginning n) (match-end n))
403 (buffer-substring (match-beginning n) (match-end n))))
405 (defsubst Man-make-page-mode-string ()
406 "Formats part of the mode line for Man mode."
407 (format "%s page %d of %d"
408 (or (nth 2 (nth (1- Man-current-page) Man-page-list))
410 Man-current-page
411 (length Man-page-list)))
413 (defsubst Man-build-man-command ()
414 "Builds the entire background manpage and cleaning command."
415 (let ((command (concat manual-program " " Man-switches " %s 2>/dev/null"))
416 (flist Man-filter-list))
417 (while (and flist (car flist))
418 (let ((pcom (car (car flist)))
419 (pargs (cdr (car flist))))
420 (setq command
421 (concat command " | " pcom " "
422 (mapconcat '(lambda (phrase)
423 (if (not (stringp phrase))
424 (error "Malformed Man-filter-list"))
425 phrase)
426 pargs " ")))
427 (setq flist (cdr flist))))
428 command))
430 (defun Man-translate-references (ref)
431 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style.
432 Leave it as is if already in that style. Possibly downcase and
433 translate the section (see the Man-downcase-section-letters-flag
434 and the Man-section-translations-alist variables)."
435 (let ((name "")
436 (section "")
437 (slist Man-section-translations-alist))
438 (cond
439 ;; "chmod(2V)" case ?
440 ((string-match (concat Man-reference-regexp "$") ref)
441 (setq name (Man-match-substring 1 ref)
442 section (Man-match-substring 2 ref)))
443 ;; "2v chmod" case ?
444 ((string-match (concat "\\(" Man-section-regexp
445 "\\) +\\(" Man-name-regexp "\\)$") ref)
446 (setq name (Man-match-substring 2 ref)
447 section (Man-match-substring 1 ref))))
448 (if (string= name "")
449 ref ; Return the reference as is
450 (if Man-downcase-section-letters-flag
451 (setq section (downcase section)))
452 (while slist
453 (let ((s1 (car (car slist)))
454 (s2 (cdr (car slist))))
455 (setq slist (cdr slist))
456 (if Man-downcase-section-letters-flag
457 (setq s1 (downcase s1)))
458 (if (not (string= s1 section)) nil
459 (setq section (if Man-downcase-section-letters-flag
460 (downcase s2)
462 slist nil))))
463 (concat Man-specified-section-option section " " name))))
466 ;; ======================================================================
467 ;; default man entry: get word under point
469 (defsubst Man-default-man-entry ()
470 "Make a guess at a default manual entry.
471 This guess is based on the text surrounding the cursor, and the
472 default section number is selected from `Man-auto-section-alist'."
473 (let (default-title)
474 (save-excursion
476 ;; Default man entry title is any word the cursor is on, or if
477 ;; cursor not on a word, then nearest preceding word. Cannot
478 ;; use the current-word function because it skips the dots.
479 (if (not (looking-at "[-a-zA-Z_.]"))
480 (skip-chars-backward "^a-zA-Z"))
481 (skip-chars-backward "-(a-zA-Z_0-9_.")
482 (if (looking-at "(") (forward-char 1))
483 (setq default-title
484 (buffer-substring
485 (point)
486 (progn (skip-chars-forward "-a-zA-Z0-9_.") (point))))
488 ;; If looking at something like ioctl(2) or brc(1M), include the
489 ;; section number in the returned value.
490 (concat
491 default-title
492 (if (looking-at
493 (concat "[ \t]*([ \t]*\\(" Man-section-regexp "\\)[ \t]*)"))
494 (format "(%s)" (Man-match-substring 1)))
495 ))))
498 ;; ======================================================================
499 ;; Top level command and background process sentinel
501 ;; For compatibility with older versions.
502 ;;;###autoload
503 (defalias 'manual-entry 'man)
505 ;;;###autoload
506 (defun man (man-args prefix-arg)
507 "Get a Un*x manual page and put it in a buffer.
508 This command is the top-level command in the man package. It runs a Un*x
509 command to retrieve and clean a manpage in the background and places the
510 results in a Man mode (manpage browsing) buffer. See variable
511 `Man-notify-method' for what happens when the buffer is ready.
512 Normally, if a buffer already exists for this man page, it will display
513 immediately; either a prefix argument or a nil value to `Man-reuse-okay-flag'
514 overrides this and forces the man page to be regenerated."
515 (interactive
516 (list
517 ;; first argument
518 (let* ((default-entry (Man-default-man-entry))
519 (input (read-string
520 (format "Manual entry%s: "
521 (if (string= default-entry "")
523 (format " (default %s)" default-entry))))))
524 (if (string= input "")
525 (if (string= default-entry "")
526 (error "No man args given")
527 default-entry)
528 input))
529 ;; second argument
530 current-prefix-arg))
532 ;; Init the man package variables, if not already done.
533 (Man-init-defvars)
535 ;; Possibly translate the "subject(section)" syntax into the
536 ;; "section subject" syntax and possibly downcase the section.
537 (setq man-args (Man-translate-references man-args))
539 (Man-getpage-in-background man-args (consp prefix-arg)))
542 (defun Man-getpage-in-background (topic &optional override-reuse-p)
543 "Uses TOPIC to build and fire off the manpage and cleaning command.
544 Optional OVERRIDE-REUSE-P, when non-nil, means to
545 start a background process even if a buffer already exists and
546 `Man-reuse-okay-flag' is non-nil."
547 (let* ((man-args topic)
548 (bufname (concat "*Man " man-args "*"))
549 (buffer (get-buffer bufname)))
550 (if (and Man-reuse-okay-flag
551 (not override-reuse-p)
552 buffer)
553 (Man-notify-when-ready buffer)
554 (require 'env)
555 (message "Invoking %s %s in the background" manual-program man-args)
556 (setq buffer (generate-new-buffer bufname))
557 (save-excursion
558 (set-buffer buffer)
559 (setq Man-original-frame (selected-frame))
560 (setq Man-arguments man-args))
561 (let ((process-environment (copy-sequence process-environment)))
562 ;; Prevent any attempt to use display terminal fanciness.
563 (setenv "TERM" "dumb")
564 (set-process-sentinel
565 (start-process manual-program buffer "sh" "-c"
566 (format (Man-build-man-command) man-args))
567 'Man-bgproc-sentinel))
570 (defun Man-notify-when-ready (man-buffer)
571 "Notify the user when MAN-BUFFER is ready.
572 See the variable `Man-notify-method' for the different notification behaviors."
573 (let ((saved-frame (save-excursion
574 (set-buffer man-buffer)
575 Man-original-frame)))
576 (cond
577 ((eq Man-notify-method 'newframe)
578 ;; Since we run asynchronously, perhaps while Emacs is waiting
579 ;; for input, we must not leave a different buffer current. We
580 ;; can't rely on the editor command loop to reselect the
581 ;; selected window's buffer.
582 (save-excursion
583 (set-buffer man-buffer)
584 (make-frame Man-frame-parameters)))
585 ((eq Man-notify-method 'pushy)
586 (switch-to-buffer man-buffer))
587 ((eq Man-notify-method 'bully)
588 (and window-system
589 (frame-live-p saved-frame)
590 (select-frame saved-frame))
591 (pop-to-buffer man-buffer)
592 (delete-other-windows))
593 ((eq Man-notify-method 'aggressive)
594 (and window-system
595 (frame-live-p saved-frame)
596 (select-frame saved-frame))
597 (pop-to-buffer man-buffer))
598 ((eq Man-notify-method 'friendly)
599 (and window-system
600 (frame-live-p saved-frame)
601 (select-frame saved-frame))
602 (display-buffer man-buffer 'not-this-window))
603 ((eq Man-notify-method 'polite)
604 (beep)
605 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
606 ((eq Man-notify-method 'quiet)
607 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
608 ((or (eq Man-notify-method 'meek)
610 (message ""))
613 (defun Man-fontify-manpage ()
614 "Convert overstriking and underlining to the correct fonts.
615 Same for the ANSI bold and normal escape sequences."
616 (interactive)
617 (message "Please wait: making up the %s man page..." Man-arguments)
618 (goto-char (point-min))
619 (while (search-forward "\e[1m" nil t)
620 (delete-backward-char 4)
621 (put-text-property (point)
622 (progn (if (search-forward "\e[0m" nil 'move)
623 (delete-backward-char 4))
624 (point))
625 'face Man-overstrike-face))
626 (goto-char (point-min))
627 (while (search-forward "_\b" nil t)
628 (backward-delete-char 2)
629 (put-text-property (point) (1+ (point)) 'face Man-underline-face))
630 (goto-char (point-min))
631 (while (search-forward "\b_" nil t)
632 (backward-delete-char 2)
633 (put-text-property (1- (point)) (point) 'face Man-underline-face))
634 (goto-char (point-min))
635 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
636 (replace-match "\\1")
637 (put-text-property (1- (point)) (point) 'face Man-overstrike-face))
638 (goto-char (point-min))
639 (while (search-forward "o\b+" nil t)
640 (backward-delete-char 2)
641 (put-text-property (1- (point)) (point) 'face 'bold))
642 (goto-char (point-min))
643 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t)
644 (replace-match "+")
645 (put-text-property (1- (point)) (point) 'face 'bold))
646 (message "%s man page made up" Man-arguments))
648 (defun Man-cleanup-manpage ()
649 "Remove overstriking and underlining from the current buffer."
650 (interactive)
651 (message "Please wait: cleaning up the %s man page..."
652 Man-arguments)
653 (if (or (interactive-p) (not Man-sed-script))
654 (progn
655 (goto-char (point-min))
656 (while (search-forward "_\b" nil t) (backward-delete-char 2))
657 (goto-char (point-min))
658 (while (search-forward "\b_" nil t) (backward-delete-char 2))
659 (goto-char (point-min))
660 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
661 (replace-match "\\1"))
662 (goto-char (point-min))
663 (while (re-search-forward "\e\\[[0-9]+m" nil t) (replace-match ""))
664 (goto-char (point-min))
665 (while (search-forward "o\b+" nil t) (backward-delete-char 2))
667 (goto-char (point-min))
668 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t) (replace-match "+"))
669 (message "%s man page cleaned up" Man-arguments))
671 (defun Man-bgproc-sentinel (process msg)
672 "Manpage background process sentinel."
673 (let ((Man-buffer (process-buffer process))
674 (delete-buff nil)
675 (err-mess nil))
677 (if (null (buffer-name Man-buffer)) ;; deleted buffer
678 (set-process-buffer process nil)
680 (save-excursion
681 (set-buffer Man-buffer)
682 (save-match-data
683 (let ((case-fold-search nil))
684 (goto-char (point-min))
685 (cond ((or (looking-at "No \\(manual \\)*entry for")
686 (looking-at "[^\n]*: nothing appropriate$"))
687 (setq err-mess (buffer-substring (point)
688 (progn
689 (end-of-line) (point)))
690 delete-buff t))
691 ((not (and (eq (process-status process) 'exit)
692 (= (process-exit-status process) 0)))
693 (setq err-mess
694 (concat (buffer-name Man-buffer)
695 ": process "
696 (let ((eos (1- (length msg))))
697 (if (= (aref msg eos) ?\n)
698 (substring msg 0 eos) msg))))
699 (goto-char (point-max))
700 (insert (format "\nprocess %s" msg))
702 (if delete-buff
703 (kill-buffer Man-buffer)
704 (if Man-fontify-manpage-flag
705 (Man-fontify-manpage)
706 (Man-cleanup-manpage))
707 (run-hooks 'Man-cooked-hook)
708 (Man-mode)
709 (set-buffer-modified-p nil)
711 ;; Restore case-fold-search before calling
712 ;; Man-notify-when-ready because it may switch buffers.
714 (if (not delete-buff)
715 (Man-notify-when-ready Man-buffer))
717 (if err-mess
718 (error err-mess))
719 )))))
722 ;; ======================================================================
723 ;; set up manual mode in buffer and build alists
725 (defun Man-mode ()
726 "A mode for browsing Un*x manual pages.
728 The following man commands are available in the buffer. Try
729 \"\\[describe-key] <key> RET\" for more information:
731 \\[man] Prompt to retrieve a new manpage.
732 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
733 \\[Man-next-manpage] Jump to next manpage in circular list.
734 \\[Man-previous-manpage] Jump to previous manpage in circular list.
735 \\[Man-next-section] Jump to next manpage section.
736 \\[Man-previous-section] Jump to previous manpage section.
737 \\[Man-goto-section] Go to a manpage section.
738 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
739 \\[Man-quit] Deletes the manpage window, bury its buffer.
740 \\[Man-kill] Deletes the manpage window, kill its buffer.
741 \\[describe-mode] Prints this help text.
743 The following variables may be of some use. Try
744 \"\\[describe-variable] <variable-name> RET\" for more information:
746 Man-notify-method What happens when manpage formatting is done.
747 Man-reuse-okay-flag Reuse already formatted buffer.
748 Man-downcase-section-letters-flag Force section letters to lower case.
749 Man-circular-pages-flag Treat multiple manpage list as circular.
750 Man-auto-section-alist List of major modes and their section numbers.
751 Man-section-translations-alist List of section numbers and their Un*x equiv.
752 Man-filter-list Background manpage filter command.
753 Man-mode-line-format Mode line format for Man mode buffers.
754 Man-mode-map Keymap bindings for Man mode buffers.
755 Man-mode-hook Normal hook run on entry to Man mode.
756 Man-section-regexp Regexp describing manpage section letters.
757 Man-heading-regexp Regexp describing section headers.
758 Man-see-also-regexp Regexp for SEE ALSO section (or your equiv).
759 Man-first-heading-regexp Regexp for first heading on a manpage.
760 Man-reference-regexp Regexp matching a references in SEE ALSO.
761 Man-switches Background `man' command switches.
763 The following key bindings are currently in effect in the buffer:
764 \\{Man-mode-map}"
765 (interactive)
766 (setq major-mode 'Man-mode
767 mode-name "Man"
768 buffer-auto-save-file-name nil
769 mode-line-format Man-mode-line-format
770 truncate-lines t
771 buffer-read-only t)
772 (buffer-disable-undo (current-buffer))
773 (auto-fill-mode -1)
774 (use-local-map Man-mode-map)
775 (Man-build-page-list)
776 (Man-strip-page-headers)
777 (Man-unindent)
778 (Man-goto-page 1)
779 (run-hooks 'Man-mode-hook))
781 (defsubst Man-build-section-alist ()
782 "Build the association list of manpage sections."
783 (setq Man-sections-alist nil)
784 (goto-char (point-min))
785 (let ((case-fold-search nil))
786 (while (re-search-forward Man-heading-regexp (point-max) t)
787 (aput 'Man-sections-alist (Man-match-substring 1))
788 (forward-line 1))))
790 (defsubst Man-build-references-alist ()
791 "Build the association list of references (in the SEE ALSO section)."
792 (setq Man-refpages-alist nil)
793 (save-excursion
794 (if (Man-find-section Man-see-also-regexp)
795 (let ((start (progn (forward-line 1) (point)))
796 (end (progn
797 (Man-next-section 1)
798 (point)))
799 hyphenated
800 (runningpoint -1))
801 (save-restriction
802 (narrow-to-region start end)
803 (goto-char (point-min))
804 (back-to-indentation)
805 (while (and (not (eobp)) (/= (point) runningpoint))
806 (setq runningpoint (point))
807 (if (re-search-forward Man-reference-regexp end t)
808 (let* ((word (Man-match-substring 0))
809 (len (1- (length word))))
810 (if hyphenated
811 (setq word (concat hyphenated word)
812 hyphenated nil))
813 (if (= (aref word len) ?-)
814 (setq hyphenated (substring word 0 len))
815 (aput 'Man-refpages-alist word))))
816 (skip-chars-forward " \t\n,")))))))
818 (defun Man-build-page-list ()
819 "Build the list of separate manpages in the buffer."
820 (setq Man-page-list nil)
821 (let ((page-start (point-min))
822 (page-end (point-max))
823 (header ""))
824 (goto-char page-start)
825 ;; (switch-to-buffer (current-buffer))(debug)
826 (while (not (eobp))
827 (setq header
828 (if (looking-at Man-page-header-regexp)
829 (Man-match-substring 1)
830 nil))
831 ;; Go past both the current and the next Man-first-heading-regexp
832 (if (re-search-forward Man-first-heading-regexp nil 'move 2)
833 (let ((p (progn (beginning-of-line) (point))))
834 ;; We assume that the page header is delimited by blank
835 ;; lines and that it contains at most one blank line. So
836 ;; if we back by three blank lines we will be sure to be
837 ;; before the page header but not before the possible
838 ;; previous page header.
839 (search-backward "\n\n" nil t 3)
840 (if (re-search-forward Man-page-header-regexp p 'move)
841 (beginning-of-line))))
842 (setq page-end (point))
843 (setq Man-page-list (append Man-page-list
844 (list (list (copy-marker page-start)
845 (copy-marker page-end)
846 header))))
847 (setq page-start page-end)
850 (defun Man-strip-page-headers ()
851 "Strip all the page headers but the first from the manpage."
852 (let ((buffer-read-only nil)
853 (case-fold-search nil)
854 (page-list Man-page-list)
855 (page ())
856 (header ""))
857 (while page-list
858 (setq page (car page-list))
859 (and (nth 2 page)
860 (goto-char (car page))
861 (re-search-forward Man-first-heading-regexp nil t)
862 (setq header (buffer-substring (car page) (match-beginning 0)))
863 ;; Since the awk script collapses all successive blank
864 ;; lines into one, and since we don't want to get rid of
865 ;; the fast awk script, one must choose between adding
866 ;; spare blank lines between pages when there were none and
867 ;; deleting blank lines at page boundaries when there were
868 ;; some. We choose the first, so we comment the following
869 ;; line.
870 ;; (setq header (concat "\n" header)))
871 (while (search-forward header (nth 1 page) t)
872 (replace-match "")))
873 (setq page-list (cdr page-list)))))
875 (defun Man-unindent ()
876 "Delete the leading spaces that indent the manpage."
877 (let ((buffer-read-only nil)
878 (case-fold-search nil)
879 (page-list Man-page-list))
880 (while page-list
881 (let ((page (car page-list))
882 (indent "")
883 (nindent 0))
884 (narrow-to-region (car page) (car (cdr page)))
885 (if Man-uses-untabify-flag
886 (untabify (point-min) (point-max)))
887 (if (catch 'unindent
888 (goto-char (point-min))
889 (if (not (re-search-forward Man-first-heading-regexp nil t))
890 (throw 'unindent nil))
891 (beginning-of-line)
892 (setq indent (buffer-substring (point)
893 (progn
894 (skip-chars-forward " ")
895 (point))))
896 (setq nindent (length indent))
897 (if (zerop nindent)
898 (throw 'unindent nil))
899 (setq indent (concat indent "\\|$"))
900 (goto-char (point-min))
901 (while (not (eobp))
902 (if (looking-at indent)
903 (forward-line 1)
904 (throw 'unindent nil)))
905 (goto-char (point-min)))
906 (while (not (eobp))
907 (or (eolp)
908 (delete-char nindent))
909 (forward-line 1)))
910 (setq page-list (cdr page-list))
911 ))))
914 ;; ======================================================================
915 ;; Man mode commands
917 (defun Man-next-section (n)
918 "Move point to Nth next section (default 1)."
919 (interactive "p")
920 (let ((case-fold-search nil))
921 (if (looking-at Man-heading-regexp)
922 (forward-line 1))
923 (if (re-search-forward Man-heading-regexp (point-max) t n)
924 (beginning-of-line)
925 (goto-char (point-max)))))
927 (defun Man-previous-section (n)
928 "Move point to Nth previous section (default 1)."
929 (interactive "p")
930 (let ((case-fold-search nil))
931 (if (looking-at Man-heading-regexp)
932 (forward-line -1))
933 (if (re-search-backward Man-heading-regexp (point-min) t n)
934 (beginning-of-line)
935 (goto-char (point-min)))))
937 (defun Man-find-section (section)
938 "Move point to SECTION if it exists, otherwise don't move point.
939 Returns t if section is found, nil otherwise."
940 (let ((curpos (point))
941 (case-fold-search nil))
942 (goto-char (point-min))
943 (if (re-search-forward (concat "^" section) (point-max) t)
944 (progn (beginning-of-line) t)
945 (goto-char curpos)
946 nil)
949 (defun Man-goto-section ()
950 "Query for section to move point to."
951 (interactive)
952 (aput 'Man-sections-alist
953 (let* ((default (aheadsym Man-sections-alist))
954 (completion-ignore-case t)
955 chosen
956 (prompt (concat "Go to section: (default " default ") ")))
957 (setq chosen (completing-read prompt Man-sections-alist))
958 (if (or (not chosen)
959 (string= chosen ""))
960 default
961 chosen)))
962 (Man-find-section (aheadsym Man-sections-alist)))
964 (defun Man-goto-see-also-section ()
965 "Move point the the \"SEE ALSO\" section.
966 Actually the section moved to is described by `Man-see-also-regexp'."
967 (interactive)
968 (if (not (Man-find-section Man-see-also-regexp))
969 (error (concat "No " Man-see-also-regexp
970 " section found in the current manpage"))))
972 (defun Man-follow-manual-reference (arg reference)
973 "Get one of the manpages referred to in the \"SEE ALSO\" section.
974 Specify which reference to use; default is based on word at point.
975 Prefix argument ARG is passed to `Man-getpage-in-background'."
976 (interactive
977 (if (not Man-refpages-alist)
978 (error "There are no references in the current man page")
979 (list current-prefix-arg
980 (let* ((default (or
981 (car (all-completions
982 (save-excursion
983 (skip-syntax-backward "w()")
984 (skip-chars-forward " \t")
985 (let ((word (current-word)))
986 ;; strip a trailing '-':
987 (if (string-match "-$" word)
988 (substring word 0
989 (match-beginning 0))
990 word)))
991 Man-refpages-alist))
992 (aheadsym Man-refpages-alist)))
993 chosen
994 (prompt (concat "Refer to: (default " default ") ")))
995 (setq chosen (completing-read prompt Man-refpages-alist nil t))
996 (if (or (not chosen)
997 (string= chosen ""))
998 default
999 chosen)))))
1000 (if (not Man-refpages-alist)
1001 (error "Can't find any references in the current manpage")
1002 (aput 'Man-refpages-alist reference)
1003 (Man-getpage-in-background
1004 (Man-translate-references (aheadsym Man-refpages-alist))
1005 arg)))
1007 (defun Man-kill ()
1008 "Kill the buffer containing the manpage."
1009 (interactive)
1010 (let ((buff (current-buffer)))
1011 (delete-windows-on buff)
1012 (kill-buffer buff))
1013 (if (and window-system
1014 (or (eq Man-notify-method 'newframe)
1015 (and pop-up-frames
1016 (eq Man-notify-method 'bully))))
1017 (delete-frame)))
1019 (defun Man-quit ()
1020 "Bury the buffer containing the manpage."
1021 (interactive)
1022 (let ((buff (current-buffer)))
1023 (delete-windows-on buff)
1024 (bury-buffer buff))
1025 (if (and window-system
1026 (or (eq Man-notify-method 'newframe)
1027 (and pop-up-frames
1028 (eq Man-notify-method 'bully))))
1029 (delete-frame)))
1031 (defun Man-goto-page (page)
1032 "Go to the manual page on page PAGE."
1033 (interactive
1034 (if (not Man-page-list)
1035 (let ((args Man-arguments))
1036 (kill-buffer (current-buffer))
1037 (error "Can't find the %s manpage" args))
1038 (if (= (length Man-page-list) 1)
1039 (error "You're looking at the only manpage in the buffer")
1040 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1041 (length Man-page-list)))))))
1042 (if (not Man-page-list)
1043 (let ((args Man-arguments))
1044 (kill-buffer (current-buffer))
1045 (error "Can't find the %s manpage" args)))
1046 (if (or (< page 1)
1047 (> page (length Man-page-list)))
1048 (error "No manpage %d found" page))
1049 (let* ((page-range (nth (1- page) Man-page-list))
1050 (page-start (car page-range))
1051 (page-end (car (cdr page-range))))
1052 (setq Man-current-page page
1053 Man-page-mode-string (Man-make-page-mode-string))
1054 (widen)
1055 (goto-char page-start)
1056 (narrow-to-region page-start page-end)
1057 (Man-build-section-alist)
1058 (Man-build-references-alist)
1059 (goto-char (point-min))))
1062 (defun Man-next-manpage ()
1063 "Find the next manpage entry in the buffer."
1064 (interactive)
1065 (if (= (length Man-page-list) 1)
1066 (error "This is the only manpage in the buffer"))
1067 (if (< Man-current-page (length Man-page-list))
1068 (Man-goto-page (1+ Man-current-page))
1069 (if Man-circular-pages-flag
1070 (Man-goto-page 1)
1071 (error "You're looking at the last manpage in the buffer"))))
1073 (defun Man-previous-manpage ()
1074 "Find the previous manpage entry in the buffer."
1075 (interactive)
1076 (if (= (length Man-page-list) 1)
1077 (error "This is the only manpage in the buffer"))
1078 (if (> Man-current-page 1)
1079 (Man-goto-page (1- Man-current-page))
1080 (if Man-circular-pages-flag
1081 (Man-goto-page (length Man-page-list))
1082 (error "You're looking at the first manpage in the buffer"))))
1084 (provide 'man)
1086 ;;; man.el ends here