ox-texinfo.el 63 KB

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