]> code.delx.au - gnu-emacs/blob - lisp/man.el
(Man-specified-section-option): Test system-configuration.
[gnu-emacs] / lisp / man.el
1 ;;; man.el --- browse UNIX manual pages
2
3 ;; Copyright (C) 1993 Free Software Foundation, Inc.
4
5 ;; Author: Barry A. Warsaw <bwarsaw@cen.com>
6 ;; Last-Modified: 31-Jul-1991
7 ;; Version: 1.1
8 ;; Keywords: help
9 ;; Adapted-By: ESR
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
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26
27 ;;; Commentary:
28
29 ;; This code provides a function, manual-entry, with which you can
30 ;; browse UNIX manual pages. Formatting is done in background so that
31 ;; you can continue to use your Emacs while processing is going on.
32 ;;
33 ;; The mode also supports hypertext-like following of manual page SEE
34 ;; ALSO references, and other features. See below or do `?' in a
35 ;; manual page buffer for details.
36
37 ;; ========== Credits and History ==========
38 ;; In mid 1991, several people posted some interesting improvements to
39 ;; man.el from the standard emacs 18.57 distribution. I liked many of
40 ;; these, but wanted everthing in one single package, so I decided
41 ;; to encorporate them into a single manual browsing mode. While
42 ;; much of the code here has been rewritten, and some features added,
43 ;; these folks deserve lots of credit for providing the initial
44 ;; excellent packages on which this one is based.
45
46 ;; Nick Duffek <duffek@chaos.cs.brandeis.edu>, posted a very nice
47 ;; improvement which retrieved and cleaned the manpages in a
48 ;; background process, and which correctly deciphered such options as
49 ;; man -k.
50
51 ;; Eric Rose <erose@jessica.stanford.edu>, submitted manual.el which
52 ;; provided a very nice manual browsing mode.
53
54 ;; This package was available as `superman.el' from the LCD package
55 ;; for some time before it was accepted into Emacs 19. The entry
56 ;; point and some other names have been changed to make it a drop-in
57 ;; replacement for the old man.el package.
58
59 ;; ========== Features ==========
60 ;; + Runs "man" in the background and pipes the results through a
61 ;; series of sed and awk scripts so that all retrieving and cleaning
62 ;; is done in the background. The cleaning commands are configurable.
63 ;; + Syntax is the same as Un*x man
64 ;; + Functionality is the same as Un*x man, including "man -k" and
65 ;; "man <section>", etc.
66 ;; + Provides a manual browsing mode with keybindings for traversing
67 ;; the sections of a manpage, following references in the SEE ALSO
68 ;; section, and more.
69 ;; + Multiple manpages created with the same man command are put into
70 ;; a narrowed buffer circular list.
71
72 ;;; Code:
73
74 (require 'assoc)
75
76 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
77 ;; user variables
78
79 (defvar Man-notify 'friendly
80 "*Selects the behavior when manpage is ready.
81 This variable may have one of the following values:
82
83 newframe -- put the manpage in its own frame (see `Man-frame-parameters')
84 bully -- make the manpage the current buffer and only window
85 aggressive -- make the manpage the current buffer in the other window
86 friendly -- display manpage in other window but don't make current
87 polite -- don't display manpage, but prints message when ready (beeps)
88 quiet -- like `polite', but don't beep
89 meek -- make no indication that manpage is ready
90
91 Any other value of `Man-notify' is equivalent to `meek'.")
92
93 (defvar Man-frame-parameters nil
94 "*Frame parameter list for creating a new frame for a manual page.")
95
96 (defvar Man-reuse-okay-p t
97 "*Reuse a manpage buffer if possible.
98 When t, and a manpage buffer already exists with the same invocation,
99 man just indicates the manpage is ready according to the value of
100 `Man-notify'. When nil, it always fires off a background process, putting
101 the results in a uniquely named buffer.")
102
103 (defvar Man-downcase-section-letters-p t
104 "*Letters in sections are converted to lower case.
105 Some Un*x man commands can't handle uppercase letters in sections, for
106 example \"man 2V chmod\", but they are often displayed in the manpage
107 with the upper case letter. When this variable is t, the section
108 letter (e.g., \"2V\") is converted to lowercase (e.g., \"2v\") before
109 being sent to the man background process.")
110
111 (defvar Man-circular-pages-p t
112 "*If t, the manpage list is treated as circular for traversal.")
113
114 ;; I changed this to nil because it is a bad idea
115 ;; to fail to recognize things in other sections.
116 (defvar Man-auto-section-alist
117 nil
118 ;; '((c-mode . ("2" "3"))
119 ;; (c++-mode . ("2" "3"))
120 ;; (c++-c-mode . ("2" "3"))
121 ;; (shell-mode . ("1" "8"))
122 ;; (cmushell-mode . ("1" "8"))
123 ;; (text-mode . "1")
124 ;; )
125 "*Association list of major modes and their default section numbers.
126 List is of the form: (MAJOR-MODE . [SECTION | (SECTION*)]). If current
127 major mode is not in list, then the default is to check for manpages
128 in all sections.")
129
130 (defvar Man-section-translations-alist
131 '(("3C++" . "3")
132 ("3X" . "3") ; Xlib man pages
133 ("3X11" . "3")
134 ("1-UCB" . ""))
135 "*Association list of bogus sections to real section numbers.
136 Some manpages (e.g. the Sun C++ 2.1 manpages) have section numbers in
137 their references which Un*x `man' does not recognize. This
138 association list is used to translate those sections, when found, to
139 the associated section number.")
140
141 (defvar Man-filter-list
142 '(("sed "
143 (;;"-e 's/.\010//g'"
144 "-e '/[Nn]o such file or directory/d'"
145 "-e '/Reformatting page. Wait... done/d'"
146 "-e '/^\\([A-Z][A-Z.]*([0-9A-Za-z][-0-9A-Za-z+]*)\\).*\\1$/d'"
147 "-e '/^[ \\t]*Hewlett-Packard Company[ \\t]*- [0-9]* -.*$/d'"
148 "-e '/^[ \\t]*Hewlett-Packard[ \\t]*- [0-9]* -.*$/d'"
149 "-e '/^ *Page [0-9]*.*(printed [0-9\\/]*)$/d'"
150 "-e '/^Printed [0-9].*[0-9]$/d'"
151 "-e '/^[ \\t]*X Version 1[01].*Release [0-9]/d'"
152 "-e '/^[A-za-z].*Last change:/d'"
153 "-e '/^Sun Release [0-9].*[0-9]$/d'"
154 "-e '/^\\n$/D'"
155 ))
156 ("awk '\n"
157 ("BEGIN { blankline=0; anonblank=0; }\n"
158 "/^$/ { if (anonblank==0) next; }\n"
159 "{ anonblank=1; }\n"
160 "/^$/ { blankline++; next; }\n"
161 "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }\n"
162 "'"
163 ))
164 )
165 "*Manpage cleaning filter command phrases.
166 This variable contains an association list of the following form:
167
168 '((command-string (phrase-string*))*)
169
170 Each phrase-string is concatenated onto the command-string to form a
171 command filter. The (standard) output (and standard error) of the Un*x
172 man command is piped through each command filter in the order the
173 commands appear in the association list. The final output is placed in
174 the manpage buffer.")
175
176 (defvar Man-mode-line-format
177 '("" mode-line-modified
178 mode-line-buffer-identification " "
179 global-mode-string
180 " " Man-page-mode-string
181 " %[(" mode-name minor-mode-alist mode-line-process ")%]----"
182 (-3 . "%p") "-%-")
183 "*Mode line format for manual mode buffer.")
184
185 (defvar Man-mode-map nil
186 "*Keymap for Man mode.")
187
188 (defvar Man-mode-hook nil
189 "*Normal hook run when Man mode is enabled.")
190
191 (defvar Man-section-regexp "[0-9][a-zA-Z+]*\\|[LNln]"
192 "*Regular expression describing a manpage section within parentheses.")
193
194 (defvar Man-heading-regexp "^ ?[A-Z]"
195 "*Regular expression describing a manpage heading entry.")
196
197 (defvar Man-see-also-regexp "SEE ALSO"
198 "*Regular expression for SEE ALSO heading (or your equivalent).
199 This regexp should not start with a `^' character.")
200
201 (defvar Man-first-heading-regexp "^ ?NAME$\\|^ ?No manual entry for .*$"
202 "*Regular expression describing first heading on a manpage.
203 This regular expression should start with a `^' character.")
204
205 (defvar Man-reference-regexp
206 "[-a-zA-Z0-9_][-a-zA-Z0-9_.]*\\(([0-9][a-zA-Z+]*)\\)?"
207 "*Regular expression describing a reference in the SEE ALSO section.")
208
209 (defvar Man-switches ""
210 "*Switches passed to the man command, as a single string.")
211
212 ;; Would someone like to provide a good test for being on Solaris?
213 ;; We could give it its own value of system-type, but that has drawbacks;
214 ;; it would require changes in lots of places that test system-type.
215 (defvar Man-specified-section-option
216 (if (string-match "-solaris[0-9.]*$" system-configuration)
217 "-s"
218 "")
219 "*Option that indicates a specified a manual section name.")
220
221 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
222 ;; end user variables
223 \f
224 ;; other variables and keymap initializations
225 (make-variable-buffer-local 'Man-sections-alist)
226 (make-variable-buffer-local 'Man-refpages-alist)
227 (make-variable-buffer-local 'Man-page-list)
228 (make-variable-buffer-local 'Man-current-page)
229 (make-variable-buffer-local 'Man-page-mode-string)
230
231 (setq-default Man-sections-alist nil)
232 (setq-default Man-refpages-alist nil)
233 (setq-default Man-page-list nil)
234 (setq-default Man-current-page 0)
235 (setq-default Man-page-mode-string "1 (of 1)")
236
237 (if Man-mode-map
238 nil
239 (setq Man-mode-map (make-keymap))
240 (suppress-keymap Man-mode-map)
241 (define-key Man-mode-map " " 'scroll-up)
242 (define-key Man-mode-map "\177" 'scroll-down)
243 (define-key Man-mode-map "n" 'Man-next-section)
244 (define-key Man-mode-map "p" 'Man-previous-section)
245 (define-key Man-mode-map "\en" 'Man-next-manpage)
246 (define-key Man-mode-map "\ep" 'Man-previous-manpage)
247 (define-key Man-mode-map "," 'beginning-of-buffer)
248 (define-key Man-mode-map "." 'end-of-buffer)
249 (define-key Man-mode-map "r" 'Man-follow-manual-reference)
250 (define-key Man-mode-map "t" 'toggle-truncate-lines)
251 (define-key Man-mode-map "g" 'Man-goto-section)
252 (define-key Man-mode-map "s" 'Man-goto-see-also-section)
253 (define-key Man-mode-map "q" 'Man-quit)
254 (define-key Man-mode-map "m" 'manual-entry)
255 (define-key Man-mode-map "?" 'describe-mode)
256 )
257
258 \f
259 ;; ======================================================================
260 ;; utilities
261
262 (defun Man-page-mode-string ()
263 "Formats part of the mode line for Man mode."
264 (format "%d (of %d)" Man-current-page (length Man-page-list)))
265
266 (defun Man-delete-trailing-newline (str)
267 (if (string= (substring str (1- (length str))) "\n")
268 (substring str 0 (1- (length str)))
269 str))
270
271 (defun Man-build-man-command ()
272 "Builds the entire background manpage and cleaning command."
273 (let ((command (concat "man " Man-switches " %s 2>&1"))
274 (flist Man-filter-list))
275 (while flist
276 (let ((pcom (car (car flist)))
277 (pargs (car (cdr (car flist)))))
278 (setq flist (cdr flist))
279 (if (or (not (stringp pcom))
280 (not (listp pargs)))
281 (error "malformed Man-filter-list."))
282 (setq command (concat command " | " pcom
283 (mapconcat '(lambda (phrase) phrase)
284 pargs " ")))))
285 command))
286
287 (defun Man-downcase (man-args)
288 "Downcases section letters in MAN-ARGS."
289 (let ((newargs "")
290 (s 0)
291 mstart mend
292 (len (length man-args)))
293 (while (and (< s len)
294 (setq mstart (string-match Man-section-regexp man-args s)))
295 (setq mend (match-end 0)
296 newargs (concat newargs (substring man-args s mstart)))
297 (setq newargs (concat newargs (downcase
298 (substring man-args mstart mend)))
299 s mend))
300 (concat newargs (substring man-args s len))))
301
302 (defun Man-translate-references (ref)
303 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style."
304 (if (string-match (concat "(" Man-section-regexp ")$") ref)
305 (let* ((word (progn (string-match "(" ref)
306 (substring ref 0 (1- (match-end 0)))))
307 (section-re (concat "(\\(" Man-section-regexp "\\))"))
308 (section (if (string-match section-re ref)
309 (substring ref (match-beginning 1) (match-end 1))
310 ""))
311 (slist Man-section-translations-alist)
312 )
313 (if Man-downcase-section-letters-p
314 (setq section (Man-downcase section)))
315 (while slist
316 (let ((s1 (car (car slist)))
317 (s2 (cdr (car slist))))
318 (setq slist (cdr slist))
319 (if Man-downcase-section-letters-p
320 (setq s1 (Man-downcase s1)))
321 (if (not (string= s1 section)) nil
322 (setq section (if Man-downcase-section-letters-p
323 (Man-downcase s2)
324 s2)
325 slist nil))))
326 (concat Man-specified-section-option section " " word))
327 ref))
328
329 (defun Man-linepos (&optional position col-p)
330 "Return the character position at various line/buffer positions.
331 Preserves the state of point, mark, etc. Optional arg POSITION can be one
332 of the following symbols:
333 bol == beginning of line
334 boi == beginning of indentation
335 eol == end of line [default]
336 bob == beginning of buffer
337 eob == end of buffer
338
339 Optional arg COL-P, if non-nil, means to return
340 the current column instead of character position."
341 (let ((tpnt (point))
342 rval)
343 (cond
344 ((eq position 'bol) (beginning-of-line))
345 ((eq position 'boi) (back-to-indentation))
346 ((eq position 'bob) (goto-char (point-min)))
347 ((eq position 'eob) (goto-char (point-max)))
348 (t (end-of-line)))
349 (setq rval (if col-p (current-column) (point)))
350 (goto-char tpnt)
351 rval))
352
353 \f
354 ;; ======================================================================
355 ;; default man entry and get word under point
356
357 (defun Man-default-man-args (manword)
358 "Build the default man args from MANWORD and buffer's major mode."
359 (let ((sections (cdr (assq major-mode Man-auto-section-alist))))
360 (cond
361 ((null sections) manword)
362 ((consp sections)
363 (mapconcat (lambda (n) (concat n " " manword)) sections " "))
364 (t
365 (concat sections " " manword)))))
366
367 (defun Man-default-man-entry ()
368 "Make a guess at a default manual entry.
369 This guess is based on the text surrounding the cursor, and the
370 default section number is selected from `Man-auto-section-alist'."
371 (let (default-title)
372 (save-excursion
373
374 ;; Default man entry title is any word the cursor is on,
375 ;; or if cursor not on a word, then nearest preceding
376 ;; word.
377 (and (not (looking-at "[a-zA-Z_]"))
378 (skip-chars-backward "^a-zA-Z_"))
379 (skip-chars-backward "(a-zA-Z_0-9")
380 (and (looking-at "(") (forward-char 1))
381 (setq default-title
382 (buffer-substring
383 (point)
384 (progn (skip-chars-forward "a-zA-Z0-9_") (point))))
385
386 ;; If looking at something like ioctl(2) or brc(1M), include
387 ;; section number in default-entry
388 (if (looking-at "[ \t]*([ \t]*[0-9][a-zA-Z]?[ \t]*)")
389 (progn (skip-chars-forward "^0-9")
390 (setq default-title
391 (concat (buffer-substring
392 (point)
393 (progn
394 (skip-chars-forward "0-9a-zA-Z")
395 (point)))
396 " "
397 default-title)))
398 (setq default-title (Man-default-man-args default-title)))
399 default-title)))
400
401 \f
402 ;; ======================================================================
403 ;; top level command and background process sentinel
404
405 ;;; This alias makes completion more predictable if ignoring case.
406 ;;;###autoload
407 (defalias 'man 'manual-entry)
408
409 ;;;###autoload
410 (defun manual-entry (arg)
411 "Get a Un*x manual page and put it in a buffer.
412 This command is the top-level command in the man package. It runs a Un*x
413 command to retrieve and clean a manpage in the background and places the
414 results in a Man mode (manpage browsing) buffer. See variable
415 `Man-notify' for what happens when the buffer is ready.
416 Universal argument ARG, is passed to `Man-getpage-in-background'."
417 (interactive "P")
418 (let* ((default-entry (Man-default-man-entry))
419 (man-args
420 (read-string (format "Manual-entry: %s"
421 (if (string= default-entry "") ""
422 (format "(default: %s) "
423 default-entry))))))
424 (and (string= man-args "")
425 (if (string= default-entry "")
426 (error "No man args given.")
427 (setq man-args default-entry)))
428
429 ;; Recognize the subject(section) syntax.
430 (setq man-args (Man-translate-references man-args))
431
432 ;; This is apparently already done correctly via Man-translate-references.
433 ;; (if Man-downcase-section-letters-p
434 ;; (setq man-args (Man-downcase man-args)))
435 (Man-getpage-in-background man-args (consp arg))
436 ))
437
438 (defun Man-getpage-in-background (topic &optional override-reuse-p)
439 "Uses TOPIC to build and fire off the manpage and cleaning command.
440 Optional OVERRIDE-REUSE-P, when non-nil, means to
441 start a background process even if a buffer already exists and
442 `Man-reuse-okay-p' is non-nil."
443 (let* ((man-args topic)
444 (bufname (concat "*man " man-args "*"))
445 (buffer (get-buffer bufname)))
446 (if (and Man-reuse-okay-p
447 (not override-reuse-p)
448 buffer)
449 (Man-notify-when-ready buffer)
450 (message "Invoking man %s in background..." man-args)
451 (setq buffer (generate-new-buffer bufname))
452 (let ((process-environment (copy-sequence process-environment)))
453 ;; Prevent any attempt to use display terminal fanciness.
454 (setenv "TERM" "dumb")
455 (set-process-sentinel
456 (start-process "man" buffer "sh" "-c"
457 (format (Man-build-man-command) man-args))
458 'Man-bgproc-sentinel))
459 )))
460
461 (defun Man-notify-when-ready (man-buffer)
462 "Notify the user when MAN-BUFFER is ready.
463 See the variable `Man-notify' for the different notification behaviors."
464 (cond
465 ((eq Man-notify 'newframe)
466 (set-buffer man-buffer)
467 (new-frame Man-frame-parameters))
468 ((eq Man-notify 'bully)
469 (pop-to-buffer man-buffer)
470 (delete-other-windows))
471 ((eq Man-notify 'aggressive)
472 (pop-to-buffer man-buffer))
473 ((eq Man-notify 'friendly)
474 (display-buffer man-buffer 'not-this-window))
475 ((eq Man-notify 'polite)
476 (beep)
477 (message "Manual buffer %s is ready." (buffer-name man-buffer)))
478 ((eq Man-notify 'quiet)
479 (message "Manual buffer %s is ready." (buffer-name man-buffer)))
480 ((or (eq Man-notify 'meek)
481 t)
482 (message ""))
483 ))
484
485 (defun Man-set-fonts ()
486 (goto-char (point-min))
487 (while (re-search-forward "\\(.\b\\)+" nil t)
488 (let ((st (match-beginning 0)) (en (match-end 0)))
489 (goto-char st)
490 (if window-system
491 (put-text-property st (if (= en (point-max)) en (1+ en)) 'face
492 (if (looking-at "_") 'underline 'bold)))
493 (while (and (< (point) en) (looking-at ".\b"))
494 (replace-match "")))))
495
496 (defun Man-bgproc-sentinel (process msg)
497 "Manpage background process sentinel."
498 (let ((Man-buffer (process-buffer process))
499 (delete-buff nil)
500 (err-mess nil))
501 (if (null (buffer-name Man-buffer)) ;; deleted buffer
502 (set-process-buffer process nil)
503 (save-excursion
504 (set-buffer Man-buffer)
505 (goto-char (point-min))
506 (cond ((or (looking-at "No \\(manual \\)*entry for")
507 (looking-at "[^\n]*: nothing appropriate$"))
508 (setq err-mess (buffer-substring (point) (Man-linepos 'eol))
509 delete-buff t)
510 )
511 ((not (and (eq (process-status process) 'exit)
512 (= (process-exit-status process) 0)))
513 (setq err-mess
514 (concat (buffer-name Man-buffer)
515 ": process "
516 (let ((eos (1- (length msg))))
517 (if (= (aref msg eos) ?\n)
518 (substring msg 0 eos) msg))))
519 (goto-char (point-max))
520 (insert (format "\nprocess %s" msg))
521 )))
522 (if delete-buff
523 (kill-buffer Man-buffer)
524 (save-window-excursion
525 (save-excursion
526 (set-buffer Man-buffer)
527 (Man-set-fonts)
528 (Man-mode)
529 (set-buffer-modified-p nil)))
530 (Man-notify-when-ready Man-buffer))
531
532 (if err-mess
533 (error err-mess))
534 )))
535
536 \f
537 ;; ======================================================================
538 ;; set up manual mode in buffer and build alists
539
540 (defun Man-mode ()
541 "A mode for browsing Un*x manual pages.
542
543 The following man commands are available in the buffer. Try
544 \"\\[describe-key] <key> RET\" for more information:
545
546 \\[manual-entry] Prompt to retrieve a new manpage.
547 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
548 \\[Man-next-manpage] Jump to next manpage in circular list.
549 \\[Man-previous-manpage] Jump to previous manpage in circular list.
550 \\[Man-next-section] Jump to next manpage section.
551 \\[Man-previous-section] Jump to previous manpage section.
552 \\[Man-goto-section] Go to a manpage section.
553 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
554 \\[Man-quit] Deletes the manpage, its buffer, and window.
555 \\[describe-mode] Prints this help text.
556
557 The following variables may be of some use. Try
558 \"\\[describe-variable] <variable-name> RET\" for more information:
559
560 Man-notify What happens when manpage formatting is done.
561 Man-reuse-okay-p Okay to reuse already formatted buffer?
562 Man-downcase-section-letters-p Force section letters to lower case?
563 Man-circular-pages-p Multiple manpage list treated as circular?
564 Man-auto-section-alist List of major modes and their section numbers.
565 Man-section-translations-alist List of section numbers and their Un*x equiv.
566 Man-filter-list Background manpage filter command.
567 Man-mode-line-format Mode line format for Man mode buffers.
568 Man-mode-map Keymap bindings for Man mode buffers.
569 Man-mode-hook Normal hook run on entry to Man mode.
570 Man-section-regexp Regexp describing manpage section letters.
571 Man-heading-regexp Regexp describing section headers.
572 Man-see-also-regexp Regexp for SEE ALSO section (or your equiv).
573 Man-first-heading-regexp Regexp for first heading on a manpage.
574 Man-reference-regexp Regexp matching a references in SEE ALSO.
575 Man-switches Background `man' command switches.
576
577 The following key bindings are currently in effect in the buffer:
578 \\{Man-mode-map}"
579 (interactive)
580 (setq major-mode 'Man-mode
581 mode-name "Man"
582 buffer-auto-save-file-name nil
583 mode-line-format Man-mode-line-format
584 truncate-lines t
585 buffer-read-only t)
586 (buffer-disable-undo (current-buffer))
587 (auto-fill-mode -1)
588 (use-local-map Man-mode-map)
589 (goto-char (point-min))
590 (Man-build-page-list)
591 (Man-goto-page 1)
592 (run-hooks 'Man-mode-hook))
593
594 (defun Man-build-section-alist ()
595 "Build the association list of manpage sections."
596 (setq Man-sections-alist nil)
597 (goto-char (point-min))
598 (while (re-search-forward Man-heading-regexp (point-max) t)
599 (aput 'Man-sections-alist
600 (buffer-substring (Man-linepos 'bol) (Man-linepos)))
601 (forward-line 1)
602 ))
603
604 (defun Man-build-references-alist ()
605 "Build the association list of references (in the SEE ALSO section)."
606 (setq Man-refpages-alist nil)
607 (save-excursion
608 (if (Man-find-section Man-see-also-regexp)
609 (let ((start (progn (forward-line 1) (point)))
610 (end (progn
611 (Man-next-section 1)
612 (point)))
613 hyphenated
614 (runningpoint -1))
615 (narrow-to-region start end)
616 (goto-char (point-min))
617 (back-to-indentation)
618 (while (and (not (eobp)) (/= (point) runningpoint))
619 (setq runningpoint (point))
620 (let* ((eow (re-search-forward Man-reference-regexp end t))
621 (word (buffer-substring (match-beginning 0) (match-end 0)))
622 (len (1- (length word))))
623 (if (not eow) nil
624 (if hyphenated
625 (setq word (concat hyphenated word)
626 hyphenated nil))
627 (if (= (aref word len) ?-)
628 (setq hyphenated (substring word 0 len))
629 (aput 'Man-refpages-alist word))))
630 (skip-chars-forward " \t\n,"))
631 ))))
632
633 (defun Man-build-page-list ()
634 "Build the list of separate manpages in the buffer."
635 (setq Man-page-list nil)
636 (save-excursion
637 (let ((page-start (Man-linepos 'bob))
638 (page-end (Man-linepos 'eob))
639 (regexp Man-first-heading-regexp))
640 (goto-char (point-min))
641 (re-search-forward regexp (point-max) t)
642 (while (not (eobp))
643 (if (re-search-forward regexp (point-max) t)
644 (progn
645 (setq page-end (Man-linepos 'bol))
646 (end-of-line))
647 (goto-char (point-max))
648 (setq page-end (point)))
649 (setq Man-page-list (append Man-page-list
650 (list (cons page-start page-end)))
651 page-start page-end)
652 ))))
653
654 \f
655 ;; ======================================================================
656 ;; Man mode commands
657
658 (defun Man-next-section (n)
659 "Move point to Nth next section (default 1)."
660 (interactive "p")
661 (if (looking-at Man-heading-regexp)
662 (forward-line 1))
663 (if (re-search-forward Man-heading-regexp (point-max) t n)
664 (beginning-of-line)
665 (goto-char (point-max))))
666
667 (defun Man-previous-section (n)
668 "Move point to Nth previous section (default 1)."
669 (interactive "p")
670 (if (looking-at Man-heading-regexp)
671 (forward-line -1))
672 (if (re-search-backward Man-heading-regexp (point-min) t n)
673 (beginning-of-line)
674 (goto-char (point-min))))
675
676 (defun Man-find-section (section)
677 "Move point to SECTION if it exists, otherwise don't move point.
678 Returns t if section is found, nil otherwise."
679 (let ((curpos (point)))
680 (goto-char (point-min))
681 (if (re-search-forward (concat "^\\s-?" section) (point-max) t)
682 (progn (beginning-of-line) t)
683 (goto-char curpos)
684 nil)
685 ))
686
687 (defun Man-goto-section ()
688 "Query for section to move point to."
689 (interactive)
690 (aput 'Man-sections-alist
691 (let* ((default (aheadsym Man-sections-alist))
692 (completion-ignore-case t)
693 chosen
694 (prompt (concat "Go to section: (default " default ") ")))
695 (setq chosen (completing-read prompt Man-sections-alist))
696 (if (or (not chosen)
697 (string= chosen ""))
698 default
699 chosen)))
700 (Man-find-section (aheadsym Man-sections-alist)))
701
702 (defun Man-goto-see-also-section ()
703 "Move point the the \"SEE ALSO\" section.
704 Actually the section moved to is described by `Man-see-also-regexp'."
705 (interactive)
706 (if (not (Man-find-section Man-see-also-regexp))
707 (error (concat "No " Man-see-also-regexp
708 " section found in current manpage."))))
709
710 (defun Man-follow-manual-reference (arg reference)
711 "Get one of the manpages referred to in the \"SEE ALSO\" section.
712 Specify which reference to use; default is based on word at point.
713 Prefix argument ARG is passed to `Man-getpage-in-background'."
714 (interactive
715 (if (not Man-refpages-alist)
716 (error "No references in current man page")
717 (list current-prefix-arg
718 (let* ((default (or
719 (car (all-completions
720 (save-excursion
721 (skip-syntax-backward "w()")
722 (skip-chars-forward " \t")
723 (let ((word (current-word)))
724 ;; strip a trailing '-':
725 (if (string-match "-$" word)
726 (substring word 0 (match-beginning 0))
727 word)))
728 Man-refpages-alist))
729 (aheadsym Man-refpages-alist)))
730 chosen
731 (prompt (concat "Refer to: (default " default ") ")))
732 (setq chosen (completing-read prompt Man-refpages-alist nil t))
733 (if (or (not chosen)
734 (string= chosen ""))
735 default
736 chosen)))))
737 (if (not Man-refpages-alist)
738 (error "No references found in current manpage")
739 (aput 'Man-refpages-alist reference)
740 (Man-getpage-in-background
741 (Man-translate-references (aheadsym Man-refpages-alist))
742 arg)))
743
744 (defun Man-quit ()
745 "Kill the buffer containing the manpage."
746 (interactive)
747 (let ((buff (current-buffer)))
748 (delete-windows-on buff)
749 (kill-buffer buff)))
750
751 (defun Man-goto-page (page)
752 "Go to the manual page on page PAGE."
753 (interactive
754 (if (not Man-page-list)
755 (error "You're looking at the only manpage in the buffer.")
756 (format "nGo to manpage [1-%d]: " (length Man-page-list))))
757 (if (or (< page 1)
758 (> page (length Man-page-list)))
759 (error "No manpage %d found" page))
760 (let* ((page-range (nth (1- page) Man-page-list))
761 (page-start (car page-range))
762 (page-end (cdr page-range)))
763 (setq Man-current-page page
764 Man-page-mode-string (Man-page-mode-string))
765 (widen)
766 (goto-char page-start)
767 (narrow-to-region page-start page-end)
768 (Man-build-section-alist)
769 ;; Don't let bugs in Man-build-references-alist
770 ;; interfere with ordinary use of this package.
771 (condition-case nil
772 (Man-build-references-alist)
773 (error))
774 (widen)
775 (narrow-to-region page-start page-end)
776 (goto-char (point-min))))
777
778
779 (defun Man-next-manpage ()
780 "Find the next manpage entry in the buffer."
781 (interactive)
782 (if (= (length Man-page-list) 1)
783 (error "This is the only manpage in the buffer."))
784 (if (< Man-current-page (length Man-page-list))
785 (Man-goto-page (1+ Man-current-page))
786 (if Man-circular-pages-p
787 (Man-goto-page 1)
788 (error "You're looking at the last manpage in the buffer."))))
789
790 (defun Man-previous-manpage ()
791 "Find the previous manpage entry in the buffer."
792 (interactive)
793 (if (= (length Man-page-list) 1)
794 (error "This is the only manpage in the buffer."))
795 (if (> Man-current-page 1)
796 (Man-goto-page (1- Man-current-page))
797 (if Man-circular-pages-p
798 (Man-goto-page (length Man-page-list))
799 (error "You're looking at the first manpage in the buffer."))))
800 \f
801 (provide 'man)
802
803 ;;; man.el ends here