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