org-e-groff.el 73 KB

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