ox-texinfo.el 63 KB

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