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