ox-texinfo.el 65 KB

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