ox-texinfo.el 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854
  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 . (lambda (&rest args) ""))
  59. (comment-block . (lambda (&rest args) ""))
  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 ""
  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 'ignore
  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 listings.
  200. If a node title is greater than this length, the description will
  201. be placed after the end of the title."
  202. :group 'org-export-texinfo
  203. :type 'integer)
  204. ;;; Footnotes
  205. ;;
  206. ;; Footnotes are inserted directly
  207. ;;; Timestamps
  208. (defcustom org-texinfo-active-timestamp-format "@emph{%s}"
  209. "A printf format string to be applied to active timestamps."
  210. :group 'org-export-texinfo
  211. :type 'string)
  212. (defcustom org-texinfo-inactive-timestamp-format "@emph{%s}"
  213. "A printf format string to be applied to inactive timestamps."
  214. :group 'org-export-texinfo
  215. :type 'string)
  216. (defcustom org-texinfo-diary-timestamp-format "@emph{%s}"
  217. "A printf format string to be applied to diary timestamps."
  218. :group 'org-export-texinfo
  219. :type 'string)
  220. ;;; Links
  221. (defcustom org-texinfo-link-with-unknown-path-format "@indicateurl{%s}"
  222. "Format string for links with unknown path type."
  223. :group 'org-export-texinfo
  224. :type 'string)
  225. ;;; Tables
  226. (defcustom org-texinfo-tables-verbatim nil
  227. "When non-nil, tables are exported verbatim."
  228. :group 'org-export-texinfo
  229. :type 'boolean)
  230. (defcustom org-texinfo-table-scientific-notation "%s\\,(%s)"
  231. "Format string to display numbers in scientific notation.
  232. The format should have \"%s\" twice, for mantissa and exponent
  233. \(i.e. \"%s\\\\times10^{%s}\").
  234. When nil, no transformation is made."
  235. :group 'org-export-texinfo
  236. :type '(choice
  237. (string :tag "Format string")
  238. (const :tag "No formatting" nil)))
  239. (defcustom org-texinfo-def-table-markup "@samp"
  240. "Default setting for @table environments."
  241. :group 'org-export-texinfo
  242. :type 'string)
  243. ;;; Text markup
  244. (defcustom org-texinfo-text-markup-alist '((bold . "@strong{%s}")
  245. (code . code)
  246. (italic . "@emph{%s}")
  247. (verbatim . verb)
  248. (comment . "@c %s"))
  249. "Alist of Texinfo expressions to convert text markup.
  250. The key must be a symbol among `bold', `italic' and `comment'.
  251. The value is a formatting string to wrap fontified text with.
  252. Value can also be set to the following symbols: `verb' and
  253. `code'. For the former, Org will use \"@verb\" to
  254. create a format string and select a delimiter character that
  255. isn't in the string. For the latter, Org will use \"@code\"
  256. to typeset and try to protect special characters.
  257. If no association can be found for a given markup, text will be
  258. returned as-is."
  259. :group 'org-export-texinfo
  260. :type 'alist
  261. :options '(bold code italic verbatim comment))
  262. ;;; Drawers
  263. (defcustom org-texinfo-format-drawer-function
  264. (lambda (name contents) contents)
  265. "Function called to format a drawer in Texinfo code.
  266. The function must accept two parameters:
  267. NAME the drawer name, like \"LOGBOOK\"
  268. CONTENTS the contents of the drawer.
  269. The function should return the string to be exported.
  270. The default function simply returns the value of CONTENTS."
  271. :group 'org-export-texinfo
  272. :version "24.4"
  273. :package-version '(Org . "8.2")
  274. :type 'function)
  275. ;;; Inlinetasks
  276. (defcustom org-texinfo-format-inlinetask-function 'ignore
  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 behavior:
  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. (header (nth 1 (assoc (plist-get info :texinfo-class)
  574. org-texinfo-classes)))
  575. ;; Copying data is the contents of the first headline in
  576. ;; parse tree with a non-nil copying property.
  577. (copying (org-element-map (plist-get info :parse-tree) 'headline
  578. (lambda (hl)
  579. (and (org-not-nil (org-element-property :COPYING hl))
  580. (org-element-contents hl)))
  581. info t)))
  582. (concat
  583. ;; Header
  584. header "\n"
  585. "@c %**start of header\n"
  586. ;; Filename and Title
  587. "@setfilename " info-filename "\n"
  588. (format "@settitle %s\n" title)
  589. ;; Coding system.
  590. (format
  591. "@documentencoding %s\n"
  592. (catch 'coding-system
  593. (let ((case-fold-search t)
  594. (name (symbol-name (or org-texinfo-coding-system
  595. buffer-file-coding-system))))
  596. (dolist (system org-texinfo-supported-coding-systems "UTF-8")
  597. (when (org-string-match-p (regexp-quote system) name)
  598. (throw 'coding-system system))))))
  599. ;; Language.
  600. (format "@documentlanguage %s\n" (plist-get info :language))
  601. ;; Additional header options set by #+TEXINFO_HEADER.
  602. (let ((texinfo-header (plist-get info :texinfo-header)))
  603. (and texinfo-header (org-element-normalize-string texinfo-header)))
  604. "@c %**end of header\n\n"
  605. ;; Additional options set by #+TEXINFO_POST_HEADER.
  606. (let ((texinfo-post-header (plist-get info :texinfo-post-header)))
  607. (and texinfo-post-header
  608. (org-element-normalize-string texinfo-post-header)))
  609. ;; Copying.
  610. (and copying
  611. (format "@copying\n%s@end copying\n\n"
  612. (org-element-normalize-string
  613. (org-export-data copying info))))
  614. ;; Info directory information. Only supply if both title and
  615. ;; category are provided.
  616. (let ((dircat (plist-get info :texinfo-dircat))
  617. (dirtitle
  618. (let ((title (plist-get info :texinfo-dirtitle)))
  619. (and title
  620. (string-match "^\\(?:\\* \\)?\\(.*?\\)\\(\\.\\)?$" title)
  621. (format "* %s." (match-string 1 title))))))
  622. (when (and dircat dirtitle)
  623. (concat "@dircategory " dircat "\n"
  624. "@direntry\n"
  625. (let ((dirdesc
  626. (let ((desc (plist-get info :texinfo-dirdesc)))
  627. (cond ((not desc) nil)
  628. ((org-string-match-p "\\.$" desc) desc)
  629. (t (concat desc "."))))))
  630. (if dirdesc (format "%-23s %s" dirtitle dirdesc) dirtitle))
  631. "\n"
  632. "@end direntry\n\n")))
  633. ;; Title
  634. "@finalout\n"
  635. "@titlepage\n"
  636. "@title " title "\n"
  637. (let ((subtitle (plist-get info :subtitle)))
  638. (and subtitle
  639. (org-element-normalize-string
  640. (replace-regexp-in-string "^" "@subtitle " subtitle))))
  641. (when (plist-get info :with-author)
  642. (concat
  643. ;; Primary author.
  644. (let ((author (org-string-nw-p
  645. (org-export-data (plist-get info :author) info)))
  646. (email (and (plist-get info :with-email)
  647. (org-string-nw-p
  648. (org-export-data (plist-get info :email) info)))))
  649. (cond ((and author email)
  650. (format "@author %s (@email{%s})\n" author email))
  651. (author (format "@author %s\n" author))
  652. (email (format "@author @email{%s}\n" email))))
  653. ;; Other authors.
  654. (let ((subauthor (plist-get info :subauthor)))
  655. (and subauthor
  656. (org-element-normalize-string
  657. (replace-regexp-in-string "^" "@author " subauthor))))))
  658. (and copying "@page\n@vskip 0pt plus 1filll\n@insertcopying\n")
  659. "@end titlepage\n\n"
  660. ;; Table of contents.
  661. (and (plist-get info :with-toc) "@contents\n\n")
  662. ;; Configure Top Node when not for Tex
  663. "@ifnottex\n"
  664. "@node Top\n"
  665. "@top " title " Manual\n"
  666. (and copying "@insertcopying\n")
  667. "@end ifnottex\n\n"
  668. ;; Menu.
  669. (let ((menu (org-texinfo-make-menu info 'main))
  670. (detail-menu (org-texinfo-make-menu info 'detailed)))
  671. (and menu
  672. (concat "@menu\n"
  673. menu "\n"
  674. (and detail-menu
  675. (concat "\n@detailmenu\n"
  676. " --- The Detailed Node Listing ---\n"
  677. detail-menu "\n"
  678. "@end detailmenu\n"))
  679. "@end menu\n\n")))
  680. ;; Document's body.
  681. contents "\n"
  682. ;; Creator.
  683. (case (plist-get info :with-creator)
  684. ((nil) nil)
  685. (comment (format "@c %s\n" (plist-get info :creator)))
  686. (otherwise (concat (plist-get info :creator) "\n")))
  687. ;; Document end.
  688. "@bye")))
  689. ;;; Transcode Functions
  690. ;;; Bold
  691. (defun org-texinfo-bold (bold contents info)
  692. "Transcode BOLD from Org to Texinfo.
  693. CONTENTS is the text with bold markup. INFO is a plist holding
  694. contextual information."
  695. (org-texinfo--text-markup contents 'bold))
  696. ;;; Center Block
  697. (defun org-texinfo-center-block (center-block contents info)
  698. "Transcode a CENTER-BLOCK element from Org to Texinfo.
  699. CONTENTS holds the contents of the block. INFO is a plist used
  700. as a communication channel."
  701. contents)
  702. ;;; Clock
  703. (defun org-texinfo-clock (clock contents info)
  704. "Transcode a CLOCK element from Org to Texinfo.
  705. CONTENTS is nil. INFO is a plist holding contextual
  706. information."
  707. (concat
  708. "@noindent"
  709. (format "@strong{%s} " org-clock-string)
  710. (format org-texinfo-inactive-timestamp-format
  711. (concat (org-translate-time
  712. (org-element-property :raw-value
  713. (org-element-property :value clock)))
  714. (let ((time (org-element-property :duration clock)))
  715. (and time (format " (%s)" time)))))
  716. "@*"))
  717. ;;; Code
  718. (defun org-texinfo-code (code contents info)
  719. "Transcode a CODE object from Org to Texinfo.
  720. CONTENTS is nil. INFO is a plist used as a communication
  721. channel."
  722. (org-texinfo--text-markup (org-element-property :value code) 'code))
  723. ;;; Drawer
  724. (defun org-texinfo-drawer (drawer contents info)
  725. "Transcode a DRAWER element from Org to Texinfo.
  726. CONTENTS holds the contents of the block. INFO is a plist
  727. holding contextual information."
  728. (let* ((name (org-element-property :drawer-name drawer))
  729. (output (funcall org-texinfo-format-drawer-function
  730. name contents)))
  731. output))
  732. ;;; Dynamic Block
  733. (defun org-texinfo-dynamic-block (dynamic-block contents info)
  734. "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
  735. CONTENTS holds the contents of the block. INFO is a plist
  736. holding contextual information. See `org-export-data'."
  737. contents)
  738. ;;; Entity
  739. (defun org-texinfo-entity (entity contents info)
  740. "Transcode an ENTITY object from Org to Texinfo.
  741. CONTENTS are the definition itself. INFO is a plist holding
  742. contextual information."
  743. (let ((ent (org-element-property :latex entity)))
  744. (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
  745. ;;; Example Block
  746. (defun org-texinfo-example-block (example-block contents info)
  747. "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
  748. CONTENTS is nil. INFO is a plist holding contextual
  749. information."
  750. (format "@verbatim\n%s@end verbatim"
  751. (org-export-format-code-default example-block info)))
  752. ;;; Export Block
  753. (defun org-texinfo-export-block (export-block contents info)
  754. "Transcode a EXPORT-BLOCK element from Org to Texinfo.
  755. CONTENTS is nil. INFO is a plist holding contextual information."
  756. (when (string= (org-element-property :type export-block) "TEXINFO")
  757. (org-remove-indentation (org-element-property :value export-block))))
  758. ;;; Export Snippet
  759. (defun org-texinfo-export-snippet (export-snippet contents info)
  760. "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
  761. CONTENTS is nil. INFO is a plist holding contextual information."
  762. (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
  763. (org-element-property :value export-snippet)))
  764. ;;; Fixed Width
  765. (defun org-texinfo-fixed-width (fixed-width contents info)
  766. "Transcode a FIXED-WIDTH element from Org to Texinfo.
  767. CONTENTS is nil. INFO is a plist holding contextual information."
  768. (format "@example\n%s\n@end example"
  769. (org-remove-indentation
  770. (org-texinfo--sanitize-content
  771. (org-element-property :value fixed-width)))))
  772. ;;; Footnote Reference
  773. ;;
  774. (defun org-texinfo-footnote-reference (footnote contents info)
  775. "Create a footnote reference for FOOTNOTE.
  776. FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
  777. plist holding contextual information."
  778. (let ((def (org-export-get-footnote-definition footnote info)))
  779. (format "@footnote{%s}"
  780. (org-trim (org-export-data def info)))))
  781. ;;; Headline
  782. (defun org-texinfo-headline (headline contents info)
  783. "Transcode a HEADLINE element from Org to Texinfo.
  784. CONTENTS holds the contents of the headline. INFO is a plist
  785. holding contextual information."
  786. (let* ((class (plist-get info :texinfo-class))
  787. (level (org-export-get-relative-level headline info))
  788. (numberedp (org-export-numbered-headline-p headline info))
  789. (class-sectioning (assoc class org-texinfo-classes))
  790. ;; Find the index type, if any
  791. (index (org-element-property :INDEX headline))
  792. ;; Check if it is an appendix
  793. (appendix (org-element-property :APPENDIX headline))
  794. ;; Retrieve headline text
  795. (text (org-texinfo--sanitize-headline
  796. (org-element-property :title headline) info))
  797. ;; Create node info, to insert it before section formatting.
  798. ;; Use custom menu title if present
  799. (node (format "@node %s\n" (org-texinfo--get-node headline info)))
  800. ;; Menus must be generated with first child, otherwise they
  801. ;; will not nest properly
  802. (menu (let* ((first (org-export-first-sibling-p headline info))
  803. (parent (org-export-get-parent-headline headline))
  804. (title (org-texinfo--sanitize-headline
  805. (org-element-property :title parent) info))
  806. heading listing
  807. (tree (plist-get info :parse-tree)))
  808. (if first
  809. (org-element-map (plist-get info :parse-tree) 'headline
  810. (lambda (ref)
  811. (if (member title (org-element-property :title ref))
  812. (push ref heading)))
  813. info t))
  814. (setq listing (org-texinfo--build-menu
  815. (car heading) level info))
  816. (if listing
  817. (setq listing (replace-regexp-in-string
  818. "%" "%%" listing)
  819. listing (format
  820. "\n@menu\n%s\n@end menu\n\n" listing))
  821. 'nil)))
  822. ;; Section formatting will set two placeholders: one for the
  823. ;; title and the other for the contents.
  824. (section-fmt
  825. (let ((sec (if (and (symbolp (nth 2 class-sectioning))
  826. (fboundp (nth 2 class-sectioning)))
  827. (funcall (nth 2 class-sectioning) level numberedp)
  828. (nth (1+ level) class-sectioning))))
  829. (cond
  830. ;; No section available for that LEVEL.
  831. ((not sec) nil)
  832. ;; Section format directly returned by a function.
  833. ((stringp sec) sec)
  834. ;; (numbered-section . unnumbered-section)
  835. ((not (consp (cdr sec)))
  836. (cond
  837. ;;If an index, always unnumbered
  838. (index
  839. (concat menu node (cdr sec) "\n%s"))
  840. (appendix
  841. (concat menu node (replace-regexp-in-string
  842. "unnumbered"
  843. "appendix"
  844. (cdr sec)) "\n%s"))
  845. ;; Otherwise number as needed.
  846. (t
  847. (concat menu node
  848. (funcall
  849. (if numberedp #'car #'cdr) sec) "\n%s")))))))
  850. (todo
  851. (and (plist-get info :with-todo-keywords)
  852. (let ((todo (org-element-property :todo-keyword headline)))
  853. (and todo (org-export-data todo info)))))
  854. (todo-type (and todo (org-element-property :todo-type headline)))
  855. (tags (and (plist-get info :with-tags)
  856. (org-export-get-tags headline info)))
  857. (priority (and (plist-get info :with-priority)
  858. (org-element-property :priority headline)))
  859. ;; Create the headline text along with a no-tag version. The
  860. ;; latter is required to remove tags from table of contents.
  861. (full-text (org-texinfo--sanitize-content
  862. (if (not (eq org-texinfo-format-headline-function 'ignore))
  863. ;; User-defined formatting function.
  864. (funcall org-texinfo-format-headline-function
  865. todo todo-type priority text tags)
  866. ;; Default formatting.
  867. (concat
  868. (when todo
  869. (format "@strong{%s} " todo))
  870. (when priority (format "@emph{#%s} " priority))
  871. text
  872. (when tags
  873. (format " :%s:"
  874. (mapconcat 'identity tags ":")))))))
  875. (full-text-no-tag
  876. (org-texinfo--sanitize-content
  877. (if (not (eq org-texinfo-format-headline-function 'ignore))
  878. ;; User-defined formatting function.
  879. (funcall org-texinfo-format-headline-function
  880. todo todo-type priority text nil)
  881. ;; Default formatting.
  882. (concat
  883. (when todo (format "@strong{%s} " todo))
  884. (when priority (format "@emph{#%c} " priority))
  885. text))))
  886. (pre-blanks
  887. (make-string (org-element-property :pre-blank headline) 10)))
  888. (cond
  889. ;; Case 1: This is a footnote section: ignore it.
  890. ((org-element-property :footnote-section-p headline) nil)
  891. ;; Case 2: This is the `copying' section: ignore it
  892. ;; This is used elsewhere.
  893. ((org-element-property :COPYING headline) nil)
  894. ;; Case 3: An index. If it matches one of the known indexes,
  895. ;; print it as such following the contents, otherwise
  896. ;; print the contents and leave the index up to the user.
  897. (index
  898. (format
  899. section-fmt full-text
  900. (concat pre-blanks contents "\n"
  901. (if (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
  902. (concat "@printindex " index)))))
  903. ;; Case 4: This is a deep sub-tree: export it as a list item.
  904. ;; Also export as items headlines for which no section
  905. ;; format has been found.
  906. ((or (not section-fmt) (org-export-low-level-p headline info))
  907. ;; Build the real contents of the sub-tree.
  908. (let ((low-level-body
  909. (concat
  910. ;; If the headline is the first sibling, start a list.
  911. (when (org-export-first-sibling-p headline info)
  912. (format "@%s\n" (if numberedp 'enumerate 'itemize)))
  913. ;; Itemize headline
  914. "@item\n" full-text "\n" pre-blanks contents)))
  915. ;; If headline is not the last sibling simply return
  916. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
  917. ;; blank line.
  918. (if (not (org-export-last-sibling-p headline info)) low-level-body
  919. (replace-regexp-in-string
  920. "[ \t\n]*\\'"
  921. (format "\n@end %s" (if numberedp 'enumerate 'itemize))
  922. low-level-body))))
  923. ;; Case 5: Standard headline. Export it as a section.
  924. (t
  925. (cond
  926. ((not (and tags (eq (plist-get info :with-tags) 'not-in-toc)))
  927. ;; Regular section. Use specified format string.
  928. (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
  929. (concat pre-blanks contents)))
  930. ((string-match "\\`@\\(.*?\\){" section-fmt)
  931. ;; If tags should be removed from table of contents, insert
  932. ;; title without tags as an alternative heading in sectioning
  933. ;; command.
  934. (format (replace-match (concat (match-string 1 section-fmt) "[%s]")
  935. nil nil section-fmt 1)
  936. ;; Replace square brackets with parenthesis since
  937. ;; square brackets are not supported in optional
  938. ;; arguments.
  939. (replace-regexp-in-string
  940. "\\[" "("
  941. (replace-regexp-in-string
  942. "\\]" ")"
  943. full-text-no-tag))
  944. full-text
  945. (concat pre-blanks contents)))
  946. (t
  947. ;; Impossible to add an alternative heading. Fallback to
  948. ;; regular sectioning format string.
  949. (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
  950. (concat pre-blanks contents))))))))
  951. ;;; Inline Src Block
  952. (defun org-texinfo-inline-src-block (inline-src-block contents info)
  953. "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
  954. CONTENTS holds the contents of the item. INFO is a plist holding
  955. contextual information."
  956. (let* ((code (org-element-property :value inline-src-block))
  957. (separator (org-texinfo--find-verb-separator code)))
  958. (concat "@verb{" separator code separator "}")))
  959. ;;; Inlinetask
  960. (defun org-texinfo-inlinetask (inlinetask contents info)
  961. "Transcode an INLINETASK element from Org to Texinfo.
  962. CONTENTS holds the contents of the block. INFO is a plist
  963. holding contextual information."
  964. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  965. (todo (and (plist-get info :with-todo-keywords)
  966. (let ((todo (org-element-property :todo-keyword inlinetask)))
  967. (and todo (org-export-data todo info)))))
  968. (todo-type (org-element-property :todo-type inlinetask))
  969. (tags (and (plist-get info :with-tags)
  970. (org-export-get-tags inlinetask info)))
  971. (priority (and (plist-get info :with-priority)
  972. (org-element-property :priority inlinetask))))
  973. ;; If `org-texinfo-format-inlinetask-function' is provided, call it
  974. ;; with appropriate arguments.
  975. (if (not (eq org-texinfo-format-inlinetask-function 'ignore))
  976. (funcall org-texinfo-format-inlinetask-function
  977. todo todo-type priority title tags contents)
  978. ;; Otherwise, use a default template.
  979. (let ((full-title
  980. (concat
  981. (when todo (format "@strong{%s} " todo))
  982. (when priority (format "#%c " priority))
  983. title
  984. (when tags (format ":%s:"
  985. (mapconcat 'identity tags ":"))))))
  986. (format (concat "@center %s\n\n"
  987. "%s"
  988. "\n")
  989. full-title contents)))))
  990. ;;; Italic
  991. (defun org-texinfo-italic (italic contents info)
  992. "Transcode ITALIC from Org to Texinfo.
  993. CONTENTS is the text with italic markup. INFO is a plist holding
  994. contextual information."
  995. (org-texinfo--text-markup contents 'italic))
  996. ;;; Item
  997. (defun org-texinfo-item (item contents info)
  998. "Transcode an ITEM element from Org to Texinfo.
  999. CONTENTS holds the contents of the item. INFO is a plist holding
  1000. contextual information."
  1001. (let* ((tag (org-element-property :tag item))
  1002. (desc (org-export-data tag info)))
  1003. (concat "\n@item " (if tag desc) "\n"
  1004. (and contents (org-trim contents)) "\n")))
  1005. ;;; Keyword
  1006. (defun org-texinfo-keyword (keyword contents info)
  1007. "Transcode a KEYWORD element from Org to Texinfo.
  1008. CONTENTS is nil. INFO is a plist holding contextual information."
  1009. (let ((key (org-element-property :key keyword))
  1010. (value (org-element-property :value keyword)))
  1011. (cond
  1012. ((string= key "TEXINFO") value)
  1013. ((string= key "CINDEX") (format "@cindex %s" value))
  1014. ((string= key "FINDEX") (format "@findex %s" value))
  1015. ((string= key "KINDEX") (format "@kindex %s" value))
  1016. ((string= key "PINDEX") (format "@pindex %s" value))
  1017. ((string= key "TINDEX") (format "@tindex %s" value))
  1018. ((string= key "VINDEX") (format "@vindex %s" value)))))
  1019. ;;; Line Break
  1020. (defun org-texinfo-line-break (line-break contents info)
  1021. "Transcode a LINE-BREAK object from Org to Texinfo.
  1022. CONTENTS is nil. INFO is a plist holding contextual information."
  1023. "@*\n")
  1024. ;;; Link
  1025. (defun org-texinfo-link (link desc info)
  1026. "Transcode a LINK object from Org to Texinfo.
  1027. DESC is the description part of the link, or the empty string.
  1028. INFO is a plist holding contextual information. See
  1029. `org-export-data'."
  1030. (let* ((type (org-element-property :type link))
  1031. (raw-path (org-element-property :path link))
  1032. ;; Ensure DESC really exists, or set it to nil.
  1033. (desc (and (not (string= desc "")) desc))
  1034. (path (cond
  1035. ((member type '("http" "https" "ftp"))
  1036. (concat type ":" raw-path))
  1037. ((and (string= type "file") (file-name-absolute-p raw-path))
  1038. (concat "file:" raw-path))
  1039. (t raw-path)))
  1040. (email (if (string= type "mailto")
  1041. (let ((text (replace-regexp-in-string
  1042. "@" "@@" raw-path)))
  1043. (concat text (if desc (concat "," desc))))))
  1044. protocol)
  1045. (cond
  1046. ;; Links pointing to a headline: Find destination and build
  1047. ;; appropriate referencing command.
  1048. ((member type '("custom-id" "id"))
  1049. (let ((destination (org-export-resolve-id-link link info)))
  1050. (case (org-element-type destination)
  1051. ;; Id link points to an external file.
  1052. (plain-text
  1053. (if desc (format "@uref{file://%s,%s}" destination desc)
  1054. (format "@uref{file://%s}" destination)))
  1055. ;; LINK points to a headline. Use the headline as the NODE target
  1056. (headline
  1057. (format "@ref{%s,%s}"
  1058. (org-texinfo--get-node destination info)
  1059. (or desc "")))
  1060. (otherwise
  1061. (let ((path (org-export-solidify-link-text path)))
  1062. (if (not desc) (format "@ref{%s}" path)
  1063. (format "@ref{%s,,%s}" path desc)))))))
  1064. ((member type '("info"))
  1065. (let* ((info-path (split-string path "[:#]"))
  1066. (info-manual (car info-path))
  1067. (info-node (or (cadr info-path) "top"))
  1068. (title (or desc "")))
  1069. (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
  1070. ((member type '("fuzzy"))
  1071. (let ((destination (org-export-resolve-fuzzy-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. ;; Special case for email addresses
  1087. (email
  1088. (format "@email{%s}" email))
  1089. ;; External link with a description part.
  1090. ((and path desc) (format "@uref{%s,%s}" path desc))
  1091. ;; External link without a description part.
  1092. (path (format "@uref{%s}" path))
  1093. ;; No path, only description. Try to do something useful.
  1094. (t (format org-texinfo-link-with-unknown-path-format desc)))))
  1095. ;;; Menu
  1096. (defun org-texinfo-make-menu (info level)
  1097. "Create the menu for inclusion in the texifo document.
  1098. INFO is the parsed buffer that contains the headlines. LEVEL
  1099. determines whether to make the main menu, or the detailed menu.
  1100. This is only used for generating the primary menu. In-Node menus
  1101. are generated directly."
  1102. (let ((parse (plist-get info :parse-tree)))
  1103. (cond
  1104. ;; Generate the main menu
  1105. ((eq level 'main) (org-texinfo--build-menu parse 1 info))
  1106. ;; Generate the detailed (recursive) menu
  1107. ((eq level 'detailed)
  1108. ;; Requires recursion
  1109. ;;(org-texinfo--build-detailed-menu parse top info)
  1110. (org-texinfo--build-menu parse 1 info 'detailed)))))
  1111. ;;; Paragraph
  1112. (defun org-texinfo-paragraph (paragraph contents info)
  1113. "Transcode a PARAGRAPH element from Org to Texinfo.
  1114. CONTENTS is the contents of the paragraph, as a string. INFO is
  1115. the plist used as a communication channel."
  1116. contents)
  1117. ;;; Plain List
  1118. (defun org-texinfo-plain-list (plain-list contents info)
  1119. "Transcode a PLAIN-LIST element from Org to Texinfo.
  1120. CONTENTS is the contents of the list. INFO is a plist holding
  1121. contextual information."
  1122. (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
  1123. (indic (or (plist-get attr :indic)
  1124. org-texinfo-def-table-markup))
  1125. (type (org-element-property :type plain-list))
  1126. (table-type (plist-get attr :table-type))
  1127. ;; Ensure valid texinfo table type.
  1128. (table-type (if (member table-type '("ftable" "vtable")) table-type
  1129. "table"))
  1130. (list-type (cond
  1131. ((eq type 'ordered) "enumerate")
  1132. ((eq type 'unordered) "itemize")
  1133. ((eq type 'descriptive) table-type))))
  1134. (format "@%s%s\n@end %s"
  1135. (if (eq type 'descriptive)
  1136. (concat list-type " " indic)
  1137. list-type)
  1138. contents
  1139. list-type)))
  1140. ;;; Plain Text
  1141. (defun org-texinfo-plain-text (text info)
  1142. "Transcode a TEXT string from Org to Texinfo.
  1143. TEXT is the string to transcode. INFO is a plist holding
  1144. contextual information."
  1145. ;; First protect @, { and }.
  1146. (let ((output (org-texinfo--sanitize-content text)))
  1147. ;; Activate smart quotes. Be sure to provide original TEXT string
  1148. ;; since OUTPUT may have been modified.
  1149. (when (plist-get info :with-smart-quotes)
  1150. (setq output
  1151. (org-export-activate-smart-quotes output :texinfo info text)))
  1152. ;; LaTeX into @LaTeX{} and TeX into @TeX{}
  1153. (let ((case-fold-search nil)
  1154. (start 0))
  1155. (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
  1156. (setq output (replace-match
  1157. (format "@%s{}" (match-string 1 output)) nil t output)
  1158. start (match-end 0))))
  1159. ;; Convert special strings.
  1160. (when (plist-get info :with-special-strings)
  1161. (while (string-match (regexp-quote "...") output)
  1162. (setq output (replace-match "@dots{}" nil t output))))
  1163. ;; Handle break preservation if required.
  1164. (when (plist-get info :preserve-breaks)
  1165. (setq output (replace-regexp-in-string
  1166. "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
  1167. ;; Return value.
  1168. output))
  1169. ;;; Planning
  1170. (defun org-texinfo-planning (planning contents info)
  1171. "Transcode a PLANNING element from Org to Texinfo.
  1172. CONTENTS is nil. INFO is a plist holding contextual
  1173. information."
  1174. (concat
  1175. "@noindent"
  1176. (mapconcat
  1177. 'identity
  1178. (delq nil
  1179. (list
  1180. (let ((closed (org-element-property :closed planning)))
  1181. (when closed
  1182. (concat
  1183. (format "@strong{%s} " org-closed-string)
  1184. (format org-texinfo-inactive-timestamp-format
  1185. (org-translate-time
  1186. (org-element-property :raw-value closed))))))
  1187. (let ((deadline (org-element-property :deadline planning)))
  1188. (when deadline
  1189. (concat
  1190. (format "@strong{%s} " org-deadline-string)
  1191. (format org-texinfo-active-timestamp-format
  1192. (org-translate-time
  1193. (org-element-property :raw-value deadline))))))
  1194. (let ((scheduled (org-element-property :scheduled planning)))
  1195. (when scheduled
  1196. (concat
  1197. (format "@strong{%s} " org-scheduled-string)
  1198. (format org-texinfo-active-timestamp-format
  1199. (org-translate-time
  1200. (org-element-property :raw-value scheduled))))))))
  1201. " ")
  1202. "@*"))
  1203. ;;; Property Drawer
  1204. (defun org-texinfo-property-drawer (property-drawer contents info)
  1205. "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
  1206. CONTENTS is nil. INFO is a plist holding contextual
  1207. information."
  1208. ;; The property drawer isn't exported but we want separating blank
  1209. ;; lines nonetheless.
  1210. "")
  1211. ;;; Quote Block
  1212. (defun org-texinfo-quote-block (quote-block contents info)
  1213. "Transcode a QUOTE-BLOCK element from Org to Texinfo.
  1214. CONTENTS holds the contents of the block. INFO is a plist
  1215. holding contextual information."
  1216. (let* ((title (org-element-property :name quote-block))
  1217. (start-quote (concat "@quotation"
  1218. (if title
  1219. (format " %s" title)))))
  1220. (format "%s\n%s@end quotation" start-quote contents)))
  1221. ;;; Quote Section
  1222. (defun org-texinfo-quote-section (quote-section contents info)
  1223. "Transcode a QUOTE-SECTION element from Org to Texinfo.
  1224. CONTENTS is nil. INFO is a plist holding contextual information."
  1225. (let ((value (org-remove-indentation
  1226. (org-element-property :value quote-section))))
  1227. (when value (format "@verbatim\n%s@end verbatim" value))))
  1228. ;;; Radio Target
  1229. (defun org-texinfo-radio-target (radio-target text info)
  1230. "Transcode a RADIO-TARGET object from Org to Texinfo.
  1231. TEXT is the text of the target. INFO is a plist holding
  1232. contextual information."
  1233. (format "@anchor{%s}%s"
  1234. (org-export-solidify-link-text
  1235. (org-element-property :value radio-target))
  1236. text))
  1237. ;;; Section
  1238. (defun org-texinfo-section (section contents info)
  1239. "Transcode a SECTION element from Org to Texinfo.
  1240. CONTENTS holds the contents of the section. INFO is a plist
  1241. holding contextual information."
  1242. contents)
  1243. ;;; Special Block
  1244. (defun org-texinfo-special-block (special-block contents info)
  1245. "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
  1246. CONTENTS holds the contents of the block. INFO is a plist used
  1247. as a communication channel."
  1248. contents)
  1249. ;;; Src Block
  1250. (defun org-texinfo-src-block (src-block contents info)
  1251. "Transcode a SRC-BLOCK element from Org to Texinfo.
  1252. CONTENTS holds the contents of the item. INFO is a plist holding
  1253. contextual information."
  1254. (let* ((lang (org-element-property :language src-block))
  1255. (lisp-p (string-match-p "lisp" lang))
  1256. (src-contents (org-texinfo--sanitize-content
  1257. (org-export-format-code-default src-block info))))
  1258. (cond
  1259. ;; Case 1. Lisp Block
  1260. (lisp-p
  1261. (format "@lisp\n%s@end lisp"
  1262. src-contents))
  1263. ;; Case 2. Other blocks
  1264. (t
  1265. (format "@example\n%s@end example"
  1266. src-contents)))))
  1267. ;;; Statistics Cookie
  1268. (defun org-texinfo-statistics-cookie (statistics-cookie contents info)
  1269. "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
  1270. CONTENTS is nil. INFO is a plist holding contextual information."
  1271. (org-element-property :value statistics-cookie))
  1272. ;;; Subscript
  1273. (defun org-texinfo-subscript (subscript contents info)
  1274. "Transcode a SUBSCRIPT object from Org to Texinfo.
  1275. CONTENTS is the contents of the object. INFO is a plist holding
  1276. contextual information."
  1277. (format "@math{_%s}" contents))
  1278. ;;; Superscript
  1279. (defun org-texinfo-superscript (superscript contents info)
  1280. "Transcode a SUPERSCRIPT object from Org to Texinfo.
  1281. CONTENTS is the contents of the object. INFO is a plist holding
  1282. contextual information."
  1283. (format "@math{^%s}" contents))
  1284. ;;; Table
  1285. ;;
  1286. ;; `org-texinfo-table' is the entry point for table transcoding. It
  1287. ;; takes care of tables with a "verbatim" attribute. Otherwise, it
  1288. ;; delegates the job to either `org-texinfo-table--table.el-table' or
  1289. ;; `org-texinfo-table--org-table' functions, depending of the type of
  1290. ;; the table.
  1291. ;;
  1292. ;; `org-texinfo-table--align-string' is a subroutine used to build
  1293. ;; alignment string for Org tables.
  1294. (defun org-texinfo-table (table contents info)
  1295. "Transcode a TABLE element from Org to Texinfo.
  1296. CONTENTS is the contents of the table. INFO is a plist holding
  1297. contextual information."
  1298. (cond
  1299. ;; Case 1: verbatim table.
  1300. ((or org-texinfo-tables-verbatim
  1301. (let ((attr (mapconcat 'identity
  1302. (org-element-property :attr_latex table)
  1303. " ")))
  1304. (and attr (string-match "\\<verbatim\\>" attr))))
  1305. (format "@verbatim \n%s\n@end verbatim"
  1306. ;; Re-create table, without affiliated keywords.
  1307. (org-trim
  1308. (org-element-interpret-data
  1309. `(table nil ,@(org-element-contents table))))))
  1310. ;; Case 2: table.el table. Convert it using appropriate tools.
  1311. ((eq (org-element-property :type table) 'table.el)
  1312. (org-texinfo-table--table.el-table table contents info))
  1313. ;; Case 3: Standard table.
  1314. (t (org-texinfo-table--org-table table contents info))))
  1315. (defun org-texinfo-table-column-widths (table info)
  1316. "Determine the largest table cell in each column to process alignment.
  1317. TABLE is the table element to transcode. INFO is a plist used as
  1318. a communication channel."
  1319. (let* ((rows (org-element-map table 'table-row 'identity info))
  1320. (collected (loop for row in rows collect
  1321. (org-element-map row 'table-cell 'identity info)))
  1322. (number-cells (length (car collected)))
  1323. cells counts)
  1324. (loop for row in collected do
  1325. (push (mapcar (lambda (ref)
  1326. (let* ((start (org-element-property :contents-begin ref))
  1327. (end (org-element-property :contents-end ref))
  1328. (length (- end start)))
  1329. length)) row) cells))
  1330. (setq cells (org-remove-if 'null cells))
  1331. (push (loop for count from 0 to (- number-cells 1) collect
  1332. (loop for item in cells collect
  1333. (nth count item))) counts)
  1334. (mapconcat (lambda (size)
  1335. (make-string size ?a)) (mapcar (lambda (ref)
  1336. (apply 'max `(,@ref))) (car counts))
  1337. "} {")))
  1338. (defun org-texinfo-table--org-table (table contents info)
  1339. "Return appropriate Texinfo code for an Org table.
  1340. TABLE is the table type element to transcode. CONTENTS is its
  1341. contents, as a string. INFO is a plist used as a communication
  1342. channel.
  1343. This function assumes TABLE has `org' as its `:type' attribute."
  1344. (let* ((attr (org-export-read-attribute :attr_texinfo table))
  1345. (col-width (plist-get attr :columns))
  1346. (columns (if col-width
  1347. (format "@columnfractions %s"
  1348. col-width)
  1349. (format "{%s}"
  1350. (org-texinfo-table-column-widths
  1351. table info)))))
  1352. ;; Prepare the final format string for the table.
  1353. (cond
  1354. ;; Longtable.
  1355. ;; Others.
  1356. (t (concat
  1357. (format "@multitable %s\n%s@end multitable"
  1358. columns
  1359. contents))))))
  1360. (defun org-texinfo-table--table.el-table (table contents info)
  1361. "Returns nothing.
  1362. Rather than return an invalid table, nothing is returned."
  1363. 'nil)
  1364. ;;; Table Cell
  1365. (defun org-texinfo-table-cell (table-cell contents info)
  1366. "Transcode a TABLE-CELL element from Org to Texinfo.
  1367. CONTENTS is the cell contents. INFO is a plist used as
  1368. a communication channel."
  1369. (concat (if (and contents
  1370. org-texinfo-table-scientific-notation
  1371. (string-match orgtbl-exp-regexp contents))
  1372. ;; Use appropriate format string for scientific
  1373. ;; notation.
  1374. (format org-texinfo-table-scientific-notation
  1375. (match-string 1 contents)
  1376. (match-string 2 contents))
  1377. contents)
  1378. (when (org-export-get-next-element table-cell info) "\n@tab ")))
  1379. ;;; Table Row
  1380. (defun org-texinfo-table-row (table-row contents info)
  1381. "Transcode a TABLE-ROW element from Org to Texinfo.
  1382. CONTENTS is the contents of the row. INFO is a plist used as
  1383. a communication channel."
  1384. ;; Rules are ignored since table separators are deduced from
  1385. ;; borders of the current row.
  1386. (when (eq (org-element-property :type table-row) 'standard)
  1387. (let ((rowgroup-tag
  1388. (cond
  1389. ;; Case 1: Belongs to second or subsequent rowgroup.
  1390. ((not (= 1 (org-export-table-row-group table-row info)))
  1391. "@item ")
  1392. ;; Case 2: Row is from first rowgroup. Table has >=1 rowgroups.
  1393. ((org-export-table-has-header-p
  1394. (org-export-get-parent-table table-row) info)
  1395. "@headitem ")
  1396. ;; Case 3: Row is from first and only row group.
  1397. (t "@item "))))
  1398. (when (eq (org-element-property :type table-row) 'standard)
  1399. (concat rowgroup-tag contents "\n")))))
  1400. ;;; Target
  1401. (defun org-texinfo-target (target contents info)
  1402. "Transcode a TARGET object from Org to Texinfo.
  1403. CONTENTS is nil. INFO is a plist holding contextual
  1404. information."
  1405. (format "@anchor{%s}"
  1406. (org-export-solidify-link-text (org-element-property :value target))))
  1407. ;;; Timestamp
  1408. (defun org-texinfo-timestamp (timestamp contents info)
  1409. "Transcode a TIMESTAMP object from Org to Texinfo.
  1410. CONTENTS is nil. INFO is a plist holding contextual
  1411. information."
  1412. (let ((value (org-texinfo-plain-text
  1413. (org-timestamp-translate timestamp) info)))
  1414. (case (org-element-property :type timestamp)
  1415. ((active active-range)
  1416. (format org-texinfo-active-timestamp-format value))
  1417. ((inactive inactive-range)
  1418. (format org-texinfo-inactive-timestamp-format value))
  1419. (t (format org-texinfo-diary-timestamp-format value)))))
  1420. ;;; Verbatim
  1421. (defun org-texinfo-verbatim (verbatim contents info)
  1422. "Transcode a VERBATIM object from Org to Texinfo.
  1423. CONTENTS is nil. INFO is a plist used as a communication
  1424. channel."
  1425. (org-texinfo--text-markup (org-element-property :value verbatim) 'verbatim))
  1426. ;;; Verse Block
  1427. (defun org-texinfo-verse-block (verse-block contents info)
  1428. "Transcode a VERSE-BLOCK element from Org to Texinfo.
  1429. CONTENTS is verse block contents. INFO is a plist holding
  1430. contextual information."
  1431. ;; In a verse environment, add a line break to each newline
  1432. ;; character and change each white space at beginning of a line
  1433. ;; into a space of 1 em. Also change each blank line with
  1434. ;; a vertical space of 1 em.
  1435. (progn
  1436. (setq contents (replace-regexp-in-string
  1437. "^ *\\\\\\\\$" "\\\\vspace*{1em}"
  1438. (replace-regexp-in-string
  1439. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
  1440. (while (string-match "^[ \t]+" contents)
  1441. (let ((new-str (format "\\hspace*{%dem}"
  1442. (length (match-string 0 contents)))))
  1443. (setq contents (replace-match new-str nil t contents))))
  1444. (format "\\begin{verse}\n%s\\end{verse}" contents)))
  1445. ;;; Interactive functions
  1446. (defun org-texinfo-export-to-texinfo
  1447. (&optional async subtreep visible-only body-only ext-plist)
  1448. "Export current buffer to a Texinfo file.
  1449. If narrowing is active in the current buffer, only export its
  1450. narrowed part.
  1451. If a region is active, export that region.
  1452. A non-nil optional argument ASYNC means the process should happen
  1453. asynchronously. The resulting file should be accessible through
  1454. the `org-export-stack' interface.
  1455. When optional argument SUBTREEP is non-nil, export the sub-tree
  1456. at point, extracting information from the headline properties
  1457. first.
  1458. When optional argument VISIBLE-ONLY is non-nil, don't export
  1459. contents of hidden elements.
  1460. When optional argument BODY-ONLY is non-nil, only write code
  1461. between \"\\begin{document}\" and \"\\end{document}\".
  1462. EXT-PLIST, when provided, is a property list with external
  1463. parameters overriding Org default settings, but still inferior to
  1464. file-local settings.
  1465. Return output file's name."
  1466. (interactive)
  1467. (let ((outfile (org-export-output-file-name ".texi" subtreep))
  1468. (org-export-coding-system `,org-texinfo-coding-system))
  1469. (org-export-to-file 'texinfo outfile
  1470. async subtreep visible-only body-only ext-plist)))
  1471. (defun org-texinfo-export-to-info
  1472. (&optional async subtreep visible-only body-only ext-plist)
  1473. "Export current buffer to Texinfo then process through to INFO.
  1474. If narrowing is active in the current buffer, only export its
  1475. narrowed part.
  1476. If a region is active, export that region.
  1477. A non-nil optional argument ASYNC means the process should happen
  1478. asynchronously. The resulting file should be accessible through
  1479. the `org-export-stack' interface.
  1480. When optional argument SUBTREEP is non-nil, export the sub-tree
  1481. at point, extracting information from the headline properties
  1482. first.
  1483. When optional argument VISIBLE-ONLY is non-nil, don't export
  1484. contents of hidden elements.
  1485. When optional argument BODY-ONLY is non-nil, only write code
  1486. between \"\\begin{document}\" and \"\\end{document}\".
  1487. EXT-PLIST, when provided, is a property list with external
  1488. parameters overriding Org default settings, but still inferior to
  1489. file-local settings.
  1490. When optional argument PUB-DIR is set, use it as the publishing
  1491. directory.
  1492. Return INFO file's name."
  1493. (interactive)
  1494. (let ((outfile (org-export-output-file-name ".texi" subtreep))
  1495. (org-export-coding-system `,org-texinfo-coding-system))
  1496. (org-export-to-file 'texinfo outfile
  1497. async subtreep visible-only body-only ext-plist
  1498. (lambda (file) (org-texinfo-compile file)))))
  1499. ;;;###autoload
  1500. (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
  1501. "Publish an org file to Texinfo.
  1502. FILENAME is the filename of the Org file to be published. PLIST
  1503. is the property list for the given project. PUB-DIR is the
  1504. publishing directory.
  1505. Return output file name."
  1506. (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
  1507. ;;;###autoload
  1508. (defun org-texinfo-convert-region-to-texinfo ()
  1509. "Assume the current region has org-mode syntax, and convert it to Texinfo.
  1510. This can be used in any buffer. For example, you can write an
  1511. itemized list in org-mode syntax in an Texinfo buffer and use
  1512. this command to convert it."
  1513. (interactive)
  1514. (org-export-replace-region-by 'texinfo))
  1515. (defun org-texinfo-compile (file)
  1516. "Compile a texinfo file.
  1517. FILE is the name of the file being compiled. Processing is
  1518. done through the command specified in `org-texinfo-info-process'.
  1519. Return INFO file name or an error if it couldn't be produced."
  1520. (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
  1521. (full-name (file-truename file))
  1522. (out-dir (file-name-directory file))
  1523. ;; Properly set working directory for compilation.
  1524. (default-directory (if (file-name-absolute-p file)
  1525. (file-name-directory full-name)
  1526. default-directory))
  1527. errors)
  1528. (message (format "Processing Texinfo file %s..." file))
  1529. (save-window-excursion
  1530. (cond
  1531. ;; A function is provided: Apply it.
  1532. ((functionp org-texinfo-info-process)
  1533. (funcall org-texinfo-info-process (shell-quote-argument file)))
  1534. ;; A list is provided: Replace %b, %f and %o with appropriate
  1535. ;; values in each command before applying it. Output is
  1536. ;; redirected to "*Org INFO Texinfo Output*" buffer.
  1537. ((consp org-texinfo-info-process)
  1538. (let ((outbuf (get-buffer-create "*Org INFO Texinfo Output*")))
  1539. (mapc
  1540. (lambda (command)
  1541. (shell-command
  1542. (replace-regexp-in-string
  1543. "%b" (shell-quote-argument base-name)
  1544. (replace-regexp-in-string
  1545. "%f" (shell-quote-argument full-name)
  1546. (replace-regexp-in-string
  1547. "%o" (shell-quote-argument out-dir) command t t) t t) t t)
  1548. outbuf))
  1549. org-texinfo-info-process)
  1550. ;; Collect standard errors from output buffer.
  1551. (setq errors (org-texinfo-collect-errors outbuf))))
  1552. (t (error "No valid command to process to Info")))
  1553. (let ((infofile (concat out-dir base-name ".info")))
  1554. ;; Check for process failure. Provide collected errors if
  1555. ;; possible.
  1556. (if (not (file-exists-p infofile))
  1557. (error (concat (format "INFO file %s wasn't produced" infofile)
  1558. (when errors (concat ": " errors))))
  1559. ;; Else remove log files, when specified, and signal end of
  1560. ;; process to user, along with any error encountered.
  1561. (when org-texinfo-remove-logfiles
  1562. (dolist (ext org-texinfo-logfiles-extensions)
  1563. (let ((file (concat out-dir base-name "." ext)))
  1564. (when (file-exists-p file) (delete-file file)))))
  1565. (message (concat "Process completed"
  1566. (if (not errors) "."
  1567. (concat " with errors: " errors)))))
  1568. ;; Return output file name.
  1569. infofile))))
  1570. (defun org-texinfo-collect-errors (buffer)
  1571. "Collect some kind of errors from \"makeinfo\" command output.
  1572. BUFFER is the buffer containing output.
  1573. Return collected error types as a string, or nil if there was
  1574. none."
  1575. (with-current-buffer buffer
  1576. (save-excursion
  1577. (goto-char (point-min))
  1578. ;; Find final "makeinfo" run.
  1579. (when t
  1580. (let ((case-fold-search t)
  1581. (errors ""))
  1582. (when (save-excursion
  1583. (re-search-forward "perhaps incorrect sectioning?" nil t))
  1584. (setq errors (concat errors " [incorrect sectioning]")))
  1585. (when (save-excursion
  1586. (re-search-forward "missing close brace" nil t))
  1587. (setq errors (concat errors " [syntax error]")))
  1588. (when (save-excursion
  1589. (re-search-forward "Unknown command" nil t))
  1590. (setq errors (concat errors " [undefined @command]")))
  1591. (when (save-excursion
  1592. (re-search-forward "No matching @end" nil t))
  1593. (setq errors (concat errors " [block incomplete]")))
  1594. (when (save-excursion
  1595. (re-search-forward "requires a sectioning" nil t))
  1596. (setq errors (concat errors " [invalid section command]")))
  1597. (when (save-excursion
  1598. (re-search-forward "\\[unexpected\]" nil t))
  1599. (setq errors (concat errors " [unexpected error]")))
  1600. (when (save-excursion
  1601. (re-search-forward "misplaced " nil t))
  1602. (setq errors (concat errors " [syntax error]")))
  1603. (and (org-string-nw-p errors) (org-trim errors)))))))
  1604. (provide 'ox-texinfo)
  1605. ;; Local variables:
  1606. ;; generated-autoload-file: "org-loaddefs.el"
  1607. ;; End:
  1608. ;;; ox-texinfo.el ends here