org-e-man.el 44 KB

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