ox-texinfo.el 64 KB

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