ox-texinfo.el 56 KB

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