org-e-texinfo.el 64 KB

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