org-e-groff.el 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  1. ;; org-e-groff.el --- Groff 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 Groff Memorandum Macro back-end for
  20. ;; Org generic exporter.
  21. ;;
  22. ;; To test it, run
  23. ;;
  24. ;; M-: (org-export-to-buffer 'e-groff "*Test e-Groff*") RET
  25. ;;
  26. ;; in an org-mode buffer then switch to the buffer to see the Groff
  27. ;; export. See contrib/lisp/org-export.el for more details on how
  28. ;; this exporter works.
  29. ;;
  30. ;; It introduces two new buffer keywords: "GROFF_CLASS" and
  31. ;; "GROFF_CLASS_OPTIONS".
  32. ;;; Code:
  33. (eval-when-compile (require 'cl))
  34. (defvar org-export-groff-default-packages-alist)
  35. (defvar org-export-groff-packages-alist)
  36. (require 'org-export)
  37. ;;; Define Back-End
  38. (defvar org-e-groff-translate-alist
  39. '((babel-call . org-e-groff-babel-call)
  40. (bold . org-e-groff-bold)
  41. (center-block . org-e-groff-center-block)
  42. (clock . org-e-groff-clock)
  43. (code . org-e-groff-code)
  44. (comment . org-e-groff-comment)
  45. (comment-block . org-e-groff-comment-block)
  46. (drawer . org-e-groff-drawer)
  47. (dynamic-block . org-e-groff-dynamic-block)
  48. (entity . org-e-groff-entity)
  49. (example-block . org-e-groff-example-block)
  50. (export-block . org-e-groff-export-block)
  51. (export-snippet . org-e-groff-export-snippet)
  52. (fixed-width . org-e-groff-fixed-width)
  53. (footnote-definition . org-e-groff-footnote-definition)
  54. (footnote-reference . org-e-groff-footnote-reference)
  55. (headline . org-e-groff-headline)
  56. (horizontal-rule . org-e-groff-horizontal-rule)
  57. (inline-babel-call . org-e-groff-inline-babel-call)
  58. (inline-src-block . org-e-groff-inline-src-block)
  59. (inlinetask . org-e-groff-inlinetask)
  60. (italic . org-e-groff-italic)
  61. (item . org-e-groff-item)
  62. (keyword . org-e-groff-keyword)
  63. (groff-environment . org-e-groff-groff-environment)
  64. (groff-fragment . org-e-groff-groff-fragment)
  65. (line-break . org-e-groff-line-break)
  66. (link . org-e-groff-link)
  67. (macro . org-e-groff-macro)
  68. (paragraph . org-e-groff-paragraph)
  69. (plain-list . org-e-groff-plain-list)
  70. (plain-text . org-e-groff-plain-text)
  71. (planning . org-e-groff-planning)
  72. (property-drawer . org-e-groff-property-drawer)
  73. (quote-block . org-e-groff-quote-block)
  74. (quote-section . org-e-groff-quote-section)
  75. (radio-target . org-e-groff-radio-target)
  76. (section . org-e-groff-section)
  77. (special-block . org-e-groff-special-block)
  78. (src-block . org-e-groff-src-block)
  79. (statistics-cookie . org-e-groff-statistics-cookie)
  80. (strike-through . org-e-groff-strike-through)
  81. (subscript . org-e-groff-subscript)
  82. (superscript . org-e-groff-superscript)
  83. (table . org-e-groff-table)
  84. (table-cell . org-e-groff-table-cell)
  85. (table-row . org-e-groff-table-row)
  86. (target . org-e-groff-target)
  87. (template . org-e-groff-template)
  88. (timestamp . org-e-groff-timestamp)
  89. (underline . org-e-groff-underline)
  90. (verbatim . org-e-groff-verbatim)
  91. (verse-block . org-e-groff-verse-block))
  92. "Alist between element or object types and translators.")
  93. (defconst org-e-groff-options-alist
  94. '((:date "DATE" nil org-e-groff-date-format t)
  95. (:groff-class "GROFF_CLASS" nil org-e-groff-default-class t)
  96. (:groff-class-options "GROFF_CLASS_OPTIONS" nil nil t)
  97. (:groff-header-extra "GROFF_HEADER" nil nil newline))
  98. "Alist between Groff export properties and ways to set them.
  99. See `org-export-options-alist' for more information on the
  100. structure of the values.")
  101. ;;; User Configurable Variables
  102. (defgroup org-export-e-groff nil
  103. "Options for exporting Org mode files to Groff."
  104. :tag "Org Export Groff"
  105. :group 'org-export)
  106. ;;; Preamble
  107. (defcustom org-e-groff-default-class "internal"
  108. "The default Groff class."
  109. :group 'org-export-e-groff
  110. :type '(string :tag "Groff class"))
  111. (defcustom org-e-groff-classes
  112. '(("file" ".MT 1"
  113. (:heading 'default :type "memo" :last-section "toc"))
  114. ("internal" ".MT 0"
  115. (:heading 'default :type "memo" :last-section "toc"))
  116. ("programmer" ".MT 2"
  117. (:heading 'default :type "memo" :last-section "toc"))
  118. ("engineer" ".MT 3"
  119. (:heading 'default :type "memo" :last-section "toc"))
  120. ("external" ".MT 4"
  121. (:heading 'default :type "memo" :last-section "toc"))
  122. ("letter" ".MT 5"
  123. (:heading 'default :type "memo" :last-section "sign"))
  124. ("custom" ".so file"
  125. (:heading custom-function :type "custom" :last-section "toc"))
  126. ("dummy" ""
  127. (:heading 'default :type "memo"))
  128. ;; Dummy means, no Cover or Memorandum Type but calls to AU, AT,
  129. ;; ND and TL are made. This is to facilitate Abstract Insertion.
  130. ("ms" "ms"
  131. (:heading 'default :type "cover" :last-section "toc"))
  132. ("se_ms" "se_ms"
  133. (:heading 'default :type "cover" :last-section "toc"))
  134. ("none" "" '(:heading 'default :type "custom")))
  135. ;; None means, no Cover or Memorandum Type and no calls to AU, AT,
  136. ;; ND and TL This is to facilitate the creation of custom pages.
  137. "This list describes the attributes for the documents being created.
  138. It allows for the creation of new "
  139. :group 'org-export-e-groff
  140. :type '(repeat
  141. (list (string :tag "Document Type")
  142. (string :tag "Header")
  143. (repeat :tag "Options" :inline t
  144. (choice
  145. (list :tag "Heading")
  146. (function :tag "Hook computing sectioning"))))))
  147. (defcustom org-e-groff-date-format
  148. (format-time-string "%Y-%m-%d")
  149. "Format string for .ND "
  150. :group 'org-export-e-groff
  151. :type 'boolean)
  152. ;;; Headline
  153. (defcustom org-e-groff-format-headline-function nil
  154. "Function to format headline text.
  155. This function will be called with 5 arguments:
  156. TODO the todo keyword (string or nil).
  157. TODO-TYPE the type of todo (symbol: `todo', `done', nil)
  158. PRIORITY the priority of the headline (integer or nil)
  159. TEXT the main headline text (string).
  160. TAGS the tags as a list of strings (list of strings or nil).
  161. The function result will be used in the section format string.
  162. As an example, one could set the variable to the following, in
  163. order to reproduce the default set-up:
  164. \(defun org-e-groff-format-headline (todo todo-type priority text tags)
  165. \"Default format function for an headline.\"
  166. \(concat (when todo
  167. \(format \"\\fB%s\\fP \" todo))
  168. \(when priority
  169. \(format \"[\\#%c] \" priority))
  170. text
  171. \(when tags
  172. \(format \" %s \"
  173. \(mapconcat 'identity tags \":\"))))"
  174. :group 'org-export-e-groff
  175. :type 'function)
  176. ;;; Timestamps
  177. (defcustom org-e-groff-active-timestamp-format "\\fI%s\\fP"
  178. "A printf format string to be applied to active timestamps."
  179. :group 'org-export-e-groff
  180. :type 'string)
  181. (defcustom org-e-groff-inactive-timestamp-format "\\fI%s\\fP"
  182. "A printf format string to be applied to inactive timestamps."
  183. :group 'org-export-e-groff
  184. :type 'string)
  185. (defcustom org-e-groff-diary-timestamp-format "\\fI%s\\fP"
  186. "A printf format string to be applied to diary timestamps."
  187. :group 'org-export-e-groff
  188. :type 'string)
  189. ;;; Links
  190. (defcustom org-e-groff-inline-image-rules
  191. '(("file" . "\\.\\(pdf\\|ps\\|eps\\|pic\\)\\'")
  192. ("fuzzy" . "\\.\\(pdf\\|ps\\|eps\\|pic\\)\\'"))
  193. "Rules characterizing image files that can be inlined into Groff.
  194. A rule consists in an association whose key is the type of link
  195. to consider, and value is a regexp that will be matched against
  196. link's path.
  197. Note that, by default, the image extensions actually allowed
  198. depend on the way the Groff file is processed. When used with
  199. pdfgroff, pdf, jpg and png images are OK. When processing
  200. through dvi to Postscript, only ps and eps are allowed. The
  201. default we use here encompasses both."
  202. :group 'org-export-e-groff
  203. :type '(alist :key-type (string :tag "Type")
  204. :value-type (regexp :tag "Path")))
  205. (defcustom org-e-groff-link-with-unknown-path-format "\\fI%s\\fP"
  206. "Format string for links with unknown path type."
  207. :group 'org-export-groff
  208. :type 'string)
  209. ;;; Tables
  210. (defcustom org-e-groff-tables-centered t
  211. "When non-nil, tables are exported in a center environment."
  212. :group 'org-export-e-groff
  213. :type 'boolean)
  214. (defcustom org-e-groff-tables-verbatim nil
  215. "When non-nil, tables are exported verbatim."
  216. :group 'org-export-e-groff
  217. :type 'boolean)
  218. (defcustom org-e-groff-table-scientific-notation "%sE%s"
  219. "Format string to display numbers in scientific notation.
  220. The format should have \"%s\" twice, for mantissa and exponent
  221. \(i.e. \"%s\\\\times10^{%s}\").
  222. When nil, no transformation is made."
  223. :group 'org-export-e-groff
  224. :type '(choice
  225. (string :tag "Format string")
  226. (const :tag "No formatting")))
  227. ;;; Text markup
  228. (defcustom org-e-groff-text-markup-alist
  229. '((bold . "\\fB%s\\fP")
  230. ;; from "verb"
  231. (code . "\\fC%s\\fP")
  232. (italic . "\\fI%s\\fP")
  233. (strike-through . "\\fC%s\\fP") ; Strike through and underline
  234. (underline . "\\fI%s\\fP") ; need to be revised.
  235. (verbatim . "protectedtexttt"))
  236. "Alist of Groff expressions to convert text markup.
  237. The key must be a symbol among `bold', `code', `italic',
  238. `strike-through', `underline' and `verbatim'. The value is
  239. a formatting string to wrap fontified text with it.
  240. If no association can be found for a given markup, text will be
  241. returned as-is."
  242. :group 'org-export-e-groff
  243. :type 'alist
  244. :options '(bold code italic strike-through underline verbatim))
  245. ;;; Drawers
  246. (defcustom org-e-groff-format-drawer-function nil
  247. "Function called to format a drawer in Groff code.
  248. The function must accept two parameters:
  249. NAME the drawer name, like \"LOGBOOK\"
  250. CONTENTS the contents of the drawer.
  251. The function should return the string to be exported.
  252. For example, the variable could be set to the following function
  253. in order to mimic default behaviour:
  254. \(defun org-e-groff-format-drawer-default \(name contents\)
  255. \"Format a drawer element for Groff export.\"
  256. contents\)"
  257. :group 'org-export-e-groff
  258. :type 'function)
  259. ;;; Inlinetasks
  260. (defcustom org-e-groff-format-inlinetask-function nil
  261. "Function called to format an inlinetask in Groff code.
  262. The function must accept six parameters:
  263. TODO the todo keyword, as a string
  264. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  265. PRIORITY the inlinetask priority, as a string
  266. NAME the inlinetask name, as a string.
  267. TAGS the inlinetask tags, as a list of strings.
  268. CONTENTS the contents of the inlinetask, as a string.
  269. The function should return the string to be exported.
  270. For example, the variable could be set to the following function
  271. in order to mimic default behaviour:
  272. \(defun org-e-groff-format-inlinetask \(todo type priority name tags contents\)
  273. \"Format an inline task element for Groff export.\"
  274. \(let ((full-title
  275. \(concat
  276. \(when todo
  277. \(format \"\\fB%s\\fP \" todo))
  278. \(when priority (format \"[\\#%c] \" priority))
  279. title
  280. \(when tags
  281. \(format \":%s:\"
  282. \(mapconcat 'identity tags \":\")))))
  283. \(format (concat \".DS L\\n\"
  284. \"%s\\n\\n\"
  285. \"%s\"
  286. \".DE\")
  287. full-title contents))"
  288. :group 'org-export-e-groff
  289. :type 'function)
  290. ;;; Src blocks
  291. (defcustom org-e-groff-source-highlight nil
  292. "Use GNU source highlight to embellish source blocks "
  293. :group 'org-export-e-groff
  294. :type 'boolean)
  295. (defcustom org-e-groff-source-highlight-langs
  296. '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
  297. (scheme "scheme")
  298. (c "c") (cc "cpp") (csharp "csharp") (d "d")
  299. (fortran "fortran") (cobol "cobol") (pascal "pascal")
  300. (ada "ada") (asm "asm")
  301. (perl "perl") (cperl "perl")
  302. (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
  303. (java "java") (javascript "javascript")
  304. (tex "latex")
  305. (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
  306. (ocaml "caml") (caml "caml")
  307. (sql "sql") (sqlite "sql")
  308. (html "html") (css "css") (xml "xml")
  309. (bat "bat") (bison "bison") (clipper "clipper")
  310. (ldap "ldap") (opa "opa")
  311. (php "php") (postscript "postscript") (prolog "prolog")
  312. (properties "properties") (makefile "makefile")
  313. (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
  314. "Alist mapping languages to their listing language counterpart.
  315. The key is a symbol, the major mode symbol without the \"-mode\".
  316. The value is the string that should be inserted as the language
  317. parameter for the listings package. If the mode name and the
  318. listings name are the same, the language does not need an entry
  319. in this list - but it does not hurt if it is present."
  320. :group 'org-export-e-groff
  321. :type '(repeat
  322. (list
  323. (symbol :tag "Major mode ")
  324. (string :tag "Listings language"))))
  325. (defcustom org-e-groff-source-highlight-options nil
  326. "Association list of options for the groff listings package.
  327. These options are supplied as a comma-separated list to the
  328. \\lstset command. Each element of the association list should be
  329. a list containing two strings: the name of the option, and the
  330. value. For example,
  331. (setq org-e-groff-source-highlight-options
  332. '((\"basicstyle\" \"\\small\")
  333. (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
  334. will typeset the code in a small size font with underlined, bold
  335. black keywords.
  336. Note that the same options will be applied to blocks of all
  337. languages."
  338. :group 'org-export-e-groff
  339. :type '(repeat
  340. (list
  341. (string :tag "Listings option name ")
  342. (string :tag "Listings option value"))))
  343. (defvar org-e-groff-custom-lang-environments nil
  344. "Alist mapping languages to language-specific Groff environments.
  345. It is used during export of src blocks by the listings and
  346. groff packages. For example,
  347. \(setq org-e-groff-custom-lang-environments
  348. '\(\(python \"pythoncode\"\)\)\)
  349. would have the effect that if org encounters begin_src python
  350. during groff export it will use pythoncode as the source-highlight
  351. language.")
  352. ;;; Plain text
  353. (defcustom org-e-groff-quotes
  354. '(("fr"
  355. ("\\(\\s-\\|[[(]\\|^\\)\"" . "«~")
  356. ("\\(\\S-\\)\"" . "~»")
  357. ("\\(\\s-\\|(\\|^\\)'" . "'"))
  358. ("en"
  359. ("\\(\\s-\\|[[(]\\|^\\)\"" . "``")
  360. ("\\(\\S-\\)\"" . "''")
  361. ("\\(\\s-\\|(\\|^\\)'" . "`")))
  362. "Alist for quotes to use when converting english double-quotes.
  363. The CAR of each item in this alist is the language code.
  364. The CDR of each item in this alist is a list of three CONS:
  365. - the first CONS defines the opening quote;
  366. - the second CONS defines the closing quote;
  367. - the last CONS defines single quotes.
  368. For each item in a CONS, the first string is a regexp
  369. for allowed characters before/after the quote, the second
  370. string defines the replacement string for this quote."
  371. :group 'org-export-e-groff
  372. :type '(list
  373. (cons :tag "Opening quote"
  374. (string :tag "Regexp for char before")
  375. (string :tag "Replacement quote "))
  376. (cons :tag "Closing quote"
  377. (string :tag "Regexp for char after ")
  378. (string :tag "Replacement quote "))
  379. (cons :tag "Single quote"
  380. (string :tag "Regexp for char before")
  381. (string :tag "Replacement quote "))))
  382. (defcustom org-e-groff-special-char
  383. '(("(c)" . "\\\\(co")
  384. ("(tm)" . "\\\\(tm")
  385. ("(rg)" . "\\\\(rg"))
  386. "CONS list in which the value of the car
  387. is replace on the value of the CDR. "
  388. :group 'org-export-e-groff
  389. :type '(list
  390. (cons :tag "Character Subtitute"
  391. (string :tag "Original Character Group")
  392. (string :tag "Replacement Character"))))
  393. ;;; Compilation
  394. (defcustom org-e-groff-pdf-process
  395. '("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
  396. "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
  397. "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf")
  398. "Commands to process a Groff file to a PDF file.
  399. This is a list of strings, each of them will be given to the
  400. shell as a command. %f in the command will be replaced by the
  401. full file name, %b by the file base name \(i.e. without
  402. extension) and %o by the base directory of the file."
  403. :group 'org-export-pdf
  404. :type '(choice
  405. (repeat :tag "Shell command sequence"
  406. (string :tag "Shell command"))
  407. (const :tag "2 runs of pdfgroff"
  408. ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
  409. "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
  410. (const :tag "3 runs of pdfgroff"
  411. ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
  412. "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
  413. "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
  414. (function)))
  415. (defcustom org-e-groff-logfiles-extensions
  416. '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
  417. "The list of file extensions to consider as Groff logfiles."
  418. :group 'org-export-e-groff
  419. :type '(repeat (string :tag "Extension")))
  420. (defcustom org-e-groff-remove-logfiles t
  421. "Non-nil means remove the logfiles produced by PDF production.
  422. These are the .aux, .log, .out, and .toc files."
  423. :group 'org-export-e-groff
  424. :type 'boolean)
  425. (defcustom org-e-groff-organization "Org User"
  426. "Name of the organization used to populate the .AF command."
  427. :group 'org-export-e-groff
  428. :type 'string)
  429. ;;; Preamble
  430. ;; Adding GROFF as a block parser to make sure that its contents
  431. ;; does not execute
  432. (defvar org-e-groff-registered-references nil)
  433. (add-to-list 'org-element-block-name-alist
  434. '("GROFF" . org-element-export-block-parser))
  435. ;;; Internal Functions
  436. (defun org-e-groff--caption/label-string (caption label info)
  437. "Return caption and label Groff string for floats.
  438. CAPTION is a cons cell of secondary strings, the car being the
  439. standard caption and the cdr its short form. LABEL is a string
  440. representing the label. INFO is a plist holding contextual
  441. information.
  442. If there's no caption nor label, return the empty string.
  443. For non-floats, see `org-e-groff--wrap-label'."
  444. (let ((label-str ""))
  445. (cond
  446. ((and (not caption) (not label)) "")
  447. ((not caption) (format "\\fI%s\\fP" label))
  448. ;; Option caption format with short name.
  449. ((cdr caption)
  450. (format "%s\n.br\n%s - %s\n"
  451. (org-export-data (cdr caption) info)
  452. label-str
  453. (org-export-data (car caption) info)))
  454. ;; Standard caption format.
  455. (t (format "\\fR%s\\fP"
  456. (org-export-data (car caption) info))))))
  457. (defun org-e-groff--quotation-marks (text info)
  458. "Export quotation marks depending on language conventions.
  459. TEXT is a string containing quotation marks to be replaced. INFO
  460. is a plist used as a communication channel."
  461. (mapc (lambda(l)
  462. (let ((start 0))
  463. (while (setq start (string-match (car l) text start))
  464. (let ((new-quote (concat (match-string 1 text) (cdr l))))
  465. (setq text (replace-match new-quote t t text))))))
  466. (cdr (or (assoc (plist-get info :language) org-e-groff-quotes)
  467. ;; Falls back on English.
  468. (assoc "en" org-e-groff-quotes))))
  469. text)
  470. (defun org-e-groff--wrap-label (element output)
  471. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  472. This function shouldn't be used for floats. See
  473. `org-e-groff--caption/label-string'."
  474. (let ((label (org-element-property :name element)))
  475. (if (or (not output) (not label) (string= output "") (string= label ""))
  476. output
  477. (concat (format "%s\n.br\n" label) output))))
  478. (defun org-e-groff--text-markup (text markup)
  479. "Format TEXT depending on MARKUP text markup.
  480. See `org-e-groff-text-markup-alist' for details."
  481. (let ((fmt (cdr (assq markup org-e-groff-text-markup-alist))))
  482. (cond
  483. ;; No format string: Return raw text.
  484. ((not fmt) text)
  485. ((string= "protectedtexttt" fmt)
  486. (let ((start 0)
  487. (trans '(("\\" . "\\")))
  488. (rtn "")
  489. char)
  490. (while (string-match "[\\{}$%&_#~^]" text)
  491. (setq char (match-string 0 text))
  492. (if (> (match-beginning 0) 0)
  493. (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
  494. (setq text (substring text (1+ (match-beginning 0))))
  495. (setq char (or (cdr (assoc char trans)) (concat "\\" char))
  496. rtn (concat rtn char)))
  497. (setq text (concat rtn text))
  498. (format "\\fC%s\\fP" text)))
  499. ;; Else use format string.
  500. (t (format fmt text)))))
  501. ;;; Template
  502. (defun org-e-groff-template (contents info)
  503. "Return complete document string after Groff conversion.
  504. CONTENTS is the transcoded contents string. INFO is a plist
  505. holding export options."
  506. (let* ((title (org-export-data (plist-get info :title) info))
  507. (attr (read
  508. (format "(%s)"
  509. (mapconcat
  510. #'identity
  511. (list (plist-get info :groff-class-options))
  512. " "))))
  513. (class (plist-get info :groff-class))
  514. (class-options (plist-get info :groff-class-options))
  515. (classes (assoc class org-e-groff-classes))
  516. (classes-options (car (last classes)))
  517. (heading-option (plist-get classes-options :heading))
  518. (type-option (plist-get classes-options :type))
  519. (last-option (plist-get classes-options :last-section))
  520. (document-class-string
  521. (progn
  522. (org-element-normalize-string
  523. (let* ((header (nth 1 (assoc class org-e-groff-classes)))
  524. (document-class-item (if (stringp header) header "")))
  525. document-class-item)))))
  526. (concat
  527. (unless (string= type-option "custom")
  528. (progn
  529. (concat
  530. (when (and (stringp document-class-string)
  531. (string= type-option "cover"))
  532. (format ".COVER %s\n" document-class-string))
  533. ;; 1. Insert Organization
  534. (let ((firm-option (plist-get attr :firm)))
  535. (cond
  536. ((stringp firm-option)
  537. (format ".AF \"%s\" \n" firm-option))
  538. (t (format ".AF \"%s\" \n" (or org-e-groff-organization "")))))
  539. ;; 2. Title
  540. (let ((subtitle1 (plist-get attr :subtitle1))
  541. (subtitle2 (plist-get attr :subtitle2)))
  542. (cond
  543. ((string= "" title)
  544. (format ".TL \"%s\" \"%s\" \n%s\n"
  545. (or subtitle1 "")
  546. (or subtitle2 "") " "))
  547. ((not (or subtitle1 subtitle2))
  548. (format ".TL\n%s\n"
  549. (or title "" )))
  550. (t
  551. (format ".TL \"%s\" \"%s \" \n%s\n"
  552. (or subtitle1 "")
  553. (or subtitle2 "") title))))
  554. ;; 3. Author. In Groff, .AU *MUST* be placed after .TL
  555. (let ((author (and (plist-get info :with-author)
  556. (let ((auth (plist-get info :author)))
  557. (and auth (org-export-data auth info)))))
  558. (email (and (plist-get info :with-email)
  559. (org-export-data (plist-get info :email) info))))
  560. (cond ((and author email (not (string= "" email)))
  561. (format ".AU \"%s\" \"%s\"\n" author email))
  562. (author (format ".AU \"%s\"\n" author))
  563. (t ".AU \"\" \n")))
  564. ;; 4. Author Title, if present
  565. (let ((at-item (plist-get attr :author-title)))
  566. (if (and at-item (stringp at-item))
  567. (format ".AT \"%s\" \n" at-item)
  568. ""))
  569. ;; 5. Date.
  570. (let ((date (org-export-data (plist-get info :date) info)))
  571. (and date (format ".ND \"%s\"\n" date)))
  572. (when (string= type-option "cover")
  573. ".COVEND\n"))))
  574. ;;6. Hyphenation and Right Justification
  575. (let ((hyphenate (plist-get attr :hyphenate))
  576. (justify-right (plist-get attr :justify-right)))
  577. (concat
  578. (if justify-right
  579. (case justify-right
  580. ('yes ".SA 1 \n")
  581. ('no ".SA 0 \n")
  582. (t ""))
  583. "")
  584. (if hyphenate
  585. (case hyphenate
  586. ('yes ".nr Hy 1 \n")
  587. ('no ".nr Hy 0 \n")
  588. (t ""))
  589. "")))
  590. (when (string= type-option "memo")
  591. document-class-string)
  592. ;; 7. Document's body.
  593. contents
  594. ;; 8. Table of Content must be placed at the end being that it
  595. ;; gets collected from all the headers. In the case of letters,
  596. ;; signature will be placed instead.
  597. (cond
  598. ((string= last-option "toc") ".TC")
  599. ((string= last-option "sign")
  600. (let ((fc-item (plist-get attr :closing)))
  601. (concat (if (stringp fc-item)
  602. (format ".FC \"%s\" \n" fc-item)
  603. ".FC\n")
  604. ".SG")))
  605. (t "")))))
  606. ;;; Transcode Functions
  607. ;;; Babel Call
  608. ;;
  609. ;; Babel Calls are ignored.
  610. ;;; Bold
  611. (defun org-e-groff-bold (bold contents info)
  612. "Transcode BOLD from Org to Groff.
  613. CONTENTS is the text with bold markup. INFO is a plist holding
  614. contextual information."
  615. (org-e-groff--text-markup contents 'bold))
  616. ;;; Center Block
  617. (defun org-e-groff-center-block (center-block contents info)
  618. "Transcode a CENTER-BLOCK element from Org to Groff.
  619. CONTENTS holds the contents of the center block. INFO is a plist
  620. holding contextual information."
  621. (org-e-groff--wrap-label
  622. center-block
  623. (format ".DS C \n%s\n.DE" contents)))
  624. ;;; Clock
  625. (defun org-e-groff-clock (clock contents info)
  626. "Transcode a CLOCK element from Org to Groff.
  627. CONTENTS is nil. INFO is a plist holding contextual
  628. information."
  629. (concat
  630. (format "\\fB%s\\fP " org-clock-string)
  631. (format org-e-groff-inactive-timestamp-format
  632. (concat (org-translate-time (org-element-property :value clock))
  633. (let ((time (org-element-property :time clock)))
  634. (and time (format " (%s)" time)))))))
  635. ;;; Code
  636. (defun org-e-groff-code (code contents info)
  637. "Transcode a CODE object from Org to Groff.
  638. CONTENTS is nil. INFO is a plist used as a communication
  639. channel."
  640. (org-e-groff--text-markup (org-element-property :value code) 'code))
  641. ;;; Comment and comment blocks
  642. ;;
  643. ;; Comment and comment blocks are ignored.
  644. ;;; Drawer
  645. (defun org-e-groff-drawer (drawer contents info)
  646. "Transcode a DRAWER element from Org to Groff.
  647. CONTENTS holds the contents of the block. INFO is a plist
  648. holding contextual information."
  649. (let* ((name (org-element-property :drawer-name drawer))
  650. (output (if (functionp org-e-groff-format-drawer-function)
  651. (funcall org-e-groff-format-drawer-function
  652. name contents)
  653. ;; If there's no user defined function: simply
  654. ;; display contents of the drawer.
  655. contents)))
  656. (org-e-groff--wrap-label drawer output)))
  657. ;;; Dynamic Block
  658. (defun org-e-groff-dynamic-block (dynamic-block contents info)
  659. "Transcode a DYNAMIC-BLOCK element from Org to Groff.
  660. CONTENTS holds the contents of the block. INFO is a plist
  661. holding contextual information. See `org-export-data'."
  662. (org-e-groff--wrap-label dynamic-block contents))
  663. ;;; Entity
  664. (defun org-e-groff-entity (entity contents info)
  665. "Transcode an ENTITY object from Org to Groff.
  666. CONTENTS are the definition itself. INFO is a plist holding
  667. contextual information."
  668. (let ((ent (org-element-property :utf8 entity))) ent))
  669. ;;; Example Block
  670. (defun org-e-groff-example-block (example-block contents info)
  671. "Transcode an EXAMPLE-BLOCK element from Org to Groff.
  672. CONTENTS is nil. INFO is a plist holding contextual
  673. information."
  674. (org-e-groff--wrap-label
  675. example-block
  676. (format ".DS L\n%s\n.DE"
  677. (org-export-format-code-default example-block info))))
  678. ;;; Export Block
  679. (defun org-e-groff-export-block (export-block contents info)
  680. "Transcode a EXPORT-BLOCK element from Org to Groff.
  681. CONTENTS is nil. INFO is a plist holding contextual information."
  682. (when (string= (org-element-property :type export-block) "GROFF")
  683. (org-remove-indentation (org-element-property :value export-block))))
  684. ;;; Export Snippet
  685. (defun org-e-groff-export-snippet (export-snippet contents info)
  686. "Transcode a EXPORT-SNIPPET object from Org to Groff.
  687. CONTENTS is nil. INFO is a plist holding contextual information."
  688. (when (eq (org-export-snippet-backend export-snippet) 'e-groff)
  689. (org-element-property :value export-snippet)))
  690. ;;; Fixed Width
  691. (defun org-e-groff-fixed-width (fixed-width contents info)
  692. "Transcode a FIXED-WIDTH element from Org to Groff.
  693. CONTENTS is nil. INFO is a plist holding contextual information."
  694. (org-e-groff--wrap-label
  695. fixed-width
  696. (format "\\fC\n%s\\fP"
  697. (org-remove-indentation
  698. (org-element-property :value fixed-width)))))
  699. ;;; Footnote Definition
  700. ;;
  701. ;; Footnote Definitions are ignored.
  702. ;;
  703. ;; Footnotes are handled automatically in GROFF. Although manual
  704. ;; references can be added, not really required.
  705. (defun org-e-groff-footnote-reference (footnote-reference contents info)
  706. ;; Changing from info to footnote-reference
  707. (let* ((raw (org-export-get-footnote-definition footnote-reference info))
  708. (n (org-export-get-footnote-number footnote-reference info))
  709. (data (org-trim (org-export-data raw info)))
  710. (ref-id (plist-get (nth 1 footnote-reference) :label)))
  711. ;; It is a reference
  712. (if (string-match "fn:rl" ref-id)
  713. (if (member ref-id org-e-groff-registered-references)
  714. (format "\\*[%s]" ref-id)
  715. (progn
  716. (push ref-id org-e-groff-registered-references)
  717. (format "\\*(Rf\n.RS \"%s\" \n%s\n.RF\n" ref-id data)))
  718. ;; else it is a footnote
  719. (format "\\u\\s-2%s\\d\\s+2\n.FS %s\n%s\n.FE\n" n n data))))
  720. ;;; Headline
  721. (defun org-e-groff-headline (headline contents info)
  722. "Transcode an HEADLINE element from Org to Groff.
  723. CONTENTS holds the contents of the headline. INFO is a plist
  724. holding contextual information."
  725. (let* ((class (plist-get info :groff-class))
  726. (level (org-export-get-relative-level headline info))
  727. (numberedp (org-export-numbered-headline-p headline info))
  728. ;; Section formatting will set two placeholders: one for the
  729. ;; title and the other for the contents.
  730. (classes (assoc class org-e-groff-classes))
  731. (classes-options (car (last classes)))
  732. (heading-option (plist-get classes-options :heading))
  733. (section-fmt
  734. (progn
  735. (cond
  736. ((and (symbolp heading-option)
  737. (fboundp heading-option))
  738. (funcall heading-option level numberedp))
  739. ((> level 7) nil)
  740. (t (if numberedp
  741. (concat ".H " (number-to-string level) " \"%s\"\n%s")
  742. ".HU \"%s\"\n%s")))))
  743. ;; End of section-fmt
  744. (text (org-export-data (org-element-property :title headline) info))
  745. (todo
  746. (and (plist-get info :with-todo-keywords)
  747. (let ((todo (org-element-property :todo-keyword headline)))
  748. (and todo (org-export-data todo info)))))
  749. (todo-type (and todo (org-element-property :todo-type headline)))
  750. (tags (and (plist-get info :with-tags)
  751. (org-export-get-tags headline info)))
  752. (priority (and (plist-get info :with-priority)
  753. (org-element-property :priority headline)))
  754. ;; Create the headline text along with a no-tag version. The
  755. ;; latter is required to remove tags from table of contents.
  756. (full-text (if (functionp org-e-groff-format-headline-function)
  757. ;; User-defined formatting function.
  758. (funcall org-e-groff-format-headline-function
  759. todo todo-type priority text tags)
  760. ;; Default formatting.
  761. (concat
  762. (when todo
  763. (format "\\fB%s\\fP " todo))
  764. (when priority (format " [\\#%c] " priority))
  765. text
  766. (when tags
  767. (format " \\fC:%s:\\fP "
  768. (mapconcat 'identity tags ":"))))))
  769. (full-text-no-tag
  770. (if (functionp org-e-groff-format-headline-function)
  771. ;; User-defined formatting function.
  772. (funcall org-e-groff-format-headline-function
  773. todo todo-type priority text nil)
  774. ;; Default formatting.
  775. (concat
  776. (when todo (format "\\fB%s\\fP " todo))
  777. (when priority (format " [\\#%c] " priority))
  778. text)))
  779. ;; Associate some \label to the headline for internal links.
  780. ;; (headline-label
  781. ;; (format "\\label{sec-%s}\n"
  782. ;; (mapconcat 'number-to-string
  783. ;; (org-export-get-headline-number headline info)
  784. ;; "-")))
  785. (headline-label "")
  786. (pre-blanks
  787. (make-string (org-element-property :pre-blank headline) 10)))
  788. (cond
  789. ;; Case 1: This is a footnote section: ignore it.
  790. ((org-element-property :footnote-section-p headline) nil)
  791. ;; Case 2. This is a deep sub-tree: export it as a list item.
  792. ;; Also export as items headlines for which no section format has
  793. ;; been found.
  794. ((or (not section-fmt) (org-export-low-level-p headline info))
  795. ;; Build the real contents of the sub-tree.
  796. (let ((low-level-body
  797. (concat
  798. ;; If the headline is the first sibling, start a list.
  799. (when (org-export-first-sibling-p headline info)
  800. (format "%s\n" (if numberedp ".AL 1\n" ".DL \n")))
  801. ;; Itemize headline
  802. ".LI\n" full-text "\n" headline-label pre-blanks contents)))
  803. ;; If headline is not the last sibling simply return
  804. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
  805. ;; blank line.
  806. (if (not (org-export-last-sibling-p headline info)) low-level-body
  807. (replace-regexp-in-string
  808. "[ \t\n]*\\'"
  809. (concat "\n.LE")
  810. low-level-body))))
  811. ;; Case 3. Standard headline. Export it as a section.
  812. (t
  813. (format section-fmt full-text
  814. (concat headline-label pre-blanks contents))))))
  815. ;;; Horizontal Rule
  816. ;; Not supported
  817. ;;; Inline Babel Call
  818. ;;
  819. ;; Inline Babel Calls are ignored.
  820. ;;; Inline Src Block
  821. (defun org-e-groff-inline-src-block (inline-src-block contents info)
  822. "Transcode an INLINE-SRC-BLOCK element from Org to Groff.
  823. CONTENTS holds the contents of the item. INFO is a plist holding
  824. contextual information."
  825. (let* ((code (org-element-property :value inline-src-block)))
  826. (cond
  827. (org-e-groff-source-highlight
  828. (let* ((tmpdir (if (featurep 'xemacs)
  829. temp-directory
  830. temporary-file-directory))
  831. (in-file (make-temp-name
  832. (expand-file-name "srchilite" tmpdir)))
  833. (out-file (make-temp-name
  834. (expand-file-name "reshilite" tmpdir)))
  835. (org-lang (org-element-property :language inline-src-block))
  836. (lst-lang (cadr (assq (intern org-lang)
  837. org-e-groff-source-highlight-langs)))
  838. (cmd (concat (expand-file-name "source-highlight")
  839. " -s " lst-lang
  840. " -f groff_mm_color "
  841. " -i " in-file
  842. " -o " out-file)))
  843. (if lst-lang
  844. (let ((code-block ""))
  845. (with-temp-file in-file (insert code))
  846. (shell-command cmd)
  847. (setq code-block (org-file-contents out-file))
  848. (delete-file in-file)
  849. (delete-file out-file)
  850. code-block)
  851. (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
  852. code))))
  853. ;; Do not use a special package: transcode it verbatim.
  854. (t
  855. (concat ".DS I\n" "\\fC" code "\\fP\n.DE\n")))))
  856. ;;; Inlinetask
  857. (defun org-e-groff-inlinetask (inlinetask contents info)
  858. "Transcode an INLINETASK element from Org to Groff.
  859. CONTENTS holds the contents of the block. INFO is a plist
  860. holding contextual information."
  861. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  862. (todo (and (plist-get info :with-todo-keywords)
  863. (let ((todo (org-element-property :todo-keyword inlinetask)))
  864. (and todo (org-export-data todo info)))))
  865. (todo-type (org-element-property :todo-type inlinetask))
  866. (tags (and (plist-get info :with-tags)
  867. (org-export-get-tags inlinetask info)))
  868. (priority (and (plist-get info :with-priority)
  869. (org-element-property :priority inlinetask))))
  870. ;; If `org-e-groff-format-inlinetask-function' is provided, call it
  871. ;; with appropriate arguments.
  872. (if (functionp org-e-groff-format-inlinetask-function)
  873. (funcall org-e-groff-format-inlinetask-function
  874. todo todo-type priority title tags contents)
  875. ;; Otherwise, use a default template.
  876. (org-e-groff--wrap-label
  877. inlinetask
  878. (let ((full-title
  879. (concat
  880. (when todo (format "\\fB%s\\fP " todo))
  881. (when priority (format " [\\#%c] " priority))
  882. title
  883. (when tags (format " \\fC:%s:\\fP "
  884. (mapconcat 'identity tags ":"))))))
  885. (format (concat "\n.DS I\n"
  886. "%s\n"
  887. ".sp"
  888. "%s\n"
  889. ".DE")
  890. full-title contents))))))
  891. ;;; Italic
  892. (defun org-e-groff-italic (italic contents info)
  893. "Transcode ITALIC from Org to Groff.
  894. CONTENTS is the text with italic markup. INFO is a plist holding
  895. contextual information."
  896. (org-e-groff--text-markup contents 'italic))
  897. ;;; Item
  898. (defun org-e-groff-item (item contents info)
  899. "Transcode an ITEM element from Org to Groff.
  900. CONTENTS holds the contents of the item. INFO is a plist holding
  901. contextual information."
  902. (let* ((bullet (org-element-property :bullet item))
  903. (type (org-element-property
  904. :type (org-element-property :parent item)))
  905. (checkbox (case (org-element-property :checkbox item)
  906. (on "\\o'\\(sq\\(mu'")
  907. (off "\\(sq")
  908. (trans "\\o'\\(sq\\(mi'")))
  909. (tag (let ((tag (org-element-property :tag item)))
  910. ;; Check-boxes must belong to the tag.
  911. (and tag (format "[%s]"
  912. (concat checkbox
  913. (org-export-data tag info)))))))
  914. (cond
  915. ((or checkbox tag)
  916. (concat ".LI ""\"" (or tag (concat "\\ " checkbox)) "\""
  917. "\n"
  918. (org-trim (or contents " "))))
  919. ((eq type 'ordered)
  920. (concat ".LI"
  921. "\n"
  922. (org-trim (or contents " "))))
  923. (t
  924. (let* ((bullet (org-trim bullet))
  925. (marker (cond ((string= "-" bullet) "\\(em")
  926. ((string= "*" bullet) "\\(bu")
  927. (t "\\(dg"))))
  928. (concat ".LI " marker "\n"
  929. (org-trim (or contents " " ))))))))
  930. ;;; Keyword
  931. (defun org-e-groff-keyword (keyword contents info)
  932. "Transcode a KEYWORD element from Org to Groff.
  933. CONTENTS is nil. INFO is a plist holding contextual information."
  934. (let ((key (org-element-property :key keyword))
  935. (value (org-element-property :value keyword)))
  936. (cond
  937. ((string= key "GROFF") value)
  938. (t nil))))
  939. ;;; Groff Environment
  940. (defun org-e-groff-groff-environment (groff-environment contents info)
  941. "Transcode a GROFF-ENVIRONMENT element from Org to Groff.
  942. CONTENTS is nil. INFO is a plist holding contextual information."
  943. (let ((label (org-element-property :name groff-environment))
  944. (value (org-remove-indentation
  945. (org-element-property :value groff-environment))))
  946. (if (not (org-string-nw-p label)) value
  947. ;; Environment is labelled: label must be within the environment
  948. ;; (otherwise, a reference pointing to that element will count
  949. ;; the section instead).
  950. (with-temp-buffer
  951. (insert value)
  952. (goto-char (point-min))
  953. (forward-line)
  954. (insert (format "%s\n" label))
  955. (buffer-string)))))
  956. ;;; Groff Fragment
  957. (defun org-e-groff-groff-fragment (groff-fragment contents info)
  958. "Transcode a GROFF-FRAGMENT object from Org to Groff.
  959. CONTENTS is nil. INFO is a plist holding contextual information."
  960. (org-element-property :value groff-fragment))
  961. ;;; Line Break
  962. (defun org-e-groff-line-break (line-break contents info)
  963. "Transcode a LINE-BREAK object from Org to Groff.
  964. CONTENTS is nil. INFO is a plist holding contextual information."
  965. ".br\n")
  966. ;;; Link
  967. ;; Inline images just place a call to .PSPIC or .PS/.PE and load the
  968. ;; graph.
  969. (defun org-e-groff-link--inline-image (link info)
  970. "Return Groff code for an inline image.
  971. LINK is the link pointing to the inline image. INFO is a plist
  972. used as a communication channel."
  973. (let* ((parent (org-export-get-parent-element link))
  974. (path (let ((raw-path (org-element-property :path link)))
  975. (if (not (file-name-absolute-p raw-path)) raw-path
  976. (expand-file-name raw-path))))
  977. (attr (read (format "(%s)"
  978. (mapconcat
  979. #'identity
  980. (org-element-property :attr_groff parent)
  981. " "))))
  982. (placement
  983. (case (plist-get attr :position)
  984. ('center "")
  985. ('left "-L")
  986. ('right "-R")
  987. (t "")))
  988. (width (or (plist-get attr :width) ""))
  989. (height (or (plist-get attr :height) ""))
  990. (disable-caption (plist-get attr :disable-caption))
  991. (caption
  992. (org-e-groff--caption/label-string
  993. (org-element-property :caption parent)
  994. (org-element-property :name parent)
  995. info)))
  996. ;; Now clear ATTR from any special keyword and set a default value
  997. ;; if nothing is left. Return proper string.
  998. (concat
  999. (cond
  1000. ((string-match ".\.pic$" path)
  1001. (format "\n.PS\ncopy \"%s\"\n.PE" path))
  1002. (t (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
  1003. placement path width height)))
  1004. (unless disable-caption (format "\n.FG \"%s\"" caption)))))
  1005. (defun org-e-groff-link (link desc info)
  1006. "Transcode a LINK object from Org to Groff.
  1007. DESC is the description part of the link, or the empty string.
  1008. INFO is a plist holding contextual information. See
  1009. `org-export-data'."
  1010. (let* ((type (org-element-property :type link))
  1011. (raw-path (org-element-property :path link))
  1012. ;; Ensure DESC really exists, or set it to nil.
  1013. (desc (and (not (string= desc "")) desc))
  1014. (imagep (org-export-inline-image-p
  1015. link org-e-groff-inline-image-rules))
  1016. (path (cond
  1017. ((member type '("http" "https" "ftp" "mailto"))
  1018. (concat type ":" raw-path))
  1019. ((string= type "file")
  1020. (when (string-match "\\(.+\\)::.+" raw-path)
  1021. (setq raw-path (match-string 1 raw-path)))
  1022. (if (file-name-absolute-p raw-path)
  1023. (concat "file://" (expand-file-name raw-path))
  1024. (concat "file://" raw-path)))
  1025. (t raw-path)))
  1026. protocol)
  1027. (cond
  1028. ;; Image file.
  1029. (imagep (org-e-groff-link--inline-image link info))
  1030. ;; Import groff files.
  1031. ((and (string= type "file")
  1032. (string-match ".\.groff$" raw-path))
  1033. (concat ".so " raw-path "\n"))
  1034. ;; Radio link: transcode target's contents and use them as link's
  1035. ;; description.
  1036. ((string= type "radio")
  1037. (let ((destination (org-export-resolve-radio-link link info)))
  1038. (when destination
  1039. (format "\\fI [%s] \\fP"
  1040. (org-export-solidify-link-text path)))))
  1041. ;; Links pointing to an headline: find destination and build
  1042. ;; appropriate referencing command.
  1043. ((member type '("custom-id" "fuzzy" "id"))
  1044. (let ((destination (if (string= type "fuzzy")
  1045. (org-export-resolve-fuzzy-link link info)
  1046. (org-export-resolve-id-link link info))))
  1047. (case (org-element-type destination)
  1048. ;; Id link points to an external file.
  1049. (plain-text
  1050. (if desc (format "%s \\fBat\\fP \\fIfile://%s\\fP" desc destination)
  1051. (format "\\fI file://%s \\fP" destination)))
  1052. ;; Fuzzy link points nowhere.
  1053. ('nil
  1054. (format org-e-groff-link-with-unknown-path-format
  1055. (or desc
  1056. (org-export-data
  1057. (org-element-property :raw-link link) info))))
  1058. ;; Fuzzy link points to an invisible target.
  1059. (keyword nil)
  1060. ;; LINK points to an headline. If headlines are numbered
  1061. ;; and the link has no description, display headline's
  1062. ;; number. Otherwise, display description or headline's
  1063. ;; title.
  1064. (headline
  1065. (let ((label ""))
  1066. (if (and (plist-get info :section-numbers) (not desc))
  1067. (format "\\fI%s\\fP" label)
  1068. (format "\\fI%s\\fP"
  1069. (or desc
  1070. (org-export-data
  1071. (org-element-property :title destination) info))))))
  1072. ;; Fuzzy link points to a target. Do as above.
  1073. (otherwise
  1074. (let ((path (org-export-solidify-link-text path)))
  1075. (if (not desc) (format "\\fI%s\\fP" path)
  1076. (format "%s \\fBat\\fP \\fI%s\\fP" desc path)))))))
  1077. ;; External link with a description part.
  1078. ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
  1079. ;; External link without a description part.
  1080. (path (format "\\fI%s\\fP" path))
  1081. ;; No path, only description. Try to do something useful.
  1082. (t (format org-e-groff-link-with-unknown-path-format desc)))))
  1083. ;;; Macro
  1084. (defun org-e-groff-macro (macro contents info)
  1085. "Transcode a MACRO element from Org to Groff.
  1086. CONTENTS is nil. INFO is a plist holding contextual information."
  1087. ;; Use available tools.
  1088. (org-export-expand-macro macro info))
  1089. ;;; Paragraph
  1090. (defun org-e-groff-paragraph (paragraph contents info)
  1091. "Transcode a PARAGRAPH element from Org to Groff.
  1092. CONTENTS is the contents of the paragraph, as a string. INFO is
  1093. the plist used as a communication channel."
  1094. (let ((parent (plist-get (nth 1 paragraph) :parent)))
  1095. (when parent
  1096. (let* ((parent-type (car parent))
  1097. (fixed-paragraph "")
  1098. (class (plist-get info :groff-class))
  1099. (class-options (plist-get info :groff-class-options))
  1100. (classes (assoc class org-e-groff-classes))
  1101. (classes-options (car (last classes)))
  1102. (paragraph-option (plist-get classes-options :paragraph)))
  1103. (cond
  1104. ((and (symbolp paragraph-option)
  1105. (fboundp paragraph-option))
  1106. (funcall paragraph-option parent-type parent contents))
  1107. ((and (eq parent-type 'item)
  1108. (plist-get (nth 1 parent) :bullet))
  1109. (setq fixed-paragraph (concat "" contents)))
  1110. ((eq parent-type 'section)
  1111. (setq fixed-paragraph (concat ".P\n" contents)))
  1112. ((eq parent-type 'footnote-definition)
  1113. (setq fixed-paragraph (concat "" contents)))
  1114. (t (setq fixed-paragraph (concat "" contents))))
  1115. fixed-paragraph))))
  1116. ;;; Plain List
  1117. (defun org-e-groff-plain-list (plain-list contents info)
  1118. "Transcode a PLAIN-LIST element from Org to Groff.
  1119. CONTENTS is the contents of the list. INFO is a plist holding
  1120. contextual information."
  1121. (let* ((type (org-element-property :type plain-list))
  1122. (attr (mapconcat #'identity
  1123. (org-element-property :attr_groff plain-list)
  1124. " "))
  1125. (groff-type (cond
  1126. ((eq type 'ordered) ".AL")
  1127. ((eq type 'unordered) ".BL")
  1128. ((eq type 'descriptive) ".VL 2.0i"))))
  1129. (org-e-groff--wrap-label
  1130. plain-list
  1131. (format "%s\n%s\n.LE" groff-type contents))))
  1132. ;;; Plain Text
  1133. (defun org-e-groff-plain-text (text info)
  1134. "Transcode a TEXT string from Org to Groff.
  1135. TEXT is the string to transcode. INFO is a plist holding
  1136. contextual information."
  1137. ;; Protect
  1138. (setq text (replace-regexp-in-string
  1139. "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
  1140. "$\\" text nil t 1))
  1141. ;; Handle quotation marks
  1142. (setq text (org-e-groff--quotation-marks text info))
  1143. (if org-e-groff-special-char
  1144. (dolist (special-char-list org-e-groff-special-char)
  1145. (setq text
  1146. (replace-regexp-in-string (car special-char-list)
  1147. (cdr special-char-list) text))))
  1148. ;; Handle break preservation if required
  1149. (when (plist-get info :preserve-breaks)
  1150. (setq text (replace-regexp-in-string
  1151. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" text)))
  1152. ;; Return value.
  1153. text)
  1154. ;;; Planning
  1155. (defun org-e-groff-planning (planning contents info)
  1156. "Transcode a PLANNING element from Org to Groff.
  1157. CONTENTS is nil. INFO is a plist holding contextual
  1158. information."
  1159. (concat
  1160. (mapconcat
  1161. 'identity
  1162. (delq nil
  1163. (list
  1164. (let ((closed (org-element-property :closed planning)))
  1165. (when closed
  1166. (concat
  1167. (format "\\fR %s \\fP" org-closed-string)
  1168. (format org-e-groff-inactive-timestamp-format
  1169. (org-translate-time closed)))))
  1170. (let ((deadline (org-element-property :deadline planning)))
  1171. (when deadline
  1172. (concat
  1173. (format "\\fB %s \\fP" org-deadline-string)
  1174. (format org-e-groff-active-timestamp-format
  1175. (org-translate-time deadline)))))
  1176. (let ((scheduled (org-element-property :scheduled planning)))
  1177. (when scheduled
  1178. (concat
  1179. (format "\\fR %s \\fP" org-scheduled-string)
  1180. (format org-e-groff-active-timestamp-format
  1181. (org-translate-time scheduled)))))))
  1182. "")
  1183. ""))
  1184. ;;; Property Drawer
  1185. (defun org-e-groff-property-drawer (property-drawer contents info)
  1186. "Transcode a PROPERTY-DRAWER element from Org to Groff.
  1187. CONTENTS is nil. INFO is a plist holding contextual
  1188. information."
  1189. ;; The property drawer isn't exported but we want separating blank
  1190. ;; lines nonetheless.
  1191. "")
  1192. ;;; Quote Block
  1193. (defun org-e-groff-quote-block (quote-block contents info)
  1194. "Transcode a QUOTE-BLOCK element from Org to Groff.
  1195. CONTENTS holds the contents of the block. INFO is a plist
  1196. holding contextual information."
  1197. (org-e-groff--wrap-label
  1198. quote-block
  1199. (format ".DS I\n.I\n%s\n.R\n.DE" contents)))
  1200. ;;; Quote Section
  1201. (defun org-e-groff-quote-section (quote-section contents info)
  1202. "Transcode a QUOTE-SECTION element from Org to Groff.
  1203. CONTENTS is nil. INFO is a plist holding contextual information."
  1204. (let ((value (org-remove-indentation
  1205. (org-element-property :value quote-section))))
  1206. (when value (format ".DS L\n\\fI%s\\fP\n.DE\n" value))))
  1207. ;;; Radio Target
  1208. (defun org-e-groff-radio-target (radio-target text info)
  1209. "Transcode a RADIO-TARGET object from Org to Groff.
  1210. TEXT is the text of the target. INFO is a plist holding
  1211. contextual information."
  1212. (format "%s - %s"
  1213. (org-export-solidify-link-text
  1214. (org-element-property :value radio-target))
  1215. text))
  1216. ;;; Section
  1217. (defun org-e-groff-section (section contents info)
  1218. "Transcode a SECTION element from Org to Groff.
  1219. CONTENTS holds the contents of the section. INFO is a plist
  1220. holding contextual information."
  1221. contents)
  1222. ;;; Special Block
  1223. (defun org-e-groff-special-block (special-block contents info)
  1224. "Transcode a SPECIAL-BLOCK element from Org to Groff.
  1225. CONTENTS holds the contents of the block. INFO is a plist
  1226. holding contextual information."
  1227. (let ((type (downcase (org-element-property :type special-block))))
  1228. (org-e-groff--wrap-label
  1229. special-block
  1230. (format "%s\n" contents))))
  1231. ;;; Src Block
  1232. (defun org-e-groff-src-block (src-block contents info)
  1233. "Transcode a SRC-BLOCK element from Org to Groff.
  1234. CONTENTS holds the contents of the item. INFO is a plist holding
  1235. contextual information."
  1236. (let* ((lang (org-element-property :language src-block))
  1237. (caption (org-element-property :caption src-block))
  1238. (label (org-element-property :name src-block))
  1239. (code (org-element-property :value src-block))
  1240. (custom-env (and lang
  1241. (cadr (assq (intern lang)
  1242. org-e-groff-custom-lang-environments))))
  1243. (num-start (case (org-element-property :number-lines src-block)
  1244. (continued (org-export-get-loc src-block info))
  1245. (new 0)))
  1246. (retain-labels (org-element-property :retain-labels src-block))
  1247. (attr
  1248. (read (format "(%s)"
  1249. (mapconcat #'identity
  1250. (org-element-property :attr_groff src-block)
  1251. " "))))
  1252. (disable-caption (plist-get attr :disable-caption)))
  1253. (cond
  1254. ;; Case 1. No source fontification.
  1255. ((not org-e-groff-source-highlight)
  1256. (let ((caption-str (org-e-groff--caption/label-string caption label info)))
  1257. (concat
  1258. (format ".DS I\n\\fC%s\\fP\n.DE\n"
  1259. (org-export-format-code-default src-block info))
  1260. (unless disable-caption (format ".EX \"%s\" " caption-str)))))
  1261. ;; Case 2. Source fontification.
  1262. (org-e-groff-source-highlight
  1263. (let* ((tmpdir (if (featurep 'xemacs)
  1264. temp-directory
  1265. temporary-file-directory))
  1266. (caption-str (org-e-groff--caption/label-string caption label info))
  1267. (in-file (make-temp-name
  1268. (expand-file-name "srchilite" tmpdir)))
  1269. (out-file (make-temp-name
  1270. (expand-file-name "reshilite" tmpdir)))
  1271. (org-lang (org-element-property :language src-block))
  1272. (lst-lang (cadr (assq (intern org-lang)
  1273. org-e-groff-source-highlight-langs)))
  1274. (cmd (concat "source-highlight"
  1275. " -s " lst-lang
  1276. " -f groff_mm_color "
  1277. " -i " in-file
  1278. " -o " out-file)))
  1279. (concat
  1280. (if lst-lang
  1281. (let ((code-block "" ))
  1282. (with-temp-file in-file (insert code))
  1283. (shell-command cmd)
  1284. (setq code-block (org-file-contents out-file))
  1285. (delete-file in-file)
  1286. (delete-file out-file)
  1287. (format "%s\n" code-block))
  1288. (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
  1289. code))
  1290. (unless disable-caption (format ".EX \"%s\" " caption-str))))))))
  1291. ;;; Statistics Cookie
  1292. (defun org-e-groff-statistics-cookie (statistics-cookie contents info)
  1293. "Transcode a STATISTICS-COOKIE object from Org to Groff.
  1294. CONTENTS is nil. INFO is a plist holding contextual information."
  1295. (org-element-property :value statistics-cookie))
  1296. ;;; Strike-Through
  1297. (defun org-e-groff-strike-through (strike-through contents info)
  1298. "Transcode STRIKE-THROUGH from Org to Groff.
  1299. CONTENTS is the text with strike-through markup. INFO is a plist
  1300. holding contextual information."
  1301. (org-e-groff--text-markup contents 'strike-through))
  1302. ;;; Subscript
  1303. (defun org-e-groff-subscript (subscript contents info)
  1304. "Transcode a SUBSCRIPT object from Org to Groff.
  1305. CONTENTS is the contents of the object. INFO is a plist holding
  1306. contextual information."
  1307. (format "\\d\\s-2%s\\s+2\\u" contents))
  1308. ;;; Superscript "^_%s$
  1309. (defun org-e-groff-superscript (superscript contents info)
  1310. "Transcode a SUPERSCRIPT object from Org to Groff.
  1311. CONTENTS is the contents of the object. INFO is a plist holding
  1312. contextual information."
  1313. (format "\\u\\s-2%s\\s+2\\d" contents))
  1314. ;;; Table
  1315. ;;
  1316. ;; `org-e-groff-table' is the entry point for table transcoding. It
  1317. ;; takes care of tables with a "verbatim" attribute. Otherwise, it
  1318. ;; delegates the job to `org-e-groff-table--org-table' function,
  1319. ;; depending of the type of the table.
  1320. ;;
  1321. ;; `org-e-groff-table--align-string' is a subroutine used to build
  1322. ;; alignment string for Org tables.
  1323. (defun org-e-groff-table (table contents info)
  1324. "Transcode a TABLE element from Org to Groff.
  1325. CONTENTS is the contents of the table. INFO is a plist holding
  1326. contextual information."
  1327. (cond
  1328. ;; Case 1: verbatim table.
  1329. ((or org-e-groff-tables-verbatim
  1330. (let ((attr (read (format "(%s)"
  1331. (mapconcat
  1332. #'identity
  1333. (org-element-property :attr_groff table) " ")))))
  1334. (and attr (plist-get attr :verbatim))))
  1335. (format ".DS L\n\\fC%s\\fP\n.DE"
  1336. ;; Re-create table, without affiliated keywords.
  1337. (org-trim
  1338. (org-element-interpret-data
  1339. `(table nil ,@(org-element-contents table))))))
  1340. ;; Case 2: Standard table.
  1341. (t (org-e-groff-table--org-table table contents info))))
  1342. (defun org-e-groff-table--align-string (divider table info)
  1343. "Return an appropriate Groff alignment string.
  1344. TABLE is the considered table. INFO is a plist used as
  1345. a communication channel."
  1346. (let (alignment)
  1347. (org-element-map
  1348. (org-element-map
  1349. table 'table-row
  1350. (lambda (row)
  1351. (and (eq (org-element-property :type row) 'standard) row))
  1352. info 'first-match)
  1353. 'table-cell
  1354. (lambda (cell)
  1355. (let* ((borders (org-export-table-cell-borders cell info))
  1356. (raw-width (org-export-table-cell-width cell info))
  1357. (width-cm (when raw-width (/ raw-width 5)))
  1358. (width (if raw-width (format "w(%dc)"
  1359. (if (< width-cm 1) 1 width-cm)) "")))
  1360. ;; Check left border for the first cell only.
  1361. ;; Alignment is nil on assignment
  1362. (when (and (memq 'left borders) (not alignment))
  1363. (push "|" alignment))
  1364. (push
  1365. (case (org-export-table-cell-alignment cell info)
  1366. (left (concat "l" width divider))
  1367. (right (concat "r" width divider))
  1368. (center (concat "c" width divider)))
  1369. alignment)
  1370. (when (memq 'right borders) (push "|" alignment))))
  1371. info)
  1372. (apply 'concat (reverse alignment))))
  1373. (defun org-e-groff-table--org-table (table contents info)
  1374. "Return appropriate Groff code for an Org table.
  1375. TABLE is the table type element to transcode. CONTENTS is its
  1376. contents, as a string. INFO is a plist used as a communication
  1377. channel.
  1378. This function assumes TABLE has `org' as its `:type' attribute."
  1379. (let* ((label (org-element-property :name table))
  1380. (caption (org-e-groff--caption/label-string
  1381. (org-element-property :caption table) label info))
  1382. (attr (read (format "(%s)"
  1383. (mapconcat #'identity
  1384. (org-element-property :attr_groff table)
  1385. " "))))
  1386. (divider (if (plist-get attr :divider) "|" " "))
  1387. ;; Determine alignment string.
  1388. (alignment (org-e-groff-table--align-string divider table info))
  1389. ;; Extract others display options.
  1390. (lines (org-split-string contents "\n"))
  1391. (attr-list
  1392. (let (result-list)
  1393. (dolist (attr-item
  1394. (list
  1395. (if (plist-get attr :expand)
  1396. "expand" nil)
  1397. (case (plist-get attr :placement)
  1398. ('center "center")
  1399. ('left nil)
  1400. (t
  1401. (if org-e-groff-tables-centered
  1402. "center"
  1403. "")))
  1404. (case (plist-get attr :boxtype)
  1405. ('box "box")
  1406. ('doublebox "doublebox")
  1407. ('allbox "allbox")
  1408. ('none nil)
  1409. (t "box"))))
  1410. (if (not (null attr-item))
  1411. (add-to-list 'result-list attr-item)))
  1412. result-list))
  1413. (title-line (plist-get attr :title-line))
  1414. (disable-caption (plist-get attr :disable-caption))
  1415. (long-cells (plist-get attr :long-cells))
  1416. (table-format
  1417. (concat
  1418. (format "%s"
  1419. (or (car attr-list) ""))
  1420. (or
  1421. (let (output-list)
  1422. (when (cdr attr-list)
  1423. (dolist (attr-item (cdr attr-list))
  1424. (setq output-list (concat output-list
  1425. (format ",%s" attr-item)))))
  1426. output-list) "")))
  1427. (first-line
  1428. (when lines (org-split-string (car lines) "\t"))))
  1429. ;; Prepare the final format string for the table.
  1430. (cond
  1431. ;; Others.
  1432. (lines
  1433. (concat ".TS\n " table-format ";\n"
  1434. (format "%s.\n"
  1435. (let ((final-line ""))
  1436. (when title-line
  1437. (dotimes (i (length first-line))
  1438. (setq final-line (concat final-line "cb" divider))))
  1439. (setq final-line (concat final-line "\n"))
  1440. (if alignment
  1441. (setq final-line (concat final-line alignment))
  1442. (dotimes (i (length first-line))
  1443. (setq final-line (concat final-line "c" divider))))
  1444. final-line))
  1445. (format "%s\n.TE\n"
  1446. (let ((final-line "")
  1447. (long-line ""))
  1448. (dolist (line-item lines)
  1449. (setq long-line "")
  1450. (if long-cells
  1451. (if (string= line-item "_")
  1452. (setq long-line (format "%s\n" line-item))
  1453. ;; else
  1454. (let ((cell-item-list (org-split-string line-item "\t")))
  1455. (dolist (cell-item cell-item-list)
  1456. (cond ((eq cell-item (car (last cell-item-list)))
  1457. (setq long-line
  1458. (concat long-line
  1459. (format "T{\n%s\nT}\t\n" cell-item))))
  1460. (t
  1461. (setq long-line
  1462. (concat long-line
  1463. (format "T{\n%s\nT}\t" cell-item))))))
  1464. long-line)
  1465. (setq final-line (concat final-line long-line)))
  1466. ;; else
  1467. (setq final-line (concat final-line line-item "\n")))) final-line))
  1468. (if (not disable-caption)
  1469. (format ".TB \"%s\"" caption)
  1470. ""))))))
  1471. ;;; Table Cell
  1472. (defun org-e-groff-table-cell (table-cell contents info)
  1473. "Transcode a TABLE-CELL element from Org to Groff
  1474. CONTENTS is the cell contents. INFO is a plist used as
  1475. a communication channel."
  1476. (concat (if (and contents
  1477. org-e-groff-table-scientific-notation
  1478. (string-match orgtbl-exp-regexp contents))
  1479. ;; Use appropriate format string for scientific
  1480. ;; notation.
  1481. (format org-e-groff-table-scientific-notation
  1482. (match-string 1 contents)
  1483. (match-string 2 contents))
  1484. contents)
  1485. (when (org-export-get-next-element table-cell info) "\t")))
  1486. ;;; Table Row
  1487. (defun org-e-groff-table-row (table-row contents info)
  1488. "Transcode a TABLE-ROW element from Org to Groff
  1489. CONTENTS is the contents of the row. INFO is a plist used as
  1490. a communication channel."
  1491. ;; Rules are ignored since table separators are deduced from
  1492. ;; borders of the current row.
  1493. (when (eq (org-element-property :type table-row) 'standard)
  1494. (let* ((attr (mapconcat 'identity
  1495. (org-element-property
  1496. :attr_groff (org-export-get-parent table-row))
  1497. " "))
  1498. ;; TABLE-ROW's borders are extracted from its first cell.
  1499. (borders
  1500. (org-export-table-cell-borders
  1501. (car (org-element-contents table-row)) info)))
  1502. (concat
  1503. ;; Mark "hline" for horizontal lines.
  1504. (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
  1505. contents
  1506. (cond
  1507. ;; When BOOKTABS are activated enforce bottom rule even when
  1508. ;; no hline was specifically marked.
  1509. ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
  1510. ((memq 'below borders) "\n_"))))))
  1511. ;;; Target
  1512. (defun org-e-groff-target (target contents info)
  1513. "Transcode a TARGET object from Org to Groff.
  1514. CONTENTS is nil. INFO is a plist holding contextual
  1515. information."
  1516. (format "\\fI%s\\fP"
  1517. (org-export-solidify-link-text (org-element-property :value target))))
  1518. ;;; Timestamp
  1519. (defun org-e-groff-timestamp (timestamp contents info)
  1520. "Transcode a TIMESTAMP object from Org to Groff.
  1521. CONTENTS is nil. INFO is a plist holding contextual
  1522. information."
  1523. (let ((value (org-translate-time (org-element-property :value timestamp)))
  1524. (type (org-element-property :type timestamp)))
  1525. (cond ((memq type '(active active-range))
  1526. (format org-e-groff-active-timestamp-format value))
  1527. ((memq type '(inactive inactive-range))
  1528. (format org-e-groff-inactive-timestamp-format value))
  1529. (t (format org-e-groff-diary-timestamp-format value)))))
  1530. ;;; Underline
  1531. (defun org-e-groff-underline (underline contents info)
  1532. "Transcode UNDERLINE from Org to Groff.
  1533. CONTENTS is the text with underline markup. INFO is a plist
  1534. holding contextual information."
  1535. (org-e-groff--text-markup contents 'underline))
  1536. ;;; Verbatim
  1537. (defun org-e-groff-verbatim (verbatim contents info)
  1538. "Transcode a VERBATIM object from Org to Groff.
  1539. CONTENTS is nil. INFO is a plist used as a communication
  1540. channel."
  1541. (org-e-groff--text-markup (org-element-property :value verbatim) 'verbatim))
  1542. ;;; Verse Block
  1543. (defun org-e-groff-verse-block (verse-block contents info)
  1544. "Transcode a VERSE-BLOCK element from Org to Groff.
  1545. CONTENTS is verse block contents. INFO is a plist holding
  1546. contextual information."
  1547. (format ".DS C\n.ft HI\n%s\n.ft\n.DE" contents))
  1548. ;;; Interactive functions
  1549. (defun org-e-groff-export-to-groff
  1550. (&optional subtreep visible-only body-only ext-plist pub-dir)
  1551. "Export current buffer to a Groff file.
  1552. If narrowing is active in the current buffer, only export its
  1553. narrowed part.
  1554. If a region is active, export that region.
  1555. When optional argument SUBTREEP is non-nil, export the sub-tree
  1556. at point, extracting information from the headline properties
  1557. first.
  1558. When optional argument VISIBLE-ONLY is non-nil, don't export
  1559. contents of hidden elements.
  1560. EXT-PLIST, when provided, is a property list with external
  1561. parameters overriding Org default settings, but still inferior to
  1562. file-local settings.
  1563. When optional argument PUB-DIR is set, use it as the publishing
  1564. directory.
  1565. Return output file's name."
  1566. (interactive)
  1567. (setq org-e-groff-registered-references nil)
  1568. (let ((outfile (org-export-output-file-name ".groff" subtreep pub-dir)))
  1569. (org-export-to-file
  1570. 'e-groff outfile subtreep visible-only body-only ext-plist)))
  1571. (defun org-e-groff-export-to-pdf
  1572. (&optional subtreep visible-only body-only ext-plist pub-dir)
  1573. "Export current buffer to Groff then process through to PDF.
  1574. If narrowing is active in the current buffer, only export its
  1575. narrowed part.
  1576. If a region is active, export that region.
  1577. When optional argument SUBTREEP is non-nil, export the sub-tree
  1578. at point, extracting information from the headline properties
  1579. first.
  1580. When optional argument VISIBLE-ONLY is non-nil, don't export
  1581. contents of hidden elements.
  1582. EXT-PLIST, when provided, is a property list with external
  1583. parameters overriding Org default settings, but still inferior to
  1584. file-local settings.
  1585. When optional argument PUB-DIR is set, use it as the publishing
  1586. directory.
  1587. Return PDF file's name."
  1588. (interactive)
  1589. (org-e-groff-compile
  1590. (org-e-groff-export-to-groff
  1591. subtreep visible-only body-only ext-plist pub-dir)))
  1592. (defun org-e-groff-compile (grofffile)
  1593. "Compile a Groff file.
  1594. GROFFFILE is the name of the file being compiled. Processing is
  1595. done through the command specified in `org-e-groff-pdf-process'.
  1596. Return PDF file name or an error if it couldn't be produced."
  1597. (let* ((wconfig (current-window-configuration))
  1598. (grofffile (file-truename grofffile))
  1599. (base (file-name-sans-extension grofffile))
  1600. errors)
  1601. (message (format "Processing Groff file %s ..." grofffile))
  1602. (unwind-protect
  1603. (progn
  1604. (cond
  1605. ;; A function is provided: Apply it.
  1606. ((functionp org-e-groff-pdf-process)
  1607. (funcall org-e-groff-pdf-process (shell-quote-argument grofffile)))
  1608. ;; A list is provided: Replace %b, %f and %o with
  1609. ;; appropriate values in each command before applying it.
  1610. ;; Output is redirected to "*Org PDF Groff Output*" buffer.
  1611. ((consp org-e-groff-pdf-process)
  1612. (let* ((out-dir (or (file-name-directory grofffile) "./"))
  1613. (outbuf (get-buffer-create "*Org PDF Groff Output*")))
  1614. (mapc
  1615. (lambda (command)
  1616. (shell-command
  1617. (replace-regexp-in-string
  1618. "%b" (shell-quote-argument base)
  1619. (replace-regexp-in-string
  1620. "%f" (shell-quote-argument grofffile)
  1621. (replace-regexp-in-string
  1622. "%o" (shell-quote-argument out-dir) command t t)
  1623. t t) t t)
  1624. outbuf))
  1625. org-e-groff-pdf-process)
  1626. ;; Collect standard errors from output buffer.
  1627. (setq errors (org-e-groff-collect-errors outbuf))))
  1628. (t (error "No valid command to process to PDF")))
  1629. (let ((pdffile (concat base ".pdf")))
  1630. ;; Check for process failure. Provide collected errors if
  1631. ;; possible.
  1632. (if (not (file-exists-p pdffile))
  1633. (error (concat (format "PDF file %s wasn't produced" pdffile)
  1634. (when errors (concat ": " errors))))
  1635. ;; Else remove log files, when specified, and signal end of
  1636. ;; process to user, along with any error encountered.
  1637. (when org-e-groff-remove-logfiles
  1638. (dolist (ext org-e-groff-logfiles-extensions)
  1639. (let ((file (concat base "." ext)))
  1640. (when (file-exists-p file) (delete-file file)))))
  1641. (message (concat "Process completed"
  1642. (if (not errors) "."
  1643. (concat " with errors: " errors)))))
  1644. ;; Return output file name.
  1645. pdffile))
  1646. (set-window-configuration wconfig))))
  1647. (defun org-e-groff-collect-errors (buffer)
  1648. "Collect some kind of errors from \"groff\" output
  1649. BUFFER is the buffer containing output.
  1650. Return collected error types as a string, or nil if there was
  1651. none."
  1652. (with-current-buffer buffer
  1653. (save-excursion
  1654. (goto-char (point-max))
  1655. ;; Find final run
  1656. nil)))
  1657. (provide 'org-e-groff)
  1658. ;;; org-e-groff.el ends here