Inhibit `epa-file-handler' in Tramp
[emacs.git] / lisp / man.el
blobc5dbcba83a1c73933774f630f18f1618b18891f1
1 ;;; man.el --- browse UNIX manual pages -*- coding: utf-8 -*-
3 ;; Copyright (C) 1993-1994, 1996-1997, 2001-2015 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 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 'sexp
188 :group 'man)
190 (defcustom Man-downcase-section-letters-flag t
191 "Non-nil means letters in sections are converted to lower case.
192 Some Un*x man commands can't handle uppercase letters in sections, for
193 example \"man 2V chmod\", but they are often displayed in the manpage
194 with the upper case letter. When this variable is t, the section
195 letter (e.g., \"2V\") is converted to lowercase (e.g., \"2v\") before
196 being sent to the man background process."
197 :type 'boolean
198 :group 'man)
200 (defcustom Man-circular-pages-flag t
201 "Non-nil means the manpage list is treated as circular for traversal."
202 :type 'boolean
203 :group 'man)
205 (defcustom Man-section-translations-alist
206 (list
207 '("3C++" . "3")
208 ;; Some systems have a real 3x man section, so let's comment this.
209 ;; '("3X" . "3") ; Xlib man pages
210 '("3X11" . "3")
211 '("1-UCB" . ""))
212 "Association list of bogus sections to real section numbers.
213 Some manpages (e.g. the Sun C++ 2.1 manpages) have section numbers in
214 their references which Un*x `man' does not recognize. This
215 association list is used to translate those sections, when found, to
216 the associated section number."
217 :type '(repeat (cons (string :tag "Bogus Section")
218 (string :tag "Real Section")))
219 :group 'man)
221 ;; FIXME see comments at ffap-c-path.
222 (defcustom Man-header-file-path
223 (let ((arch (with-temp-buffer
224 (when (eq 0 (ignore-errors
225 (call-process "gcc" nil '(t nil) nil
226 "-print-multiarch")))
227 (goto-char (point-min))
228 (buffer-substring (point) (line-end-position)))))
229 (base '("/usr/include" "/usr/local/include")))
230 (if (zerop (length arch))
231 base
232 (append base (list (expand-file-name arch "/usr/include")))))
233 "C Header file search path used in Man."
234 :version "24.1" ; add multiarch
235 :type '(repeat string)
236 :group 'man)
238 (defcustom Man-name-local-regexp (concat "^" (regexp-opt '("NOM" "NAME")) "$")
239 "Regexp that matches the text that precedes the command's name.
240 Used in `bookmark-set' to get the default bookmark name."
241 :version "24.1"
242 :type 'string :group 'bookmark)
244 (defcustom manual-program "man"
245 "Program used by `man' to produce man pages."
246 :type 'string
247 :group 'man)
249 (defcustom Man-untabify-command "pr"
250 "Program used by `man' for untabifying."
251 :type 'string
252 :group 'man)
254 (defcustom Man-untabify-command-args (list "-t" "-e")
255 "List of arguments to be passed to `Man-untabify-command' (which see)."
256 :type '(repeat string)
257 :group 'man)
259 (defcustom Man-sed-command "sed"
260 "Program used by `man' to process sed scripts."
261 :type 'string
262 :group 'man)
264 (defcustom Man-awk-command "awk"
265 "Program used by `man' to process awk scripts."
266 :type 'string
267 :group 'man)
269 (defcustom Man-mode-hook nil
270 "Hook run when Man mode is enabled."
271 :type 'hook
272 :group 'man)
274 (defcustom Man-cooked-hook nil
275 "Hook run after removing backspaces but before `Man-mode' processing."
276 :type 'hook
277 :group 'man)
279 (defvar Man-name-regexp "[-a-zA-Z0-9_­+][-a-zA-Z0-9_.:­+]*"
280 "Regular expression describing the name of a manpage (without section).")
282 (defvar Man-section-regexp "[0-9][a-zA-Z0-9+]*\\|[LNln]"
283 "Regular expression describing a manpage section within parentheses.")
285 (defvar Man-page-header-regexp
286 (if (string-match "-solaris2\\." system-configuration)
287 (concat "^[-A-Za-z0-9_].*[ \t]\\(" Man-name-regexp
288 "(\\(" Man-section-regexp "\\))\\)$")
289 (concat "^[ \t]*\\(" Man-name-regexp
290 "(\\(" Man-section-regexp "\\))\\).*\\1"))
291 "Regular expression describing the heading of a page.")
293 (defvar Man-heading-regexp "^\\([A-Z][A-Z0-9 /-]+\\)$"
294 "Regular expression describing a manpage heading entry.")
296 (defvar Man-see-also-regexp "SEE ALSO"
297 "Regular expression for SEE ALSO heading (or your equivalent).
298 This regexp should not start with a `^' character.")
300 ;; This used to have leading space [ \t]*, but was removed because it
301 ;; causes false page splits on an occasional NAME with leading space
302 ;; inside a manpage. And `Man-heading-regexp' doesn't have [ \t]* anyway.
303 (defvar Man-first-heading-regexp "^NAME$\\|^[ \t]*No manual entry fo.*$"
304 "Regular expression describing first heading on a manpage.
305 This regular expression should start with a `^' character.")
307 (defvar Man-reference-regexp
308 (concat "\\(" Man-name-regexp
309 "\\(\n[ \t]+" Man-name-regexp "\\)*\\)[ \t]*(\\("
310 Man-section-regexp "\\))")
311 "Regular expression describing a reference to another manpage.")
313 (defvar Man-apropos-regexp
314 (concat "\\\[\\(" Man-name-regexp "\\)\\\][ \t]*(\\(" Man-section-regexp "\\))")
315 "Regular expression describing a reference to manpages in \"man -k output\".")
317 (defvar Man-synopsis-regexp "SYNOPSIS"
318 "Regular expression for SYNOPSIS heading (or your equivalent).
319 This regexp should not start with a `^' character.")
321 (defvar Man-files-regexp "FILES\\>"
322 ;; Add \> so as not to match mount(8)'s FILESYSTEM INDEPENDENT MOUNT OPTIONS.
323 "Regular expression for FILES heading (or your equivalent).
324 This regexp should not start with a `^' character.")
326 (defvar Man-include-regexp "#[ \t]*include[ \t]*"
327 "Regular expression describing the #include (directive of cpp).")
329 (defvar Man-file-name-regexp "[^<>\", \t\n]+"
330 "Regular expression describing <> in #include line (directive of cpp).")
332 (defvar Man-normal-file-prefix-regexp "[/~$]"
333 "Regular expression describing a file path appeared in FILES section.")
335 (defvar Man-header-regexp
336 (concat "\\(" Man-include-regexp "\\)"
337 "[<\"]"
338 "\\(" Man-file-name-regexp "\\)"
339 "[>\"]")
340 "Regular expression describing references to header files.")
342 (defvar Man-normal-file-regexp
343 (concat Man-normal-file-prefix-regexp Man-file-name-regexp)
344 "Regular expression describing references to normal files.")
346 ;; This includes the section as an optional part to catch hyphenated
347 ;; references to manpages.
348 (defvar Man-hyphenated-reference-regexp
349 (concat "\\(" Man-name-regexp "\\)\\((\\(" Man-section-regexp "\\))\\)?")
350 "Regular expression describing a reference in the SEE ALSO section.")
352 (defcustom Man-switches ""
353 "Switches passed to the man command, as a single string.
354 For example, the -a switch lets you see all the manpages for a
355 specified subject, if your `man' program supports it."
356 :type 'string
357 :group 'man)
359 (defvar Man-specified-section-option
360 (if (string-match "-solaris[0-9.]*$" system-configuration)
361 "-s"
363 "Option that indicates a specified a manual section name.")
365 (defvar Man-support-local-filenames 'auto-detect
366 "Internal cache for the value of the function `Man-support-local-filenames'.
367 `auto-detect' means the value is not yet determined.
368 Otherwise, the value is whatever the function
369 `Man-support-local-filenames' should return.")
371 (defcustom man-imenu-title "Contents"
372 "The title to use if man adds a Contents menu to the menubar."
373 :version "24.4"
374 :type 'string
375 :group 'man)
378 ;; other variables and keymap initializations
379 (defvar Man-original-frame)
380 (make-variable-buffer-local 'Man-original-frame)
381 (defvar Man-arguments)
382 (make-variable-buffer-local 'Man-arguments)
383 (put 'Man-arguments 'permanent-local t)
385 (defvar Man--sections nil)
386 (make-variable-buffer-local 'Man--sections)
387 (defvar Man--refpages nil)
388 (make-variable-buffer-local 'Man--refpages)
389 (defvar Man-page-list nil)
390 (make-variable-buffer-local 'Man-page-list)
391 (defvar Man-current-page 0)
392 (make-variable-buffer-local 'Man-current-page)
393 (defvar Man-page-mode-string "1 of 1")
394 (make-variable-buffer-local 'Man-page-mode-string)
396 (defconst Man-sysv-sed-script "\
397 /\b/ { s/_\b//g
398 s/\b_//g
399 s/o\b+/o/g
400 s/+\bo/o/g
401 :ovstrk
402 s/\\(.\\)\b\\1/\\1/g
403 t ovstrk
405 /\e\\[[0-9][0-9]*m/ s///g"
406 "Script for sysV-like sed to nuke backspaces and ANSI codes from manpages.")
408 (defconst Man-berkeley-sed-script "\
409 /\b/ { s/_\b//g\\
410 s/\b_//g\\
411 s/o\b+/o/g\\
412 s/+\bo/o/g\\
413 :ovstrk\\
414 s/\\(.\\)\b\\1/\\1/g\\
415 t ovstrk\\
417 /\e\\[[0-9][0-9]*m/ s///g"
418 "Script for berkeley-like sed to nuke backspaces and ANSI codes from manpages.")
420 (defvar Man-topic-history nil "Topic read history.")
422 (defvar Man-mode-syntax-table
423 (let ((table (copy-syntax-table (standard-syntax-table))))
424 (modify-syntax-entry ?. "w" table)
425 (modify-syntax-entry ?_ "w" table)
426 (modify-syntax-entry ?: "w" table) ; for PDL::Primitive in Perl man pages
427 table)
428 "Syntax table used in Man mode buffers.")
430 (defvar Man-mode-map
431 (let ((map (make-sparse-keymap)))
432 (suppress-keymap map)
433 (set-keymap-parent map button-buffer-map)
435 (define-key map [?\S-\ ] 'scroll-down-command)
436 (define-key map " " 'scroll-up-command)
437 (define-key map "\177" 'scroll-down-command)
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 ">" 'end-of-buffer)
443 (define-key map "<" 'beginning-of-buffer)
444 (define-key map "." 'beginning-of-buffer)
445 (define-key map "r" 'Man-follow-manual-reference)
446 (define-key map "g" 'Man-goto-section)
447 (define-key map "s" 'Man-goto-see-also-section)
448 (define-key map "k" 'Man-kill)
449 (define-key map "q" 'Man-quit)
450 (define-key map "u" 'Man-update-manpage)
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)
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.")
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)
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)))))
498 (define-button-type 'Man-xref-man-page
499 :supertype 'Man-abstract-xref-man-page
500 'func 'man-follow)
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")
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")
524 ;; ======================================================================
525 ;; utilities
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."
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)
542 nil))))
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 "'")
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'"
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"
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))
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))
607 Man-current-page
608 (length Man-page-list)))
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))
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))
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)
681 slist nil))))
682 (concat Man-specified-section-option section " " name))))
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 (file-accessible-directory-p default-directory)
698 default-directory
699 (expand-file-name "~/"))))
700 (ignore-errors
701 (call-process manual-program nil t nil "--help")))
702 (cond ((search-backward "--local-file" nil 'move)
703 'man-db)
704 ;; This feature seems to be present in at least ver 1.4f,
705 ;; which is about 20 years old.
706 ;; I don't know if this version has an official name?
707 ((looking-at "^man, versione? [1-9]")
708 'man))))
709 Man-support-local-filenames))
712 ;; ======================================================================
713 ;; default man entry: get word near point
715 (defun Man-default-man-entry (&optional pos)
716 "Guess default manual entry based on the text near position POS.
717 POS defaults to `point'."
718 (let (word start column distance)
719 (save-excursion
720 (when pos (goto-char pos))
721 (setq pos (point))
722 ;; The default title is the nearest entry-like object before or
723 ;; after POS.
724 (if (and (skip-chars-backward " \ta-zA-Z0-9+")
725 (not (zerop (skip-chars-backward "(")))
726 ;; Try to handle the special case where POS is on a
727 ;; section number.
728 (looking-at
729 (concat "([ \t]*\\(" Man-section-regexp "\\)[ \t]*)"))
730 ;; We skipped a valid section number backwards, look at
731 ;; preceding text.
732 (or (and (skip-chars-backward ",; \t")
733 (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:"))))
734 ;; Not a valid entry, move POS after closing paren.
735 (not (setq pos (match-end 0)))))
736 ;; We have a candidate, make `start' record its starting
737 ;; position.
738 (setq start (point))
739 ;; Otherwise look at char before POS.
740 (goto-char pos)
741 (if (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:")))
742 ;; Our candidate is just before or around POS.
743 (setq start (point))
744 ;; Otherwise record the current column and look backwards.
745 (setq column (current-column))
746 (skip-chars-backward ",; \t")
747 ;; Record the distance traveled.
748 (setq distance (- column (current-column)))
749 (when (looking-back
750 (concat "([ \t]*\\(?:" Man-section-regexp "\\)[ \t]*)")
751 (line-beginning-position))
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)))))))
801 ;; ======================================================================
802 ;; Top level command and background process sentinel
804 ;; For compatibility with older versions.
805 ;;;###autoload
806 (defalias 'manual-entry 'man)
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).")
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.
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.")
825 (defun Man-parse-man-k ()
826 "Parse \"man -k\" output and return the list of page names.
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).
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.
838 foo[, bar [, ...]] [other stuff] (sec) - description
839 foo(sec)[, bar(sec) [, ...]] [other stuff] - description
841 For more details and some regression tests, please see
842 test/automated/man-tests.el in the emacs 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)))
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))
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)))))))
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.
935 It 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. The variable `Man-width' defines the number of columns in
938 formatted manual pages. The buffer is displayed immediately.
939 The variable `Man-notify-method' defines how the buffer is displayed.
940 If a buffer already exists for this man page, it will be displayed
941 without running the man command.
943 For a manpage from a particular section, use either of the
944 following. \"cat(1)\" is how cross-references appear and is
945 passed to man as \"1 cat\".
947 cat(1)
948 1 cat
950 To see manpages from all sections related to a subject, use an
951 \"all pages\" option (which might be \"-a\" if it's not the
952 default), then step through with `Man-next-manpage' (\\<Man-mode-map>\\[Man-next-manpage]) etc.
953 Add to `Man-switches' to make this option permanent.
955 -a chmod
957 An explicit filename can be given too. Use -l if it might
958 otherwise look like a page name.
960 /my/file/name.1.gz
961 -l somefile.1
963 An \"apropos\" query with -k gives a buffer of matching page
964 names or descriptions. The pattern argument is usually an
965 \"egrep\" style regexp.
967 -k pattern"
969 (interactive
970 (list (let* ((default-entry (Man-default-man-entry))
971 ;; ignore case because that's friendly for bizarre
972 ;; caps things like the X11 function names and because
973 ;; "man" itself is case-insensitive on the command line
974 ;; so you're accustomed not to bother about the case
975 ;; ("man -k" is case-insensitive similarly, so the
976 ;; table has everything available to complete)
977 (completion-ignore-case t)
978 Man-completion-cache ;Don't cache across calls.
979 (input (completing-read
980 (format "Manual entry%s"
981 (if (string= default-entry "")
982 ": "
983 (format " (default %s): " default-entry)))
984 'Man-completion-table
985 nil nil nil 'Man-topic-history default-entry)))
986 (if (string= input "")
987 (error "No man args given")
988 input))))
990 ;; Possibly translate the "subject(section)" syntax into the
991 ;; "section subject" syntax and possibly downcase the section.
992 (setq man-args (Man-translate-references man-args))
994 (Man-getpage-in-background man-args))
996 ;;;###autoload
997 (defun man-follow (man-args)
998 "Get a Un*x manual page of the item under point and put it in a buffer."
999 (interactive (list (Man-default-man-entry)))
1000 (if (or (not man-args)
1001 (string= man-args ""))
1002 (error "No item under point")
1003 (man man-args)))
1005 (defmacro Man-start-calling (&rest body)
1006 "Start the man command in `body' after setting up the environment"
1007 `(let ((process-environment (copy-sequence process-environment))
1008 ;; The following is so Awk script gets \n intact
1009 ;; But don't prevent decoding of the outside.
1010 (coding-system-for-write 'raw-text-unix)
1011 ;; We must decode the output by a coding system that the
1012 ;; system's locale suggests in multibyte mode.
1013 (coding-system-for-read locale-coding-system)
1014 ;; Avoid possible error by using a directory that always exists.
1015 (default-directory
1016 (if (and (file-directory-p default-directory)
1017 (not (find-file-name-handler default-directory
1018 'file-directory-p)))
1019 default-directory
1020 "/")))
1021 ;; Prevent any attempt to use display terminal fanciness.
1022 (setenv "TERM" "dumb")
1023 ;; In Debian Woody, at least, we get overlong lines under X
1024 ;; unless COLUMNS or MANWIDTH is set. This isn't a problem on
1025 ;; a tty. man(1) says:
1026 ;; MANWIDTH
1027 ;; If $MANWIDTH is set, its value is used as the line
1028 ;; length for which manual pages should be formatted.
1029 ;; If it is not set, manual pages will be formatted
1030 ;; with a line length appropriate to the current ter-
1031 ;; minal (using an ioctl(2) if available, the value of
1032 ;; $COLUMNS, or falling back to 80 characters if nei-
1033 ;; ther is available).
1034 (when (or window-system
1035 (not (or (getenv "MANWIDTH") (getenv "COLUMNS"))))
1036 ;; Since the page buffer is displayed beforehand,
1037 ;; we can select its window and get the window/frame width.
1038 (setenv "COLUMNS" (number-to-string
1039 (cond
1040 ((and (integerp Man-width) (> Man-width 0))
1041 Man-width)
1042 (Man-width
1043 (if (window-live-p (get-buffer-window (current-buffer) t))
1044 (with-selected-window (get-buffer-window (current-buffer) t)
1045 (frame-width))
1046 (frame-width)))
1048 (if (window-live-p (get-buffer-window (current-buffer) t))
1049 (with-selected-window (get-buffer-window (current-buffer) t)
1050 (window-width))
1051 (window-width)))))))
1052 ;; Since man-db 2.4.3-1, man writes plain text with no escape
1053 ;; sequences when stdout is not a tty. In 2.5.0, the following
1054 ;; env-var was added to allow control of this (see Debian Bug#340673).
1055 (setenv "MAN_KEEP_FORMATTING" "1")
1056 ,@body))
1058 (defun Man-getpage-in-background (topic)
1059 "Use TOPIC to build and fire off the manpage and cleaning command.
1060 Return the buffer in which the manpage will appear."
1061 (let* ((man-args topic)
1062 (bufname (concat "*Man " man-args "*"))
1063 (buffer (get-buffer bufname)))
1064 (if buffer
1065 (Man-notify-when-ready buffer)
1066 (require 'env)
1067 (message "Invoking %s %s in the background" manual-program man-args)
1068 (setq buffer (generate-new-buffer bufname))
1069 (with-current-buffer buffer
1070 (Man-notify-when-ready buffer)
1071 (setq buffer-undo-list t)
1072 (setq Man-original-frame (selected-frame))
1073 (setq Man-arguments man-args)
1074 (Man-mode)
1075 (setq mode-line-process
1076 (concat " " (propertize (if Man-fontify-manpage-flag
1077 "[formatting...]"
1078 "[cleaning...]")
1079 'face 'mode-line-emphasis)))
1080 (Man-start-calling
1081 (if (fboundp 'start-process)
1082 (let ((proc (start-process
1083 manual-program buffer
1084 (if (memq system-type '(cygwin windows-nt))
1085 shell-file-name
1086 "sh")
1087 shell-command-switch
1088 (format (Man-build-man-command) man-args))))
1089 (set-process-sentinel proc 'Man-bgproc-sentinel)
1090 (set-process-filter proc 'Man-bgproc-filter))
1091 (let* ((inhibit-read-only t)
1092 (exit-status
1093 (call-process shell-file-name nil (list buffer nil) nil
1094 shell-command-switch
1095 (format (Man-build-man-command) man-args)))
1096 (msg ""))
1097 (or (and (numberp exit-status)
1098 (= exit-status 0))
1099 (and (numberp exit-status)
1100 (setq msg
1101 (format "exited abnormally with code %d"
1102 exit-status)))
1103 (setq msg exit-status))
1104 (if Man-fontify-manpage-flag
1105 (Man-fontify-manpage)
1106 (Man-cleanup-manpage))
1107 (Man-bgproc-sentinel bufname msg))))))
1108 buffer))
1110 (defun Man-update-manpage ()
1111 "Reformat current manpage by calling the man command again synchronously."
1112 (interactive)
1113 (when (eq Man-arguments nil)
1114 ;;this shouldn't happen unless it is not in a Man buffer."
1115 (error "Man-arguments not initialized"))
1116 (let ((old-pos (point))
1117 (text (current-word))
1118 (old-size (buffer-size))
1119 (inhibit-read-only t)
1120 (buffer-read-only nil))
1121 (erase-buffer)
1122 (Man-start-calling
1123 (call-process shell-file-name nil (list (current-buffer) nil) nil
1124 shell-command-switch
1125 (format (Man-build-man-command) Man-arguments)))
1126 (if Man-fontify-manpage-flag
1127 (Man-fontify-manpage)
1128 (Man-cleanup-manpage))
1129 (goto-char old-pos)
1130 ;;restore the point, not strictly right.
1131 (unless (or (eq text nil) (= old-size (buffer-size)))
1132 (let ((case-fold-search nil))
1133 (if (> old-size (buffer-size))
1134 (search-backward text nil t))
1135 (search-forward text nil t)))))
1137 (defun Man-notify-when-ready (man-buffer)
1138 "Notify the user when MAN-BUFFER is ready.
1139 See the variable `Man-notify-method' for the different notification behaviors."
1140 (let ((saved-frame (with-current-buffer man-buffer
1141 Man-original-frame)))
1142 (pcase Man-notify-method
1143 (`newframe
1144 ;; Since we run asynchronously, perhaps while Emacs is waiting
1145 ;; for input, we must not leave a different buffer current. We
1146 ;; can't rely on the editor command loop to reselect the
1147 ;; selected window's buffer.
1148 (save-excursion
1149 (let ((frame (make-frame Man-frame-parameters)))
1150 (set-window-buffer (frame-selected-window frame) man-buffer)
1151 (set-window-dedicated-p (frame-selected-window frame) t)
1152 (or (display-multi-frame-p frame)
1153 (select-frame frame)))))
1154 (`pushy
1155 (switch-to-buffer man-buffer))
1156 (`bully
1157 (and (frame-live-p saved-frame)
1158 (select-frame saved-frame))
1159 (pop-to-buffer man-buffer)
1160 (delete-other-windows))
1161 (`aggressive
1162 (and (frame-live-p saved-frame)
1163 (select-frame saved-frame))
1164 (pop-to-buffer man-buffer))
1165 (`friendly
1166 (and (frame-live-p saved-frame)
1167 (select-frame saved-frame))
1168 (display-buffer man-buffer 'not-this-window))
1169 (`polite
1170 (beep)
1171 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
1172 (`quiet
1173 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
1174 (_ ;; meek
1175 (message ""))
1178 (defun Man-softhyphen-to-minus ()
1179 ;; \255 is SOFT HYPHEN in Latin-N. Versions of Debian man, at
1180 ;; least, emit it even when not in a Latin-N locale.
1181 (unless (eq t (compare-strings "latin-" 0 nil
1182 current-language-environment 0 6 t))
1183 (goto-char (point-min))
1184 (let ((str "\255"))
1185 (if enable-multibyte-characters
1186 (setq str (string-as-multibyte str)))
1187 (while (search-forward str nil t) (replace-match "-")))))
1189 (defun Man-fontify-manpage ()
1190 "Convert overstriking and underlining to the correct fonts.
1191 Same for the ANSI bold and normal escape sequences."
1192 (interactive)
1193 (goto-char (point-min))
1194 ;; Fontify ANSI escapes.
1195 (let ((ansi-color-apply-face-function
1196 (lambda (beg end face)
1197 (when face
1198 (put-text-property beg end 'face face))))
1199 (ansi-color-map Man-ansi-color-map))
1200 (ansi-color-apply-on-region (point-min) (point-max)))
1201 ;; Other highlighting.
1202 (let ((buffer-undo-list t))
1203 (if (< (buffer-size) (position-bytes (point-max)))
1204 ;; Multibyte characters exist.
1205 (progn
1206 (goto-char (point-min))
1207 (while (and (search-forward "__\b\b" nil t) (not (eobp)))
1208 (backward-delete-char 4)
1209 (put-text-property (point) (1+ (point)) 'face 'Man-underline))
1210 (goto-char (point-min))
1211 (while (search-forward "\b\b__" nil t)
1212 (backward-delete-char 4)
1213 (put-text-property (1- (point)) (point) 'face 'Man-underline))))
1214 (goto-char (point-min))
1215 (while (and (search-forward "_\b" nil t) (not (eobp)))
1216 (backward-delete-char 2)
1217 (put-text-property (point) (1+ (point)) 'face 'Man-underline))
1218 (goto-char (point-min))
1219 (while (search-forward "\b_" nil t)
1220 (backward-delete-char 2)
1221 (put-text-property (1- (point)) (point) 'face 'Man-underline))
1222 (goto-char (point-min))
1223 (while (re-search-forward "\\(.\\)\\(\b+\\1\\)+" nil t)
1224 (replace-match "\\1")
1225 (put-text-property (1- (point)) (point) 'face 'Man-overstrike))
1226 (goto-char (point-min))
1227 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t)
1228 (replace-match "o")
1229 (put-text-property (1- (point)) (point) 'face 'bold))
1230 (goto-char (point-min))
1231 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t)
1232 (replace-match "+")
1233 (put-text-property (1- (point)) (point) 'face 'bold))
1234 ;; When the header is longer than the manpage name, groff tries to
1235 ;; condense it to a shorter line interspersed with ^H. Remove ^H with
1236 ;; their preceding chars (but don't put Man-overstrike). (Bug#5566)
1237 (goto-char (point-min))
1238 (while (re-search-forward ".\b" nil t) (backward-delete-char 2))
1239 (goto-char (point-min))
1240 ;; Try to recognize common forms of cross references.
1241 (Man-highlight-references)
1242 (Man-softhyphen-to-minus)
1243 (goto-char (point-min))
1244 (while (re-search-forward Man-heading-regexp nil t)
1245 (put-text-property (match-beginning 0)
1246 (match-end 0)
1247 'face 'Man-overstrike))))
1249 (defun Man-highlight-references (&optional xref-man-type)
1250 "Highlight the references on mouse-over.
1251 References include items in the SEE ALSO section,
1252 header file (#include <foo.h>), and files in FILES.
1253 If optional argument XREF-MAN-TYPE is non-nil, it used as the
1254 button type for items in SEE ALSO section. If it is nil, the
1255 default type, `Man-xref-man-page' is used for the buttons."
1256 ;; `Man-highlight-references' is used from woman.el, too.
1257 ;; woman.el doesn't set `Man-arguments'.
1258 (unless Man-arguments
1259 (setq Man-arguments ""))
1260 (if (string-match "-k " Man-arguments)
1261 (progn
1262 (Man-highlight-references0 nil Man-reference-regexp 1
1263 'Man-default-man-entry
1264 (or xref-man-type 'Man-xref-man-page))
1265 (Man-highlight-references0 nil Man-apropos-regexp 1
1266 'Man-default-man-entry
1267 (or xref-man-type 'Man-xref-man-page)))
1268 (Man-highlight-references0 Man-see-also-regexp Man-reference-regexp 1
1269 'Man-default-man-entry
1270 (or xref-man-type 'Man-xref-man-page))
1271 (Man-highlight-references0 Man-synopsis-regexp Man-header-regexp 0 2
1272 'Man-xref-header-file)
1273 (Man-highlight-references0 Man-files-regexp Man-normal-file-regexp 0 0
1274 'Man-xref-normal-file)))
1276 (defun Man-highlight-references0 (start-section regexp button-pos target type)
1277 ;; Based on `Man-build-references-alist'
1278 (when (or (null start-section)
1279 (Man-find-section start-section))
1280 (let ((end (if start-section
1281 (progn
1282 (forward-line 1)
1283 (back-to-indentation)
1284 (save-excursion
1285 (Man-next-section 1)
1286 (point)))
1287 (goto-char (point-min))
1288 nil)))
1289 (while (re-search-forward regexp end t)
1290 ;; An overlay button is preferable because the underlying text
1291 ;; may have text property highlights (Bug#7881).
1292 (make-button
1293 (match-beginning button-pos)
1294 (match-end button-pos)
1295 'type type
1296 'Man-target-string (cond
1297 ((numberp target)
1298 (match-string target))
1299 ((functionp target)
1300 target)
1301 (t nil)))))))
1303 (defun Man-cleanup-manpage (&optional interactive)
1304 "Remove overstriking and underlining from the current buffer.
1305 Normally skip any jobs that should have been done by the sed script,
1306 but when called interactively, do those jobs even if the sed
1307 script would have done them."
1308 (interactive "p")
1309 (if (or interactive (not Man-sed-script))
1310 (progn
1311 (goto-char (point-min))
1312 (while (search-forward "_\b" nil t) (backward-delete-char 2))
1313 (goto-char (point-min))
1314 (while (search-forward "\b_" nil t) (backward-delete-char 2))
1315 (goto-char (point-min))
1316 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
1317 (replace-match "\\1"))
1318 (goto-char (point-min))
1319 (while (re-search-forward "\e\\[[0-9]+m" nil t) (replace-match ""))
1320 (goto-char (point-min))
1321 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t) (replace-match "o"))
1323 (goto-char (point-min))
1324 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t) (replace-match "+"))
1325 ;; When the header is longer than the manpage name, groff tries to
1326 ;; condense it to a shorter line interspersed with ^H. Remove ^H with
1327 ;; their preceding chars (but don't put Man-overstrike). (Bug#5566)
1328 (goto-char (point-min))
1329 (while (re-search-forward ".\b" nil t) (backward-delete-char 2))
1330 (Man-softhyphen-to-minus))
1332 (defun Man-bgproc-filter (process string)
1333 "Manpage background process filter.
1334 When manpage command is run asynchronously, PROCESS is the process
1335 object for the manpage command; when manpage command is run
1336 synchronously, PROCESS is the name of the buffer where the manpage
1337 command is run. Second argument STRING is the entire string of output."
1338 (save-excursion
1339 (let ((Man-buffer (process-buffer process)))
1340 (if (null (buffer-name Man-buffer)) ;; deleted buffer
1341 (set-process-buffer process nil)
1343 (with-current-buffer Man-buffer
1344 (let ((inhibit-read-only t)
1345 (beg (marker-position (process-mark process))))
1346 (save-excursion
1347 (goto-char beg)
1348 (insert string)
1349 (save-restriction
1350 (narrow-to-region
1351 (save-excursion
1352 (goto-char beg)
1353 (line-beginning-position))
1354 (point))
1355 (if Man-fontify-manpage-flag
1356 (Man-fontify-manpage)
1357 (Man-cleanup-manpage)))
1358 (set-marker (process-mark process) (point-max)))))))))
1360 (defun Man-bgproc-sentinel (process msg)
1361 "Manpage background process sentinel.
1362 When manpage command is run asynchronously, PROCESS is the process
1363 object for the manpage command; when manpage command is run
1364 synchronously, PROCESS is the name of the buffer where the manpage
1365 command is run. Second argument MSG is the exit message of the
1366 manpage command."
1367 (let ((Man-buffer (if (stringp process) (get-buffer process)
1368 (process-buffer process)))
1369 (delete-buff nil)
1370 (err-mess nil))
1372 (if (null (buffer-name Man-buffer)) ;; deleted buffer
1373 (or (stringp process)
1374 (set-process-buffer process nil))
1376 (with-current-buffer Man-buffer
1377 (save-excursion
1378 (let ((case-fold-search nil))
1379 (goto-char (point-min))
1380 (cond ((or (looking-at "No \\(manual \\)*entry for")
1381 (looking-at "[^\n]*: nothing appropriate$"))
1382 (setq err-mess (buffer-substring (point)
1383 (progn
1384 (end-of-line) (point)))
1385 delete-buff t))
1387 ;; "-k foo", successful exit, but no output (from man-db)
1388 ;; ENHANCE-ME: share the check for -k with
1389 ;; `Man-highlight-references'. The \\s- bits here are
1390 ;; meant to allow for multiple options with -k among them.
1391 ((and (string-match "\\(\\`\\|\\s-\\)-k\\s-" Man-arguments)
1392 (eq (process-status process) 'exit)
1393 (= (process-exit-status process) 0)
1394 (= (point-min) (point-max)))
1395 (setq err-mess (format "%s: no matches" Man-arguments)
1396 delete-buff t))
1398 ((or (stringp process)
1399 (not (and (eq (process-status process) 'exit)
1400 (= (process-exit-status process) 0))))
1401 (or (zerop (length msg))
1402 (progn
1403 (setq err-mess
1404 (concat (buffer-name Man-buffer)
1405 ": process "
1406 (let ((eos (1- (length msg))))
1407 (if (= (aref msg eos) ?\n)
1408 (substring msg 0 eos) msg))))
1409 (goto-char (point-max))
1410 (insert (format "\nprocess %s" msg))))
1412 (if delete-buff
1413 (if (window-live-p (get-buffer-window Man-buffer t))
1414 (quit-restore-window
1415 (get-buffer-window Man-buffer t) 'kill)
1416 (kill-buffer Man-buffer))
1418 (run-hooks 'Man-cooked-hook)
1420 (Man-build-page-list)
1421 (Man-strip-page-headers)
1422 (Man-unindent)
1423 (Man-goto-page 1 t)
1425 (if (not Man-page-list)
1426 (let ((args Man-arguments))
1427 (if (window-live-p (get-buffer-window (current-buffer) t))
1428 (quit-restore-window
1429 (get-buffer-window (current-buffer) t) 'kill)
1430 (kill-buffer (current-buffer)))
1431 (message "Can't find the %s manpage"
1432 (Man-page-from-arguments args)))
1434 (if Man-fontify-manpage-flag
1435 (message "%s man page formatted"
1436 (Man-page-from-arguments Man-arguments))
1437 (message "%s man page cleaned up"
1438 (Man-page-from-arguments Man-arguments)))
1439 (unless (and (processp process)
1440 (not (eq (process-status process) 'exit)))
1441 (setq mode-line-process nil))
1442 (set-buffer-modified-p nil)))))
1444 (if err-mess
1445 (message "%s" err-mess))
1446 ))))
1448 (defun Man-page-from-arguments (args)
1449 ;; Skip arguments and only print the page name.
1450 (mapconcat
1451 'identity
1452 (delete nil
1453 (mapcar
1454 (lambda (elem)
1455 (and (not (string-match "^-" elem))
1456 elem))
1457 (split-string args " ")))
1458 " "))
1461 ;; ======================================================================
1462 ;; set up manual mode in buffer and build alists
1464 (defvar bookmark-make-record-function)
1466 (put 'Man-mode 'mode-class 'special)
1468 (define-derived-mode Man-mode fundamental-mode "Man"
1469 "A mode for browsing Un*x manual pages.
1471 The following man commands are available in the buffer. Try
1472 \"\\[describe-key] <key> RET\" for more information:
1474 \\[man] Prompt to retrieve a new manpage.
1475 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
1476 \\[Man-next-manpage] Jump to next manpage in circular list.
1477 \\[Man-previous-manpage] Jump to previous manpage in circular list.
1478 \\[Man-next-section] Jump to next manpage section.
1479 \\[Man-previous-section] Jump to previous manpage section.
1480 \\[Man-goto-section] Go to a manpage section.
1481 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
1482 \\[Man-quit] Deletes the manpage window, bury its buffer.
1483 \\[Man-kill] Deletes the manpage window, kill its buffer.
1484 \\[describe-mode] Prints this help text.
1486 The following variables may be of some use. Try
1487 \"\\[describe-variable] <variable-name> RET\" for more information:
1489 `Man-notify-method' What happens when manpage is ready to display.
1490 `Man-downcase-section-letters-flag' Force section letters to lower case.
1491 `Man-circular-pages-flag' Treat multiple manpage list as circular.
1492 `Man-section-translations-alist' List of section numbers and their Un*x equiv.
1493 `Man-filter-list' Background manpage filter command.
1494 `Man-mode-map' Keymap bindings for Man mode buffers.
1495 `Man-mode-hook' Normal hook run on entry to Man mode.
1496 `Man-section-regexp' Regexp describing manpage section letters.
1497 `Man-heading-regexp' Regexp describing section headers.
1498 `Man-see-also-regexp' Regexp for SEE ALSO section (or your equiv).
1499 `Man-first-heading-regexp' Regexp for first heading on a manpage.
1500 `Man-reference-regexp' Regexp matching a references in SEE ALSO.
1501 `Man-switches' Background `man' command switches.
1503 The following key bindings are currently in effect in the buffer:
1504 \\{Man-mode-map}"
1505 (setq buffer-auto-save-file-name nil
1506 mode-line-buffer-identification
1507 (list (default-value 'mode-line-buffer-identification)
1508 " {" 'Man-page-mode-string "}")
1509 truncate-lines t
1510 buffer-read-only t)
1511 (buffer-disable-undo)
1512 (auto-fill-mode -1)
1513 (setq imenu-generic-expression (list (list nil Man-heading-regexp 0)))
1514 (imenu-add-to-menubar man-imenu-title)
1515 (set (make-local-variable 'outline-regexp) Man-heading-regexp)
1516 (set (make-local-variable 'outline-level) (lambda () 1))
1517 (set (make-local-variable 'bookmark-make-record-function)
1518 'Man-bookmark-make-record))
1520 (defsubst Man-build-section-alist ()
1521 "Build the list of manpage sections."
1522 (setq Man--sections nil)
1523 (goto-char (point-min))
1524 (let ((case-fold-search nil))
1525 (while (re-search-forward Man-heading-regexp (point-max) t)
1526 (let ((section (match-string 1)))
1527 (unless (member section Man--sections)
1528 (push section Man--sections)))
1529 (forward-line 1))))
1531 (defsubst Man-build-references-alist ()
1532 "Build the list of references (in the SEE ALSO section)."
1533 (setq Man--refpages nil)
1534 (save-excursion
1535 (if (Man-find-section Man-see-also-regexp)
1536 (let ((start (progn (forward-line 1) (point)))
1537 (end (progn
1538 (Man-next-section 1)
1539 (point)))
1540 hyphenated
1541 (runningpoint -1))
1542 (save-restriction
1543 (narrow-to-region start end)
1544 (goto-char (point-min))
1545 (back-to-indentation)
1546 (while (and (not (eobp)) (/= (point) runningpoint))
1547 (setq runningpoint (point))
1548 (if (re-search-forward Man-hyphenated-reference-regexp end t)
1549 (let* ((word (match-string 0))
1550 (len (1- (length word))))
1551 (if hyphenated
1552 (setq word (concat hyphenated word)
1553 hyphenated nil
1554 ;; Update len, in case a reference spans
1555 ;; more than two lines (paranoia).
1556 len (1- (length word))))
1557 (if (memq (aref word len) '(?- ?­))
1558 (setq hyphenated (substring word 0 len)))
1559 (and (string-match Man-reference-regexp word)
1560 (not (member word Man--refpages))
1561 (push word Man--refpages))))
1562 (skip-chars-forward " \t\n,"))))))
1563 (setq Man--refpages (nreverse Man--refpages)))
1565 (defun Man-build-page-list ()
1566 "Build the list of separate manpages in the buffer."
1567 (setq Man-page-list nil)
1568 (let ((page-start (point-min))
1569 (page-end (point-max))
1570 (header ""))
1571 (goto-char page-start)
1572 (while (not (eobp))
1573 (setq header
1574 (if (looking-at Man-page-header-regexp)
1575 (match-string 1)
1576 nil))
1577 ;; Go past both the current and the next Man-first-heading-regexp
1578 (if (re-search-forward Man-first-heading-regexp nil 'move 2)
1579 (let ((p (progn (beginning-of-line) (point))))
1580 ;; We assume that the page header is delimited by blank
1581 ;; lines and that it contains at most one blank line. So
1582 ;; if we back by three blank lines we will be sure to be
1583 ;; before the page header but not before the possible
1584 ;; previous page header.
1585 (search-backward "\n\n" nil t 3)
1586 (if (re-search-forward Man-page-header-regexp p 'move)
1587 (beginning-of-line))))
1588 (setq page-end (point))
1589 (setq Man-page-list (append Man-page-list
1590 (list (list (copy-marker page-start)
1591 (copy-marker page-end)
1592 header))))
1593 (setq page-start page-end)
1596 (defun Man-strip-page-headers ()
1597 "Strip all the page headers but the first from the manpage."
1598 (let ((inhibit-read-only t)
1599 (case-fold-search nil)
1600 (header ""))
1601 (dolist (page Man-page-list)
1602 (and (nth 2 page)
1603 (goto-char (car page))
1604 (re-search-forward Man-first-heading-regexp nil t)
1605 (setq header (buffer-substring (car page) (match-beginning 0)))
1606 ;; Since the awk script collapses all successive blank
1607 ;; lines into one, and since we don't want to get rid of
1608 ;; the fast awk script, one must choose between adding
1609 ;; spare blank lines between pages when there were none and
1610 ;; deleting blank lines at page boundaries when there were
1611 ;; some. We choose the first, so we comment the following
1612 ;; line.
1613 ;; (setq header (concat "\n" header)))
1614 (while (search-forward header (nth 1 page) t)
1615 (replace-match ""))))))
1617 (defun Man-unindent ()
1618 "Delete the leading spaces that indent the manpage."
1619 (let ((inhibit-read-only t)
1620 (case-fold-search nil))
1621 (dolist (page Man-page-list)
1622 (let ((indent "")
1623 (nindent 0))
1624 (narrow-to-region (car page) (car (cdr page)))
1625 (if Man-uses-untabify-flag
1626 ;; The space characters inserted by `untabify' inherit
1627 ;; sticky text properties, which is unnecessary and looks
1628 ;; ugly with underlining (Bug#11408).
1629 (let ((text-property-default-nonsticky
1630 (cons '(face . t) text-property-default-nonsticky)))
1631 (untabify (point-min) (point-max))))
1632 (if (catch 'unindent
1633 (goto-char (point-min))
1634 (if (not (re-search-forward Man-first-heading-regexp nil t))
1635 (throw 'unindent nil))
1636 (beginning-of-line)
1637 (setq indent (buffer-substring (point)
1638 (progn
1639 (skip-chars-forward " ")
1640 (point))))
1641 (setq nindent (length indent))
1642 (if (zerop nindent)
1643 (throw 'unindent nil))
1644 (setq indent (concat indent "\\|$"))
1645 (goto-char (point-min))
1646 (while (not (eobp))
1647 (if (looking-at indent)
1648 (forward-line 1)
1649 (throw 'unindent nil)))
1650 (goto-char (point-min)))
1651 (while (not (eobp))
1652 (or (eolp)
1653 (delete-char nindent))
1654 (forward-line 1)))
1655 ))))
1658 ;; ======================================================================
1659 ;; Man mode commands
1661 (defun Man-next-section (n)
1662 "Move point to Nth next section (default 1)."
1663 (interactive "p")
1664 (let ((case-fold-search nil)
1665 (start (point)))
1666 (if (looking-at Man-heading-regexp)
1667 (forward-line 1))
1668 (if (re-search-forward Man-heading-regexp (point-max) t n)
1669 (beginning-of-line)
1670 (goto-char (point-max))
1671 ;; The last line doesn't belong to any section.
1672 (forward-line -1))
1673 ;; But don't move back from the starting point (can happen if `start'
1674 ;; is somewhere on the last line).
1675 (if (< (point) start) (goto-char start))))
1677 (defun Man-previous-section (n)
1678 "Move point to Nth previous section (default 1)."
1679 (interactive "p")
1680 (let ((case-fold-search nil))
1681 (if (looking-at Man-heading-regexp)
1682 (forward-line -1))
1683 (if (re-search-backward Man-heading-regexp (point-min) t n)
1684 (beginning-of-line)
1685 (goto-char (point-min)))))
1687 (defun Man-find-section (section)
1688 "Move point to SECTION if it exists, otherwise don't move point.
1689 Returns t if section is found, nil otherwise."
1690 (let ((curpos (point))
1691 (case-fold-search nil))
1692 (goto-char (point-min))
1693 (if (re-search-forward (concat "^" section) (point-max) t)
1694 (progn (beginning-of-line) t)
1695 (goto-char curpos)
1696 nil)
1699 (defvar Man--last-section nil)
1701 (defun Man-goto-section (section)
1702 "Move point to SECTION."
1703 (interactive
1704 (let* ((default (if (member Man--last-section Man--sections)
1705 Man--last-section
1706 (car Man--sections)))
1707 (completion-ignore-case t)
1708 (prompt (concat "Go to section (default " default "): "))
1709 (chosen (completing-read prompt Man--sections
1710 nil nil nil nil default)))
1711 (list chosen)))
1712 (setq Man--last-section section)
1713 (unless (Man-find-section section)
1714 (error "Section %s not found" section)))
1717 (defun Man-goto-see-also-section ()
1718 "Move point to the \"SEE ALSO\" section.
1719 Actually the section moved to is described by `Man-see-also-regexp'."
1720 (interactive)
1721 (if (not (Man-find-section Man-see-also-regexp))
1722 (error "%s" (concat "No " Man-see-also-regexp
1723 " section found in the current manpage"))))
1725 (defun Man-possibly-hyphenated-word ()
1726 "Return a possibly hyphenated word at point.
1727 If the word starts at the first non-whitespace column, and the
1728 previous line ends with a hyphen, return the last word on the previous
1729 line instead. Thus, if a reference to \"tcgetpgrp(3V)\" is hyphenated
1730 as \"tcgetp-grp(3V)\", and point is at \"grp(3V)\", we return
1731 \"tcgetp-\" instead of \"grp\"."
1732 (save-excursion
1733 (skip-syntax-backward "w()")
1734 (skip-chars-forward " \t")
1735 (let ((beg (point))
1736 (word (current-word)))
1737 (when (eq beg (save-excursion
1738 (back-to-indentation)
1739 (point)))
1740 (end-of-line 0)
1741 (if (eq (char-before) ?-)
1742 (setq word (current-word))))
1743 word)))
1745 (defvar Man--last-refpage nil)
1747 (defun Man-follow-manual-reference (reference)
1748 "Get one of the manpages referred to in the \"SEE ALSO\" section.
1749 Specify which REFERENCE to use; default is based on word at point."
1750 (interactive
1751 (if (not Man--refpages)
1752 (error "There are no references in the current man page")
1753 (list
1754 (let* ((default (or
1755 (car (all-completions
1756 (let ((word
1757 (or (Man-possibly-hyphenated-word)
1758 "")))
1759 ;; strip a trailing '-':
1760 (if (string-match "-$" word)
1761 (substring word 0
1762 (match-beginning 0))
1763 word))
1764 Man--refpages))
1765 (if (member Man--last-refpage Man--refpages)
1766 Man--last-refpage
1767 (car Man--refpages))))
1768 (defaults
1769 (mapcar 'substring-no-properties
1770 (cons default Man--refpages)))
1771 (prompt (concat "Refer to (default " default "): "))
1772 (chosen (completing-read prompt Man--refpages
1773 nil nil nil nil defaults)))
1774 chosen))))
1775 (if (not Man--refpages)
1776 (error "Can't find any references in the current manpage")
1777 (setq Man--last-refpage reference)
1778 (Man-getpage-in-background
1779 (Man-translate-references reference))))
1781 (defun Man-kill ()
1782 "Kill the buffer containing the manpage."
1783 (interactive)
1784 (quit-window t))
1786 (defun Man-quit ()
1787 "Bury the buffer containing the manpage."
1788 (interactive)
1789 (quit-window))
1791 (defun Man-goto-page (page &optional noerror)
1792 "Go to the manual page on page PAGE."
1793 (interactive
1794 (if (not Man-page-list)
1795 (error "Not a man page buffer")
1796 (if (= (length Man-page-list) 1)
1797 (error "You're looking at the only manpage in the buffer")
1798 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1799 (length Man-page-list)))))))
1800 (if (and (not Man-page-list) (not noerror))
1801 (error "Not a man page buffer"))
1802 (when Man-page-list
1803 (if (or (< page 1)
1804 (> page (length Man-page-list)))
1805 (user-error "No manpage %d found" page))
1806 (let* ((page-range (nth (1- page) Man-page-list))
1807 (page-start (car page-range))
1808 (page-end (car (cdr page-range))))
1809 (setq Man-current-page page
1810 Man-page-mode-string (Man-make-page-mode-string))
1811 (widen)
1812 (goto-char page-start)
1813 (narrow-to-region page-start page-end)
1814 (Man-build-section-alist)
1815 (Man-build-references-alist)
1816 (goto-char (point-min)))))
1819 (defun Man-next-manpage ()
1820 "Find the next manpage entry in the buffer."
1821 (interactive)
1822 (if (= (length Man-page-list) 1)
1823 (error "This is the only manpage in the buffer"))
1824 (if (< Man-current-page (length Man-page-list))
1825 (Man-goto-page (1+ Man-current-page))
1826 (if Man-circular-pages-flag
1827 (Man-goto-page 1)
1828 (error "You're looking at the last manpage in the buffer"))))
1830 (defun Man-previous-manpage ()
1831 "Find the previous manpage entry in the buffer."
1832 (interactive)
1833 (if (= (length Man-page-list) 1)
1834 (error "This is the only manpage in the buffer"))
1835 (if (> Man-current-page 1)
1836 (Man-goto-page (1- Man-current-page))
1837 (if Man-circular-pages-flag
1838 (Man-goto-page (length Man-page-list))
1839 (error "You're looking at the first manpage in the buffer"))))
1841 ;; Header file support
1842 (defun Man-view-header-file (file)
1843 "View a header file specified by FILE from `Man-header-file-path'."
1844 (let ((path Man-header-file-path)
1845 complete-path)
1846 (while path
1847 (setq complete-path (expand-file-name file (car path))
1848 path (cdr path))
1849 (if (file-readable-p complete-path)
1850 (progn (view-file complete-path)
1851 (setq path nil))
1852 (setq complete-path nil)))
1853 complete-path))
1855 ;;; Bookmark Man Support
1856 (declare-function bookmark-make-record-default
1857 "bookmark" (&optional no-file no-context posn))
1858 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
1859 (declare-function bookmark-default-handler "bookmark" (bmk))
1860 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
1862 (defun Man-default-bookmark-title ()
1863 "Default bookmark name for Man or WoMan pages.
1864 Uses `Man-name-local-regexp'."
1865 (save-excursion
1866 (goto-char (point-min))
1867 (when (re-search-forward Man-name-local-regexp nil t)
1868 (skip-chars-forward "\n\t ")
1869 (buffer-substring-no-properties (point) (line-end-position)))))
1871 (defun Man-bookmark-make-record ()
1872 "Make a bookmark entry for a Man buffer."
1873 `(,(Man-default-bookmark-title)
1874 ,@(bookmark-make-record-default 'no-file)
1875 (location . ,(concat "man " Man-arguments))
1876 (man-args . ,Man-arguments)
1877 (handler . Man-bookmark-jump)))
1879 ;;;###autoload
1880 (defun Man-bookmark-jump (bookmark)
1881 "Default bookmark handler for Man buffers."
1882 (let* ((man-args (bookmark-prop-get bookmark 'man-args))
1883 ;; Let bookmark.el do the window handling.
1884 ;; This let-binding needs to be active during the call to both
1885 ;; Man-getpage-in-background and accept-process-output.
1886 (Man-notify-method 'meek)
1887 (buf (Man-getpage-in-background man-args))
1888 (proc (get-buffer-process buf)))
1889 (while (and proc (eq (process-status proc) 'run))
1890 (accept-process-output proc))
1891 (bookmark-default-handler
1892 `("" (buffer . ,buf) . ,(bookmark-get-bookmark-record bookmark)))))
1895 ;; Init the man package variables, if not already done.
1896 (Man-init-defvars)
1898 (provide 'man)
1900 ;;; man.el ends here