Minor text-quoting-style fixes
[emacs.git] / lisp / man.el
blobd127decfebd8497411944ae66f67972d577ee225
1 ;;; man.el --- browse UNIX manual pages
3 ;; Copyright (C) 1993-1994, 1996-1997, 2001-2016 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 button-buffer-map)
437 (define-key map [?\S-\ ] 'scroll-down-command)
438 (define-key map " " 'scroll-up-command)
439 (define-key map "\177" 'scroll-down-command)
440 (define-key map "n" 'Man-next-section)
441 (define-key map "p" 'Man-previous-section)
442 (define-key map "\en" 'Man-next-manpage)
443 (define-key map "\ep" 'Man-previous-manpage)
444 (define-key map ">" 'end-of-buffer)
445 (define-key map "<" 'beginning-of-buffer)
446 (define-key map "." 'beginning-of-buffer)
447 (define-key map "r" 'Man-follow-manual-reference)
448 (define-key map "g" 'Man-goto-section)
449 (define-key map "s" 'Man-goto-see-also-section)
450 (define-key map "k" 'Man-kill)
451 (define-key map "q" 'Man-quit)
452 (define-key map "u" 'Man-update-manpage)
453 (define-key map "m" 'man)
454 ;; Not all the man references get buttons currently. The text in the
455 ;; manual page can contain references to other man pages
456 (define-key map "\r" 'man-follow)
457 (define-key map "?" 'describe-mode)
459 (easy-menu-define nil map
460 "`Man-mode' menu."
461 '("Man"
462 ["Next Section" Man-next-section t]
463 ["Previous Section" Man-previous-section t]
464 ["Go To Section..." Man-goto-section t]
465 ["Go To \"SEE ALSO\" Section" Man-goto-see-also-section
466 :active (cl-member Man-see-also-regexp Man--sections
467 :test #'string-match-p)]
468 ["Follow Reference..." Man-follow-manual-reference
469 :active Man--refpages
470 :help "Go to a manpage referred to in the \"SEE ALSO\" section"]
471 "--"
472 ["Next Manpage" Man-next-manpage
473 :active (> (length Man-page-list) 1)]
474 ["Previous Manpage" Man-previous-manpage
475 :active (> (length Man-page-list) 1)]
476 "--"
477 ["Man..." man t]
478 ["Kill Buffer" Man-kill t]
479 ["Quit" Man-quit t]))
480 map)
481 "Keymap for Man mode.")
483 ;; buttons
484 (define-button-type 'Man-abstract-xref-man-page
485 'follow-link t
486 'help-echo "mouse-2, RET: display this man page"
487 'func nil
488 'action #'Man-xref-button-action)
490 (defun Man-xref-button-action (button)
491 (let ((target (button-get button 'Man-target-string)))
492 (funcall
493 (button-get button 'func)
494 (cond ((null target)
495 (button-label button))
496 ((functionp target)
497 (funcall target (button-start button)))
498 (t target)))))
500 (define-button-type 'Man-xref-man-page
501 :supertype 'Man-abstract-xref-man-page
502 'func 'man-follow)
505 (define-button-type 'Man-xref-header-file
506 'action (lambda (button)
507 (let ((w (button-get button 'Man-target-string)))
508 (unless (Man-view-header-file w)
509 (error "Cannot find header file: %s" w))))
510 'follow-link t
511 'help-echo "mouse-2: display this header file")
513 (define-button-type 'Man-xref-normal-file
514 'action (lambda (button)
515 (let ((f (substitute-in-file-name
516 (button-get button 'Man-target-string))))
517 (if (file-exists-p f)
518 (if (file-readable-p f)
519 (view-file f)
520 (error "Cannot read a file: %s" f))
521 (error "Cannot find a file: %s" f))))
522 'follow-link t
523 'help-echo "mouse-2: display this file")
526 ;; ======================================================================
527 ;; utilities
529 (defun Man-init-defvars ()
530 "Used for initializing variables based on display's color support.
531 This is necessary if one wants to dump man.el with Emacs."
533 ;; Avoid possible error in call-process by using a directory that must exist.
534 (let ((default-directory "/"))
535 (setq Man-sed-script
536 (cond
537 (Man-fontify-manpage-flag
538 nil)
539 ((eq 0 (call-process Man-sed-command nil nil nil Man-sysv-sed-script))
540 Man-sysv-sed-script)
541 ((eq 0 (call-process Man-sed-command nil nil nil Man-berkeley-sed-script))
542 Man-berkeley-sed-script)
544 nil))))
546 (setq Man-filter-list
547 ;; Avoid trailing nil which confuses customize.
548 (apply 'list
549 (cons
550 Man-sed-command
551 (if (eq system-type 'windows-nt)
552 ;; Windows needs ".." quoting, not '..'.
553 (list
554 "-e \"/Reformatting page. Wait/d\""
555 "-e \"/Reformatting entry. Wait/d\""
556 "-e \"/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d\""
557 "-e \"/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d\""
558 "-e \"/^Printed[ \t][0-9].*[0-9]$/d\""
559 "-e \"/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d\""
560 "-e \"/^[A-Za-z].*Last[ \t]change:/d\""
561 "-e \"/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d\""
562 "-e \"/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d\"")
563 (list
564 (if Man-sed-script
565 (concat "-e '" Man-sed-script "'")
567 "-e '/^[\001-\032][\001-\032]*$/d'"
568 "-e '/\e[789]/s///g'"
569 "-e '/Reformatting page. Wait/d'"
570 "-e '/Reformatting entry. Wait/d'"
571 "-e '/^[ \t]*Hewlett-Packard[ \t]Company[ \t]*-[ \t][0-9]*[ \t]-/d'"
572 "-e '/^[ \t]*Hewlett-Packard[ \t]*-[ \t][0-9]*[ \t]-.*$/d'"
573 "-e '/^[ \t][ \t]*-[ \t][0-9]*[ \t]-[ \t]*Formatted:.*[0-9]$/d'"
574 "-e '/^[ \t]*Page[ \t][0-9]*.*(printed[ \t][0-9\\/]*)$/d'"
575 "-e '/^Printed[ \t][0-9].*[0-9]$/d'"
576 "-e '/^[ \t]*X[ \t]Version[ \t]1[01].*Release[ \t][0-9]/d'"
577 "-e '/^[A-Za-z].*Last[ \t]change:/d'"
578 "-e '/^Sun[ \t]Release[ \t][0-9].*[0-9]$/d'"
579 "-e '/[ \t]*Copyright [0-9]* UNIX System Laboratories, Inc.$/d'"
580 "-e '/^[ \t]*Rev\\..*Page [0-9][0-9]*$/d'"
582 ;; Windows doesn't support multi-line commands, so don't
583 ;; invoke Awk there.
584 (unless (eq system-type 'windows-nt)
585 (cons
586 Man-awk-command
587 (list
588 "'\n"
589 "BEGIN { blankline=0; anonblank=0; }\n"
590 "/^$/ { if (anonblank==0) next; }\n"
591 "{ anonblank=1; }\n"
592 "/^$/ { blankline++; next; }\n"
593 "{ if (blankline>0) { print \"\"; blankline=0; } print $0; }\n"
596 (if (not Man-uses-untabify-flag)
597 ;; The outer list will be stripped off by apply.
598 (list (cons
599 Man-untabify-command
600 Man-untabify-command-args))
604 (defsubst Man-make-page-mode-string ()
605 "Formats part of the mode line for Man mode."
606 (format "%s page %d of %d"
607 (or (nth 2 (nth (1- Man-current-page) Man-page-list))
609 Man-current-page
610 (length Man-page-list)))
612 (defsubst Man-build-man-command ()
613 "Builds the entire background manpage and cleaning command."
614 (let ((command (concat manual-program " " Man-switches
615 (cond
616 ;; Already has %s
617 ((string-match "%s" manual-program) "")
618 ;; Stock MS-DOS shells cannot redirect stderr;
619 ;; `call-process' below sends it to /dev/null,
620 ;; so we don't need `2>' even with DOS shells
621 ;; which do support stderr redirection.
622 ((not (fboundp 'make-process)) " %s")
623 ((concat " %s 2>" null-device)))))
624 (flist Man-filter-list))
625 (while (and flist (car flist))
626 (let ((pcom (car (car flist)))
627 (pargs (cdr (car flist))))
628 (setq command
629 (concat command " | " pcom " "
630 (mapconcat (lambda (phrase)
631 (if (not (stringp phrase))
632 (error "Malformed Man-filter-list"))
633 phrase)
634 pargs " ")))
635 (setq flist (cdr flist))))
636 command))
639 (defun Man-translate-cleanup (string)
640 "Strip leading, trailing and middle spaces."
641 (when (stringp string)
642 ;; Strip leading and trailing
643 (if (string-match "^[ \t\f\r\n]*\\(.+[^ \t\f\r\n]\\)" string)
644 (setq string (match-string 1 string)))
645 ;; middle spaces
646 (setq string (replace-regexp-in-string "[\t\r\n]" " " string))
647 (setq string (replace-regexp-in-string " +" " " string))
648 string))
650 (defun Man-translate-references (ref)
651 "Translates REF from \"chmod(2V)\" to \"2v chmod\" style.
652 Leave it as is if already in that style. Possibly downcase and
653 translate the section (see the `Man-downcase-section-letters-flag'
654 and the `Man-section-translations-alist' variables)."
655 (let ((name "")
656 (section "")
657 (slist Man-section-translations-alist))
658 (setq ref (Man-translate-cleanup ref))
659 (cond
660 ;; "chmod(2V)" case ?
661 ((string-match (concat "^" Man-reference-regexp "$") ref)
662 (setq name (replace-regexp-in-string "[\n\t ]" "" (match-string 1 ref))
663 section (match-string 3 ref)))
664 ;; "2v chmod" case ?
665 ((string-match (concat "^\\(" Man-section-regexp
666 "\\) +\\(" Man-name-regexp "\\)$") ref)
667 (setq name (match-string 2 ref)
668 section (match-string 1 ref))))
669 (if (string= name "")
670 ref ; Return the reference as is
671 (if Man-downcase-section-letters-flag
672 (setq section (downcase section)))
673 (while slist
674 (let ((s1 (car (car slist)))
675 (s2 (cdr (car slist))))
676 (setq slist (cdr slist))
677 (if Man-downcase-section-letters-flag
678 (setq s1 (downcase s1)))
679 (if (not (string= s1 section)) nil
680 (setq section (if Man-downcase-section-letters-flag
681 (downcase s2)
683 slist nil))))
684 (concat Man-specified-section-option section " " name))))
686 (defun Man-support-local-filenames ()
687 "Return non-nil if the man command supports local filenames.
688 Different man programs support this feature in different ways.
689 The default Debian man program (\"man-db\") has a `--local-file'
690 \(or `-l') option for this purpose. The default Red Hat man
691 program has no such option, but interprets any name containing
692 a \"/\" as a local filename. The function returns either `man-db'
693 `man', or nil."
694 (if (eq Man-support-local-filenames 'auto-detect)
695 (setq Man-support-local-filenames
696 (with-temp-buffer
697 (let ((default-directory
698 ;; Ensure that `default-directory' exists and is readable.
699 (if (file-accessible-directory-p default-directory)
700 default-directory
701 (expand-file-name "~/"))))
702 (ignore-errors
703 (call-process manual-program nil t nil "--help")))
704 (cond ((search-backward "--local-file" nil 'move)
705 'man-db)
706 ;; This feature seems to be present in at least ver 1.4f,
707 ;; which is about 20 years old.
708 ;; I don't know if this version has an official name?
709 ((looking-at "^man, versione? [1-9]")
710 'man))))
711 Man-support-local-filenames))
714 ;; ======================================================================
715 ;; default man entry: get word near point
717 (defun Man-default-man-entry (&optional pos)
718 "Guess default manual entry based on the text near position POS.
719 POS defaults to `point'."
720 (let (word start column distance)
721 (save-excursion
722 (when pos (goto-char pos))
723 (setq pos (point))
724 ;; The default title is the nearest entry-like object before or
725 ;; after POS.
726 (if (and (skip-chars-backward " \ta-zA-Z0-9+")
727 (not (zerop (skip-chars-backward "(")))
728 ;; Try to handle the special case where POS is on a
729 ;; section number.
730 (looking-at
731 (concat "([ \t]*\\(" Man-section-regexp "\\)[ \t]*)"))
732 ;; We skipped a valid section number backwards, look at
733 ;; preceding text.
734 (or (and (skip-chars-backward ",; \t")
735 (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:"))))
736 ;; Not a valid entry, move POS after closing paren.
737 (not (setq pos (match-end 0)))))
738 ;; We have a candidate, make `start' record its starting
739 ;; position.
740 (setq start (point))
741 ;; Otherwise look at char before POS.
742 (goto-char pos)
743 (if (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:")))
744 ;; Our candidate is just before or around POS.
745 (setq start (point))
746 ;; Otherwise record the current column and look backwards.
747 (setq column (current-column))
748 (skip-chars-backward ",; \t")
749 ;; Record the distance traveled.
750 (setq distance (- column (current-column)))
751 (when (looking-back
752 (concat "([ \t]*\\(?:" Man-section-regexp "\\)[ \t]*)")
753 (line-beginning-position))
754 ;; Skip section number backwards.
755 (goto-char (match-beginning 0))
756 (skip-chars-backward " \t"))
757 (if (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:")))
758 (progn
759 ;; We have a candidate before POS ...
760 (setq start (point))
761 (goto-char pos)
762 (if (and (skip-chars-forward ",; \t")
763 (< (- (current-column) column) distance)
764 (looking-at "[-a-zA-Z0-9._+:]"))
765 ;; ... but the one after POS is better.
766 (setq start (point))
767 ;; ... and anything after POS is worse.
768 (goto-char start)))
769 ;; No candidate before POS.
770 (goto-char pos)
771 (skip-chars-forward ",; \t")
772 (setq start (point)))))
773 ;; We have found a suitable starting point, try to skip at least
774 ;; one character.
775 (skip-chars-forward "-a-zA-Z0-9._+:")
776 (setq word (buffer-substring-no-properties start (point)))
777 ;; If there is a continuation at the end of line, check the
778 ;; following line too, eg:
779 ;; see this-
780 ;; command-here(1)
781 ;; Note: This code gets executed iff our entry is after POS.
782 (when (looking-at "‐?[ \t\r\n]+\\([-a-zA-Z0-9._+:]+\\)([0-9])")
783 (setq word (concat word (match-string-no-properties 1)))
784 ;; Make sure the section number gets included by the code below.
785 (goto-char (match-end 1)))
786 (when (string-match "[-._]+$" word)
787 (setq word (substring word 0 (match-beginning 0))))
788 ;; The following was commented out since the preceding code
789 ;; should not produce a leading "*" in the first place.
790 ;;; ;; If looking at something like *strcat(... , remove the '*'
791 ;;; (when (string-match "^*" word)
792 ;;; (setq word (substring word 1)))
793 (concat
794 word
795 (and (not (string-equal word ""))
796 ;; If looking at something like ioctl(2) or brc(1M),
797 ;; include the section number in the returned value.
798 (looking-at
799 (concat "[ \t]*([ \t]*\\(" Man-section-regexp "\\)[ \t]*)"))
800 (format "(%s)" (match-string-no-properties 1)))))))
803 ;; ======================================================================
804 ;; Top level command and background process sentinel
806 ;; For compatibility with older versions.
807 ;;;###autoload
808 (defalias 'manual-entry 'man)
810 (defvar Man-completion-cache nil
811 ;; On my machine, "man -k" is so fast that a cache makes no sense,
812 ;; but apparently that's not the case in all cases, so let's add a cache.
813 "Cache of completion table of the form (PREFIX . TABLE).")
815 (defvar Man-man-k-use-anchor
816 ;; man-db or man-1.*
817 (memq system-type '(gnu gnu/linux gnu/kfreebsd))
818 "If non-nil prepend ^ to the prefix passed to \"man -k\" for completion.
819 The value should be nil if \"man -k ^PREFIX\" may omit some man
820 pages whose names start with PREFIX.
822 Currently, the default value depends on `system-type' and is
823 non-nil where the standard man programs are known to behave
824 properly. Setting the value to nil always gives correct results
825 but computing the list of completions may take a bit longer.")
827 (defun Man-parse-man-k ()
828 "Parse \"man -k\" output and return the list of page names.
830 The current buffer should contain the output of a command of the
831 form \"man -k keyword\", which is traditionally also available with
832 apropos(1).
834 While POSIX man(1p) is a bit vague about what to expect here,
835 this function tries to parse some commonly used formats, which
836 can be described in the following informal way, with square brackets
837 indicating optional parts and whitespace being interpreted
838 somewhat loosely.
840 foo[, bar [, ...]] [other stuff] (sec) - description
841 foo(sec)[, bar(sec) [, ...]] [other stuff] - description
843 For more details and some regression tests, please see
844 test/automated/man-tests.el in the emacs repository."
845 (goto-char (point-min))
846 ;; See man-tests for data about which systems use which format (hopefully we
847 ;; will be able to simplify the code if/when some of those formats aren't
848 ;; used any more).
849 (let (table)
850 (while (search-forward-regexp "^\\([^ \t,\n]+\\)\\(.*?\\)\
851 \\(?:[ \t]\\(([^ \t,\n]+?)\\)\\)?\\(?:[ \t]+- ?\\(.*\\)\\)?$" nil t)
852 (let ((section (match-string 3))
853 (description (match-string 4))
854 (bound (match-end 2)))
855 (goto-char (match-end 1))
856 (while
857 (progn
858 ;; The first regexp grouping may already match the section
859 ;; tacked on to the name, which is ok since for the formats we
860 ;; claim to support the third (non-shy) grouping does not
861 ;; match in this case, i.e., section is nil.
862 (push (propertize (concat (match-string 1) section)
863 'help-echo description)
864 table)
865 (search-forward-regexp "\\=, *\\([^ \t,]+\\)" bound t)))))
866 (nreverse table)))
868 (defun Man-completion-table (string pred action)
869 (cond
870 ;; This ends up returning t for pretty much any string, and hence leads to
871 ;; spurious "complete but not unique" messages. And since `man' doesn't
872 ;; require-match anyway, there's not point being clever.
873 ;;((eq action 'lambda) (not (string-match "([^)]*\\'" string)))
874 ((equal string "-k")
875 ;; Let SPC (minibuffer-complete-word) insert the space.
876 (complete-with-action action '("-k ") string pred))
878 (let ((table (cdr Man-completion-cache))
879 (section nil)
880 (prefix string))
881 (when (string-match "\\`\\([[:digit:]].*?\\) " string)
882 (setq section (match-string 1 string))
883 (setq prefix (substring string (match-end 0))))
884 (unless (and Man-completion-cache
885 (string-prefix-p (car Man-completion-cache) prefix))
886 (with-temp-buffer
887 (setq default-directory "/") ;; in case inherited doesn't exist
888 ;; Actually for my `man' the arg is a regexp.
889 ;; POSIX says it must be ERE and "man-db" seems to agree,
890 ;; whereas under MacOSX it seems to be BRE-style and doesn't
891 ;; accept backslashes at all. Let's not bother to
892 ;; quote anything.
893 (let ((process-environment (copy-sequence process-environment)))
894 (setenv "COLUMNS" "999") ;; don't truncate long names
895 ;; manual-program might not even exist. And since it's
896 ;; run differently in Man-getpage-in-background, an error
897 ;; here may not necessarily mean that we'll also get an
898 ;; error later.
899 (ignore-errors
900 (call-process manual-program nil '(t nil) nil
901 "-k" (concat (when (or Man-man-k-use-anchor
902 (string-equal prefix ""))
903 "^")
904 prefix))))
905 (setq table (Man-parse-man-k)))
906 ;; Cache the table for later reuse.
907 (setq Man-completion-cache (cons prefix table)))
908 ;; The table may contain false positives since the match is made
909 ;; by "man -k" not just on the manpage's name.
910 (if section
911 (let ((re (concat "(" (regexp-quote section) ")\\'")))
912 (dolist (comp (prog1 table (setq table nil)))
913 (if (string-match re comp)
914 (push (substring comp 0 (match-beginning 0)) table)))
915 (completion-table-with-context (concat section " ") table
916 prefix pred action))
917 ;; If the current text looks like a possible section name,
918 ;; then add a completion entry that just adds a space so SPC
919 ;; can be used to insert a space.
920 (if (string-match "\\`[[:digit:]]" string)
921 (push (concat string " ") table))
922 (let ((res (complete-with-action action table string pred)))
923 ;; In case we're completing to a single name that exists in
924 ;; several sections, the longest prefix will look like "foo(".
925 (if (and (stringp res)
926 (string-match "([^(]*\\'" res)
927 ;; In case the paren was already in `prefix', don't
928 ;; remove it.
929 (> (match-beginning 0) (length prefix)))
930 (substring res 0 (match-beginning 0))
931 res)))))))
933 ;;;###autoload
934 (defun man (man-args)
935 "Get a Un*x manual page and put it in a buffer.
936 This command is the top-level command in the man package.
937 It runs a Un*x command to retrieve and clean a manpage in the
938 background and places the results in a `Man-mode' browsing
939 buffer. The variable `Man-width' defines the number of columns in
940 formatted manual pages. The buffer is displayed immediately.
941 The variable `Man-notify-method' defines how the buffer is displayed.
942 If a buffer already exists for this man page, it will be displayed
943 without running the man command.
945 For a manpage from a particular section, use either of the
946 following. \"cat(1)\" is how cross-references appear and is
947 passed to man as \"1 cat\".
949 cat(1)
950 1 cat
952 To see manpages from all sections related to a subject, use an
953 \"all pages\" option (which might be \"-a\" if it's not the
954 default), then step through with `Man-next-manpage' (\\<Man-mode-map>\\[Man-next-manpage]) etc.
955 Add to `Man-switches' to make this option permanent.
957 -a chmod
959 An explicit filename can be given too. Use -l if it might
960 otherwise look like a page name.
962 /my/file/name.1.gz
963 -l somefile.1
965 An \"apropos\" query with -k gives a buffer of matching page
966 names or descriptions. The pattern argument is usually an
967 \"grep -E\" style regexp.
969 -k pattern"
971 (interactive
972 (list (let* ((default-entry (Man-default-man-entry))
973 ;; ignore case because that's friendly for bizarre
974 ;; caps things like the X11 function names and because
975 ;; "man" itself is case-insensitive on the command line
976 ;; so you're accustomed not to bother about the case
977 ;; ("man -k" is case-insensitive similarly, so the
978 ;; table has everything available to complete)
979 (completion-ignore-case t)
980 Man-completion-cache ;Don't cache across calls.
981 (input (completing-read
982 (format "Manual entry%s"
983 (if (string= default-entry "")
984 ": "
985 (format " (default %s): " default-entry)))
986 'Man-completion-table
987 nil nil nil 'Man-topic-history default-entry)))
988 (if (string= input "")
989 (error "No man args given")
990 input))))
992 ;; Possibly translate the "subject(section)" syntax into the
993 ;; "section subject" syntax and possibly downcase the section.
994 (setq man-args (Man-translate-references man-args))
996 (Man-getpage-in-background man-args))
998 ;;;###autoload
999 (defun man-follow (man-args)
1000 "Get a Un*x manual page of the item under point and put it in a buffer."
1001 (interactive (list (Man-default-man-entry)))
1002 (if (or (not man-args)
1003 (string= man-args ""))
1004 (error "No item under point")
1005 (man man-args)))
1007 (defmacro Man-start-calling (&rest body)
1008 "Start the man command in `body' after setting up the environment"
1009 `(let ((process-environment (copy-sequence process-environment))
1010 ;; The following is so Awk script gets \n intact
1011 ;; But don't prevent decoding of the outside.
1012 (coding-system-for-write 'raw-text-unix)
1013 ;; We must decode the output by a coding system that the
1014 ;; system's locale suggests in multibyte mode.
1015 (coding-system-for-read locale-coding-system)
1016 ;; Avoid possible error by using a directory that always exists.
1017 (default-directory
1018 (if (and (file-directory-p default-directory)
1019 (not (find-file-name-handler default-directory
1020 'file-directory-p)))
1021 default-directory
1022 "/")))
1023 ;; Prevent any attempt to use display terminal fanciness.
1024 (setenv "TERM" "dumb")
1025 ;; In Debian Woody, at least, we get overlong lines under X
1026 ;; unless COLUMNS or MANWIDTH is set. This isn't a problem on
1027 ;; a tty. man(1) says:
1028 ;; MANWIDTH
1029 ;; If $MANWIDTH is set, its value is used as the line
1030 ;; length for which manual pages should be formatted.
1031 ;; If it is not set, manual pages will be formatted
1032 ;; with a line length appropriate to the current ter-
1033 ;; minal (using an ioctl(2) if available, the value of
1034 ;; $COLUMNS, or falling back to 80 characters if nei-
1035 ;; ther is available).
1036 (when (or window-system
1037 (not (or (getenv "MANWIDTH") (getenv "COLUMNS"))))
1038 ;; Since the page buffer is displayed beforehand,
1039 ;; we can select its window and get the window/frame width.
1040 (setenv "COLUMNS" (number-to-string
1041 (cond
1042 ((and (integerp Man-width) (> Man-width 0))
1043 Man-width)
1044 (Man-width
1045 (if (window-live-p (get-buffer-window (current-buffer) t))
1046 (with-selected-window (get-buffer-window (current-buffer) t)
1047 (frame-width))
1048 (frame-width)))
1050 (if (window-live-p (get-buffer-window (current-buffer) t))
1051 (with-selected-window (get-buffer-window (current-buffer) t)
1052 (window-width))
1053 (window-width)))))))
1054 ;; Since man-db 2.4.3-1, man writes plain text with no escape
1055 ;; sequences when stdout is not a tty. In 2.5.0, the following
1056 ;; env-var was added to allow control of this (see Debian Bug#340673).
1057 (setenv "MAN_KEEP_FORMATTING" "1")
1058 ,@body))
1060 (defun Man-getpage-in-background (topic)
1061 "Use TOPIC to build and fire off the manpage and cleaning command.
1062 Return the buffer in which the manpage will appear."
1063 (let* ((man-args topic)
1064 (bufname (concat "*Man " man-args "*"))
1065 (buffer (get-buffer bufname)))
1066 (if buffer
1067 (Man-notify-when-ready buffer)
1068 (require 'env)
1069 (message "Invoking %s %s in the background" manual-program man-args)
1070 (setq buffer (generate-new-buffer bufname))
1071 (with-current-buffer buffer
1072 (Man-notify-when-ready buffer)
1073 (setq buffer-undo-list t)
1074 (setq Man-original-frame (selected-frame))
1075 (setq Man-arguments man-args)
1076 (Man-mode)
1077 (setq mode-line-process
1078 (concat " " (propertize (if Man-fontify-manpage-flag
1079 "[formatting...]"
1080 "[cleaning...]")
1081 'face 'mode-line-emphasis)))
1082 (Man-start-calling
1083 (if (fboundp 'make-process)
1084 (let ((proc (start-process
1085 manual-program buffer
1086 (if (memq system-type '(cygwin windows-nt))
1087 shell-file-name
1088 "sh")
1089 shell-command-switch
1090 (format (Man-build-man-command) man-args))))
1091 (set-process-sentinel proc 'Man-bgproc-sentinel)
1092 (set-process-filter proc 'Man-bgproc-filter))
1093 (let* ((inhibit-read-only t)
1094 (exit-status
1095 (call-process shell-file-name nil (list buffer nil) nil
1096 shell-command-switch
1097 (format (Man-build-man-command) man-args)))
1098 (msg ""))
1099 (or (and (numberp exit-status)
1100 (= exit-status 0))
1101 (and (numberp exit-status)
1102 (setq msg
1103 (format "exited abnormally with code %d"
1104 exit-status)))
1105 (setq msg exit-status))
1106 (if Man-fontify-manpage-flag
1107 (Man-fontify-manpage)
1108 (Man-cleanup-manpage))
1109 (Man-bgproc-sentinel bufname msg))))))
1110 buffer))
1112 (defun Man-update-manpage ()
1113 "Reformat current manpage by calling the man command again synchronously."
1114 (interactive)
1115 (when (eq Man-arguments nil)
1116 ;;this shouldn't happen unless it is not in a Man buffer."
1117 (error "Man-arguments not initialized"))
1118 (let ((old-pos (point))
1119 (text (current-word))
1120 (old-size (buffer-size))
1121 (inhibit-read-only t)
1122 (buffer-read-only nil))
1123 (erase-buffer)
1124 (Man-start-calling
1125 (call-process shell-file-name nil (list (current-buffer) nil) nil
1126 shell-command-switch
1127 (format (Man-build-man-command) Man-arguments)))
1128 (if Man-fontify-manpage-flag
1129 (Man-fontify-manpage)
1130 (Man-cleanup-manpage))
1131 (goto-char old-pos)
1132 ;;restore the point, not strictly right.
1133 (unless (or (eq text nil) (= old-size (buffer-size)))
1134 (let ((case-fold-search nil))
1135 (if (> old-size (buffer-size))
1136 (search-backward text nil t))
1137 (search-forward text nil t)))))
1139 (defun Man-notify-when-ready (man-buffer)
1140 "Notify the user when MAN-BUFFER is ready.
1141 See the variable `Man-notify-method' for the different notification behaviors."
1142 (let ((saved-frame (with-current-buffer man-buffer
1143 Man-original-frame)))
1144 (pcase Man-notify-method
1145 (`newframe
1146 ;; Since we run asynchronously, perhaps while Emacs is waiting
1147 ;; for input, we must not leave a different buffer current. We
1148 ;; can't rely on the editor command loop to reselect the
1149 ;; selected window's buffer.
1150 (save-excursion
1151 (let ((frame (make-frame Man-frame-parameters)))
1152 (set-window-buffer (frame-selected-window frame) man-buffer)
1153 (set-window-dedicated-p (frame-selected-window frame) t)
1154 (or (display-multi-frame-p frame)
1155 (select-frame frame)))))
1156 (`pushy
1157 (switch-to-buffer man-buffer))
1158 (`bully
1159 (and (frame-live-p saved-frame)
1160 (select-frame saved-frame))
1161 (pop-to-buffer man-buffer)
1162 (delete-other-windows))
1163 (`aggressive
1164 (and (frame-live-p saved-frame)
1165 (select-frame saved-frame))
1166 (pop-to-buffer man-buffer))
1167 (`friendly
1168 (and (frame-live-p saved-frame)
1169 (select-frame saved-frame))
1170 (display-buffer man-buffer 'not-this-window))
1171 (`polite
1172 (beep)
1173 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
1174 (`quiet
1175 (message "Manual buffer %s is ready" (buffer-name man-buffer)))
1176 (_ ;; meek
1177 (message ""))
1180 (defun Man-softhyphen-to-minus ()
1181 ;; \255 is SOFT HYPHEN in Latin-N. Versions of Debian man, at
1182 ;; least, emit it even when not in a Latin-N locale.
1183 (unless (eq t (compare-strings "latin-" 0 nil
1184 current-language-environment 0 6 t))
1185 (goto-char (point-min))
1186 (let ((str "\255"))
1187 (if enable-multibyte-characters
1188 (setq str (string-as-multibyte str)))
1189 (while (search-forward str nil t) (replace-match "-")))))
1191 (defun Man-fontify-manpage ()
1192 "Convert overstriking and underlining to the correct fonts.
1193 Same for the ANSI bold and normal escape sequences."
1194 (interactive)
1195 (goto-char (point-min))
1196 ;; Fontify ANSI escapes.
1197 (let ((ansi-color-apply-face-function
1198 (lambda (beg end face)
1199 (when face
1200 (put-text-property beg end 'face face))))
1201 (ansi-color-map Man-ansi-color-map))
1202 (ansi-color-apply-on-region (point-min) (point-max)))
1203 ;; Other highlighting.
1204 (let ((buffer-undo-list t))
1205 (if (< (buffer-size) (position-bytes (point-max)))
1206 ;; Multibyte characters exist.
1207 (progn
1208 (goto-char (point-min))
1209 (while (and (search-forward "__\b\b" nil t) (not (eobp)))
1210 (backward-delete-char 4)
1211 (put-text-property (point) (1+ (point)) 'face 'Man-underline))
1212 (goto-char (point-min))
1213 (while (search-forward "\b\b__" nil t)
1214 (backward-delete-char 4)
1215 (put-text-property (1- (point)) (point) 'face 'Man-underline))))
1216 (goto-char (point-min))
1217 (while (and (search-forward "_\b" nil t) (not (eobp)))
1218 (backward-delete-char 2)
1219 (put-text-property (point) (1+ (point)) 'face 'Man-underline))
1220 (goto-char (point-min))
1221 (while (search-forward "\b_" nil t)
1222 (backward-delete-char 2)
1223 (put-text-property (1- (point)) (point) 'face 'Man-underline))
1224 (goto-char (point-min))
1225 (while (re-search-forward "\\(.\\)\\(\b+\\1\\)+" nil t)
1226 (replace-match "\\1")
1227 (put-text-property (1- (point)) (point) 'face 'Man-overstrike))
1228 (goto-char (point-min))
1229 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t)
1230 (replace-match "o")
1231 (put-text-property (1- (point)) (point) 'face 'bold))
1232 (goto-char (point-min))
1233 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t)
1234 (replace-match "+")
1235 (put-text-property (1- (point)) (point) 'face 'bold))
1236 ;; When the header is longer than the manpage name, groff tries to
1237 ;; condense it to a shorter line interspersed with ^H. Remove ^H with
1238 ;; their preceding chars (but don't put Man-overstrike). (Bug#5566)
1239 (goto-char (point-min))
1240 (while (re-search-forward ".\b" nil t) (backward-delete-char 2))
1241 (goto-char (point-min))
1242 ;; Try to recognize common forms of cross references.
1243 (Man-highlight-references)
1244 (Man-softhyphen-to-minus)
1245 (goto-char (point-min))
1246 (while (re-search-forward Man-heading-regexp nil t)
1247 (put-text-property (match-beginning 0)
1248 (match-end 0)
1249 'face 'Man-overstrike))))
1251 (defun Man-highlight-references (&optional xref-man-type)
1252 "Highlight the references on mouse-over.
1253 References include items in the SEE ALSO section,
1254 header file (#include <foo.h>), and files in FILES.
1255 If optional argument XREF-MAN-TYPE is non-nil, it used as the
1256 button type for items in SEE ALSO section. If it is nil, the
1257 default type, `Man-xref-man-page' is used for the buttons."
1258 ;; `Man-highlight-references' is used from woman.el, too.
1259 ;; woman.el doesn't set `Man-arguments'.
1260 (unless Man-arguments
1261 (setq Man-arguments ""))
1262 (if (string-match "-k " Man-arguments)
1263 (progn
1264 (Man-highlight-references0 nil Man-reference-regexp 1
1265 'Man-default-man-entry
1266 (or xref-man-type 'Man-xref-man-page))
1267 (Man-highlight-references0 nil Man-apropos-regexp 1
1268 'Man-default-man-entry
1269 (or xref-man-type 'Man-xref-man-page)))
1270 (Man-highlight-references0 Man-see-also-regexp Man-reference-regexp 1
1271 'Man-default-man-entry
1272 (or xref-man-type 'Man-xref-man-page))
1273 (Man-highlight-references0 Man-synopsis-regexp Man-header-regexp 0 2
1274 'Man-xref-header-file)
1275 (Man-highlight-references0 Man-files-regexp Man-normal-file-regexp 0 0
1276 'Man-xref-normal-file)))
1278 (defun Man-highlight-references0 (start-section regexp button-pos target type)
1279 ;; Based on `Man-build-references-alist'
1280 (when (or (null start-section)
1281 (Man-find-section start-section))
1282 (let ((end (if start-section
1283 (progn
1284 (forward-line 1)
1285 (back-to-indentation)
1286 (save-excursion
1287 (Man-next-section 1)
1288 (point)))
1289 (goto-char (point-min))
1290 nil)))
1291 (while (re-search-forward regexp end t)
1292 ;; An overlay button is preferable because the underlying text
1293 ;; may have text property highlights (Bug#7881).
1294 (make-button
1295 (match-beginning button-pos)
1296 (match-end button-pos)
1297 'type type
1298 'Man-target-string (cond
1299 ((numberp target)
1300 (match-string target))
1301 ((functionp target)
1302 target)
1303 (t nil)))))))
1305 (defun Man-cleanup-manpage (&optional interactive)
1306 "Remove overstriking and underlining from the current buffer.
1307 Normally skip any jobs that should have been done by the sed script,
1308 but when called interactively, do those jobs even if the sed
1309 script would have done them."
1310 (interactive "p")
1311 (if (or interactive (not Man-sed-script))
1312 (progn
1313 (goto-char (point-min))
1314 (while (search-forward "_\b" nil t) (backward-delete-char 2))
1315 (goto-char (point-min))
1316 (while (search-forward "\b_" nil t) (backward-delete-char 2))
1317 (goto-char (point-min))
1318 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
1319 (replace-match "\\1"))
1320 (goto-char (point-min))
1321 (while (re-search-forward "\e\\[[0-9]+m" nil t) (replace-match ""))
1322 (goto-char (point-min))
1323 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t) (replace-match "o"))
1325 (goto-char (point-min))
1326 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t) (replace-match "+"))
1327 ;; When the header is longer than the manpage name, groff tries to
1328 ;; condense it to a shorter line interspersed with ^H. Remove ^H with
1329 ;; their preceding chars (but don't put Man-overstrike). (Bug#5566)
1330 (goto-char (point-min))
1331 (while (re-search-forward ".\b" nil t) (backward-delete-char 2))
1332 (Man-softhyphen-to-minus))
1334 (defun Man-bgproc-filter (process string)
1335 "Manpage background process filter.
1336 When manpage command is run asynchronously, PROCESS is the process
1337 object for the manpage command; when manpage command is run
1338 synchronously, PROCESS is the name of the buffer where the manpage
1339 command is run. Second argument STRING is the entire string of output."
1340 (save-excursion
1341 (let ((Man-buffer (process-buffer process)))
1342 (if (null (buffer-name Man-buffer)) ;; deleted buffer
1343 (set-process-buffer process nil)
1345 (with-current-buffer Man-buffer
1346 (let ((inhibit-read-only t)
1347 (beg (marker-position (process-mark process))))
1348 (save-excursion
1349 (goto-char beg)
1350 (insert string)
1351 (save-restriction
1352 (narrow-to-region
1353 (save-excursion
1354 (goto-char beg)
1355 (line-beginning-position))
1356 (point))
1357 (if Man-fontify-manpage-flag
1358 (Man-fontify-manpage)
1359 (Man-cleanup-manpage)))
1360 (set-marker (process-mark process) (point-max)))))))))
1362 (defun Man-bgproc-sentinel (process msg)
1363 "Manpage background process sentinel.
1364 When manpage command is run asynchronously, PROCESS is the process
1365 object for the manpage command; when manpage command is run
1366 synchronously, PROCESS is the name of the buffer where the manpage
1367 command is run. Second argument MSG is the exit message of the
1368 manpage command."
1369 (let ((Man-buffer (if (stringp process) (get-buffer process)
1370 (process-buffer process)))
1371 (delete-buff nil)
1372 (err-mess nil))
1374 (if (null (buffer-name Man-buffer)) ;; deleted buffer
1375 (or (stringp process)
1376 (set-process-buffer process nil))
1378 (with-current-buffer Man-buffer
1379 (save-excursion
1380 (let ((case-fold-search nil))
1381 (goto-char (point-min))
1382 (cond ((or (looking-at "No \\(manual \\)*entry for")
1383 (looking-at "[^\n]*: nothing appropriate$"))
1384 (setq err-mess (buffer-substring (point)
1385 (progn
1386 (end-of-line) (point)))
1387 delete-buff t))
1389 ;; "-k foo", successful exit, but no output (from man-db)
1390 ;; ENHANCE-ME: share the check for -k with
1391 ;; `Man-highlight-references'. The \\s- bits here are
1392 ;; meant to allow for multiple options with -k among them.
1393 ((and (string-match "\\(\\`\\|\\s-\\)-k\\s-" Man-arguments)
1394 (eq (process-status process) 'exit)
1395 (= (process-exit-status process) 0)
1396 (= (point-min) (point-max)))
1397 (setq err-mess (format "%s: no matches" Man-arguments)
1398 delete-buff t))
1400 ((or (stringp process)
1401 (not (and (eq (process-status process) 'exit)
1402 (= (process-exit-status process) 0))))
1403 (or (zerop (length msg))
1404 (progn
1405 (setq err-mess
1406 (concat (buffer-name Man-buffer)
1407 ": process "
1408 (let ((eos (1- (length msg))))
1409 (if (= (aref msg eos) ?\n)
1410 (substring msg 0 eos) msg))))
1411 (goto-char (point-max))
1412 (insert (format "\nprocess %s" msg))))
1414 (if delete-buff
1415 (if (window-live-p (get-buffer-window Man-buffer t))
1416 (quit-restore-window
1417 (get-buffer-window Man-buffer t) 'kill)
1418 (kill-buffer Man-buffer))
1420 (run-hooks 'Man-cooked-hook)
1422 (Man-build-page-list)
1423 (Man-strip-page-headers)
1424 (Man-unindent)
1425 (Man-goto-page 1 t)
1427 (if (not Man-page-list)
1428 (let ((args Man-arguments))
1429 (if (window-live-p (get-buffer-window (current-buffer) t))
1430 (quit-restore-window
1431 (get-buffer-window (current-buffer) t) 'kill)
1432 (kill-buffer (current-buffer)))
1433 ;; Entries hyphenated due to the window's width
1434 ;; won't be found in the man database, so remove
1435 ;; the hyphenation -- assuming Groff hyphenates
1436 ;; either with hyphen-minus (ASCII 45, #x2d),
1437 ;; hyphen (#x2010) or soft hyphen (#xad) -- and
1438 ;; look again.
1439 (if (string-match "[-‐­]" args)
1440 (let ((str (replace-match "" nil nil args)))
1441 (Man-getpage-in-background str))
1442 (message "Can't find the %s manpage"
1443 (Man-page-from-arguments args))))
1445 (if Man-fontify-manpage-flag
1446 (message "%s man page formatted"
1447 (Man-page-from-arguments Man-arguments))
1448 (message "%s man page cleaned up"
1449 (Man-page-from-arguments Man-arguments)))
1450 (unless (and (processp process)
1451 (not (eq (process-status process) 'exit)))
1452 (setq mode-line-process nil))
1453 (set-buffer-modified-p nil)))))
1455 (if err-mess
1456 (message "%s" err-mess))
1457 ))))
1459 (defun Man-page-from-arguments (args)
1460 ;; Skip arguments and only print the page name.
1461 (mapconcat
1462 'identity
1463 (delete nil
1464 (mapcar
1465 (lambda (elem)
1466 (and (not (string-match "^-" elem))
1467 elem))
1468 (split-string args " ")))
1469 " "))
1472 ;; ======================================================================
1473 ;; set up manual mode in buffer and build alists
1475 (defvar bookmark-make-record-function)
1477 (put 'Man-mode 'mode-class 'special)
1479 (define-derived-mode Man-mode fundamental-mode "Man"
1480 "A mode for browsing Un*x manual pages.
1482 The following man commands are available in the buffer. Try
1483 \"\\[describe-key] <key> RET\" for more information:
1485 \\[man] Prompt to retrieve a new manpage.
1486 \\[Man-follow-manual-reference] Retrieve reference in SEE ALSO section.
1487 \\[Man-next-manpage] Jump to next manpage in circular list.
1488 \\[Man-previous-manpage] Jump to previous manpage in circular list.
1489 \\[Man-next-section] Jump to next manpage section.
1490 \\[Man-previous-section] Jump to previous manpage section.
1491 \\[Man-goto-section] Go to a manpage section.
1492 \\[Man-goto-see-also-section] Jumps to the SEE ALSO manpage section.
1493 \\[Man-quit] Deletes the manpage window, bury its buffer.
1494 \\[Man-kill] Deletes the manpage window, kill its buffer.
1495 \\[describe-mode] Prints this help text.
1497 The following variables may be of some use. Try
1498 \"\\[describe-variable] <variable-name> RET\" for more information:
1500 `Man-notify-method' What happens when manpage is ready to display.
1501 `Man-downcase-section-letters-flag' Force section letters to lower case.
1502 `Man-circular-pages-flag' Treat multiple manpage list as circular.
1503 `Man-section-translations-alist' List of section numbers and their Un*x equiv.
1504 `Man-filter-list' Background manpage filter command.
1505 `Man-mode-map' Keymap bindings for Man mode buffers.
1506 `Man-mode-hook' Normal hook run on entry to Man mode.
1507 `Man-section-regexp' Regexp describing manpage section letters.
1508 `Man-heading-regexp' Regexp describing section headers.
1509 `Man-see-also-regexp' Regexp for SEE ALSO section (or your equiv).
1510 `Man-first-heading-regexp' Regexp for first heading on a manpage.
1511 `Man-reference-regexp' Regexp matching a references in SEE ALSO.
1512 `Man-switches' Background `man' command switches.
1514 The following key bindings are currently in effect in the buffer:
1515 \\{Man-mode-map}"
1516 (setq buffer-auto-save-file-name nil
1517 mode-line-buffer-identification
1518 (list (default-value 'mode-line-buffer-identification)
1519 " {" 'Man-page-mode-string "}")
1520 truncate-lines t
1521 buffer-read-only t)
1522 (buffer-disable-undo)
1523 (auto-fill-mode -1)
1524 (setq imenu-generic-expression (list (list nil Man-heading-regexp 0)))
1525 (imenu-add-to-menubar man-imenu-title)
1526 (set (make-local-variable 'outline-regexp) Man-heading-regexp)
1527 (set (make-local-variable 'outline-level) (lambda () 1))
1528 (set (make-local-variable 'bookmark-make-record-function)
1529 'Man-bookmark-make-record))
1531 (defsubst Man-build-section-alist ()
1532 "Build the list of manpage sections."
1533 (setq Man--sections nil)
1534 (goto-char (point-min))
1535 (let ((case-fold-search nil))
1536 (while (re-search-forward Man-heading-regexp (point-max) t)
1537 (let ((section (match-string 1)))
1538 (unless (member section Man--sections)
1539 (push section Man--sections)))
1540 (forward-line 1))))
1542 (defsubst Man-build-references-alist ()
1543 "Build the list of references (in the SEE ALSO section)."
1544 (setq Man--refpages nil)
1545 (save-excursion
1546 (if (Man-find-section Man-see-also-regexp)
1547 (let ((start (progn (forward-line 1) (point)))
1548 (end (progn
1549 (Man-next-section 1)
1550 (point)))
1551 hyphenated
1552 (runningpoint -1))
1553 (save-restriction
1554 (narrow-to-region start end)
1555 (goto-char (point-min))
1556 (back-to-indentation)
1557 (while (and (not (eobp)) (/= (point) runningpoint))
1558 (setq runningpoint (point))
1559 (if (re-search-forward Man-hyphenated-reference-regexp end t)
1560 (let* ((word (match-string 0))
1561 (len (1- (length word))))
1562 (if hyphenated
1563 (setq word (concat hyphenated word)
1564 hyphenated nil
1565 ;; Update len, in case a reference spans
1566 ;; more than two lines (paranoia).
1567 len (1- (length word))))
1568 (if (memq (aref word len) '(?- ?­))
1569 (setq hyphenated (substring word 0 len)))
1570 (and (string-match Man-reference-regexp word)
1571 (not (member word Man--refpages))
1572 (push word Man--refpages))))
1573 (skip-chars-forward " \t\n,"))))))
1574 (setq Man--refpages (nreverse Man--refpages)))
1576 (defun Man-build-page-list ()
1577 "Build the list of separate manpages in the buffer."
1578 (setq Man-page-list nil)
1579 (let ((page-start (point-min))
1580 (page-end (point-max))
1581 (header ""))
1582 (goto-char page-start)
1583 (while (not (eobp))
1584 (setq header
1585 (if (looking-at Man-page-header-regexp)
1586 (match-string 1)
1587 nil))
1588 ;; Go past both the current and the next Man-first-heading-regexp
1589 (if (re-search-forward Man-first-heading-regexp nil 'move 2)
1590 (let ((p (progn (beginning-of-line) (point))))
1591 ;; We assume that the page header is delimited by blank
1592 ;; lines and that it contains at most one blank line. So
1593 ;; if we back by three blank lines we will be sure to be
1594 ;; before the page header but not before the possible
1595 ;; previous page header.
1596 (search-backward "\n\n" nil t 3)
1597 (if (re-search-forward Man-page-header-regexp p 'move)
1598 (beginning-of-line))))
1599 (setq page-end (point))
1600 (setq Man-page-list (append Man-page-list
1601 (list (list (copy-marker page-start)
1602 (copy-marker page-end)
1603 header))))
1604 (setq page-start page-end)
1607 (defun Man-strip-page-headers ()
1608 "Strip all the page headers but the first from the manpage."
1609 (let ((inhibit-read-only t)
1610 (case-fold-search nil)
1611 (header ""))
1612 (dolist (page Man-page-list)
1613 (and (nth 2 page)
1614 (goto-char (car page))
1615 (re-search-forward Man-first-heading-regexp nil t)
1616 (setq header (buffer-substring (car page) (match-beginning 0)))
1617 ;; Since the awk script collapses all successive blank
1618 ;; lines into one, and since we don't want to get rid of
1619 ;; the fast awk script, one must choose between adding
1620 ;; spare blank lines between pages when there were none and
1621 ;; deleting blank lines at page boundaries when there were
1622 ;; some. We choose the first, so we comment the following
1623 ;; line.
1624 ;; (setq header (concat "\n" header)))
1625 (while (search-forward header (nth 1 page) t)
1626 (replace-match ""))))))
1628 (defun Man-unindent ()
1629 "Delete the leading spaces that indent the manpage."
1630 (let ((inhibit-read-only t)
1631 (case-fold-search nil))
1632 (dolist (page Man-page-list)
1633 (let ((indent "")
1634 (nindent 0))
1635 (narrow-to-region (car page) (car (cdr page)))
1636 (if Man-uses-untabify-flag
1637 ;; The space characters inserted by `untabify' inherit
1638 ;; sticky text properties, which is unnecessary and looks
1639 ;; ugly with underlining (Bug#11408).
1640 (let ((text-property-default-nonsticky
1641 (cons '(face . t) text-property-default-nonsticky)))
1642 (untabify (point-min) (point-max))))
1643 (if (catch 'unindent
1644 (goto-char (point-min))
1645 (if (not (re-search-forward Man-first-heading-regexp nil t))
1646 (throw 'unindent nil))
1647 (beginning-of-line)
1648 (setq indent (buffer-substring (point)
1649 (progn
1650 (skip-chars-forward " ")
1651 (point))))
1652 (setq nindent (length indent))
1653 (if (zerop nindent)
1654 (throw 'unindent nil))
1655 (setq indent (concat indent "\\|$"))
1656 (goto-char (point-min))
1657 (while (not (eobp))
1658 (if (looking-at indent)
1659 (forward-line 1)
1660 (throw 'unindent nil)))
1661 (goto-char (point-min)))
1662 (while (not (eobp))
1663 (or (eolp)
1664 (delete-char nindent))
1665 (forward-line 1)))
1666 ))))
1669 ;; ======================================================================
1670 ;; Man mode commands
1672 (defun Man-next-section (n)
1673 "Move point to Nth next section (default 1)."
1674 (interactive "p")
1675 (let ((case-fold-search nil)
1676 (start (point)))
1677 (if (looking-at Man-heading-regexp)
1678 (forward-line 1))
1679 (if (re-search-forward Man-heading-regexp (point-max) t n)
1680 (beginning-of-line)
1681 (goto-char (point-max))
1682 ;; The last line doesn't belong to any section.
1683 (forward-line -1))
1684 ;; But don't move back from the starting point (can happen if `start'
1685 ;; is somewhere on the last line).
1686 (if (< (point) start) (goto-char start))))
1688 (defun Man-previous-section (n)
1689 "Move point to Nth previous section (default 1)."
1690 (interactive "p")
1691 (let ((case-fold-search nil))
1692 (if (looking-at Man-heading-regexp)
1693 (forward-line -1))
1694 (if (re-search-backward Man-heading-regexp (point-min) t n)
1695 (beginning-of-line)
1696 (goto-char (point-min)))))
1698 (defun Man-find-section (section)
1699 "Move point to SECTION if it exists, otherwise don't move point.
1700 Returns t if section is found, nil otherwise."
1701 (let ((curpos (point))
1702 (case-fold-search nil))
1703 (goto-char (point-min))
1704 (if (re-search-forward (concat "^" section) (point-max) t)
1705 (progn (beginning-of-line) t)
1706 (goto-char curpos)
1707 nil)
1710 (defvar Man--last-section nil)
1712 (defun Man-goto-section (section)
1713 "Move point to SECTION."
1714 (interactive
1715 (let* ((default (if (member Man--last-section Man--sections)
1716 Man--last-section
1717 (car Man--sections)))
1718 (completion-ignore-case t)
1719 (prompt (concat "Go to section (default " default "): "))
1720 (chosen (completing-read prompt Man--sections
1721 nil nil nil nil default)))
1722 (list chosen)))
1723 (setq Man--last-section section)
1724 (unless (Man-find-section section)
1725 (error "Section %s not found" section)))
1728 (defun Man-goto-see-also-section ()
1729 "Move point to the \"SEE ALSO\" section.
1730 Actually the section moved to is described by `Man-see-also-regexp'."
1731 (interactive)
1732 (if (not (Man-find-section Man-see-also-regexp))
1733 (error "%s" (concat "No " Man-see-also-regexp
1734 " section found in the current manpage"))))
1736 (defun Man-possibly-hyphenated-word ()
1737 "Return a possibly hyphenated word at point.
1738 If the word starts at the first non-whitespace column, and the
1739 previous line ends with a hyphen, return the last word on the previous
1740 line instead. Thus, if a reference to \"tcgetpgrp(3V)\" is hyphenated
1741 as \"tcgetp-grp(3V)\", and point is at \"grp(3V)\", we return
1742 \"tcgetp-\" instead of \"grp\"."
1743 (save-excursion
1744 (skip-syntax-backward "w()")
1745 (skip-chars-forward " \t")
1746 (let ((beg (point))
1747 (word (current-word)))
1748 (when (eq beg (save-excursion
1749 (back-to-indentation)
1750 (point)))
1751 (end-of-line 0)
1752 (if (eq (char-before) ?-)
1753 (setq word (current-word))))
1754 word)))
1756 (defvar Man--last-refpage nil)
1758 (defun Man-follow-manual-reference (reference)
1759 "Get one of the manpages referred to in the \"SEE ALSO\" section.
1760 Specify which REFERENCE to use; default is based on word at point."
1761 (interactive
1762 (if (not Man--refpages)
1763 (error "There are no references in the current man page")
1764 (list
1765 (let* ((default (or
1766 (car (all-completions
1767 (let ((word
1768 (or (Man-possibly-hyphenated-word)
1769 "")))
1770 ;; strip a trailing '-':
1771 (if (string-match "-$" word)
1772 (substring word 0
1773 (match-beginning 0))
1774 word))
1775 Man--refpages))
1776 (if (member Man--last-refpage Man--refpages)
1777 Man--last-refpage
1778 (car Man--refpages))))
1779 (defaults
1780 (mapcar 'substring-no-properties
1781 (cons default Man--refpages)))
1782 (prompt (concat "Refer to (default " default "): "))
1783 (chosen (completing-read prompt Man--refpages
1784 nil nil nil nil defaults)))
1785 chosen))))
1786 (if (not Man--refpages)
1787 (error "Can't find any references in the current manpage")
1788 (setq Man--last-refpage reference)
1789 (Man-getpage-in-background
1790 (Man-translate-references reference))))
1792 (defun Man-kill ()
1793 "Kill the buffer containing the manpage."
1794 (interactive)
1795 (quit-window t))
1797 (defun Man-quit ()
1798 "Bury the buffer containing the manpage."
1799 (interactive)
1800 (quit-window))
1802 (defun Man-goto-page (page &optional noerror)
1803 "Go to the manual page on page PAGE."
1804 (interactive
1805 (if (not Man-page-list)
1806 (error "Not a man page buffer")
1807 (if (= (length Man-page-list) 1)
1808 (error "You're looking at the only manpage in the buffer")
1809 (list (read-minibuffer (format "Go to manpage [1-%d]: "
1810 (length Man-page-list)))))))
1811 (if (and (not Man-page-list) (not noerror))
1812 (error "Not a man page buffer"))
1813 (when Man-page-list
1814 (if (or (< page 1)
1815 (> page (length Man-page-list)))
1816 (user-error "No manpage %d found" page))
1817 (let* ((page-range (nth (1- page) Man-page-list))
1818 (page-start (car page-range))
1819 (page-end (car (cdr page-range))))
1820 (setq Man-current-page page
1821 Man-page-mode-string (Man-make-page-mode-string))
1822 (widen)
1823 (goto-char page-start)
1824 (narrow-to-region page-start page-end)
1825 (Man-build-section-alist)
1826 (Man-build-references-alist)
1827 (goto-char (point-min)))))
1830 (defun Man-next-manpage ()
1831 "Find the next 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 (length Man-page-list))
1836 (Man-goto-page (1+ Man-current-page))
1837 (if Man-circular-pages-flag
1838 (Man-goto-page 1)
1839 (error "You're looking at the last manpage in the buffer"))))
1841 (defun Man-previous-manpage ()
1842 "Find the previous manpage entry in the buffer."
1843 (interactive)
1844 (if (= (length Man-page-list) 1)
1845 (error "This is the only manpage in the buffer"))
1846 (if (> Man-current-page 1)
1847 (Man-goto-page (1- Man-current-page))
1848 (if Man-circular-pages-flag
1849 (Man-goto-page (length Man-page-list))
1850 (error "You're looking at the first manpage in the buffer"))))
1852 ;; Header file support
1853 (defun Man-view-header-file (file)
1854 "View a header file specified by FILE from `Man-header-file-path'."
1855 (let ((path Man-header-file-path)
1856 complete-path)
1857 (while path
1858 (setq complete-path (expand-file-name file (car path))
1859 path (cdr path))
1860 (if (file-readable-p complete-path)
1861 (progn (view-file complete-path)
1862 (setq path nil))
1863 (setq complete-path nil)))
1864 complete-path))
1866 ;;; Bookmark Man Support
1867 (declare-function bookmark-make-record-default
1868 "bookmark" (&optional no-file no-context posn))
1869 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
1870 (declare-function bookmark-default-handler "bookmark" (bmk))
1871 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
1873 (defun Man-default-bookmark-title ()
1874 "Default bookmark name for Man or WoMan pages.
1875 Uses `Man-name-local-regexp'."
1876 (save-excursion
1877 (goto-char (point-min))
1878 (when (re-search-forward Man-name-local-regexp nil t)
1879 (skip-chars-forward "\n\t ")
1880 (buffer-substring-no-properties (point) (line-end-position)))))
1882 (defun Man-bookmark-make-record ()
1883 "Make a bookmark entry for a Man buffer."
1884 `(,(Man-default-bookmark-title)
1885 ,@(bookmark-make-record-default 'no-file)
1886 (location . ,(concat "man " Man-arguments))
1887 (man-args . ,Man-arguments)
1888 (handler . Man-bookmark-jump)))
1890 ;;;###autoload
1891 (defun Man-bookmark-jump (bookmark)
1892 "Default bookmark handler for Man buffers."
1893 (let* ((man-args (bookmark-prop-get bookmark 'man-args))
1894 ;; Let bookmark.el do the window handling.
1895 ;; This let-binding needs to be active during the call to both
1896 ;; Man-getpage-in-background and accept-process-output.
1897 (Man-notify-method 'meek)
1898 (buf (Man-getpage-in-background man-args))
1899 (proc (get-buffer-process buf)))
1900 (while (and proc (eq (process-status proc) 'run))
1901 (accept-process-output proc))
1902 (bookmark-default-handler
1903 `("" (buffer . ,buf) . ,(bookmark-get-bookmark-record bookmark)))))
1906 ;; Init the man package variables, if not already done.
1907 (Man-init-defvars)
1909 (provide 'man)
1911 ;;; man.el ends here