org-e-texinfo.el 61 KB

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