Add menu for M-x man
[bpt/emacs.git] / lisp / man.el
1 ;;; man.el --- browse UNIX manual pages -*- coding: utf-8 -*-
2
3 ;; Copyright (C) 1993-1994, 1996-1997, 2001-2013 Free Software
4 ;; Foundation, Inc.
5
6 ;; Author: Barry A. Warsaw <bwarsaw@cen.com>
7 ;; Maintainer: FSF
8 ;; Keywords: help
9 ;; Adapted-By: ESR, pot
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; This code provides a function, `man', with which you can browse
29 ;; UNIX manual pages. Formatting is done in background so that you
30 ;; can continue to use your Emacs while processing is going on.
31 ;;
32 ;; The mode also supports hypertext-like following of manual page SEE
33 ;; ALSO references, and other features. See below or do `?' in a
34 ;; manual page buffer for details.
35
36 ;; ========== Credits and History ==========
37 ;; In mid 1991, several people posted some interesting improvements to
38 ;; man.el from the standard Emacs 18.57 distribution. I liked many of
39 ;; these, but wanted everything in one single package, so I decided
40 ;; to incorporate them into a single manual browsing mode. While
41 ;; much of the code here has been rewritten, and some features added,
42 ;; these folks deserve lots of credit for providing the initial
43 ;; excellent packages on which this one is based.
44
45 ;; Nick Duffek <duffek@chaos.cs.brandeis.edu>, posted a very nice
46 ;; improvement which retrieved and cleaned the manpages in a
47 ;; background process, and which correctly deciphered such options as
48 ;; man -k.
49
50 ;; Eric Rose <erose@jessica.stanford.edu>, submitted manual.el which
51 ;; provided a very nice manual browsing mode.
52
53 ;; This package was available as `superman.el' from the LCD package
54 ;; for some time before it was accepted into Emacs 19. The entry
55 ;; point and some other names have been changed to make it a drop-in
56 ;; replacement for the old man.el package.
57
58 ;; Francesco Potortì <pot@cnuce.cnr.it> cleaned it up thoroughly,
59 ;; making it faster, more robust and more tolerant of different
60 ;; systems' man idiosyncrasies.
61
62 ;; ========== Features ==========
63 ;; + Runs "man" in the background and pipes the results through a
64 ;; series of sed and awk scripts so that all retrieving and cleaning
65 ;; is done in the background. The cleaning commands are configurable.
66 ;; + Syntax is the same as Un*x man
67 ;; + Functionality is the same as Un*x man, including "man -k" and
68 ;; "man <section>", etc.
69 ;; + Provides a manual browsing mode with keybindings for traversing
70 ;; the sections of a manpage, following references in the SEE ALSO
71 ;; section, and more.
72 ;; + Multiple manpages created with the same man command are put into
73 ;; a narrowed buffer circular list.
74
75 ;; ============= TODO ===========
76 ;; - Add a command for printing.
77 ;; - The awk script deletes multiple blank lines. This behavior does
78 ;; not allow to understand if there was indeed a blank line at the
79 ;; end or beginning of a page (after the header, or before the
80 ;; footer). A different algorithm should be used. It is easy to
81 ;; compute how many blank lines there are before and after the page
82 ;; headers, and after the page footer. But it is possible to compute
83 ;; the number of blank lines before the page footer by heuristics
84 ;; only. Is it worth doing?
85 ;; - Allow a user option to mean that all the manpages should go in
86 ;; the same buffer, where they can be browsed with M-n and M-p.
87
88 \f
89 ;;; Code:
90
91 (require 'ansi-color)
92 (require 'cl-lib)
93 (require 'button)
94
95 (defgroup man nil
96 "Browse UNIX manual pages."
97 :prefix "Man-"
98 :group 'external
99 :group 'help)
100
101 (defvar Man-notify)
102
103 (defcustom Man-filter-list nil
104 "Manpage cleaning filter command phrases.
105 This variable contains a list of the following form:
106
107 '((command-string phrase-string*)*)
108
109 Each phrase-string is concatenated onto the command-string to form a
110 command filter. The (standard) output (and standard error) of the Un*x
111 man command is piped through each command filter in the order the
112 commands appear in the association list. The final output is placed in
113 the manpage buffer."
114 :type '(repeat (list (string :tag "Command String")
115 (repeat :inline t
116 (string :tag "Phrase String"))))
117 :group 'man)
118
119 (defvar Man-uses-untabify-flag t
120 "Non-nil means use `untabify' instead of `Man-untabify-command'.")
121 (defvar Man-sed-script nil
122 "Script for sed to nuke backspaces and ANSI codes from manpages.")
123
124 (defcustom Man-fontify-manpage-flag t
125 "Non-nil means make up the manpage with fonts."
126 :type 'boolean
127 :group 'man)
128
129 (defface Man-overstrike
130 '((t (:inherit bold)))
131 "Face to use when fontifying overstrike."
132 :group 'man
133 :version "24.3")
134
135 (defface Man-underline
136 '((t (:inherit underline)))
137 "Face to use when fontifying underlining."
138 :group 'man
139 :version "24.3")
140
141 (defface Man-reverse
142 '((t (:inherit highlight)))
143 "Face to use when fontifying reverse video."
144 :group 'man
145 :version "24.3")
146
147 (defvar Man-ansi-color-map (let ((ansi-color-faces-vector
148 [ default Man-overstrike default Man-underline
149 Man-underline default default Man-reverse ]))
150 (ansi-color-make-color-map))
151 "The value used here for `ansi-color-map'.")
152
153 ;; Use the value of the obsolete user option Man-notify, if set.
154 (defcustom Man-notify-method (if (boundp 'Man-notify) Man-notify 'friendly)
155 "Selects the behavior when manpage is ready.
156 This variable may have one of the following values, where (sf) means
157 that the frames are switched, so the manpage is displayed in the frame
158 where the man command was called from:
159
160 newframe -- put the manpage in its own frame (see `Man-frame-parameters')
161 pushy -- make the manpage the current buffer in the current window
162 bully -- make the manpage the current buffer and only window (sf)
163 aggressive -- make the manpage the current buffer in the other window (sf)
164 friendly -- display manpage in the other window but don't make current (sf)
165 polite -- don't display manpage, but prints message and beep when ready
166 quiet -- like `polite', but don't beep
167 meek -- make no indication that the manpage is ready
168
169 Any other value of `Man-notify-method' is equivalent to `meek'."
170 :type '(radio (const newframe) (const pushy) (const bully)
171 (const aggressive) (const friendly)
172 (const polite) (const quiet) (const meek))
173 :group 'man)
174
175 (defcustom Man-width nil
176 "Number of columns for which manual pages should be formatted.
177 If nil, the width of the window selected at the moment of man
178 invocation is used. If non-nil, the width of the frame selected
179 at the moment of man invocation is used. The value also can be a
180 positive integer."
181 :type '(choice (const :tag "Window width" nil)
182 (const :tag "Frame width" t)
183 (integer :tag "Specific width" :value 65))
184 :group 'man)
185
186 (defcustom Man-frame-parameters nil
187 "Frame parameter list for creating a new frame for a manual page."
188 :type 'sexp
189 :group 'man)
190
191 (defcustom Man-downcase-section-letters-flag t
192 "Non-nil means letters in sections are converted to lower case.
193 Some Un*x man commands can't handle uppercase letters in sections, for
194 example \"man 2V chmod\", but they are often displayed in the manpage
195 with the upper case letter. When this variable is t, the section
196 letter (e.g., \"2V\") is converted to lowercase (e.g., \"2v\") before
197 being sent to the man background process."
198 :type 'boolean
199 :group 'man)
200
201 (defcustom Man-circular-pages-flag t
202 "Non-nil means the manpage list is treated as circular for traversal."
203 :type 'boolean
204 :group 'man)
205
206 (defcustom Man-section-translations-alist
207 (list
208 '("3C++" . "3")
209 ;; Some systems have a real 3x man section, so let's comment this.
210 ;; '("3X" . "3") ; Xlib man pages
211 '("3X11" . "3")
212 '("1-UCB" . ""))
213 "Association list of bogus sections to real section numbers.
214 Some manpages (e.g. the Sun C++ 2.1 manpages) have section numbers in
215 their references which Un*x `man' does not recognize. This
216 association list is used to translate those sections, when found, to
217 the associated section number."
218 :type '(repeat (cons (string :tag "Bogus Section")
219 (string :tag "Real Section")))
220 :group 'man)
221
222 ;; FIXME see comments at ffap-c-path.
223 (defcustom Man-header-file-path
224 (let ((arch (with-temp-buffer
225 (when (eq 0 (ignore-errors
226 (call-process "gcc" nil '(t nil) nil
227 "-print-multiarch")))
228 (goto-char (point-min))
229 (buffer-substring (point) (line-end-position)))))
230 (base '("/usr/include" "/usr/local/include")))
231 (if (zerop (length arch))
232 base
233 (append base (list (expand-file-name arch "/usr/include")))))
234 "C Header file search path used in Man."
235 :version "24.1" ; add multiarch
236 :type '(repeat string)
237 :group 'man)
238
239 (defcustom Man-name-local-regexp (concat "^" (regexp-opt '("NOM" "NAME")) "$")
240 "Regexp that matches the text that precedes the command's name.
241 Used in `bookmark-set' to get the default bookmark name."
242 :version "24.1"
243 :type 'string :group 'bookmark)
244
245 (defcustom manual-program "man"
246 "Program used by `man' to produce man pages."
247 :type 'string
248 :group 'man)
249
250 (defcustom Man-untabify-command "pr"
251 "Program used by `man' for untabifying."
252 :type 'string
253 :group 'man)
254
255 (defcustom Man-untabify-command-args (list "-t" "-e")
256 "List of arguments to be passed to `Man-untabify-command' (which see)."
257 :type '(repeat string)
258 :group 'man)
259
260 (defcustom Man-sed-command "sed"
261 "Program used by `man' to process sed scripts."
262 :type 'string
263 :group 'man)
264
265 (defcustom Man-awk-command "awk"
266 "Program used by `man' to process awk scripts."
267 :type 'string
268 :group 'man)
269
270 (defcustom Man-mode-hook nil
271 "Hook run when Man mode is enabled."
272 :type 'hook
273 :group 'man)
274
275 (defcustom Man-cooked-hook nil
276 "Hook run after removing backspaces but before `Man-mode' processing."
277 :type 'hook
278 :group 'man)
279
280 (defvar Man-name-regexp "[-a-zA-Z0-9_­+][-a-zA-Z0-9_.:­+]*"
281 "Regular expression describing the name of a manpage (without section).")
282
283 (defvar Man-section-regexp "[0-9][a-zA-Z0-9+]*\\|[LNln]"
284 "Regular expression describing a manpage section within parentheses.")
285
286 (defvar Man-page-header-regexp
287 (if (string-match "-solaris2\\." system-configuration)
288 (concat "^[-A-Za-z0-9_].*[ \t]\\(" Man-name-regexp
289 "(\\(" Man-section-regexp "\\))\\)$")
290 (concat "^[ \t]*\\(" Man-name-regexp
291 "(\\(" Man-section-regexp "\\))\\).*\\1"))
292 "Regular expression describing the heading of a page.")
293
294 (defvar Man-heading-regexp "^\\([A-Z][A-Z0-9 /-]+\\)$"
295 "Regular expression describing a manpage heading entry.")
296
297 (defvar Man-see-also-regexp "SEE ALSO"
298 "Regular expression for SEE ALSO heading (or your equivalent).
299 This regexp should not start with a `^' character.")
300
301 ;; This used to have leading space [ \t]*, but was removed because it
302 ;; causes false page splits on an occasional NAME with leading space
303 ;; inside a manpage. And `Man-heading-regexp' doesn't have [ \t]* anyway.
304 (defvar Man-first-heading-regexp "^NAME$\\|^[ \t]*No manual entry fo.*$"
305 "Regular expression describing first heading on a manpage.
306 This regular expression should start with a `^' character.")
307
308 (defvar Man-reference-regexp
309 (concat "\\(" Man-name-regexp
310 "\\(\n[ \t]+" Man-name-regexp "\\)*\\)[ \t]*(\\("
311 Man-section-regexp "\\))")
312 "Regular expression describing a reference to another manpage.")
313
314 (defvar Man-apropos-regexp
315 (concat "\\\[\\(" Man-name-regexp "\\)\\\][ \t]*(\\(" Man-section-regexp "\\))")
316 "Regular expression describing a reference to manpages in \"man -k output\".")
317
318 (defvar Man-synopsis-regexp "SYNOPSIS"
319 "Regular expression for SYNOPSIS heading (or your equivalent).
320 This regexp should not start with a `^' character.")
321
322 (defvar Man-files-regexp "FILES\\>"
323 ;; Add \> so as not to match mount(8)'s FILESYSTEM INDEPENDENT MOUNT OPTIONS.
324 "Regular expression for FILES heading (or your equivalent).
325 This regexp should not start with a `^' character.")
326
327 (defvar Man-include-regexp "#[ \t]*include[ \t]*"
328 "Regular expression describing the #include (directive of cpp).")
329
330 (defvar Man-file-name-regexp "[^<>\", \t\n]+"
331 "Regular expression describing <> in #include line (directive of cpp).")
332
333 (defvar Man-normal-file-prefix-regexp "[/~$]"
334 "Regular expression describing a file path appeared in FILES section.")
335
336 (defvar Man-header-regexp
337 (concat "\\(" Man-include-regexp "\\)"
338 "[<\"]"
339 "\\(" Man-file-name-regexp "\\)"
340 "[>\"]")
341 "Regular expression describing references to header files.")
342
343 (defvar Man-normal-file-regexp
344 (concat Man-normal-file-prefix-regexp Man-file-name-regexp)
345 "Regular expression describing references to normal files.")
346
347 ;; This includes the section as an optional part to catch hyphenated
348 ;; references to manpages.
349 (defvar Man-hyphenated-reference-regexp
350 (concat "\\(" Man-name-regexp "\\)\\((\\(" Man-section-regexp "\\))\\)?")
351 "Regular expression describing a reference in the SEE ALSO section.")
352
353 (defcustom Man-switches ""
354 "Switches passed to the man command, as a single string.
355 For example, the -a switch lets you see all the manpages for a
356 specified subject, if your `man' program supports it."
357 :type 'string
358 :group 'man)
359
360 (defvar Man-specified-section-option
361 (if (string-match "-solaris[0-9.]*$" system-configuration)
362 "-s"
363 "")
364 "Option that indicates a specified a manual section name.")
365
366 (defvar Man-support-local-filenames 'auto-detect
367 "Internal cache for the value of the function `Man-support-local-filenames'.
368 `auto-detect' means the value is not yet determined.
369 Otherwise, the value is whatever the function
370 `Man-support-local-filenames' should return.")
371
372 (defcustom man-imenu-title "Contents"
373 "The title to use if man adds a Contents menu to the menubar."
374 :version "24.4"
375 :type 'string
376 :group 'man)
377
378 \f
379 ;; other variables and keymap initializations
380 (defvar Man-original-frame)
381 (make-variable-buffer-local 'Man-original-frame)
382 (defvar Man-arguments)
383 (make-variable-buffer-local 'Man-arguments)
384 (put 'Man-arguments 'permanent-local t)
385
386 (defvar Man--sections nil)
387 (make-variable-buffer-local 'Man--sections)
388 (defvar Man--refpages nil)
389 (make-variable-buffer-local 'Man--refpages)
390 (defvar Man-page-list nil)
391 (make-variable-buffer-local 'Man-page-list)
392 (defvar Man-current-page 0)
393 (make-variable-buffer-local 'Man-current-page)
394 (defvar Man-page-mode-string "1 of 1")
395 (make-variable-buffer-local 'Man-page-mode-string)
396
397 (defconst Man-sysv-sed-script "\
398 /\b/ { s/_\b//g
399 s/\b_//g
400 s/o\b+/o/g
401 s/+\bo/o/g
402 :ovstrk
403 s/\\(.\\)\b\\1/\\1/g
404 t ovstrk
405 }
406 /\e\\[[0-9][0-9]*m/ s///g"
407 "Script for sysV-like sed to nuke backspaces and ANSI codes from manpages.")
408
409 (defconst Man-berkeley-sed-script "\
410 /\b/ { s/_\b//g\\
411 s/\b_//g\\
412 s/o\b+/o/g\\
413 s/+\bo/o/g\\
414 :ovstrk\\
415 s/\\(.\\)\b\\1/\\1/g\\
416 t ovstrk\\
417 }\\
418 /\e\\[[0-9][0-9]*m/ s///g"
419 "Script for berkeley-like sed to nuke backspaces and ANSI codes from manpages.")
420
421 (defvar Man-topic-history nil "Topic read history.")
422
423 (defvar Man-mode-syntax-table
424 (let ((table (copy-syntax-table (standard-syntax-table))))
425 (modify-syntax-entry ?. "w" table)
426 (modify-syntax-entry ?_ "w" table)
427 (modify-syntax-entry ?: "w" table) ; for PDL::Primitive in Perl man pages
428 table)
429 "Syntax table used in Man mode buffers.")
430
431 (defvar Man-mode-map
432 (let ((map (make-sparse-keymap)))
433 (suppress-keymap map)
434 (set-keymap-parent map button-buffer-map)
435
436 (define-key map [?\S-\ ] 'scroll-down-command)
437 (define-key map " " 'scroll-up-command)
438 (define-key map "\177" 'scroll-down-command)
439 (define-key map "n" 'Man-next-section)
440 (define-key map "p" 'Man-previous-section)
441 (define-key map "\en" 'Man-next-manpage)
442 (define-key map "\ep" 'Man-previous-manpage)
443 (define-key map ">" 'end-of-buffer)
444 (define-key map "<" 'beginning-of-buffer)
445 (define-key map "." 'beginning-of-buffer)
446 (define-key map "r" 'Man-follow-manual-reference)
447 (define-key map "g" 'Man-goto-section)
448 (define-key map "s" 'Man-goto-see-also-section)
449 (define-key map "k" 'Man-kill)
450 (define-key map "q" 'Man-quit)
451 (define-key map "m" 'man)
452 ;; Not all the man references get buttons currently. The text in the
453 ;; manual page can contain references to other man pages
454 (define-key map "\r" 'man-follow)
455 (define-key map "?" 'describe-mode)
456
457 (easy-menu-define nil map
458 "`Man-mode' menu."
459 '("Man"
460 ["Next Section" Man-next-section t]
461 ["Previous Section" Man-previous-section t]
462 ["Go To Section..." Man-goto-section t]
463 ["Go To \"SEE ALSO\" Section" Man-goto-see-also-section
464 :active (cl-member Man-see-also-regexp Man--sections
465 :test #'string-match-p)]
466 ["Follow Reference..." Man-follow-manual-reference
467 :active Man--refpages
468 :help "Go to a manpage referred to in the \"SEE ALSO\" section"]
469 "--"
470 ["Next Manpage" Man-next-manpage
471 :active (> (length Man-page-list) 1)]
472 ["Previous Manpage" Man-previous-manpage
473 :active (> (length Man-page-list) 1)]
474 "--"
475 ["Man..." man t]
476 ["Kill Buffer" Man-kill t]
477 ["Quit" Man-quit t]))
478 map)
479 "Keymap for Man mode.")
480
481 ;; buttons
482 (define-button-type 'Man-abstract-xref-man-page
483 'follow-link t
484 'help-echo "mouse-2, RET: display this man page"
485 'func nil
486 'action #'Man-xref-button-action)
487
488 (defun Man-xref-button-action (button)
489 (let ((target (button-get button 'Man-target-string)))
490 (funcall
491 (button-get button 'func)
492 (cond ((null target)
493 (button-label button))
494 ((functionp target)
495 (funcall target (button-start button)))
496 (t target)))))
497
498 (define-button-type 'Man-xref-man-page
499 :supertype 'Man-abstract-xref-man-page
500 'func 'man-follow)
501
502
503 (define-button-type 'Man-xref-header-file
504 'action (lambda (button)
505 (let ((w (button-get button 'Man-target-string)))
506 (unless (Man-view-header-file w)
507 (error "Cannot find header file: %s" w))))
508 'follow-link t
509 'help-echo "mouse-2: display this header file")
510
511 (define-button-type 'Man-xref-normal-file
512 'action (lambda (button)
513 (let ((f (substitute-in-file-name
514 (button-get button 'Man-target-string))))
515 (if (file-exists-p f)
516 (if (file-readable-p f)
517 (view-file f)
518 (error "Cannot read a file: %s" f))
519 (error "Cannot find a file: %s" f))))
520 'follow-link t
521 'help-echo "mouse-2: display this file")
522
523 \f
524 ;; ======================================================================
525 ;; utilities
526
527 (defun Man-init-defvars ()
528 "Used for initializing variables based on display's color support.
529 This is necessary if one wants to dump man.el with Emacs."
530
531 ;; Avoid possible error in call-process by using a directory that must exist.
532 (let ((default-directory "/"))
533 (setq Man-sed-script
534 (cond
535 (Man-fontify-manpage-flag
536 nil)
537 ((eq 0 (call-process Man-sed-command nil nil nil Man-sysv-sed-script))
538 Man-sysv-sed-script)
539 ((eq 0 (call-process Man-sed-command nil nil nil Man-berkeley-sed-script))
540 Man-berkeley-sed-script)
541 (t
542 nil))))
543
544 (setq Man-filter-list
545 ;; Avoid trailing nil which confuses customize.
546 (apply 'list
547 (cons
548 Man-sed-command
549 (if (eq system-type 'windows-nt)
550 ;; Windows needs ".." quoting, not '..'.
551 (list
552 "-e \"/Reformatting page. Wait/d\""
553 "-e \"/Reformatting entry. Wait/d\""
554 "-e \"/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d\""
555 "-e \"/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d\""
556 "-e \"/^Printed[ \t][0-9].*[0-9]$/d\""
557 "-e \"/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d\""
558 "-e \"/^[A-Za-z].*Last[ \t]change:/d\""
559 "-e \"/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d\""
560 "-e \"/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d\"")
561 (list
562 (if Man-sed-script
563 (concat "-e '" Man-sed-script "'")
564 "")
565 "-e '/^[\001-\032][\001-\032]*$/d'"
566 "-e '/\e[789]/s///g'"
567 "-e '/Reformatting page. Wait/d'"
568 "-e '/Reformatting entry. Wait/d'"
569 "-e '/^[ \t]*Hewlett-Packard[ \t]Company[ \t]*-[ \t][0-9]*[ \t]-/d'"
570 "-e '/^[ \t]*Hewlett-Packard[ \t]*-[ \t][0-9]*[ \t]-.*$/d'"
571 "-e '/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d'"
572 "-e '/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d'"
573 "-e '/^Printed[ \t][0-9].*[0-9]$/d'"
574 "-e '/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d'"
575 "-e '/^[A-Za-z].*Last[ \t]change:/d'"
576 "-e '/^Sun[ \t]Release[ \t][0-9].*[0-9]$/d'"
577 "-e '/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d'"
578 "-e '/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d'"
579 )))
580 ;; Windows doesn't support multi-line commands, so don't
581 ;; invoke Awk there.
582 (unless (eq system-type 'windows-nt)
583 (cons
584 Man-awk-command
585 (list
586 "'\n"
587 "BEGIN { blankline=0; anonblank=0; }\n"
588 "/^$/ { if (anonblank==0) next; }\n"
589 "{ anonblank=1; }\n"
590 "/^$/ { blankline++; next; }\n"
591 "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }\n"
592 "'"
593 )))
594 (if (not Man-uses-untabify-flag)
595 ;; The outer list will be stripped off by apply.
596 (list (cons
597 Man-untabify-command
598 Man-untabify-command-args))
599 )))
600 )
601
602 (defsubst Man-make-page-mode-string ()
603 "Formats part of the mode line for Man mode."
604 (format "%s page %d of %d"
605 (or (nth 2 (nth (1- Man-current-page) Man-page-list))
606 "")
607 Man-current-page
608 (length Man-page-list)))
609
610 (defsubst Man-build-man-command ()
611 "Builds the entire background manpage and cleaning command."
612 (let ((command (concat manual-program " " Man-switches
613 (cond
614 ;; Already has %s
615 ((string-match "%s" manual-program) "")
616 ;; Stock MS-DOS shells cannot redirect stderr;
617 ;; `call-process' below sends it to /dev/null,
618 ;; so we don't need `2>' even with DOS shells
619 ;; which do support stderr redirection.
620 ((not (fboundp 'start-process)) " %s")
621 ((concat " %s 2>" null-device)))))
622 (flist Man-filter-list))
623 (while (and flist (car flist))
624 (let ((pcom (car (car flist)))
625 (pargs (cdr (car flist))))
626 (setq command
627 (concat command " | " pcom " "
628 (mapconcat (lambda (phrase)
629 (if (not (stringp phrase))
630 (error "Malformed Man-filter-list"))
631 phrase)
632 pargs " ")))
633 (setq flist (cdr flist))))
634 command))
635
636
637 (defun Man-translate-cleanup (string)
638 "Strip leading, trailing and middle spaces."
639 (when (stringp string)
640 ;; Strip leading and trailing
641 (if (string-match "^[ \t\f\r\n]*\\(.+[^ \t\f\r\n]\\)" string)
642 (setq string (match-string 1 string)))
643 ;; middle spaces
644 (setq string (replace-regexp-in-string "[\t\r\n]" " " string))
645 (setq string (replace-regexp-in-string " +" " " string))
646 string))
647
648 (defun Man-translate-references (ref)
649 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style.
650 Leave it as is if already in that style. Possibly downcase and
651 translate the section (see the `Man-downcase-section-letters-flag'
652 and the `Man-section-translations-alist' variables)."
653 (let ((name "")
654 (section "")
655 (slist Man-section-translations-alist))
656 (setq ref (Man-translate-cleanup ref))
657 (cond
658 ;; "chmod(2V)" case ?
659 ((string-match (concat "^" Man-reference-regexp "$") ref)
660 (setq name (replace-regexp-in-string "[\n\t ]" "" (match-string 1 ref))
661 section (match-string 3 ref)))
662 ;; "2v chmod" case ?
663 ((string-match (concat "^\\(" Man-section-regexp
664 "\\) +\\(" Man-name-regexp "\\)$") ref)
665 (setq name (match-string 2 ref)
666 section (match-string 1 ref))))
667 (if (string= name "")
668 ref ; Return the reference as is
669 (if Man-downcase-section-letters-flag
670 (setq section (downcase section)))
671 (while slist
672 (let ((s1 (car (car slist)))
673 (s2 (cdr (car slist))))
674 (setq slist (cdr slist))
675 (if Man-downcase-section-letters-flag
676 (setq s1 (downcase s1)))
677 (if (not (string= s1 section)) nil
678 (setq section (if Man-downcase-section-letters-flag
679 (downcase s2)
680 s2)
681 slist nil))))
682 (concat Man-specified-section-option section " " name))))
683
684 (defun Man-support-local-filenames ()
685 "Return non-nil if the man command supports local filenames.
686 Different man programs support this feature in different ways.
687 The default Debian man program (\"man-db\") has a `--local-file'
688 \(or `-l') option for this purpose. The default Red Hat man
689 program has no such option, but interprets any name containing
690 a \"/\" as a local filename. The function returns either `man-db'
691 `man', or nil."
692 (if (eq Man-support-local-filenames 'auto-detect)
693 (setq Man-support-local-filenames
694 (with-temp-buffer
695 (let ((default-directory
696 ;; Ensure that `default-directory' exists and is readable.
697 (if (and (file-directory-p default-directory)
698 (file-readable-p default-directory))
699 default-directory
700 (expand-file-name "~/"))))
701 (ignore-errors
702 (call-process manual-program nil t nil "--help")))
703 (cond ((search-backward "--local-file" nil 'move)
704 'man-db)
705 ;; This feature seems to be present in at least ver 1.4f,
706 ;; which is about 20 years old.
707 ;; I don't know if this version has an official name?
708 ((looking-at "^man, versione? [1-9]")
709 'man))))
710 Man-support-local-filenames))
711
712 \f
713 ;; ======================================================================
714 ;; default man entry: get word near point
715
716 (defun Man-default-man-entry (&optional pos)
717 "Guess default manual entry based on the text near position POS.
718 POS defaults to `point'."
719 (let (word start column distance)
720 (save-excursion
721 (when pos (goto-char pos))
722 (setq pos (point))
723 ;; The default title is the nearest entry-like object before or
724 ;; after POS.
725 (if (and (skip-chars-backward " \ta-zA-Z0-9+")
726 (not (zerop (skip-chars-backward "(")))
727 ;; Try to handle the special case where POS is on a
728 ;; section number.
729 (looking-at
730 (concat "([ \t]*\\(" Man-section-regexp "\\)[ \t]*)"))
731 ;; We skipped a valid section number backwards, look at
732 ;; preceding text.
733 (or (and (skip-chars-backward ",; \t")
734 (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:"))))
735 ;; Not a valid entry, move POS after closing paren.
736 (not (setq pos (match-end 0)))))
737 ;; We have a candidate, make `start' record its starting
738 ;; position.
739 (setq start (point))
740 ;; Otherwise look at char before POS.
741 (goto-char pos)
742 (if (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:")))
743 ;; Our candidate is just before or around POS.
744 (setq start (point))
745 ;; Otherwise record the current column and look backwards.
746 (setq column (current-column))
747 (skip-chars-backward ",; \t")
748 ;; Record the distance traveled.
749 (setq distance (- column (current-column)))
750 (when (looking-back
751 (concat "([ \t]*\\(?:" Man-section-regexp "\\)[ \t]*)"))
752 ;; Skip section number backwards.
753 (goto-char (match-beginning 0))
754 (skip-chars-backward " \t"))
755 (if (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:")))
756 (progn
757 ;; We have a candidate before POS ...
758 (setq start (point))
759 (goto-char pos)
760 (if (and (skip-chars-forward ",; \t")
761 (< (- (current-column) column) distance)
762 (looking-at "[-a-zA-Z0-9._+:]"))
763 ;; ... but the one after POS is better.
764 (setq start (point))
765 ;; ... and anything after POS is worse.
766 (goto-char start)))
767 ;; No candidate before POS.
768 (goto-char pos)
769 (skip-chars-forward ",; \t")
770 (setq start (point)))))
771 ;; We have found a suitable starting point, try to skip at least
772 ;; one character.
773 (skip-chars-forward "-a-zA-Z0-9._+:")
774 (setq word (buffer-substring-no-properties start (point)))
775 ;; If there is a continuation at the end of line, check the
776 ;; following line too, eg:
777 ;; see this-
778 ;; command-here(1)
779 ;; Note: This code gets executed iff our entry is after POS.
780 (when (looking-at "[ \t\r\n]+\\([-a-zA-Z0-9._+:]+\\)([0-9])")
781 (setq word (concat word (match-string-no-properties 1)))
782 ;; Make sure the section number gets included by the code below.
783 (goto-char (match-end 1)))
784 (when (string-match "[-._]+$" word)
785 (setq word (substring word 0 (match-beginning 0))))
786 ;; The following was commented out since the preceding code
787 ;; should not produce a leading "*" in the first place.
788 ;;; ;; If looking at something like *strcat(... , remove the '*'
789 ;;; (when (string-match "^*" word)
790 ;;; (setq word (substring word 1)))
791 (concat
792 word
793 (and (not (string-equal word ""))
794 ;; If looking at something like ioctl(2) or brc(1M),
795 ;; include the section number in the returned value.
796 (looking-at
797 (concat "[ \t]*([ \t]*\\(" Man-section-regexp "\\)[ \t]*)"))
798 (format "(%s)" (match-string-no-properties 1)))))))
799
800 \f
801 ;; ======================================================================
802 ;; Top level command and background process sentinel
803
804 ;; For compatibility with older versions.
805 ;;;###autoload
806 (defalias 'manual-entry 'man)
807
808 (defvar Man-completion-cache nil
809 ;; On my machine, "man -k" is so fast that a cache makes no sense,
810 ;; but apparently that's not the case in all cases, so let's add a cache.
811 "Cache of completion table of the form (PREFIX . TABLE).")
812
813 (defvar Man-man-k-use-anchor
814 ;; man-db or man-1.*
815 (memq system-type '(gnu gnu/linux gnu/kfreebsd))
816 "If non-nil prepend ^ to the prefix passed to \"man -k\" for completion.
817 The value should be nil if \"man -k ^PREFIX\" may omit some man
818 pages whose names start with PREFIX.
819
820 Currently, the default value depends on `system-type' and is
821 non-nil where the standard man programs are known to behave
822 properly. Setting the value to nil always gives correct results
823 but computing the list of completions may take a bit longer.")
824
825 (defun Man-parse-man-k ()
826 "Parse \"man -k\" output and return the list of page names.
827
828 The current buffer should contain the output of a command of the
829 form \"man -k keyword\", which is traditionally also available with
830 apropos(1).
831
832 While POSIX man(1p) is a bit vague about what to expect here,
833 this function tries to parse some commonly used formats, which
834 can be described in the following informal way, with square brackets
835 indicating optional parts and whitespace being interpreted
836 somewhat loosely.
837
838 foo[, bar [, ...]] [other stuff] (sec) - description
839 foo(sec)[, bar(sec) [, ...]] [other stuff] - description
840
841 For more details and some regression tests, please see
842 test/automated/man-tests.el in the emacs bzr repository."
843 (goto-char (point-min))
844 ;; See man-tests for data about which systems use which format (hopefully we
845 ;; will be able to simplify the code if/when some of those formats aren't
846 ;; used any more).
847 (let (table)
848 (while (search-forward-regexp "^\\([^ \t,\n]+\\)\\(.*?\\)\
849 \\(?:[ \t]\\(([^ \t,\n]+?)\\)\\)?\\(?:[ \t]+- ?\\(.*\\)\\)?$" nil t)
850 (let ((section (match-string 3))
851 (description (match-string 4))
852 (bound (match-end 2)))
853 (goto-char (match-end 1))
854 (while
855 (progn
856 ;; The first regexp grouping may already match the section
857 ;; tacked on to the name, which is ok since for the formats we
858 ;; claim to support the third (non-shy) grouping does not
859 ;; match in this case, i.e., section is nil.
860 (push (propertize (concat (match-string 1) section)
861 'help-echo description)
862 table)
863 (search-forward-regexp "\\=, *\\([^ \t,]+\\)" bound t)))))
864 (nreverse table)))
865
866 (defun Man-completion-table (string pred action)
867 (cond
868 ;; This ends up returning t for pretty much any string, and hence leads to
869 ;; spurious "complete but not unique" messages. And since `man' doesn't
870 ;; require-match anyway, there's not point being clever.
871 ;;((eq action 'lambda) (not (string-match "([^)]*\\'" string)))
872 ((equal string "-k")
873 ;; Let SPC (minibuffer-complete-word) insert the space.
874 (complete-with-action action '("-k ") string pred))
875 (t
876 (let ((table (cdr Man-completion-cache))
877 (section nil)
878 (prefix string))
879 (when (string-match "\\`\\([[:digit:]].*?\\) " string)
880 (setq section (match-string 1 string))
881 (setq prefix (substring string (match-end 0))))
882 (unless (and Man-completion-cache
883 (string-prefix-p (car Man-completion-cache) prefix))
884 (with-temp-buffer
885 (setq default-directory "/") ;; in case inherited doesn't exist
886 ;; Actually for my `man' the arg is a regexp.
887 ;; POSIX says it must be ERE and "man-db" seems to agree,
888 ;; whereas under MacOSX it seems to be BRE-style and doesn't
889 ;; accept backslashes at all. Let's not bother to
890 ;; quote anything.
891 (let ((process-environment (copy-sequence process-environment)))
892 (setenv "COLUMNS" "999") ;; don't truncate long names
893 ;; manual-program might not even exist. And since it's
894 ;; run differently in Man-getpage-in-background, an error
895 ;; here may not necessarily mean that we'll also get an
896 ;; error later.
897 (ignore-errors
898 (call-process manual-program nil '(t nil) nil
899 "-k" (concat (when (or Man-man-k-use-anchor
900 (string-equal prefix ""))
901 "^")
902 prefix))))
903 (setq table (Man-parse-man-k)))
904 ;; Cache the table for later reuse.
905 (setq Man-completion-cache (cons prefix table)))
906 ;; The table may contain false positives since the match is made
907 ;; by "man -k" not just on the manpage's name.
908 (if section
909 (let ((re (concat "(" (regexp-quote section) ")\\'")))
910 (dolist (comp (prog1 table (setq table nil)))
911 (if (string-match re comp)
912 (push (substring comp 0 (match-beginning 0)) table)))
913 (completion-table-with-context (concat section " ") table
914 prefix pred action))
915 ;; If the current text looks like a possible section name,
916 ;; then add a completion entry that just adds a space so SPC
917 ;; can be used to insert a space.
918 (if (string-match "\\`[[:digit:]]" string)
919 (push (concat string " ") table))
920 (let ((res (complete-with-action action table string pred)))
921 ;; In case we're completing to a single name that exists in
922 ;; several sections, the longest prefix will look like "foo(".
923 (if (and (stringp res)
924 (string-match "([^(]*\\'" res)
925 ;; In case the paren was already in `prefix', don't
926 ;; remove it.
927 (> (match-beginning 0) (length prefix)))
928 (substring res 0 (match-beginning 0))
929 res)))))))
930
931 ;;;###autoload
932 (defun man (man-args)
933 "Get a Un*x manual page and put it in a buffer.
934 This command is the top-level command in the man package. It
935 runs a Un*x command to retrieve and clean a manpage in the
936 background and places the results in a `Man-mode' browsing
937 buffer. See variable `Man-notify-method' for what happens when
938 the buffer is ready. If a buffer already exists for this man
939 page, it will display immediately.
940
941 For a manpage from a particular section, use either of the
942 following. \"cat(1)\" is how cross-references appear and is
943 passed to man as \"1 cat\".
944
945 cat(1)
946 1 cat
947
948 To see manpages from all sections related to a subject, use an
949 \"all pages\" option (which might be \"-a\" if it's not the
950 default), then step through with `Man-next-manpage' (\\<Man-mode-map>\\[Man-next-manpage]) etc.
951 Add to `Man-switches' to make this option permanent.
952
953 -a chmod
954
955 An explicit filename can be given too. Use -l if it might
956 otherwise look like a page name.
957
958 /my/file/name.1.gz
959 -l somefile.1
960
961 An \"apropos\" query with -k gives a buffer of matching page
962 names or descriptions. The pattern argument is usually an
963 \"egrep\" style regexp.
964
965 -k pattern"
966
967 (interactive
968 (list (let* ((default-entry (Man-default-man-entry))
969 ;; ignore case because that's friendly for bizarre
970 ;; caps things like the X11 function names and because
971 ;; "man" itself is case-insensitive on the command line
972 ;; so you're accustomed not to bother about the case
973 ;; ("man -k" is case-insensitive similarly, so the
974 ;; table has everything available to complete)
975 (completion-ignore-case t)
976 Man-completion-cache ;Don't cache across calls.
977 (input (completing-read
978 (format "Manual entry%s"
979 (if (string= default-entry "")
980 ": "
981 (format " (default %s): " default-entry)))
982 'Man-completion-table
983 nil nil nil 'Man-topic-history default-entry)))
984 (if (string= input "")
985 (error "No man args given")
986 input))))
987
988 ;; Possibly translate the "subject(section)" syntax into the
989 ;; "section subject" syntax and possibly downcase the section.
990 (setq man-args (Man-translate-references man-args))
991
992 (Man-getpage-in-background man-args))
993
994 ;;;###autoload
995 (defun man-follow (man-args)
996 "Get a Un*x manual page of the item under point and put it in a buffer."
997 (interactive (list (Man-default-man-entry)))
998 (if (or (not man-args)
999 (string= man-args ""))
1000 (error "No item under point")
1001 (man man-args)))
1002
1003 (defun Man-getpage-in-background (topic)
1004 "Use TOPIC to build and fire off the manpage and cleaning command.
1005 Return the buffer in which the manpage will appear."
1006 (let* ((man-args topic)
1007 (bufname (concat "*Man " man-args "*"))
1008 (buffer (get-buffer bufname)))
1009 (if buffer
1010 (Man-notify-when-ready buffer)
1011 (require 'env)
1012 (message "Invoking %s %s in the background" manual-program man-args)
1013 (setq buffer (generate-new-buffer bufname))
1014 (with-current-buffer buffer
1015 (setq buffer-undo-list t)
1016 (setq Man-original-frame (selected-frame))
1017 (setq Man-arguments man-args))
1018 (let ((process-environment (copy-sequence process-environment))
1019 ;; The following is so Awk script gets \n intact
1020 ;; But don't prevent decoding of the outside.
1021 (coding-system-for-write 'raw-text-unix)
1022 ;; We must decode the output by a coding system that the
1023 ;; system's locale suggests in multibyte mode.
1024 (coding-system-for-read
1025 (if (default-value 'enable-multibyte-characters)
1026 locale-coding-system 'raw-text-unix))
1027 ;; Avoid possible error by using a directory that always exists.
1028 (default-directory
1029 (if (and (file-directory-p default-directory)
1030 (not (find-file-name-handler default-directory
1031 'file-directory-p)))
1032 default-directory
1033 "/")))
1034 ;; Prevent any attempt to use display terminal fanciness.
1035 (setenv "TERM" "dumb")
1036 ;; In Debian Woody, at least, we get overlong lines under X
1037 ;; unless COLUMNS or MANWIDTH is set. This isn't a problem on
1038 ;; a tty. man(1) says:
1039 ;; MANWIDTH
1040 ;; If $MANWIDTH is set, its value is used as the line
1041 ;; length for which manual pages should be formatted.
1042 ;; If it is not set, manual pages will be formatted
1043 ;; with a line length appropriate to the current ter-
1044 ;; minal (using an ioctl(2) if available, the value of
1045 ;; $COLUMNS, or falling back to 80 characters if nei-
1046 ;; ther is available).
1047 (when (or window-system
1048 (not (or (getenv "MANWIDTH") (getenv "COLUMNS"))))
1049 ;; This isn't strictly correct, since we don't know how
1050 ;; the page will actually be displayed, but it seems
1051 ;; reasonable.
1052 (setenv "COLUMNS" (number-to-string
1053 (cond
1054 ((and (integerp Man-width) (> Man-width 0))
1055 Man-width)
1056 (Man-width (frame-width))
1057 ((window-width))))))
1058 ;; Since man-db 2.4.3-1, man writes plain text with no escape
1059 ;; sequences when stdout is not a tty. In 2.5.0, the following
1060 ;; env-var was added to allow control of this (see Debian Bug#340673).
1061 (setenv "MAN_KEEP_FORMATTING" "1")
1062 (if (fboundp 'start-process)
1063 (set-process-sentinel
1064 (start-process manual-program buffer
1065 (if (memq system-type '(cygwin windows-nt))
1066 shell-file-name
1067 "sh")
1068 shell-command-switch
1069 (format (Man-build-man-command) man-args))
1070 'Man-bgproc-sentinel)
1071 (let ((exit-status
1072 (call-process shell-file-name nil (list buffer nil) nil
1073 shell-command-switch
1074 (format (Man-build-man-command) man-args)))
1075 (msg ""))
1076 (or (and (numberp exit-status)
1077 (= exit-status 0))
1078 (and (numberp exit-status)
1079 (setq msg
1080 (format "exited abnormally with code %d"
1081 exit-status)))
1082 (setq msg exit-status))
1083 (Man-bgproc-sentinel bufname msg)))))
1084 buffer))
1085
1086 (defun Man-notify-when-ready (man-buffer)
1087 "Notify the user when MAN-BUFFER is ready.
1088 See the variable `Man-notify-method' for the different notification behaviors."
1089 (let ((saved-frame (with-current-buffer man-buffer
1090 Man-original-frame)))
1091 (pcase Man-notify-method
1092 (`newframe
1093 ;; Since we run asynchronously, perhaps while Emacs is waiting
1094 ;; for input, we must not leave a different buffer current. We
1095 ;; can't rely on the editor command loop to reselect the
1096 ;; selected window's buffer.
1097 (save-excursion
1098 (let ((frame (make-frame Man-frame-parameters)))
1099 (set-window-buffer (frame-selected-window frame) man-buffer)
1100 (set-window-dedicated-p (frame-selected-window frame) t)
1101 (or (display-multi-frame-p frame)
1102 (select-frame frame)))))
1103 (`pushy
1104 (switch-to-buffer man-buffer))
1105 (`bully
1106 (and (frame-live-p saved-frame)
1107 (select-frame saved-frame))
1108 (pop-to-buffer man-buffer)
1109 (delete-other-windows))
1110 (`aggressive
1111 (and (frame-live-p saved-frame)
1112 (select-frame saved-frame))
1113 (pop-to-buffer man-buffer))
1114 (`friendly
1115 (and (frame-live-p saved-frame)
1116 (select-frame saved-frame))
1117 (display-buffer man-buffer 'not-this-window))
1118 (`polite
1119 (beep)
1120 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
1121 (`quiet
1122 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
1123 (_ ;; meek
1124 (message ""))
1125 )))
1126
1127 (defun Man-softhyphen-to-minus ()
1128 ;; \255 is SOFT HYPHEN in Latin-N. Versions of Debian man, at
1129 ;; least, emit it even when not in a Latin-N locale.
1130 (unless (eq t (compare-strings "latin-" 0 nil
1131 current-language-environment 0 6 t))
1132 (goto-char (point-min))
1133 (let ((str "\255"))
1134 (if enable-multibyte-characters
1135 (setq str (string-as-multibyte str)))
1136 (while (search-forward str nil t) (replace-match "-")))))
1137
1138 (defun Man-fontify-manpage ()
1139 "Convert overstriking and underlining to the correct fonts.
1140 Same for the ANSI bold and normal escape sequences."
1141 (interactive)
1142 (message "Please wait: formatting the %s man page..." Man-arguments)
1143 (goto-char (point-min))
1144 ;; Fontify ANSI escapes.
1145 (let ((ansi-color-apply-face-function
1146 (lambda (beg end face)
1147 (when face
1148 (put-text-property beg end 'face face))))
1149 (ansi-color-map Man-ansi-color-map))
1150 (ansi-color-apply-on-region (point-min) (point-max)))
1151 ;; Other highlighting.
1152 (let ((buffer-undo-list t))
1153 (if (< (buffer-size) (position-bytes (point-max)))
1154 ;; Multibyte characters exist.
1155 (progn
1156 (goto-char (point-min))
1157 (while (search-forward "__\b\b" nil t)
1158 (backward-delete-char 4)
1159 (put-text-property (point) (1+ (point)) 'face 'Man-underline))
1160 (goto-char (point-min))
1161 (while (search-forward "\b\b__" nil t)
1162 (backward-delete-char 4)
1163 (put-text-property (1- (point)) (point) 'face 'Man-underline))))
1164 (goto-char (point-min))
1165 (while (search-forward "_\b" nil t)
1166 (backward-delete-char 2)
1167 (put-text-property (point) (1+ (point)) 'face 'Man-underline))
1168 (goto-char (point-min))
1169 (while (search-forward "\b_" nil t)
1170 (backward-delete-char 2)
1171 (put-text-property (1- (point)) (point) 'face 'Man-underline))
1172 (goto-char (point-min))
1173 (while (re-search-forward "\\(.\\)\\(\b+\\1\\)+" nil t)
1174 (replace-match "\\1")
1175 (put-text-property (1- (point)) (point) 'face 'Man-overstrike))
1176 (goto-char (point-min))
1177 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t)
1178 (replace-match "o")
1179 (put-text-property (1- (point)) (point) 'face 'bold))
1180 (goto-char (point-min))
1181 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t)
1182 (replace-match "+")
1183 (put-text-property (1- (point)) (point) 'face 'bold))
1184 ;; When the header is longer than the manpage name, groff tries to
1185 ;; condense it to a shorter line interspersed with ^H. Remove ^H with
1186 ;; their preceding chars (but don't put Man-overstrike). (Bug#5566)
1187 (goto-char (point-min))
1188 (while (re-search-forward ".\b" nil t) (backward-delete-char 2))
1189 (goto-char (point-min))
1190 ;; Try to recognize common forms of cross references.
1191 (Man-highlight-references)
1192 (Man-softhyphen-to-minus)
1193 (goto-char (point-min))
1194 (while (re-search-forward Man-heading-regexp nil t)
1195 (put-text-property (match-beginning 0)
1196 (match-end 0)
1197 'face 'Man-overstrike)))
1198 (message "%s man page formatted" (Man-page-from-arguments Man-arguments)))
1199
1200 (defun Man-highlight-references (&optional xref-man-type)
1201 "Highlight the references on mouse-over.
1202 References include items in the SEE ALSO section,
1203 header file (#include <foo.h>), and files in FILES.
1204 If optional argument XREF-MAN-TYPE is non-nil, it used as the
1205 button type for items in SEE ALSO section. If it is nil, the
1206 default type, `Man-xref-man-page' is used for the buttons."
1207 ;; `Man-highlight-references' is used from woman.el, too.
1208 ;; woman.el doesn't set `Man-arguments'.
1209 (unless Man-arguments
1210 (setq Man-arguments ""))
1211 (if (string-match "-k " Man-arguments)
1212 (progn
1213 (Man-highlight-references0 nil Man-reference-regexp 1
1214 'Man-default-man-entry
1215 (or xref-man-type 'Man-xref-man-page))
1216 (Man-highlight-references0 nil Man-apropos-regexp 1
1217 'Man-default-man-entry
1218 (or xref-man-type 'Man-xref-man-page)))
1219 (Man-highlight-references0 Man-see-also-regexp Man-reference-regexp 1
1220 'Man-default-man-entry
1221 (or xref-man-type 'Man-xref-man-page))
1222 (Man-highlight-references0 Man-synopsis-regexp Man-header-regexp 0 2
1223 'Man-xref-header-file)
1224 (Man-highlight-references0 Man-files-regexp Man-normal-file-regexp 0 0
1225 'Man-xref-normal-file)))
1226
1227 (defun Man-highlight-references0 (start-section regexp button-pos target type)
1228 ;; Based on `Man-build-references-alist'
1229 (when (or (null start-section)
1230 (Man-find-section start-section))
1231 (let ((end (if start-section
1232 (progn
1233 (forward-line 1)
1234 (back-to-indentation)
1235 (save-excursion
1236 (Man-next-section 1)
1237 (point)))
1238 (goto-char (point-min))
1239 nil)))
1240 (while (re-search-forward regexp end t)
1241 ;; An overlay button is preferable because the underlying text
1242 ;; may have text property highlights (Bug#7881).
1243 (make-button
1244 (match-beginning button-pos)
1245 (match-end button-pos)
1246 'type type
1247 'Man-target-string (cond
1248 ((numberp target)
1249 (match-string target))
1250 ((functionp target)
1251 target)
1252 (t nil)))))))
1253
1254 (defun Man-cleanup-manpage (&optional interactive)
1255 "Remove overstriking and underlining from the current buffer.
1256 Normally skip any jobs that should have been done by the sed script,
1257 but when called interactively, do those jobs even if the sed
1258 script would have done them."
1259 (interactive "p")
1260 (message "Please wait: cleaning up the %s man page..."
1261 Man-arguments)
1262 (if (or interactive (not Man-sed-script))
1263 (progn
1264 (goto-char (point-min))
1265 (while (search-forward "_\b" nil t) (backward-delete-char 2))
1266 (goto-char (point-min))
1267 (while (search-forward "\b_" nil t) (backward-delete-char 2))
1268 (goto-char (point-min))
1269 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
1270 (replace-match "\\1"))
1271 (goto-char (point-min))
1272 (while (re-search-forward "\e\\[[0-9]+m" nil t) (replace-match ""))
1273 (goto-char (point-min))
1274 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t) (replace-match "o"))
1275 ))
1276 (goto-char (point-min))
1277 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t) (replace-match "+"))
1278 ;; When the header is longer than the manpage name, groff tries to
1279 ;; condense it to a shorter line interspersed with ^H. Remove ^H with
1280 ;; their preceding chars (but don't put Man-overstrike). (Bug#5566)
1281 (goto-char (point-min))
1282 (while (re-search-forward ".\b" nil t) (backward-delete-char 2))
1283 (Man-softhyphen-to-minus)
1284 (message "%s man page cleaned up" Man-arguments))
1285
1286 (defun Man-bgproc-sentinel (process msg)
1287 "Manpage background process sentinel.
1288 When manpage command is run asynchronously, PROCESS is the process
1289 object for the manpage command; when manpage command is run
1290 synchronously, PROCESS is the name of the buffer where the manpage
1291 command is run. Second argument MSG is the exit message of the
1292 manpage command."
1293 (let ((Man-buffer (if (stringp process) (get-buffer process)
1294 (process-buffer process)))
1295 (delete-buff nil)
1296 (err-mess nil))
1297
1298 (if (null (buffer-name Man-buffer)) ;; deleted buffer
1299 (or (stringp process)
1300 (set-process-buffer process nil))
1301
1302 (with-current-buffer Man-buffer
1303 (let ((case-fold-search nil))
1304 (goto-char (point-min))
1305 (cond ((or (looking-at "No \\(manual \\)*entry for")
1306 (looking-at "[^\n]*: nothing appropriate$"))
1307 (setq err-mess (buffer-substring (point)
1308 (progn
1309 (end-of-line) (point)))
1310 delete-buff t))
1311
1312 ;; "-k foo", successful exit, but no output (from man-db)
1313 ;; ENHANCE-ME: share the check for -k with
1314 ;; `Man-highlight-references'. The \\s- bits here are
1315 ;; meant to allow for multiple options with -k among them.
1316 ((and (string-match "\\(\\`\\|\\s-\\)-k\\s-" Man-arguments)
1317 (eq (process-status process) 'exit)
1318 (= (process-exit-status process) 0)
1319 (= (point-min) (point-max)))
1320 (setq err-mess (format "%s: no matches" Man-arguments)
1321 delete-buff t))
1322
1323 ((or (stringp process)
1324 (not (and (eq (process-status process) 'exit)
1325 (= (process-exit-status process) 0))))
1326 (or (zerop (length msg))
1327 (progn
1328 (setq err-mess
1329 (concat (buffer-name Man-buffer)
1330 ": process "
1331 (let ((eos (1- (length msg))))
1332 (if (= (aref msg eos) ?\n)
1333 (substring msg 0 eos) msg))))
1334 (goto-char (point-max))
1335 (insert (format "\nprocess %s" msg))))
1336 ))
1337 (if delete-buff
1338 (kill-buffer Man-buffer)
1339 (if Man-fontify-manpage-flag
1340 (Man-fontify-manpage)
1341 (Man-cleanup-manpage))
1342
1343 (run-hooks 'Man-cooked-hook)
1344 (Man-mode)
1345
1346 (if (not Man-page-list)
1347 (let ((args Man-arguments))
1348 (kill-buffer (current-buffer))
1349 (user-error "Can't find the %s manpage"
1350 (Man-page-from-arguments args)))
1351 (set-buffer-modified-p nil))))
1352 ;; Restore case-fold-search before calling
1353 ;; Man-notify-when-ready because it may switch buffers.
1354
1355 (if (not delete-buff)
1356 (Man-notify-when-ready Man-buffer))
1357
1358 (if err-mess
1359 (error "%s" err-mess))
1360 ))))
1361
1362 (defun Man-page-from-arguments (args)
1363 ;; Skip arguments and only print the page name.
1364 (mapconcat
1365 'identity
1366 (delete nil
1367 (mapcar
1368 (lambda (elem)
1369 (and (not (string-match "^-" elem))
1370 elem))
1371 (split-string args " ")))
1372 " "))
1373
1374 \f
1375 ;; ======================================================================
1376 ;; set up manual mode in buffer and build alists
1377
1378 (defvar bookmark-make-record-function)
1379
1380 (put 'Man-mode 'mode-class 'special)
1381
1382 (define-derived-mode Man-mode fundamental-mode "Man"
1383 "A mode for browsing Un*x manual pages.
1384
1385 The following man commands are available in the buffer. Try
1386 \"\\[describe-key] <key> RET\" for more information:
1387
1388 \\[man] Prompt to retrieve a new manpage.
1389 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
1390 \\[Man-next-manpage] Jump to next manpage in circular list.
1391 \\[Man-previous-manpage] Jump to previous manpage in circular list.
1392 \\[Man-next-section] Jump to next manpage section.
1393 \\[Man-previous-section] Jump to previous manpage section.
1394 \\[Man-goto-section] Go to a manpage section.
1395 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
1396 \\[Man-quit] Deletes the manpage window, bury its buffer.
1397 \\[Man-kill] Deletes the manpage window, kill its buffer.
1398 \\[describe-mode] Prints this help text.
1399
1400 The following variables may be of some use. Try
1401 \"\\[describe-variable] <variable-name> RET\" for more information:
1402
1403 `Man-notify-method' What happens when manpage formatting is done.
1404 `Man-downcase-section-letters-flag' Force section letters to lower case.
1405 `Man-circular-pages-flag' Treat multiple manpage list as circular.
1406 `Man-section-translations-alist' List of section numbers and their Un*x equiv.
1407 `Man-filter-list' Background manpage filter command.
1408 `Man-mode-map' Keymap bindings for Man mode buffers.
1409 `Man-mode-hook' Normal hook run on entry to Man mode.
1410 `Man-section-regexp' Regexp describing manpage section letters.
1411 `Man-heading-regexp' Regexp describing section headers.
1412 `Man-see-also-regexp' Regexp for SEE ALSO section (or your equiv).
1413 `Man-first-heading-regexp' Regexp for first heading on a manpage.
1414 `Man-reference-regexp' Regexp matching a references in SEE ALSO.
1415 `Man-switches' Background `man' command switches.
1416
1417 The following key bindings are currently in effect in the buffer:
1418 \\{Man-mode-map}"
1419 (setq buffer-auto-save-file-name nil
1420 mode-line-buffer-identification
1421 (list (default-value 'mode-line-buffer-identification)
1422 " {" 'Man-page-mode-string "}")
1423 truncate-lines t
1424 buffer-read-only t)
1425 (buffer-disable-undo)
1426 (auto-fill-mode -1)
1427 (setq imenu-generic-expression (list (list nil Man-heading-regexp 0)))
1428 (imenu-add-to-menubar man-imenu-title)
1429 (set (make-local-variable 'outline-regexp) Man-heading-regexp)
1430 (set (make-local-variable 'outline-level) (lambda () 1))
1431 (set (make-local-variable 'bookmark-make-record-function)
1432 'Man-bookmark-make-record)
1433 (Man-build-page-list)
1434 (Man-strip-page-headers)
1435 (Man-unindent)
1436 (Man-goto-page 1 t))
1437
1438 (defsubst Man-build-section-alist ()
1439 "Build the list of manpage sections."
1440 (setq Man--sections nil)
1441 (goto-char (point-min))
1442 (let ((case-fold-search nil))
1443 (while (re-search-forward Man-heading-regexp (point-max) t)
1444 (let ((section (match-string 1)))
1445 (unless (member section Man--sections)
1446 (push section Man--sections)))
1447 (forward-line 1))))
1448
1449 (defsubst Man-build-references-alist ()
1450 "Build the list of references (in the SEE ALSO section)."
1451 (setq Man--refpages nil)
1452 (save-excursion
1453 (if (Man-find-section Man-see-also-regexp)
1454 (let ((start (progn (forward-line 1) (point)))
1455 (end (progn
1456 (Man-next-section 1)
1457 (point)))
1458 hyphenated
1459 (runningpoint -1))
1460 (save-restriction
1461 (narrow-to-region start end)
1462 (goto-char (point-min))
1463 (back-to-indentation)
1464 (while (and (not (eobp)) (/= (point) runningpoint))
1465 (setq runningpoint (point))
1466 (if (re-search-forward Man-hyphenated-reference-regexp end t)
1467 (let* ((word (match-string 0))
1468 (len (1- (length word))))
1469 (if hyphenated
1470 (setq word (concat hyphenated word)
1471 hyphenated nil
1472 ;; Update len, in case a reference spans
1473 ;; more than two lines (paranoia).
1474 len (1- (length word))))
1475 (if (memq (aref word len) '(?- ?­))
1476 (setq hyphenated (substring word 0 len)))
1477 (and (string-match Man-reference-regexp word)
1478 (not (member word Man--refpages))
1479 (push word Man--refpages))))
1480 (skip-chars-forward " \t\n,"))))))
1481 (setq Man--refpages (nreverse Man--refpages)))
1482
1483 (defun Man-build-page-list ()
1484 "Build the list of separate manpages in the buffer."
1485 (setq Man-page-list nil)
1486 (let ((page-start (point-min))
1487 (page-end (point-max))
1488 (header ""))
1489 (goto-char page-start)
1490 ;; (switch-to-buffer (current-buffer))(debug)
1491 (while (not (eobp))
1492 (setq header
1493 (if (looking-at Man-page-header-regexp)
1494 (match-string 1)
1495 nil))
1496 ;; Go past both the current and the next Man-first-heading-regexp
1497 (if (re-search-forward Man-first-heading-regexp nil 'move 2)
1498 (let ((p (progn (beginning-of-line) (point))))
1499 ;; We assume that the page header is delimited by blank
1500 ;; lines and that it contains at most one blank line. So
1501 ;; if we back by three blank lines we will be sure to be
1502 ;; before the page header but not before the possible
1503 ;; previous page header.
1504 (search-backward "\n\n" nil t 3)
1505 (if (re-search-forward Man-page-header-regexp p 'move)
1506 (beginning-of-line))))
1507 (setq page-end (point))
1508 (setq Man-page-list (append Man-page-list
1509 (list (list (copy-marker page-start)
1510 (copy-marker page-end)
1511 header))))
1512 (setq page-start page-end)
1513 )))
1514
1515 (defun Man-strip-page-headers ()
1516 "Strip all the page headers but the first from the manpage."
1517 (let ((inhibit-read-only t)
1518 (case-fold-search nil)
1519 (header ""))
1520 (dolist (page Man-page-list)
1521 (and (nth 2 page)
1522 (goto-char (car page))
1523 (re-search-forward Man-first-heading-regexp nil t)
1524 (setq header (buffer-substring (car page) (match-beginning 0)))
1525 ;; Since the awk script collapses all successive blank
1526 ;; lines into one, and since we don't want to get rid of
1527 ;; the fast awk script, one must choose between adding
1528 ;; spare blank lines between pages when there were none and
1529 ;; deleting blank lines at page boundaries when there were
1530 ;; some. We choose the first, so we comment the following
1531 ;; line.
1532 ;; (setq header (concat "\n" header)))
1533 (while (search-forward header (nth 1 page) t)
1534 (replace-match ""))))))
1535
1536 (defun Man-unindent ()
1537 "Delete the leading spaces that indent the manpage."
1538 (let ((inhibit-read-only t)
1539 (case-fold-search nil))
1540 (dolist (page Man-page-list)
1541 (let ((indent "")
1542 (nindent 0))
1543 (narrow-to-region (car page) (car (cdr page)))
1544 (if Man-uses-untabify-flag
1545 ;; The space characters inserted by `untabify' inherit
1546 ;; sticky text properties, which is unnecessary and looks
1547 ;; ugly with underlining (Bug#11408).
1548 (let ((text-property-default-nonsticky
1549 (cons '(face . t) text-property-default-nonsticky)))
1550 (untabify (point-min) (point-max))))
1551 (if (catch 'unindent
1552 (goto-char (point-min))
1553 (if (not (re-search-forward Man-first-heading-regexp nil t))
1554 (throw 'unindent nil))
1555 (beginning-of-line)
1556 (setq indent (buffer-substring (point)
1557 (progn
1558 (skip-chars-forward " ")
1559 (point))))
1560 (setq nindent (length indent))
1561 (if (zerop nindent)
1562 (throw 'unindent nil))
1563 (setq indent (concat indent "\\|$"))
1564 (goto-char (point-min))
1565 (while (not (eobp))
1566 (if (looking-at indent)
1567 (forward-line 1)
1568 (throw 'unindent nil)))
1569 (goto-char (point-min)))
1570 (while (not (eobp))
1571 (or (eolp)
1572 (delete-char nindent))
1573 (forward-line 1)))
1574 ))))
1575
1576 \f
1577 ;; ======================================================================
1578 ;; Man mode commands
1579
1580 (defun Man-next-section (n)
1581 "Move point to Nth next section (default 1)."
1582 (interactive "p")
1583 (let ((case-fold-search nil)
1584 (start (point)))
1585 (if (looking-at Man-heading-regexp)
1586 (forward-line 1))
1587 (if (re-search-forward Man-heading-regexp (point-max) t n)
1588 (beginning-of-line)
1589 (goto-char (point-max))
1590 ;; The last line doesn't belong to any section.
1591 (forward-line -1))
1592 ;; But don't move back from the starting point (can happen if `start'
1593 ;; is somewhere on the last line).
1594 (if (< (point) start) (goto-char start))))
1595
1596 (defun Man-previous-section (n)
1597 "Move point to Nth previous section (default 1)."
1598 (interactive "p")
1599 (let ((case-fold-search nil))
1600 (if (looking-at Man-heading-regexp)
1601 (forward-line -1))
1602 (if (re-search-backward Man-heading-regexp (point-min) t n)
1603 (beginning-of-line)
1604 (goto-char (point-min)))))
1605
1606 (defun Man-find-section (section)
1607 "Move point to SECTION if it exists, otherwise don't move point.
1608 Returns t if section is found, nil otherwise."
1609 (let ((curpos (point))
1610 (case-fold-search nil))
1611 (goto-char (point-min))
1612 (if (re-search-forward (concat "^" section) (point-max) t)
1613 (progn (beginning-of-line) t)
1614 (goto-char curpos)
1615 nil)
1616 ))
1617
1618 (defvar Man--last-section nil)
1619
1620 (defun Man-goto-section (section)
1621 "Move point to SECTION."
1622 (interactive
1623 (let* ((default (if (member Man--last-section Man--sections)
1624 Man--last-section
1625 (car Man--sections)))
1626 (completion-ignore-case t)
1627 (prompt (concat "Go to section (default " default "): "))
1628 (chosen (completing-read prompt Man--sections
1629 nil nil nil nil default)))
1630 (list chosen)))
1631 (setq Man--last-section section)
1632 (unless (Man-find-section section)
1633 (error "Section %s not found" section)))
1634
1635
1636 (defun Man-goto-see-also-section ()
1637 "Move point to the \"SEE ALSO\" section.
1638 Actually the section moved to is described by `Man-see-also-regexp'."
1639 (interactive)
1640 (if (not (Man-find-section Man-see-also-regexp))
1641 (error "%s" (concat "No " Man-see-also-regexp
1642 " section found in the current manpage"))))
1643
1644 (defun Man-possibly-hyphenated-word ()
1645 "Return a possibly hyphenated word at point.
1646 If the word starts at the first non-whitespace column, and the
1647 previous line ends with a hyphen, return the last word on the previous
1648 line instead. Thus, if a reference to \"tcgetpgrp(3V)\" is hyphenated
1649 as \"tcgetp-grp(3V)\", and point is at \"grp(3V)\", we return
1650 \"tcgetp-\" instead of \"grp\"."
1651 (save-excursion
1652 (skip-syntax-backward "w()")
1653 (skip-chars-forward " \t")
1654 (let ((beg (point))
1655 (word (current-word)))
1656 (when (eq beg (save-excursion
1657 (back-to-indentation)
1658 (point)))
1659 (end-of-line 0)
1660 (if (eq (char-before) ?-)
1661 (setq word (current-word))))
1662 word)))
1663
1664 (defvar Man--last-refpage nil)
1665
1666 (defun Man-follow-manual-reference (reference)
1667 "Get one of the manpages referred to in the \"SEE ALSO\" section.
1668 Specify which REFERENCE to use; default is based on word at point."
1669 (interactive
1670 (if (not Man--refpages)
1671 (error "There are no references in the current man page")
1672 (list
1673 (let* ((default (or
1674 (car (all-completions
1675 (let ((word
1676 (or (Man-possibly-hyphenated-word)
1677 "")))
1678 ;; strip a trailing '-':
1679 (if (string-match "-$" word)
1680 (substring word 0
1681 (match-beginning 0))
1682 word))
1683 Man--refpages))
1684 (if (member Man--last-refpage Man--refpages)
1685 Man--last-refpage
1686 (car Man--refpages))))
1687 (defaults
1688 (mapcar 'substring-no-properties
1689 (cons default Man--refpages)))
1690 (prompt (concat "Refer to (default " default "): "))
1691 (chosen (completing-read prompt Man--refpages
1692 nil nil nil nil defaults)))
1693 chosen))))
1694 (if (not Man--refpages)
1695 (error "Can't find any references in the current manpage")
1696 (setq Man--last-refpage reference)
1697 (Man-getpage-in-background
1698 (Man-translate-references reference))))
1699
1700 (defun Man-kill ()
1701 "Kill the buffer containing the manpage."
1702 (interactive)
1703 (quit-window t))
1704
1705 (defun Man-quit ()
1706 "Bury the buffer containing the manpage."
1707 (interactive)
1708 (quit-window))
1709
1710 (defun Man-goto-page (page &optional noerror)
1711 "Go to the manual page on page PAGE."
1712 (interactive
1713 (if (not Man-page-list)
1714 (error "Not a man page buffer")
1715 (if (= (length Man-page-list) 1)
1716 (error "You're looking at the only manpage in the buffer")
1717 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1718 (length Man-page-list)))))))
1719 (if (and (not Man-page-list) (not noerror))
1720 (error "Not a man page buffer"))
1721 (when Man-page-list
1722 (if (or (< page 1)
1723 (> page (length Man-page-list)))
1724 (user-error "No manpage %d found" page))
1725 (let* ((page-range (nth (1- page) Man-page-list))
1726 (page-start (car page-range))
1727 (page-end (car (cdr page-range))))
1728 (setq Man-current-page page
1729 Man-page-mode-string (Man-make-page-mode-string))
1730 (widen)
1731 (goto-char page-start)
1732 (narrow-to-region page-start page-end)
1733 (Man-build-section-alist)
1734 (Man-build-references-alist)
1735 (goto-char (point-min)))))
1736
1737
1738 (defun Man-next-manpage ()
1739 "Find the next manpage entry in the buffer."
1740 (interactive)
1741 (if (= (length Man-page-list) 1)
1742 (error "This is the only manpage in the buffer"))
1743 (if (< Man-current-page (length Man-page-list))
1744 (Man-goto-page (1+ Man-current-page))
1745 (if Man-circular-pages-flag
1746 (Man-goto-page 1)
1747 (error "You're looking at the last manpage in the buffer"))))
1748
1749 (defun Man-previous-manpage ()
1750 "Find the previous manpage entry in the buffer."
1751 (interactive)
1752 (if (= (length Man-page-list) 1)
1753 (error "This is the only manpage in the buffer"))
1754 (if (> Man-current-page 1)
1755 (Man-goto-page (1- Man-current-page))
1756 (if Man-circular-pages-flag
1757 (Man-goto-page (length Man-page-list))
1758 (error "You're looking at the first manpage in the buffer"))))
1759
1760 ;; Header file support
1761 (defun Man-view-header-file (file)
1762 "View a header file specified by FILE from `Man-header-file-path'."
1763 (let ((path Man-header-file-path)
1764 complete-path)
1765 (while path
1766 (setq complete-path (expand-file-name file (car path))
1767 path (cdr path))
1768 (if (file-readable-p complete-path)
1769 (progn (view-file complete-path)
1770 (setq path nil))
1771 (setq complete-path nil)))
1772 complete-path))
1773
1774 ;;; Bookmark Man Support
1775 (declare-function bookmark-make-record-default
1776 "bookmark" (&optional no-file no-context posn))
1777 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
1778 (declare-function bookmark-default-handler "bookmark" (bmk))
1779 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
1780
1781 (defun Man-default-bookmark-title ()
1782 "Default bookmark name for Man or WoMan pages.
1783 Uses `Man-name-local-regexp'."
1784 (save-excursion
1785 (goto-char (point-min))
1786 (when (re-search-forward Man-name-local-regexp nil t)
1787 (skip-chars-forward "\n\t ")
1788 (buffer-substring-no-properties (point) (line-end-position)))))
1789
1790 (defun Man-bookmark-make-record ()
1791 "Make a bookmark entry for a Man buffer."
1792 `(,(Man-default-bookmark-title)
1793 ,@(bookmark-make-record-default 'no-file)
1794 (location . ,(concat "man " Man-arguments))
1795 (man-args . ,Man-arguments)
1796 (handler . Man-bookmark-jump)))
1797
1798 ;;;###autoload
1799 (defun Man-bookmark-jump (bookmark)
1800 "Default bookmark handler for Man buffers."
1801 (let* ((man-args (bookmark-prop-get bookmark 'man-args))
1802 ;; Let bookmark.el do the window handling.
1803 ;; This let-binding needs to be active during the call to both
1804 ;; Man-getpage-in-background and accept-process-output.
1805 (Man-notify-method 'meek)
1806 (buf (Man-getpage-in-background man-args))
1807 (proc (get-buffer-process buf)))
1808 (while (and proc (eq (process-status proc) 'run))
1809 (accept-process-output proc))
1810 (bookmark-default-handler
1811 `("" (buffer . ,buf) . ,(bookmark-get-bookmark-record bookmark)))))
1812
1813 \f
1814 ;; Init the man package variables, if not already done.
1815 (Man-init-defvars)
1816
1817 (provide 'man)
1818
1819 ;;; man.el ends here