ox-texinfo.el 65 KB

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