ox-texinfo.el 56 KB

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