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