org-e-man.el 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. ;; org-e-man.el --- Man Back-End For Org Export Engine
  2. ;; Copyright (C) 2011-2012 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Author: Luis R Anaya <papoanaya aroba hot mail punto com>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;;
  7. ;; This program is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; This program is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;
  19. ;; This library implements a Man back-end for Org generic exporter.
  20. ;;
  21. ;; To test it, run
  22. ;;
  23. ;; M-: (org-export-to-buffer 'e-man "*Test e-Man*") RET
  24. ;;
  25. ;; in an org-mode buffer then switch to the buffer to see the Man
  26. ;; export. See contrib/lisp/org-export.el for more details on how
  27. ;; this exporter works.
  28. ;;
  29. ;; It introduces one new buffer keywords:
  30. ;; "MAN_CLASS_OPTIONS".
  31. ;;; Code:
  32. (require 'org-export)
  33. (eval-when-compile (require 'cl))
  34. (defvar org-export-man-default-packages-alist)
  35. (defvar org-export-man-packages-alist)
  36. ;;; Define Back-End
  37. (defvar org-e-man-translate-alist
  38. '((babel-call . org-e-man-babel-call)
  39. (bold . org-e-man-bold)
  40. (center-block . org-e-man-center-block)
  41. (clock . org-e-man-clock)
  42. (code . org-e-man-code)
  43. (comment . org-e-man-comment)
  44. (comment-block . org-e-man-comment-block)
  45. (drawer . org-e-man-drawer)
  46. (dynamic-block . org-e-man-dynamic-block)
  47. (entity . org-e-man-entity)
  48. (example-block . org-e-man-example-block)
  49. (export-block . org-e-man-export-block)
  50. (export-snippet . org-e-man-export-snippet)
  51. (fixed-width . org-e-man-fixed-width)
  52. (footnote-definition . org-e-man-footnote-definition)
  53. (footnote-reference . org-e-man-footnote-reference)
  54. (headline . org-e-man-headline)
  55. (horizontal-rule . org-e-man-horizontal-rule)
  56. (inline-babel-call . org-e-man-inline-babel-call)
  57. (inline-src-block . org-e-man-inline-src-block)
  58. (inlinetask . org-e-man-inlinetask)
  59. (italic . org-e-man-italic)
  60. (item . org-e-man-item)
  61. (keyword . org-e-man-keyword)
  62. (man-environment . org-e-man-man-environment)
  63. (man-fragment . org-e-man-man-fragment)
  64. (line-break . org-e-man-line-break)
  65. (link . org-e-man-link)
  66. (paragraph . org-e-man-paragraph)
  67. (plain-list . org-e-man-plain-list)
  68. (plain-text . org-e-man-plain-text)
  69. (planning . org-e-man-planning)
  70. (property-drawer . org-e-man-property-drawer)
  71. (quote-block . org-e-man-quote-block)
  72. (quote-section . org-e-man-quote-section)
  73. (radio-target . org-e-man-radio-target)
  74. (section . org-e-man-section)
  75. (special-block . org-e-man-special-block)
  76. (src-block . org-e-man-src-block)
  77. (statistics-cookie . org-e-man-statistics-cookie)
  78. (strike-through . org-e-man-strike-through)
  79. (subscript . org-e-man-subscript)
  80. (superscript . org-e-man-superscript)
  81. (table . org-e-man-table)
  82. (table-cell . org-e-man-table-cell)
  83. (table-row . org-e-man-table-row)
  84. (target . org-e-man-target)
  85. (template . org-e-man-template)
  86. (timestamp . org-e-man-timestamp)
  87. (underline . org-e-man-underline)
  88. (verbatim . org-e-man-verbatim)
  89. (verse-block . org-e-man-verse-block))
  90. "Alist between element or object types and translators.")
  91. (defconst org-e-man-options-alist
  92. '((:date "DATE" nil nil t)
  93. (:man-class "MAN_CLASS" nil nil t)
  94. (:man-class-options "MAN_CLASS_OPTIONS" nil nil t)
  95. (:man-header-extra "MAN_HEADER" nil nil newline))
  96. "Alist between Man export properties and ways to set them.
  97. See `org-export-options-alist' for more information on the
  98. structure of the values.")
  99. ;;; User Configurable Variables
  100. (defgroup org-export-e-man nil
  101. "Options for exporting Org mode files to Man."
  102. :tag "Org Export Man"
  103. :group 'org-export)
  104. ;;; Tables
  105. (defcustom org-e-man-tables-centered t
  106. "When non-nil, tables are exported in a center environment."
  107. :group 'org-export-e-man
  108. :type 'boolean)
  109. (defcustom org-e-man-tables-verbatim nil
  110. "When non-nil, tables are exported verbatim."
  111. :group 'org-export-e-man
  112. :type 'boolean)
  113. (defcustom org-e-man-table-scientific-notation "%sE%s"
  114. "Format string to display numbers in scientific notation.
  115. The format should have \"%s\" twice, for mantissa and exponent
  116. \(i.e. \"%s\\\\times10^{%s}\").
  117. When nil, no transformation is made."
  118. :group 'org-export-e-man
  119. :type '(choice
  120. (string :tag "Format string")
  121. (const :tag "No formatting")))
  122. ;;; Inlinetasks
  123. ;; Src blocks
  124. (defcustom org-e-man-source-highlight nil
  125. "Use GNU source highlight to embellish source blocks "
  126. :group 'org-export-e-man
  127. :type 'boolean)
  128. (defcustom org-e-man-source-highlight-langs
  129. '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
  130. (scheme "scheme")
  131. (c "c") (cc "cpp") (csharp "csharp") (d "d")
  132. (fortran "fortran") (cobol "cobol") (pascal "pascal")
  133. (ada "ada") (asm "asm")
  134. (perl "perl") (cperl "perl")
  135. (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
  136. (java "java") (javascript "javascript")
  137. (tex "latex")
  138. (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
  139. (ocaml "caml") (caml "caml")
  140. (sql "sql") (sqlite "sql")
  141. (html "html") (css "css") (xml "xml")
  142. (bat "bat") (bison "bison") (clipper "clipper")
  143. (ldap "ldap") (opa "opa")
  144. (php "php") (postscript "postscript") (prolog "prolog")
  145. (properties "properties") (makefile "makefile")
  146. (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
  147. "Alist mapping languages to their listing language counterpart.
  148. The key is a symbol, the major mode symbol without the \"-mode\".
  149. The value is the string that should be inserted as the language
  150. parameter for the listings package. If the mode name and the
  151. listings name are the same, the language does not need an entry
  152. in this list - but it does not hurt if it is present."
  153. :group 'org-export-e-man
  154. :type '(repeat
  155. (list
  156. (symbol :tag "Major mode ")
  157. (string :tag "Listings language"))))
  158. (defvar org-e-man-custom-lang-environments nil
  159. "Alist mapping languages to language-specific Man environments.
  160. It is used during export of src blocks by the listings and
  161. man packages. For example,
  162. \(setq org-e-man-custom-lang-environments
  163. '\(\(python \"pythoncode\"\)\)\)
  164. would have the effect that if org encounters begin_src python
  165. during man export."
  166. )
  167. ;;; Plain text
  168. (defcustom org-e-man-quotes
  169. '(("fr"
  170. ("\\(\\s-\\|[[(]\\|^\\)\"" . "«~")
  171. ("\\(\\S-\\)\"" . "~»")
  172. ("\\(\\s-\\|(\\|^\\)'" . "'"))
  173. ("en"
  174. ("\\(\\s-\\|[[(]\\|^\\)\"" . "``")
  175. ("\\(\\S-\\)\"" . "''")
  176. ("\\(\\s-\\|(\\|^\\)'" . "`")))
  177. "Alist for quotes to use when converting english double-quotes.
  178. The CAR of each item in this alist is the language code.
  179. The CDR of each item in this alist is a list of three CONS:
  180. - the first CONS defines the opening quote;
  181. - the second CONS defines the closing quote;
  182. - the last CONS defines single quotes.
  183. For each item in a CONS, the first string is a regexp
  184. for allowed characters before/after the quote, the second
  185. string defines the replacement string for this quote."
  186. :group 'org-export-e-man
  187. :type '(list
  188. (cons :tag "Opening quote"
  189. (string :tag "Regexp for char before")
  190. (string :tag "Replacement quote "))
  191. (cons :tag "Closing quote"
  192. (string :tag "Regexp for char after ")
  193. (string :tag "Replacement quote "))
  194. (cons :tag "Single quote"
  195. (string :tag "Regexp for char before")
  196. (string :tag "Replacement quote "))))
  197. ;;; Compilation
  198. (defcustom org-e-man-pdf-process
  199. '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
  200. "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
  201. "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
  202. "Commands to process a Man file to a PDF file.
  203. This is a list of strings, each of them will be given to the
  204. shell as a command. %f in the command will be replaced by the
  205. full file name, %b by the file base name \(i.e. without
  206. extension) and %o by the base directory of the file.
  207. By default, Org uses 3 runs of to do the processing.
  208. Alternatively, this may be a Lisp function that does the
  209. processing. This function should accept the file name as
  210. its single argument."
  211. :group 'org-export-pdf
  212. :type '(choice
  213. (repeat :tag "Shell command sequence"
  214. (string :tag "Shell command"))
  215. (const :tag "2 runs of pdfgroff"
  216. ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
  217. "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf" ))
  218. (const :tag "3 runs of pdfgroff"
  219. ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
  220. "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
  221. "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
  222. (function)))
  223. (defcustom org-e-man-logfiles-extensions
  224. '("log" "out" "toc")
  225. "The list of file extensions to consider as Man logfiles."
  226. :group 'org-export-e-man
  227. :type '(repeat (string :tag "Extension")))
  228. (defcustom org-e-man-remove-logfiles t
  229. "Non-nil means remove the logfiles produced by PDF production.
  230. These are the .aux, .log, .out, and .toc files."
  231. :group 'org-export-e-man
  232. :type 'boolean)
  233. ;; Preamble
  234. ;; Adding MAN as a block parser to make sure that its contents
  235. ;; does not execute
  236. (add-to-list 'org-element-block-name-alist
  237. '("MAN" . org-element-export-block-parser))
  238. ;;; Internal Functions
  239. (defun org-e-man--caption/label-string (caption label info)
  240. "Return caption and label Man string for floats.
  241. CAPTION is a cons cell of secondary strings, the car being the
  242. standard caption and the cdr its short form. LABEL is a string
  243. representing the label. INFO is a plist holding contextual
  244. information.
  245. If there's no caption nor label, return the empty string.
  246. For non-floats, see `org-e-man--wrap-label'."
  247. (let ((label-str "" ))
  248. (cond
  249. ((and (not caption) (not label)) "")
  250. ((not caption) (format "\\fI%s\\fP" label))
  251. ;; Option caption format with short name.
  252. ((cdr caption)
  253. (format "\\fR%s\\fP - \\fI%s\\P - %s\n"
  254. (org-export-data (cdr caption) info)
  255. label-str
  256. (org-export-data (car caption) info)))
  257. ;; Standard caption format.
  258. (t (format "\\fR%s\\fP"
  259. (org-export-data (car caption) info))))))
  260. (defun org-e-man--quotation-marks (text info)
  261. "Export quotation marks depending on language conventions.
  262. TEXT is a string containing quotation marks to be replaced. INFO
  263. is a plist used as a communication channel."
  264. (mapc (lambda(l)
  265. (let ((start 0))
  266. (while (setq start (string-match (car l) text start))
  267. (let ((new-quote (concat (match-string 1 text) (cdr l))))
  268. (setq text (replace-match new-quote t t text))))))
  269. (cdr (or (assoc (plist-get info :language) org-e-man-quotes)
  270. ;; Falls back on English.
  271. (assoc "en" org-e-man-quotes)))) text)
  272. (defun org-e-man--wrap-label (element output)
  273. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  274. This function shouldn't be used for floats. See
  275. `org-e-man--caption/label-string'."
  276. (let ((label (org-element-property :name element)))
  277. (if (or (not output) (not label) (string= output "") (string= label ""))
  278. output
  279. (concat (format "%s\n.br\n" label) output))))
  280. ;;; Template
  281. (defun org-e-man-template (contents info)
  282. "Return complete document string after Man conversion.
  283. CONTENTS is the transcoded contents string. INFO is a plist
  284. holding export options."
  285. (let* ((title (org-export-data (plist-get info :title) info))
  286. (attr (read (format "(%s)"
  287. (mapconcat
  288. #'identity
  289. (list (plist-get info :man-class-options))
  290. " "))))
  291. (section-item (plist-get attr :section-id)))
  292. (concat
  293. (cond
  294. ((and title (stringp section-item))
  295. (format ".TH \"%s\" \"%s\" \n" title section-item))
  296. ((and (string= "" title) (stringp section-item))
  297. (format ".TH \"%s\" \"%s\" \n" " " section-item))
  298. (title
  299. (format ".TH \"%s\" \"1\" \n" title))
  300. (t
  301. ".TH \" \" \"1\" "))
  302. contents)))
  303. ;;; Transcode Functions
  304. ;;; Babel Call
  305. ;;
  306. ;; Babel Calls are ignored.
  307. ;;; Bold
  308. (defun org-e-man-bold (bold contents info)
  309. "Transcode BOLD from Org to Man.
  310. CONTENTS is the text with bold markup. INFO is a plist holding
  311. contextual information."
  312. (format "\\fB%s\\fP" contents))
  313. ;;; Center Block
  314. (defun org-e-man-center-block (center-block contents info)
  315. "Transcode a CENTER-BLOCK element from Org to Man.
  316. CONTENTS holds the contents of the center block. INFO is a plist
  317. holding contextual information."
  318. (org-e-man--wrap-label
  319. center-block
  320. (format ".ce %d\n.nf\n%s\n.fi"
  321. (- (length (split-string contents "\n")) 1 )
  322. contents)))
  323. ;;; Clock
  324. (defun org-e-man-clock (clock contents info)
  325. "Transcode a CLOCK element from Org to Man.
  326. CONTENTS is nil. INFO is a plist holding contextual
  327. information."
  328. "" )
  329. ;;; Code
  330. (defun org-e-man-code (code contents info)
  331. "Transcode a CODE object from Org to Man.
  332. CONTENTS is nil. INFO is a plist used as a communication
  333. channel."
  334. (format "\\fC%s\\fP" code))
  335. ;;; Comment
  336. ;;
  337. ;; Comments are ignored.
  338. ;;; Comment Block
  339. ;;
  340. ;; Comment Blocks are ignored.
  341. ;;; Drawer
  342. (defun org-e-man-drawer (drawer contents info)
  343. "Transcode a DRAWER element from Org to Man.
  344. DRAWER holds the drawer information
  345. CONTENTS holds the contents of the block.
  346. INFO is a plist holding contextual information. "
  347. contents)
  348. ;;; Dynamic Block
  349. (defun org-e-man-dynamic-block (dynamic-block contents info)
  350. "Transcode a DYNAMIC-BLOCK element from Org to Man.
  351. CONTENTS holds the contents of the block. INFO is a plist
  352. holding contextual information. See `org-export-data'."
  353. (org-e-man--wrap-label dynamic-block contents))
  354. ;;; Entity
  355. (defun org-e-man-entity (entity contents info)
  356. "Transcode an ENTITY object from Org to Man.
  357. CONTENTS are the definition itself. INFO is a plist holding
  358. contextual information."
  359. (let ((ent (org-element-property :utf8 entity))) ent))
  360. ;;; Example Block
  361. (defun org-e-man-example-block (example-block contents info)
  362. "Transcode an EXAMPLE-BLOCK element from Org to Man.
  363. CONTENTS is nil. INFO is a plist holding contextual
  364. information."
  365. (org-e-man--wrap-label
  366. example-block
  367. (format ".RS\n.nf\n%s\n.fi\n.RE"
  368. (org-export-format-code-default example-block info))))
  369. ;;; Export Block
  370. (defun org-e-man-export-block (export-block contents info)
  371. "Transcode a EXPORT-BLOCK element from Org to Man.
  372. CONTENTS is nil. INFO is a plist holding contextual information."
  373. (when (string= (org-element-property :type export-block) "MAN")
  374. (org-remove-indentation (org-element-property :value export-block))))
  375. ;;; Export Snippet
  376. (defun org-e-man-export-snippet (export-snippet contents info)
  377. "Transcode a EXPORT-SNIPPET object from Org to Man.
  378. CONTENTS is nil. INFO is a plist holding contextual information."
  379. (when (eq (org-export-snippet-backend export-snippet) 'e-man)
  380. (org-element-property :value export-snippet)))
  381. ;;; Fixed Width
  382. (defun org-e-man-fixed-width (fixed-width contents info)
  383. "Transcode a FIXED-WIDTH element from Org to Man.
  384. CONTENTS is nil. INFO is a plist holding contextual information."
  385. (org-e-man--wrap-label
  386. fixed-width
  387. (format "\\fC\n%s\\fP"
  388. (org-remove-indentation
  389. (org-element-property :value fixed-width)))))
  390. ;;; Footnote Definition
  391. ;;
  392. ;; Footnote Definitions are ignored.
  393. ;;; Footnote References
  394. ;;
  395. ;; Footnote References are Ignored
  396. ;;; Headline
  397. (defun org-e-man-headline (headline contents info)
  398. "Transcode an HEADLINE element from Org to Man.
  399. CONTENTS holds the contents of the headline. INFO is a plist
  400. holding contextual information."
  401. (let* ((level (org-export-get-relative-level headline info))
  402. (numberedp (org-export-numbered-headline-p headline info))
  403. ;; Section formatting will set two placeholders: one for the
  404. ;; title and the other for the contents.
  405. (section-fmt
  406. (case level
  407. (1 ".SH \"%s\"\n%s")
  408. (2 ".SS \"%s\"\n%s")
  409. (3 ".SS \"%s\"\n%s")
  410. (t nil)))
  411. (text (org-export-data (org-element-property :title headline) info)))
  412. (cond
  413. ;; Case 1: This is a footnote section: ignore it.
  414. ((org-element-property :footnote-section-p headline) nil)
  415. ;; Case 2. This is a deep sub-tree: export it as a list item.
  416. ;; Also export as items headlines for which no section
  417. ;; format has been found.
  418. ((or (not section-fmt) (org-export-low-level-p headline info))
  419. ;; Build the real contents of the sub-tree.
  420. (let ((low-level-body
  421. (concat
  422. ;; If the headline is the first sibling, start a list.
  423. (when (org-export-first-sibling-p headline info)
  424. (format "%s\n" ".RS"))
  425. ;; Itemize headline
  426. ".TP\n.ft I\n" text "\n.ft\n"
  427. contents ".RE")))
  428. ;; If headline is not the last sibling simply return
  429. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
  430. ;; blank line.
  431. (if (not (org-export-last-sibling-p headline info)) low-level-body
  432. (replace-regexp-in-string
  433. "[ \t\n]*\\'" ""
  434. low-level-body))))
  435. ;; Case 3. Standard headline. Export it as a section.
  436. (t (format section-fmt text contents )))))
  437. ;;; Horizontal Rule
  438. ;; Not supported
  439. ;;; Inline Babel Call
  440. ;;
  441. ;; Inline Babel Calls are ignored.
  442. ;;; Inline Src Block
  443. (defun org-e-man-inline-src-block (inline-src-block contents info)
  444. "Transcode an INLINE-SRC-BLOCK element from Org to Man.
  445. CONTENTS holds the contents of the item. INFO is a plist holding
  446. contextual information."
  447. (let* ((code (org-element-property :value inline-src-block)))
  448. (cond
  449. (org-e-man-source-highlight
  450. (let* ((tmpdir (if (featurep 'xemacs)
  451. temp-directory
  452. temporary-file-directory ))
  453. (in-file (make-temp-name
  454. (expand-file-name "srchilite" tmpdir)))
  455. (out-file (make-temp-name
  456. (expand-file-name "reshilite" tmpdir)))
  457. (org-lang (org-element-property :language inline-src-block))
  458. (lst-lang (cadr (assq (intern org-lang)
  459. org-e-man-source-highlight-langs)))
  460. (cmd (concat (expand-file-name "source-highlight")
  461. " -s " lst-lang
  462. " -f groff_man"
  463. " -i " in-file
  464. " -o " out-file )))
  465. (if lst-lang
  466. (let ((code-block "" ))
  467. (with-temp-file in-file (insert code))
  468. (shell-command cmd)
  469. (setq code-block (org-file-contents out-file))
  470. (delete-file in-file)
  471. (delete-file out-file)
  472. code-block)
  473. (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
  474. code))))
  475. ;; Do not use a special package: transcode it verbatim.
  476. (t
  477. (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
  478. "\\fP\n.fi\n.RE\n")))))
  479. ;;; Inlinetask
  480. ;;; Italic
  481. (defun org-e-man-italic (italic contents info)
  482. "Transcode ITALIC from Org to Man.
  483. CONTENTS is the text with italic markup. INFO is a plist holding
  484. contextual information."
  485. (format "\\fI%s\\fP" contents))
  486. ;;; Item
  487. (defun org-e-man-item (item contents info)
  488. "Transcode an ITEM element from Org to Man.
  489. CONTENTS holds the contents of the item. INFO is a plist holding
  490. contextual information."
  491. (let* ((bullet (org-element-property :bullet item))
  492. (type (org-element-property :type (org-element-property :parent item)))
  493. (checkbox (case (org-element-property :checkbox item)
  494. (on "\\o'\\(sq\\(mu'") ;;
  495. (off "\\(sq ") ;;
  496. (trans "\\o'\\(sq\\(mi'" ))) ;;
  497. (tag (let ((tag (org-element-property :tag item)))
  498. ;; Check-boxes must belong to the tag.
  499. (and tag (format "\\fB%s\\fP"
  500. (concat checkbox
  501. (org-export-data tag info)))))))
  502. (if (and (null tag )
  503. (null checkbox))
  504. (let* ((bullet (org-trim bullet))
  505. (marker (cond ((string= "-" bullet) "\\(em")
  506. ((string= "*" bullet) "\\(bu")
  507. ((eq type 'ordered)
  508. (format "%s " (org-trim bullet)))
  509. (t "\\(dg"))))
  510. (concat ".IP " marker " 4\n"
  511. (org-trim (or contents " " ))))
  512. ; else
  513. (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
  514. (org-trim (or contents " " ))))))
  515. ;;; Keyword
  516. (defun org-e-man-keyword (keyword contents info)
  517. "Transcode a KEYWORD element from Org to Man.
  518. CONTENTS is nil. INFO is a plist holding contextual information."
  519. (let ((key (org-element-property :key keyword))
  520. (value (org-element-property :value keyword)))
  521. (cond
  522. ((string= key "MAN") value)
  523. ((string= key "INDEX") nil)
  524. ;; Invisible targets.
  525. ((string= key "TARGET") nil)
  526. ((string= key "TOC" ) nil))))
  527. ;;; Man Environment
  528. (defun org-e-man-man-environment (man-environment contents info)
  529. "Transcode a MAN-ENVIRONMENT element from Org to Man.
  530. CONTENTS is nil. INFO is a plist holding contextual information."
  531. (let ((label (org-element-property :name man-environment))
  532. (value (org-remove-indentation
  533. (org-element-property :value man-environment))))
  534. (if (not (org-string-nw-p label)) value
  535. ;; Environment is labelled: label must be within the environment
  536. ;; (otherwise, a reference pointing to that element will count
  537. ;; the section instead).
  538. (with-temp-buffer
  539. (insert value)
  540. (goto-char (point-min))
  541. (forward-line)
  542. (insert (format "%s\n" label))
  543. (buffer-string)))))
  544. ;;; Man Fragment
  545. (defun org-e-man-man-fragment (man-fragment contents info)
  546. "Transcode a MAN-FRAGMENT object from Org to Man.
  547. CONTENTS is nil. INFO is a plist holding contextual information."
  548. (org-element-property :value man-fragment))
  549. ;;; Line Break
  550. (defun org-e-man-line-break (line-break contents info)
  551. "Transcode a LINE-BREAK object from Org to Man.
  552. CONTENTS is nil. INFO is a plist holding contextual information."
  553. ".br\n")
  554. ;;; Link
  555. (defun org-e-man-link (link desc info)
  556. "Transcode a LINK object from Org to Man.
  557. DESC is the description part of the link, or the empty string.
  558. INFO is a plist holding contextual information. See
  559. `org-export-data'."
  560. (let* ((type (org-element-property :type link))
  561. (raw-path (org-element-property :path link))
  562. ;; Ensure DESC really exists, or set it to nil.
  563. (desc (and (not (string= desc "")) desc))
  564. (path (cond
  565. ((member type '("http" "https" "ftp" "mailto"))
  566. (concat type ":" raw-path))
  567. ((string= type "file")
  568. (when (string-match "\\(.+\\)::.+" raw-path)
  569. (setq raw-path (match-string 1 raw-path)))
  570. (if (file-name-absolute-p raw-path)
  571. (concat "file://" (expand-file-name raw-path))
  572. (concat "file://" raw-path)))
  573. (t raw-path)))
  574. protocol)
  575. (cond
  576. ;; External link with a description part.
  577. ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
  578. ;; External link without a description part.
  579. (path (format "\\fI%s\\fP" path))
  580. ;; No path, only description. Try to do something useful.
  581. (t (format "\\fI%s\\fP" desc)))))
  582. ;;; Paragraph
  583. (defun org-e-man-paragraph (paragraph contents info)
  584. "Transcode a PARAGRAPH element from Org to Man.
  585. CONTENTS is the contents of the paragraph, as a string. INFO is
  586. the plist used as a communication channel."
  587. (let ((parent (plist-get (nth 1 paragraph) :parent)))
  588. (when parent
  589. (let ((parent-type (car parent))
  590. (fixed-paragraph ""))
  591. (cond ((and (eq parent-type 'item)
  592. (plist-get (nth 1 parent) :bullet ))
  593. (setq fixed-paragraph (concat "" contents)))
  594. ((eq parent-type 'section)
  595. (setq fixed-paragraph (concat ".PP\n" contents)))
  596. ((eq parent-type 'footnote-definition)
  597. (setq fixed-paragraph contents))
  598. (t (setq fixed-paragraph (concat "" contents))))
  599. fixed-paragraph ))))
  600. ;;; Plain List
  601. (defun org-e-man-plain-list (plain-list contents info)
  602. "Transcode a PLAIN-LIST element from Org to Man.
  603. CONTENTS is the contents of the list. INFO is a plist holding
  604. contextual information."
  605. contents)
  606. ;;; Plain Text
  607. (defun org-e-man-plain-text (text info)
  608. "Transcode a TEXT string from Org to Man.
  609. TEXT is the string to transcode. INFO is a plist holding
  610. contextual information."
  611. ;; Protect
  612. (setq text (replace-regexp-in-string
  613. "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
  614. "$\\" text nil t 1))
  615. ;; Handle quotation marks
  616. (setq text (org-e-man--quotation-marks text info))
  617. ;; Handle break preservation if required.
  618. (when (plist-get info :preserve-breaks)
  619. (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
  620. text)))
  621. ;; Return value.
  622. text)
  623. ;;; Planning
  624. ;;; Property Drawer
  625. ;;; Quote Block
  626. (defun org-e-man-quote-block (quote-block contents info)
  627. "Transcode a QUOTE-BLOCK element from Org to Man.
  628. CONTENTS holds the contents of the block. INFO is a plist
  629. holding contextual information."
  630. (org-e-man--wrap-label
  631. quote-block
  632. (format ".RS\n%s\n.RE" contents)))
  633. ;;; Quote Section
  634. (defun org-e-man-quote-section (quote-section contents info)
  635. "Transcode a QUOTE-SECTION element from Org to Man.
  636. CONTENTS is nil. INFO is a plist holding contextual information."
  637. (let ((value (org-remove-indentation
  638. (org-element-property :value quote-section))))
  639. (when value (format ".RS\\fI%s\\fP\n.RE\n" value))))
  640. ;;; Radio Target
  641. (defun org-e-man-radio-target (radio-target text info)
  642. "Transcode a RADIO-TARGET object from Org to Man.
  643. TEXT is the text of the target. INFO is a plist holding
  644. contextual information."
  645. text )
  646. ;;; Section
  647. (defun org-e-man-section (section contents info)
  648. "Transcode a SECTION element from Org to Man.
  649. CONTENTS holds the contents of the section. INFO is a plist
  650. holding contextual information."
  651. contents)
  652. ;;; Special Block
  653. (defun org-e-man-special-block (special-block contents info)
  654. "Transcode a SPECIAL-BLOCK element from Org to Man.
  655. CONTENTS holds the contents of the block. INFO is a plist
  656. holding contextual information."
  657. (let ((type (downcase (org-element-property :type special-block))))
  658. (org-e-man--wrap-label
  659. special-block
  660. (format "%s\n" contents))))
  661. ;;; Src Block
  662. (defun org-e-man-src-block (src-block contents info)
  663. "Transcode a SRC-BLOCK element from Org to Man.
  664. CONTENTS holds the contents of the item. INFO is a plist holding
  665. contextual information."
  666. (let* ((lang (org-element-property :language src-block))
  667. (caption (org-element-property :caption src-block))
  668. (label (org-element-property :name src-block))
  669. (code (org-element-property :value src-block))
  670. (custom-env (and lang
  671. (cadr (assq (intern lang)
  672. org-e-man-custom-lang-environments))))
  673. (num-start (case (org-element-property :number-lines src-block)
  674. (continued (org-export-get-loc src-block info))
  675. (new 0)))
  676. (retain-labels (org-element-property :retain-labels src-block)))
  677. (cond
  678. ;; Case 1. No source fontification.
  679. ((not org-e-man-source-highlight)
  680. (let ((caption-str (org-e-man--caption/label-string caption label info)))
  681. (concat
  682. (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
  683. (org-export-format-code-default src-block info)))))
  684. ( (and org-e-man-source-highlight)
  685. (let* ((tmpdir (if (featurep 'xemacs)
  686. temp-directory
  687. temporary-file-directory ))
  688. (in-file (make-temp-name
  689. (expand-file-name "srchilite" tmpdir)))
  690. (out-file (make-temp-name
  691. (expand-file-name "reshilite" tmpdir)))
  692. (org-lang (org-element-property :language src-block))
  693. (lst-lang (cadr (assq (intern org-lang)
  694. org-e-man-source-highlight-langs)))
  695. (cmd (concat "source-highlight"
  696. " -s " lst-lang
  697. " -f groff_man "
  698. " -i " in-file
  699. " -o " out-file)))
  700. (if lst-lang
  701. (let ((code-block "" ))
  702. (with-temp-file in-file (insert code))
  703. (shell-command cmd)
  704. (setq code-block (org-file-contents out-file))
  705. (delete-file in-file)
  706. (delete-file out-file)
  707. code-block)
  708. (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE"
  709. code)))))))
  710. ;;; Statistics Cookie
  711. (defun org-e-man-statistics-cookie (statistics-cookie contents info)
  712. "Transcode a STATISTICS-COOKIE object from Org to Man.
  713. CONTENTS is nil. INFO is a plist holding contextual information."
  714. (org-element-property :value statistics-cookie))
  715. ;;; Strike-Through
  716. (defun org-e-man-strike-through (strike-through contents info)
  717. "Transcode STRIKE-THROUGH from Org to Man.
  718. CONTENTS is the text with strike-through markup. INFO is a plist
  719. holding contextual information."
  720. (format "\\fI%s\\fP" contents))
  721. ;;; Subscript
  722. (defun org-e-man-subscript (subscript contents info)
  723. "Transcode a SUBSCRIPT object from Org to Man.
  724. CONTENTS is the contents of the object. INFO is a plist holding
  725. contextual information."
  726. (format "\\d\\s-2%s\\s+2\\u" contents))
  727. ;;; Superscript "^_%s$
  728. (defun org-e-man-superscript (superscript contents info)
  729. "Transcode a SUPERSCRIPT object from Org to Man.
  730. CONTENTS is the contents of the object. INFO is a plist holding
  731. contextual information."
  732. (format "\\u\\s-2%s\\s+2\\d" contents))
  733. ;;; Table
  734. ;;
  735. ;; `org-e-man-table' is the entry point for table transcoding. It
  736. ;; takes care of tables with a "verbatim" attribute. Otherwise, it
  737. ;; delegates the job to either `org-e-man-table--table.el-table' or
  738. ;; `org-e-man-table--org-table' functions, depending of the type of
  739. ;; the table.
  740. ;;
  741. ;; `org-e-man-table--align-string' is a subroutine used to build
  742. ;; alignment string for Org tables.
  743. (defun org-e-man-table (table contents info)
  744. "Transcode a TABLE element from Org to Man.
  745. CONTENTS is the contents of the table. INFO is a plist holding
  746. contextual information."
  747. (cond
  748. ;; Case 1: verbatim table.
  749. ((or org-e-man-tables-verbatim
  750. (let ((attr (read (format "(%s)"
  751. (mapconcat
  752. #'identity
  753. (org-element-property :attr_man table)
  754. " ")))))
  755. (and attr (plist-get attr :verbatim))))
  756. (format ".nf\n\\fC%s\\fP\n.fi"
  757. ;; Re-create table, without affiliated keywords.
  758. (org-trim
  759. (org-element-interpret-data
  760. `(table nil ,@(org-element-contents table))))))
  761. ;; Case 2: Standard table.
  762. (t (org-e-man-table--org-table table contents info))))
  763. (defun org-e-man-table--align-string (divider table info)
  764. "Return an appropriate Man alignment string.
  765. TABLE is the considered table. INFO is a plist used as
  766. a communication channel."
  767. (let (alignment)
  768. ;; Extract column groups and alignment from first (non-rule)
  769. ;; row.
  770. (org-element-map
  771. (org-element-map
  772. table 'table-row
  773. (lambda (row)
  774. (and (eq (org-element-property :type row) 'standard) row))
  775. info 'first-match)
  776. 'table-cell
  777. (lambda (cell)
  778. (let* ((borders (org-export-table-cell-borders cell info))
  779. (raw-width (org-export-table-cell-width cell info))
  780. (width-cm (when raw-width (/ raw-width 5)))
  781. (width (if raw-width (format "w(%dc)"
  782. (if (< width-cm 1) 1 width-cm)) "")))
  783. ;; Check left border for the first cell only.
  784. (when (and (memq 'left borders) (not alignment))
  785. (push "|" alignment))
  786. (push
  787. (case (org-export-table-cell-alignment cell info)
  788. (left (concat "l" width divider))
  789. (right (concat "r" width divider))
  790. (center (concat "c" width divider)))
  791. alignment)
  792. (when (memq 'right borders) (push "|" alignment))))
  793. info)
  794. (apply 'concat (reverse alignment))))
  795. (defun org-e-man-table--org-table (table contents info)
  796. "Return appropriate Man code for an Org table.
  797. TABLE is the table type element to transcode. CONTENTS is its
  798. contents, as a string. INFO is a plist used as a communication
  799. channel.
  800. This function assumes TABLE has `org' as its `:type' attribute."
  801. (let* ((label (org-element-property :name table))
  802. (caption (org-e-man--caption/label-string
  803. (org-element-property :caption table) label info))
  804. (attr (read
  805. (format "(%s)"
  806. (mapconcat
  807. #'identity
  808. (org-element-property :attr_man table)
  809. " "))))
  810. (divider (if (plist-get attr :divider)
  811. "|"
  812. " "))
  813. ;; Determine alignment string.
  814. (alignment (org-e-man-table--align-string divider table info))
  815. ;; Extract others display options.
  816. (lines (org-split-string contents "\n"))
  817. (attr-list
  818. (let ((result-list '()))
  819. (dolist (attr-item
  820. (list
  821. (if (plist-get attr :expand)
  822. "expand" nil)
  823. (case (plist-get attr :placement)
  824. ('center "center")
  825. ('left nil)
  826. (t
  827. (if org-e-man-tables-centered
  828. "center" "")))
  829. (case (plist-get attr :boxtype)
  830. ('box "box")
  831. ('doublebox "doublebox")
  832. ('allbox "allbox")
  833. ('none nil)
  834. (t "box"))))
  835. (if attr-item
  836. (add-to-list 'result-list attr-item)))
  837. result-list ))
  838. (title-line (plist-get attr :title-line))
  839. (disable-caption (plist-get attr :disable-caption))
  840. (long-cells (plist-get attr :long-cells))
  841. (table-format (concat
  842. (format "%s" (or (car attr-list) "" ))
  843. (or
  844. (let ((output-list '()))
  845. (when (cdr attr-list)
  846. (dolist (attr-item (cdr attr-list))
  847. (setq output-list (concat output-list (format ",%s" attr-item)))))
  848. output-list)
  849. "")))
  850. (first-line (when lines (org-split-string (car lines) "\t"))))
  851. ;; Prepare the final format string for the table.
  852. (cond
  853. ;; Others.
  854. (lines (concat ".TS\n " table-format ";\n"
  855. (format "%s.\n"
  856. (let ((final-line ""))
  857. (when title-line
  858. (dotimes (i (length first-line))
  859. (setq final-line (concat final-line "cb" divider))))
  860. (setq final-line (concat final-line "\n"))
  861. (if alignment
  862. (setq final-line (concat final-line alignment))
  863. (dotimes (i (length first-line))
  864. (setq final-line (concat final-line "c" divider))))
  865. final-line ))
  866. (format "%s.TE\n"
  867. (let ((final-line "")
  868. (long-line "")
  869. (lines (org-split-string contents "\n")))
  870. (dolist (line-item lines)
  871. (setq long-line "")
  872. (if long-cells
  873. (progn
  874. (if (string= line-item "_")
  875. (setq long-line (format "%s\n" line-item))
  876. ;; else string =
  877. (let ((cell-item-list (org-split-string line-item "\t")))
  878. (dolist (cell-item cell-item-list)
  879. (cond ((eq cell-item (car (last cell-item-list)))
  880. (setq long-line (concat long-line
  881. (format "T{\n%s\nT}\t\n" cell-item ))))
  882. (t
  883. (setq long-line (concat long-line
  884. (format "T{\n%s\nT}\t" cell-item ))))))
  885. long-line))
  886. ;; else long cells
  887. (setq final-line (concat final-line long-line )))
  888. (setq final-line (concat final-line line-item "\n"))))
  889. final-line))
  890. (if (not disable-caption)
  891. (format ".TB \"%s\""
  892. caption) ""))))))
  893. ;;; Table Cell
  894. (defun org-e-man-table-cell (table-cell contents info)
  895. "Transcode a TABLE-CELL element from Org to Man
  896. CONTENTS is the cell contents. INFO is a plist used as
  897. a communication channel."
  898. (concat (if (and contents
  899. org-e-man-table-scientific-notation
  900. (string-match orgtbl-exp-regexp contents))
  901. ;; Use appropriate format string for scientific
  902. ;; notation.
  903. (format org-e-man-table-scientific-notation
  904. (match-string 1 contents)
  905. (match-string 2 contents))
  906. contents )
  907. (when (org-export-get-next-element table-cell info) "\t")))
  908. ;;; Table Row
  909. (defun org-e-man-table-row (table-row contents info)
  910. "Transcode a TABLE-ROW element from Org to Man
  911. CONTENTS is the contents of the row. INFO is a plist used as
  912. a communication channel."
  913. ;; Rules are ignored since table separators are deduced from
  914. ;; borders of the current row.
  915. (when (eq (org-element-property :type table-row) 'standard)
  916. (let* ((attr (mapconcat 'identity
  917. (org-element-property
  918. :attr_man (org-export-get-parent table-row))
  919. " "))
  920. ;; TABLE-ROW's borders are extracted from its first cell.
  921. (borders
  922. (org-export-table-cell-borders
  923. (car (org-element-contents table-row)) info)))
  924. (concat
  925. ;; Mark horizontal lines
  926. (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
  927. contents
  928. (cond
  929. ;; When BOOKTABS are activated enforce bottom rule even when
  930. ;; no hline was specifically marked.
  931. ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
  932. ((memq 'below borders) "\n_"))))))
  933. ;;; Target
  934. (defun org-e-man-target (target contents info)
  935. "Transcode a TARGET object from Org to Man.
  936. CONTENTS is nil. INFO is a plist holding contextual
  937. information."
  938. (format "\\fI%s\\fP"
  939. (org-export-solidify-link-text (org-element-property :value target))))
  940. ;;; Timestamp
  941. (defun org-e-man-timestamp (timestamp contents info)
  942. "Transcode a TIMESTAMP object from Org to Man.
  943. CONTENTS is nil. INFO is a plist holding contextual
  944. information."
  945. "" )
  946. ;;; Underline
  947. (defun org-e-man-underline (underline contents info)
  948. "Transcode UNDERLINE from Org to Man.
  949. CONTENTS is the text with underline markup. INFO is a plist
  950. holding contextual information."
  951. (format "\\fI%s\\fP" contents))
  952. ;;; Verbatim
  953. (defun org-e-man-verbatim (verbatim contents info)
  954. "Transcode a VERBATIM object from Org to Man.
  955. CONTENTS is nil. INFO is a plist used as a communication
  956. channel."
  957. (format ".nf\n%s\n.fi" contents))
  958. ;;; Verse Block
  959. (defun org-e-man-verse-block (verse-block contents info)
  960. "Transcode a VERSE-BLOCK element from Org to Man.
  961. CONTENTS is verse block contents. INFO is a plist holding
  962. contextual information."
  963. (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
  964. ;;; Interactive functions
  965. (defun org-e-man-export-to-man
  966. (&optional subtreep visible-only body-only ext-plist pub-dir)
  967. "Export current buffer to a Man file.
  968. If narrowing is active in the current buffer, only export its
  969. narrowed part.
  970. If a region is active, export that region.
  971. When optional argument SUBTREEP is non-nil, export the sub-tree
  972. at point, extracting information from the headline properties
  973. first.
  974. When optional argument VISIBLE-ONLY is non-nil, don't export
  975. contents of hidden elements.
  976. When optional argument BODY-ONLY is non-nil, only the body
  977. without any markers.
  978. EXT-PLIST, when provided, is a property list with external
  979. parameters overriding Org default settings, but still inferior to
  980. file-local settings.
  981. When optional argument PUB-DIR is set, use it as the publishing
  982. directory.
  983. Return output file's name."
  984. (interactive)
  985. (let ((outfile (org-export-output-file-name ".man" subtreep pub-dir)))
  986. (org-export-to-file
  987. 'e-man outfile subtreep visible-only body-only ext-plist)))
  988. (defun org-e-man-export-to-pdf
  989. (&optional subtreep visible-only body-only ext-plist pub-dir)
  990. "Export current buffer to Groff then process through to PDF.
  991. If narrowing is active in the current buffer, only export its
  992. narrowed part.
  993. If a region is active, export that region.
  994. When optional argument SUBTREEP is non-nil, export the sub-tree
  995. at point, extracting information from the headline properties
  996. first.
  997. When optional argument VISIBLE-ONLY is non-nil, don't export
  998. contents of hidden elements.
  999. When optional argument BODY-ONLY is non-nil, only write between
  1000. markers.
  1001. EXT-PLIST, when provided, is a property list with external
  1002. parameters overriding Org default settings, but still inferior to
  1003. file-local settings.
  1004. When optional argument PUB-DIR is set, use it as the publishing
  1005. directory.
  1006. Return PDF file's name."
  1007. (interactive)
  1008. (org-e-man-compile
  1009. (org-e-man-export-to-man
  1010. subtreep visible-only body-only ext-plist pub-dir)))
  1011. (defun org-e-man-compile (grofffile)
  1012. "Compile a Groff file.
  1013. GROFFFILE is the name of the file being compiled. Processing is
  1014. done through the command specified in `org-e-man-pdf-process'.
  1015. Return PDF file name or an error if it couldn't be produced."
  1016. (let* ((wconfig (current-window-configuration))
  1017. (grofffile (file-truename grofffile))
  1018. (base (file-name-sans-extension grofffile))
  1019. errors)
  1020. (message (format "Processing Groff file %s ..." grofffile))
  1021. (unwind-protect
  1022. (progn
  1023. (cond
  1024. ;; A function is provided: Apply it.
  1025. ((functionp org-e-man-pdf-process)
  1026. (funcall org-e-man-pdf-process (shell-quote-argument grofffile)))
  1027. ;; A list is provided: Replace %b, %f and %o with appropriate
  1028. ;; values in each command before applying it. Output is
  1029. ;; redirected to "*Org PDF Groff Output*" buffer.
  1030. ((consp org-e-man-pdf-process)
  1031. (let* ((out-dir (or (file-name-directory grofffile) "./"))
  1032. (outbuf (get-buffer-create "*Org PDF Groff Output*")))
  1033. (mapc
  1034. (lambda (command)
  1035. (shell-command
  1036. (replace-regexp-in-string
  1037. "%b" (shell-quote-argument base)
  1038. (replace-regexp-in-string
  1039. "%f" (shell-quote-argument grofffile)
  1040. (replace-regexp-in-string
  1041. "%o" (shell-quote-argument out-dir) command t t) t t) t t)
  1042. outbuf))
  1043. org-e-man-pdf-process)
  1044. ;; Collect standard errors from output buffer.
  1045. (setq errors (org-e-man-collect-errors outbuf))))
  1046. (t (error "No valid command to process to PDF")))
  1047. (let ((pdffile (concat base ".pdf")))
  1048. ;; Check for process failure. Provide collected errors if
  1049. ;; possible.
  1050. (if (not (file-exists-p pdffile))
  1051. (error (concat (format "PDF file %s wasn't produced" pdffile)
  1052. (when errors (concat ": " errors))))
  1053. ;; Else remove log files, when specified, and signal end of
  1054. ;; process to user, along with any error encountered.
  1055. (when org-e-man-remove-logfiles
  1056. (dolist (ext org-e-man-logfiles-extensions)
  1057. (let ((file (concat base "." ext)))
  1058. (when (file-exists-p file) (delete-file file)))))
  1059. (message (concat "Process completed"
  1060. (if (not errors) "."
  1061. (concat " with errors: " errors)))))
  1062. ;; Return output file name.
  1063. pdffile))
  1064. (set-window-configuration wconfig))))
  1065. (defun org-e-man-collect-errors (buffer)
  1066. "Collect some kind of errors from \"groff\" output
  1067. BUFFER is the buffer containing output.
  1068. Return collected error types as a string, or nil if there was
  1069. none."
  1070. (with-current-buffer buffer
  1071. (save-excursion
  1072. (goto-char (point-max))
  1073. ;; Find final run
  1074. nil )))
  1075. (provide 'org-e-man)
  1076. ;;; org-e-man.el ends here