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