ox-texinfo.el 55 KB

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