~alpha, not ~ftp !
[emacs.git] / lisp / man.el
bloba7b0ff16fa1250dafce3c8adaf22e56ead993f38
1 ;;; man.el --- browse UNIX manual pages
3 ;; Copyright (C) 1993, 1994, 1996, 1997, 2001 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-map nil
229 "Keymap for Man mode.")
231 (defvar Man-mode-hook nil
232 "Hook run when Man mode is enabled.")
234 (defvar Man-cooked-hook nil
235 "Hook run after removing backspaces but before `Man-mode' processing.")
237 (defvar Man-name-regexp "[-a-zA-Z0-9_][-a-zA-Z0-9_.]*"
238 "Regular expression describing the name of a manpage (without section).")
240 (defvar Man-section-regexp "[0-9][a-zA-Z+]*\\|[LNln]"
241 "Regular expression describing a manpage section within parentheses.")
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.")
252 (defvar Man-heading-regexp "^\\([A-Z][A-Z ]+\\)$"
253 "Regular expression describing a manpage heading entry.")
255 (defvar Man-see-also-regexp "SEE ALSO"
256 "Regular expression for SEE ALSO heading (or your equivalent).
257 This regexp should not start with a `^' character.")
259 (defvar Man-first-heading-regexp "^[ \t]*NAME$\\|^[ \t]*No manual entry fo.*$"
260 "Regular expression describing first heading on a manpage.
261 This regular expression should start with a `^' character.")
263 (defvar Man-reference-regexp
264 (concat "\\(" Man-name-regexp "\\)(\\(" Man-section-regexp "\\))")
265 "Regular expression describing a reference to another manpage.")
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.")
273 (defvar Man-switches ""
274 "Switches passed to the man command, as a single string.
276 If you want to be able to see all the manpages for a subject you type,
277 make -a one of the switches, if your `man' program supports it.")
279 (defvar Man-specified-section-option
280 (if (string-match "-solaris[0-9.]*$" system-configuration)
281 "-s"
283 "Option that indicates a specified a manual section name.")
285 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
286 ;; end user variables
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)
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")
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
312 /\e\\[[0-9][0-9]*m/ s///g"
313 "Script for sysV-like sed to nuke backspaces and ANSI codes from manpages.")
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\\
324 /\e\\[[0-9][0-9]*m/ s///g"
325 "Script for berkeley-like sed to nuke backspaces and ANSI codes from manpages.")
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.")
334 (if Man-mode-map
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)
358 ;; ======================================================================
359 ;; utilities
361 (defun Man-init-defvars ()
362 "Used for initialising variables based on display's color support.
363 This is necessary if one wants to dump man.el with Emacs."
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)
376 nil))))
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 "'")
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'"
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"
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))
421 (defsubst Man-match-substring (&optional n string)
422 "Return the substring matched by the last search.
423 Optional arg N means return the substring matched by the Nth paren
424 grouping. Optional second arg STRING means return a substring from
425 that 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))))
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))
436 Man-current-page
437 (length Man-page-list)))
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))
463 (defun Man-translate-references (ref)
464 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style.
465 Leave it as is if already in that style. Possibly downcase and
466 translate the section (see the Man-downcase-section-letters-flag
467 and 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)
495 slist nil))))
496 (concat Man-specified-section-option section " " name))))
499 ;; ======================================================================
500 ;; default man entry: get word under point
502 (defsubst Man-default-man-entry ()
503 "Make a guess at a default manual entry.
504 This 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 "")))))
524 ;; ======================================================================
525 ;; Top level command and background process sentinel
527 ;; For compatibility with older versions.
528 ;;;###autoload
529 (defalias 'manual-entry 'man)
531 ;;;###autoload
532 (defun man (man-args)
533 "Get a Un*x manual page and put it in a buffer.
534 This command is the top-level command in the man package. It runs a Un*x
535 command to retrieve and clean a manpage in the background and places the
536 results in a Man mode (manpage browsing) buffer. See variable
537 `Man-notify-method' for what happens when the buffer is ready.
538 If a buffer already exists for this man page, it will display immediately.
540 To specify a man page from a certain section, type SUBJECT(SECTION) or
541 SECTION SUBJECT when prompted for a manual entry. To see manpages from
542 all sections related to a subject, put something appropriate into the
543 `Man-switches' variable, which see."
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 "")
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))))
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))
561 (Man-getpage-in-background man-args))
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)))
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 ;; In Debian Woody, at least, we get overlong lines under X
600 ;; unless COLUMNS or MANWIDTH is set. This isn't a problem on
601 ;; a tty. man(1) says:
602 ;; MANWIDTH
603 ;; If $MANWIDTH is set, its value is used as the line
604 ;; length for which manual pages should be formatted.
605 ;; If it is not set, manual pages will be formatted
606 ;; with a line length appropriate to the current ter-
607 ;; minal (using an ioctl(2) if available, the value of
608 ;; $COLUMNS, or falling back to 80 characters if nei-
609 ;; ther is available).
610 (if window-system
611 (unless (or (getenv "MANWIDTH") (getenv "COLUMNS"))
612 ;; This isn't strictly correct, since we don't know how
613 ;; the page will actually be displayed, but it seems
614 ;; reasonable.
615 (setenv "COLUMNS" (number-to-string (frame-width)))))
616 (if (fboundp 'start-process)
617 (set-process-sentinel
618 (start-process manual-program buffer "sh" "-c"
619 (format (Man-build-man-command) man-args))
620 'Man-bgproc-sentinel)
621 (progn
622 (let ((exit-status
623 (call-process shell-file-name nil (list buffer nil) nil "-c"
624 (format (Man-build-man-command) man-args)))
625 (msg ""))
626 (or (and (numberp exit-status)
627 (= exit-status 0))
628 (and (numberp exit-status)
629 (setq msg
630 (format "exited abnormally with code %d"
631 exit-status)))
632 (setq msg exit-status))
633 (Man-bgproc-sentinel bufname msg))))))))
635 (defun Man-notify-when-ready (man-buffer)
636 "Notify the user when MAN-BUFFER is ready.
637 See the variable `Man-notify-method' for the different notification behaviors."
638 (let ((saved-frame (save-excursion
639 (set-buffer man-buffer)
640 Man-original-frame)))
641 (cond
642 ((eq Man-notify-method 'newframe)
643 ;; Since we run asynchronously, perhaps while Emacs is waiting
644 ;; for input, we must not leave a different buffer current. We
645 ;; can't rely on the editor command loop to reselect the
646 ;; selected window's buffer.
647 (save-excursion
648 (let ((frame (make-frame Man-frame-parameters)))
649 (set-window-buffer (frame-selected-window frame) man-buffer)
650 (set-window-dedicated-p (frame-selected-window frame) t)
651 (or (display-multi-frame-p frame)
652 (select-frame frame)))))
653 ((eq Man-notify-method 'pushy)
654 (switch-to-buffer man-buffer))
655 ((eq Man-notify-method 'bully)
656 (and (frame-live-p saved-frame)
657 (select-frame saved-frame))
658 (pop-to-buffer man-buffer)
659 (delete-other-windows))
660 ((eq Man-notify-method 'aggressive)
661 (and (frame-live-p saved-frame)
662 (select-frame saved-frame))
663 (pop-to-buffer man-buffer))
664 ((eq Man-notify-method 'friendly)
665 (and (frame-live-p saved-frame)
666 (select-frame saved-frame))
667 (display-buffer man-buffer 'not-this-window))
668 ((eq Man-notify-method 'polite)
669 (beep)
670 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
671 ((eq Man-notify-method 'quiet)
672 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
673 ((or (eq Man-notify-method 'meek)
675 (message ""))
678 (defun Man-softhyphen-to-minus ()
679 ;; \255 is SOFT HYPHEN in Latin-N. Versions of Debian man, at
680 ;; least, emit it even when not in a Latin-N locale.
681 (unless (eq t (compare-strings "latin-" 0 nil
682 current-language-environment 0 6 t))
683 (goto-char (point-min))
684 (let ((str "\255"))
685 (if enable-multibyte-characters
686 (setq str (string-as-multibyte str)))
687 (while (search-forward str nil t) (replace-match "-")))))
689 (defun Man-fontify-manpage ()
690 "Convert overstriking and underlining to the correct fonts.
691 Same for the ANSI bold and normal escape sequences."
692 (interactive)
693 (message "Please wait: making up the %s man page..." Man-arguments)
694 (goto-char (point-min))
695 (while (search-forward "\e[1m" nil t)
696 (delete-backward-char 4)
697 (put-text-property (point)
698 (progn (if (search-forward "\e[0m" nil 'move)
699 (delete-backward-char 4))
700 (point))
701 'face Man-overstrike-face))
702 (if (< (buffer-size) (position-bytes (point-max)))
703 ;; Multibyte characters exist.
704 (progn
705 (goto-char (point-min))
706 (while (search-forward "__\b\b" nil t)
707 (backward-delete-char 4)
708 (put-text-property (point) (1+ (point)) 'face Man-underline-face))
709 (goto-char (point-min))
710 (while (search-forward "\b\b__" nil t)
711 (backward-delete-char 4)
712 (put-text-property (1- (point)) (point) 'face Man-underline-face))))
713 (goto-char (point-min))
714 (while (search-forward "_\b" nil t)
715 (backward-delete-char 2)
716 (put-text-property (point) (1+ (point)) 'face Man-underline-face))
717 (goto-char (point-min))
718 (while (search-forward "\b_" nil t)
719 (backward-delete-char 2)
720 (put-text-property (1- (point)) (point) 'face Man-underline-face))
721 (goto-char (point-min))
722 (while (re-search-forward "\\(.\\)\\(\b+\\1\\)+" nil t)
723 (replace-match "\\1")
724 (put-text-property (1- (point)) (point) 'face Man-overstrike-face))
725 (goto-char (point-min))
726 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t)
727 (replace-match "o")
728 (put-text-property (1- (point)) (point) 'face 'bold))
729 (goto-char (point-min))
730 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t)
731 (replace-match "+")
732 (put-text-property (1- (point)) (point) 'face 'bold))
733 (Man-softhyphen-to-minus)
734 (message "%s man page made up" Man-arguments))
736 (defun Man-cleanup-manpage ()
737 "Remove overstriking and underlining from the current buffer."
738 (interactive)
739 (message "Please wait: cleaning up the %s man page..."
740 Man-arguments)
741 (if (or (interactive-p) (not Man-sed-script))
742 (progn
743 (goto-char (point-min))
744 (while (search-forward "_\b" nil t) (backward-delete-char 2))
745 (goto-char (point-min))
746 (while (search-forward "\b_" nil t) (backward-delete-char 2))
747 (goto-char (point-min))
748 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
749 (replace-match "\\1"))
750 (goto-char (point-min))
751 (while (re-search-forward "\e\\[[0-9]+m" nil t) (replace-match ""))
752 (goto-char (point-min))
753 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t) (replace-match "o"))
755 (goto-char (point-min))
756 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t) (replace-match "+"))
757 (Man-softhyphen-to-minus)
758 (message "%s man page cleaned up" Man-arguments))
760 (defun Man-bgproc-sentinel (process msg)
761 "Manpage background process sentinel.
762 When manpage command is run asynchronously, PROCESS is the process
763 object for the manpage command; when manpage command is run
764 synchronously, PROCESS is the name of the buffer where the manpage
765 command is run. Second argument MSG is the exit message of the
766 manpage command."
767 (let ((Man-buffer (if (stringp process) (get-buffer process)
768 (process-buffer process)))
769 (delete-buff nil)
770 (err-mess nil))
772 (if (null (buffer-name Man-buffer)) ;; deleted buffer
773 (or (stringp process)
774 (set-process-buffer process nil))
776 (save-excursion
777 (set-buffer Man-buffer)
778 (let ((case-fold-search nil))
779 (goto-char (point-min))
780 (cond ((or (looking-at "No \\(manual \\)*entry for")
781 (looking-at "[^\n]*: nothing appropriate$"))
782 (setq err-mess (buffer-substring (point)
783 (progn
784 (end-of-line) (point)))
785 delete-buff t))
786 ((or (stringp process)
787 (not (and (eq (process-status process) 'exit)
788 (= (process-exit-status process) 0))))
789 (or (zerop (length msg))
790 (progn
791 (setq err-mess
792 (concat (buffer-name Man-buffer)
793 ": process "
794 (let ((eos (1- (length msg))))
795 (if (= (aref msg eos) ?\n)
796 (substring msg 0 eos) msg))))
797 (goto-char (point-max))
798 (insert (format "\nprocess %s" msg))))
800 (if delete-buff
801 (kill-buffer Man-buffer)
802 (if Man-fontify-manpage-flag
803 (Man-fontify-manpage)
804 (Man-cleanup-manpage))
805 (run-hooks 'Man-cooked-hook)
806 (Man-mode)
807 (set-buffer-modified-p nil)
809 ;; Restore case-fold-search before calling
810 ;; Man-notify-when-ready because it may switch buffers.
812 (if (not delete-buff)
813 (Man-notify-when-ready Man-buffer))
815 (if err-mess
816 (error err-mess))
817 ))))
820 ;; ======================================================================
821 ;; set up manual mode in buffer and build alists
823 (defun Man-mode ()
824 "A mode for browsing Un*x manual pages.
826 The following man commands are available in the buffer. Try
827 \"\\[describe-key] <key> RET\" for more information:
829 \\[man] Prompt to retrieve a new manpage.
830 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
831 \\[Man-next-manpage] Jump to next manpage in circular list.
832 \\[Man-previous-manpage] Jump to previous manpage in circular list.
833 \\[Man-next-section] Jump to next manpage section.
834 \\[Man-previous-section] Jump to previous manpage section.
835 \\[Man-goto-section] Go to a manpage section.
836 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
837 \\[Man-quit] Deletes the manpage window, bury its buffer.
838 \\[Man-kill] Deletes the manpage window, kill its buffer.
839 \\[describe-mode] Prints this help text.
841 The following variables may be of some use. Try
842 \"\\[describe-variable] <variable-name> RET\" for more information:
844 `Man-notify-method' What happens when manpage formatting is done.
845 `Man-downcase-section-letters-flag' Force section letters to lower case.
846 `Man-circular-pages-flag' Treat multiple manpage list as circular.
847 `Man-section-translations-alist' List of section numbers and their Un*x equiv.
848 `Man-filter-list' Background manpage filter command.
849 `Man-mode-map' Keymap bindings for Man mode buffers.
850 `Man-mode-hook' Normal hook run on entry to Man mode.
851 `Man-section-regexp' Regexp describing manpage section letters.
852 `Man-heading-regexp' Regexp describing section headers.
853 `Man-see-also-regexp' Regexp for SEE ALSO section (or your equiv).
854 `Man-first-heading-regexp' Regexp for first heading on a manpage.
855 `Man-reference-regexp' Regexp matching a references in SEE ALSO.
856 `Man-switches' Background `man' command switches.
858 The following key bindings are currently in effect in the buffer:
859 \\{Man-mode-map}"
860 (interactive)
861 (setq major-mode 'Man-mode
862 mode-name "Man"
863 buffer-auto-save-file-name nil
864 mode-line-buffer-identification
865 (list (default-value 'mode-line-buffer-identification)
866 " {" 'Man-page-mode-string "}")
867 truncate-lines t
868 buffer-read-only t)
869 (buffer-disable-undo (current-buffer))
870 (auto-fill-mode -1)
871 (use-local-map Man-mode-map)
872 (set-syntax-table man-mode-syntax-table)
873 (Man-build-page-list)
874 (Man-strip-page-headers)
875 (Man-unindent)
876 (Man-goto-page 1)
877 (run-hooks 'Man-mode-hook))
879 (defsubst Man-build-section-alist ()
880 "Build the association list of manpage sections."
881 (setq Man-sections-alist nil)
882 (goto-char (point-min))
883 (let ((case-fold-search nil))
884 (while (re-search-forward Man-heading-regexp (point-max) t)
885 (aput 'Man-sections-alist (Man-match-substring 1))
886 (forward-line 1))))
888 (defsubst Man-build-references-alist ()
889 "Build the association list of references (in the SEE ALSO section)."
890 (setq Man-refpages-alist nil)
891 (save-excursion
892 (if (Man-find-section Man-see-also-regexp)
893 (let ((start (progn (forward-line 1) (point)))
894 (end (progn
895 (Man-next-section 1)
896 (point)))
897 hyphenated
898 (runningpoint -1))
899 (save-restriction
900 (narrow-to-region start end)
901 (goto-char (point-min))
902 (back-to-indentation)
903 (while (and (not (eobp)) (/= (point) runningpoint))
904 (setq runningpoint (point))
905 (if (re-search-forward Man-hyphenated-reference-regexp end t)
906 (let* ((word (Man-match-substring 0))
907 (len (1- (length word))))
908 (if hyphenated
909 (setq word (concat hyphenated word)
910 hyphenated nil
911 ;; Update len, in case a reference spans
912 ;; more than two lines (paranoia).
913 len (1- (length word))))
914 (if (= (aref word len) ?-)
915 (setq hyphenated (substring word 0 len)))
916 (if (string-match Man-reference-regexp word)
917 (aput 'Man-refpages-alist word))))
918 (skip-chars-forward " \t\n,"))))))
919 (setq Man-refpages-alist (nreverse Man-refpages-alist)))
921 (defun Man-build-page-list ()
922 "Build the list of separate manpages in the buffer."
923 (setq Man-page-list nil)
924 (let ((page-start (point-min))
925 (page-end (point-max))
926 (header ""))
927 (goto-char page-start)
928 ;; (switch-to-buffer (current-buffer))(debug)
929 (while (not (eobp))
930 (setq header
931 (if (looking-at Man-page-header-regexp)
932 (Man-match-substring 1)
933 nil))
934 ;; Go past both the current and the next Man-first-heading-regexp
935 (if (re-search-forward Man-first-heading-regexp nil 'move 2)
936 (let ((p (progn (beginning-of-line) (point))))
937 ;; We assume that the page header is delimited by blank
938 ;; lines and that it contains at most one blank line. So
939 ;; if we back by three blank lines we will be sure to be
940 ;; before the page header but not before the possible
941 ;; previous page header.
942 (search-backward "\n\n" nil t 3)
943 (if (re-search-forward Man-page-header-regexp p 'move)
944 (beginning-of-line))))
945 (setq page-end (point))
946 (setq Man-page-list (append Man-page-list
947 (list (list (copy-marker page-start)
948 (copy-marker page-end)
949 header))))
950 (setq page-start page-end)
953 (defun Man-strip-page-headers ()
954 "Strip all the page headers but the first from the manpage."
955 (let ((buffer-read-only nil)
956 (case-fold-search nil)
957 (page-list Man-page-list)
958 (page ())
959 (header ""))
960 (while page-list
961 (setq page (car page-list))
962 (and (nth 2 page)
963 (goto-char (car page))
964 (re-search-forward Man-first-heading-regexp nil t)
965 (setq header (buffer-substring (car page) (match-beginning 0)))
966 ;; Since the awk script collapses all successive blank
967 ;; lines into one, and since we don't want to get rid of
968 ;; the fast awk script, one must choose between adding
969 ;; spare blank lines between pages when there were none and
970 ;; deleting blank lines at page boundaries when there were
971 ;; some. We choose the first, so we comment the following
972 ;; line.
973 ;; (setq header (concat "\n" header)))
974 (while (search-forward header (nth 1 page) t)
975 (replace-match "")))
976 (setq page-list (cdr page-list)))))
978 (defun Man-unindent ()
979 "Delete the leading spaces that indent the manpage."
980 (let ((buffer-read-only nil)
981 (case-fold-search nil)
982 (page-list Man-page-list))
983 (while page-list
984 (let ((page (car page-list))
985 (indent "")
986 (nindent 0))
987 (narrow-to-region (car page) (car (cdr page)))
988 (if Man-uses-untabify-flag
989 (untabify (point-min) (point-max)))
990 (if (catch 'unindent
991 (goto-char (point-min))
992 (if (not (re-search-forward Man-first-heading-regexp nil t))
993 (throw 'unindent nil))
994 (beginning-of-line)
995 (setq indent (buffer-substring (point)
996 (progn
997 (skip-chars-forward " ")
998 (point))))
999 (setq nindent (length indent))
1000 (if (zerop nindent)
1001 (throw 'unindent nil))
1002 (setq indent (concat indent "\\|$"))
1003 (goto-char (point-min))
1004 (while (not (eobp))
1005 (if (looking-at indent)
1006 (forward-line 1)
1007 (throw 'unindent nil)))
1008 (goto-char (point-min)))
1009 (while (not (eobp))
1010 (or (eolp)
1011 (delete-char nindent))
1012 (forward-line 1)))
1013 (setq page-list (cdr page-list))
1014 ))))
1017 ;; ======================================================================
1018 ;; Man mode commands
1020 (defun Man-next-section (n)
1021 "Move point to Nth next section (default 1)."
1022 (interactive "p")
1023 (let ((case-fold-search nil))
1024 (if (looking-at Man-heading-regexp)
1025 (forward-line 1))
1026 (if (re-search-forward Man-heading-regexp (point-max) t n)
1027 (beginning-of-line)
1028 (goto-char (point-max)))))
1030 (defun Man-previous-section (n)
1031 "Move point to Nth previous section (default 1)."
1032 (interactive "p")
1033 (let ((case-fold-search nil))
1034 (if (looking-at Man-heading-regexp)
1035 (forward-line -1))
1036 (if (re-search-backward Man-heading-regexp (point-min) t n)
1037 (beginning-of-line)
1038 (goto-char (point-min)))))
1040 (defun Man-find-section (section)
1041 "Move point to SECTION if it exists, otherwise don't move point.
1042 Returns t if section is found, nil otherwise."
1043 (let ((curpos (point))
1044 (case-fold-search nil))
1045 (goto-char (point-min))
1046 (if (re-search-forward (concat "^" section) (point-max) t)
1047 (progn (beginning-of-line) t)
1048 (goto-char curpos)
1049 nil)
1052 (defun Man-goto-section ()
1053 "Query for section to move point to."
1054 (interactive)
1055 (aput 'Man-sections-alist
1056 (let* ((default (aheadsym Man-sections-alist))
1057 (completion-ignore-case t)
1058 chosen
1059 (prompt (concat "Go to section: (default " default ") ")))
1060 (setq chosen (completing-read prompt Man-sections-alist))
1061 (if (or (not chosen)
1062 (string= chosen ""))
1063 default
1064 chosen)))
1065 (Man-find-section (aheadsym Man-sections-alist)))
1067 (defun Man-goto-see-also-section ()
1068 "Move point to the \"SEE ALSO\" section.
1069 Actually the section moved to is described by `Man-see-also-regexp'."
1070 (interactive)
1071 (if (not (Man-find-section Man-see-also-regexp))
1072 (error (concat "No " Man-see-also-regexp
1073 " section found in the current manpage"))))
1075 (defun Man-possibly-hyphenated-word ()
1076 "Return a possibly hyphenated word at point.
1077 If the word starts at the first non-whitespace column, and the
1078 previous line ends with a hyphen, return the last word on the previous
1079 line instead. Thus, if a reference to \"tcgetpgrp(3V)\" is hyphenated
1080 as \"tcgetp-grp(3V)\", and point is at \"grp(3V)\", we return
1081 \"tcgetp-\" instead of \"grp\"."
1082 (save-excursion
1083 (skip-syntax-backward "w()")
1084 (skip-chars-forward " \t")
1085 (let ((beg (point))
1086 (word (current-word)))
1087 (when (eq beg (save-excursion
1088 (back-to-indentation)
1089 (point)))
1090 (end-of-line 0)
1091 (if (eq (char-before) ?-)
1092 (setq word (current-word))))
1093 word)))
1095 (defun Man-follow-manual-reference (reference)
1096 "Get one of the manpages referred to in the \"SEE ALSO\" section.
1097 Specify which REFERENCE to use; default is based on word at point."
1098 (interactive
1099 (if (not Man-refpages-alist)
1100 (error "There are no references in the current man page")
1101 (list (let* ((default (or
1102 (car (all-completions
1103 (let ((word (Man-possibly-hyphenated-word)))
1104 ;; strip a trailing '-':
1105 (if (string-match "-$" word)
1106 (substring word 0
1107 (match-beginning 0))
1108 word))
1109 Man-refpages-alist))
1110 (aheadsym Man-refpages-alist)))
1111 chosen
1112 (prompt (concat "Refer to: (default " default ") ")))
1113 (setq chosen (completing-read prompt Man-refpages-alist nil t))
1114 (if (or (not chosen)
1115 (string= chosen ""))
1116 default
1117 chosen)))))
1118 (if (not Man-refpages-alist)
1119 (error "Can't find any references in the current manpage")
1120 (aput 'Man-refpages-alist reference)
1121 (Man-getpage-in-background
1122 (Man-translate-references (aheadsym Man-refpages-alist)))))
1124 (defun Man-kill ()
1125 "Kill the buffer containing the manpage."
1126 (interactive)
1127 (quit-window t))
1129 (defun Man-quit ()
1130 "Bury the buffer containing the manpage."
1131 (interactive)
1132 (quit-window))
1134 (defun Man-goto-page (page)
1135 "Go to the manual page on page PAGE."
1136 (interactive
1137 (if (not Man-page-list)
1138 (let ((args Man-arguments))
1139 (kill-buffer (current-buffer))
1140 (error "Can't find the %s manpage" args))
1141 (if (= (length Man-page-list) 1)
1142 (error "You're looking at the only manpage in the buffer")
1143 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1144 (length Man-page-list)))))))
1145 (if (not Man-page-list)
1146 (let ((args Man-arguments))
1147 (kill-buffer (current-buffer))
1148 (error "Can't find the %s manpage" args)))
1149 (if (or (< page 1)
1150 (> page (length Man-page-list)))
1151 (error "No manpage %d found" page))
1152 (let* ((page-range (nth (1- page) Man-page-list))
1153 (page-start (car page-range))
1154 (page-end (car (cdr page-range))))
1155 (setq Man-current-page page
1156 Man-page-mode-string (Man-make-page-mode-string))
1157 (widen)
1158 (goto-char page-start)
1159 (narrow-to-region page-start page-end)
1160 (Man-build-section-alist)
1161 (Man-build-references-alist)
1162 (goto-char (point-min))))
1165 (defun Man-next-manpage ()
1166 "Find the next manpage entry in the buffer."
1167 (interactive)
1168 (if (= (length Man-page-list) 1)
1169 (error "This is the only manpage in the buffer"))
1170 (if (< Man-current-page (length Man-page-list))
1171 (Man-goto-page (1+ Man-current-page))
1172 (if Man-circular-pages-flag
1173 (Man-goto-page 1)
1174 (error "You're looking at the last manpage in the buffer"))))
1176 (defun Man-previous-manpage ()
1177 "Find the previous manpage entry in the buffer."
1178 (interactive)
1179 (if (= (length Man-page-list) 1)
1180 (error "This is the only manpage in the buffer"))
1181 (if (> Man-current-page 1)
1182 (Man-goto-page (1- Man-current-page))
1183 (if Man-circular-pages-flag
1184 (Man-goto-page (length Man-page-list))
1185 (error "You're looking at the first manpage in the buffer"))))
1187 ;; Init the man package variables, if not already done.
1188 (Man-init-defvars)
1190 (add-to-list 'debug-ignored-errors "^No manpage [0-9]* found$")
1191 (add-to-list 'debug-ignored-errors "^Can't find the .* manpage$")
1193 (provide 'man)
1195 ;;; man.el ends here