org-e-groff.el 76 KB

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