ox-texinfo.el 65 KB

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