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