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