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
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;; This code provides a function, `man', with which you can browse
28 ;; UNIX manual pages. Formatting is done in background so that you
29 ;; can continue to use your Emacs while processing is going on.
31 ;; The mode also supports hypertext-like following of manual page SEE
32 ;; ALSO references, and other features. See below or do `?' in a
33 ;; manual page buffer for details.
35 ;; ========== Credits and History ==========
36 ;; In mid 1991, several people posted some interesting improvements to
37 ;; man.el from the standard emacs 18.57 distribution. I liked many of
38 ;; these, but wanted everthing in one single package, so I decided
39 ;; to incorporate them into a single manual browsing mode. While
40 ;; much of the code here has been rewritten, and some features added,
41 ;; these folks deserve lots of credit for providing the initial
42 ;; excellent packages on which this one is based.
44 ;; Nick Duffek <duffek@chaos.cs.brandeis.edu>, posted a very nice
45 ;; improvement which retrieved and cleaned the manpages in a
46 ;; background process, and which correctly deciphered such options as
49 ;; Eric Rose <erose@jessica.stanford.edu>, submitted manual.el which
50 ;; provided a very nice manual browsing mode.
52 ;; This package was available as `superman.el' from the LCD package
53 ;; for some time before it was accepted into Emacs 19. The entry
54 ;; point and some other names have been changed to make it a drop-in
55 ;; replacement for the old man.el package.
57 ;; Francesco Potorti` <pot@cnuce.cnr.it> cleaned it up thoroughly,
58 ;; making it faster, more robust and more tolerant of different
59 ;; systems' man idiosynchrasies.
61 ;; ========== Features ==========
62 ;; + Runs "man" in the background and pipes the results through a
63 ;; series of sed and awk scripts so that all retrieving and cleaning
64 ;; is done in the background. The cleaning commands are configurable.
65 ;; + Syntax is the same as Un*x man
66 ;; + Functionality is the same as Un*x man, including "man -k" and
67 ;; "man <section>", etc.
68 ;; + Provides a manual browsing mode with keybindings for traversing
69 ;; the sections of a manpage, following references in the SEE ALSO
71 ;; + Multiple manpages created with the same man command are put into
72 ;; a narrowed buffer circular list.
74 ;; ============= TODO ===========
75 ;; - Add a command for printing.
76 ;; - The awk script deletes multiple blank lines. This behaviour does
77 ;; not allow to understand if there was indeed a blank line at the
78 ;; end or beginning of a page (after the header, or before the
79 ;; footer). A different algorithm should be used. It is easy to
80 ;; compute how many blank lines there are before and after the page
81 ;; headers, and after the page footer. But it is possible to compute
82 ;; the number of blank lines before the page footer by euristhics
83 ;; only. Is it worth doing?
84 ;; - Allow a user option to mean that all the manpages should go in
85 ;; the same buffer, where they can be browsed with M-n and M-p.
86 ;; - Allow completion on the manpage name when calling man. This
87 ;; requires a reliable list of places where manpages can be found. The
88 ;; drawback would be that if the list is not complete, the user might
89 ;; be led to believe that the manpages in the missing directories do
97 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
98 ;; empty defvars (keep the compiler quiet)
101 (defvar Man-current-page
)
102 (defvar Man-page-list
)
103 (defvar Man-filter-list
)
104 (defvar Man-original-frame
)
105 (defvar Man-arguments
)
106 (defvar Man-sections-alist
)
107 (defvar Man-refpages-alist
)
108 (defvar Man-uses-untabify-flag
)
109 (defvar Man-page-mode-string
)
110 (defvar Man-sed-script
)
112 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
115 (defvar Man-fontify-manpage-flag t
116 "*Make up the manpage with fonts.")
118 (defvar Man-overstrike-face
'bold
119 "*Face to use when fontifying overstrike.")
121 (defvar Man-underline-face
'underline
122 "*Face to use when fontifying underlining.")
124 ;; Use the value of the obsolete user option Man-notify, if set.
125 (defvar Man-notify-method
(if (boundp 'Man-notify
) Man-notify
'friendly
)
126 "*Selects the behavior when manpage is ready.
127 This variable may have one of the following values, where (sf) means
128 that the frames are switched, so the manpage is displayed in the frame
129 where the man command was called from:
131 newframe -- put the manpage in its own frame (see `Man-frame-parameters')
132 pushy -- make the manpage the current buffer in the current window
133 bully -- make the manpage the current buffer and only window (sf)
134 aggressive -- make the manpage the current buffer in the other window (sf)
135 friendly -- display manpage in the other window but don't make current (sf)
136 polite -- don't display manpage, but prints message and beep when ready
137 quiet -- like `polite', but don't beep
138 meek -- make no indication that the manpage is ready
140 Any other value of `Man-notify-method' is equivalent to `meek'.")
142 (defvar Man-frame-parameters nil
143 "*Frame parameter list for creating a new frame for a manual page.")
145 (defvar Man-downcase-section-letters-flag t
146 "*Letters in sections are converted to lower case.
147 Some Un*x man commands can't handle uppercase letters in sections, for
148 example \"man 2V chmod\", but they are often displayed in the manpage
149 with the upper case letter. When this variable is t, the section
150 letter (e.g., \"2V\") is converted to lowercase (e.g., \"2v\") before
151 being sent to the man background process.")
153 (defvar Man-circular-pages-flag t
154 "*If t, the manpage list is treated as circular for traversal.")
156 (defvar Man-section-translations-alist
159 ;; Some systems have a real 3x man section, so let's comment this.
160 ;; '("3X" . "3") ; Xlib man pages
163 "*Association list of bogus sections to real section numbers.
164 Some manpages (e.g. the Sun C++ 2.1 manpages) have section numbers in
165 their references which Un*x `man' does not recognize. This
166 association list is used to translate those sections, when found, to
167 the associated section number.")
169 (defvar manual-program
"man"
170 "The name of the program that produces man pages.")
172 (defvar Man-untabify-command
"pr"
173 "Command used for untabifying.")
175 (defvar Man-untabify-command-args
(list "-t" "-e")
176 "List of arguments to be passed to Man-untabify-command (which see).")
178 (defvar Man-sed-command
"sed"
179 "Command used for processing sed scripts.")
181 (defvar Man-awk-command
"awk"
182 "Command used for processing awk scripts.")
184 (defvar Man-mode-line-format
185 '("" mode-line-modified
186 mode-line-buffer-identification
" "
188 " " Man-page-mode-string
189 " %[(" mode-name mode-line-process minor-mode-alist
")%]----"
191 "Mode line format for manual mode buffer.")
193 (defvar Man-mode-map nil
194 "Keymap for Man mode.")
196 (defvar Man-mode-hook nil
197 "Hook run when Man mode is enabled.")
199 (defvar Man-cooked-hook nil
200 "Hook run after removing backspaces but before Man-mode processing.")
202 (defvar Man-name-regexp
"[-a-zA-Z0-9_][-a-zA-Z0-9_.]*"
203 "Regular expression describing the name of a manpage (without section).")
205 (defvar Man-section-regexp
"[0-9][a-zA-Z+]*\\|[LNln]"
206 "Regular expression describing a manpage section within parentheses.")
208 (defvar Man-page-header-regexp
209 (concat "^[ \t]*\\(" Man-name-regexp
210 "(\\(" Man-section-regexp
"\\))\\).*\\1")
211 "Regular expression describing the heading of a page.")
213 (defvar Man-heading-regexp
"^\\([A-Z][A-Z ]+\\)$"
214 "Regular expression describing a manpage heading entry.")
216 (defvar Man-see-also-regexp
"SEE ALSO"
217 "Regular expression for SEE ALSO heading (or your equivalent).
218 This regexp should not start with a `^' character.")
220 (defvar Man-first-heading-regexp
"^[ \t]*NAME$\\|^[ \t]*No manual entry fo.*$"
221 "Regular expression describing first heading on a manpage.
222 This regular expression should start with a `^' character.")
224 (defvar Man-reference-regexp
225 (concat "\\(" Man-name-regexp
"\\)(\\(" Man-section-regexp
"\\))")
226 "Regular expression describing a reference in the SEE ALSO section.")
228 (defvar Man-switches
""
229 "Switches passed to the man command, as a single string.")
231 (defvar Man-specified-section-option
232 (if (string-match "-solaris[0-9.]*$" system-configuration
)
235 "Option that indicates a specified a manual section name.")
237 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
238 ;; end user variables
240 ;; other variables and keymap initializations
241 (make-variable-buffer-local 'Man-sections-alist
)
242 (make-variable-buffer-local 'Man-refpages-alist
)
243 (make-variable-buffer-local 'Man-page-list
)
244 (make-variable-buffer-local 'Man-current-page
)
245 (make-variable-buffer-local 'Man-page-mode-string
)
246 (make-variable-buffer-local 'Man-original-frame
)
247 (make-variable-buffer-local 'Man-arguments
)
249 (setq-default Man-sections-alist nil
)
250 (setq-default Man-refpages-alist nil
)
251 (setq-default Man-page-list nil
)
252 (setq-default Man-current-page
0)
253 (setq-default Man-page-mode-string
"1 of 1")
255 (defconst Man-sysv-sed-script
"\
264 /\e\\[[0-9][0-9]*m/ s///g"
265 "Script for sysV-like sed to nuke backspaces and ANSI codes from manpages.")
267 (defconst Man-berkeley-sed-script
"\
273 s/\\(.\\)\b\\1/\\1/g\\
276 /\e\\[[0-9][0-9]*m/ s///g"
277 "Script for berkeley-like sed to nuke backspaces and ANSI codes from manpages.")
281 (setq Man-mode-map
(make-keymap))
282 (suppress-keymap Man-mode-map
)
283 (define-key Man-mode-map
" " 'scroll-up
)
284 (define-key Man-mode-map
"\177" 'scroll-down
)
285 (define-key Man-mode-map
"n" 'Man-next-section
)
286 (define-key Man-mode-map
"p" 'Man-previous-section
)
287 (define-key Man-mode-map
"\en" 'Man-next-manpage
)
288 (define-key Man-mode-map
"\ep" 'Man-previous-manpage
)
289 (define-key Man-mode-map
">" 'end-of-buffer
)
290 (define-key Man-mode-map
"<" 'beginning-of-buffer
)
291 (define-key Man-mode-map
"." 'beginning-of-buffer
)
292 (define-key Man-mode-map
"r" 'Man-follow-manual-reference
)
293 (define-key Man-mode-map
"t" 'toggle-truncate-lines
)
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'"
362 "BEGIN { blankline=0; anonblank=0; }\n"
363 "/^$/ { if (anonblank==0) next; }\n"
365 "/^$/ { blankline++; next; }\n"
366 "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }\n"
369 (if (not Man-uses-untabify-flag
)
372 Man-untabify-command-args
)
374 "*Manpage cleaning filter command phrases.
375 This variable contains a list of the following form:
377 '((command-string phrase-string*)*)
379 Each phrase-string is concatenated onto the command-string to form a
380 command filter. The (standard) output (and standard error) of the Un*x
381 man command is piped through each command filter in the order the
382 commands appear in the association list. The final output is placed in
383 the manpage buffer.")
386 (defsubst Man-match-substring
(&optional n string
)
387 "Return the substring matched by the last search.
388 Optional arg N means return the substring matched by the Nth paren
389 grouping. Optional second arg STRING means return a substring from
390 that string instead of from the current buffer."
391 (if (null n
) (setq n
0))
393 (substring string
(match-beginning n
) (match-end n
))
394 (buffer-substring (match-beginning n
) (match-end n
))))
396 (defsubst Man-make-page-mode-string
()
397 "Formats part of the mode line for Man mode."
398 (format "%s page %d of %d"
399 (or (nth 2 (nth (1- Man-current-page
) Man-page-list
))
402 (length Man-page-list
)))
404 (defsubst Man-build-man-command
()
405 "Builds the entire background manpage and cleaning command."
406 (let ((command (concat manual-program
" " Man-switches
" %s 2>/dev/null"))
407 (flist Man-filter-list
))
408 (while (and flist
(car flist
))
409 (let ((pcom (car (car flist
)))
410 (pargs (cdr (car flist
))))
412 (concat command
" | " pcom
" "
413 (mapconcat '(lambda (phrase)
414 (if (not (stringp phrase
))
415 (error "Malformed Man-filter-list"))
418 (setq flist
(cdr flist
))))
421 (defun Man-translate-references (ref)
422 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style.
423 Leave it as is if already in that style. Possibly downcase and
424 translate the section (see the Man-downcase-section-letters-flag
425 and the Man-section-translations-alist variables)."
428 (slist Man-section-translations-alist
))
430 ;; "chmod(2V)" case ?
431 ((string-match (concat "^" Man-reference-regexp
"$") ref
)
432 (setq name
(Man-match-substring 1 ref
)
433 section
(Man-match-substring 2 ref
)))
435 ((string-match (concat "^\\(" Man-section-regexp
436 "\\) +\\(" Man-name-regexp
"\\)$") ref
)
437 (setq name
(Man-match-substring 2 ref
)
438 section
(Man-match-substring 1 ref
))))
439 (if (string= name
"")
440 ref
; Return the reference as is
441 (if Man-downcase-section-letters-flag
442 (setq section
(downcase section
)))
444 (let ((s1 (car (car slist
)))
445 (s2 (cdr (car slist
))))
446 (setq slist
(cdr slist
))
447 (if Man-downcase-section-letters-flag
448 (setq s1
(downcase s1
)))
449 (if (not (string= s1 section
)) nil
450 (setq section
(if Man-downcase-section-letters-flag
454 (concat Man-specified-section-option section
" " name
))))
457 ;; ======================================================================
458 ;; default man entry: get word under point
460 (defsubst Man-default-man-entry
()
461 "Make a guess at a default manual entry.
462 This guess is based on the text surrounding the cursor, and the
463 default section number is selected from `Man-auto-section-alist'."
467 ;; Default man entry title is any word the cursor is on, or if
468 ;; cursor not on a word, then nearest preceding word. Cannot
469 ;; use the current-word function because it skips the dots.
470 (if (not (looking-at "[-a-zA-Z_.]"))
471 (skip-chars-backward "^a-zA-Z"))
472 (skip-chars-backward "-(a-zA-Z_0-9_.")
473 (if (looking-at "(") (forward-char 1))
477 (progn (skip-chars-forward "-a-zA-Z0-9_.") (point))))
479 ;; If looking at something like ioctl(2) or brc(1M), include the
480 ;; section number in the returned value. Remove text properties.
481 (let ((result (concat
484 (concat "[ \t]*([ \t]*\\("
485 Man-section-regexp
"\\)[ \t]*)"))
486 (format "(%s)" (Man-match-substring 1))))))
487 (set-text-properties 0 (length result
) nil result
)
491 ;; ======================================================================
492 ;; Top level command and background process sentinel
494 ;; For compatibility with older versions.
496 (defalias 'manual-entry
'man
)
499 (defun man (man-args)
500 "Get a Un*x manual page and put it in a buffer.
501 This command is the top-level command in the man package. It runs a Un*x
502 command to retrieve and clean a manpage in the background and places the
503 results in a Man mode (manpage browsing) buffer. See variable
504 `Man-notify-method' for what happens when the buffer is ready.
505 If a buffer already exists for this man page, it will display immediately."
507 (list (let* ((default-entry (Man-default-man-entry))
509 (format "Manual entry%s: "
510 (if (string= default-entry
"")
512 (format " (default %s)" default-entry
))))))
513 (if (string= input
"")
514 (if (string= default-entry
"")
515 (error "No man args given")
519 ;; Init the man package variables, if not already done.
522 ;; Possibly translate the "subject(section)" syntax into the
523 ;; "section subject" syntax and possibly downcase the section.
524 (setq man-args
(Man-translate-references man-args
))
526 (Man-getpage-in-background man-args
))
529 (defun Man-getpage-in-background (topic)
530 "Uses TOPIC to build and fire off the manpage and cleaning command."
531 (let* ((man-args topic
)
532 (bufname (concat "*Man " man-args
"*"))
533 (buffer (get-buffer bufname
)))
535 (Man-notify-when-ready buffer
)
537 (message "Invoking %s %s in the background" manual-program man-args
)
538 (setq buffer
(generate-new-buffer bufname
))
541 (setq Man-original-frame
(selected-frame))
542 (setq Man-arguments man-args
))
543 (let ((process-environment (copy-sequence process-environment
)))
544 ;; Prevent any attempt to use display terminal fanciness.
545 (setenv "TERM" "dumb")
546 (set-process-sentinel
547 (start-process manual-program buffer
"sh" "-c"
548 (format (Man-build-man-command) man-args
))
549 'Man-bgproc-sentinel
)))))
551 (defun Man-notify-when-ready (man-buffer)
552 "Notify the user when MAN-BUFFER is ready.
553 See the variable `Man-notify-method' for the different notification behaviors."
554 (let ((saved-frame (save-excursion
555 (set-buffer man-buffer
)
556 Man-original-frame
)))
558 ((eq Man-notify-method
'newframe
)
559 ;; Since we run asynchronously, perhaps while Emacs is waiting
560 ;; for input, we must not leave a different buffer current. We
561 ;; can't rely on the editor command loop to reselect the
562 ;; selected window's buffer.
564 (set-buffer man-buffer
)
565 (make-frame Man-frame-parameters
)))
566 ((eq Man-notify-method
'pushy
)
567 (switch-to-buffer man-buffer
))
568 ((eq Man-notify-method
'bully
)
570 (frame-live-p saved-frame
)
571 (select-frame saved-frame
))
572 (pop-to-buffer man-buffer
)
573 (delete-other-windows))
574 ((eq Man-notify-method
'aggressive
)
576 (frame-live-p saved-frame
)
577 (select-frame saved-frame
))
578 (pop-to-buffer man-buffer
))
579 ((eq Man-notify-method
'friendly
)
581 (frame-live-p saved-frame
)
582 (select-frame saved-frame
))
583 (display-buffer man-buffer
'not-this-window
))
584 ((eq Man-notify-method
'polite
)
586 (message "Manual buffer %s is ready" (buffer-name man-buffer
)))
587 ((eq Man-notify-method
'quiet
)
588 (message "Manual buffer %s is ready" (buffer-name man-buffer
)))
589 ((or (eq Man-notify-method
'meek
)
594 (defun Man-fontify-manpage ()
595 "Convert overstriking and underlining to the correct fonts.
596 Same for the ANSI bold and normal escape sequences."
598 (message "Please wait: making up the %s man page..." Man-arguments
)
599 (goto-char (point-min))
600 (while (search-forward "\e[1m" nil t
)
601 (delete-backward-char 4)
602 (put-text-property (point)
603 (progn (if (search-forward "\e[0m" nil
'move
)
604 (delete-backward-char 4))
606 'face Man-overstrike-face
))
607 (goto-char (point-min))
608 (while (search-forward "_\b" nil t
)
609 (backward-delete-char 2)
610 (put-text-property (point) (1+ (point)) 'face Man-underline-face
))
611 (goto-char (point-min))
612 (while (search-forward "\b_" nil t
)
613 (backward-delete-char 2)
614 (put-text-property (1- (point)) (point) 'face Man-underline-face
))
615 (goto-char (point-min))
616 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t
)
617 (replace-match "\\1")
618 (put-text-property (1- (point)) (point) 'face Man-overstrike-face
))
619 (goto-char (point-min))
620 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t
)
622 (put-text-property (1- (point)) (point) 'face
'bold
))
623 (goto-char (point-min))
624 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t
)
626 (put-text-property (1- (point)) (point) 'face
'bold
))
627 ;; \255 is some kind of dash in Latin-1.
628 (goto-char (point-min))
629 (while (search-forward "\255" nil t
) (replace-match "-"))
630 (message "%s man page made up" Man-arguments
))
632 (defun Man-cleanup-manpage ()
633 "Remove overstriking and underlining from the current buffer."
635 (message "Please wait: cleaning up the %s man page..."
637 (if (or (interactive-p) (not Man-sed-script
))
639 (goto-char (point-min))
640 (while (search-forward "_\b" nil t
) (backward-delete-char 2))
641 (goto-char (point-min))
642 (while (search-forward "\b_" nil t
) (backward-delete-char 2))
643 (goto-char (point-min))
644 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t
)
645 (replace-match "\\1"))
646 (goto-char (point-min))
647 (while (re-search-forward "\e\\[[0-9]+m" nil t
) (replace-match ""))
648 (goto-char (point-min))
649 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t
) (replace-match "o"))
651 (goto-char (point-min))
652 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t
) (replace-match "+"))
653 ;; \255 is some kind of dash in Latin-1.
654 (goto-char (point-min))
655 (while (search-forward "\255" nil t
) (replace-match "-"))
656 (message "%s man page cleaned up" Man-arguments
))
658 (defun Man-bgproc-sentinel (process msg
)
659 "Manpage background process sentinel."
660 (let ((Man-buffer (process-buffer process
))
664 (if (null (buffer-name Man-buffer
)) ;; deleted buffer
665 (set-process-buffer process nil
)
668 (set-buffer Man-buffer
)
669 (let ((case-fold-search nil
))
670 (goto-char (point-min))
671 (cond ((or (looking-at "No \\(manual \\)*entry for")
672 (looking-at "[^\n]*: nothing appropriate$"))
673 (setq err-mess
(buffer-substring (point)
675 (end-of-line) (point)))
677 ((not (and (eq (process-status process
) 'exit
)
678 (= (process-exit-status process
) 0)))
680 (concat (buffer-name Man-buffer
)
682 (let ((eos (1- (length msg
))))
683 (if (= (aref msg eos
) ?
\n)
684 (substring msg
0 eos
) msg
))))
685 (goto-char (point-max))
686 (insert (format "\nprocess %s" msg
))
689 (kill-buffer Man-buffer
)
690 (if Man-fontify-manpage-flag
691 (Man-fontify-manpage)
692 (Man-cleanup-manpage))
693 (run-hooks 'Man-cooked-hook
)
695 (set-buffer-modified-p nil
)
697 ;; Restore case-fold-search before calling
698 ;; Man-notify-when-ready because it may switch buffers.
700 (if (not delete-buff
)
701 (Man-notify-when-ready Man-buffer
))
708 ;; ======================================================================
709 ;; set up manual mode in buffer and build alists
712 "A mode for browsing Un*x manual pages.
714 The following man commands are available in the buffer. Try
715 \"\\[describe-key] <key> RET\" for more information:
717 \\[man] Prompt to retrieve a new manpage.
718 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
719 \\[Man-next-manpage] Jump to next manpage in circular list.
720 \\[Man-previous-manpage] Jump to previous manpage in circular list.
721 \\[Man-next-section] Jump to next manpage section.
722 \\[Man-previous-section] Jump to previous manpage section.
723 \\[Man-goto-section] Go to a manpage section.
724 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
725 \\[Man-quit] Deletes the manpage window, bury its buffer.
726 \\[Man-kill] Deletes the manpage window, kill its buffer.
727 \\[describe-mode] Prints this help text.
729 The following variables may be of some use. Try
730 \"\\[describe-variable] <variable-name> RET\" for more information:
732 Man-notify-method What happens when manpage formatting is done.
733 Man-downcase-section-letters-flag Force section letters to lower case.
734 Man-circular-pages-flag Treat multiple manpage list as circular.
735 Man-auto-section-alist List of major modes and their section numbers.
736 Man-section-translations-alist List of section numbers and their Un*x equiv.
737 Man-filter-list Background manpage filter command.
738 Man-mode-line-format Mode line format for Man mode buffers.
739 Man-mode-map Keymap bindings for Man mode buffers.
740 Man-mode-hook Normal hook run on entry to Man mode.
741 Man-section-regexp Regexp describing manpage section letters.
742 Man-heading-regexp Regexp describing section headers.
743 Man-see-also-regexp Regexp for SEE ALSO section (or your equiv).
744 Man-first-heading-regexp Regexp for first heading on a manpage.
745 Man-reference-regexp Regexp matching a references in SEE ALSO.
746 Man-switches Background `man' command switches.
748 The following key bindings are currently in effect in the buffer:
751 (setq major-mode
'Man-mode
753 buffer-auto-save-file-name nil
754 mode-line-format Man-mode-line-format
757 (buffer-disable-undo (current-buffer))
759 (use-local-map Man-mode-map
)
760 (Man-build-page-list)
761 (Man-strip-page-headers)
764 (run-hooks 'Man-mode-hook
))
766 (defsubst Man-build-section-alist
()
767 "Build the association list of manpage sections."
768 (setq Man-sections-alist nil
)
769 (goto-char (point-min))
770 (let ((case-fold-search nil
))
771 (while (re-search-forward Man-heading-regexp
(point-max) t
)
772 (aput 'Man-sections-alist
(Man-match-substring 1))
775 (defsubst Man-build-references-alist
()
776 "Build the association list of references (in the SEE ALSO section)."
777 (setq Man-refpages-alist nil
)
779 (if (Man-find-section Man-see-also-regexp
)
780 (let ((start (progn (forward-line 1) (point)))
787 (narrow-to-region start end
)
788 (goto-char (point-min))
789 (back-to-indentation)
790 (while (and (not (eobp)) (/= (point) runningpoint
))
791 (setq runningpoint
(point))
792 (if (re-search-forward Man-reference-regexp end t
)
793 (let* ((word (Man-match-substring 0))
794 (len (1- (length word
))))
796 (setq word
(concat hyphenated word
)
798 (if (= (aref word len
) ?-
)
799 (setq hyphenated
(substring word
0 len
))
800 (aput 'Man-refpages-alist word
))))
801 (skip-chars-forward " \t\n,")))))))
803 (defun Man-build-page-list ()
804 "Build the list of separate manpages in the buffer."
805 (setq Man-page-list nil
)
806 (let ((page-start (point-min))
807 (page-end (point-max))
809 (goto-char page-start
)
810 ;; (switch-to-buffer (current-buffer))(debug)
813 (if (looking-at Man-page-header-regexp
)
814 (Man-match-substring 1)
816 ;; Go past both the current and the next Man-first-heading-regexp
817 (if (re-search-forward Man-first-heading-regexp nil
'move
2)
818 (let ((p (progn (beginning-of-line) (point))))
819 ;; We assume that the page header is delimited by blank
820 ;; lines and that it contains at most one blank line. So
821 ;; if we back by three blank lines we will be sure to be
822 ;; before the page header but not before the possible
823 ;; previous page header.
824 (search-backward "\n\n" nil t
3)
825 (if (re-search-forward Man-page-header-regexp p
'move
)
826 (beginning-of-line))))
827 (setq page-end
(point))
828 (setq Man-page-list
(append Man-page-list
829 (list (list (copy-marker page-start
)
830 (copy-marker page-end
)
832 (setq page-start page-end
)
835 (defun Man-strip-page-headers ()
836 "Strip all the page headers but the first from the manpage."
837 (let ((buffer-read-only nil
)
838 (case-fold-search nil
)
839 (page-list Man-page-list
)
843 (setq page
(car page-list
))
845 (goto-char (car page
))
846 (re-search-forward Man-first-heading-regexp nil t
)
847 (setq header
(buffer-substring (car page
) (match-beginning 0)))
848 ;; Since the awk script collapses all successive blank
849 ;; lines into one, and since we don't want to get rid of
850 ;; the fast awk script, one must choose between adding
851 ;; spare blank lines between pages when there were none and
852 ;; deleting blank lines at page boundaries when there were
853 ;; some. We choose the first, so we comment the following
855 ;; (setq header (concat "\n" header)))
856 (while (search-forward header
(nth 1 page
) t
)
858 (setq page-list
(cdr page-list
)))))
860 (defun Man-unindent ()
861 "Delete the leading spaces that indent the manpage."
862 (let ((buffer-read-only nil
)
863 (case-fold-search nil
)
864 (page-list Man-page-list
))
866 (let ((page (car page-list
))
869 (narrow-to-region (car page
) (car (cdr page
)))
870 (if Man-uses-untabify-flag
871 (untabify (point-min) (point-max)))
873 (goto-char (point-min))
874 (if (not (re-search-forward Man-first-heading-regexp nil t
))
875 (throw 'unindent nil
))
877 (setq indent
(buffer-substring (point)
879 (skip-chars-forward " ")
881 (setq nindent
(length indent
))
883 (throw 'unindent nil
))
884 (setq indent
(concat indent
"\\|$"))
885 (goto-char (point-min))
887 (if (looking-at indent
)
889 (throw 'unindent nil
)))
890 (goto-char (point-min)))
893 (delete-char nindent
))
895 (setq page-list
(cdr page-list
))
899 ;; ======================================================================
902 (defun Man-next-section (n)
903 "Move point to Nth next section (default 1)."
905 (let ((case-fold-search nil
))
906 (if (looking-at Man-heading-regexp
)
908 (if (re-search-forward Man-heading-regexp
(point-max) t n
)
910 (goto-char (point-max)))))
912 (defun Man-previous-section (n)
913 "Move point to Nth previous section (default 1)."
915 (let ((case-fold-search nil
))
916 (if (looking-at Man-heading-regexp
)
918 (if (re-search-backward Man-heading-regexp
(point-min) t n
)
920 (goto-char (point-min)))))
922 (defun Man-find-section (section)
923 "Move point to SECTION if it exists, otherwise don't move point.
924 Returns t if section is found, nil otherwise."
925 (let ((curpos (point))
926 (case-fold-search nil
))
927 (goto-char (point-min))
928 (if (re-search-forward (concat "^" section
) (point-max) t
)
929 (progn (beginning-of-line) t
)
934 (defun Man-goto-section ()
935 "Query for section to move point to."
937 (aput 'Man-sections-alist
938 (let* ((default (aheadsym Man-sections-alist
))
939 (completion-ignore-case t
)
941 (prompt (concat "Go to section: (default " default
") ")))
942 (setq chosen
(completing-read prompt Man-sections-alist
))
947 (Man-find-section (aheadsym Man-sections-alist
)))
949 (defun Man-goto-see-also-section ()
950 "Move point the the \"SEE ALSO\" section.
951 Actually the section moved to is described by `Man-see-also-regexp'."
953 (if (not (Man-find-section Man-see-also-regexp
))
954 (error (concat "No " Man-see-also-regexp
955 " section found in the current manpage"))))
957 (defun Man-follow-manual-reference (reference)
958 "Get one of the manpages referred to in the \"SEE ALSO\" section.
959 Specify which reference to use; default is based on word at point."
961 (if (not Man-refpages-alist
)
962 (error "There are no references in the current man page")
963 (list (let* ((default (or
964 (car (all-completions
966 (skip-syntax-backward "w()")
967 (skip-chars-forward " \t")
968 (let ((word (current-word)))
969 ;; strip a trailing '-':
970 (if (string-match "-$" word
)
975 (aheadsym Man-refpages-alist
)))
977 (prompt (concat "Refer to: (default " default
") ")))
978 (setq chosen
(completing-read prompt Man-refpages-alist nil t
))
983 (if (not Man-refpages-alist
)
984 (error "Can't find any references in the current manpage")
985 (aput 'Man-refpages-alist reference
)
986 (Man-getpage-in-background
987 (Man-translate-references (aheadsym Man-refpages-alist
)))))
990 "Kill the buffer containing the manpage."
992 (let ((buff (current-buffer)))
993 (delete-windows-on buff
)
995 (if (and window-system
996 (or (eq Man-notify-method
'newframe
)
998 (eq Man-notify-method
'bully
))))
1002 "Bury the buffer containing the manpage."
1004 (let ((buff (current-buffer)))
1005 (delete-windows-on buff
)
1007 (if (and window-system
1008 (or (eq Man-notify-method
'newframe
)
1010 (eq Man-notify-method
'bully
))))
1013 (defun Man-goto-page (page)
1014 "Go to the manual page on page PAGE."
1016 (if (not Man-page-list
)
1017 (let ((args Man-arguments
))
1018 (kill-buffer (current-buffer))
1019 (error "Can't find the %s manpage" args
))
1020 (if (= (length Man-page-list
) 1)
1021 (error "You're looking at the only manpage in the buffer")
1022 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1023 (length Man-page-list
)))))))
1024 (if (not Man-page-list
)
1025 (let ((args Man-arguments
))
1026 (kill-buffer (current-buffer))
1027 (error "Can't find the %s manpage" args
)))
1029 (> page
(length Man-page-list
)))
1030 (error "No manpage %d found" page
))
1031 (let* ((page-range (nth (1- page
) Man-page-list
))
1032 (page-start (car page-range
))
1033 (page-end (car (cdr page-range
))))
1034 (setq Man-current-page page
1035 Man-page-mode-string
(Man-make-page-mode-string))
1037 (goto-char page-start
)
1038 (narrow-to-region page-start page-end
)
1039 (Man-build-section-alist)
1040 (Man-build-references-alist)
1041 (goto-char (point-min))))
1044 (defun Man-next-manpage ()
1045 "Find the next manpage entry in the buffer."
1047 (if (= (length Man-page-list
) 1)
1048 (error "This is the only manpage in the buffer"))
1049 (if (< Man-current-page
(length Man-page-list
))
1050 (Man-goto-page (1+ Man-current-page
))
1051 (if Man-circular-pages-flag
1053 (error "You're looking at the last manpage in the buffer"))))
1055 (defun Man-previous-manpage ()
1056 "Find the previous manpage entry in the buffer."
1058 (if (= (length Man-page-list
) 1)
1059 (error "This is the only manpage in the buffer"))
1060 (if (> Man-current-page
1)
1061 (Man-goto-page (1- Man-current-page
))
1062 (if Man-circular-pages-flag
1063 (Man-goto-page (length Man-page-list
))
1064 (error "You're looking at the first manpage in the buffer"))))
1068 ;;; man.el ends here