org-e-man.el 44 KB

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