ox-texinfo.el 63 KB

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