org-e-texinfo.el 63 KB

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