ox-texinfo.el 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  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 end of each section.
  335. Return new tree."
  336. (org-element-map tree 'headline
  337. (lambda (hl)
  338. (org-element-put-property hl :post-blank 1)
  339. (let ((contents (org-element-contents hl)))
  340. (when contents
  341. (let ((first (org-element-map contents '(headline section)
  342. #'identity info t)))
  343. (unless (eq (org-element-type first) 'section)
  344. (apply #'org-element-set-contents
  345. hl
  346. (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. (replace-regexp-in-string
  402. "[:,.]" ""
  403. (replace-regexp-in-string
  404. "\\`(\\(.*)\\)" "[\\1"
  405. (org-trim (replace-regexp-in-string "[ \t]\\{2,\\}" " " title)))))
  406. ;;;; Content sanitizing
  407. (defun org-texinfo--sanitize-content (text)
  408. "Escape special characters in string TEXT.
  409. Special characters are: @ { }"
  410. (replace-regexp-in-string "\\\([@{}]\\\)" "@\\1" text))
  411. ;;; Template
  412. (defun org-texinfo-template (contents info)
  413. "Return complete document string after Texinfo conversion.
  414. CONTENTS is the transcoded contents string. INFO is a plist
  415. holding export options."
  416. (let ((title (org-export-data (plist-get info :title) info))
  417. ;; Copying data is the contents of the first headline in
  418. ;; parse tree with a non-nil copying property.
  419. (copying (org-element-map (plist-get info :parse-tree) 'headline
  420. (lambda (hl)
  421. (and (org-not-nil (org-element-property :COPYING hl))
  422. (org-element-contents hl)))
  423. info t)))
  424. (concat
  425. "\\input texinfo @c -*- texinfo -*-\n"
  426. "@c %**start of header\n"
  427. (let ((file (or (plist-get info :texinfo-filename)
  428. (let ((f (plist-get info :output-file)))
  429. (and f (concat (file-name-sans-extension f) ".info"))))))
  430. (and file (format "@setfilename %s\n" file)))
  431. (format "@settitle %s\n" title)
  432. ;; Insert class-defined header.
  433. (org-element-normalize-string
  434. (let ((header (nth 1 (assoc (plist-get info :texinfo-class)
  435. org-texinfo-classes)))
  436. (coding
  437. (catch 'coding-system
  438. (let ((case-fold-search t)
  439. (name (symbol-name (or org-texinfo-coding-system
  440. buffer-file-coding-system))))
  441. (dolist (system org-texinfo-supported-coding-systems "UTF-8")
  442. (when (org-string-match-p (regexp-quote system) name)
  443. (throw 'coding-system system))))))
  444. (language (plist-get info :language))
  445. (case-fold-search nil))
  446. ;; Auto coding system.
  447. (replace-regexp-in-string
  448. "^@documentencoding \\(AUTO\\)$"
  449. coding
  450. (replace-regexp-in-string
  451. "^@documentlanguage \\(AUTO\\)$" language header t nil 1) t nil 1)))
  452. ;; Additional header options set by #+TEXINFO_HEADER.
  453. (let ((texinfo-header (plist-get info :texinfo-header)))
  454. (and texinfo-header (org-element-normalize-string texinfo-header)))
  455. "@c %**end of header\n\n"
  456. ;; Additional options set by #+TEXINFO_POST_HEADER.
  457. (let ((texinfo-post-header (plist-get info :texinfo-post-header)))
  458. (and texinfo-post-header
  459. (org-element-normalize-string texinfo-post-header)))
  460. ;; Copying.
  461. (and copying
  462. (format "@copying\n%s@end copying\n\n"
  463. (org-element-normalize-string
  464. (org-export-data copying info))))
  465. ;; Info directory information. Only supply if both title and
  466. ;; category are provided.
  467. (let ((dircat (plist-get info :texinfo-dircat))
  468. (dirtitle
  469. (let ((title (plist-get info :texinfo-dirtitle)))
  470. (and title
  471. (string-match "^\\(?:\\* \\)?\\(.*?\\)\\(\\.\\)?$" title)
  472. (format "* %s." (match-string 1 title))))))
  473. (when (and dircat dirtitle)
  474. (concat "@dircategory " dircat "\n"
  475. "@direntry\n"
  476. (let ((dirdesc
  477. (let ((desc (plist-get info :texinfo-dirdesc)))
  478. (cond ((not desc) nil)
  479. ((org-string-match-p "\\.$" desc) desc)
  480. (t (concat desc "."))))))
  481. (if dirdesc (format "%-23s %s" dirtitle dirdesc) dirtitle))
  482. "\n"
  483. "@end direntry\n\n")))
  484. ;; Title
  485. "@finalout\n"
  486. "@titlepage\n"
  487. (format "@title %s\n" (or (plist-get info :texinfo-printed-title) title))
  488. (let ((subtitle (plist-get info :subtitle)))
  489. (and subtitle
  490. (org-element-normalize-string
  491. (replace-regexp-in-string "^" "@subtitle " subtitle))))
  492. (when (plist-get info :with-author)
  493. (concat
  494. ;; Primary author.
  495. (let ((author (org-string-nw-p
  496. (org-export-data (plist-get info :author) info)))
  497. (email (and (plist-get info :with-email)
  498. (org-string-nw-p
  499. (org-export-data (plist-get info :email) info)))))
  500. (cond ((and author email)
  501. (format "@author %s (@email{%s})\n" author email))
  502. (author (format "@author %s\n" author))
  503. (email (format "@author @email{%s}\n" email))))
  504. ;; Other authors.
  505. (let ((subauthor (plist-get info :subauthor)))
  506. (and subauthor
  507. (org-element-normalize-string
  508. (replace-regexp-in-string "^" "@author " subauthor))))))
  509. (and copying "@page\n@vskip 0pt plus 1filll\n@insertcopying\n")
  510. "@end titlepage\n\n"
  511. ;; Table of contents.
  512. (and (plist-get info :with-toc) "@contents\n\n")
  513. ;; Configure Top Node when not for Tex
  514. "@ifnottex\n"
  515. "@node Top\n"
  516. (format "@top %s\n" title)
  517. (and copying "@insertcopying\n")
  518. "@end ifnottex\n\n"
  519. ;; Menu.
  520. (org-texinfo-make-menu (plist-get info :parse-tree) info 'master)
  521. "\n"
  522. ;; Document's body.
  523. contents "\n"
  524. ;; Creator.
  525. (case (plist-get info :with-creator)
  526. ((nil) nil)
  527. (comment (format "@c %s\n" (plist-get info :creator)))
  528. (otherwise (concat (plist-get info :creator) "\n")))
  529. ;; Document end.
  530. "@bye")))
  531. ;;; Transcode Functions
  532. ;;;; Bold
  533. (defun org-texinfo-bold (bold contents info)
  534. "Transcode BOLD from Org to Texinfo.
  535. CONTENTS is the text with bold markup. INFO is a plist holding
  536. contextual information."
  537. (org-texinfo--text-markup contents 'bold))
  538. ;;;; Center Block
  539. (defun org-texinfo-center-block (center-block contents info)
  540. "Transcode a CENTER-BLOCK element from Org to Texinfo.
  541. CONTENTS holds the contents of the block. INFO is a plist used
  542. as a communication channel."
  543. contents)
  544. ;;;; Clock
  545. (defun org-texinfo-clock (clock contents info)
  546. "Transcode a CLOCK element from Org to Texinfo.
  547. CONTENTS is nil. INFO is a plist holding contextual
  548. information."
  549. (concat
  550. "@noindent"
  551. (format "@strong{%s} " org-clock-string)
  552. (format org-texinfo-inactive-timestamp-format
  553. (concat (org-translate-time
  554. (org-element-property :raw-value
  555. (org-element-property :value clock)))
  556. (let ((time (org-element-property :duration clock)))
  557. (and time (format " (%s)" time)))))
  558. "@*"))
  559. ;;;; Code
  560. (defun org-texinfo-code (code contents info)
  561. "Transcode a CODE object from Org to Texinfo.
  562. CONTENTS is nil. INFO is a plist used as a communication
  563. channel."
  564. (org-texinfo--text-markup (org-element-property :value code) 'code))
  565. ;;;; Drawer
  566. (defun org-texinfo-drawer (drawer contents info)
  567. "Transcode a DRAWER element from Org to Texinfo.
  568. CONTENTS holds the contents of the block. INFO is a plist
  569. holding contextual information."
  570. (let* ((name (org-element-property :drawer-name drawer))
  571. (output (funcall org-texinfo-format-drawer-function
  572. name contents)))
  573. output))
  574. ;;;; Dynamic Block
  575. (defun org-texinfo-dynamic-block (dynamic-block contents info)
  576. "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
  577. CONTENTS holds the contents of the block. INFO is a plist
  578. holding contextual information."
  579. contents)
  580. ;;;; Entity
  581. (defun org-texinfo-entity (entity contents info)
  582. "Transcode an ENTITY object from Org to Texinfo.
  583. CONTENTS are the definition itself. INFO is a plist holding
  584. contextual information."
  585. (let ((ent (org-element-property :latex entity)))
  586. (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
  587. ;;;; Example Block
  588. (defun org-texinfo-example-block (example-block contents info)
  589. "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
  590. CONTENTS is nil. INFO is a plist holding contextual
  591. information."
  592. (format "@verbatim\n%s@end verbatim"
  593. (org-export-format-code-default example-block info)))
  594. ;;;; Export Block
  595. (defun org-texinfo-export-block (export-block contents info)
  596. "Transcode a EXPORT-BLOCK element from Org to Texinfo.
  597. CONTENTS is nil. INFO is a plist holding contextual information."
  598. (when (string= (org-element-property :type export-block) "TEXINFO")
  599. (org-remove-indentation (org-element-property :value export-block))))
  600. ;;;; Export Snippet
  601. (defun org-texinfo-export-snippet (export-snippet contents info)
  602. "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
  603. CONTENTS is nil. INFO is a plist holding contextual information."
  604. (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
  605. (org-element-property :value export-snippet)))
  606. ;;;; Fixed Width
  607. (defun org-texinfo-fixed-width (fixed-width contents info)
  608. "Transcode a FIXED-WIDTH element from Org to Texinfo.
  609. CONTENTS is nil. INFO is a plist holding contextual information."
  610. (format "@example\n%s\n@end example"
  611. (org-remove-indentation
  612. (org-texinfo--sanitize-content
  613. (org-element-property :value fixed-width)))))
  614. ;;;; Footnote Reference
  615. (defun org-texinfo-footnote-reference (footnote contents info)
  616. "Create a footnote reference for FOOTNOTE.
  617. FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
  618. plist holding contextual information."
  619. (let ((def (org-export-get-footnote-definition footnote info)))
  620. (format "@footnote{%s}"
  621. (org-trim (org-export-data def info)))))
  622. ;;;; Headline
  623. (defun org-texinfo-headline (headline contents info)
  624. "Transcode a HEADLINE element from Org to Texinfo.
  625. CONTENTS holds the contents of the headline. INFO is a plist
  626. holding contextual information."
  627. (let* ((class (plist-get info :texinfo-class))
  628. (level (org-export-get-relative-level headline info))
  629. (numberedp (org-export-numbered-headline-p headline info))
  630. (class-sectioning (assoc class org-texinfo-classes))
  631. ;; Find the index type, if any.
  632. (index (org-element-property :INDEX headline))
  633. ;; Create node info, to insert it before section formatting.
  634. ;; Use custom menu title if present.
  635. (node (format "@node %s\n" (org-texinfo--get-node headline info)))
  636. ;; Section formatting will set two placeholders: one for the
  637. ;; title and the other for the contents.
  638. (section-fmt
  639. (if (org-not-nil (org-element-property :APPENDIX headline))
  640. "@appendix %s\n%s"
  641. (let ((sec (if (and (symbolp (nth 2 class-sectioning))
  642. (fboundp (nth 2 class-sectioning)))
  643. (funcall (nth 2 class-sectioning) level numberedp)
  644. (nth (1+ level) class-sectioning))))
  645. (cond
  646. ;; No section available for that LEVEL.
  647. ((not sec) nil)
  648. ;; Section format directly returned by a function.
  649. ((stringp sec) sec)
  650. ;; (numbered-section . unnumbered-section)
  651. ((not (consp (cdr sec)))
  652. (concat (if (or index (not numberedp)) (cdr sec) (car sec))
  653. "\n%s"))))))
  654. (todo
  655. (and (plist-get info :with-todo-keywords)
  656. (let ((todo (org-element-property :todo-keyword headline)))
  657. (and todo (org-export-data todo info)))))
  658. (todo-type (and todo (org-element-property :todo-type headline)))
  659. (tags (and (plist-get info :with-tags)
  660. (org-export-get-tags headline info)))
  661. (priority (and (plist-get info :with-priority)
  662. (org-element-property :priority headline)))
  663. (text (org-export-data (org-element-property :title headline) info))
  664. (full-text (if (not (eq org-texinfo-format-headline-function 'ignore))
  665. ;; User-defined formatting function.
  666. (funcall org-texinfo-format-headline-function
  667. todo todo-type priority text tags)
  668. ;; Default formatting.
  669. (concat
  670. (when todo
  671. (format "@strong{%s} " todo))
  672. (when priority (format "@emph{#%s} " priority))
  673. text
  674. (when tags
  675. (format " :%s:"
  676. (mapconcat 'identity tags ":"))))))
  677. (contents (if (org-string-nw-p contents) (concat "\n" contents) "")))
  678. (cond
  679. ;; Case 1: This is a footnote section: ignore it.
  680. ((org-element-property :footnote-section-p headline) nil)
  681. ;; Case 2: This is the `copying' section: ignore it
  682. ;; This is used elsewhere.
  683. ((org-not-nil (org-element-property :COPYING headline)) nil)
  684. ;; Case 3: An index. If it matches one of the known indexes,
  685. ;; print it as such following the contents, otherwise
  686. ;; print the contents and leave the index up to the user.
  687. (index
  688. (concat node
  689. (format
  690. section-fmt
  691. full-text
  692. (concat contents
  693. (and (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
  694. (concat "\n@printindex " index))))))
  695. ;; Case 4: This is a deep sub-tree: export it as a list item.
  696. ;; Also export as items headlines for which no section
  697. ;; format has been found.
  698. ((or (not section-fmt) (org-export-low-level-p headline info))
  699. ;; Build the real contents of the sub-tree.
  700. (concat (and (org-export-first-sibling-p headline info)
  701. (format "@%s\n" (if numberedp 'enumerate 'itemize)))
  702. "@item\n" full-text "\n"
  703. contents
  704. (if (org-export-last-sibling-p headline info)
  705. (format "@end %s" (if numberedp 'enumerate 'itemize))
  706. "\n")))
  707. ;; Case 5: Standard headline. Export it as a section.
  708. (t (concat node (format section-fmt full-text contents))))))
  709. ;;;; Inline Src Block
  710. (defun org-texinfo-inline-src-block (inline-src-block contents info)
  711. "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
  712. CONTENTS holds the contents of the item. INFO is a plist holding
  713. contextual information."
  714. (let* ((code (org-element-property :value inline-src-block))
  715. (separator (org-texinfo--find-verb-separator code)))
  716. (concat "@verb{" separator code separator "}")))
  717. ;;;; Inlinetask
  718. (defun org-texinfo-inlinetask (inlinetask contents info)
  719. "Transcode an INLINETASK element from Org to Texinfo.
  720. CONTENTS holds the contents of the block. INFO is a plist
  721. holding contextual information."
  722. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  723. (todo (and (plist-get info :with-todo-keywords)
  724. (let ((todo (org-element-property :todo-keyword inlinetask)))
  725. (and todo (org-export-data todo info)))))
  726. (todo-type (org-element-property :todo-type inlinetask))
  727. (tags (and (plist-get info :with-tags)
  728. (org-export-get-tags inlinetask info)))
  729. (priority (and (plist-get info :with-priority)
  730. (org-element-property :priority inlinetask))))
  731. ;; If `org-texinfo-format-inlinetask-function' is provided, call it
  732. ;; with appropriate arguments.
  733. (if (not (eq org-texinfo-format-inlinetask-function 'ignore))
  734. (funcall org-texinfo-format-inlinetask-function
  735. todo todo-type priority title tags contents)
  736. ;; Otherwise, use a default template.
  737. (let ((full-title
  738. (concat
  739. (when todo (format "@strong{%s} " todo))
  740. (when priority (format "#%c " priority))
  741. title
  742. (when tags (format ":%s:"
  743. (mapconcat 'identity tags ":"))))))
  744. (format (concat "@center %s\n\n"
  745. "%s"
  746. "\n")
  747. full-title contents)))))
  748. ;;;; Italic
  749. (defun org-texinfo-italic (italic contents info)
  750. "Transcode ITALIC from Org to Texinfo.
  751. CONTENTS is the text with italic markup. INFO is a plist holding
  752. contextual information."
  753. (org-texinfo--text-markup contents 'italic))
  754. ;;;; Item
  755. (defun org-texinfo-item (item contents info)
  756. "Transcode an ITEM element from Org to Texinfo.
  757. CONTENTS holds the contents of the item. INFO is a plist holding
  758. contextual information."
  759. (format "@item%s\n%s"
  760. (let ((tag (org-element-property :tag item)))
  761. (if tag (concat " " (org-export-data tag info)) ""))
  762. (or contents "")))
  763. ;;;; Keyword
  764. (defun org-texinfo-keyword (keyword contents info)
  765. "Transcode a KEYWORD element from Org to Texinfo.
  766. CONTENTS is nil. INFO is a plist holding contextual information."
  767. (let ((key (org-element-property :key keyword))
  768. (value (org-element-property :value keyword)))
  769. (cond
  770. ((string= key "TEXINFO") value)
  771. ((string= key "CINDEX") (format "@cindex %s" value))
  772. ((string= key "FINDEX") (format "@findex %s" value))
  773. ((string= key "KINDEX") (format "@kindex %s" value))
  774. ((string= key "PINDEX") (format "@pindex %s" value))
  775. ((string= key "TINDEX") (format "@tindex %s" value))
  776. ((string= key "VINDEX") (format "@vindex %s" value)))))
  777. ;;;; Line Break
  778. (defun org-texinfo-line-break (line-break contents info)
  779. "Transcode a LINE-BREAK object from Org to Texinfo.
  780. CONTENTS is nil. INFO is a plist holding contextual information."
  781. "@*\n")
  782. ;;;; Link
  783. (defun org-texinfo-link (link desc info)
  784. "Transcode a LINK object from Org to Texinfo.
  785. DESC is the description part of the link, or the empty string.
  786. INFO is a plist holding contextual information. See
  787. `org-export-data'."
  788. (let* ((type (org-element-property :type link))
  789. (raw-path (org-element-property :path link))
  790. ;; Ensure DESC really exists, or set it to nil.
  791. (desc (and (not (string= desc "")) desc))
  792. (path (cond
  793. ((member type '("http" "https" "ftp"))
  794. (concat type ":" raw-path))
  795. ((and (string= type "file") (file-name-absolute-p raw-path))
  796. (concat "file:" raw-path))
  797. (t raw-path)))
  798. (email (if (string= type "mailto")
  799. (let ((text (replace-regexp-in-string
  800. "@" "@@" raw-path)))
  801. (concat text (if desc (concat "," desc))))))
  802. protocol)
  803. (cond
  804. ;; Links pointing to a headline: Find destination and build
  805. ;; appropriate referencing command.
  806. ((member type '("custom-id" "id"))
  807. (let ((destination (org-export-resolve-id-link link info)))
  808. (case (org-element-type destination)
  809. ;; Id link points to an external file.
  810. (plain-text
  811. (if desc (format "@uref{file://%s,%s}" destination desc)
  812. (format "@uref{file://%s}" destination)))
  813. ;; LINK points to a headline. Use the headline as the NODE target
  814. (headline
  815. (format "@ref{%s,%s}"
  816. (org-texinfo--get-node destination info)
  817. (or desc "")))
  818. (otherwise
  819. (let ((path (org-export-solidify-link-text path)))
  820. (if (not desc) (format "@ref{%s}" path)
  821. (format "@ref{%s,,%s}" path desc)))))))
  822. ((member type '("info"))
  823. (let* ((info-path (split-string path "[:#]"))
  824. (info-manual (car info-path))
  825. (info-node (or (cadr info-path) "top"))
  826. (title (or desc "")))
  827. (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
  828. ((member type '("fuzzy"))
  829. (let ((destination (org-export-resolve-fuzzy-link link info)))
  830. (case (org-element-type destination)
  831. ;; Id link points to an external file.
  832. (plain-text
  833. (if desc (format "@uref{file://%s,%s}" destination desc)
  834. (format "@uref{file://%s}" destination)))
  835. ;; LINK points to a headline. Use the headline as the NODE target
  836. (headline
  837. (format "@ref{%s,%s}"
  838. (org-texinfo--get-node destination info)
  839. (or desc "")))
  840. (otherwise
  841. (let ((path (org-export-solidify-link-text path)))
  842. (if (not desc) (format "@ref{%s}" path)
  843. (format "@ref{%s,,%s}" path desc)))))))
  844. ;; Special case for email addresses
  845. (email
  846. (format "@email{%s}" email))
  847. ;; External link with a description part.
  848. ((and path desc) (format "@uref{%s,%s}" path desc))
  849. ;; External link without a description part.
  850. (path (format "@uref{%s}" path))
  851. ;; No path, only description. Try to do something useful.
  852. (t (format org-texinfo-link-with-unknown-path-format desc)))))
  853. ;;;; Menu
  854. (defun org-texinfo-make-menu (scope info &optional master)
  855. "Create the menu for inclusion in the Texinfo document.
  856. SCOPE is a headline or a full parse tree. INFO is the
  857. communication channel, as a plist.
  858. When optional argument MASTER is non-nil, generate a master menu,
  859. including detailed node listing."
  860. (let ((menu (org-texinfo--build-menu scope info)))
  861. (when (org-string-nw-p menu)
  862. (org-element-normalize-string
  863. (format
  864. "@menu\n%s@end menu"
  865. (concat menu
  866. (when master
  867. (let ((detailmenu
  868. (org-texinfo--build-menu
  869. scope info
  870. (let ((toc-depth (plist-get info :with-toc)))
  871. (if (wholenump toc-depth) toc-depth
  872. org-texinfo-max-toc-depth)))))
  873. (when (org-string-nw-p detailmenu)
  874. (concat "\n@detailmenu\n"
  875. "--- The Detailed Node Listing ---\n\n"
  876. detailmenu
  877. "@end detailmenu\n"))))))))))
  878. (defun org-texinfo--build-menu (scope info &optional level)
  879. "Build menu for entries within SCOPE.
  880. SCOPE is a headline or a full parse tree. INFO is a plist
  881. containing contextual information. When optional argument LEVEL
  882. is an integer, build the menu recursively, down to this depth."
  883. (cond
  884. ((not level)
  885. (org-texinfo--format-entries (org-texinfo--menu-entries scope info) info))
  886. ((zerop level) nil)
  887. (t
  888. (org-element-normalize-string
  889. (mapconcat
  890. (lambda (h)
  891. (let ((entries (org-texinfo--menu-entries h info)))
  892. (when entries
  893. (concat
  894. (format "%s\n\n%s\n"
  895. (org-export-data (org-export-get-alt-title h info) info)
  896. (org-texinfo--format-entries entries info))
  897. (org-texinfo--build-menu h info (1- level))))))
  898. (org-texinfo--menu-entries scope info) "")))))
  899. (defun org-texinfo--format-entries (entries info)
  900. "Format all direct menu entries in SCOPE, as a string.
  901. SCOPE is either a headline or a full Org document. INFO is
  902. a plist containing contextual information."
  903. (org-element-normalize-string
  904. (mapconcat
  905. (lambda (h)
  906. (let* ((title (org-export-data
  907. (org-export-get-alt-title h info) info))
  908. (node (org-texinfo--get-node h info))
  909. (entry (concat "* " title ":"
  910. (if (string= title node) ":"
  911. (concat " " node ". "))))
  912. (desc (org-element-property :DESCRIPTION h)))
  913. (if (not desc) entry
  914. (format (format "%%-%ds %%s" org-texinfo-node-description-column)
  915. entry desc))))
  916. entries "\n")))
  917. (defun org-texinfo--menu-entries (scope info)
  918. "List direct children in SCOPE needing a menu entry.
  919. SCOPE is a headline or a full parse tree. INFO is a plist
  920. holding contextual information."
  921. (let* ((cache (or (plist-get info :texinfo-entries-cache)
  922. (plist-get (plist-put info :texinfo-entries-cache
  923. (make-hash-table :test #'eq))
  924. :texinfo-entries-cache)))
  925. (cached-entries (gethash scope cache 'no-cache)))
  926. (if (not (eq cached-entries 'no-cache)) cached-entries
  927. (puthash scope
  928. (org-element-map (org-element-contents scope) 'headline
  929. (lambda (h)
  930. (and (not (org-not-nil (org-element-property :COPYING h)))
  931. (not (org-element-property :footnote-section-p h))
  932. (not (org-export-low-level-p h info))
  933. h))
  934. info nil 'headline)
  935. cache))))
  936. ;;;; Paragraph
  937. (defun org-texinfo-paragraph (paragraph contents info)
  938. "Transcode a PARAGRAPH element from Org to Texinfo.
  939. CONTENTS is the contents of the paragraph, as a string. INFO is
  940. the plist used as a communication channel."
  941. contents)
  942. ;;;; Plain List
  943. (defun org-texinfo-plain-list (plain-list contents info)
  944. "Transcode a PLAIN-LIST element from Org to Texinfo.
  945. CONTENTS is the contents of the list. INFO is a plist holding
  946. contextual information."
  947. (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
  948. (indic (or (plist-get attr :indic) org-texinfo-def-table-markup))
  949. (table-type (plist-get attr :table-type))
  950. (type (org-element-property :type plain-list))
  951. (list-type (cond
  952. ((eq type 'ordered) "enumerate")
  953. ((eq type 'unordered) "itemize")
  954. ((member table-type '("ftable" "vtable")) table-type)
  955. (t "table"))))
  956. (format "@%s\n%s@end %s"
  957. (if (eq type 'descriptive) (concat list-type " " indic) list-type)
  958. contents
  959. list-type)))
  960. ;;;; Plain Text
  961. (defun org-texinfo-plain-text (text info)
  962. "Transcode a TEXT string from Org to Texinfo.
  963. TEXT is the string to transcode. INFO is a plist holding
  964. contextual information."
  965. ;; First protect @, { and }.
  966. (let ((output (org-texinfo--sanitize-content text)))
  967. ;; Activate smart quotes. Be sure to provide original TEXT string
  968. ;; since OUTPUT may have been modified.
  969. (when (plist-get info :with-smart-quotes)
  970. (setq output
  971. (org-export-activate-smart-quotes output :texinfo info text)))
  972. ;; LaTeX into @LaTeX{} and TeX into @TeX{}
  973. (let ((case-fold-search nil)
  974. (start 0))
  975. (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
  976. (setq output (replace-match
  977. (format "@%s{}" (match-string 1 output)) nil t output)
  978. start (match-end 0))))
  979. ;; Convert special strings.
  980. (when (plist-get info :with-special-strings)
  981. (while (string-match (regexp-quote "...") output)
  982. (setq output (replace-match "@dots{}" nil t output))))
  983. ;; Handle break preservation if required.
  984. (when (plist-get info :preserve-breaks)
  985. (setq output (replace-regexp-in-string
  986. "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
  987. ;; Return value.
  988. output))
  989. ;;;; Planning
  990. (defun org-texinfo-planning (planning contents info)
  991. "Transcode a PLANNING element from Org to Texinfo.
  992. CONTENTS is nil. INFO is a plist holding contextual
  993. information."
  994. (concat
  995. "@noindent"
  996. (mapconcat
  997. 'identity
  998. (delq nil
  999. (list
  1000. (let ((closed (org-element-property :closed planning)))
  1001. (when closed
  1002. (concat
  1003. (format "@strong{%s} " org-closed-string)
  1004. (format org-texinfo-inactive-timestamp-format
  1005. (org-translate-time
  1006. (org-element-property :raw-value closed))))))
  1007. (let ((deadline (org-element-property :deadline planning)))
  1008. (when deadline
  1009. (concat
  1010. (format "@strong{%s} " org-deadline-string)
  1011. (format org-texinfo-active-timestamp-format
  1012. (org-translate-time
  1013. (org-element-property :raw-value deadline))))))
  1014. (let ((scheduled (org-element-property :scheduled planning)))
  1015. (when scheduled
  1016. (concat
  1017. (format "@strong{%s} " org-scheduled-string)
  1018. (format org-texinfo-active-timestamp-format
  1019. (org-translate-time
  1020. (org-element-property :raw-value scheduled))))))))
  1021. " ")
  1022. "@*"))
  1023. ;;;; Property Drawer
  1024. (defun org-texinfo-property-drawer (property-drawer contents info)
  1025. "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
  1026. CONTENTS is nil. INFO is a plist holding contextual
  1027. information."
  1028. ;; The property drawer isn't exported but we want separating blank
  1029. ;; lines nonetheless.
  1030. "")
  1031. ;;;; Quote Block
  1032. (defun org-texinfo-quote-block (quote-block contents info)
  1033. "Transcode a QUOTE-BLOCK element from Org to Texinfo.
  1034. CONTENTS holds the contents of the block. INFO is a plist
  1035. holding contextual information."
  1036. (let* ((title (org-element-property :name quote-block))
  1037. (start-quote (concat "@quotation"
  1038. (if title
  1039. (format " %s" title)))))
  1040. (format "%s\n%s@end quotation" start-quote contents)))
  1041. ;;;; Quote Section
  1042. (defun org-texinfo-quote-section (quote-section contents info)
  1043. "Transcode a QUOTE-SECTION element from Org to Texinfo.
  1044. CONTENTS is nil. INFO is a plist holding contextual information."
  1045. (let ((value (org-remove-indentation
  1046. (org-element-property :value quote-section))))
  1047. (when value (format "@verbatim\n%s@end verbatim" value))))
  1048. ;;;; Radio Target
  1049. (defun org-texinfo-radio-target (radio-target text info)
  1050. "Transcode a RADIO-TARGET object from Org to Texinfo.
  1051. TEXT is the text of the target. INFO is a plist holding
  1052. contextual information."
  1053. (format "@anchor{%s}%s"
  1054. (org-export-solidify-link-text
  1055. (org-element-property :value radio-target))
  1056. text))
  1057. ;;;; Section
  1058. (defun org-texinfo-section (section contents info)
  1059. "Transcode a SECTION element from Org to Texinfo.
  1060. CONTENTS holds the contents of the section. INFO is a plist
  1061. holding contextual information."
  1062. (concat contents
  1063. (let ((parent (org-export-get-parent-headline section)))
  1064. (and parent (org-texinfo-make-menu parent info)))))
  1065. ;;;; Special Block
  1066. (defun org-texinfo-special-block (special-block contents info)
  1067. "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
  1068. CONTENTS holds the contents of the block. INFO is a plist used
  1069. as a communication channel."
  1070. contents)
  1071. ;;;; Src Block
  1072. (defun org-texinfo-src-block (src-block contents info)
  1073. "Transcode a SRC-BLOCK element from Org to Texinfo.
  1074. CONTENTS holds the contents of the item. INFO is a plist holding
  1075. contextual information."
  1076. (let ((lispp (org-string-match-p "lisp"
  1077. (org-element-property :language src-block)))
  1078. (code (org-texinfo--sanitize-content
  1079. (org-export-format-code-default src-block info))))
  1080. (format (if lispp "@lisp\n%s@end lisp" "@example\n%s@end example") code)))
  1081. ;;;; Statistics Cookie
  1082. (defun org-texinfo-statistics-cookie (statistics-cookie contents info)
  1083. "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
  1084. CONTENTS is nil. INFO is a plist holding contextual information."
  1085. (org-element-property :value statistics-cookie))
  1086. ;;;; Subscript
  1087. (defun org-texinfo-subscript (subscript contents info)
  1088. "Transcode a SUBSCRIPT object from Org to Texinfo.
  1089. CONTENTS is the contents of the object. INFO is a plist holding
  1090. contextual information."
  1091. (format "@math{_%s}" contents))
  1092. ;;;; Superscript
  1093. (defun org-texinfo-superscript (superscript contents info)
  1094. "Transcode a SUPERSCRIPT object from Org to Texinfo.
  1095. CONTENTS is the contents of the object. INFO is a plist holding
  1096. contextual information."
  1097. (format "@math{^%s}" contents))
  1098. ;;;; Table
  1099. (defun org-texinfo-table (table contents info)
  1100. "Transcode a TABLE element from Org to Texinfo.
  1101. CONTENTS is the contents of the table. INFO is a plist holding
  1102. contextual information."
  1103. (if (eq (org-element-property :type table) 'table.el)
  1104. (format "@verbatim\n%s@end verbatim"
  1105. (org-element-normalize-string
  1106. (org-element-property :value table)))
  1107. (let* ((col-width (org-export-read-attribute :attr_texinfo table :columns))
  1108. (columns
  1109. (if col-width (format "@columnfractions %s" col-width)
  1110. (org-texinfo-table-column-widths table info))))
  1111. (format "@multitable %s\n%s@end multitable"
  1112. columns
  1113. contents))))
  1114. (defun org-texinfo-table-column-widths (table info)
  1115. "Determine the largest table cell in each column to process alignment.
  1116. TABLE is the table element to transcode. INFO is a plist used as
  1117. a communication channel."
  1118. (let ((widths (make-vector (cdr (org-export-table-dimensions table info)) 0)))
  1119. (org-element-map table 'table-row
  1120. (lambda (row)
  1121. (let ((idx 0))
  1122. (org-element-map row 'table-cell
  1123. (lambda (cell)
  1124. ;; Length of the cell in the original buffer is only an
  1125. ;; approximation of the length of the cell in the
  1126. ;; output. It can sometimes fail (e.g. it considers
  1127. ;; "/a/" being larger than "ab").
  1128. (let ((w (- (org-element-property :contents-end cell)
  1129. (org-element-property :contents-begin cell))))
  1130. (aset widths idx (max w (aref widths idx))))
  1131. (incf idx))
  1132. info)))
  1133. info)
  1134. (format "{%s}" (mapconcat (lambda (w) (make-string w ?a)) widths "} {"))))
  1135. ;;;; Table Cell
  1136. (defun org-texinfo-table-cell (table-cell contents info)
  1137. "Transcode a TABLE-CELL element from Org to Texinfo.
  1138. CONTENTS is the cell contents. INFO is a plist used as
  1139. a communication channel."
  1140. (concat (if (and contents
  1141. org-texinfo-table-scientific-notation
  1142. (string-match orgtbl-exp-regexp contents))
  1143. ;; Use appropriate format string for scientific
  1144. ;; notation.
  1145. (format org-texinfo-table-scientific-notation
  1146. (match-string 1 contents)
  1147. (match-string 2 contents))
  1148. contents)
  1149. (when (org-export-get-next-element table-cell info) "\n@tab ")))
  1150. ;;;; Table Row
  1151. (defun org-texinfo-table-row (table-row contents info)
  1152. "Transcode a TABLE-ROW element from Org to Texinfo.
  1153. CONTENTS is the contents of the row. INFO is a plist used as
  1154. a communication channel."
  1155. ;; Rules are ignored since table separators are deduced from
  1156. ;; borders of the current row.
  1157. (when (eq (org-element-property :type table-row) 'standard)
  1158. (let ((rowgroup-tag
  1159. (if (and (= 1 (org-export-table-row-group table-row info))
  1160. (org-export-table-has-header-p
  1161. (org-export-get-parent-table table-row) info))
  1162. "@headitem "
  1163. "@item ")))
  1164. (concat rowgroup-tag contents "\n"))))
  1165. ;;;; Target
  1166. (defun org-texinfo-target (target contents info)
  1167. "Transcode a TARGET object from Org to Texinfo.
  1168. CONTENTS is nil. INFO is a plist holding contextual
  1169. information."
  1170. (format "@anchor{%s}"
  1171. (org-export-solidify-link-text (org-element-property :value target))))
  1172. ;;;; Timestamp
  1173. (defun org-texinfo-timestamp (timestamp contents info)
  1174. "Transcode a TIMESTAMP object from Org to Texinfo.
  1175. CONTENTS is nil. INFO is a plist holding contextual
  1176. information."
  1177. (let ((value (org-texinfo-plain-text
  1178. (org-timestamp-translate timestamp) info)))
  1179. (case (org-element-property :type timestamp)
  1180. ((active active-range)
  1181. (format org-texinfo-active-timestamp-format value))
  1182. ((inactive inactive-range)
  1183. (format org-texinfo-inactive-timestamp-format value))
  1184. (t (format org-texinfo-diary-timestamp-format value)))))
  1185. ;;;; Verbatim
  1186. (defun org-texinfo-verbatim (verbatim contents info)
  1187. "Transcode a VERBATIM object from Org to Texinfo.
  1188. CONTENTS is nil. INFO is a plist used as a communication
  1189. channel."
  1190. (org-texinfo--text-markup (org-element-property :value verbatim) 'verbatim))
  1191. ;;;; Verse Block
  1192. (defun org-texinfo-verse-block (verse-block contents info)
  1193. "Transcode a VERSE-BLOCK element from Org to Texinfo.
  1194. CONTENTS is verse block contents. INFO is a plist holding
  1195. contextual information."
  1196. (format "@display\n%s@end display" contents))
  1197. ;;; Interactive functions
  1198. (defun org-texinfo-export-to-texinfo
  1199. (&optional async subtreep visible-only body-only ext-plist)
  1200. "Export current buffer to a Texinfo file.
  1201. If narrowing is active in the current buffer, only export its
  1202. narrowed part.
  1203. If a region is active, export that region.
  1204. A non-nil optional argument ASYNC means the process should happen
  1205. asynchronously. The resulting file should be accessible through
  1206. the `org-export-stack' interface.
  1207. When optional argument SUBTREEP is non-nil, export the sub-tree
  1208. at point, extracting information from the headline properties
  1209. first.
  1210. When optional argument VISIBLE-ONLY is non-nil, don't export
  1211. contents of hidden elements.
  1212. When optional argument BODY-ONLY is non-nil, only write code
  1213. between \"\\begin{document}\" and \"\\end{document}\".
  1214. EXT-PLIST, when provided, is a property list with external
  1215. parameters overriding Org default settings, but still inferior to
  1216. file-local settings.
  1217. Return output file's name."
  1218. (interactive)
  1219. (let ((outfile (org-export-output-file-name ".texi" subtreep))
  1220. (org-export-coding-system `,org-texinfo-coding-system))
  1221. (org-export-to-file 'texinfo outfile
  1222. async subtreep visible-only body-only ext-plist)))
  1223. (defun org-texinfo-export-to-info
  1224. (&optional async subtreep visible-only body-only ext-plist)
  1225. "Export current buffer to Texinfo then process through to INFO.
  1226. If narrowing is active in the current buffer, only export its
  1227. narrowed part.
  1228. If a region is active, export that region.
  1229. A non-nil optional argument ASYNC means the process should happen
  1230. asynchronously. The resulting file should be accessible through
  1231. the `org-export-stack' interface.
  1232. When optional argument SUBTREEP is non-nil, export the sub-tree
  1233. at point, extracting information from the headline properties
  1234. first.
  1235. When optional argument VISIBLE-ONLY is non-nil, don't export
  1236. contents of hidden elements.
  1237. When optional argument BODY-ONLY is non-nil, only write code
  1238. between \"\\begin{document}\" and \"\\end{document}\".
  1239. EXT-PLIST, when provided, is a property list with external
  1240. parameters overriding Org default settings, but still inferior to
  1241. file-local settings.
  1242. When optional argument PUB-DIR is set, use it as the publishing
  1243. directory.
  1244. Return INFO file's name."
  1245. (interactive)
  1246. (let ((outfile (org-export-output-file-name ".texi" subtreep))
  1247. (org-export-coding-system `,org-texinfo-coding-system))
  1248. (org-export-to-file 'texinfo outfile
  1249. async subtreep visible-only body-only ext-plist
  1250. (lambda (file) (org-texinfo-compile file)))))
  1251. ;;;###autoload
  1252. (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
  1253. "Publish an org file to Texinfo.
  1254. FILENAME is the filename of the Org file to be published. PLIST
  1255. is the property list for the given project. PUB-DIR is the
  1256. publishing directory.
  1257. Return output file name."
  1258. (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
  1259. ;;;###autoload
  1260. (defun org-texinfo-convert-region-to-texinfo ()
  1261. "Assume the current region has org-mode syntax, and convert it to Texinfo.
  1262. This can be used in any buffer. For example, you can write an
  1263. itemized list in org-mode syntax in an Texinfo buffer and use
  1264. this command to convert it."
  1265. (interactive)
  1266. (org-export-replace-region-by 'texinfo))
  1267. (defun org-texinfo-compile (file)
  1268. "Compile a texinfo file.
  1269. FILE is the name of the file being compiled. Processing is
  1270. done through the command specified in `org-texinfo-info-process'.
  1271. Return INFO file name or an error if it couldn't be produced."
  1272. (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
  1273. (full-name (file-truename file))
  1274. (out-dir (file-name-directory file))
  1275. ;; Properly set working directory for compilation.
  1276. (default-directory (if (file-name-absolute-p file)
  1277. (file-name-directory full-name)
  1278. default-directory))
  1279. errors)
  1280. (message (format "Processing Texinfo file %s..." file))
  1281. (save-window-excursion
  1282. ;; Replace %b, %f and %o with appropriate values in each command
  1283. ;; before applying it. Output is redirected to "*Org INFO
  1284. ;; Texinfo Output*" buffer.
  1285. (let ((outbuf (get-buffer-create "*Org INFO Texinfo Output*")))
  1286. (dolist (command org-texinfo-info-process)
  1287. (shell-command
  1288. (replace-regexp-in-string
  1289. "%b" (shell-quote-argument base-name)
  1290. (replace-regexp-in-string
  1291. "%f" (shell-quote-argument full-name)
  1292. (replace-regexp-in-string
  1293. "%o" (shell-quote-argument out-dir) command t t) t t) t t)
  1294. outbuf))
  1295. ;; Collect standard errors from output buffer.
  1296. (setq errors (org-texinfo-collect-errors outbuf)))
  1297. (let ((infofile (concat out-dir base-name ".info")))
  1298. ;; Check for process failure. Provide collected errors if
  1299. ;; possible.
  1300. (if (not (file-exists-p infofile))
  1301. (error (concat (format "INFO file %s wasn't produced" infofile)
  1302. (when errors (concat ": " errors))))
  1303. ;; Else remove log files, when specified, and signal end of
  1304. ;; process to user, along with any error encountered.
  1305. (when org-texinfo-remove-logfiles
  1306. (dolist (ext org-texinfo-logfiles-extensions)
  1307. (let ((file (concat out-dir base-name "." ext)))
  1308. (when (file-exists-p file) (delete-file file)))))
  1309. (message (concat "Process completed"
  1310. (if (not errors) "."
  1311. (concat " with errors: " errors)))))
  1312. ;; Return output file name.
  1313. infofile))))
  1314. (defun org-texinfo-collect-errors (buffer)
  1315. "Collect some kind of errors from \"makeinfo\" command output.
  1316. BUFFER is the buffer containing output.
  1317. Return collected error types as a string, or nil if there was
  1318. none."
  1319. (with-current-buffer buffer
  1320. (save-excursion
  1321. (goto-char (point-min))
  1322. ;; Find final "makeinfo" run.
  1323. (when t
  1324. (let ((case-fold-search t)
  1325. (errors ""))
  1326. (when (save-excursion
  1327. (re-search-forward "perhaps incorrect sectioning?" nil t))
  1328. (setq errors (concat errors " [incorrect sectioning]")))
  1329. (when (save-excursion
  1330. (re-search-forward "missing close brace" nil t))
  1331. (setq errors (concat errors " [syntax error]")))
  1332. (when (save-excursion
  1333. (re-search-forward "Unknown command" nil t))
  1334. (setq errors (concat errors " [undefined @command]")))
  1335. (when (save-excursion
  1336. (re-search-forward "No matching @end" nil t))
  1337. (setq errors (concat errors " [block incomplete]")))
  1338. (when (save-excursion
  1339. (re-search-forward "requires a sectioning" nil t))
  1340. (setq errors (concat errors " [invalid section command]")))
  1341. (when (save-excursion
  1342. (re-search-forward "\\[unexpected\]" nil t))
  1343. (setq errors (concat errors " [unexpected error]")))
  1344. (when (save-excursion
  1345. (re-search-forward "misplaced " nil t))
  1346. (setq errors (concat errors " [syntax error]")))
  1347. (and (org-string-nw-p errors) (org-trim errors)))))))
  1348. (provide 'ox-texinfo)
  1349. ;; Local variables:
  1350. ;; generated-autoload-file: "org-loaddefs.el"
  1351. ;; End:
  1352. ;;; ox-texinfo.el ends here