org-e-groff.el 75 KB

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