org-e-texinfo.el 64 KB

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