org-e-texinfo.el 62 KB

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