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