1 ;;; org-e-texinfo.el --- Texinfo Back-End For Org Export Engine
3 ;; Copyright (C) 2012, 2013 Jonathan Leech-Pepin
4 ;; Author: Jonathan Leech-Pepin <jonathan.leechpepin at gmail dot com>
5 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; This file is not part of GNU Emacs.
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
24 ;; This library implements a Texinfo back-end for Org generic
29 ;; M-: (org-export-to-buffer 'e-texinfo "*Test e-texinfo*") RET
31 ;; in an org-mode buffer then switch to the buffer to see the Texinfo
32 ;; export. See contrib/lisp/org-export.el for more details on how
33 ;; this exporter works.
36 ;; It introduces nine new buffer keywords: "TEXINFO_CLASS",
37 ;; "TEXINFO_FILENAME", "TEXINFO_HEADER", "TEXINFO_POST_HEADER",
38 ;; "TEXINFO_DIR_CATEGORY", "TEXINFO_DIR_TITLE", "TEXINFO_DIR_DESC"
39 ;; "SUBTITLE" and "SUBAUTHOR".
42 ;; It introduces 1 new headline property keywords:
43 ;; "TEXINFO_MENU_TITLE" for optional menu titles.
45 ;; To include inline code snippets (for example for generating @kbd{}
46 ;; and @key{} commands), the following export-snippet keys are
53 ;; You can add them for export snippets via any of the below:
55 ;; (add-to-list 'org-export-snippet-translation-alist
56 ;; '("e-info" . "e-texinfo"))
57 ;; (add-to-list 'org-export-snippet-translation-alist
58 ;; '("e-texinfo" . "e-texinfo"))
59 ;; (add-to-list 'org-export-snippet-translation-alist
60 ;; '("info" . "e-texinfo"))
65 (eval-when-compile (require 'cl
))
68 (defvar orgtbl-exp-regexp
)
74 (org-export-define-backend e-texinfo
75 ((bold . org-e-texinfo-bold
)
76 (center-block . org-e-texinfo-center-block
)
77 (clock . org-e-texinfo-clock
)
78 (code . org-e-texinfo-code
)
79 (comment . org-e-texinfo-comment
)
80 (comment-block . org-e-texinfo-comment-block
)
81 (drawer . org-e-texinfo-drawer
)
82 (dynamic-block . org-e-texinfo-dynamic-block
)
83 (entity . org-e-texinfo-entity
)
84 (example-block . org-e-texinfo-example-block
)
85 (export-block . org-e-texinfo-export-block
)
86 (export-snippet . org-e-texinfo-export-snippet
)
87 (fixed-width . org-e-texinfo-fixed-width
)
88 (footnote-definition . org-e-texinfo-footnote-definition
)
89 (footnote-reference . org-e-texinfo-footnote-reference
)
90 (headline . org-e-texinfo-headline
)
91 (inline-src-block . org-e-texinfo-inline-src-block
)
92 (inlinetask . org-e-texinfo-inlinetask
)
93 (italic . org-e-texinfo-italic
)
94 (item . org-e-texinfo-item
)
95 (keyword . org-e-texinfo-keyword
)
96 (line-break . org-e-texinfo-line-break
)
97 (link . org-e-texinfo-link
)
98 (paragraph . org-e-texinfo-paragraph
)
99 (plain-list . org-e-texinfo-plain-list
)
100 (plain-text . org-e-texinfo-plain-text
)
101 (planning . org-e-texinfo-planning
)
102 (property-drawer . org-e-texinfo-property-drawer
)
103 (quote-block . org-e-texinfo-quote-block
)
104 (quote-section . org-e-texinfo-quote-section
)
105 (radio-target . org-e-texinfo-radio-target
)
106 (section . org-e-texinfo-section
)
107 (special-block . org-e-texinfo-special-block
)
108 (src-block . org-e-texinfo-src-block
)
109 (statistics-cookie . org-e-texinfo-statistics-cookie
)
110 (subscript . org-e-texinfo-subscript
)
111 (superscript . org-e-texinfo-superscript
)
112 (table . org-e-texinfo-table
)
113 (table-cell . org-e-texinfo-table-cell
)
114 (table-row . org-e-texinfo-table-row
)
115 (target . org-e-texinfo-target
)
116 (template . org-e-texinfo-template
)
117 (timestamp . org-e-texinfo-timestamp
)
118 (verbatim . org-e-texinfo-verbatim
)
119 (verse-block . org-e-texinfo-verse-block
))
120 :export-block
"TEXINFO"
122 ((:filter-headline . org-e-texinfo-filter-section-blank-lines
)
123 (:filter-section . org-e-texinfo-filter-section-blank-lines
))
125 (?i
"Export to Texinfo"
126 ((?t
"As TEXI file" org-e-texinfo-export-to-texinfo
)
127 (?i
"As INFO file" org-e-texinfo-export-to-info
)))
129 ((:texinfo-filename
"TEXINFO_FILENAME" nil org-e-texinfo-filename t
)
130 (:texinfo-class
"TEXINFO_CLASS" nil org-e-texinfo-default-class t
)
131 (:texinfo-header
"TEXINFO_HEADER" nil nil newline
)
132 (:texinfo-post-header
"TEXINFO_POST_HEADER" nil nil newline
)
133 (:subtitle
"SUBTITLE" nil nil newline
)
134 (:subauthor
"SUBAUTHOR" nil nil newline
)
135 (:texinfo-dircat
"TEXINFO_DIR_CATEGORY" nil nil t
)
136 (:texinfo-dirtitle
"TEXINFO_DIR_TITLE" nil nil t
)
137 (:texinfo-dirdesc
"TEXINFO_DIR_DESC" nil nil t
)
138 (:texinfo-menu-title
"TEXINFO_MENU_TITLE" nil nil newline
)))
142 ;;; User Configurable Variables
144 (defgroup org-export-e-texinfo nil
145 "Options for exporting Org mode files to Texinfo."
146 :tag
"Org Export Texinfo"
151 (defcustom org-e-texinfo-filename nil
152 "Default filename for texinfo output."
153 :group
'org-export-e-texinfo
154 :type
'(string :tag
"Export Filename"))
156 (defcustom org-e-texinfo-default-class
"info"
157 "The default Texinfo class."
158 :group
'org-export-e-texinfo
159 :type
'(string :tag
"Texinfo class"))
161 (defcustom org-e-texinfo-classes
163 "\\input texinfo @c -*- texinfo -*-"
164 ("@chapter %s" .
"@unnumbered %s")
165 ("@section %s" .
"@unnumberedsec %s")
166 ("@subsection %s" .
"@unnumberedsubsec %s")
167 ("@subsubsection %s" .
"@unnumberedsubsubsec %s")))
168 "Alist of Texinfo classes and associated header and structure.
169 If #+Texinfo_CLASS is set in the buffer, use its value and the
170 associated information. Here is the structure of each cell:
174 \(numbered-section . unnumbered-section\)
177 The sectioning structure
178 ------------------------
180 The sectioning structure of the class is given by the elements
181 following the header string. For each sectioning level, a number
182 of strings is specified. A %s formatter is mandatory in each
183 section string and will be replaced by the title of the section.
185 Instead of a list of sectioning commands, you can also specify
186 a function name. That function will be called with two
187 parameters, the \(reduced) level of the headline, and a predicate
188 non-nil when the headline should be numbered. It must return
189 a format string in which the section title will be added."
190 :group
'org-export-e-texinfo
192 (list (string :tag
"Texinfo class")
193 (string :tag
"Texinfo header")
194 (repeat :tag
"Levels" :inline t
197 (string :tag
" numbered")
198 (string :tag
"unnumbered"))
199 (function :tag
"Hook computing sectioning"))))))
203 (defcustom org-e-texinfo-format-headline-function nil
204 "Function to format headline text.
206 This function will be called with 5 arguments:
207 TODO the todo keyword (string or nil).
208 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
209 PRIORITY the priority of the headline (integer or nil)
210 TEXT the main headline text (string).
211 TAGS the tags as a list of strings (list of strings or nil).
213 The function result will be used in the section format string.
215 As an example, one could set the variable to the following, in
216 order to reproduce the default set-up:
218 \(defun org-e-texinfo-format-headline (todo todo-type priority text tags)
219 \"Default format function for an headline.\"
221 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo))
223 \(format \"\\\\framebox{\\\\#%c} \" priority))
226 \(format \"\\\\hfill{}\\\\textsc{%s}\"
227 \(mapconcat 'identity tags \":\"))))"
228 :group
'org-export-e-texinfo
234 ;; Footnotes are inserted directly
238 (defcustom org-e-texinfo-active-timestamp-format
"@emph{%s}"
239 "A printf format string to be applied to active timestamps."
240 :group
'org-export-e-texinfo
243 (defcustom org-e-texinfo-inactive-timestamp-format
"@emph{%s}"
244 "A printf format string to be applied to inactive timestamps."
245 :group
'org-export-e-texinfo
248 (defcustom org-e-texinfo-diary-timestamp-format
"@emph{%s}"
249 "A printf format string to be applied to diary timestamps."
250 :group
'org-export-e-texinfo
255 (defcustom org-e-texinfo-link-with-unknown-path-format
"@indicateurl{%s}"
256 "Format string for links with unknown path type."
257 :group
'org-export-e-texinfo
262 (defcustom org-e-texinfo-tables-verbatim nil
263 "When non-nil, tables are exported verbatim."
264 :group
'org-export-e-texinfo
267 (defcustom org-e-texinfo-table-scientific-notation
"%s\\,(%s)"
268 "Format string to display numbers in scientific notation.
269 The format should have \"%s\" twice, for mantissa and exponent
270 \(i.e. \"%s\\\\times10^{%s}\").
272 When nil, no transformation is made."
273 :group
'org-export-e-texinfo
275 (string :tag
"Format string")
276 (const :tag
"No formatting")))
278 (defcustom org-e-texinfo-def-table-markup
"@samp"
279 "Default setting for @table environments.")
283 (defcustom org-e-texinfo-text-markup-alist
'((bold .
"@strong{%s}")
285 (italic .
"@emph{%s}")
288 "Alist of Texinfo expressions to convert text markup.
290 The key must be a symbol among `bold', `italic' and `comment'.
291 The value is a formatting string to wrap fontified text with.
293 Value can also be set to the following symbols: `verb' and
294 `code'. For the former, Org will use \"@verb\" to
295 create a format string and select a delimiter character that
296 isn't in the string. For the latter, Org will use \"@code\"
297 to typeset and try to protect special characters.
299 If no association can be found for a given markup, text will be
301 :group
'org-export-e-texinfo
303 :options
'(bold code italic verbatim comment
))
307 (defcustom org-e-texinfo-format-drawer-function nil
308 "Function called to format a drawer in Texinfo code.
310 The function must accept two parameters:
311 NAME the drawer name, like \"LOGBOOK\"
312 CONTENTS the contents of the drawer.
314 The function should return the string to be exported.
316 For example, the variable could be set to the following function
317 in order to mimic default behaviour:
319 \(defun org-e-texinfo-format-drawer-default \(name contents\)
320 \"Format a drawer element for Texinfo export.\"
322 :group
'org-export-e-texinfo
327 (defcustom org-e-texinfo-format-inlinetask-function nil
328 "Function called to format an inlinetask in Texinfo code.
330 The function must accept six parameters:
331 TODO the todo keyword, as a string
332 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
333 PRIORITY the inlinetask priority, as a string
334 NAME the inlinetask name, as a string.
335 TAGS the inlinetask tags, as a list of strings.
336 CONTENTS the contents of the inlinetask, as a string.
338 The function should return the string to be exported.
340 For example, the variable could be set to the following function
341 in order to mimic default behaviour:
343 \(defun org-e-texinfo-format-inlinetask \(todo type priority name tags contents\)
344 \"Format an inline task element for Texinfo export.\"
348 \(format \"@strong{%s} \" todo))
349 \(when priority (format \"#%c \" priority))
353 \(mapconcat 'identity tags \":\")))))
354 \(format (concat \"@center %s\n\n\"
357 full-title contents))"
358 :group
'org-export-e-texinfo
363 ;; Src Blocks are example blocks, except for LISP
367 (defcustom org-e-texinfo-info-process
369 "Commands to process a texinfo file to an INFO file.
370 This is list of strings, each of them will be given to the shell
371 as a command. %f in the command will be replaced by the full
372 file name, %b by the file base name \(i.e without extension) and
373 %o by the base directory of the file."
374 :group
'org-export-texinfo
375 :type
'(repeat :tag
"Shell command sequence"
376 (string :tag
"Shell command")))
379 ;;; Internal Functions
381 (defun org-e-texinfo-filter-section-blank-lines (headline back-end info
)
382 "Filter controlling number of blank lines after a section."
383 (let ((blanks (make-string 2 ?
\n)))
384 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
)))
386 (defun org-e-texinfo--find-copying (info)
387 "Retrieve the headline identified by the property :copying:.
389 INFO is the plist containing the export options and tree. It is
390 used to find and extract the single desired headline. This
391 cannot be treated as a standard headline since it must be
392 inserted in a specific location."
394 (org-element-map (plist-get info
:parse-tree
) 'headline
396 (when (org-element-property :copying copy
)
397 (push copy copying
))) info t
)
398 ;; Retrieve the single entry
401 (defun org-e-texinfo--find-verb-separator (s)
402 "Return a character not used in string S.
403 This is used to choose a separator for constructs like \\verb."
404 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
405 (loop for c across ll
406 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
407 return
(char-to-string c
))))
409 (defun org-e-texinfo--make-option-string (options)
410 "Return a comma separated string of keywords and values.
411 OPTIONS is an alist where the key is the options keyword as
412 a string, and the value a list containing the keyword value, or
414 (mapconcat (lambda (pair)
416 (when (> (length (second pair
)) 0)
417 (concat "=" (second pair
)))))
421 (defun org-e-texinfo--text-markup (text markup
)
422 "Format TEXT depending on MARKUP text markup.
423 See `org-e-texinfo-text-markup-alist' for details."
424 (let ((fmt (cdr (assq markup org-e-texinfo-text-markup-alist
))))
426 ;; No format string: Return raw text.
429 (let ((separator (org-e-texinfo--find-verb-separator text
)))
430 (concat "@verb{" separator text separator
"}")))
435 (while (string-match "[@{}]" text
)
436 (setq char
(match-string 0 text
))
437 (if (> (match-beginning 0) 0)
438 (setq rtn
(concat rtn
(substring text
0 (match-beginning 0)))))
439 (setq text
(substring text
(1+ (match-beginning 0))))
440 (setq char
(concat "@" char
)
441 rtn
(concat rtn char
)))
442 (setq text
(concat rtn text
)
445 ;; Else use format string.
446 (t (format fmt text
)))))
448 ;;; Headline sanitizing
450 (defun org-e-texinfo--sanitize-headline (headline info
)
451 "Remove all formatting from the text of a headline for use in
452 node and menu listing."
454 (org-e-texinfo--sanitize-headline-contents headline info
) " "))
456 (defun org-e-texinfo--sanitize-headline-contents (headline info
)
457 "Retrieve the content of the headline.
459 Any content that can contain further formatting is checked
460 recursively, to ensure that nested content is also properly
462 (loop for contents in headline append
466 (list (replace-regexp-in-string " $" "" contents
)))
467 ;; Is exported as-is (value)
468 ((org-element-map contents
'(verbatim code
)
470 (org-element-property :value value
)) info
))
471 ;; Has content and recurse into the content
472 ((org-element-contents contents
)
473 (org-e-texinfo--sanitize-headline-contents
474 (org-element-contents contents
) info
)))))
478 (defun org-e-texinfo--sanitize-menu (title)
479 "Remove invalid characters from TITLE for use in menus and
482 Based on TEXINFO specifications, the following must be removed:
484 (replace-regexp-in-string "[@{}():,.]" "" title
))
486 ;;; Content sanitizing
488 (defun org-e-texinfo--sanitize-content (text)
489 "Ensure characters are properly escaped when used in headlines or blocks.
491 Escape characters are: @ { }"
492 (replace-regexp-in-string "\\\([@{}]\\\)" "@\\1" text
))
496 (defun org-e-texinfo--build-menu (tree level info
&optional detailed
)
497 "Create the @menu/@end menu information from TREE at headline
500 TREE contains the parse-tree to work with, either of the entire
501 document or of a specific parent headline. LEVEL indicates what
502 level of headlines to look at when generating the menu. INFO is
503 a plist containing contextual information.
505 Detailed determines whether to build a single level of menu, or
506 recurse into all children as well."
507 (let ((menu (org-e-texinfo--generate-menu-list tree level info
))
511 ;; Looping is done within the menu generation.
512 (setq text-menu
(org-e-texinfo--generate-detailed menu level info
)))
514 (setq text-menu
(org-e-texinfo--generate-menu-items menu info
))))
516 (setq output
(org-e-texinfo--format-menu text-menu
))
517 (mapconcat 'identity output
"\n"))))
519 (defun org-e-texinfo--generate-detailed (menu level info
)
520 "Generate a detailed listing of all subheadings within MENU starting at LEVEL.
522 MENU is the parse-tree to work with. LEVEL is the starting level
523 for the menu headlines and from which recursion occurs. INFO is
524 a plist containing contextual information."
526 (let ((max-depth (plist-get info
:headline-levels
)))
527 (when (> max-depth level
)
528 (loop for headline in menu append
529 (let* ((title (org-e-texinfo--menu-headlines headline info
))
530 ;; Create list of menu entries for the next level
531 (sublist (org-e-texinfo--generate-menu-list
532 headline
(1+ level
) info
))
533 ;; Generate the menu items for that level. If
534 ;; there are none omit that heading completely,
535 ;; otherwise join the title to it's related entries.
536 (submenu (if (org-e-texinfo--generate-menu-items sublist info
)
538 (org-e-texinfo--generate-menu-items sublist info
))
540 ;; Start the process over the next level down.
541 (recursion (org-e-texinfo--generate-detailed sublist
(1+ level
) info
)))
542 (setq recursion
(append submenu recursion
))
545 (defun org-e-texinfo--generate-menu-list (tree level info
)
546 "Generate the list of headlines that are within a given level
547 of the tree for further formatting.
549 TREE is the parse-tree containing the headlines. LEVEL is the
550 headline level to generate a list of. INFO is a plist holding
551 contextual information."
553 (noexport (string= "noexport"
554 (and (plist-get info
:with-tags
)
555 (org-export-get-tags tree info
)))))
559 (when (org-element-property :level head
)
560 (if (and (eq level
(org-element-property :level head
))
561 ;; Do not take note of footnotes or copying
562 ;; headlines. Also ignore :noexport: headlines
564 (not (org-element-property :copying head
))
565 (not (org-element-property :footnote-section-p head
)))
568 ;; Return the list of headlines (reverse to have in actual order)
571 (defun org-e-texinfo--generate-menu-items (items info
)
572 "Generate a list of headline information from the listing ITEMS.
574 ITEMS is a list of the headlines to be converted into entries.
575 INFO is a plist containing contextual information.
577 Returns a list containing the following information from each
578 headline: length, title, description. This is used to format the
579 menu using `org-e-texinfo--format-menu'."
580 (loop for headline in items collect
581 (let* ((menu-title (org-e-texinfo--sanitize-menu
583 (org-element-property :texinfo-menu-title headline
)
585 (title (org-e-texinfo--sanitize-menu
586 (org-e-texinfo--sanitize-headline
587 (org-element-property :title headline
) info
)))
588 (descr (org-export-data
589 (org-element-property :description headline
)
591 (menu-entry (if (string= "" menu-title
) title menu-title
))
592 (len (length menu-entry
))
593 (output (list len menu-entry descr
)))
596 (defun org-e-texinfo--menu-headlines (headline info
)
597 "Retrieve the title from HEADLINE.
599 INFO is a plist holding contextual information.
601 Return the headline as a list of (length title description) with
602 length of -1 and nil description. This is used in
603 `org-e-texinfo--format-menu' to identify headlines as opposed to
605 (let ((title (org-export-data
606 (org-element-property :title headline
) info
)))
607 (list -
1 title
'nil
)))
609 (defun org-e-texinfo--format-menu (text-menu)
610 "Format the TEXT-MENU items to be properly printed in the menu.
612 Each entry in the menu should be provided as (length title
615 Headlines in the detailed menu are given length -1 to ensure they
616 are never confused with other entries. They also have no
619 Other menu items are output as:
622 With the spacing between :: and description based on the length
623 of the longest menu entry."
625 (let* ((lengths (mapcar 'car text-menu
))
626 (max-length (apply 'max lengths
))
629 (mapcar (lambda (name)
630 (let* ((title (nth 1 name
))
632 (length (nth 0 name
)))
634 (concat "* " title
":: "
636 (- (+ 3 max-length
) length
)
640 (concat "\n" title
"\n"))))
646 (defun org-e-texinfo-template (contents info
)
647 "Return complete document string after Texinfo conversion.
648 CONTENTS is the transcoded contents string. INFO is a plist
649 holding export options."
650 (let* ((title (org-export-data (plist-get info
:title
) info
))
651 (info-filename (or (plist-get info
:texinfo-filename
)
652 (file-name-nondirectory
653 (org-export-output-file-name ".info"))))
654 (author (org-export-data (plist-get info
:author
) info
))
655 (texinfo-header (plist-get info
:texinfo-header
))
656 (texinfo-post-header (plist-get info
:texinfo-post-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
667 (dirspacing (- 29 (length dirtitle
)))
668 (menu (org-e-texinfo-make-menu info
'main
))
669 (detail-menu (org-e-texinfo-make-menu info
'detailed
)))
673 "@c %**start of header\n"
674 ;; Filename and Title
675 "@setfilename " info-filename
"\n"
676 "@settitle " title
"\n"
678 "@c Version and Contact Info\n"
679 "@set AUTHOR " author
"\n"
681 ;; Additional Header Options set by `#+TEXINFO_HEADER
687 "@c %**end of header\n"
691 ;; Additional Header Options set by #+TEXINFO_POST_HEADER
692 (if texinfo-post-header
699 ;; Only export the content of the headline, do not need the
701 (org-export-data (nth 2 copying
) info
)
705 ;; Info directory information
706 ;; Only supply if both title and category are provided
707 (if (and dircat dirtitle
)
708 (concat "@dircategory " dircat
"\n"
711 (make-string dirspacing ?\s
)
718 "@title " title
"\n\n"
720 (concat "@subtitle " subtitle
"\n"))
721 "@author " author
"\n"
723 (concat subauthor
"\n"))
725 "@c The following two commands start the copyright page.\n"
727 "@vskip 0pt plus 1filll\n"
730 "@c Output the table of contents at the beginning.\n"
733 ;; Configure Top Node when not for Tex
736 "@top " title
" Manual\n"
740 ;; Do not output menus if they are empty
748 (concat "@detailmenu\n"
749 " --- The Detailed Node Listing ---\n"
752 "@end detailmenu\n"))
760 (let ((creator-info (plist-get info
:with-creator
)))
762 ((not creator-info
) "")
763 ((eq creator-info
'comment
)
764 (format "@c %s\n" (plist-get info
:creator
)))
765 (t (concat (plist-get info
:creator
) "\n"))))
771 ;;; Transcode Functions
775 (defun org-e-texinfo-bold (bold contents info
)
776 "Transcode BOLD from Org to Texinfo.
777 CONTENTS is the text with bold markup. INFO is a plist holding
778 contextual information."
779 (org-e-texinfo--text-markup contents
'bold
))
783 (defun org-e-texinfo-center-block (center-block contents info
)
784 "Transcode a CENTER-BLOCK element from Org to Texinfo.
785 CONTENTS holds the contents of the block. INFO is a plist used
786 as a communication channel."
791 (defun org-e-texinfo-clock (clock contents info
)
792 "Transcode a CLOCK element from Org to Texinfo.
793 CONTENTS is nil. INFO is a plist holding contextual
797 (format "@strong{%s} " org-clock-string
)
798 (format org-e-texinfo-inactive-timestamp-format
799 (concat (org-translate-time
800 (org-element-property :raw-value
801 (org-element-property :value clock
)))
802 (let ((time (org-element-property :duration clock
)))
803 (and time
(format " (%s)" time
)))))
808 (defun org-e-texinfo-code (code contents info
)
809 "Transcode a CODE object from Org to Texinfo.
810 CONTENTS is nil. INFO is a plist used as a communication
812 (org-e-texinfo--text-markup (org-element-property :value code
) 'code
))
816 (defun org-e-texinfo-comment (comment contents info
)
817 "Transcode a COMMENT object from Org to Texinfo.
818 CONTENTS is the text in the comment. INFO is a plist holding
819 contextual information."
820 (org-e-texinfo--text-markup (org-element-property :value comment
) 'comment
))
824 (defun org-e-texinfo-comment-block (comment-block contents info
)
825 "Transcode a COMMENT-BLOCK object from Org to Texinfo.
826 CONTENTS is the text within the block. INFO is a plist holding
827 contextual information."
828 (format "@ignore\n%s@end ignore" (org-element-property :value comment-block
)))
832 (defun org-e-texinfo-drawer (drawer contents info
)
833 "Transcode a DRAWER element from Org to Texinfo.
834 CONTENTS holds the contents of the block. INFO is a plist
835 holding contextual information."
836 (let* ((name (org-element-property :drawer-name drawer
))
837 (output (if (functionp org-e-texinfo-format-drawer-function
)
838 (funcall org-e-texinfo-format-drawer-function
840 ;; If there's no user defined function: simply
841 ;; display contents of the drawer.
847 (defun org-e-texinfo-dynamic-block (dynamic-block contents info
)
848 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
849 CONTENTS holds the contents of the block. INFO is a plist
850 holding contextual information. See `org-export-data'."
855 (defun org-e-texinfo-entity (entity contents info
)
856 "Transcode an ENTITY object from Org to Texinfo.
857 CONTENTS are the definition itself. INFO is a plist holding
858 contextual information."
859 (let ((ent (org-element-property :latex entity
)))
860 (if (org-element-property :latex-math-p entity
) (format "@math{%s}" ent
) ent
)))
864 (defun org-e-texinfo-example-block (example-block contents info
)
865 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
866 CONTENTS is nil. INFO is a plist holding contextual
868 (format "@verbatim\n%s@end verbatim"
869 (org-export-format-code-default example-block info
)))
873 (defun org-e-texinfo-export-block (export-block contents info
)
874 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
875 CONTENTS is nil. INFO is a plist holding contextual information."
876 (when (string= (org-element-property :type export-block
) "TEXINFO")
877 (org-remove-indentation (org-element-property :value export-block
))))
881 (defun org-e-texinfo-export-snippet (export-snippet contents info
)
882 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
883 CONTENTS is nil. INFO is a plist holding contextual information."
884 (when (eq (org-export-snippet-backend export-snippet
) 'e-texinfo
)
885 (org-element-property :value export-snippet
)))
889 (defun org-e-texinfo-fixed-width (fixed-width contents info
)
890 "Transcode a FIXED-WIDTH element from Org to Texinfo.
891 CONTENTS is nil. INFO is a plist holding contextual information."
892 (format "@example\n%s\n@end example"
893 (org-remove-indentation
894 (org-e-texinfo--sanitize-content
895 (org-element-property :value fixed-width
)))))
897 ;;; Footnote Reference
900 (defun org-e-texinfo-footnote-reference (footnote contents info
)
901 "Create a footnote reference for FOOTNOTE.
903 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
904 plist holding contextual information."
905 (let ((def (org-export-get-footnote-definition footnote info
)))
906 (format "@footnote{%s}"
907 (org-trim (org-export-data def info
)))))
911 (defun org-e-texinfo-headline (headline contents info
)
912 "Transcode an HEADLINE element from Org to Texinfo.
913 CONTENTS holds the contents of the headline. INFO is a plist
914 holding contextual information."
915 (let* ((class (plist-get info
:texinfo-class
))
916 (level (org-export-get-relative-level headline info
))
917 (numberedp (org-export-numbered-headline-p headline info
))
918 (class-sectionning (assoc class org-e-texinfo-classes
))
919 ;; Find the index type, if any
920 (index (org-element-property :index headline
))
921 ;; Retrieve custom menu title (if any)
922 (menu-title (org-e-texinfo--sanitize-menu
924 (org-element-property :texinfo-menu-title headline
)
926 ;; Retrieve headline text
927 (text (org-e-texinfo--sanitize-headline
928 (org-element-property :title headline
) info
))
929 ;; Create node info, to insert it before section formatting.
930 ;; Use custom menu title if present
931 (node (format "@node %s\n"
932 (org-e-texinfo--sanitize-menu
933 (replace-regexp-in-string "%" "%%"
934 (if (not (string= "" menu-title
))
937 ;; Menus must be generated with first child, otherwise they
938 ;; will not nest properly
939 (menu (let* ((first (org-export-first-sibling-p headline info
))
940 (parent (org-export-get-parent-headline headline
))
941 (title (org-e-texinfo--sanitize-headline
942 (org-element-property :title parent
) info
))
944 (tree (plist-get info
:parse-tree
)))
947 (plist-get info
:parse-tree
) 'headline
949 (if (member title
(org-element-property :title ref
))
952 (setq listing
(org-e-texinfo--build-menu
953 (car heading
) level info
))
955 (setq listing
(replace-regexp-in-string
958 "\n@menu\n%s\n@end menu\n\n" listing
))
960 ;; Section formatting will set two placeholders: one for the
961 ;; title and the other for the contents.
963 (let ((sec (if (and (symbolp (nth 2 class-sectionning
))
964 (fboundp (nth 2 class-sectionning
)))
965 (funcall (nth 2 class-sectionning
) level numberedp
)
966 (nth (1+ level
) class-sectionning
))))
968 ;; No section available for that LEVEL.
970 ;; Section format directly returned by a function.
972 ;; (numbered-section . unnumbered-section)
973 ((not (consp (cdr sec
)))
974 ;; If an index, always unnumbered
976 (concat menu node
(cdr sec
) "\n%s")
977 ;; Otherwise number as needed.
980 (if numberedp
#'car
#'cdr
) sec
) "\n%s"))))))
982 (and (plist-get info
:with-todo-keywords
)
983 (let ((todo (org-element-property :todo-keyword headline
)))
984 (and todo
(org-export-data todo info
)))))
985 (todo-type (and todo
(org-element-property :todo-type headline
)))
986 (tags (and (plist-get info
:with-tags
)
987 (org-export-get-tags headline info
)))
988 (priority (and (plist-get info
:with-priority
)
989 (org-element-property :priority headline
)))
990 ;; Create the headline text along with a no-tag version. The
991 ;; latter is required to remove tags from table of contents.
992 (full-text (org-e-texinfo--sanitize-content
993 (if (functionp org-e-texinfo-format-headline-function
)
994 ;; User-defined formatting function.
995 (funcall org-e-texinfo-format-headline-function
996 todo todo-type priority text tags
)
997 ;; Default formatting.
1000 (format "@strong{%s} " todo
))
1001 (when priority
(format "@emph{#%s} " priority
))
1005 (mapconcat 'identity tags
":")))))))
1007 (org-e-texinfo--sanitize-content
1008 (if (functionp org-e-texinfo-format-headline-function
)
1009 ;; User-defined formatting function.
1010 (funcall org-e-texinfo-format-headline-function
1011 todo todo-type priority text nil
)
1012 ;; Default formatting.
1014 (when todo
(format "@strong{%s} " todo
))
1015 (when priority
(format "@emph{#%c} " priority
))
1018 (make-string (org-element-property :pre-blank headline
) 10)))
1020 ;; Case 1: This is a footnote section: ignore it.
1021 ((org-element-property :footnote-section-p headline
) nil
)
1022 ;; Case 2: This is the `copying' section: ignore it
1023 ;; This is used elsewhere.
1024 ((org-element-property :copying headline
) nil
)
1025 ;; Case 3: An index. If it matches one of the known indexes,
1026 ;; print it as such following the contents, otherwise
1027 ;; print the contents and leave the index up to the user.
1030 section-fmt full-text
1031 (concat pre-blanks contents
"\n"
1032 (if (member index
'("cp" "fn" "ky" "pg" "tp" "vr"))
1033 (concat "@printindex " index
)))))
1034 ;; Case 4: This is a deep sub-tree: export it as a list item.
1035 ;; Also export as items headlines for which no section
1036 ;; format has been found.
1037 ((or (not section-fmt
) (org-export-low-level-p headline info
))
1038 ;; Build the real contents of the sub-tree.
1039 (let ((low-level-body
1041 ;; If the headline is the first sibling, start a list.
1042 (when (org-export-first-sibling-p headline info
)
1043 (format "@%s\n" (if numberedp
'enumerate
'itemize
)))
1045 "@item\n" full-text
"\n" pre-blanks contents
)))
1046 ;; If headline is not the last sibling simply return
1047 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1049 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1050 (replace-regexp-in-string
1052 (format "\n@end %s" (if numberedp
'enumerate
'itemize
))
1054 ;; Case 5: Standard headline. Export it as a section.
1057 ((not (and tags
(eq (plist-get info
:with-tags
) 'not-in-toc
)))
1058 ;; Regular section. Use specified format string.
1059 (format (replace-regexp-in-string "%]" "%%]" section-fmt
) full-text
1060 (concat pre-blanks contents
)))
1061 ((string-match "\\`@\\(.*?\\){" section-fmt
)
1062 ;; If tags should be removed from table of contents, insert
1063 ;; title without tags as an alternative heading in sectioning
1065 (format (replace-match (concat (match-string 1 section-fmt
) "[%s]")
1066 nil nil section-fmt
1)
1067 ;; Replace square brackets with parenthesis since
1068 ;; square brackets are not supported in optional
1070 (replace-regexp-in-string
1072 (replace-regexp-in-string
1076 (concat pre-blanks contents
)))
1078 ;; Impossible to add an alternative heading. Fallback to
1079 ;; regular sectioning format string.
1080 (format (replace-regexp-in-string "%]" "%%]" section-fmt
) full-text
1081 (concat pre-blanks contents
))))))))
1083 ;;; Inline Src Block
1085 (defun org-e-texinfo-inline-src-block (inline-src-block contents info
)
1086 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
1087 CONTENTS holds the contents of the item. INFO is a plist holding
1088 contextual information."
1089 (let* ((code (org-element-property :value inline-src-block
))
1090 (separator (org-e-texinfo--find-verb-separator code
)))
1091 (concat "@verb{" separator code separator
"}")))
1095 (defun org-e-texinfo-inlinetask (inlinetask contents info
)
1096 "Transcode an INLINETASK element from Org to Texinfo.
1097 CONTENTS holds the contents of the block. INFO is a plist
1098 holding contextual information."
1099 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
1100 (todo (and (plist-get info
:with-todo-keywords
)
1101 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1102 (and todo
(org-export-data todo info
)))))
1103 (todo-type (org-element-property :todo-type inlinetask
))
1104 (tags (and (plist-get info
:with-tags
)
1105 (org-export-get-tags inlinetask info
)))
1106 (priority (and (plist-get info
:with-priority
)
1107 (org-element-property :priority inlinetask
))))
1108 ;; If `org-e-texinfo-format-inlinetask-function' is provided, call it
1109 ;; with appropriate arguments.
1110 (if (functionp org-e-texinfo-format-inlinetask-function
)
1111 (funcall org-e-texinfo-format-inlinetask-function
1112 todo todo-type priority title tags contents
)
1113 ;; Otherwise, use a default template.
1116 (when todo
(format "@strong{%s} " todo
))
1117 (when priority
(format "#%c " priority
))
1119 (when tags
(format ":%s:"
1120 (mapconcat 'identity tags
":"))))))
1121 (format (concat "@center %s\n\n"
1124 full-title contents
)))))
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
))
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")))
1147 (defun org-e-texinfo-keyword (keyword contents info
)
1148 "Transcode a KEYWORD element from Org to Texinfo.
1149 CONTENTS is nil. INFO is a plist holding contextual information."
1150 (let ((key (org-element-property :key keyword
))
1151 (value (org-element-property :value keyword
)))
1153 ((string= key
"TEXINFO") value
)
1154 ((string= key
"CINDEX") (format "@cindex %s" value
))
1155 ((string= key
"FINDEX") (format "@findex %s" value
))
1156 ((string= key
"KINDEX") (format "@kindex %s" value
))
1157 ((string= key
"PINDEX") (format "@pindex %s" value
))
1158 ((string= key
"TINDEX") (format "@tindex %s" value
))
1159 ((string= key
"VINDEX") (format "@vindex %s" value
)))))
1163 (defun org-e-texinfo-line-break (line-break contents info
)
1164 "Transcode a LINE-BREAK object from Org to Texinfo.
1165 CONTENTS is nil. INFO is a plist holding contextual information."
1170 (defun org-e-texinfo-link (link desc info
)
1171 "Transcode a LINK object from Org to Texinfo.
1173 DESC is the description part of the link, or the empty string.
1174 INFO is a plist holding contextual information. See
1176 (let* ((type (org-element-property :type link
))
1177 (raw-path (org-element-property :path link
))
1178 ;; Ensure DESC really exists, or set it to nil.
1179 (desc (and (not (string= desc
"")) desc
))
1181 ((member type
'("http" "https" "ftp"))
1182 (concat type
":" raw-path
))
1183 ((string= type
"file")
1184 (if (file-name-absolute-p raw-path
)
1185 (concat "file://" (expand-file-name raw-path
))
1186 (concat "file://" raw-path
)))
1188 (email (if (string= type
"mailto")
1189 (let ((text (replace-regexp-in-string
1190 "@" "@@" raw-path
)))
1191 (concat text
(if desc
(concat "," desc
))))))
1194 ;; Links pointing to an headline: Find destination and build
1195 ;; appropriate referencing command.
1196 ((member type
'("custom-id" "id"))
1197 (let ((destination (org-export-resolve-id-link link info
)))
1198 (case (org-element-type destination
)
1199 ;; Id link points to an external file.
1201 (if desc
(format "@uref{file://%s,%s}" destination desc
)
1202 (format "@uref{file://%s}" destination
)))
1203 ;; LINK points to an headline. Use the headline as the NODE target
1205 (format "@ref{%s,%s}"
1208 (org-element-property :texinfo-menu-title destination
)
1209 (org-element-property :title destination
)) info
)
1212 (let ((path (org-export-solidify-link-text path
)))
1213 (if (not desc
) (format "@ref{%s}" path
)
1214 (format "@ref{%s,,%s}" path desc
)))))))
1215 ((member type
'("info"))
1216 (let* ((info-path (split-string path
":"))
1217 (info-manual (car info-path
))
1218 (info-node (or (cadr info-path
) "top"))
1219 (title (or desc
"")))
1220 (format "@ref{%s,%s,,%s,}" info-node title info-manual
)))
1221 ((member type
'("fuzzy"))
1222 (let ((destination (org-export-resolve-fuzzy-link link info
)))
1223 (case (org-element-type destination
)
1224 ;; Id link points to an external file.
1226 (if desc
(format "@uref{file://%s,%s}" destination desc
)
1227 (format "@uref{file://%s}" destination
)))
1228 ;; LINK points to an headline. Use the headline as the NODE target
1230 (format "@ref{%s,%s}"
1233 (org-element-property :texinfo-menu-title destination
)
1234 (org-element-property :title destination
)) info
)
1237 (let ((path (org-export-solidify-link-text path
)))
1238 (if (not desc
) (format "@ref{%s}" path
)
1239 (format "@ref{%s,,%s}" path desc
)))))))
1240 ;; Special case for email addresses
1242 (format "@email{%s}" email
))
1243 ;; External link with a description part.
1244 ((and path desc
) (format "@uref{%s,%s}" path desc
))
1245 ;; External link without a description part.
1246 (path (format "@uref{%s}" path
))
1247 ;; No path, only description. Try to do something useful.
1248 (t (format org-e-texinfo-link-with-unknown-path-format desc
)))))
1253 (defun org-e-texinfo-make-menu (info level
)
1254 "Create the menu for inclusion in the texifo document.
1256 INFO is the parsed buffer that contains the headlines. LEVEL
1257 determines whether to make the main menu, or the detailed menu.
1259 This is only used for generating the primary menu. In-Node menus
1260 are generated directly."
1261 (let* ((parse (plist-get info
:parse-tree
))
1262 ;; Top determines level to build menu from, it finds the
1263 ;; level of the first headline in the export.
1264 (top (org-element-map
1267 (org-element-property :level headline
)) info t
)))
1269 ;; Generate the main menu
1271 (org-e-texinfo--build-menu parse top info
))
1272 ;; Generate the detailed (recursive) menu
1273 ((eq level
'detailed
)
1274 ;; Requires recursion
1275 ;;(org-e-texinfo--build-detailed-menu parse top info)
1276 (org-e-texinfo--build-menu parse top info
'detailed
))
1277 ;; Otherwise do nothing
1282 (defun org-e-texinfo-paragraph (paragraph contents info
)
1283 "Transcode a PARAGRAPH element from Org to Texinfo.
1284 CONTENTS is the contents of the paragraph, as a string. INFO is
1285 the plist used as a communication channel."
1290 (defun org-e-texinfo-plain-list (plain-list contents info
)
1291 "Transcode a PLAIN-LIST element from Org to Texinfo.
1292 CONTENTS is the contents of the list. INFO is a plist holding
1293 contextual information."
1294 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list
))
1295 (indic (or (plist-get attr
:indic
)
1296 org-e-texinfo-def-table-markup
))
1297 (type (org-element-property :type plain-list
))
1298 (table-type (or (plist-get attr
:table-type
)
1300 ;; Ensure valid texinfo table type.
1301 (table-type (if (memq table-type
'("table" "ftable" "vtable"))
1305 ((eq type
'ordered
) "enumerate")
1306 ((eq type
'unordered
) "itemize")
1307 ((eq type
'descriptive
) table-type
))))
1308 (format "@%s%s\n@end %s"
1309 (if (eq type
'descriptive
)
1310 (concat list-type
" " indic
)
1317 (defun org-e-texinfo-plain-text (text info
)
1318 "Transcode a TEXT string from Org to Texinfo.
1319 TEXT is the string to transcode. INFO is a plist holding
1320 contextual information."
1321 ;; First protect @, { and }.
1322 (let ((output (org-e-texinfo--sanitize-content text
)))
1323 ;; Activate smart quotes. Be sure to provide original TEXT string
1324 ;; since OUTPUT may have been modified.
1325 (when (plist-get info
:with-smart-quotes
)
1327 (org-export-activate-smart-quotes output
:texinfo info text
)))
1328 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1329 (let ((case-fold-search nil
)
1331 (while (string-match "\\(\\(?:La\\)?TeX\\)" output start
)
1332 (setq output
(replace-match
1333 (format "@%s{}" (match-string 1 output
)) nil t output
)
1334 start
(match-end 0))))
1335 ;; Convert special strings.
1336 (when (plist-get info
:with-special-strings
)
1337 (while (string-match (regexp-quote "...") output
)
1338 (setq output
(replace-match "@dots{}" nil t output
))))
1339 ;; Handle break preservation if required.
1340 (when (plist-get info
:preserve-breaks
)
1341 (setq output
(replace-regexp-in-string
1342 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output
)))
1348 (defun org-e-texinfo-planning (planning contents info
)
1349 "Transcode a PLANNING element from Org to Texinfo.
1350 CONTENTS is nil. INFO is a plist holding contextual
1358 (let ((closed (org-element-property :closed planning
)))
1361 (format "@strong{%s} " org-closed-string
)
1362 (format org-e-texinfo-inactive-timestamp-format
1364 (org-element-property :raw-value closed
))))))
1365 (let ((deadline (org-element-property :deadline planning
)))
1368 (format "@strong{%s} " org-deadline-string
)
1369 (format org-e-texinfo-active-timestamp-format
1371 (org-element-property :raw-value deadline
))))))
1372 (let ((scheduled (org-element-property :scheduled planning
)))
1375 (format "@strong{%s} " org-scheduled-string
)
1376 (format org-e-texinfo-active-timestamp-format
1378 (org-element-property :raw-value scheduled
))))))))
1384 (defun org-e-texinfo-property-drawer (property-drawer contents info
)
1385 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1386 CONTENTS is nil. INFO is a plist holding contextual
1388 ;; The property drawer isn't exported but we want separating blank
1389 ;; lines nonetheless.
1394 (defun org-e-texinfo-quote-block (quote-block contents info
)
1395 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1396 CONTENTS holds the contents of the block. INFO is a plist
1397 holding contextual information."
1398 (let* ((title (org-element-property :name quote-block
))
1399 (start-quote (concat "@quotation"
1401 (format " %s" title
)))))
1402 (format "%s\n%s@end quotation" start-quote contents
)))
1406 (defun org-e-texinfo-quote-section (quote-section contents info
)
1407 "Transcode a QUOTE-SECTION element from Org to Texinfo.
1408 CONTENTS is nil. INFO is a plist holding contextual information."
1409 (let ((value (org-remove-indentation
1410 (org-element-property :value quote-section
))))
1411 (when value
(format "@verbatim\n%s@end verbatim" value
))))
1415 (defun org-e-texinfo-radio-target (radio-target text info
)
1416 "Transcode a RADIO-TARGET object from Org to Texinfo.
1417 TEXT is the text of the target. INFO is a plist holding
1418 contextual information."
1419 (format "@anchor{%s}%s"
1420 (org-export-solidify-link-text
1421 (org-element-property :value radio-target
))
1426 (defun org-e-texinfo-section (section contents info
)
1427 "Transcode a SECTION element from Org to Texinfo.
1428 CONTENTS holds the contents of the section. INFO is a plist
1429 holding contextual information."
1434 (defun org-e-texinfo-special-block (special-block contents info
)
1435 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1436 CONTENTS holds the contents of the block. INFO is a plist used
1437 as a communication channel."
1442 (defun org-e-texinfo-src-block (src-block contents info
)
1443 "Transcode a SRC-BLOCK element from Org to Texinfo.
1444 CONTENTS holds the contents of the item. INFO is a plist holding
1445 contextual information."
1446 (let* ((lang (org-element-property :language src-block
))
1447 (lisp-p (string-match-p "lisp" lang
)))
1449 ;; Case 1. Lisp Block
1451 (format "@lisp\n%s\n@end lisp"
1452 (org-export-format-code-default src-block info
)))
1453 ;; Case 2. Other blocks
1455 (format "@example\n%s\n@end example"
1456 (org-export-format-code-default src-block info
))))))
1458 ;;; Statistics Cookie
1460 (defun org-e-texinfo-statistics-cookie (statistics-cookie contents info
)
1461 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1462 CONTENTS is nil. INFO is a plist holding contextual information."
1463 (org-element-property :value statistics-cookie
))
1467 (defun org-e-texinfo-subscript (subscript contents info
)
1468 "Transcode a SUBSCRIPT object from Org to Texinfo.
1469 CONTENTS is the contents of the object. INFO is a plist holding
1470 contextual information."
1471 (format "@math{_%s}" contents
))
1475 (defun org-e-texinfo-superscript (superscript contents info
)
1476 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1477 CONTENTS is the contents of the object. INFO is a plist holding
1478 contextual information."
1479 (format "@math{^%s}" contents
))
1483 ;; `org-e-texinfo-table' is the entry point for table transcoding. It
1484 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1485 ;; delegates the job to either `org-e-texinfo-table--table.el-table' or
1486 ;; `org-e-texinfo-table--org-table' functions, depending of the type of
1489 ;; `org-e-texinfo-table--align-string' is a subroutine used to build
1490 ;; alignment string for Org tables.
1492 (defun org-e-texinfo-table (table contents info
)
1493 "Transcode a TABLE element from Org to Texinfo.
1494 CONTENTS is the contents of the table. INFO is a plist holding
1495 contextual information."
1497 ;; Case 1: verbatim table.
1498 ((or org-e-texinfo-tables-verbatim
1499 (let ((attr (mapconcat 'identity
1500 (org-element-property :attr_latex table
)
1502 (and attr
(string-match "\\<verbatim\\>" attr
))))
1503 (format "@verbatim \n%s\n@end verbatim"
1504 ;; Re-create table, without affiliated keywords.
1506 (org-element-interpret-data
1507 `(table nil
,@(org-element-contents table
))))))
1508 ;; Case 2: table.el table. Convert it using appropriate tools.
1509 ((eq (org-element-property :type table
) 'table.el
)
1510 (org-e-texinfo-table--table.el-table table contents info
))
1511 ;; Case 3: Standard table.
1512 (t (org-e-texinfo-table--org-table table contents info
))))
1514 (defun org-e-texinfo-table-column-widths (table info
)
1515 "Determine the largest table cell in each column to process alignment.
1517 TABLE is the table element to transcode. INFO is a plist used as
1518 a communication channel."
1519 (let* ((rows (org-element-map table
'table-row
'identity info
))
1520 (collected (loop for row in rows collect
1521 (org-element-map row
'table-cell
'identity info
)))
1522 (number-cells (length (car collected
)))
1524 (loop for row in collected do
1525 (push (mapcar (lambda (ref)
1526 (let* ((start (org-element-property :contents-begin ref
))
1527 (end (org-element-property :contents-end ref
))
1528 (length (- end start
)))
1529 length
)) row
) cells
))
1530 (setq cells
(org-remove-if 'null cells
))
1531 (push (loop for count from
0 to
(- number-cells
1) collect
1532 (loop for item in cells collect
1533 (nth count item
))) counts
)
1534 (mapconcat (lambda (size)
1535 (make-string size ?a
)) (mapcar (lambda (ref)
1536 (apply 'max
`,@ref
)) (car counts
))
1539 (defun org-e-texinfo-table--org-table (table contents info
)
1540 "Return appropriate Texinfo code for an Org table.
1542 TABLE is the table type element to transcode. CONTENTS is its
1543 contents, as a string. INFO is a plist used as a communication
1546 This function assumes TABLE has `org' as its `:type' attribute."
1547 (let* ((attr (org-export-read-attribute :attr_texinfo table
))
1548 (col-width (plist-get attr
:columns
))
1549 (columns (if col-width
1550 (format "@columnfractions %s"
1553 (org-e-texinfo-table-column-widths
1555 ;; Prepare the final format string for the table.
1560 (format "@multitable %s\n%s@end multitable"
1564 (defun org-e-texinfo-table--table.el-table
(table contents info
)
1567 Rather than return an invalid table, nothing is returned."
1572 (defun org-e-texinfo-table-cell (table-cell contents info
)
1573 "Transcode a TABLE-CELL element from Org to Texinfo.
1574 CONTENTS is the cell contents. INFO is a plist used as
1575 a communication channel."
1576 (concat (if (and contents
1577 org-e-texinfo-table-scientific-notation
1578 (string-match orgtbl-exp-regexp contents
))
1579 ;; Use appropriate format string for scientific
1581 (format org-e-texinfo-table-scientific-notation
1582 (match-string 1 contents
)
1583 (match-string 2 contents
))
1585 (when (org-export-get-next-element table-cell info
) "\n@tab ")))
1589 (defun org-e-texinfo-table-row (table-row contents info
)
1590 "Transcode a TABLE-ROW element from Org to Texinfo.
1591 CONTENTS is the contents of the row. INFO is a plist used as
1592 a communication channel."
1593 ;; Rules are ignored since table separators are deduced from
1594 ;; borders of the current row.
1595 (when (eq (org-element-property :type table-row
) 'standard
)
1598 ;; Case 1: Belongs to second or subsequent rowgroup.
1599 ((not (= 1 (org-export-table-row-group table-row info
)))
1601 ;; Case 2: Row is from first rowgroup. Table has >=1 rowgroups.
1602 ((org-export-table-has-header-p
1603 (org-export-get-parent-table table-row
) info
)
1605 ;; Case 3: Row is from first and only row group.
1607 (when (eq (org-element-property :type table-row
) 'standard
)
1608 (concat rowgroup-tag contents
"\n")))))
1612 (defun org-e-texinfo-target (target contents info
)
1613 "Transcode a TARGET object from Org to Texinfo.
1614 CONTENTS is nil. INFO is a plist holding contextual
1616 (format "@anchor{%s}"
1617 (org-export-solidify-link-text (org-element-property :value target
))))
1621 (defun org-e-texinfo-timestamp (timestamp contents info
)
1622 "Transcode a TIMESTAMP object from Org to Texinfo.
1623 CONTENTS is nil. INFO is a plist holding contextual
1625 (let ((value (org-e-texinfo-plain-text
1626 (org-export-translate-timestamp timestamp
) info
)))
1627 (case (org-element-property :type timestamp
)
1628 ((active active-range
)
1629 (format org-e-texinfo-active-timestamp-format value
))
1630 ((inactive inactive-range
)
1631 (format org-e-texinfo-inactive-timestamp-format value
))
1632 (t (format org-e-texinfo-diary-timestamp-format value
)))))
1636 (defun org-e-texinfo-verbatim (verbatim contents info
)
1637 "Transcode a VERBATIM object from Org to Texinfo.
1638 CONTENTS is nil. INFO is a plist used as a communication
1640 (org-e-texinfo--text-markup (org-element-property :value verbatim
) 'verbatim
))
1644 (defun org-e-texinfo-verse-block (verse-block contents info
)
1645 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1646 CONTENTS is verse block contents. INFO is a plist holding
1647 contextual information."
1648 ;; In a verse environment, add a line break to each newline
1649 ;; character and change each white space at beginning of a line
1650 ;; into a space of 1 em. Also change each blank line with
1651 ;; a vertical space of 1 em.
1653 (setq contents
(replace-regexp-in-string
1654 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
1655 (replace-regexp-in-string
1656 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents
)))
1657 (while (string-match "^[ \t]+" contents
)
1658 (let ((new-str (format "\\hspace*{%dem}"
1659 (length (match-string 0 contents
)))))
1660 (setq contents
(replace-match new-str nil t contents
))))
1661 (format "\\begin{verse}\n%s\\end{verse}" contents
)))
1664 ;;; Interactive functions
1666 (defun org-e-texinfo-export-to-texinfo
1667 (&optional async subtreep visible-only body-only ext-plist
)
1668 "Export current buffer to a Texinfo file.
1670 If narrowing is active in the current buffer, only export its
1673 If a region is active, export that region.
1675 A non-nil optional argument ASYNC means the process should happen
1676 asynchronously. The resulting file should be accessible through
1677 the `org-export-stack' interface.
1679 When optional argument SUBTREEP is non-nil, export the sub-tree
1680 at point, extracting information from the headline properties
1683 When optional argument VISIBLE-ONLY is non-nil, don't export
1684 contents of hidden elements.
1686 When optional argument BODY-ONLY is non-nil, only write code
1687 between \"\\begin{document}\" and \"\\end{document}\".
1689 EXT-PLIST, when provided, is a property list with external
1690 parameters overriding Org default settings, but still inferior to
1691 file-local settings.
1693 Return output file's name."
1695 (let ((outfile (org-export-output-file-name ".texi" subtreep
)))
1697 (org-export-async-start
1698 (lambda (f) (org-export-add-to-stack f
'e-texinfo
))
1701 'e-texinfo
,outfile
,subtreep
,visible-only
,body-only
1704 'e-texinfo outfile subtreep visible-only body-only ext-plist
))))
1706 (defun org-e-texinfo-export-to-info
1707 (&optional async subtreep visible-only body-only ext-plist
)
1708 "Export current buffer to Texinfo then process through to INFO.
1710 If narrowing is active in the current buffer, only export its
1713 If a region is active, export that region.
1715 A non-nil optional argument ASYNC means the process should happen
1716 asynchronously. The resulting file should be accessible through
1717 the `org-export-stack' interface.
1719 When optional argument SUBTREEP is non-nil, export the sub-tree
1720 at point, extracting information from the headline properties
1723 When optional argument VISIBLE-ONLY is non-nil, don't export
1724 contents of hidden elements.
1726 When optional argument BODY-ONLY is non-nil, only write code
1727 between \"\\begin{document}\" and \"\\end{document}\".
1729 EXT-PLIST, when provided, is a property list with external
1730 parameters overriding Org default settings, but still inferior to
1731 file-local settings.
1733 When optional argument PUB-DIR is set, use it as the publishing
1736 Return INFO file's name."
1739 (let ((outfile (org-export-output-file-name ".texi" subtreep
)))
1740 (org-export-async-start
1741 (lambda (f) (org-export-add-to-stack f
'e-texinfo
))
1743 (org-e-texinfo-compile
1745 'e-texinfo
,outfile
,subtreep
,visible-only
,body-only
1747 (org-e-texinfo-compile
1748 (org-e-texinfo-export-to-texinfo
1749 nil subtreep visible-only body-only ext-plist
))))
1751 (defun org-e-texinfo-compile (file)
1752 "Compile a texinfo file.
1754 FILE is the name of the file being compiled. Processing is
1755 done through the command specified in `org-e-texinfo-info-process'.
1757 Return INFO file name or an error if it couldn't be produced."
1758 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file
)))
1759 (full-name (file-truename file
))
1760 (out-dir (file-name-directory file
))
1761 ;; Make sure `default-directory' is set to FILE directory,
1762 ;; not to whatever value the current buffer may have.
1763 (default-directory (file-name-directory full-name
))
1765 (message (format "Processing Texinfo file %s ..." file
))
1766 (save-window-excursion
1768 ;; A function is provided: Apply it.
1769 ((functionp org-e-texinfo-info-process
)
1770 (funcall org-e-texinfo-info-process
(shell-quote-argument file
)))
1771 ;; A list is provided: Replace %b, %f and %o with appropriate
1772 ;; values in each command before applying it. Output is
1773 ;; redirected to "*Org INFO Texinfo Output*" buffer.
1774 ((consp org-e-texinfo-info-process
)
1775 (let ((outbuf (get-buffer-create "*Org INFO Texinfo Output*")))
1779 (replace-regexp-in-string
1780 "%b" (shell-quote-argument base-name
)
1781 (replace-regexp-in-string
1782 "%f" (shell-quote-argument full-name
)
1783 (replace-regexp-in-string
1784 "%o" (shell-quote-argument out-dir
) command t t
) t t
) t t
)
1786 org-e-texinfo-info-process
)
1787 ;; Collect standard errors from output buffer.
1788 (setq errors
(org-e-texinfo-collect-errors outbuf
))))
1789 (t (error "No valid command to process to Info")))
1790 (let ((infofile (concat out-dir base-name
".info")))
1791 ;; Check for process failure. Provide collected errors if
1793 (if (not (file-exists-p infofile
))
1794 (error (concat (format "INFO file %s wasn't produced" infofile
)
1795 (when errors
(concat ": " errors
))))
1796 ;; Else remove log files, when specified, and signal end of
1797 ;; process to user, along with any error encountered.
1798 (message (concat "Process completed"
1799 (if (not errors
) "."
1800 (concat " with errors: " errors
)))))
1801 ;; Return output file name.
1804 (defun org-e-texinfo-collect-errors (buffer)
1805 "Collect some kind of errors from \"makeinfo\" command output.
1807 BUFFER is the buffer containing output.
1809 Return collected error types as a string, or nil if there was
1811 (with-current-buffer buffer
1813 (goto-char (point-min))
1814 ;; Find final "makeinfo" run.
1816 (let ((case-fold-search t
)
1818 (when (save-excursion
1819 (re-search-forward "perhaps incorrect sectioning?" nil t
))
1820 (setq errors
(concat errors
" [incorrect sectioning]")))
1821 (when (save-excursion
1822 (re-search-forward "missing close brace" nil t
))
1823 (setq errors
(concat errors
" [syntax error]")))
1824 (when (save-excursion
1825 (re-search-forward "Unknown command" nil t
))
1826 (setq errors
(concat errors
" [undefined @command]")))
1827 (when (save-excursion
1828 (re-search-forward "No matching @end" nil t
))
1829 (setq errors
(concat errors
" [block incomplete]")))
1830 (when (save-excursion
1831 (re-search-forward "requires a sectioning" nil t
))
1832 (setq errors
(concat errors
" [invalid section command]")))
1833 (when (save-excursion
1834 (re-search-forward "\\[unexpected\]" nil t
))
1835 (setq errors
(concat errors
" [unexpected error]")))
1836 (when (save-excursion
1837 (re-search-forward "misplaced " nil t
))
1838 (setq errors
(concat errors
" [syntax error]")))
1839 (and (org-string-nw-p errors
) (org-trim errors
)))))))
1841 (provide 'org-e-texinfo
)
1842 ;;; org-e-texinfo.el ends here