ox-texinfo.el 57 KB

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