(DEFVAR_DISPLAY): New macro.
[emacs.git] / lisp / man.el
blob719b12ee9d1c5beca0d953d0f1c90ba1cc8e9cca
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/11/09 12:38:31 $
7 ;; Version: $Revision: 1.59 $
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 a user option to mean that all the manpages should go in
87 ;; the same buffer, where they can be browsed with M-n and M-p.
88 ;; - Allow completion on the manpage name when calling man. This
89 ;; requires a reliable list of places where manpages can be found. The
90 ;; drawback would be that if the list is not complete, the user might
91 ;; be led to believe that the manpages in the missing directories do
92 ;; not exist.
95 ;;; Code:
97 (require 'assoc)
99 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
100 ;; empty defvars (keep the compiler quiet)
102 (defvar Man-notify)
103 (defvar Man-current-page)
104 (defvar Man-page-list)
105 (defvar Man-filter-list)
106 (defvar Man-original-frame)
107 (defvar Man-arguments)
108 (defvar Man-sections-alist)
109 (defvar Man-refpages-alist)
110 (defvar Man-uses-untabify-flag)
111 (defvar Man-page-mode-string)
112 (defvar Man-sed-script)
114 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
115 ;; user variables
117 (defvar Man-fontify-manpage-flag t
118 "*Make up the manpage with fonts.")
120 (defvar Man-overstrike-face 'bold
121 "*Face to use when fontifying overstrike.")
123 (defvar Man-underline-face 'underline
124 "*Face to use when fontifying underlinining.")
126 ;; Use the value of the obsolete user option Man-notify, if set.
127 (defvar Man-notify-method (if (boundp 'Man-notify) Man-notify 'friendly)
128 "*Selects the behavior when manpage is ready.
129 This variable may have one of the following values, where (sf) means
130 that the frames are switched, so the manpage is displayed in the frame
131 where the man command was called from:
133 newframe -- put the manpage in its own frame (see `Man-frame-parameters')
134 pushy -- make the manpage the current buffer in the current window
135 bully -- make the manpage the current buffer and only window (sf)
136 aggressive -- make the manpage the current buffer in the other window (sf)
137 friendly -- display manpage in the other window but don't make current (sf)
138 polite -- don't display manpage, but prints message and beep when ready
139 quiet -- like `polite', but don't beep
140 meek -- make no indication that the manpage is ready
142 Any other value of `Man-notify-method' is equivalent to `meek'.")
144 (defvar Man-frame-parameters nil
145 "*Frame parameter list for creating a new frame for a manual page.")
147 (defvar Man-downcase-section-letters-flag t
148 "*Letters in sections are converted to lower case.
149 Some Un*x man commands can't handle uppercase letters in sections, for
150 example \"man 2V chmod\", but they are often displayed in the manpage
151 with the upper case letter. When this variable is t, the section
152 letter (e.g., \"2V\") is converted to lowercase (e.g., \"2v\") before
153 being sent to the man background process.")
155 (defvar Man-circular-pages-flag t
156 "*If t, the manpage list is treated as circular for traversal.")
158 (defvar Man-section-translations-alist
159 (list
160 '("3C++" . "3")
161 ;; Some systems have a real 3x man section, so let's comment this.
162 ;; '("3X" . "3") ; Xlib man pages
163 '("3X11" . "3")
164 '("1-UCB" . ""))
165 "*Association list of bogus sections to real section numbers.
166 Some manpages (e.g. the Sun C++ 2.1 manpages) have section numbers in
167 their references which Un*x `man' does not recognize. This
168 association list is used to translate those sections, when found, to
169 the associated section number.")
171 (defvar manual-program "man"
172 "The name of the program that produces man pages.")
174 (defvar Man-untabify-command "pr"
175 "Command used for untabifying.")
177 (defvar Man-untabify-command-args (list "-t" "-e")
178 "List of arguments to be passed to Man-untabify-command (which see).")
180 (defvar Man-sed-command "sed"
181 "Command used for processing sed scripts.")
183 (defvar Man-awk-command "awk"
184 "Command used for processing awk scripts.")
186 (defvar Man-mode-line-format
187 '("" mode-line-modified
188 mode-line-buffer-identification " "
189 global-mode-string
190 " " Man-page-mode-string
191 " %[(" mode-name mode-line-process minor-mode-alist ")%]----"
192 (-3 . "%p") "-%-")
193 "Mode line format for manual mode buffer.")
195 (defvar Man-mode-map nil
196 "Keymap for Man mode.")
198 (defvar Man-mode-hook nil
199 "Hook run when Man mode is enabled.")
201 (defvar Man-cooked-hook nil
202 "Hook run after removing backspaces but before Man-mode processing.")
204 (defvar Man-name-regexp "[-a-zA-Z0-9_][-a-zA-Z0-9_.]*"
205 "Regular expression describing the name of a manpage (without section).")
207 (defvar Man-section-regexp "[0-9][a-zA-Z+]*\\|[LNln]"
208 "Regular expression describing a manpage section within parentheses.")
210 (defvar Man-page-header-regexp
211 (concat "^[ \t]*\\(" Man-name-regexp
212 "(\\(" Man-section-regexp "\\))\\).*\\1")
213 "Regular expression describing the heading of a page.")
215 (defvar Man-heading-regexp "^\\([A-Z][A-Z ]+\\)$"
216 "Regular expression describing a manpage heading entry.")
218 (defvar Man-see-also-regexp "SEE ALSO"
219 "Regular expression for SEE ALSO heading (or your equivalent).
220 This regexp should not start with a `^' character.")
222 (defvar Man-first-heading-regexp "^[ \t]*NAME$\\|^[ \t]*No manual entry fo.*$"
223 "Regular expression describing first heading on a manpage.
224 This regular expression should start with a `^' character.")
226 (defvar Man-reference-regexp
227 (concat "\\(" Man-name-regexp "\\)(\\(" Man-section-regexp "\\))")
228 "Regular expression describing a reference in the SEE ALSO section.")
230 (defvar Man-switches ""
231 "Switches passed to the man command, as a single string.")
233 (defvar Man-specified-section-option
234 (if (string-match "-solaris[0-9.]*$" system-configuration)
235 "-s"
237 "Option that indicates a specified a manual section name.")
239 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
240 ;; end user variables
242 ;; other variables and keymap initializations
243 (make-variable-buffer-local 'Man-sections-alist)
244 (make-variable-buffer-local 'Man-refpages-alist)
245 (make-variable-buffer-local 'Man-page-list)
246 (make-variable-buffer-local 'Man-current-page)
247 (make-variable-buffer-local 'Man-page-mode-string)
248 (make-variable-buffer-local 'Man-original-frame)
249 (make-variable-buffer-local 'Man-arguments)
251 (setq-default Man-sections-alist nil)
252 (setq-default Man-refpages-alist nil)
253 (setq-default Man-page-list nil)
254 (setq-default Man-current-page 0)
255 (setq-default Man-page-mode-string "1 of 1")
257 (defconst Man-sysv-sed-script "\
258 /\b/ { s/_\b//g
259 s/\b_//g
260 s/o\b+/o/g
261 s/+\bo/o/g
262 :ovstrk
263 s/\\(.\\)\b\\1/\\1/g
264 t ovstrk
266 /\e\\[[0-9][0-9]*m/ s///g"
267 "Script for sysV-like sed to nuke backspaces and ANSI codes from manpages.")
269 (defconst Man-berkeley-sed-script "\
270 /\b/ { s/_\b//g\\
271 s/\b_//g\\
272 s/o\b+/o/g\\
273 s/+\bo/o/g\\
274 :ovstrk\\
275 s/\\(.\\)\b\\1/\\1/g\\
276 t ovstrk\\
278 /\e\\[[0-9][0-9]*m/ s///g"
279 "Script for berkeley-like sed to nuke backspaces and ANSI codes from manpages.")
281 (if Man-mode-map
283 (setq Man-mode-map (make-keymap))
284 (suppress-keymap Man-mode-map)
285 (define-key Man-mode-map " " 'scroll-up)
286 (define-key Man-mode-map "\177" 'scroll-down)
287 (define-key Man-mode-map "n" 'Man-next-section)
288 (define-key Man-mode-map "p" 'Man-previous-section)
289 (define-key Man-mode-map "\en" 'Man-next-manpage)
290 (define-key Man-mode-map "\ep" 'Man-previous-manpage)
291 (define-key Man-mode-map ">" 'end-of-buffer)
292 (define-key Man-mode-map "<" 'beginning-of-buffer)
293 (define-key Man-mode-map "." 'beginning-of-buffer)
294 (define-key Man-mode-map "r" 'Man-follow-manual-reference)
295 (define-key Man-mode-map "t" 'toggle-truncate-lines)
296 (define-key Man-mode-map "g" 'Man-goto-section)
297 (define-key Man-mode-map "s" 'Man-goto-see-also-section)
298 (define-key Man-mode-map "k" 'Man-kill)
299 (define-key Man-mode-map "q" 'Man-quit)
300 (define-key Man-mode-map "m" 'man)
301 (define-key Man-mode-map "?" 'describe-mode)
305 ;; ======================================================================
306 ;; utilities
308 (defsubst Man-init-defvars ()
309 "Used for initialising variables based on the value of window-system.
310 This is necessary if one wants to dump man.el with emacs."
312 ;; The following is necessary until fonts are implemented on
313 ;; terminals.
314 (setq Man-fontify-manpage-flag (and Man-fontify-manpage-flag
315 window-system))
317 (defconst Man-uses-untabify-flag t
318 ;; don't use pr: it is buggy
319 ;; (or (not (file-readable-p "/etc/passwd"))
320 ;; (/= 0 (apply 'call-process
321 ;; Man-untabify-command nil nil nil
322 ;; (append Man-untabify-command-args
323 ;; (list "/etc/passwd")))))
324 "When non-nil use `untabify' instead of Man-untabify-command.")
326 (defconst Man-sed-script
327 (cond
328 (Man-fontify-manpage-flag
329 nil)
330 ((= 0 (call-process Man-sed-command nil nil nil Man-sysv-sed-script))
331 Man-sysv-sed-script)
332 ((= 0 (call-process Man-sed-command nil nil nil Man-berkeley-sed-script))
333 Man-berkeley-sed-script)
335 nil))
336 "Script for sed to nuke backspaces and ANSI codes from manpages.")
338 (defvar Man-filter-list
339 (list
340 (cons
341 Man-sed-command
342 (list
343 (if Man-sed-script
344 (concat "-e '" Man-sed-script "'")
346 "-e '/^[\001-\032][\001-\032]*$/d'"
347 "-e '/\e[789]/s///g'"
348 "-e '/[Nn]o such file or directory/d'"
349 "-e '/Reformatting page. Wait/d'"
350 "-e '/Reformatting entry. Wait/d'"
351 "-e '/^[ \t]*Hewlett-Packard[ \t]Company[ \t]*-[ \t][0-9]*[ \t]-/d'"
352 "-e '/^[ \t]*Hewlett-Packard[ \t]*-[ \t][0-9]*[ \t]-.*$/d'"
353 "-e '/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d'"
354 "-e '/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d'"
355 "-e '/^Printed[ \t][0-9].*[0-9]$/d'"
356 "-e '/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d'"
357 "-e '/^[A-za-z].*Last[ \t]change:/d'"
358 "-e '/^Sun[ \t]Release[ \t][0-9].*[0-9]$/d'"
359 "-e '/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d'"
361 (cons
362 Man-awk-command
363 (list
364 "'\n"
365 "BEGIN { blankline=0; anonblank=0; }\n"
366 "/^$/ { if (anonblank==0) next; }\n"
367 "{ anonblank=1; }\n"
368 "/^$/ { blankline++; next; }\n"
369 "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }\n"
372 (if (not Man-uses-untabify-flag)
373 (cons
374 Man-untabify-command
375 Man-untabify-command-args)
377 "*Manpage cleaning filter command phrases.
378 This variable contains a list of the following form:
380 '((command-string phrase-string*)*)
382 Each phrase-string is concatenated onto the command-string to form a
383 command filter. The (standard) output (and standard error) of the Un*x
384 man command is piped through each command filter in the order the
385 commands appear in the association list. The final output is placed in
386 the manpage buffer.")
389 (defsubst Man-match-substring (&optional n string)
390 "Return the substring matched by the last search.
391 Optional arg N means return the substring matched by the Nth paren
392 grouping. Optional second arg STRING means return a substring from
393 that string instead of from the current buffer."
394 (if (null n) (setq n 0))
395 (if string
396 (substring string (match-beginning n) (match-end n))
397 (buffer-substring (match-beginning n) (match-end n))))
399 (defsubst Man-make-page-mode-string ()
400 "Formats part of the mode line for Man mode."
401 (format "%s page %d of %d"
402 (or (nth 2 (nth (1- Man-current-page) Man-page-list))
404 Man-current-page
405 (length Man-page-list)))
407 (defsubst Man-build-man-command ()
408 "Builds the entire background manpage and cleaning command."
409 (let ((command (concat manual-program " " Man-switches " %s 2>/dev/null"))
410 (flist Man-filter-list))
411 (while (and flist (car flist))
412 (let ((pcom (car (car flist)))
413 (pargs (cdr (car flist))))
414 (setq command
415 (concat command " | " pcom " "
416 (mapconcat '(lambda (phrase)
417 (if (not (stringp phrase))
418 (error "Malformed Man-filter-list"))
419 phrase)
420 pargs " ")))
421 (setq flist (cdr flist))))
422 command))
424 (defun Man-translate-references (ref)
425 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style.
426 Leave it as is if already in that style. Possibly downcase and
427 translate the section (see the Man-downcase-section-letters-flag
428 and the Man-section-translations-alist variables)."
429 (let ((name "")
430 (section "")
431 (slist Man-section-translations-alist))
432 (cond
433 ;; "chmod(2V)" case ?
434 ((string-match (concat Man-reference-regexp "$") ref)
435 (setq name (Man-match-substring 1 ref)
436 section (Man-match-substring 2 ref)))
437 ;; "2v chmod" case ?
438 ((string-match (concat "\\(" Man-section-regexp
439 "\\) +\\(" Man-name-regexp "\\)$") ref)
440 (setq name (Man-match-substring 2 ref)
441 section (Man-match-substring 1 ref))))
442 (if (string= name "")
443 ref ; Return the reference as is
444 (if Man-downcase-section-letters-flag
445 (setq section (downcase section)))
446 (while slist
447 (let ((s1 (car (car slist)))
448 (s2 (cdr (car slist))))
449 (setq slist (cdr slist))
450 (if Man-downcase-section-letters-flag
451 (setq s1 (downcase s1)))
452 (if (not (string= s1 section)) nil
453 (setq section (if Man-downcase-section-letters-flag
454 (downcase s2)
456 slist nil))))
457 (concat Man-specified-section-option section " " name))))
460 ;; ======================================================================
461 ;; default man entry: get word under point
463 (defsubst Man-default-man-entry ()
464 "Make a guess at a default manual entry.
465 This guess is based on the text surrounding the cursor, and the
466 default section number is selected from `Man-auto-section-alist'."
467 (let (default-title)
468 (save-excursion
470 ;; Default man entry title is any word the cursor is on, or if
471 ;; cursor not on a word, then nearest preceding word. Cannot
472 ;; use the current-word function because it skips the dots.
473 (if (not (looking-at "[-a-zA-Z_.]"))
474 (skip-chars-backward "^a-zA-Z"))
475 (skip-chars-backward "-(a-zA-Z_0-9_.")
476 (if (looking-at "(") (forward-char 1))
477 (setq default-title
478 (buffer-substring
479 (point)
480 (progn (skip-chars-forward "-a-zA-Z0-9_.") (point))))
482 ;; If looking at something like ioctl(2) or brc(1M), include the
483 ;; section number in the returned value.
484 (concat
485 default-title
486 (if (looking-at
487 (concat "[ \t]*([ \t]*\\(" Man-section-regexp "\\)[ \t]*)"))
488 (format "(%s)" (Man-match-substring 1)))
489 ))))
492 ;; ======================================================================
493 ;; Top level command and background process sentinel
495 ;; For compatibility with older versions.
496 ;;;###autoload
497 (defalias 'manual-entry 'man)
499 ;;;###autoload
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."
507 (interactive
508 (list (let* ((default-entry (Man-default-man-entry))
509 (input (read-string
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")
517 default-entry)
518 input))))
520 ;; Init the man package variables, if not already done.
521 (Man-init-defvars)
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)))
535 (if buffer
536 (Man-notify-when-ready buffer)
537 (require 'env)
538 (message "Invoking %s %s in the background" manual-program man-args)
539 (setq buffer (generate-new-buffer bufname))
540 (save-excursion
541 (set-buffer buffer)
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)))
558 (cond
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.
564 (save-excursion
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)
570 (and window-system
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)
576 (and window-system
577 (frame-live-p saved-frame)
578 (select-frame saved-frame))
579 (pop-to-buffer man-buffer))
580 ((eq Man-notify-method 'friendly)
581 (and window-system
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)
586 (beep)
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)
592 (message ""))
595 (defun Man-fontify-manpage ()
596 "Convert overstriking and underlining to the correct fonts.
597 Same for the ANSI bold and normal escape sequences."
598 (interactive)
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))
606 (point))
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)
622 (replace-match "o")
623 (put-text-property (1- (point)) (point) 'face 'bold))
624 (goto-char (point-min))
625 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t)
626 (replace-match "+")
627 (put-text-property (1- (point)) (point) 'face 'bold))
628 (message "%s man page made up" Man-arguments))
630 (defun Man-cleanup-manpage ()
631 "Remove overstriking and underlining from the current buffer."
632 (interactive)
633 (message "Please wait: cleaning up the %s man page..."
634 Man-arguments)
635 (if (or (interactive-p) (not Man-sed-script))
636 (progn
637 (goto-char (point-min))
638 (while (search-forward "_\b" nil t) (backward-delete-char 2))
639 (goto-char (point-min))
640 (while (search-forward "\b_" nil t) (backward-delete-char 2))
641 (goto-char (point-min))
642 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
643 (replace-match "\\1"))
644 (goto-char (point-min))
645 (while (re-search-forward "\e\\[[0-9]+m" nil t) (replace-match ""))
646 (goto-char (point-min))
647 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t) (replace-match "o"))
649 (goto-char (point-min))
650 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t) (replace-match "+"))
651 (message "%s man page cleaned up" Man-arguments))
653 (defun Man-bgproc-sentinel (process msg)
654 "Manpage background process sentinel."
655 (let ((Man-buffer (process-buffer process))
656 (delete-buff nil)
657 (err-mess nil))
659 (if (null (buffer-name Man-buffer)) ;; deleted buffer
660 (set-process-buffer process nil)
662 (save-excursion
663 (set-buffer Man-buffer)
664 (let ((case-fold-search nil))
665 (goto-char (point-min))
666 (cond ((or (looking-at "No \\(manual \\)*entry for")
667 (looking-at "[^\n]*: nothing appropriate$"))
668 (setq err-mess (buffer-substring (point)
669 (progn
670 (end-of-line) (point)))
671 delete-buff t))
672 ((not (and (eq (process-status process) 'exit)
673 (= (process-exit-status process) 0)))
674 (setq err-mess
675 (concat (buffer-name Man-buffer)
676 ": process "
677 (let ((eos (1- (length msg))))
678 (if (= (aref msg eos) ?\n)
679 (substring msg 0 eos) msg))))
680 (goto-char (point-max))
681 (insert (format "\nprocess %s" msg))
683 (if delete-buff
684 (kill-buffer Man-buffer)
685 (if Man-fontify-manpage-flag
686 (Man-fontify-manpage)
687 (Man-cleanup-manpage))
688 (run-hooks 'Man-cooked-hook)
689 (Man-mode)
690 (set-buffer-modified-p nil)
692 ;; Restore case-fold-search before calling
693 ;; Man-notify-when-ready because it may switch buffers.
695 (if (not delete-buff)
696 (Man-notify-when-ready Man-buffer))
698 (if err-mess
699 (error err-mess))
700 ))))
703 ;; ======================================================================
704 ;; set up manual mode in buffer and build alists
706 (defun Man-mode ()
707 "A mode for browsing Un*x manual pages.
709 The following man commands are available in the buffer. Try
710 \"\\[describe-key] <key> RET\" for more information:
712 \\[man] Prompt to retrieve a new manpage.
713 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
714 \\[Man-next-manpage] Jump to next manpage in circular list.
715 \\[Man-previous-manpage] Jump to previous manpage in circular list.
716 \\[Man-next-section] Jump to next manpage section.
717 \\[Man-previous-section] Jump to previous manpage section.
718 \\[Man-goto-section] Go to a manpage section.
719 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
720 \\[Man-quit] Deletes the manpage window, bury its buffer.
721 \\[Man-kill] Deletes the manpage window, kill its buffer.
722 \\[describe-mode] Prints this help text.
724 The following variables may be of some use. Try
725 \"\\[describe-variable] <variable-name> RET\" for more information:
727 Man-notify-method What happens when manpage formatting is done.
728 Man-downcase-section-letters-flag Force section letters to lower case.
729 Man-circular-pages-flag Treat multiple manpage list as circular.
730 Man-auto-section-alist List of major modes and their section numbers.
731 Man-section-translations-alist List of section numbers and their Un*x equiv.
732 Man-filter-list Background manpage filter command.
733 Man-mode-line-format Mode line format for Man mode buffers.
734 Man-mode-map Keymap bindings for Man mode buffers.
735 Man-mode-hook Normal hook run on entry to Man mode.
736 Man-section-regexp Regexp describing manpage section letters.
737 Man-heading-regexp Regexp describing section headers.
738 Man-see-also-regexp Regexp for SEE ALSO section (or your equiv).
739 Man-first-heading-regexp Regexp for first heading on a manpage.
740 Man-reference-regexp Regexp matching a references in SEE ALSO.
741 Man-switches Background `man' command switches.
743 The following key bindings are currently in effect in the buffer:
744 \\{Man-mode-map}"
745 (interactive)
746 (setq major-mode 'Man-mode
747 mode-name "Man"
748 buffer-auto-save-file-name nil
749 mode-line-format Man-mode-line-format
750 truncate-lines t
751 buffer-read-only t)
752 (buffer-disable-undo (current-buffer))
753 (auto-fill-mode -1)
754 (use-local-map Man-mode-map)
755 (Man-build-page-list)
756 (Man-strip-page-headers)
757 (Man-unindent)
758 (Man-goto-page 1)
759 (run-hooks 'Man-mode-hook))
761 (defsubst Man-build-section-alist ()
762 "Build the association list of manpage sections."
763 (setq Man-sections-alist nil)
764 (goto-char (point-min))
765 (let ((case-fold-search nil))
766 (while (re-search-forward Man-heading-regexp (point-max) t)
767 (aput 'Man-sections-alist (Man-match-substring 1))
768 (forward-line 1))))
770 (defsubst Man-build-references-alist ()
771 "Build the association list of references (in the SEE ALSO section)."
772 (setq Man-refpages-alist nil)
773 (save-excursion
774 (if (Man-find-section Man-see-also-regexp)
775 (let ((start (progn (forward-line 1) (point)))
776 (end (progn
777 (Man-next-section 1)
778 (point)))
779 hyphenated
780 (runningpoint -1))
781 (save-restriction
782 (narrow-to-region start end)
783 (goto-char (point-min))
784 (back-to-indentation)
785 (while (and (not (eobp)) (/= (point) runningpoint))
786 (setq runningpoint (point))
787 (if (re-search-forward Man-reference-regexp end t)
788 (let* ((word (Man-match-substring 0))
789 (len (1- (length word))))
790 (if hyphenated
791 (setq word (concat hyphenated word)
792 hyphenated nil))
793 (if (= (aref word len) ?-)
794 (setq hyphenated (substring word 0 len))
795 (aput 'Man-refpages-alist word))))
796 (skip-chars-forward " \t\n,")))))))
798 (defun Man-build-page-list ()
799 "Build the list of separate manpages in the buffer."
800 (setq Man-page-list nil)
801 (let ((page-start (point-min))
802 (page-end (point-max))
803 (header ""))
804 (goto-char page-start)
805 ;; (switch-to-buffer (current-buffer))(debug)
806 (while (not (eobp))
807 (setq header
808 (if (looking-at Man-page-header-regexp)
809 (Man-match-substring 1)
810 nil))
811 ;; Go past both the current and the next Man-first-heading-regexp
812 (if (re-search-forward Man-first-heading-regexp nil 'move 2)
813 (let ((p (progn (beginning-of-line) (point))))
814 ;; We assume that the page header is delimited by blank
815 ;; lines and that it contains at most one blank line. So
816 ;; if we back by three blank lines we will be sure to be
817 ;; before the page header but not before the possible
818 ;; previous page header.
819 (search-backward "\n\n" nil t 3)
820 (if (re-search-forward Man-page-header-regexp p 'move)
821 (beginning-of-line))))
822 (setq page-end (point))
823 (setq Man-page-list (append Man-page-list
824 (list (list (copy-marker page-start)
825 (copy-marker page-end)
826 header))))
827 (setq page-start page-end)
830 (defun Man-strip-page-headers ()
831 "Strip all the page headers but the first from the manpage."
832 (let ((buffer-read-only nil)
833 (case-fold-search nil)
834 (page-list Man-page-list)
835 (page ())
836 (header ""))
837 (while page-list
838 (setq page (car page-list))
839 (and (nth 2 page)
840 (goto-char (car page))
841 (re-search-forward Man-first-heading-regexp nil t)
842 (setq header (buffer-substring (car page) (match-beginning 0)))
843 ;; Since the awk script collapses all successive blank
844 ;; lines into one, and since we don't want to get rid of
845 ;; the fast awk script, one must choose between adding
846 ;; spare blank lines between pages when there were none and
847 ;; deleting blank lines at page boundaries when there were
848 ;; some. We choose the first, so we comment the following
849 ;; line.
850 ;; (setq header (concat "\n" header)))
851 (while (search-forward header (nth 1 page) t)
852 (replace-match "")))
853 (setq page-list (cdr page-list)))))
855 (defun Man-unindent ()
856 "Delete the leading spaces that indent the manpage."
857 (let ((buffer-read-only nil)
858 (case-fold-search nil)
859 (page-list Man-page-list))
860 (while page-list
861 (let ((page (car page-list))
862 (indent "")
863 (nindent 0))
864 (narrow-to-region (car page) (car (cdr page)))
865 (if Man-uses-untabify-flag
866 (untabify (point-min) (point-max)))
867 (if (catch 'unindent
868 (goto-char (point-min))
869 (if (not (re-search-forward Man-first-heading-regexp nil t))
870 (throw 'unindent nil))
871 (beginning-of-line)
872 (setq indent (buffer-substring (point)
873 (progn
874 (skip-chars-forward " ")
875 (point))))
876 (setq nindent (length indent))
877 (if (zerop nindent)
878 (throw 'unindent nil))
879 (setq indent (concat indent "\\|$"))
880 (goto-char (point-min))
881 (while (not (eobp))
882 (if (looking-at indent)
883 (forward-line 1)
884 (throw 'unindent nil)))
885 (goto-char (point-min)))
886 (while (not (eobp))
887 (or (eolp)
888 (delete-char nindent))
889 (forward-line 1)))
890 (setq page-list (cdr page-list))
891 ))))
894 ;; ======================================================================
895 ;; Man mode commands
897 (defun Man-next-section (n)
898 "Move point to Nth next section (default 1)."
899 (interactive "p")
900 (let ((case-fold-search nil))
901 (if (looking-at Man-heading-regexp)
902 (forward-line 1))
903 (if (re-search-forward Man-heading-regexp (point-max) t n)
904 (beginning-of-line)
905 (goto-char (point-max)))))
907 (defun Man-previous-section (n)
908 "Move point to Nth previous section (default 1)."
909 (interactive "p")
910 (let ((case-fold-search nil))
911 (if (looking-at Man-heading-regexp)
912 (forward-line -1))
913 (if (re-search-backward Man-heading-regexp (point-min) t n)
914 (beginning-of-line)
915 (goto-char (point-min)))))
917 (defun Man-find-section (section)
918 "Move point to SECTION if it exists, otherwise don't move point.
919 Returns t if section is found, nil otherwise."
920 (let ((curpos (point))
921 (case-fold-search nil))
922 (goto-char (point-min))
923 (if (re-search-forward (concat "^" section) (point-max) t)
924 (progn (beginning-of-line) t)
925 (goto-char curpos)
926 nil)
929 (defun Man-goto-section ()
930 "Query for section to move point to."
931 (interactive)
932 (aput 'Man-sections-alist
933 (let* ((default (aheadsym Man-sections-alist))
934 (completion-ignore-case t)
935 chosen
936 (prompt (concat "Go to section: (default " default ") ")))
937 (setq chosen (completing-read prompt Man-sections-alist))
938 (if (or (not chosen)
939 (string= chosen ""))
940 default
941 chosen)))
942 (Man-find-section (aheadsym Man-sections-alist)))
944 (defun Man-goto-see-also-section ()
945 "Move point the the \"SEE ALSO\" section.
946 Actually the section moved to is described by `Man-see-also-regexp'."
947 (interactive)
948 (if (not (Man-find-section Man-see-also-regexp))
949 (error (concat "No " Man-see-also-regexp
950 " section found in the current manpage"))))
952 (defun Man-follow-manual-reference (reference)
953 "Get one of the manpages referred to in the \"SEE ALSO\" section.
954 Specify which reference to use; default is based on word at point."
955 (interactive
956 (if (not Man-refpages-alist)
957 (error "There are no references in the current man page")
958 (list (let* ((default (or
959 (car (all-completions
960 (save-excursion
961 (skip-syntax-backward "w()")
962 (skip-chars-forward " \t")
963 (let ((word (current-word)))
964 ;; strip a trailing '-':
965 (if (string-match "-$" word)
966 (substring word 0
967 (match-beginning 0))
968 word)))
969 Man-refpages-alist))
970 (aheadsym Man-refpages-alist)))
971 chosen
972 (prompt (concat "Refer to: (default " default ") ")))
973 (setq chosen (completing-read prompt Man-refpages-alist nil t))
974 (if (or (not chosen)
975 (string= chosen ""))
976 default
977 chosen)))))
978 (if (not Man-refpages-alist)
979 (error "Can't find any references in the current manpage")
980 (aput 'Man-refpages-alist reference)
981 (Man-getpage-in-background
982 (Man-translate-references (aheadsym Man-refpages-alist)))))
984 (defun Man-kill ()
985 "Kill the buffer containing the manpage."
986 (interactive)
987 (let ((buff (current-buffer)))
988 (delete-windows-on buff)
989 (kill-buffer buff))
990 (if (and window-system
991 (or (eq Man-notify-method 'newframe)
992 (and pop-up-frames
993 (eq Man-notify-method 'bully))))
994 (delete-frame)))
996 (defun Man-quit ()
997 "Bury the buffer containing the manpage."
998 (interactive)
999 (let ((buff (current-buffer)))
1000 (delete-windows-on buff)
1001 (bury-buffer buff))
1002 (if (and window-system
1003 (or (eq Man-notify-method 'newframe)
1004 (and pop-up-frames
1005 (eq Man-notify-method 'bully))))
1006 (delete-frame)))
1008 (defun Man-goto-page (page)
1009 "Go to the manual page on page PAGE."
1010 (interactive
1011 (if (not Man-page-list)
1012 (let ((args Man-arguments))
1013 (kill-buffer (current-buffer))
1014 (error "Can't find the %s manpage" args))
1015 (if (= (length Man-page-list) 1)
1016 (error "You're looking at the only manpage in the buffer")
1017 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1018 (length Man-page-list)))))))
1019 (if (not Man-page-list)
1020 (let ((args Man-arguments))
1021 (kill-buffer (current-buffer))
1022 (error "Can't find the %s manpage" args)))
1023 (if (or (< page 1)
1024 (> page (length Man-page-list)))
1025 (error "No manpage %d found" page))
1026 (let* ((page-range (nth (1- page) Man-page-list))
1027 (page-start (car page-range))
1028 (page-end (car (cdr page-range))))
1029 (setq Man-current-page page
1030 Man-page-mode-string (Man-make-page-mode-string))
1031 (widen)
1032 (goto-char page-start)
1033 (narrow-to-region page-start page-end)
1034 (Man-build-section-alist)
1035 (Man-build-references-alist)
1036 (goto-char (point-min))))
1039 (defun Man-next-manpage ()
1040 "Find the next manpage entry in the buffer."
1041 (interactive)
1042 (if (= (length Man-page-list) 1)
1043 (error "This is the only manpage in the buffer"))
1044 (if (< Man-current-page (length Man-page-list))
1045 (Man-goto-page (1+ Man-current-page))
1046 (if Man-circular-pages-flag
1047 (Man-goto-page 1)
1048 (error "You're looking at the last manpage in the buffer"))))
1050 (defun Man-previous-manpage ()
1051 "Find the previous manpage entry in the buffer."
1052 (interactive)
1053 (if (= (length Man-page-list) 1)
1054 (error "This is the only manpage in the buffer"))
1055 (if (> Man-current-page 1)
1056 (Man-goto-page (1- Man-current-page))
1057 (if Man-circular-pages-flag
1058 (Man-goto-page (length Man-page-list))
1059 (error "You're looking at the first manpage in the buffer"))))
1061 (provide 'man)
1063 ;;; man.el ends here