ox-texinfo.el 62 KB

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