org-e-texinfo.el 62 KB

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