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