ox-texinfo.el 65 KB

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