man.el 65 KB

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