ox-texinfo.el 63 KB

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