ox-texinfo.el 71 KB

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