org-e-texinfo.el 63 KB

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