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