org-e-man.el 44 KB

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