org-e-texinfo.el 63 KB

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