]> code.delx.au - gnu-emacs/blob - lisp/man.el
(ps-face-foreground-name, ps-face-background-name):
[gnu-emacs] / lisp / man.el
1 ;;; man.el --- browse UNIX manual pages -*- coding: iso-8859-1 -*-
2
3 ;; Copyright (C) 1993, 1994, 1996, 1997, 2001, 2003, 2004
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Barry A. Warsaw <bwarsaw@cen.com>
7 ;; Maintainer: FSF
8 ;; Keywords: help
9 ;; Adapted-By: ESR, pot
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;; This code provides a function, `man', with which you can browse
31 ;; UNIX manual pages. Formatting is done in background so that you
32 ;; can continue to use your Emacs while processing is going on.
33 ;;
34 ;; The mode also supports hypertext-like following of manual page SEE
35 ;; ALSO references, and other features. See below or do `?' in a
36 ;; manual page buffer for details.
37
38 ;; ========== Credits and History ==========
39 ;; In mid 1991, several people posted some interesting improvements to
40 ;; man.el from the standard emacs 18.57 distribution. I liked many of
41 ;; these, but wanted everything in one single package, so I decided
42 ;; to incorporate them into a single manual browsing mode. While
43 ;; much of the code here has been rewritten, and some features added,
44 ;; these folks deserve lots of credit for providing the initial
45 ;; excellent packages on which this one is based.
46
47 ;; Nick Duffek <duffek@chaos.cs.brandeis.edu>, posted a very nice
48 ;; improvement which retrieved and cleaned the manpages in a
49 ;; background process, and which correctly deciphered such options as
50 ;; man -k.
51
52 ;; Eric Rose <erose@jessica.stanford.edu>, submitted manual.el which
53 ;; provided a very nice manual browsing mode.
54
55 ;; This package was available as `superman.el' from the LCD package
56 ;; for some time before it was accepted into Emacs 19. The entry
57 ;; point and some other names have been changed to make it a drop-in
58 ;; replacement for the old man.el package.
59
60 ;; Francesco Potorti` <pot@cnuce.cnr.it> cleaned it up thoroughly,
61 ;; making it faster, more robust and more tolerant of different
62 ;; systems' man idiosyncrasies.
63
64 ;; ========== Features ==========
65 ;; + Runs "man" in the background and pipes the results through a
66 ;; series of sed and awk scripts so that all retrieving and cleaning
67 ;; is done in the background. The cleaning commands are configurable.
68 ;; + Syntax is the same as Un*x man
69 ;; + Functionality is the same as Un*x man, including "man -k" and
70 ;; "man <section>", etc.
71 ;; + Provides a manual browsing mode with keybindings for traversing
72 ;; the sections of a manpage, following references in the SEE ALSO
73 ;; section, and more.
74 ;; + Multiple manpages created with the same man command are put into
75 ;; a narrowed buffer circular list.
76
77 ;; ============= TODO ===========
78 ;; - Add a command for printing.
79 ;; - The awk script deletes multiple blank lines. This behaviour does
80 ;; not allow to understand if there was indeed a blank line at the
81 ;; end or beginning of a page (after the header, or before the
82 ;; footer). A different algorithm should be used. It is easy to
83 ;; compute how many blank lines there are before and after the page
84 ;; headers, and after the page footer. But it is possible to compute
85 ;; the number of blank lines before the page footer by heuristics
86 ;; only. Is it worth doing?
87 ;; - Allow a user option to mean that all the manpages should go in
88 ;; the same buffer, where they can be browsed with M-n and M-p.
89 ;; - Allow completion on the manpage name when calling man. This
90 ;; requires a reliable list of places where manpages can be found. The
91 ;; drawback would be that if the list is not complete, the user might
92 ;; be led to believe that the manpages in the missing directories do
93 ;; not exist.
94
95 \f
96 ;;; Code:
97
98 (eval-when-compile (require 'cl))
99 (require 'assoc)
100 (require 'button)
101
102 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
103 ;; empty defvars (keep the compiler quiet)
104
105 (defgroup man nil
106 "Browse UNIX manual pages."
107 :prefix "Man-"
108 :group 'help)
109
110
111 (defvar Man-notify)
112 (defvar Man-current-page)
113 (defvar Man-page-list)
114 (defcustom Man-filter-list nil
115 "*Manpage cleaning filter command phrases.
116 This variable contains a list of the following form:
117
118 '((command-string phrase-string*)*)
119
120 Each phrase-string is concatenated onto the command-string to form a
121 command filter. The (standard) output (and standard error) of the Un*x
122 man command is piped through each command filter in the order the
123 commands appear in the association list. The final output is placed in
124 the manpage buffer."
125 :type '(repeat (list (string :tag "Command String")
126 (repeat :inline t
127 (string :tag "Phrase String"))))
128 :group 'man)
129
130 (defvar Man-original-frame)
131 (defvar Man-arguments)
132 (defvar Man-sections-alist)
133 (defvar Man-refpages-alist)
134 (defvar Man-uses-untabify-flag t
135 "Non-nil means use `untabify' instead of `Man-untabify-command'.")
136 (defvar Man-page-mode-string)
137 (defvar Man-sed-script nil
138 "Script for sed to nuke backspaces and ANSI codes from manpages.")
139
140 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
141 ;; user variables
142
143 (defcustom Man-fontify-manpage-flag t
144 "*Non-nil means make up the manpage with fonts."
145 :type 'boolean
146 :group 'man)
147
148 (defcustom Man-overstrike-face 'bold
149 "*Face to use when fontifying overstrike."
150 :type 'face
151 :group 'man)
152
153 (defcustom Man-underline-face 'underline
154 "*Face to use when fontifying underlining."
155 :type 'face
156 :group 'man)
157
158 (defcustom Man-reverse-face 'highlight
159 "*Face to use when fontifying reverse video."
160 :type 'face
161 :group 'man)
162
163 ;; Use the value of the obsolete user option Man-notify, if set.
164 (defcustom Man-notify-method (if (boundp 'Man-notify) Man-notify 'friendly)
165 "*Selects the behavior when manpage is ready.
166 This variable may have one of the following values, where (sf) means
167 that the frames are switched, so the manpage is displayed in the frame
168 where the man command was called from:
169
170 newframe -- put the manpage in its own frame (see `Man-frame-parameters')
171 pushy -- make the manpage the current buffer in the current window
172 bully -- make the manpage the current buffer and only window (sf)
173 aggressive -- make the manpage the current buffer in the other window (sf)
174 friendly -- display manpage in the other window but don't make current (sf)
175 polite -- don't display manpage, but prints message and beep when ready
176 quiet -- like `polite', but don't beep
177 meek -- make no indication that the manpage is ready
178
179 Any other value of `Man-notify-method' is equivalent to `meek'."
180 :type '(radio (const newframe) (const pushy) (const bully)
181 (const aggressive) (const friendly)
182 (const polite) (const quiet) (const meek))
183 :group 'man)
184
185 (defcustom Man-width nil
186 "*Number of columns for which manual pages should be formatted.
187 If nil, the width of the window selected at the moment of man
188 invocation is used. If non-nil, the width of the frame selected
189 at the moment of man invocation is used. The value also can be a
190 positive integer."
191 :type '(choice (const :tag "Window width" nil)
192 (const :tag "Frame width" t)
193 (integer :tag "Specific width" :value 65))
194 :group 'man)
195
196 (defcustom Man-frame-parameters nil
197 "*Frame parameter list for creating a new frame for a manual page."
198 :type 'sexp
199 :group 'man)
200
201 (defcustom Man-downcase-section-letters-flag t
202 "*Non-nil means letters in sections are converted to lower case.
203 Some Un*x man commands can't handle uppercase letters in sections, for
204 example \"man 2V chmod\", but they are often displayed in the manpage
205 with the upper case letter. When this variable is t, the section
206 letter (e.g., \"2V\") is converted to lowercase (e.g., \"2v\") before
207 being sent to the man background process."
208 :type 'boolean
209 :group 'man)
210
211 (defcustom Man-circular-pages-flag t
212 "*Non-nil means the manpage list is treated as circular for traversal."
213 :type 'boolean
214 :group 'man)
215
216 (defcustom Man-section-translations-alist
217 (list
218 '("3C++" . "3")
219 ;; Some systems have a real 3x man section, so let's comment this.
220 ;; '("3X" . "3") ; Xlib man pages
221 '("3X11" . "3")
222 '("1-UCB" . ""))
223 "*Association list of bogus sections to real section numbers.
224 Some manpages (e.g. the Sun C++ 2.1 manpages) have section numbers in
225 their references which Un*x `man' does not recognize. This
226 association list is used to translate those sections, when found, to
227 the associated section number."
228 :type '(repeat (cons (string :tag "Bogus Section")
229 (string :tag "Real Section")))
230 :group 'man)
231
232 (defcustom Man-header-file-path
233 '("/usr/include" "/usr/local/include")
234 "C Header file search path used in Man."
235 :type '(repeat string)
236 :group 'man)
237
238 (defvar manual-program "man"
239 "The name of the program that produces man pages.")
240
241 (defvar Man-untabify-command "pr"
242 "Command used for untabifying.")
243
244 (defvar Man-untabify-command-args (list "-t" "-e")
245 "List of arguments to be passed to `Man-untabify-command' (which see).")
246
247 (defvar Man-sed-command "sed"
248 "Command used for processing sed scripts.")
249
250 (defvar Man-awk-command "awk"
251 "Command used for processing awk scripts.")
252
253 (defvar Man-mode-map nil
254 "Keymap for Man mode.")
255
256 (defvar Man-mode-hook nil
257 "Hook run when Man mode is enabled.")
258
259 (defvar Man-cooked-hook nil
260 "Hook run after removing backspaces but before `Man-mode' processing.")
261
262 (defvar Man-name-regexp "[-a-zA-Z0-9_­+][-a-zA-Z0-9_.:­+]*"
263 "Regular expression describing the name of a manpage (without section).")
264
265 (defvar Man-section-regexp "[0-9][a-zA-Z+]*\\|[LNln]"
266 "Regular expression describing a manpage section within parentheses.")
267
268 (defvar Man-page-header-regexp
269 (if (and (string-match "-solaris2\\." system-configuration)
270 (not (string-match "-solaris2\\.[123435]$" system-configuration)))
271 (concat "^[-A-Za-z0-9_].*[ \t]\\(" Man-name-regexp
272 "(\\(" Man-section-regexp "\\))\\)$")
273 (concat "^[ \t]*\\(" Man-name-regexp
274 "(\\(" Man-section-regexp "\\))\\).*\\1"))
275 "Regular expression describing the heading of a page.")
276
277 (defvar Man-heading-regexp "^\\([A-Z][A-Z -]+\\)$"
278 "Regular expression describing a manpage heading entry.")
279
280 (defvar Man-see-also-regexp "SEE ALSO"
281 "Regular expression for SEE ALSO heading (or your equivalent).
282 This regexp should not start with a `^' character.")
283
284 (defvar Man-first-heading-regexp "^[ \t]*NAME$\\|^[ \t]*No manual entry fo.*$"
285 "Regular expression describing first heading on a manpage.
286 This regular expression should start with a `^' character.")
287
288 (defvar Man-reference-regexp
289 (concat "\\(" Man-name-regexp "\\)(\\(" Man-section-regexp "\\))")
290 "Regular expression describing a reference to another manpage.")
291
292 (defvar Man-synopsis-regexp "SYNOPSIS"
293 "Regular expression for SYNOPSIS heading (or your equivalent).
294 This regexp should not start with a `^' character.")
295
296 (defvar Man-files-regexp "FILES"
297 "Regular expression for FILES heading (or your equivalent).
298 This regexp should not start with a `^' character.")
299
300 (defvar Man-include-regexp "#[ \t]*include[ \t]*"
301 "Regular expression describing the #include (directive of cpp).")
302
303 (defvar Man-file-name-regexp "[^<>\" \t\n]+"
304 "Regular expression describing <> in #include line (directive of cpp).")
305
306 (defvar Man-normal-file-prefix-regexp "[/~$]"
307 "Regular expression describing a file path appeared in FILES section.")
308
309 (defvar Man-header-regexp
310 (concat "\\(" Man-include-regexp "\\)"
311 "[<\"]"
312 "\\(" Man-file-name-regexp "\\)"
313 "[>\"]")
314 "Regular expression describing references to header files.")
315
316 (defvar Man-normal-file-regexp
317 (concat Man-normal-file-prefix-regexp Man-file-name-regexp)
318 "Regular expression describing references to normal files.")
319
320 ;; This includes the section as an optional part to catch hyphenated
321 ;; refernces to manpages.
322 (defvar Man-hyphenated-reference-regexp
323 (concat "\\(" Man-name-regexp "\\)\\((\\(" Man-section-regexp "\\))\\)?")
324 "Regular expression describing a reference in the SEE ALSO section.")
325
326 (defvar Man-switches ""
327 "Switches passed to the man command, as a single string.
328
329 If you want to be able to see all the manpages for a subject you type,
330 make -a one of the switches, if your `man' program supports it.")
331
332 (defvar Man-specified-section-option
333 (if (string-match "-solaris[0-9.]*$" system-configuration)
334 "-s"
335 "")
336 "Option that indicates a specified a manual section name.")
337
338 (defvar Man-support-local-filenames 'auto-detect
339 "Internal cache for the value of the function `Man-support-local-filenames'.
340 `auto-detect' means the value is not yet determined.
341 Otherwise, the value is whatever the function
342 `Man-support-local-filenames' should return.")
343
344 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
345 ;; end user variables
346 \f
347 ;; other variables and keymap initializations
348 (make-variable-buffer-local 'Man-sections-alist)
349 (make-variable-buffer-local 'Man-refpages-alist)
350 (make-variable-buffer-local 'Man-page-list)
351 (make-variable-buffer-local 'Man-current-page)
352 (make-variable-buffer-local 'Man-page-mode-string)
353 (make-variable-buffer-local 'Man-original-frame)
354 (make-variable-buffer-local 'Man-arguments)
355
356 (setq-default Man-sections-alist nil)
357 (setq-default Man-refpages-alist nil)
358 (setq-default Man-page-list nil)
359 (setq-default Man-current-page 0)
360 (setq-default Man-page-mode-string "1 of 1")
361
362 (defconst Man-sysv-sed-script "\
363 /\b/ { s/_\b//g
364 s/\b_//g
365 s/o\b+/o/g
366 s/+\bo/o/g
367 :ovstrk
368 s/\\(.\\)\b\\1/\\1/g
369 t ovstrk
370 }
371 /\e\\[[0-9][0-9]*m/ s///g"
372 "Script for sysV-like sed to nuke backspaces and ANSI codes from manpages.")
373
374 (defconst Man-berkeley-sed-script "\
375 /\b/ { s/_\b//g\\
376 s/\b_//g\\
377 s/o\b+/o/g\\
378 s/+\bo/o/g\\
379 :ovstrk\\
380 s/\\(.\\)\b\\1/\\1/g\\
381 t ovstrk\\
382 }\\
383 /\e\\[[0-9][0-9]*m/ s///g"
384 "Script for berkeley-like sed to nuke backspaces and ANSI codes from manpages.")
385
386 (defvar man-mode-syntax-table
387 (let ((table (copy-syntax-table (standard-syntax-table))))
388 (modify-syntax-entry ?. "w" table)
389 (modify-syntax-entry ?_ "w" table)
390 (modify-syntax-entry ?: "w" table) ; for PDL::Primitive in Perl man pages
391 table)
392 "Syntax table used in Man mode buffers.")
393
394 (unless Man-mode-map
395 (setq Man-mode-map (make-sparse-keymap))
396 (suppress-keymap Man-mode-map)
397 (set-keymap-parent Man-mode-map button-buffer-map)
398
399 (define-key Man-mode-map " " 'scroll-up)
400 (define-key Man-mode-map "\177" 'scroll-down)
401 (define-key Man-mode-map "n" 'Man-next-section)
402 (define-key Man-mode-map "p" 'Man-previous-section)
403 (define-key Man-mode-map "\en" 'Man-next-manpage)
404 (define-key Man-mode-map "\ep" 'Man-previous-manpage)
405 (define-key Man-mode-map ">" 'end-of-buffer)
406 (define-key Man-mode-map "<" 'beginning-of-buffer)
407 (define-key Man-mode-map "." 'beginning-of-buffer)
408 (define-key Man-mode-map "r" 'Man-follow-manual-reference)
409 (define-key Man-mode-map "g" 'Man-goto-section)
410 (define-key Man-mode-map "s" 'Man-goto-see-also-section)
411 (define-key Man-mode-map "k" 'Man-kill)
412 (define-key Man-mode-map "q" 'Man-quit)
413 (define-key Man-mode-map "m" 'man)
414 (define-key Man-mode-map "?" 'describe-mode))
415
416 ;; buttons
417 (define-button-type 'Man-xref-man-page
418 'action (lambda (button) (man-follow (button-label button)))
419 'follow-link t
420 'help-echo "mouse-2, RET: display this man page")
421
422 (define-button-type 'Man-xref-header-file
423 'action (lambda (button)
424 (let ((w (button-get button 'Man-target-string)))
425 (unless (Man-view-header-file w)
426 (error "Cannot find header file: %s" w))))
427 'follow-link t
428 'help-echo "mouse-2: display this header file")
429
430 (define-button-type 'Man-xref-normal-file
431 'action (lambda (button)
432 (let ((f (substitute-in-file-name
433 (button-get button 'Man-target-string))))
434 (if (file-exists-p f)
435 (if (file-readable-p f)
436 (view-file f)
437 (error "Cannot read a file: %s" f))
438 (error "Cannot find a file: %s" f))))
439 'follow-link t
440 'help-echo "mouse-2: display this file")
441
442 \f
443 ;; ======================================================================
444 ;; utilities
445
446 (defun Man-init-defvars ()
447 "Used for initialising variables based on display's color support.
448 This is necessary if one wants to dump man.el with Emacs."
449
450 ;; Avoid possible error in call-process by using a directory that must exist.
451 (let ((default-directory "/"))
452 (setq Man-sed-script
453 (cond
454 (Man-fontify-manpage-flag
455 nil)
456 ((eq 0 (call-process Man-sed-command nil nil nil Man-sysv-sed-script))
457 Man-sysv-sed-script)
458 ((eq 0 (call-process Man-sed-command nil nil nil Man-berkeley-sed-script))
459 Man-berkeley-sed-script)
460 (t
461 nil))))
462
463 (setq Man-filter-list
464 ;; Avoid trailing nil which confuses customize.
465 (apply 'list
466 (cons
467 Man-sed-command
468 (list
469 (if Man-sed-script
470 (concat "-e '" Man-sed-script "'")
471 "")
472 "-e '/^[\001-\032][\001-\032]*$/d'"
473 "-e '/\e[789]/s///g'"
474 "-e '/Reformatting page. Wait/d'"
475 "-e '/Reformatting entry. Wait/d'"
476 "-e '/^[ \t]*Hewlett-Packard[ \t]Company[ \t]*-[ \t][0-9]*[ \t]-/d'"
477 "-e '/^[ \t]*Hewlett-Packard[ \t]*-[ \t][0-9]*[ \t]-.*$/d'"
478 "-e '/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d'"
479 "-e '/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d'"
480 "-e '/^Printed[ \t][0-9].*[0-9]$/d'"
481 "-e '/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d'"
482 "-e '/^[A-Za-z].*Last[ \t]change:/d'"
483 "-e '/^Sun[ \t]Release[ \t][0-9].*[0-9]$/d'"
484 "-e '/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d'"
485 "-e '/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d'"
486 ))
487 (cons
488 Man-awk-command
489 (list
490 "'\n"
491 "BEGIN { blankline=0; anonblank=0; }\n"
492 "/^$/ { if (anonblank==0) next; }\n"
493 "{ anonblank=1; }\n"
494 "/^$/ { blankline++; next; }\n"
495 "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }\n"
496 "'"
497 ))
498 (if (not Man-uses-untabify-flag)
499 ;; The outer list will be stripped off by apply.
500 (list (cons
501 Man-untabify-command
502 Man-untabify-command-args))
503 )))
504 )
505
506 (defsubst Man-make-page-mode-string ()
507 "Formats part of the mode line for Man mode."
508 (format "%s page %d of %d"
509 (or (nth 2 (nth (1- Man-current-page) Man-page-list))
510 "")
511 Man-current-page
512 (length Man-page-list)))
513
514 (defsubst Man-build-man-command ()
515 "Builds the entire background manpage and cleaning command."
516 (let ((command (concat manual-program " " Man-switches
517 (cond
518 ;; Already has %s
519 ((string-match "%s" manual-program) "")
520 ;; Stock MS-DOS shells cannot redirect stderr;
521 ;; `call-process' below sends it to /dev/null,
522 ;; so we don't need `2>' even with DOS shells
523 ;; which do support stderr redirection.
524 ((not (fboundp 'start-process)) " %s")
525 ((concat " %s 2>" null-device)))))
526 (flist Man-filter-list))
527 (while (and flist (car flist))
528 (let ((pcom (car (car flist)))
529 (pargs (cdr (car flist))))
530 (setq command
531 (concat command " | " pcom " "
532 (mapconcat (lambda (phrase)
533 (if (not (stringp phrase))
534 (error "Malformed Man-filter-list"))
535 phrase)
536 pargs " ")))
537 (setq flist (cdr flist))))
538 command))
539
540
541 (defun Man-translate-cleanup (string)
542 "Strip leading, trailing and middle spaces."
543 (when (stringp string)
544 ;; Strip leading and trailing
545 (if (string-match "^[ \t\f\r\n]*\\(.+[^ \t\f\r\n]\\)" string)
546 (setq string (match-string 1 string)))
547 ;; middle spaces
548 (setq string (replace-regexp-in-string "[\t\r\n]" " " string))
549 (setq string (replace-regexp-in-string " +" " " string))
550 string))
551
552 (defun Man-translate-references (ref)
553 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style.
554 Leave it as is if already in that style. Possibly downcase and
555 translate the section (see the Man-downcase-section-letters-flag
556 and the Man-section-translations-alist variables)."
557 (let ((name "")
558 (section "")
559 (slist Man-section-translations-alist))
560 (setq ref (Man-translate-cleanup ref))
561 (cond
562 ;; "chmod(2V)" case ?
563 ((string-match (concat "^" Man-reference-regexp "$") ref)
564 (setq name (match-string 1 ref)
565 section (match-string 2 ref)))
566 ;; "2v chmod" case ?
567 ((string-match (concat "^\\(" Man-section-regexp
568 "\\) +\\(" Man-name-regexp "\\)$") ref)
569 (setq name (match-string 2 ref)
570 section (match-string 1 ref))))
571 (if (string= name "")
572 ref ; Return the reference as is
573 (if Man-downcase-section-letters-flag
574 (setq section (downcase section)))
575 (while slist
576 (let ((s1 (car (car slist)))
577 (s2 (cdr (car slist))))
578 (setq slist (cdr slist))
579 (if Man-downcase-section-letters-flag
580 (setq s1 (downcase s1)))
581 (if (not (string= s1 section)) nil
582 (setq section (if Man-downcase-section-letters-flag
583 (downcase s2)
584 s2)
585 slist nil))))
586 (concat Man-specified-section-option section " " name))))
587
588 (defun Man-support-local-filenames ()
589 "Check the availability of `-l' option of the man command.
590 This option allows `man' to interpret command line arguments
591 as local filenames.
592 Return the value of the variable `Man-support-local-filenames'
593 if it was set to nil or t before the call of this function.
594 If t, the man command supports `-l' option. If nil, it don't.
595 Otherwise, if the value of `Man-support-local-filenames'
596 is neither t nor nil, then determine a new value, set it
597 to the variable `Man-support-local-filenames' and return
598 a new value."
599 (if (or (not Man-support-local-filenames)
600 (eq Man-support-local-filenames t))
601 Man-support-local-filenames
602 (setq Man-support-local-filenames
603 (with-temp-buffer
604 (and (equal (condition-case nil
605 (call-process manual-program nil t nil "--help")
606 (error nil))
607 0)
608 (progn
609 (goto-char (point-min))
610 (search-forward "--local-file" nil t))
611 t)))))
612
613 \f
614 ;; ======================================================================
615 ;; default man entry: get word under point
616
617 (defsubst Man-default-man-entry ()
618 "Make a guess at a default manual entry.
619 This guess is based on the text surrounding the cursor."
620 (let (word)
621 (save-excursion
622 ;; Default man entry title is any word the cursor is on, or if
623 ;; cursor not on a word, then nearest preceding word.
624 (skip-chars-backward "-a-zA-Z0-9._+:")
625 (let ((start (point)))
626 (skip-chars-forward "-a-zA-Z0-9._+:")
627 (setq word (buffer-substring-no-properties start (point))))
628 (if (string-match "[._]+$" word)
629 (setq word (substring word 0 (match-beginning 0))))
630 ;; If looking at something like *strcat(... , remove the '*'
631 (if (string-match "^*" word)
632 (setq word (substring word 1)))
633 ;; If looking at something like ioctl(2) or brc(1M), include the
634 ;; section number in the returned value. Remove text properties.
635 (concat word
636 (if (looking-at
637 (concat "[ \t]*([ \t]*\\(" Man-section-regexp "\\)[ \t]*)"))
638 (format "(%s)" (match-string-no-properties 1)))))))
639
640 \f
641 ;; ======================================================================
642 ;; Top level command and background process sentinel
643
644 ;; For compatibility with older versions.
645 ;;;###autoload
646 (defalias 'manual-entry 'man)
647
648
649 ;;;###autoload
650 (defun man (man-args)
651 "Get a Un*x manual page and put it in a buffer.
652 This command is the top-level command in the man package. It runs a Un*x
653 command to retrieve and clean a manpage in the background and places the
654 results in a Man mode (manpage browsing) buffer. See variable
655 `Man-notify-method' for what happens when the buffer is ready.
656 If a buffer already exists for this man page, it will display immediately.
657
658 To specify a man page from a certain section, type SUBJECT(SECTION) or
659 SECTION SUBJECT when prompted for a manual entry. To see manpages from
660 all sections related to a subject, put something appropriate into the
661 `Man-switches' variable, which see."
662 (interactive
663 (list (let* ((default-entry (Man-default-man-entry))
664 (input (read-string
665 (format "Manual entry%s: "
666 (if (string= default-entry "")
667 ""
668 (format " (default %s)" default-entry)))
669 nil nil default-entry)))
670 (if (string= input "")
671 (error "No man args given")
672 input))))
673
674 ;; Possibly translate the "subject(section)" syntax into the
675 ;; "section subject" syntax and possibly downcase the section.
676 (setq man-args (Man-translate-references man-args))
677
678 (Man-getpage-in-background man-args))
679
680 ;;;###autoload
681 (defun man-follow (man-args)
682 "Get a Un*x manual page of the item under point and put it in a buffer."
683 (interactive (list (Man-default-man-entry)))
684 (if (or (not man-args)
685 (string= man-args ""))
686 (error "No item under point")
687 (man man-args)))
688
689 (defun Man-getpage-in-background (topic)
690 "Use TOPIC to build and fire off the manpage and cleaning command."
691 (let* ((man-args topic)
692 (bufname (concat "*Man " man-args "*"))
693 (buffer (get-buffer bufname)))
694 (if buffer
695 (Man-notify-when-ready buffer)
696 (require 'env)
697 (message "Invoking %s %s in the background" manual-program man-args)
698 (setq buffer (generate-new-buffer bufname))
699 (save-excursion
700 (set-buffer buffer)
701 (setq buffer-undo-list t)
702 (setq Man-original-frame (selected-frame))
703 (setq Man-arguments man-args))
704 (let ((process-environment (copy-sequence process-environment))
705 ;; The following is so Awk script gets \n intact
706 ;; But don't prevent decoding of the outside.
707 (coding-system-for-write 'raw-text-unix)
708 ;; We must decode the output by a coding system that the
709 ;; system's locale suggests in multibyte mode.
710 (coding-system-for-read
711 (if default-enable-multibyte-characters
712 locale-coding-system 'raw-text-unix))
713 ;; Avoid possible error by using a directory that always exists.
714 (default-directory
715 (if (and (file-directory-p default-directory)
716 (not (find-file-name-handler default-directory
717 'file-directory-p)))
718 default-directory
719 "/")))
720 ;; Prevent any attempt to use display terminal fanciness.
721 (setenv "TERM" "dumb")
722 ;; In Debian Woody, at least, we get overlong lines under X
723 ;; unless COLUMNS or MANWIDTH is set. This isn't a problem on
724 ;; a tty. man(1) says:
725 ;; MANWIDTH
726 ;; If $MANWIDTH is set, its value is used as the line
727 ;; length for which manual pages should be formatted.
728 ;; If it is not set, manual pages will be formatted
729 ;; with a line length appropriate to the current ter-
730 ;; minal (using an ioctl(2) if available, the value of
731 ;; $COLUMNS, or falling back to 80 characters if nei-
732 ;; ther is available).
733 (if window-system
734 (unless (or (getenv "MANWIDTH") (getenv "COLUMNS"))
735 ;; This isn't strictly correct, since we don't know how
736 ;; the page will actually be displayed, but it seems
737 ;; reasonable.
738 (setenv "COLUMNS" (number-to-string
739 (cond
740 ((and (integerp Man-width) (> Man-width 0))
741 Man-width)
742 (Man-width (frame-width))
743 ((window-width)))))))
744 (setenv "GROFF_NO_SGR" "1")
745 (if (fboundp 'start-process)
746 (set-process-sentinel
747 (start-process manual-program buffer
748 (if (memq system-type '(cygwin windows-nt))
749 shell-file-name
750 "sh")
751 shell-command-switch
752 (format (Man-build-man-command) man-args))
753 'Man-bgproc-sentinel)
754 (let ((exit-status
755 (call-process shell-file-name nil (list buffer nil) nil
756 shell-command-switch
757 (format (Man-build-man-command) man-args)))
758 (msg ""))
759 (or (and (numberp exit-status)
760 (= exit-status 0))
761 (and (numberp exit-status)
762 (setq msg
763 (format "exited abnormally with code %d"
764 exit-status)))
765 (setq msg exit-status))
766 (Man-bgproc-sentinel bufname msg)))))))
767
768 (defun Man-notify-when-ready (man-buffer)
769 "Notify the user when MAN-BUFFER is ready.
770 See the variable `Man-notify-method' for the different notification behaviors."
771 (let ((saved-frame (save-excursion
772 (set-buffer man-buffer)
773 Man-original-frame)))
774 (cond
775 ((eq Man-notify-method 'newframe)
776 ;; Since we run asynchronously, perhaps while Emacs is waiting
777 ;; for input, we must not leave a different buffer current. We
778 ;; can't rely on the editor command loop to reselect the
779 ;; selected window's buffer.
780 (save-excursion
781 (let ((frame (make-frame Man-frame-parameters)))
782 (set-window-buffer (frame-selected-window frame) man-buffer)
783 (set-window-dedicated-p (frame-selected-window frame) t)
784 (or (display-multi-frame-p frame)
785 (select-frame frame)))))
786 ((eq Man-notify-method 'pushy)
787 (switch-to-buffer man-buffer))
788 ((eq Man-notify-method 'bully)
789 (and (frame-live-p saved-frame)
790 (select-frame saved-frame))
791 (pop-to-buffer man-buffer)
792 (delete-other-windows))
793 ((eq Man-notify-method 'aggressive)
794 (and (frame-live-p saved-frame)
795 (select-frame saved-frame))
796 (pop-to-buffer man-buffer))
797 ((eq Man-notify-method 'friendly)
798 (and (frame-live-p saved-frame)
799 (select-frame saved-frame))
800 (display-buffer man-buffer 'not-this-window))
801 ((eq Man-notify-method 'polite)
802 (beep)
803 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
804 ((eq Man-notify-method 'quiet)
805 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
806 ((or (eq Man-notify-method 'meek)
807 t)
808 (message ""))
809 )))
810
811 (defun Man-softhyphen-to-minus ()
812 ;; \255 is SOFT HYPHEN in Latin-N. Versions of Debian man, at
813 ;; least, emit it even when not in a Latin-N locale.
814 (unless (eq t (compare-strings "latin-" 0 nil
815 current-language-environment 0 6 t))
816 (goto-char (point-min))
817 (let ((str "\255"))
818 (if enable-multibyte-characters
819 (setq str (string-as-multibyte str)))
820 (while (search-forward str nil t) (replace-match "-")))))
821
822 (defun Man-fontify-manpage ()
823 "Convert overstriking and underlining to the correct fonts.
824 Same for the ANSI bold and normal escape sequences."
825 (interactive)
826 (message "Please wait: formatting the %s man page..." Man-arguments)
827 (goto-char (point-min))
828 ;; Fontify ANSI escapes.
829 (let ((faces nil)
830 (buffer-undo-list t)
831 (start (point)))
832 ;; http://www.isthe.com/chongo/tech/comp/ansi_escapes.html
833 ;; suggests many codes, but we only handle:
834 ;; ESC [ 00 m reset to normal display
835 ;; ESC [ 01 m bold
836 ;; ESC [ 04 m underline
837 ;; ESC [ 07 m reverse-video
838 ;; ESC [ 22 m no-bold
839 ;; ESC [ 24 m no-underline
840 ;; ESC [ 27 m no-reverse-video
841 (while (re-search-forward "\e\\[0?\\([1470]\\|2\\([247]\\)\\)m" nil t)
842 (if faces (put-text-property start (match-beginning 0) 'face
843 (if (cdr faces) faces (car faces))))
844 (setq faces
845 (cond
846 ((match-beginning 2)
847 (delq (case (char-after (match-beginning 2))
848 (?2 Man-overstrike-face)
849 (?4 Man-underline-face)
850 (?7 Man-reverse-face))
851 faces))
852 ((eq (char-after (match-beginning 1)) ?0) nil)
853 (t
854 (cons (case (char-after (match-beginning 1))
855 (?1 Man-overstrike-face)
856 (?4 Man-underline-face)
857 (?7 Man-reverse-face))
858 faces))))
859 (delete-region (match-beginning 0) (match-end 0))
860 (setq start (point))))
861 ;; Other highlighting.
862 (let ((buffer-undo-list t))
863 (if (< (buffer-size) (position-bytes (point-max)))
864 ;; Multibyte characters exist.
865 (progn
866 (goto-char (point-min))
867 (while (search-forward "__\b\b" nil t)
868 (backward-delete-char 4)
869 (put-text-property (point) (1+ (point)) 'face Man-underline-face))
870 (goto-char (point-min))
871 (while (search-forward "\b\b__" nil t)
872 (backward-delete-char 4)
873 (put-text-property (1- (point)) (point) 'face Man-underline-face))))
874 (goto-char (point-min))
875 (while (search-forward "_\b" nil t)
876 (backward-delete-char 2)
877 (put-text-property (point) (1+ (point)) 'face Man-underline-face))
878 (goto-char (point-min))
879 (while (search-forward "\b_" nil t)
880 (backward-delete-char 2)
881 (put-text-property (1- (point)) (point) 'face Man-underline-face))
882 (goto-char (point-min))
883 (while (re-search-forward "\\(.\\)\\(\b+\\1\\)+" nil t)
884 (replace-match "\\1")
885 (put-text-property (1- (point)) (point) 'face Man-overstrike-face))
886 (goto-char (point-min))
887 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t)
888 (replace-match "o")
889 (put-text-property (1- (point)) (point) 'face 'bold))
890 (goto-char (point-min))
891 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t)
892 (replace-match "+")
893 (put-text-property (1- (point)) (point) 'face 'bold))
894 (goto-char (point-min))
895 ;; Try to recognize common forms of cross references.
896 (Man-highlight-references)
897 (Man-softhyphen-to-minus)
898 (goto-char (point-min))
899 (while (re-search-forward Man-heading-regexp nil t)
900 (put-text-property (match-beginning 0)
901 (match-end 0)
902 'face Man-overstrike-face)))
903 (message "%s man page formatted" Man-arguments))
904
905 (defun Man-highlight-references ()
906 "Highlight the references on mouse-over.
907 references include items in the SEE ALSO section,
908 header file(#include <foo.h>) and files in FILES"
909 (let ((dummy 0))
910 (Man-highlight-references0
911 Man-see-also-regexp Man-reference-regexp 1 dummy
912 'Man-xref-man-page)
913 (Man-highlight-references0
914 Man-synopsis-regexp Man-header-regexp 0 2
915 'Man-xref-header-file)
916 (Man-highlight-references0
917 Man-files-regexp Man-normal-file-regexp 0 0
918 'Man-xref-normal-file)))
919
920 (defun Man-highlight-references0 (start-section regexp button-pos target-pos type)
921 ;; Based on `Man-build-references-alist'
922 (when (Man-find-section start-section)
923 (forward-line 1)
924 (let ((end (save-excursion
925 (Man-next-section 1)
926 (point))))
927 (back-to-indentation)
928 (while (re-search-forward regexp end t)
929 (make-text-button
930 (match-beginning button-pos)
931 (match-end button-pos)
932 'type type
933 'Man-target-string (match-string target-pos)
934 )))))
935
936 (defun Man-cleanup-manpage (&optional interactive)
937 "Remove overstriking and underlining from the current buffer.
938 Normally skip any jobs that should have been done by the sed script,
939 but when called interactively, do those jobs even if the sed
940 script would have done them."
941 (interactive "p")
942 (message "Please wait: cleaning up the %s man page..."
943 Man-arguments)
944 (if (or interactive (not Man-sed-script))
945 (progn
946 (goto-char (point-min))
947 (while (search-forward "_\b" nil t) (backward-delete-char 2))
948 (goto-char (point-min))
949 (while (search-forward "\b_" nil t) (backward-delete-char 2))
950 (goto-char (point-min))
951 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
952 (replace-match "\\1"))
953 (goto-char (point-min))
954 (while (re-search-forward "\e\\[[0-9]+m" nil t) (replace-match ""))
955 (goto-char (point-min))
956 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t) (replace-match "o"))
957 ))
958 (goto-char (point-min))
959 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t) (replace-match "+"))
960 (Man-softhyphen-to-minus)
961 (message "%s man page cleaned up" Man-arguments))
962
963 (defun Man-bgproc-sentinel (process msg)
964 "Manpage background process sentinel.
965 When manpage command is run asynchronously, PROCESS is the process
966 object for the manpage command; when manpage command is run
967 synchronously, PROCESS is the name of the buffer where the manpage
968 command is run. Second argument MSG is the exit message of the
969 manpage command."
970 (let ((Man-buffer (if (stringp process) (get-buffer process)
971 (process-buffer process)))
972 (delete-buff nil)
973 (err-mess nil))
974
975 (if (null (buffer-name Man-buffer)) ;; deleted buffer
976 (or (stringp process)
977 (set-process-buffer process nil))
978
979 (save-excursion
980 (set-buffer Man-buffer)
981 (let ((case-fold-search nil))
982 (goto-char (point-min))
983 (cond ((or (looking-at "No \\(manual \\)*entry for")
984 (looking-at "[^\n]*: nothing appropriate$"))
985 (setq err-mess (buffer-substring (point)
986 (progn
987 (end-of-line) (point)))
988 delete-buff t))
989 ((or (stringp process)
990 (not (and (eq (process-status process) 'exit)
991 (= (process-exit-status process) 0))))
992 (or (zerop (length msg))
993 (progn
994 (setq err-mess
995 (concat (buffer-name Man-buffer)
996 ": process "
997 (let ((eos (1- (length msg))))
998 (if (= (aref msg eos) ?\n)
999 (substring msg 0 eos) msg))))
1000 (goto-char (point-max))
1001 (insert (format "\nprocess %s" msg))))
1002 ))
1003 (if delete-buff
1004 (kill-buffer Man-buffer)
1005 (if Man-fontify-manpage-flag
1006 (Man-fontify-manpage)
1007 (Man-cleanup-manpage))
1008 (run-hooks 'Man-cooked-hook)
1009 (Man-mode)
1010 (set-buffer-modified-p nil)
1011 ))
1012 ;; Restore case-fold-search before calling
1013 ;; Man-notify-when-ready because it may switch buffers.
1014
1015 (if (not delete-buff)
1016 (Man-notify-when-ready Man-buffer))
1017
1018 (if err-mess
1019 (error err-mess))
1020 ))))
1021
1022 \f
1023 ;; ======================================================================
1024 ;; set up manual mode in buffer and build alists
1025
1026 (put 'Man-mode 'mode-class 'special)
1027
1028 (defun Man-mode ()
1029 "A mode for browsing Un*x manual pages.
1030
1031 The following man commands are available in the buffer. Try
1032 \"\\[describe-key] <key> RET\" for more information:
1033
1034 \\[man] Prompt to retrieve a new manpage.
1035 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
1036 \\[Man-next-manpage] Jump to next manpage in circular list.
1037 \\[Man-previous-manpage] Jump to previous manpage in circular list.
1038 \\[Man-next-section] Jump to next manpage section.
1039 \\[Man-previous-section] Jump to previous manpage section.
1040 \\[Man-goto-section] Go to a manpage section.
1041 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
1042 \\[Man-quit] Deletes the manpage window, bury its buffer.
1043 \\[Man-kill] Deletes the manpage window, kill its buffer.
1044 \\[describe-mode] Prints this help text.
1045
1046 The following variables may be of some use. Try
1047 \"\\[describe-variable] <variable-name> RET\" for more information:
1048
1049 `Man-notify-method' What happens when manpage formatting is done.
1050 `Man-downcase-section-letters-flag' Force section letters to lower case.
1051 `Man-circular-pages-flag' Treat multiple manpage list as circular.
1052 `Man-section-translations-alist' List of section numbers and their Un*x equiv.
1053 `Man-filter-list' Background manpage filter command.
1054 `Man-mode-map' Keymap bindings for Man mode buffers.
1055 `Man-mode-hook' Normal hook run on entry to Man mode.
1056 `Man-section-regexp' Regexp describing manpage section letters.
1057 `Man-heading-regexp' Regexp describing section headers.
1058 `Man-see-also-regexp' Regexp for SEE ALSO section (or your equiv).
1059 `Man-first-heading-regexp' Regexp for first heading on a manpage.
1060 `Man-reference-regexp' Regexp matching a references in SEE ALSO.
1061 `Man-switches' Background `man' command switches.
1062
1063 The following key bindings are currently in effect in the buffer:
1064 \\{Man-mode-map}"
1065 (interactive)
1066 (kill-all-local-variables)
1067 (setq major-mode 'Man-mode
1068 mode-name "Man"
1069 buffer-auto-save-file-name nil
1070 mode-line-buffer-identification
1071 (list (default-value 'mode-line-buffer-identification)
1072 " {" 'Man-page-mode-string "}")
1073 truncate-lines t
1074 buffer-read-only t)
1075 (buffer-disable-undo)
1076 (auto-fill-mode -1)
1077 (use-local-map Man-mode-map)
1078 (set-syntax-table man-mode-syntax-table)
1079 (setq imenu-generic-expression (list (list nil Man-heading-regexp 0)))
1080 (set (make-local-variable 'outline-regexp) Man-heading-regexp)
1081 (set (make-local-variable 'outline-level) (lambda () 1))
1082 (Man-build-page-list)
1083 (Man-strip-page-headers)
1084 (Man-unindent)
1085 (Man-goto-page 1)
1086 (run-mode-hooks 'Man-mode-hook))
1087
1088 (defsubst Man-build-section-alist ()
1089 "Build the association list of manpage sections."
1090 (setq Man-sections-alist nil)
1091 (goto-char (point-min))
1092 (let ((case-fold-search nil))
1093 (while (re-search-forward Man-heading-regexp (point-max) t)
1094 (aput 'Man-sections-alist (match-string 1))
1095 (forward-line 1))))
1096
1097 (defsubst Man-build-references-alist ()
1098 "Build the association list of references (in the SEE ALSO section)."
1099 (setq Man-refpages-alist nil)
1100 (save-excursion
1101 (if (Man-find-section Man-see-also-regexp)
1102 (let ((start (progn (forward-line 1) (point)))
1103 (end (progn
1104 (Man-next-section 1)
1105 (point)))
1106 hyphenated
1107 (runningpoint -1))
1108 (save-restriction
1109 (narrow-to-region start end)
1110 (goto-char (point-min))
1111 (back-to-indentation)
1112 (while (and (not (eobp)) (/= (point) runningpoint))
1113 (setq runningpoint (point))
1114 (if (re-search-forward Man-hyphenated-reference-regexp end t)
1115 (let* ((word (match-string 0))
1116 (len (1- (length word))))
1117 (if hyphenated
1118 (setq word (concat hyphenated word)
1119 hyphenated nil
1120 ;; Update len, in case a reference spans
1121 ;; more than two lines (paranoia).
1122 len (1- (length word))))
1123 (if (memq (aref word len) '(?- ?­))
1124 (setq hyphenated (substring word 0 len)))
1125 (if (string-match Man-reference-regexp word)
1126 (aput 'Man-refpages-alist word))))
1127 (skip-chars-forward " \t\n,"))))))
1128 (setq Man-refpages-alist (nreverse Man-refpages-alist)))
1129
1130 (defun Man-build-page-list ()
1131 "Build the list of separate manpages in the buffer."
1132 (setq Man-page-list nil)
1133 (let ((page-start (point-min))
1134 (page-end (point-max))
1135 (header ""))
1136 (goto-char page-start)
1137 ;; (switch-to-buffer (current-buffer))(debug)
1138 (while (not (eobp))
1139 (setq header
1140 (if (looking-at Man-page-header-regexp)
1141 (match-string 1)
1142 nil))
1143 ;; Go past both the current and the next Man-first-heading-regexp
1144 (if (re-search-forward Man-first-heading-regexp nil 'move 2)
1145 (let ((p (progn (beginning-of-line) (point))))
1146 ;; We assume that the page header is delimited by blank
1147 ;; lines and that it contains at most one blank line. So
1148 ;; if we back by three blank lines we will be sure to be
1149 ;; before the page header but not before the possible
1150 ;; previous page header.
1151 (search-backward "\n\n" nil t 3)
1152 (if (re-search-forward Man-page-header-regexp p 'move)
1153 (beginning-of-line))))
1154 (setq page-end (point))
1155 (setq Man-page-list (append Man-page-list
1156 (list (list (copy-marker page-start)
1157 (copy-marker page-end)
1158 header))))
1159 (setq page-start page-end)
1160 )))
1161
1162 (defun Man-strip-page-headers ()
1163 "Strip all the page headers but the first from the manpage."
1164 (let ((buffer-read-only nil)
1165 (case-fold-search nil)
1166 (page-list Man-page-list)
1167 (page ())
1168 (header ""))
1169 (while page-list
1170 (setq page (car page-list))
1171 (and (nth 2 page)
1172 (goto-char (car page))
1173 (re-search-forward Man-first-heading-regexp nil t)
1174 (setq header (buffer-substring (car page) (match-beginning 0)))
1175 ;; Since the awk script collapses all successive blank
1176 ;; lines into one, and since we don't want to get rid of
1177 ;; the fast awk script, one must choose between adding
1178 ;; spare blank lines between pages when there were none and
1179 ;; deleting blank lines at page boundaries when there were
1180 ;; some. We choose the first, so we comment the following
1181 ;; line.
1182 ;; (setq header (concat "\n" header)))
1183 (while (search-forward header (nth 1 page) t)
1184 (replace-match "")))
1185 (setq page-list (cdr page-list)))))
1186
1187 (defun Man-unindent ()
1188 "Delete the leading spaces that indent the manpage."
1189 (let ((buffer-read-only nil)
1190 (case-fold-search nil)
1191 (page-list Man-page-list))
1192 (while page-list
1193 (let ((page (car page-list))
1194 (indent "")
1195 (nindent 0))
1196 (narrow-to-region (car page) (car (cdr page)))
1197 (if Man-uses-untabify-flag
1198 (untabify (point-min) (point-max)))
1199 (if (catch 'unindent
1200 (goto-char (point-min))
1201 (if (not (re-search-forward Man-first-heading-regexp nil t))
1202 (throw 'unindent nil))
1203 (beginning-of-line)
1204 (setq indent (buffer-substring (point)
1205 (progn
1206 (skip-chars-forward " ")
1207 (point))))
1208 (setq nindent (length indent))
1209 (if (zerop nindent)
1210 (throw 'unindent nil))
1211 (setq indent (concat indent "\\|$"))
1212 (goto-char (point-min))
1213 (while (not (eobp))
1214 (if (looking-at indent)
1215 (forward-line 1)
1216 (throw 'unindent nil)))
1217 (goto-char (point-min)))
1218 (while (not (eobp))
1219 (or (eolp)
1220 (delete-char nindent))
1221 (forward-line 1)))
1222 (setq page-list (cdr page-list))
1223 ))))
1224
1225 \f
1226 ;; ======================================================================
1227 ;; Man mode commands
1228
1229 (defun Man-next-section (n)
1230 "Move point to Nth next section (default 1)."
1231 (interactive "p")
1232 (let ((case-fold-search nil))
1233 (if (looking-at Man-heading-regexp)
1234 (forward-line 1))
1235 (if (re-search-forward Man-heading-regexp (point-max) t n)
1236 (beginning-of-line)
1237 (goto-char (point-max)))))
1238
1239 (defun Man-previous-section (n)
1240 "Move point to Nth previous section (default 1)."
1241 (interactive "p")
1242 (let ((case-fold-search nil))
1243 (if (looking-at Man-heading-regexp)
1244 (forward-line -1))
1245 (if (re-search-backward Man-heading-regexp (point-min) t n)
1246 (beginning-of-line)
1247 (goto-char (point-min)))))
1248
1249 (defun Man-find-section (section)
1250 "Move point to SECTION if it exists, otherwise don't move point.
1251 Returns t if section is found, nil otherwise."
1252 (let ((curpos (point))
1253 (case-fold-search nil))
1254 (goto-char (point-min))
1255 (if (re-search-forward (concat "^" section) (point-max) t)
1256 (progn (beginning-of-line) t)
1257 (goto-char curpos)
1258 nil)
1259 ))
1260
1261 (defun Man-goto-section ()
1262 "Query for section to move point to."
1263 (interactive)
1264 (aput 'Man-sections-alist
1265 (let* ((default (aheadsym Man-sections-alist))
1266 (completion-ignore-case t)
1267 chosen
1268 (prompt (concat "Go to section: (default " default ") ")))
1269 (setq chosen (completing-read prompt Man-sections-alist))
1270 (if (or (not chosen)
1271 (string= chosen ""))
1272 default
1273 chosen)))
1274 (Man-find-section (aheadsym Man-sections-alist)))
1275
1276 (defun Man-goto-see-also-section ()
1277 "Move point to the \"SEE ALSO\" section.
1278 Actually the section moved to is described by `Man-see-also-regexp'."
1279 (interactive)
1280 (if (not (Man-find-section Man-see-also-regexp))
1281 (error (concat "No " Man-see-also-regexp
1282 " section found in the current manpage"))))
1283
1284 (defun Man-possibly-hyphenated-word ()
1285 "Return a possibly hyphenated word at point.
1286 If the word starts at the first non-whitespace column, and the
1287 previous line ends with a hyphen, return the last word on the previous
1288 line instead. Thus, if a reference to \"tcgetpgrp(3V)\" is hyphenated
1289 as \"tcgetp-grp(3V)\", and point is at \"grp(3V)\", we return
1290 \"tcgetp-\" instead of \"grp\"."
1291 (save-excursion
1292 (skip-syntax-backward "w()")
1293 (skip-chars-forward " \t")
1294 (let ((beg (point))
1295 (word (current-word)))
1296 (when (eq beg (save-excursion
1297 (back-to-indentation)
1298 (point)))
1299 (end-of-line 0)
1300 (if (eq (char-before) ?-)
1301 (setq word (current-word))))
1302 word)))
1303
1304 (defun Man-follow-manual-reference (reference)
1305 "Get one of the manpages referred to in the \"SEE ALSO\" section.
1306 Specify which REFERENCE to use; default is based on word at point."
1307 (interactive
1308 (if (not Man-refpages-alist)
1309 (error "There are no references in the current man page")
1310 (list (let* ((default (or
1311 (car (all-completions
1312 (let ((word
1313 (or (Man-possibly-hyphenated-word)
1314 "")))
1315 ;; strip a trailing '-':
1316 (if (string-match "-$" word)
1317 (substring word 0
1318 (match-beginning 0))
1319 word))
1320 Man-refpages-alist))
1321 (aheadsym Man-refpages-alist)))
1322 chosen
1323 (prompt (concat "Refer to: (default " default ") ")))
1324 (setq chosen (completing-read prompt Man-refpages-alist))
1325 (if (or (not chosen)
1326 (string= chosen ""))
1327 default
1328 chosen)))))
1329 (if (not Man-refpages-alist)
1330 (error "Can't find any references in the current manpage")
1331 (aput 'Man-refpages-alist reference)
1332 (Man-getpage-in-background
1333 (Man-translate-references (aheadsym Man-refpages-alist)))))
1334
1335 (defun Man-kill ()
1336 "Kill the buffer containing the manpage."
1337 (interactive)
1338 (quit-window t))
1339
1340 (defun Man-quit ()
1341 "Bury the buffer containing the manpage."
1342 (interactive)
1343 (quit-window))
1344
1345 (defun Man-goto-page (page)
1346 "Go to the manual page on page PAGE."
1347 (interactive
1348 (if (not Man-page-list)
1349 (let ((args Man-arguments))
1350 (kill-buffer (current-buffer))
1351 (error "Can't find the %s manpage" args))
1352 (if (= (length Man-page-list) 1)
1353 (error "You're looking at the only manpage in the buffer")
1354 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1355 (length Man-page-list)))))))
1356 (if (not Man-page-list)
1357 (let ((args Man-arguments))
1358 (kill-buffer (current-buffer))
1359 (error "Can't find the %s manpage" args)))
1360 (if (or (< page 1)
1361 (> page (length Man-page-list)))
1362 (error "No manpage %d found" page))
1363 (let* ((page-range (nth (1- page) Man-page-list))
1364 (page-start (car page-range))
1365 (page-end (car (cdr page-range))))
1366 (setq Man-current-page page
1367 Man-page-mode-string (Man-make-page-mode-string))
1368 (widen)
1369 (goto-char page-start)
1370 (narrow-to-region page-start page-end)
1371 (Man-build-section-alist)
1372 (Man-build-references-alist)
1373 (goto-char (point-min))))
1374
1375
1376 (defun Man-next-manpage ()
1377 "Find the next manpage entry in the buffer."
1378 (interactive)
1379 (if (= (length Man-page-list) 1)
1380 (error "This is the only manpage in the buffer"))
1381 (if (< Man-current-page (length Man-page-list))
1382 (Man-goto-page (1+ Man-current-page))
1383 (if Man-circular-pages-flag
1384 (Man-goto-page 1)
1385 (error "You're looking at the last manpage in the buffer"))))
1386
1387 (defun Man-previous-manpage ()
1388 "Find the previous manpage entry in the buffer."
1389 (interactive)
1390 (if (= (length Man-page-list) 1)
1391 (error "This is the only manpage in the buffer"))
1392 (if (> Man-current-page 1)
1393 (Man-goto-page (1- Man-current-page))
1394 (if Man-circular-pages-flag
1395 (Man-goto-page (length Man-page-list))
1396 (error "You're looking at the first manpage in the buffer"))))
1397
1398 ;; Header file support
1399 (defun Man-view-header-file (file)
1400 "View a header file specified by FILE from `Man-header-file-path'."
1401 (let ((path Man-header-file-path)
1402 complete-path)
1403 (while path
1404 (setq complete-path (concat (car path) "/" file)
1405 path (cdr path))
1406 (if (file-readable-p complete-path)
1407 (progn (view-file complete-path)
1408 (setq path nil))
1409 (setq complete-path nil)))
1410 complete-path))
1411 \f
1412 ;; Init the man package variables, if not already done.
1413 (Man-init-defvars)
1414
1415 (add-to-list 'debug-ignored-errors "^No manpage [0-9]* found$")
1416 (add-to-list 'debug-ignored-errors "^Can't find the .* manpage$")
1417
1418 (provide 'man)
1419
1420 ;; arch-tag: 587cda76-8e23-4594-b1f3-89b6b09a0d47
1421 ;;; man.el ends here