org-e-groff.el 73 KB

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