ox-texinfo.el 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961
  1. ;;; ox-texinfo.el --- Texinfo Back-End for Org Export Engine -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. ;; Author: Jonathan Leech-Pepin <jonathan.leechpepin at gmail dot com>
  4. ;; Maintainer: Nicolas Goaziou <mail@nicolasgoaziou.fr>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs 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. ;; GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;
  19. ;; See Org manual for details.
  20. ;;; Code:
  21. (require 'org-macs)
  22. (org-assert-version)
  23. (require 'cl-lib)
  24. (require 'ox)
  25. (defvar orgtbl-exp-regexp)
  26. ;;; Define Back-End
  27. (org-export-define-backend 'texinfo
  28. '((bold . org-texinfo-bold)
  29. (center-block . org-texinfo-center-block)
  30. (clock . org-texinfo-clock)
  31. (code . org-texinfo-code)
  32. (drawer . org-texinfo-drawer)
  33. (dynamic-block . org-texinfo-dynamic-block)
  34. (entity . org-texinfo-entity)
  35. (example-block . org-texinfo-example-block)
  36. (export-block . org-texinfo-export-block)
  37. (export-snippet . org-texinfo-export-snippet)
  38. (fixed-width . org-texinfo-fixed-width)
  39. (footnote-definition . org-texinfo-footnote-definition)
  40. (footnote-reference . org-texinfo-footnote-reference)
  41. (headline . org-texinfo-headline)
  42. (inline-src-block . org-texinfo-inline-src-block)
  43. (inlinetask . org-texinfo-inlinetask)
  44. (italic . org-texinfo-italic)
  45. (item . org-texinfo-item)
  46. (keyword . org-texinfo-keyword)
  47. (line-break . org-texinfo-line-break)
  48. (link . org-texinfo-link)
  49. (node-property . org-texinfo-node-property)
  50. (paragraph . org-texinfo-paragraph)
  51. (plain-list . org-texinfo-plain-list)
  52. (plain-text . org-texinfo-plain-text)
  53. (planning . org-texinfo-planning)
  54. (property-drawer . org-texinfo-property-drawer)
  55. (quote-block . org-texinfo-quote-block)
  56. (radio-target . org-texinfo-radio-target)
  57. (section . org-texinfo-section)
  58. (special-block . org-texinfo-special-block)
  59. (src-block . org-texinfo-src-block)
  60. (statistics-cookie . org-texinfo-statistics-cookie)
  61. (strike-through . org-texinfo-strike-through)
  62. (subscript . org-texinfo-subscript)
  63. (superscript . org-texinfo-superscript)
  64. (table . org-texinfo-table)
  65. (table-cell . org-texinfo-table-cell)
  66. (table-row . org-texinfo-table-row)
  67. (target . org-texinfo-target)
  68. (template . org-texinfo-template)
  69. (timestamp . org-texinfo-timestamp)
  70. (underline . org-texinfo-underline)
  71. (verbatim . org-texinfo-verbatim)
  72. (verse-block . org-texinfo-verse-block))
  73. :filters-alist
  74. '((:filter-headline . org-texinfo--filter-section-blank-lines)
  75. (:filter-parse-tree . (org-texinfo--normalize-headlines
  76. org-texinfo--separate-definitions))
  77. (:filter-section . org-texinfo--filter-section-blank-lines)
  78. (:filter-final-output . org-texinfo--untabify))
  79. :menu-entry
  80. '(?i "Export to Texinfo"
  81. ((?t "As TEXI file" org-texinfo-export-to-texinfo)
  82. (?i "As INFO file" org-texinfo-export-to-info)
  83. (?o "As INFO file and open"
  84. (lambda (a s v b)
  85. (if a (org-texinfo-export-to-info t s v b)
  86. (org-open-file (org-texinfo-export-to-info nil s v b)))))))
  87. :options-alist
  88. '((:texinfo-filename "TEXINFO_FILENAME" nil nil t)
  89. (:texinfo-class "TEXINFO_CLASS" nil org-texinfo-default-class t)
  90. (:texinfo-header "TEXINFO_HEADER" nil nil newline)
  91. (:texinfo-post-header "TEXINFO_POST_HEADER" nil nil newline)
  92. (:subtitle "SUBTITLE" nil nil parse)
  93. (:subauthor "SUBAUTHOR" nil nil newline)
  94. (:texinfo-dircat "TEXINFO_DIR_CATEGORY" nil nil t)
  95. (:texinfo-dirtitle "TEXINFO_DIR_TITLE" nil nil t)
  96. (:texinfo-dirdesc "TEXINFO_DIR_DESC" nil nil t)
  97. (:texinfo-printed-title "TEXINFO_PRINTED_TITLE" nil nil t)
  98. ;; Other variables.
  99. (:texinfo-classes nil nil org-texinfo-classes)
  100. (:texinfo-format-headline-function nil nil org-texinfo-format-headline-function)
  101. (:texinfo-node-description-column nil nil org-texinfo-node-description-column)
  102. (:texinfo-active-timestamp-format nil nil org-texinfo-active-timestamp-format)
  103. (:texinfo-inactive-timestamp-format nil nil org-texinfo-inactive-timestamp-format)
  104. (:texinfo-diary-timestamp-format nil nil org-texinfo-diary-timestamp-format)
  105. (:texinfo-link-with-unknown-path-format nil nil org-texinfo-link-with-unknown-path-format)
  106. (:texinfo-tables-verbatim nil nil org-texinfo-tables-verbatim)
  107. (:texinfo-table-scientific-notation nil nil org-texinfo-table-scientific-notation)
  108. (:texinfo-table-default-markup nil nil org-texinfo-table-default-markup)
  109. (:texinfo-text-markup-alist nil nil org-texinfo-text-markup-alist)
  110. (:texinfo-format-drawer-function nil nil org-texinfo-format-drawer-function)
  111. (:texinfo-format-inlinetask-function nil nil org-texinfo-format-inlinetask-function)
  112. (:texinfo-compact-itemx nil "compact-itemx" org-texinfo-compact-itemx)))
  113. ;;; User Configurable Variables
  114. (defgroup org-export-texinfo nil
  115. "Options for exporting Org mode files to Texinfo."
  116. :tag "Org Export Texinfo"
  117. :version "24.4"
  118. :package-version '(Org . "8.0")
  119. :group 'org-export)
  120. ;;;; Preamble
  121. (defcustom org-texinfo-coding-system nil
  122. "Default document encoding for Texinfo output.
  123. If nil it will default to `buffer-file-coding-system'."
  124. :group 'org-export-texinfo
  125. :type 'coding-system)
  126. (defcustom org-texinfo-default-class "info"
  127. "The default Texinfo class."
  128. :group 'org-export-texinfo
  129. :type '(string :tag "Texinfo class"))
  130. (defcustom org-texinfo-classes
  131. '(("info"
  132. "@documentencoding AUTO\n@documentlanguage AUTO"
  133. ("@chapter %s" "@unnumbered %s" "@chapheading %s" "@appendix %s")
  134. ("@section %s" "@unnumberedsec %s" "@heading %s" "@appendixsec %s")
  135. ("@subsection %s" "@unnumberedsubsec %s" "@subheading %s"
  136. "@appendixsubsec %s")
  137. ("@subsubsection %s" "@unnumberedsubsubsec %s" "@subsubheading %s"
  138. "@appendixsubsubsec %s")))
  139. "Alist of Texinfo classes and associated header and structure.
  140. If #+TEXINFO_CLASS is set in the buffer, use its value and the
  141. associated information. Here is the structure of a class
  142. definition:
  143. (class-name
  144. header-string
  145. (numbered-1 unnumbered-1 unnumbered-no-toc-1 appendix-1)
  146. (numbered-2 unnumbered-2 unnumbered-no-toc-2 appendix-2)
  147. ...)
  148. The header string
  149. -----------------
  150. The header string is inserted in the header of the generated
  151. document, right after \"@setfilename\" and \"@settitle\"
  152. commands.
  153. If it contains the special string
  154. \"@documentencoding AUTO\"
  155. \"AUTO\" will be replaced with an appropriate coding system. See
  156. `org-texinfo-coding-system' for more information. Likewise, if
  157. the string contains the special string
  158. \"@documentlanguage AUTO\"
  159. \"AUTO\" will be replaced with the language defined in the
  160. buffer, through #+LANGUAGE keyword, or globally, with
  161. `org-export-default-language', which see.
  162. The sectioning structure
  163. ------------------------
  164. The sectioning structure of the class is given by the elements
  165. following the header string. For each sectioning level, a number
  166. of strings is specified. A %s formatter is mandatory in each
  167. section string and will be replaced by the title of the section."
  168. :group 'org-export-texinfo
  169. :version "27.1"
  170. :package-version '(Org . "9.2")
  171. :type '(repeat
  172. (list (string :tag "Texinfo class")
  173. (string :tag "Texinfo header")
  174. (repeat :tag "Levels" :inline t
  175. (choice
  176. (list :tag "Heading"
  177. (string :tag " numbered")
  178. (string :tag " unnumbered")
  179. (string :tag "unnumbered-no-toc")
  180. (string :tag " appendix")))))))
  181. ;;;; Headline
  182. (defcustom org-texinfo-format-headline-function
  183. 'org-texinfo-format-headline-default-function
  184. "Function to format headline text.
  185. This function will be called with 5 arguments:
  186. TODO the todo keyword (string or nil).
  187. TODO-TYPE the type of todo (symbol: `todo', `done', nil)
  188. PRIORITY the priority of the headline (integer or nil)
  189. TEXT the main headline text (string).
  190. TAGS the tags as a list of strings (list of strings or nil).
  191. The function result will be used in the section format string."
  192. :group 'org-export-texinfo
  193. :type 'function
  194. :version "26.1"
  195. :package-version '(Org . "8.3"))
  196. ;;;; Node listing (menu)
  197. (defcustom org-texinfo-node-description-column 32
  198. "Column at which to start the description in the node listings.
  199. If a node title is greater than this length, the description will
  200. be placed after the end of the title."
  201. :group 'org-export-texinfo
  202. :type 'integer)
  203. ;;;; Timestamps
  204. (defcustom org-texinfo-active-timestamp-format "@emph{%s}"
  205. "A printf format string to be applied to active timestamps."
  206. :group 'org-export-texinfo
  207. :type 'string)
  208. (defcustom org-texinfo-inactive-timestamp-format "@emph{%s}"
  209. "A printf format string to be applied to inactive timestamps."
  210. :group 'org-export-texinfo
  211. :type 'string)
  212. (defcustom org-texinfo-diary-timestamp-format "@emph{%s}"
  213. "A printf format string to be applied to diary timestamps."
  214. :group 'org-export-texinfo
  215. :type 'string)
  216. ;;;; Links
  217. (defcustom org-texinfo-link-with-unknown-path-format "@indicateurl{%s}"
  218. "Format string for links with unknown path type."
  219. :group 'org-export-texinfo
  220. :type 'string)
  221. ;;;; Tables
  222. (defcustom org-texinfo-tables-verbatim nil
  223. "When non-nil, tables are exported verbatim."
  224. :group 'org-export-texinfo
  225. :type 'boolean)
  226. (defcustom org-texinfo-table-scientific-notation nil
  227. "Format string to display numbers in scientific notation.
  228. The format should have \"%s\" twice, for mantissa and exponent
  229. \(i.e. \"%s\\\\times10^{%s}\").
  230. When nil, no transformation is made."
  231. :group 'org-export-texinfo
  232. :type '(choice
  233. (string :tag "Format string")
  234. (const :tag "No formatting" nil)))
  235. (defcustom org-texinfo-table-default-markup "@asis"
  236. "Default markup for first column in two-column tables.
  237. This should an indicating command, e.g., \"@code\", \"@kbd\" or
  238. \"@samp\".
  239. It can be overridden locally using the \":indic\" attribute."
  240. :group 'org-export-texinfo
  241. :type 'string
  242. :version "26.1"
  243. :package-version '(Org . "9.1")
  244. :safe #'stringp)
  245. ;;;; Text markup
  246. (defcustom org-texinfo-text-markup-alist '((bold . "@strong{%s}")
  247. (code . code)
  248. (italic . "@emph{%s}")
  249. (verbatim . samp))
  250. "Alist of Texinfo expressions to convert text markup.
  251. The key must be a symbol among `bold', `code', `italic',
  252. `strike-through', `underscore' and `verbatim'. The value is
  253. a formatting string to wrap fontified text with.
  254. Value can also be set to the following symbols: `verb', `samp'
  255. and `code'. With the first one, Org uses \"@verb\" to create
  256. a format string and selects a delimiter character that isn't in
  257. the string. For the other two, Org uses \"@samp\" or \"@code\"
  258. to typeset and protects special characters.
  259. When no association is found for a given markup, text is returned
  260. as-is."
  261. :group 'org-export-texinfo
  262. :version "26.1"
  263. :package-version '(Org . "9.1")
  264. :type 'alist
  265. :options '(bold code italic strike-through underscore verbatim))
  266. ;;;; Drawers
  267. (defcustom org-texinfo-format-drawer-function (lambda (_name contents) contents)
  268. "Function called to format a drawer in Texinfo code.
  269. The function must accept two parameters:
  270. NAME the drawer name, like \"LOGBOOK\"
  271. CONTENTS the contents of the drawer.
  272. The function should return the string to be exported.
  273. The default function simply returns the value of CONTENTS."
  274. :group 'org-export-texinfo
  275. :version "24.4"
  276. :package-version '(Org . "8.2")
  277. :type 'function)
  278. ;;;; Inlinetasks
  279. (defcustom org-texinfo-format-inlinetask-function
  280. 'org-texinfo-format-inlinetask-default-function
  281. "Function called to format an inlinetask in Texinfo code.
  282. The function must accept six parameters:
  283. TODO the todo keyword, as a string
  284. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  285. PRIORITY the inlinetask priority, as a string
  286. NAME the inlinetask name, as a string.
  287. TAGS the inlinetask tags, as a list of strings.
  288. CONTENTS the contents of the inlinetask, as a string.
  289. The function should return the string to be exported."
  290. :group 'org-export-texinfo
  291. :type 'function)
  292. ;;;; Itemx
  293. (defcustom org-texinfo-compact-itemx nil
  294. "Non-nil means certain items in description list become `@itemx'.
  295. If this is non-nil and an item in a description list has no
  296. body but is followed by another item, then the second item is
  297. transcoded to `@itemx'. See info node `(org)Plain lists in
  298. Texinfo export' for how to enable this for individual lists."
  299. :package-version '(Org . "9.6")
  300. :group 'org-export-texinfo
  301. :type 'boolean
  302. :safe t)
  303. ;;;; Compilation
  304. (defcustom org-texinfo-info-process '("makeinfo --no-split %f")
  305. "Commands to process a Texinfo file to an INFO file.
  306. This is a list of strings, each of them will be given to the
  307. shell as a command. %f in the command will be replaced by the
  308. relative file name, %F by the absolute file name, %b by the file
  309. base name (i.e. without directory and extension parts), %o by the
  310. base directory of the file and %O by the absolute file name of
  311. the output file."
  312. :group 'org-export-texinfo
  313. :version "26.1"
  314. :package-version '(Org . "9.1")
  315. :type '(repeat :tag "Shell command sequence"
  316. (string :tag "Shell command")))
  317. (defcustom org-texinfo-logfiles-extensions
  318. '("aux" "toc" "cp" "fn" "ky" "pg" "tp" "vr")
  319. "The list of file extensions to consider as Texinfo logfiles.
  320. The logfiles will be remove if `org-texinfo-remove-logfiles' is
  321. non-nil."
  322. :group 'org-export-texinfo
  323. :type '(repeat (string :tag "Extension")))
  324. (defcustom org-texinfo-remove-logfiles t
  325. "Non-nil means remove the logfiles produced by compiling a Texinfo file.
  326. By default, logfiles are files with these extensions: .aux, .toc,
  327. .cp, .fn, .ky, .pg and .tp. To define the set of logfiles to remove,
  328. set `org-texinfo-logfiles-extensions'."
  329. :group 'org-export-latex
  330. :type 'boolean)
  331. ;;; Constants
  332. (defconst org-texinfo-max-toc-depth 4
  333. "Maximum depth for creation of detailed menu listings.
  334. Beyond this depth, Texinfo will not recognize the nodes and will
  335. cause errors. Left as a constant in case this value ever
  336. changes.")
  337. (defconst org-texinfo-supported-coding-systems
  338. '("US-ASCII" "UTF-8" "ISO-8859-15" "ISO-8859-1" "ISO-8859-2" "koi8-r" "koi8-u")
  339. "List of coding systems supported by Texinfo, as strings.
  340. Specified coding system will be matched against these strings.
  341. If two strings share the same prefix (e.g. \"ISO-8859-1\" and
  342. \"ISO-8859-15\"), the most specific one has to be listed first.")
  343. (defconst org-texinfo-inline-image-rules
  344. (list (cons "file"
  345. (regexp-opt '("eps" "pdf" "png" "jpg" "jpeg" "gif" "svg"))))
  346. "Rules characterizing image files that can be inlined.")
  347. (defvar org-texinfo--quoted-keys-regexp
  348. (regexp-opt '("BS" "TAB" "RET" "ESC" "SPC" "DEL"
  349. "LFD" "DELETE" "SHIFT" "Ctrl" "Meta" "Alt"
  350. "Cmd" "Super" "UP" "LEFT" "RIGHT" "DOWN")
  351. 'words)
  352. "Regexp matching keys that have to be quoted using @key{KEY}.")
  353. (defconst org-texinfo--definition-command-alist
  354. '(("deffn Command" . "Command")
  355. ("defun" . "Function")
  356. ("defmac" . "Macro")
  357. ("defspec" . "Special Form")
  358. ("defvar" . "Variable")
  359. ("defopt" . "User Option")
  360. (nil . "Key"))
  361. "Alist mapping Texinfo definition commands to output in Info files.")
  362. (defconst org-texinfo--definition-command-regexp
  363. (format "\\`%s: \\(.+\\)"
  364. (regexp-opt
  365. (delq nil (mapcar #'cdr org-texinfo--definition-command-alist))
  366. t))
  367. "Regexp used to match definition commands in descriptive lists.")
  368. ;;; Internal Functions
  369. (defun org-texinfo--untabify (s _backend _info)
  370. "Remove TAB characters in string S."
  371. (replace-regexp-in-string "\t" (make-string tab-width ?\s) s))
  372. (defun org-texinfo--filter-section-blank-lines (headline _backend _info)
  373. "Filter controlling number of blank lines after a section."
  374. (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" "\n\n" headline))
  375. (defun org-texinfo--normalize-headlines (tree _backend info)
  376. "Normalize headlines in TREE.
  377. BACK-END is the symbol specifying back-end used for export.
  378. INFO is a plist used as a communication channel.
  379. Make sure every headline in TREE contains a section, since those
  380. are required to install a menu. Also put exactly one blank line
  381. at the end of each section.
  382. Return new tree."
  383. (org-element-map tree 'headline
  384. (lambda (hl)
  385. (org-element-put-property hl :post-blank 1)
  386. (let ((contents (org-element-contents hl)))
  387. (when contents
  388. (let ((first (org-element-map contents '(headline section)
  389. #'identity info t)))
  390. (unless (eq (org-element-type first) 'section)
  391. (apply #'org-element-set-contents
  392. hl
  393. (cons `(section (:parent ,hl)) contents)))))))
  394. info)
  395. tree)
  396. (defun org-texinfo--find-verb-separator (s)
  397. "Return a character not used in string S.
  398. This is used to choose a separator for constructs like \\verb."
  399. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  400. (cl-loop for c across ll
  401. when (not (string-match (regexp-quote (char-to-string c)) s))
  402. return (char-to-string c))))
  403. (defun org-texinfo--text-markup (text markup _info)
  404. "Format TEXT depending on MARKUP text markup.
  405. INFO is a plist used as a communication channel. See
  406. `org-texinfo-text-markup-alist' for details."
  407. (pcase (cdr (assq markup org-texinfo-text-markup-alist))
  408. (`nil text) ;no markup: return raw text
  409. (`code (format "@code{%s}" (org-texinfo--sanitize-content text)))
  410. (`samp (format "@samp{%s}" (org-texinfo--sanitize-content text)))
  411. (`verb
  412. (let ((separator (org-texinfo--find-verb-separator text)))
  413. (format "@verb{%s%s%s}" separator text separator)))
  414. ;; Else use format string.
  415. (fmt (format fmt text))))
  416. (defun org-texinfo--get-node (datum info)
  417. "Return node or anchor associated to DATUM.
  418. DATUM is a headline, a radio-target or a target. INFO is a plist
  419. used as a communication channel. The function guarantees the
  420. node or anchor name is unique."
  421. (let ((cache (plist-get info :texinfo-node-cache)))
  422. (or (cdr (assq datum cache))
  423. (let* ((salt 0)
  424. (basename
  425. (org-texinfo--sanitize-node
  426. (pcase (org-element-type datum)
  427. (`headline
  428. (org-texinfo--sanitize-title
  429. (org-export-get-alt-title datum info) info))
  430. (`radio-target
  431. (org-export-data (org-element-contents datum) info))
  432. (`target
  433. (org-element-property :value datum))
  434. (_
  435. (or (org-element-property :name datum)
  436. (org-export-get-reference datum info))))))
  437. (name basename))
  438. ;; Org exports deeper elements before their parents. If two
  439. ;; node names collide -- e.g., they have the same title --
  440. ;; within the same hierarchy, the second one would get the
  441. ;; smaller node name. This is counter-intuitive.
  442. ;; Consequently, we ensure that every parent headline gets
  443. ;; its node beforehand. As a recursive operation, this
  444. ;; achieves the desired effect.
  445. (let ((parent (org-element-lineage datum '(headline))))
  446. (when (and parent (not (assq parent cache)))
  447. (org-texinfo--get-node parent info)
  448. (setq cache (plist-get info :texinfo-node-cache))))
  449. ;; Ensure NAME is unique and not reserved node name "Top",
  450. ;; no matter what case is used.
  451. (while (or (string-equal "Top" (capitalize name))
  452. (rassoc name cache))
  453. (setq name (concat basename (format " (%d)" (cl-incf salt)))))
  454. (plist-put info :texinfo-node-cache (cons (cons datum name) cache))
  455. name))))
  456. (defun org-texinfo--sanitize-node (title)
  457. "Bend string TITLE to node line requirements.
  458. Trim string and collapse multiple whitespace characters as they
  459. are not significant. Replace leading left parenthesis, when
  460. followed by a right parenthesis, with a square bracket. Remove
  461. periods, commas and colons."
  462. (org-trim
  463. (replace-regexp-in-string
  464. "[ \t]+" " "
  465. (replace-regexp-in-string
  466. "[:,.]" ""
  467. (replace-regexp-in-string "\\`(\\(.*?)\\)" "[\\1" title)))))
  468. (defun org-texinfo--sanitize-title (title info)
  469. "Make TITLE suitable as a section name.
  470. TITLE is a string or a secondary string. INFO is the current
  471. export state, as a plist."
  472. (org-export-data-with-backend
  473. title (org-export-toc-entry-backend 'texinfo) info))
  474. (defun org-texinfo--sanitize-content (text)
  475. "Escape special characters in string TEXT.
  476. Special characters are: @ { }"
  477. (replace-regexp-in-string "[@{}]" "@\\&" text))
  478. (defun org-texinfo--wrap-float (value info &optional type label caption short)
  479. "Wrap string VALUE within a @float command.
  480. INFO is the current export state, as a plist. TYPE is float
  481. type, as a string. LABEL is the cross reference label for the
  482. float, as a string. CAPTION and SHORT are, respectively, the
  483. caption and shortcaption used for the float, as secondary
  484. strings (e.g., returned by `org-export-get-caption')."
  485. (let* ((backend
  486. (org-export-toc-entry-backend 'texinfo
  487. (cons 'footnote-reference
  488. (lambda (f c i) (org-export-with-backend 'texinfo f c i)))))
  489. (short-backend
  490. (org-export-toc-entry-backend 'texinfo
  491. '(inline-src-block . ignore)
  492. '(verbatim . ignore)))
  493. (short-str
  494. (if (and short caption)
  495. (format "@shortcaption{%s}\n"
  496. (org-export-data-with-backend short short-backend info))
  497. ""))
  498. (caption-str
  499. (if (or short caption)
  500. (format "@caption{%s}\n"
  501. (org-export-data-with-backend
  502. (or caption short)
  503. (if (equal short-str "") short-backend backend)
  504. info))
  505. "")))
  506. (format "@float %s%s\n%s\n%s%s@end float"
  507. type (if label (concat "," label) "") value caption-str short-str)))
  508. (defun org-texinfo--sectioning-structure (info)
  509. "Return sectioning structure used in the document.
  510. INFO is a plist holding export options."
  511. (let ((class (plist-get info :texinfo-class)))
  512. (pcase (assoc class (plist-get info :texinfo-classes))
  513. (`(,_ ,_ . ,sections) sections)
  514. (_ (user-error "Unknown Texinfo class: %S" class)))))
  515. (defun org-texinfo--separate-definitions (tree _backend info)
  516. "Split up descriptive lists in TREE that contain Texinfo definition commands.
  517. INFO is a plist used as a communication channel.
  518. Return new tree."
  519. (org-element-map tree 'plain-list
  520. (lambda (plain-list)
  521. (when (eq (org-element-property :type plain-list) 'descriptive)
  522. (let ((contents (org-element-contents plain-list))
  523. (items nil))
  524. (dolist (item contents)
  525. (pcase-let ((`(,cmd . ,args) (org-texinfo--match-definition item)))
  526. (cond
  527. (cmd
  528. (when items
  529. (org-texinfo--split-plain-list plain-list (nreverse items))
  530. (setq items nil))
  531. (org-texinfo--split-definition plain-list item cmd args))
  532. (t
  533. (when args
  534. (org-texinfo--massage-key-item plain-list item args info))
  535. (push item items)))))
  536. (unless (org-element-contents plain-list)
  537. (org-element-extract-element plain-list)))))
  538. info)
  539. tree)
  540. (defun org-texinfo--match-definition (item)
  541. "Return a cons-cell if ITEM specifies a Texinfo definition command.
  542. The car is the command and the cdr is its arguments."
  543. (let ((tag (car-safe (org-element-property :tag item))))
  544. (and tag
  545. (stringp tag)
  546. (string-match org-texinfo--definition-command-regexp tag)
  547. (pcase-let*
  548. ((cmd (car (rassoc (match-string-no-properties 1 tag)
  549. org-texinfo--definition-command-alist)))
  550. (`(,cmd ,category)
  551. (and cmd (save-match-data (split-string cmd " "))))
  552. (args (match-string-no-properties 2 tag)))
  553. (cons cmd (if category (concat category " " args) args))))))
  554. (defun org-texinfo--split-definition (plain-list item cmd args)
  555. "Insert a definition command before list PLAIN-LIST.
  556. Replace list item ITEM with a special-block that inherits the
  557. contents of ITEM and whose type and Texinfo attributes are
  558. specified by CMD and ARGS."
  559. (let ((contents (org-element-contents item)))
  560. (org-element-insert-before
  561. (apply #'org-element-create 'special-block
  562. (list :type cmd
  563. :attr_texinfo (list (format ":options %s" args))
  564. :post-blank (if contents 1 0))
  565. (mapc #'org-element-extract-element contents))
  566. plain-list))
  567. (org-element-extract-element item))
  568. (defun org-texinfo--split-plain-list (plain-list items)
  569. "Insert a new plain list before the plain list PLAIN-LIST.
  570. Remove ITEMS from PLAIN-LIST and use them as the contents of the
  571. new plain list."
  572. (org-element-insert-before
  573. (apply #'org-element-create 'plain-list
  574. (list :type 'descriptive
  575. :attr_texinfo (org-element-property :attr_texinfo plain-list)
  576. :post-blank 1)
  577. (mapc #'org-element-extract-element items))
  578. plain-list))
  579. (defun org-texinfo--massage-key-item (plain-list item args info)
  580. "In PLAIN-LIST modify ITEM based on ARGS.
  581. Reformat ITEM's tag property and determine the arguments for the
  582. `@findex' and `@kindex' commands for ITEM and store them in ITEM
  583. using the `:findex' and `:kindex' properties.
  584. If PLAIN-LIST is a description list whose `:compact' attribute is
  585. non-nil and ITEM has no content but is followed by another item,
  586. then store the `@findex' and `@kindex' values in the next item.
  587. If the previous item stored its respective values in this item,
  588. then move them to the next item.
  589. INFO is a plist used as a communication channel."
  590. (let ((key nil)
  591. (cmd nil))
  592. (if (string-match (rx (+ " ")
  593. "(" (group (+ (not (any "()")))) ")"
  594. (* " ")
  595. eos)
  596. args)
  597. (setq key (substring args 0 (match-beginning 0))
  598. cmd (match-string 1 args))
  599. (setq key args))
  600. (org-element-put-property
  601. item :tag
  602. (cons (org-export-raw-string (org-texinfo-kbd-macro key t))
  603. (and cmd `(" (" (code (:value ,cmd :post-blank 0)) ")"))))
  604. (let ((findex (org-element-property :findex item))
  605. (kindex (org-element-property :kindex item))
  606. (next-item (org-export-get-next-element item nil))
  607. (mx (string-prefix-p "M-x " key)))
  608. (when (and (not cmd) mx)
  609. (setq cmd (substring key 4)))
  610. (when (and cmd (not (member cmd findex)))
  611. (setq findex (nconc findex (list cmd))))
  612. (unless mx
  613. (setq kindex (nconc kindex (list key))))
  614. (cond
  615. ((and next-item
  616. (or (plist-get info :texinfo-compact-itemx)
  617. (org-not-nil
  618. (org-export-read-attribute :attr_texinfo plain-list :compact)))
  619. (not (org-element-contents item))
  620. (eq 1 (org-element-property :post-blank item)))
  621. (org-element-put-property next-item :findex findex)
  622. (org-element-put-property next-item :kindex kindex)
  623. (org-element-put-property item :findex nil)
  624. (org-element-put-property item :kindex nil))
  625. (t
  626. (org-element-set-contents
  627. item
  628. (nconc (mapcar (lambda (key) `(keyword (:key "KINDEX" :value ,key))) kindex)
  629. (mapcar (lambda (cmd) `(keyword (:key "FINDEX" :value ,cmd))) findex)
  630. (org-element-contents item))))))))
  631. ;;; Template
  632. (defun org-texinfo-template (contents info)
  633. "Return complete document string after Texinfo conversion.
  634. CONTENTS is the transcoded contents string. INFO is a plist
  635. holding export options."
  636. (let ((title (org-export-data (plist-get info :title) info))
  637. ;; Copying data is the contents of the first headline in
  638. ;; parse tree with a non-nil copying property.
  639. (copying (org-element-map (plist-get info :parse-tree) 'headline
  640. (lambda (hl)
  641. (and (org-not-nil (org-element-property :COPYING hl))
  642. (org-element-contents hl)))
  643. info t)))
  644. (concat
  645. "\\input texinfo @c -*- texinfo -*-\n"
  646. "@c %**start of header\n"
  647. (let ((file (or (org-strip-quotes (plist-get info :texinfo-filename))
  648. (let ((f (plist-get info :output-file)))
  649. (and f (concat (file-name-sans-extension f) ".info"))))))
  650. (and file (format "@setfilename %s\n" file)))
  651. (format "@settitle %s\n" title)
  652. ;; Insert class-defined header.
  653. (org-element-normalize-string
  654. (let ((header (nth 1 (assoc (plist-get info :texinfo-class)
  655. org-texinfo-classes)))
  656. (coding
  657. (catch 'coding-system
  658. (let ((case-fold-search t)
  659. (name (symbol-name (or org-texinfo-coding-system
  660. buffer-file-coding-system))))
  661. (dolist (system org-texinfo-supported-coding-systems "UTF-8")
  662. (when (string-match-p (regexp-quote system) name)
  663. (throw 'coding-system system))))))
  664. (language (plist-get info :language))
  665. (case-fold-search nil))
  666. ;; Auto coding system.
  667. (replace-regexp-in-string
  668. "^@documentencoding \\(AUTO\\)$"
  669. coding
  670. (replace-regexp-in-string
  671. "^@documentlanguage \\(AUTO\\)$" language header t nil 1)
  672. t nil 1)))
  673. ;; Additional header options set by #+TEXINFO_HEADER.
  674. (let ((texinfo-header (plist-get info :texinfo-header)))
  675. (and texinfo-header (org-element-normalize-string texinfo-header)))
  676. "@c %**end of header\n\n"
  677. ;; Additional options set by #+TEXINFO_POST_HEADER.
  678. (let ((texinfo-post-header (plist-get info :texinfo-post-header)))
  679. (and texinfo-post-header
  680. (org-element-normalize-string texinfo-post-header)))
  681. ;; Copying.
  682. (and copying
  683. (format "@copying\n%s@end copying\n\n"
  684. (org-element-normalize-string
  685. (org-export-data copying info))))
  686. ;; Info directory information. Only supply if both title and
  687. ;; category are provided.
  688. (let ((dircat (plist-get info :texinfo-dircat))
  689. (dirtitle
  690. (let ((title (plist-get info :texinfo-dirtitle)))
  691. (and title
  692. (string-match "^\\(?:\\* \\)?\\(.*?\\)\\(\\.\\)?$" title)
  693. (format "* %s." (match-string 1 title))))))
  694. (when (and dircat dirtitle)
  695. (concat "@dircategory " dircat "\n"
  696. "@direntry\n"
  697. (let ((dirdesc
  698. (let ((desc (plist-get info :texinfo-dirdesc)))
  699. (cond ((not desc) nil)
  700. ((string-suffix-p "." desc) desc)
  701. (t (concat desc "."))))))
  702. (if dirdesc (format "%-23s %s" dirtitle dirdesc) dirtitle))
  703. "\n"
  704. "@end direntry\n\n")))
  705. ;; Title
  706. "@finalout\n"
  707. "@titlepage\n"
  708. (when (plist-get info :with-title)
  709. (concat
  710. (format "@title %s\n"
  711. (or (plist-get info :texinfo-printed-title) title ""))
  712. (let ((subtitle (plist-get info :subtitle)))
  713. (when subtitle
  714. (format "@subtitle %s\n"
  715. (org-export-data subtitle info))))))
  716. (when (plist-get info :with-author)
  717. (concat
  718. ;; Primary author.
  719. (let ((author (org-string-nw-p
  720. (org-export-data (plist-get info :author) info)))
  721. (email (and (plist-get info :with-email)
  722. (org-string-nw-p
  723. (org-export-data (plist-get info :email) info)))))
  724. (cond ((and author email)
  725. (format "@author %s (@email{%s})\n" author email))
  726. (author (format "@author %s\n" author))
  727. (email (format "@author @email{%s}\n" email))))
  728. ;; Other authors.
  729. (let ((subauthor (plist-get info :subauthor)))
  730. (and subauthor
  731. (org-element-normalize-string
  732. (replace-regexp-in-string "^" "@author " subauthor))))))
  733. (and copying "@page\n@vskip 0pt plus 1filll\n@insertcopying\n")
  734. "@end titlepage\n\n"
  735. ;; Table of contents.
  736. (and (plist-get info :with-toc) "@contents\n\n")
  737. ;; Configure Top Node when not for TeX. Also include contents
  738. ;; from the first section of the document.
  739. "@ifnottex\n"
  740. "@node Top\n"
  741. (format "@top %s\n" title)
  742. (let* ((first-section
  743. (org-element-map (plist-get info :parse-tree) 'section
  744. #'identity info t '(headline)))
  745. (top-contents
  746. (org-export-data (org-element-contents first-section) info)))
  747. (and (org-string-nw-p top-contents) (concat "\n" top-contents)))
  748. "@end ifnottex\n\n"
  749. ;; Menu.
  750. (org-texinfo-make-menu (plist-get info :parse-tree) info 'master)
  751. "\n"
  752. ;; Document's body.
  753. contents "\n"
  754. ;; Creator.
  755. (and (plist-get info :with-creator)
  756. (concat (plist-get info :creator) "\n"))
  757. ;; Document end.
  758. "@bye")))
  759. ;;; Transcode Functions
  760. ;;;; Bold
  761. (defun org-texinfo-bold (_bold contents info)
  762. "Transcode BOLD from Org to Texinfo.
  763. CONTENTS is the text with bold markup. INFO is a plist holding
  764. contextual information."
  765. (org-texinfo--text-markup contents 'bold info))
  766. ;;;; Center Block
  767. (defun org-texinfo-center-block (_center-block contents _info)
  768. "Transcode a CENTER-BLOCK element from Org to Texinfo.
  769. CONTENTS holds the contents of the block. INFO is a plist used
  770. as a communication channel."
  771. (replace-regexp-in-string "\\(^\\).*?\\S-" "@center " contents nil nil 1))
  772. ;;;; Clock
  773. (defun org-texinfo-clock (clock _contents info)
  774. "Transcode a CLOCK element from Org to Texinfo.
  775. CONTENTS is nil. INFO is a plist holding contextual
  776. information."
  777. (concat
  778. "@noindent"
  779. (format "@strong{%s} " org-clock-string)
  780. (format (plist-get info :texinfo-inactive-timestamp-format)
  781. (concat (org-timestamp-translate (org-element-property :value clock))
  782. (let ((time (org-element-property :duration clock)))
  783. (and time (format " (%s)" time)))))
  784. "@*"))
  785. ;;;; Code
  786. (defun org-texinfo-code (code _contents info)
  787. "Transcode a CODE object from Org to Texinfo.
  788. CONTENTS is nil. INFO is a plist used as a communication
  789. channel."
  790. (org-texinfo--text-markup (org-element-property :value code) 'code info))
  791. ;;;; Drawer
  792. (defun org-texinfo-drawer (drawer contents info)
  793. "Transcode a DRAWER element from Org to Texinfo.
  794. CONTENTS holds the contents of the block. INFO is a plist
  795. holding contextual information."
  796. (let* ((name (org-element-property :drawer-name drawer))
  797. (output (funcall (plist-get info :texinfo-format-drawer-function)
  798. name contents)))
  799. output))
  800. ;;;; Dynamic Block
  801. (defun org-texinfo-dynamic-block (_dynamic-block contents _info)
  802. "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
  803. CONTENTS holds the contents of the block. INFO is a plist
  804. holding contextual information."
  805. contents)
  806. ;;;; Entity
  807. (defun org-texinfo-entity (entity _contents _info)
  808. "Transcode an ENTITY object from Org to Texinfo."
  809. ;; Since there is not specific Texinfo entry in entities, use
  810. ;; Texinfo-specific commands whenever possible, and fallback to
  811. ;; UTF-8 otherwise.
  812. (pcase (org-element-property :name entity)
  813. ("AElig" "@AE{}")
  814. ("aelig" "@ae{}")
  815. ((or "bull" "bullet") "@bullet{}")
  816. ("copy" "@copyright{}")
  817. ("deg" "@textdegree{}")
  818. ((or "dots" "hellip") "@dots{}")
  819. ("equiv" "@equiv{}")
  820. ((or "euro" "EUR") "@euro{}")
  821. ((or "ge" "geq") "@geq{}")
  822. ("laquo" "@guillemetleft{}")
  823. ("iexcl" "@exclamdown{}")
  824. ("imath" "@dotless{i}")
  825. ("iquest" "@questiondown{}")
  826. ("jmath" "@dotless{j}")
  827. ((or "le" "leq") "@leq{}")
  828. ("lsaquo" "@guilsinglleft{}")
  829. ("mdash" "---")
  830. ("minus" "@minus{}")
  831. ("nbsp" "@tie{}")
  832. ("ndash" "--")
  833. ("OElig" "@OE{}")
  834. ("oelig" "@oe{}")
  835. ("ordf" "@ordf{}")
  836. ("ordm" "@ordm{}")
  837. ("pound" "@pound{}")
  838. ("raquo" "@guillemetright{}")
  839. ((or "rArr" "Rightarrow") "@result{}")
  840. ("reg" "@registeredsymbol{}")
  841. ((or "rightarrow" "to" "rarr") "@arrow{}")
  842. ("rsaquo" "@guilsinglright{}")
  843. ("thorn" "@th{}")
  844. ("THORN" "@TH{}")
  845. ((and (pred (string-prefix-p "_")) name) ;spacing entities
  846. (format "@w{%s}" (substring name 1)))
  847. (_ (org-element-property :utf-8 entity))))
  848. ;;;; Example Block
  849. (defun org-texinfo-example-block (example-block _contents info)
  850. "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
  851. CONTENTS is nil. INFO is a plist holding contextual
  852. information."
  853. (format "@example\n%s@end example"
  854. (org-texinfo--sanitize-content
  855. (org-export-format-code-default example-block info))))
  856. ;;; Export Block
  857. (defun org-texinfo-export-block (export-block _contents _info)
  858. "Transcode a EXPORT-BLOCK element from Org to Texinfo.
  859. CONTENTS is nil. INFO is a plist holding contextual information."
  860. (when (string= (org-element-property :type export-block) "TEXINFO")
  861. (org-remove-indentation (org-element-property :value export-block))))
  862. ;;; Export Snippet
  863. (defun org-texinfo-export-snippet (export-snippet _contents _info)
  864. "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
  865. CONTENTS is nil. INFO is a plist holding contextual information."
  866. (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
  867. (org-element-property :value export-snippet)))
  868. ;;;; Fixed Width
  869. (defun org-texinfo-fixed-width (fixed-width _contents _info)
  870. "Transcode a FIXED-WIDTH element from Org to Texinfo.
  871. CONTENTS is nil. INFO is a plist holding contextual information."
  872. (format "@example\n%s\n@end example"
  873. (org-remove-indentation
  874. (org-texinfo--sanitize-content
  875. (org-element-property :value fixed-width)))))
  876. ;;;; Footnote Reference
  877. (defun org-texinfo-footnote-reference (footnote _contents info)
  878. "Create a footnote reference for FOOTNOTE.
  879. FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
  880. plist holding contextual information."
  881. (let* ((contents (org-export-get-footnote-definition footnote info))
  882. (data (org-export-data contents info)))
  883. (format "@footnote{%s}"
  884. ;; It is invalid to close a footnote on a line starting
  885. ;; with "@end". As a safety net, we leave a newline
  886. ;; character before the closing brace. However, when the
  887. ;; footnote ends with a paragraph, it is visually pleasing
  888. ;; to move the brace right after its end.
  889. (if (eq 'paragraph (org-element-type (org-last contents)))
  890. (org-trim data)
  891. data))))
  892. ;;;; Headline
  893. (defun org-texinfo-headline (headline contents info)
  894. "Transcode a HEADLINE element from Org to Texinfo.
  895. CONTENTS holds the contents of the headline. INFO is a plist
  896. holding contextual information."
  897. (cond
  898. ((org-element-property :footnote-section-p headline) nil)
  899. ((org-not-nil (org-export-get-node-property :COPYING headline t)) nil)
  900. (t
  901. (let* ((index (let ((i (org-export-get-node-property :INDEX headline t)))
  902. (and (member i '("cp" "fn" "ky" "pg" "tp" "vr")) i)))
  903. (numbered? (org-export-numbered-headline-p headline info))
  904. (notoc? (org-export-excluded-from-toc-p headline info))
  905. (command
  906. (and
  907. (not (org-export-low-level-p headline info))
  908. (let ((sections (org-texinfo--sectioning-structure info)))
  909. (pcase (nth (1- (org-export-get-relative-level headline info))
  910. sections)
  911. (`(,numbered ,unnumbered ,unnumbered-no-toc ,appendix)
  912. (cond
  913. ((org-not-nil
  914. (org-export-get-node-property :APPENDIX headline t))
  915. appendix)
  916. (numbered? numbered)
  917. (index unnumbered)
  918. (notoc? unnumbered-no-toc)
  919. (t unnumbered)))
  920. (`nil nil)
  921. (_ (user-error "Invalid Texinfo class specification: %S"
  922. (plist-get info :texinfo-class)))))))
  923. (todo
  924. (and (plist-get info :with-todo-keywords)
  925. (let ((todo (org-element-property :todo-keyword headline)))
  926. (and todo (org-export-data todo info)))))
  927. (todo-type (and todo (org-element-property :todo-type headline)))
  928. (tags (and (plist-get info :with-tags)
  929. (org-export-get-tags headline info)))
  930. (priority (and (plist-get info :with-priority)
  931. (org-element-property :priority headline)))
  932. (text (org-texinfo--sanitize-title
  933. (org-element-property :title headline) info))
  934. (full-text
  935. (funcall (plist-get info :texinfo-format-headline-function)
  936. todo todo-type priority text tags))
  937. (contents
  938. (concat "\n"
  939. (if (org-string-nw-p contents) (concat "\n" contents) "")
  940. (and index (format "\n@printindex %s\n" index))))
  941. (node (org-texinfo--get-node headline info)))
  942. (if (not command)
  943. (concat (and (org-export-first-sibling-p headline info)
  944. (format "@%s\n" (if numbered? 'enumerate 'itemize)))
  945. (format "@item\n@anchor{%s}%s\n" node full-text)
  946. contents
  947. (if (org-export-last-sibling-p headline info)
  948. (format "@end %s" (if numbered? 'enumerate 'itemize))
  949. "\n"))
  950. (concat
  951. ;; Even if HEADLINE is using @subheading and al., leave an
  952. ;; anchor so cross-references in the Org document still work.
  953. (format (if notoc? "@anchor{%s}\n" "@node %s\n") node)
  954. (format command full-text)
  955. contents))))))
  956. (defun org-texinfo-format-headline-default-function
  957. (todo _todo-type priority text tags)
  958. "Default format function for a headline.
  959. See `org-texinfo-format-headline-function' for details."
  960. (concat (and todo (format "@strong{%s} " todo))
  961. (and priority (format "@emph{#%s} " priority))
  962. text
  963. (and tags (concat " " (org-make-tag-string tags)))))
  964. ;;;; Inline Src Block
  965. (defun org-texinfo-inline-src-block (inline-src-block _contents _info)
  966. "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
  967. CONTENTS holds the contents of the item. INFO is a plist holding
  968. contextual information."
  969. (format "@code{%s}"
  970. (org-texinfo--sanitize-content
  971. (org-element-property :value inline-src-block))))
  972. ;;;; Inlinetask
  973. (defun org-texinfo-inlinetask (inlinetask contents info)
  974. "Transcode an INLINETASK element from Org to Texinfo.
  975. CONTENTS holds the contents of the block. INFO is a plist
  976. holding contextual information."
  977. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  978. (todo (and (plist-get info :with-todo-keywords)
  979. (let ((todo (org-element-property :todo-keyword inlinetask)))
  980. (and todo (org-export-data todo info)))))
  981. (todo-type (org-element-property :todo-type inlinetask))
  982. (tags (and (plist-get info :with-tags)
  983. (org-export-get-tags inlinetask info)))
  984. (priority (and (plist-get info :with-priority)
  985. (org-element-property :priority inlinetask))))
  986. (funcall (plist-get info :texinfo-format-inlinetask-function)
  987. todo todo-type priority title tags contents)))
  988. (defun org-texinfo-format-inlinetask-default-function
  989. (todo _todo-type priority title tags contents)
  990. "Default format function for inlinetasks.
  991. See `org-texinfo-format-inlinetask-function' for details."
  992. (let ((full-title
  993. (concat (when todo (format "@strong{%s} " todo))
  994. (when priority (format "#%c " priority))
  995. title
  996. (when tags (org-make-tag-string tags)))))
  997. (format "@center %s\n\n%s\n" full-title contents)))
  998. ;;;; Italic
  999. (defun org-texinfo-italic (_italic contents info)
  1000. "Transcode ITALIC from Org to Texinfo.
  1001. CONTENTS is the text with italic markup. INFO is a plist holding
  1002. contextual information."
  1003. (org-texinfo--text-markup contents 'italic info))
  1004. ;;;; Item
  1005. (defun org-texinfo-item (item contents info)
  1006. "Transcode an ITEM element from Org to Texinfo.
  1007. CONTENTS holds the contents of the item. INFO is a plist holding
  1008. contextual information."
  1009. (let* ((tag (org-element-property :tag item))
  1010. (plain-list (org-element-property :parent item))
  1011. (compact (and (eq (org-element-property :type plain-list) 'descriptive)
  1012. (or (plist-get info :texinfo-compact-itemx)
  1013. (org-not-nil (org-export-read-attribute
  1014. :attr_texinfo plain-list :compact)))))
  1015. (previous-item nil))
  1016. (when (and compact
  1017. (org-export-get-next-element item info)
  1018. (not (org-element-contents item))
  1019. (eq 1 (org-element-property :post-blank item)))
  1020. (org-element-put-property item :post-blank 0))
  1021. (if (and compact
  1022. (setq previous-item (org-export-get-previous-element item info))
  1023. (not (org-element-contents previous-item))
  1024. (eq 0 (org-element-property :post-blank previous-item)))
  1025. (format "@itemx%s\n%s"
  1026. (if tag (concat " " (org-export-data tag info)) "")
  1027. (or contents ""))
  1028. (let* ((split (org-string-nw-p (org-export-read-attribute
  1029. :attr_texinfo plain-list :sep)))
  1030. (items (and tag
  1031. (let ((tag (org-export-data tag info)))
  1032. (if split
  1033. (split-string tag (regexp-quote split)
  1034. t "[ \t\n]+")
  1035. (list tag))))))
  1036. (format "%s\n%s"
  1037. (pcase items
  1038. (`nil "@item")
  1039. (`(,item) (concat "@item " item))
  1040. (`(,item . ,items)
  1041. (concat "@item " item "\n"
  1042. (mapconcat (lambda (i) (concat "@itemx " i))
  1043. items
  1044. "\n"))))
  1045. (or contents ""))))))
  1046. ;;;; Keyword
  1047. (defun org-texinfo-keyword (keyword _contents info)
  1048. "Transcode a KEYWORD element from Org to Texinfo.
  1049. CONTENTS is nil. INFO is a plist holding contextual information."
  1050. (let ((value (org-element-property :value keyword)))
  1051. (pcase (org-element-property :key keyword)
  1052. ("TEXINFO" value)
  1053. ("CINDEX" (format "@cindex %s" value))
  1054. ("FINDEX" (format "@findex %s" value))
  1055. ("KINDEX" (format "@kindex %s" value))
  1056. ("PINDEX" (format "@pindex %s" value))
  1057. ("TINDEX" (format "@tindex %s" value))
  1058. ("VINDEX" (format "@vindex %s" value))
  1059. ("TOC"
  1060. (cond ((string-match-p "\\<tables\\>" value)
  1061. (concat "@listoffloats "
  1062. (org-export-translate "Table" :utf-8 info)))
  1063. ((string-match-p "\\<listings\\>" value)
  1064. (concat "@listoffloats "
  1065. (org-export-translate "Listing" :utf-8 info))))))))
  1066. ;;;; Line Break
  1067. (defun org-texinfo-line-break (_line-break _contents _info)
  1068. "Transcode a LINE-BREAK object from Org to Texinfo.
  1069. CONTENTS is nil. INFO is a plist holding contextual information."
  1070. "@*\n")
  1071. ;;;; Link
  1072. (defun org-texinfo--@ref (datum description info)
  1073. "Return @ref command for element or object DATUM.
  1074. DESCRIPTION is the printed name of the section, as a string, or
  1075. nil."
  1076. (let ((node-name (org-texinfo--get-node datum info))
  1077. ;; Sanitize DESCRIPTION for cross-reference use. In
  1078. ;; particular, remove colons as they seem to cause pain (even
  1079. ;; within @asis{...}) to the Texinfo reader.
  1080. (title (and description
  1081. (replace-regexp-in-string
  1082. "[ \t]*:+" ""
  1083. (replace-regexp-in-string "," "@comma{}" description)))))
  1084. (if (or (not title) (equal title node-name))
  1085. (format "@ref{%s}" node-name)
  1086. (format "@ref{%s, , %s}" node-name title))))
  1087. (defun org-texinfo-link (link desc info)
  1088. "Transcode a LINK object from Org to Texinfo.
  1089. DESC is the description part of the link, or the empty string.
  1090. INFO is a plist holding contextual information. See
  1091. `org-export-data'."
  1092. (let* ((type (org-element-property :type link))
  1093. (raw-path (org-element-property :path link))
  1094. ;; Ensure DESC really exists, or set it to nil.
  1095. (desc (and (not (string= desc "")) desc))
  1096. (path (org-texinfo--sanitize-content
  1097. (cond
  1098. ((member type '("http" "https" "ftp"))
  1099. (concat type ":" raw-path))
  1100. ((string-equal type "file")
  1101. (org-export-file-uri raw-path))
  1102. (t raw-path)))))
  1103. (cond
  1104. ((org-export-custom-protocol-maybe link desc 'texinfo info))
  1105. ((org-export-inline-image-p link org-texinfo-inline-image-rules)
  1106. (org-texinfo--inline-image link info))
  1107. ((equal type "radio")
  1108. (let ((destination (org-export-resolve-radio-link link info)))
  1109. (if (not destination) desc
  1110. (org-texinfo--@ref destination desc info))))
  1111. ((member type '("custom-id" "id" "fuzzy"))
  1112. (let ((destination
  1113. (if (equal type "fuzzy")
  1114. (org-export-resolve-fuzzy-link link info)
  1115. (org-export-resolve-id-link link info))))
  1116. (pcase (org-element-type destination)
  1117. (`nil
  1118. (format org-texinfo-link-with-unknown-path-format path))
  1119. ;; Id link points to an external file.
  1120. (`plain-text
  1121. (if desc (format "@uref{file://%s,%s}" destination desc)
  1122. (format "@uref{file://%s}" destination)))
  1123. ((or `headline
  1124. ;; Targets within headlines cannot be turned into
  1125. ;; @anchor{}, so we refer to the headline parent
  1126. ;; directly.
  1127. (and `target
  1128. (guard (eq 'headline
  1129. (org-element-type
  1130. (org-element-property :parent destination))))))
  1131. (let ((headline (org-element-lineage destination '(headline) t)))
  1132. (org-texinfo--@ref headline desc info)))
  1133. (_ (org-texinfo--@ref destination desc info)))))
  1134. ((string= type "mailto")
  1135. (format "@email{%s}"
  1136. (concat path (and desc (concat ", " desc)))))
  1137. ;; External link with a description part.
  1138. ((and path desc) (format "@uref{%s, %s}" path desc))
  1139. ;; External link without a description part.
  1140. (path (format "@uref{%s}" path))
  1141. ;; No path, only description. Try to do something useful.
  1142. (t
  1143. (format (plist-get info :texinfo-link-with-unknown-path-format) desc)))))
  1144. (defun org-texinfo--inline-image (link info)
  1145. "Return Texinfo code for an inline image.
  1146. LINK is the link pointing to the inline image. INFO is the
  1147. current state of the export, as a plist."
  1148. (let* ((parent (org-export-get-parent-element link))
  1149. (label (and (org-element-property :name parent)
  1150. (org-texinfo--get-node parent info)))
  1151. (caption (org-export-get-caption parent))
  1152. (shortcaption (org-export-get-caption parent t))
  1153. (path (org-element-property :path link))
  1154. (filename
  1155. (file-name-sans-extension
  1156. (if (file-name-absolute-p path)
  1157. (expand-file-name path)
  1158. (file-relative-name path))))
  1159. (extension (file-name-extension path))
  1160. (attributes (org-export-read-attribute :attr_texinfo parent))
  1161. (height (or (plist-get attributes :height) ""))
  1162. (width (or (plist-get attributes :width) ""))
  1163. (alt (or (plist-get attributes :alt) ""))
  1164. (image (format "@image{%s,%s,%s,%s,%s}"
  1165. filename width height alt extension)))
  1166. (cond ((or caption shortcaption)
  1167. (org-texinfo--wrap-float image
  1168. info
  1169. (org-export-translate "Figure" :utf-8 info)
  1170. label
  1171. caption
  1172. shortcaption))
  1173. (label (concat "@anchor{" label "}\n" image))
  1174. (t image))))
  1175. ;;;; Menu
  1176. (defun org-texinfo-make-menu (scope info &optional master)
  1177. "Create the menu for inclusion in the Texinfo document.
  1178. SCOPE is a headline or a full parse tree. INFO is the
  1179. communication channel, as a plist.
  1180. When optional argument MASTER is non-nil, generate a master menu,
  1181. including detailed node listing."
  1182. (let ((menu (org-texinfo--build-menu scope info)))
  1183. (when (org-string-nw-p menu)
  1184. (org-element-normalize-string
  1185. (format
  1186. "@menu\n%s@end menu"
  1187. (concat menu
  1188. (when master
  1189. (let ((detailmenu
  1190. (org-texinfo--build-menu
  1191. scope info
  1192. (let ((toc-depth (plist-get info :with-toc)))
  1193. (if (wholenump toc-depth) toc-depth
  1194. org-texinfo-max-toc-depth)))))
  1195. (when (org-string-nw-p detailmenu)
  1196. (concat "\n@detailmenu\n"
  1197. "--- The Detailed Node Listing ---\n\n"
  1198. detailmenu
  1199. "@end detailmenu\n"))))))))))
  1200. (defun org-texinfo--build-menu (scope info &optional level)
  1201. "Build menu for entries within SCOPE.
  1202. SCOPE is a headline or a full parse tree. INFO is a plist
  1203. containing contextual information. When optional argument LEVEL
  1204. is an integer, build the menu recursively, down to this depth."
  1205. (cond
  1206. ((not level)
  1207. (org-texinfo--format-entries (org-texinfo--menu-entries scope info) info))
  1208. ((zerop level) "\n")
  1209. (t
  1210. (mapconcat
  1211. (lambda (h)
  1212. (let ((entries (org-texinfo--menu-entries h info)))
  1213. (when entries
  1214. (concat
  1215. (format "%s\n\n%s\n"
  1216. (org-export-data (org-export-get-alt-title h info) info)
  1217. (org-texinfo--format-entries entries info))
  1218. (org-texinfo--build-menu h info (1- level))))))
  1219. (org-texinfo--menu-entries scope info)
  1220. ""))))
  1221. (defun org-texinfo--format-entries (entries info)
  1222. "Format all direct menu entries in SCOPE, as a string.
  1223. SCOPE is either a headline or a full Org document. INFO is
  1224. a plist containing contextual information."
  1225. (org-element-normalize-string
  1226. (mapconcat
  1227. (lambda (h)
  1228. (let* ((title
  1229. ;; Colons are used as a separator between title and node
  1230. ;; name. Remove them.
  1231. (replace-regexp-in-string
  1232. "[ \t]*:+" ""
  1233. (org-texinfo--sanitize-title
  1234. (org-export-get-alt-title h info) info)))
  1235. (node (org-texinfo--get-node h info))
  1236. (entry (concat "* " title ":"
  1237. (if (string= title node) ":"
  1238. (concat " " node ". "))))
  1239. (desc (org-element-property :DESCRIPTION h)))
  1240. (if (not desc) entry
  1241. (format (format "%%-%ds %%s" org-texinfo-node-description-column)
  1242. entry desc))))
  1243. entries "\n")))
  1244. (defun org-texinfo--menu-entries (scope info)
  1245. "List direct children in SCOPE needing a menu entry.
  1246. SCOPE is a headline or a full parse tree. INFO is a plist
  1247. holding contextual information."
  1248. (let* ((cache (or (plist-get info :texinfo-entries-cache)
  1249. (plist-get (plist-put info :texinfo-entries-cache
  1250. (make-hash-table :test #'eq))
  1251. :texinfo-entries-cache)))
  1252. (cached-entries (gethash scope cache 'no-cache)))
  1253. (if (not (eq cached-entries 'no-cache)) cached-entries
  1254. (let* ((sections (org-texinfo--sectioning-structure info))
  1255. (max-depth (length sections)))
  1256. (puthash scope
  1257. (cl-remove-if
  1258. (lambda (h)
  1259. (or (org-not-nil (org-export-get-node-property :COPYING h t))
  1260. (< max-depth (org-export-get-relative-level h info))))
  1261. (org-export-collect-headlines info 1 scope))
  1262. cache)))))
  1263. ;;;; Node Property
  1264. (defun org-texinfo-node-property (node-property _contents _info)
  1265. "Transcode a NODE-PROPERTY element from Org to Texinfo.
  1266. CONTENTS is nil. INFO is a plist holding contextual
  1267. information."
  1268. (format "%s:%s"
  1269. (org-element-property :key node-property)
  1270. (let ((value (org-element-property :value node-property)))
  1271. (if value (concat " " value) ""))))
  1272. ;;;; Paragraph
  1273. (defun org-texinfo-paragraph (_paragraph contents _info)
  1274. "Transcode a PARAGRAPH element from Org to Texinfo.
  1275. CONTENTS is the contents of the paragraph, as a string. INFO is
  1276. the plist used as a communication channel."
  1277. contents)
  1278. ;;;; Plain List
  1279. (defun org-texinfo-plain-list (plain-list contents info)
  1280. "Transcode a PLAIN-LIST element from Org to Texinfo.
  1281. CONTENTS is the contents of the list. INFO is a plist holding
  1282. contextual information."
  1283. (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
  1284. (indic (let ((i (or (plist-get attr :indic)
  1285. (plist-get info :texinfo-table-default-markup))))
  1286. ;; Allow indicating commands with missing @ sign.
  1287. (if (string-prefix-p "@" i) i (concat "@" i))))
  1288. (table-type (plist-get attr :table-type))
  1289. (type (org-element-property :type plain-list))
  1290. (enum
  1291. (cond ((not (eq type 'ordered)) nil)
  1292. ((plist-member attr :enum) (plist-get attr :enum))
  1293. (t
  1294. ;; Texinfo only supports initial counters, i.e., it
  1295. ;; cannot change the numbering mid-list.
  1296. (let ((first-item (car (org-element-contents plain-list))))
  1297. (org-element-property :counter first-item)))))
  1298. (list-type (cond
  1299. ((eq type 'ordered) "enumerate")
  1300. ((eq type 'unordered) "itemize")
  1301. ((member table-type '("ftable" "vtable")) table-type)
  1302. (t "table"))))
  1303. (format "@%s\n%s@end %s"
  1304. (cond ((eq type 'descriptive) (concat list-type " " indic))
  1305. (enum (format "%s %s" list-type enum))
  1306. (t list-type))
  1307. contents
  1308. list-type)))
  1309. ;;;; Plain Text
  1310. (defun org-texinfo-plain-text (text info)
  1311. "Transcode a TEXT string from Org to Texinfo.
  1312. TEXT is the string to transcode. INFO is a plist holding
  1313. contextual information."
  1314. ;; First protect @, { and }.
  1315. (let ((output (org-texinfo--sanitize-content text)))
  1316. ;; Activate smart quotes. Be sure to provide original TEXT string
  1317. ;; since OUTPUT may have been modified.
  1318. (when (plist-get info :with-smart-quotes)
  1319. (setq output
  1320. (org-export-activate-smart-quotes output :texinfo info text)))
  1321. ;; LaTeX into @LaTeX{} and TeX into @TeX{}
  1322. (let ((case-fold-search nil))
  1323. (setq output (replace-regexp-in-string "\\(?:La\\)?TeX" "@\\&{}" output)))
  1324. ;; Convert special strings.
  1325. (when (plist-get info :with-special-strings)
  1326. (setq output
  1327. (replace-regexp-in-string
  1328. "\\.\\.\\." "@dots{}"
  1329. (replace-regexp-in-string "\\\\-" "@-" output))))
  1330. ;; Handle break preservation if required.
  1331. (when (plist-get info :preserve-breaks)
  1332. (setq output (replace-regexp-in-string
  1333. "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
  1334. ;; Reverse sentence ending. A sentence can end with a capital
  1335. ;; letter. Use non-breaking space if it shouldn't.
  1336. (let ((case-fold-search nil))
  1337. (replace-regexp-in-string
  1338. "[A-Z]\\([.?!]\\)\\(?:[])]\\|'\\{1,2\\}\\)?\\(?: \\|$\\)"
  1339. "@\\1"
  1340. output nil nil 1))))
  1341. ;;;; Planning
  1342. (defun org-texinfo-planning (planning _contents info)
  1343. "Transcode a PLANNING element from Org to Texinfo.
  1344. CONTENTS is nil. INFO is a plist holding contextual
  1345. information."
  1346. (concat
  1347. "@noindent"
  1348. (mapconcat
  1349. 'identity
  1350. (delq nil
  1351. (list
  1352. (let ((closed (org-element-property :closed planning)))
  1353. (when closed
  1354. (concat
  1355. (format "@strong{%s} " org-closed-string)
  1356. (format (plist-get info :texinfo-inactive-timestamp-format)
  1357. (org-timestamp-translate closed)))))
  1358. (let ((deadline (org-element-property :deadline planning)))
  1359. (when deadline
  1360. (concat
  1361. (format "@strong{%s} " org-deadline-string)
  1362. (format (plist-get info :texinfo-active-timestamp-format)
  1363. (org-timestamp-translate deadline)))))
  1364. (let ((scheduled (org-element-property :scheduled planning)))
  1365. (when scheduled
  1366. (concat
  1367. (format "@strong{%s} " org-scheduled-string)
  1368. (format (plist-get info :texinfo-active-timestamp-format)
  1369. (org-timestamp-translate scheduled)))))))
  1370. " ")
  1371. "@*"))
  1372. ;;;; Property Drawer
  1373. (defun org-texinfo-property-drawer (_property-drawer contents _info)
  1374. "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
  1375. CONTENTS holds the contents of the drawer. INFO is a plist
  1376. holding contextual information."
  1377. (and (org-string-nw-p contents)
  1378. (format "@verbatim\n%s@end verbatim" contents)))
  1379. ;;;; Quote Block
  1380. (defun org-texinfo-quote-block (quote-block contents _info)
  1381. "Transcode a QUOTE-BLOCK element from Org to Texinfo.
  1382. CONTENTS holds the contents of the block. INFO is a plist
  1383. holding contextual information."
  1384. (let ((tag (org-export-read-attribute :attr_texinfo quote-block :tag))
  1385. (author (org-export-read-attribute :attr_texinfo quote-block :author)))
  1386. (format "@quotation%s\n%s%s\n@end quotation"
  1387. (if tag (concat " " tag) "")
  1388. contents
  1389. (if author (concat "\n@author " author) ""))))
  1390. ;;;; Radio Target
  1391. (defun org-texinfo-radio-target (radio-target text info)
  1392. "Transcode a RADIO-TARGET object from Org to Texinfo.
  1393. TEXT is the text of the target. INFO is a plist holding
  1394. contextual information."
  1395. (format "@anchor{%s}%s"
  1396. (org-texinfo--get-node radio-target info)
  1397. text))
  1398. ;;;; Section
  1399. (defun org-texinfo-section (section contents info)
  1400. "Transcode a SECTION element from Org to Texinfo.
  1401. CONTENTS holds the contents of the section. INFO is a plist
  1402. holding contextual information."
  1403. (let ((parent (org-export-get-parent-headline section)))
  1404. (when parent ;first section is handled in `org-texinfo-template'
  1405. (org-trim
  1406. (concat contents
  1407. "\n"
  1408. (and (not (org-export-excluded-from-toc-p parent info))
  1409. (org-texinfo-make-menu parent info)))))))
  1410. ;;;; Special Block
  1411. (defun org-texinfo-special-block (special-block contents _info)
  1412. "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
  1413. CONTENTS holds the contents of the block. INFO is a plist used
  1414. as a communication channel."
  1415. (let ((opt (org-export-read-attribute :attr_texinfo special-block :options))
  1416. (type (org-element-property :type special-block)))
  1417. (format "@%s%s\n%s@end %s"
  1418. type
  1419. (if opt (concat " " opt) "")
  1420. (or contents "")
  1421. type)))
  1422. ;;;; Src Block
  1423. (defun org-texinfo-src-block (src-block _contents info)
  1424. "Transcode a SRC-BLOCK element from Org to Texinfo.
  1425. CONTENTS holds the contents of the item. INFO is a plist holding
  1426. contextual information."
  1427. (let* ((lisp (string-match-p "lisp"
  1428. (org-element-property :language src-block)))
  1429. (code (org-texinfo--sanitize-content
  1430. (org-export-format-code-default src-block info)))
  1431. (value (format
  1432. (if lisp "@lisp\n%s@end lisp" "@example\n%s@end example")
  1433. code))
  1434. (caption (org-export-get-caption src-block))
  1435. (shortcaption (org-export-get-caption src-block t)))
  1436. (if (not (or caption shortcaption)) value
  1437. (org-texinfo--wrap-float value
  1438. info
  1439. (org-export-translate "Listing" :utf-8 info)
  1440. (org-texinfo--get-node src-block info)
  1441. caption
  1442. shortcaption))))
  1443. ;;;; Statistics Cookie
  1444. (defun org-texinfo-statistics-cookie (statistics-cookie _contents _info)
  1445. "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
  1446. CONTENTS is nil. INFO is a plist holding contextual information."
  1447. (org-element-property :value statistics-cookie))
  1448. ;;;; Strike-through
  1449. (defun org-texinfo-strike-through (_strike-through contents info)
  1450. "Transcode STRIKE-THROUGH from Org to Texinfo.
  1451. CONTENTS is the text with strike-through markup. INFO is a plist
  1452. holding contextual information."
  1453. (org-texinfo--text-markup contents 'strike-through info))
  1454. ;;;; Subscript
  1455. (defun org-texinfo-subscript (_subscript contents _info)
  1456. "Transcode a SUBSCRIPT object from Org to Texinfo.
  1457. CONTENTS is the contents of the object. INFO is a plist holding
  1458. contextual information."
  1459. (format "@math{_%s}" contents))
  1460. ;;;; Superscript
  1461. (defun org-texinfo-superscript (_superscript contents _info)
  1462. "Transcode a SUPERSCRIPT object from Org to Texinfo.
  1463. CONTENTS is the contents of the object. INFO is a plist holding
  1464. contextual information."
  1465. (format "@math{^%s}" contents))
  1466. ;;;; Table
  1467. (defun org-texinfo-table (table contents info)
  1468. "Transcode a TABLE element from Org to Texinfo.
  1469. CONTENTS is the contents of the table. INFO is a plist holding
  1470. contextual information."
  1471. (if (eq (org-element-property :type table) 'table.el)
  1472. (format "@verbatim\n%s@end verbatim"
  1473. (org-element-normalize-string
  1474. (org-element-property :value table)))
  1475. (let* ((col-width (org-export-read-attribute :attr_texinfo table :columns))
  1476. (columns
  1477. (if col-width (format "@columnfractions %s" col-width)
  1478. (org-texinfo-table-column-widths table info)))
  1479. (caption (org-export-get-caption table))
  1480. (shortcaption (org-export-get-caption table t))
  1481. (table-str (format "@multitable %s\n%s@end multitable"
  1482. columns
  1483. contents)))
  1484. (if (not (or caption shortcaption)) table-str
  1485. (org-texinfo--wrap-float table-str
  1486. info
  1487. (org-export-translate "Table" :utf-8 info)
  1488. (org-texinfo--get-node table info)
  1489. caption
  1490. shortcaption)))))
  1491. (defun org-texinfo-table-column-widths (table info)
  1492. "Determine the largest table cell in each column to process alignment.
  1493. TABLE is the table element to transcode. INFO is a plist used as
  1494. a communication channel."
  1495. (let ((widths (make-vector (cdr (org-export-table-dimensions table info)) 0)))
  1496. (org-element-map table 'table-row
  1497. (lambda (row)
  1498. (let ((idx 0))
  1499. (org-element-map row 'table-cell
  1500. (lambda (cell)
  1501. ;; Length of the cell in the original buffer is only an
  1502. ;; approximation of the length of the cell in the
  1503. ;; output. It can sometimes fail (e.g. it considers
  1504. ;; "/a/" being larger than "ab").
  1505. (let ((w (- (org-element-property :contents-end cell)
  1506. (org-element-property :contents-begin cell))))
  1507. (aset widths idx (max w (aref widths idx))))
  1508. (cl-incf idx))
  1509. info)))
  1510. info)
  1511. (format "{%s}" (mapconcat (lambda (w) (make-string w ?a)) widths "} {"))))
  1512. ;;;; Table Cell
  1513. (defun org-texinfo-table-cell (table-cell contents info)
  1514. "Transcode a TABLE-CELL element from Org to Texinfo.
  1515. CONTENTS is the cell contents. INFO is a plist used as
  1516. a communication channel."
  1517. (concat
  1518. (let ((scientific-notation
  1519. (plist-get info :texinfo-table-scientific-notation)))
  1520. (if (and contents
  1521. scientific-notation
  1522. (string-match orgtbl-exp-regexp contents))
  1523. ;; Use appropriate format string for scientific notation.
  1524. (format scientific-notation
  1525. (match-string 1 contents)
  1526. (match-string 2 contents))
  1527. contents))
  1528. (when (org-export-get-next-element table-cell info) "\n@tab ")))
  1529. ;;;; Table Row
  1530. (defun org-texinfo-table-row (table-row contents info)
  1531. "Transcode a TABLE-ROW element from Org to Texinfo.
  1532. CONTENTS is the contents of the row. INFO is a plist used as
  1533. a communication channel."
  1534. ;; Rules are ignored since table separators are deduced from
  1535. ;; borders of the current row.
  1536. (when (eq (org-element-property :type table-row) 'standard)
  1537. (let ((rowgroup-tag
  1538. (if (and (= 1 (org-export-table-row-group table-row info))
  1539. (org-export-table-has-header-p
  1540. (org-export-get-parent-table table-row) info))
  1541. "@headitem "
  1542. "@item ")))
  1543. (concat rowgroup-tag contents "\n"))))
  1544. ;;;; Target
  1545. (defun org-texinfo-target (target _contents info)
  1546. "Transcode a TARGET object from Org to Texinfo.
  1547. CONTENTS is nil. INFO is a plist holding contextual
  1548. information."
  1549. (format "@anchor{%s}" (org-texinfo--get-node target info)))
  1550. ;;;; Timestamp
  1551. (defun org-texinfo-timestamp (timestamp _contents info)
  1552. "Transcode a TIMESTAMP object from Org to Texinfo.
  1553. CONTENTS is nil. INFO is a plist holding contextual
  1554. information."
  1555. (let ((value (org-texinfo-plain-text
  1556. (org-timestamp-translate timestamp) info)))
  1557. (pcase (org-element-property :type timestamp)
  1558. ((or `active `active-range)
  1559. (format (plist-get info :texinfo-active-timestamp-format) value))
  1560. ((or `inactive `inactive-range)
  1561. (format (plist-get info :texinfo-inactive-timestamp-format) value))
  1562. (_ (format (plist-get info :texinfo-diary-timestamp-format) value)))))
  1563. ;;;; Underline
  1564. (defun org-texinfo-underline (_underline contents info)
  1565. "Transcode UNDERLINE from Org to Texinfo.
  1566. CONTENTS is the text with underline markup. INFO is a plist
  1567. holding contextual information."
  1568. (org-texinfo--text-markup contents 'underline info))
  1569. ;;;; Verbatim
  1570. (defun org-texinfo-verbatim (verbatim _contents info)
  1571. "Transcode a VERBATIM object from Org to Texinfo.
  1572. CONTENTS is nil. INFO is a plist used as a communication
  1573. channel."
  1574. (org-texinfo--text-markup
  1575. (org-element-property :value verbatim) 'verbatim info))
  1576. ;;;; Verse Block
  1577. (defun org-texinfo-verse-block (_verse-block contents _info)
  1578. "Transcode a VERSE-BLOCK element from Org to Texinfo.
  1579. CONTENTS is verse block contents. INFO is a plist holding
  1580. contextual information."
  1581. (format "@display\n%s@end display" contents))
  1582. ;;; Public Functions
  1583. (defun org-texinfo-kbd-macro (key &optional noquote)
  1584. "Quote KEY using @kbd{...} and if necessary @key{...}.
  1585. This is intended to be used as an Org macro like so:
  1586. #+macro: kbd (eval (org-texinfo-kbd-macro $1))
  1587. Type {{{kbd(C-c SPC)}}}.
  1588. Also see info node `(org)Key bindings in Texinfo export'.
  1589. If optional NOQOUTE is non-nil, then do not add the quoting
  1590. that is necessary when using this in an Org macro."
  1591. (format (if noquote "@kbd{%s}" "@@texinfo:@kbd{@@%s@@texinfo:}@@")
  1592. (let ((case-fold-search nil))
  1593. (replace-regexp-in-string
  1594. org-texinfo--quoted-keys-regexp
  1595. (if noquote "@key{\\&}" "@@texinfo:@key{@@\\&@@texinfo:}@@")
  1596. key t))))
  1597. ;;; Interactive Functions
  1598. ;;;###autoload
  1599. (defun org-texinfo-export-to-texinfo
  1600. (&optional async subtreep visible-only body-only ext-plist)
  1601. "Export current buffer to a Texinfo file.
  1602. If narrowing is active in the current buffer, only export its
  1603. narrowed part.
  1604. If a region is active, export that region.
  1605. A non-nil optional argument ASYNC means the process should happen
  1606. asynchronously. The resulting file should be accessible through
  1607. the `org-export-stack' interface.
  1608. When optional argument SUBTREEP is non-nil, export the sub-tree
  1609. at point, extracting information from the headline properties
  1610. first.
  1611. When optional argument VISIBLE-ONLY is non-nil, don't export
  1612. contents of hidden elements.
  1613. When optional argument BODY-ONLY is non-nil, only write code
  1614. between \"\\begin{document}\" and \"\\end{document}\".
  1615. EXT-PLIST, when provided, is a property list with external
  1616. parameters overriding Org default settings, but still inferior to
  1617. file-local settings.
  1618. Return output file's name."
  1619. (interactive)
  1620. (let ((outfile (org-export-output-file-name ".texi" subtreep))
  1621. (org-export-coding-system org-texinfo-coding-system))
  1622. (org-export-to-file 'texinfo outfile
  1623. async subtreep visible-only body-only ext-plist)))
  1624. (defun org-texinfo-export-to-texinfo-batch ()
  1625. "Export Org file INFILE to Texinfo file OUTFILE, in batch mode.
  1626. Overwrites existing output file.
  1627. Usage: emacs -batch -f org-texinfo-export-to-texinfo-batch INFILE OUTFILE"
  1628. (or noninteractive (user-error "Batch mode use only"))
  1629. (let ((infile (pop command-line-args-left))
  1630. (outfile (pop command-line-args-left))
  1631. (org-export-coding-system org-texinfo-coding-system)
  1632. (make-backup-files nil))
  1633. (unless (file-readable-p infile)
  1634. (message "File `%s' not readable" infile)
  1635. (kill-emacs 1))
  1636. (with-temp-buffer
  1637. (insert-file-contents infile)
  1638. (org-export-to-file 'texinfo outfile))))
  1639. ;;;###autoload
  1640. (defun org-texinfo-export-to-info
  1641. (&optional async subtreep visible-only body-only ext-plist)
  1642. "Export current buffer to Texinfo then process through to INFO.
  1643. If narrowing is active in the current buffer, only export its
  1644. narrowed part.
  1645. If a region is active, export that region.
  1646. A non-nil optional argument ASYNC means the process should happen
  1647. asynchronously. The resulting file should be accessible through
  1648. the `org-export-stack' interface.
  1649. When optional argument SUBTREEP is non-nil, export the sub-tree
  1650. at point, extracting information from the headline properties
  1651. first.
  1652. When optional argument VISIBLE-ONLY is non-nil, don't export
  1653. contents of hidden elements.
  1654. When optional argument BODY-ONLY is non-nil, only write code
  1655. between \"\\begin{document}\" and \"\\end{document}\".
  1656. EXT-PLIST, when provided, is a property list with external
  1657. parameters overriding Org default settings, but still inferior to
  1658. file-local settings.
  1659. When optional argument PUB-DIR is set, use it as the publishing
  1660. directory.
  1661. Return INFO file's name."
  1662. (interactive)
  1663. (let ((outfile (org-export-output-file-name ".texi" subtreep))
  1664. (org-export-coding-system org-texinfo-coding-system))
  1665. (org-export-to-file 'texinfo outfile
  1666. async subtreep visible-only body-only ext-plist
  1667. #'org-texinfo-compile)))
  1668. ;;;###autoload
  1669. (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
  1670. "Publish an org file to Texinfo.
  1671. FILENAME is the filename of the Org file to be published. PLIST
  1672. is the property list for the given project. PUB-DIR is the
  1673. publishing directory.
  1674. Return output file name."
  1675. (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
  1676. ;;;###autoload
  1677. (defun org-texinfo-convert-region-to-texinfo ()
  1678. "Assume the current region has Org syntax, and convert it to Texinfo.
  1679. This can be used in any buffer. For example, you can write an
  1680. itemized list in Org syntax in an Texinfo buffer and use this
  1681. command to convert it."
  1682. (interactive)
  1683. (org-export-replace-region-by 'texinfo))
  1684. (defun org-texinfo-compile (file)
  1685. "Compile a texinfo file.
  1686. FILE is the name of the file being compiled. Processing is done
  1687. through the command specified in `org-texinfo-info-process',
  1688. which see. Output is redirected to \"*Org INFO Texinfo Output*\"
  1689. buffer.
  1690. Return INFO file name or an error if it couldn't be produced."
  1691. (message "Processing Texinfo file %s..." file)
  1692. (let* ((log-name "*Org INFO Texinfo Output*")
  1693. (log (get-buffer-create log-name))
  1694. (output
  1695. (org-compile-file file org-texinfo-info-process "info"
  1696. (format "See %S for details" log-name)
  1697. log)))
  1698. (when org-texinfo-remove-logfiles
  1699. (let ((base (file-name-sans-extension output)))
  1700. (dolist (ext org-texinfo-logfiles-extensions)
  1701. (let ((file (concat base "." ext)))
  1702. (when (file-exists-p file) (delete-file file))))))
  1703. (message "Process completed.")
  1704. output))
  1705. (provide 'ox-texinfo)
  1706. ;; Local variables:
  1707. ;; generated-autoload-file: "org-loaddefs.el"
  1708. ;; End:
  1709. ;;; ox-texinfo.el ends here