ox-texinfo.el 66 KB

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