ox-texinfo.el 62 KB

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