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>
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)
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.
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
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
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 heuristics
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
99 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
100 ;; empty defvars (keep the compiler quiet)
103 "Browse UNIX manual pages."
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
122 :type
'(repeat (list (string :tag
"Command String")
124 (string :tag
"Phrase String"))))
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
140 (defcustom Man-fontify-manpage-flag t
141 "*Non-nil means make up the manpage with fonts."
145 (defcustom Man-overstrike-face
'bold
146 "*Face to use when fontifying overstrike."
150 (defcustom Man-underline-face
'underline
151 "*Face to use when fontifying underlining."
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
))
177 (defcustom Man-frame-parameters nil
178 "*Frame parameter list for creating a new frame for a manual page."
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."
192 (defcustom Man-circular-pages-flag t
193 "*Non-nil means the manpage list is treated as circular for traversal."
197 (defcustom Man-section-translations-alist
200 ;; Some systems have a real 3x man section, so let's comment this.
201 ;; '("3X" . "3") ; Xlib man pages
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")))
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
)
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
"\
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
"\
321 s/\\(.\\)\b\\1/\\1/g\\
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
)
332 "Syntax table used in Man mode buffers.")
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
[mouse-2
] 'man-follow-mouse
)
355 (define-key Man-mode-map
"?" 'describe-mode
)
359 ;; ======================================================================
362 (defun Man-init-defvars ()
363 "Used for initialising variables based on display's color support.
364 This is necessary if one wants to dump man.el with Emacs."
366 ;; Avoid possible error in call-process by using a directory that must exist.
367 (let ((default-directory "/"))
370 (Man-fontify-manpage-flag
372 ((= 0 (call-process Man-sed-command nil nil nil Man-sysv-sed-script
))
374 ((= 0 (call-process Man-sed-command nil nil nil Man-berkeley-sed-script
))
375 Man-berkeley-sed-script
)
379 (setq Man-filter-list
380 ;; Avoid trailing nil which confuses customize.
386 (concat "-e '" Man-sed-script
"'")
388 "-e '/^[\001-\032][\001-\032]*$/d'"
389 "-e '/\e[789]/s///g'"
390 "-e '/Reformatting page. Wait/d'"
391 "-e '/Reformatting entry. Wait/d'"
392 "-e '/^[ \t]*Hewlett-Packard[ \t]Company[ \t]*-[ \t][0-9]*[ \t]-/d'"
393 "-e '/^[ \t]*Hewlett-Packard[ \t]*-[ \t][0-9]*[ \t]-.*$/d'"
394 "-e '/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d'"
395 "-e '/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d'"
396 "-e '/^Printed[ \t][0-9].*[0-9]$/d'"
397 "-e '/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d'"
398 "-e '/^[A-Za-z].*Last[ \t]change:/d'"
399 "-e '/^Sun[ \t]Release[ \t][0-9].*[0-9]$/d'"
400 "-e '/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d'"
401 "-e '/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d'"
407 "BEGIN { blankline=0; anonblank=0; }\n"
408 "/^$/ { if (anonblank==0) next; }\n"
410 "/^$/ { blankline++; next; }\n"
411 "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }\n"
414 (if (not Man-uses-untabify-flag
)
415 ;; The outer list will be stripped off by apply.
418 Man-untabify-command-args
))
422 (defsubst Man-make-page-mode-string
()
423 "Formats part of the mode line for Man mode."
424 (format "%s page %d of %d"
425 (or (nth 2 (nth (1- Man-current-page
) Man-page-list
))
428 (length Man-page-list
)))
430 (defsubst Man-build-man-command
()
431 "Builds the entire background manpage and cleaning command."
432 (let ((command (concat manual-program
" " Man-switches
433 ; Stock MS-DOS shells cannot redirect stderr;
434 ; `call-process' below sends it to /dev/null,
435 ; so we don't need `2>' even with DOS shells
436 ; which do support stderr redirection.
437 (if (not (fboundp 'start-process
))
439 (concat " %s 2>" null-device
))))
440 (flist Man-filter-list
))
441 (while (and flist
(car flist
))
442 (let ((pcom (car (car flist
)))
443 (pargs (cdr (car flist
))))
445 (concat command
" | " pcom
" "
446 (mapconcat (lambda (phrase)
447 (if (not (stringp phrase
))
448 (error "Malformed Man-filter-list"))
451 (setq flist
(cdr flist
))))
454 (defun Man-translate-references (ref)
455 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style.
456 Leave it as is if already in that style. Possibly downcase and
457 translate the section (see the Man-downcase-section-letters-flag
458 and the Man-section-translations-alist variables)."
461 (slist Man-section-translations-alist
))
463 ;; "chmod(2V)" case ?
464 ((string-match (concat "^" Man-reference-regexp
"$") ref
)
465 (setq name
(match-string 1 ref
)
466 section
(match-string 2 ref
)))
468 ((string-match (concat "^\\(" Man-section-regexp
469 "\\) +\\(" Man-name-regexp
"\\)$") ref
)
470 (setq name
(match-string 2 ref
)
471 section
(match-string 1 ref
))))
472 (if (string= name
"")
473 ref
; Return the reference as is
474 (if Man-downcase-section-letters-flag
475 (setq section
(downcase section
)))
477 (let ((s1 (car (car slist
)))
478 (s2 (cdr (car slist
))))
479 (setq slist
(cdr slist
))
480 (if Man-downcase-section-letters-flag
481 (setq s1
(downcase s1
)))
482 (if (not (string= s1 section
)) nil
483 (setq section
(if Man-downcase-section-letters-flag
487 (concat Man-specified-section-option section
" " name
))))
490 ;; ======================================================================
491 ;; default man entry: get word under point
493 (defsubst Man-default-man-entry
()
494 "Make a guess at a default manual entry.
495 This guess is based on the text surrounding the cursor."
498 ;; Default man entry title is any word the cursor is on, or if
499 ;; cursor not on a word, then nearest preceding word.
500 (setq word
(current-word))
501 (if (string-match "[._]+$" word
)
502 (setq word
(substring word
0 (match-beginning 0))))
503 ;; If looking at something like ioctl(2) or brc(1M), include the
504 ;; section number in the returned value. Remove text properties.
506 ;; Use `format' here to clear any text props from `word'.
510 (concat "[ \t]*([ \t]*\\(" Man-section-regexp
"\\)[ \t]*)"))
511 (format "(%s)" (match-string 1))
515 ;; ======================================================================
516 ;; Top level command and background process sentinel
518 ;; For compatibility with older versions.
520 (defalias 'manual-entry
'man
)
523 (defun man (man-args)
524 "Get a Un*x manual page and put it in a buffer.
525 This command is the top-level command in the man package. It runs a Un*x
526 command to retrieve and clean a manpage in the background and places the
527 results in a Man mode (manpage browsing) buffer. See variable
528 `Man-notify-method' for what happens when the buffer is ready.
529 If a buffer already exists for this man page, it will display immediately.
531 To specify a man page from a certain section, type SUBJECT(SECTION) or
532 SECTION SUBJECT when prompted for a manual entry. To see manpages from
533 all sections related to a subject, put something appropriate into the
534 `Man-switches' variable, which see."
536 (list (let* ((default-entry (Man-default-man-entry))
538 (format "Manual entry%s: "
539 (if (string= default-entry
"")
541 (format " (default %s)" default-entry
)))
542 nil nil default-entry
)))
543 (if (string= input
"")
544 (error "No man args given")
547 ;; Possibly translate the "subject(section)" syntax into the
548 ;; "section subject" syntax and possibly downcase the section.
549 (setq man-args
(Man-translate-references man-args
))
551 (Man-getpage-in-background man-args
))
554 (defun man-follow (man-args)
555 "Get a Un*x manual page of the item under point and put it in a buffer."
556 (interactive (list (Man-default-man-entry)))
557 (if (or (not man-args
)
558 (string= man-args
""))
559 (error "No item under point")
562 (defun man-follow-mouse (e)
563 "Get a Un*x manual page of the item under the mouse and put it in a buffer."
567 (call-interactively 'man-follow
)))
569 (defun Man-getpage-in-background (topic)
570 "Use TOPIC to build and fire off the manpage and cleaning command."
571 (let* ((man-args topic
)
572 (bufname (concat "*Man " man-args
"*"))
573 (buffer (get-buffer bufname
)))
575 (Man-notify-when-ready buffer
)
577 (message "Invoking %s %s in the background" manual-program man-args
)
578 (setq buffer
(generate-new-buffer bufname
))
581 (setq Man-original-frame
(selected-frame))
582 (setq Man-arguments man-args
))
583 (let ((process-environment (copy-sequence process-environment
))
584 ;; The following is so Awk script gets \n intact
585 ;; But don't prevent decoding of the outside.
586 (coding-system-for-write 'raw-text-unix
)
587 ;; We must decode the output by a coding system that the
588 ;; system's locale suggests in multibyte mode.
589 (coding-system-for-read
590 (if default-enable-multibyte-characters
591 locale-coding-system
'raw-text-unix
))
592 ;; Avoid possible error by using a directory that always exists.
593 (default-directory "/"))
594 ;; Prevent any attempt to use display terminal fanciness.
595 (setenv "TERM" "dumb")
596 (if (fboundp 'start-process
)
597 (set-process-sentinel
598 (start-process manual-program buffer
"sh" "-c"
599 (format (Man-build-man-command) man-args
))
600 'Man-bgproc-sentinel
)
603 (call-process shell-file-name nil
(list buffer nil
) nil
"-c"
604 (format (Man-build-man-command) man-args
)))
606 (or (and (numberp exit-status
)
608 (and (numberp exit-status
)
610 (format "exited abnormally with code %d"
612 (setq msg exit-status
))
613 (Man-bgproc-sentinel bufname msg
))))))))
615 (defun Man-notify-when-ready (man-buffer)
616 "Notify the user when MAN-BUFFER is ready.
617 See the variable `Man-notify-method' for the different notification behaviors."
618 (let ((saved-frame (save-excursion
619 (set-buffer man-buffer
)
620 Man-original-frame
)))
622 ((eq Man-notify-method
'newframe
)
623 ;; Since we run asynchronously, perhaps while Emacs is waiting
624 ;; for input, we must not leave a different buffer current. We
625 ;; can't rely on the editor command loop to reselect the
626 ;; selected window's buffer.
628 (let ((frame (make-frame Man-frame-parameters
)))
629 (set-window-buffer (frame-selected-window frame
) man-buffer
)
630 (set-window-dedicated-p (frame-selected-window frame
) t
)
631 (or (display-multi-frame-p frame
)
632 (select-frame frame
)))))
633 ((eq Man-notify-method
'pushy
)
634 (switch-to-buffer man-buffer
))
635 ((eq Man-notify-method
'bully
)
636 (and (frame-live-p saved-frame
)
637 (select-frame saved-frame
))
638 (pop-to-buffer man-buffer
)
639 (delete-other-windows))
640 ((eq Man-notify-method
'aggressive
)
641 (and (frame-live-p saved-frame
)
642 (select-frame saved-frame
))
643 (pop-to-buffer man-buffer
))
644 ((eq Man-notify-method
'friendly
)
645 (and (frame-live-p saved-frame
)
646 (select-frame saved-frame
))
647 (display-buffer man-buffer
'not-this-window
))
648 ((eq Man-notify-method
'polite
)
650 (message "Manual buffer %s is ready" (buffer-name man-buffer
)))
651 ((eq Man-notify-method
'quiet
)
652 (message "Manual buffer %s is ready" (buffer-name man-buffer
)))
653 ((or (eq Man-notify-method
'meek
)
658 (defun Man-softhyphen-to-minus ()
659 ;; \255 is some kind of dash in Latin-N. Versions of Debian man, at
660 ;; least, emit it even when not in a Latin-N locale.
661 (unless (eq t
(compare-strings "latin-" 0 nil
662 current-language-environment
0 6 t
))
663 (goto-char (point-min))
665 (if enable-multibyte-characters
666 (setq str
(string-as-multibyte str
)))
667 (while (search-forward str nil t
) (replace-match "-")))))
669 (defun Man-fontify-manpage ()
670 "Convert overstriking and underlining to the correct fonts.
671 Same for the ANSI bold and normal escape sequences."
673 (message "Please wait: making up the %s man page..." Man-arguments
)
674 (goto-char (point-min))
675 (while (search-forward "\e[1m" nil t
)
676 (delete-backward-char 4)
677 (put-text-property (point)
678 (progn (if (search-forward "\e[0m" nil
'move
)
679 (delete-backward-char 4))
681 'face Man-overstrike-face
))
682 (if (< (buffer-size) (position-bytes (point-max)))
683 ;; Multibyte characters exist.
685 (goto-char (point-min))
686 (while (search-forward "__\b\b" nil t
)
687 (backward-delete-char 4)
688 (put-text-property (point) (1+ (point)) 'face Man-underline-face
))
689 (goto-char (point-min))
690 (while (search-forward "\b\b__" nil t
)
691 (backward-delete-char 4)
692 (put-text-property (1- (point)) (point) 'face Man-underline-face
))))
693 (goto-char (point-min))
694 (while (search-forward "_\b" nil t
)
695 (backward-delete-char 2)
696 (put-text-property (point) (1+ (point)) 'face Man-underline-face
))
697 (goto-char (point-min))
698 (while (search-forward "\b_" nil t
)
699 (backward-delete-char 2)
700 (put-text-property (1- (point)) (point) 'face Man-underline-face
))
701 (goto-char (point-min))
702 (while (re-search-forward "\\(.\\)\\(\b+\\1\\)+" nil t
)
703 (replace-match "\\1")
704 (put-text-property (1- (point)) (point) 'face Man-overstrike-face
))
705 (goto-char (point-min))
706 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t
)
708 (put-text-property (1- (point)) (point) 'face
'bold
))
709 (goto-char (point-min))
710 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t
)
712 (put-text-property (1- (point)) (point) 'face
'bold
))
713 (goto-char (point-min))
714 ;; Try to recognize common forms of cross references.
715 (while (re-search-forward "\\w+([0-9].?)" nil t
)
716 (put-text-property (match-beginning 0) (match-end 0)
717 'mouse-face
'highlight
))
718 (Man-softhyphen-to-minus)
719 (message "%s man page made up" Man-arguments
))
721 (defun Man-cleanup-manpage ()
722 "Remove overstriking and underlining from the current buffer."
724 (message "Please wait: cleaning up the %s man page..."
726 (if (or (interactive-p) (not Man-sed-script
))
728 (goto-char (point-min))
729 (while (search-forward "_\b" nil t
) (backward-delete-char 2))
730 (goto-char (point-min))
731 (while (search-forward "\b_" nil t
) (backward-delete-char 2))
732 (goto-char (point-min))
733 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t
)
734 (replace-match "\\1"))
735 (goto-char (point-min))
736 (while (re-search-forward "\e\\[[0-9]+m" nil t
) (replace-match ""))
737 (goto-char (point-min))
738 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t
) (replace-match "o"))
740 (goto-char (point-min))
741 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t
) (replace-match "+"))
742 (Man-softhyphen-to-minus)
743 (message "%s man page cleaned up" Man-arguments
))
745 (defun Man-bgproc-sentinel (process msg
)
746 "Manpage background process sentinel.
747 When manpage command is run asynchronously, PROCESS is the process
748 object for the manpage command; when manpage command is run
749 synchronously, PROCESS is the name of the buffer where the manpage
750 command is run. Second argument MSG is the exit message of the
752 (let ((Man-buffer (if (stringp process
) (get-buffer process
)
753 (process-buffer process
)))
757 (if (null (buffer-name Man-buffer
)) ;; deleted buffer
758 (or (stringp process
)
759 (set-process-buffer process nil
))
762 (set-buffer Man-buffer
)
763 (let ((case-fold-search nil
))
764 (goto-char (point-min))
765 (cond ((or (looking-at "No \\(manual \\)*entry for")
766 (looking-at "[^\n]*: nothing appropriate$"))
767 (setq err-mess
(buffer-substring (point)
769 (end-of-line) (point)))
771 ((or (stringp process
)
772 (not (and (eq (process-status process
) 'exit
)
773 (= (process-exit-status process
) 0))))
774 (or (zerop (length msg
))
777 (concat (buffer-name Man-buffer
)
779 (let ((eos (1- (length msg
))))
780 (if (= (aref msg eos
) ?
\n)
781 (substring msg
0 eos
) msg
))))
782 (goto-char (point-max))
783 (insert (format "\nprocess %s" msg
))))
786 (kill-buffer Man-buffer
)
787 (if Man-fontify-manpage-flag
788 (Man-fontify-manpage)
789 (Man-cleanup-manpage))
790 (run-hooks 'Man-cooked-hook
)
792 (set-buffer-modified-p nil
)
794 ;; Restore case-fold-search before calling
795 ;; Man-notify-when-ready because it may switch buffers.
797 (if (not delete-buff
)
798 (Man-notify-when-ready Man-buffer
))
805 ;; ======================================================================
806 ;; set up manual mode in buffer and build alists
809 "A mode for browsing Un*x manual pages.
811 The following man commands are available in the buffer. Try
812 \"\\[describe-key] <key> RET\" for more information:
814 \\[man] Prompt to retrieve a new manpage.
815 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
816 \\[Man-next-manpage] Jump to next manpage in circular list.
817 \\[Man-previous-manpage] Jump to previous manpage in circular list.
818 \\[Man-next-section] Jump to next manpage section.
819 \\[Man-previous-section] Jump to previous manpage section.
820 \\[Man-goto-section] Go to a manpage section.
821 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
822 \\[Man-quit] Deletes the manpage window, bury its buffer.
823 \\[Man-kill] Deletes the manpage window, kill its buffer.
824 \\[describe-mode] Prints this help text.
826 The following variables may be of some use. Try
827 \"\\[describe-variable] <variable-name> RET\" for more information:
829 `Man-notify-method' What happens when manpage formatting is done.
830 `Man-downcase-section-letters-flag' Force section letters to lower case.
831 `Man-circular-pages-flag' Treat multiple manpage list as circular.
832 `Man-section-translations-alist' List of section numbers and their Un*x equiv.
833 `Man-filter-list' Background manpage filter command.
834 `Man-mode-map' Keymap bindings for Man mode buffers.
835 `Man-mode-hook' Normal hook run on entry to Man mode.
836 `Man-section-regexp' Regexp describing manpage section letters.
837 `Man-heading-regexp' Regexp describing section headers.
838 `Man-see-also-regexp' Regexp for SEE ALSO section (or your equiv).
839 `Man-first-heading-regexp' Regexp for first heading on a manpage.
840 `Man-reference-regexp' Regexp matching a references in SEE ALSO.
841 `Man-switches' Background `man' command switches.
843 The following key bindings are currently in effect in the buffer:
846 (setq major-mode
'Man-mode
848 buffer-auto-save-file-name nil
849 mode-line-buffer-identification
850 (list (default-value 'mode-line-buffer-identification
)
851 " {" 'Man-page-mode-string
"}")
854 (buffer-disable-undo (current-buffer))
856 (use-local-map Man-mode-map
)
857 (set-syntax-table man-mode-syntax-table
)
858 (Man-build-page-list)
859 (Man-strip-page-headers)
862 (run-hooks 'Man-mode-hook
))
864 (defsubst Man-build-section-alist
()
865 "Build the association list of manpage sections."
866 (setq Man-sections-alist nil
)
867 (goto-char (point-min))
868 (let ((case-fold-search nil
))
869 (while (re-search-forward Man-heading-regexp
(point-max) t
)
870 (aput 'Man-sections-alist
(match-string 1))
873 (defsubst Man-build-references-alist
()
874 "Build the association list of references (in the SEE ALSO section)."
875 (setq Man-refpages-alist nil
)
877 (if (Man-find-section Man-see-also-regexp
)
878 (let ((start (progn (forward-line 1) (point)))
885 (narrow-to-region start end
)
886 (goto-char (point-min))
887 (back-to-indentation)
888 (while (and (not (eobp)) (/= (point) runningpoint
))
889 (setq runningpoint
(point))
890 (if (re-search-forward Man-hyphenated-reference-regexp end t
)
891 (let* ((word (match-string 0))
892 (len (1- (length word
))))
894 (setq word
(concat hyphenated word
)
896 ;; Update len, in case a reference spans
897 ;; more than two lines (paranoia).
898 len
(1- (length word
))))
899 (if (= (aref word len
) ?-
)
900 (setq hyphenated
(substring word
0 len
)))
901 (if (string-match Man-reference-regexp word
)
902 (aput 'Man-refpages-alist word
))))
903 (skip-chars-forward " \t\n,"))))))
904 (setq Man-refpages-alist
(nreverse Man-refpages-alist
)))
906 (defun Man-build-page-list ()
907 "Build the list of separate manpages in the buffer."
908 (setq Man-page-list nil
)
909 (let ((page-start (point-min))
910 (page-end (point-max))
912 (goto-char page-start
)
913 ;; (switch-to-buffer (current-buffer))(debug)
916 (if (looking-at Man-page-header-regexp
)
919 ;; Go past both the current and the next Man-first-heading-regexp
920 (if (re-search-forward Man-first-heading-regexp nil
'move
2)
921 (let ((p (progn (beginning-of-line) (point))))
922 ;; We assume that the page header is delimited by blank
923 ;; lines and that it contains at most one blank line. So
924 ;; if we back by three blank lines we will be sure to be
925 ;; before the page header but not before the possible
926 ;; previous page header.
927 (search-backward "\n\n" nil t
3)
928 (if (re-search-forward Man-page-header-regexp p
'move
)
929 (beginning-of-line))))
930 (setq page-end
(point))
931 (setq Man-page-list
(append Man-page-list
932 (list (list (copy-marker page-start
)
933 (copy-marker page-end
)
935 (setq page-start page-end
)
938 (defun Man-strip-page-headers ()
939 "Strip all the page headers but the first from the manpage."
940 (let ((buffer-read-only nil
)
941 (case-fold-search nil
)
942 (page-list Man-page-list
)
946 (setq page
(car page-list
))
948 (goto-char (car page
))
949 (re-search-forward Man-first-heading-regexp nil t
)
950 (setq header
(buffer-substring (car page
) (match-beginning 0)))
951 ;; Since the awk script collapses all successive blank
952 ;; lines into one, and since we don't want to get rid of
953 ;; the fast awk script, one must choose between adding
954 ;; spare blank lines between pages when there were none and
955 ;; deleting blank lines at page boundaries when there were
956 ;; some. We choose the first, so we comment the following
958 ;; (setq header (concat "\n" header)))
959 (while (search-forward header
(nth 1 page
) t
)
961 (setq page-list
(cdr page-list
)))))
963 (defun Man-unindent ()
964 "Delete the leading spaces that indent the manpage."
965 (let ((buffer-read-only nil
)
966 (case-fold-search nil
)
967 (page-list Man-page-list
))
969 (let ((page (car page-list
))
972 (narrow-to-region (car page
) (car (cdr page
)))
973 (if Man-uses-untabify-flag
974 (untabify (point-min) (point-max)))
976 (goto-char (point-min))
977 (if (not (re-search-forward Man-first-heading-regexp nil t
))
978 (throw 'unindent nil
))
980 (setq indent
(buffer-substring (point)
982 (skip-chars-forward " ")
984 (setq nindent
(length indent
))
986 (throw 'unindent nil
))
987 (setq indent
(concat indent
"\\|$"))
988 (goto-char (point-min))
990 (if (looking-at indent
)
992 (throw 'unindent nil
)))
993 (goto-char (point-min)))
996 (delete-char nindent
))
998 (setq page-list
(cdr page-list
))
1002 ;; ======================================================================
1003 ;; Man mode commands
1005 (defun Man-next-section (n)
1006 "Move point to Nth next section (default 1)."
1008 (let ((case-fold-search nil
))
1009 (if (looking-at Man-heading-regexp
)
1011 (if (re-search-forward Man-heading-regexp
(point-max) t n
)
1013 (goto-char (point-max)))))
1015 (defun Man-previous-section (n)
1016 "Move point to Nth previous section (default 1)."
1018 (let ((case-fold-search nil
))
1019 (if (looking-at Man-heading-regexp
)
1021 (if (re-search-backward Man-heading-regexp
(point-min) t n
)
1023 (goto-char (point-min)))))
1025 (defun Man-find-section (section)
1026 "Move point to SECTION if it exists, otherwise don't move point.
1027 Returns t if section is found, nil otherwise."
1028 (let ((curpos (point))
1029 (case-fold-search nil
))
1030 (goto-char (point-min))
1031 (if (re-search-forward (concat "^" section
) (point-max) t
)
1032 (progn (beginning-of-line) t
)
1037 (defun Man-goto-section ()
1038 "Query for section to move point to."
1040 (aput 'Man-sections-alist
1041 (let* ((default (aheadsym Man-sections-alist
))
1042 (completion-ignore-case t
)
1044 (prompt (concat "Go to section: (default " default
") ")))
1045 (setq chosen
(completing-read prompt Man-sections-alist
))
1046 (if (or (not chosen
)
1047 (string= chosen
""))
1050 (Man-find-section (aheadsym Man-sections-alist
)))
1052 (defun Man-goto-see-also-section ()
1053 "Move point to the \"SEE ALSO\" section.
1054 Actually the section moved to is described by `Man-see-also-regexp'."
1056 (if (not (Man-find-section Man-see-also-regexp
))
1057 (error (concat "No " Man-see-also-regexp
1058 " section found in the current manpage"))))
1060 (defun Man-possibly-hyphenated-word ()
1061 "Return a possibly hyphenated word at point.
1062 If the word starts at the first non-whitespace column, and the
1063 previous line ends with a hyphen, return the last word on the previous
1064 line instead. Thus, if a reference to \"tcgetpgrp(3V)\" is hyphenated
1065 as \"tcgetp-grp(3V)\", and point is at \"grp(3V)\", we return
1066 \"tcgetp-\" instead of \"grp\"."
1068 (skip-syntax-backward "w()")
1069 (skip-chars-forward " \t")
1071 (word (current-word)))
1072 (when (eq beg
(save-excursion
1073 (back-to-indentation)
1076 (if (eq (char-before) ?-
)
1077 (setq word
(current-word))))
1080 (defun Man-follow-manual-reference (reference)
1081 "Get one of the manpages referred to in the \"SEE ALSO\" section.
1082 Specify which REFERENCE to use; default is based on word at point."
1084 (if (not Man-refpages-alist
)
1085 (error "There are no references in the current man page")
1086 (list (let* ((default (or
1087 (car (all-completions
1088 (let ((word (Man-possibly-hyphenated-word)))
1089 ;; strip a trailing '-':
1090 (if (string-match "-$" word
)
1092 (match-beginning 0))
1094 Man-refpages-alist
))
1095 (aheadsym Man-refpages-alist
)))
1097 (prompt (concat "Refer to: (default " default
") ")))
1098 (setq chosen
(completing-read prompt Man-refpages-alist nil t
))
1099 (if (or (not chosen
)
1100 (string= chosen
""))
1103 (if (not Man-refpages-alist
)
1104 (error "Can't find any references in the current manpage")
1105 (aput 'Man-refpages-alist reference
)
1106 (Man-getpage-in-background
1107 (Man-translate-references (aheadsym Man-refpages-alist
)))))
1110 "Kill the buffer containing the manpage."
1115 "Bury the buffer containing the manpage."
1119 (defun Man-goto-page (page)
1120 "Go to the manual page on page PAGE."
1122 (if (not Man-page-list
)
1123 (let ((args Man-arguments
))
1124 (kill-buffer (current-buffer))
1125 (error "Can't find the %s manpage" args
))
1126 (if (= (length Man-page-list
) 1)
1127 (error "You're looking at the only manpage in the buffer")
1128 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1129 (length Man-page-list
)))))))
1130 (if (not Man-page-list
)
1131 (let ((args Man-arguments
))
1132 (kill-buffer (current-buffer))
1133 (error "Can't find the %s manpage" args
)))
1135 (> page
(length Man-page-list
)))
1136 (error "No manpage %d found" page
))
1137 (let* ((page-range (nth (1- page
) Man-page-list
))
1138 (page-start (car page-range
))
1139 (page-end (car (cdr page-range
))))
1140 (setq Man-current-page page
1141 Man-page-mode-string
(Man-make-page-mode-string))
1143 (goto-char page-start
)
1144 (narrow-to-region page-start page-end
)
1145 (Man-build-section-alist)
1146 (Man-build-references-alist)
1147 (goto-char (point-min))))
1150 (defun Man-next-manpage ()
1151 "Find the next manpage entry in the buffer."
1153 (if (= (length Man-page-list
) 1)
1154 (error "This is the only manpage in the buffer"))
1155 (if (< Man-current-page
(length Man-page-list
))
1156 (Man-goto-page (1+ Man-current-page
))
1157 (if Man-circular-pages-flag
1159 (error "You're looking at the last manpage in the buffer"))))
1161 (defun Man-previous-manpage ()
1162 "Find the previous manpage entry in the buffer."
1164 (if (= (length Man-page-list
) 1)
1165 (error "This is the only manpage in the buffer"))
1166 (if (> Man-current-page
1)
1167 (Man-goto-page (1- Man-current-page
))
1168 (if Man-circular-pages-flag
1169 (Man-goto-page (length Man-page-list
))
1170 (error "You're looking at the first manpage in the buffer"))))
1172 ;; Init the man package variables, if not already done.
1175 (add-to-list 'debug-ignored-errors
"^No manpage [0-9]* found$")
1176 (add-to-list 'debug-ignored-errors
"^Can't find the .* manpage$")
1180 ;;; man.el ends here