ox-texinfo.el 62 KB

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