org-e-texinfo.el 61 KB

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