ox-texinfo.el 75 KB

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