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))))
  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. ;; Return the list of headlines (reverse to have in actual order)
  467. (reverse seq)))
  468. (defun org-e-texinfo--generate-menu-items (items info)
  469. "Generate a list of headline information from the listing ITEMS.
  470. ITEMS is a list of the headlines to be converted into entries.
  471. INFO is a plist containing contextual information.
  472. Returns a list containing the following information from each
  473. headline: length, title, description. This is used to format the
  474. menu using `org-e-texinfo--format-menu'."
  475. (loop for headline in items collect
  476. (let* ((title (org-e-texinfo--sanitize-menu
  477. (org-e-texinfo--sanitize-headline
  478. (org-element-property :title headline) info)))
  479. (descr (org-export-data
  480. (org-element-property :description headline) info))
  481. (len (length title))
  482. (output (list len title descr)))
  483. output)))
  484. (defun org-e-texinfo--menu-headlines (headline info)
  485. "Retrieve the title from HEADLINE.
  486. INFO is a plist holding contextual information.
  487. Return the headline as a list of (length title description) with
  488. length of -1 and nil description. This is used in
  489. `org-e-texinfo--format-menu' to identify headlines as opposed to
  490. entries."
  491. (let ((title (org-export-data
  492. (org-element-property :title headline) info)))
  493. (list -1 title 'nil)))
  494. (defun org-e-texinfo--format-menu (text-menu)
  495. "Format the TEXT-MENU items to be properly printed in the menu.
  496. Each entry in the menu should be provided as (length title
  497. description).
  498. Headlines in the detailed menu are given length -1 to ensure they
  499. are never confused with other entries. They also have no
  500. description.
  501. Other menu items are output as:
  502. Title:: description
  503. With the spacing between :: and description based on the length
  504. of the longest menu entry."
  505. (let* ((lengths (mapcar 'car text-menu))
  506. (max-length (apply 'max lengths))
  507. output)
  508. (setq output
  509. (mapcar (lambda (name)
  510. (let* ((title (nth 1 name))
  511. (desc (nth 2 name))
  512. (length (nth 0 name)))
  513. (if (> length -1)
  514. (concat "* " title ":: "
  515. (make-string
  516. (- (+ 3 max-length) length)
  517. ?\s)
  518. (if desc
  519. (concat desc)))
  520. (concat "\n" title "\n"))))
  521. text-menu))
  522. output))
  523. ;;; Template
  524. (defun org-e-texinfo-template (contents info)
  525. "Return complete document string after Texinfo conversion.
  526. CONTENTS is the transcoded contents string. INFO is a plist
  527. holding export options."
  528. (let* ((title (org-export-data (plist-get info :title) info))
  529. (info-filename (or (plist-get info :texinfo-filename)
  530. (file-name-nondirectory
  531. (org-export-output-file-name ".info"))))
  532. (author (org-export-data (plist-get info :author) info))
  533. (texinfo-header (plist-get info :texinfo-header))
  534. (subtitle (plist-get info :subtitle))
  535. (subauthor (plist-get info :subauthor))
  536. (class (plist-get info :texinfo-class))
  537. (header (nth 1 (assoc class org-e-texinfo-classes)))
  538. (copying (org-e-texinfo--find-copying info))
  539. (dircat (plist-get info :texinfo-dircat))
  540. (dirtitle (plist-get info :texinfo-dirtitle))
  541. (dirdesc (plist-get info :texinfo-dirdesc))
  542. ;; Spacing to align description (column 32 - 3 for `* ' and
  543. ;; `.' in text.
  544. (dirspacing (- 29 (length dirtitle)))
  545. (menu (org-e-texinfo-make-menu info 'main))
  546. (detail-menu (org-e-texinfo-make-menu info 'detailed)))
  547. (concat
  548. ;; Header
  549. header "\n"
  550. "@c %**start of header\n"
  551. ;; Filename and Title
  552. "@setfilename " info-filename "\n"
  553. "@settitle " title "\n"
  554. "\n\n"
  555. "@c Version and Contact Info\n"
  556. "@set AUTHOR " author "\n"
  557. ;; Additional Header Options set by `#+TEXINFO_HEADER
  558. (if texinfo-header
  559. (concat "\n"
  560. texinfo-header
  561. "\n"))
  562. "@c %**end of header\n"
  563. "@finalout\n"
  564. "\n\n"
  565. ;; Copying
  566. "@copying\n"
  567. ;; Only export the content of the headline, do not need the
  568. ;; initial headline.
  569. (org-export-data (nth 2 copying) info)
  570. "@end copying\n"
  571. "\n\n"
  572. ;; Info directory information
  573. ;; Only supply if both title and category are provided
  574. (if (and dircat dirtitle)
  575. (concat "@dircategory " dircat "\n"
  576. "@direntry\n"
  577. "* " dirtitle "."
  578. (make-string dirspacing ?\s)
  579. dirdesc "\n"
  580. "@end direntry\n"))
  581. "\n\n"
  582. ;; Title
  583. "@titlepage\n"
  584. "@title " title "\n\n"
  585. (if subtitle
  586. (concat "@subtitle " subtitle "\n"))
  587. "@author " author "\n"
  588. (if subauthor
  589. (concat subauthor "\n"))
  590. "\n"
  591. "@c The following two commands start the copyright page.\n"
  592. "@page\n"
  593. "@vskip 0pt plus 1filll\n"
  594. "@insertcopying\n"
  595. "@end titlepage\n\n"
  596. "@c Output the table of contents at the beginning.\n"
  597. "@contents\n\n"
  598. ;; Configure Top Node when not for Tex
  599. "@ifnottex\n"
  600. "@node Top\n"
  601. "@top " title " Manual\n"
  602. "@insertcopying\n"
  603. "@end ifnottex\n\n"
  604. ;; Do not output menus if they are empty
  605. (if menu
  606. ;; Menu
  607. (concat "@menu\n"
  608. menu
  609. "\n\n"
  610. ;; Detailed Menu
  611. (if detail-menu
  612. (concat "@detailmenu\n"
  613. " --- The Detailed Node Listing ---\n"
  614. detail-menu
  615. "\n\n"
  616. "@end detailmenu\n"))
  617. "@end menu\n"))
  618. "\n\n"
  619. ;; Document's body.
  620. contents
  621. "\n"
  622. ;; Creator.
  623. (let ((creator-info (plist-get info :with-creator)))
  624. (cond
  625. ((not creator-info) "")
  626. ((eq creator-info 'comment)
  627. (format "@c %s\n" (plist-get info :creator)))
  628. (t (concat (plist-get info :creator) "\n"))))
  629. ;; Document end.
  630. "\n@bye")))
  631. ;;; Transcode Functions
  632. ;;; Bold
  633. (defun org-e-texinfo-bold (bold contents info)
  634. "Transcode BOLD from Org to Texinfo.
  635. CONTENTS is the text with bold markup. INFO is a plist holding
  636. contextual information."
  637. (org-e-texinfo--text-markup contents 'bold))
  638. ;;; Center Block
  639. (defun org-e-texinfo-center-block (center-block contents info)
  640. "Transcode a CENTER-BLOCK element from Org to Texinfo.
  641. CONTENTS holds the contents of the block. INFO is a plist used
  642. as a communication channel."
  643. contents)
  644. ;;; Clock
  645. (defun org-e-texinfo-clock (clock contents info)
  646. "Transcode a CLOCK element from Org to Texinfo.
  647. CONTENTS is nil. INFO is a plist holding contextual
  648. information."
  649. (concat
  650. "@noindent"
  651. (format "@strong{%s} " org-clock-string)
  652. (format org-e-texinfo-inactive-timestamp-format
  653. (concat (org-translate-time
  654. (org-element-property :raw-value
  655. (org-element-property :value clock)))
  656. (let ((time (org-element-property :duration clock)))
  657. (and time (format " (%s)" time)))))
  658. "@*"))
  659. ;;; Code
  660. (defun org-e-texinfo-code (code contents info)
  661. "Transcode a CODE object from Org to Texinfo.
  662. CONTENTS is nil. INFO is a plist used as a communication
  663. channel."
  664. (org-e-texinfo--text-markup (org-element-property :value code) 'code))
  665. ;;; Comment
  666. (defun org-e-texinfo-comment (comment contents info)
  667. "Transcode a COMMENT object from Org to Texinfo.
  668. CONTENTS is the text in the comment. INFO is a plist holding
  669. contextual information."
  670. (org-e-texinfo--text-markup (org-element-property :value comment) 'comment))
  671. ;;; Comment Block
  672. (defun org-e-texinfo-comment-block (comment-block contents info)
  673. "Transcode a COMMENT-BLOCK object from Org to Texinfo.
  674. CONTENTS is the text within the block. INFO is a plist holding
  675. contextual information."
  676. (format "@ignore\n%s@end ignore" (org-element-property :value comment-block)))
  677. ;;; Drawer
  678. (defun org-e-texinfo-drawer (drawer contents info)
  679. "Transcode a DRAWER element from Org to Texinfo.
  680. CONTENTS holds the contents of the block. INFO is a plist
  681. holding contextual information."
  682. (let* ((name (org-element-property :drawer-name drawer))
  683. (output (if (functionp org-e-texinfo-format-drawer-function)
  684. (funcall org-e-texinfo-format-drawer-function
  685. name contents)
  686. ;; If there's no user defined function: simply
  687. ;; display contents of the drawer.
  688. contents)))
  689. output))
  690. ;;; Dynamic Block
  691. (defun org-e-texinfo-dynamic-block (dynamic-block contents info)
  692. "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
  693. CONTENTS holds the contents of the block. INFO is a plist
  694. holding contextual information. See `org-export-data'."
  695. contents)
  696. ;;; Entity
  697. (defun org-e-texinfo-entity (entity contents info)
  698. "Transcode an ENTITY object from Org to Texinfo.
  699. CONTENTS are the definition itself. INFO is a plist holding
  700. contextual information."
  701. (let ((ent (org-element-property :latex entity)))
  702. (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
  703. ;;; Example Block
  704. (defun org-e-texinfo-example-block (example-block contents info)
  705. "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
  706. CONTENTS is nil. INFO is a plist holding contextual
  707. information."
  708. (format "@verbatim\n%s@end verbatim"
  709. (org-export-format-code-default example-block info)))
  710. ;;; Export Block
  711. (defun org-e-texinfo-export-block (export-block contents info)
  712. "Transcode a EXPORT-BLOCK element from Org to Texinfo.
  713. CONTENTS is nil. INFO is a plist holding contextual information."
  714. (when (string= (org-element-property :type export-block) "TEXINFO")
  715. (org-remove-indentation (org-element-property :value export-block))))
  716. ;;; Export Snippet
  717. (defun org-e-texinfo-export-snippet (export-snippet contents info)
  718. "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
  719. CONTENTS is nil. INFO is a plist holding contextual information."
  720. (when (eq (org-export-snippet-backend export-snippet) 'e-texinfo)
  721. (org-element-property :value export-snippet)))
  722. ;;; Fixed Width
  723. (defun org-e-texinfo-fixed-width (fixed-width contents info)
  724. "Transcode a FIXED-WIDTH element from Org to Texinfo.
  725. CONTENTS is nil. INFO is a plist holding contextual information."
  726. (format "@example\n%s\n@end example"
  727. (org-remove-indentation
  728. (org-e-texinfo--sanitize-content
  729. (org-element-property :value fixed-width)))))
  730. ;;; Footnote Reference
  731. ;;
  732. (defun org-e-texinfo-footnote-reference (footnote contents info)
  733. "Create a footnote reference for FOOTNOTE.
  734. FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
  735. plist holding contextual information."
  736. (let ((def (org-export-get-footnote-definition footnote info)))
  737. (format "@footnote{%s}"
  738. (org-trim (org-export-data def info)))))
  739. ;;; Headline
  740. (defun org-e-texinfo-headline (headline contents info)
  741. "Transcode an HEADLINE element from Org to Texinfo.
  742. CONTENTS holds the contents of the headline. INFO is a plist
  743. holding contextual information."
  744. (let* ((class (plist-get info :texinfo-class))
  745. (level (org-export-get-relative-level headline info))
  746. (numberedp (org-export-numbered-headline-p headline info))
  747. (class-sectionning (assoc class org-e-texinfo-classes))
  748. ;; Find the index type, if any
  749. (index (org-element-property :index headline))
  750. ;; Retrieve headline text
  751. (text (org-e-texinfo--sanitize-headline
  752. (org-element-property :title headline) info))
  753. ;; Create node info, to insert it before section formatting.
  754. (node (format "@node %s\n"
  755. (org-e-texinfo--sanitize-menu
  756. (replace-regexp-in-string "%" "%%" text))))
  757. ;; Menus must be generated with first child, otherwise they
  758. ;; will not nest properly
  759. (menu (let* ((first (org-export-first-sibling-p headline info))
  760. (parent (org-export-get-parent-headline headline))
  761. (title (org-e-texinfo--sanitize-headline
  762. (org-element-property :title parent) info))
  763. heading listing
  764. (tree (plist-get info :parse-tree)))
  765. (if first
  766. (org-element-map
  767. (plist-get info :parse-tree) 'headline
  768. (lambda (ref)
  769. (if (member title (org-element-property :title ref))
  770. (push ref heading)))
  771. info 't))
  772. (setq listing (org-e-texinfo--build-menu
  773. (car heading) level info))
  774. (if listing
  775. (setq listing (replace-regexp-in-string
  776. "%" "%%" listing)
  777. listing (format
  778. "\n@menu\n%s\n@end menu\n\n" listing))
  779. 'nil)))
  780. ;; Section formatting will set two placeholders: one for the
  781. ;; title and the other for the contents.
  782. (section-fmt
  783. (let ((sec (if (and (symbolp (nth 2 class-sectionning))
  784. (fboundp (nth 2 class-sectionning)))
  785. (funcall (nth 2 class-sectionning) level numberedp)
  786. (nth (1+ level) class-sectionning))))
  787. (cond
  788. ;; No section available for that LEVEL.
  789. ((not sec) nil)
  790. ;; Section format directly returned by a function.
  791. ((stringp sec) sec)
  792. ;; (numbered-section . unnumbered-section)
  793. ((not (consp (cdr sec)))
  794. ;; If an index, always unnumbered
  795. (if index
  796. (concat menu node (cdr sec) "\n%s")
  797. ;; Otherwise number as needed.
  798. (concat menu node
  799. (funcall
  800. (if numberedp #'car #'cdr) sec) "\n%s"))))))
  801. (todo
  802. (and (plist-get info :with-todo-keywords)
  803. (let ((todo (org-element-property :todo-keyword headline)))
  804. (and todo (org-export-data todo info)))))
  805. (todo-type (and todo (org-element-property :todo-type headline)))
  806. (tags (and (plist-get info :with-tags)
  807. (org-export-get-tags headline info)))
  808. (priority (and (plist-get info :with-priority)
  809. (org-element-property :priority headline)))
  810. ;; Create the headline text along with a no-tag version. The
  811. ;; latter is required to remove tags from table of contents.
  812. (full-text (org-e-texinfo--sanitize-content
  813. (if (functionp org-e-texinfo-format-headline-function)
  814. ;; User-defined formatting function.
  815. (funcall org-e-texinfo-format-headline-function
  816. todo todo-type priority text tags)
  817. ;; Default formatting.
  818. (concat
  819. (when todo
  820. (format "@strong{%s} " todo))
  821. (when priority (format "@emph{#%s} " priority))
  822. text
  823. (when tags
  824. (format ":%s:"
  825. (mapconcat 'identity tags ":")))))))
  826. (full-text-no-tag
  827. (org-e-texinfo--sanitize-content
  828. (if (functionp org-e-texinfo-format-headline-function)
  829. ;; User-defined formatting function.
  830. (funcall org-e-texinfo-format-headline-function
  831. todo todo-type priority text nil)
  832. ;; Default formatting.
  833. (concat
  834. (when todo (format "@strong{%s} " todo))
  835. (when priority (format "@emph{#%c} " priority))
  836. text))))
  837. (pre-blanks
  838. (make-string (org-element-property :pre-blank headline) 10)))
  839. (cond
  840. ;; Case 1: This is a footnote section: ignore it.
  841. ((org-element-property :footnote-section-p headline) nil)
  842. ;; Case 2: This is the `copying' section: ignore it
  843. ;; This is used elsewhere.
  844. ((org-element-property :copying headline) nil)
  845. ;; Case 3: An index. If it matches one of the known indexes,
  846. ;; print it as such following the contents, otherwise
  847. ;; print the contents and leave the index up to the user.
  848. (index
  849. (format
  850. section-fmt full-text
  851. (concat pre-blanks contents "\n"
  852. (if (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
  853. (concat "@printindex " index)))))
  854. ;; Case 4: This is a deep sub-tree: export it as a list item.
  855. ;; Also export as items headlines for which no section
  856. ;; format has been found.
  857. ((or (not section-fmt) (org-export-low-level-p headline info))
  858. ;; Build the real contents of the sub-tree.
  859. (let ((low-level-body
  860. (concat
  861. ;; If the headline is the first sibling, start a list.
  862. (when (org-export-first-sibling-p headline info)
  863. (format "@%s\n" (if numberedp 'enumerate 'itemize)))
  864. ;; Itemize headline
  865. "@item\n" full-text "\n" pre-blanks contents)))
  866. ;; If headline is not the last sibling simply return
  867. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
  868. ;; blank line.
  869. (if (not (org-export-last-sibling-p headline info)) low-level-body
  870. (replace-regexp-in-string
  871. "[ \t\n]*\\'"
  872. (format "\n@end %s" (if numberedp 'enumerate 'itemize))
  873. low-level-body))))
  874. ;; Case 5: Standard headline. Export it as a section.
  875. (t
  876. (cond
  877. ((not (and tags (eq (plist-get info :with-tags) 'not-in-toc)))
  878. ;; Regular section. Use specified format string.
  879. (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
  880. (concat pre-blanks contents)))
  881. ((string-match "\\`@\\(.*?\\){" section-fmt)
  882. ;; If tags should be removed from table of contents, insert
  883. ;; title without tags as an alternative heading in sectioning
  884. ;; command.
  885. (format (replace-match (concat (match-string 1 section-fmt) "[%s]")
  886. nil nil section-fmt 1)
  887. ;; Replace square brackets with parenthesis since
  888. ;; square brackets are not supported in optional
  889. ;; arguments.
  890. (replace-regexp-in-string
  891. "\\[" "("
  892. (replace-regexp-in-string
  893. "\\]" ")"
  894. full-text-no-tag))
  895. full-text
  896. (concat pre-blanks contents)))
  897. (t
  898. ;; Impossible to add an alternative heading. Fallback to
  899. ;; regular sectioning format string.
  900. (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
  901. (concat pre-blanks contents))))))))
  902. ;;; Inline Src Block
  903. (defun org-e-texinfo-inline-src-block (inline-src-block contents info)
  904. "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
  905. CONTENTS holds the contents of the item. INFO is a plist holding
  906. contextual information."
  907. (let* ((code (org-element-property :value inline-src-block))
  908. (separator (org-e-texinfo--find-verb-separator code)))
  909. (concat "@verb{" separator code separator "}")))
  910. ;;; Inlinetask
  911. (defun org-e-texinfo-inlinetask (inlinetask contents info)
  912. "Transcode an INLINETASK element from Org to Texinfo.
  913. CONTENTS holds the contents of the block. INFO is a plist
  914. holding contextual information."
  915. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  916. (todo (and (plist-get info :with-todo-keywords)
  917. (let ((todo (org-element-property :todo-keyword inlinetask)))
  918. (and todo (org-export-data todo info)))))
  919. (todo-type (org-element-property :todo-type inlinetask))
  920. (tags (and (plist-get info :with-tags)
  921. (org-export-get-tags inlinetask info)))
  922. (priority (and (plist-get info :with-priority)
  923. (org-element-property :priority inlinetask))))
  924. ;; If `org-e-texinfo-format-inlinetask-function' is provided, call it
  925. ;; with appropriate arguments.
  926. (if (functionp org-e-texinfo-format-inlinetask-function)
  927. (funcall org-e-texinfo-format-inlinetask-function
  928. todo todo-type priority title tags contents)
  929. ;; Otherwise, use a default template.
  930. (let ((full-title
  931. (concat
  932. (when todo (format "@strong{%s} " todo))
  933. (when priority (format "#%c " priority))
  934. title
  935. (when tags (format ":%s:"
  936. (mapconcat 'identity tags ":"))))))
  937. (format (concat "@center %s\n\n"
  938. "%s"
  939. "\n")
  940. full-title contents)))))
  941. ;;; Italic
  942. (defun org-e-texinfo-italic (italic contents info)
  943. "Transcode ITALIC from Org to Texinfo.
  944. CONTENTS is the text with italic markup. INFO is a plist holding
  945. contextual information."
  946. (org-e-texinfo--text-markup contents 'italic))
  947. ;;; Item
  948. (defun org-e-texinfo-item (item contents info)
  949. "Transcode an ITEM element from Org to Texinfo.
  950. CONTENTS holds the contents of the item. INFO is a plist holding
  951. contextual information."
  952. (let* ((tag (org-element-property :tag item))
  953. (desc (org-export-data tag info)))
  954. (concat "\n@item " (if tag desc) "\n"
  955. (org-trim contents) "\n")))
  956. ;;; Keyword
  957. (defun org-e-texinfo-keyword (keyword contents info)
  958. "Transcode a KEYWORD element from Org to Texinfo.
  959. CONTENTS is nil. INFO is a plist holding contextual information."
  960. (let ((key (org-element-property :key keyword))
  961. (value (org-element-property :value keyword)))
  962. (cond
  963. ((string= key "TEXINFO") value)
  964. ((string= key "CINDEX") (format "@cindex %s" value))
  965. ((string= key "FINDEX") (format "@findex %s" value))
  966. ((string= key "KINDEX") (format "@kindex %s" value))
  967. ((string= key "PINDEX") (format "@pindex %s" value))
  968. ((string= key "TINDEX") (format "@tindex %s" value))
  969. ((string= key "VINDEX") (format "@vindex %s" value)))))
  970. ;;; Line Break
  971. (defun org-e-texinfo-line-break (line-break contents info)
  972. "Transcode a LINE-BREAK object from Org to Texinfo.
  973. CONTENTS is nil. INFO is a plist holding contextual information."
  974. "@*")
  975. ;;; Link
  976. (defun org-e-texinfo-link (link desc info)
  977. "Transcode a LINK object from Org to Texinfo.
  978. DESC is the description part of the link, or the empty string.
  979. INFO is a plist holding contextual information. See
  980. `org-export-data'."
  981. (let* ((type (org-element-property :type link))
  982. (raw-path (org-element-property :path link))
  983. ;; Ensure DESC really exists, or set it to nil.
  984. (desc (and (not (string= desc "")) desc))
  985. (path (cond
  986. ((member type '("http" "https" "ftp"))
  987. (concat type ":" raw-path))
  988. ((string= type "file")
  989. (if (file-name-absolute-p raw-path)
  990. (concat "file://" (expand-file-name raw-path))
  991. (concat "file://" raw-path)))
  992. (t raw-path)))
  993. (email (if (string= type "mailto")
  994. (let ((text (replace-regexp-in-string
  995. "@" "@@" raw-path)))
  996. (concat text (if desc (concat "," desc))))))
  997. protocol)
  998. (cond
  999. ;; Links pointing to an headline: Find destination and build
  1000. ;; appropriate referencing command.
  1001. ((member type '("custom-id" "id"))
  1002. (let ((destination (org-export-resolve-id-link link info)))
  1003. (case (org-element-type destination)
  1004. ;; Id link points to an external file.
  1005. (plain-text
  1006. (if desc (format "@uref{file://%s,%s}" destination desc)
  1007. (format "@uref{file://%s}" destination)))
  1008. ;; LINK points to an headline. Use the headline as the NODE target
  1009. (headline
  1010. (format "@ref{%s}"
  1011. (org-export-data
  1012. (org-element-property :title destination) info)))
  1013. (otherwise
  1014. (let ((path (org-export-solidify-link-text path)))
  1015. (if (not desc) (format "@ref{%s}" path)
  1016. (format "@ref{%s,,%s}" path desc)))))))
  1017. ((member type '("fuzzy"))
  1018. (let ((destination (org-export-resolve-fuzzy-link link info)))
  1019. (case (org-element-type destination)
  1020. ;; Id link points to an external file.
  1021. (plain-text
  1022. (if desc (format "@uref{file://%s,%s}" destination desc)
  1023. (format "@uref{file://%s}" destination)))
  1024. ;; LINK points to an headline. Use the headline as the NODE target
  1025. (headline
  1026. (format "@ref{%s}"
  1027. (org-export-data
  1028. (org-element-property :title destination) info)))
  1029. (otherwise
  1030. (let ((path (org-export-solidify-link-text path)))
  1031. (if (not desc) (format "@ref{%s}" path)
  1032. (format "@ref{%s,,%s}" path desc)))))))
  1033. ;; Special case for email addresses
  1034. (email
  1035. (format "@email{%s}" email))
  1036. ;; External link with a description part.
  1037. ((and path desc) (format "@uref{%s,%s}" path desc))
  1038. ;; External link without a description part.
  1039. (path (format "@uref{%s}" path))
  1040. ;; No path, only description. Try to do something useful.
  1041. (t (format org-e-texinfo-link-with-unknown-path-format desc)))))
  1042. ;;; Menu
  1043. (defun org-e-texinfo-make-menu (info level)
  1044. "Create the menu for inclusion in the texifo document.
  1045. INFO is the parsed buffer that contains the headlines. LEVEL
  1046. determines whether to make the main menu, or the detailed menu.
  1047. This is only used for generating the primary menu. In-Node menus
  1048. are generated directly."
  1049. (let* ((parse (plist-get info :parse-tree))
  1050. ;; Top determines level to build menu from, it finds the
  1051. ;; level of the first headline in the export.
  1052. (top (org-element-map
  1053. parse 'headline
  1054. (lambda (headline)
  1055. (org-element-property :level headline)) info 't)))
  1056. (cond
  1057. ;; Generate the main menu
  1058. ((eq level 'main)
  1059. (org-e-texinfo--build-menu parse top info))
  1060. ;; Generate the detailed (recursive) menu
  1061. ((eq level 'detailed)
  1062. ;; Requires recursion
  1063. ;;(org-e-texinfo--build-detailed-menu parse top info)
  1064. (org-e-texinfo--build-menu parse top info 'detailed))
  1065. ;; Otherwise do nothing
  1066. (t))))
  1067. ;;; Paragraph
  1068. (defun org-e-texinfo-paragraph (paragraph contents info)
  1069. "Transcode a PARAGRAPH element from Org to Texinfo.
  1070. CONTENTS is the contents of the paragraph, as a string. INFO is
  1071. the plist used as a communication channel."
  1072. contents)
  1073. ;;; Plain List
  1074. (defun org-e-texinfo-plain-list (plain-list contents info)
  1075. "Transcode a PLAIN-LIST element from Org to Texinfo.
  1076. CONTENTS is the contents of the list. INFO is a plist holding
  1077. contextual information."
  1078. (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
  1079. (indic (or (plist-get attr :indic)
  1080. org-e-texinfo-def-table-markup))
  1081. (type (org-element-property :type plain-list))
  1082. (table-type (or (plist-get attr :table-type)
  1083. "table"))
  1084. ;; Ensure valid texinfo table type.
  1085. (table-type (if (memq table-type '("table" "ftable" "vtable"))
  1086. table-type
  1087. "table"))
  1088. (list-type (cond
  1089. ((eq type 'ordered) "enumerate")
  1090. ((eq type 'unordered) "itemize")
  1091. ((eq type 'descriptive) table-type))))
  1092. (format "@%s%s\n@end %s"
  1093. (if (eq type 'descriptive)
  1094. (concat list-type " " indic)
  1095. list-type)
  1096. contents
  1097. list-type)))
  1098. ;;; Plain Text
  1099. (defun org-e-texinfo-plain-text (text info)
  1100. "Transcode a TEXT string from Org to Texinfo.
  1101. TEXT is the string to transcode. INFO is a plist holding
  1102. contextual information."
  1103. ;; First protect @, { and }.
  1104. (let ((output (org-e-texinfo--sanitize-content text)))
  1105. ;; Activate smart quotes. Be sure to provide original TEXT string
  1106. ;; since OUTPUT may have been modified.
  1107. (when (plist-get info :with-smart-quotes)
  1108. (setq output
  1109. (org-export-activate-smart-quotes output :texinfo info text)))
  1110. ;; LaTeX into @LaTeX{} and TeX into @TeX{}
  1111. (let ((case-fold-search nil)
  1112. (start 0))
  1113. (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
  1114. (setq output (replace-match
  1115. (format "@%s{}" (match-string 1 output)) nil t output)
  1116. start (match-end 0))))
  1117. ;; Convert special strings.
  1118. (when (plist-get info :with-special-strings)
  1119. (while (string-match (regexp-quote "...") output)
  1120. (setq output (replace-match "@dots{}" nil t output))))
  1121. ;; Handle break preservation if required.
  1122. (when (plist-get info :preserve-breaks)
  1123. (setq output (replace-regexp-in-string
  1124. "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
  1125. ;; Return value.
  1126. output))
  1127. ;;; Planning
  1128. (defun org-e-texinfo-planning (planning contents info)
  1129. "Transcode a PLANNING element from Org to Texinfo.
  1130. CONTENTS is nil. INFO is a plist holding contextual
  1131. information."
  1132. (concat
  1133. "@noindent"
  1134. (mapconcat
  1135. 'identity
  1136. (delq nil
  1137. (list
  1138. (let ((closed (org-element-property :closed planning)))
  1139. (when closed
  1140. (concat
  1141. (format "@strong{%s} " org-closed-string)
  1142. (format org-e-texinfo-inactive-timestamp-format
  1143. (org-translate-time
  1144. (org-element-property :raw-value closed))))))
  1145. (let ((deadline (org-element-property :deadline planning)))
  1146. (when deadline
  1147. (concat
  1148. (format "@strong{%s} " org-deadline-string)
  1149. (format org-e-texinfo-active-timestamp-format
  1150. (org-translate-time
  1151. (org-element-property :raw-value deadline))))))
  1152. (let ((scheduled (org-element-property :scheduled planning)))
  1153. (when scheduled
  1154. (concat
  1155. (format "@strong{%s} " org-scheduled-string)
  1156. (format org-e-texinfo-active-timestamp-format
  1157. (org-translate-time
  1158. (org-element-property :raw-value scheduled))))))))
  1159. " ")
  1160. "@*"))
  1161. ;;; Property Drawer
  1162. (defun org-e-texinfo-property-drawer (property-drawer contents info)
  1163. "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
  1164. CONTENTS is nil. INFO is a plist holding contextual
  1165. information."
  1166. ;; The property drawer isn't exported but we want separating blank
  1167. ;; lines nonetheless.
  1168. "")
  1169. ;;; Quote Block
  1170. (defun org-e-texinfo-quote-block (quote-block contents info)
  1171. "Transcode a QUOTE-BLOCK element from Org to Texinfo.
  1172. CONTENTS holds the contents of the block. INFO is a plist
  1173. holding contextual information."
  1174. (let* ((title (org-element-property :name quote-block))
  1175. (start-quote (concat "@quotation"
  1176. (if title
  1177. (format " %s" title)))))
  1178. (format "%s\n%s@end quotation" start-quote contents)))
  1179. ;;; Quote Section
  1180. (defun org-e-texinfo-quote-section (quote-section contents info)
  1181. "Transcode a QUOTE-SECTION element from Org to Texinfo.
  1182. CONTENTS is nil. INFO is a plist holding contextual information."
  1183. (let ((value (org-remove-indentation
  1184. (org-element-property :value quote-section))))
  1185. (when value (format "@verbatim\n%s@end verbatim" value))))
  1186. ;;; Radio Target
  1187. (defun org-e-texinfo-radio-target (radio-target text info)
  1188. "Transcode a RADIO-TARGET object from Org to Texinfo.
  1189. TEXT is the text of the target. INFO is a plist holding
  1190. contextual information."
  1191. (format "@anchor{%s}%s"
  1192. (org-export-solidify-link-text
  1193. (org-element-property :value radio-target))
  1194. text))
  1195. ;;; Section
  1196. (defun org-e-texinfo-section (section contents info)
  1197. "Transcode a SECTION element from Org to Texinfo.
  1198. CONTENTS holds the contents of the section. INFO is a plist
  1199. holding contextual information."
  1200. contents)
  1201. ;;; Special Block
  1202. (defun org-e-texinfo-special-block (special-block contents info)
  1203. "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
  1204. CONTENTS holds the contents of the block. INFO is a plist used
  1205. as a communication channel."
  1206. contents)
  1207. ;;; Src Block
  1208. (defun org-e-texinfo-src-block (src-block contents info)
  1209. "Transcode a SRC-BLOCK element from Org to Texinfo.
  1210. CONTENTS holds the contents of the item. INFO is a plist holding
  1211. contextual information."
  1212. (let* ((lang (org-element-property :language src-block))
  1213. (lisp-p (string-match-p "lisp" lang)))
  1214. (cond
  1215. ;; Case 1. Lisp Block
  1216. (lisp-p
  1217. (format "@lisp\n%s\n@end lisp"
  1218. (org-export-format-code-default src-block info)))
  1219. ;; Case 2. Other blocks
  1220. (t
  1221. (format "@example\n%s\n@end example"
  1222. (org-export-format-code-default src-block info))))))
  1223. ;;; Statistics Cookie
  1224. (defun org-e-texinfo-statistics-cookie (statistics-cookie contents info)
  1225. "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
  1226. CONTENTS is nil. INFO is a plist holding contextual information."
  1227. (org-element-property :value statistics-cookie))
  1228. ;;; Subscript
  1229. (defun org-e-texinfo-subscript (subscript contents info)
  1230. "Transcode a SUBSCRIPT object from Org to Texinfo.
  1231. CONTENTS is the contents of the object. INFO is a plist holding
  1232. contextual information."
  1233. (format "@math{_%s}" contents))
  1234. ;;; Superscript
  1235. (defun org-e-texinfo-superscript (superscript contents info)
  1236. "Transcode a SUPERSCRIPT object from Org to Texinfo.
  1237. CONTENTS is the contents of the object. INFO is a plist holding
  1238. contextual information."
  1239. (format "@math{^%s}" contents))
  1240. ;;; Table
  1241. ;;
  1242. ;; `org-e-texinfo-table' is the entry point for table transcoding. It
  1243. ;; takes care of tables with a "verbatim" attribute. Otherwise, it
  1244. ;; delegates the job to either `org-e-texinfo-table--table.el-table' or
  1245. ;; `org-e-texinfo-table--org-table' functions, depending of the type of
  1246. ;; the table.
  1247. ;;
  1248. ;; `org-e-texinfo-table--align-string' is a subroutine used to build
  1249. ;; alignment string for Org tables.
  1250. (defun org-e-texinfo-table (table contents info)
  1251. "Transcode a TABLE element from Org to Texinfo.
  1252. CONTENTS is the contents of the table. INFO is a plist holding
  1253. contextual information."
  1254. (cond
  1255. ;; Case 1: verbatim table.
  1256. ((or org-e-texinfo-tables-verbatim
  1257. (let ((attr (mapconcat 'identity
  1258. (org-element-property :attr_latex table)
  1259. " ")))
  1260. (and attr (string-match "\\<verbatim\\>" attr))))
  1261. (format "@verbatim \n%s\n@end verbatim"
  1262. ;; Re-create table, without affiliated keywords.
  1263. (org-trim
  1264. (org-element-interpret-data
  1265. `(table nil ,@(org-element-contents table))))))
  1266. ;; Case 2: table.el table. Convert it using appropriate tools.
  1267. ((eq (org-element-property :type table) 'table.el)
  1268. (org-e-texinfo-table--table.el-table table contents info))
  1269. ;; Case 3: Standard table.
  1270. (t (org-e-texinfo-table--org-table table contents info))))
  1271. (defun org-e-texinfo-table-column-widths (table info)
  1272. "Determine the largest table cell in each column to process alignment.
  1273. TABLE is the table element to transcode. INFO is a plist used as
  1274. a communication channel."
  1275. (let* ((rows (org-element-map table 'table-row 'identity info))
  1276. (collected (loop for row in rows collect
  1277. (org-element-map
  1278. 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 sectionnng]")))
  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