ox-texinfo.el 63 KB

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