ox-texinfo.el 63 KB

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