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