ox-texinfo.el 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  1. ;;; ox-texinfo.el --- Texinfo Back-End for Org Export Engine
  2. ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
  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. ;; See Org manual for details.
  19. ;;; Code:
  20. (eval-when-compile (require 'cl))
  21. (require 'ox)
  22. (defvar orgtbl-exp-regexp)
  23. ;;; Define Back-End
  24. (org-export-define-backend 'texinfo
  25. '((bold . org-texinfo-bold)
  26. (center-block . org-texinfo-center-block)
  27. (clock . org-texinfo-clock)
  28. (code . org-texinfo-code)
  29. (comment . (lambda (&rest args) ""))
  30. (comment-block . (lambda (&rest args) ""))
  31. (drawer . org-texinfo-drawer)
  32. (dynamic-block . org-texinfo-dynamic-block)
  33. (entity . org-texinfo-entity)
  34. (example-block . org-texinfo-example-block)
  35. (export-snippet . org-texinfo-export-snippet)
  36. (fixed-width . org-texinfo-fixed-width)
  37. (footnote-definition . org-texinfo-footnote-definition)
  38. (footnote-reference . org-texinfo-footnote-reference)
  39. (headline . org-texinfo-headline)
  40. (inline-src-block . org-texinfo-inline-src-block)
  41. (inlinetask . org-texinfo-inlinetask)
  42. (italic . org-texinfo-italic)
  43. (item . org-texinfo-item)
  44. (keyword . org-texinfo-keyword)
  45. (line-break . org-texinfo-line-break)
  46. (link . org-texinfo-link)
  47. (node-property . org-texinfo-node-property)
  48. (paragraph . org-texinfo-paragraph)
  49. (plain-list . org-texinfo-plain-list)
  50. (plain-text . org-texinfo-plain-text)
  51. (planning . org-texinfo-planning)
  52. (property-drawer . org-texinfo-property-drawer)
  53. (quote-block . org-texinfo-quote-block)
  54. (radio-target . org-texinfo-radio-target)
  55. (section . org-texinfo-section)
  56. (special-block . org-texinfo-special-block)
  57. (src-block . org-texinfo-src-block)
  58. (statistics-cookie . org-texinfo-statistics-cookie)
  59. (subscript . org-texinfo-subscript)
  60. (superscript . org-texinfo-superscript)
  61. (table . org-texinfo-table)
  62. (table-cell . org-texinfo-table-cell)
  63. (table-row . org-texinfo-table-row)
  64. (target . org-texinfo-target)
  65. (template . org-texinfo-template)
  66. (timestamp . org-texinfo-timestamp)
  67. (verbatim . org-texinfo-verbatim)
  68. (verse-block . org-texinfo-verse-block))
  69. :export-block "TEXINFO"
  70. :filters-alist
  71. '((:filter-headline . org-texinfo-filter-section-blank-lines)
  72. (:filter-parse-tree . org-texinfo--normalize-headlines)
  73. (:filter-section . org-texinfo-filter-section-blank-lines))
  74. :menu-entry
  75. '(?i "Export to Texinfo"
  76. ((?t "As TEXI file" org-texinfo-export-to-texinfo)
  77. (?i "As INFO file" org-texinfo-export-to-info)))
  78. :options-alist
  79. '((:texinfo-filename "TEXINFO_FILENAME" nil nil t)
  80. (:texinfo-class "TEXINFO_CLASS" nil org-texinfo-default-class t)
  81. (:texinfo-header "TEXINFO_HEADER" nil nil newline)
  82. (:texinfo-post-header "TEXINFO_POST_HEADER" nil nil newline)
  83. (:subtitle "SUBTITLE" nil nil newline)
  84. (:subauthor "SUBAUTHOR" nil nil newline)
  85. (:texinfo-dircat "TEXINFO_DIR_CATEGORY" nil nil t)
  86. (:texinfo-dirtitle "TEXINFO_DIR_TITLE" nil nil t)
  87. (:texinfo-dirdesc "TEXINFO_DIR_DESC" nil nil t)
  88. (:texinfo-printed-title "TEXINFO_PRINTED_TITLE" nil nil t)
  89. ;; Other variables.
  90. (:texinfo-classes nil nil org-texinfo-classes)
  91. (:texinfo-format-headline-function nil nil org-texinfo-format-headline-function)
  92. (:texinfo-node-description-column nil nil org-texinfo-node-description-column)
  93. (:texinfo-active-timestamp-format nil nil org-texinfo-active-timestamp-format)
  94. (:texinfo-inactive-timestamp-format nil nil org-texinfo-inactive-timestamp-format)
  95. (:texinfo-diary-timestamp-format nil nil org-texinfo-diary-timestamp-format)
  96. (:texinfo-link-with-unknown-path-format nil nil org-texinfo-link-with-unknown-path-format)
  97. (:texinfo-tables-verbatim nil nil org-texinfo-tables-verbatim)
  98. (:texinfo-table-scientific-notation nil nil org-texinfo-table-scientific-notation)
  99. (:texinfo-def-table-markup nil nil org-texinfo-def-table-markup)
  100. (:texinfo-text-markup-alist nil nil org-texinfo-text-markup-alist)
  101. (:texinfo-format-drawer-function nil nil org-texinfo-format-drawer-function)
  102. (:texinfo-format-inlinetask-function nil nil org-texinfo-format-inlinetask-function)))
  103. ;;; User Configurable Variables
  104. (defgroup org-export-texinfo nil
  105. "Options for exporting Org mode files to Texinfo."
  106. :tag "Org Export Texinfo"
  107. :version "24.4"
  108. :package-version '(Org . "8.0")
  109. :group 'org-export)
  110. ;;;; Preamble
  111. (defcustom org-texinfo-coding-system nil
  112. "Default document encoding for Texinfo output.
  113. If `nil' it will default to `buffer-file-coding-system'."
  114. :group 'org-export-texinfo
  115. :type 'coding-system)
  116. (defcustom org-texinfo-default-class "info"
  117. "The default Texinfo class."
  118. :group 'org-export-texinfo
  119. :type '(string :tag "Texinfo class"))
  120. (defcustom org-texinfo-classes
  121. '(("info"
  122. "@documentencoding AUTO\n@documentlanguage AUTO"
  123. ("@chapter %s" . "@unnumbered %s")
  124. ("@section %s" . "@unnumberedsec %s")
  125. ("@subsection %s" . "@unnumberedsubsec %s")
  126. ("@subsubsection %s" . "@unnumberedsubsubsec %s")))
  127. "Alist of Texinfo classes and associated header and structure.
  128. If #+TEXINFO_CLASS is set in the buffer, use its value and the
  129. associated information. Here is the structure of each cell:
  130. \(class-name
  131. header-string
  132. \(numbered-section . unnumbered-section)
  133. ...)
  134. The header string
  135. -----------------
  136. The header string is inserted in the header of the generated
  137. document, right after \"@setfilename\" and \"@settitle\"
  138. commands.
  139. If it contains the special string
  140. \"@documentencoding AUTO\"
  141. \"AUTO\" will be replaced with an appropriate coding system. See
  142. `org-texinfo-coding-system' for more information. Likewise, if
  143. the string contains the special string
  144. \"@documentlanguage AUTO\"
  145. \"AUTO\" will be replaced with the language defined in the
  146. buffer, through #+LANGUAGE keyword, or globally, with
  147. `org-export-default-language', which see.
  148. The sectioning structure
  149. ------------------------
  150. The sectioning structure of the class is given by the elements
  151. following the header string. For each sectioning level, a number
  152. of strings is specified. A %s formatter is mandatory in each
  153. section string and will be replaced by the title of the section.
  154. Instead of a list of sectioning commands, you can also specify
  155. a function name. That function will be called with two
  156. parameters, the reduced) level of the headline, and a predicate
  157. non-nil when the headline should be numbered. It must return
  158. a format string in which the section title will be added."
  159. :group 'org-export-texinfo
  160. :version "24.4"
  161. :package-version '(Org . "8.2")
  162. :type '(repeat
  163. (list (string :tag "Texinfo class")
  164. (string :tag "Texinfo header")
  165. (repeat :tag "Levels" :inline t
  166. (choice
  167. (cons :tag "Heading"
  168. (string :tag " numbered")
  169. (string :tag "unnumbered"))
  170. (function :tag "Hook computing sectioning"))))))
  171. ;;;; Headline
  172. (defcustom org-texinfo-format-headline-function
  173. 'org-texinfo-format-headline-default-function
  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. :group 'org-export-texinfo
  183. :type 'function
  184. :version "24.5"
  185. :package-version '(Org . "8.3"))
  186. ;;;; Node listing (menu)
  187. (defcustom org-texinfo-node-description-column 32
  188. "Column at which to start the description in the node listings.
  189. If a node title is greater than this length, the description will
  190. be placed after the end of the title."
  191. :group 'org-export-texinfo
  192. :type 'integer)
  193. ;;;; Timestamps
  194. (defcustom org-texinfo-active-timestamp-format "@emph{%s}"
  195. "A printf format string to be applied to active timestamps."
  196. :group 'org-export-texinfo
  197. :type 'string)
  198. (defcustom org-texinfo-inactive-timestamp-format "@emph{%s}"
  199. "A printf format string to be applied to inactive timestamps."
  200. :group 'org-export-texinfo
  201. :type 'string)
  202. (defcustom org-texinfo-diary-timestamp-format "@emph{%s}"
  203. "A printf format string to be applied to diary timestamps."
  204. :group 'org-export-texinfo
  205. :type 'string)
  206. ;;;; Links
  207. (defcustom org-texinfo-link-with-unknown-path-format "@indicateurl{%s}"
  208. "Format string for links with unknown path type."
  209. :group 'org-export-texinfo
  210. :type 'string)
  211. ;;;; Tables
  212. (defcustom org-texinfo-tables-verbatim nil
  213. "When non-nil, tables are exported verbatim."
  214. :group 'org-export-texinfo
  215. :type 'boolean)
  216. (defcustom org-texinfo-table-scientific-notation "%s\\,(%s)"
  217. "Format string to display numbers in scientific notation.
  218. The format should have \"%s\" twice, for mantissa and exponent
  219. \(i.e. \"%s\\\\times10^{%s}\").
  220. When nil, no transformation is made."
  221. :group 'org-export-texinfo
  222. :type '(choice
  223. (string :tag "Format string")
  224. (const :tag "No formatting" nil)))
  225. (defcustom org-texinfo-def-table-markup "@samp"
  226. "Default setting for @table environments."
  227. :group 'org-export-texinfo
  228. :type 'string)
  229. ;;;; Text markup
  230. (defcustom org-texinfo-text-markup-alist '((bold . "@strong{%s}")
  231. (code . code)
  232. (italic . "@emph{%s}")
  233. (verbatim . verb)
  234. (comment . "@c %s"))
  235. "Alist of Texinfo expressions to convert text markup.
  236. The key must be a symbol among `bold', `italic' and `comment'.
  237. The value is a formatting string to wrap fontified text with.
  238. Value can also be set to the following symbols: `verb' and
  239. `code'. For the former, Org will use \"@verb\" to
  240. create a format string and select a delimiter character that
  241. isn't in the string. For the latter, Org will use \"@code\"
  242. to typeset and try to protect special characters.
  243. If no association can be found for a given markup, text will be
  244. returned as-is."
  245. :group 'org-export-texinfo
  246. :type 'alist
  247. :options '(bold code italic verbatim comment))
  248. ;;;; Drawers
  249. (defcustom org-texinfo-format-drawer-function
  250. (lambda (name contents) contents)
  251. "Function called to format a drawer in Texinfo code.
  252. The function must accept two parameters:
  253. NAME the drawer name, like \"LOGBOOK\"
  254. CONTENTS the contents of the drawer.
  255. The function should return the string to be exported.
  256. The default function simply returns the value of CONTENTS."
  257. :group 'org-export-texinfo
  258. :version "24.4"
  259. :package-version '(Org . "8.2")
  260. :type 'function)
  261. ;;;; Inlinetasks
  262. (defcustom org-texinfo-format-inlinetask-function
  263. 'org-texinfo-format-inlinetask-default-function
  264. "Function called to format an inlinetask in Texinfo code.
  265. The function must accept six parameters:
  266. TODO the todo keyword, as a string
  267. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  268. PRIORITY the inlinetask priority, as a string
  269. NAME the inlinetask name, as a string.
  270. TAGS the inlinetask tags, as a list of strings.
  271. CONTENTS the contents of the inlinetask, as a string.
  272. The function should return the string to be exported."
  273. :group 'org-export-texinfo
  274. :type 'function)
  275. ;;;; Compilation
  276. (defcustom org-texinfo-info-process '("makeinfo %f")
  277. "Commands to process a Texinfo file to an INFO file.
  278. This is list of strings, each of them will be given to the shell
  279. as a command. %f in the command will be replaced by the full
  280. file name, %b by the file base name (i.e without extension) and
  281. %o by the base directory of the file."
  282. :group 'org-export-texinfo
  283. :type '(repeat :tag "Shell command sequence"
  284. (string :tag "Shell command")))
  285. (defcustom org-texinfo-logfiles-extensions
  286. '("aux" "toc" "cp" "fn" "ky" "pg" "tp" "vr")
  287. "The list of file extensions to consider as Texinfo logfiles.
  288. The logfiles will be remove if `org-texinfo-remove-logfiles' is
  289. non-nil."
  290. :group 'org-export-texinfo
  291. :type '(repeat (string :tag "Extension")))
  292. (defcustom org-texinfo-remove-logfiles t
  293. "Non-nil means remove the logfiles produced by compiling a Texinfo file.
  294. By default, logfiles are files with these extensions: .aux, .toc,
  295. .cp, .fn, .ky, .pg and .tp. To define the set of logfiles to remove,
  296. set `org-texinfo-logfiles-extensions'."
  297. :group 'org-export-latex
  298. :type 'boolean)
  299. ;;; Constants
  300. (defconst org-texinfo-max-toc-depth 4
  301. "Maximum depth for creation of detailed menu listings.
  302. Beyond this depth, Texinfo will not recognize the nodes and will
  303. cause errors. Left as a constant in case this value ever
  304. changes.")
  305. (defconst org-texinfo-supported-coding-systems
  306. '("US-ASCII" "UTF-8" "ISO-8859-15" "ISO-8859-1" "ISO-8859-2" "koi8-r" "koi8-u")
  307. "List of coding systems supported by Texinfo, as strings.
  308. Specified coding system will be matched against these strings.
  309. If two strings share the same prefix (e.g. \"ISO-8859-1\" and
  310. \"ISO-8859-15\"), the most specific one has to be listed first.")
  311. ;;; Internal Functions
  312. (defun org-texinfo-filter-section-blank-lines (headline back-end info)
  313. "Filter controlling number of blank lines after a section."
  314. (let ((blanks (make-string 2 ?\n)))
  315. (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline)))
  316. (defun org-texinfo--normalize-headlines (tree back-end info)
  317. "Normalize headlines in TREE.
  318. BACK-END is the symbol specifying back-end used for export. INFO
  319. is a plist used as a communication channel.
  320. Make sure every headline in TREE contains a section, since those
  321. are required to install a menu. Also put exactly one blank line
  322. at the end of each section.
  323. Return new tree."
  324. (org-element-map tree 'headline
  325. (lambda (hl)
  326. (org-element-put-property hl :post-blank 1)
  327. (let ((contents (org-element-contents hl)))
  328. (when contents
  329. (let ((first (org-element-map contents '(headline section)
  330. #'identity info t)))
  331. (unless (eq (org-element-type first) 'section)
  332. (apply #'org-element-set-contents
  333. hl
  334. (cons `(section (:parent ,hl)) contents)))))))
  335. info)
  336. tree)
  337. (defun org-texinfo--find-verb-separator (s)
  338. "Return a character not used in string S.
  339. This is used to choose a separator for constructs like \\verb."
  340. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  341. (loop for c across ll
  342. when (not (string-match (regexp-quote (char-to-string c)) s))
  343. return (char-to-string c))))
  344. (defun org-texinfo--text-markup (text markup info)
  345. "Format TEXT depending on MARKUP text markup.
  346. INFO is a plist used as a communication channel. See
  347. `org-texinfo-text-markup-alist' for details."
  348. (let ((fmt (cdr (assq markup org-texinfo-text-markup-alist))))
  349. (cond
  350. ;; No format string: Return raw text.
  351. ((not fmt) text)
  352. ((eq 'verb fmt)
  353. (let ((separator (org-texinfo--find-verb-separator text)))
  354. (concat "@verb{" separator text separator "}")))
  355. ((eq 'code fmt)
  356. (let ((start 0)
  357. (rtn "")
  358. char)
  359. (while (string-match "[@{}]" text)
  360. (setq char (match-string 0 text))
  361. (if (> (match-beginning 0) 0)
  362. (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
  363. (setq text (substring text (1+ (match-beginning 0))))
  364. (setq char (concat "@" char)
  365. rtn (concat rtn char)))
  366. (setq text (concat rtn text)
  367. fmt "@code{%s}")
  368. (format fmt text)))
  369. ;; Else use format string.
  370. (t (format fmt text)))))
  371. (defun org-texinfo--get-node (headline info)
  372. "Return node entry associated to HEADLINE.
  373. INFO is a plist used as a communication channel. The function
  374. guarantees the node name is unique."
  375. (let ((cache (plist-get info :texinfo-node-cache)))
  376. (or (cdr (assq headline cache))
  377. (let ((name (org-texinfo--sanitize-node
  378. (org-export-data
  379. (org-export-get-alt-title headline info) info))))
  380. ;; Ensure NAME is unique.
  381. (while (rassoc name cache) (setq name (concat name "x")))
  382. (plist-put info :texinfo-node-cache (cons (cons headline name) cache))
  383. name))))
  384. ;;;; Menu sanitizing
  385. (defun org-texinfo--sanitize-node (title)
  386. "Bend string TITLE to node line requirements.
  387. Trim string and collapse multiple whitespace characters as they
  388. are not significant. Also remove the following characters: @
  389. { } ( ) : . ,"
  390. (replace-regexp-in-string
  391. "[:,.]" ""
  392. (replace-regexp-in-string
  393. "\\`(\\(.*)\\)" "[\\1"
  394. (org-trim (replace-regexp-in-string "[ \t]\\{2,\\}" " " title)))))
  395. ;;;; Content sanitizing
  396. (defun org-texinfo--sanitize-content (text)
  397. "Escape special characters in string TEXT.
  398. Special characters are: @ { }"
  399. (replace-regexp-in-string "\\\([@{}]\\\)" "@\\1" text))
  400. ;;; Template
  401. (defun org-texinfo-template (contents info)
  402. "Return complete document string after Texinfo conversion.
  403. CONTENTS is the transcoded contents string. INFO is a plist
  404. holding export options."
  405. (let ((title (org-export-data (plist-get info :title) info))
  406. ;; Copying data is the contents of the first headline in
  407. ;; parse tree with a non-nil copying property.
  408. (copying (org-element-map (plist-get info :parse-tree) 'headline
  409. (lambda (hl)
  410. (and (org-not-nil (org-element-property :COPYING hl))
  411. (org-element-contents hl)))
  412. info t)))
  413. (concat
  414. "\\input texinfo @c -*- texinfo -*-\n"
  415. "@c %**start of header\n"
  416. (let ((file (or (plist-get info :texinfo-filename)
  417. (let ((f (plist-get info :output-file)))
  418. (and f (concat (file-name-sans-extension f) ".info"))))))
  419. (and file (format "@setfilename %s\n" file)))
  420. (format "@settitle %s\n" title)
  421. ;; Insert class-defined header.
  422. (org-element-normalize-string
  423. (let ((header (nth 1 (assoc (plist-get info :texinfo-class)
  424. org-texinfo-classes)))
  425. (coding
  426. (catch 'coding-system
  427. (let ((case-fold-search t)
  428. (name (symbol-name (or org-texinfo-coding-system
  429. buffer-file-coding-system))))
  430. (dolist (system org-texinfo-supported-coding-systems "UTF-8")
  431. (when (org-string-match-p (regexp-quote system) name)
  432. (throw 'coding-system system))))))
  433. (language (plist-get info :language))
  434. (case-fold-search nil))
  435. ;; Auto coding system.
  436. (replace-regexp-in-string
  437. "^@documentencoding \\(AUTO\\)$"
  438. coding
  439. (replace-regexp-in-string
  440. "^@documentlanguage \\(AUTO\\)$" language header nil nil 1)
  441. nil nil 1)))
  442. ;; Additional header options set by #+TEXINFO_HEADER.
  443. (let ((texinfo-header (plist-get info :texinfo-header)))
  444. (and texinfo-header (org-element-normalize-string texinfo-header)))
  445. "@c %**end of header\n\n"
  446. ;; Additional options set by #+TEXINFO_POST_HEADER.
  447. (let ((texinfo-post-header (plist-get info :texinfo-post-header)))
  448. (and texinfo-post-header
  449. (org-element-normalize-string texinfo-post-header)))
  450. ;; Copying.
  451. (and copying
  452. (format "@copying\n%s@end copying\n\n"
  453. (org-element-normalize-string
  454. (org-export-data copying info))))
  455. ;; Info directory information. Only supply if both title and
  456. ;; category are provided.
  457. (let ((dircat (plist-get info :texinfo-dircat))
  458. (dirtitle
  459. (let ((title (plist-get info :texinfo-dirtitle)))
  460. (and title
  461. (string-match "^\\(?:\\* \\)?\\(.*?\\)\\(\\.\\)?$" title)
  462. (format "* %s." (match-string 1 title))))))
  463. (when (and dircat dirtitle)
  464. (concat "@dircategory " dircat "\n"
  465. "@direntry\n"
  466. (let ((dirdesc
  467. (let ((desc (plist-get info :texinfo-dirdesc)))
  468. (cond ((not desc) nil)
  469. ((org-string-match-p "\\.$" desc) desc)
  470. (t (concat desc "."))))))
  471. (if dirdesc (format "%-23s %s" dirtitle dirdesc) dirtitle))
  472. "\n"
  473. "@end direntry\n\n")))
  474. ;; Title
  475. "@finalout\n"
  476. "@titlepage\n"
  477. (format "@title %s\n" (or (plist-get info :texinfo-printed-title) title))
  478. (let ((subtitle (plist-get info :subtitle)))
  479. (and subtitle
  480. (org-element-normalize-string
  481. (replace-regexp-in-string "^" "@subtitle " subtitle))))
  482. (when (plist-get info :with-author)
  483. (concat
  484. ;; Primary author.
  485. (let ((author (org-string-nw-p
  486. (org-export-data (plist-get info :author) info)))
  487. (email (and (plist-get info :with-email)
  488. (org-string-nw-p
  489. (org-export-data (plist-get info :email) info)))))
  490. (cond ((and author email)
  491. (format "@author %s (@email{%s})\n" author email))
  492. (author (format "@author %s\n" author))
  493. (email (format "@author @email{%s}\n" email))))
  494. ;; Other authors.
  495. (let ((subauthor (plist-get info :subauthor)))
  496. (and subauthor
  497. (org-element-normalize-string
  498. (replace-regexp-in-string "^" "@author " subauthor))))))
  499. (and copying "@page\n@vskip 0pt plus 1filll\n@insertcopying\n")
  500. "@end titlepage\n\n"
  501. ;; Table of contents.
  502. (and (plist-get info :with-toc) "@contents\n\n")
  503. ;; Configure Top Node when not for Tex
  504. "@ifnottex\n"
  505. "@node Top\n"
  506. (format "@top %s\n" title)
  507. (and copying "@insertcopying\n")
  508. "@end ifnottex\n\n"
  509. ;; Menu.
  510. (org-texinfo-make-menu (plist-get info :parse-tree) info 'master)
  511. "\n"
  512. ;; Document's body.
  513. contents "\n"
  514. ;; Creator.
  515. (case (plist-get info :with-creator)
  516. ((nil) nil)
  517. (comment (format "@c %s\n" (plist-get info :creator)))
  518. (otherwise (concat (plist-get info :creator) "\n")))
  519. ;; Document end.
  520. "@bye")))
  521. ;;; Transcode Functions
  522. ;;;; Bold
  523. (defun org-texinfo-bold (bold contents info)
  524. "Transcode BOLD from Org to Texinfo.
  525. CONTENTS is the text with bold markup. INFO is a plist holding
  526. contextual information."
  527. (org-texinfo--text-markup contents 'bold info))
  528. ;;;; Center Block
  529. (defun org-texinfo-center-block (center-block contents info)
  530. "Transcode a CENTER-BLOCK element from Org to Texinfo.
  531. CONTENTS holds the contents of the block. INFO is a plist used
  532. as a communication channel."
  533. contents)
  534. ;;;; Clock
  535. (defun org-texinfo-clock (clock contents info)
  536. "Transcode a CLOCK element from Org to Texinfo.
  537. CONTENTS is nil. INFO is a plist holding contextual
  538. information."
  539. (concat
  540. "@noindent"
  541. (format "@strong{%s} " org-clock-string)
  542. (format (plist-get info :texinfo-inactive-timestamp-format)
  543. (concat (org-translate-time
  544. (org-element-property :raw-value
  545. (org-element-property :value clock)))
  546. (let ((time (org-element-property :duration clock)))
  547. (and time (format " (%s)" time)))))
  548. "@*"))
  549. ;;;; Code
  550. (defun org-texinfo-code (code contents info)
  551. "Transcode a CODE object from Org to Texinfo.
  552. CONTENTS is nil. INFO is a plist used as a communication
  553. channel."
  554. (org-texinfo--text-markup (org-element-property :value code) 'code info))
  555. ;;;; Drawer
  556. (defun org-texinfo-drawer (drawer contents info)
  557. "Transcode a DRAWER element from Org to Texinfo.
  558. CONTENTS holds the contents of the block. INFO is a plist
  559. holding contextual information."
  560. (let* ((name (org-element-property :drawer-name drawer))
  561. (output (funcall (plist-get info :texinfo-format-drawer-function)
  562. name contents)))
  563. output))
  564. ;;;; Dynamic Block
  565. (defun org-texinfo-dynamic-block (dynamic-block contents info)
  566. "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
  567. CONTENTS holds the contents of the block. INFO is a plist
  568. holding contextual information."
  569. contents)
  570. ;;;; Entity
  571. (defun org-texinfo-entity (entity contents info)
  572. "Transcode an ENTITY object from Org to Texinfo.
  573. CONTENTS are the definition itself. INFO is a plist holding
  574. contextual information."
  575. (let ((ent (org-element-property :latex entity)))
  576. (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
  577. ;;;; Example Block
  578. (defun org-texinfo-example-block (example-block contents info)
  579. "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
  580. CONTENTS is nil. INFO is a plist holding contextual
  581. information."
  582. (format "@verbatim\n%s@end verbatim"
  583. (org-export-format-code-default example-block info)))
  584. ;;;; Export Snippet
  585. (defun org-texinfo-export-snippet (export-snippet contents info)
  586. "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
  587. CONTENTS is nil. INFO is a plist holding contextual information."
  588. (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
  589. (org-element-property :value export-snippet)))
  590. ;;;; Fixed Width
  591. (defun org-texinfo-fixed-width (fixed-width contents info)
  592. "Transcode a FIXED-WIDTH element from Org to Texinfo.
  593. CONTENTS is nil. INFO is a plist holding contextual information."
  594. (format "@example\n%s\n@end example"
  595. (org-remove-indentation
  596. (org-texinfo--sanitize-content
  597. (org-element-property :value fixed-width)))))
  598. ;;;; Footnote Reference
  599. (defun org-texinfo-footnote-reference (footnote contents info)
  600. "Create a footnote reference for FOOTNOTE.
  601. FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
  602. plist holding contextual information."
  603. (let ((def (org-export-get-footnote-definition footnote info)))
  604. (format "@footnote{%s}"
  605. (org-trim (org-export-data def info)))))
  606. ;;;; Headline
  607. (defun org-texinfo-headline (headline contents info)
  608. "Transcode a HEADLINE element from Org to Texinfo.
  609. CONTENTS holds the contents of the headline. INFO is a plist
  610. holding contextual information."
  611. (let* ((class (plist-get info :texinfo-class))
  612. (level (org-export-get-relative-level headline info))
  613. (numberedp (org-export-numbered-headline-p headline info))
  614. (class-sectioning (assoc class (plist-get info :texinfo-classes)))
  615. ;; Find the index type, if any.
  616. (index (org-element-property :INDEX headline))
  617. ;; Create node info, to insert it before section formatting.
  618. ;; Use custom menu title if present.
  619. (node (format "@node %s\n" (org-texinfo--get-node headline info)))
  620. ;; Section formatting will set two placeholders: one for the
  621. ;; title and the other for the contents.
  622. (section-fmt
  623. (if (org-not-nil (org-element-property :APPENDIX headline))
  624. "@appendix %s\n%s"
  625. (let ((sec (if (and (symbolp (nth 2 class-sectioning))
  626. (fboundp (nth 2 class-sectioning)))
  627. (funcall (nth 2 class-sectioning) level numberedp)
  628. (nth (1+ level) class-sectioning))))
  629. (cond
  630. ;; No section available for that LEVEL.
  631. ((not sec) nil)
  632. ;; Section format directly returned by a function.
  633. ((stringp sec) sec)
  634. ;; (numbered-section . unnumbered-section)
  635. ((not (consp (cdr sec)))
  636. (concat (if (or index (not numberedp)) (cdr sec) (car sec))
  637. "\n%s"))))))
  638. (todo
  639. (and (plist-get info :with-todo-keywords)
  640. (let ((todo (org-element-property :todo-keyword headline)))
  641. (and todo (org-export-data todo info)))))
  642. (todo-type (and todo (org-element-property :todo-type headline)))
  643. (tags (and (plist-get info :with-tags)
  644. (org-export-get-tags headline info)))
  645. (priority (and (plist-get info :with-priority)
  646. (org-element-property :priority headline)))
  647. (text (org-export-data (org-element-property :title headline) info))
  648. (full-text (funcall (plist-get info :texinfo-format-headline-function)
  649. todo todo-type priority text tags))
  650. (contents (if (org-string-nw-p contents) (concat "\n" contents) "")))
  651. (cond
  652. ;; Case 1: This is a footnote section: ignore it.
  653. ((org-element-property :footnote-section-p headline) nil)
  654. ;; Case 2: This is the `copying' section: ignore it
  655. ;; This is used elsewhere.
  656. ((org-not-nil (org-element-property :COPYING headline)) nil)
  657. ;; Case 3: An index. If it matches one of the known indexes,
  658. ;; print it as such following the contents, otherwise
  659. ;; print the contents and leave the index up to the user.
  660. (index
  661. (concat node
  662. (format
  663. section-fmt
  664. full-text
  665. (concat contents
  666. (and (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
  667. (concat "\n@printindex " index))))))
  668. ;; Case 4: This is a deep sub-tree: export it as a list item.
  669. ;; Also export as items headlines for which no section
  670. ;; format has been found.
  671. ((or (not section-fmt) (org-export-low-level-p headline info))
  672. ;; Build the real contents of the sub-tree.
  673. (concat (and (org-export-first-sibling-p headline info)
  674. (format "@%s\n" (if numberedp 'enumerate 'itemize)))
  675. "@item\n" full-text "\n"
  676. contents
  677. (if (org-export-last-sibling-p headline info)
  678. (format "@end %s" (if numberedp 'enumerate 'itemize))
  679. "\n")))
  680. ;; Case 5: Standard headline. Export it as a section.
  681. (t (concat node (format section-fmt full-text contents))))))
  682. (defun org-texinfo-format-headline-default-function
  683. (todo todo-type priority text tags)
  684. "Default format function for a headline.
  685. See `org-texinfo-format-headline-function' for details."
  686. (concat (when todo (format "@strong{%s} " todo))
  687. (when priority (format "@emph{#%s} " priority))
  688. text
  689. (when tags (format " :%s:" (mapconcat 'identity tags ":")))))
  690. ;;;; Inline Src Block
  691. (defun org-texinfo-inline-src-block (inline-src-block contents info)
  692. "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
  693. CONTENTS holds the contents of the item. INFO is a plist holding
  694. contextual information."
  695. (let* ((code (org-element-property :value inline-src-block))
  696. (separator (org-texinfo--find-verb-separator code)))
  697. (concat "@verb{" separator code separator "}")))
  698. ;;;; Inlinetask
  699. (defun org-texinfo-inlinetask (inlinetask contents info)
  700. "Transcode an INLINETASK element from Org to Texinfo.
  701. CONTENTS holds the contents of the block. INFO is a plist
  702. holding contextual information."
  703. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  704. (todo (and (plist-get info :with-todo-keywords)
  705. (let ((todo (org-element-property :todo-keyword inlinetask)))
  706. (and todo (org-export-data todo info)))))
  707. (todo-type (org-element-property :todo-type inlinetask))
  708. (tags (and (plist-get info :with-tags)
  709. (org-export-get-tags inlinetask info)))
  710. (priority (and (plist-get info :with-priority)
  711. (org-element-property :priority inlinetask))))
  712. (funcall (plist-get info :texinfo-format-inlinetask-function)
  713. todo todo-type priority title tags contents)))
  714. (defun org-texinfo-format-inlinetask-default-function
  715. (todo todo-type priority title tags contents)
  716. "Default format function for a inlinetasks.
  717. See `org-texinfo-format-inlinetask-function' for details."
  718. (let ((full-title
  719. (concat (when todo (format "@strong{%s} " todo))
  720. (when priority (format "#%c " priority))
  721. title
  722. (when tags (format ":%s:" (mapconcat #'identity tags ":"))))))
  723. (format "@center %s\n\n%s\n" full-title contents)))
  724. ;;;; Italic
  725. (defun org-texinfo-italic (italic contents info)
  726. "Transcode ITALIC from Org to Texinfo.
  727. CONTENTS is the text with italic markup. INFO is a plist holding
  728. contextual information."
  729. (org-texinfo--text-markup contents 'italic info))
  730. ;;;; Item
  731. (defun org-texinfo-item (item contents info)
  732. "Transcode an ITEM element from Org to Texinfo.
  733. CONTENTS holds the contents of the item. INFO is a plist holding
  734. contextual information."
  735. (format "@item%s\n%s"
  736. (let ((tag (org-element-property :tag item)))
  737. (if tag (concat " " (org-export-data tag info)) ""))
  738. (or contents "")))
  739. ;;;; Keyword
  740. (defun org-texinfo-keyword (keyword contents info)
  741. "Transcode a KEYWORD element from Org to Texinfo.
  742. CONTENTS is nil. INFO is a plist holding contextual information."
  743. (let ((key (org-element-property :key keyword))
  744. (value (org-element-property :value keyword)))
  745. (cond
  746. ((string= key "TEXINFO") value)
  747. ((string= key "CINDEX") (format "@cindex %s" value))
  748. ((string= key "FINDEX") (format "@findex %s" value))
  749. ((string= key "KINDEX") (format "@kindex %s" value))
  750. ((string= key "PINDEX") (format "@pindex %s" value))
  751. ((string= key "TINDEX") (format "@tindex %s" value))
  752. ((string= key "VINDEX") (format "@vindex %s" value)))))
  753. ;;;; Line Break
  754. (defun org-texinfo-line-break (line-break contents info)
  755. "Transcode a LINE-BREAK object from Org to Texinfo.
  756. CONTENTS is nil. INFO is a plist holding contextual information."
  757. "@*\n")
  758. ;;;; Link
  759. (defun org-texinfo-link (link desc info)
  760. "Transcode a LINK object from Org to Texinfo.
  761. DESC is the description part of the link, or the empty string.
  762. INFO is a plist holding contextual information. See
  763. `org-export-data'."
  764. (let* ((type (org-element-property :type link))
  765. (raw-path (org-element-property :path link))
  766. ;; Ensure DESC really exists, or set it to nil.
  767. (desc (and (not (string= desc "")) desc))
  768. (path (cond
  769. ((member type '("http" "https" "ftp"))
  770. (concat type ":" raw-path))
  771. ((and (string= type "file") (file-name-absolute-p raw-path))
  772. (concat "file:" raw-path))
  773. (t raw-path)))
  774. (email (if (string= type "mailto")
  775. (let ((text (replace-regexp-in-string
  776. "@" "@@" raw-path)))
  777. (concat text (if desc (concat "," desc))))))
  778. protocol)
  779. (cond
  780. ;; Links pointing to a headline: Find destination and build
  781. ;; appropriate referencing command.
  782. ((member type '("custom-id" "id"))
  783. (let ((destination (org-export-resolve-id-link link info)))
  784. (case (org-element-type destination)
  785. ;; Id link points to an external file.
  786. (plain-text
  787. (if desc (format "@uref{file://%s,%s}" destination desc)
  788. (format "@uref{file://%s}" destination)))
  789. ;; LINK points to a headline. Use the headline as the NODE target
  790. (headline
  791. (format "@ref{%s,%s}"
  792. (org-texinfo--get-node destination info)
  793. (or desc "")))
  794. (otherwise
  795. (let ((path (org-export-solidify-link-text path)))
  796. (if (not desc) (format "@ref{%s}" path)
  797. (format "@ref{%s,,%s}" path desc)))))))
  798. ((member type '("info"))
  799. (let* ((info-path (split-string path "[:#]"))
  800. (info-manual (car info-path))
  801. (info-node (or (cadr info-path) "top"))
  802. (title (or desc "")))
  803. (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
  804. ((member type '("fuzzy"))
  805. (let ((destination (org-export-resolve-fuzzy-link link info)))
  806. (case (org-element-type destination)
  807. ;; Id link points to an external file.
  808. (plain-text
  809. (if desc (format "@uref{file://%s,%s}" destination desc)
  810. (format "@uref{file://%s}" destination)))
  811. ;; LINK points to a headline. Use the headline as the NODE target
  812. (headline
  813. (format "@ref{%s,%s}"
  814. (org-texinfo--get-node destination info)
  815. (or desc "")))
  816. (otherwise
  817. (let ((path (org-export-solidify-link-text path)))
  818. (if (not desc) (format "@ref{%s}" path)
  819. (format "@ref{%s,,%s}" path desc)))))))
  820. ;; Special case for email addresses
  821. (email
  822. (format "@email{%s}" email))
  823. ;; External link with a description part.
  824. ((and path desc) (format "@uref{%s,%s}" path desc))
  825. ;; External link without a description part.
  826. (path (format "@uref{%s}" path))
  827. ;; No path, only description. Try to do something useful.
  828. (t
  829. (format (plist-get info :texinfo-link-with-unknown-path-format) desc)))))
  830. ;;;; Menu
  831. (defun org-texinfo-make-menu (scope info &optional master)
  832. "Create the menu for inclusion in the Texinfo document.
  833. SCOPE is a headline or a full parse tree. INFO is the
  834. communication channel, as a plist.
  835. When optional argument MASTER is non-nil, generate a master menu,
  836. including detailed node listing."
  837. (let ((menu (org-texinfo--build-menu scope info)))
  838. (when (org-string-nw-p menu)
  839. (org-element-normalize-string
  840. (format
  841. "@menu\n%s@end menu"
  842. (concat menu
  843. (when master
  844. (let ((detailmenu
  845. (org-texinfo--build-menu
  846. scope info
  847. (let ((toc-depth (plist-get info :with-toc)))
  848. (if (wholenump toc-depth) toc-depth
  849. org-texinfo-max-toc-depth)))))
  850. (when (org-string-nw-p detailmenu)
  851. (concat "\n@detailmenu\n"
  852. "--- The Detailed Node Listing ---\n\n"
  853. detailmenu
  854. "@end detailmenu\n"))))))))))
  855. (defun org-texinfo--build-menu (scope info &optional level)
  856. "Build menu for entries within SCOPE.
  857. SCOPE is a headline or a full parse tree. INFO is a plist
  858. containing contextual information. When optional argument LEVEL
  859. is an integer, build the menu recursively, down to this depth."
  860. (cond
  861. ((not level)
  862. (org-texinfo--format-entries (org-texinfo--menu-entries scope info) info))
  863. ((zerop level) nil)
  864. (t
  865. (org-element-normalize-string
  866. (mapconcat
  867. (lambda (h)
  868. (let ((entries (org-texinfo--menu-entries h info)))
  869. (when entries
  870. (concat
  871. (format "%s\n\n%s\n"
  872. (org-export-data (org-export-get-alt-title h info) info)
  873. (org-texinfo--format-entries entries info))
  874. (org-texinfo--build-menu h info (1- level))))))
  875. (org-texinfo--menu-entries scope info) "")))))
  876. (defun org-texinfo--format-entries (entries info)
  877. "Format all direct menu entries in SCOPE, as a string.
  878. SCOPE is either a headline or a full Org document. INFO is
  879. a plist containing contextual information."
  880. (org-element-normalize-string
  881. (mapconcat
  882. (lambda (h)
  883. (let* ((title (org-export-data
  884. (org-export-get-alt-title h info) info))
  885. (node (org-texinfo--get-node h info))
  886. (entry (concat "* " title ":"
  887. (if (string= title node) ":"
  888. (concat " " node ". "))))
  889. (desc (org-element-property :DESCRIPTION h)))
  890. (if (not desc) entry
  891. (format (format "%%-%ds %%s" org-texinfo-node-description-column)
  892. entry desc))))
  893. entries "\n")))
  894. (defun org-texinfo--menu-entries (scope info)
  895. "List direct children in SCOPE needing a menu entry.
  896. SCOPE is a headline or a full parse tree. INFO is a plist
  897. holding contextual information."
  898. (let* ((cache (or (plist-get info :texinfo-entries-cache)
  899. (plist-get (plist-put info :texinfo-entries-cache
  900. (make-hash-table :test #'eq))
  901. :texinfo-entries-cache)))
  902. (cached-entries (gethash scope cache 'no-cache)))
  903. (if (not (eq cached-entries 'no-cache)) cached-entries
  904. (puthash scope
  905. (org-element-map (org-element-contents scope) 'headline
  906. (lambda (h)
  907. (and (not (org-not-nil (org-element-property :COPYING h)))
  908. (not (org-element-property :footnote-section-p h))
  909. (not (org-export-low-level-p h info))
  910. h))
  911. info nil 'headline)
  912. cache))))
  913. ;;;; Node Property
  914. (defun org-texinfo-node-property (node-property contents info)
  915. "Transcode a NODE-PROPERTY element from Org to Texinfo.
  916. CONTENTS is nil. INFO is a plist holding contextual
  917. information."
  918. (format "%s:%s"
  919. (org-element-property :key node-property)
  920. (let ((value (org-element-property :value node-property)))
  921. (if value (concat " " value) ""))))
  922. ;;;; Paragraph
  923. (defun org-texinfo-paragraph (paragraph contents info)
  924. "Transcode a PARAGRAPH element from Org to Texinfo.
  925. CONTENTS is the contents of the paragraph, as a string. INFO is
  926. the plist used as a communication channel."
  927. contents)
  928. ;;;; Plain List
  929. (defun org-texinfo-plain-list (plain-list contents info)
  930. "Transcode a PLAIN-LIST element from Org to Texinfo.
  931. CONTENTS is the contents of the list. INFO is a plist holding
  932. contextual information."
  933. (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
  934. (indic (or (plist-get attr :indic)
  935. (plist-get info :texinfo-def-table-markup)))
  936. (table-type (plist-get attr :table-type))
  937. (type (org-element-property :type plain-list))
  938. (list-type (cond
  939. ((eq type 'ordered) "enumerate")
  940. ((eq type 'unordered) "itemize")
  941. ((member table-type '("ftable" "vtable")) table-type)
  942. (t "table"))))
  943. (format "@%s\n%s@end %s"
  944. (if (eq type 'descriptive) (concat list-type " " indic) list-type)
  945. contents
  946. list-type)))
  947. ;;;; Plain Text
  948. (defun org-texinfo-plain-text (text info)
  949. "Transcode a TEXT string from Org to Texinfo.
  950. TEXT is the string to transcode. INFO is a plist holding
  951. contextual information."
  952. ;; First protect @, { and }.
  953. (let ((output (org-texinfo--sanitize-content text)))
  954. ;; Activate smart quotes. Be sure to provide original TEXT string
  955. ;; since OUTPUT may have been modified.
  956. (when (plist-get info :with-smart-quotes)
  957. (setq output
  958. (org-export-activate-smart-quotes output :texinfo info text)))
  959. ;; LaTeX into @LaTeX{} and TeX into @TeX{}
  960. (let ((case-fold-search nil)
  961. (start 0))
  962. (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
  963. (setq output (replace-match
  964. (format "@%s{}" (match-string 1 output)) nil t output)
  965. start (match-end 0))))
  966. ;; Convert special strings.
  967. (when (plist-get info :with-special-strings)
  968. (while (string-match (regexp-quote "...") output)
  969. (setq output (replace-match "@dots{}" nil t output))))
  970. ;; Handle break preservation if required.
  971. (when (plist-get info :preserve-breaks)
  972. (setq output (replace-regexp-in-string
  973. "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
  974. ;; Return value.
  975. output))
  976. ;;;; Planning
  977. (defun org-texinfo-planning (planning contents info)
  978. "Transcode a PLANNING element from Org to Texinfo.
  979. CONTENTS is nil. INFO is a plist holding contextual
  980. information."
  981. (concat
  982. "@noindent"
  983. (mapconcat
  984. 'identity
  985. (delq nil
  986. (list
  987. (let ((closed (org-element-property :closed planning)))
  988. (when closed
  989. (concat
  990. (format "@strong{%s} " org-closed-string)
  991. (format (plist-get info :texinfo-inactive-timestamp-format)
  992. (org-translate-time
  993. (org-element-property :raw-value closed))))))
  994. (let ((deadline (org-element-property :deadline planning)))
  995. (when deadline
  996. (concat
  997. (format "@strong{%s} " org-deadline-string)
  998. (format (plist-get info :texinfo-active-timestamp-format)
  999. (org-translate-time
  1000. (org-element-property :raw-value deadline))))))
  1001. (let ((scheduled (org-element-property :scheduled planning)))
  1002. (when scheduled
  1003. (concat
  1004. (format "@strong{%s} " org-scheduled-string)
  1005. (format (plist-get info :texinfo-active-timestamp-format)
  1006. (org-translate-time
  1007. (org-element-property :raw-value scheduled))))))))
  1008. " ")
  1009. "@*"))
  1010. ;;;; Property Drawer
  1011. (defun org-texinfo-property-drawer (property-drawer contents info)
  1012. "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
  1013. CONTENTS holds the contents of the drawer. INFO is a plist
  1014. holding contextual information."
  1015. (and (org-string-nw-p contents)
  1016. (format "@verbatim\n%s@end verbatim" contents)))
  1017. ;;;; Quote Block
  1018. (defun org-texinfo-quote-block (quote-block contents info)
  1019. "Transcode a QUOTE-BLOCK element from Org to Texinfo.
  1020. CONTENTS holds the contents of the block. INFO is a plist
  1021. holding contextual information."
  1022. (let* ((title (org-element-property :name quote-block))
  1023. (start-quote (concat "@quotation"
  1024. (if title
  1025. (format " %s" title)))))
  1026. (format "%s\n%s@end quotation" start-quote contents)))
  1027. ;;;; Radio Target
  1028. (defun org-texinfo-radio-target (radio-target text info)
  1029. "Transcode a RADIO-TARGET object from Org to Texinfo.
  1030. TEXT is the text of the target. INFO is a plist holding
  1031. contextual information."
  1032. (format "@anchor{%s}%s"
  1033. (org-export-solidify-link-text
  1034. (org-element-property :value radio-target))
  1035. text))
  1036. ;;;; Section
  1037. (defun org-texinfo-section (section contents info)
  1038. "Transcode a SECTION element from Org to Texinfo.
  1039. CONTENTS holds the contents of the section. INFO is a plist
  1040. holding contextual information."
  1041. (concat contents
  1042. (let ((parent (org-export-get-parent-headline section)))
  1043. (and parent (org-texinfo-make-menu parent info)))))
  1044. ;;;; Special Block
  1045. (defun org-texinfo-special-block (special-block contents info)
  1046. "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
  1047. CONTENTS holds the contents of the block. INFO is a plist used
  1048. as a communication channel."
  1049. (if (org-export-raw-special-block-p special-block info)
  1050. (org-remove-indentation (org-element-property :raw-value special-block))
  1051. contents))
  1052. ;;;; Src Block
  1053. (defun org-texinfo-src-block (src-block contents info)
  1054. "Transcode a SRC-BLOCK element from Org to Texinfo.
  1055. CONTENTS holds the contents of the item. INFO is a plist holding
  1056. contextual information."
  1057. (let ((lispp (org-string-match-p "lisp"
  1058. (org-element-property :language src-block)))
  1059. (code (org-texinfo--sanitize-content
  1060. (org-export-format-code-default src-block info))))
  1061. (format (if lispp "@lisp\n%s@end lisp" "@example\n%s@end example") code)))
  1062. ;;;; Statistics Cookie
  1063. (defun org-texinfo-statistics-cookie (statistics-cookie contents info)
  1064. "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
  1065. CONTENTS is nil. INFO is a plist holding contextual information."
  1066. (org-element-property :value statistics-cookie))
  1067. ;;;; Subscript
  1068. (defun org-texinfo-subscript (subscript contents info)
  1069. "Transcode a SUBSCRIPT object from Org to Texinfo.
  1070. CONTENTS is the contents of the object. INFO is a plist holding
  1071. contextual information."
  1072. (format "@math{_%s}" contents))
  1073. ;;;; Superscript
  1074. (defun org-texinfo-superscript (superscript contents info)
  1075. "Transcode a SUPERSCRIPT object from Org to Texinfo.
  1076. CONTENTS is the contents of the object. INFO is a plist holding
  1077. contextual information."
  1078. (format "@math{^%s}" contents))
  1079. ;;;; Table
  1080. (defun org-texinfo-table (table contents info)
  1081. "Transcode a TABLE element from Org to Texinfo.
  1082. CONTENTS is the contents of the table. INFO is a plist holding
  1083. contextual information."
  1084. (if (eq (org-element-property :type table) 'table.el)
  1085. (format "@verbatim\n%s@end verbatim"
  1086. (org-element-normalize-string
  1087. (org-element-property :value table)))
  1088. (let* ((col-width (org-export-read-attribute :attr_texinfo table :columns))
  1089. (columns
  1090. (if col-width (format "@columnfractions %s" col-width)
  1091. (org-texinfo-table-column-widths table info))))
  1092. (format "@multitable %s\n%s@end multitable"
  1093. columns
  1094. contents))))
  1095. (defun org-texinfo-table-column-widths (table info)
  1096. "Determine the largest table cell in each column to process alignment.
  1097. TABLE is the table element to transcode. INFO is a plist used as
  1098. a communication channel."
  1099. (let ((widths (make-vector (cdr (org-export-table-dimensions table info)) 0)))
  1100. (org-element-map table 'table-row
  1101. (lambda (row)
  1102. (let ((idx 0))
  1103. (org-element-map row 'table-cell
  1104. (lambda (cell)
  1105. ;; Length of the cell in the original buffer is only an
  1106. ;; approximation of the length of the cell in the
  1107. ;; output. It can sometimes fail (e.g. it considers
  1108. ;; "/a/" being larger than "ab").
  1109. (let ((w (- (org-element-property :contents-end cell)
  1110. (org-element-property :contents-begin cell))))
  1111. (aset widths idx (max w (aref widths idx))))
  1112. (incf idx))
  1113. info)))
  1114. info)
  1115. (format "{%s}" (mapconcat (lambda (w) (make-string w ?a)) widths "} {"))))
  1116. ;;;; Table Cell
  1117. (defun org-texinfo-table-cell (table-cell contents info)
  1118. "Transcode a TABLE-CELL element from Org to Texinfo.
  1119. CONTENTS is the cell contents. INFO is a plist used as
  1120. a communication channel."
  1121. (concat
  1122. (let ((scientific-notation
  1123. (plist-get info :texinfo-table-scientific-notation)))
  1124. (if (and contents
  1125. scientific-notation
  1126. (string-match orgtbl-exp-regexp contents))
  1127. ;; Use appropriate format string for scientific notation.
  1128. (format scientific-notation
  1129. (match-string 1 contents)
  1130. (match-string 2 contents))
  1131. contents))
  1132. (when (org-export-get-next-element table-cell info) "\n@tab ")))
  1133. ;;;; Table Row
  1134. (defun org-texinfo-table-row (table-row contents info)
  1135. "Transcode a TABLE-ROW element from Org to Texinfo.
  1136. CONTENTS is the contents of the row. INFO is a plist used as
  1137. a communication channel."
  1138. ;; Rules are ignored since table separators are deduced from
  1139. ;; borders of the current row.
  1140. (when (eq (org-element-property :type table-row) 'standard)
  1141. (let ((rowgroup-tag
  1142. (if (and (= 1 (org-export-table-row-group table-row info))
  1143. (org-export-table-has-header-p
  1144. (org-export-get-parent-table table-row) info))
  1145. "@headitem "
  1146. "@item ")))
  1147. (concat rowgroup-tag contents "\n"))))
  1148. ;;;; Target
  1149. (defun org-texinfo-target (target contents info)
  1150. "Transcode a TARGET object from Org to Texinfo.
  1151. CONTENTS is nil. INFO is a plist holding contextual
  1152. information."
  1153. (format "@anchor{%s}"
  1154. (org-export-solidify-link-text (org-element-property :value target))))
  1155. ;;;; Timestamp
  1156. (defun org-texinfo-timestamp (timestamp contents info)
  1157. "Transcode a TIMESTAMP object from Org to Texinfo.
  1158. CONTENTS is nil. INFO is a plist holding contextual
  1159. information."
  1160. (let ((value (org-texinfo-plain-text
  1161. (org-timestamp-translate timestamp) info)))
  1162. (case (org-element-property :type timestamp)
  1163. ((active active-range)
  1164. (format (plist-get info :texinfo-active-timestamp-format) value))
  1165. ((inactive inactive-range)
  1166. (format (plist-get info :texinfo-inactive-timestamp-format) value))
  1167. (t (format (plist-get info :texinfo-diary-timestamp-format) value)))))
  1168. ;;;; Verbatim
  1169. (defun org-texinfo-verbatim (verbatim contents info)
  1170. "Transcode a VERBATIM object from Org to Texinfo.
  1171. CONTENTS is nil. INFO is a plist used as a communication
  1172. channel."
  1173. (org-texinfo--text-markup
  1174. (org-element-property :value verbatim) 'verbatim info))
  1175. ;;;; Verse Block
  1176. (defun org-texinfo-verse-block (verse-block contents info)
  1177. "Transcode a VERSE-BLOCK element from Org to Texinfo.
  1178. CONTENTS is verse block contents. INFO is a plist holding
  1179. contextual information."
  1180. (format "@display\n%s@end display" contents))
  1181. ;;; Interactive functions
  1182. (defun org-texinfo-export-to-texinfo
  1183. (&optional async subtreep visible-only body-only ext-plist)
  1184. "Export current buffer to a Texinfo file.
  1185. If narrowing is active in the current buffer, only export its
  1186. narrowed part.
  1187. If a region is active, export that region.
  1188. A non-nil optional argument ASYNC means the process should happen
  1189. asynchronously. The resulting file should be accessible through
  1190. the `org-export-stack' interface.
  1191. When optional argument SUBTREEP is non-nil, export the sub-tree
  1192. at point, extracting information from the headline properties
  1193. first.
  1194. When optional argument VISIBLE-ONLY is non-nil, don't export
  1195. contents of hidden elements.
  1196. When optional argument BODY-ONLY is non-nil, only write code
  1197. between \"\\begin{document}\" and \"\\end{document}\".
  1198. EXT-PLIST, when provided, is a property list with external
  1199. parameters overriding Org default settings, but still inferior to
  1200. file-local settings.
  1201. Return output file's name."
  1202. (interactive)
  1203. (let ((outfile (org-export-output-file-name ".texi" subtreep))
  1204. (org-export-coding-system org-texinfo-coding-system))
  1205. (org-export-to-file 'texinfo outfile
  1206. async subtreep visible-only body-only ext-plist)))
  1207. (defun org-texinfo-export-to-info
  1208. (&optional async subtreep visible-only body-only ext-plist)
  1209. "Export current buffer to Texinfo then process through to INFO.
  1210. If narrowing is active in the current buffer, only export its
  1211. narrowed part.
  1212. If a region is active, export that region.
  1213. A non-nil optional argument ASYNC means the process should happen
  1214. asynchronously. The resulting file should be accessible through
  1215. the `org-export-stack' interface.
  1216. When optional argument SUBTREEP is non-nil, export the sub-tree
  1217. at point, extracting information from the headline properties
  1218. first.
  1219. When optional argument VISIBLE-ONLY is non-nil, don't export
  1220. contents of hidden elements.
  1221. When optional argument BODY-ONLY is non-nil, only write code
  1222. between \"\\begin{document}\" and \"\\end{document}\".
  1223. EXT-PLIST, when provided, is a property list with external
  1224. parameters overriding Org default settings, but still inferior to
  1225. file-local settings.
  1226. When optional argument PUB-DIR is set, use it as the publishing
  1227. directory.
  1228. Return INFO file's name."
  1229. (interactive)
  1230. (let ((outfile (org-export-output-file-name ".texi" subtreep))
  1231. (org-export-coding-system org-texinfo-coding-system))
  1232. (org-export-to-file 'texinfo outfile
  1233. async subtreep visible-only body-only ext-plist
  1234. (lambda (file) (org-texinfo-compile file)))))
  1235. ;;;###autoload
  1236. (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
  1237. "Publish an org file to Texinfo.
  1238. FILENAME is the filename of the Org file to be published. PLIST
  1239. is the property list for the given project. PUB-DIR is the
  1240. publishing directory.
  1241. Return output file name."
  1242. (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
  1243. ;;;###autoload
  1244. (defun org-texinfo-convert-region-to-texinfo ()
  1245. "Assume the current region has org-mode syntax, and convert it to Texinfo.
  1246. This can be used in any buffer. For example, you can write an
  1247. itemized list in org-mode syntax in an Texinfo buffer and use
  1248. this command to convert it."
  1249. (interactive)
  1250. (org-export-replace-region-by 'texinfo))
  1251. (defun org-texinfo-compile (file)
  1252. "Compile a texinfo file.
  1253. FILE is the name of the file being compiled. Processing is
  1254. done through the command specified in `org-texinfo-info-process'.
  1255. Return INFO file name or an error if it couldn't be produced."
  1256. (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
  1257. (full-name (file-truename file))
  1258. (out-dir (file-name-directory file))
  1259. ;; Properly set working directory for compilation.
  1260. (default-directory (if (file-name-absolute-p file)
  1261. (file-name-directory full-name)
  1262. default-directory))
  1263. errors)
  1264. (message (format "Processing Texinfo file %s..." file))
  1265. (save-window-excursion
  1266. ;; Replace %b, %f and %o with appropriate values in each command
  1267. ;; before applying it. Output is redirected to "*Org INFO
  1268. ;; Texinfo Output*" buffer.
  1269. (let ((outbuf (get-buffer-create "*Org INFO Texinfo Output*")))
  1270. (dolist (command org-texinfo-info-process)
  1271. (shell-command
  1272. (replace-regexp-in-string
  1273. "%b" (shell-quote-argument base-name)
  1274. (replace-regexp-in-string
  1275. "%f" (shell-quote-argument full-name)
  1276. (replace-regexp-in-string
  1277. "%o" (shell-quote-argument out-dir) command t t) t t) t t)
  1278. outbuf))
  1279. ;; Collect standard errors from output buffer.
  1280. (setq errors (org-texinfo-collect-errors outbuf)))
  1281. (let ((infofile (concat out-dir base-name ".info")))
  1282. ;; Check for process failure. Provide collected errors if
  1283. ;; possible.
  1284. (if (not (file-exists-p infofile))
  1285. (error (concat (format "INFO file %s wasn't produced" infofile)
  1286. (when errors (concat ": " errors))))
  1287. ;; Else remove log files, when specified, and signal end of
  1288. ;; process to user, along with any error encountered.
  1289. (when org-texinfo-remove-logfiles
  1290. (dolist (ext org-texinfo-logfiles-extensions)
  1291. (let ((file (concat out-dir base-name "." ext)))
  1292. (when (file-exists-p file) (delete-file file)))))
  1293. (message (concat "Process completed"
  1294. (if (not errors) "."
  1295. (concat " with errors: " errors)))))
  1296. ;; Return output file name.
  1297. infofile))))
  1298. (defun org-texinfo-collect-errors (buffer)
  1299. "Collect some kind of errors from \"makeinfo\" command output.
  1300. BUFFER is the buffer containing output.
  1301. Return collected error types as a string, or nil if there was
  1302. none."
  1303. (with-current-buffer buffer
  1304. (save-excursion
  1305. (goto-char (point-min))
  1306. ;; Find final "makeinfo" run.
  1307. (when t
  1308. (let ((case-fold-search t)
  1309. (errors ""))
  1310. (when (save-excursion
  1311. (re-search-forward "perhaps incorrect sectioning?" nil t))
  1312. (setq errors (concat errors " [incorrect sectioning]")))
  1313. (when (save-excursion
  1314. (re-search-forward "missing close brace" nil t))
  1315. (setq errors (concat errors " [syntax error]")))
  1316. (when (save-excursion
  1317. (re-search-forward "Unknown command" nil t))
  1318. (setq errors (concat errors " [undefined @command]")))
  1319. (when (save-excursion
  1320. (re-search-forward "No matching @end" nil t))
  1321. (setq errors (concat errors " [block incomplete]")))
  1322. (when (save-excursion
  1323. (re-search-forward "requires a sectioning" nil t))
  1324. (setq errors (concat errors " [invalid section command]")))
  1325. (when (save-excursion
  1326. (re-search-forward "\\[unexpected\]" nil t))
  1327. (setq errors (concat errors " [unexpected error]")))
  1328. (when (save-excursion
  1329. (re-search-forward "misplaced " nil t))
  1330. (setq errors (concat errors " [syntax error]")))
  1331. (and (org-string-nw-p errors) (org-trim errors)))))))
  1332. (provide 'ox-texinfo)
  1333. ;; Local variables:
  1334. ;; generated-autoload-file: "org-loaddefs.el"
  1335. ;; End:
  1336. ;;; ox-texinfo.el ends here