org-e-man.el 44 KB

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