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