org-e-texinfo.el 63 KB

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