]> code.delx.au - gnu-emacs/blob - lisp/man.el
(Man-filter-list): Add an element for X man pages.
[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 (provide 'man)
76
77 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
78 ;; user variables
79
80 (defvar Man-notify 'friendly
81 "*Selects the behavior when manpage is ready.
82 This variable may have one of the following values:
83
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-reuse-okay-p t
94 "*Reuse a manpage buffer if possible.
95 When t, and a manpage buffer already exists with the same invocation,
96 man just indicates the manpage is ready according to the value of
97 Man-notify. When nil, it always fires off a background process, putting
98 the results in a uniquely named buffer.")
99
100 (defvar Man-downcase-section-letters-p t
101 "*Letters in sections are converted to lower case.
102 Some Un*x man commands can't handle uppercase letters in sections, for
103 example \"man 2V chmod\", but they are often displayed in the manpage
104 with the upper case letter. When this variable is t, the section
105 letter (e.g., \"2V\") is converted to lowercase (e.g., \"2v\") before
106 being sent to the man background process.")
107
108 (defvar Man-circular-pages-p t
109 "*If t, the manpage list is treated as circular for traversal.")
110
111 (defvar Man-auto-section-alist
112 '((c-mode . ("2" "3"))
113 (c++-mode . ("2" "3"))
114 (shell-mode . ("1" "8"))
115 (cmushell-mode . ("1" "8"))
116 (text-mode . "1")
117 )
118 "*Association list of major modes and their default section numbers.
119 List is of the form: (MAJOR-MODE . [SECTION | (SECTION*)]). If current
120 major mode is not in list, then the default is to check for manpages
121 in all sections.")
122
123 (defvar Man-section-translations-alist
124 '(("3C++" . "3")
125 ("1-UCB" . ""))
126 "*Association list of bogus sections to real section numbers.
127 Some manpages (e.g. the Sun C++ 2.1 manpages) have section numbers in
128 their references which Un*x man(1) does not recognize. This
129 association list is used to translate those sections, when found, to
130 the associated section number.")
131
132 (defvar Man-filter-list
133 '(("sed "
134 ("-e 's/.\010//g'"
135 "-e '/[Nn]o such file or directory/d'"
136 "-e '/Reformatting page. Wait... done/d'"
137 "-e '/^\\([A-Z][A-Z.]*([0-9A-Za-z][-0-9A-Za-z+]*)\\).*\\1$/d'"
138 "-e '/^[ \\t]*Hewlett-Packard Company[ \\t]*- [0-9]* -.*$/d'"
139 "-e '/^[ \\t]*Hewlett-Packard[ \\t]*- [0-9]* -.*$/d'"
140 "-e '/^ *Page [0-9]*.*(printed [0-9\\/]*)$/d'"
141 "-e '/^Printed [0-9].*[0-9]$/d'"
142 "-e '/^[ \\t]*X Version 1[01].*Release [0-9]/d'"
143 "-e '/^Sun Microsystems.*Last change:/d'"
144 "-e '/^Sun Release [0-9].*[0-9]$/d'"
145 "-e '/^\\n$/D'"
146 ))
147 ("awk '"
148 ("BEGIN { blankline=0; anonblank=0; }"
149 "/^$/ { if (anonblank==0) next; }"
150 "{ anonblank=1; }"
151 "/^$/ { blankline++; next; }"
152 "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }"
153 "'"
154 ))
155 )
156 "*Manpage cleaning filter command phrases.
157 This variable contains an association list of the following form:
158
159 '((command-string (phrase-string*))*)
160
161 Each phrase-string is concatenated onto the command-string to form a
162 command filter. The (standard) output (and standard error) of the Un*x
163 man command is piped through each command filter in the order the
164 commands appear in the association list. The final output is placed in
165 the manpage buffer.")
166
167 (defvar Man-mode-line-format
168 '("" mode-line-modified
169 mode-line-buffer-identification " "
170 global-mode-string
171 Man-page-mode-string
172 " %[(" mode-name minor-mode-alist mode-line-process ")%]----"
173 (-3 . "%p") "-%-")
174 "*Mode line format for manual mode buffer.")
175
176 (defvar Man-mode-map nil
177 "*Keymap for Man-mode.")
178
179 (defvar Man-mode-hooks nil
180 "*Hooks for Man-mode.")
181
182 (defvar Man-section-regexp "[0-9][a-zA-Z+]*"
183 "*Regular expression describing a manpage section within parentheses.")
184
185 (defvar Man-heading-regexp "^[A-Z]"
186 "*Regular expression describing a manpage heading entry.")
187
188 (defvar Man-see-also-regexp "SEE ALSO"
189 "*Regular expression for SEE ALSO heading (or your equivalent).
190 This regexp should not start with a `^' character.")
191
192 (defvar Man-first-heading-regexp "^NAME$\\|^No manual entry for .*$"
193 "*Regular expression describing first heading on a manpage.
194 This regular expression should start with a `^' character.")
195
196 (defvar Man-reference-regexp "[-a-zA-Z0-9_.]+\\(([0-9][a-zA-Z+]*)\\)?"
197 "*Regular expression describing a reference in the SEE ALSO section.")
198
199
200 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
201 ;; end user variables
202
203 (defconst Man-version-number "1.1"
204 "man's version number.")
205
206 \f
207 ;; other variables and keymap initializations
208 (make-variable-buffer-local 'Man-sections-alist)
209 (make-variable-buffer-local 'Man-refpages-alist)
210 (make-variable-buffer-local 'Man-page-list)
211 (make-variable-buffer-local 'Man-current-page)
212 (make-variable-buffer-local 'Man-page-mode-string)
213
214 (setq-default Man-sections-alist nil)
215 (setq-default Man-refpages-alist nil)
216 (setq-default Man-page-list nil)
217 (setq-default Man-current-page 0)
218 (setq-default Man-page-mode-string "1 (of 1)")
219
220 (if Man-mode-map
221 nil
222 (setq Man-mode-map (make-keymap))
223 (suppress-keymap Man-mode-map)
224 (define-key Man-mode-map " " 'scroll-up)
225 (define-key Man-mode-map "\177" 'scroll-down)
226 (define-key Man-mode-map "n" 'Man-next-section)
227 (define-key Man-mode-map "p" 'Man-previous-section)
228 (define-key Man-mode-map "\en" 'Man-next-manpage)
229 (define-key Man-mode-map "\ep" 'Man-previous-manpage)
230 (define-key Man-mode-map "," 'beginning-of-buffer)
231 (define-key Man-mode-map "." 'end-of-buffer)
232 (define-key Man-mode-map "r" 'Man-follow-manual-reference)
233 (define-key Man-mode-map "t" 'toggle-truncate-lines)
234 (define-key Man-mode-map "g" 'Man-goto-section)
235 (define-key Man-mode-map "s" 'Man-goto-see-also-section)
236 (define-key Man-mode-map "q" 'Man-quit)
237 (define-key Man-mode-map "m" 'manual-entry)
238 (define-key Man-mode-map "v" 'Man-version)
239 (define-key Man-mode-map "?" 'describe-mode)
240 )
241
242 \f
243 ;; ======================================================================
244 ;; utilities
245
246 (defun Man-page-mode-string ()
247 "Formats part of the mode line for manual mode."
248 (format "%d (of %d)" Man-current-page (length Man-page-list)))
249
250 (defun Man-delete-trailing-newline (str)
251 (if (string= (substring str (1- (length str))) "\n")
252 (substring str 0 (1- (length str)))
253 str))
254
255 (defun Man-build-man-command ()
256 "Builds the entire background manpage and cleaning command."
257 (let ((command "man %s 2>&1 | ")
258 (flist Man-filter-list))
259 (while flist
260 (let ((pcom (car (car flist)))
261 (pargs (car (cdr (car flist)))))
262 (setq flist (cdr flist))
263 (if (or (not (stringp pcom))
264 (not (listp pargs)))
265 (error "malformed Man-filter-list."))
266 (setq command (concat command pcom
267 (mapconcat '(lambda (phrase) phrase)
268 pargs " "))))
269 (if flist
270 (setq command (concat command " | " ))))
271 command))
272
273 (defun Man-downcase (man-args)
274 "Downcases section letters in MAN-ARGS."
275 (let ((newargs "")
276 (s 0)
277 mstart mend
278 (len (length man-args)))
279 (while (and (< s len)
280 (setq mstart (string-match Man-section-regexp man-args s)))
281 (setq mend (match-end 0)
282 newargs (concat newargs (substring man-args s mstart)))
283 (setq newargs (concat newargs (downcase
284 (substring man-args mstart mend)))
285 s mend))
286 (concat newargs (substring man-args s len))))
287
288 (defun Man-translate-references (ref)
289 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style."
290 (if (string-match (concat "(" Man-section-regexp ")$") ref)
291 (let* ((word (progn (string-match "(" ref)
292 (substring ref 0 (1- (match-end 0)))))
293 (section-re (concat "(\\(" Man-section-regexp "\\))"))
294 (section (if (string-match section-re ref)
295 (substring ref (match-beginning 1) (match-end 1))
296 ""))
297 (slist Man-section-translations-alist)
298 )
299 (if Man-downcase-section-letters-p
300 (setq section (Man-downcase section)))
301 (while slist
302 (let ((s1 (car (car slist)))
303 (s2 (cdr (car slist))))
304 (setq slist (cdr slist))
305 (if Man-downcase-section-letters-p
306 (setq s1 (Man-downcase s1)))
307 (if (not (string= s1 section)) nil
308 (setq section (if Man-downcase-section-letters-p
309 (Man-downcase s2)
310 s2)
311 slist nil))))
312 (concat section " " word))
313 ref))
314
315 (defun Man-linepos (&optional position col-p)
316 "Return the character position at various line/buffer positions.
317 Preserves the state of point, mark, etc. Optional POSITION can be one
318 of the following symbols:
319 bol == beginning of line
320 boi == beginning of indentation
321 eol == end of line [default]
322 bob == beginning of buffer
323 eob == end of buffer
324
325 Optional COL-P non-nil returns current-column instead of character position."
326 (let ((tpnt (point))
327 rval)
328 (cond
329 ((eq position 'bol) (beginning-of-line))
330 ((eq position 'boi) (back-to-indentation))
331 ((eq position 'bob) (goto-char (point-min)))
332 ((eq position 'eob) (goto-char (point-max)))
333 (t (end-of-line)))
334 (setq rval (if col-p (current-column) (point)))
335 (goto-char tpnt)
336 rval))
337
338 \f
339 ;; ======================================================================
340 ;; default man entry and get word under point
341
342 (defun Man-default-man-args (manword)
343 "Build the default man args from MANWORD and major-mode."
344 (let ((mode major-mode)
345 (slist Man-auto-section-alist))
346 (while (and slist
347 (not (eq (car (car slist)) mode)))
348 (setq slist (cdr slist)))
349 (if (not slist)
350 manword
351 (let ((sections (cdr (car slist))))
352 (if (not (listp sections))
353 (concat sections " " manword)
354 (let ((manarg ""))
355 (while sections
356 (setq manarg (concat manarg " " (car sections) " " manword))
357 (setq sections (cdr sections)))
358 manarg)
359 )))))
360
361 (defun Man-default-man-entry ()
362 "Make a guess at a default manual entry.
363 This guess is based on the text surrounding the cursor, and the
364 default section number is selected from Man-auto-section-alist."
365 (let ((default-section nil)
366 default-title)
367 (save-excursion
368
369 ;; Default man entry title is any word the cursor is on,
370 ;; or if cursor not on a word, then nearest preceding
371 ;; word.
372 (and (not (looking-at "[a-zA-Z_]"))
373 (skip-chars-backward "^a-zA-Z_"))
374 (skip-chars-backward "(a-zA-Z_0-9")
375 (and (looking-at "(") (forward-char 1))
376 (setq default-title
377 (buffer-substring
378 (point)
379 (progn (skip-chars-forward "a-zA-Z0-9_") (point))))
380
381 ;; If looking at something like ioctl(2) or brc(1M), include
382 ;; section number in default-entry
383 (if (looking-at "[ \t]*([ \t]*[0-9][a-zA-Z]?[ \t]*)")
384 (progn (skip-chars-forward "^0-9")
385 (setq default-section
386 (buffer-substring
387 (point)
388 (progn
389 (skip-chars-forward "0-9a-zA-Z")
390 (point)))))
391
392 ;; Otherwise, assume section number to be 2 if we're
393 ;; in C code
394 (and (eq major-mode 'c-mode)
395 (setq default-section "2")))
396 (if default-section
397 (format "%s %s" default-section default-title)
398 default-title))))
399
400 \f
401 ;; ======================================================================
402 ;; top level command and background process sentinel
403
404 ;;;###autoload
405 (defun manual-entry (arg)
406 "Get a Un*x manual page and put it in a buffer.
407 This command is the top-level command in the man package. It runs a Un*x
408 command to retrieve and clean a manpage in the background and places the
409 results in a Man-mode (manpage browsing) buffer. See variable
410 Man-notify for what happens when the buffer is ready.
411 Universal argument ARG, is passed to Man-getpage-in-background."
412 (interactive "P")
413 (let* ((default-entry (Man-default-man-entry))
414 (man-args
415 (read-string (format "Manual-entry: %s"
416 (if (string= default-entry "") ""
417 (format "(default: %s) "
418 default-entry))))))
419 (and (string= man-args "")
420 (if (string= default-entry "")
421 (error "No man args given.")
422 (setq man-args default-entry)))
423 ;; Recognize the subject(section) syntax.
424 (if (string-match "^[ \t]*\\([^( \t]+\\)[ \t]*(\\([^)]+\\))[ \t]*$"
425 man-args)
426 (setq man-args
427 (concat (substring man-args (match-beginning 2) (match-end 2))
428 " "
429 (substring man-args (match-beginning 1) (match-end 1)))))
430 (if Man-downcase-section-letters-p
431 (setq man-args (Man-downcase man-args)))
432 (Man-getpage-in-background man-args (consp arg))
433 ))
434
435 (defun Man-getpage-in-background (man-args &optional override-reuse-p)
436 "Uses MAN-ARGS to build and fire off the manpage and cleaning command.
437 Optional OVERRIDE-REUSE-P, when supplied non-nil forces man to
438 start a background process even if a buffer already exists and
439 Man-reuse-okay-p is non-nil."
440 (let* ((bufname (concat "*man " man-args "*"))
441 (buffer (get-buffer bufname)))
442 (if (and Man-reuse-okay-p
443 (not override-reuse-p)
444 buffer)
445 (Man-notify-when-ready buffer)
446 (message "Invoking man %s in background..." man-args)
447 (setq buffer (generate-new-buffer bufname))
448 (set-process-sentinel
449 (start-process "man" buffer "sh" "-c"
450 (format (Man-build-man-command) man-args))
451 'Man-bgproc-sentinel))
452 ))
453
454 (defun Man-notify-when-ready (man-buffer)
455 "Notify the user when MAN-BUFFER is ready.
456 See the variable `Man-notify' for the different notification behaviors."
457 (cond
458 ((eq Man-notify 'bully)
459 (pop-to-buffer man-buffer)
460 (delete-other-windows))
461 ((eq Man-notify 'aggressive)
462 (pop-to-buffer man-buffer))
463 ((eq Man-notify 'friendly)
464 (display-buffer man-buffer 'not-this-window))
465 ((eq Man-notify 'polite)
466 (beep)
467 (message "Manual buffer %s is ready." (buffer-name man-buffer)))
468 ((eq Man-notify 'quiet)
469 (message "Manual buffer %s is ready." (buffer-name man-buffer)))
470 ((or (eq Man-notify 'meek)
471 t)
472 (message ""))
473 ))
474
475 (defun Man-bgproc-sentinel (process msg)
476 "Manpage background process sentinel."
477 (let ((Man-buffer (process-buffer process))
478 (delete-buff nil)
479 (err-mess nil))
480 (if (null (buffer-name Man-buffer)) ;; deleted buffer
481 (set-process-buffer process nil)
482 (save-excursion
483 (set-buffer Man-buffer)
484 (goto-char (point-min))
485 (cond ((or (looking-at "No \\(manual \\)*entry for")
486 (looking-at "[^\n]*: nothing appropriate$"))
487 (setq err-mess (buffer-substring (point) (Man-linepos 'eol))
488 delete-buff t)
489 )
490 ((not (and (eq (process-status process) 'exit)
491 (= (process-exit-status process) 0)))
492 (setq err-mess
493 (concat (buffer-name Man-buffer)
494 ": process "
495 (let ((eos (1- (length msg))))
496 (if (= (aref msg eos) ?\n)
497 (substring msg 0 eos) msg))))
498 (goto-char (point-max))
499 (insert (format "\nprocess %s" msg))
500 )))
501 (if delete-buff
502 (kill-buffer Man-buffer)
503 (save-window-excursion
504 (save-excursion
505 (set-buffer Man-buffer)
506 (Man-mode)
507 (set-buffer-modified-p nil)))
508 (Man-notify-when-ready Man-buffer))
509
510 (if err-mess
511 (error err-mess))
512 )))
513
514 \f
515 ;; ======================================================================
516 ;; set up manual mode in buffer and build alists
517
518 (defun Man-mode ()
519 "SUPERMAN 1.1: A mode for browsing Un*x manual pages.
520
521 The following man commands are available in the buffer. Try
522 \"\\[describe-key] <key> RET\" for more information:
523
524 \\[manual-entry] Prompt to retrieve a new manpage.
525 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
526 \\[Man-next-manpage] Jump to next manpage in circular list.
527 \\[Man-previous-manpage] Jump to previous manpage in circular list.
528 \\[Man-next-section] Jump to next manpage section.
529 \\[Man-previous-section] Jump to previous manpage section.
530 \\[Man-goto-section] Go to a manpage section.
531 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
532 \\[Man-quit] Deletes the manpage, its buffer, and window.
533 \\[Man-version] Prints man's version number.
534 \\[describe-mode] Prints this help text.
535
536 The following variables may be of some use. Try
537 \"\\[describe-variable] <variable-name> RET\" for more information:
538
539 Man-notify What happens when manpage formatting is done.
540 Man-reuse-okay-p Okay to reuse already formatted buffer?
541 Man-downcase-section-letters-p Force section letters to lower case?
542 Man-circular-pages-p Multiple manpage list treated as circular?
543 Man-auto-section-alist List of major modes and their section numbers.
544 Man-section-translations-alist List of section numbers and their Un*x equiv.
545 Man-filter-list Background manpage filter command.
546 Man-mode-line-format Mode line format for Man-mode buffers.
547 Man-mode-map Keymap bindings for Man-mode buffers.
548 Man-mode-hooks Hooks for Man-mode.
549 Man-section-regexp Regexp describing manpage section letters.
550 Man-heading-regexp Regexp describing section headers.
551 Man-see-also-regexp Regexp for SEE ALSO section (or your equiv).
552 Man-first-heading-regexp Regexp for first heading on a manpage.
553 Man-reference-regexp Regexp matching a references in SEE ALSO.
554 Man-version-number Superman version number.
555
556 The following key bindings are currently in effect in the buffer:
557 \\{Man-mode-map}"
558 (interactive)
559 (setq major-mode 'Man-mode
560 mode-name "Manual"
561 buffer-auto-save-file-name nil
562 mode-line-format Man-mode-line-format
563 truncate-lines t
564 buffer-read-only t)
565 (buffer-disable-undo (current-buffer))
566 (auto-fill-mode -1)
567 (use-local-map Man-mode-map)
568 (goto-char (point-min))
569 (Man-build-page-list)
570 (Man-goto-page 1)
571 (run-hooks 'Man-mode-hook))
572
573 (defun Man-build-section-alist ()
574 "Build the association list of manpage sections."
575 (setq Man-sections-alist nil)
576 (goto-char (point-min))
577 (while (re-search-forward Man-heading-regexp (point-max) t)
578 (aput 'Man-sections-alist
579 (buffer-substring (Man-linepos 'bol) (Man-linepos)))
580 (forward-line 1)
581 ))
582
583 (defun Man-build-references-alist ()
584 "Build the association list of references (in the SEE ALSO section)."
585 (setq Man-refpages-alist nil)
586 (save-excursion
587 (if (Man-find-section Man-see-also-regexp)
588 (let ((start (progn (forward-line 1) (point)))
589 (end (progn
590 (Man-next-section 1)
591 (point)))
592 hyphenated
593 (runningpoint -1))
594 (narrow-to-region start end)
595 (goto-char (point-min))
596 (back-to-indentation)
597 (while (and (not (eobp)) (/= (point) runningpoint))
598 (setq runningpoint (point))
599 (let* ((bow (point))
600 (eow (re-search-forward Man-reference-regexp end t))
601 (word (buffer-substring bow (match-end 0)))
602 (len (1- (length word))))
603 (if (not eow) nil
604 (if hyphenated
605 (setq word (concat hyphenated word)
606 hyphenated nil))
607 (if (= (aref word len) ?-)
608 (setq hyphenated (substring word 0 len))
609 (aput 'Man-refpages-alist word))))
610 (skip-chars-forward " \t\n,"))
611 ))))
612
613 (defun Man-build-page-list ()
614 "Build the list of separate manpages in the buffer."
615 (setq Man-page-list nil)
616 (save-excursion
617 (let ((page-start (Man-linepos 'bob))
618 (page-end (Man-linepos 'eob))
619 (regexp Man-first-heading-regexp))
620 (goto-char (point-min))
621 (re-search-forward regexp (point-max) t)
622 (while (not (eobp))
623 (if (re-search-forward regexp (point-max) t)
624 (progn
625 (setq page-end (Man-linepos 'bol))
626 (end-of-line))
627 (goto-char (point-max))
628 (setq page-end (point)))
629 (setq Man-page-list (append Man-page-list
630 (list (cons page-start page-end)))
631 page-start page-end)
632 ))))
633
634 \f
635 ;; ======================================================================
636 ;; Man-mode commands
637
638 (defun Man-next-section (n)
639 "Move point to Nth next section (default 1)."
640 (interactive "p")
641 (if (looking-at Man-heading-regexp)
642 (forward-line 1))
643 (if (re-search-forward Man-heading-regexp (point-max) t n)
644 (beginning-of-line)
645 (goto-char (point-max))))
646
647 (defun Man-previous-section (n)
648 "Move point to Nth previous section (default 1)."
649 (interactive "p")
650 (if (looking-at Man-heading-regexp)
651 (forward-line -1))
652 (if (re-search-backward Man-heading-regexp (point-min) t n)
653 (beginning-of-line)
654 (goto-char (point-min))))
655
656 (defun Man-find-section (section)
657 "Move point to SECTION if it exists, otherwise don't move point.
658 Returns t if section is found, nil otherwise."
659 (let ((curpos (point)))
660 (goto-char (point-min))
661 (if (re-search-forward (concat "^" section) (point-max) t)
662 (progn (beginning-of-line) t)
663 (goto-char curpos)
664 nil)
665 ))
666
667 (defun Man-goto-section ()
668 "Query for section to move point to."
669 (interactive)
670 (aput 'Man-sections-alist
671 (let* ((default (aheadsym Man-sections-alist))
672 (completion-ignore-case t)
673 chosen
674 (prompt (concat "Go to section: (default " default ") ")))
675 (setq chosen (completing-read prompt Man-sections-alist))
676 (if (or (not chosen)
677 (string= chosen ""))
678 default
679 chosen)))
680 (Man-find-section (aheadsym Man-sections-alist)))
681
682 (defun Man-goto-see-also-section ()
683 "Move point the the \"SEE ALSO\" section.
684 Actually the section moved to is described by Man-see-also-regexp."
685 (interactive)
686 (if (not (Man-find-section Man-see-also-regexp))
687 (error (concat "No " Man-see-also-regexp
688 " section found in current manpage."))))
689
690 (defun Man-follow-manual-reference (arg)
691 "Get one of the manpages referred to in the \"SEE ALSO\" section.
692 Queries you for the page to retrieve. Of course it does this in the
693 background. Universal argument ARG is passed to Man-getpage-in-background."
694 (interactive "P")
695 (if (not Man-refpages-alist)
696 (error (concat "No references found in current manpage."))
697 (aput 'Man-refpages-alist
698 (let* ((default (aheadsym Man-refpages-alist))
699 chosen
700 (prompt (concat "Refer to: (default " default ") ")))
701 (setq chosen (completing-read prompt Man-refpages-alist nil t))
702 (if (or (not chosen)
703 (string= chosen ""))
704 default
705 chosen)))
706 (Man-getpage-in-background
707 (Man-translate-references (aheadsym Man-refpages-alist))
708 (consp arg))))
709
710 (defun Man-quit ()
711 "Kill the buffer containing the manpage."
712 (interactive)
713 (let ((buff (current-buffer)))
714 (delete-windows-on buff)
715 (kill-buffer buff)))
716
717 (defun Man-goto-page (page)
718 "Go to the manual page on page PAGE."
719 (interactive
720 (if (not Man-page-list)
721 (error "You're looking at the only manpage in the buffer.")
722 (format "nGo to manpage [1-%d]: " (length Man-page-list))))
723 (if (or (< page 1)
724 (> page (length Man-page-list)))
725 (error "No manpage %d found" page))
726 (let* ((page-range (nth (1- page) Man-page-list))
727 (page-start (car page-range))
728 (page-end (cdr page-range)))
729 (setq Man-current-page page
730 Man-page-mode-string (Man-page-mode-string))
731 (widen)
732 (goto-char page-start)
733 (narrow-to-region page-start page-end)
734 (Man-build-section-alist)
735 ;; Don't let bugs in Man-build-references-alist
736 ;; interfere with ordinary use of this package.
737 (condition-case nil
738 (Man-build-references-alist)
739 (error))
740 (widen)
741 (narrow-to-region page-start page-end)
742 (goto-char (point-min))))
743
744
745 (defun Man-next-manpage ()
746 "Find the next manpage entry in the buffer."
747 (interactive)
748 (if (= (length Man-page-list) 1)
749 (error "This is the only manpage in the buffer."))
750 (if (< Man-current-page (length Man-page-list))
751 (Man-goto-page (1+ Man-current-page))
752 (if Man-circular-pages-p
753 (Man-goto-page 1)
754 (error "You're looking at the last manpage in the buffer."))))
755
756 (defun Man-previous-manpage ()
757 "Find the previous manpage entry in the buffer."
758 (interactive)
759 (if (= (length Man-page-list) 1)
760 (error "This is the only manpage in the buffer."))
761 (if (> Man-current-page 1)
762 (Man-goto-page (1- Man-current-page))
763 (if Man-circular-pages-p
764 (Man-goto-page (length Man-page-list))
765 (error "You're looking at the first manpage in the buffer."))))
766
767 (defun Man-version (arg)
768 "Show man's version.
769 Universal argument (\\[universal-argument]) ARG inserts version
770 information in the current buffer instead of printing the message in
771 the echo area."
772 (interactive "P")
773 (if (consp arg)
774 (insert "Using Superman version " Man-version-number ".")
775 (message "Using Superman version %s." Man-version-number)))
776
777 ;;; man.el ends here