1 ;;; man.el --- browse UNIX manual pages
3 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
5 ;; Author: Barry A. Warsaw <bwarsaw@cen.com>
7 ;; Adapted-By: ESR, pot
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This code provides a function, `man', with which you can browse
29 ;; UNIX manual pages. Formatting is done in background so that you
30 ;; can continue to use your Emacs while processing is going on.
32 ;; The mode also supports hypertext-like following of manual page SEE
33 ;; ALSO references, and other features. See below or do `?' in a
34 ;; manual page buffer for details.
36 ;; ========== Credits and History ==========
37 ;; In mid 1991, several people posted some interesting improvements to
38 ;; man.el from the standard emacs 18.57 distribution. I liked many of
39 ;; these, but wanted everything in one single package, so I decided
40 ;; to incorporate them into a single manual browsing mode. While
41 ;; much of the code here has been rewritten, and some features added,
42 ;; these folks deserve lots of credit for providing the initial
43 ;; excellent packages on which this one is based.
45 ;; Nick Duffek <duffek@chaos.cs.brandeis.edu>, posted a very nice
46 ;; improvement which retrieved and cleaned the manpages in a
47 ;; background process, and which correctly deciphered such options as
50 ;; Eric Rose <erose@jessica.stanford.edu>, submitted manual.el which
51 ;; provided a very nice manual browsing mode.
53 ;; This package was available as `superman.el' from the LCD package
54 ;; for some time before it was accepted into Emacs 19. The entry
55 ;; point and some other names have been changed to make it a drop-in
56 ;; replacement for the old man.el package.
58 ;; Francesco Potorti` <pot@cnuce.cnr.it> cleaned it up thoroughly,
59 ;; making it faster, more robust and more tolerant of different
60 ;; systems' man idiosyncrasies.
62 ;; ========== Features ==========
63 ;; + Runs "man" in the background and pipes the results through a
64 ;; series of sed and awk scripts so that all retrieving and cleaning
65 ;; is done in the background. The cleaning commands are configurable.
66 ;; + Syntax is the same as Un*x man
67 ;; + Functionality is the same as Un*x man, including "man -k" and
68 ;; "man <section>", etc.
69 ;; + Provides a manual browsing mode with keybindings for traversing
70 ;; the sections of a manpage, following references in the SEE ALSO
72 ;; + Multiple manpages created with the same man command are put into
73 ;; a narrowed buffer circular list.
75 ;; ============= TODO ===========
76 ;; - Add a command for printing.
77 ;; - The awk script deletes multiple blank lines. This behaviour does
78 ;; not allow to understand if there was indeed a blank line at the
79 ;; end or beginning of a page (after the header, or before the
80 ;; footer). A different algorithm should be used. It is easy to
81 ;; compute how many blank lines there are before and after the page
82 ;; headers, and after the page footer. But it is possible to compute
83 ;; the number of blank lines before the page footer by euristhics
84 ;; only. Is it worth doing?
85 ;; - Allow a user option to mean that all the manpages should go in
86 ;; the same buffer, where they can be browsed with M-n and M-p.
87 ;; - Allow completion on the manpage name when calling man. This
88 ;; requires a reliable list of places where manpages can be found. The
89 ;; drawback would be that if the list is not complete, the user might
90 ;; be led to believe that the manpages in the missing directories do
98 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
99 ;; empty defvars (keep the compiler quiet)
102 (defvar Man-current-page)
103 (defvar Man-page-list)
104 (defvar Man-filter-list)
105 (defvar Man-original-frame)
106 (defvar Man-arguments)
107 (defvar Man-sections-alist)
108 (defvar Man-refpages-alist)
109 (defvar Man-uses-untabify-flag)
110 (defvar Man-page-mode-string)
111 (defvar Man-sed-script)
113 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
116 (defvar Man-fontify-manpage-flag t
117 "*Make up the manpage with fonts.")
119 (defvar Man-overstrike-face 'bold
120 "*Face to use when fontifying overstrike.")
122 (defvar Man-underline-face 'underline
123 "*Face to use when fontifying underlining.")
125 ;; Use the value of the obsolete user option Man-notify, if set.
126 (defvar Man-notify-method (if (boundp 'Man-notify) Man-notify 'friendly)
127 "*Selects the behavior when manpage is ready.
128 This variable may have one of the following values, where (sf) means
129 that the frames are switched, so the manpage is displayed in the frame
130 where the man command was called from:
132 newframe -- put the manpage in its own frame (see `Man-frame-parameters')
133 pushy -- make the manpage the current buffer in the current window
134 bully -- make the manpage the current buffer and only window (sf)
135 aggressive -- make the manpage the current buffer in the other window (sf)
136 friendly -- display manpage in the other window but don't make current (sf)
137 polite -- don't display manpage, but prints message and beep when ready
138 quiet -- like `polite', but don't beep
139 meek -- make no indication that the manpage is ready
141 Any other value of `Man-notify-method' is equivalent to `meek'.")
143 (defvar Man-frame-parameters nil
144 "*Frame parameter list for creating a new frame for a manual page.")
146 (defvar Man-downcase-section-letters-flag t
147 "*Letters in sections are converted to lower case.
148 Some Un*x man commands can't handle uppercase letters in sections, for
149 example \"man 2V chmod\", but they are often displayed in the manpage
150 with the upper case letter. When this variable is t, the section
151 letter (e.g., \"2V\") is converted to lowercase (e.g., \"2v\") before
152 being sent to the man background process.")
154 (defvar Man-circular-pages-flag t
155 "*If t, the manpage list is treated as circular for traversal.")
157 (defvar Man-section-translations-alist
160 ;; Some systems have a real 3x man section, so let's comment this.
161 ;; '("3X" . "3") ; Xlib man pages
164 "*Association list of bogus sections to real section numbers.
165 Some manpages (e.g. the Sun C++ 2.1 manpages) have section numbers in
166 their references which Un*x `man' does not recognize. This
167 association list is used to translate those sections, when found, to
168 the associated section number.")
170 (defvar manual-program "man"
171 "The name of the program that produces man pages.")
173 (defvar Man-untabify-command "pr"
174 "Command used for untabifying.")
176 (defvar Man-untabify-command-args (list "-t" "-e")
177 "List of arguments to be passed to Man-untabify-command (which see).")
179 (defvar Man-sed-command "sed"
180 "Command used for processing sed scripts.")
182 (defvar Man-awk-command "awk"
183 "Command used for processing awk scripts.")
185 (defvar Man-mode-line-format
186 '("" mode-line-modified
187 mode-line-buffer-identification " "
189 " " Man-page-mode-string
190 " %[(" mode-name mode-line-process minor-mode-alist ")%]----"
192 "Mode line format for manual mode buffer.")
194 (defvar Man-mode-map nil
195 "Keymap for Man mode.")
197 (defvar Man-mode-hook nil
198 "Hook run when Man mode is enabled.")
200 (defvar Man-cooked-hook nil
201 "Hook run after removing backspaces but before Man-mode processing.")
203 (defvar Man-name-regexp "[-a-zA-Z0-9_][-a-zA-Z0-9_.]*"
204 "Regular expression describing the name of a manpage (without section).")
206 (defvar Man-section-regexp "[0-9][a-zA-Z+]*\\|[LNln]"
207 "Regular expression describing a manpage section within parentheses.")
209 (defvar Man-page-header-regexp
210 (concat "^[ \t]*\\(" Man-name-regexp
211 "(\\(" Man-section-regexp "\\))\\).*\\1")
212 "Regular expression describing the heading of a page.")
214 (defvar Man-heading-regexp "^\\([A-Z][A-Z ]+\\)$"
215 "Regular expression describing a manpage heading entry.")
217 (defvar Man-see-also-regexp "SEE ALSO"
218 "Regular expression for SEE ALSO heading (or your equivalent).
219 This regexp should not start with a `^' character.")
221 (defvar Man-first-heading-regexp "^[ \t]*NAME$\\|^[ \t]*No manual entry fo.*$"
222 "Regular expression describing first heading on a manpage.
223 This regular expression should start with a `^' character.")
225 (defvar Man-reference-regexp
226 (concat "\\(" Man-name-regexp "\\)(\\(" Man-section-regexp "\\))")
227 "Regular expression describing a reference in the SEE ALSO section.")
229 (defvar Man-switches ""
230 "Switches passed to the man command, as a single string.")
232 (defvar Man-specified-section-option
233 (if (string-match "-solaris[0-9.]*$" system-configuration)
236 "Option that indicates a specified a manual section name.")
238 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
239 ;; end user variables
241 ;; other variables and keymap initializations
242 (make-variable-buffer-local 'Man-sections-alist)
243 (make-variable-buffer-local 'Man-refpages-alist)
244 (make-variable-buffer-local 'Man-page-list)
245 (make-variable-buffer-local 'Man-current-page)
246 (make-variable-buffer-local 'Man-page-mode-string)
247 (make-variable-buffer-local 'Man-original-frame)
248 (make-variable-buffer-local 'Man-arguments)
250 (setq-default Man-sections-alist nil)
251 (setq-default Man-refpages-alist nil)
252 (setq-default Man-page-list nil)
253 (setq-default Man-current-page 0)
254 (setq-default Man-page-mode-string "1 of 1")
256 (defconst Man-sysv-sed-script "\
265 /\e\\[[0-9][0-9]*m/ s///g"
266 "Script for sysV-like sed to nuke backspaces and ANSI codes from manpages.")
268 (defconst Man-berkeley-sed-script "\
274 s/\\(.\\)\b\\1/\\1/g\\
277 /\e\\[[0-9][0-9]*m/ s///g"
278 "Script for berkeley-like sed to nuke backspaces and ANSI codes from manpages.")
282 (setq Man-mode-map (make-keymap))
283 (suppress-keymap Man-mode-map)
284 (define-key Man-mode-map " " 'scroll-up)
285 (define-key Man-mode-map "\177" 'scroll-down)
286 (define-key Man-mode-map "n" 'Man-next-section)
287 (define-key Man-mode-map "p" 'Man-previous-section)
288 (define-key Man-mode-map "\en" 'Man-next-manpage)
289 (define-key Man-mode-map "\ep" 'Man-previous-manpage)
290 (define-key Man-mode-map ">" 'end-of-buffer)
291 (define-key Man-mode-map "<" 'beginning-of-buffer)
292 (define-key Man-mode-map "." 'beginning-of-buffer)
293 (define-key Man-mode-map "r" 'Man-follow-manual-reference)
294 (define-key Man-mode-map "g" 'Man-goto-section)
295 (define-key Man-mode-map "s" 'Man-goto-see-also-section)
296 (define-key Man-mode-map "k" 'Man-kill)
297 (define-key Man-mode-map "q" 'Man-quit)
298 (define-key Man-mode-map "m" 'man)
299 (define-key Man-mode-map "?" 'describe-mode)
303 ;; ======================================================================
306 (defsubst Man-init-defvars ()
307 "Used for initialising variables based on the value of window-system.
308 This is necessary if one wants to dump man.el with emacs."
310 ;; The following is necessary until fonts are implemented on
312 (setq Man-fontify-manpage-flag (and Man-fontify-manpage-flag
315 (defconst Man-uses-untabify-flag t
316 ;; don't use pr: it is buggy
317 ;; (or (not (file-readable-p "/etc/passwd"))
318 ;; (/= 0 (apply 'call-process
319 ;; Man-untabify-command nil nil nil
320 ;; (append Man-untabify-command-args
321 ;; (list "/etc/passwd")))))
322 "When non-nil use `untabify' instead of Man-untabify-command.")
324 (defconst Man-sed-script
326 (Man-fontify-manpage-flag
328 ((= 0 (call-process Man-sed-command nil nil nil Man-sysv-sed-script))
330 ((= 0 (call-process Man-sed-command nil nil nil Man-berkeley-sed-script))
331 Man-berkeley-sed-script)
334 "Script for sed to nuke backspaces and ANSI codes from manpages.")
336 (defvar Man-filter-list
342 (concat "-e '" Man-sed-script "'")
344 "-e '/^[\001-\032][\001-\032]*$/d'"
345 "-e '/\e[789]/s///g'"
346 "-e '/Reformatting page. Wait/d'"
347 "-e '/Reformatting entry. Wait/d'"
348 "-e '/^[ \t]*Hewlett-Packard[ \t]Company[ \t]*-[ \t][0-9]*[ \t]-/d'"
349 "-e '/^[ \t]*Hewlett-Packard[ \t]*-[ \t][0-9]*[ \t]-.*$/d'"
350 "-e '/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d'"
351 "-e '/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d'"
352 "-e '/^Printed[ \t][0-9].*[0-9]$/d'"
353 "-e '/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d'"
354 "-e '/^[A-za-z].*Last[ \t]change:/d'"
355 "-e '/^Sun[ \t]Release[ \t][0-9].*[0-9]$/d'"
356 "-e '/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d'"
357 "-e '/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d'"
363 "BEGIN { blankline=0; anonblank=0; }\n"
364 "/^$/ { if (anonblank==0) next; }\n"
366 "/^$/ { blankline++; next; }\n"
367 "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }\n"
370 (if (not Man-uses-untabify-flag)
373 Man-untabify-command-args)
375 "*Manpage cleaning filter command phrases.
376 This variable contains a list of the following form:
378 '((command-string phrase-string*)*)
380 Each phrase-string is concatenated onto the command-string to form a
381 command filter. The (standard) output (and standard error) of the Un*x
382 man command is piped through each command filter in the order the
383 commands appear in the association list. The final output is placed in
384 the manpage buffer.")
387 (defsubst Man-match-substring (&optional n string)
388 "Return the substring matched by the last search.
389 Optional arg N means return the substring matched by the Nth paren
390 grouping. Optional second arg STRING means return a substring from
391 that string instead of from the current buffer."
392 (if (null n) (setq n 0))
394 (substring string (match-beginning n) (match-end n))
395 (buffer-substring (match-beginning n) (match-end n))))
397 (defsubst Man-make-page-mode-string ()
398 "Formats part of the mode line for Man mode."
399 (format "%s page %d of %d"
400 (or (nth 2 (nth (1- Man-current-page) Man-page-list))
403 (length Man-page-list)))
405 (defsubst Man-build-man-command ()
406 "Builds the entire background manpage and cleaning command."
407 (let ((command (concat manual-program " " Man-switches " %s 2>/dev/null"))
408 (flist Man-filter-list))
409 (while (and flist (car flist))
410 (let ((pcom (car (car flist)))
411 (pargs (cdr (car flist))))
413 (concat command " | " pcom " "
414 (mapconcat '(lambda (phrase)
415 (if (not (stringp phrase))
416 (error "Malformed Man-filter-list"))
419 (setq flist (cdr flist))))
422 (defun Man-translate-references (ref)
423 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style.
424 Leave it as is if already in that style. Possibly downcase and
425 translate the section (see the Man-downcase-section-letters-flag
426 and the Man-section-translations-alist variables)."
429 (slist Man-section-translations-alist))
431 ;; "chmod(2V)" case ?
432 ((string-match (concat "^" Man-reference-regexp "$") ref)
433 (setq name (Man-match-substring 1 ref)
434 section (Man-match-substring 2 ref)))
436 ((string-match (concat "^\\(" Man-section-regexp
437 "\\) +\\(" Man-name-regexp "\\)$") ref)
438 (setq name (Man-match-substring 2 ref)
439 section (Man-match-substring 1 ref))))
440 (if (string= name "")
441 ref ; Return the reference as is
442 (if Man-downcase-section-letters-flag
443 (setq section (downcase section)))
445 (let ((s1 (car (car slist)))
446 (s2 (cdr (car slist))))
447 (setq slist (cdr slist))
448 (if Man-downcase-section-letters-flag
449 (setq s1 (downcase s1)))
450 (if (not (string= s1 section)) nil
451 (setq section (if Man-downcase-section-letters-flag
455 (concat Man-specified-section-option section " " name))))
458 ;; ======================================================================
459 ;; default man entry: get word under point
461 (defsubst Man-default-man-entry ()
462 "Make a guess at a default manual entry.
463 This guess is based on the text surrounding the cursor, and the
464 default section number is selected from `Man-auto-section-alist'."
468 ;; Default man entry title is any word the cursor is on, or if
469 ;; cursor not on a word, then nearest preceding word. Cannot
470 ;; use the current-word function because it skips the dots.
471 (if (not (looking-at "[-a-zA-Z_.]"))
472 (skip-chars-backward "^a-zA-Z"))
473 (skip-chars-backward "-(a-zA-Z_0-9_.")
474 (if (looking-at "(") (forward-char 1))
478 (progn (skip-chars-forward "-a-zA-Z0-9_.") (point))))
480 ;; If looking at something like ioctl(2) or brc(1M), include the
481 ;; section number in the returned value. Remove text properties.
482 (let ((result (concat
485 (concat "[ \t]*([ \t]*\\("
486 Man-section-regexp "\\)[ \t]*)"))
487 (format "(%s)" (Man-match-substring 1))))))
488 (set-text-properties 0 (length result) nil result)
492 ;; ======================================================================
493 ;; Top level command and background process sentinel
495 ;; For compatibility with older versions.
497 (defalias 'manual-entry 'man)
500 (defun man (man-args)
501 "Get a Un*x manual page and put it in a buffer.
502 This command is the top-level command in the man package. It runs a Un*x
503 command to retrieve and clean a manpage in the background and places the
504 results in a Man mode (manpage browsing) buffer. See variable
505 `Man-notify-method' for what happens when the buffer is ready.
506 If a buffer already exists for this man page, it will display immediately."
508 (list (let* ((default-entry (Man-default-man-entry))
510 (format "Manual entry%s: "
511 (if (string= default-entry "")
513 (format " (default %s)" default-entry))))))
514 (if (string= input "")
515 (if (string= default-entry "")
516 (error "No man args given")
520 ;; Init the man package variables, if not already done.
523 ;; Possibly translate the "subject(section)" syntax into the
524 ;; "section subject" syntax and possibly downcase the section.
525 (setq man-args (Man-translate-references man-args))
527 (Man-getpage-in-background man-args))
530 (defun Man-getpage-in-background (topic)
531 "Uses TOPIC to build and fire off the manpage and cleaning command."
532 (let* ((man-args topic)
533 (bufname (concat "*Man " man-args "*"))
534 (buffer (get-buffer bufname)))
536 (Man-notify-when-ready buffer)
538 (message "Invoking %s %s in the background" manual-program man-args)
539 (setq buffer (generate-new-buffer bufname))
542 (setq Man-original-frame (selected-frame))
543 (setq Man-arguments man-args))
544 (let ((process-environment (copy-sequence process-environment)))
545 ;; Prevent any attempt to use display terminal fanciness.
546 (setenv "TERM" "dumb")
547 (set-process-sentinel
548 (start-process manual-program buffer "sh" "-c"
549 (format (Man-build-man-command) man-args))
550 'Man-bgproc-sentinel)))))
552 (defun Man-notify-when-ready (man-buffer)
553 "Notify the user when MAN-BUFFER is ready.
554 See the variable `Man-notify-method' for the different notification behaviors."
555 (let ((saved-frame (save-excursion
556 (set-buffer man-buffer)
557 Man-original-frame)))
559 ((eq Man-notify-method 'newframe)
560 ;; Since we run asynchronously, perhaps while Emacs is waiting
561 ;; for input, we must not leave a different buffer current. We
562 ;; can't rely on the editor command loop to reselect the
563 ;; selected window's buffer.
565 (set-buffer man-buffer)
566 (make-frame Man-frame-parameters)))
567 ((eq Man-notify-method 'pushy)
568 (switch-to-buffer man-buffer))
569 ((eq Man-notify-method 'bully)
571 (frame-live-p saved-frame)
572 (select-frame saved-frame))
573 (pop-to-buffer man-buffer)
574 (delete-other-windows))
575 ((eq Man-notify-method 'aggressive)
577 (frame-live-p saved-frame)
578 (select-frame saved-frame))
579 (pop-to-buffer man-buffer))
580 ((eq Man-notify-method 'friendly)
582 (frame-live-p saved-frame)
583 (select-frame saved-frame))
584 (display-buffer man-buffer 'not-this-window))
585 ((eq Man-notify-method 'polite)
587 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
588 ((eq Man-notify-method 'quiet)
589 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
590 ((or (eq Man-notify-method 'meek)
595 (defun Man-fontify-manpage ()
596 "Convert overstriking and underlining to the correct fonts.
597 Same for the ANSI bold and normal escape sequences."
599 (message "Please wait: making up the %s man page..." Man-arguments)
600 (goto-char (point-min))
601 (while (search-forward "\e[1m" nil t)
602 (delete-backward-char 4)
603 (put-text-property (point)
604 (progn (if (search-forward "\e[0m" nil 'move)
605 (delete-backward-char 4))
607 'face Man-overstrike-face))
608 (goto-char (point-min))
609 (while (search-forward "_\b" nil t)
610 (backward-delete-char 2)
611 (put-text-property (point) (1+ (point)) 'face Man-underline-face))
612 (goto-char (point-min))
613 (while (search-forward "\b_" nil t)
614 (backward-delete-char 2)
615 (put-text-property (1- (point)) (point) 'face Man-underline-face))
616 (goto-char (point-min))
617 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
618 (replace-match "\\1")
619 (put-text-property (1- (point)) (point) 'face Man-overstrike-face))
620 (goto-char (point-min))
621 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t)
623 (put-text-property (1- (point)) (point) 'face 'bold))
624 (goto-char (point-min))
625 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t)
627 (put-text-property (1- (point)) (point) 'face 'bold))
628 ;; \255 is some kind of dash in Latin-1.
629 (goto-char (point-min))
630 (while (search-forward "\255" nil t) (replace-match "-"))
631 (message "%s man page made up" Man-arguments))
633 (defun Man-cleanup-manpage ()
634 "Remove overstriking and underlining from the current buffer."
636 (message "Please wait: cleaning up the %s man page..."
638 (if (or (interactive-p) (not Man-sed-script))
640 (goto-char (point-min))
641 (while (search-forward "_\b" nil t) (backward-delete-char 2))
642 (goto-char (point-min))
643 (while (search-forward "\b_" nil t) (backward-delete-char 2))
644 (goto-char (point-min))
645 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
646 (replace-match "\\1"))
647 (goto-char (point-min))
648 (while (re-search-forward "\e\\[[0-9]+m" nil t) (replace-match ""))
649 (goto-char (point-min))
650 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t) (replace-match "o"))
652 (goto-char (point-min))
653 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t) (replace-match "+"))
654 ;; \255 is some kind of dash in Latin-1.
655 (goto-char (point-min))
656 (while (search-forward "\255" nil t) (replace-match "-"))
657 (message "%s man page cleaned up" Man-arguments))
659 (defun Man-bgproc-sentinel (process msg)
660 "Manpage background process sentinel."
661 (let ((Man-buffer (process-buffer process))
665 (if (null (buffer-name Man-buffer)) ;; deleted buffer
666 (set-process-buffer process nil)
669 (set-buffer Man-buffer)
670 (let ((case-fold-search nil))
671 (goto-char (point-min))
672 (cond ((or (looking-at "No \\(manual \\)*entry for")
673 (looking-at "[^\n]*: nothing appropriate$"))
674 (setq err-mess (buffer-substring (point)
676 (end-of-line) (point)))
678 ((not (and (eq (process-status process) 'exit)
679 (= (process-exit-status process) 0)))
681 (concat (buffer-name Man-buffer)
683 (let ((eos (1- (length msg))))
684 (if (= (aref msg eos) ?\n)
685 (substring msg 0 eos) msg))))
686 (goto-char (point-max))
687 (insert (format "\nprocess %s" msg))
690 (kill-buffer Man-buffer)
691 (if Man-fontify-manpage-flag
692 (Man-fontify-manpage)
693 (Man-cleanup-manpage))
694 (run-hooks 'Man-cooked-hook)
696 (set-buffer-modified-p nil)
698 ;; Restore case-fold-search before calling
699 ;; Man-notify-when-ready because it may switch buffers.
701 (if (not delete-buff)
702 (Man-notify-when-ready Man-buffer))
709 ;; ======================================================================
710 ;; set up manual mode in buffer and build alists
713 "A mode for browsing Un*x manual pages.
715 The following man commands are available in the buffer. Try
716 \"\\[describe-key] <key> RET\" for more information:
718 \\[man] Prompt to retrieve a new manpage.
719 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
720 \\[Man-next-manpage] Jump to next manpage in circular list.
721 \\[Man-previous-manpage] Jump to previous manpage in circular list.
722 \\[Man-next-section] Jump to next manpage section.
723 \\[Man-previous-section] Jump to previous manpage section.
724 \\[Man-goto-section] Go to a manpage section.
725 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
726 \\[Man-quit] Deletes the manpage window, bury its buffer.
727 \\[Man-kill] Deletes the manpage window, kill its buffer.
728 \\[describe-mode] Prints this help text.
730 The following variables may be of some use. Try
731 \"\\[describe-variable] <variable-name> RET\" for more information:
733 Man-notify-method What happens when manpage formatting is done.
734 Man-downcase-section-letters-flag Force section letters to lower case.
735 Man-circular-pages-flag Treat multiple manpage list as circular.
736 Man-auto-section-alist List of major modes and their section numbers.
737 Man-section-translations-alist List of section numbers and their Un*x equiv.
738 Man-filter-list Background manpage filter command.
739 Man-mode-line-format Mode line format for Man mode buffers.
740 Man-mode-map Keymap bindings for Man mode buffers.
741 Man-mode-hook Normal hook run on entry to Man mode.
742 Man-section-regexp Regexp describing manpage section letters.
743 Man-heading-regexp Regexp describing section headers.
744 Man-see-also-regexp Regexp for SEE ALSO section (or your equiv).
745 Man-first-heading-regexp Regexp for first heading on a manpage.
746 Man-reference-regexp Regexp matching a references in SEE ALSO.
747 Man-switches Background `man' command switches.
749 The following key bindings are currently in effect in the buffer:
752 (setq major-mode 'Man-mode
754 buffer-auto-save-file-name nil
755 mode-line-format Man-mode-line-format
758 (buffer-disable-undo (current-buffer))
760 (use-local-map Man-mode-map)
761 (Man-build-page-list)
762 (Man-strip-page-headers)
765 (run-hooks 'Man-mode-hook))
767 (defsubst Man-build-section-alist ()
768 "Build the association list of manpage sections."
769 (setq Man-sections-alist nil)
770 (goto-char (point-min))
771 (let ((case-fold-search nil))
772 (while (re-search-forward Man-heading-regexp (point-max) t)
773 (aput 'Man-sections-alist (Man-match-substring 1))
776 (defsubst Man-build-references-alist ()
777 "Build the association list of references (in the SEE ALSO section)."
778 (setq Man-refpages-alist nil)
780 (if (Man-find-section Man-see-also-regexp)
781 (let ((start (progn (forward-line 1) (point)))
788 (narrow-to-region start end)
789 (goto-char (point-min))
790 (back-to-indentation)
791 (while (and (not (eobp)) (/= (point) runningpoint))
792 (setq runningpoint (point))
793 (if (re-search-forward Man-reference-regexp end t)
794 (let* ((word (Man-match-substring 0))
795 (len (1- (length word))))
797 (setq word (concat hyphenated word)
799 (if (= (aref word len) ?-)
800 (setq hyphenated (substring word 0 len))
801 (aput 'Man-refpages-alist word))))
802 (skip-chars-forward " \t\n,")))))))
804 (defun Man-build-page-list ()
805 "Build the list of separate manpages in the buffer."
806 (setq Man-page-list nil)
807 (let ((page-start (point-min))
808 (page-end (point-max))
810 (goto-char page-start)
811 ;; (switch-to-buffer (current-buffer))(debug)
814 (if (looking-at Man-page-header-regexp)
815 (Man-match-substring 1)
817 ;; Go past both the current and the next Man-first-heading-regexp
818 (if (re-search-forward Man-first-heading-regexp nil 'move 2)
819 (let ((p (progn (beginning-of-line) (point))))
820 ;; We assume that the page header is delimited by blank
821 ;; lines and that it contains at most one blank line. So
822 ;; if we back by three blank lines we will be sure to be
823 ;; before the page header but not before the possible
824 ;; previous page header.
825 (search-backward "\n\n" nil t 3)
826 (if (re-search-forward Man-page-header-regexp p 'move)
827 (beginning-of-line))))
828 (setq page-end (point))
829 (setq Man-page-list (append Man-page-list
830 (list (list (copy-marker page-start)
831 (copy-marker page-end)
833 (setq page-start page-end)
836 (defun Man-strip-page-headers ()
837 "Strip all the page headers but the first from the manpage."
838 (let ((buffer-read-only nil)
839 (case-fold-search nil)
840 (page-list Man-page-list)
844 (setq page (car page-list))
846 (goto-char (car page))
847 (re-search-forward Man-first-heading-regexp nil t)
848 (setq header (buffer-substring (car page) (match-beginning 0)))
849 ;; Since the awk script collapses all successive blank
850 ;; lines into one, and since we don't want to get rid of
851 ;; the fast awk script, one must choose between adding
852 ;; spare blank lines between pages when there were none and
853 ;; deleting blank lines at page boundaries when there were
854 ;; some. We choose the first, so we comment the following
856 ;; (setq header (concat "\n" header)))
857 (while (search-forward header (nth 1 page) t)
859 (setq page-list (cdr page-list)))))
861 (defun Man-unindent ()
862 "Delete the leading spaces that indent the manpage."
863 (let ((buffer-read-only nil)
864 (case-fold-search nil)
865 (page-list Man-page-list))
867 (let ((page (car page-list))
870 (narrow-to-region (car page) (car (cdr page)))
871 (if Man-uses-untabify-flag
872 (untabify (point-min) (point-max)))
874 (goto-char (point-min))
875 (if (not (re-search-forward Man-first-heading-regexp nil t))
876 (throw 'unindent nil))
878 (setq indent (buffer-substring (point)
880 (skip-chars-forward " ")
882 (setq nindent (length indent))
884 (throw 'unindent nil))
885 (setq indent (concat indent "\\|$"))
886 (goto-char (point-min))
888 (if (looking-at indent)
890 (throw 'unindent nil)))
891 (goto-char (point-min)))
894 (delete-char nindent))
896 (setq page-list (cdr page-list))
900 ;; ======================================================================
903 (defun Man-next-section (n)
904 "Move point to Nth next section (default 1)."
906 (let ((case-fold-search nil))
907 (if (looking-at Man-heading-regexp)
909 (if (re-search-forward Man-heading-regexp (point-max) t n)
911 (goto-char (point-max)))))
913 (defun Man-previous-section (n)
914 "Move point to Nth previous section (default 1)."
916 (let ((case-fold-search nil))
917 (if (looking-at Man-heading-regexp)
919 (if (re-search-backward Man-heading-regexp (point-min) t n)
921 (goto-char (point-min)))))
923 (defun Man-find-section (section)
924 "Move point to SECTION if it exists, otherwise don't move point.
925 Returns t if section is found, nil otherwise."
926 (let ((curpos (point))
927 (case-fold-search nil))
928 (goto-char (point-min))
929 (if (re-search-forward (concat "^" section) (point-max) t)
930 (progn (beginning-of-line) t)
935 (defun Man-goto-section ()
936 "Query for section to move point to."
938 (aput 'Man-sections-alist
939 (let* ((default (aheadsym Man-sections-alist))
940 (completion-ignore-case t)
942 (prompt (concat "Go to section: (default " default ") ")))
943 (setq chosen (completing-read prompt Man-sections-alist))
948 (Man-find-section (aheadsym Man-sections-alist)))
950 (defun Man-goto-see-also-section ()
951 "Move point the the \"SEE ALSO\" section.
952 Actually the section moved to is described by `Man-see-also-regexp'."
954 (if (not (Man-find-section Man-see-also-regexp))
955 (error (concat "No " Man-see-also-regexp
956 " section found in the current manpage"))))
958 (defun Man-follow-manual-reference (reference)
959 "Get one of the manpages referred to in the \"SEE ALSO\" section.
960 Specify which reference to use; default is based on word at point."
962 (if (not Man-refpages-alist)
963 (error "There are no references in the current man page")
964 (list (let* ((default (or
965 (car (all-completions
967 (skip-syntax-backward "w()")
968 (skip-chars-forward " \t")
969 (let ((word (current-word)))
970 ;; strip a trailing '-':
971 (if (string-match "-$" word)
976 (aheadsym Man-refpages-alist)))
978 (prompt (concat "Refer to: (default " default ") ")))
979 (setq chosen (completing-read prompt Man-refpages-alist nil t))
984 (if (not Man-refpages-alist)
985 (error "Can't find any references in the current manpage")
986 (aput 'Man-refpages-alist reference)
987 (Man-getpage-in-background
988 (Man-translate-references (aheadsym Man-refpages-alist)))))
991 "Kill the buffer containing the manpage."
993 (let ((buff (current-buffer)))
994 (delete-windows-on buff)
996 (if (and window-system
997 (or (eq Man-notify-method 'newframe)
999 (eq Man-notify-method 'bully))))
1003 "Bury the buffer containing the manpage."
1005 (let ((buff (current-buffer)))
1006 (delete-windows-on buff)
1008 (if (and window-system
1009 (or (eq Man-notify-method 'newframe)
1011 (eq Man-notify-method 'bully))))
1014 (defun Man-goto-page (page)
1015 "Go to the manual page on page PAGE."
1017 (if (not Man-page-list)
1018 (let ((args Man-arguments))
1019 (kill-buffer (current-buffer))
1020 (error "Can't find the %s manpage" args))
1021 (if (= (length Man-page-list) 1)
1022 (error "You're looking at the only manpage in the buffer")
1023 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1024 (length Man-page-list)))))))
1025 (if (not Man-page-list)
1026 (let ((args Man-arguments))
1027 (kill-buffer (current-buffer))
1028 (error "Can't find the %s manpage" args)))
1030 (> page (length Man-page-list)))
1031 (error "No manpage %d found" page))
1032 (let* ((page-range (nth (1- page) Man-page-list))
1033 (page-start (car page-range))
1034 (page-end (car (cdr page-range))))
1035 (setq Man-current-page page
1036 Man-page-mode-string (Man-make-page-mode-string))
1038 (goto-char page-start)
1039 (narrow-to-region page-start page-end)
1040 (Man-build-section-alist)
1041 (Man-build-references-alist)
1042 (goto-char (point-min))))
1045 (defun Man-next-manpage ()
1046 "Find the next manpage entry in the buffer."
1048 (if (= (length Man-page-list) 1)
1049 (error "This is the only manpage in the buffer"))
1050 (if (< Man-current-page (length Man-page-list))
1051 (Man-goto-page (1+ Man-current-page))
1052 (if Man-circular-pages-flag
1054 (error "You're looking at the last manpage in the buffer"))))
1056 (defun Man-previous-manpage ()
1057 "Find the previous manpage entry in the buffer."
1059 (if (= (length Man-page-list) 1)
1060 (error "This is the only manpage in the buffer"))
1061 (if (> Man-current-page 1)
1062 (Man-goto-page (1- Man-current-page))
1063 (if Man-circular-pages-flag
1064 (Man-goto-page (length Man-page-list))
1065 (error "You're looking at the first manpage in the buffer"))))
1069 ;;; man.el ends here