man.el 65 KB

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