org-e-texinfo.el 63 KB

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