ox-texinfo.el 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  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. (pre-blanks
  683. (make-string (org-element-property :pre-blank headline) ?\n)))
  684. (cond
  685. ;; Case 1: This is a footnote section: ignore it.
  686. ((org-element-property :footnote-section-p headline) nil)
  687. ;; Case 2: This is the `copying' section: ignore it
  688. ;; This is used elsewhere.
  689. ((org-not-nil (org-element-property :COPYING headline)) nil)
  690. ;; Case 3: An index. If it matches one of the known indexes,
  691. ;; print it as such following the contents, otherwise
  692. ;; print the contents and leave the index up to the user.
  693. (index
  694. (concat node
  695. (format
  696. section-fmt
  697. full-text
  698. (concat pre-blanks contents (and (org-string-nw-p contents) "\n")
  699. (if (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
  700. (concat "@printindex " index))))))
  701. ;; Case 4: This is a deep sub-tree: export it as a list item.
  702. ;; Also export as items headlines for which no section
  703. ;; format has been found.
  704. ((or (not section-fmt) (org-export-low-level-p headline info))
  705. ;; Build the real contents of the sub-tree.
  706. (let ((low-level-body
  707. (concat
  708. ;; If the headline is the first sibling, start a list.
  709. (when (org-export-first-sibling-p headline info)
  710. (format "@%s\n" (if numberedp 'enumerate 'itemize)))
  711. ;; Itemize headline
  712. "@item\n" full-text "\n" pre-blanks contents)))
  713. ;; If headline is not the last sibling simply return
  714. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
  715. ;; blank line.
  716. (if (not (org-export-last-sibling-p headline info)) low-level-body
  717. (replace-regexp-in-string
  718. "[ \t\n]*\\'"
  719. (format "\n@end %s" (if numberedp 'enumerate 'itemize))
  720. low-level-body))))
  721. ;; Case 5: Standard headline. Export it as a section.
  722. (t
  723. (concat node
  724. (format section-fmt full-text (concat pre-blanks contents)))))))
  725. ;;;; Inline Src Block
  726. (defun org-texinfo-inline-src-block (inline-src-block contents info)
  727. "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
  728. CONTENTS holds the contents of the item. INFO is a plist holding
  729. contextual information."
  730. (let* ((code (org-element-property :value inline-src-block))
  731. (separator (org-texinfo--find-verb-separator code)))
  732. (concat "@verb{" separator code separator "}")))
  733. ;;;; Inlinetask
  734. (defun org-texinfo-inlinetask (inlinetask contents info)
  735. "Transcode an INLINETASK element from Org to Texinfo.
  736. CONTENTS holds the contents of the block. INFO is a plist
  737. holding contextual information."
  738. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  739. (todo (and (plist-get info :with-todo-keywords)
  740. (let ((todo (org-element-property :todo-keyword inlinetask)))
  741. (and todo (org-export-data todo info)))))
  742. (todo-type (org-element-property :todo-type inlinetask))
  743. (tags (and (plist-get info :with-tags)
  744. (org-export-get-tags inlinetask info)))
  745. (priority (and (plist-get info :with-priority)
  746. (org-element-property :priority inlinetask))))
  747. ;; If `org-texinfo-format-inlinetask-function' is provided, call it
  748. ;; with appropriate arguments.
  749. (if (not (eq org-texinfo-format-inlinetask-function 'ignore))
  750. (funcall org-texinfo-format-inlinetask-function
  751. todo todo-type priority title tags contents)
  752. ;; Otherwise, use a default template.
  753. (let ((full-title
  754. (concat
  755. (when todo (format "@strong{%s} " todo))
  756. (when priority (format "#%c " priority))
  757. title
  758. (when tags (format ":%s:"
  759. (mapconcat 'identity tags ":"))))))
  760. (format (concat "@center %s\n\n"
  761. "%s"
  762. "\n")
  763. full-title contents)))))
  764. ;;;; Italic
  765. (defun org-texinfo-italic (italic contents info)
  766. "Transcode ITALIC from Org to Texinfo.
  767. CONTENTS is the text with italic markup. INFO is a plist holding
  768. contextual information."
  769. (org-texinfo--text-markup contents 'italic))
  770. ;;;; Item
  771. (defun org-texinfo-item (item contents info)
  772. "Transcode an ITEM element from Org to Texinfo.
  773. CONTENTS holds the contents of the item. INFO is a plist holding
  774. contextual information."
  775. (format "@item%s\n%s"
  776. (let ((tag (org-element-property :tag item)))
  777. (if tag (concat " " (org-export-data tag info)) ""))
  778. (or contents "")))
  779. ;;;; Keyword
  780. (defun org-texinfo-keyword (keyword contents info)
  781. "Transcode a KEYWORD element from Org to Texinfo.
  782. CONTENTS is nil. INFO is a plist holding contextual information."
  783. (let ((key (org-element-property :key keyword))
  784. (value (org-element-property :value keyword)))
  785. (cond
  786. ((string= key "TEXINFO") value)
  787. ((string= key "CINDEX") (format "@cindex %s" value))
  788. ((string= key "FINDEX") (format "@findex %s" value))
  789. ((string= key "KINDEX") (format "@kindex %s" value))
  790. ((string= key "PINDEX") (format "@pindex %s" value))
  791. ((string= key "TINDEX") (format "@tindex %s" value))
  792. ((string= key "VINDEX") (format "@vindex %s" value)))))
  793. ;;;; Line Break
  794. (defun org-texinfo-line-break (line-break contents info)
  795. "Transcode a LINE-BREAK object from Org to Texinfo.
  796. CONTENTS is nil. INFO is a plist holding contextual information."
  797. "@*\n")
  798. ;;;; Link
  799. (defun org-texinfo-link (link desc info)
  800. "Transcode a LINK object from Org to Texinfo.
  801. DESC is the description part of the link, or the empty string.
  802. INFO is a plist holding contextual information. See
  803. `org-export-data'."
  804. (let* ((type (org-element-property :type link))
  805. (raw-path (org-element-property :path link))
  806. ;; Ensure DESC really exists, or set it to nil.
  807. (desc (and (not (string= desc "")) desc))
  808. (path (cond
  809. ((member type '("http" "https" "ftp"))
  810. (concat type ":" raw-path))
  811. ((and (string= type "file") (file-name-absolute-p raw-path))
  812. (concat "file:" raw-path))
  813. (t raw-path)))
  814. (email (if (string= type "mailto")
  815. (let ((text (replace-regexp-in-string
  816. "@" "@@" raw-path)))
  817. (concat text (if desc (concat "," desc))))))
  818. protocol)
  819. (cond
  820. ;; Links pointing to a headline: Find destination and build
  821. ;; appropriate referencing command.
  822. ((member type '("custom-id" "id"))
  823. (let ((destination (org-export-resolve-id-link link info)))
  824. (case (org-element-type destination)
  825. ;; Id link points to an external file.
  826. (plain-text
  827. (if desc (format "@uref{file://%s,%s}" destination desc)
  828. (format "@uref{file://%s}" destination)))
  829. ;; LINK points to a headline. Use the headline as the NODE target
  830. (headline
  831. (format "@ref{%s,%s}"
  832. (org-texinfo--get-node destination info)
  833. (or desc "")))
  834. (otherwise
  835. (let ((path (org-export-solidify-link-text path)))
  836. (if (not desc) (format "@ref{%s}" path)
  837. (format "@ref{%s,,%s}" path desc)))))))
  838. ((member type '("info"))
  839. (let* ((info-path (split-string path "[:#]"))
  840. (info-manual (car info-path))
  841. (info-node (or (cadr info-path) "top"))
  842. (title (or desc "")))
  843. (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
  844. ((member type '("fuzzy"))
  845. (let ((destination (org-export-resolve-fuzzy-link link info)))
  846. (case (org-element-type destination)
  847. ;; Id link points to an external file.
  848. (plain-text
  849. (if desc (format "@uref{file://%s,%s}" destination desc)
  850. (format "@uref{file://%s}" destination)))
  851. ;; LINK points to a headline. Use the headline as the NODE target
  852. (headline
  853. (format "@ref{%s,%s}"
  854. (org-texinfo--get-node destination info)
  855. (or desc "")))
  856. (otherwise
  857. (let ((path (org-export-solidify-link-text path)))
  858. (if (not desc) (format "@ref{%s}" path)
  859. (format "@ref{%s,,%s}" path desc)))))))
  860. ;; Special case for email addresses
  861. (email
  862. (format "@email{%s}" email))
  863. ;; External link with a description part.
  864. ((and path desc) (format "@uref{%s,%s}" path desc))
  865. ;; External link without a description part.
  866. (path (format "@uref{%s}" path))
  867. ;; No path, only description. Try to do something useful.
  868. (t (format org-texinfo-link-with-unknown-path-format desc)))))
  869. ;;;; Menu
  870. (defun org-texinfo-make-menu (scope info &optional master)
  871. "Create the menu for inclusion in the Texinfo document.
  872. SCOPE is a headline or a full parse tree. INFO is the
  873. communication channel, as a plist.
  874. When optional argument MASTER is non-nil, generate a master menu,
  875. including detailed node listing."
  876. (let ((menu (org-texinfo--build-menu scope info)))
  877. (when (org-string-nw-p menu)
  878. (org-element-normalize-string
  879. (format
  880. "@menu\n%s@end menu"
  881. (concat menu
  882. (when master
  883. (let ((detailmenu
  884. (org-texinfo--build-menu
  885. scope info
  886. (let ((toc-depth (plist-get info :with-toc)))
  887. (if (wholenump toc-depth) toc-depth
  888. org-texinfo-max-toc-depth)))))
  889. (when (org-string-nw-p detailmenu)
  890. (concat "\n@detailmenu\n"
  891. "--- The Detailed Node Listing ---\n\n"
  892. detailmenu
  893. "@end detailmenu\n"))))))))))
  894. (defun org-texinfo--build-menu (scope info &optional level)
  895. "Build menu for entries within SCOPE.
  896. SCOPE is a headline or a full parse tree. INFO is a plist
  897. containing contextual information. When optional argument LEVEL
  898. is an integer, build the menu recursively, down to this depth."
  899. (cond
  900. ((not level)
  901. (org-texinfo--format-entries (org-texinfo--menu-entries scope info) info))
  902. ((zerop level) nil)
  903. (t
  904. (org-element-normalize-string
  905. (mapconcat
  906. (lambda (h)
  907. (let ((entries (org-texinfo--menu-entries h info)))
  908. (when entries
  909. (concat
  910. (format "%s\n\n%s\n"
  911. (org-export-data (org-export-get-alt-title h info) info)
  912. (org-texinfo--format-entries entries info))
  913. (org-texinfo--build-menu h info (1- level))))))
  914. (org-texinfo--menu-entries scope info) "")))))
  915. (defun org-texinfo--format-entries (entries info)
  916. "Format all direct menu entries in SCOPE, as a string.
  917. SCOPE is either a headline or a full Org document. INFO is
  918. a plist containing contextual information."
  919. (org-element-normalize-string
  920. (mapconcat
  921. (lambda (h)
  922. (let* ((title (org-export-data
  923. (org-export-get-alt-title h info) info))
  924. (node (org-texinfo--get-node h info))
  925. (entry (concat "* " title ":"
  926. (if (string= title node) ":"
  927. (concat " " node ". "))))
  928. (desc (org-element-property :DESCRIPTION h)))
  929. (if (not desc) entry
  930. (format (format "%%-%ds %%s" org-texinfo-node-description-column)
  931. entry desc))))
  932. entries "\n")))
  933. (defun org-texinfo--menu-entries (scope info)
  934. "List direct children in SCOPE needing a menu entry.
  935. SCOPE is a headline or a full parse tree. INFO is a plist
  936. holding contextual information."
  937. (let* ((cache (or (plist-get info :texinfo-entries-cache)
  938. (plist-get (plist-put info :texinfo-entries-cache
  939. (make-hash-table :test #'eq))
  940. :texinfo-entries-cache)))
  941. (cached-entries (gethash scope cache 'no-cache)))
  942. (if (not (eq cached-entries 'no-cache)) cached-entries
  943. (puthash scope
  944. (org-element-map (org-element-contents scope) 'headline
  945. (lambda (h)
  946. (and (not (org-not-nil (org-element-property :COPYING h)))
  947. (not (org-element-property :footnote-section-p h))
  948. (not (org-export-low-level-p h info))
  949. h))
  950. info nil 'headline)
  951. cache))))
  952. ;;;; Paragraph
  953. (defun org-texinfo-paragraph (paragraph contents info)
  954. "Transcode a PARAGRAPH element from Org to Texinfo.
  955. CONTENTS is the contents of the paragraph, as a string. INFO is
  956. the plist used as a communication channel."
  957. contents)
  958. ;;;; Plain List
  959. (defun org-texinfo-plain-list (plain-list contents info)
  960. "Transcode a PLAIN-LIST element from Org to Texinfo.
  961. CONTENTS is the contents of the list. INFO is a plist holding
  962. contextual information."
  963. (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
  964. (indic (or (plist-get attr :indic) org-texinfo-def-table-markup))
  965. (table-type (plist-get attr :table-type))
  966. (type (org-element-property :type plain-list))
  967. (list-type (cond
  968. ((eq type 'ordered) "enumerate")
  969. ((eq type 'unordered) "itemize")
  970. ((member table-type '("ftable" "vtable")) table-type)
  971. (t "table"))))
  972. (format "@%s\n%s@end %s"
  973. (if (eq type 'descriptive) (concat list-type " " indic) list-type)
  974. contents
  975. list-type)))
  976. ;;;; Plain Text
  977. (defun org-texinfo-plain-text (text info)
  978. "Transcode a TEXT string from Org to Texinfo.
  979. TEXT is the string to transcode. INFO is a plist holding
  980. contextual information."
  981. ;; First protect @, { and }.
  982. (let ((output (org-texinfo--sanitize-content text)))
  983. ;; Activate smart quotes. Be sure to provide original TEXT string
  984. ;; since OUTPUT may have been modified.
  985. (when (plist-get info :with-smart-quotes)
  986. (setq output
  987. (org-export-activate-smart-quotes output :texinfo info text)))
  988. ;; LaTeX into @LaTeX{} and TeX into @TeX{}
  989. (let ((case-fold-search nil)
  990. (start 0))
  991. (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
  992. (setq output (replace-match
  993. (format "@%s{}" (match-string 1 output)) nil t output)
  994. start (match-end 0))))
  995. ;; Convert special strings.
  996. (when (plist-get info :with-special-strings)
  997. (while (string-match (regexp-quote "...") output)
  998. (setq output (replace-match "@dots{}" nil t output))))
  999. ;; Handle break preservation if required.
  1000. (when (plist-get info :preserve-breaks)
  1001. (setq output (replace-regexp-in-string
  1002. "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
  1003. ;; Return value.
  1004. output))
  1005. ;;;; Planning
  1006. (defun org-texinfo-planning (planning contents info)
  1007. "Transcode a PLANNING element from Org to Texinfo.
  1008. CONTENTS is nil. INFO is a plist holding contextual
  1009. information."
  1010. (concat
  1011. "@noindent"
  1012. (mapconcat
  1013. 'identity
  1014. (delq nil
  1015. (list
  1016. (let ((closed (org-element-property :closed planning)))
  1017. (when closed
  1018. (concat
  1019. (format "@strong{%s} " org-closed-string)
  1020. (format org-texinfo-inactive-timestamp-format
  1021. (org-translate-time
  1022. (org-element-property :raw-value closed))))))
  1023. (let ((deadline (org-element-property :deadline planning)))
  1024. (when deadline
  1025. (concat
  1026. (format "@strong{%s} " org-deadline-string)
  1027. (format org-texinfo-active-timestamp-format
  1028. (org-translate-time
  1029. (org-element-property :raw-value deadline))))))
  1030. (let ((scheduled (org-element-property :scheduled planning)))
  1031. (when scheduled
  1032. (concat
  1033. (format "@strong{%s} " org-scheduled-string)
  1034. (format org-texinfo-active-timestamp-format
  1035. (org-translate-time
  1036. (org-element-property :raw-value scheduled))))))))
  1037. " ")
  1038. "@*"))
  1039. ;;;; Property Drawer
  1040. (defun org-texinfo-property-drawer (property-drawer contents info)
  1041. "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
  1042. CONTENTS is nil. INFO is a plist holding contextual
  1043. information."
  1044. ;; The property drawer isn't exported but we want separating blank
  1045. ;; lines nonetheless.
  1046. "")
  1047. ;;;; Quote Block
  1048. (defun org-texinfo-quote-block (quote-block contents info)
  1049. "Transcode a QUOTE-BLOCK element from Org to Texinfo.
  1050. CONTENTS holds the contents of the block. INFO is a plist
  1051. holding contextual information."
  1052. (let* ((title (org-element-property :name quote-block))
  1053. (start-quote (concat "@quotation"
  1054. (if title
  1055. (format " %s" title)))))
  1056. (format "%s\n%s@end quotation" start-quote contents)))
  1057. ;;;; Quote Section
  1058. (defun org-texinfo-quote-section (quote-section contents info)
  1059. "Transcode a QUOTE-SECTION element from Org to Texinfo.
  1060. CONTENTS is nil. INFO is a plist holding contextual information."
  1061. (let ((value (org-remove-indentation
  1062. (org-element-property :value quote-section))))
  1063. (when value (format "@verbatim\n%s@end verbatim" value))))
  1064. ;;;; Radio Target
  1065. (defun org-texinfo-radio-target (radio-target text info)
  1066. "Transcode a RADIO-TARGET object from Org to Texinfo.
  1067. TEXT is the text of the target. INFO is a plist holding
  1068. contextual information."
  1069. (format "@anchor{%s}%s"
  1070. (org-export-solidify-link-text
  1071. (org-element-property :value radio-target))
  1072. text))
  1073. ;;;; Section
  1074. (defun org-texinfo-section (section contents info)
  1075. "Transcode a SECTION element from Org to Texinfo.
  1076. CONTENTS holds the contents of the section. INFO is a plist
  1077. holding contextual information."
  1078. (concat contents
  1079. (let ((parent (org-export-get-parent-headline section)))
  1080. (and parent (org-texinfo-make-menu parent info)))))
  1081. ;;;; Special Block
  1082. (defun org-texinfo-special-block (special-block contents info)
  1083. "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
  1084. CONTENTS holds the contents of the block. INFO is a plist used
  1085. as a communication channel."
  1086. contents)
  1087. ;;;; Src Block
  1088. (defun org-texinfo-src-block (src-block contents info)
  1089. "Transcode a SRC-BLOCK element from Org to Texinfo.
  1090. CONTENTS holds the contents of the item. INFO is a plist holding
  1091. contextual information."
  1092. (let* ((lang (org-element-property :language src-block))
  1093. (lisp-p (string-match-p "lisp" lang))
  1094. (src-contents (org-texinfo--sanitize-content
  1095. (org-export-format-code-default src-block info))))
  1096. (cond
  1097. ;; Case 1. Lisp Block
  1098. (lisp-p
  1099. (format "@lisp\n%s@end lisp"
  1100. src-contents))
  1101. ;; Case 2. Other blocks
  1102. (t
  1103. (format "@example\n%s@end example"
  1104. src-contents)))))
  1105. ;;;; Statistics Cookie
  1106. (defun org-texinfo-statistics-cookie (statistics-cookie contents info)
  1107. "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
  1108. CONTENTS is nil. INFO is a plist holding contextual information."
  1109. (org-element-property :value statistics-cookie))
  1110. ;;;; Subscript
  1111. (defun org-texinfo-subscript (subscript contents info)
  1112. "Transcode a SUBSCRIPT object from Org to Texinfo.
  1113. CONTENTS is the contents of the object. INFO is a plist holding
  1114. contextual information."
  1115. (format "@math{_%s}" contents))
  1116. ;;;; Superscript
  1117. (defun org-texinfo-superscript (superscript contents info)
  1118. "Transcode a SUPERSCRIPT object from Org to Texinfo.
  1119. CONTENTS is the contents of the object. INFO is a plist holding
  1120. contextual information."
  1121. (format "@math{^%s}" contents))
  1122. ;;;; Table
  1123. (defun org-texinfo-table (table contents info)
  1124. "Transcode a TABLE element from Org to Texinfo.
  1125. CONTENTS is the contents of the table. INFO is a plist holding
  1126. contextual information."
  1127. (if (eq (org-element-property :type table) 'table.el)
  1128. (format "@verbatim\n%s@end verbatim"
  1129. (org-element-normalize-string
  1130. (org-element-property :value table)))
  1131. (let* ((col-width (org-export-read-attribute :attr_texinfo table :columns))
  1132. (columns
  1133. (if col-width (format "@columnfractions %s" col-width)
  1134. (org-texinfo-table-column-widths table info))))
  1135. (format "@multitable %s\n%s@end multitable"
  1136. columns
  1137. contents))))
  1138. (defun org-texinfo-table-column-widths (table info)
  1139. "Determine the largest table cell in each column to process alignment.
  1140. TABLE is the table element to transcode. INFO is a plist used as
  1141. a communication channel."
  1142. (let ((widths (make-vector (cdr (org-export-table-dimensions table info)) 0)))
  1143. (org-element-map table 'table-row
  1144. (lambda (row)
  1145. (let ((idx 0))
  1146. (org-element-map row 'table-cell
  1147. (lambda (cell)
  1148. ;; Length of the cell in the original buffer is only an
  1149. ;; approximation of the length of the cell in the
  1150. ;; output. It can sometimes fail (e.g. it considers
  1151. ;; "/a/" being larger than "ab").
  1152. (let ((w (- (org-element-property :contents-end cell)
  1153. (org-element-property :contents-begin cell))))
  1154. (aset widths idx (max w (aref widths idx))))
  1155. (incf idx))
  1156. info)))
  1157. info)
  1158. (format "{%s}" (mapconcat (lambda (w) (make-string w ?a)) widths "} {"))))
  1159. ;;;; Table Cell
  1160. (defun org-texinfo-table-cell (table-cell contents info)
  1161. "Transcode a TABLE-CELL element from Org to Texinfo.
  1162. CONTENTS is the cell contents. INFO is a plist used as
  1163. a communication channel."
  1164. (concat (if (and contents
  1165. org-texinfo-table-scientific-notation
  1166. (string-match orgtbl-exp-regexp contents))
  1167. ;; Use appropriate format string for scientific
  1168. ;; notation.
  1169. (format org-texinfo-table-scientific-notation
  1170. (match-string 1 contents)
  1171. (match-string 2 contents))
  1172. contents)
  1173. (when (org-export-get-next-element table-cell info) "\n@tab ")))
  1174. ;;;; Table Row
  1175. (defun org-texinfo-table-row (table-row contents info)
  1176. "Transcode a TABLE-ROW element from Org to Texinfo.
  1177. CONTENTS is the contents of the row. INFO is a plist used as
  1178. a communication channel."
  1179. ;; Rules are ignored since table separators are deduced from
  1180. ;; borders of the current row.
  1181. (when (eq (org-element-property :type table-row) 'standard)
  1182. (let ((rowgroup-tag
  1183. (if (and (= 1 (org-export-table-row-group table-row info))
  1184. (org-export-table-has-header-p
  1185. (org-export-get-parent-table table-row) info))
  1186. "@headitem "
  1187. "@item ")))
  1188. (concat rowgroup-tag contents "\n"))))
  1189. ;;;; Target
  1190. (defun org-texinfo-target (target contents info)
  1191. "Transcode a TARGET object from Org to Texinfo.
  1192. CONTENTS is nil. INFO is a plist holding contextual
  1193. information."
  1194. (format "@anchor{%s}"
  1195. (org-export-solidify-link-text (org-element-property :value target))))
  1196. ;;;; Timestamp
  1197. (defun org-texinfo-timestamp (timestamp contents info)
  1198. "Transcode a TIMESTAMP object from Org to Texinfo.
  1199. CONTENTS is nil. INFO is a plist holding contextual
  1200. information."
  1201. (let ((value (org-texinfo-plain-text
  1202. (org-timestamp-translate timestamp) info)))
  1203. (case (org-element-property :type timestamp)
  1204. ((active active-range)
  1205. (format org-texinfo-active-timestamp-format value))
  1206. ((inactive inactive-range)
  1207. (format org-texinfo-inactive-timestamp-format value))
  1208. (t (format org-texinfo-diary-timestamp-format value)))))
  1209. ;;;; Verbatim
  1210. (defun org-texinfo-verbatim (verbatim contents info)
  1211. "Transcode a VERBATIM object from Org to Texinfo.
  1212. CONTENTS is nil. INFO is a plist used as a communication
  1213. channel."
  1214. (org-texinfo--text-markup (org-element-property :value verbatim) 'verbatim))
  1215. ;;;; Verse Block
  1216. (defun org-texinfo-verse-block (verse-block contents info)
  1217. "Transcode a VERSE-BLOCK element from Org to Texinfo.
  1218. CONTENTS is verse block contents. INFO is a plist holding
  1219. contextual information."
  1220. (format "@display\n%s@end display" contents))
  1221. ;;; Interactive functions
  1222. (defun org-texinfo-export-to-texinfo
  1223. (&optional async subtreep visible-only body-only ext-plist)
  1224. "Export current buffer to a Texinfo file.
  1225. If narrowing is active in the current buffer, only export its
  1226. narrowed part.
  1227. If a region is active, export that region.
  1228. A non-nil optional argument ASYNC means the process should happen
  1229. asynchronously. The resulting file should be accessible through
  1230. the `org-export-stack' interface.
  1231. When optional argument SUBTREEP is non-nil, export the sub-tree
  1232. at point, extracting information from the headline properties
  1233. first.
  1234. When optional argument VISIBLE-ONLY is non-nil, don't export
  1235. contents of hidden elements.
  1236. When optional argument BODY-ONLY is non-nil, only write code
  1237. between \"\\begin{document}\" and \"\\end{document}\".
  1238. EXT-PLIST, when provided, is a property list with external
  1239. parameters overriding Org default settings, but still inferior to
  1240. file-local settings.
  1241. Return output file's name."
  1242. (interactive)
  1243. (let ((outfile (org-export-output-file-name ".texi" subtreep))
  1244. (org-export-coding-system `,org-texinfo-coding-system))
  1245. (org-export-to-file 'texinfo outfile
  1246. async subtreep visible-only body-only ext-plist)))
  1247. (defun org-texinfo-export-to-info
  1248. (&optional async subtreep visible-only body-only ext-plist)
  1249. "Export current buffer to Texinfo then process through to INFO.
  1250. If narrowing is active in the current buffer, only export its
  1251. narrowed part.
  1252. If a region is active, export that region.
  1253. A non-nil optional argument ASYNC means the process should happen
  1254. asynchronously. The resulting file should be accessible through
  1255. the `org-export-stack' interface.
  1256. When optional argument SUBTREEP is non-nil, export the sub-tree
  1257. at point, extracting information from the headline properties
  1258. first.
  1259. When optional argument VISIBLE-ONLY is non-nil, don't export
  1260. contents of hidden elements.
  1261. When optional argument BODY-ONLY is non-nil, only write code
  1262. between \"\\begin{document}\" and \"\\end{document}\".
  1263. EXT-PLIST, when provided, is a property list with external
  1264. parameters overriding Org default settings, but still inferior to
  1265. file-local settings.
  1266. When optional argument PUB-DIR is set, use it as the publishing
  1267. directory.
  1268. Return INFO file's name."
  1269. (interactive)
  1270. (let ((outfile (org-export-output-file-name ".texi" subtreep))
  1271. (org-export-coding-system `,org-texinfo-coding-system))
  1272. (org-export-to-file 'texinfo outfile
  1273. async subtreep visible-only body-only ext-plist
  1274. (lambda (file) (org-texinfo-compile file)))))
  1275. ;;;###autoload
  1276. (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
  1277. "Publish an org file to Texinfo.
  1278. FILENAME is the filename of the Org file to be published. PLIST
  1279. is the property list for the given project. PUB-DIR is the
  1280. publishing directory.
  1281. Return output file name."
  1282. (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
  1283. ;;;###autoload
  1284. (defun org-texinfo-convert-region-to-texinfo ()
  1285. "Assume the current region has org-mode syntax, and convert it to Texinfo.
  1286. This can be used in any buffer. For example, you can write an
  1287. itemized list in org-mode syntax in an Texinfo buffer and use
  1288. this command to convert it."
  1289. (interactive)
  1290. (org-export-replace-region-by 'texinfo))
  1291. (defun org-texinfo-compile (file)
  1292. "Compile a texinfo file.
  1293. FILE is the name of the file being compiled. Processing is
  1294. done through the command specified in `org-texinfo-info-process'.
  1295. Return INFO file name or an error if it couldn't be produced."
  1296. (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
  1297. (full-name (file-truename file))
  1298. (out-dir (file-name-directory file))
  1299. ;; Properly set working directory for compilation.
  1300. (default-directory (if (file-name-absolute-p file)
  1301. (file-name-directory full-name)
  1302. default-directory))
  1303. errors)
  1304. (message (format "Processing Texinfo file %s..." file))
  1305. (save-window-excursion
  1306. ;; Replace %b, %f and %o with appropriate values in each command
  1307. ;; before applying it. Output is redirected to "*Org INFO
  1308. ;; Texinfo Output*" buffer.
  1309. (let ((outbuf (get-buffer-create "*Org INFO Texinfo Output*")))
  1310. (dolist (command org-texinfo-info-process)
  1311. (shell-command
  1312. (replace-regexp-in-string
  1313. "%b" (shell-quote-argument base-name)
  1314. (replace-regexp-in-string
  1315. "%f" (shell-quote-argument full-name)
  1316. (replace-regexp-in-string
  1317. "%o" (shell-quote-argument out-dir) command t t) t t) t t)
  1318. outbuf))
  1319. ;; Collect standard errors from output buffer.
  1320. (setq errors (org-texinfo-collect-errors outbuf)))
  1321. (let ((infofile (concat out-dir base-name ".info")))
  1322. ;; Check for process failure. Provide collected errors if
  1323. ;; possible.
  1324. (if (not (file-exists-p infofile))
  1325. (error (concat (format "INFO file %s wasn't produced" infofile)
  1326. (when errors (concat ": " errors))))
  1327. ;; Else remove log files, when specified, and signal end of
  1328. ;; process to user, along with any error encountered.
  1329. (when org-texinfo-remove-logfiles
  1330. (dolist (ext org-texinfo-logfiles-extensions)
  1331. (let ((file (concat out-dir base-name "." ext)))
  1332. (when (file-exists-p file) (delete-file file)))))
  1333. (message (concat "Process completed"
  1334. (if (not errors) "."
  1335. (concat " with errors: " errors)))))
  1336. ;; Return output file name.
  1337. infofile))))
  1338. (defun org-texinfo-collect-errors (buffer)
  1339. "Collect some kind of errors from \"makeinfo\" command output.
  1340. BUFFER is the buffer containing output.
  1341. Return collected error types as a string, or nil if there was
  1342. none."
  1343. (with-current-buffer buffer
  1344. (save-excursion
  1345. (goto-char (point-min))
  1346. ;; Find final "makeinfo" run.
  1347. (when t
  1348. (let ((case-fold-search t)
  1349. (errors ""))
  1350. (when (save-excursion
  1351. (re-search-forward "perhaps incorrect sectioning?" nil t))
  1352. (setq errors (concat errors " [incorrect sectioning]")))
  1353. (when (save-excursion
  1354. (re-search-forward "missing close brace" nil t))
  1355. (setq errors (concat errors " [syntax error]")))
  1356. (when (save-excursion
  1357. (re-search-forward "Unknown command" nil t))
  1358. (setq errors (concat errors " [undefined @command]")))
  1359. (when (save-excursion
  1360. (re-search-forward "No matching @end" nil t))
  1361. (setq errors (concat errors " [block incomplete]")))
  1362. (when (save-excursion
  1363. (re-search-forward "requires a sectioning" nil t))
  1364. (setq errors (concat errors " [invalid section command]")))
  1365. (when (save-excursion
  1366. (re-search-forward "\\[unexpected\]" nil t))
  1367. (setq errors (concat errors " [unexpected error]")))
  1368. (when (save-excursion
  1369. (re-search-forward "misplaced " nil t))
  1370. (setq errors (concat errors " [syntax error]")))
  1371. (and (org-string-nw-p errors) (org-trim errors)))))))
  1372. (provide 'ox-texinfo)
  1373. ;; Local variables:
  1374. ;; generated-autoload-file: "org-loaddefs.el"
  1375. ;; End:
  1376. ;;; ox-texinfo.el ends here