org-e-groff.el 72 KB

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